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

Implementation of the Generic OnOff Server model for BLE Mesh. This file contains the initialization, state management, and message handling logic for the Generic OnOff Server model in the MeshX platform. More...

Include dependency graph for esp_gen_srv_model.c:

Go to the source code of this file.

Macros

#define CONTROL_TASK_MSG_EVT_TO_BLE_GEN_SRV_MASK    CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV
 

Functions

static meshx_err_t meshx_plat_gen_srv_create (void *p_model, void **p_pub, void **p_srv)
 Creates and initializes the Generic Server model platform resources.
 
static void esp_ble_mesh_generic_server_cb (MESHX_GEN_SRV_CB_EVT event, MESHX_GEN_SRV_CB_PARAM *param)
 Callback function for BLE Mesh Generic Server events.
 
meshx_err_t meshx_plat_gen_srv_send_status (meshx_model_t *p_model, meshx_ctx_t *p_ctx, meshx_ptr_t p_data, uint32_t data_len)
 Send a status message from the Generic Server model.
 
meshx_err_t meshx_plat_set_gen_srv_state (void *p_model, uint8_t on_off_state)
 Set the state of a generic server model.
 
meshx_err_t meshx_plat_gen_srv_init (void)
 Initialize the meshxuction generic server.
 
meshx_err_t meshx_plat_on_off_gen_srv_create (void *p_model, void **p_pub, void **p_onoff_srv)
 Creates a Generic OnOff Server model and its publication context.
 
meshx_err_t meshx_plat_gen_srv_delete (void **p_pub, void **p_srv)
 Deletes the Generic OnOff Server model and its associated resources.
 
meshx_err_t meshx_plat_gen_on_off_srv_restore (void *p_model, uint8_t state)
 Restores the state of the Generic OnOff Server model.
 

Variables

static const char * server_state_str []
 String representation of the server state change events.
 

Detailed Description

Implementation of the Generic OnOff Server model for BLE Mesh. This file contains the initialization, state management, and message handling logic for the Generic OnOff Server model in the MeshX platform.

Copyright (c) 2024 - 2025 MeshX

The Generic OnOff Server model is responsible for managing the on/off state of a device in a BLE Mesh network. It handles incoming messages, updates the state, and publishes the state changes to the network.

Author
Pranjal Chanda

Definition in file esp_gen_srv_model.c.

Macro Definition Documentation

◆ CONTROL_TASK_MSG_EVT_TO_BLE_GEN_SRV_MASK

#define CONTROL_TASK_MSG_EVT_TO_BLE_GEN_SRV_MASK    CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV

Definition at line 21 of file esp_gen_srv_model.c.

21#define CONTROL_TASK_MSG_EVT_TO_BLE_GEN_SRV_MASK \
22 CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV

Function Documentation

◆ esp_ble_mesh_generic_server_cb()

static void esp_ble_mesh_generic_server_cb ( MESHX_GEN_SRV_CB_EVT event,
MESHX_GEN_SRV_CB_PARAM * param )
static

Callback function for BLE Mesh Generic Server events.

This function is called whenever a BLE Mesh Generic Server event occurs.

Parameters
[in]eventThe event type for the BLE Mesh Generic Server.
[in]paramParameters associated with the event.

Definition at line 80 of file esp_gen_srv_model.c.

82{
83 ESP_UNUSED(server_state_str);
84 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "%s, op|src|dst:%04" PRIx32 "|%04x|%04x",
85 server_state_str[event], param->ctx.recv_op, param->ctx.addr, param->ctx.recv_dst);
86
87 if (event != ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT)
88 return;
89
90 meshx_gen_srv_cb_param_t pub_param = {
91 .ctx = {
92 .net_idx = param->ctx.net_idx,
93 .app_idx = param->ctx.app_idx,
94 .dst_addr = param->ctx.recv_dst,
95 .src_addr = param->ctx.addr,
96 .opcode = param->ctx.recv_op,
97 .p_ctx = &param->ctx
98 },
99 .model = {
100 .pub_addr = param->model->pub->publish_addr,
101 .model_id = param->model->model_id,
102 .el_id = param->model->element_idx,
103 .p_model = param->model
104 },
105 .state_change = {
106 .onoff_set.onoff = param->value.state_change.onoff_set.onoff
107 }
108 };
109
110 if(pub_param.model.model_id == ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV)
111 {
112 MESHX_GEN_ONOFF_SRV *srv = (MESHX_GEN_ONOFF_SRV *)param->model->user_data;
113 srv->state.onoff = pub_param.state_change.onoff_set.onoff;
114 }
115
118 pub_param.model.model_id,
119 &pub_param,
121 if (err)
122 {
123 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to publish to control task");
124 }
125}
static const char * server_state_str[]
String representation of the server state change events.
struct meshx_gen_srv_cb_param meshx_gen_srv_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.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
#define MESHX_GEN_ONOFF_SRV
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:30
meshx_gen_srv_state_change_t state_change
meshx_state_change_gen_onoff_set_t onoff_set
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_plat_gen_on_off_srv_restore()

