From fc6d8813f64a9dd0d00e41e37cc8faf9851d680e Mon Sep 17 00:00:00 2001 From: Heikki Paajanen Date: Mon, 2 Jun 2008 13:45:12 +0300 Subject: [PATCH] Refactored handling of multiple interfaces --- src/call.c | 2 +- src/modem.c | 505 +++++++++++++++++++++++++-------------------------- src/modem_internal.h | 2 +- 3 files changed, 248 insertions(+), 261 deletions(-) diff --git a/src/call.c b/src/call.c index 6a891bd..4d51eaf 100644 --- a/src/call.c +++ b/src/call.c @@ -743,7 +743,7 @@ static void gsmd_call_command_activate (CallInterface *call, //Make sure the call we are trying to activate isn't already activated if (cur->status == CALL_CONNECTED) { g_debug("%s: Trying to activate a call that is already active.",__func__); - modem->call_ipc->activate_reply(ipc_data,modem->call_ipc); + modem->call_ipc->activate_reply(modem->call_ipc, ipc_data); return; } diff --git a/src/modem.c b/src/modem.c index 3c06dda..deff624 100644 --- a/src/modem.c +++ b/src/modem.c @@ -66,6 +66,41 @@ /* static Modem *modem_priv; */ +/** +* @brief Private data used when reading and writing data to/from serial. +*/ +struct _SerialData { + + /** + * Pointer to modem interface used + */ + Modem *modem; + + /** + * Index of the serial device to use + */ + guint device_index; + +}; + + +typedef struct _SerialData SerialData; + +typedef struct _SerialDevice { + /** + * AT command queue + */ + GQueue *commandQueue; + /** + * IOChannel of serial device + */ + Serial *serial; + /** + * Data used by IOChannel + */ + SerialData *serial_data; + +} SerialDevice; /** * @brief Modem Struct @@ -82,9 +117,9 @@ struct _Modem { void* vendor_handle; /** - * AT command queue + * Serial devices */ - GArray *commandQueues; + GArray *serial_devices; /** * AT command table @@ -92,16 +127,6 @@ struct _Modem { GHashTable *commands_table; /** - * IOChannels of modem - */ - GArray *serial; - - /** - * Data used by IOChannels - */ - GArray *serial_data; - - /** * modem interface */ ModemInterface *modem_if; @@ -114,45 +139,25 @@ struct _Modem { }; - -/** -* @brief Private data used when reading and writing data to/from serial. -*/ -struct _SerialData { - - /** - * Pointer to modem interface used - */ - Modem *modem; - - /** - * Index of the serial device to use - */ - guint device_index; -}; - - -typedef struct _SerialData SerialData; - /*********************service function**************************/ static void gsmd_modem_init ( ModemInterface* modem); static AtCommandHandlerStatus gsmd_modem_handler_unsolicite (ModemInterface *modem, AtCommandContext *at, GString *response); -static void gsmd_modem_process_next_command (Modem* modem, guint device_index); +static void gsmd_modem_process_next_command (Modem* modem, InterfaceType interface); static gboolean gsmd_modem_timeout_handle (gpointer data); static void gsmd_modem_send_command_raw ( ModemInterface *modem, const gchar *command, - guint device_index ); + InterfaceType interface ); static AtCommandHandlerStatus gsmd_modem_handler_alive(ModemInterface *modem, AtCommandContext *at, GString *response); static -SerialData *gsmd_modem_get_serial_data(Modem *modem, guint device_index); +SerialData *gsmd_modem_get_serial_data(Modem *modem, InterfaceType interface); static AtCommandHandlerStatus gsmd_modem_handler_current_calls(ModemInterface *modem, @@ -509,48 +514,20 @@ void gsmd_modem_clear_queue_func(gpointer data, void gsmd_modem_close_serial_ports(Modem *modem) { g_debug("%s",__func__); - Serial *serial = NULL; - SerialData *serial_data = NULL; gint i=0; - GQueue *queue = NULL; - - if (modem->serial) { - for (i=0;iserial->len;i++) { - serial = g_array_index(modem->serial,Serial*,i); - - /* //ask vendor to close it */ - /* if (modem->vendor->close_serial) */ - /* modem->vendor->close_serial(modem->vendor, */ - /* serial->fd); */ - /* else //or close it ourselves */ - /* close(serial->fd); */ - gsmd_serial_free(serial); - } - g_array_free(modem->serial,TRUE); - modem->serial = NULL; - } - - if (modem->serial_data) { - for (i=0;iserial_data->len;i++) { - serial_data = g_array_index(modem->serial_data,SerialData*,i); - g_free(serial_data); - } - g_array_free(modem->serial_data,TRUE); - modem->serial_data = NULL; - } - if (modem->commandQueues) { - for (i=0;icommandQueues->len;i++) { - queue = g_array_index(modem->commandQueues,GQueue*,i); - g_queue_foreach(queue,gsmd_modem_clear_queue_func,NULL); - g_queue_free(queue); + if (modem->serial_devices) { + for (i=0;iserial_devices->len;i++) { + SerialDevice *serial = g_array_index(modem->serial_devices,SerialDevice*,i); + gsmd_serial_free(serial->serial); + g_free(serial->serial_data); + g_queue_foreach(serial->commandQueue,gsmd_modem_clear_queue_func,NULL); + g_queue_free(serial->commandQueue); } - - g_array_free(modem->commandQueues,TRUE); - modem->commandQueues = NULL; + g_array_free(modem->serial_devices,TRUE); + modem->serial_devices = NULL; } - modem->modem_if->status = MODEM_UNINITIALIZED; } @@ -565,26 +542,19 @@ gboolean gsmd_modem_init_serial_ports(Modem *modem) modem->modem_if->status = MODEM_SERIAL_INIT; - GQueue *queue = NULL; gint i=0; g_assert(modem); g_assert(modem->modem_if); g_assert(modem->modem_if->devices); - GList *device_it = modem->modem_if->devices; - gchar *device = NULL; - gint device_fd = 0; - Serial *serial = NULL; - SerialData *serial_data = NULL; - modem->commandQueues = g_array_new(FALSE,TRUE,sizeof(GQueue*)); - modem->serial_data = g_array_new(FALSE,TRUE,sizeof(SerialData*)); - modem->serial = g_array_new(TRUE,TRUE,sizeof(Serial*)); + modem->serial_devices = g_array_new(TRUE,TRUE,sizeof(SerialDevice*)); + GList *device_it = modem->modem_if->devices; while (device_it) { - device = device_it->data; + gchar *device = device_it->data; g_debug("Adding device %s",device); - device_fd = modem->vendor->init_serial( modem->vendor, device); + gint device_fd = modem->vendor->init_serial( modem->vendor, device); if ( !device_fd ) { g_warning ("%s : init_serial failed\n", @@ -592,7 +562,7 @@ gboolean gsmd_modem_init_serial_ports(Modem *modem) return FALSE; } - serial = gsmd_serial_open_port (device_fd); + Serial *serial = gsmd_serial_open_port (device_fd); if (!serial) { g_warning("%s : Failed to open device %s", __func__, @@ -601,8 +571,7 @@ gboolean gsmd_modem_init_serial_ports(Modem *modem) gsmd_modem_close (modem->modem_if); return FALSE; } - - serial_data = g_new0(SerialData,1); + SerialData *serial_data = g_new0(SerialData,1); serial_data->modem = modem; serial_data->device_index = i; @@ -619,10 +588,11 @@ gboolean gsmd_modem_init_serial_ports(Modem *modem) (gpointer)serial_data, FALSE); - g_array_append_val(modem->serial,serial); - g_array_append_val(modem->serial_data,serial_data); - queue = g_queue_new(); - g_array_append_val(modem->commandQueues,queue); + SerialDevice *serial_device = g_new0(SerialDevice, 1); + serial_device->serial = serial; + serial_device->serial_data = serial_data; + serial_device->commandQueue = g_queue_new(); + g_array_append_val(modem->serial_devices,serial_device); device_it = g_list_next(device_it); i++; } @@ -694,6 +664,7 @@ ModemInterface* gsmd_modem_open (gchar **devices, i=0; device = devices[i]; while (device) { + g_debug("%s : '%s'", __func__, device); modem->modem_if->devices = g_list_append(modem->modem_if->devices,device); i++; device = devices[i]; @@ -703,7 +674,10 @@ ModemInterface* gsmd_modem_open (gchar **devices, return NULL; - gsmd_modem_assign_device_indices(modem->modem_if,interface_devices); + if( !gsmd_modem_assign_device_indices(modem->modem_if,interface_devices) ) { + gsmd_modem_close(modem->modem_if); + return NULL; + } return modem->modem_if; } @@ -723,6 +697,29 @@ void gsmd_modem_change_sim_status(ModemInterface *modem, SIMStatus status) modem->sim_status = status; } +void gsmd_modem_serial_devices_free(Modem *modem) +{ + gint i=0; + if (modem->serial_devices) { + for (i=0;iserial_devices->len;i++) { + SerialDevice *device = g_array_index(modem->serial_devices,SerialDevice*,i); + /* //ask vendor to close it */ + /* if (modem->vendor->close_serial) */ + /* modem->vendor->close_serial(modem->vendor, */ + /* serial->fd); */ + /* else //or close it ourselves */ + /* close(serial->fd); */ + gsmd_serial_free(device->serial); + g_queue_foreach(device->commandQueue,gsmd_modem_clear_queue_func,NULL); + g_queue_free(device->commandQueue); + g_free(device->serial_data); + + } + + g_array_free(modem->serial_devices,FALSE); + modem->serial_devices = NULL; + } +} /** * @brief Close modem @@ -731,10 +728,6 @@ void gsmd_modem_change_sim_status(ModemInterface *modem, SIMStatus status) */ void gsmd_modem_close (ModemInterface* modem) { - gint i = 0; - Serial *serial = NULL; - GQueue *queue = NULL; - Modem *modem_priv = (Modem*)modem->priv; g_assert (modem_priv); g_debug("%s", __func__); @@ -771,19 +764,8 @@ void gsmd_modem_close (ModemInterface* modem) g_scanner_destroy (modem->scanner); g_hash_table_destroy (modem_priv->commands_table); + gsmd_modem_serial_devices_free(modem_priv); - if (modem_priv->serial) { - for (i=0;iserial->len;i++) { - serial = g_array_index(modem_priv->serial,Serial*,i); - queue = g_array_index(modem_priv->commandQueues,GQueue*,i); - gsmd_serial_free(serial); - g_queue_free(queue); - - } - - g_array_free(modem_priv->serial,FALSE); - g_array_free (modem_priv->commandQueues,FALSE); - } GList *list = modem->calls; while ( list ) { @@ -802,6 +784,27 @@ void gsmd_modem_close (ModemInterface* modem) /************************service function**************************************/ +SerialDevice* gsmd_modem_get_serial_device_index(Modem *modem,guint device_index) +{ + g_assert(modem); + g_assert(modem->serial_devices); + if( modem->serial_devices->len > device_index ) { + return g_array_index(modem->serial_devices, SerialDevice*, device_index); + } else { + g_warning("%s : Invalid index: %d. We have %d device(s)", + __func__, device_index, modem->serial_devices->len); + g_assert(FALSE); + } + return NULL; +} +SerialDevice* gsmd_modem_get_serial_device(Modem *modem,InterfaceType interface) +{ + g_assert(modem); + g_assert(modem->serial_devices); + guint device_index = modem->modem_if->interface_devices[interface]; + return gsmd_modem_get_serial_device_index(modem, device_index); +} + /** * @brief Sends initializing at commands to the modem * @@ -904,10 +907,10 @@ void gsmd_modem_general_at_init_sim_ready (ModemInterface* modem) * @return pointer to ipc data */ gpointer* gsmd_modem_get_current_ipc_data (ModemInterface *modem, - guint device_index) + InterfaceType interface) { g_assert( modem ); - AtCommandContext* at = gsmd_modem_get_current_at_command(modem,device_index); + AtCommandContext* at = gsmd_modem_get_current_at_command(modem,interface); if (at) return at->ipc_data; return NULL; @@ -920,22 +923,35 @@ gpointer* gsmd_modem_get_current_ipc_data (ModemInterface *modem, * * @return atcommand that is currently processed */ +AtCommandContext* gsmd_modem_get_current_at_command_index (ModemInterface *modem, + guint device_index) +{ + g_assert( modem ); + Modem *modem_priv = (Modem*)modem->priv; + g_assert (modem_priv); + g_assert (modem_priv->serial_devices); + SerialDevice *device = gsmd_modem_get_serial_device_index( modem_priv, device_index); + if (device) + return g_queue_peek_head(device->commandQueue); + return NULL; +} +/** + * @brief Get AT command processed currently + * + * @param modem modem device struct pointer + * + * @return atcommand that is currently processed + */ AtCommandContext* gsmd_modem_get_current_at_command (ModemInterface *modem, - guint device_index) + InterfaceType interface) { g_assert( modem ); Modem *modem_priv = (Modem*)modem->priv; g_assert (modem_priv); - g_assert (modem_priv->commandQueues); - GQueue *queue = NULL; - if ( modem_priv->commandQueues->len > device_index ) { - queue = g_array_index(modem_priv->commandQueues, - GQueue*, - device_index); - } - - if (queue) - return g_queue_peek_head(queue); + g_assert (modem_priv->serial_devices); + SerialDevice *device = gsmd_modem_get_serial_device( modem_priv, interface); + if (device) + return g_queue_peek_head(device->commandQueue); return NULL; } @@ -951,20 +967,18 @@ AtCommandContext* gsmd_modem_get_current_at_command (ModemInterface *modem, */ void gsmd_modem_send_command_force_next (Modem *modem, AtCommandContext *at, - guint device_index) + InterfaceType interface) { g_assert( modem ); g_assert( at ); - GQueue *queue = g_array_index(modem->commandQueues, - GQueue*, - device_index); + SerialDevice *device = gsmd_modem_get_serial_device( modem, interface); - if (queue) { - AtCommandContext *current_at = g_queue_pop_head ( queue ); - g_queue_push_tail ( queue, at ); + if (device) { + AtCommandContext *current_at = g_queue_pop_head ( device->commandQueue ); + g_queue_push_tail ( device->commandQueue, at ); if ( current_at ) { - g_queue_push_head (queue, current_at ); + g_queue_push_head (device->commandQueue, current_at ); } } } @@ -984,17 +998,9 @@ gboolean gsmd_modem_post_alive_test (ModemInterface *modem_if, gboolean reset) g_assert(modem_if); Modem *modem = (Modem*)modem_if->priv; g_assert(modem); - g_assert(modem->commandQueues); - guint device_index = gsmd_modem_get_interface_device_index(modem->modem_if, - INTERFACE_GENERAL); - if (device_index >= modem->commandQueues->len) - return FALSE; - - GQueue *queue = g_array_index(modem->commandQueues, - GQueue*, - device_index); - - + g_assert(modem->serial_devices); + SerialDevice *device = gsmd_modem_get_serial_device( modem, INTERFACE_GENERAL); + g_assert(device); AtCommandContext *at = NULL; if (reset) { @@ -1015,8 +1021,8 @@ gboolean gsmd_modem_post_alive_test (ModemInterface *modem_if, gboolean reset) NULL, NULL); } - g_queue_push_head (queue, at); - gsmd_modem_process_next_command(modem,device_index); + g_queue_push_head (device->commandQueue, at); + gsmd_modem_process_next_command(modem,INTERFACE_GENERAL); return TRUE; } @@ -1042,21 +1048,17 @@ gboolean gsmd_modem_post_at_command_and_override (ModemInterface *modem, { g_assert(modem); Modem *modem_priv = (Modem*)modem->priv; - g_assert(modem_priv->commandQueues); + g_assert(modem_priv->serial_devices); - guint device_index = gsmd_modem_get_interface_device_index(modem,interface); + SerialDevice *device = gsmd_modem_get_serial_device( modem_priv, interface); - if (device_index >= modem_priv->commandQueues->len) { + if (!device) { g_warning("%s : improper device index",__func__); return FALSE; } - GQueue *queue = g_array_index(modem_priv->commandQueues, - GQueue*, - device_index); - AtCommandContext *current_at = gsmd_modem_get_current_at_command ( modem, - device_index ); + interface ); //Most commands require PIN to be set if (new_at->command->required_state > modem->sim_status) { @@ -1076,8 +1078,8 @@ gboolean gsmd_modem_post_at_command_and_override (ModemInterface *modem, //If no commands in queue, insert this one and process the queue if (!current_at) { g_debug("%s : No commands in queue, no need to override",__func__); - g_queue_push_tail (queue, new_at); - gsmd_modem_process_next_command(modem_priv,device_index); + g_queue_push_tail (device->commandQueue, new_at); + gsmd_modem_process_next_command(modem_priv,interface); return TRUE; } @@ -1090,16 +1092,16 @@ gboolean gsmd_modem_post_at_command_and_override (ModemInterface *modem, g_source_remove(current_at->timeout_event_id); } - g_queue_push_head (queue, new_at); - gsmd_modem_send_command_raw (modem,"+++\r\n",device_index); + g_queue_push_head (device->commandQueue, new_at); + gsmd_modem_send_command_raw (modem,"+++\r\n",interface); //Process the new command - gsmd_modem_process_next_command(modem_priv,device_index); + gsmd_modem_process_next_command(modem_priv,interface); } else { //if the current command isn't overrideable push our new command second in line g_debug("%s : Current command can't be overridden, moving next in queue", __func__); - g_queue_pop_head(queue); - g_queue_push_head(queue,new_at); - g_queue_push_head(queue,current_at); + g_queue_pop_head(device->commandQueue); + g_queue_push_head(device->commandQueue, new_at); + g_queue_push_head(device->commandQueue,current_at); } @@ -1124,9 +1126,7 @@ gboolean gsmd_modem_post_at_command ( ModemInterface *modem, gsmd_utils_print_data(__func__,"command: ",at->command->command); Modem *modem_priv = (Modem*)modem->priv; - if ( !modem_priv->serial - && !modem_priv->serial_data - && !modem_priv->commandQueues ) { + if ( !modem_priv->serial_devices ) { //If serial ports aren't initialized then initialize them gsmd_modem_init_serial_ports(modem_priv); @@ -1149,25 +1149,21 @@ gboolean gsmd_modem_post_at_command ( ModemInterface *modem, - guint device_index = gsmd_modem_get_interface_device_index(modem,interface); - AtCommandContext *current_at = gsmd_modem_get_current_at_command(modem, - device_index); + interface); - if (device_index > modem_priv->commandQueues->len) + SerialDevice *device = gsmd_modem_get_serial_device( modem_priv, interface); + if (!device) { return FALSE; - - GQueue *queue = g_array_index(modem_priv->commandQueues, - GQueue*, - device_index); + } if (at) { - g_queue_push_tail (queue, at); + g_queue_push_tail (device->commandQueue, at); } if (!current_at) { g_debug("%s : process next command", __func__); - gsmd_modem_process_next_command(modem_priv,device_index); + gsmd_modem_process_next_command(modem_priv,interface); } return TRUE; @@ -1325,21 +1321,20 @@ void gsmd_at_command_context_free (AtCommandContext* at) * AtCommand will be removed from queue and freed. * * @param modem pointer to the modem - * @param device_index index of the device whose command to finish + * @param interface device whose command to finish * @param test_alive should test alive command be sent */ static void gsmd_modem_finish_command (Modem* modem, - guint device_index, + InterfaceType interface, gboolean test_alive) { g_debug("%s\n",__func__); - if (!modem->commandQueues || device_index >= modem->commandQueues->len) { + SerialDevice *device = gsmd_modem_get_serial_device( modem, interface); + if (!device) { return; } - GQueue *queue = g_array_index(modem->commandQueues, - GQueue*, - device_index); + GQueue *queue = device->commandQueue; AtCommandContext *at = g_queue_pop_head (queue); @@ -1368,7 +1363,7 @@ static void gsmd_modem_finish_command (Modem* modem, gsmd_modem_post_alive_test(modem->modem_if, FALSE); } else { g_debug("%s : processing next command",__func__); - gsmd_modem_process_next_command(modem,device_index); + gsmd_modem_process_next_command(modem,interface); } } @@ -1376,17 +1371,16 @@ static void gsmd_modem_finish_command (Modem* modem, * @brief Returns serial data from specified device index * * @param modem pointer to modem -* @param device_index index of the device whose serial data to get +* @param interface device whose serial data to get * @return specified device's serial data or NULL if none found */ -static SerialData *gsmd_modem_get_serial_data(Modem *modem, guint device_index) +static SerialData *gsmd_modem_get_serial_data(Modem *modem, InterfaceType interface) { g_assert(modem); - g_assert(modem->serial_data); - if (device_index >= 0 && device_index < modem->serial_data->len) - return g_array_index(modem->serial_data, - SerialData*, - device_index); + g_assert(modem->serial_devices); + SerialDevice *device = gsmd_modem_get_serial_device(modem, interface); + if (device) + return device->serial_data; return NULL; } @@ -1405,7 +1399,7 @@ static void gsmd_modem_print_single_command(gpointer data,gpointer user_data) g_debug("%s : %s",__func__,at->command->command); } -static void gsmd_modem_print_commands(Modem* modem, guint device_index) +static void gsmd_modem_print_commands(Modem* modem, InterfaceType interface) { g_assert( modem ); GQueue *queue = NULL; @@ -1427,11 +1421,11 @@ static void gsmd_modem_print_commands(Modem* modem, guint device_index) * * @param modem whose next command to process */ -static void gsmd_modem_process_next_command (Modem* modem, guint device_index) +static void gsmd_modem_process_next_command (Modem* modem, InterfaceType interface) { - g_debug("%s: device: %d",__func__,device_index); + g_debug("%s: device: %d",__func__,interface); AtCommandContext *at = gsmd_modem_get_current_at_command ( modem->modem_if, - device_index ); + interface ); if (modem->vendor->command_prepare) { at = modem->vendor->command_prepare(modem->vendor,modem->modem_if,at); @@ -1445,9 +1439,9 @@ static void gsmd_modem_process_next_command (Modem* modem, guint device_index) __func__, at->command->command); */ - GQueue *queue = g_array_index(modem->commandQueues, - GQueue*, - device_index); + SerialDevice *device = gsmd_modem_get_serial_device(modem, interface); + g_assert(device); + GQueue *queue = device->commandQueue; g_queue_push_head (queue, at); } @@ -1455,7 +1449,7 @@ static void gsmd_modem_process_next_command (Modem* modem, guint device_index) //Get current command again to allow vendor to insert their own command at = gsmd_modem_get_current_at_command ( modem->modem_if, - device_index ); + interface ); if (!at) { g_debug("%s : No commands in queue", __func__); @@ -1472,7 +1466,7 @@ static void gsmd_modem_process_next_command (Modem* modem, guint device_index) "SIM AUTHENTICATION ERROR"); } - gsmd_modem_finish_command(modem,device_index,FALSE); + gsmd_modem_finish_command(modem, interface, FALSE); return; } @@ -1515,11 +1509,11 @@ static void gsmd_modem_process_next_command (Modem* modem, guint device_index) data->timeoutid = &at->timeout_event_id; data->timeout_data = gsmd_modem_get_serial_data(modem, - device_index); + interface); data->timeout_function = &gsmd_modem_timeout_handle; data->write_complete_data = at; data->write_complete_function = &gsmd_modem_write_complete; - gsmd_modem_serial_queue_write (modem->modem_if, data,device_index); + gsmd_modem_serial_queue_write (modem->modem_if, data,interface); g_string_free(command,TRUE); } @@ -1556,7 +1550,7 @@ void gsmd_modem_data_in (GString *buffer, gpointer data ) AtCommandContext *at = NULL; g_assert(modem); - at = gsmd_modem_get_current_at_command ( modem_if, serial->device_index); + at = gsmd_modem_get_current_at_command_index ( modem_if, serial->device_index); if ( at && !at->write_complete) { g_debug("%s : Got input, but current command has not been written.", __func__); at = NULL; @@ -1667,26 +1661,21 @@ Trigger* gsmd_modem_serial_register_trigger(ModemInterface *modem, SerialDataIn handler, gpointer data, gboolean onetime, - InterfaceType interface_index) + InterfaceType interface) { Modem *modem_priv = (Modem*)modem->priv; - guint device_index = gsmd_modem_get_interface_device_index(modem,interface_index); - Serial *serial = NULL; - if ( modem_priv->serial->len > device_index ) { - serial = g_array_index(modem_priv->serial,Serial*,device_index); - } - - if (!serial) { + SerialDevice *device = gsmd_modem_get_serial_device(modem_priv, interface); + if (!device) { return NULL; } if ( !handler ) { g_debug("%s : Using default handler/data", __func__); handler = &gsmd_modem_data_in; - data = gsmd_modem_get_serial_data(modem_priv, device_index); + data = device->serial_data; } - return gsmd_serial_register_trigger(serial, + return gsmd_serial_register_trigger(device->serial, trigger_str, handler, data, @@ -1703,16 +1692,16 @@ Trigger* gsmd_modem_serial_register_trigger(ModemInterface *modem, */ void gsmd_modem_serial_queue_write ( ModemInterface *modem, WriteData *data, - guint device_index) + InterfaceType interface) { Modem *modem_priv = (Modem*)modem->priv; - if (device_index < modem_priv->serial->len && device_index >= 0) { - Serial *serial = g_array_index(modem_priv->serial,Serial*,device_index); - gsmd_serial_queue_write( serial, data); + SerialDevice *device = gsmd_modem_get_serial_device(modem_priv, interface); + if (device) { + gsmd_serial_queue_write( device->serial, data); } else { g_warning("%s : Writing data to improper serial device %d", __func__, - device_index); + device->serial_data->device_index); } } @@ -1726,16 +1715,16 @@ void gsmd_modem_serial_queue_write ( ModemInterface *modem, */ void gsmd_modem_serial_write ( ModemInterface *modem, WriteData *data, - guint device_index) + InterfaceType interface) { Modem *modem_priv = (Modem*)modem->priv; - if (device_index < modem_priv->serial->len && device_index >= 0) { - Serial *serial = g_array_index(modem_priv->serial,Serial*,device_index); - gsmd_serial_write( serial, data); + SerialDevice *device = gsmd_modem_get_serial_device(modem_priv, interface); + if (device) { + gsmd_serial_write( device->serial, data); } else { g_warning("%s : Writing data to improper serial device %d", __func__, - device_index); + device->serial_data->device_index); } } @@ -1745,47 +1734,41 @@ void gsmd_modem_serial_write ( ModemInterface *modem, * * @param modem modem device struct pointer * @param command AT command to write - * @param device_index index of the serial device to use + * @param interface which interface to be used to write command */ static void gsmd_modem_send_command_raw ( ModemInterface *modem, const gchar *command, - guint device_index ) -{ - Modem *modem_priv = modem->priv; - if (device_index < modem_priv->serial->len) { - Serial *serial = g_array_index(modem_priv->serial,Serial*,device_index); - if (serial) { - WriteData *data = g_try_new0(WriteData,1); - data->command = g_strdup(command); - data->timeoutid = NULL; - data->timeout = 0; - data->timeout_data = NULL; - data->timeout_function = NULL; - gsmd_modem_serial_write(modem,data,device_index); - } - } + InterfaceType interface ) +{ + WriteData *data = g_try_new0(WriteData,1); + data->command = g_strdup(command); + data->timeoutid = NULL; + data->timeout = 0; + data->timeout_data = NULL; + data->timeout_function = NULL; + gsmd_modem_serial_write(modem,data,interface); } /** -* @brief Write data directly to serial port without any timeouts -* Automatically creates an empty WriteData structure -* with given command. -* -* @param modem pointer to modeminterface -* @param command command to write to serial port -* @param interface_index which interface to be used to write command -*/ + * @brief Write command directly to serial port + * + * @param modem modem device struct pointer + * @param command AT command to write + * @param interface which interface to be used to write command + */ void gsmd_modem_serial_write_simple ( ModemInterface *modem, const gchar *command, - InterfaceType interface_index) + InterfaceType interface ) { - guint device_index = gsmd_modem_get_interface_device_index(modem, - interface_index); - - gsmd_modem_send_command_raw(modem,command,device_index); + WriteData *data = g_try_new0(WriteData,1); + data->command = g_strdup(command); + data->timeoutid = NULL; + data->timeout = 0; + data->timeout_data = NULL; + data->timeout_function = NULL; + gsmd_modem_serial_write(modem,data,interface); } - /** * @brief Initializes modem * @@ -1829,8 +1812,8 @@ static gboolean gsmd_modem_timeout_handle (gpointer data) AtCommandHandlerStatus handled = AT_HANDLER_DONT_UNDERSTAND; Modem* modem = serial->modem; AtCommandHandler handler; - AtCommandContext *at = gsmd_modem_get_current_at_command ( modem->modem_if, - serial->device_index ); + AtCommandContext *at = gsmd_modem_get_current_at_command_index ( modem->modem_if, + device_index ); if ( !at ) { g_warning("%s : No current command!",__func__); @@ -2525,13 +2508,13 @@ AtCommandHandlerStatus gsmd_modem_handler_unsolicite (ModemInterface *modem, * * @param modem Modem device struct pointer */ -void gsmd_modem_retry_current_command (Modem* modem, guint device_index) +void gsmd_modem_retry_current_command (Modem* modem, InterfaceType interface) { - g_debug("%s: device: %d", __func__,device_index); + g_debug("%s: device: %d", __func__,interface); g_assert ( modem ); - g_assert ( modem->commandQueues ); + g_assert ( modem->serial_devices ); AtCommandContext *at = gsmd_modem_get_current_at_command ( modem->modem_if, - device_index ); + interface ); modem->is_timeout = FALSE; @@ -2546,10 +2529,10 @@ void gsmd_modem_retry_current_command (Modem* modem, guint device_index) if (at->retry_counter > 0) { g_debug("Retrying command %s",at->command->command); at->retry_counter--; - gsmd_modem_process_next_command (modem,device_index); + gsmd_modem_process_next_command (modem,interface); } else { g_debug("Finished retrying command %s",at->command->command); - gsmd_modem_finish_command(modem,device_index,FALSE); + gsmd_modem_finish_command(modem,interface,FALSE); } @@ -2561,11 +2544,10 @@ gboolean gsmd_modem_cancel_command_id(ModemInterface *modem, AtCommandID cmd_id, InterfaceType interface) { - guint device_index = gsmd_modem_get_interface_device_index(modem,interface); Modem * modem_priv = (Modem*)modem->priv; - GQueue* queue = g_array_index(modem_priv->commandQueues, - GQueue*, - device_index); + SerialDevice *device = gsmd_modem_get_serial_device(modem_priv, interface); + g_assert(device); + GQueue* queue = device->commandQueue; g_assert(queue); GList* list = NULL; gboolean res = FALSE; @@ -2589,12 +2571,17 @@ gboolean gsmd_modem_cancel_command_id(ModemInterface *modem, return res; } -void gsmd_modem_assign_device_indices(ModemInterface *modem, int *indices) +gboolean gsmd_modem_assign_device_indices(ModemInterface *modem, int *indices) { int i=0; for (i=0;ipriv,indices[i]); + if(!device) { + return FALSE; + } modem->interface_devices[i]=indices[i]; } + return TRUE; } guint gsmd_modem_get_interface_device_index(ModemInterface *modem, diff --git a/src/modem_internal.h b/src/modem_internal.h index ee3bb82..7679964 100644 --- a/src/modem_internal.h +++ b/src/modem_internal.h @@ -46,7 +46,7 @@ typedef struct _Modem Modem; -void gsmd_modem_assign_device_indices(ModemInterface *modem, int *indices); +gboolean gsmd_modem_assign_device_indices(ModemInterface *modem, int *indices); ModemInterface* gsmd_modem_open (gchar **devices, const gchar *vendor, -- 2.11.4.GIT