test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #include <string.h>
  9. #include <errno.h>
  10. #include "adv.h"
  11. #include "scan.h"
  12. #include "mesh.h"
  13. #include "test.h"
  14. #include "crypto.h"
  15. #include "access.h"
  16. #include "foundation.h"
  17. #include "mesh/main.h"
  18. #include "transport.h"
  19. #include "proxy_common.h"
  20. #include "proxy_server.h"
  21. #if CONFIG_BLE_MESH_SELF_TEST
  22. int bt_mesh_test(void)
  23. {
  24. return 0;
  25. }
  26. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK
  27. int bt_mesh_device_auto_enter_network(struct bt_mesh_device_network_info *info)
  28. {
  29. const struct bt_mesh_comp *comp = NULL;
  30. struct bt_mesh_model *model = NULL;
  31. struct bt_mesh_elem *elem = NULL;
  32. struct bt_mesh_app_keys *keys = NULL;
  33. struct bt_mesh_app_key *key = NULL;
  34. struct bt_mesh_subnet *sub = NULL;
  35. int i, j, k;
  36. int err = 0;
  37. if (info == NULL || !BLE_MESH_ADDR_IS_UNICAST(info->unicast_addr) ||
  38. !BLE_MESH_ADDR_IS_GROUP(info->group_addr)) {
  39. return -EINVAL;
  40. }
  41. bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
  42. /* The device becomes a node and enters the network */
  43. err = bt_mesh_provision(info->net_key, info->net_idx, info->flags, info->iv_index,
  44. info->unicast_addr, info->dev_key);
  45. if (err) {
  46. BT_ERR("bt_mesh_provision() failed (err %d)", err);
  47. return err;
  48. }
  49. /* Adds application key to device */
  50. sub = bt_mesh_subnet_get(info->net_idx);
  51. if (!sub) {
  52. BT_ERR("Invalid NetKeyIndex 0x%04x", info->net_idx);
  53. return -ENODEV;
  54. }
  55. for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
  56. key = &bt_mesh.app_keys[i];
  57. if (key->net_idx == BLE_MESH_KEY_UNUSED) {
  58. break;
  59. }
  60. }
  61. if (i == ARRAY_SIZE(bt_mesh.app_keys)) {
  62. BT_ERR("Failed to allocate AppKey, 0x%04x", info->app_idx);
  63. return -ENOMEM;
  64. }
  65. keys = sub->kr_flag ? &key->keys[1] : &key->keys[0];
  66. if (bt_mesh_app_id(info->app_key, &keys->id)) {
  67. BT_ERR("Failed to calculate AID, 0x%04x", info->app_idx);
  68. return -EIO;
  69. }
  70. key->net_idx = info->net_idx;
  71. key->app_idx = info->app_idx;
  72. memcpy(keys->val, info->app_key, 16);
  73. /* Binds AppKey with all non-config models, adds group address to all these models */
  74. comp = bt_mesh_comp_get();
  75. if (!comp) {
  76. BT_ERR("Invalid composition data");
  77. return -ENODEV;
  78. }
  79. for (i = 0; i < comp->elem_count; i++) {
  80. elem = &comp->elem[i];
  81. for (j = 0; j < elem->model_count; j++) {
  82. model = &elem->models[j];
  83. if (model->id == BLE_MESH_MODEL_ID_CFG_SRV ||
  84. model->id == BLE_MESH_MODEL_ID_CFG_CLI) {
  85. continue;
  86. }
  87. for (k = 0; k < ARRAY_SIZE(model->keys); k++) {
  88. if (model->keys[k] == BLE_MESH_KEY_UNUSED) {
  89. model->keys[k] = info->app_idx;
  90. break;
  91. }
  92. }
  93. for (k = 0; k < ARRAY_SIZE(model->groups); k++) {
  94. if (model->groups[k] == BLE_MESH_ADDR_UNASSIGNED) {
  95. model->groups[k] = info->group_addr;
  96. break;
  97. }
  98. }
  99. }
  100. for (j = 0; j < elem->vnd_model_count; j++) {
  101. model = &elem->vnd_models[j];
  102. for (k = 0; k < ARRAY_SIZE(model->keys); k++) {
  103. if (model->keys[k] == BLE_MESH_KEY_UNUSED) {
  104. model->keys[k] = info->app_idx;
  105. break;
  106. }
  107. }
  108. for (k = 0; k < ARRAY_SIZE(model->groups); k++) {
  109. if (model->groups[k] == BLE_MESH_ADDR_UNASSIGNED) {
  110. model->groups[k] = info->group_addr;
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. return 0;
  117. }
  118. #endif /* CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK */
  119. #if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST
  120. int bt_mesh_test_update_white_list(struct bt_mesh_white_list *wl)
  121. {
  122. int err = 0;
  123. if (wl == NULL) {
  124. BT_ERR("%s, Invalid parameter", __func__);
  125. return -EINVAL;
  126. }
  127. BT_INFO("%s, addr %s, addr_type 0x%02x", wl->add_remove ? "Add" : "Remove",
  128. bt_hex(wl->remote_bda, BLE_MESH_ADDR_LEN), wl->addr_type);
  129. err = bt_le_update_white_list(wl);
  130. if (err) {
  131. BT_ERR("Failed to update white list");
  132. }
  133. return err;
  134. }
  135. int bt_mesh_test_start_scanning(bool wl_en)
  136. {
  137. BT_INFO("Scan with filter policy %s", wl_en ? "enabled" : "disabled");
  138. if (wl_en) {
  139. return bt_mesh_scan_with_wl_enable();
  140. }
  141. return bt_mesh_scan_enable();
  142. }
  143. int bt_mesh_test_stop_scanning(void)
  144. {
  145. return bt_mesh_scan_disable();
  146. }
  147. #endif /* CONFIG_BLE_MESH_TEST_USE_WHITE_LIST */
  148. bt_mesh_test_net_pdu_cb_t net_pdu_test_cb;
  149. void bt_mesh_test_register_net_pdu_cb(bt_mesh_test_net_pdu_cb_t cb)
  150. {
  151. net_pdu_test_cb = cb;
  152. }
  153. void bt_mesh_test_set_seq(uint32_t seq)
  154. {
  155. if (seq > 0xFFFFFF) {
  156. BT_ERR("Invalid SEQ 0x%08x", seq);
  157. return;
  158. }
  159. bt_mesh.seq = seq;
  160. }
  161. #endif /* CONFIG_BLE_MESH_SELF_TEST */