summaryrefslogtreecommitdiff
path: root/instr-daemon.c
blob: e110863f824c54246598814f5e0d9babcf6cc8ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "socket-common.h"
#include "response.h"
#include "lcd.h"
#include "flash.h"
#include "globals.h"
#include "bus.h"
#include "device-functions.h"
#include "nicutils.h"
#include "monitor.h"
#include "menus.h"
#include "gpib.h"
#include "parser.h"
#include "error_utils.h"

#include <stdlib.h>
#include <ctype.h>
#include <glib.h>
#include <unistd.h>
#include <fcntl.h>

#define STDIN_BUF_SIZE 1024


static gboolean periodic_poll (void);
static gboolean finish_boot (void);


int port=3333; //port to listen

guint signalMyCb; //signal to register which is used in cbClientInput(), step 10 from requirements
GAsyncQueue** stdinQueue=NULL;
GThread **peers; //actual connected peers


void send_message(gchar* message)
{
	if(NULL == message) {
		return;
	}
	int count=0;
	gssize size = strlen(message);
	if(!size) {
		return;
	}

	//send the final buffer to the queue
	for(count=0; count<MAX_SESSIONS; count++) {
		//allocate on the heap and deallocate in response.c, cbClientOutput()
		char *toSend = realloc(NULL, size+1);
		memcpy(toSend, message, size+1);

		//make sure we send to only valid peers
		if(peers[count] != 0) {
			g_async_queue_push(stdinQueue[count], toSend);
		}
	}
}

/** pulls an integer position from the vector which contains the
	position to use for the stdinQueue vector
	@param id - the thread id
*/
static int pullIndex(GThread* id)
{
	int i=0,ret=-1;
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	g_static_mutex_lock (&mutex);
	for(i=0; i<MAX_SESSIONS; i++)
		if(peers[i] == id) {
			peers[i] = (GThread*)0;
			ret=i;
			break;
		}
	g_static_mutex_unlock (&mutex);
	return ret;
}

/**
 * pushed in the peers vector the thread id for future reference
 * @param id
 */
static int pushIndex(GThread* id)
{
	int i=0,ret=-1;
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	g_static_mutex_lock (&mutex);
	for(i=0; i<MAX_SESSIONS; i++)
		if(peers[i] == 0) {
			peers[i] = id;
			ret=i;
			break;
		}

	g_static_mutex_unlock (&mutex);
	return ret;
}

//handler for incoming connection, closes the connection if maxCon connections are already handled
static gboolean
incomingConnection (GSocketService *service,
                    GSocketConnection      *connection,
                    GSocketListener        *listener,
                    gpointer                user_data)
{
	if(globals.Remote.terminal_connections +1 > MAX_SESSIONS) {
		g_print_debug("Connection closed. Max reached\n");
		return TRUE;
	}
	globals.Remote.terminal_connections++;
	g_print_debug("Incoming connection\n");
	return FALSE;
}


//thread connection handler
static gboolean
handler (GThreadedSocketService *service,
         GSocketConnection      *connection,
         GSocketListener        *listener,
         gpointer                user_data)
{


	GOutputStream *out;
	GInputStream *in;

	out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
	in = g_io_stream_get_input_stream (G_IO_STREAM (connection));

	g_print_debug("Handling %d connections\n", globals.Remote.terminal_connections);

	//register ourselves in the peers vector, use the index obtained in the stdinQueue
	//should not get -1
	GThread* self = g_thread_self();
	int index=pushIndex(self);

	GSource *outSource = NULL;
	GSource *inSource = g_pollable_input_stream_create_source((struct GPollableInputStream*)in, NULL);

	//get a reference to the async queue
	GAsyncQueue *queue = g_async_queue_ref(stdinQueue[index]);

	//input from client
	g_source_set_callback(inSource, cbClientInput, (GPollableOutputStream*)out, NULL);
	g_source_attach(inSource, NULL);

	//keep thread alive, every 1000 microseconds
	while(g_source_is_destroyed(inSource)==FALSE) {
		g_usleep(1000);
		gint elems;

		//verify our queue length and activate/deactivate the "out" Source for this connection
		//if we don't do this, the out Source will be scheduled frequently and will busy loop
		if((elems=g_async_queue_length(queue))>0) {
			if(outSource == NULL) {

				outSource = g_pollable_output_stream_create_source((struct GPollableOutputStream*)out, NULL);
				g_source_set_callback(outSource, cbClientOutput, queue, NULL);
				g_source_attach(outSource, NULL);
			} else {
				g_source_destroy(outSource);
				g_source_unref(outSource);
				outSource = NULL;
			}

		} else {
			if(outSource!= NULL && !g_source_is_destroyed(outSource)) {
				g_print_debug("Destroy source\n");
				g_source_destroy(outSource);
				g_source_unref(outSource);
				outSource = NULL; //added
			}
		}
		//end of activate/deactivate
	}

	//reached the end of the thread
	if (g_output_stream_close(out, NULL, NULL) == FALSE) {
		g_print_debug("out not closed\n");
	}
	if (g_input_stream_close(in, NULL, NULL) == FALSE) {
		g_print_debug("in not closed\n");
	}

	g_print_debug("Thread end\n");
	globals.Remote.terminal_connections--; //keep track of connections
	g_async_queue_unref(queue); //unreference the queue
	pullIndex(g_thread_self()); //unregister from the peers vector
	return TRUE;
}

