sensor_server.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <errno.h>
  7. #include "btc_ble_mesh_sensor_model.h"
  8. #include "mesh/config.h"
  9. #include "access.h"
  10. #include "transport.h"
  11. #include "mesh/model_opcode.h"
  12. #include "mesh/state_transition.h"
  13. #include "mesh/device_property.h"
  14. #if CONFIG_BLE_MESH_SENSOR_SERVER
  15. static void update_sensor_periodic_pub(struct bt_mesh_model *model, uint16_t prop_id);
  16. /* message handlers (Start) */
  17. /* Sensor Server & Sensor Setup Server message handlers */
  18. static void send_sensor_descriptor_status(struct bt_mesh_model *model,
  19. struct bt_mesh_msg_ctx *ctx,
  20. uint16_t prop_id, bool get_all)
  21. {
  22. struct bt_mesh_sensor_srv *srv = model->user_data;
  23. struct bt_mesh_sensor_state *state = NULL;
  24. struct net_buf_simple *msg = NULL;
  25. uint16_t total_len = 5U;
  26. int i;
  27. msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
  28. if (msg == NULL) {
  29. BT_ERR("%s, Out of memory", __func__);
  30. return;
  31. }
  32. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS);
  33. if (get_all == true) {
  34. for (i = 0; i < srv->state_count; i++) {
  35. state = &srv->states[i];
  36. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) {
  37. total_len += SENSOR_DESCRIPTOR_LEN;
  38. if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
  39. /* Add this in case the message is too long */
  40. BT_WARN("Too large sensor descriptor status");
  41. break;
  42. }
  43. net_buf_simple_add_le16(msg, state->sensor_property_id);
  44. net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) |
  45. (state->descriptor.negative_tolerance << 12) |
  46. (state->descriptor.positive_tolerance));
  47. net_buf_simple_add_u8(msg, state->descriptor.measure_period);
  48. net_buf_simple_add_u8(msg, state->descriptor.update_interval);
  49. }
  50. }
  51. } else {
  52. for (i = 0; i < srv->state_count; i++) {
  53. state = &srv->states[i];
  54. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  55. state->sensor_property_id == prop_id) {
  56. total_len += SENSOR_DESCRIPTOR_LEN;
  57. if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
  58. /* Add this in case the message is too long */
  59. BT_WARN("Too large sensor descriptor status");
  60. break;
  61. }
  62. net_buf_simple_add_le16(msg, state->sensor_property_id);
  63. net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) |
  64. (state->descriptor.negative_tolerance << 12) |
  65. (state->descriptor.positive_tolerance));
  66. net_buf_simple_add_u8(msg, state->descriptor.measure_period);
  67. net_buf_simple_add_u8(msg, state->descriptor.update_interval);
  68. break;
  69. }
  70. }
  71. if (i == srv->state_count) {
  72. BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
  73. net_buf_simple_add_le16(msg, prop_id);
  74. }
  75. }
  76. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  77. bt_mesh_free_buf(msg);
  78. }
  79. static void send_sensor_data_status(struct bt_mesh_model *model,
  80. struct bt_mesh_msg_ctx *ctx,
  81. uint16_t prop_id, bool get_all)
  82. {
  83. struct bt_mesh_sensor_srv *srv = model->user_data;
  84. struct bt_mesh_sensor_state *state = NULL;
  85. struct net_buf_simple *msg = NULL;
  86. uint16_t total_len = 5U;
  87. int i;
  88. msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
  89. if (msg == NULL) {
  90. BT_ERR("%s, Out of memory", __func__);
  91. return;
  92. }
  93. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_STATUS);
  94. if (get_all == true) {
  95. for (i = 0; i < srv->state_count; i++) {
  96. state = &srv->states[i];
  97. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) {
  98. uint8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ?
  99. SENSOR_DATA_FORMAT_A_MPID_LEN : SENSOR_DATA_FORMAT_B_MPID_LEN;
  100. total_len += (mpid_len + (state->sensor_data.raw_value ?
  101. state->sensor_data.raw_value->len : 0));
  102. if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
  103. /* Add this in case the message is too long */
  104. BT_WARN("Too large sensor status");
  105. break;
  106. }
  107. if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) {
  108. uint16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) |
  109. ((state->sensor_data.length & BIT_MASK(4)) << 1) |
  110. state->sensor_data.format;
  111. net_buf_simple_add_le16(msg, mpid);
  112. } else if (state->sensor_data.format == SENSOR_DATA_FORMAT_B) {
  113. uint8_t mpid = (state->sensor_data.length << 1) | state->sensor_data.format;
  114. net_buf_simple_add_u8(msg, mpid);
  115. net_buf_simple_add_le16(msg, state->sensor_property_id);
  116. }
  117. if (state->sensor_data.raw_value) {
  118. net_buf_simple_add_mem(msg, state->sensor_data.raw_value->data, state->sensor_data.raw_value->len);
  119. }
  120. }
  121. }
  122. } else {
  123. for (i = 0; i < srv->state_count; i++) {
  124. state = &srv->states[i];
  125. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  126. state->sensor_property_id == prop_id) {
  127. uint8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ?
  128. SENSOR_DATA_FORMAT_A_MPID_LEN : SENSOR_DATA_FORMAT_B_MPID_LEN;
  129. total_len += (mpid_len + (state->sensor_data.raw_value ?
  130. state->sensor_data.raw_value->len : 0));
  131. if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
  132. /* Add this in case the message is too long */
  133. BT_WARN("Too large sensor status");
  134. break;
  135. }
  136. if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) {
  137. uint16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) |
  138. ((state->sensor_data.length & BIT_MASK(4)) << 1) |
  139. state->sensor_data.format;
  140. net_buf_simple_add_le16(msg, mpid);
  141. } else if (state->sensor_data.format == SENSOR_DATA_FORMAT_B) {
  142. uint8_t mpid = (state->sensor_data.length << 1) | state->sensor_data.format;
  143. net_buf_simple_add_u8(msg, mpid);
  144. net_buf_simple_add_le16(msg, state->sensor_property_id);
  145. }
  146. if (state->sensor_data.raw_value) {
  147. net_buf_simple_add_mem(msg, state->sensor_data.raw_value->data,
  148. state->sensor_data.raw_value->len);
  149. }
  150. break;
  151. }
  152. }
  153. if (i == srv->state_count) {
  154. BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
  155. uint8_t mpid = (SENSOR_DATA_ZERO_LEN << 1) | SENSOR_DATA_FORMAT_B;
  156. net_buf_simple_add_u8(msg, mpid);
  157. net_buf_simple_add_le16(msg, prop_id);
  158. }
  159. }
  160. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  161. bt_mesh_free_buf(msg);
  162. }
  163. static void send_sensor_cadence_status(struct bt_mesh_model *model,
  164. struct bt_mesh_msg_ctx *ctx,
  165. uint16_t prop_id, bool publish)
  166. {
  167. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  168. struct bt_mesh_sensor_state *state = NULL;
  169. struct net_buf_simple *msg = NULL;
  170. uint16_t length = 0U;
  171. int i;
  172. for (i = 0; i < srv->state_count; i++) {
  173. state = &srv->states[i];
  174. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  175. state->sensor_property_id == prop_id && state->cadence) {
  176. length = SENSOR_PROPERTY_ID_LEN + 1 + 1;
  177. if (state->cadence->trigger_delta_down) {
  178. if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
  179. length += state->cadence->trigger_delta_down->len;
  180. } else {
  181. length += SENSOR_STATUS_TRIGGER_UINT16_LEN;
  182. }
  183. }
  184. if (state->cadence->trigger_delta_up) {
  185. if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
  186. length += state->cadence->trigger_delta_up->len;
  187. } else {
  188. length += SENSOR_STATUS_TRIGGER_UINT16_LEN;
  189. }
  190. }
  191. if (state->cadence->fast_cadence_low) {
  192. length += state->cadence->fast_cadence_low->len;
  193. }
  194. if (state->cadence->fast_cadence_high) {
  195. length += state->cadence->fast_cadence_high->len;
  196. }
  197. break;
  198. }
  199. }
  200. if (i == srv->state_count) {
  201. BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
  202. length = SENSOR_PROPERTY_ID_LEN;
  203. }
  204. if (publish == false) {
  205. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  206. if (msg == NULL) {
  207. BT_ERR("%s, Out of memory", __func__);
  208. return;
  209. }
  210. } else {
  211. msg = bt_mesh_server_get_pub_msg(model, 1 + length);
  212. if (msg == NULL) {
  213. return;
  214. }
  215. }
  216. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS);
  217. net_buf_simple_add_le16(msg, prop_id);
  218. if (i != srv->state_count) {
  219. if (state->cadence) {
  220. net_buf_simple_add_u8(msg, (state->cadence->trigger_type << 7) |
  221. state->cadence->period_divisor);
  222. if (state->cadence->trigger_delta_down) {
  223. if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
  224. net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data,
  225. state->cadence->trigger_delta_down->len);
  226. } else {
  227. net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data,
  228. SENSOR_STATUS_TRIGGER_UINT16_LEN);
  229. }
  230. }
  231. if (state->cadence->trigger_delta_up) {
  232. if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
  233. net_buf_simple_add_mem(msg, state->cadence->trigger_delta_up->data,
  234. state->cadence->trigger_delta_up->len);
  235. } else {
  236. net_buf_simple_add_mem(msg, state->cadence->trigger_delta_up->data,
  237. SENSOR_STATUS_TRIGGER_UINT16_LEN);
  238. }
  239. }
  240. net_buf_simple_add_u8(msg, state->cadence->min_interval);
  241. if (state->cadence->fast_cadence_low) {
  242. net_buf_simple_add_mem(msg, state->cadence->fast_cadence_low->data,
  243. state->cadence->fast_cadence_low->len);
  244. }
  245. if (state->cadence->fast_cadence_high) {
  246. net_buf_simple_add_mem(msg, state->cadence->fast_cadence_high->data,
  247. state->cadence->fast_cadence_high->len);
  248. }
  249. }
  250. }
  251. if (publish == false) {
  252. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  253. bt_mesh_free_buf(msg);
  254. } else {
  255. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  256. }
  257. }
  258. static void send_sensor_settings_status(struct bt_mesh_model *model,
  259. struct bt_mesh_msg_ctx *ctx,
  260. uint16_t prop_id)
  261. {
  262. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  263. struct bt_mesh_sensor_state *state = NULL;
  264. struct sensor_setting *item = NULL;
  265. struct net_buf_simple *msg = NULL;
  266. uint16_t total_len = 7U;
  267. int i, j;
  268. msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
  269. if (msg == NULL) {
  270. BT_ERR("%s, Out of memory", __func__);
  271. return;
  272. }
  273. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS);
  274. net_buf_simple_add_le16(msg, prop_id);
  275. for (i = 0; i < srv->state_count; i++) {
  276. state = &srv->states[i];
  277. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  278. state->sensor_property_id == prop_id &&
  279. state->setting_count && state->settings) {
  280. for (j = 0; j < state->setting_count; j++) {
  281. item = &state->settings[j];
  282. if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID) {
  283. total_len += SENSOR_SETTING_PROPERTY_ID_LEN;
  284. if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
  285. /* Add this in case the message is too long */
  286. BT_WARN("Too large sensor settings status");
  287. break;
  288. }
  289. net_buf_simple_add_le16(msg, item->property_id);
  290. }
  291. }
  292. break;
  293. }
  294. }
  295. if (i == srv->state_count) {
  296. BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
  297. }
  298. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  299. bt_mesh_free_buf(msg);
  300. }
  301. static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model,
  302. uint16_t prop_id, uint16_t set_prop_id)
  303. {
  304. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  305. struct bt_mesh_sensor_state *state = NULL;
  306. struct sensor_setting *item = NULL;
  307. int i, j;
  308. for (i = 0; i < srv->state_count; i++) {
  309. state = &srv->states[i];
  310. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  311. state->sensor_property_id == prop_id &&
  312. state->setting_count && state->settings) {
  313. for (j = 0; j < state->setting_count; j++) {
  314. item = &state->settings[j];
  315. if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID &&
  316. item->property_id == set_prop_id) {
  317. return item;
  318. }
  319. }
  320. }
  321. }
  322. return NULL;
  323. }
  324. static void send_sensor_setting_status(struct bt_mesh_model *model,
  325. struct bt_mesh_msg_ctx *ctx, uint16_t prop_id,
  326. uint16_t set_prop_id, bool publish)
  327. {
  328. struct sensor_setting *item = NULL;
  329. struct net_buf_simple *msg = NULL;
  330. uint16_t length = 0U;
  331. item = find_sensor_setting(model, prop_id, set_prop_id);
  332. if (item) {
  333. length = SENSOR_PROPERTY_ID_LEN + SENSOR_SETTING_PROPERTY_ID_LEN +
  334. SENSOR_SETTING_ACCESS_LEN + (item->raw ? item->raw->len : 0);
  335. } else {
  336. /* If the message is sent as a response to the Sensor Setting Get message or
  337. * a Sensor Setting Set message with an unknown Sensor Property ID field or
  338. * an unknown Sensor Setting Property ID field, the Sensor Setting Access
  339. * field and the Sensor Setting Raw field shall be omitted.
  340. */
  341. BT_WARN("Sensor Setting not found, 0x%04x, 0x%04x", prop_id, set_prop_id);
  342. length = SENSOR_PROPERTY_ID_LEN + SENSOR_SETTING_PROPERTY_ID_LEN;
  343. }
  344. if (publish == false) {
  345. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  346. if (msg == NULL) {
  347. BT_ERR("%s, Out of memory", __func__);
  348. return;
  349. }
  350. } else {
  351. msg = bt_mesh_server_get_pub_msg(model, 1 + length);
  352. if (msg == NULL) {
  353. return;
  354. }
  355. }
  356. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS);
  357. net_buf_simple_add_le16(msg, prop_id);
  358. net_buf_simple_add_le16(msg, set_prop_id);
  359. if (item) {
  360. /**
  361. * If the message is sent as a response to the Sensor Setting Set message with
  362. * a Sensor Setting Property ID field that identifies an existing Sensor Setting,
  363. * and the value of the Sensor Setting Access state is 0x01 (can be read), the
  364. * Sensor Setting Property ID field shall be set to the value of the Sensor
  365. * Setting Property ID field of the incoming message, the Sensor Setting Access
  366. * field shall be set to the value of the Sensor Setting Access state field, and
  367. * the Sensor Setting Raw field shall be omitted.
  368. *
  369. * TODO: What if the Sensor Setting Access is Prohibited?
  370. */
  371. net_buf_simple_add_u8(msg, item->access);
  372. if (ctx->recv_op != BLE_MESH_MODEL_OP_SENSOR_SETTING_SET ||
  373. item->access == SENSOR_SETTING_ACCESS_READ_WRITE) {
  374. if (item->raw) {
  375. net_buf_simple_add_mem(msg, item->raw->data, item->raw->len);
  376. }
  377. }
  378. }
  379. if (publish == false) {
  380. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  381. bt_mesh_free_buf(msg);
  382. } else {
  383. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
  384. }
  385. }
  386. static void send_sensor_column_status(struct bt_mesh_model *model,
  387. struct bt_mesh_msg_ctx *ctx,
  388. struct net_buf_simple *buf, uint16_t prop_id)
  389. {
  390. struct bt_mesh_sensor_srv *srv = model->user_data;
  391. struct bt_mesh_sensor_state *state = NULL;
  392. struct net_buf_simple *msg = NULL;
  393. bool optional = false;
  394. uint16_t length = 0U;
  395. int i;
  396. for (i = 0; i < srv->state_count; i++) {
  397. state = &srv->states[i];
  398. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  399. state->sensor_property_id == prop_id) {
  400. length = SENSOR_PROPERTY_ID_LEN;
  401. if (state->series_column.raw_value_x) {
  402. length += state->series_column.raw_value_x->len;
  403. }
  404. /**
  405. * TODO: column width & raw value y in Sensor Column Status are optional,
  406. * here we need to add some conditions to decide whether put these two
  407. * in the status message.
  408. */
  409. if (optional) {
  410. if (state->series_column.column_width) {
  411. length += state->series_column.column_width->len;
  412. }
  413. if (state->series_column.raw_value_y) {
  414. length += state->series_column.raw_value_y->len;
  415. }
  416. }
  417. break;
  418. }
  419. }
  420. if (i == srv->state_count) {
  421. BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
  422. length = SENSOR_PROPERTY_ID_LEN;
  423. }
  424. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  425. if (msg == NULL) {
  426. BT_ERR("%s, Out of memory", __func__);
  427. return;
  428. }
  429. /**
  430. * TODO: Sensor Column Get contains Raw Value X which identifies a column,
  431. * we need to use this value to decide the column.
  432. */
  433. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS);
  434. net_buf_simple_add_le16(msg, prop_id);
  435. if (i != srv->state_count) {
  436. if (state->series_column.raw_value_x) {
  437. net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
  438. state->series_column.raw_value_x->len);
  439. }
  440. if (optional) {
  441. if (state->series_column.column_width) {
  442. net_buf_simple_add_mem(msg, state->series_column.column_width->data,
  443. state->series_column.column_width->len);
  444. }
  445. if (state->series_column.raw_value_y) {
  446. net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
  447. state->series_column.raw_value_y->len);
  448. }
  449. }
  450. }
  451. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  452. bt_mesh_free_buf(msg);
  453. }
  454. static void send_sensor_series_status(struct bt_mesh_model *model,
  455. struct bt_mesh_msg_ctx *ctx,
  456. struct net_buf_simple *buf, uint16_t prop_id)
  457. {
  458. struct bt_mesh_sensor_srv *srv = model->user_data;
  459. struct bt_mesh_sensor_state *state = NULL;
  460. struct net_buf_simple *msg = NULL;
  461. bool optional = false;
  462. uint16_t length = 0U;
  463. int i;
  464. for (i = 0; i < srv->state_count; i++) {
  465. state = &srv->states[i];
  466. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  467. state->sensor_property_id == prop_id) {
  468. length = SENSOR_PROPERTY_ID_LEN;
  469. /* TODO: raw value x, column width & raw value y in Sensor Series
  470. * Status are optional, here we need to add some conditions to
  471. * decide whether put these three in the status message.
  472. */
  473. if (optional) {
  474. if (state->series_column.raw_value_x) {
  475. length += state->series_column.raw_value_x->len;
  476. }
  477. if (state->series_column.column_width) {
  478. length += state->series_column.column_width->len;
  479. }
  480. if (state->series_column.raw_value_y) {
  481. length += state->series_column.raw_value_y->len;
  482. }
  483. }
  484. break;
  485. }
  486. }
  487. if (i == srv->state_count) {
  488. BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
  489. length = SENSOR_PROPERTY_ID_LEN;
  490. }
  491. msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
  492. if (msg == NULL) {
  493. BT_ERR("%s, Out of memory", __func__);
  494. return;
  495. }
  496. /**
  497. * TODO: Sensor Series Get may contain Raw Value X1 and Raw Value X2 which
  498. * identifies a starting column and a ending column, we need to use these
  499. * values to decide the columns.
  500. */
  501. bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS);
  502. net_buf_simple_add_le16(msg, prop_id);
  503. if (i != srv->state_count) {
  504. if (optional) {
  505. if (state->series_column.raw_value_x) {
  506. net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
  507. state->series_column.raw_value_x->len);
  508. }
  509. if (state->series_column.column_width) {
  510. net_buf_simple_add_mem(msg, state->series_column.column_width->data,
  511. state->series_column.column_width->len);
  512. }
  513. if (state->series_column.raw_value_y) {
  514. net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
  515. state->series_column.raw_value_y->len);
  516. }
  517. }
  518. }
  519. BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
  520. bt_mesh_free_buf(msg);
  521. }
  522. static void sensor_get(struct bt_mesh_model *model,
  523. struct bt_mesh_msg_ctx *ctx,
  524. struct net_buf_simple *buf)
  525. {
  526. uint16_t set_prop_id = INVALID_SENSOR_PROPERTY_ID;
  527. uint16_t prop_id = INVALID_SENSOR_PROPERTY_ID;
  528. if (model->user_data == NULL) {
  529. BT_ERR("%s, Invalid model user data", __func__);
  530. return;
  531. }
  532. switch (ctx->recv_op) {
  533. case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET:
  534. case BLE_MESH_MODEL_OP_SENSOR_GET:
  535. case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET:
  536. case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: {
  537. struct bt_mesh_sensor_srv *srv = model->user_data;
  538. if (srv->state_count == 0U || srv->states == NULL) {
  539. BT_ERR("Invalid Sensor Server state");
  540. return;
  541. }
  542. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET ||
  543. ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_GET) {
  544. bool get_all = buf->len ? false : true;
  545. if (buf->len) {
  546. prop_id = net_buf_simple_pull_le16(buf);
  547. if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
  548. BT_ERR("Prohibited Sensor Property ID 0x0000");
  549. return;
  550. }
  551. }
  552. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET) {
  553. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  554. bt_mesh_sensor_server_recv_get_msg_t get = {
  555. .sensor_descriptor_get.op_en = !get_all,
  556. .sensor_descriptor_get.id = prop_id,
  557. };
  558. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  559. model, ctx, (const uint8_t *)&get, sizeof(get));
  560. } else {
  561. send_sensor_descriptor_status(model, ctx, prop_id, get_all);
  562. }
  563. } else {
  564. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  565. bt_mesh_sensor_server_recv_get_msg_t get = {
  566. .sensor_get.op_en = !get_all,
  567. .sensor_get.id = prop_id,
  568. };
  569. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  570. model, ctx, (const uint8_t *)&get, sizeof(get));
  571. } else {
  572. send_sensor_data_status(model, ctx, prop_id, get_all);
  573. }
  574. }
  575. } else {
  576. prop_id = net_buf_simple_pull_le16(buf);
  577. if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
  578. BT_ERR("Prohibited Sensor Property ID 0x0000");
  579. return;
  580. }
  581. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET) {
  582. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  583. bt_mesh_sensor_server_recv_get_msg_t get = {
  584. .sensor_column_get.id = prop_id,
  585. .sensor_column_get.raw_x = buf,
  586. };
  587. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  588. model, ctx, (const uint8_t *)&get, sizeof(get));
  589. } else {
  590. send_sensor_column_status(model, ctx, buf, prop_id);
  591. }
  592. } else {
  593. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  594. bt_mesh_sensor_server_recv_get_msg_t get = {
  595. .sensor_series_get.id = prop_id,
  596. .sensor_series_get.raw = buf,
  597. };
  598. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  599. model, ctx, (const uint8_t *)&get, sizeof(get));
  600. } else {
  601. send_sensor_series_status(model, ctx, buf, prop_id);
  602. }
  603. }
  604. }
  605. return;
  606. }
  607. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET:
  608. case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET:
  609. case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: {
  610. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  611. if (srv->state_count == 0U || srv->states == NULL) {
  612. BT_ERR("Invalid Sensor Setup Server state");
  613. return;
  614. }
  615. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET ||
  616. ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET) {
  617. prop_id = net_buf_simple_pull_le16(buf);
  618. if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
  619. BT_ERR("Prohibited Sensor Property ID 0x0000");
  620. return;
  621. }
  622. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET) {
  623. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  624. bt_mesh_sensor_server_recv_get_msg_t get = {
  625. .sensor_cadence_get.id = prop_id,
  626. };
  627. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  628. model, ctx, (const uint8_t *)&get, sizeof(get));
  629. } else {
  630. send_sensor_cadence_status(model, ctx, prop_id, false);
  631. }
  632. } else {
  633. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  634. bt_mesh_sensor_server_recv_get_msg_t get = {
  635. .sensor_settings_get.id = prop_id,
  636. };
  637. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  638. model, ctx, (const uint8_t *)&get, sizeof(get));
  639. } else {
  640. send_sensor_settings_status(model, ctx, prop_id);
  641. }
  642. }
  643. } else {
  644. prop_id = net_buf_simple_pull_le16(buf);
  645. if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
  646. BT_ERR("Prohibited Sensor Property ID 0x0000");
  647. return;
  648. }
  649. set_prop_id = net_buf_simple_pull_le16(buf);
  650. if (set_prop_id == INVALID_SENSOR_PROPERTY_ID) {
  651. BT_ERR("Prohibited Sensor Setting Property ID 0x0000");
  652. return;
  653. }
  654. if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  655. bt_mesh_sensor_server_recv_get_msg_t get = {
  656. .sensor_setting_get.id = prop_id,
  657. .sensor_setting_get.setting_id = set_prop_id,
  658. };
  659. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG,
  660. model, ctx, (const uint8_t *)&get, sizeof(get));
  661. } else {
  662. send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false);
  663. }
  664. }
  665. return;
  666. }
  667. default:
  668. BT_WARN("Unknown Sensor Get opcode 0x%04x", ctx->recv_op);
  669. return;
  670. }
  671. }
  672. static void sensor_cadence_set(struct bt_mesh_model *model,
  673. struct bt_mesh_msg_ctx *ctx,
  674. struct net_buf_simple *buf)
  675. {
  676. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  677. bt_mesh_sensor_server_state_change_t change = {0};
  678. struct bt_mesh_sensor_state *state = NULL;
  679. struct bt_mesh_model *sensor_model = NULL;
  680. struct bt_mesh_elem *element = NULL;
  681. uint16_t prop_id = 0U, trigger_len = 0U;
  682. uint8_t val = 0U, divisor = 0U;
  683. int i;
  684. if (srv == NULL || srv->state_count == 0U || srv->states == NULL) {
  685. BT_ERR("%s, Invalid model user data", __func__);
  686. return;
  687. }
  688. prop_id = net_buf_simple_pull_le16(buf);
  689. if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
  690. BT_ERR("Prohibited Sensor Property ID 0x0000");
  691. return;
  692. }
  693. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  694. bt_mesh_sensor_server_recv_set_msg_t set = {
  695. .sensor_cadence_set.id = prop_id,
  696. .sensor_cadence_set.cadence = buf,
  697. };
  698. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG,
  699. model, ctx, (const uint8_t *)&set, sizeof(set));
  700. return;
  701. }
  702. for (i = 0; i < srv->state_count; i++) {
  703. state = &srv->states[i];
  704. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  705. state->sensor_property_id == prop_id) {
  706. break;
  707. }
  708. }
  709. if (i == srv->state_count || state->cadence == NULL) {
  710. /* When the message is sent as a response to the Sensor Cadence Get message or
  711. * a Sensor Cadence Set message with an unknown Property ID field or the Sensor
  712. * Server does not support the Sensor Cadence state for the sensor referred by
  713. * the Property ID, the following fields shall be omitted:
  714. * • Fast Cadence Period Divisor
  715. * • Status Trigger Type
  716. * • Status Trigger Delta Down
  717. * • Status Trigger Delta Up
  718. * • Status Min Interval
  719. * • Fast Cadence Low
  720. * • Fast Cadence High
  721. */
  722. send_sensor_cadence_status(model, ctx, prop_id, false);
  723. return;
  724. }
  725. val = net_buf_simple_pull_u8(buf);
  726. divisor = val & BIT_MASK(7);
  727. if (divisor > SENSOR_PERIOD_DIVISOR_MAX_VALUE) {
  728. BT_ERR("Prohibited Fast Cadence Period Divisor 0x%02x", divisor);
  729. return;
  730. }
  731. state->cadence->period_divisor = divisor;
  732. state->cadence->trigger_type = (val >> 7) & BIT_MASK(1);
  733. if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
  734. trigger_len = bt_mesh_get_dev_prop_len(prop_id);
  735. } else {
  736. trigger_len = SENSOR_STATUS_TRIGGER_UINT16_LEN;
  737. }
  738. if (buf->len < (trigger_len << 1) + SENSOR_STATUS_MIN_INTERVAL_LEN) {
  739. BT_ERR("Invalid Sensor Cadence Set length %d, trigger type %d",
  740. buf->len + 3, state->cadence->trigger_type);
  741. return;
  742. }
  743. if (state->cadence->trigger_delta_down) {
  744. net_buf_simple_reset(state->cadence->trigger_delta_down);
  745. net_buf_simple_add_mem(state->cadence->trigger_delta_down, buf->data, trigger_len);
  746. net_buf_simple_pull_mem(buf, trigger_len);
  747. }
  748. if (state->cadence->trigger_delta_up) {
  749. net_buf_simple_reset(state->cadence->trigger_delta_up);
  750. net_buf_simple_add_mem(state->cadence->trigger_delta_up, buf->data, trigger_len);
  751. net_buf_simple_pull_mem(buf, trigger_len);
  752. }
  753. /* The valid range for the Status Min Interval is 0–26 and other values are Prohibited. */
  754. val = net_buf_simple_pull_u8(buf);
  755. if (val > SENSOR_STATUS_MIN_INTERVAL_MAX) {
  756. BT_ERR("Invalid Status Min Interval %d", val);
  757. return;
  758. }
  759. state->cadence->min_interval = val;
  760. if (buf->len % 2) {
  761. BT_ERR("Different length of Fast Cadence Low & High, length %d", buf->len);
  762. return;
  763. }
  764. if (buf->len) {
  765. uint8_t range_len = buf->len / 2;
  766. if (state->cadence->fast_cadence_low) {
  767. net_buf_simple_reset(state->cadence->fast_cadence_low);
  768. net_buf_simple_add_mem(state->cadence->fast_cadence_low, buf->data, range_len);
  769. net_buf_simple_pull_mem(buf, range_len);
  770. }
  771. if (state->cadence->fast_cadence_high) {
  772. net_buf_simple_reset(state->cadence->fast_cadence_high);
  773. net_buf_simple_add_mem(state->cadence->fast_cadence_high, buf->data, range_len);
  774. net_buf_simple_pull_mem(buf, range_len);
  775. }
  776. }
  777. change.sensor_cadence_set.id = prop_id;
  778. change.sensor_cadence_set.period_divisor = state->cadence->period_divisor;
  779. change.sensor_cadence_set.trigger_type = state->cadence->trigger_type;
  780. change.sensor_cadence_set.trigger_delta_down = state->cadence->trigger_delta_down;
  781. change.sensor_cadence_set.trigger_delta_up = state->cadence->trigger_delta_up;
  782. change.sensor_cadence_set.min_interval = state->cadence->min_interval;
  783. change.sensor_cadence_set.fast_cadence_low = state->cadence->fast_cadence_low;
  784. change.sensor_cadence_set.fast_cadence_high = state->cadence->fast_cadence_high;
  785. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE,
  786. model, ctx, (const uint8_t *)&change, sizeof(change));
  787. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET) {
  788. send_sensor_cadence_status(model, ctx, prop_id, false);
  789. }
  790. send_sensor_cadence_status(model, ctx, prop_id, true);
  791. /* Try to find the corresponding Sensor Server Model */
  792. element = bt_mesh_model_elem(model);
  793. sensor_model = bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SENSOR_SRV);
  794. if (sensor_model == NULL) {
  795. BT_WARN("Sensor Server model not exists in the element");
  796. return;
  797. }
  798. /**
  799. * Based on the configured Sensor Cadence state, change Periodic Sensor
  800. * status publication mechanism.
  801. */
  802. update_sensor_periodic_pub(sensor_model, prop_id);
  803. }
  804. static void update_sensor_periodic_pub(struct bt_mesh_model *model, uint16_t prop_id)
  805. {
  806. struct bt_mesh_sensor_state *state = NULL;
  807. struct bt_mesh_sensor_srv *srv = NULL;
  808. int i;
  809. if (model->id != BLE_MESH_MODEL_ID_SENSOR_SRV) {
  810. BT_ERR("Invalid Sensor Server model 0x%04x", model->id);
  811. return;
  812. }
  813. srv = (struct bt_mesh_sensor_srv *)model->user_data;
  814. if (srv == NULL || srv->state_count == 0U || srv->states == NULL) {
  815. BT_ERR("%s, Invalid model user data", __func__);
  816. return;
  817. }
  818. for (i = 0; i < srv->state_count; i++) {
  819. state = &srv->states[i];
  820. if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
  821. state->sensor_property_id == prop_id) {
  822. break;
  823. }
  824. }
  825. if (i == srv->state_count) {
  826. BT_ERR("Sensor Property ID 0x%04x not exists", prop_id);
  827. return;
  828. }
  829. if (state->cadence == NULL) {
  830. BT_WARN("Sensor Cadence state not exists");
  831. return;
  832. }
  833. /**
  834. * Currently when the device receives a Sensor Cadence Set message,
  835. * a event will be callback to the application layer, and users can
  836. * change the Sensor Data publication period in the event. And this
  837. * is exactly what we do for the BQB test.
  838. */
  839. }
  840. static void sensor_setting_set(struct bt_mesh_model *model,
  841. struct bt_mesh_msg_ctx *ctx,
  842. struct net_buf_simple *buf)
  843. {
  844. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  845. bt_mesh_sensor_server_state_change_t change = {0};
  846. struct sensor_setting *item = NULL;
  847. uint16_t prop_id = 0U, set_prop_id = 0U;
  848. if (srv == NULL || srv->state_count == 0U || srv->states == NULL) {
  849. BT_ERR("%s, Invalid model user data", __func__);
  850. return;
  851. }
  852. prop_id = net_buf_simple_pull_le16(buf);
  853. if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
  854. BT_ERR("Prohibited Sensor Property ID 0x0000");
  855. return;
  856. }
  857. set_prop_id = net_buf_simple_pull_le16(buf);
  858. if (set_prop_id == INVALID_SENSOR_PROPERTY_ID) {
  859. BT_ERR("Prohibited Sensor Setting Property ID 0x0000");
  860. return;
  861. }
  862. if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
  863. bt_mesh_sensor_server_recv_set_msg_t set = {
  864. .sensor_setting_set.id = prop_id,
  865. .sensor_setting_set.setting_id = set_prop_id,
  866. .sensor_setting_set.raw = buf,
  867. };
  868. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG,
  869. model, ctx, (const uint8_t *)&set, sizeof(set));
  870. return;
  871. }
  872. item = find_sensor_setting(model, prop_id, set_prop_id);
  873. if (item) {
  874. if (item->access == SENSOR_SETTING_ACCESS_READ_WRITE && item->raw) {
  875. net_buf_simple_reset(item->raw);
  876. net_buf_simple_add_mem(item->raw, buf->data,
  877. MIN(buf->len, item->raw->size));
  878. change.sensor_setting_set.id = prop_id;
  879. change.sensor_setting_set.setting_id = set_prop_id;
  880. change.sensor_setting_set.value = item->raw;
  881. bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE,
  882. model, ctx, (const uint8_t *)&change, sizeof(change));
  883. }
  884. }
  885. if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_SETTING_SET) {
  886. send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false);
  887. }
  888. if (item) {
  889. send_sensor_setting_status(model, ctx, prop_id, set_prop_id, true);
  890. }
  891. }
  892. /* message handlers (End) */
  893. /* Mapping of message handlers for Sensor Server (0x1100) */
  894. const struct bt_mesh_model_op bt_mesh_sensor_srv_op[] = {
  895. { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET, 0, sensor_get },
  896. { BLE_MESH_MODEL_OP_SENSOR_GET, 0, sensor_get },
  897. { BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET, 2, sensor_get },
  898. { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET, 2, sensor_get },
  899. BLE_MESH_MODEL_OP_END,
  900. };
  901. /* Mapping of message handlers for Sensor Setup Server (0x1101) */
  902. const struct bt_mesh_model_op bt_mesh_sensor_setup_srv_op[] = {
  903. { BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET, 2, sensor_get },
  904. { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET, 4, sensor_cadence_set },
  905. { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK, 4, sensor_cadence_set },
  906. { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET, 2, sensor_get },
  907. { BLE_MESH_MODEL_OP_SENSOR_SETTING_GET, 4, sensor_get },
  908. { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET, 4, sensor_setting_set },
  909. { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK, 4, sensor_setting_set },
  910. BLE_MESH_MODEL_OP_END,
  911. };
  912. static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start,
  913. const uint8_t state_count)
  914. {
  915. struct bt_mesh_sensor_state *state = NULL;
  916. struct sensor_setting *setting = NULL;
  917. int i, j, k;
  918. for (i = 0; i < state_count; i++) {
  919. state = &state_start[i];
  920. if (state->sensor_property_id == INVALID_SENSOR_PROPERTY_ID) {
  921. BT_ERR("Invalid Sensor Property ID 0x%04x", state->sensor_property_id);
  922. return -EINVAL;
  923. }
  924. /* Check if the same Sensor Property ID exists */
  925. for (k = i + 1; k < state_count; k++) {
  926. if (state->sensor_property_id == state_start[k].sensor_property_id) {
  927. BT_ERR("Same Sensor Property ID 0x%04x exists", state->sensor_property_id);
  928. return -EINVAL;
  929. }
  930. }
  931. if (state->setting_count && state->settings) {
  932. for (j = 0; j < state->setting_count; j++) {
  933. setting = &state->settings[j];
  934. if (setting->property_id == INVALID_SENSOR_SETTING_PROPERTY_ID || setting->raw == NULL) {
  935. BT_ERR("Invalid Sensor Setting state");
  936. return -EINVAL;
  937. }
  938. /* Check if the same Sensor Setting Property ID exists */
  939. for (k = j + 1; k < state->setting_count; k++) {
  940. if (setting->property_id == state->settings[k].property_id) {
  941. BT_ERR("Same Sensor Setting Property ID 0x%04x exists", setting->property_id);
  942. return -EINVAL;
  943. }
  944. }
  945. }
  946. }
  947. if (state->cadence) {
  948. if (state->cadence->trigger_delta_down == NULL ||
  949. state->cadence->trigger_delta_up == NULL ||
  950. state->cadence->fast_cadence_low == NULL ||
  951. state->cadence->fast_cadence_high == NULL) {
  952. BT_ERR("Invalid Sensor Cadence state");
  953. return -EINVAL;
  954. }
  955. }
  956. if (state->sensor_data.raw_value == NULL) {
  957. BT_ERR("Invalid Sensor Data state");
  958. return -EINVAL;
  959. }
  960. }
  961. return 0;
  962. }
  963. static int sensor_server_init(struct bt_mesh_model *model)
  964. {
  965. if (model->user_data == NULL) {
  966. BT_ERR("Invalid Sensor Server user data, model id 0x%04x", model->id);
  967. return -EINVAL;
  968. }
  969. switch (model->id) {
  970. case BLE_MESH_MODEL_ID_SENSOR_SRV: {
  971. struct bt_mesh_sensor_srv *srv = model->user_data;
  972. if (srv->state_count == 0U || srv->states == NULL) {
  973. BT_ERR("Invalid Sensor state, model id 0x%04x", model->id);
  974. return -EINVAL;
  975. }
  976. if (check_sensor_server_init(srv->states, srv->state_count)) {
  977. return -EINVAL;
  978. }
  979. srv->model = model;
  980. break;
  981. }
  982. case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV: {
  983. struct bt_mesh_sensor_setup_srv *srv = model->user_data;
  984. if (srv->state_count == 0U || srv->states == NULL) {
  985. BT_ERR("Invalid Sensor state, model id 0x%04x", model->id);
  986. return -EINVAL;
  987. }
  988. if (check_sensor_server_init(srv->states, srv->state_count)) {
  989. return -EINVAL;
  990. }
  991. srv->model = model;
  992. break;
  993. }
  994. default:
  995. BT_WARN("Unknown Sensor Server, model id 0x%04x", model->id);
  996. return -EINVAL;
  997. }
  998. return 0;
  999. }
  1000. static int sensor_srv_init(struct bt_mesh_model *model)
  1001. {
  1002. if (model->pub == NULL) {
  1003. BT_ERR("Sensor Server has no publication support");
  1004. return -EINVAL;
  1005. }
  1006. /* When this model is present on an element, the corresponding Sensor Setup
  1007. * Server model shall also be present.
  1008. */
  1009. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  1010. if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV) == NULL) {
  1011. BT_WARN("Sensor Setup Server not present");
  1012. /* Just give a warning here, continue with the initialization */
  1013. }
  1014. return sensor_server_init(model);
  1015. }
  1016. static int sensor_setup_srv_init(struct bt_mesh_model *model)
  1017. {
  1018. if (model->pub == NULL) {
  1019. BT_ERR("Sensor Setup Server has no publication support");
  1020. return -EINVAL;
  1021. }
  1022. return sensor_server_init(model);
  1023. }
  1024. #if CONFIG_BLE_MESH_DEINIT
  1025. static int sensor_server_deinit(struct bt_mesh_model *model)
  1026. {
  1027. if (model->user_data == NULL) {
  1028. BT_ERR("Invalid Sensor Server user, model id 0x%04x", model->id);
  1029. return -EINVAL;
  1030. }
  1031. return 0;
  1032. }
  1033. static int sensor_srv_deinit(struct bt_mesh_model *model)
  1034. {
  1035. if (model->pub == NULL) {
  1036. BT_ERR("Sensor Server has no publication support");
  1037. return -EINVAL;
  1038. }
  1039. return sensor_server_deinit(model);
  1040. }
  1041. static int sensor_setup_srv_deinit(struct bt_mesh_model *model)
  1042. {
  1043. if (model->pub == NULL) {
  1044. BT_ERR("Sensor Setup Server has no publication support");
  1045. return -EINVAL;
  1046. }
  1047. return sensor_server_deinit(model);
  1048. }
  1049. #endif /* CONFIG_BLE_MESH_DEINIT */
  1050. const struct bt_mesh_model_cb bt_mesh_sensor_srv_cb = {
  1051. .init = sensor_srv_init,
  1052. #if CONFIG_BLE_MESH_DEINIT
  1053. .deinit = sensor_srv_deinit,
  1054. #endif /* CONFIG_BLE_MESH_DEINIT */
  1055. };
  1056. const struct bt_mesh_model_cb bt_mesh_sensor_setup_srv_cb = {
  1057. .init = sensor_setup_srv_init,
  1058. #if CONFIG_BLE_MESH_DEINIT
  1059. .deinit = sensor_setup_srv_deinit,
  1060. #endif /* CONFIG_BLE_MESH_DEINIT */
  1061. };
  1062. #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */