qmimodem: support wake on SMS receive
PATCH 1/1 qmimodem: support wake on SMS receive
From: Tom Nguyen <[email protected]>
Some modems, like Quectel EC25, support waking the host platform from a suspend state upon an SMS reception. Current SMS handling configures the modem to not save incoming messages. On platforms where the modem's interfaces (eg, cdc-wdm, wwan, etc) disconnect on suspend and reconnect on resume, oFono will re-initialize. This can cause lost messages upon resume because the QMI indication is sent before oFono is ready.
Changes to support suspend/resume with wake on SMS:
- On startup:
1. Configure modem to save messages to NV and notify for class NONE.
2. Delete all processed messages in modem memory to free space.
3. Get list of unread messages, then get and delete each.
- After startup:
1. Process message upon event indication and delete.
2. Then check for possibly more messages to process.
--- drivers/qmimodem/sms.c | 338 ++++++++++++++++++++++++++++++++++------- drivers/qmimodem/wms.h | 77 +++++++--- 2 files changed, 341 insertions(+), 74 deletions(-)
drivers/qmimodem/sms.c +281 -57
@@ -41,6 +41,14 @@ struct sms_data {uint16_t minor; }; +static struct qmi_wms_read_msg_id rd_msg_id = {0 }; +static uint8_t msg_mode =QMI_WMS_MESSAGE_MODE_GSMWCDMA ; +static bool msg_chk_all; +static bool msg_list_chk; + +static void get_msg_list (struct ofono_sms *sms); + +static void get_smsc_addr_cb (struct qmi_result *result,void *user_data) {struct cb_data *cbd = user_data;
@@ -334,21 +342,80 @@ error:g_free (cbd); } -static void raw_read_cb (struct qmi_result *result,void *user_data) +static void delete_msg_cb (struct qmi_result *result,void *user_data) {struct ofono_sms *sms = user_data; -const struct qmi_wms_raw_message * msg; -uint16_t len; -uint16_t error; +uint16_t err; -if (qmi_result_set_error (result, &error)) { -DBG ("Raw read error: %d (%s)" , error, -qmi_result_get_error (result)); +DBG ("" ); + +if (qmi_result_set_error (result, &err)) +DBG ("Err: delete %d - %s" , err,qmi_result_get_error (result)); +else { +DBG ("msg deleted" ); + +/* check for more messages */ +get_msg_list (sms); + } +} + +static void delete_msg (struct ofono_sms *sms,uint8_t tag) +{ +struct sms_data *data =ofono_sms_get_data (sms); +struct qmi_param *param; +qmi_result_func_t func =NULL ; + +DBG ("" ); + + param =qmi_param_new (); +if (param ==NULL ) + goto done; + +qmi_param_append_uint8 (param,QMI_WMS_PARAM_DEL_STORE , +QMI_WMS_STORAGE_TYPE_NV ); + +if (tag ==QMI_WMS_MT_UNDEFINE ) { +DBG ("delete read msg type %d ndx %d" , rd_msg_id.type, + rd_msg_id.ndx); + +/* delete 1 msg */ +qmi_param_append_uint32 (param,QMI_WMS_PARAM_DEL_NDX , + rd_msg_id.ndx); + func = delete_msg_cb; + }else { +DBG ("delete msg tag %d mode %d" , tag, msg_mode); + +/* delete all msgs from 1 tag type */ +qmi_param_append_uint8 (param,QMI_WMS_PARAM_DEL_TYPE , tag); + } + +qmi_param_append_uint8 (param,QMI_WMS_PARAM_DEL_MODE , msg_mode); + +if (qmi_service_send (data->wms,QMI_WMS_DELETE , param, + func, sms,NULL ) >0 )return ; + +qmi_param_free (param); + +done: +return ; +} + +static void raw_read_cb (struct qmi_result *result,void *user_data) +{ +struct ofono_sms *sms = user_data; +const struct qmi_wms_raw_message *msg; +uint16_t err; + +DBG ("" ); + +if (qmi_result_set_error (result, &err)) { +DBG ("Err: read %d - %s" , err,qmi_result_get_error (result)); + goto done; }/* Raw message data */ - msg =qmi_result_get (result,0x01 , &len); + msg =qmi_result_get (result,QMI_WMS_RESULT_READ_MSG ,NULL );if (msg) {uint16_t plen;uint16_t tpdu_len;
@@ -357,66 +424,204 @@ static void raw_read_cb(struct qmi_result *result, void *user_data) tpdu_len = plen - msg->msg_data[0 ] -1 ;ofono_sms_deliver_notify (sms, msg->msg_data, plen, tpdu_len); + }else +DBG ("Err: no data in type %d ndx %d" , rd_msg_id.type, + rd_msg_id.ndx); + +/* delete read msg */ +delete_msg (sms,QMI_WMS_MT_UNDEFINE ); + +done: +return ; +} + +static void raw_read (struct ofono_sms *sms,uint8_t type,uint32_t ndx) +{ +struct sms_data *data =ofono_sms_get_data (sms); +struct qmi_param *param; + +DBG ("" ); + + param =qmi_param_new (); +if (param ==NULL ) + goto done; + + rd_msg_id.type = type; + rd_msg_id.ndx = ndx; + +DBG ("read type %d ndx %d" , rd_msg_id.type, rd_msg_id.ndx); + +qmi_param_append (param,QMI_WMS_PARAM_READ_MSG ,sizeof (rd_msg_id), + &rd_msg_id); +qmi_param_append_uint8 (param,QMI_WMS_PARAM_READ_MODE , msg_mode); + +if (qmi_service_send (data->wms,QMI_WMS_RAW_READ , param, + raw_read_cb, sms,NULL ) >0 ) +return ; + +qmi_param_free (param); + +done: +return ; +} + +static void get_msg_list_cb (struct qmi_result *result,void *user_data) +{ +struct ofono_sms *sms = user_data; +struct sms_data *data =ofono_sms_get_data (sms); +struct qmi_param *param; +const struct qmi_wms_result_msg_list *list; +uint32_t cnt =0 ; +uint16_t tmp; + +DBG ("" ); + +if (qmi_result_set_error (result, &tmp)) { +DBG ("Err: get msg list %d - %s" , tmp, +qmi_result_get_error (result)); + goto done; + } + + list =qmi_result_get (result,QMI_WMS_RESULT_MSG_LIST ,NULL ); +if (list ==NULL ) { +DBG ("Err: get msg list empty" ); + goto done; + } + + cnt =GUINT32_FROM_LE (list->cnt); +DBG ("msgs found %d" , cnt); + +for (tmp =0 ; tmp < cnt; tmp++) { +DBG ("unread type %d ndx %d" , list->msg[tmp].type, +GUINT32_FROM_LE (list->msg[tmp].ndx)); + } + +/* get 1st msg in list */ +if (cnt) +raw_read (sms, list->msg[0 ].type, +GUINT32_FROM_LE (list->msg[0 ].ndx)); +else + msg_list_chk = false; + +done: +/* check other protocol if needed */ +if (msg_chk_all && (cnt ==0 )) { + msg_chk_all = false; + msg_mode =QMI_WMS_MESSAGE_MODE_GSMWCDMA ; +get_msg_list (sms); + } +} + +static void get_msg_list (struct ofono_sms *sms) +{ +struct sms_data *data =ofono_sms_get_data (sms); +struct qmi_param *param; + +DBG ("" ); + + param =qmi_param_new (); +if (param ==NULL ) + goto done; + + msg_list_chk = true; + +/* query NOT_READ msg list */ +qmi_param_append_uint8 (param,QMI_WMS_PARAM_STORAGE_TYPE , +QMI_WMS_STORAGE_TYPE_NV ); +qmi_param_append_uint8 (param,QMI_WMS_PARAM_TAG_TYPE , +QMI_WMS_MT_NOT_READ ); +qmi_param_append_uint8 (param,QMI_WMS_PARAM_MESSAGE_MODE , msg_mode); + +if (qmi_service_send (data->wms,QMI_WMS_GET_MSG_LIST , param, + get_msg_list_cb, sms,NULL ) >0 ) +return ; + +qmi_param_free (param); + +done: +return ; +} + +static void get_msg_protocol_cb (struct qmi_result *result,void *user_data) +{ +struct ofono_sms *sms = user_data; +uint16_t err; + +DBG ("" ); + +if (qmi_result_set_error (result, &err) && + (err !=QMI_ERR_OP_DEVICE_UNSUPPORTED )) { +DBG ("Err: protocol %d - %s" , err,qmi_result_get_error (result)); + goto done; + } + +if (err !=QMI_ERR_OP_DEVICE_UNSUPPORTED ) { +/* modem supports only 1 protocol */ +qmi_result_get_uint8 (result,QMI_WMS_PARAM_PROTOCOL , &msg_mode); }else { -DBG ("No message data available at requested position" ); +/* check both, start with 1 then switch to other */ +DBG ("device supports CDMA and WCDMA msg protocol" ); + msg_chk_all = true; + msg_mode =QMI_WMS_MESSAGE_MODE_CDMA ; } + +/* check for messages */ +get_msg_list (sms); + +done: +return ; +} + +static void get_msg_protocol (struct ofono_sms *sms) +{ +struct sms_data *data =ofono_sms_get_data (sms); + +DBG ("" ); + +qmi_service_send (data->wms,QMI_WMS_GET_MSG_PROTOCOL ,NULL , + get_msg_protocol_cb, sms,NULL ); }static void event_notify (struct qmi_result *result,void *user_data) {struct ofono_sms *sms = user_data; -struct sms_data *data =ofono_sms_get_data (sms);const struct qmi_wms_result_new_msg_notify *notify; -const struct qmi_wms_result_message *message; -uint16_t len;DBG ("" ); - notify =qmi_result_get (result,QMI_WMS_RESULT_NEW_MSG_NOTIFY , &len); + notify =qmi_result_get (result,QMI_WMS_RESULT_NEW_MSG_NOTIFY ,NULL );if (notify) { -DBG ("storage type %d index %d" , notify->storage_type, -GUINT32_FROM_LE (notify->storage_index)); - } +/* route is store and notify */ +if (!qmi_result_get_uint8 (result,QMI_WMS_RESULT_MSG_MODE , + &msg_mode)) +DBG ("msg mode not found, use mode %d" , msg_mode); + +DBG ("msg type %d ndx %d mode %d" , notify->storage_type, +GUINT32_FROM_LE (notify->storage_index), msg_mode); + +/* don't read if list is being processed, will find this msg */ +if (!msg_list_chk) +raw_read (sms, notify->storage_type, +GUINT32_FROM_LE (notify->storage_index)); + }else { +const struct qmi_wms_result_message *message; - message =qmi_result_get (result,QMI_WMS_RESULT_MESSAGE , &len); -if (message) { -uint16_t plen; + message =qmi_result_get (result,QMI_WMS_RESULT_MESSAGE ,NULL ); +if (message) { +/* route is either transfer only or transfer and ACK */ +uint16_t plen; - plen =GUINT16_FROM_LE (message->msg_length); + plen =GUINT16_FROM_LE (message->msg_length); -DBG ("ack_required %d transaction id %u" , message->ack_required, +DBG ("ack_required %d transaction id %u" , + message->ack_required,GUINT32_FROM_LE (message->transaction_id)); -DBG ("msg format %d PDU length %d" , message->msg_format, plen); +DBG ("msg format %d PDU length %d" , + message->msg_format, plen); -ofono_sms_deliver_notify (sms, message->msg_data, plen, plen); - }else { -/* The Quectel EC21, at least, does not provide the -* message data in the event notification, so a 'raw read' -* needs to be issued in order to query the message itself -*/ -struct qmi_param *param; - - param =qmi_param_new (); -if (!param) -return ; - -/* Message memory storage ID */ -qmi_param_append (param,0x01 ,sizeof (*notify), notify); -/* The 'message mode' parameter is documented as optional, -* but the Quectel EC21 errors out with error 17 (missing -* argument) if it is not provided... we default to 3GPP -* here because that's what works for me and it's not clear -* how to actually query what this should be otherwise... -*/ -/* Message mode */ -qmi_param_append_uint8 (param,0x10 , -QMI_WMS_MESSAGE_MODE_GSMWCDMA ); - -if (qmi_service_send (data->wms,QMI_WMS_RAW_READ , param, - raw_read_cb, sms,NULL ) >0 ) -return ; - -qmi_param_free (param); +ofono_sms_deliver_notify (sms, message->msg_data, + plen, plen); + } } }
@@ -427,6 +632,25 @@ static void set_routes_cb(struct qmi_result *result, void *user_data)DBG ("" );ofono_sms_register (sms); + +/* +* Modem storage is limited. As a fail safe, delete processed messages +* to free device memory to prevent blockage of new messages. +*/ + msg_mode =QMI_WMS_MESSAGE_MODE_CDMA ; +delete_msg (sms,QMI_WMS_MT_READ ); +delete_msg (sms,QMI_WMS_MO_SENT ); + msg_mode =QMI_WMS_MESSAGE_MODE_GSMWCDMA ; +delete_msg (sms,QMI_WMS_MT_READ ); +delete_msg (sms,QMI_WMS_MO_SENT ); + +/* +* Subsystem initialized, now start process to check for unread +* messages. First, query msg protocol/mode. If modem supports both +* modes, then check messages for both modes since there's no way to +* query which mode is active. +*/ +get_msg_protocol (sms); }static void get_routes_cb (struct qmi_result *result,void *user_data)
@@ -445,7 +669,7 @@ static void get_routes_cb(struct qmi_result *result, void *user_data) goto done; list =qmi_result_get (result,QMI_WMS_RESULT_ROUTE_LIST , &len); -if (!list) +if (!list) goto done; num =GUINT16_FROM_LE (list->count);
@@ -468,8 +692,8 @@ static void get_routes_cb(struct qmi_result *result, void *user_data) new_list->count =GUINT16_TO_LE (1 ); new_list->route[0 ].msg_type =QMI_WMS_MSG_TYPE_P2P ; new_list->route[0 ].msg_class =QMI_WMS_MSG_CLASS_NONE ; - new_list->route[0 ].storage_type =QMI_WMS_STORAGE_TYPE_NONE ; - new_list->route[0 ].action =QMI_WMS_ACTION_TRANSFER_AND_ACK ; + new_list->route[0 ].storage_type =QMI_WMS_STORAGE_TYPE_NV ; + new_list->route[0 ].action =QMI_WMS_ACTION_STORE_AND_NOTIFY ; param =qmi_param_new ();if (!param)
@@ -478,9 +702,9 @@ static void get_routes_cb(struct qmi_result *result, void *user_data)qmi_param_append (param,QMI_WMS_PARAM_ROUTE_LIST , len, new_list);qmi_param_append_uint8 (param,QMI_WMS_PARAM_STATUS_REPORT ,0x01 ); -if (qmi_service_send (data->wms,QMI_WMS_SET_ROUTES , param, - set_routes_cb, sms,NULL ) >0 ) -return ; +if (qmi_service_send (data->wms,QMI_WMS_SET_ROUTES , param, + set_routes_cb, sms,NULL ) >0 ) +return ;qmi_param_free (param);
drivers/qmimodem/wms.h +60 -17
114 lines not on this page · raw message
Signature · 1 line
2.20.1
Hi Tom/Joshua,
Quoted 8 lines · Joshua Watt
From: Tom Nguyen <[email protected]>
Some modems, like Quectel EC25, support waking the host platform from a suspend state upon an SMS reception. Current SMS handling configures the modem to not save incoming messages. On platforms where the modem's interfaces (eg, cdc-wdm, wwan, etc) disconnect on suspend and reconnect on resume, oFono will re-initialize. This can cause lost messages upon resume because the QMI indication is sent before oFono is ready.
So doesn't this also cause us to bring the modem offline when we wake up from resume?
Quoted 9 lines
Changes to support suspend/resume with wake on SMS:
- On startup:
1. Configure modem to save messages to NV and notify for class NONE.
2. Delete all processed messages in modem memory to free space.
3. Get list of unread messages, then get and delete each.
- After startup:
1. Process message upon event indication and delete.
2. Then check for possibly more messages to process.
Quoted 5 lines
--- drivers/qmimodem/sms.c | 338 ++++++++++++++++++++++++++++++++++------- drivers/qmimodem/wms.h | 77 +++++++--- 2 files changed, 341 insertions(+), 74 deletions(-)
Anyway, just some high-level comments based on a very quick glance-through review:
drivers/qmimodem/sms.c struct sms_data { · line 47
Quoted 12 lines
drivers/qmimodem/sms.c +4
-0@@ -41,6 +41,14 @@ struct sms_data {uint16_t minor; }; +static struct qmi_wms_read_msg_id rd_msg_id = {0 }; +static uint8_t msg_mode =QMI_WMS_MESSAGE_MODE_GSMWCDMA ; +static bool msg_chk_all; +static bool msg_list_chk;
You can't really do this since there might be multiple drivers active in a given oFono process. E.g. multiple QMI modems online at the same time (and yes, people do this today). So any state data must be allocated as part of the driver data.
Regards,
-Denis