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