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
meshx_light_ctl_client.c File Reference

Implementation of the Light CTL Client model for ESP32 BLE Mesh. More...

#include "meshx_err.h"
#include "meshx_light_ctl_client.h"
Include dependency graph for meshx_light_ctl_client.c:

Go to the source code of this file.

Macros

#define LIGHT_CTL_CLIENT_INIT_MAGIC   0x8932
 

Functions

static meshx_err_t meshx_ctl_state_change_notify (const meshx_gen_light_cli_cb_param_t *param, uint8_t status)
 Notifies about a change in the CTL (Color Temperature Lightness) state.
 
static meshx_err_t meshx_handle_gen_light_msg (const dev_struct_t *pdev, control_task_msg_evt_t model_id, const meshx_gen_light_cli_cb_param_t *param)
 Handles generic light model messages for the Light CTL client.
 
meshx_err_t meshx_light_ctl_client_init ()
 Initialize the Light CTL Client model.
 
meshx_err_t meshx_light_ctl_client_create (meshx_light_ctl_client_model_t **p_model, void *p_sig_model)
 Creates and initializes a Generic Light Client model instance.
 
meshx_err_t meshx_light_ctl_client_delete (meshx_light_ctl_client_model_t **p_model)
 Delete the Light client model instance.
 
meshx_err_t meshx_light_ctl_client_send_msg (meshx_gen_ctl_send_params_t *params)
 Sends a Light CTL (Color Temperature Lightness) message from the Light CTL Client model.
 
meshx_err_t meshx_light_ctl_temperature_client_send_msg (meshx_gen_ctl_send_params_t *params)
 Sends a Light CTL Temperature message from the client model.
 
meshx_err_t meshx_light_ctl_temp_range_client_send_msg (meshx_gen_ctl_send_params_t *params)
 Sends a Light CTL Temperature Range message from the client model.
 
meshx_err_t meshx_light_ctl_state_change_handle (const meshx_ctl_cli_el_msg_t *param, meshx_ctl_el_state_t *p_ctl_prev_state, const meshx_ctl_el_state_t *p_ctl_next_state)
 Handles state changes for the Light CTL client element.
 

Variables

static uint16_t light_ctl_client_init_flag = 0
 

Detailed Description

Implementation of the Light CTL Client model for ESP32 BLE Mesh.

Copyright © 2024 - 2025 MeshX

This file contains the implementation of the Light CTL Client model, including initialization, callback registration, and event handling.

Author
Pranjal Chanda

Definition in file meshx_light_ctl_client.c.

Macro Definition Documentation

◆ LIGHT_CTL_CLIENT_INIT_MAGIC

#define LIGHT_CTL_CLIENT_INIT_MAGIC   0x8932

Definition at line 16 of file meshx_light_ctl_client.c.

Function Documentation

◆ meshx_ctl_state_change_notify()

static meshx_err_t meshx_ctl_state_change_notify ( const meshx_gen_light_cli_cb_param_t * param,
uint8_t status )
static

Notifies about a change in the CTL (Color Temperature Lightness) state.

This function is called to notify the application or upper layers when the CTL state of a light device has changed. It provides the relevant parameters describing the new state.

Parameters
[in]paramPointer to a structure containing the CTL state change parameters.
[in]statusStatus code indicating the result of the notification operation.
Returns
meshx_err_t Returns an error code indicating the result of the notification operation.

Definition at line 33 of file meshx_light_ctl_client.c.

34{
35 if (!param)
36 return MESHX_INVALID_ARG;
37
38 meshx_ctl_cli_el_msg_t el_light_ctl_param = {
39 .err_code = status,
40 .ctx = param->ctx,
41 .model = param->model
42 };
43 switch (param->ctx.opcode)
44 {
46 el_light_ctl_param.ctl_state.lightness = param->status.ctl_status.present_ctl_lightness;
48 break;
52 break;
56 break;
58 el_light_ctl_param.ctl_state.delta_uv = param->status.ctl_default_status.delta_uv;
59 el_light_ctl_param.ctl_state.lightness = param->status.ctl_default_status.lightness;
60 el_light_ctl_param.ctl_state.temperature = param->status.ctl_default_status.temperature;
61 break;
62
63 default:
64 break;
65 }
67 {
68 el_light_ctl_param.err_code = MESHX_TIMEOUT;
69 }
70
74 &el_light_ctl_param,
76
77}
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS
#define MESHX_MODEL_OP_LIGHT_CTL_STATUS
#define MESHX_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS
@ MESHX_GEN_LIGHT_CLI_TIMEOUT
@ CONTROL_TASK_MSG_CODE_EL_STATE_CH
CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_CTL
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.
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_TIMEOUT
Definition meshx_err.h:48
struct meshx_ctl_cli_el_msg meshx_ctl_cli_el_msg_t
Structure to hold the On/Off Server to element message.
meshx_ctl_el_state_t ctl_state
meshx_gen_light_client_status_cb_t status
meshx_light_ctl_temperature_range_status_cb_t ctl_temperature_range_status
meshx_light_ctl_temperature_status_cb_t ctl_temperature_status
meshx_light_ctl_default_status_cb_t ctl_default_status
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_handle_gen_light_msg()

