Major code cleanup

This commit is contained in:
Pierre Pronchery 2011-11-03 02:32:00 +00:00
parent 24ea28e6cc
commit f54a6f475c

View File

@ -198,6 +198,8 @@ enum
HAYES_REQUEST_OPERATOR_FORMAT_NUMERIC, HAYES_REQUEST_OPERATOR_FORMAT_NUMERIC,
HAYES_REQUEST_PHONE_ACTIVE, HAYES_REQUEST_PHONE_ACTIVE,
HAYES_REQUEST_REGISTRATION, HAYES_REQUEST_REGISTRATION,
HAYES_REQUEST_REGISTRATION_AUTOMATIC,
HAYES_REQUEST_REGISTRATION_DISABLED,
HAYES_REQUEST_REGISTRATION_UNSOLLICITED_DISABLE, HAYES_REQUEST_REGISTRATION_UNSOLLICITED_DISABLE,
HAYES_REQUEST_REGISTRATION_UNSOLLICITED_ENABLE, HAYES_REQUEST_REGISTRATION_UNSOLLICITED_ENABLE,
HAYES_REQUEST_SIM_PIN_VALID, HAYES_REQUEST_SIM_PIN_VALID,
@ -224,6 +226,8 @@ static int _hayes_trigger(ModemPlugin * modem, ModemEventType event);
static void _hayes_set_mode(ModemPlugin * modem, HayesMode mode); static void _hayes_set_mode(ModemPlugin * modem, HayesMode mode);
/* useful */ /* useful */
static int _hayes_request_type(ModemPlugin * modem, ModemRequestType type);
/* messages */ /* messages */
static char * _hayes_message_to_pdu(ModemPlugin * modem, char const * number, static char * _hayes_message_to_pdu(ModemPlugin * modem, char const * number,
ModemMessageEncoding encoding, size_t length, ModemMessageEncoding encoding, size_t length,
@ -262,6 +266,8 @@ static void * _hayes_command_get_data(HayesCommand * command);
static char * _hayes_command_get_line(HayesCommand * command, static char * _hayes_command_get_line(HayesCommand * command,
char const * prefix); char const * prefix);
#endif #endif
static HayesCommandPriority _hayes_command_get_priority(HayesCommand * command);
static HayesCommandStatus _hayes_command_get_status(HayesCommand * command);
static unsigned int _hayes_command_get_timeout(HayesCommand * command); static unsigned int _hayes_command_get_timeout(HayesCommand * command);
static void _hayes_command_set_callback(HayesCommand * command, static void _hayes_command_set_callback(HayesCommand * command,
HayesCommandCallback callback, void * priv); HayesCommandCallback callback, void * priv);
@ -325,6 +331,10 @@ static HayesCommandStatus _on_request_model(HayesCommand * command,
HayesCommandStatus status, void * priv); HayesCommandStatus status, void * priv);
static HayesCommandStatus _on_request_registration(HayesCommand * command, static HayesCommandStatus _on_request_registration(HayesCommand * command,
HayesCommandStatus status, void * priv); HayesCommandStatus status, void * priv);
static HayesCommandStatus _on_request_registration_automatic(
HayesCommand * command, HayesCommandStatus status, void * priv);
static HayesCommandStatus _on_request_registration_disabled(
HayesCommand * command, HayesCommandStatus status, void * priv);
static HayesCommandStatus _on_request_sim_pin_valid(HayesCommand * command, static HayesCommandStatus _on_request_sim_pin_valid(HayesCommand * command,
HayesCommandStatus status, void * priv); HayesCommandStatus status, void * priv);
static HayesCommandStatus _on_request_unsupported(HayesCommand * command, static HayesCommandStatus _on_request_unsupported(HayesCommand * command,
@ -512,6 +522,10 @@ static HayesRequestHandler _hayes_request_handlers[] =
_on_request_call }, _on_request_call },
{ HAYES_REQUEST_REGISTRATION, "AT+CREG?", { HAYES_REQUEST_REGISTRATION, "AT+CREG?",
_on_request_generic }, _on_request_generic },
{ HAYES_REQUEST_REGISTRATION_AUTOMATIC, "AT+COPS=0",
_on_request_registration_automatic },
{ HAYES_REQUEST_REGISTRATION_DISABLED, "AT+COPS=2",
_on_request_registration_disabled },
{ HAYES_REQUEST_REGISTRATION_UNSOLLICITED_DISABLE,"AT+CREG=0", { HAYES_REQUEST_REGISTRATION_UNSOLLICITED_DISABLE,"AT+CREG=0",
_on_request_generic }, _on_request_generic },
{ HAYES_REQUEST_REGISTRATION_UNSOLLICITED_ENABLE,"AT+CREG=2", { HAYES_REQUEST_REGISTRATION_UNSOLLICITED_ENABLE,"AT+CREG=2",
@ -898,12 +912,8 @@ static char * _request_attention_call_hangup(ModemPlugin * modem)
static char * _request_attention_connectivity(ModemPlugin * modem, static char * _request_attention_connectivity(ModemPlugin * modem,
unsigned int enabled) unsigned int enabled)
{ {
ModemRequest request; _hayes_request_type(modem, enabled ? HAYES_REQUEST_FUNCTIONAL_ENABLE
: HAYES_REQUEST_FUNCTIONAL_DISABLE);
memset(&request, 0, sizeof(request));
request.type = enabled ? HAYES_REQUEST_FUNCTIONAL_ENABLE
: HAYES_REQUEST_FUNCTIONAL_DISABLE;
_hayes_request(modem, &request);
return NULL; return NULL;
} }
@ -1023,14 +1033,11 @@ static char * _request_attention_message_send(ModemPlugin * modem,
Hayes * hayes = modem->priv; Hayes * hayes = modem->priv;
char * ret; char * ret;
char const cmd[] = "AT+CMGS="; char const cmd[] = "AT+CMGS=";
ModemRequest request;
char * pdu; char * pdu;
size_t pdulen; size_t pdulen;
size_t len; size_t len;
memset(&request, 0, sizeof(request)); if(_hayes_request_type(modem, HAYES_REQUEST_MESSAGE_FORMAT_PDU) != 0)
request.type = HAYES_REQUEST_MESSAGE_FORMAT_PDU;
if(_hayes_request(modem, &request) != 0)
return NULL; return NULL;
if((pdu = _hayes_message_to_pdu(modem, number, encoding, length, if((pdu = _hayes_message_to_pdu(modem, number, encoding, length,
content)) == NULL) content)) == NULL)
@ -1058,9 +1065,13 @@ static char * _request_attention_registration(ModemPlugin * modem,
switch(mode) switch(mode)
{ {
case MODEM_REGISTRATION_MODE_AUTOMATIC: case MODEM_REGISTRATION_MODE_AUTOMATIC:
return strdup("AT+COPS=0"); _hayes_request_type(modem,
HAYES_REQUEST_REGISTRATION_AUTOMATIC);
break;
case MODEM_REGISTRATION_MODE_DISABLED: case MODEM_REGISTRATION_MODE_DISABLED:
return strdup("AT+COPS=2"); _hayes_request_type(modem,
HAYES_REQUEST_REGISTRATION_DISABLED);
break;
case MODEM_REGISTRATION_MODE_MANUAL: case MODEM_REGISTRATION_MODE_MANUAL:
if(_operator == NULL) if(_operator == NULL)
return NULL; return NULL;
@ -1155,13 +1166,11 @@ static int _hayes_trigger(ModemPlugin * modem, ModemEventType event)
{ {
int ret = 0; int ret = 0;
Hayes * hayes = modem->priv; Hayes * hayes = modem->priv;
ModemRequest request;
ModemEvent * e; ModemEvent * e;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%u)\n", __func__, event); fprintf(stderr, "DEBUG: %s(%u)\n", __func__, event);
#endif #endif
memset(&request, 0, sizeof(request));
switch(event) switch(event)
{ {
case MODEM_EVENT_TYPE_BATTERY_LEVEL: /* use the existing data */ case MODEM_EVENT_TYPE_BATTERY_LEVEL: /* use the existing data */
@ -1172,30 +1181,26 @@ static int _hayes_trigger(ModemPlugin * modem, ModemEventType event)
modem->helper->event(modem->helper->modem, e); modem->helper->event(modem->helper->modem, e);
break; break;
case MODEM_EVENT_TYPE_AUTHENTICATION: case MODEM_EVENT_TYPE_AUTHENTICATION:
request.type = HAYES_REQUEST_SIM_PIN_VALID; return _hayes_request_type(modem,
return _hayes_request(modem, &request); HAYES_REQUEST_SIM_PIN_VALID);
case MODEM_EVENT_TYPE_CONTACT: case MODEM_EVENT_TYPE_CONTACT:
request.type = MODEM_REQUEST_CONTACT_LIST; return _hayes_request_type(modem,
return _hayes_request(modem, &request); MODEM_REQUEST_CONTACT_LIST);
case MODEM_EVENT_TYPE_MESSAGE: case MODEM_EVENT_TYPE_MESSAGE:
request.type = MODEM_REQUEST_MESSAGE_LIST; return _hayes_request_type(modem,
return _hayes_request(modem, &request); MODEM_REQUEST_MESSAGE_LIST);
case MODEM_EVENT_TYPE_MODEL: case MODEM_EVENT_TYPE_MODEL:
request.type = HAYES_REQUEST_VENDOR; ret |= _hayes_request_type(modem, HAYES_REQUEST_VENDOR);
ret |= _hayes_request(modem, &request); ret |= _hayes_request_type(modem, HAYES_REQUEST_MODEL);
request.type = HAYES_REQUEST_MODEL; ret |= _hayes_request_type(modem,
ret |= _hayes_request(modem, &request); HAYES_REQUEST_VERSION);
request.type = HAYES_REQUEST_VERSION;
ret |= _hayes_request(modem, &request);
break; break;
case MODEM_EVENT_TYPE_REGISTRATION: case MODEM_EVENT_TYPE_REGISTRATION:
e = &hayes->events[MODEM_EVENT_TYPE_REGISTRATION]; e = &hayes->events[MODEM_EVENT_TYPE_REGISTRATION];
if(e->registration.status if(e->registration.status
== MODEM_REGISTRATION_STATUS_UNKNOWN) == MODEM_REGISTRATION_STATUS_UNKNOWN)
{ ret |= _hayes_request_type(modem,
request.type = HAYES_REQUEST_REGISTRATION; HAYES_REQUEST_REGISTRATION);
ret |= _hayes_request(modem, &request);
}
else else
modem->helper->event(modem->helper->modem, e); modem->helper->event(modem->helper->modem, e);
break; break;
@ -1459,19 +1464,20 @@ static int _hayes_parse(ModemPlugin * modem)
static int _parse_do(ModemPlugin * modem) static int _parse_do(ModemPlugin * modem)
{ {
Hayes * hayes = modem->priv; Hayes * hayes = modem->priv;
char const * line = hayes->rd_buf;
HayesCommand * command = (hayes->queue != NULL) ? hayes->queue->data HayesCommand * command = (hayes->queue != NULL) ? hayes->queue->data
: NULL; : NULL;
HayesCommandStatus status; HayesCommandStatus status;
if(command == NULL) if(command == NULL)
/* this was most likely unsollicited */ /* this was most likely unsollicited */
return _hayes_parse_trigger(modem, hayes->rd_buf, NULL); return _hayes_parse_trigger(modem, line, NULL);
if(_hayes_command_answer_append(command, hayes->rd_buf) != 0) if(_hayes_command_answer_append(command, line) != 0)
return -1; return -1;
if((status = _hayes_command_callback(command)) == HCS_ACTIVE) if((status = _hayes_command_callback(command)) == HCS_ACTIVE)
_hayes_parse_trigger(modem, hayes->rd_buf, command); _hayes_parse_trigger(modem, line, command);
else if(status == HCS_SUCCESS || status == HCS_ERROR if((status = _hayes_command_get_status(command)) == HCS_SUCCESS
|| status == HCS_TIMEOUT) || status == HCS_ERROR || status == HCS_TIMEOUT)
{ {
_hayes_queue_pop(modem); _hayes_queue_pop(modem);
_hayes_queue_push(modem); _hayes_queue_push(modem);
@ -1542,7 +1548,10 @@ static int _hayes_queue_command(ModemPlugin * modem, HayesCommand * command)
switch(hayes->mode) switch(hayes->mode)
{ {
case HAYES_MODE_INIT: case HAYES_MODE_INIT:
/* FIXME should ignore commands sent at this point */ /* ignore commands besides initialization */
if(_hayes_command_get_priority(command)
!= HCP_IMMEDIATE)
return -1;
case HAYES_MODE_COMMAND: case HAYES_MODE_COMMAND:
case HAYES_MODE_DATA: case HAYES_MODE_DATA:
queue = hayes->queue; queue = hayes->queue;
@ -1696,6 +1705,17 @@ static void _hayes_reset(ModemPlugin * modem)
} }
/* hayes_request_type */
static int _hayes_request_type(ModemPlugin * modem, ModemRequestType type)
{
ModemRequest request;
memset(&request, 0, sizeof(request));
request.type = type;
return _hayes_request(modem, &request);
}
/* hayes_reset_start */ /* hayes_reset_start */
static void _hayes_reset_start(ModemPlugin * modem, unsigned int retry) static void _hayes_reset_start(ModemPlugin * modem, unsigned int retry)
{ {
@ -1735,10 +1755,10 @@ static void _hayes_reset_stop(ModemPlugin * modem)
modem->helper->event(modem->helper->modem, event); modem->helper->event(modem->helper->modem, event);
} }
/* remove registration data */ /* remove registration data */
string_delete(hayes->registration_media); free(hayes->registration_media);
hayes->registration_media = NULL; hayes->registration_media = NULL;
event->registration.media = NULL; event->registration.media = NULL;
string_delete(hayes->registration_operator); free(hayes->registration_operator);
hayes->registration_operator = NULL; hayes->registration_operator = NULL;
event->registration._operator = NULL; event->registration._operator = NULL;
event->registration.signal = 0.0 / 0.0; event->registration.signal = 0.0 / 0.0;
@ -1854,6 +1874,20 @@ static char * _hayes_command_get_line(HayesCommand * command,
#endif #endif
/* hayes_command_get_priority */
static HayesCommandPriority _hayes_command_get_priority(HayesCommand * command)
{
return command->priority;
}
/* hayes_command_get_status */
static HayesCommandStatus _hayes_command_get_status(HayesCommand * command)
{
return command->status;
}
/* hayes_command_get_timeout */ /* hayes_command_get_timeout */
static unsigned int _hayes_command_get_timeout(HayesCommand * command) static unsigned int _hayes_command_get_timeout(HayesCommand * command)
{ {
@ -1890,6 +1924,7 @@ static void _hayes_command_set_status(HayesCommand * command,
HayesCommandStatus status) HayesCommandStatus status)
{ {
command->status = status; command->status = status;
_hayes_command_callback(command);
} }
@ -1910,9 +1945,9 @@ static int _hayes_command_answer_append(HayesCommand * command,
if(answer == NULL) if(answer == NULL)
return 0; return 0;
if(command->answer == NULL) if(command->answer == NULL)
p = string_new_append(answer, "\n", NULL); p = string_new(answer);
else else
p = string_new_append(command->answer, answer, "\n", NULL); p = string_new_append(command->answer, "\n", answer, NULL);
if(p == NULL) if(p == NULL)
return -1; return -1;
free(command->answer); free(command->answer);
@ -2149,44 +2184,43 @@ static HayesCommandStatus _on_reset_callback(HayesCommand * command,
{ {
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
Hayes * hayes = modem->priv; Hayes * hayes = modem->priv;
ModemRequest request;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__); fprintf(stderr, "DEBUG: %s(%u)\n", __func__, status);
#endif #endif
if(hayes->timeout != 0) if(hayes->timeout != 0)
g_source_remove(hayes->timeout); g_source_remove(hayes->timeout);
hayes->timeout = 0; hayes->timeout = 0;
switch(status) switch(status)
{ {
case HCS_PENDING: /* should not happen */ case HCS_PENDING: /* ignore */
case HCS_QUEUED: case HCS_QUEUED:
case HCS_SUCCESS: return status;
case HCS_ERROR:
break;
case HCS_ACTIVE: case HCS_ACTIVE:
/* a reply was obtained */ /* a reply was obtained */
status = _on_request_generic(command, status, modem); if((status = _on_request_generic(command, status, priv))
if(status != HCS_SUCCESS && status != HCS_ERROR) != HCS_SUCCESS)
break; return status;
_hayes_set_mode(modem, HAYES_MODE_COMMAND); _hayes_set_mode(modem, HAYES_MODE_COMMAND);
request.type = HAYES_REQUEST_LOCAL_ECHO_DISABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_LOCAL_ECHO_DISABLE);
request.type = HAYES_REQUEST_VERBOSE_ENABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_VERBOSE_ENABLE);
request.type = HAYES_REQUEST_MODEL; _hayes_request_type(modem, HAYES_REQUEST_MODEL);
_hayes_request(modem, &request); _hayes_request_type(modem,
request.type = HAYES_REQUEST_EXTENDED_ERRORS; HAYES_REQUEST_EXTENDED_ERRORS);
_hayes_request(modem, &request); _hayes_request_type(modem, HAYES_REQUEST_FUNCTIONAL);
request.type = HAYES_REQUEST_FUNCTIONAL;
_hayes_request(modem, &request);
return HCS_SUCCESS; return HCS_SUCCESS;
case HCS_TIMEOUT: case HCS_TIMEOUT:
case HCS_ERROR:
break;
case HCS_SUCCESS: /* should not happen */
status = HCS_ERROR;
break; break;
} }
/* try again */ /* try again */
_reset_settle(modem); _reset_settle(modem);
return HCS_ERROR; /* destroy and queue again */ return status;
} }
@ -2204,7 +2238,6 @@ static gboolean _on_timeout(gpointer data)
if(hayes->queue == NULL || (command = hayes->queue->data) == NULL) if(hayes->queue == NULL || (command = hayes->queue->data) == NULL)
return FALSE; return FALSE;
_hayes_command_set_status(command, HCS_TIMEOUT); _hayes_command_set_status(command, HCS_TIMEOUT);
_hayes_command_callback(command);
_hayes_queue_pop(modem); _hayes_queue_pop(modem);
_hayes_queue_push(modem); _hayes_queue_push(modem);
return FALSE; return FALSE;
@ -2428,9 +2461,7 @@ static HayesCommandStatus _on_request_authenticate(HayesCommand * command,
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
Hayes * hayes = modem->priv; Hayes * hayes = modem->priv;
ModemEvent * event = &hayes->events[MODEM_EVENT_TYPE_AUTHENTICATION]; ModemEvent * event = &hayes->events[MODEM_EVENT_TYPE_AUTHENTICATION];
ModemRequest request;
memset(&request, 0, sizeof(request));
switch((status = _on_request_generic(command, status, priv))) switch((status = _on_request_generic(command, status, priv)))
{ {
case HCS_SUCCESS: case HCS_SUCCESS:
@ -2447,11 +2478,8 @@ static HayesCommandStatus _on_request_authenticate(HayesCommand * command,
event->authentication.name) == 0 event->authentication.name) == 0
|| strcmp("SIM PUK", || strcmp("SIM PUK",
event->authentication.name) == 0)) event->authentication.name) == 0))
{
/* verify that it really worked */ /* verify that it really worked */
request.type = HAYES_REQUEST_SIM_PIN_VALID; _hayes_request_type(modem, HAYES_REQUEST_SIM_PIN_VALID);
_hayes_request(modem, &request);
}
else else
{ {
event->authentication.status = MODEM_AUTHENTICATION_STATUS_OK; event->authentication.status = MODEM_AUTHENTICATION_STATUS_OK;
@ -2534,14 +2562,11 @@ static HayesCommandStatus _on_request_call_status(HayesCommand * command,
HayesCommandStatus status, void * priv) HayesCommandStatus status, void * priv)
{ {
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
ModemRequest request;
if((status = _on_request_generic(command, status, priv)) != HCS_SUCCESS if((status = _on_request_generic(command, status, priv)) != HCS_SUCCESS
&& status != HCS_ERROR) && status != HCS_ERROR)
return status; return status;
memset(&request, 0, sizeof(request)); _hayes_request_type(modem, HAYES_REQUEST_PHONE_ACTIVE);
request.type = HAYES_REQUEST_PHONE_ACTIVE;
_hayes_request(modem, &request);
return status; return status;
} }
@ -2575,15 +2600,13 @@ static HayesCommandStatus _on_request_functional(HayesCommand * command,
HayesCommandStatus status, void * priv) HayesCommandStatus status, void * priv)
{ {
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
ModemRequest request;
memset(&request, 0, sizeof(request));
switch((status = _on_request_generic(command, status, priv))) switch((status = _on_request_generic(command, status, priv)))
{ {
case HCS_ERROR: case HCS_ERROR:
/* try to enable */ /* try to enable */
request.type = HAYES_REQUEST_FUNCTIONAL_ENABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_FUNCTIONAL_ENABLE);
break; break;
default: default:
break; break;
@ -2597,23 +2620,21 @@ static HayesCommandStatus _on_request_functional_enable(HayesCommand * command,
HayesCommandStatus status, void * priv) HayesCommandStatus status, void * priv)
{ {
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
ModemRequest request;
memset(&request, 0, sizeof(request));
switch((status = _on_request_generic(command, status, priv))) switch((status = _on_request_generic(command, status, priv)))
{ {
case HCS_ERROR: case HCS_ERROR:
/* force a reset */ /* force a reset */
request.type = HAYES_REQUEST_FUNCTIONAL_ENABLE_RESET; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_FUNCTIONAL_ENABLE_RESET);
break; break;
case HCS_SUCCESS: case HCS_SUCCESS:
_on_trigger_cfun(modem, "1"); /* XXX ugly workaround */ _on_trigger_cfun(modem, "1"); /* XXX ugly workaround */
break; break;
case HCS_TIMEOUT: case HCS_TIMEOUT:
/* repeat request */ /* repeat request */
request.type = HAYES_REQUEST_FUNCTIONAL_ENABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_FUNCTIONAL_ENABLE);
break; break;
default: default:
break; break;
@ -2627,9 +2648,7 @@ static HayesCommandStatus _on_request_functional_enable_reset(
HayesCommand * command, HayesCommandStatus status, void * priv) HayesCommand * command, HayesCommandStatus status, void * priv)
{ {
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
ModemRequest request;
memset(&request, 0, sizeof(request));
switch((status = _on_request_generic(command, status, priv))) switch((status = _on_request_generic(command, status, priv)))
{ {
case HCS_SUCCESS: case HCS_SUCCESS:
@ -2637,8 +2656,8 @@ static HayesCommandStatus _on_request_functional_enable_reset(
break; break;
case HCS_TIMEOUT: case HCS_TIMEOUT:
/* repeat request */ /* repeat request */
request.type = HAYES_REQUEST_FUNCTIONAL_ENABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_FUNCTIONAL_ENABLE);
break; break;
default: default:
break; break;
@ -2653,18 +2672,12 @@ static HayesCommandStatus _on_request_generic(HayesCommand * command,
{ {
char const * answer; char const * answer;
if(status == HCS_TIMEOUT) if(status != HCS_ACTIVE)
return HCS_TIMEOUT; return status;
else if(status != HCS_ACTIVE) /* XXX should not happen */ for(answer = _hayes_command_get_answer(command); answer != NULL;)
return HCS_ERROR; if(strcmp(answer, "OK") == 0)
if((answer = _hayes_command_get_answer(command)) == NULL)
return HCS_ERROR;
while(answer != NULL)
if(strncmp(answer, "OK\n", 3) == 0
|| strncmp(answer, "OK\r\n", 4) == 0)
return HCS_SUCCESS; return HCS_SUCCESS;
else if(strncmp(answer, "ERROR\n", 6) == 0 else if(strcmp(answer, "ERROR") == 0)
|| strncmp(answer, "ERROR\r\n", 7) == 0)
return HCS_ERROR; return HCS_ERROR;
else if((answer = strchr(answer, '\n')) != NULL) else if((answer = strchr(answer, '\n')) != NULL)
answer++; answer++;
@ -2750,14 +2763,45 @@ static HayesCommandStatus _on_request_registration(HayesCommand * command,
HayesCommandStatus status, void * priv) HayesCommandStatus status, void * priv)
{ {
ModemPlugin * modem = priv; ModemPlugin * modem = priv;
ModemRequest request;
if((status = _on_request_generic(command, status, priv)) != HCS_SUCCESS) if((status = _on_request_generic(command, status, priv)) != HCS_SUCCESS)
return status; return status;
/* force a registration status */ /* force a registration status */
memset(&request, 0, sizeof(request)); _hayes_request_type(modem, HAYES_REQUEST_REGISTRATION);
request.type = HAYES_REQUEST_REGISTRATION; return status;
_hayes_request(modem, &request); }
/* on_request_registration_automatic */
static HayesCommandStatus _on_request_registration_automatic(
HayesCommand * command, HayesCommandStatus status, void * priv)
{
ModemPlugin * modem = priv;
Hayes * hayes = modem->priv;
ModemEvent * event = &hayes->events[MODEM_EVENT_TYPE_REGISTRATION];
if((status = _on_request_generic(command, status, priv)) != HCS_SUCCESS)
return status;
event->registration.mode = MODEM_REGISTRATION_MODE_AUTOMATIC;
/* force a registration status */
_hayes_request_type(modem, HAYES_REQUEST_REGISTRATION);
return status;
}
/* on_request_registration_disabled */
static HayesCommandStatus _on_request_registration_disabled(
HayesCommand * command, HayesCommandStatus status, void * priv)
{
ModemPlugin * modem = priv;
Hayes * hayes = modem->priv;
ModemEvent * event = &hayes->events[MODEM_EVENT_TYPE_REGISTRATION];
if((status = _on_request_generic(command, status, priv)) != HCS_SUCCESS)
return status;
event->registration.mode = MODEM_REGISTRATION_MODE_DISABLED;
/* force a registration status */
_hayes_request_type(modem, HAYES_REQUEST_REGISTRATION);
return status; return status;
} }
@ -2786,37 +2830,30 @@ static HayesCommandStatus _on_request_sim_pin_valid(HayesCommand * command,
if(event->authentication.status != MODEM_AUTHENTICATION_STATUS_OK) if(event->authentication.status != MODEM_AUTHENTICATION_STATUS_OK)
return status; return status;
/* apply useful settings */ /* apply useful settings */
_hayes_request_type(modem, HAYES_REQUEST_EXTENDED_ERRORS);
_hayes_request_type(modem, HAYES_REQUEST_EXTENDED_RING_REPORTS);
memset(&request, 0, sizeof(request)); memset(&request, 0, sizeof(request));
request.type = HAYES_REQUEST_EXTENDED_ERRORS;
_hayes_request(modem, &request);
request.type = HAYES_REQUEST_EXTENDED_RING_REPORTS;
_hayes_request(modem, &request);
request.type = MODEM_REQUEST_CALL_PRESENTATION; request.type = MODEM_REQUEST_CALL_PRESENTATION;
request.call_presentation.enabled = 1; request.call_presentation.enabled = 1;
_hayes_request(modem, &request); _hayes_request(modem, &request);
request.type = HAYES_REQUEST_CALL_WAITING_UNSOLLICITED_ENABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_CALL_WAITING_UNSOLLICITED_ENABLE);
request.type = HAYES_REQUEST_CONNECTED_LINE_ENABLE; _hayes_request_type(modem, HAYES_REQUEST_CONNECTED_LINE_ENABLE);
_hayes_request(modem, &request); _hayes_request_type(modem,
request.type = HAYES_REQUEST_REGISTRATION_UNSOLLICITED_ENABLE; HAYES_REQUEST_REGISTRATION_UNSOLLICITED_ENABLE);
_hayes_request(modem, &request);
/* report new messages */ /* report new messages */
request.type = HAYES_REQUEST_MESSAGE_UNSOLLICITED_ENABLE; _hayes_request_type(modem, HAYES_REQUEST_MESSAGE_UNSOLLICITED_ENABLE);
_hayes_request(modem, &request);
/* report new notifications */ /* report new notifications */
request.type = HAYES_REQUEST_SUPPLEMENTARY_SERVICE_DATA_ENABLE; _hayes_request_type(modem,
_hayes_request(modem, &request); HAYES_REQUEST_SUPPLEMENTARY_SERVICE_DATA_ENABLE);
/* refresh the registration status */ /* refresh the registration status */
request.type = HAYES_REQUEST_REGISTRATION; _hayes_request_type(modem, HAYES_REQUEST_REGISTRATION);
_hayes_request(modem, &request);
/* refresh the current call status */ /* refresh the current call status */
_hayes_trigger(modem, MODEM_EVENT_TYPE_CALL); _hayes_trigger(modem, MODEM_EVENT_TYPE_CALL);
/* refresh the contact list */ /* refresh the contact list */
request.type = MODEM_REQUEST_CONTACT_LIST; _hayes_request_type(modem, MODEM_REQUEST_CONTACT_LIST);
_hayes_request(modem, &request);
/* refresh the message list */ /* refresh the message list */
request.type = MODEM_REQUEST_MESSAGE_LIST; _hayes_request_type(modem, MODEM_REQUEST_MESSAGE_LIST);
_hayes_request(modem, &request);
return status; return status;
} }
@ -2838,10 +2875,7 @@ static void _on_trigger_call_error(ModemPlugin * modem, char const * answer)
: NULL; : NULL;
if(command != NULL) if(command != NULL)
{
_hayes_command_set_status(command, HCS_ERROR); _hayes_command_set_status(command, HCS_ERROR);
_hayes_command_callback(command);
}
_hayes_trigger(modem, MODEM_EVENT_TYPE_CALL); _hayes_trigger(modem, MODEM_EVENT_TYPE_CALL);
} }
@ -3035,13 +3069,9 @@ static void _on_trigger_cme_error(ModemPlugin * modem, char const * answer)
unsigned int u; unsigned int u;
HayesCommand * p; HayesCommand * p;
ModemEvent * event; ModemEvent * event;
ModemRequest request;
if(command != NULL) if(command != NULL)
{
_hayes_command_set_status(command, HCS_ERROR); _hayes_command_set_status(command, HCS_ERROR);
_hayes_command_callback(command);
}
if(sscanf(answer, "%u", &u) != 1) if(sscanf(answer, "%u", &u) != 1)
return; return;
switch(u) switch(u)
@ -3080,9 +3110,7 @@ static void _on_trigger_cme_error(ModemPlugin * modem, char const * answer)
= MODEM_REGISTRATION_STATUS_REGISTERED; = MODEM_REGISTRATION_STATUS_REGISTERED;
helper->event(helper->modem, event); helper->event(helper->modem, event);
/* verify the SIM card */ /* verify the SIM card */
memset(&request, 0, sizeof(request)); _hayes_request_type(modem, HAYES_REQUEST_SIM_PIN_VALID);
request.type = HAYES_REQUEST_SIM_PIN_VALID;
_hayes_request(modem, &request);
break; break;
default: /* FIXME implement the rest */ default: /* FIXME implement the rest */
case 3: /* operation not allowed */ case 3: /* operation not allowed */
@ -3168,7 +3196,7 @@ static void _on_trigger_cmgr(ModemPlugin * modem, char const * answer)
{ {
number[sizeof(number) - 1] = '\0'; number[sizeof(number) - 1] = '\0';
string_delete(hayes->message_number); string_delete(hayes->message_number);
hayes->message_number = string_new(number); hayes->message_number = strdup(number);
event->message.number = hayes->message_number; event->message.number = hayes->message_number;
date[sizeof(date) - 1] = '\0'; date[sizeof(date) - 1] = '\0';
if(strptime(date, "%y/%m/%d,%H:%M:%S", &t) == NULL) if(strptime(date, "%y/%m/%d,%H:%M:%S", &t) == NULL)
@ -3467,10 +3495,7 @@ static void _on_trigger_cms_error(ModemPlugin * modem, char const * answer)
HayesCommand * p; HayesCommand * p;
if(command != NULL) if(command != NULL)
{
_hayes_command_set_status(command, HCS_ERROR); _hayes_command_set_status(command, HCS_ERROR);
_hayes_command_callback(command);
}
if(sscanf(answer, "%u", &u) != 1) if(sscanf(answer, "%u", &u) != 1)
return; return;
switch(u) switch(u)
@ -3605,7 +3630,6 @@ static void _on_trigger_cops(ModemPlugin * modem, char const * answer)
unsigned int v = 0; unsigned int v = 0;
char buf[32] = ""; char buf[32] = "";
unsigned int w; unsigned int w;
ModemRequest request;
if(sscanf(answer, "%u,%u,\"%31[^\"]\",%u", &u, &v, buf, &w) < 1) if(sscanf(answer, "%u,%u,\"%31[^\"]\",%u", &u, &v, buf, &w) < 1)
return; return;
@ -3627,12 +3651,8 @@ static void _on_trigger_cops(ModemPlugin * modem, char const * answer)
} }
event->registration.mode = u; event->registration.mode = u;
if(v != 0) if(v != 0)
{
/* force alphanumeric format */ /* force alphanumeric format */
memset(&request, 0, sizeof(request)); _hayes_request_type(modem, HAYES_REQUEST_OPERATOR_FORMAT_LONG);
request.type = HAYES_REQUEST_OPERATOR_FORMAT_LONG;
_hayes_request(modem, &request);
}
else else
{ {
buf[sizeof(buf) - 1] = '\0'; buf[sizeof(buf) - 1] = '\0';
@ -3769,7 +3789,6 @@ static void _on_trigger_creg(ModemPlugin * modem, char const * answer)
ModemEvent * event = &hayes->events[MODEM_EVENT_TYPE_REGISTRATION]; ModemEvent * event = &hayes->events[MODEM_EVENT_TYPE_REGISTRATION];
int res; int res;
unsigned int u[4] = { 0, 0, 0, 0 }; unsigned int u[4] = { 0, 0, 0, 0 };
ModemRequest request;
res = sscanf(answer, "%u,%u,%X,%X", &u[0], &u[1], &u[2], &u[3]); res = sscanf(answer, "%u,%u,%X,%X", &u[0], &u[1], &u[2], &u[3]);
if(res == 1) if(res == 1)
@ -3824,13 +3843,9 @@ static void _on_trigger_creg(ModemPlugin * modem, char const * answer)
{ {
case MODEM_REGISTRATION_STATUS_REGISTERED: case MODEM_REGISTRATION_STATUS_REGISTERED:
/* refresh registration data */ /* refresh registration data */
memset(&request, 0, sizeof(request)); _hayes_request_type(modem, HAYES_REQUEST_GPRS_ATTACHED);
request.type = HAYES_REQUEST_GPRS_ATTACHED; _hayes_request_type(modem, HAYES_REQUEST_OPERATOR);
_hayes_request(modem, &request); _hayes_request_type(modem, MODEM_REQUEST_SIGNAL_LEVEL);
request.type = HAYES_REQUEST_OPERATOR;
_hayes_request(modem, &request);
request.type = MODEM_REQUEST_SIGNAL_LEVEL;
_hayes_request(modem, &request);
break; break;
default: default:
free(hayes->registration_media); free(hayes->registration_media);