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_lighting_model.h"
19 #include "esp_ble_mesh_lighting_model_api.h"
20
21 #if CONFIG_BLE_MESH_LIGHTING_CLIENT
22 #include "lighting_client.h"
23
24 /* Lighting Client Models related functions */
25
btc_ble_mesh_lighting_client_cb_to_app(esp_ble_mesh_light_client_cb_event_t event,esp_ble_mesh_light_client_cb_param_t * param)26 static inline void btc_ble_mesh_lighting_client_cb_to_app(esp_ble_mesh_light_client_cb_event_t event,
27 esp_ble_mesh_light_client_cb_param_t *param)
28 {
29 esp_ble_mesh_light_client_cb_t btc_ble_mesh_cb =
30 (esp_ble_mesh_light_client_cb_t)btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT);
31 if (btc_ble_mesh_cb) {
32 btc_ble_mesh_cb(event, param);
33 }
34 }
35
btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)36 void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
37 {
38 btc_ble_mesh_lighting_client_args_t *dst = (btc_ble_mesh_lighting_client_args_t *)p_dest;
39 btc_ble_mesh_lighting_client_args_t *src = (btc_ble_mesh_lighting_client_args_t *)p_src;
40
41 if (!msg || !dst || !src) {
42 BT_ERR("%s, Invalid parameter", __func__);
43 return;
44 }
45
46 switch (msg->act) {
47 case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: {
48 dst->light_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t));
49 if (dst->light_client_get_state.params) {
50 memcpy(dst->light_client_get_state.params, src->light_client_get_state.params,
51 sizeof(esp_ble_mesh_client_common_param_t));
52 } else {
53 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
54 break;
55 }
56 if (src->light_client_get_state.get_state) {
57 dst->light_client_get_state.get_state = (esp_ble_mesh_light_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_light_client_get_state_t));
58 if (dst->light_client_get_state.get_state) {
59 memcpy(dst->light_client_get_state.get_state, src->light_client_get_state.get_state,
60 sizeof(esp_ble_mesh_light_client_get_state_t));
61 } else {
62 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
63 }
64 }
65 break;
66 }
67 case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: {
68 dst->light_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t));
69 dst->light_client_set_state.set_state = (esp_ble_mesh_light_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_light_client_set_state_t));
70 if (dst->light_client_set_state.params && dst->light_client_set_state.set_state) {
71 memcpy(dst->light_client_set_state.params, src->light_client_set_state.params,
72 sizeof(esp_ble_mesh_client_common_param_t));
73 memcpy(dst->light_client_set_state.set_state, src->light_client_set_state.set_state,
74 sizeof(esp_ble_mesh_light_client_set_state_t));
75 } else {
76 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
77 }
78 break;
79 }
80 default:
81 BT_DBG("%s, Unknown act %d", __func__, msg->act);
82 break;
83 }
84 }
85
btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t * msg)86 static void btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t *msg)
87 {
88 btc_ble_mesh_lighting_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_lighting_client_args_t *)(msg->arg);
96
97 switch (msg->act) {
98 case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE:
99 if (arg->light_client_get_state.params) {
100 bt_mesh_free(arg->light_client_get_state.params);
101 }
102 if (arg->light_client_get_state.get_state) {
103 bt_mesh_free(arg->light_client_get_state.get_state);
104 }
105 break;
106 case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE:
107 if (arg->light_client_set_state.params) {
108 bt_mesh_free(arg->light_client_set_state.params);
109 }
110 if (arg->light_client_set_state.set_state) {
111 bt_mesh_free(arg->light_client_set_state.set_state);
112 }
113 break;
114 default:
115 break;
116 }
117 }
118
btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t * msg,void * p_dest,void * p_src)119 static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src)
120 {
121 esp_ble_mesh_light_client_cb_param_t *p_dest_data = (esp_ble_mesh_light_client_cb_param_t *)p_dest;
122 esp_ble_mesh_light_client_cb_param_t *p_src_data = (esp_ble_mesh_light_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_LIGHT_CLIENT_GET_STATE_EVT:
142 case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT:
143 case ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT:
144 if (p_src_data->params) {
145 switch (p_src_data->params->opcode) {
146 case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET:
147 case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET:
148 case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS:
149 if (p_src_data->status_cb.lc_property_status.property_value) {
150 length = p_src_data->status_cb.lc_property_status.property_value->len;
151 p_dest_data->status_cb.lc_property_status.property_value = bt_mesh_alloc_buf(length);
152 if (!p_dest_data->status_cb.lc_property_status.property_value) {
153 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
154 return;
155 }
156 net_buf_simple_add_mem(p_dest_data->status_cb.lc_property_status.property_value,
157 p_src_data->status_cb.lc_property_status.property_value->data,
158 p_src_data->status_cb.lc_property_status.property_value->len);
159 }
160 break;
161 default:
162 break;
163 }
164 }
165 case ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT:
166 break;
167 default:
168 break;
169 }
170 }
171
btc_ble_mesh_lighting_client_free_req_data(btc_msg_t * msg)172 static void btc_ble_mesh_lighting_client_free_req_data(btc_msg_t *msg)
173 {
174 esp_ble_mesh_light_client_cb_param_t *arg = NULL;
175
176 if (!msg || !msg->arg) {
177 BT_ERR("%s, Invalid parameter", __func__);
178 return;
179 }
180
181 arg = (esp_ble_mesh_light_client_cb_param_t *)(msg->arg);
182
183 switch (msg->act) {
184 case ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT:
185 case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT:
186 case ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT:
187 if (arg->params) {
188 switch (arg->params->opcode) {
189 case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET:
190 case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET:
191 case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS:
192 bt_mesh_free_buf(arg->status_cb.lc_property_status.property_value);
193 break;
194 default:
195 break;
196 }
197 }
198 case ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT:
199 if (arg->params) {
200 bt_mesh_free(arg->params);
201 }
202 break;
203 default:
204 break;
205 }
206 }
207
btc_ble_mesh_lighting_client_callback(esp_ble_mesh_light_client_cb_param_t * cb_params,uint8_t act)208 static void btc_ble_mesh_lighting_client_callback(esp_ble_mesh_light_client_cb_param_t *cb_params, uint8_t act)
209 {
210 btc_msg_t msg = {0};
211
212 BT_DBG("%s", __func__);
213
214 /* If corresponding callback is not registered, event will not be posted. */
215 if (!btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT)) {
216 return;
217 }
218
219 msg.sig = BTC_SIG_API_CB;
220 msg.pid = BTC_PID_LIGHTING_CLIENT;
221 msg.act = act;
222
223 btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_light_client_cb_param_t),
224 btc_ble_mesh_lighting_client_copy_req_data);
225 }
226
bt_mesh_lighting_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)227 void bt_mesh_lighting_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type,
228 struct bt_mesh_model *model,
229 struct bt_mesh_msg_ctx *ctx,
230 const uint8_t *val, size_t len)
231 {
232 esp_ble_mesh_light_client_cb_param_t cb_params = {0};
233 esp_ble_mesh_client_common_param_t params = {0};
234 uint8_t act = 0U;
235
236 if (!model || !ctx) {
237 BT_ERR("%s, Invalid parameter", __func__);
238 return;
239 }
240
241 switch (evt_type) {
242 case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE:
243 act = ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT;
244 break;
245 case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE:
246 act = ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT;
247 break;
248 case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH:
249 act = ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT;
250 break;
251 case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT:
252 act = ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT;
253 break;
254 default:
255 BT_ERR("Unknown Lighting client event type %d", evt_type);
256 return;
257 }
258
259 params.opcode = opcode;
260 params.model = (esp_ble_mesh_model_t *)model;
261 params.ctx.net_idx = ctx->net_idx;
262 params.ctx.app_idx = ctx->app_idx;
263 params.ctx.addr = ctx->addr;
264 params.ctx.recv_ttl = ctx->recv_ttl;
265 params.ctx.recv_op = ctx->recv_op;
266 params.ctx.recv_dst = ctx->recv_dst;
267 params.ctx.recv_rssi = ctx->recv_rssi;
268 params.ctx.send_ttl = ctx->send_ttl;
269
270 cb_params.error_code = 0;
271 cb_params.params = ¶ms;
272
273 if (val && len) {
274 memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb)));
275 }
276
277 btc_ble_mesh_lighting_client_callback(&cb_params, act);
278 return;
279 }
280
btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode,struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)281 void btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model,
282 struct bt_mesh_msg_ctx *ctx,
283 struct net_buf_simple *buf)
284 {
285 if (!model || !ctx || !buf) {
286 BT_ERR("%s, Invalid parameter", __func__);
287 return;
288 }
289
290 bt_mesh_lighting_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH,
291 model, ctx, buf->data, buf->len);
292 return;
293 }
294
btc_ble_mesh_lighting_client_call_handler(btc_msg_t * msg)295 void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg)
296 {
297 esp_ble_mesh_client_common_param_t *params = NULL;
298 btc_ble_mesh_lighting_client_args_t *arg = NULL;
299 esp_ble_mesh_light_client_cb_param_t cb = {0};
300 bt_mesh_client_common_param_t common = {0};
301
302 if (!msg || !msg->arg) {
303 BT_ERR("%s, Invalid parameter", __func__);
304 return;
305 }
306
307 arg = (btc_ble_mesh_lighting_client_args_t *)(msg->arg);
308
309 switch (msg->act) {
310 case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: {
311 params = arg->light_client_get_state.params;
312 common.opcode = params->opcode;
313 common.model = (struct bt_mesh_model *)params->model;
314 common.ctx.net_idx = params->ctx.net_idx;
315 common.ctx.app_idx = params->ctx.app_idx;
316 common.ctx.addr = params->ctx.addr;
317 common.ctx.send_rel = params->ctx.send_rel;
318 common.ctx.send_ttl = params->ctx.send_ttl;
319 common.msg_timeout = params->msg_timeout;
320 common.msg_role = params->msg_role;
321
322 cb.params = arg->light_client_get_state.params;
323 cb.error_code = bt_mesh_light_client_get_state(&common, arg->light_client_get_state.get_state);
324 if (cb.error_code) {
325 /* If send failed, callback error_code to app layer immediately */
326 btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT);
327 }
328 break;
329 }
330 case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: {
331 params = arg->light_client_set_state.params;
332 common.opcode = params->opcode;
333 common.model = (struct bt_mesh_model *)params->model;
334 common.ctx.net_idx = params->ctx.net_idx;
335 common.ctx.app_idx = params->ctx.app_idx;
336 common.ctx.addr = params->ctx.addr;
337 common.ctx.send_rel = params->ctx.send_rel;
338 common.ctx.send_ttl = params->ctx.send_ttl;
339 common.msg_timeout = params->msg_timeout;
340 common.msg_role = params->msg_role;
341
342 cb.params = arg->light_client_set_state.params;
343 cb.error_code = bt_mesh_light_client_set_state(&common, arg->light_client_set_state.set_state);
344 if (cb.error_code) {
345 /* If send failed, callback error_code to app layer immediately */
346 btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT);
347 }
348 break;
349 }
350 default:
351 break;
352 }
353
354 btc_ble_mesh_lighting_client_arg_deep_free(msg);
355 return;
356 }
357
btc_ble_mesh_lighting_client_cb_handler(btc_msg_t * msg)358 void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg)
359 {
360 esp_ble_mesh_light_client_cb_param_t *param = NULL;
361
362 if (!msg || !msg->arg) {
363 BT_ERR("%s, Invalid parameter", __func__);
364 return;
365 }
366
367 param = (esp_ble_mesh_light_client_cb_param_t *)(msg->arg);
368
369 if (msg->act < ESP_BLE_MESH_LIGHT_CLIENT_EVT_MAX) {
370 btc_ble_mesh_lighting_client_cb_to_app(msg->act, param);
371 } else {
372 BT_ERR("%s, Unknown act %d", __func__, msg->act);
373 }
374
375 btc_ble_mesh_lighting_client_free_req_data(msg);
376 return;
377 }
378
379 #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
380
381 #if CONFIG_BLE_MESH_LIGHTING_SERVER
382
383 /* Lighting Server Models related functions */
384
btc_ble_mesh_lighting_server_cb_to_app(esp_ble_mesh_lighting_server_cb_event_t event,esp_ble_mesh_lighting_server_cb_param_t * param)385 static inline void btc_ble_mesh_lighting_server_cb_to_app(esp_ble_mesh_lighting_server_cb_event_t event,
386 esp_ble_mesh_lighting_server_cb_param_t *param)
387 {
388 esp_ble_mesh_lighting_server_cb_t btc_ble_mesh_cb =
389 (esp_ble_mesh_lighting_server_cb_t)btc_profile_cb_get(BTC_PID_LIGHTING_SERVER);
390 if (btc_ble_mesh_cb) {
391 btc_ble_mesh_cb(event, param);
392 }
393 }
394
btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t * msg,void * p_dest,void * p_src)395 static void btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src)
396 {
397 esp_ble_mesh_lighting_server_cb_param_t *p_dest_data = (esp_ble_mesh_lighting_server_cb_param_t *)p_dest;
398 esp_ble_mesh_lighting_server_cb_param_t *p_src_data = (esp_ble_mesh_lighting_server_cb_param_t *)p_src;
399 uint16_t length = 0U;
400
401 if (!msg || !p_src_data || !p_dest_data) {
402 BT_ERR("%s, Invalid parameter", __func__);
403 return;
404 }
405
406 switch (msg->act) {
407 case ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT:
408 if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET ||
409 p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) {
410 if (p_src_data->value.state_change.lc_property_set.property_value) {
411 length = p_src_data->value.state_change.lc_property_set.property_value->len;
412 p_dest_data->value.state_change.lc_property_set.property_value = bt_mesh_alloc_buf(length);
413 if (p_dest_data->value.state_change.lc_property_set.property_value == NULL) {
414 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
415 return;
416 }
417 net_buf_simple_add_mem(p_dest_data->value.state_change.lc_property_set.property_value,
418 p_src_data->value.state_change.lc_property_set.property_value->data,
419 p_src_data->value.state_change.lc_property_set.property_value->len);
420 }
421 }
422 break;
423 case ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT:
424 if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET ||
425 p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) {
426 if (p_src_data->value.set.lc_property.property_value) {
427 length = p_src_data->value.set.lc_property.property_value->len;
428 p_dest_data->value.set.lc_property.property_value = bt_mesh_alloc_buf(length);
429 if (p_dest_data->value.set.lc_property.property_value == NULL) {
430 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
431 return;
432 }
433 net_buf_simple_add_mem(p_dest_data->value.set.lc_property.property_value,
434 p_src_data->value.set.lc_property.property_value->data,
435 p_src_data->value.set.lc_property.property_value->len);
436 }
437 }
438 break;
439 case ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT:
440 if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS) {
441 if (p_src_data->value.status.sensor_status.data) {
442 length = p_src_data->value.status.sensor_status.data->len;
443 p_dest_data->value.status.sensor_status.data = bt_mesh_alloc_buf(length);
444 if (p_dest_data->value.status.sensor_status.data == NULL) {
445 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
446 return;
447 }
448 net_buf_simple_add_mem(p_dest_data->value.status.sensor_status.data,
449 p_src_data->value.status.sensor_status.data->data,
450 p_src_data->value.status.sensor_status.data->len);
451 }
452 }
453 break;
454 default:
455 break;
456 }
457 }
458
btc_ble_mesh_lighting_server_free_req_data(btc_msg_t * msg)459 static void btc_ble_mesh_lighting_server_free_req_data(btc_msg_t *msg)
460 {
461 esp_ble_mesh_lighting_server_cb_param_t *arg = NULL;
462
463 if (!msg || !msg->arg) {
464 BT_ERR("%s, Invalid parameter", __func__);
465 return;
466 }
467
468 arg = (esp_ble_mesh_lighting_server_cb_param_t *)(msg->arg);
469
470 switch (msg->act) {
471 case ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT:
472 if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET ||
473 arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) {
474 bt_mesh_free_buf(arg->value.state_change.lc_property_set.property_value);
475 }
476 break;
477 case ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT:
478 if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET ||
479 arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) {
480 bt_mesh_free_buf(arg->value.set.lc_property.property_value);
481 }
482 break;
483 case ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT:
484 if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS) {
485 bt_mesh_free_buf(arg->value.status.sensor_status.data);
486 }
487 break;
488 default:
489 break;
490 }
491 }
492
btc_ble_mesh_lighting_server_callback(esp_ble_mesh_lighting_server_cb_param_t * cb_params,uint8_t act)493 static void btc_ble_mesh_lighting_server_callback(esp_ble_mesh_lighting_server_cb_param_t *cb_params, uint8_t act)
494 {
495 btc_msg_t msg = {0};
496
497 BT_DBG("%s", __func__);
498
499 /* If corresponding callback is not registered, event will not be posted. */
500 if (!btc_profile_cb_get(BTC_PID_LIGHTING_SERVER)) {
501 return;
502 }
503
504 msg.sig = BTC_SIG_API_CB;
505 msg.pid = BTC_PID_LIGHTING_SERVER;
506 msg.act = act;
507
508 btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_lighting_server_cb_param_t),
509 btc_ble_mesh_lighting_server_copy_req_data);
510 }
511
bt_mesh_lighting_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)512 void bt_mesh_lighting_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model,
513 struct bt_mesh_msg_ctx *ctx,
514 const uint8_t *val, size_t len)
515 {
516 esp_ble_mesh_lighting_server_cb_param_t cb_params = {0};
517 uint8_t act = 0U;
518
519 if (model == NULL || ctx == NULL) {
520 BT_ERR("%s, Invalid parameter", __func__);
521 return;
522 }
523
524 switch (evt_type) {
525 case BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE:
526 act = ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT;
527 break;
528 case BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG:
529 act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_GET_MSG_EVT;
530 break;
531 case BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG:
532 act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT;
533 break;
534 case BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG:
535 act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT;
536 break;
537 default:
538 BT_ERR("Unknown Lighting server event type %d", evt_type);
539 return;
540 }
541
542 cb_params.model = (esp_ble_mesh_model_t *)model;
543 cb_params.ctx.net_idx = ctx->net_idx;
544 cb_params.ctx.app_idx = ctx->app_idx;
545 cb_params.ctx.addr = ctx->addr;
546 cb_params.ctx.recv_ttl = ctx->recv_ttl;
547 cb_params.ctx.recv_op = ctx->recv_op;
548 cb_params.ctx.recv_dst = ctx->recv_dst;
549 cb_params.ctx.recv_rssi = ctx->recv_rssi;
550 cb_params.ctx.send_ttl = ctx->send_ttl;
551
552 if (val && len) {
553 memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value)));
554 }
555
556 btc_ble_mesh_lighting_server_callback(&cb_params, act);
557 return;
558 }
559
btc_ble_mesh_lighting_server_cb_handler(btc_msg_t * msg)560 void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg)
561 {
562 esp_ble_mesh_lighting_server_cb_param_t *param = NULL;
563
564 if (!msg || !msg->arg) {
565 BT_ERR("%s, Invalid parameter", __func__);
566 return;
567 }
568
569 param = (esp_ble_mesh_lighting_server_cb_param_t *)(msg->arg);
570
571 if (msg->act < ESP_BLE_MESH_LIGHTING_SERVER_EVT_MAX) {
572 btc_ble_mesh_lighting_server_cb_to_app(msg->act, param);
573 } else {
574 BT_ERR("%s, Unknown act %d", __func__, msg->act);
575 }
576
577 btc_ble_mesh_lighting_server_free_req_data(msg);
578 return;
579 }
580
581 #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
582