static meshx_err_t meshx_handle_gen_light_msg ( const dev_struct_t * pdev,
control_task_msg_evt_t model_id,
const meshx_gen_light_cli_cb_param_t * param )
static

Handles generic light model messages for the Light CTL client.

This function processes incoming messages related to the generic light model and performs the necessary actions based on the message event and parameters.

Parameters
[in]pdevPointer to the device structure containing device-specific information.
[in]model_idIdentifier for the control task message event associated with the model.
[in,out]paramPointer to the callback parameter structure for the generic light client.
Returns
meshx_err_t Error code indicating the result of the operation.

Definition at line 91 of file meshx_light_ctl_client.c.

96{
97 if (model_id != MESHX_MODEL_ID_LIGHT_CTL_CLI || !pdev || !param)
98 return MESHX_INVALID_ARG;
99
100 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "op|src|dst:%04" PRIx32 "|%04x|%04x",
101 param->ctx.opcode, param->ctx.src_addr, param->ctx.dst_addr);
105
106 return err;
107}
#define MESHX_MODEL_ID_LIGHT_CTL_CLI
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
static meshx_err_t meshx_ctl_state_change_notify(const meshx_gen_light_cli_cb_param_t *param, uint8_t status)
Notifies about a change in the CTL (Color Temperature Lightness) state.
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:30
uint16_t src_addr
uint16_t dst_addr
Here is the call graph for this function:

◆ meshx_light_ctl_client_create()

meshx_err_t meshx_light_ctl_client_create ( meshx_light_ctl_client_model_t ** p_model,
void * p_sig_model )

Creates and initializes a Generic Light Client model instance.

This function allocates and sets up a Generic Light Client model, associating it with the provided SIG model context.

Parameters
[out]p_modelPointer to a pointer where the created model instance will be stored.
[in]p_sig_modelPointer to the SIG model context to associate with the client model.
Returns
meshx_err_t Returns an error code indicating the result of the operation.
  • MESHX_OK on success
  • Appropriate error code otherwise

Definition at line 157 of file meshx_light_ctl_client.c.

158{
159 if (!p_model || !p_sig_model)
160 {
161 return MESHX_INVALID_ARG;
162 }
163
165 if (!*p_model)
166 {
167 return MESHX_NO_MEM;
168 }
169
171 p_sig_model,
172 &((*p_model)->meshx_pub),
173 &((*p_model)->meshx_gen));
174}
meshx_err_t meshx_plat_light_ctl_client_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_light_ctl_cli)
Creates and initializes a Light CTL (Color Temperature Light) client model instance.
#define MESHX_CALOC
Definition meshx_err.h:28
@ MESHX_NO_MEM
Definition meshx_err.h:44
meshx_model_interface_t meshx_light_ctl_client_model_t
Structure representing the Light CTL (Color Temperature Lightness) client model in MeshX.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_light_ctl_client_delete()

meshx_err_t meshx_light_ctl_client_delete ( meshx_light_ctl_client_model_t ** p_model)

Delete the Light client model instance.

This function deletes an instance of the Light client model, freeing associated resources and setting the model pointer to NULL.

Parameters
[in,out]p_modelDouble pointer to the Light client model instance to be deleted.
Returns
  • MESHX_SUCCESS: Successfully deleted the model.
  • MESHX_INVALID_ARG: Invalid argument provided.

Definition at line 188 of file meshx_light_ctl_client.c.

189{
190 if (p_model == NULL || *p_model == NULL)
191 {
192 return MESHX_INVALID_ARG;
193 }
194
196 &((*p_model)->meshx_pub),
197 &((*p_model)->meshx_gen)
198 );
199
200 MESHX_FREE(*p_model);
201 *p_model = NULL;
202
203 return MESHX_SUCCESS;
204}
meshx_err_t meshx_plat_light_client_delete(meshx_ptr_t *p_pub, meshx_ptr_t *p_cli)
Deletes the Light client instance and its associated publication context.
#define MESHX_FREE
Definition meshx_err.h:32
Here is the call graph for this function:

◆ meshx_light_ctl_client_init()

