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

Implementation of the CW-WW (Cool White - Warm White) client model for BLE Mesh. More...

Include dependency graph for meshx_light_cwww_client.c:

Go to the source code of this file.

Macros

#define CONFIG_SERVER_CB_MASK
 Configuration server callback event mask for cwww server.
 
#define MOD_LCC   MODULE_ID_ELEMENT_LIGHT_CWWWW_CLIENT
 
#define CWWW_CLI_MESHX_ONOFF_ENABLE_CB   true
 
#define CWWW_CLI_EL_STATE_CH_EVT_MASK   CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_ON_OFF | CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_CTL
 
#define IS_EL_IN_RANGE(_element_id)
 
#define GET_RELATIVE_EL_IDX(_element_id)
 
#define CWWW_CLI_EL(_el_id)
 

Functions

static meshx_err_t meshx_element_struct_init (uint16_t n_max)
 Initializes the CW-WW client model.
 
static meshx_err_t meshx_element_struct_deinit (uint16_t n_max)
 Deinitializes the CW-WW client model.
 
static meshx_err_t meshx_dev_create_cwww_model_space (dev_struct_t const *pdev, uint16_t n_max)
 Creates a CW-WW model space for the given device.
 
static meshx_err_t meshx_add_cwww_cli_model_to_element_list (dev_struct_t *pdev, uint16_t *start_idx, uint16_t n_max)
 This function adds the CW-WW client models to the element list of the specified device. It initializes the necessary structures and configurations for each model.
 
static meshx_err_t meshx_cwww_cli_reg_app_req_cb ()
 Registers a callback handler for CW-WW application requests.
 
static meshx_err_t meshx_cwww_cli_el_state_change_reg_cb ()
 
meshx_err_t meshx_cwww_el_get_state (uint16_t element_id, cwww_cli_sig_id_t model_id)
 Retrieves the current state of the CW/WW (Cool White/Warm White) light element for the specified element ID.
 
meshx_err_t create_cwww_client_elements (dev_struct_t *pdev, uint16_t element_cnt)
 Create Dynamic CW-WW Model Elements.
 
 REG_MESHX_ELEMENT_FN (cwww_cli_el, MESHX_ELEMENT_TYPE_LIGHT_CWWW_CLIENT, create_cwww_client_elements)
 

Variables

static meshx_cwww_client_elements_ctrl_t cwww_client_element_init_ctrl
 

Detailed Description

Implementation of the CW-WW (Cool White - Warm White) client model for BLE Mesh.

Copyright © 2024 - 2025 MeshX

This file contains the implementation of the CW-WW client model, including initialization, configuration, and message handling functions. The CW-WW client model is used to control and manage CW-WW lighting devices in a BLE Mesh network.

Note
This implementation supports configuration server callbacks and generic client callbacks. It also includes functions to create and add CW-WW client models to the device's element list.

The CW-WW client model supports the following features:

  • Initialization and allocation of resources for CW-WW models.
  • Handling of configuration server events such as model publication and application key binding.
  • Handling of generic client callback events for CW-WW models.
  • Sending CW-WW messages to the server.
Author
Pranjal Chanda

Definition in file meshx_light_cwww_client.c.

Macro Definition Documentation

◆ CONFIG_SERVER_CB_MASK

#define CONFIG_SERVER_CB_MASK
Value:

Configuration server callback event mask for cwww server.

Definition at line 38 of file meshx_light_cwww_client.c.

38#define CONFIG_SERVER_CB_MASK \
39 CONTROL_TASK_MSG_EVT_PUB_ADD \
40 | CONTROL_TASK_MSG_EVT_SUB_ADD \
41 | CONTROL_TASK_MSG_EVT_APP_KEY_BIND

◆ CWWW_CLI_EL

#define CWWW_CLI_EL ( _el_id)
Value:
static meshx_cwww_client_elements_ctrl_t cwww_client_element_init_ctrl

Definition at line 59 of file meshx_light_cwww_client.c.

◆ CWWW_CLI_EL_STATE_CH_EVT_MASK

Definition at line 55 of file meshx_light_cwww_client.c.

◆ CWWW_CLI_MESHX_ONOFF_ENABLE_CB

#define CWWW_CLI_MESHX_ONOFF_ENABLE_CB   true

Definition at line 54 of file meshx_light_cwww_client.c.

◆ GET_RELATIVE_EL_IDX

#define GET_RELATIVE_EL_IDX ( _element_id)
Value:
((_element_id) - cwww_client_element_init_ctrl.element_id_start)

