1 #include "qmi-message.h"
3 #define MIN(a,b) (((a)<(b))?(a):(b))
4 #define CEILDIV(x,y) (((x) + (y) - 1) / (y))
6 static void cmd_wms_list_messages_cb(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
8 struct qmi_wms_list_messages_response res
;
12 qmi_parse_wms_list_messages_response(msg
, &res
);
13 c
= blobmsg_open_array(&status
, NULL
);
14 for (i
= 0; i
< res
.data
.message_list_n
; i
++)
15 blobmsg_add_u32(&status
, NULL
, res
.data
.message_list
[i
].memory_index
);
17 blobmsg_close_array(&status
, c
);
20 static enum qmi_cmd_result
21 cmd_wms_list_messages_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
23 static struct qmi_wms_list_messages_request mreq
= {
24 QMI_INIT(storage_type
, QMI_WMS_STORAGE_TYPE_UIM
),
25 QMI_INIT(message_tag
, QMI_WMS_MESSAGE_TAG_TYPE_MT_NOT_READ
),
28 qmi_set_wms_list_messages_request(msg
, &mreq
);
30 return QMI_CMD_REQUEST
;
34 put_unicode_char(char *dest
, uint16_t c
)
39 } else if (c
< 0x800) {
40 *(dest
++) = 0xc0 | ((c
>> 6) & 0x1f);
41 *dest
= 0x80 | (c
& 0x3f);
44 *(dest
++) = 0xe0 | ((c
>> 12) & 0xf);
45 *(dest
++) = 0x80 | ((c
>> 6) & 0x3f);
46 *dest
= 0x80 | (c
& 0x3f);
53 pdu_decode_7bit_char(char *dest
, int len
, unsigned char c
, bool *escape
)
55 uint16_t conv_0x20
[] = {
56 0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
57 0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
58 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
59 0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
61 uint16_t conv_0x5b
[] = {
62 0x00C4, 0x00D6, 0x00D1, 0x00DC, 0x00A7, 0x00BF,
64 uint16_t conv_0x7b
[] = {
65 0x00E4, 0x00F6, 0x00F1, 0x00FC, 0x00E0
70 fprintf(stderr
, " %02x", c
);
109 *(dest
++) = conv_0x20
[0x1B];
122 outc
= conv_0x20
[(int) c
];
125 else if (c
>= 0x5b && c
<= 0x60)
126 outc
= conv_0x5b
[c
- 0x5b];
127 else if (c
>= 0x7b && c
<= 0x7f)
128 outc
= conv_0x7b
[c
- 0x7b];
133 return cur_len
+ put_unicode_char(dest
, outc
);
137 pdu_decode_7bit_str(char *dest
, const unsigned char *data
, int data_len
, int bit_offset
)
143 fprintf(stderr
, "Raw text:");
144 for (i
= 0; i
< data_len
; i
++) {
145 int pos
= (i
+ bit_offset
) % 7;
148 len
+= pdu_decode_7bit_char(dest
, len
, data
[i
] & 0x7f, &escape
);
151 len
+= pdu_decode_7bit_char(dest
, len
,
152 (data
[i
- 1] >> (7 + 1 - pos
)) |
153 ((data
[i
] << pos
) & 0x7f), &escape
);
156 len
+= pdu_decode_7bit_char(dest
, len
, (data
[i
] >> 1) & 0x7f,
161 fprintf(stderr
, "\n");
165 static int decode_udh(const unsigned char *data
)
167 const unsigned char *end
;
168 unsigned int type
, len
, udh_len
;
171 end
= data
+ udh_len
;
173 const unsigned char *val
;
184 blobmsg_add_u32(&status
, "concat_ref", (uint32_t) val
[0]);
185 blobmsg_add_u32(&status
, "concat_part", (uint32_t) val
[2]);
186 blobmsg_add_u32(&status
, "concat_parts", (uint32_t) val
[1]);
189 blobmsg_add_u32(&status
, "concat_ref", (uint32_t) (val
[0] << 8 | val
[1]));
190 blobmsg_add_u32(&status
, "concat_part", (uint32_t) val
[3]);
191 blobmsg_add_u32(&status
, "concat_parts", (uint32_t) val
[2]);
201 static void decode_7bit_field(char *name
, const unsigned char *data
, int data_len
, int bit_offset
)
203 char *dest
= blobmsg_alloc_string_buffer(&status
, name
, 3 * data_len
+ 2);
204 pdu_decode_7bit_str(dest
, data
, CEILDIV(data_len
* 7, 8), bit_offset
);
206 blobmsg_add_string_buffer(&status
);
209 static char *pdu_add_semioctet(char *str
, char val
)
211 *str
= '0' + (val
& 0xf);
215 *str
= '0' + ((val
>> 4) & 0xf);
223 pdu_decode_address(char *str
, unsigned char *data
, int len
)
228 switch (toa
& 0x70) {
230 pdu_decode_7bit_str(str
, data
, len
, 0);
237 str
= pdu_add_semioctet(str
, *data
);
245 static void wms_decode_address(char *name
, unsigned char *data
, int len
)
247 char *str
= blobmsg_alloc_string_buffer(&status
, name
, len
* 2 + 2);
248 pdu_decode_address(str
, data
, len
);
249 blobmsg_add_string_buffer(&status
);
252 static void blobmsg_add_hex(struct blob_buf
*buf
, const char *name
, unsigned const char *data
, int len
)
254 char* str
= blobmsg_alloc_string_buffer(buf
, name
, len
* 2 + 1);
255 for (int i
= 0; i
< len
; i
++) {
256 str
+= sprintf(str
, "%02x", data
[i
]);
258 blobmsg_add_string_buffer(buf
);
261 #define cmd_wms_delete_message_cb no_cb
262 static enum qmi_cmd_result
263 cmd_wms_delete_message_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
268 id
= strtoul(arg
, &err
, 10);
270 uqmi_add_error("Invalid message ID");
274 static struct qmi_wms_delete_request mreq
= {
275 QMI_INIT(memory_storage
, QMI_WMS_STORAGE_TYPE_UIM
),
276 QMI_INIT(message_mode
, QMI_WMS_MESSAGE_MODE_GSM_WCDMA
),
279 mreq
.set
.memory_index
= 1;
280 mreq
.data
.memory_index
= id
;
282 qmi_set_wms_delete_request(msg
, &mreq
);
284 return QMI_CMD_REQUEST
;
288 static void cmd_wms_get_message_cb(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
290 struct qmi_wms_raw_read_response res
;
291 unsigned char *data
, *end
;
295 unsigned char first
, dcs
;
298 qmi_parse_wms_raw_read_response(msg
, &res
);
299 c
= blobmsg_open_table(&status
, NULL
);
300 data
= (unsigned char *) res
.data
.raw_message_data
.raw_data
;
301 end
= data
+ res
.data
.raw_message_data
.raw_data_n
;
304 if (data
+ cur_len
>= end
)
308 wms_decode_address("smsc", data
, cur_len
- 1);
316 sent
= (first
& 0x3) == 1;
321 if (data
+ cur_len
>= end
)
325 cur_len
= (cur_len
+ 1) / 2;
326 wms_decode_address(sent
? "receiver" : "sender", data
, cur_len
);
341 blobmsg_add_u32(&status
, "class", (dcs
& 3));
344 /* Message validity */
350 str
= blobmsg_alloc_string_buffer(&status
, "timestamp", 32);
355 str
= pdu_add_semioctet(str
, data
[0]);
358 str
= pdu_add_semioctet(str
, data
[1]);
361 str
= pdu_add_semioctet(str
, data
[2]);
365 str
= pdu_add_semioctet(str
, data
[3]);
368 str
= pdu_add_semioctet(str
, data
[4]);
371 str
= pdu_add_semioctet(str
, data
[5]);
374 blobmsg_add_string_buffer(&status
);
379 int message_len
= *(data
++);
383 /* User Data Header */
385 udh_len
= decode_udh(data
);
387 bit_offset
= udh_len
% 7;
395 /* 7 bit GSM alphabet */
396 message_len
= message_len
- CEILDIV(udh_len
* 8, 7);
397 message_len
= MIN(message_len
, CEILDIV((end
- data
) * 8, 7));
398 decode_7bit_field("text", data
, message_len
, bit_offset
);
402 message_len
= MIN(message_len
- udh_len
, end
- data
);
403 blobmsg_add_hex(&status
, "data", data
, message_len
);
406 /* 16 bit UCS-2 string */
407 message_len
= MIN(message_len
- udh_len
, end
- data
);
408 blobmsg_add_hex(&status
, "ucs-2", data
, message_len
);
414 blobmsg_close_table(&status
, c
);
418 blobmsg_close_table(&status
, c
);
419 fprintf(stderr
, "There was an error reading message.\n");
422 static enum qmi_cmd_result
423 cmd_wms_get_message_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
425 static struct qmi_wms_raw_read_request mreq
= {
426 QMI_INIT_SEQUENCE(message_memory_storage_id
,
427 .storage_type
= QMI_WMS_STORAGE_TYPE_UIM
,
429 QMI_INIT(message_mode
, QMI_WMS_MESSAGE_MODE_GSM_WCDMA
),
434 id
= strtoul(arg
, &err
, 10);
436 uqmi_add_error("Invalid message ID");
440 mreq
.data
.message_memory_storage_id
.memory_index
= id
;
441 qmi_set_wms_raw_read_request(msg
, &mreq
);
443 return QMI_CMD_REQUEST
;
447 static void cmd_wms_get_raw_message_cb(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
449 struct qmi_wms_raw_read_response res
;
454 qmi_parse_wms_raw_read_response(msg
, &res
);
455 data
= (unsigned char *) res
.data
.raw_message_data
.raw_data
;
456 str
= blobmsg_alloc_string_buffer(&status
, NULL
, res
.data
.raw_message_data
.raw_data_n
* 3);
457 for (i
= 0; i
< res
.data
.raw_message_data
.raw_data_n
; i
++) {
458 str
+= sprintf(str
, &" %02x"[i
? 0 : 1], data
[i
]);
460 blobmsg_add_string_buffer(&status
);
463 #define cmd_wms_get_raw_message_prepare cmd_wms_get_message_prepare
473 #define cmd_wms_send_message_smsc_cb no_cb
474 static enum qmi_cmd_result
475 cmd_wms_send_message_smsc_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
481 #define cmd_wms_send_message_target_cb no_cb
482 static enum qmi_cmd_result
483 cmd_wms_send_message_target_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
489 #define cmd_wms_send_message_flash_cb no_cb
490 static enum qmi_cmd_result
491 cmd_wms_send_message_flash_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
498 pdu_encode_semioctet(unsigned char *dest
, const char *str
)
504 char digit
= *str
- '0';
507 dest
[len
] = 0xf0 | digit
;
509 dest
[len
++] &= (digit
<< 4) | 0xf;
519 pdu_encode_7bit_str(unsigned char *data
, const char *str
)
535 data
[len
++] |= c
<< (8 - ofs
);
536 data
[len
] = c
>> ofs
;
547 pdu_encode_number(unsigned char *dest
, const char *str
, bool smsc
)
549 unsigned char format
;
562 for (i
= 0; str
[i
]; i
++) {
563 if (str
[i
] >= '0' && str
[i
] <= '9')
573 dest
[len
++] = format
;
575 len
+= pdu_encode_semioctet(&dest
[len
], str
);
577 len
+= pdu_encode_7bit_str(&dest
[len
], str
);
582 dest
[0] = strlen(str
);
588 pdu_encode_data(unsigned char *dest
, const char *str
)
593 len
+= pdu_encode_7bit_str(&dest
[len
], str
);
594 dest
[0] = strlen(str
);
599 #define cmd_wms_send_message_cb no_cb
600 static enum qmi_cmd_result
601 cmd_wms_send_message_prepare(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
, char *arg
)
603 static unsigned char buf
[512];
604 static struct qmi_wms_raw_send_request mreq
= {
605 QMI_INIT_SEQUENCE(raw_message_data
,
606 .format
= QMI_WMS_MESSAGE_FORMAT_GSM_WCDMA_POINT_TO_POINT
,
610 unsigned char *cur
= buf
;
611 unsigned char first_octet
= 0x11;
612 unsigned char protocol_id
= 0x00;
613 unsigned char dcs
= 0x00;
615 if (!_send
.smsc
|| !*_send
.smsc
|| !_send
.target
|| !*_send
.target
) {
616 uqmi_add_error("Missing argument");
620 if (strlen(_send
.smsc
) > 16 || strlen(_send
.target
) > 16 || strlen(arg
) > 160) {
621 uqmi_add_error("Argument too long");
628 cur
+= pdu_encode_number(cur
, _send
.smsc
, true);
629 *(cur
++) = first_octet
;
630 *(cur
++) = 0; /* reference */
632 cur
+= pdu_encode_number(cur
, _send
.target
, false);
633 *(cur
++) = protocol_id
;
636 *(cur
++) = 0xff; /* validity */
637 cur
+= pdu_encode_data(cur
, arg
);
639 mreq
.data
.raw_message_data
.raw_data_n
= cur
- buf
;
640 qmi_set_wms_raw_send_request(msg
, &mreq
);
642 return QMI_CMD_REQUEST
;