meshx_err_t meshx_light_ctl_client_init ( )

Initialize the Light CTL Client model.

This function initializes the Light CTL Client model by registering the Light Client callback with the BLE Mesh stack.

Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 117 of file meshx_light_ctl_client.c.

118{
120
122 return MESHX_SUCCESS;
123
125
127 if (err)
128 {
129 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Failed to initialize meshx client");
130 }
131
135 );
136 if (err)
137 {
138 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Failed to register Light CTL Client callback: %d", err);
139 }
140
141 return err;
142}
control_task_msg_handle_t meshx_gen_light_client_cb_t
static meshx_err_t meshx_handle_gen_light_msg(dev_struct_t *pdev, control_task_msg_evt_t model_id, meshx_gen_light_cli_cb_param_t *param)
Handle the Generic Light Client messages.
meshx_err_t meshx_gen_light_client_from_ble_reg_cb(uint16_t model_id, meshx_gen_light_client_cb_t cb)
Registers a callback function for getting Generic Light Client messages from BLE.
meshx_err_t meshx_gen_light_cli_init(void)
Initialize the meshxuction generic client.
#define LIGHT_CTL_CLIENT_INIT_MAGIC
static uint16_t light_ctl_client_init_flag
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
@ MODULE_ID_MODEL_CLIENT
Definition module_id.h:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_light_ctl_client_send_msg()

meshx_err_t meshx_light_ctl_client_send_msg ( meshx_gen_ctl_send_params_t * params)

Sends a Light CTL (Color Temperature Lightness) message from the Light CTL Client model.

This function constructs and sends a Light CTL message to a specified destination address using the provided network and application indices. The message contains the desired lightness, temperature, and a transaction identifier (TID).

Parameters
[in]paramsPointer to a structure containing the message parameters.
Returns
meshx_err_t Returns the result of the message send operation.

Definition at line 217 of file meshx_light_ctl_client.c.

218{
220 if (!params || !params->model || !params->model->meshx_sig)
221 {
222 return MESHX_INVALID_ARG; // Invalid model pointer
223 }
225 .state = &set,
226 .addr = params->addr,
227 .opcode = params->opcode,
228 .app_idx = params->app_idx,
229 .net_idx = params->net_idx,
230 .model = params->model->meshx_sig,
231 };
232
234 {
236 }
237 else if (params->opcode == MESHX_MODEL_OP_LIGHT_CTL_SET ||
239 {
240 set.ctl_set.op_en = false;
241 set.ctl_set.tid = params->tid;
242 set.ctl_set.ctl_delta_uv = params->delta_uv;
243 set.ctl_set.ctl_lightness = params->lightness;
244 set.ctl_set.ctl_temperature = params->temperature;
245 }
246 else{
247 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid opcode for Light CTL Client: %04x", params->opcode);
248 return MESHX_INVALID_ARG;
249 }
250 return meshx_gen_light_send_msg(&send_params);
251}
#define MESHX_MODEL_OP_LIGHT_CTL_SET
#define MESHX_MODEL_OP_LIGHT_CTL_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_GET
#define MESHX_DO_NOTHING
Definition meshx_err.h:17
meshx_err_t meshx_gen_light_send_msg(const meshx_gen_light_client_send_params_t *params)
Send a message using the generic client model.
struct meshx_gen_light_client_send_params meshx_gen_light_client_send_params_t
Generic Light Client Model send parameters. This structure is used to pass parameters to the Generic ...
meshx_light_ctl_client_model_t * model
Lighting Client Model set message union.
Here is the call graph for this function:

◆ meshx_light_ctl_state_change_handle()

meshx_err_t meshx_light_ctl_state_change_handle ( const meshx_ctl_cli_el_msg_t * param,
meshx_ctl_el_state_t * p_ctl_prev_state,
const meshx_ctl_el_state_t * p_ctl_next_state )

Handles state changes for the Light CTL client element.

This function processes state change events for the Light CTL client element.

Parameters
[in]paramPointer to the message structure containing the state change parameters.
[in,out]p_ctl_prev_statePointer to the previous state structure.
[in,out]p_ctl_next_statePointer to the next state structure (currently unused).
Returns
meshx_err_t Returns an error code indicating the result of the handler execution.

Definition at line 357 of file meshx_light_ctl_client.c.