meshx_err_t meshx_plat_gen_on_off_srv_restore ( void * p_model,
uint8_t state )

Restores the state of the Generic OnOff Server model.

This function sets the user data of the specified model to the given state. It checks if the model pointer is valid before proceeding with the operation.

Parameters
[in]p_modelPointer to the model structure.
[in]stateThe state to be restored in the model.
Returns
  • MESHX_SUCCESS: State restored successfully.
  • MESHX_INVALID_ARG: Invalid model pointer.

Definition at line 247 of file esp_gen_srv_model.c.

248{
249 return meshx_plat_set_gen_srv_state (p_model, state);
250}
meshx_err_t meshx_plat_set_gen_srv_state(void *p_model, uint8_t on_off_state)
Set the state of a generic server model.
Here is the call graph for this function:

◆ meshx_plat_gen_srv_create()

static meshx_err_t meshx_plat_gen_srv_create ( void * p_model,
void ** p_pub,
void ** p_srv )
static

Creates and initializes the Generic Server model platform resources.

This function sets up the necessary resources for a Generic Server model, including publication and OnOff server instances.

Parameters
[in]p_modelPointer to the model instance to be initialized.
[out]p_pubPointer to the location where the publication context will be stored.
[out]p_srvPointer to the location where the OnOff server instance will be stored.
Returns
meshx_err_t Returns an error code indicating success or failure of the operation.

Definition at line 36 of file esp_gen_srv_model.c.

37{
38 if(!p_model || !p_pub || !p_srv)
39 return MESHX_INVALID_ARG;
40
42
43 err = meshx_plat_create_model_pub(p_pub, 1);
44 if (err)
45 return meshx_plat_del_model_pub(p_pub);
46
48 if(!*p_srv)
49 return MESHX_NO_MEM;
50
51 ((MESHX_GEN_ONOFF_SRV*)*p_srv)->rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP;
52 ((MESHX_GEN_ONOFF_SRV*)*p_srv)->rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP;
53 ((MESHX_MODEL*)p_model)->user_data = *p_srv;
54
55 void **temp = (void**) &((MESHX_MODEL*)p_model)->pub;
56
57 *temp = *p_pub;
58
59 return err;
60}
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.
#define MESHX_CALOC
Definition meshx_err.h:28
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_NO_MEM
Definition meshx_err.h:44
#define MESHX_MODEL
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_plat_gen_srv_delete()

meshx_err_t meshx_plat_gen_srv_delete ( void ** p_pub,
void ** p_srv )

Deletes the Generic OnOff Server model and its associated resources.

This function frees the memory allocated for the Generic OnOff Server and sets the pointer to NULL. It also deletes the model publication resources associated with the server.

Parameters
[in,out]p_pubPointer to the publication structure to be deleted.
[in,out]p_srvPointer to the Generic Server structure to be freed.
Returns
  • MESHX_SUCCESS: Model and publication deleted successfully.
  • MESHX_FAIL: Failed to delete the model or publication.

Definition at line 236 of file esp_gen_srv_model.c.

237{
238 if(p_srv)
239 {
240 MESHX_FREE(*p_srv);
241 *p_srv = NULL;
242 }
243
244 return meshx_plat_del_model_pub(p_pub);
245}
#define MESHX_FREE
Definition meshx_err.h:32
Here is the call graph for this function:

◆ meshx_plat_gen_srv_init()

meshx_err_t meshx_plat_gen_srv_init ( void )

Initialize the meshxuction generic server.

Initialize the generic server model platform.

This function sets up the necessary configurations and initializes the meshxuction generic server for the BLE mesh node.

Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to initialize the server

Definition at line 210 of file esp_gen_srv_model.c.

211{
213
214 /* Register the ESP Generic Server callback */
215 esp_err_t esp_err = esp_ble_mesh_register_generic_server_callback(
217 );
218 if(esp_err != ESP_OK)
219 err = MESHX_ERR_PLAT;
220
221 return err;
222}
static void esp_ble_mesh_generic_server_cb(MESHX_GEN_SRV_CB_EVT event, MESHX_GEN_SRV_CB_PARAM *param)
Callback function for BLE Mesh Generic Server events.
@ MESHX_ERR_PLAT
Definition meshx_err.h:43
#define MESHX_GEN_SRV_CB
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_plat_gen_srv_send_status()

meshx_err_t meshx_plat_gen_srv_send_status ( meshx_model_t * p_model,
meshx_ctx_t * p_ctx,
meshx_ptr_t p_data,
uint32_t data_len )

Send a status message from the Generic Server model.

This function sends a status message to the specified context with the provided data.

