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