Definition at line 58 of file meshx_light_cwww_client.c.

◆ IS_EL_IN_RANGE

#define IS_EL_IN_RANGE ( _element_id)
Value:
((_element_id) >= cwww_client_element_init_ctrl.element_id_start && (_element_id) < cwww_client_element_init_ctrl.element_id_end)

Definition at line 57 of file meshx_light_cwww_client.c.

◆ MOD_LCC

Definition at line 53 of file meshx_light_cwww_client.c.

Function Documentation

◆ create_cwww_client_elements()

meshx_err_t create_cwww_client_elements ( dev_struct_t * pdev,
uint16_t element_cnt )

Create Dynamic CW-WW Model Elements.

Parameters
[in]pdevPointer to device structure
[in]element_cntMaximum number of CW-WW models
Returns
meshx_err_t

Definition at line 998 of file meshx_light_cwww_client.c.

999{
1000 meshx_err_t err;
1001
1002 err = meshx_dev_create_cwww_model_space(pdev, element_cnt);
1003 if (err)
1004 {
1005 MESHX_LOGE(MOD_LCC, "CWWW Model space create failed: (%d)", err);
1006 return err;
1007 }
1008
1009 err = meshx_add_cwww_cli_model_to_element_list(pdev, (uint16_t *)&pdev->element_idx, element_cnt);
1010 if (err)
1011 {
1012 MESHX_LOGE(MOD_LCC, "CWWW Model add to element create failed: (%d)", err);
1013 return err;
1014 }
1015
1016 err = meshx_on_off_client_init();
1017 if (err)
1018 {
1019 MESHX_LOGE(MOD_LCC, "meshx_onoff_client_init failed: (%d)", err);
1020 return err;
1021 }
1022
1024 if (err)
1025 {
1026 MESHX_LOGE(MOD_LCC, "meshx_light_ctl_client_init failed: (%d)", err);
1027 return err;
1028 }
1029
1030#if CONFIG_ENABLE_CONFIG_SERVER
1032 (config_srv_cb_t) &cwww_client_config_srv_cb,
1034 if (err)
1035 {
1036 MESHX_LOGE(MOD_LCC, "Light CWWW config server callback reg failed: (%d)", err);
1037 return err;
1038 }
1039#endif /* CONFIG_ENABLE_CONFIG_SERVER */
1040
1041#if defined(__MESHX_CONTROL_TASK__)
1042 /* Register control task callback */
1044 if (err)
1045 {
1046 MESHX_LOGE(MOD_LCC, "control task callback reg failed: (%d)", err);
1047 return err;
1048 }
1049 /* Register element state change callback */
1051 if (err)
1052 {
1053 MESHX_LOGE(MOD_LCC, "element state change callback reg failed: (%d)", err);
1054 return err;
1055 }
1056 /* Register freshboot control task callback */
1058 (prov_srv_cb_t)&cwww_cli_prov_srv_msg_handle);
1059 if (err)
1060 {
1061 MESHX_LOGE(MOD_LCC, "control task callback reg failed: (%d)", err);
1062 return err;
1063 }
1064
1065#endif /* __MESHX_CONTROL_TASK__ */
1066#if CONFIG_ENABLE_UNIT_TEST
1067 err = register_unit_test(MODULE_ID_ELEMENT_LIGHT_CWWWW_CLIENT, &cwww_cli_unit_test_cb_handler);
1068 if (err)
1069 {
1070 MESHX_LOGE(MOD_LCC, "unit_test reg failed: (%d)", err);
1071 return err;
1072 }
1073#endif /* CONFIG_ENABLE_UNIT_TEST */
1074
1075 return MESHX_SUCCESS;
1076}
control_task_msg_handle_t config_srv_cb_t
control_task_msg_handle_t prov_srv_cb_t
meshx_err_t meshx_config_server_cb_reg(config_srv_cb_t cb, uint32_t config_evt_bmap)
Registers a configuration server callback for specific events.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
meshx_err_t meshx_light_ctl_client_init()
Initialize the Light CTL Client model.
#define CONFIG_SERVER_CB_MASK
Configuration server callback event mask for cwww server.
static meshx_err_t meshx_cwww_cli_el_state_change_reg_cb()
static meshx_err_t meshx_cwww_cli_reg_app_req_cb()
Registers a callback handler for CW-WW application requests.
#define MOD_LCC
static meshx_err_t meshx_dev_create_cwww_model_space(dev_struct_t const *pdev, uint16_t n_max)
Creates a CW-WW model space for the given device.
static meshx_err_t meshx_add_cwww_cli_model_to_element_list(dev_struct_t *pdev, uint16_t *start_idx, uint16_t n_max)
This function adds the CW-WW client models to the element list of the specified device....
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
meshx_err_t meshx_prov_srv_reg_el_client_cb(prov_srv_cb_t cb)
Register a callback function for provisioning events.
@ MODULE_ID_ELEMENT_LIGHT_CWWWW_CLIENT
Definition module_id.h:25
size_t element_idx
meshx_err_t register_unit_test(module_id_t module_id, module_callback_t callback)
Register a unit test for a specific module.
Definition unit_test.c:149
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_add_cwww_cli_model_to_element_list()

