1 // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string.h>
16 #include <errno.h>
17
18 #include "btc_ble_mesh_config_model.h"
19 #include "foundation.h"
20 #include "esp_ble_mesh_config_model_api.h"
21
22 #if CONFIG_BLE_MESH_CFG_CLI
23 #include "cfg_cli.h"
24
25 /* Configuration Client Model related functions */
26
btc_ble_mesh_config_client_cb_to_app(esp_ble_mesh_cfg_client_cb_event_t event,esp_ble_mesh_cfg_client_cb_param_t * param)27 static inline void btc_ble_mesh_config_client_cb_to_app(esp_ble_mesh_cfg_client_cb_event_t event,
28 esp_ble_mesh_cfg_client_cb_param_t *param)
29 {
30 esp_ble_mesh_cfg_client_cb_t btc_ble_mesh_cb =
31 (esp_ble_mesh_cfg_client_cb_t)btc_profile_cb_get(BTC_PID_CONFIG_CLIENT);
32 if (btc_ble_mesh_cb) {
33 btc_ble_mesh_cb(event, param);
34 }
35 }
36
btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)37 void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
38 {
39 btc_ble_mesh_config_client_args_t *dst = (btc_ble_mesh_config_client_args_t *)p_dest;
40 btc_ble_mesh_config_client_args_t *src = (btc_ble_mesh_config_client_args_t *)p_src;
41
42 if (!msg || !dst || !src) {
43 BT_ERR("%s, Invalid parameter", __func__);
44 return;
45 }
46
47 switch (msg->act) {
48 case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: {
49 dst->cfg_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t));
50 if (dst->cfg_client_get_state.params) {
51 memcpy(dst->cfg_client_get_state.params, src->cfg_client_get_state.params,
52 sizeof(esp_ble_mesh_client_common_param_t));
53 } else {
54 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
55 break;
56 }
57 if (src->cfg_client_get_state.get_state) {
58 dst->cfg_client_get_state.get_state = (esp_ble_mesh_cfg_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_cfg_client_get_state_t));
59 if (dst->cfg_client_get_state.get_state) {
60 memcpy(dst->cfg_client_get_state.get_state, src->cfg_client_get_state.get_state,
61 sizeof(esp_ble_mesh_cfg_client_get_state_t));
62 } else {
63 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
64 }
65 }
66 break;
67 }
68 case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: {
69 dst->cfg_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t));
70 if (dst->cfg_client_set_state.params) {
71 memcpy(dst->cfg_client_set_state.params, src->cfg_client_set_state.params,
72 sizeof(esp_ble_mesh_client_common_param_t));
73 } else {
74 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
75 break;
76 }
77 if (src->cfg_client_set_state.set_state) {
78 dst->cfg_client_set_state.set_state = (esp_ble_mesh_cfg_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_cfg_client_set_state_t));
79 if (dst->cfg_client_set_state.set_state) {
80 memcpy(dst->cfg_client_set_state.set_state, src->cfg_client_set_state.set_state,
81 sizeof(esp_ble_mesh_cfg_client_set_state_t));
82 } else {
83 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
84 }
85 }
86 break;
87 }
88 default:
89 BT_DBG("%s, Unknown act %d", __func__, msg->act);
90 break;
91 }
92 }
93
btc_ble_mesh_config_client_arg_deep_free(btc_msg_t * msg)94 static void btc_ble_mesh_config_client_arg_deep_free(btc_msg_t *msg)
95 {
96 btc_ble_mesh_config_client_args_t *arg = NULL;
97
98 if (!msg || !msg->arg) {
99 BT_ERR("%s, Invalid parameter", __func__);
100 return;
101 }
102
103 arg = (btc_ble_mesh_config_client_args_t *)(msg->arg);
104
105 switch (msg->act) {
106 case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE:
107 if (arg->cfg_client_get_state.params) {
108 bt_mesh_free(arg->cfg_client_get_state.params);
109 }
110 if (arg->cfg_client_get_state.get_state) {
111 bt_mesh_free(arg->cfg_client_get_state.get_state);
112 }
113 break;
114 case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE:
115 if (arg->cfg_client_set_state.params) {
116 bt_mesh_free(arg->cfg_client_set_state.params);
117 }
118 if (arg->cfg_client_set_state.set_state) {
119 bt_mesh_free(arg->cfg_client_set_state.set_state);
120 }
121 break;
122 default:
123 break;
124 }
125 }
126
btc_ble_mesh_config_client_copy_req_data(btc_msg_t * msg,void * p_dest,void * p_src)127 static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src)
128 {
129 esp_ble_mesh_cfg_client_cb_param_t *p_dest_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_dest;
130 esp_ble_mesh_cfg_client_cb_param_t *p_src_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_src;
131 uint16_t length = 0U;
132
133 if (!msg || !p_src_data || !p_dest_data) {
134 BT_ERR("%s, Invalid parameter", __func__);
135 return;
136 }
137
138 if (p_src_data->params) {
139 p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t));
140 if (!p_dest_data->params) {
141 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
142 return;
143 }
144
145 memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t));
146 }
147
148 switch (msg->act) {
149 case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT:
150 case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT:
151 case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT:
152 if (p_src_data->params) {
153 switch (p_src_data->params->opcode) {
154 case OP_DEV_COMP_DATA_GET:
155 case OP_DEV_COMP_DATA_STATUS:
156 if (p_src_data->status_cb.comp_data_status.composition_data) {
157 length = p_src_data->status_cb.comp_data_status.composition_data->len;
158 p_dest_data->status_cb.comp_data_status.composition_data = bt_mesh_alloc_buf(length);
159 if (!p_dest_data->status_cb.comp_data_status.composition_data) {
160 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
161 return;
162 }
163 net_buf_simple_add_mem(p_dest_data->status_cb.comp_data_status.composition_data,
164 p_src_data->status_cb.comp_data_status.composition_data->data,
165 p_src_data->status_cb.comp_data_status.composition_data->len);
166 }
167 break;
168 case OP_MOD_SUB_GET:
169 case OP_MOD_SUB_GET_VND:
170 case OP_MOD_SUB_LIST:
171 case OP_MOD_SUB_LIST_VND:
172 if (p_src_data->status_cb.model_sub_list.sub_addr) {
173 length = p_src_data->status_cb.model_sub_list.sub_addr->len;
174 p_dest_data->status_cb.model_sub_list.sub_addr = bt_mesh_alloc_buf(length);
175 if (!p_dest_data->status_cb.model_sub_list.sub_addr) {
176 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
177 return;
178 }
179 net_buf_simple_add_mem(p_dest_data->status_cb.model_sub_list.sub_addr,
180 p_src_data->status_cb.model_sub_list.sub_addr->data,
181 p_src_data->status_cb.model_sub_list.sub_addr->len);
182 }
183 break;
184 case OP_NET_KEY_GET:
185 case OP_NET_KEY_LIST:
186 if (p_src_data->status_cb.netkey_list.net_idx) {
187 length = p_src_data->status_cb.netkey_list.net_idx->len;
188 p_dest_data->status_cb.netkey_list.net_idx = bt_mesh_alloc_buf(length);
189 if (!p_dest_data->status_cb.netkey_list.net_idx) {
190 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
191 return;
192 }
193 net_buf_simple_add_mem(p_dest_data->status_cb.netkey_list.net_idx,
194 p_src_data->status_cb.netkey_list.net_idx->data,
195 p_src_data->status_cb.netkey_list.net_idx->len);
196 }
197 break;
198 case OP_APP_KEY_GET:
199 case OP_APP_KEY_LIST:
200 if (p_src_data->status_cb.appkey_list.app_idx) {
201 length = p_src_data->status_cb.appkey_list.app_idx->len;
202 p_dest_data->status_cb.appkey_list.app_idx = bt_mesh_alloc_buf(length);
203 if (!p_dest_data->status_cb.appkey_list.app_idx) {
204 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
205 return;
206 }
207 net_buf_simple_add_mem(p_dest_data->status_cb.appkey_list.app_idx,
208 p_src_data->status_cb.appkey_list.app_idx->data,
209 p_src_data->status_cb.appkey_list.app_idx->len);
210 }
211 break;
212 case OP_SIG_MOD_APP_GET:
213 case OP_VND_MOD_APP_GET:
214 case OP_SIG_MOD_APP_LIST:
215 case OP_VND_MOD_APP_LIST:
216 if (p_src_data->status_cb.model_app_list.app_idx) {
217 length = p_src_data->status_cb.model_app_list.app_idx->len;
218 p_dest_data->status_cb.model_app_list.app_idx = bt_mesh_alloc_buf(length);
219 if (!p_dest_data->status_cb.model_app_list.app_idx) {
220 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
221 return;
222 }
223 net_buf_simple_add_mem(p_dest_data->status_cb.model_app_list.app_idx,
224 p_src_data->status_cb.model_app_list.app_idx->data,
225 p_src_data->status_cb.model_app_list.app_idx->len);
226 }
227 break;
228 default:
229 break;
230 }
231 }
232 case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT:
233 break;
234 default:
235 break;
236 }
237 }
238
btc_ble_mesh_config_client_free_req_data(btc_msg_t * msg)239 static void btc_ble_mesh_config_client_free_req_data(btc_msg_t *msg)
240 {
241 esp_ble_mesh_cfg_client_cb_param_t *arg = NULL;
242
243 if (!msg || !msg->arg) {
244 BT_ERR("%s, Invalid parameter", __func__);
245 return;
246 }
247
248 arg = (esp_ble_mesh_cfg_client_cb_param_t *)(msg->arg);
249
250 switch (msg->act) {
251 case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT:
252 case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT:
253 case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT:
254 if (arg->params) {
255 switch (arg->params->opcode) {
256 case OP_DEV_COMP_DATA_GET:
257 case OP_DEV_COMP_DATA_STATUS:
258 bt_mesh_free_buf(arg->status_cb.comp_data_status.composition_data);
259 break;
260 case OP_MOD_SUB_GET:
261 case OP_MOD_SUB_GET_VND:
262 case OP_MOD_SUB_LIST:
263 case OP_MOD_SUB_LIST_VND:
264 bt_mesh_free_buf(arg->status_cb.model_sub_list.sub_addr);
265 break;
266 case OP_NET_KEY_GET:
267 case OP_NET_KEY_LIST:
268 bt_mesh_free_buf(arg->status_cb.netkey_list.net_idx);
269 break;
270 case OP_APP_KEY_GET:
271 case OP_APP_KEY_LIST:
272 bt_mesh_free_buf(arg->status_cb.appkey_list.app_idx);
273 break;
274 case OP_SIG_MOD_APP_GET:
275 case OP_VND_MOD_APP_GET:
276 case OP_SIG_MOD_APP_LIST:
277 case OP_VND_MOD_APP_LIST:
278 bt_mesh_free_buf(arg->status_cb.model_app_list.app_idx);
279 break;
280 default:
281 break;
282 }
283 }
284 case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT:
285 if (arg->params) {
286 bt_mesh_free(arg->params);
287 }
288 break;
289 default:
290 break;
291 }
292 }
293
btc_ble_mesh_config_client_callback(esp_ble_mesh_cfg_client_cb_param_t * cb_params,uint8_t act)294 static void btc_ble_mesh_config_client_callback(esp_ble_mesh_cfg_client_cb_param_t *cb_params, uint8_t act)
295 {
296 btc_msg_t msg = {0};
297
298 BT_DBG("%s", __func__);
299
300 /* If corresponding callback is not registered, event will not be posted. */
301 if (!btc_profile_cb_get(BTC_PID_CONFIG_CLIENT)) {
302 return;
303 }
304
305 msg.sig = BTC_SIG_API_CB;
306 msg.pid = BTC_PID_CONFIG_CLIENT;
307 msg.act = act;
308
309 btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_cfg_client_cb_param_t),
310 btc_ble_mesh_config_client_copy_req_data);
311 }
312
bt_mesh_config_client_cb_evt_to_btc(uint32_t opcode,uint8_t evt_type,struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,const uint8_t * val,size_t len)313 void bt_mesh_config_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type,
314 struct bt_mesh_model *model,
315 struct bt_mesh_msg_ctx *ctx,
316 const uint8_t *val, size_t len)
317 {
318 esp_ble_mesh_cfg_client_cb_param_t cb_params = {0};
319 esp_ble_mesh_client_common_param_t params = {0};
320 uint8_t act = 0U;
321
322 if (!model || !ctx) {
323 BT_ERR("%s, Invalid parameter", __func__);
324 return;
325 }
326
327 switch (evt_type) {
328 case BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE:
329 act = ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT;
330 break;
331 case BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE:
332 act = ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT;
333 break;
334 case BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH:
335 act = ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT;
336 break;
337 case BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT:
338 act = ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT;
339 break;
340 default:
341 BT_ERR("Unknown Config client event type %d", evt_type);
342 return;
343 }
344
345 params.opcode = opcode;
346 params.model = (esp_ble_mesh_model_t *)model;
347 params.ctx.net_idx = ctx->net_idx;
348 params.ctx.app_idx = ctx->app_idx;
349 params.ctx.addr = ctx->addr;
350 params.ctx.recv_ttl = ctx->recv_ttl;
351 params.ctx.recv_op = ctx->recv_op;
352 params.ctx.recv_dst = ctx->recv_dst;
353 params.ctx.recv_rssi = ctx->recv_rssi;
354 params.ctx.send_ttl = ctx->send_ttl;
355
356 cb_params.error_code = 0;
357 cb_params.params = ¶ms;
358
359 if (val && len) {
360 memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb)));
361 }
362
363 btc_ble_mesh_config_client_callback(&cb_params, act);
364 return;
365 }
366
btc_ble_mesh_config_client_publish_callback(uint32_t opcode,struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)367 void btc_ble_mesh_config_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model,
368 struct bt_mesh_msg_ctx *ctx,
369 struct net_buf_simple *buf)
370 {
371 if (!model || !ctx || !buf) {
372 BT_ERR("%s, Invalid parameter", __func__);
373 return;
374 }
375
376 bt_mesh_config_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH,
377 model, ctx, buf->data, buf->len);
378 return;
379 }
380
btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t * params,esp_ble_mesh_cfg_client_get_state_t * get)381 static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params,
382 esp_ble_mesh_cfg_client_get_state_t *get)
383 {
384 bt_mesh_client_common_param_t param = {0};
385
386 if (params == NULL) {
387 BT_ERR("%s, Invalid parameter", __func__);
388 return -EINVAL;
389 }
390
391 switch (params->opcode) {
392 case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET:
393 case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET:
394 case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET:
395 case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET:
396 case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET:
397 case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET:
398 case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET:
399 case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET:
400 case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET:
401 case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET:
402 if (get == NULL) {
403 BT_ERR("Invalid Configuration Get");
404 return -EINVAL;
405 }
406 break;
407 default:
408 break;
409 }
410
411 param.opcode = params->opcode;
412 param.model = (struct bt_mesh_model *)params->model;
413 param.ctx.net_idx = params->ctx.net_idx;
414 param.ctx.app_idx = BLE_MESH_KEY_DEV;
415 param.ctx.addr = params->ctx.addr;
416 param.ctx.send_rel = params->ctx.send_rel;
417 param.ctx.send_ttl = params->ctx.send_ttl;
418 param.msg_timeout = params->msg_timeout;
419 param.msg_role = params->msg_role;
420
421 switch (param.opcode) {
422 case ESP_BLE_MESH_MODEL_OP_BEACON_GET:
423 return bt_mesh_cfg_beacon_get(¶m);
424 case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET:
425 return bt_mesh_cfg_ttl_get(¶m);
426 case ESP_BLE_MESH_MODEL_OP_FRIEND_GET:
427 return bt_mesh_cfg_friend_get(¶m);
428 case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET:
429 return bt_mesh_cfg_gatt_proxy_get(¶m);
430 case ESP_BLE_MESH_MODEL_OP_RELAY_GET:
431 return bt_mesh_cfg_relay_get(¶m);
432 case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET:
433 return bt_mesh_cfg_mod_pub_get(¶m, get->model_pub_get.element_addr,
434 get->model_pub_get.model_id,
435 get->model_pub_get.company_id);
436 case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET:
437 return bt_mesh_cfg_hb_pub_get(¶m);
438 case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET:
439 return bt_mesh_cfg_hb_sub_get(¶m);
440 case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET:
441 return bt_mesh_cfg_comp_data_get(¶m, get->comp_data_get.page);
442 case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET:
443 return bt_mesh_cfg_mod_sub_get(¶m, get->sig_model_sub_get.element_addr,
444 get->sig_model_sub_get.model_id);
445 case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET:
446 return bt_mesh_cfg_mod_sub_get_vnd(¶m, get->vnd_model_sub_get.element_addr,
447 get->vnd_model_sub_get.model_id,
448 get->vnd_model_sub_get.company_id);
449 case ESP_BLE_MESH_MODEL_OP_NET_KEY_GET:
450 return bt_mesh_cfg_net_key_get(¶m);
451 case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET:
452 return bt_mesh_cfg_app_key_get(¶m, get->app_key_get.net_idx);
453 case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET:
454 return bt_mesh_cfg_node_identity_get(¶m, get->node_identity_get.net_idx);
455 case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET:
456 return bt_mesh_cfg_mod_app_get(¶m, get->sig_model_app_get.element_addr,
457 get->sig_model_app_get.model_id);
458 case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET:
459 return bt_mesh_cfg_mod_app_get_vnd(¶m, get->vnd_model_app_get.element_addr,
460 get->vnd_model_app_get.model_id,
461 get->vnd_model_app_get.company_id);
462 case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET:
463 return bt_mesh_cfg_kr_phase_get(¶m, get->kr_phase_get.net_idx);
464 case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET:
465 return bt_mesh_cfg_lpn_timeout_get(¶m, get->lpn_pollto_get.lpn_addr);
466 case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET:
467 return bt_mesh_cfg_net_transmit_get(¶m);
468 default:
469 BT_ERR("Invalid Configuration Get opcode 0x%04x", param.opcode);
470 return -EINVAL;
471 }
472
473 return 0;
474 }
475
btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t * params,esp_ble_mesh_cfg_client_set_state_t * set)476 static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params,
477 esp_ble_mesh_cfg_client_set_state_t *set)
478 {
479 bt_mesh_client_common_param_t param = {0};
480
481 if (params == NULL) {
482 BT_ERR("%s, Invalid parameter", __func__);
483 return -EINVAL;
484 }
485
486 if (params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && set == NULL) {
487 BT_ERR("Invalid Configuration Set");
488 return -EINVAL;
489 }
490
491 param.opcode = params->opcode;
492 param.model = (struct bt_mesh_model *)params->model;
493 param.ctx.net_idx = params->ctx.net_idx;
494 param.ctx.app_idx = BLE_MESH_KEY_DEV;
495 param.ctx.addr = params->ctx.addr;
496 param.ctx.send_rel = params->ctx.send_rel;
497 param.ctx.send_ttl = params->ctx.send_ttl;
498 param.msg_timeout = params->msg_timeout;
499 param.msg_role = params->msg_role;
500
501 switch (param.opcode) {
502 case ESP_BLE_MESH_MODEL_OP_BEACON_SET:
503 return bt_mesh_cfg_beacon_set(¶m, set->beacon_set.beacon);
504 case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET:
505 return bt_mesh_cfg_ttl_set(¶m, set->default_ttl_set.ttl);
506 case ESP_BLE_MESH_MODEL_OP_FRIEND_SET:
507 return bt_mesh_cfg_friend_set(¶m, set->friend_set.friend_state);
508 case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET:
509 return bt_mesh_cfg_gatt_proxy_set(¶m, set->gatt_proxy_set.gatt_proxy);
510 case ESP_BLE_MESH_MODEL_OP_RELAY_SET:
511 return bt_mesh_cfg_relay_set(¶m, set->relay_set.relay,
512 set->relay_set.relay_retransmit);
513 case ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD:
514 return bt_mesh_cfg_net_key_add(¶m, set->net_key_add.net_idx,
515 &set->net_key_add.net_key[0]);
516 case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD:
517 return bt_mesh_cfg_app_key_add(¶m, set->app_key_add.net_idx,
518 set->app_key_add.app_idx,
519 &set->app_key_add.app_key[0]);
520 case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND:
521 return bt_mesh_cfg_mod_app_bind(¶m, set->model_app_bind.element_addr,
522 set->model_app_bind.model_app_idx,
523 set->model_app_bind.model_id,
524 set->model_app_bind.company_id);
525 case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET: {
526 struct bt_mesh_cfg_mod_pub model_pub = {
527 .addr = set->model_pub_set.publish_addr,
528 .app_idx = set->model_pub_set.publish_app_idx,
529 .cred_flag = set->model_pub_set.cred_flag,
530 .ttl = set->model_pub_set.publish_ttl,
531 .period = set->model_pub_set.publish_period,
532 .transmit = set->model_pub_set.publish_retransmit,
533 };
534 return bt_mesh_cfg_mod_pub_set(¶m, set->model_pub_set.element_addr,
535 set->model_pub_set.model_id,
536 set->model_pub_set.company_id, &model_pub);
537 }
538 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD:
539 return bt_mesh_cfg_mod_sub_add(¶m, set->model_sub_add.element_addr,
540 set->model_sub_add.sub_addr,
541 set->model_sub_add.model_id,
542 set->model_sub_add.company_id);
543 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE:
544 return bt_mesh_cfg_mod_sub_del(¶m, set->model_sub_delete.element_addr,
545 set->model_sub_delete.sub_addr,
546 set->model_sub_delete.model_id,
547 set->model_sub_delete.company_id);
548 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE:
549 return bt_mesh_cfg_mod_sub_overwrite(¶m, set->model_sub_overwrite.element_addr,
550 set->model_sub_overwrite.sub_addr,
551 set->model_sub_overwrite.model_id,
552 set->model_sub_overwrite.company_id);
553 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD:
554 return bt_mesh_cfg_mod_sub_va_add(¶m, set->model_sub_va_add.element_addr,
555 &set->model_sub_va_add.label_uuid[0],
556 set->model_sub_va_add.model_id,
557 set->model_sub_va_add.company_id);
558 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE:
559 return bt_mesh_cfg_mod_sub_va_overwrite(¶m, set->model_sub_va_overwrite.element_addr,
560 &set->model_sub_va_overwrite.label_uuid[0],
561 set->model_sub_va_overwrite.model_id,
562 set->model_sub_va_overwrite.company_id);
563 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE:
564 return bt_mesh_cfg_mod_sub_va_del(¶m, set->model_sub_va_delete.element_addr,
565 &set->model_sub_va_delete.label_uuid[0],
566 set->model_sub_va_delete.model_id,
567 set->model_sub_va_delete.company_id);
568 case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET:
569 return bt_mesh_cfg_hb_sub_set(¶m, (struct bt_mesh_cfg_hb_sub *)&set->heartbeat_sub_set);
570 case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET:
571 return bt_mesh_cfg_hb_pub_set(¶m, (struct bt_mesh_cfg_hb_pub *)&set->heartbeat_pub_set);
572 case ESP_BLE_MESH_MODEL_OP_NODE_RESET:
573 return bt_mesh_cfg_node_reset(¶m);
574 case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET: {
575 struct bt_mesh_cfg_mod_pub model_pub = {
576 .app_idx = set->model_pub_va_set.publish_app_idx,
577 .cred_flag = set->model_pub_va_set.cred_flag,
578 .ttl = set->model_pub_va_set.publish_ttl,
579 .period = set->model_pub_va_set.publish_period,
580 .transmit = set->model_pub_va_set.publish_retransmit,
581 };
582 return bt_mesh_cfg_mod_pub_va_set(¶m, set->model_pub_va_set.element_addr,
583 set->model_pub_va_set.model_id,
584 set->model_pub_va_set.company_id,
585 set->model_pub_va_set.label_uuid, &model_pub);
586 }
587 case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL:
588 return bt_mesh_cfg_mod_sub_del_all(¶m, set->model_sub_delete_all.element_addr,
589 set->model_sub_delete_all.model_id,
590 set->model_sub_delete_all.company_id);
591 case ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE:
592 return bt_mesh_cfg_net_key_update(¶m, set->net_key_update.net_idx,
593 set->net_key_update.net_key);
594 case ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE:
595 return bt_mesh_cfg_net_key_delete(¶m, set->net_key_delete.net_idx);
596 case ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE:
597 return bt_mesh_cfg_app_key_update(¶m, set->app_key_update.net_idx,
598 set->app_key_update.app_idx,
599 set->app_key_update.app_key);
600 case ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE:
601 return bt_mesh_cfg_app_key_delete(¶m, set->app_key_delete.net_idx,
602 set->app_key_delete.app_idx);
603 case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET:
604 return bt_mesh_cfg_node_identity_set(¶m, set->node_identity_set.net_idx,
605 set->node_identity_set.identity);
606 case ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND:
607 return bt_mesh_cfg_mod_app_unbind(¶m, set->model_app_unbind.element_addr,
608 set->model_app_unbind.model_app_idx,
609 set->model_app_unbind.model_id,
610 set->model_app_unbind.company_id);
611 case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET:
612 return bt_mesh_cfg_kr_phase_set(¶m, set->kr_phase_set.net_idx,
613 set->kr_phase_set.transition);
614 case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET:
615 return bt_mesh_cfg_net_transmit_set(¶m, set->net_transmit_set.net_transmit);
616 default:
617 BT_ERR("Invalid Configuration Set opcode 0x%04x", param.opcode);
618 return -EINVAL;
619 }
620
621 return 0;
622 }
623
btc_ble_mesh_config_client_call_handler(btc_msg_t * msg)624 void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg)
625 {
626 btc_ble_mesh_config_client_args_t *arg = NULL;
627 esp_ble_mesh_cfg_client_cb_param_t cb = {0};
628
629 if (!msg || !msg->arg) {
630 BT_ERR("%s, Invalid parameter", __func__);
631 return;
632 }
633
634 arg = (btc_ble_mesh_config_client_args_t *)(msg->arg);
635
636 switch (msg->act) {
637 case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: {
638 cb.params = arg->cfg_client_get_state.params;
639 cb.error_code = btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params,
640 arg->cfg_client_get_state.get_state);
641 if (cb.error_code) {
642 btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT);
643 }
644 break;
645 }
646 case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: {
647 cb.params = arg->cfg_client_set_state.params;
648 cb.error_code = btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params,
649 arg->cfg_client_set_state.set_state);
650 if (cb.error_code) {
651 btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT);
652 }
653 break;
654 }
655 default:
656 break;
657 }
658
659 btc_ble_mesh_config_client_arg_deep_free(msg);
660 return;
661 }
662
btc_ble_mesh_config_client_cb_handler(btc_msg_t * msg)663 void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg)
664 {
665 esp_ble_mesh_cfg_client_cb_param_t *param = NULL;
666
667 if (!msg || !msg->arg) {
668 BT_ERR("%s, Invalid parameter", __func__);
669 return;
670 }
671
672 param = (esp_ble_mesh_cfg_client_cb_param_t *)(msg->arg);
673
674 if (msg->act < ESP_BLE_MESH_CFG_CLIENT_EVT_MAX) {
675 btc_ble_mesh_config_client_cb_to_app(msg->act, param);
676 } else {
677 BT_ERR("%s, Unknown act %d", __func__, msg->act);
678 }
679
680 btc_ble_mesh_config_client_free_req_data(msg);
681 return;
682 }
683
684 #endif /* CONFIG_BLE_MESH_CFG_CLI */
685
686 /* Configuration Server Model related functions */
687
btc_ble_mesh_config_server_cb_to_app(esp_ble_mesh_cfg_server_cb_event_t event,esp_ble_mesh_cfg_server_cb_param_t * param)688 static inline void btc_ble_mesh_config_server_cb_to_app(esp_ble_mesh_cfg_server_cb_event_t event,
689 esp_ble_mesh_cfg_server_cb_param_t *param)
690 {
691 esp_ble_mesh_cfg_server_cb_t btc_ble_mesh_cb =
692 (esp_ble_mesh_cfg_server_cb_t)btc_profile_cb_get(BTC_PID_CONFIG_SERVER);
693 if (btc_ble_mesh_cb) {
694 btc_ble_mesh_cb(event, param);
695 }
696 }
697
btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param_t * cb_params,uint8_t act)698 static void btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param_t *cb_params, uint8_t act)
699 {
700 btc_msg_t msg = {0};
701
702 BT_DBG("%s", __func__);
703
704 /* If corresponding callback is not registered, event will not be posted. */
705 if (!btc_profile_cb_get(BTC_PID_CONFIG_SERVER)) {
706 return;
707 }
708
709 msg.sig = BTC_SIG_API_CB;
710 msg.pid = BTC_PID_CONFIG_SERVER;
711 msg.act = act;
712
713 btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_cfg_server_cb_param_t), NULL);
714 }
715
bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type,struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,const uint8_t * val,size_t len)716 void bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model,
717 struct bt_mesh_msg_ctx *ctx,
718 const uint8_t *val, size_t len)
719 {
720 esp_ble_mesh_cfg_server_cb_param_t cb_params = {0};
721 uint8_t act = 0U;
722
723 if (!model || !ctx) {
724 BT_ERR("%s, Invalid parameter", __func__);
725 return;
726 }
727
728 switch (evt_type) {
729 case BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE:
730 act = ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT;
731 break;
732 default:
733 BT_ERR("Unknown Config server event type %d", evt_type);
734 return;
735 }
736
737 cb_params.model = (esp_ble_mesh_model_t *)model;
738 cb_params.ctx.net_idx = ctx->net_idx;
739 cb_params.ctx.app_idx = ctx->app_idx;
740 cb_params.ctx.addr = ctx->addr;
741 cb_params.ctx.recv_ttl = ctx->recv_ttl;
742 cb_params.ctx.recv_op = ctx->recv_op;
743 cb_params.ctx.recv_dst = ctx->recv_dst;
744 cb_params.ctx.recv_rssi = ctx->recv_rssi;
745 cb_params.ctx.send_ttl = ctx->send_ttl;
746
747 if (val && len) {
748 memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value)));
749 }
750
751 btc_ble_mesh_config_server_callback(&cb_params, act);
752 return;
753 }
754
btc_ble_mesh_config_server_cb_handler(btc_msg_t * msg)755 void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg)
756 {
757 esp_ble_mesh_cfg_server_cb_param_t *param = NULL;
758
759 if (!msg || !msg->arg) {
760 BT_ERR("%s, Invalid parameter", __func__);
761 return;
762 }
763
764 param = (esp_ble_mesh_cfg_server_cb_param_t *)(msg->arg);
765
766 if (msg->act < ESP_BLE_MESH_CFG_SERVER_EVT_MAX) {
767 btc_ble_mesh_config_server_cb_to_app(msg->act, param);
768 } else {
769 BT_ERR("%s, Unknown act %d", __func__, msg->act);
770 }
771 }
772