commands.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <stdio.h>
  22. #include <string.h>
  23. #include <strings.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <libubox/blobmsg.h>
  27. #include <libubox/blobmsg_json.h>
  28. #include "uqmi.h"
  29. #include "commands.h"
  30. static struct blob_buf status;
  31. bool single_line = false;
  32. static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
  33. {
  34. }
  35. static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
  36. {
  37. struct qmi_ctl_get_version_info_response res;
  38. void *c;
  39. char name_buf[16];
  40. int i;
  41. qmi_parse_ctl_get_version_info_response(msg, &res);
  42. c = blobmsg_open_table(&status, NULL);
  43. for (i = 0; i < res.data.service_list_n; i++) {
  44. sprintf(name_buf, "service_%d", res.data.service_list[i].service);
  45. blobmsg_printf(&status, name_buf, "%d,%d",
  46. res.data.service_list[i].major_version,
  47. res.data.service_list[i].minor_version);
  48. }
  49. blobmsg_close_table(&status, c);
  50. }
  51. static enum qmi_cmd_result
  52. cmd_version_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  53. {
  54. qmi_set_ctl_get_version_info_request(msg);
  55. return QMI_CMD_REQUEST;
  56. }
  57. #define cmd_sync_cb no_cb
  58. static enum qmi_cmd_result
  59. cmd_sync_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  60. {
  61. qmi_set_ctl_sync_request(msg);
  62. return QMI_CMD_REQUEST;
  63. }
  64. #define cmd_get_client_id_cb no_cb
  65. static enum qmi_cmd_result
  66. cmd_get_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  67. {
  68. QmiService svc = qmi_service_get_by_name(arg);
  69. if (svc < 0) {
  70. fprintf(stderr, "Invalid service name '%s'\n", arg);
  71. return QMI_CMD_EXIT;
  72. }
  73. if (qmi_service_connect(qmi, svc, -1)) {
  74. fprintf(stderr, "Failed to connect to service\n");
  75. return QMI_CMD_EXIT;
  76. }
  77. printf("%d\n", qmi_service_get_client_id(qmi, svc));
  78. return QMI_CMD_DONE;
  79. }
  80. #define cmd_set_client_id_cb no_cb
  81. static enum qmi_cmd_result
  82. cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  83. {
  84. QmiService svc;
  85. int id;
  86. char *s;
  87. s = strchr(arg, ',');
  88. if (!s) {
  89. fprintf(stderr, "Invalid argument\n");
  90. return QMI_CMD_EXIT;
  91. }
  92. *s = 0;
  93. s++;
  94. id = strtoul(s, &s, 0);
  95. if (s && *s) {
  96. fprintf(stderr, "Invalid argument\n");
  97. return QMI_CMD_EXIT;
  98. }
  99. svc = qmi_service_get_by_name(arg);
  100. if (svc < 0) {
  101. fprintf(stderr, "Invalid service name '%s'\n", arg);
  102. return QMI_CMD_EXIT;
  103. }
  104. if (qmi_service_connect(qmi, svc, id)) {
  105. fprintf(stderr, "Failed to connect to service\n");
  106. return QMI_CMD_EXIT;
  107. }
  108. return QMI_CMD_DONE;
  109. }
  110. static int
  111. qmi_get_array_idx(const char **array, int size, const char *str)
  112. {
  113. int i;
  114. for (i = 0; i < size; i++) {
  115. if (!array[i])
  116. continue;
  117. if (!strcmp(array[i], str))
  118. return i;
  119. }
  120. return -1;
  121. }
  122. #define cmd_ctl_set_data_format_cb no_cb
  123. static enum qmi_cmd_result
  124. cmd_ctl_set_data_format_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
  125. {
  126. struct qmi_ctl_set_data_format_request sreq = {};
  127. const char *modes[] = {
  128. [QMI_CTL_DATA_LINK_PROTOCOL_802_3] = "802.3",
  129. [QMI_CTL_DATA_LINK_PROTOCOL_RAW_IP] = "raw-ip",
  130. };
  131. int mode = qmi_get_array_idx(modes, ARRAY_SIZE(modes), arg);
  132. if (mode < 0) {
  133. uqmi_add_error("Invalid mode (modes: 802.3, raw-ip)");
  134. return QMI_CMD_EXIT;
  135. }
  136. qmi_set_ctl_set_data_format_request(msg, &sreq);
  137. return QMI_CMD_DONE;
  138. }
  139. #include "commands-wds.c"
  140. #include "commands-dms.c"
  141. #include "commands-nas.c"
  142. #include "commands-wms.c"
  143. #include "commands-wda.c"
  144. #include "commands-uim.c"
  145. #define __uqmi_command(_name, _optname, _arg, _type) \
  146. [__UQMI_COMMAND_##_name] = { \
  147. .name = #_optname, \
  148. .type = _type, \
  149. .prepare = cmd_##_name##_prepare, \
  150. .cb = cmd_##_name##_cb, \
  151. }
  152. const struct uqmi_cmd_handler uqmi_cmd_handler[__UQMI_COMMAND_LAST] = {
  153. __uqmi_commands
  154. };
  155. #undef __uqmi_command
  156. static struct uqmi_cmd *cmds;
  157. static int n_cmds;
  158. void uqmi_add_command(char *arg, int cmd)
  159. {
  160. int idx = n_cmds++;
  161. cmds = realloc(cmds, n_cmds * sizeof(*cmds));
  162. cmds[idx].handler = &uqmi_cmd_handler[cmd];
  163. cmds[idx].arg = arg;
  164. }
  165. static void uqmi_print_result(struct blob_attr *data)
  166. {
  167. char *str;
  168. if (!blob_len(data))
  169. return;
  170. str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
  171. if (!str)
  172. return;
  173. printf("%s\n", str);
  174. free(str);
  175. }
  176. static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
  177. {
  178. static struct qmi_request req;
  179. char *buf = qmi->buf;
  180. int i;
  181. for (i = 0; i < n_cmds; i++) {
  182. enum qmi_cmd_result res;
  183. bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
  184. bool do_break = false;
  185. if (cmd_option != option)
  186. continue;
  187. blob_buf_init(&status, 0);
  188. if (cmds[i].handler->type > QMI_SERVICE_CTL &&
  189. qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
  190. uqmi_add_error("Failed to connect to service");
  191. res = QMI_CMD_EXIT;
  192. } else {
  193. res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
  194. }
  195. if (res == QMI_CMD_REQUEST) {
  196. qmi_request_start(qmi, &req, cmds[i].handler->cb);
  197. req.no_error_cb = true;
  198. if (qmi_request_wait(qmi, &req)) {
  199. uqmi_add_error(qmi_get_error_str(req.ret));
  200. do_break = true;
  201. }
  202. } else if (res == QMI_CMD_EXIT) {
  203. do_break = true;
  204. }
  205. uqmi_print_result(status.head);
  206. if (do_break)
  207. return false;
  208. }
  209. return true;
  210. }
  211. int uqmi_add_error(const char *msg)
  212. {
  213. blobmsg_add_string(&status, NULL, msg);
  214. return QMI_CMD_EXIT;
  215. }
  216. bool uqmi_run_commands(struct qmi_dev *qmi)
  217. {
  218. bool ret;
  219. ret = __uqmi_run_commands(qmi, true) &&
  220. __uqmi_run_commands(qmi, false);
  221. free(cmds);
  222. cmds = NULL;
  223. n_cmds = 0;
  224. return ret;
  225. }