Parameters
[in]p_modelPointer to the model instance.
[in]p_ctxPointer to the context structure containing destination address and other parameters.
[in]p_dataPointer to the data to be sent.
[in]data_lenLength of the data to be sent.
Returns
  • MESHX_SUCCESS: Message sent successfully.
  • MESHX_NO_MEM: Memory allocation failed.
  • MESHX_ERR_PLAT: Platform error occurred while sending the message.

Definition at line 142 of file esp_gen_srv_model.c.

148{
149 static esp_ble_mesh_msg_ctx_t ctx;
150 const esp_ble_mesh_msg_ctx_t *pctx = (esp_ble_mesh_msg_ctx_t *)p_ctx->p_ctx;
151 if(pctx != NULL)
152 {
153 memcpy(&ctx, pctx, sizeof(esp_ble_mesh_msg_ctx_t));
154 }
155
156 ctx.net_idx = p_ctx->net_idx;
157 ctx.app_idx = p_ctx->app_idx;
158 ctx.addr = p_ctx->dst_addr;
159 ctx.send_ttl = ESP_BLE_MESH_TTL_DEFAULT;
160 ctx.send_cred = 0;
161 ctx.send_tag = BIT1;
162
163 esp_err_t err = esp_ble_mesh_server_model_send_msg(p_model->p_model,
164 &ctx,
165 p_ctx->opcode,
166 (uint16_t)data_len,
167 (uint8_t*)p_data);
168 if(err)
169 {
170 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Mesh Model msg send failed (err: 0x%x)", err);
171 return MESHX_ERR_PLAT;
172 }
173 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "Mesh Model msg sent (opcode: 0x%04x)", p_ctx->opcode);
174 return MESHX_SUCCESS;
175}
meshx_ptr_t p_ctx
uint16_t dst_addr
meshx_ptr_t p_model
Here is the caller graph for this function:

◆ meshx_plat_on_off_gen_srv_create()

meshx_err_t meshx_plat_on_off_gen_srv_create ( void * p_model,
void ** p_pub,
void ** p_onoff_srv )

Creates a Generic OnOff Server model and its publication context.

This function initializes the Generic OnOff Server model, its publication context, and allocates memory for the server instance. It checks for invalid arguments and handles memory allocation failures.

Parameters
[out]p_modelPointer to the model structure to be created.
[out]p_pubPointer to the publication context to be created.
[out]p_onoff_srvPointer to the OnOff server instance to be allocated.
Returns
  • MESHX_SUCCESS: Successfully created the model and publication context.
  • MESHX_INVALID_ARG: One or more arguments are invalid.
  • MESHX_NO_MEM: Memory allocation failed.

Definition at line 224 of file esp_gen_srv_model.c.

225{
226 if(!p_model || !p_pub || !p_onoff_srv)
227 return MESHX_INVALID_ARG;
228
229 /* SIG ON OFF initialisation */
230 uint16_t model_id = ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV;
231 memcpy((meshx_ptr_t) &(((MESHX_MODEL*)p_model)->model_id), &model_id, sizeof(model_id));
232
233 return meshx_plat_gen_srv_create(p_model, p_pub, p_onoff_srv);
234}
static meshx_err_t meshx_plat_gen_srv_create(void *p_model, void **p_pub, void **p_srv)
Creates and initializes the Generic Server model platform resources.
void * meshx_ptr_t
Here is the call graph for this function:

◆ meshx_plat_set_gen_srv_state()

meshx_err_t meshx_plat_set_gen_srv_state ( void * p_model,
uint8_t on_off_state )

Set the state of a generic server model.

This function updates the on/off state of a specified generic server model.

Parameters
[in]p_modelPointer to the model whose state is to be set.
[in]on_off_stateThe desired on/off state to set for the model.
Returns
MESHX_SUCCESS on success, or an appropriate error code on failure.

Definition at line 187 of file esp_gen_srv_model.c.

188{
189 if(!p_model)
190 return MESHX_INVALID_ARG;
191
192 MESHX_MODEL * model = (MESHX_MODEL *)p_model;
193 MESHX_GEN_ONOFF_SRV *srv = (MESHX_GEN_ONOFF_SRV *)model->user_data;
194
195 srv->state.onoff = on_off_state;
196
197 return MESHX_SUCCESS;
198}
Here is the caller graph for this function:

Variable Documentation

◆ server_state_str

const char* server_state_str[]
static
Initial value:
= {
[ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT] = "SRV_STATE_CH",
[ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT] = "SRV_RECV_GET",
[ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT] = "SRV_RECV_SET"
}

String representation of the server state change events.

Definition at line 65 of file esp_gen_srv_model.c.

65 {
66 [ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT] = "SRV_STATE_CH",
67 [ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT] = "SRV_RECV_GET",
68 [ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT] = "SRV_RECV_SET"
69};