Implementation for open/close channel, list applications,
and session based file read.
---
 drivers/atmodem/sim.c | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 236 insertions(+)

diff --git a/drivers/atmodem/sim.c b/drivers/atmodem/sim.c
index 6395a04..ee03fc8 100644
--- a/drivers/atmodem/sim.c
+++ b/drivers/atmodem/sim.c
@@ -39,6 +39,7 @@
 #include "gatresult.h"
 #include "simutil.h"
 #include "vendor.h"
+#include "util.h"
 
 #include "atmodem.h"
 
@@ -69,6 +70,9 @@ static const char *pct_prefix[] = { "#PCT:", NULL };
 static const char *pnnm_prefix[] = { "+PNNM:", NULL };
 static const char *qpinc_prefix[] = { "+QPINC:", NULL };
 static const char *upincnt_prefix[] = { "+UPINCNT:", NULL };
+static const char *cuad_prefix[] = { "+CUAD:", NULL };
+static const char *ccho_prefix[] = { "+CCHO:", NULL };
+static const char *crla_prefix[] = { "+CRLA:", NULL };
 static const char *none_prefix[] = { NULL };
 
 static void at_crsm_info_cb(gboolean ok, GAtResult *result, gpointer user_data)
@@ -1568,6 +1572,234 @@ error:
        CALLBACK_WITH_FAILURE(cb, -1, data);
 }
 
