MeshX 0.3
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
esp_light_srv_model.c
Go to the documentation of this file.
1
18
22
27{
28 struct
29 {
30 uint16_t lightness;
31 uint16_t temperature;
33 struct
34 {
35 uint16_t temperature;
36 uint16_t delta_uv;
38 struct
39 {
40 uint16_t lightness_def;
41 uint16_t temperature_def;
42 uint16_t delta_uv_def;
44 struct
45 {
46 uint8_t status_code;
47 uint16_t range_min;
48 uint16_t range_max;
51
55static const MESHX_MODEL light_ctl_setup_sig_template = ESP_BLE_MESH_SIG_MODEL(
56 MESHX_MODEL_ID_LIGHT_CTL_SETUP_SRV, NULL, NULL, NULL);
57
61static const MESHX_MODEL light_ctl_sig_template = ESP_BLE_MESH_SIG_MODEL(
62 MESHX_MODEL_ID_LIGHT_CTL_SRV, NULL, NULL, NULL);
63
74{
75 MESHX_UNUSED(event);
76 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "evt|op|src|dst: %02x|%04x|%04x|%04x|%04x",
77 event, (unsigned)param->ctx.recv_op, param->ctx.addr, param->ctx.recv_dst,
78 param->model->model_id);
79
80 if(event != ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT)
81 {
82 return;
83 }
84 MESHX_LIGHT_CTL_SRV *srv = (MESHX_LIGHT_CTL_SRV *)param->model->user_data;
85
86 bool publish_flag = false;
87 uint32_t op_code = param->ctx.recv_op;
89 .ctx = {
90 .net_idx = param->ctx.net_idx,
91 .app_idx = param->ctx.app_idx,
92 .dst_addr = param->ctx.recv_dst,
93 .src_addr = param->ctx.addr,
94 .opcode = param->ctx.recv_op,
95 .p_ctx = &param->ctx
96 },
97 .model = {
98 .el_id = param->model->element_idx,
99 .pub_addr = param->model->pub->publish_addr,
100 .model_id = param->model->model_id,
101 .p_model = param->model
102 }
103 };
104
105 switch (op_code)
106 {
111 if (op_code != MESHX_MODEL_OP_LIGHT_CTL_GET)
112 {
113 srv->state->temperature = param->value.state_change.ctl_set.temperature;
114 srv->state->lightness = param->value.state_change.ctl_set.lightness;
115 srv->state->delta_uv = param->value.state_change.ctl_set.delta_uv;
116 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "lightness|temp|del_uv:%d|%d|%d",
117 srv->state->lightness,
118 srv->state->temperature,
119 srv->state->delta_uv);
120
121 pub_param.state_change.ctl_set.delta_uv = srv->state->delta_uv;
122 pub_param.state_change.ctl_set.lightness = srv->state->lightness;
123 pub_param.state_change.ctl_set.temperature = srv->state->temperature;
124
125 publish_flag = true;
126 }
127 break;
132 {
133 srv->state->temperature = param->value.state_change.ctl_temp_set.temperature;
134 srv->state->delta_uv = param->value.state_change.ctl_temp_set.delta_uv;
135 MESHX_LOGI(MODULE_ID_MODEL_SERVER, "lightness|del_uv:%d|%d",
136 srv->state->temperature,
137 srv->state->delta_uv);
138
139 pub_param.state_change.ctl_temp_set.delta_uv = srv->state->delta_uv;
140 pub_param.state_change.ctl_temp_set.temperature = srv->state->temperature;
141
142 publish_flag = true;
143 }
144 break;
150 {
151 MESHX_LOGI(MODULE_ID_MODEL_SERVER, "lightness|temp|del_uv:%d|%d|%d",
152 param->value.state_change.ctl_default_set.lightness,
153 param->value.state_change.ctl_default_set.temperature,
154 param->value.state_change.ctl_default_set.delta_uv);
155 srv->state->temperature_default = param->value.state_change.ctl_default_set.temperature;
156 srv->state->lightness_default = param->value.state_change.ctl_default_set.lightness;
157 srv->state->delta_uv_default = param->value.state_change.ctl_default_set.delta_uv;
158
159 pub_param.state_change.ctl_default_set.delta_uv = srv->state->delta_uv_default;
160 pub_param.state_change.ctl_default_set.lightness = srv->state->lightness_default;
161 pub_param.state_change.ctl_default_set.temperature = srv->state->temperature_default;
162
163 publish_flag = true;
164 }
165 break;
170 {
171 MESHX_LOGI(MODULE_ID_MODEL_SERVER, "temp min|max: %dK|%dK",
172 param->value.state_change.ctl_temp_range_set.range_min,
173 param->value.state_change.ctl_temp_range_set.range_max);
174 srv->state->temperature_range_min = param->value.state_change.ctl_temp_range_set.range_min;
175 srv->state->temperature_range_max = param->value.state_change.ctl_temp_range_set.range_max;
176
177 pub_param.state_change.ctl_temp_range_set.range_max = srv->state->temperature_range_max;
178 pub_param.state_change.ctl_temp_range_set.range_min = srv->state->temperature_range_min;
179
180 publish_flag = true;
181 }
182 break;
183 default:
184 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "CTL Unhandled Event %p", (meshx_ptr_t )param->ctx.recv_op);
185 break;
186 }
187 if (publish_flag)
189 pub_param.model.model_id,
190 &pub_param,
192}
193
195 const meshx_model_t *p_model,
196 const meshx_ctx_t *p_ctx,
197 const meshx_lighting_server_state_change_t *state_change)
198{
199 if (!p_model || !p_ctx || !state_change)
200 {
201 return MESHX_INVALID_ARG;
202 }
204 static esp_ble_mesh_msg_ctx_t ctx;
205 const esp_ble_mesh_msg_ctx_t *pctx = (esp_ble_mesh_msg_ctx_t *)p_ctx->p_ctx;
206 if(pctx != NULL)
207 {
208 memcpy(&ctx, pctx, sizeof(esp_ble_mesh_msg_ctx_t));
209 }
210 meshx_plat_ctl_status_t ctl_status_union;
211 uint8_t ctl_status_pack_len = 0;
212
213 switch (p_ctx->opcode)
214 {
216 ctl_status_union.ctl_status.temperature = state_change->ctl_set.temperature;
217 ctl_status_union.ctl_status.lightness = state_change->ctl_set.lightness;
218 ctl_status_pack_len = sizeof(ctl_status_union.ctl_status);
219 break;
221 ctl_status_union.ctl_temp_status.temperature = state_change->ctl_temp_set.temperature;
222 ctl_status_union.ctl_temp_status.delta_uv = state_change->ctl_temp_set.delta_uv;
223 ctl_status_pack_len = sizeof(ctl_status_union.ctl_temp_status);
224 break;
226 ctl_status_union.ctl_default.delta_uv_def = state_change->ctl_default_set.delta_uv;
227 ctl_status_union.ctl_default.lightness_def = state_change->ctl_default_set.lightness;
228 ctl_status_union.ctl_default.temperature_def = state_change->ctl_default_set.temperature;
229 ctl_status_pack_len = sizeof(ctl_status_union.ctl_default);
230 break;
232 ctl_status_union.ctl_temp_range.status_code = MESHX_SUCCESS;
233 ctl_status_union.ctl_temp_range.range_min = state_change->ctl_temp_range_set.range_min;
234 ctl_status_union.ctl_temp_range.range_max = state_change->ctl_temp_range_set.range_max;
235 ctl_status_pack_len = sizeof(ctl_status_union.ctl_temp_range);
236 break;
237 default:
238 err = MESHX_INVALID_ARG;
239 }
240
241 ctx.net_idx = p_ctx->net_idx;
242 ctx.app_idx = p_ctx->app_idx;
243 ctx.addr = p_ctx->dst_addr;
244 ctx.send_ttl = ESP_BLE_MESH_TTL_DEFAULT;
245 ctx.send_cred = 0;
246 ctx.send_tag = BIT1;
247
248 esp_err_t esp_err = esp_ble_mesh_server_model_send_msg(p_model->p_model,
249 &ctx,
250 p_ctx->opcode,
251 ctl_status_pack_len,
252 (uint8_t *)&ctl_status_union);
253 if (esp_err)
254 {
255 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Mesh Model msg send failed (err: 0x%x)", esp_err);
256 return MESHX_ERR_PLAT;
257 }
258 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "Mesh Model msg sent (opcode: 0x%04x, len: %d)", p_ctx->opcode, ctl_status_pack_len);
259
260 return err;
261}
262
264{
265 /* Register callback for enabling Sending of Model Msg From MeshX to BLE Layer */
267 /* Register the ESP Generic Server callback */
268 esp_err_t esp_err = esp_ble_mesh_register_lighting_server_callback(
270 );
271 if (esp_err != ESP_OK)
272 err = MESHX_ERR_PLAT;
273
274 return err;
275}
276
278{
279 if (p_ctl_srv)
280 {
281 if(*p_ctl_srv && ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state)
282 {
283 MESHX_FREE(((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state);
284 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state = NULL;
285 }
286 MESHX_FREE(*p_ctl_srv);
287 *p_ctl_srv = NULL;
288 }
289
290 return meshx_plat_del_model_pub(p_pub);
291}
292
294{
295 if (!p_model || !p_pub || !p_ctl_srv)
296 return MESHX_INVALID_ARG;
297
299
300 err = meshx_plat_create_model_pub(p_pub, 1);
301 if (err)
302 return meshx_plat_del_model_pub(p_pub);
303
305 if (!*p_ctl_srv)
306 return MESHX_NO_MEM;
307
308 /* SIG CTL Setup Server initialisation */
309 memcpy(p_model, &light_ctl_setup_sig_template, sizeof(MESHX_MODEL));
310
311 ((MESHX_LIGHT_CTL_SETUP_SRV *)*p_ctl_srv)->rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP;
312 ((MESHX_LIGHT_CTL_SETUP_SRV *)*p_ctl_srv)->rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP;
313
315 if (!((MESHX_LIGHT_CTL_SETUP_SRV *)*p_ctl_srv)->state)
316 {
317 MESHX_FREE(*p_ctl_srv);
318 return MESHX_NO_MEM;
319 }
320
321 ((MESHX_LIGHT_CTL_SETUP_SRV *)*p_ctl_srv)->state->temperature_range_max = 0;
322 ((MESHX_LIGHT_CTL_SETUP_SRV *)*p_ctl_srv)->state->temperature_range_min = 0;
323
324 ((MESHX_MODEL *)p_model)->user_data = *p_ctl_srv;
325
326 meshx_ptr_t *temp = (meshx_ptr_t *)&((MESHX_MODEL *)p_model)->pub;
327
328 *temp = *p_pub;
329
330 return err;
331}
332
334{
335 if (!p_model || !p_pub || !p_ctl_srv)
336 return MESHX_INVALID_ARG;
337
339
340 err = meshx_plat_create_model_pub(p_pub, 1);
341 if (err)
342 return meshx_plat_del_model_pub(p_pub);
343
344 *p_ctl_srv = (MESHX_LIGHT_CTL_SRV *)MESHX_CALOC(1, sizeof(MESHX_LIGHT_CTL_SRV));
345 if (!*p_ctl_srv)
346 return MESHX_NO_MEM;
347
348 /* SIG ON OFF initialisation */
349
350 memcpy(p_model, &light_ctl_sig_template, sizeof(MESHX_MODEL));
351
352 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP;
353 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP;
354
355 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state = (MESHX_LIGHT_CTL_STATE *)MESHX_CALOC(1, sizeof(MESHX_LIGHT_CTL_STATE));
356 if (!((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state)
357 {
358 MESHX_FREE(*p_ctl_srv);
359 return MESHX_NO_MEM;
360 }
361
362 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state->temperature = 0;
363 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state->lightness = 0;
364 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state->delta_uv = 0;
365 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state->temperature_range_min = 0;
366 ((MESHX_LIGHT_CTL_SRV *)*p_ctl_srv)->state->temperature_range_max = 0;
367
368 ((MESHX_MODEL *)p_model)->user_data = *p_ctl_srv;
369
370 meshx_ptr_t *temp = (meshx_ptr_t *)&((MESHX_MODEL *)p_model)->pub;
371
372 *temp = *p_pub;
373
374 return err;
375}
376
378 uint16_t delta_uv,
379 uint16_t lightness,
380 uint16_t temperature,
381 uint16_t temp_range_max,
382 uint16_t temp_range_min
383 )
384{
385 if (!p_model)
386 return MESHX_INVALID_ARG;
387
388 MESHX_MODEL *model = (MESHX_MODEL *)p_model;
389 MESHX_LIGHT_CTL_SRV *srv = (MESHX_LIGHT_CTL_SRV *)model->user_data;
390 srv->state->delta_uv = delta_uv;
391 srv->state->lightness = lightness;
392 srv->state->temperature = temperature;
393 srv->state->temperature_range_min = temp_range_min;
394 srv->state->temperature_range_max = temp_range_max;
395
396 return MESHX_SUCCESS;
397}
399 uint16_t delta_uv,
400 uint16_t lightness,
401 uint16_t temperature,
402 uint16_t temp_range_max,
403 uint16_t temp_range_min)
404{
406 p_model,
407 delta_uv,
408 lightness,
409 temperature,
410 temp_range_max,
411 temp_range_min
412 );
413}
meshx_err_t meshx_plat_light_ctl_setup_srv_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_ctl_srv)
Creates and initializes a Light CTL (Color Temperature Lightness) Setup Server model instance.
meshx_err_t meshx_plat_set_light_ctl_srv_state(meshx_ptr_t p_model, uint16_t delta_uv, uint16_t lightness, uint16_t temperature, uint16_t temp_range_max, uint16_t temp_range_min)
Set the state of the Light CTL Server.
meshx_err_t meshx_plat_light_srv_delete(meshx_ptr_t *p_pub, meshx_ptr_t *p_ctl_srv)
Delete a Light CTL Server instance.
static void meshx_ble_lightness_server_cb(MESHX_LIGHT_SRV_CB_EVT event, MESHX_LIGHT_SRV_CB_PARAM *param)
Callback function for BLE Mesh Lightness Server events.
meshx_err_t meshx_plat_light_ctl_srv_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_ctl_srv)
Create a Light CTL Server instance.
static const MESHX_MODEL light_ctl_setup_sig_template
Template for CTL Setup Srv SIG model initialization.
static const MESHX_MODEL light_ctl_sig_template
Template for CTL Setup Srv SIG model initialization.
meshx_err_t meshx_plat_gen_light_srv_send_status(const meshx_model_t *p_model, const meshx_ctx_t *p_ctx, const meshx_lighting_server_state_change_t *state_change)
Send a status message from the Light Server. This function constructs and sends a status message cont...
union meshx_plat_ctl_status meshx_plat_ctl_status_t
Light CTL status packet.
meshx_err_t meshx_plat_light_ctl_srv_restore(meshx_ptr_t p_model, uint16_t delta_uv, uint16_t lightness, uint16_t temperature, uint16_t temp_range_max, uint16_t temp_range_min)
Restore the state of the Light CTL Server.
meshx_err_t meshx_plat_light_srv_init(void)
Initialize the platform-specific Light Server.
meshx_err_t meshx_plat_del_model_pub(meshx_ptr_t *p_pub)
Deletes the model and publication objects.
meshx_err_t meshx_plat_create_model_pub(meshx_ptr_t *p_pub, uint16_t nmax)
Creates and initializes model and publication structures.
struct meshx_model meshx_model_t
struct meshx_ctx meshx_ctx_t
Structure to hold context information for BLE Mesh operations.
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS
#define MESHX_MODEL_OP_LIGHT_CTL_SET
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET
#define MESHX_MODEL_OP_LIGHT_CTL_STATUS
void * meshx_ptr_t
#define MESHX_MODEL_ID_LIGHT_CTL_SETUP_SRV
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_DEFAULT_GET
#define MESHX_MODEL_ID_LIGHT_CTL_SRV
#define MESHX_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS
#define MESHX_MODEL_OP_LIGHT_CTL_DEFAULT_SET
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET
#define MESHX_MODEL_OP_LIGHT_CTL_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET
#define MESHX_MODEL_OP_LIGHT_CTL_GET
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET
Header file for Generic Server Models in MeshX BLE Mesh.
Header file for the MeshX BLE Mesh Light Server module. This file defines the data structures,...
struct meshx_lighting_server_cb_param meshx_lighting_server_cb_param_t
@ CONTROL_TASK_MSG_CODE_FRM_BLE
meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params)
Publish a control task message.
#define MESHX_CALOC
Definition meshx_err.h:28
#define MESHX_UNUSED(x)
Definition meshx_err.h:15
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_ERR_PLAT
Definition meshx_err.h:43
@ MESHX_NO_MEM
Definition meshx_err.h:44
#define MESHX_FREE
Definition meshx_err.h:32
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:100
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
Header file for MeshX BLE Mesh platform abstraction layer. This file provides type definitions and ma...
#define MESHX_LIGHT_SRV_CB_PARAM
#define MESHX_LIGHT_CTL_STATE
#define MESHX_LIGHT_SRV_CB
#define MESHX_LIGHT_SRV_CB_EVT
#define MESHX_LIGHT_CTL_SRV
#define MESHX_LIGHT_CTL_SETUP_SRV
#define MESHX_MODEL
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:30
meshx_ptr_t p_ctx
uint16_t dst_addr
meshx_lighting_server_state_change_t state_change
meshx_ptr_t p_model
Lighting Server Model state change value union.
meshx_state_change_light_ctl_temperature_set_t ctl_temp_set
meshx_state_change_light_ctl_default_set_t ctl_default_set
meshx_state_change_light_ctl_temperature_range_set_t ctl_temp_range_set
meshx_state_change_light_ctl_set_t ctl_set
Light CTL status packet.
struct meshx_plat_ctl_status::@245236127353215275375207066165060111051263370136 ctl_default
struct meshx_plat_ctl_status::@056332017355315015251101121062125272154151302377 ctl_temp_status
struct meshx_plat_ctl_status::@113331121155070315153135031056271051132165372174 ctl_temp_range
struct meshx_plat_ctl_status::@120032367014025213103020351221177247351027370012 ctl_status