#include "socket-common.h" #include "response.h" #include "lcd.h" #include "flash.h" #include "globals.h" #include "bus.h" #include "device-functions.h" #include "monitor.h" #include "menus.h" #include "gpib.h" #include "parser.h" #include "error_utils.h" #include #include #include #include #include #include #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) { 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; } static void get_root_mount () { struct fstab *sys_fstab; sys_fstab = getfsfile ("/"); if (sys_fstab == NULL) { return; } strcpy (globals.HWDetect.remount_point, sys_fstab->fs_spec); printf ("root mount = %s\n", globals.HWDetect.remount_point); return; } static void do_toggle_pulse (int chan, float pw) { int j; for (j=0;j 7e-9) && (globals.Flash.min_pw[i] < 7e-9)) { use_pw = globals.Flash.min_pw[i]; do_toggle_pulse (i, use_pw); } } } 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; idxmessage); 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) { // always enable vxi now 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; LCD_initialize(); // to fix flaky LCD startup on BBB? Show_Main_Menu(); // report error if GPIB chip not found if (!globals.HWDetect.has_gpib) { queue_error_and_display_on_LCD(GPIB_missing); } return FALSE; // no more calls to this function are needed } static gboolean periodic_poll (void) { gboolean power_fail; power_fail = bus_getpin (POWER_FAIL); if (power_fail) { // verify after a short delay (25 ms), to ignore short power glitches g_usleep (25e3); power_fail = bus_getpin (POWER_FAIL); } if (power_fail) { globals.Sys.shutdown_started = TRUE; int i; for (i=0;i0) && (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