+static void at_discover_apps_cb(gboolean ok, GAtResult *result,
+                               gpointer user_data)
+{
+       struct cb_data *cbd = user_data;
+       GAtResultIter iter;
+       ofono_sim_list_apps_cb_t cb = cbd->cb;
+       struct ofono_error error;
+       const unsigned char *dataobj;
+       gint linelen;
+       unsigned char *buffer;
+       int len;
+
+       decode_at_error(&error, g_at_result_final_response(result));
+
+       if (!ok) {
+               cb(&error, NULL, 0, cbd->data);
+               return;
+       }
+
+       g_at_result_iter_init(&iter, result);
+
+       len = 0;
+       while (g_at_result_iter_next(&iter, "+CUAD:")) {
+               if (!g_at_result_iter_next_hexstring(&iter, NULL, &linelen))
+                       goto error;
+
+               len += linelen;
+       }
+
+       g_at_result_iter_init(&iter, result);
+
+       buffer = g_malloc(len);
+       len = 0;
+
+       while (g_at_result_iter_next(&iter, "+CUAD:")) {
+               g_at_result_iter_next_hexstring(&iter, &dataobj, &linelen);
+               memcpy(buffer + len, dataobj, linelen);
+               len += linelen;
+       }
+
+       cb(&error, buffer, len, cbd->data);
+
+       g_free(buffer);
+       return;
+
+error:
+       CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
+}
+
+static void at_discover_apps(struct ofono_sim *sim,
+                               ofono_sim_list_apps_cb_t cb,
+                               void *data)
+{
+       struct sim_data *sd = ofono_sim_get_data(sim);
+       struct cb_data *cbd = cb_data_new(cb, data);
+
+       if (g_at_chat_send(sd->chat, "AT+CUAD", cuad_prefix,
+                       at_discover_apps_cb, cbd, g_free) > 0)
+               return;
+
+       g_free(cbd);
+
+       CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
+}
+
+static void at_open_channel_cb(gboolean ok, GAtResult *result,
+               gpointer user_data)
+{
+       struct cb_data *cbd = user_data;
+       GAtResultIter iter;
+       ofono_sim_open_channel_cb_t cb = cbd->cb;
+       struct ofono_error error;
+       int session_id = -1;
+
+       decode_at_error(&error, g_at_result_final_response(result));
+
+       if (!ok)
+               goto error;
+
+       g_at_result_iter_init(&iter, result);
+
+       if (!g_at_result_iter_next(&iter, "+CCHO:"))
+               goto error;
+
+       if (!g_at_result_iter_next_number(&iter, &session_id))
+               goto error;
+
+       cb(&error, session_id, cbd->data);
+
+       return;
+
+error:
+       cb(&error, -1, cbd->data);
+}
+
+static void at_open_channel(struct ofono_sim *sim, const unsigned char *aid,
+               ofono_sim_open_channel_cb_t cb, void *data)
+{
+       struct sim_data *sd = ofono_sim_get_data(sim);
+       struct cb_data *cbd = cb_data_new(cb, data);
+       char cmd[43];
+       int ret = 0;
+
+       strcpy(cmd, "AT+CCHO=\"");
+       ret += 9;
+
+       encode_hex_own_buf(aid, 16, 0, cmd + ret);
+       ret += 32;
+
+       strcpy(cmd + ret, "\"");
+
+       if (g_at_chat_send(sd->chat, cmd, ccho_prefix, at_open_channel_cb,
+                       cbd, g_free) > 0)
+               return;
+
+       g_free(cbd);
+
+       CALLBACK_WITH_FAILURE(cb, -1, data);
+}
+
+static void at_close_channel_cb(gboolean ok, GAtResult *result,
+               gpointer user_data)
+{
+       struct cb_data *cbd = user_data;
+       ofono_sim_close_channel_cb_t cb = cbd->cb;
+       struct ofono_error error;
+
+       decode_at_error(&error, g_at_result_final_response(result));
+
+       if (cb)
+               cb(&error, cbd->data);
+}
+
+static void at_close_channel(struct ofono_sim *sim, int session_id,
+               ofono_sim_close_channel_cb_t cb, void *data)
+{
+       struct sim_data *sd = ofono_sim_get_data(sim);
+       struct cb_data *cbd = cb_data_new(cb, data);
+       char cmd[15];
+
+       sprintf(cmd, "AT+CCHC=%d", session_id);
+
+       g_at_chat_send(sd->chat, cmd, NULL, at_close_channel_cb, cbd, g_free);
+}
+
+static void at_crla_read_cb(gboolean ok, GAtResult *result,
+                               gpointer user_data)
+{
+       struct cb_data *cbd = user_data;
+       GAtResultIter iter;
+       ofono_sim_read_cb_t cb = cbd->cb;
+       struct ofono_error error;
+       const guint8 *response;
+       gint sw1, sw2, len;
+
+       decode_at_error(&error, g_at_result_final_response(result));
+
+       if (!ok) {
+               cb(&error, NULL, 0, cbd->data);
+               return;
+       }
+
+       g_at_result_iter_init(&iter, result);
+
+       if (!g_at_result_iter_next(&iter, "+CRLA:")) {
+               CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
+               return;
+       }
+
+       g_at_result_iter_next_number(&iter, &sw1);
+       g_at_result_iter_next_number(&iter, &sw2);
+
+       if ((sw1 != 0x90 && sw1 != 0x91 && sw1 != 0x92 && sw1 != 0x9f) ||
+                       (sw1 == 0x90 && sw2 != 0x00)) {
+               memset(&error, 0, sizeof(error));
+
+               error.type = OFONO_ERROR_TYPE_SIM;
+               error.error = (sw1 << 8) | sw2;
+
+               cb(&error, NULL, 0, cbd->data);
+               return;
+       }
+
+       if (!g_at_result_iter_next_hexstring(&iter, &response, &len)) {
+               CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
+               return;
+       }
+
+       DBG("crla_read_cb: %02x, %02x, %d", sw1, sw2, len);
+
+       cb(&error, response, len, cbd->data);
+}
+
+static void at_read_file_session(struct ofono_sim *sim, int session,
+                               int fileid, int start, int length,
+                               const unsigned char *path,
+                               unsigned int path_len,
+                               ofono_sim_read_cb_t cb, void *data)
+{
+       struct sim_data *sd = ofono_sim_get_data(sim);
+       struct cb_data *cbd = cb_data_new(cb, data);
+       char buf[64];
+       unsigned int len;
+
+       len = snprintf(buf, sizeof(buf), "AT+CRLA=%i,176,%i,%i,%i,%i,,",
+                       session, fileid, start >> 8, start & 0xff, length);
+
+       if (path_len > 0) {
+               buf[len++] = ',';
+               buf[len++] = ',';
+               buf[len++] = '\"';
+
+               for (; path_len; path_len--)
+                       len += sprintf(buf + len, "%02hhX", *path++);
+
+               buf[len++] = '\"';
+               buf[len] = '\0';
+       }
+
+       if (g_at_chat_send(sd->chat, buf, crla_prefix,
+                       at_crla_read_cb, cbd, g_free) > 0)
+               return;
+
+       g_free(cbd);
+
+       CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
+}
+
 static gboolean at_sim_register(gpointer user)
 {
        struct ofono_sim *sim = user;
@@ -1629,6 +1861,10 @@ static struct ofono_sim_driver driver = {
        .lock                   = at_pin_enable,
        .change_passwd          = at_change_passwd,
        .query_facility_lock    = at_query_clck,
+       .list_apps              = at_discover_apps,
+       .open_channel           = at_open_channel,
+       .close_channel          = at_close_channel,
+       .read_file_session      = at_read_file_session
 };
 
 static struct ofono_sim_driver driver_noef = {
-- 
2.7.4

_______________________________________________
ofono mailing list
[email protected]
https://lists.ofono.org/mailman/listinfo/ofono

Reply via email to