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_cli_model.c
Go to the documentation of this file.
1
18/*****************************************************************************************************************
19 * INCLUDES
20 ****************************************************************************************************************/
21#include "meshx_control_task.h"
23
24/*****************************************************************************************************************
25 * DEFINES
26 ****************************************************************************************************************/
27#define MESHX_CLIENT_INIT_MAGIC_NO 0x8709 // Magic number to indicate initialization state
28
29/*****************************************************************************************************************
30 * STATIC VARIABLES
31 ****************************************************************************************************************/
32static uint16_t meshx_client_init = 0; // Magic number to indicate initialization state
33
37static const char *client_state_str[] =
38{
39 [ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT] = "PUBLISH_EVT",
40 [ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT] = "TIMEOUT_EVT",
41 [ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT] = "GET_STATE_EVT",
42 [ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT] = "SET_STATE_EVT",
43};
44
45/*****************************************************************************************************************
46 * STATIC FUNCTION PROTOTYPES
47 ****************************************************************************************************************/
49/*****************************************************************************************************************
50 * STATIC FUNCTION DEFINITIONS
51 ****************************************************************************************************************/
52
70
73{
75 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "%s, err|op|src|dst: %d|%04" PRIx32 "|%04x|%04x",
76 client_state_str[event], param->error_code,
77 param->params->ctx.recv_op, param->params->ctx.addr, param->params->ctx.recv_dst);
78
79 meshx_gen_cli_cb_param_t pub_param = {
80 .ctx = {
81 .net_idx = param->params->ctx.net_idx,
82 .app_idx = param->params->ctx.app_idx,
83 .dst_addr = param->params->ctx.recv_dst,
84 .src_addr = param->params->ctx.addr,
85 .opcode = param->params->ctx.recv_op,
86 .p_ctx = &param->params->ctx
87 },
88 .model = {
89 .pub_addr = param->params->model->pub->publish_addr,
90 .model_id = param->params->model->model_id,
91 .el_id = param->params->model->element_idx,
92 .p_model = param->params->model
93 },
94 .evt = MESHX_BIT(event),
95 .err_code = param->error_code,
96 };
97
98 /* Copy the msg data from BLE Layer to MeshX Layer */
99 memcpy(&pub_param.status, &param->status_cb, sizeof(meshx_gen_client_status_cb_t));
100
103 pub_param.model.model_id,
104 &pub_param,
106 if (err)
107 {
108 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Failed to publish to control task");
109 }
110}
111
112/*****************************************************************************************************************
113 * INTERFACE FUNCTION DEFINITIONS
114 ****************************************************************************************************************/
126{
127
130 {
131 return MESHX_SUCCESS; // Already initialized
132 }
134
135 // Register the callback for handling messages sent to the BLE layer
136 esp_err_t esp_err = esp_ble_mesh_register_generic_client_callback(
138 if (esp_err != ESP_OK)
139 err = MESHX_ERR_PLAT;
140
141 return err;
142}
143
161{
162 if (!p_model || !p_pub || !p_onoff_cli)
163 {
164 return MESHX_INVALID_ARG; // Invalid arguments
165 }
166 /* SIG On OFF Init */
167 uint16_t model_id = ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI;
168 memcpy((meshx_ptr_t)&(((MESHX_MODEL *)p_model)->model_id), &model_id, sizeof(model_id));
169
170 return meshx_plat_client_create(p_model, p_pub, p_onoff_cli);
171}
172
188{
189 if (p_cli)
190 {
191 MESHX_FREE(*p_cli);
192 *p_cli = NULL;
193 }
194
195 return meshx_plat_del_model_pub(p_pub);
196}
197
215 meshx_ptr_t p_model, meshx_gen_cli_set_t *p_set,
216 uint16_t opcode, uint16_t addr,
217 uint16_t net_idx, uint16_t app_idx,
218 bool is_get_opcode
219)
220{
221 if (!p_model || !p_set)
222 {
223 return MESHX_INVALID_ARG; // Invalid arguments
224 }
225
226 esp_ble_mesh_client_common_param_t common = {0};
227 common.model = p_model;
228 common.opcode = opcode;
229 common.ctx.addr = addr;
230 common.ctx.net_idx = net_idx;
231 common.ctx.app_idx = app_idx;
232 common.ctx.send_ttl = BLE_MESH_TTL_DEFAULT;
233 common.msg_timeout = 0; /* 0 indicates that timeout value from menuconfig will be used */
234
235 esp_err_t err = ESP_OK;
236 if(!is_get_opcode)
237 {
238 p_set->onoff_set.trans_time = 0;
239 if(!(p_set->onoff_set.onoff == 0 || p_set->onoff_set.onoff == 1))
240 {
241 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid OnOff value %d", p_set->onoff_set.onoff);
242 return MESHX_INVALID_ARG;
243 }
244 err = esp_ble_mesh_generic_client_set_state(
245 &common,
246 (esp_ble_mesh_generic_client_set_state_t *)p_set
247 );
248 if (err != ESP_OK)
249 {
250 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Send Generic OnOff failed: %d", err);
251 return MESHX_FAIL;
252 }
253 }
254 else
255 {
256 err = esp_ble_mesh_generic_client_get_state(
257 &common,
258 (esp_ble_mesh_generic_client_get_state_t *)p_set
259 );
260 if (err != ESP_OK)
261 {
262 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Send Generic OnOff Get failed: %d", err);
263 return MESHX_FAIL;
264 }
265 }
266 if (!err)
267 {
268 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "Client Send Success");
269 }
270
271 return MESHX_SUCCESS;
272}
static void esp_ble_mesh_generic_client_cb(MESHX_GEN_CLI_CB_EVT event, MESHX_GEN_CLI_CB_PARAM *param)
Callback function for handling BLE Mesh Generic Client events.
meshx_err_t meshx_plat_gen_cli_delete(meshx_ptr_t *p_pub, meshx_ptr_t *p_cli)
Deletes the Generic OnOff Client model and its associated resources.
meshx_err_t meshx_plat_gen_cli_send_msg(meshx_ptr_t p_model, meshx_gen_cli_set_t *p_set, uint16_t opcode, uint16_t addr, uint16_t net_idx, uint16_t app_idx, bool is_get_opcode)
Sends a Generic Client message over BLE Mesh.
meshx_err_t meshx_plat_on_off_gen_cli_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_onoff_cli)
Creates a Generic OnOff client model and its publication context.
meshx_err_t meshx_plat_gen_cli_init(void)
Initialize the meshxuction generic client.
static const char * client_state_str[]
Mapping of BLE Mesh client state events to string representations.
meshx_err_t meshx_plat_del_model_pub(meshx_ptr_t *p_pub)
Deletes the model and publication objects.
meshx_err_t meshx_plat_client_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_cli)
Creates and initializes a generic client model for BLE Mesh.
void * meshx_ptr_t
#define MESHX_BIT(nr)
Header file for BLE Mesh Generic Client functionality in the MeshX framework. This file contains decl...
struct meshx_gen_cli_cb_param meshx_gen_cli_cb_param_t
Callback parameters for Generic Client Model events. This structure is used to pass information about...
Header file for the control task in the BLE mesh node application.
@ 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_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_FAIL
Definition meshx_err.h:41
@ MESHX_ERR_PLAT
Definition meshx_err.h:43
#define MESHX_FREE
Definition meshx_err.h:32
#define MESHX_CLIENT_INIT_MAGIC_NO
uint16_t meshx_client_init
#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_CLI_CB_EVT
#define MESHX_GEN_CLI_CB
#define MESHX_GEN_CLI_CB_PARAM
#define MESHX_MODEL
@ MODULE_ID_MODEL_CLIENT
Definition module_id.h:31
meshx_gen_client_status_cb_t status
Generic Client Model set message union.
meshx_gen_onoff_set_t onoff_set
Generic Client Model received message union.