add sms read access
authorFelix Fietkau <nbd@openwrt.org>
Mon, 18 Feb 2013 04:16:27 +0000 (05:16 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 18 Feb 2013 04:16:27 +0000 (05:16 +0100)
commands-wms.c [new file with mode: 0644]
commands-wms.h [new file with mode: 0644]
commands.c
commands.h
main.c

diff --git a/commands-wms.c b/commands-wms.c
new file mode 100644 (file)
index 0000000..74fb69a
--- /dev/null
@@ -0,0 +1,223 @@
+#include "qmi-message.h"
+
+static void cmd_wms_list_messages_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
+{
+       struct qmi_wms_list_messages_response res;
+       int i, len = 0;
+
+       qmi_parse_wms_list_messages_response(msg, &res);
+       blobmsg_alloc_string_buffer(&status, "messages", 1);
+       for (i = 0; i < res.data.message_list_n; i++) {
+               len += sprintf(blobmsg_realloc_string_buffer(&status, len + 12) + len,
+                              " %d" + (len ? 0 : 1),
+                                          res.data.message_list[i].memory_index);
+       }
+       blobmsg_add_string_buffer(&status);
+}
+
+static enum qmi_cmd_result
+cmd_wms_list_messages_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
+{
+       static struct qmi_wms_list_messages_request mreq = {
+               QMI_INIT(storage_type, QMI_WMS_STORAGE_TYPE_UIM),
+               QMI_INIT(message_tag, QMI_WMS_MESSAGE_TAG_TYPE_MT_NOT_READ),
+       };
+
+       qmi_set_wms_list_messages_request(msg, &mreq);
+
+       return QMI_CMD_REQUEST;
+}
+
+static void decode_7bit(char *name, const unsigned char *data, int data_len)
+{
+       bool multipart = false;
+       char *dest;
+       int part = 0, n_parts = 0;
+       int len, pos_offset = 0;
+       int i;
+
+       if (data[0] == 5 && data[1] == 0 && data[2] == 3) {
+               multipart = true;
+               n_parts = data[4];
+               part = data[5];
+       } else if (data[0] == 6 && data[1] == 8 && data[2] == 4) {
+               multipart = true;
+               n_parts = data[5];
+               part = data[6];
+       }
+
+       if (multipart) {
+               len = data[0] + 1;
+               data += len;
+               data_len -= len;
+               pos_offset = 6;
+       }
+
+       dest = blobmsg_alloc_string_buffer(&status, name, data_len * 8 / 7 + 2);
+       for (i = 0; i < data_len; i++) {
+               int pos = (i + pos_offset) % 7;
+
+               if (pos == 0) {
+                       *(dest++) = data[i] & 0x7f;
+               } else {
+                       if (i)
+                               *(dest++) = (data[i - 1] >> (7 + 1 - pos)) |
+                                           ((data[i] << pos) & 0x7f);
+
+                       if (pos == 6)
+                               *(dest++) = (data[i] >> 1) & 0x7f;
+               }
+       }
+       *dest = 0;
+       blobmsg_add_string_buffer(&status);
+
+       if (multipart) {
+               blobmsg_add_u32(&status, "part", part + 1);
+               blobmsg_add_u32(&status, "parts", n_parts);
+       }
+}
+
+static char *add_semioctet(char *str, char val)
+{
+       *str = '0' + (val & 0xf);
+       if (*str <= '9')
+               str++;
+
+       *str = '0' + ((val >> 4) & 0xf);
+       if (*str <= '9')
+               str++;
+
+       return str;
+}
+
+static unsigned char *
+decode_semioctet_number(char *str, char *name, unsigned char *data, int len)
+{
+       str = blobmsg_alloc_string_buffer(&status, name, len * 2 + 2);
+       if (*data == 0x91) {
+               len--;
+               *(str++) = '+';
+       }
+       data++;
+       while (len) {
+               str = add_semioctet(str, *data);
+               data++;
+               len--;
+       }
+
+       *str = 0;
+       blobmsg_add_string_buffer(&status);
+
+       return data;
+}
+
+static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
+{
+       struct qmi_wms_raw_read_response res;
+       unsigned char *data, *end;
+       char *str;
+       int i, cur_len;
+       bool sent;
+
+       qmi_parse_wms_raw_read_response(msg, &res);
+       data = (unsigned char *) res.data.raw_message_data.raw_data;
+       end = data + res.data.raw_message_data.raw_data_n;
+
+       do {
+               cur_len = *(data++);
+               if (data + cur_len >= end)
+                       break;
+
+               if (cur_len)
+                       data = decode_semioctet_number(str, "smsc", data, cur_len);
+
+               if (data + 3 >= end)
+                       break;
+
+               sent = (*data & 0x3) == 1;
+               data++;
+               if (sent)
+                       data++;
+
+               cur_len = *(data++);
+               if (data + cur_len >= end)
+                       break;
+
+               if (cur_len)
+                       data = decode_semioctet_number(str, sent ? "receiver" : "sender", data, (cur_len + 1) / 2);
+
+               if (data + 3 >= end)
+                       break;
+
+               /* Protocol ID */
+               if (*(data++) != 0)
+                       break;
+
+               /* Data Encoding */
+               if (*(data++) != 0)
+                       break;
+
+               if (sent) {
+                       /* Message validity */
+                       data++;
+               } else {
+                       if (data + 6 >= end)
+                               break;
+
+                       str = blobmsg_alloc_string_buffer(&status, "timestamp", 32);
+
+                       /* year */
+                       *(str++) = '2';
+                       *(str++) = '0';
+                       str = add_semioctet(str, data[0]);
+                       /* month */
+                       *(str++) = '-';
+                       str = add_semioctet(str, data[1]);
+                       /* day */
+                       *(str++) = '-';
+                       str = add_semioctet(str, data[2]);
+
+                       /* hour */
+                       *(str++) = ' ';
+                       str = add_semioctet(str, data[3]);
+                       /* minute */
+                       *(str++) = ':';
+                       str = add_semioctet(str, data[4]);
+                       /* second */
+                       *(str++) = ':';
+                       str = add_semioctet(str, data[5]);
+                       *str = 0;
+
+                       blobmsg_add_string_buffer(&status);
+
+                       data += 7;
+               }
+
+               cur_len = *(data++);
+               decode_7bit("text", data, end - data);
+       } while (0);
+}
+
+static enum qmi_cmd_result
+cmd_wms_get_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
+{
+       static struct qmi_wms_raw_read_request mreq = {
+               QMI_INIT_SEQUENCE(message_memory_storage_id,
+                       .storage_type = QMI_WMS_STORAGE_TYPE_UIM,
+               ),
+               QMI_INIT(message_mode, QMI_WMS_MESSAGE_MODE_GSM_WCDMA),
+       };
+       char *err;
+       int id;
+
+       id = strtoul(arg, &err, 10);
+       if (err && *err) {
+               blobmsg_add_string(&status, "error", "Invalid message ID");
+               return QMI_CMD_EXIT;
+       }
+
+       mreq.data.message_memory_storage_id.memory_index = id;
+       qmi_set_wms_raw_read_request(msg, &mreq);
+
+       return QMI_CMD_REQUEST;
+}
diff --git a/commands-wms.h b/commands-wms.h
new file mode 100644 (file)
index 0000000..d63f7f2
--- /dev/null
@@ -0,0 +1,8 @@
+#define __uqmi_wms_commands \
+       __uqmi_command(wms_list_messages, list-messages, no, QMI_SERVICE_WMS), \
+       __uqmi_command(wms_get_message, get-message, required, QMI_SERVICE_WMS) \
+
+#define wms_helptext \
+               "  --list-messages:                  List SMS messages\n" \
+               "  --get-message <id>:               Get SMS message at index <id>\n" \
+
index 044760e7f7dd7530b00814ef4f9925d1ee01cc59..43b9ca999c8ae7f4dfa0f5bf459a3f2d07b353a2 100644 (file)
@@ -96,6 +96,7 @@ cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct q
 #include "commands-wds.c"
 #include "commands-dms.c"
 #include "commands-nas.c"
+#include "commands-wms.c"
 
 #define __uqmi_command(_name, _optname, _arg, _type) \
        [__UQMI_COMMAND_##_name] = { \
index 7854eb549e50564e5423bd809c538a5609312f3d..62d63f83d783b2d52dd549f09150aa123f6571d4 100644 (file)
@@ -5,6 +5,7 @@
 #include "commands-wds.h"
 #include "commands-dms.h"
 #include "commands-nas.h"
+#include "commands-wms.h"
 
 enum qmi_cmd_result {
        QMI_CMD_DONE,
@@ -35,7 +36,8 @@ struct uqmi_cmd {
        __uqmi_command(get_client_id, get-client-id, required, QMI_SERVICE_CTL), \
        __uqmi_wds_commands, \
        __uqmi_dms_commands, \
-       __uqmi_nas_commands
+       __uqmi_nas_commands, \
+       __uqmi_wms_commands
 
 #define __uqmi_command(_name, _optname, _arg, _option) __UQMI_COMMAND_##_name
 enum uqmi_command {
diff --git a/main.c b/main.c
index cde1f8dfadf568d895c3452752f9d0ccc626e41b..5a4a8e818f4eb3f69717cb69e0a4beef2862d307 100644 (file)
--- a/main.c
+++ b/main.c
@@ -42,6 +42,7 @@ static int usage(const char *progname)
                wds_helptext
                dms_helptext
                nas_helptext
+               wms_helptext
                "\n", progname);
        return 1;
 }