X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=commands-wms.c;h=700d79ff9636c3cdb06ef34d33c3f3ce21d226dc;hb=22a64fe3e9a7b5dcf97d0b240f2ce09919f4b02f;hp=9760d856780bf4b846059d34dd6e942ab18f7f95;hpb=894ffc95a6cccfe1b5aaf4361312b8939e685dff;p=project%2Fuqmi.git diff --git a/commands-wms.c b/commands-wms.c index 9760d85..700d79f 100644 --- a/commands-wms.c +++ b/commands-wms.c @@ -1,5 +1,29 @@ +/* + * uqmi -- tiny QMI support implementation + * + * Copyright (C) 2014-2015 Felix Fietkau + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + */ + #include "qmi-message.h" +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define CEILDIV(x,y) (((x) + (y) - 1) / (y)) + 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; @@ -67,6 +91,7 @@ pdu_decode_7bit_char(char *dest, int len, unsigned char c, bool *escape) fprintf(stderr, " %02x", c); dest += len; if (*escape) { + *escape = false; switch(c) { case 0x0A: *dest = 0x0C; @@ -176,11 +201,16 @@ static int decode_udh(const unsigned char *data) break; switch (type) { - case 0: + case 0x00: blobmsg_add_u32(&status, "concat_ref", (uint32_t) val[0]); blobmsg_add_u32(&status, "concat_part", (uint32_t) val[2]); blobmsg_add_u32(&status, "concat_parts", (uint32_t) val[1]); break; + case 0x08: + blobmsg_add_u32(&status, "concat_ref", (uint32_t) (val[0] << 8 | val[1])); + blobmsg_add_u32(&status, "concat_part", (uint32_t) val[3]); + blobmsg_add_u32(&status, "concat_parts", (uint32_t) val[2]); + break; default: break; } @@ -191,8 +221,9 @@ static int decode_udh(const unsigned char *data) static void decode_7bit_field(char *name, const unsigned char *data, int data_len, int bit_offset) { - char *dest = blobmsg_alloc_string_buffer(&status, name, 3 * (data_len * 8 / 7) + 2); - pdu_decode_7bit_str(dest, data, data_len, bit_offset); + char *dest = blobmsg_alloc_string_buffer(&status, name, 3 * data_len + 2); + pdu_decode_7bit_str(dest, data, CEILDIV(data_len * 7, 8), bit_offset); + dest[data_len] = 0; blobmsg_add_string_buffer(&status); } @@ -239,6 +270,15 @@ static void wms_decode_address(char *name, unsigned char *data, int len) blobmsg_add_string_buffer(&status); } +static void blobmsg_add_hex(struct blob_buf *buf, const char *name, unsigned const char *data, int len) +{ + char* str = blobmsg_alloc_string_buffer(buf, name, len * 2 + 1); + for (int i = 0; i < len; i++) { + str += sprintf(str, "%02x", data[i]); + } + blobmsg_add_string_buffer(buf); +} + #define cmd_wms_delete_message_cb no_cb static enum qmi_cmd_result cmd_wms_delete_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg) @@ -318,10 +358,6 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, /* Data Encoding */ dcs = *(data++); - /* only 7-bit encoding supported for now */ - if (dcs & 0x0c) - goto error; - if (dcs & 0x10) blobmsg_add_u32(&status, "class", (dcs & 3)); @@ -361,12 +397,13 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, data += 7; } - data++; + int message_len = *(data++); + int udh_len = 0; int bit_offset = 0; /* User Data Header */ if (first & 0x40) { - int udh_len = decode_udh(data); + udh_len = decode_udh(data); data += udh_len; bit_offset = udh_len % 7; } @@ -374,9 +411,28 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, if (data >= end) goto error; - decode_7bit_field("text", data, end - data, bit_offset); - blobmsg_close_table(&status, c); + switch(dcs & 0x0c) { + case 0x00: + /* 7 bit GSM alphabet */ + message_len = message_len - CEILDIV(udh_len * 8, 7); + message_len = MIN(message_len, CEILDIV((end - data) * 8, 7)); + decode_7bit_field("text", data, message_len, bit_offset); + break; + case 0x04: + /* 8 bit data */ + message_len = MIN(message_len - udh_len, end - data); + blobmsg_add_hex(&status, "data", data, message_len); + break; + case 0x08: + /* 16 bit UCS-2 string */ + message_len = MIN(message_len - udh_len, end - data); + blobmsg_add_hex(&status, "ucs-2", data, message_len); + break; + default: + goto error; + } + blobmsg_close_table(&status, c); return; error: @@ -477,7 +533,7 @@ pdu_encode_semioctet(unsigned char *dest, const char *str) str++; } - return len; + return lower ? len : (len + 1); } static int @@ -525,7 +581,7 @@ pdu_encode_number(unsigned char *dest, const char *str, bool smsc) } for (i = 0; str[i]; i++) { - if (str[i] >= '0' || str[i] <= '9') + if (str[i] >= '0' && str[i] <= '9') continue; ascii = true; @@ -556,7 +612,7 @@ pdu_encode_data(unsigned char *dest, const char *str) dest[len++] = 0; len += pdu_encode_7bit_str(&dest[len], str); - dest[0] = len - 1; + dest[0] = strlen(str); return len; } @@ -577,12 +633,12 @@ cmd_wms_send_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struc unsigned char protocol_id = 0x00; unsigned char dcs = 0x00; - if (!_send.smsc || !*_send.smsc || !_send.target || !*_send.target) { + if (!_send.target || !*_send.target) { uqmi_add_error("Missing argument"); return QMI_CMD_EXIT; } - if (strlen(_send.smsc) > 16 || strlen(_send.target) > 16 || strlen(arg) > 160) { + if ((_send.smsc && strlen(_send.smsc) > 16) || strlen(_send.target) > 16 || strlen(arg) > 160) { uqmi_add_error("Argument too long"); return QMI_CMD_EXIT; } @@ -590,7 +646,11 @@ cmd_wms_send_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struc if (_send.flash) dcs |= 0x10; - cur += pdu_encode_number(cur, _send.smsc, true); + if (!_send.smsc || !*_send.smsc) + *(cur++) = 0; + else + cur += pdu_encode_number(cur, _send.smsc, true); + *(cur++) = first_octet; *(cur++) = 0; /* reference */