X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuqmi.git;a=blobdiff_plain;f=commands-wms.c;h=62199abe87b50dab59734f892b387ec7d25f3794;hp=3155dcf1d1f705556c8d6b31e487e21820ff314c;hb=889316ea13bdb113a60870445cd334bd0cd3fede;hpb=6b7fd82050227c307a019de73f30fe46d56a2107 diff --git a/commands-wms.c b/commands-wms.c index 3155dcf..62199ab 100644 --- a/commands-wms.c +++ b/commands-wms.c @@ -52,39 +52,56 @@ pdu_decode_7bit_str(char *dest, const unsigned char *data, int data_len, int bit return dest - orig_dest; } -static void decode_7bit_field(char *name, const unsigned char *data, int data_len) +static void decode_udh(const unsigned char *data) { - bool multipart = false; + const unsigned char *end; + unsigned int type, len; + + len = *(data++); + end = data + len; + while (data < end) { + const unsigned char *val; + + type = data[0]; + len = data[1]; + val = &data[2]; + data += 2 + len; + if (data > end) + break; + + switch (type) { + case 0: + blobmsg_add_u32(&status, "concat_ref", (uint32_t) val[0]); + blobmsg_add_u32(&status, "concat_part", (uint32_t) val[2] + 1); + blobmsg_add_u32(&status, "concat_parts", (uint32_t) val[1]); + break; + default: + break; + } + } +} + +static void decode_7bit_field(char *name, const unsigned char *data, int data_len, bool udh) +{ + const unsigned char *udh_start; char *dest; - int part = 0, n_parts = 0; - int len, pos_offset = 0; - int i; + int pos_offset = 0; - 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 (udh) { + int len = data[0] + 1; - if (multipart) { - len = data[0] + 1; + udh_start = data; data += len; data_len -= len; - pos_offset = 6; + pos_offset = len % 7; } dest = blobmsg_alloc_string_buffer(&status, name, data_len * 8 / 7 + 2); pdu_decode_7bit_str(dest, data, data_len, pos_offset); blobmsg_add_string_buffer(&status); - if (multipart) { - blobmsg_add_u32(&status, "part", part + 1); - blobmsg_add_u32(&status, "parts", n_parts); - } + if (udh) + decode_udh(udh_start); } static char *pdu_add_semioctet(char *str, char val) @@ -135,8 +152,9 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_wms_raw_read_response res; unsigned char *data, *end; char *str; - int i, cur_len; + int cur_len; bool sent; + unsigned char first, dcs; qmi_parse_wms_raw_read_response(msg, &res); data = (unsigned char *) res.data.raw_message_data.raw_data; @@ -154,8 +172,8 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, if (data + 3 >= end) return; - sent = (*data & 0x3) == 1; - data++; + first = *(data++); + sent = (first & 0x3) == 1; if (sent) data++; @@ -177,9 +195,15 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, return; /* Data Encoding */ - if (*(data++) != 0) + dcs = *(data++); + + /* only 7-bit encoding supported for now */ + if (dcs & 0x0c) return; + if (dcs & 0x10) + blobmsg_add_u32(&status, "class", (dcs & 3)); + if (sent) { /* Message validity */ data++; @@ -217,7 +241,7 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, } cur_len = *(data++); - decode_7bit_field("text", data, end - data); + decode_7bit_field("text", data, end - data, !!(first & 0x40)); } static enum qmi_cmd_result @@ -249,8 +273,8 @@ static void cmd_wms_get_raw_message_cb(struct qmi_dev *qmi, struct qmi_request * { struct qmi_wms_raw_read_response res; unsigned char *data; - int i, len = 0; char *str; + int i; qmi_parse_wms_raw_read_response(msg, &res); data = (unsigned char *) res.data.raw_message_data.raw_data; @@ -262,3 +286,183 @@ static void cmd_wms_get_raw_message_cb(struct qmi_dev *qmi, struct qmi_request * } #define cmd_wms_get_raw_message_prepare cmd_wms_get_message_prepare + + +static struct { + const char *smsc; + const char *target; + bool flash; +} send; + + +#define cmd_wms_send_message_smsc_cb no_cb +static enum qmi_cmd_result +cmd_wms_send_message_smsc_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg) +{ + send.smsc = arg; + return QMI_CMD_DONE; +} + +#define cmd_wms_send_message_target_cb no_cb +static enum qmi_cmd_result +cmd_wms_send_message_target_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg) +{ + send.target = arg; + return QMI_CMD_DONE; +} + +#define cmd_wms_send_message_flash_cb no_cb +static enum qmi_cmd_result +cmd_wms_send_message_flash_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg) +{ + send.flash = true; + return QMI_CMD_DONE; +} + +static int +pdu_encode_semioctet(unsigned char *dest, const char *str) +{ + int len = 0; + bool lower = true; + + while (*str) { + char digit = *str - '0'; + + if (lower) + dest[len] = 0xf0 | digit; + else + dest[len++] &= (digit << 4) | 0xf; + + lower = !lower; + str++; + } + + return len; +} + +static int +pdu_encode_7bit_str(unsigned char *data, const char *str) +{ + unsigned char c; + int len = 0; + int ofs = 0; + + while(1) { + c = *(str++) & 0x7f; + if (!c) + break; + + switch(ofs) { + case 0: + data[len] = c; + break; + default: + data[len++] |= c << (8 - ofs); + data[len] = c >> ofs; + break; + } + + ofs = (ofs + 1) % 7; + } + + return len + 1; +} + +static int +pdu_encode_number(unsigned char *dest, const char *str, bool smsc) +{ + unsigned char format; + bool ascii = false; + int len = 0; + int i; + + dest[len++] = 0; + if (*str == '+') { + str++; + format = 0x91; + } else { + format = 0x81; + } + + for (i = 0; str[i]; i++) { + if (str[i] >= '0' || str[i] <= '9') + continue; + + ascii = true; + break; + } + + if (ascii) + format |= 0x40; + + dest[len++] = format; + if (!ascii) + len += pdu_encode_semioctet(&dest[len], str); + else + len += pdu_encode_7bit_str(&dest[len], str); + + if (smsc) + dest[0] = len - 1; + else + dest[0] = strlen(str); + + return len; +} + +static int +pdu_encode_data(unsigned char *dest, const char *str) +{ + int len = 0; + + dest[len++] = 0; + len += pdu_encode_7bit_str(&dest[len], str); + dest[0] = len - 1; + + return len; +} + +#define cmd_wms_send_message_cb no_cb +static enum qmi_cmd_result +cmd_wms_send_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg) +{ + static unsigned char buf[512]; + static struct qmi_wms_raw_send_request mreq = { + QMI_INIT_SEQUENCE(raw_message_data, + .format = QMI_WMS_MESSAGE_FORMAT_GSM_WCDMA_POINT_TO_POINT, + .raw_data = buf, + ), + }; + unsigned char *cur = buf; + unsigned char first_octet = 0x11; + unsigned char protocol_id = 0x00; + unsigned char dcs = 0x00; + + if (!send.smsc || !*send.smsc || !send.target || !*send.target) { + blobmsg_add_string(&status, "error", "Missing argument"); + return QMI_CMD_EXIT; + } + + if (strlen(send.smsc) > 16 || strlen(send.target) > 16 || strlen(arg) > 160) { + blobmsg_add_string(&status, "error", "Argument too long"); + return QMI_CMD_EXIT; + } + + if (send.flash) + dcs |= 0x10; + + cur += pdu_encode_number(cur, send.smsc, true); + *(cur++) = first_octet; + *(cur++) = 0; /* reference */ + + cur += pdu_encode_number(cur, send.target, false); + *(cur++) = protocol_id; + *(cur++) = dcs; + + *(cur++) = 0xff; /* validity */ + cur += pdu_encode_data(cur, arg); + + mreq.data.raw_message_data.raw_data_n = cur - buf; + qmi_set_wms_raw_send_request(msg, &mreq); + + return QMI_CMD_REQUEST; +}