bnep-tester.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 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/bnep.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. struct test_data {
  28. struct mgmt *mgmt;
  29. uint16_t mgmt_index;
  30. struct hciemu *hciemu;
  31. enum hciemu_type hciemu_type;
  32. const void *test_data;
  33. unsigned int io_id;
  34. uint16_t conn_handle;
  35. };
  36. struct rfcomm_client_data {
  37. uint8_t server_channel;
  38. uint8_t client_channel;
  39. int expected_connect_err;
  40. const uint8_t *send_data;
  41. const uint8_t *read_data;
  42. uint16_t data_len;
  43. };
  44. struct rfcomm_server_data {
  45. uint8_t server_channel;
  46. uint8_t client_channel;
  47. bool expected_status;
  48. const uint8_t *send_data;
  49. const uint8_t *read_data;
  50. uint16_t data_len;
  51. };
  52. static void print_debug(const char *str, void *user_data)
  53. {
  54. const char *prefix = user_data;
  55. tester_print("%s%s", prefix, str);
  56. }
  57. static void read_info_callback(uint8_t status, uint16_t length,
  58. const void *param, void *user_data)
  59. {
  60. struct test_data *data = tester_get_data();
  61. const struct mgmt_rp_read_info *rp = param;
  62. char addr[18];
  63. uint16_t manufacturer;
  64. uint32_t supported_settings, current_settings;
  65. tester_print("Read Info callback");
  66. tester_print(" Status: 0x%02x", status);
  67. if (status || !param) {
  68. tester_pre_setup_failed();
  69. return;
  70. }
  71. ba2str(&rp->bdaddr, addr);
  72. manufacturer = btohs(rp->manufacturer);
  73. supported_settings = btohl(rp->supported_settings);
  74. current_settings = btohl(rp->current_settings);
  75. tester_print(" Address: %s", addr);
  76. tester_print(" Version: 0x%02x", rp->version);
  77. tester_print(" Manufacturer: 0x%04x", manufacturer);
  78. tester_print(" Supported settings: 0x%08x", supported_settings);
  79. tester_print(" Current settings: 0x%08x", current_settings);
  80. tester_print(" Class: 0x%02x%02x%02x",
  81. rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
  82. tester_print(" Name: %s", rp->name);
  83. tester_print(" Short name: %s", rp->short_name);
  84. if (strcmp(hciemu_get_address(data->hciemu), addr)) {
  85. tester_pre_setup_failed();
  86. return;
  87. }
  88. tester_pre_setup_complete();
  89. }
  90. static void index_added_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 Added callback");
  95. tester_print(" Index: 0x%04x", index);
  96. data->mgmt_index = index;
  97. mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
  98. read_info_callback, NULL, NULL);
  99. }
  100. static void index_removed_callback(uint16_t index, uint16_t length,
  101. const void *param, void *user_data)
  102. {
  103. struct test_data *data = tester_get_data();
  104. tester_print("Index Removed callback");
  105. tester_print(" Index: 0x%04x", index);
  106. if (index != data->mgmt_index)
  107. return;
  108. mgmt_unregister_index(data->mgmt, data->mgmt_index);
  109. mgmt_unref(data->mgmt);
  110. data->mgmt = NULL;
  111. tester_post_teardown_complete();
  112. }
  113. static void read_index_list_callback(uint8_t status, uint16_t length,
  114. const void *param, void *user_data)
  115. {
  116. struct test_data *data = tester_get_data();
  117. tester_print("Read Index List callback");
  118. tester_print(" Status: 0x%02x", status);
  119. if (status || !param) {
  120. tester_pre_setup_failed();
  121. return;
  122. }
  123. mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
  124. index_added_callback, NULL, NULL);
  125. mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
  126. index_removed_callback, NULL, NULL);
  127. data->hciemu = hciemu_new(data->hciemu_type);
  128. if (!data->hciemu) {
  129. tester_warn("Failed to setup HCI emulation");
  130. tester_pre_setup_failed();
  131. }
  132. if (tester_use_debug())
  133. hciemu_set_debug(data->hciemu, print_debug, "hciemu: ", NULL);
  134. tester_print("New hciemu instance created");
  135. }
  136. static void test_pre_setup(const void *test_data)
  137. {
  138. struct test_data *data = tester_get_data();
  139. data->mgmt = mgmt_new_default();
  140. if (!data->mgmt) {
  141. tester_warn("Failed to setup management interface");
  142. tester_pre_setup_failed();
  143. return;
  144. }
  145. if (tester_use_debug())
  146. mgmt_set_debug(data->mgmt, print_debug, "mgmt: ", NULL);
  147. mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
  148. read_index_list_callback, NULL, NULL);
  149. }
  150. static void test_post_teardown(const void *test_data)
  151. {
  152. struct test_data *data = tester_get_data();
  153. if (data->io_id > 0) {
  154. g_source_remove(data->io_id);
  155. data->io_id = 0;
  156. }
  157. hciemu_unref(data->hciemu);
  158. data->hciemu = NULL;
  159. }
  160. static void test_data_free(void *test_data)
  161. {
  162. struct test_data *data = test_data;
  163. free(data);
  164. }
  165. static void client_connectable_complete(uint16_t opcode, uint8_t status,
  166. const void *param, uint8_t len,
  167. void *user_data)
  168. {
  169. switch (opcode) {
  170. case BT_HCI_CMD_WRITE_SCAN_ENABLE:
  171. break;
  172. default:
  173. return;
  174. }
  175. tester_print("Client set connectable status 0x%02x", status);
  176. if (status)
  177. tester_setup_failed();
  178. else
  179. tester_setup_complete();
  180. }
  181. static void setup_powered_client_callback(uint8_t status, uint16_t length,
  182. const void *param, void *user_data)
  183. {
  184. struct test_data *data = tester_get_data();
  185. struct bthost *bthost;
  186. if (status != MGMT_STATUS_SUCCESS) {
  187. tester_setup_failed();
  188. return;
  189. }
  190. tester_print("Controller powered on");
  191. bthost = hciemu_client_get_host(data->hciemu);
  192. bthost_set_cmd_complete_cb(bthost, client_connectable_complete, data);
  193. bthost_write_scan_enable(bthost, 0x03);
  194. }
  195. static void setup_powered_client(const void *test_data)
  196. {
  197. struct test_data *data = tester_get_data();
  198. unsigned char param[] = { 0x01 };
  199. tester_print("Powering on controller");
  200. mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
  201. sizeof(param), param, setup_powered_client_callback,
  202. NULL, NULL);
  203. }
  204. static void test_basic(const void *test_data)
  205. {
  206. int sk;
  207. sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
  208. if (sk < 0) {
  209. tester_warn("Can't create socket: %s (%d)", strerror(errno),
  210. errno);
  211. tester_test_failed();
  212. return;
  213. }
  214. close(sk);
  215. tester_test_passed();
  216. }
  217. #define test_bnep(name, data, setup, func) \
  218. do { \
  219. struct test_data *user; \
  220. user = malloc(sizeof(struct test_data)); \
  221. if (!user) \
  222. break; \
  223. user->hciemu_type = HCIEMU_TYPE_BREDR; \
  224. user->test_data = data; \
  225. user->io_id = 0; \
  226. tester_add_full(name, data, \
  227. test_pre_setup, setup, func, NULL, \
  228. test_post_teardown, 2, user, test_data_free); \
  229. } while (0)
  230. int main(int argc, char *argv[])
  231. {
  232. tester_init(&argc, &argv);
  233. test_bnep("Basic BNEP Socket - Success", NULL,
  234. setup_powered_client, test_basic);
  235. return tester_run();
  236. }