bt_manager_shell.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2019 Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief bt manager.
  9. */
  10. #define SYS_LOG_NO_NEWLINE
  11. #define SYS_LOG_DOMAIN "bt shell"
  12. #include <os_common_api.h>
  13. #include <zephyr.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <msg_manager.h>
  18. #include <mem_manager.h>
  19. #include <bt_manager.h>
  20. #include "bt_manager_inner.h"
  21. #include <sys_event.h>
  22. #include <btservice_api.h>
  23. #include <shell/shell.h>
  24. #include <acts_bluetooth/host_interface.h>
  25. #define DEVICE_NAME "ble_test_308a"
  26. #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
  27. struct bt_data ad[] = {
  28. BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL)),
  29. BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
  30. };
  31. #if CONFIG_BT_BR_ACTS
  32. static int shell_dump_bt_info(const struct shell *shell, size_t argc, char *argv[])
  33. {
  34. bt_manager_dump_info();
  35. return 0;
  36. }
  37. static int shell_cmd_br_test(const struct shell *shell, size_t argc, char *argv[])
  38. {
  39. if (argc < 2) {
  40. SYS_LOG_INF("Used: bt br_test on/off");
  41. return 0;
  42. }
  43. if (!strcmp(argv[1], "on")) {
  44. bt_manager_set_visible(true);
  45. bt_manager_set_connectable(true);
  46. SYS_LOG_INF("Br test on");
  47. } else if (!strcmp(argv[1], "off")) {
  48. bt_manager_set_visible(false);
  49. bt_manager_set_connectable(false);
  50. SYS_LOG_INF("Br test off");
  51. } else if (!strcmp(argv[1], "visible") && argc > 2) {
  52. if (!strcmp(argv[2], "on")) {
  53. bt_manager_set_visible(true);
  54. } else if (!strcmp(argv[2], "off")) {
  55. bt_manager_set_visible(false);
  56. } else {
  57. SYS_LOG_INF("Unknow visible cmd %s", argv[2]);
  58. }
  59. } else if (!strcmp(argv[1], "connectable") && argc > 2) {
  60. if (!strcmp(argv[2], "on")) {
  61. bt_manager_set_connectable(true);
  62. } else if (!strcmp(argv[2], "off")) {
  63. bt_manager_set_connectable(false);
  64. } else {
  65. SYS_LOG_INF("Unknow connectable cmd %s", argv[2]);
  66. }
  67. } else if (!strcmp(argv[1], "passkey") && argc > 2) {
  68. void btsrv_adapter_passkey_display(bool mode);
  69. if (!strcmp(argv[2], "on")) {
  70. btsrv_adapter_passkey_display(true);
  71. } else if (!strcmp(argv[2], "off")) {
  72. btsrv_adapter_passkey_display(false);
  73. } else {
  74. SYS_LOG_INF("Unknow connectable cmd %s", argv[2]);
  75. }
  76. } else {
  77. SYS_LOG_INF("Unknow cmd %s", argv[1]);
  78. }
  79. return 0;
  80. }
  81. #endif
  82. static int shell_cmd_ble_test(const struct shell *shell, size_t argc, char *argv[])
  83. {
  84. #ifdef CONFIG_BT_BLE
  85. if (argc < 2) {
  86. SYS_LOG_INF("Used: bt ble_test on/off");
  87. return 0;
  88. }
  89. if (!strcmp(argv[1], "on")) {
  90. bt_manager_ble_adv_start();
  91. SYS_LOG_INF("ble test on");
  92. } else if (!strcmp(argv[1], "off")) {
  93. bt_manager_ble_adv_stop();
  94. SYS_LOG_INF("ble test off");
  95. } else if (!strcmp(argv[1], "adv_set")) {
  96. size_t len = ARRAY_SIZE(ad);
  97. bt_manager_ble_set_adv_data(ad, len, NULL, 0);
  98. SYS_LOG_INF("ble test adv_reset");
  99. } else if (!strcmp(argv[1], "passkey") && argc > 2) {
  100. void bt_manager_ble_passkey_display(bool mode);
  101. if (!strcmp(argv[2], "on")) {
  102. bt_manager_ble_passkey_display(true);
  103. } else if (!strcmp(argv[2], "off")) {
  104. bt_manager_ble_passkey_display(false);
  105. } else {
  106. SYS_LOG_INF("Unknow connectable cmd %s", argv[2]);
  107. }
  108. } else {
  109. SYS_LOG_INF("Unknow cmd %s", argv[1]);
  110. }
  111. #endif
  112. return 0;
  113. }
  114. static int shell_cmd_set_ble_speed(const struct shell *shell, size_t argc, char *argv[])
  115. {
  116. uint32_t val;
  117. if (argc < 2) {
  118. SYS_LOG_INF("Used: bt ble_speed 0/1/2");
  119. return 0;
  120. }
  121. val = strtoul(argv[1], NULL, 16);
  122. if (val <= 2) {
  123. bt_manager_ble_super_set_speed(val);
  124. } else {
  125. SYS_LOG_INF("Unknow speed %s", argv[1]);
  126. }
  127. return 0;
  128. }
  129. #if CONFIG_BT_BR_ACTS
  130. static int shell_cmd_send_hfp_cmd(const struct shell *shell, size_t argc, char *argv[])
  131. {
  132. char cmd_buf[32];
  133. uint8_t len;
  134. if (argc < 2) {
  135. SYS_LOG_INF("Used: bt hfp cmd");
  136. return 0;
  137. }
  138. memset(cmd_buf, 0, sizeof(cmd_buf));
  139. if (!strcmp(argv[1], "DTMF")) {
  140. /* Send DTMF one by one,
  141. * Cmd: bt hfp DTMF 1/2/3/...
  142. */
  143. len = strlen("AT+VTS=");
  144. memcpy(cmd_buf, "AT+VTS=", len);
  145. if (argc >= 3) {
  146. cmd_buf[len] = argv[2][0];
  147. } else {
  148. cmd_buf[len] = '1';
  149. }
  150. SYS_LOG_INF("Hfp send DTMF %s", &cmd_buf[len]);
  151. #ifdef CONFIG_BT_HFP_HF
  152. bt_manager_hfp_send_at_command(cmd_buf, 1);
  153. #endif
  154. }
  155. return 0;
  156. }
  157. #endif
  158. SHELL_STATIC_SUBCMD_SET_CREATE(bt_cmds,
  159. #if CONFIG_BT_BR_ACTS
  160. SHELL_CMD(info, NULL, "dump bt info", shell_dump_bt_info),
  161. SHELL_CMD(br_test, NULL, "br power test", shell_cmd_br_test),
  162. #endif
  163. SHELL_CMD(ble_test, NULL, "ble power test", shell_cmd_ble_test),
  164. SHELL_CMD(ble_speed, NULL, "Set ble speed", shell_cmd_set_ble_speed),
  165. #if CONFIG_BT_BR_ACTS
  166. SHELL_CMD(hfp, NULL, "send hfp cmd", shell_cmd_send_hfp_cmd),
  167. #endif
  168. SHELL_SUBCMD_SET_END
  169. );
  170. static int cmd_bt_shell(const struct shell *shell, size_t argc, char **argv)
  171. {
  172. if (argc == 1) {
  173. shell_help(shell);
  174. return SHELL_CMD_HELP_PRINTED;
  175. }
  176. shell_error(shell, "%s unknown parameter: %s", argv[0], argv[1]);
  177. return -EINVAL;
  178. }
  179. SHELL_CMD_REGISTER(bt, &bt_cmds, "Bluetooth manager commands", cmd_bt_shell);