static meshx_err_t meshx_add_cwww_cli_model_to_element_list ( dev_struct_t * pdev,
uint16_t * start_idx,
uint16_t n_max )
static

This function adds the CW-WW client models to the element list of the specified device. It initializes the necessary structures and configurations for each model.

Parameters
[in]pdevPointer to the device structure.
[in]start_idxPointer to the element index.
[in]n_maxMaximum number of elements that can be created in the model space.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_NO_MEM: Memory allocation failure
  • MESHX_INVALID_ARG: Invalid arguments

Definition at line 854 of file meshx_light_cwww_client.c.

855{
856 if (!pdev || !start_idx || !n_max)
857 return MESHX_INVALID_ARG;
858
859 if ((n_max + *start_idx) > CONFIG_MAX_ELEMENT_COUNT)
860 {
861 MESHX_LOGE(MOD_LCC, "No of elements limit reached");
862 return MESHX_NO_MEM;
863 }
865
866 cwww_client_element_init_ctrl.element_id_start = *start_idx;
867
868 for (uint16_t i = *start_idx; i < (n_max + *start_idx) && (i - *start_idx) < CWWW_CLI_MODEL_SIG_CNT; i++)
869 {
870 if (i == 0)
871 continue;
873 i,
874 pdev->elements,
875 CWWW_CLI_EL(i - *start_idx).cwww_cli_sig_model_list,
876 NULL,
879 if (err)
880 {
881 MESHX_LOGE(MOD_LCC, "Failed to add element to composition: (%d)", err);
882 return err;
883 }
885 CWWW_CLI_EL(i - *start_idx).cwww_cli_ctx,
887 if (err != MESHX_SUCCESS)
888 {
889 MESHX_LOGW(MOD_LCC, "Failed to get cwww cli element context: (0x%x)", err);
890 }
891 }
892 /* Increment the index for further registrations */
893 cwww_client_element_init_ctrl.element_id_end = *start_idx += n_max;
894 return MESHX_SUCCESS;
895}
meshx_err_t meshx_plat_add_element_to_composition(uint16_t index, meshx_ptr_t p_element_list, meshx_ptr_t p_sig_models, meshx_ptr_t p_ven_models, uint8_t sig_cnt, uint8_t ven_cnt)
Adds an element to the BLE Mesh composition.
#define CONFIG_MAX_ELEMENT_COUNT
Total Element Count in the Composition.
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_NO_MEM
Definition meshx_err.h:44
#define CWWW_CLI_EL(_el_id)
#define CWWW_CLI_MODEL_VEN_CNT
Number of Vendor models in a CW-WW model element.
#define CWWW_CLI_MODEL_SIG_CNT
Number of SIG models in a CW-WW model element.
struct cwww_cli_ctx meshx_cwww_client_model_ctx_t
Structure to hold the context of the cwww client.
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:87
meshx_err_t meshx_nvs_element_ctx_get(uint16_t element_id, void *blob, size_t blob_size)
Retrieve the context of a specific element from NVS.
Definition meshx_nvs.c:424
Structure to hold the context of the cwww client.
MESHX_ELEMENT elements[MAX_ELE_CNT]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_cwww_cli_el_state_change_reg_cb()

static meshx_err_t meshx_cwww_cli_el_state_change_reg_cb ( )
static

Definition at line 913 of file meshx_light_cwww_client.c.