362{
363 if (!p_ctl_prev_state || !param || !p_ctl_next_state)
364 return MESHX_INVALID_ARG;
365
366 /* Kept for future use */
367 MESHX_UNUSED(p_ctl_next_state);
368 bool state_change = false;
369 if(param->err_code == MESHX_SUCCESS)
370 {
372 {
373 if (p_ctl_prev_state->lightness != param->ctl_state.lightness
374 || p_ctl_prev_state->temperature != param->ctl_state.temperature
375 )
376 {
377 p_ctl_prev_state->lightness = param->ctl_state.lightness;
378 p_ctl_prev_state->temperature = param->ctl_state.temperature;
379 state_change = true;
380 }
381 }
383 {
384 if(p_ctl_prev_state->delta_uv != param->ctl_state.delta_uv
385 || p_ctl_prev_state->temperature != param->ctl_state.temperature)
386 {
387 p_ctl_prev_state->delta_uv = param->ctl_state.delta_uv;
388 p_ctl_prev_state->temperature = param->ctl_state.temperature;
389 state_change = true;
390 }
391 }
393 {
394 if (p_ctl_prev_state->temp_range_max != param->ctl_state.temp_range_max
395 || p_ctl_prev_state->temp_range_min != param->ctl_state.temp_range_min)
396 {
397 p_ctl_prev_state->temp_range_max = param->ctl_state.temp_range_max;
398 p_ctl_prev_state->temp_range_min = param->ctl_state.temp_range_min;
399 state_change = true;
400 }
401 }
403 {
405 }
406 else
407 {
408 /* Return as No CTL related OPCode were received */
410 }
411 }
412 else
413 {
414 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "CWWW Client Element Message: Error (%d)", param->err_code);
415 /* Retry sending the failed packet done by MeshX Light CTL Layer. Do not notify App */
417 }
418 return state_change ? MESHX_SUCCESS : MESHX_INVALID_STATE;
419}
#define MESHX_UNUSED(x)
Definition meshx_err.h:15
@ MESHX_INVALID_STATE
Definition meshx_err.h:45

◆ meshx_light_ctl_temp_range_client_send_msg()

meshx_err_t meshx_light_ctl_temp_range_client_send_msg ( meshx_gen_ctl_send_params_t * params)

Sends a Light CTL Temperature Range message from the client model.

This function constructs and sends a Light CTL Temperature Range message to a specified address using the provided network and application indices. It allows the client to set or get the temperature range of a lighting element in a mesh network.

Parameters
[in]paramsPointer to a structure containing the message parameters.
Returns
meshx_err_t Result of the message send operation.

Definition at line 312 of file meshx_light_ctl_client.c.

313{
315 if (!params || !params->model || !params->model->meshx_sig)
316 {
317 return MESHX_INVALID_ARG; // Invalid model pointer
318 }
320 .state = &set,
321 .addr = params->addr,
322 .opcode = params->opcode,
323 .app_idx = params->app_idx,
324 .net_idx = params->net_idx,
325 .model = params->model->meshx_sig,
326 };
327
329 {
331 }
334 {
337 }
338 else{
339 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid opcode for Light CTL Client: %04x", params->opcode);
340 return MESHX_INVALID_ARG;
341 }
342
343 return meshx_gen_light_send_msg(&send_params);
344}
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET
meshx_light_ctl_temperature_range_set_t ctl_temperature_range_set
Here is the call graph for this function:

◆ meshx_light_ctl_temperature_client_send_msg()

meshx_err_t meshx_light_ctl_temperature_client_send_msg ( meshx_gen_ctl_send_params_t * params)

Sends a Light CTL Temperature message from the client model.

This function constructs and sends a Light CTL Temperature message to a specified address using the provided network and application indices. It allows the client to control the color temperature and delta UV of a lighting element in a mesh network.

Parameters
[in]paramsPointer to a structure containing the message parameters.
Returns
meshx_err_t Result of the message send operation.

Definition at line 264 of file meshx_light_ctl_client.c.

265{
267 if (!params || !params->model || !params->model->meshx_sig)
268 {
269 return MESHX_INVALID_ARG; // Invalid model pointer
270 }
272 .state = &set,
273 .addr = params->addr,
274 .opcode = params->opcode,
275 .app_idx = params->app_idx,
276 .net_idx = params->net_idx,
277 .model = params->model->meshx_sig,
278 };
279
281 {
283 }
286 {
287 set.ctl_temperature_set.op_en = false;
288 set.ctl_temperature_set.tid = params->tid;
291
292 }
293 else{
294 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid opcode for Light CTL Client: %04x", params->opcode);
295 return MESHX_INVALID_ARG;
296 }
297
298 return meshx_gen_light_send_msg(&send_params);
299}
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET
meshx_light_ctl_temperature_set_t ctl_temperature_set
Here is the call graph for this function:

Variable Documentation

◆ light_ctl_client_init_flag

uint16_t light_ctl_client_init_flag = 0
static

Definition at line 20 of file meshx_light_ctl_client.c.