diff options
-rw-r--r-- | CMakeLists.txt | 19 | ||||
-rw-r--r-- | instr-client.c | 39 | ||||
-rw-r--r-- | instr-daemon.c | 8 | ||||
-rw-r--r-- | response.c | 101 | ||||
-rw-r--r-- | response.h | 4 | ||||
-rw-r--r-- | socket-common.h | 2 | ||||
-rw-r--r-- | version.h | 2 |
7 files changed, 104 insertions, 71 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 56f5ee4..882db13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,23 @@ cmake_minimum_required(VERSION 2.8) - project(Instrument) -# add the binary tree to the search path for include files -include_directories("${PROJECT_BINARY_DIR}") - set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) find_package(Glib) find_package(GIO) include_directories(${GLIB_PKG_INCLUDE_DIRS}) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") -add_executable(instr-daemon instr-daemon.c signalobject.c response.c i2c.c lcd.c) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall") +add_executable(instr-daemon instr-daemon.c + signalobject.c + response.c + parser.c + error_utils.c + device-functions.c + string_utils.c + globals.c + i2c.c + lcd.c + dummy_functions.c +) add_executable(instr-client instr-client.c) target_link_libraries(instr-daemon gio-2.0 gobject-2.0 glib-2.0) diff --git a/instr-client.c b/instr-client.c index ed5c64f..fc15a20 100644 --- a/instr-client.c +++ b/instr-client.c @@ -1,11 +1,12 @@ #include <stdio.h> #include <gio/gio.h> #include <string.h> -#include "response.h" -#include "version.h" #include <stdlib.h> #include <strings.h> +#include "response.h" +#include "version.h" + static int port = 3333; #define BUFSIZE 1024 @@ -68,8 +69,18 @@ stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) //write the message gssize written=0; GPollableOutputStream* out = (GPollableOutputStream*)data; - written = g_pollable_output_stream_write_nonblocking(out, tmp, size, NULL, NULL); - if (written <=0 || written == G_IO_ERROR_WOULD_BLOCK) { + if (g_pollable_output_stream_is_writable(out) == FALSE) { + g_print("stream is not writable\n"); + } + + GError* error = NULL; + + written = g_pollable_output_stream_write_nonblocking(out, tmp, size, NULL, &error); + if(error != NULL && error->message) + g_print("Got error: %s\n", error->message); + + if (written != size || written <= 0) { + g_print("Could not write. blocking. Written: %d\n", written); return FALSE; } @@ -84,11 +95,16 @@ static gboolean cbServerInput(gpointer data, gpointer additional) memset(buffer,0,BUFSIZE); GPollableInputStream* inStream = (GPollableInputStream*)data; + GError *error = NULL; + + size=g_pollable_input_stream_read_nonblocking(inStream, buffer, BUFSIZE, NULL, &error); + if(size <=0) { + terminate("Connection to server lost", -1); + } - size=g_pollable_input_stream_read_nonblocking(inStream, buffer, BUFSIZE, NULL, NULL); - if(size <=0) terminate("Connection to server lost", -1); - - g_print("\n%s", buffer); + if(buffer[0] != ' ') { + g_print("%s", buffer); + } g_print("> "); return TRUE; } @@ -103,8 +119,7 @@ int main(int argc, char** argv) GError* error = NULL; GIOChannel* stdinChannel = NULL; client = g_socket_client_new(); - GSource *source = NULL; - + /* connect to the host */ connection = g_socket_client_connect_to_host (client, (gchar*)"localhost", @@ -117,11 +132,9 @@ int main(int argc, char** argv) g_object_unref(client); return -1; } - g_printf("Welcome to InstrumentShell v%s\n\n",FW_VERSION); g_print("> "); - //register Pollable sources out = g_io_stream_get_output_stream (G_IO_STREAM (connection)); in = g_io_stream_get_input_stream (G_IO_STREAM (connection)); @@ -142,4 +155,4 @@ int main(int argc, char** argv) g_source_attach(inSource, NULL); g_main_loop_run (g_main_loop_new (NULL, FALSE)); return 0; -} +}
\ No newline at end of file diff --git a/instr-daemon.c b/instr-daemon.c index dc941b5..ff43579 100644 --- a/instr-daemon.c +++ b/instr-daemon.c @@ -21,7 +21,6 @@ stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) { static char* buffer=NULL; static gint64 allocated=0; - GAsyncQueue* queue=NULL; //temporary buffer to read from stdin char tmp[STDIN_BUF_SIZE]; @@ -43,7 +42,6 @@ stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) //final reallocation of the buffer to accomodate the whole string buffer=realloc(buffer,allocated+1); buffer[allocated] = '\0'; - int idx=0; g_print_debug("Got the buffer: \"%s\", tid: %p\n", buffer, g_thread_self()); @@ -146,8 +144,6 @@ handler (GThreadedSocketService *service, GOutputStream *out; GInputStream *in; - gssize size; - out = g_io_stream_get_output_stream (G_IO_STREAM (connection)); in = g_io_stream_get_input_stream (G_IO_STREAM (connection)); @@ -220,10 +216,10 @@ int main(int argc, char **argv) GError *error = NULL; GIOChannel* stdinChannel = NULL; - LCD_initialize(); - g_type_init (); + LCD_initialize(); + //register stdin channel stdinChannel = g_io_channel_unix_new(0); if(stdinChannel == NULL) { @@ -2,6 +2,7 @@ #include "socket-common.h" #include "lcd.h" #include "i2c.h" +#include "parser.h" #include <string.h> #include <stdlib.h> #include <glib.h> @@ -57,14 +58,23 @@ gboolean cbClientInput(gpointer data, gpointer additional) GPollableInputStream* inStream = (GPollableInputStream*)data; GPollableOutputStream* outStream = (GPollableOutputStream*)additional; - - size=g_pollable_input_stream_read_nonblocking(inStream, buffer, 1024, NULL, NULL); - if(size == -1) { + GError* error = NULL; + + if(!g_pollable_input_stream_is_readable(inStream)) { return FALSE; } + size=g_pollable_input_stream_read_nonblocking(inStream, buffer, 1024, NULL, &error); + + if(error && error->message) + { + g_print_debug("Error: %s\n",error->message); + return FALSE; + } + + if(size <= 0) { g_print_debug("Got: %d\n", size); return FALSE; } - //emit a signal which calls the responseCb function + //emit a signal which calls the echoCb function SignalObjectClass myStruct; myStruct.instance = NULL; myStruct.cb = writeOutput; @@ -86,48 +96,57 @@ static gssize writeOutput(GPollableOutputStream* stream, gchar* data, gssize siz //initialize the signals void initSignals(guint *signal) { - *signal = g_signal_new("runEchoService", SIGNAL_OBJECT_TYPE, G_SIGNAL_RUN_FIRST, 0, NULL, NULL, + *signal = g_signal_new("runService", SIGNAL_OBJECT_TYPE, G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__POINTER , G_TYPE_NONE, 1, G_TYPE_POINTER); signalObject = g_object_new(SIGNAL_OBJECT_TYPE, NULL); - g_signal_connect (signalObject, "runEchoService", G_CALLBACK (responseCb), NULL); + g_signal_connect (signalObject, "runService", G_CALLBACK (responseCb), NULL); } -//send to client 3 times +//parse message and send to client void responseCb(gpointer instance, GObject *arg1, gpointer user_data) { - static GStaticMutex mutex = G_STATIC_MUTEX_INIT; - g_static_mutex_lock (&mutex); - SignalObjectClass *data = (SignalObjectClass*)arg1; - GPollableOutputStream* stream = (GPollableOutputStream*)data->outStream; - gchar* str = data->data; - gssize len = data->size; - gssize written = 0; - - // remove leading and trailing whitespace - g_strstrip (str); - - // I2C/LCD test function - LCD_display_error_message(str); - - guchar status = 0; // delete me, later - FIXME - // I2C test code - I2C_Write (PCF8574A + Upper_Encoder_Port, 0xff); - status = I2C_Read (PCF8574A + Upper_Encoder_Port); - - // dummy response function - gchar* upper = g_ascii_strup(str, len); - gchar *out_string = g_strdup_printf("%s\n%s\n%s\n%x\n",upper,upper,upper,status); - gssize out_len = strlen (out_string); - - //send response back to client - written = data->cb(stream, out_string, out_len); - if(written == -1) { - g_print_debug("Could not send message to client\n"); - } - - g_free(upper); - g_free(out_string); - - g_static_mutex_unlock (&mutex); + static GStaticMutex mutex = G_STATIC_MUTEX_INIT; + g_static_mutex_lock (&mutex); + SignalObjectClass *data = (SignalObjectClass*)arg1; + GPollableOutputStream* stream = (GPollableOutputStream*)data->outStream; + gchar* str = data->data; + + gssize written = 0; + int errors=OK; + + gchar* out = NULL; + + g_strstrip(str); + + // I2C/LCD test functions - start + guchar status = I2C_Read (PCF8574A + Upper_Encoder_Port); + gchar *lcd_str = g_strdup_printf ("%s %x",str,status); + LCD_display_error_message(lcd_str); + // I2C/LCD test functions - stop + + Parser_main(str, &out, 1, &errors); + + if(errors) { + + } + + if(out==NULL) out=g_strdup(" "); + + if(!strlen(out)) { + strcpy(out, " "); + } + + if(out[strlen(out)-1] != '\n' && out[strlen(out)-1] != '\r') { + strcat(out, "\n"); + } + + written = data->cb(stream, out, strlen(out)); + if (written == -1) { + g_print("Could not send message to client\n"); + } + + g_free(out); + + g_static_mutex_unlock (&mutex); } @@ -9,9 +9,7 @@ typedef struct _streamStruct { GPollableOutputStream* out; } streamStruct; -void responseCb(gpointer instance, GObject *arg1, gpointer user_data); - gboolean cbClientInput(gpointer data, gpointer additional); gboolean cbClientOutput(gpointer data, gpointer additional); void initSignals(guint *signal); -void responseCb(gpointer instance, GObject *arg1, gpointer user_data); +void responseCb(gpointer instance, GObject *arg1, gpointer user_data);
\ No newline at end of file diff --git a/socket-common.h b/socket-common.h index 4e4f694..dfba8f8 100644 --- a/socket-common.h +++ b/socket-common.h @@ -5,7 +5,7 @@ #include <gio/gio.h> #include <string.h> -//#define DEBUG_ON - uncomment this to have debug messages +#define DEBUG_ON - uncomment this to have debug messages #ifdef DEBUG_ON #define g_print_debug(...) g_print(__VA_ARGS__) @@ -1 +1 @@ -#define FW_VERSION "5.00" +#define FW_VERSION "5.00"
\ No newline at end of file |