914{
918 (control_task_msg_handle_t)&meshx_cwww_client_element_state_change_handler
919 );
920}
meshx_err_t(* control_task_msg_handle_t)(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Function pointer type for control task message handler.
@ CONTROL_TASK_MSG_CODE_EL_STATE_CH
meshx_err_t control_task_msg_subscribe(control_task_msg_code_t msg_code, control_task_msg_evt_t evt_bmap, control_task_msg_handle_t callback)
Subscribe to a control task message.
#define CWWW_CLI_EL_STATE_CH_EVT_MASK
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_cwww_cli_reg_app_req_cb()

static meshx_err_t meshx_cwww_cli_reg_app_req_cb ( )
static

Registers a callback handler for CW-WW application requests.

This function subscribes the provided callback to control task messages related to BLE events. It ensures the callback is valid before subscribing.

Definition at line 904 of file meshx_light_cwww_client.c.

905{
909 (control_task_msg_handle_t)&meshx_cwww_client_element_to_ble_handler
910 );
911}
@ CONTROL_TASK_MSG_CODE_TO_BLE
#define CONTROL_TASK_MSG_CODE_EVT_MASK
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_cwww_el_get_state()

meshx_err_t meshx_cwww_el_get_state ( uint16_t element_id,
cwww_cli_sig_id_t model_id )

Retrieves the current state of the CW/WW (Cool White/Warm White) light element for the specified element ID.

This function queries the state of a light element identified by the given element_id.

Parameters
[in]element_idThe unique identifier of the light element whose state is to be retrieved.
[in]model_idThe model ID to specify which model's state to retrieve. If set to CWWW_CLI_SIG_ID_MAX, it retrieves the state for all models.
Returns
meshx_err_t Returns MESHX_OK on success, or an appropriate error code on failure.

Definition at line 931 of file meshx_light_cwww_client.c.

932{
933 if (!IS_EL_IN_RANGE(element_id))
934 {
935 MESHX_LOGE(MOD_LCC, "Invalid element id: %d", element_id);
936 return MESHX_INVALID_ARG;
937 }
938 meshx_cwww_client_msg_t msg = {0};
939 msg.ack = CWWW_CLI_MSG_ACK;
941 msg.element_id = element_id;
942
943 if (model_id != CWWW_CLI_SIG_ID_MAX)
944 {
945 MESHX_LOGD(MOD_LCC, "Sending GET for model: %d", model_id);
946 if(model_id == CWWW_CLI_SIG_L_CTL_MODEL_ID)
947 {
951 &msg,
952 sizeof(msg));
953 }
954 else
955 {
959 &msg,
960 sizeof(msg));
961 }
962 }
963 else
964 {
965 for(model_id = CWWW_CLI_SIG_ONOFF_MODEL_ID; model_id < CWWW_CLI_SIG_ID_MAX; model_id++)
966 {
967
968 MESHX_LOGD(MOD_LCC, "Sending GET for model: %d", model_id);
969 if(model_id == CWWW_CLI_SIG_L_CTL_MODEL_ID)
970 {
974 &msg,
975 sizeof(msg));
976 }
977 else
978 {
982 &msg,
983 sizeof(msg));
984 }
985 }
986 }
987 return MESHX_SUCCESS;
988}
CONTROL_TASK_MSG_EVT_TO_BLE_SET_CTL
CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF
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 IS_EL_IN_RANGE(_element_id)
#define CWWW_CLI_MSG_ACK
Acknowledgment message type.
struct cwww_client_msg meshx_cwww_client_msg_t
Structure to hold the cwww client message sent from APP layer.
#define CWWW_CLI_MSG_GET
Message type for getting CW-WW client state.
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
Here is the call graph for this function:

◆ meshx_dev_create_cwww_model_space()

static meshx_err_t meshx_dev_create_cwww_model_space ( dev_struct_t const * pdev,
uint16_t n_max )
static

Creates a CW-WW model space for the given device.

This function initializes and allocates resources for a CW-WW (Cool White - Warm White) model space for the specified device. It sets up the necessary structures and configurations to manage the CW-WW model.

Parameters
[in]pdevPointer to the device structure.
[in]n_maxMaximum number of elements that can be created in the model space.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_NO_MEM: Memory allocation failure
  • MESHX_INVALID_ARG: Invalid arguments

Definition at line 825 of file meshx_light_cwww_client.c.

826{
827 if (!pdev)
828 return MESHX_INVALID_STATE;
829
831 if (err)
832 {
833 MESHX_LOGE(MOD_LCC, "Failed to initialize cwww element structures: (%d)", err);
835 return err;
836 }
837 return MESHX_SUCCESS;
838}
@ MESHX_INVALID_STATE
Definition meshx_err.h:45
static meshx_err_t meshx_element_struct_init(uint16_t n_max)
Initializes the CW-WW client model.
static meshx_err_t meshx_element_struct_deinit(void)
Deinitializes the mesh element structure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_element_struct_deinit()

