1
6
7
15
16
17
18
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
56
57
58
59
60
61
62
63
64
65
69
70
74
75
76
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
102
111
112
113
114
115
116
117
118
119
120
121
122
126
127
131
132
133
152
153
154
155
159
160
161
162
163
164
165
179
180
181
190
191
192
193
194
195
196
197
200
201
202
205
206
207
208
209
210
211
212
213
216
217
218
219
220
221
222
223
224
225
226
227
231
232
233
237
238
239
244
245
246
247
248
/* ... */
#include <errno.h>
#include "mesh.h"
#include "mesh/config.h"
#include "access.h"
#include "mesh/common.h"
#include "mesh/generic_server.h"
#include "mesh/lighting_server.h"7 includes
#if CONFIG_BLE_MESH_SERVER_MODEL
/* ... */
#define INSTANTANEOUS_TRANS_TIME 0
uint8_t bt_mesh_get_default_trans_time(struct bt_mesh_model *model)
{
/* ... */
struct bt_mesh_elem *element = bt_mesh_model_elem(model);
struct bt_mesh_gen_def_trans_time_srv *state = NULL;
uint16_t primary_addr = bt_mesh_primary_addr();
struct bt_mesh_model *srv = NULL;
for (uint16_t addr = element->addr; addr >= primary_addr; addr--) {
element = bt_mesh_elem_find(addr);
if (element) {
srv = bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV);
if (srv) {
state = (struct bt_mesh_gen_def_trans_time_srv *)srv->user_data;
if (state) {
return state->state.trans_time;
}{...}
}{...}
}{...}
}{...}
return INSTANTANEOUS_TRANS_TIME;
}{ ... }
int bt_mesh_get_light_lc_trans_time(struct bt_mesh_model *model, uint8_t *trans_time)
{
struct bt_mesh_light_lc_srv *srv = NULL;
uint32_t value = 0U;
if (model == NULL || trans_time == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
if (model->id != BLE_MESH_MODEL_ID_LIGHT_LC_SRV) {
BT_ERR("Invalid a Light LC Server 0x%04x", model->id);
return -EINVAL;
}{...}
srv = (struct bt_mesh_light_lc_srv *)model->user_data;
if (srv == NULL) {
BT_ERR("Invalid Light LC Server user data");
return -EINVAL;
}{...}
/* ... */
value = srv->lc->prop_state.time_run_on & 0xFFFFFF;
/* ... */
if (value <= 6200) {
*trans_time = (0 << 6) | (value / 100);
}{...} else if (value <= 62000) {
*trans_time = (1 << 6) | (value / 1000);
}{...} else if (value <= 620000) {
*trans_time = (2 << 6) | (value / 10000);
}{...} else {
*trans_time = (3 << 6) | (value / 600000);
}{...}
return 0;
}{ ... }
int bt_mesh_server_get_optional(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf,
uint8_t *trans_time, uint8_t *delay,
bool *optional)
{
if (model == NULL || buf == NULL || trans_time == NULL ||
delay == NULL || optional == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
if (buf->len != 0x00 && buf->len != 0x02) {
BT_ERR("Invalid optional message length %d", buf->len);
return -EINVAL;
}{...}
if (buf->len == 0x00) {
if (model->id == BLE_MESH_MODEL_ID_LIGHT_LC_SRV) {
/* ... */
if (bt_mesh_get_light_lc_trans_time(model, trans_time)) {
BT_ERR("Failed to get Light LC transition time");
return -EIO;
}{...}
}{...} else {
*trans_time = bt_mesh_get_default_trans_time(model);
}{...}
*delay = 0U;
*optional = false;
return 0;
}{...}
*trans_time = net_buf_simple_pull_u8(buf);
if ((*trans_time & 0x3F) == 0x3F) {
BT_ERR("Invalid Transaction Number of Steps 0x3f");
return -EINVAL;
}{...}
*delay = net_buf_simple_pull_u8(buf);
*optional = true;
return 0;
}{ ... }
void bt_mesh_server_alloc_ctx(struct k_work *work)
{
/* ... */
__ASSERT(work, "Invalid parameter");
if (!work->user_data) {
work->user_data = bt_mesh_calloc(sizeof(struct bt_mesh_msg_ctx));
__ASSERT(work->user_data, "Out of memory");
}{...}
}{ ... }
#if CONFIG_BLE_MESH_DEINIT
void bt_mesh_server_free_ctx(struct k_work *work)
{
__ASSERT(work, "Invalid parameter");
if (work->user_data) {
bt_mesh_free(work->user_data);
work->user_data = NULL;
}{...}
}{ ... }
/* ... */#endif
bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last,
uint8_t tid, uint16_t src, uint16_t dst, int64_t *now)
{
*now = k_uptime_get();
if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
return false;
}{...}
if (last->tid == tid && last->src == src && last->dst == dst &&
(*now - last->timestamp <= K_SECONDS(6))) {
return true;
}{...}
return false;
}{ ... }
void bt_mesh_server_update_last_msg(struct bt_mesh_last_msg_info *last,
uint8_t tid, uint16_t src, uint16_t dst, int64_t *now)
{
if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
return;
}{...}
last->tid = tid;
last->src = src;
last->dst = dst;
last->timestamp = *now;
}{ ... }
struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, uint16_t msg_len)
{
struct net_buf_simple *buf = NULL;
if (model == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return NULL;
}{...}
if (model->pub == NULL || model->pub->msg == NULL ||
model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) {
BT_DBG("No publication support, model id 0x%04x", model->id);
return NULL;
}{...}
buf = model->pub->msg;
if (buf->size < msg_len) {
BT_ERR("Too small publication msg size %d, model id 0x%04x",
buf->size, model->id);
return NULL;
}{...}
return buf;
}{ ... }
/* ... */#endif