raw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /**
  2. * Copyright (c) 2018 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf_cli.h"
  41. #include "cli_utils.h"
  42. #include "raw.h"
  43. /* =============================================================================================
  44. * Raw utility functions
  45. * =============================================================================================
  46. */
  47. static void print_hex(uint8_t * p_data, size_t size);
  48. /** @brief Callback function to print hex data using currently selected format.
  49. * @param p_data Pointer to data to print.
  50. * @param size Number of bytes to print.
  51. */
  52. typedef void (*print_fn_t)(uint8_t * p_data, size_t size);
  53. /** @brief Function that prints hex in currently selected format.
  54. */
  55. print_fn_t m_print_fn = print_hex;
  56. /** @brief Prints in raw hex format. @see print_fn_t.
  57. */
  58. static void print_hex(uint8_t * p_data, size_t size)
  59. {
  60. nrf_cli_t const * p_cli = cli_get();
  61. size_t i;
  62. size_t col = 0;
  63. for (i = 0; i < size; i++)
  64. {
  65. col = i & 0xF;
  66. if (col == 0)
  67. {
  68. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, " ");
  69. }
  70. nrf_cli_fprintf(p_cli,
  71. NRF_CLI_NORMAL,
  72. "%02X%s",
  73. p_data[i],
  74. (col < 0xF ? " " : ""));
  75. if (col == 0xF)
  76. {
  77. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
  78. }
  79. }
  80. if (col != 0xF)
  81. {
  82. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
  83. }
  84. }
  85. /** @brief Prints in C format. @see print_fn_t.
  86. */
  87. static void print_c(uint8_t * p_data, size_t size)
  88. {
  89. nrf_cli_t const * p_cli = cli_get();
  90. size_t i;
  91. size_t col = 0;
  92. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "{\n");
  93. for (i = 0; i < size; i++)
  94. {
  95. col = i & 0xF;
  96. if (col == 0)
  97. {
  98. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, " ");
  99. }
  100. nrf_cli_fprintf(p_cli,
  101. NRF_CLI_NORMAL,
  102. "0x%02X,%s",
  103. p_data[i],
  104. (col < 0xF ? " " : ""));
  105. if (col == 0xF)
  106. {
  107. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
  108. }
  109. }
  110. if (col != 0xF)
  111. {
  112. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
  113. }
  114. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "}\n");
  115. }
  116. /** @brief Prints in CLI format. @see print_fn_t.
  117. */
  118. static void print_cli(uint8_t * p_data, size_t size)
  119. {
  120. nrf_cli_t const * p_cli = cli_get();
  121. size_t i;
  122. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\"");
  123. for (i = 0; i < size; i++)
  124. {
  125. nrf_cli_fprintf(p_cli,
  126. NRF_CLI_NORMAL,
  127. "%02X%s",
  128. p_data[i],
  129. (i < (size - 1) ? " " : ""));
  130. }
  131. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\"\n");
  132. }
  133. void raw_print_hex(uint8_t * p_data, size_t size)
  134. {
  135. m_print_fn(p_data, size);
  136. }
  137. /** @brief Returns true if data inside p_var is printable.
  138. */
  139. static bool is_printable(var_t * p_var)
  140. {
  141. size_t i;
  142. for (i = 0; i < p_var->data_size; i++)
  143. {
  144. if ((VAR_DATA(p_var)[i] < ' ') || (VAR_DATA(p_var)[i] >= '\x7F'))
  145. {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. /* =============================================================================================
  152. * Raw type definition
  153. * =============================================================================================
  154. */
  155. /** @brief Callback function to print raw variables. @see var_print_fn_t.
  156. */
  157. static void raw_print(var_t * p_var, bool short_info)
  158. {
  159. size_t i;
  160. nrf_cli_t const * p_cli = cli_get();
  161. bool printable = is_printable(p_var);
  162. if (short_info)
  163. {
  164. if (printable)
  165. {
  166. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "Printable ASCII");
  167. }
  168. else
  169. {
  170. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "Binary");
  171. }
  172. }
  173. else
  174. {
  175. raw_print_hex(VAR_DATA(p_var), p_var->data_size);
  176. if (printable)
  177. {
  178. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, " \"");
  179. for (i = 0; i < p_var->data_size; i++)
  180. {
  181. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%c", VAR_DATA(p_var)[i]);
  182. }
  183. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\"\n");
  184. }
  185. }
  186. }
  187. const var_type_t g_raw_type =
  188. {
  189. .p_description = "Raw data buffer",
  190. .free_fn = NULL,
  191. .print_fn = raw_print,
  192. .p_tag = NULL,
  193. };
  194. /* =============================================================================================
  195. * Sub commands used in arguments
  196. * =============================================================================================
  197. */
  198. VAR_REGISTER_SUB_COMMANDS(m_subcmd_var, NULL);
  199. static void subcmd_var_all_get(size_t idx, nrf_cli_static_entry_t * p_static);
  200. NRF_CLI_CREATE_DYNAMIC_CMD(m_subcmd_var_all, subcmd_var_all_get);
  201. static void subcmd_var_all_get(size_t idx, nrf_cli_static_entry_t * p_static)
  202. {
  203. var_dynamic_cmd(&m_subcmd_var_all, idx, p_static);
  204. }
  205. /* =============================================================================================
  206. * Raw data manipulation functions
  207. * =============================================================================================
  208. */
  209. /** @brief Command to create new raw variable. See command help for details.
  210. */
  211. static bool cmd_raw(nrf_cli_t const * p_cli, size_t argc, char ** argv)
  212. {
  213. size_t i;
  214. size_t total_size;
  215. var_t * p_output_var;
  216. var_t ** pp_args;
  217. UNUSED_PARAMETER(p_cli);
  218. UNUSED_PARAMETER(argv);
  219. CLI_ARGS_CHECK(argc >= 2);
  220. pp_args = (var_t **)var_temp_alloc(sizeof(var_t *) * (argc - 2));
  221. CLI_CHECK(pp_args != NULL);
  222. total_size = 0;
  223. for (i = 1; i < argc - 1; i++)
  224. {
  225. pp_args[i - 1] = cli_input_arg_get(i, &g_raw_type);
  226. CLI_CHECK(pp_args[i - 1] != NULL);
  227. total_size += pp_args[i - 1]->data_size;
  228. }
  229. p_output_var = cli_output_arg_get(argc - 1, &g_raw_type, total_size);
  230. CLI_CHECK(p_output_var != NULL);
  231. total_size = 0;
  232. for (i = 1; i < argc - 1; i++)
  233. {
  234. memcpy(&(VAR_DATA(p_output_var)[total_size]),
  235. VAR_DATA(pp_args[i - 1]),
  236. pp_args[i - 1]->data_size);
  237. total_size += pp_args[i - 1]->data_size;
  238. }
  239. return true;
  240. }
  241. CLI_CMD_REGISTER(raw, &m_subcmd_var_all, cmd_raw,
  242. "Create variable with raw data.\n"
  243. "Usage:\n"
  244. " raw [data1 [data2 ...]] output\n"
  245. "Arguments:\n"
  246. " IN dataN Multiple raw data arguments that will be concatenated.\n"
  247. " OUT output Newly created raw data.\n"
  248. );
  249. /** @brief Command to add data to raw variable. @see cmd_prepend, @see cmd_append.
  250. */
  251. static bool cmd_data_add(nrf_cli_t const * p_cli, size_t argc, char ** argv, bool append)
  252. {
  253. size_t i;
  254. var_t ** pp_args = NULL;
  255. UNUSED_PARAMETER(argv);
  256. CLI_ARGS_CHECK(argc >= 2);
  257. size_t total_size = 0;
  258. pp_args = var_temp_alloc(sizeof(var_t *) * (argc - 2));
  259. CLI_CHECK(pp_args != NULL);
  260. for (i = 1; i < argc - 1; i++)
  261. {
  262. pp_args[i - 1] = cli_input_arg_get(i, &g_raw_type);
  263. CLI_CHECK(pp_args[i - 1] != NULL);
  264. total_size += pp_args[i - 1]->data_size;
  265. }
  266. var_t * p_var = cli_input_arg_get(argc - 1, &g_raw_type);
  267. if (p_var == NULL)
  268. {
  269. nrf_cli_fprintf(p_cli,
  270. NRF_CLI_WARNING,
  271. "Variable does not exists or has different type. Creating new one.\n");
  272. }
  273. else
  274. {
  275. total_size += p_var->data_size;
  276. }
  277. var_t * p_output_var = cli_output_arg_get(argc - 1, &g_raw_type, total_size);
  278. CLI_CHECK(p_output_var != NULL);
  279. total_size = 0;
  280. if (append && (p_var != NULL))
  281. {
  282. memcpy(&(VAR_DATA(p_output_var)[total_size]), VAR_DATA(p_var), p_var->data_size);
  283. total_size += p_var->data_size;
  284. }
  285. for (i = 0; i < argc - 2; i++)
  286. {
  287. memcpy(&(VAR_DATA(p_output_var)[total_size]), VAR_DATA(pp_args[i]), pp_args[i]->data_size);
  288. total_size += pp_args[i]->data_size;
  289. }
  290. if (!append && (p_var != NULL))
  291. {
  292. memcpy(&(VAR_DATA(p_output_var)[total_size]), VAR_DATA(p_var), p_var->data_size);
  293. }
  294. if (p_var != NULL)
  295. {
  296. UNUSED_RETURN_VALUE(var_delete(p_var->name, false));
  297. }
  298. return true;
  299. }
  300. /** @brief Command to prepend data to raw variable. See command help for details.
  301. */
  302. static bool cmd_prepend(nrf_cli_t const * p_cli, size_t argc, char ** argv)
  303. {
  304. return cmd_data_add(p_cli, argc, argv, false);
  305. }
  306. CLI_CMD_REGISTER(prepend, &m_subcmd_var_all, cmd_prepend,
  307. "Prepend raw data to a raw variable.\n"
  308. "Usage:\n"
  309. " prepend [data1 [data2 ...]] variable\n"
  310. "Arguments:\n"
  311. " IN dataN Multiple raw data arguments that are to be concatenated.\n"
  312. " IN/OUT variable Variable where data is to be prepended.\n"
  313. );
  314. /** @brief Command to append data to raw variable. See command help for details.
  315. */
  316. static bool cmd_append(nrf_cli_t const * p_cli, size_t argc, char ** argv)
  317. {
  318. return cmd_data_add(p_cli, argc, argv, true);
  319. }
  320. CLI_CMD_REGISTER(append, &m_subcmd_var_all, cmd_append,
  321. "Append raw data to a raw variable.\n"
  322. "Usage:\n"
  323. " append [data1 [data2 ...]] variable\n"
  324. "Arguments:\n"
  325. " IN dataN Multiple raw data arguments that are to be concatenated.\n"
  326. " IN/OUT variable Variable where data is to be appended.\n"
  327. );
  328. /** @brief Command to get portion of a raw variable. See command help for details.
  329. */
  330. static bool cmd_sub(nrf_cli_t const * p_cli, size_t argc, char ** argv)
  331. {
  332. var_t * p_src = NULL;
  333. char * p_arg_end;
  334. CLI_ARGS_CHECK(argc == 5);
  335. p_src = cli_input_arg_get(1, &g_raw_type);
  336. CLI_CHECK(p_src != NULL);
  337. int from = (int)strtol(argv[2], &p_arg_end, 0);
  338. int count;
  339. if (p_arg_end[0] != '\0')
  340. {
  341. nrf_cli_fprintf(p_cli, NRF_CLI_WARNING, "Number at argument 2 is invalid.\n");
  342. return false;
  343. }
  344. if (from < 0)
  345. {
  346. from = p_src->data_size + from;
  347. }
  348. if ((from < 0) || (from > (int)p_src->data_size))
  349. {
  350. nrf_cli_fprintf(p_cli, NRF_CLI_WARNING, "Starting point outside the data.\n");
  351. return false;
  352. }
  353. if (strcmp(argv[3], "-") == 0)
  354. {
  355. count = p_src->data_size - from;
  356. }
  357. else
  358. {
  359. count = (int)strtol(argv[3], &p_arg_end, 0);
  360. if (p_arg_end[0] != '\0')
  361. {
  362. nrf_cli_fprintf(p_cli, NRF_CLI_WARNING, "Number at argument 3 is invalid.\n");
  363. return false;
  364. }
  365. }
  366. if (count < 0)
  367. {
  368. count = p_src->data_size + count - from;
  369. }
  370. if ((count < 0) || (from + count > (int)p_src->data_size))
  371. {
  372. nrf_cli_fprintf(p_cli, NRF_CLI_WARNING, "Endpoint outside the data.\n");
  373. return false;
  374. }
  375. var_t * p_output_var = cli_output_arg_get(4, &g_raw_type, count);
  376. CLI_CHECK(p_output_var != NULL);
  377. memcpy(VAR_DATA(p_output_var), &(VAR_DATA(p_src)[from]), count);
  378. return true;
  379. }
  380. CLI_CMD_REGISTER(sub, &m_subcmd_var, cmd_sub,
  381. "Create variable from part of a raw data.\n"
  382. "Usage:\n"
  383. " sub source start_index bytes_count output\n"
  384. "Arguments:\n"
  385. " IN source Source raw data to cut.\n"
  386. " IN start_index Index of byte in the source where cut starts.\n"
  387. " May be negative to count from the end of data.\n"
  388. " IN bytes_count Number of bytes to cut. Negative value indicate where cut\n"
  389. " is to end, counting from the end. Value \"-\" cuts to the end.\n"
  390. " OUT output Variable where portion of the source is to be placed.\n"
  391. );
  392. /** @brief Command to change format of hex printing. See command help for details.
  393. */
  394. static bool cmd_format(nrf_cli_t const * p_cli, size_t argc, char ** argv)
  395. {
  396. UNUSED_PARAMETER(p_cli);
  397. CLI_ARGS_CHECK(argc == 2);
  398. if (strcmp(argv[1], "hex") == 0)
  399. {
  400. m_print_fn = print_hex;
  401. }
  402. else if (strcmp(argv[1], "c") == 0)
  403. {
  404. m_print_fn = print_c;
  405. }
  406. else if (strcmp(argv[1], "cli") == 0)
  407. {
  408. m_print_fn = print_cli;
  409. }
  410. else
  411. {
  412. CLI_ARGS_CHECK(false);
  413. }
  414. return true;
  415. }
  416. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_format)
  417. {
  418. NRF_CLI_CMD(hex, NULL, "", NULL),
  419. NRF_CLI_CMD(c, NULL, "", NULL),
  420. NRF_CLI_CMD(cli, NULL, "", NULL),
  421. NRF_CLI_SUBCMD_SET_END
  422. };
  423. CLI_CMD_REGISTER(format, &m_sub_format, cmd_format,
  424. "Change printing format of the raw data.\n"
  425. "Usage:\n"
  426. " format type\n"
  427. "Arguments:\n"
  428. " IN type Source raw data to cut. Following values are allowed:\n"
  429. " hex - raw hex format (default),\n"
  430. " c - C source code,\n"
  431. " cli - string that can be copy-pasted back to CLI.\n"
  432. );