extern void vxi_main ();

static gpointer vxithreadfunc (gpointer data){
	vxi_main ();
	return NULL;
}



int main(int argc, char **argv)
{
	GSocketService *service = NULL;
	GError *error = NULL;
	GIOChannel* stdinChannel = NULL;

	g_type_init ();
	g_thread_init (NULL);

	bus_init();

	LCD_initialize();
	LCD_write(0,0,"Starting...");

	initFlash (&globals.Flash, FALSE, 0);

	gchar *message = g_strdup_printf ("%s, S/N %s", globals.Flash.model_num, globals.Flash.serial_num);
	LCD_display_extended_message (message, FALSE, FALSE);
	g_free (message);

	int i;
	for (i=0; i<8; i++) {
		set_dac(i,globals.Flash.initial_dac_settings[i]);
	}

	fixFlash(&globals.Flash);

	// count startups
	if (globals.Flash.self_cal) {
		++globals.Flash.self_cal_startups;
		int eprom_loc = (char *) &(globals.Flash.self_cal_startups) - (char *) &(globals.Flash.flash_start);
		writeUserBlock(&globals.Flash, eprom_loc, sizeof(globals.Flash.self_cal_startups));
	}

	nicinfo info;
	strcpy(info.ip,"-");
	nicutils_infofordefaultroute(&info);
	message = g_strdup_printf ("GPIB addr %d, Eth addr %s", globals.Flash.gpib_address,info.ip);
	LCD_write(2,0,message);
	g_free (message);

	GPIB_initialize();

	IO_Setup_RS232(	globals.Flash.baud,
	                globals.Flash.hardhand,
	                FALSE);

	/* start-up delay */
	LCD_write(3,0,"Warming up, please wait... ");

	globals.Timers.startup_timer_value = sec_timer ();

	Main_Rst();

	//register stdin channel
	stdinChannel = g_io_channel_unix_new(0);
	if(stdinChannel == NULL) {
		g_printerr("No io channel\n");
		exit(-1);
	}

	int idx=0;

	//allocate a MAX_SESSIONS queue on the heap
	stdinQueue = malloc(sizeof(struct GAsyncQueue*) * MAX_SESSIONS);
	//allocate peers vector on the heap
	peers = malloc(sizeof(GThread*) * MAX_SESSIONS);
	for(idx=0; idx<MAX_SESSIONS; idx++) {
		peers[idx] = 0;
		stdinQueue[idx] = g_async_queue_new();
	}

	//create a threaded service
	service = g_threaded_socket_service_new (MAX_SESSIONS+1);
	if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service),
	                                      port,
	                                      NULL,
	                                      &error)) {
		g_printerr ("%s: %s\n", argv[0], error->message);
		free(stdinQueue);
		free(peers);
		return 1;
	}

	//init the signal signalMyCb
	initSignals(&signalMyCb);

	g_print_debug("Server listening on port %d\n", port);
	g_signal_connect (service, "run", G_CALLBACK (handler), NULL);
	g_signal_connect (service, "incoming", G_CALLBACK(incomingConnection), NULL);

	GMainLoop *loop = g_main_loop_new (NULL, FALSE);

	g_timeout_add (20, (GSourceFunc) periodic_poll, NULL);
	g_timeout_add (100, (GSourceFunc) finish_boot, NULL);

	if (globals.Flash.vxi_enabled) {
		GThread *vxithread =g_thread_create(vxithreadfunc, NULL,false, NULL);
		if(vxithread == NULL)
			printf("Couldn't create vxi thread\n");
	}

	g_main_loop_run (loop);

	bus_shutdown();
	free(stdinQueue);
	free(peers);

	return 0;

}


