userchan-tester.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014-2015 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <stdbool.h>
  18. #include <glib.h>
  19. #include "lib/bluetooth.h"
  20. #include "lib/hci.h"
  21. #include "lib/mgmt.h"
  22. #include "monitor/bt.h"
  23. #include "emulator/bthost.h"
  24. #include "emulator/hciemu.h"
  25. #include "src/shared/tester.h"
  26. #include "src/shared/mgmt.h"
  27. #include "src/shared/hci.h"
  28. #include "src/shared/util.h"
  29. struct test_data {
  30. struct mgmt *mgmt;
  31. uint16_t mgmt_index;
  32. struct hciemu *hciemu;
  33. enum hciemu_type hciemu_type;
  34. const void *test_data;
  35. unsigned int remove_id;
  36. struct bt_hci *hci;
  37. uint32_t current_settings;
  38. };
  39. static void mgmt_debug(const char *str, void *user_data)
  40. {
  41. const char *prefix = user_data;
  42. tester_print("%s%s", prefix, str);
  43. }
  44. static void read_info_callback(uint8_t status, uint16_t length,
  45. const void *param, void *user_data)
  46. {
  47. struct test_data *data = tester_get_data();
  48. const struct mgmt_rp_read_info *rp = param;
  49. char addr[18];
  50. uint16_t manufacturer;
  51. uint32_t supported_settings, current_settings;
  52. tester_print("Read Info callback");
  53. tester_print(" Status: 0x%02x", status);
  54. if (status || !param) {
  55. tester_pre_setup_failed();
  56. return;
  57. }
  58. ba2str(&rp->bdaddr, addr);
  59. manufacturer = btohs(rp->manufacturer);
  60. supported_settings = btohl(rp->supported_settings);
  61. current_settings = btohl(rp->current_settings);
  62. tester_print(" Address: %s", addr);
  63. tester_print(" Version: 0x%02x", rp->version);
  64. tester_print(" Manufacturer: 0x%04x", manufacturer);
  65. tester_print(" Supported settings: 0x%08x", supported_settings);
  66. tester_print(" Current settings: 0x%08x", current_settings);
  67. tester_print(" Class: 0x%02x%02x%02x",
  68. rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
  69. tester_print(" Name: %s", rp->name);
  70. tester_print(" Short name: %s", rp->short_name);
  71. data->current_settings = current_settings;
  72. if (strcmp(hciemu_get_address(data->hciemu), addr)) {
  73. tester_pre_setup_failed();
  74. return;
  75. }
  76. tester_pre_setup_complete();
  77. }
  78. static void index_added_callback(uint16_t index, uint16_t length,
  79. const void *param, void *user_data)
  80. {
  81. struct test_data *data = tester_get_data();
  82. tester_print("Index Added callback");
  83. tester_print(" Index: 0x%04x", index);
  84. if (data->mgmt_index != MGMT_INDEX_NONE)
  85. return;
  86. data->mgmt_index = index;
  87. mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
  88. read_info_callback, NULL, NULL);
  89. }
  90. static void index_removed_callback(uint16_t index, uint16_t length,
  91. const void *param, void *user_data)
  92. {
  93. struct test_data *data = tester_get_data();
  94. tester_print("Index Removed callback");
  95. tester_print(" Index: 0x%04x", index);
  96. if (index != data->mgmt_index)
  97. return;
  98. if (data->remove_id) {
  99. mgmt_unregister(data->mgmt, data->remove_id);
  100. data->remove_id = 0;
  101. tester_test_passed();
  102. return;
  103. }
  104. mgmt_unregister_index(data->mgmt, data->mgmt_index);
  105. mgmt_unref(data->mgmt);
  106. data->mgmt = NULL;
  107. tester_post_teardown_complete();
  108. }
  109. static void read_index_list_callback(uint8_t status, uint16_t length,
  110. const void *param, void *user_data)
  111. {
  112. struct test_data *data = tester_get_data();
  113. tester_print("Read Index List callback");
  114. tester_print(" Status: 0x%02x", status);
  115. if (status || !param) {
  116. tester_pre_setup_failed();
  117. return;
  118. }
  119. mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
  120. index_added_callback, NULL, NULL);
  121. data->hciemu = hciemu_new(data->hciemu_type);
  122. if (!data->hciemu) {
  123. tester_warn("Failed to setup HCI emulation");
  124. tester_pre_setup_failed();
  125. }
  126. tester_print("New hciemu instance created");
  127. }
  128. static void test_pre_setup(const void *test_data)
  129. {
  130. struct test_data *data = tester_get_data();
  131. data->mgmt = mgmt_new_default();
  132. if (!data->mgmt) {
  133. tester_warn("Failed to setup management interface");
  134. tester_pre_setup_failed();
  135. return;
  136. }
  137. if (tester_use_debug())
  138. mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
  139. mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
  140. read_index_list_callback, NULL, NULL);
  141. }
  142. static void test_post_teardown(const void *test_data)
  143. {
  144. struct test_data *data = tester_get_data();
  145. mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, data->mgmt_index,
  146. index_removed_callback,
  147. NULL, NULL);
  148. hciemu_unref(data->hciemu);
  149. data->hciemu = NULL;
  150. }
  151. static void test_data_free(void *test_data)
  152. {
  153. struct test_data *data = test_data;
  154. free(data);
  155. }
  156. static void setup_powered_client_callback(uint8_t status, uint16_t length,
  157. const void *param, void *user_data)
  158. {
  159. if (status != MGMT_STATUS_SUCCESS) {
  160. tester_setup_failed();
  161. return;
  162. }
  163. tester_print("Controller powered on");
  164. tester_setup_complete();
  165. }
  166. static void setup_powered(const void *test_data)
  167. {
  168. struct test_data *data = tester_get_data();
  169. unsigned char param[] = { 0x01 };
  170. tester_print("Powering on controller");
  171. mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
  172. sizeof(param), param, setup_powered_client_callback,
  173. NULL, NULL);
  174. }
  175. static void toggle_powered(const void *test_data);
  176. static void toggle_powered_client_callback(uint8_t status, uint16_t length,
  177. const void *param, void *user_data)
  178. {
  179. bool power = PTR_TO_INT(user_data);
  180. if (status != MGMT_STATUS_SUCCESS) {
  181. tester_setup_failed();
  182. return;
  183. }
  184. tester_print("Controller powered %s", power ? "on" : "off");
  185. if (power)
  186. toggle_powered(false);
  187. else
  188. tester_setup_complete();
  189. }
  190. static void toggle_powered(const void *test_data)
  191. {
  192. struct test_data *data = tester_get_data();
  193. bool power = PTR_TO_INT(test_data);
  194. unsigned char param[1];
  195. param[0] = power ? 0x01 : 0x00;
  196. tester_print("Powering %s controller", power ? "on" : "off");
  197. mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
  198. sizeof(param), param, toggle_powered_client_callback,
  199. INT_TO_PTR(power), NULL);
  200. }
  201. static void test_open_success(const void *test_data)
  202. {
  203. struct test_data *data = tester_get_data();
  204. struct bt_hci *hci;
  205. data->remove_id = mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED,
  206. data->mgmt_index,
  207. index_removed_callback,
  208. NULL, NULL);
  209. hci = bt_hci_new_user_channel(data->mgmt_index);
  210. if (hci) {
  211. bt_hci_unref(hci);
  212. return;
  213. }
  214. mgmt_unregister(data->mgmt, data->remove_id);
  215. data->remove_id = 0;
  216. tester_test_failed();
  217. }
  218. static void test_open_failed(const void *test_data)
  219. {
  220. struct test_data *data = tester_get_data();
  221. struct bt_hci *hci;
  222. hci = bt_hci_new_user_channel(data->mgmt_index);
  223. if (!hci) {
  224. tester_test_passed();
  225. return;
  226. }
  227. bt_hci_unref(hci);
  228. tester_test_failed();
  229. }
  230. static void close_read_info_callback(uint8_t status, uint16_t length,
  231. const void *param, void *user_data)
  232. {
  233. const struct mgmt_rp_read_info *rp = param;
  234. uint32_t current_settings;
  235. tester_print("Read Info callback");
  236. tester_print(" Status: 0x%02x", status);
  237. if (status || !param) {
  238. tester_test_failed();
  239. return;
  240. }
  241. current_settings = btohl(rp->current_settings);
  242. if (current_settings & MGMT_SETTING_POWERED) {
  243. tester_print("Controller is powered");
  244. tester_test_failed();
  245. return;
  246. }
  247. tester_test_passed();
  248. }
  249. static void setup_channel_open(const void *test_data)
  250. {
  251. struct test_data *data = tester_get_data();
  252. /* Check power off */
  253. if (data->current_settings & MGMT_SETTING_POWERED) {
  254. tester_print("Controller is powered");
  255. tester_setup_failed();
  256. return;
  257. }
  258. /* Open Channel */
  259. data->hci = bt_hci_new_user_channel(data->mgmt_index);
  260. if (!data->hci) {
  261. mgmt_unregister(data->mgmt, data->remove_id);
  262. data->remove_id = 0;
  263. tester_setup_failed();
  264. return;
  265. }
  266. tester_print("User Channel Opened");
  267. tester_setup_complete();
  268. }
  269. static void test_close_success(const void *test_data)
  270. {
  271. struct test_data *data = tester_get_data();
  272. tester_print("Close User Channel");
  273. bt_hci_unref(data->hci);
  274. /* Check if power is off */
  275. mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
  276. close_read_info_callback, NULL, NULL);
  277. }
  278. #define test_user(name, data, setup, func) \
  279. do { \
  280. struct test_data *user; \
  281. user = malloc(sizeof(struct test_data)); \
  282. if (!user) \
  283. break; \
  284. user->hciemu_type = HCIEMU_TYPE_BREDR; \
  285. user->mgmt_index = MGMT_INDEX_NONE; \
  286. user->test_data = data; \
  287. user->remove_id = 0; \
  288. tester_add_full(name, data, \
  289. test_pre_setup, setup, func, NULL, \
  290. test_post_teardown, 2, user, test_data_free); \
  291. } while (0)
  292. int main(int argc, char *argv[])
  293. {
  294. tester_init(&argc, &argv);
  295. test_user("User channel open - Success", NULL,
  296. NULL, test_open_success);
  297. test_user("User channel open - Failed", NULL,
  298. setup_powered, test_open_failed);
  299. test_user("User channel open - Power Toggle Success", INT_TO_PTR(true),
  300. toggle_powered, test_open_success);
  301. test_user("User channel close - Success", NULL,
  302. setup_channel_open, test_close_success);
  303. return tester_run();
  304. }