1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stddef.h>
8 #include "mesh/slist.h"
9 #include "mesh/glue.h"
10 #include "mesh/access.h"
11 #include "mesh/testing.h"
12
13 #include "net.h"
14 #include "access.h"
15 #include "foundation.h"
16 #include "lpn.h"
17 #include "transport.h"
18 #include "testing.h"
19
20 static sys_slist_t cb_slist;
21
bt_test_cb_register(struct bt_test_cb * cb)22 void bt_test_cb_register(struct bt_test_cb *cb)
23 {
24 sys_slist_append(&cb_slist, &cb->node);
25 }
26
bt_test_cb_unregister(struct bt_test_cb * cb)27 void bt_test_cb_unregister(struct bt_test_cb *cb)
28 {
29 sys_slist_find_and_remove(&cb_slist, &cb->node);
30 }
31
bt_test_mesh_net_recv(u8_t ttl,u8_t ctl,u16_t src,u16_t dst,const void * payload,size_t payload_len)32 void bt_test_mesh_net_recv(u8_t ttl, u8_t ctl, u16_t src, u16_t dst,
33 const void *payload, size_t payload_len)
34 {
35 struct bt_test_cb *cb;
36 SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
37 if (cb->mesh_net_recv) {
38 cb->mesh_net_recv(ttl, ctl, src, dst, payload,
39 payload_len);
40 }
41 }
42 }
43
bt_test_mesh_model_bound(u16_t addr,struct bt_mesh_model * model,u16_t key_idx)44 void bt_test_mesh_model_bound(u16_t addr, struct bt_mesh_model *model,
45 u16_t key_idx)
46 {
47 struct bt_test_cb *cb;
48 SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
49 if (cb->mesh_model_bound) {
50 cb->mesh_model_bound(addr, model, key_idx);
51 }
52 }
53 }
54
bt_test_mesh_model_unbound(u16_t addr,struct bt_mesh_model * model,u16_t key_idx)55 void bt_test_mesh_model_unbound(u16_t addr, struct bt_mesh_model *model,
56 u16_t key_idx)
57 {
58 struct bt_test_cb *cb;
59 SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
60 if (cb->mesh_model_unbound) {
61 cb->mesh_model_unbound(addr, model, key_idx);
62 }
63 }
64 }
65
bt_test_mesh_prov_invalid_bearer(u8_t opcode)66 void bt_test_mesh_prov_invalid_bearer(u8_t opcode)
67 {
68 struct bt_test_cb *cb;
69 SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
70 if (cb->mesh_prov_invalid_bearer) {
71 cb->mesh_prov_invalid_bearer(opcode);
72 }
73 }
74 }
75
bt_test_mesh_trans_incomp_timer_exp(void)76 void bt_test_mesh_trans_incomp_timer_exp(void)
77 {
78 struct bt_test_cb *cb;
79 SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
80 if (cb->mesh_trans_incomp_timer_exp) {
81 cb->mesh_trans_incomp_timer_exp();
82 }
83 }
84 }
85
bt_test_mesh_lpn_group_add(u16_t group)86 int bt_test_mesh_lpn_group_add(u16_t group)
87 {
88 bt_mesh_lpn_group_add(group);
89 return 0;
90 }
91
bt_test_mesh_lpn_group_remove(u16_t * groups,size_t groups_count)92 int bt_test_mesh_lpn_group_remove(u16_t *groups, size_t groups_count)
93 {
94 bt_mesh_lpn_group_del(groups, groups_count);
95 return 0;
96 }
97
bt_test_mesh_rpl_clear(void)98 int bt_test_mesh_rpl_clear(void)
99 {
100 bt_mesh_rpl_clear();
101 return 0;
102 }
103
bt_test_print_credentials(void)104 void bt_test_print_credentials(void)
105 {
106 int i;
107 u8_t nid;
108 const u8_t *enc;
109 const u8_t *priv;
110 struct bt_mesh_subnet *sub;
111 struct bt_mesh_app_key *app_key;
112 printf("IV Index: %08lx\n", (long) bt_mesh.iv_index);
113 printf("Dev key: %s\n", bt_hex(bt_mesh.dev_key, 16)); // 16:len
114
115 for (i = 0; i < MYNEWT_VAL(BLE_MESH_SUBNET_COUNT); ++i) {
116 if (bt_mesh.app_keys[i].net_idx == BT_MESH_KEY_UNUSED) {
117 continue;
118 }
119
120 sub = &bt_mesh.sub[i];
121 printf("Subnet: %d\n", i);
122 printf("\tNetKeyIdx: %04x\n",
123 sub->net_idx);
124 printf("\tNetKey: %s\n",
125 bt_hex(sub->keys[sub->kr_flag].net, 16)); // 16:len
126 }
127
128 for (i = 0; i < MYNEWT_VAL(BLE_MESH_APP_KEY_COUNT); ++i) {
129 if (bt_mesh.app_keys[i].net_idx == BT_MESH_KEY_UNUSED) {
130 continue;
131 }
132
133 sub = &bt_mesh.sub[i];
134 app_key = &bt_mesh.app_keys[i];
135 printf("AppKey: %d\n", i);
136 printf("\tNetKeyIdx: %04x\n",
137 app_key->net_idx);
138 printf("\tAppKeyIdx: %04x\n",
139 app_key->app_idx);
140 printf("\tAppKey: %s\n",
141 bt_hex(app_key->keys[sub->kr_flag].val, 16)); // 16:len
142 }
143
144 for (i = 0; i < MYNEWT_VAL(BLE_MESH_SUBNET_COUNT); ++i) {
145 if (bt_mesh.sub[i].net_idx == BT_MESH_KEY_UNUSED) {
146 continue;
147 }
148
149 if (friend_cred_get(&bt_mesh.sub[i], BT_MESH_ADDR_UNASSIGNED,
150 &nid, &enc, &priv)) {
151 return;
152 }
153
154 printf("Friend cred: %d\n", i);
155 printf("\tNetKeyIdx: %04x\n",
156 bt_mesh.sub[i].net_idx);
157 printf("\tNID: %02x\n", nid);
158 printf("\tEncKey: %s\n",
159 bt_hex(enc, 16)); // 16:len
160 printf("\tPrivKey: %s\n",
161 bt_hex(priv, 16)); // 16:len
162 }
163 }
164
bt_test_shell_init(void)165 int bt_test_shell_init(void)
166 {
167 #if MYNEWT_VAL(BLE_MESH_SHELL)
168 return cmd_mesh_init(0, NULL);
169 #else
170 return -ENOTSUP;
171 #endif
172 }
173
bt_test_bind_app_key_to_model(struct bt_mesh_model * model,u16_t key_idx,u16_t id)174 int bt_test_bind_app_key_to_model(struct bt_mesh_model *model, u16_t key_idx, u16_t id)
175 {
176 struct bt_mesh_model *found_model;
177 found_model = bt_mesh_model_find(bt_mesh_model_elem(model), id);
178 if (!found_model) {
179 return STATUS_INVALID_MODEL;
180 }
181
182 return mod_bind(found_model, key_idx);
183 }