static gboolean finish_boot (void)
{

#define MIN_STARTUP_DELAY 2
#define MAX_STARTUP_DELAY 120

	long on_delay = (long)globals.Flash.turn_on_dly;
	if (on_delay < MIN_STARTUP_DELAY) {
		on_delay = MIN_STARTUP_DELAY;
	}
	if (on_delay > MAX_STARTUP_DELAY) {
		on_delay = MAX_STARTUP_DELAY;
	}

	long timer_count;

	if ((timer_count=sec_timer()-globals.Timers.startup_timer_value) < on_delay) {
		gchar *message = g_strdup_printf ("%ld ", on_delay - timer_count);
		LCD_write(3,27,message);
		g_free (message);
		return TRUE;	// exit and call by timeout again
	}

	LCD_write(3,27,"OK");

	I2C_Setup_Monitor();

	if (globals.Flash.self_cal &&
	                globals.Flash.self_cal_interval &&
	                (globals.Flash.self_cal_startups % globals.Flash.self_cal_interval) == 0 ) {
		int error_num;
		if (error_num=self_cal()) {
			queue_and_broadcast_sensor_alarm(error_num);
		}
	}


	globals.Sys.startup_complete = 1;

	Show_Main_Menu();

	return FALSE;	// no more calls to this function are needed
}

static gboolean periodic_poll (void)
{
	if (globals.HWDetect.beaglebone && bus_getpin (POWER_FAIL)) {

		globals.Sys.shutdown_started = TRUE;

		while (globals.Sys.flash_write_in_progress) {
			g_usleep(1000);
		}

		LCD_clear();
		//	       0123456789012345678901234567890123456789
		LCD_write(0,0,"PWR: Wait 10s. If still frozen, turn");
		LCD_write(1,0,"off 1min & retry.");

		// use plain old open to avoid any buffering etc
		int enablefd = open("/proc/sys/kernel/sysrq", O_SYNC | O_RDWR);
		int trgfd = open("/proc/sysrq-trigger", O_SYNC | O_RDWR);

		// enable sysrq
		write(enablefd, "1\n", 2);
		close(enablefd);

		// sync disks
		write(trgfd, "s\n", 2);

		// remount ro
		write(trgfd, "u\n", 2);
		close(trgfd);

		// poweroff
		system ("/usr/bin/systemctl poweroff -f");

		exit(0);
	}

	if (globals.Sys.startup_complete) {
		int i, output_on_time_so_far;

		for (i=0; i<(globals.Flash.ChanKey_output_state?globals.Flash.channels:1); ++i) {
			output_on_time_so_far = (int) (sec_timer()-globals.Timers.last_activity_at[i]);
			if (	(globals.Flash.output_timer[i]>0) &&
			                (globals.Timers.last_activity_at[i]>0) &&
			                (output_on_time_so_far > globals.Flash.output_timer[i])) {
				Set_Output_State(i,output_off);
				Show_Main_Menu();
			}
		}

		Update_Main_Menu_If_Visible();

		Menu_Check_Buttons ();

		for (i=0; i<max_channels; i++) {
			if (globals.Flash.monitor_enabled[i]) {
				I2C_Check_Monitors();
			}
		}

		Update_Main_Menu_If_Visible();

		// don't check GPIB interface if a VXI interface is locked,
		// or is currently handling a command

		if ((globals.VxiLocks.locked_network_server == NO_SERVER_LOCKED) &&
		    (globals.VxiLocks.command_in_progress == FALSE)) {

			// tell VXI servers that the 4882 subsystem is busy
			globals.VxiLocks.command_in_progress = TRUE;

			GPIB_check_for_device_clear_signal();

			if (GPIB_check_for_messages(globals.Registers.gpib_input_buffer)) {
				if (GPIB_handle_new_input(globals.Registers.gpib_input_buffer)) {
					Parser_main(globals.Registers.gpib_input_buffer, 0, GPIB_and_VXI_start_query_response, NULL);
				}
			}

			// send response if appropriate
			GPIB_finish_query_response();

			GPIB_check_for_device_clear_signal();

			// tell VXI servers that the 4882 subsystem is available again
			globals.VxiLocks.command_in_progress = FALSE;
		}

		Update_Main_Menu_If_Visible();
	}

	return TRUE;
}