commands-wms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * uqmi -- tiny QMI support implementation
  3. *
  4. * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301 USA.
  20. */
  21. #include "qmi-message.h"
  22. #define MIN(a,b) (((a)<(b))?(a):(b))
  23. #define CEILDIV(x,y) (((x) + (y) - 1) / (y))
  24. static struct qmi_wms_list_messages_request lmreq = {
  25. QMI_INIT(storage_type, QMI_WMS_STORAGE_TYPE_UIM),
  26. QMI_INIT(message_tag, QMI_WMS_MESSAGE_TAG_TYPE_MT_NOT_READ),
  27. };
  28. static struct qmi_wms_delete_request dmreq = {
  29. QMI_INIT(memory_storage, QMI_WMS_STORAGE_TYPE_UIM),
  30. QMI_INIT(message_mode, QMI_WMS_MESSAGE_MODE_GSM_WCDMA),
  31. };
  32. static struct qmi_wms_raw_read_request gmreq = {
  33. QMI_INIT_SEQUENCE(message_memory_storage_id,
  34. .storage_type = QMI_WMS_STORAGE_TYPE_UIM,
  35. ),
  36. QMI_INIT(message_mode, QMI_WMS_MESSAGE_MODE_GSM_WCDMA),
  37. };
  38. #define cmd_wms_storage_cb no_cb
  39. static enum qmi_cmd_result
  40. cmd_wms_storage_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  41. {
  42. if (strcmp(arg, "sim") == 0) {
  43. } else if (strcmp(arg, "me") == 0) {
  44. qmi_set_ptr(&lmreq, storage_type, QMI_WMS_STORAGE_TYPE_NV);
  45. qmi_set_ptr(&dmreq, memory_storage, QMI_WMS_STORAGE_TYPE_NV);
  46. qmi_set_ptr(&gmreq, message_memory_storage_id.storage_type, QMI_WMS_STORAGE_TYPE_NV);
  47. } else {
  48. uqmi_add_error("Invalid value (sim or me)");
  49. return QMI_CMD_EXIT;
  50. }
  51. return QMI_CMD_DONE;
  52. }
  53. static void cmd_wms_list_messages_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
  54. {
  55. struct qmi_wms_list_messages_response res;
  56. void *c;
  57. int i;
  58. qmi_parse_wms_list_messages_response(msg, &res);
  59. c = blobmsg_open_array(&status, NULL);
  60. for (i = 0; i < res.data.message_list_n; i++)
  61. blobmsg_add_u32(&status, NULL, res.data.message_list[i].memory_index);
  62. blobmsg_close_array(&status, c);
  63. }
  64. static enum qmi_cmd_result
  65. cmd_wms_list_messages_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  66. {
  67. qmi_set_wms_list_messages_request(msg, &lmreq);
  68. return QMI_CMD_REQUEST;
  69. }
  70. static int
  71. put_unicode_char(char *dest, uint16_t c)
  72. {
  73. if (c < 0x80) {
  74. *dest = c;
  75. return 1;
  76. } else if (c < 0x800) {
  77. *(dest++) = 0xc0 | ((c >> 6) & 0x1f);
  78. *dest = 0x80 | (c & 0x3f);
  79. return 2;
  80. } else {
  81. *(dest++) = 0xe0 | ((c >> 12) & 0xf);
  82. *(dest++) = 0x80 | ((c >> 6) & 0x3f);
  83. *dest = 0x80 | (c & 0x3f);
  84. return 3;
  85. }
  86. }
  87. static int
  88. pdu_decode_7bit_char(char *dest, int len, unsigned char c, bool *escape)
  89. {
  90. uint16_t conv_0x20[] = {
  91. 0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
  92. 0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
  93. 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
  94. 0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
  95. };
  96. uint16_t conv_0x5b[] = {
  97. 0x00C4, 0x00D6, 0x00D1, 0x00DC, 0x00A7, 0x00BF,
  98. };
  99. uint16_t conv_0x7b[] = {
  100. 0x00E4, 0x00F6, 0x00F1, 0x00FC, 0x00E0
  101. };
  102. int cur_len = 0;
  103. uint16_t outc;
  104. fprintf(stderr, " %02x", c);
  105. dest += len;
  106. if (*escape) {
  107. *escape = false;
  108. switch(c) {
  109. case 0x0A:
  110. *dest = 0x0C;
  111. return 1;
  112. case 0x14:
  113. *dest = 0x5E;
  114. return 1;
  115. case 0x28:
  116. *dest = 0x7B;
  117. return 1;
  118. case 0x29:
  119. *dest = 0x7D;
  120. return 1;
  121. case 0x2F:
  122. *dest = 0x5C;
  123. return 1;
  124. case 0x3C:
  125. *dest = 0x5B;
  126. return 1;
  127. case 0x3D:
  128. *dest = 0x7E;
  129. return 1;
  130. case 0x3E:
  131. *dest = 0x5D;
  132. return 1;
  133. case 0x40:
  134. *dest = 0x7C;
  135. return 1;
  136. case 0x65:
  137. outc = 0x20AC;
  138. goto out;
  139. case 0x1B:
  140. goto normal;
  141. default:
  142. /* invalid */
  143. *(dest++) = conv_0x20[0x1B];
  144. cur_len++;
  145. goto normal;
  146. }
  147. }
  148. if (c == 0x1b) {
  149. *escape = true;
  150. return 0;
  151. }
  152. normal:
  153. if (c < 0x20)
  154. outc = conv_0x20[(int) c];
  155. else if (c == 0x40)
  156. outc = 0x00A1;
  157. else if (c >= 0x5b && c <= 0x60)
  158. outc = conv_0x5b[c - 0x5b];
  159. else if (c >= 0x7b && c <= 0x7f)
  160. outc = conv_0x7b[c - 0x7b];
  161. else
  162. outc = c;
  163. out:
  164. return cur_len + put_unicode_char(dest, outc);
  165. }
  166. static int
  167. pdu_decode_7bit_str(char *dest, const unsigned char *data, int data_len, int bit_offset)
  168. {
  169. bool escape = false;
  170. int len = 0;
  171. int i;
  172. fprintf(stderr, "Raw text:");
  173. for (i = 0; i < data_len; i++) {
  174. int pos = (i + bit_offset) % 7;
  175. if (pos == 0) {
  176. len += pdu_decode_7bit_char(dest, len, data[i] & 0x7f, &escape);
  177. } else {
  178. if (i)
  179. len += pdu_decode_7bit_char(dest, len,
  180. (data[i - 1] >> (7 + 1 - pos)) |
  181. ((data[i] << pos) & 0x7f), &escape);
  182. if (pos == 6)
  183. len += pdu_decode_7bit_char(dest, len, (data[i] >> 1) & 0x7f,
  184. &escape);
  185. }
  186. }
  187. dest[len] = 0;
  188. fprintf(stderr, "\n");
  189. return len;
  190. }
  191. static int decode_udh(const unsigned char *data)
  192. {
  193. const unsigned char *end;
  194. unsigned int type, len, udh_len;
  195. udh_len = *(data++);
  196. end = data + udh_len;
  197. while (data < end) {
  198. const unsigned char *val;
  199. type = data[0];
  200. len = data[1];
  201. val = &data[2];
  202. data += 2 + len;
  203. if (data > end)
  204. break;
  205. switch (type) {
  206. case 0x00:
  207. blobmsg_add_u32(&status, "concat_ref", (uint32_t) val[0]);
  208. blobmsg_add_u32(&status, "concat_part", (uint32_t) val[2]);
  209. blobmsg_add_u32(&status, "concat_parts", (uint32_t) val[1]);
  210. break;
  211. case 0x08:
  212. blobmsg_add_u32(&status, "concat_ref", (uint32_t) (val[0] << 8 | val[1]));
  213. blobmsg_add_u32(&status, "concat_part", (uint32_t) val[3]);
  214. blobmsg_add_u32(&status, "concat_parts", (uint32_t) val[2]);
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. return udh_len + 1;
  221. }
  222. static void decode_7bit_field(char *name, const unsigned char *data, int data_len, int bit_offset)
  223. {
  224. char *dest = blobmsg_alloc_string_buffer(&status, name, 3 * data_len + 2);
  225. int out_len = pdu_decode_7bit_str(dest, data, CEILDIV(data_len * 7, 8), bit_offset);
  226. dest[out_len] = 0;
  227. blobmsg_add_string_buffer(&status);
  228. }
  229. static char *pdu_add_semioctet(char *str, char val)
  230. {
  231. *str = '0' + (val & 0xf);
  232. if (*str <= '9')
  233. str++;
  234. *str = '0' + ((val >> 4) & 0xf);
  235. if (*str <= '9')
  236. str++;
  237. return str;
  238. }
  239. static void
  240. pdu_decode_address(char *str, unsigned char *data, int len)
  241. {
  242. unsigned char toa;
  243. toa = *(data++);
  244. switch (toa & 0x70) {
  245. case 0x50:
  246. pdu_decode_7bit_str(str, data, len, 0);
  247. return;
  248. case 0x10:
  249. *(str++) = '+';
  250. /* fall through */
  251. default:
  252. while (len--) {
  253. str = pdu_add_semioctet(str, *data);
  254. data++;
  255. }
  256. }
  257. *str = 0;
  258. }
  259. static void wms_decode_address(char *name, unsigned char *data, int len)
  260. {
  261. char *str = blobmsg_alloc_string_buffer(&status, name, len * 2 + 2);
  262. pdu_decode_address(str, data, len);
  263. blobmsg_add_string_buffer(&status);
  264. }
  265. static void blobmsg_add_hex(struct blob_buf *buf, const char *name, unsigned const char *data, int len)
  266. {
  267. char* str = blobmsg_alloc_string_buffer(buf, name, len * 2 + 1);
  268. for (int i = 0; i < len; i++) {
  269. str += sprintf(str, "%02x", data[i]);
  270. }
  271. blobmsg_add_string_buffer(buf);
  272. }
  273. #define cmd_wms_delete_message_cb no_cb
  274. static enum qmi_cmd_result
  275. cmd_wms_delete_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  276. {
  277. char *err;
  278. int id;
  279. id = strtoul(arg, &err, 10);
  280. if (err && *err) {
  281. uqmi_add_error("Invalid message ID");
  282. return QMI_CMD_EXIT;
  283. }
  284. dmreq.set.memory_index = 1;
  285. dmreq.data.memory_index = id;
  286. qmi_set_wms_delete_request(msg, &dmreq);
  287. return QMI_CMD_REQUEST;
  288. }
  289. static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
  290. {
  291. struct qmi_wms_raw_read_response res;
  292. unsigned char *data, *end;
  293. char *str;
  294. int cur_len;
  295. bool sent;
  296. unsigned char first, dcs;
  297. void *c;
  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;
  302. cur_len = *(data++);
  303. if (data + cur_len >= end)
  304. goto error;
  305. if (cur_len) {
  306. wms_decode_address("smsc", data, cur_len - 1);
  307. data += cur_len;
  308. }
  309. if (data + 3 >= end)
  310. goto error;
  311. first = *(data++);
  312. sent = (first & 0x3) == 1;
  313. if (sent)
  314. data++;
  315. cur_len = *(data++);
  316. if (data + cur_len >= end)
  317. goto error;
  318. if (cur_len) {
  319. cur_len = (cur_len + 1) / 2;
  320. wms_decode_address(sent ? "receiver" : "sender", data, cur_len);
  321. data += cur_len + 1;
  322. }
  323. if (data + 3 >= end)
  324. goto error;
  325. /* Protocol ID */
  326. if (*(data++) != 0)
  327. goto error;
  328. /* Data Encoding */
  329. dcs = *(data++);
  330. if (dcs & 0x10)
  331. blobmsg_add_u32(&status, "class", (dcs & 3));
  332. if (sent) {
  333. /* Message validity */
  334. data++;
  335. } else {
  336. if (data + 6 >= end)
  337. goto error;
  338. str = blobmsg_alloc_string_buffer(&status, "timestamp", 32);
  339. /* year */
  340. *(str++) = '2';
  341. *(str++) = '0';
  342. str = pdu_add_semioctet(str, data[0]);
  343. /* month */
  344. *(str++) = '-';
  345. str = pdu_add_semioctet(str, data[1]);
  346. /* day */
  347. *(str++) = '-';
  348. str = pdu_add_semioctet(str, data[2]);
  349. /* hour */
  350. *(str++) = ' ';
  351. str = pdu_add_semioctet(str, data[3]);
  352. /* minute */
  353. *(str++) = ':';
  354. str = pdu_add_semioctet(str, data[4]);
  355. /* second */
  356. *(str++) = ':';
  357. str = pdu_add_semioctet(str, data[5]);
  358. *str = 0;
  359. blobmsg_add_string_buffer(&status);
  360. data += 7;
  361. }
  362. int message_len = *(data++);
  363. int udh_len = 0;
  364. int bit_offset = 0;
  365. /* User Data Header */
  366. if (first & 0x40) {
  367. udh_len = decode_udh(data);
  368. data += udh_len;
  369. bit_offset = udh_len % 7;
  370. }
  371. if (data >= end)
  372. goto error;
  373. switch(dcs & 0x0c) {
  374. case 0x00:
  375. /* 7 bit GSM alphabet */
  376. message_len = message_len - CEILDIV(udh_len * 8, 7);
  377. message_len = MIN(message_len, CEILDIV((end - data) * 8, 7));
  378. decode_7bit_field("text", data, message_len, bit_offset);
  379. break;
  380. case 0x04:
  381. /* 8 bit data */
  382. message_len = MIN(message_len - udh_len, end - data);
  383. blobmsg_add_hex(&status, "data", data, message_len);
  384. break;
  385. case 0x08:
  386. /* 16 bit UCS-2 string */
  387. message_len = MIN(message_len - udh_len, end - data);
  388. blobmsg_add_hex(&status, "ucs-2", data, message_len);
  389. break;
  390. default:
  391. goto error;
  392. }
  393. blobmsg_close_table(&status, c);
  394. return;
  395. error:
  396. blobmsg_close_table(&status, c);
  397. fprintf(stderr, "There was an error reading message.\n");
  398. }
  399. static enum qmi_cmd_result
  400. cmd_wms_get_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  401. {
  402. char *err;
  403. int id;
  404. id = strtoul(arg, &err, 10);
  405. if (err && *err) {
  406. uqmi_add_error("Invalid message ID");
  407. return QMI_CMD_EXIT;
  408. }
  409. gmreq.data.message_memory_storage_id.memory_index = id;
  410. qmi_set_wms_raw_read_request(msg, &gmreq);
  411. return QMI_CMD_REQUEST;
  412. }
  413. static void cmd_wms_get_raw_message_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
  414. {
  415. struct qmi_wms_raw_read_response res;
  416. unsigned char *data;
  417. char *str;
  418. int i;
  419. qmi_parse_wms_raw_read_response(msg, &res);
  420. data = (unsigned char *) res.data.raw_message_data.raw_data;
  421. str = blobmsg_alloc_string_buffer(&status, NULL, res.data.raw_message_data.raw_data_n * 3);
  422. for (i = 0; i < res.data.raw_message_data.raw_data_n; i++) {
  423. str += sprintf(str, &" %02x"[i ? 0 : 1], data[i]);
  424. }
  425. blobmsg_add_string_buffer(&status);
  426. }
  427. #define cmd_wms_get_raw_message_prepare cmd_wms_get_message_prepare
  428. static struct {
  429. const char *smsc;
  430. const char *target;
  431. bool flash;
  432. } _send;
  433. #define cmd_wms_send_message_smsc_cb no_cb
  434. static enum qmi_cmd_result
  435. cmd_wms_send_message_smsc_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  436. {
  437. _send.smsc = arg;
  438. return QMI_CMD_DONE;
  439. }
  440. #define cmd_wms_send_message_target_cb no_cb
  441. static enum qmi_cmd_result
  442. cmd_wms_send_message_target_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  443. {
  444. _send.target = arg;
  445. return QMI_CMD_DONE;
  446. }
  447. #define cmd_wms_send_message_flash_cb no_cb
  448. static enum qmi_cmd_result
  449. cmd_wms_send_message_flash_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  450. {
  451. _send.flash = true;
  452. return QMI_CMD_DONE;
  453. }
  454. static int
  455. pdu_encode_semioctet(unsigned char *dest, const char *str)
  456. {
  457. int len = 0;
  458. bool lower = true;
  459. while (*str) {
  460. char digit = *str - '0';
  461. if (lower)
  462. dest[len] = 0xf0 | digit;
  463. else
  464. dest[len++] &= (digit << 4) | 0xf;
  465. lower = !lower;
  466. str++;
  467. }
  468. return lower ? len : (len + 1);
  469. }
  470. static int
  471. pdu_encode_7bit_str(unsigned char *data, const char *str)
  472. {
  473. unsigned char c;
  474. int len = 0;
  475. int ofs = 0;
  476. while(1) {
  477. c = *(str++) & 0x7f;
  478. if (!c)
  479. break;
  480. switch(ofs) {
  481. case 0:
  482. data[len] = c;
  483. break;
  484. default:
  485. data[len++] |= c << (8 - ofs);
  486. data[len] = c >> ofs;
  487. break;
  488. }
  489. ofs = (ofs + 1) % 8;
  490. }
  491. return len + 1;
  492. }
  493. static int
  494. pdu_encode_number(unsigned char *dest, const char *str, bool smsc)
  495. {
  496. unsigned char format;
  497. bool ascii = false;
  498. int len = 0;
  499. int i;
  500. dest[len++] = 0;
  501. if (*str == '+') {
  502. str++;
  503. format = 0x91;
  504. } else {
  505. format = 0x81;
  506. }
  507. for (i = 0; str[i]; i++) {
  508. if (str[i] >= '0' && str[i] <= '9')
  509. continue;
  510. ascii = true;
  511. break;
  512. }
  513. if (ascii)
  514. format |= 0x40;
  515. dest[len++] = format;
  516. if (!ascii)
  517. len += pdu_encode_semioctet(&dest[len], str);
  518. else
  519. len += pdu_encode_7bit_str(&dest[len], str);
  520. if (smsc)
  521. dest[0] = len - 1;
  522. else
  523. dest[0] = strlen(str);
  524. return len;
  525. }
  526. static int
  527. pdu_encode_data(unsigned char *dest, const char *str)
  528. {
  529. int len = 0;
  530. dest[len++] = 0;
  531. len += pdu_encode_7bit_str(&dest[len], str);
  532. dest[0] = strlen(str);
  533. return len;
  534. }
  535. #define cmd_wms_send_message_cb no_cb
  536. static enum qmi_cmd_result
  537. cmd_wms_send_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  538. {
  539. static unsigned char buf[512];
  540. static struct qmi_wms_raw_send_request mreq = {
  541. QMI_INIT_SEQUENCE(raw_message_data,
  542. .format = QMI_WMS_MESSAGE_FORMAT_GSM_WCDMA_POINT_TO_POINT,
  543. .raw_data = buf,
  544. ),
  545. };
  546. unsigned char *cur = buf;
  547. unsigned char first_octet = 0x11;
  548. unsigned char protocol_id = 0x00;
  549. unsigned char dcs = 0x00;
  550. if (!_send.target || !*_send.target) {
  551. uqmi_add_error("Missing argument");
  552. return QMI_CMD_EXIT;
  553. }
  554. if ((_send.smsc && strlen(_send.smsc) > 16) || strlen(_send.target) > 16 || strlen(arg) > 160) {
  555. uqmi_add_error("Argument too long");
  556. return QMI_CMD_EXIT;
  557. }
  558. if (_send.flash)
  559. dcs |= 0x10;
  560. if (!_send.smsc || !*_send.smsc)
  561. *(cur++) = 0;
  562. else
  563. cur += pdu_encode_number(cur, _send.smsc, true);
  564. *(cur++) = first_octet;
  565. *(cur++) = 0; /* reference */
  566. cur += pdu_encode_number(cur, _send.target, false);
  567. *(cur++) = protocol_id;
  568. *(cur++) = dcs;
  569. *(cur++) = 0xff; /* validity */
  570. cur += pdu_encode_data(cur, arg);
  571. mreq.data.raw_message_data.raw_data_n = cur - buf;
  572. qmi_set_wms_raw_send_request(msg, &mreq);
  573. return QMI_CMD_REQUEST;
  574. }