static meshx_err_t meshx_element_struct_deinit ( uint16_t n_max)
static

Deinitializes the CW-WW client model.

This function deinitializes the CW-WW client model by freeing the memory allocated for the CW-WW client context, client list, publish list, and CW-WW client model list.

Parameters
[in]n_maxMaximum number of elements that can be created in the model space.

Definition at line 782 of file meshx_light_cwww_client.c.

783{
785 {
786 MESHX_LOGE(MOD_LCC, "CWWW element list not initialized");
787 return MESHX_INVALID_STATE;
788 }
789
791 {
792 for (size_t i = 0; i < n_max; i++)
793 {
795 {
797 CWWW_CLI_EL(i).cwww_cli_ctx = NULL;
798 }
799 }
801 cwww_client_element_init_ctrl.el_list = NULL;
802 }
803 cwww_client_element_init_ctrl.element_cnt = 0;
804 cwww_client_element_init_ctrl.element_id_end = 0;
805 cwww_client_element_init_ctrl.element_id_start = 0;
806
807 return MESHX_SUCCESS;
808}
#define MESHX_FREE
Definition meshx_err.h:32

◆ meshx_element_struct_init()

static meshx_err_t meshx_element_struct_init ( uint16_t n_max)
static

Initializes the CW-WW client model.

This function initializes the CW-WW client model by allocating memory for the CW-WW client context, client list, publish list, and CW-WW client model list.

Parameters
[in]n_maxMaximum number of elements that can be created in the model space.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_NO_MEM: Memory allocation failure
  • MESHX_INVALID_ARG: Invalid arguments

Definition at line 724 of file meshx_light_cwww_client.c.

725{
726 if(!n_max)
727 return MESHX_INVALID_ARG;
728
730
731 cwww_client_element_init_ctrl.element_cnt = n_max;
732 cwww_client_element_init_ctrl.element_id_end = 0;
733 cwww_client_element_init_ctrl.element_id_start = 0;
734
738 {
739 MESHX_LOGE(MOD_LCC, "Failed to allocate memory for cwww client elements");
740 return MESHX_NO_MEM;
741 }
742
743 for (size_t i = 0; i < cwww_client_element_init_ctrl.element_cnt; i++)
744 {
747 {
748 MESHX_LOGE(MOD_LCC, "Failed to allocate memory for cwww client context");
749 return MESHX_NO_MEM;
750 }
751 err = meshx_on_off_client_create(&CWWW_CLI_EL(i).onoff_cli_model,
752 &CWWW_CLI_EL(i).cwww_cli_sig_model_list[CWWW_CLI_SIG_ONOFF_MODEL_ID]);
753 if (err)
754 {
755 MESHX_LOGE(MOD_LCC, "Meshx On Off Client create failed (Err : 0x%x)", err);
756 return err;
757 }
758 CWWW_CLI_EL(i).onoff_cli_model->meshx_sig
759 = &CWWW_CLI_EL(i).cwww_cli_sig_model_list[CWWW_CLI_SIG_ONOFF_MODEL_ID];
760
761 err = meshx_light_ctl_client_create(&CWWW_CLI_EL(i).ctl_cli_model,
762 &CWWW_CLI_EL(i).cwww_cli_sig_model_list[CWWW_CLI_SIG_L_CTL_MODEL_ID]);
763 if (err)
764 {
765 MESHX_LOGE(MOD_LCC, "Meshx CTL Client create failed (Err : 0x%x)", err);
766 return err;
767 }
768 CWWW_CLI_EL(i).ctl_cli_model->meshx_sig
769 = &CWWW_CLI_EL(i).cwww_cli_sig_model_list[CWWW_CLI_SIG_L_CTL_MODEL_ID];
770 }
771 return MESHX_SUCCESS;
772}
#define MESHX_CALOC
Definition meshx_err.h:28
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.
struct cwww_client_element meshx_cwww_client_elements_t
Structure to hold the cwww client element.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REG_MESHX_ELEMENT_FN()

REG_MESHX_ELEMENT_FN ( cwww_cli_el ,
MESHX_ELEMENT_TYPE_LIGHT_CWWW_CLIENT ,
create_cwww_client_elements  )
Here is the call graph for this function:

Variable Documentation

◆ cwww_client_element_init_ctrl

meshx_cwww_client_elements_ctrl_t cwww_client_element_init_ctrl
static

Definition at line 61 of file meshx_light_cwww_client.c.