From 0d5faa5adfed1d32d4888793046c411eb35857ed Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Fri, 3 Nov 2017 16:30:49 -0700 Subject: [PATCH] atmodem: implement new driver APIs for AID sessions Implementation for open/close channel, list applications, and session based file read. --- drivers/atmodem/sim.c | 277 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 275 insertions(+), 2 deletions(-) diff --git a/drivers/atmodem/sim.c b/drivers/atmodem/sim.c index 421abfb8..75082810 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" @@ -70,6 +71,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 append_file_path(char *buf, const unsigned char *path, @@ -88,7 +92,8 @@ static void append_file_path(char *buf, const unsigned char *path, } } -static void at_crsm_info_cb(gboolean ok, GAtResult *result, gpointer user_data) +static void get_response_common_cb(gboolean ok, GAtResult *result, + gpointer user_data, const char *prefix) { struct cb_data *cbd = user_data; GAtResultIter iter; @@ -110,7 +115,7 @@ static void at_crsm_info_cb(gboolean ok, GAtResult *result, gpointer user_data) g_at_result_iter_init(&iter, result); - if (!g_at_result_iter_next(&iter, "+CRSM:")) + if (!g_at_result_iter_next(&iter, prefix)) goto error; g_at_result_iter_next_number(&iter, &sw1); @@ -151,6 +156,11 @@ error: EF_STATUS_INVALIDATED, cbd->data); } +static void at_crsm_info_cb(gboolean ok, GAtResult *result, gpointer user_data) +{ + get_response_common_cb(ok, result, user_data, "+CRSM:"); +} + static void at_sim_read_info(struct ofono_sim *sim, int fileid, const unsigned char *path, unsigned int path_len, @@ -1604,6 +1614,263 @@ done: ofono_sim_register(sim); } +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 *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); + + if (!g_at_result_iter_next(&iter, "+CUAD:")) + goto error; + + if (!g_at_result_iter_next_hexstring(&iter, &buffer, &len)) + goto error; + + cb(&error, buffer, len, cbd->data); + + 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_session_read_binary(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); + + append_file_path(buf + len, path, path_len); + + 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 void at_session_read_record(struct ofono_sim *sim, int session_id, + int fileid, int record, 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[128]; + unsigned int len; + + len = snprintf(buf, sizeof(buf), "AT+CRLA=%i,178,%i,%i,4,%i", + session_id, fileid, record, length); + + append_file_path(buf + len, path, path_len); + + 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 void at_crla_info_cb(gboolean ok, GAtResult *result, gpointer user_data) +{ + get_response_common_cb(ok, result, user_data, "+CRLA:"); +} + +static void at_session_read_info(struct ofono_sim *sim, int session_id, + int fileid, const unsigned char *path, + unsigned int path_len, + ofono_sim_file_info_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[128]; + unsigned int len; + + len = snprintf(buf, sizeof(buf), "AT+CRLA=%i,192,%i", session_id, + fileid); + + append_file_path(buf + len, path, path_len); + + if (g_at_chat_send(sd->chat, buf, crla_prefix, + at_crla_info_cb, cbd, g_free) > 0) + return; + + g_free(cbd); + + CALLBACK_WITH_FAILURE(cb, -1, -1, -1, NULL, + EF_STATUS_INVALIDATED, data); +} + static int at_sim_probe(struct ofono_sim *sim, unsigned int vendor, void *data) { @@ -1663,6 +1930,12 @@ 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, + .session_read_binary = at_session_read_binary, + .session_read_record = at_session_read_record, + .session_read_info = at_session_read_info, }; static struct ofono_sim_driver driver_noef = {