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