agent.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <inttypes.h>
  17. #include <lib/bluetooth.h>
  18. #include "src/shared/shell.h"
  19. #include "tools/mesh/agent.h"
  20. #define AGENT_PROMPT COLOR_BLUE "[mesh-agent]" COLOR_OFF "# "
  21. struct input_request {
  22. oob_type_t type;
  23. uint16_t len;
  24. agent_input_cb cb;
  25. void *user_data;
  26. };
  27. static struct input_request pending_request = {NONE, 0, NULL, NULL};
  28. bool agent_completion(void)
  29. {
  30. if (pending_request.type == NONE)
  31. return false;
  32. return true;
  33. }
  34. static void reset_input_request(void)
  35. {
  36. pending_request.type = NONE;
  37. pending_request.len = 0;
  38. pending_request.cb = NULL;
  39. pending_request.user_data = NULL;
  40. }
  41. static bool str2hex(const char *str, uint16_t in_len, uint8_t *out,
  42. uint16_t out_len)
  43. {
  44. uint16_t i;
  45. if (in_len < out_len * 2)
  46. return false;
  47. for (i = 0; i < out_len; i++) {
  48. if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1)
  49. return false;
  50. }
  51. return true;
  52. }
  53. static void response_hexadecimal(const char *input, void *user_data)
  54. {
  55. uint8_t buf[MAX_HEXADECIMAL_OOB_LEN];
  56. uint16_t len = pending_request.len;
  57. if (!str2hex(input, strlen(input), buf, pending_request.len) ) {
  58. bt_shell_printf("Incorrect input: expecting %d hex octets\n",
  59. pending_request.len);
  60. len = 0;
  61. }
  62. if (pending_request.cb)
  63. pending_request.cb(HEXADECIMAL, buf, len,
  64. pending_request.user_data);
  65. reset_input_request();
  66. }
  67. static void response_decimal(const char *input, void *user_data)
  68. {
  69. uint8_t buf[DECIMAL_OOB_LEN];
  70. uint16_t len = DECIMAL_OOB_LEN;
  71. if (strlen(input) > pending_request.len)
  72. len = 0;
  73. bt_put_be32(atoi(input), buf);
  74. if (pending_request.cb)
  75. pending_request.cb(DECIMAL, buf, len,
  76. pending_request.user_data);
  77. reset_input_request();
  78. }
  79. static void response_ascii(const char *input, void *user_data)
  80. {
  81. if (pending_request.cb)
  82. pending_request.cb(ASCII, (uint8_t *) input, strlen(input),
  83. pending_request.user_data);
  84. reset_input_request();
  85. }
  86. static bool request_hexadecimal(uint16_t len)
  87. {
  88. if (len > MAX_HEXADECIMAL_OOB_LEN)
  89. return false;
  90. bt_shell_printf("Request hexadecimal key (hex %d octets)\n", len);
  91. bt_shell_prompt_input(AGENT_PROMPT, "Enter key (hex number):",
  92. response_hexadecimal, NULL);
  93. return true;
  94. }
  95. static uint32_t power_ten(uint8_t power)
  96. {
  97. uint32_t ret = 1;
  98. while (power--)
  99. ret *= 10;
  100. return ret;
  101. }
  102. static bool request_decimal(const char *desc, uint16_t len)
  103. {
  104. if (!desc)
  105. bt_shell_printf("Request decimal key (0 - %d)\n",
  106. power_ten(len) - 1);
  107. else
  108. bt_shell_printf("%s (0 - %d)\n", desc, power_ten(len) - 1);
  109. bt_shell_prompt_input(AGENT_PROMPT, "Enter decimal number:",
  110. response_decimal, NULL);
  111. return true;
  112. }
  113. static bool request_ascii(uint16_t len)
  114. {
  115. if (len > MAX_ASCII_OOB_LEN)
  116. return false;
  117. bt_shell_printf("Request ASCII key (max characters %d)\n", len);
  118. bt_shell_prompt_input("mesh", "Enter key (ascii string):",
  119. response_ascii, NULL);
  120. return true;
  121. }
  122. bool agent_input_request(oob_type_t type, uint16_t max_len, const char *desc,
  123. agent_input_cb cb, void *user_data)
  124. {
  125. bool result;
  126. if (pending_request.type != NONE)
  127. return false;
  128. switch (type) {
  129. case HEXADECIMAL:
  130. result = request_hexadecimal(max_len);
  131. break;
  132. case DECIMAL:
  133. result = request_decimal(desc, max_len);
  134. break;
  135. case ASCII:
  136. result = request_ascii(max_len);
  137. break;
  138. case NONE:
  139. case OUTPUT:
  140. default:
  141. return false;
  142. };
  143. if (result) {
  144. pending_request.type = type;
  145. pending_request.len = max_len;
  146. pending_request.cb = cb;
  147. pending_request.user_data = user_data;
  148. return true;
  149. }
  150. return false;
  151. }
  152. static void response_output(const char *input, void *user_data)
  153. {
  154. reset_input_request();
  155. }
  156. bool agent_output_request(const char* str)
  157. {
  158. if (pending_request.type != NONE)
  159. return false;
  160. pending_request.type = OUTPUT;
  161. bt_shell_prompt_input("mesh", str, response_output, NULL);
  162. return true;
  163. }
  164. void agent_output_request_cancel(void)
  165. {
  166. if (pending_request.type != OUTPUT)
  167. return;
  168. pending_request.type = NONE;
  169. bt_shell_release_prompt("");
  170. }