generic_client.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <errno.h>
  8. #include "btc_ble_mesh_generic_model.h"
  9. #include "mesh_config.h"
  10. #include "model_opcode.h"
  11. #if CONFIG_BLE_MESH_GENERIC_CLIENT
  12. #include "generic_client.h"
  13. /* The followings are the macro definitions of Generic client
  14. * model message length, and a message is composed of 3 parts:
  15. * Opcode + Payload + MIC
  16. */
  17. /* Generic onoff client messages length */
  18. #define BLE_MESH_GEN_ONOFF_GET_MSG_LEN (2 + 0 + 4)
  19. #define BLE_MESH_GEN_ONOFF_SET_MSG_LEN (2 + 4 + 4)
  20. /* Generic level client messages length */
  21. #define BLE_MESH_GEN_LEVEL_GET_MSG_LEN (2 + 0 + 4)
  22. #define BLE_MESH_GEN_LEVEL_SET_MSG_LEN (2 + 5 + 4)
  23. #define BLE_MESH_GEN_DELTA_SET_MSG_LEN (2 + 7 + 4)
  24. #define BLE_MESH_GEN_MOVE_SET_MSG_LEN (2 + 5 + 4)
  25. /* Generic default transition time client messages length */
  26. #define BLE_MESH_GEN_DEF_TRANS_TIME_GET_MSG_LEN (2 + 0 + 4)
  27. #define BLE_MESH_GEN_DEF_TRANS_TIME_SET_MSG_LEN (2 + 1 + 4)
  28. /* Generic power onoff client messages length */
  29. #define BLE_MESH_GEN_ONPOWERUP_GET_MSG_LEN (2 + 0 + 4)
  30. #define BLE_MESH_GEN_ONPOWERUP_SET_MSG_LEN (2 + 1 + 4)
  31. /* Generic power level client messages length */
  32. #define BLE_MESH_GEN_POWER_LEVEL_GET_MSG_LEN (2 + 0 + 4)
  33. #define BLE_MESH_GEN_POWER_LEVEL_SET_MSG_LEN (2 + 5 + 4)
  34. #define BLE_MESH_GEN_POWER_LAST_GET_MSG_LEN (2 + 0 + 4)
  35. #define BLE_MESH_GEN_POWER_DEFAULT_GET_MSG_LEN (2 + 0 + 4)
  36. #define BLE_MESH_GEN_POWER_DEFAULT_SET_MSG_LEN (2 + 2 + 4)
  37. #define BLE_MESH_GEN_POWER_RANGE_GET_MSG_LEN (2 + 0 + 4)
  38. #define BLE_MESH_GEN_POWER_RANGE_SET_MSG_LEN (2 + 4 + 4)
  39. /* Generic battery client messages length */
  40. #define BLE_MESH_GEN_BATTERY_GET_MSG_LEN (2 + 0 + 4)
  41. /* Generic location client messages length */
  42. #define BLE_MESH_GEN_LOC_GLOBAL_GET_MSG_LEN (2 + 0 + 4)
  43. #define BLE_MESH_GEN_LOC_GLOBAL_SET_MSG_LEN (1 + 10 + 4)
  44. #define BLE_MESH_GEN_LOC_LOCAL_GET_MSG_LEN (2 + 0 + 4)
  45. #define BLE_MESH_GEN_LOC_LOCAL_SET_MSG_LEN (2 + 9 + 4)
  46. /* Generic property client messages length */
  47. #define BLE_MESH_GEN_USER_PROPERTIES_GET_MSG_LEN (2 + 0 + 4)
  48. #define BLE_MESH_GEN_USER_PROPERTY_GET_MSG_LEN (2 + 2 + 4)
  49. #define BLE_MESH_GEN_USER_PROPERTY_SET_MSG_LEN /* variable */
  50. #define BLE_MESH_GEN_ADMIN_PROPERTIES_GET_MSG_LEN (2 + 0 + 4)
  51. #define BLE_MESH_GEN_ADMIN_PROPERTY_GET_MSG_LEN (2 + 2 + 4)
  52. #define BLE_MESH_GEN_ADMIN_PROPERTY_SET_MSG_LEN /* variable */
  53. #define BLE_MESH_GEN_MANU_PROPERTIES_GET_MSG_LEN (2 + 0 + 4)
  54. #define BLE_MESH_GEN_MANU_PROPERTY_GET_MSG_LEN (2 + 2 + 4)
  55. #define BLE_MESH_GEN_MANU_PROPERTY_SET_MSG_LEN (1 + 3 + 4)
  56. #define BLE_MESH_GEN_CLINET_PROPERTIES_GET_MSG_LEN (1 + 2 + 4)
  57. #define BLE_MESH_GEN_GET_STATE_MSG_LEN (2 + 2 + 4)
  58. static const bt_mesh_client_op_pair_t gen_op_pair[] = {
  59. { BLE_MESH_MODEL_OP_GEN_ONOFF_GET, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS },
  60. { BLE_MESH_MODEL_OP_GEN_ONOFF_SET, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS },
  61. { BLE_MESH_MODEL_OP_GEN_LEVEL_GET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS },
  62. { BLE_MESH_MODEL_OP_GEN_LEVEL_SET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS },
  63. { BLE_MESH_MODEL_OP_GEN_DELTA_SET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS },
  64. { BLE_MESH_MODEL_OP_GEN_MOVE_SET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS },
  65. { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS },
  66. { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS },
  67. { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS },
  68. { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS },
  69. { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS },
  70. { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS },
  71. { BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET, BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS },
  72. { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS },
  73. { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS },
  74. { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS },
  75. { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS },
  76. { BLE_MESH_MODEL_OP_GEN_BATTERY_GET, BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS },
  77. { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET, BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS },
  78. { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET, BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS },
  79. { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET, BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS },
  80. { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET, BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS },
  81. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS },
  82. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS },
  83. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS },
  84. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS },
  85. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS },
  86. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS },
  87. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS },
  88. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS },
  89. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS },
  90. { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS },
  91. };
  92. static bt_mesh_mutex_t generic_client_lock;
  93. static inline void bt_mesh_generic_client_mutex_new(void)
  94. {
  95. if (!generic_client_lock.mutex) {
  96. bt_mesh_mutex_create(&generic_client_lock);
  97. }
  98. }
  99. #if CONFIG_BLE_MESH_DEINIT
  100. static inline void bt_mesh_generic_client_mutex_free(void)
  101. {
  102. bt_mesh_mutex_free(&generic_client_lock);
  103. }
  104. #endif /* CONFIG_BLE_MESH_DEINIT */
  105. static inline void bt_mesh_generic_client_lock(void)
  106. {
  107. bt_mesh_mutex_lock(&generic_client_lock);
  108. }
  109. static inline void bt_mesh_generic_client_unlock(void)
  110. {
  111. bt_mesh_mutex_unlock(&generic_client_lock);
  112. }
  113. static void timeout_handler(struct k_work *work)
  114. {
  115. struct k_delayed_work *timer = NULL;
  116. bt_mesh_client_node_t *node = NULL;
  117. struct bt_mesh_msg_ctx ctx = {0};
  118. uint32_t opcode = 0U;
  119. BT_WARN("Receive generic status message timeout");
  120. bt_mesh_generic_client_lock();
  121. timer = CONTAINER_OF(work, struct k_delayed_work, work);
  122. if (timer && !k_delayed_work_free(timer)) {
  123. node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work);
  124. if (node) {
  125. memcpy(&ctx, &node->ctx, sizeof(ctx));
  126. opcode = node->opcode;
  127. bt_mesh_client_free_node(node);
  128. bt_mesh_generic_client_cb_evt_to_btc(
  129. opcode, BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0);
  130. }
  131. }
  132. bt_mesh_generic_client_unlock();
  133. return;
  134. }
  135. static void generic_status(struct bt_mesh_model *model,
  136. struct bt_mesh_msg_ctx *ctx,
  137. struct net_buf_simple *buf)
  138. {
  139. bt_mesh_client_node_t *node = NULL;
  140. uint8_t *val = NULL;
  141. uint8_t evt = 0xFF;
  142. size_t len = 0U;
  143. BT_DBG("len %d, bytes %s", buf->len, bt_hex(buf->data, buf->len));
  144. switch (ctx->recv_op) {
  145. case BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS: {
  146. struct bt_mesh_gen_onoff_status *status = NULL;
  147. if (buf->len != 1 && buf->len != 3) {
  148. BT_ERR("Invalid Generic OnOff Status length %d", buf->len);
  149. return;
  150. }
  151. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_onoff_status));
  152. if (!status) {
  153. BT_ERR("%s, Out of memory", __func__);
  154. return;
  155. }
  156. status->present_onoff = net_buf_simple_pull_u8(buf);
  157. if (buf->len) {
  158. status->op_en = true;
  159. status->target_onoff = net_buf_simple_pull_u8(buf);
  160. status->remain_time = net_buf_simple_pull_u8(buf);
  161. }
  162. val = (uint8_t *)status;
  163. len = sizeof(struct bt_mesh_gen_onoff_status);
  164. break;
  165. }
  166. case BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS: {
  167. struct bt_mesh_gen_level_status *status = NULL;
  168. if (buf->len != 2 && buf->len != 5) {
  169. BT_ERR("Invalid Generic Level Status length %d", buf->len);
  170. return;
  171. }
  172. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_level_status));
  173. if (!status) {
  174. BT_ERR("%s, Out of memory", __func__);
  175. return;
  176. }
  177. status->present_level = net_buf_simple_pull_le16(buf);
  178. if (buf->len) {
  179. status->op_en = true;
  180. status->target_level = net_buf_simple_pull_le16(buf);
  181. status->remain_time = net_buf_simple_pull_u8(buf);
  182. }
  183. val = (uint8_t *)status;
  184. len = sizeof(struct bt_mesh_gen_level_status);
  185. break;
  186. }
  187. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS: {
  188. struct bt_mesh_gen_def_trans_time_status *status = NULL;
  189. if (buf->len != 1) {
  190. BT_ERR("Invalid Generic Default Trans Time Status length %d", buf->len);
  191. return;
  192. }
  193. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_def_trans_time_status));
  194. if (!status) {
  195. BT_ERR("%s, Out of memory", __func__);
  196. return;
  197. }
  198. status->trans_time = net_buf_simple_pull_u8(buf);
  199. val = (uint8_t *)status;
  200. len = sizeof(struct bt_mesh_gen_def_trans_time_status);
  201. break;
  202. }
  203. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS: {
  204. struct bt_mesh_gen_onpowerup_status *status = NULL;
  205. if (buf->len != 1) {
  206. BT_ERR("Invalid Generic OnPowerUp Status length %d", buf->len);
  207. return;
  208. }
  209. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_onpowerup_status));
  210. if (!status) {
  211. BT_ERR("%s, Out of memory", __func__);
  212. return;
  213. }
  214. status->onpowerup = net_buf_simple_pull_u8(buf);
  215. val = (uint8_t *)status;
  216. len = sizeof(struct bt_mesh_gen_onpowerup_status);
  217. break;
  218. }
  219. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS: {
  220. struct bt_mesh_gen_power_level_status *status = NULL;
  221. if (buf->len != 2 && buf->len != 5) {
  222. BT_ERR("Invalid Generic Power Level Status length %d", buf->len);
  223. return;
  224. }
  225. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_level_status));
  226. if (!status) {
  227. BT_ERR("%s, Out of memory", __func__);
  228. return;
  229. }
  230. status->present_power = net_buf_simple_pull_le16(buf);
  231. if (buf->len) {
  232. status->op_en = true;
  233. status->target_power = net_buf_simple_pull_le16(buf);
  234. status->remain_time = net_buf_simple_pull_u8(buf);
  235. }
  236. val = (uint8_t *)status;
  237. len = sizeof(struct bt_mesh_gen_power_level_status);
  238. break;
  239. }
  240. case BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS: {
  241. struct bt_mesh_gen_power_last_status *status = NULL;
  242. if (buf->len != 2) {
  243. BT_ERR("Invalid Generic Power Last Status length %d", buf->len);
  244. return;
  245. }
  246. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_last_status));
  247. if (!status) {
  248. BT_ERR("%s, Out of memory", __func__);
  249. return;
  250. }
  251. status->power = net_buf_simple_pull_le16(buf);
  252. val = (uint8_t *)status;
  253. len = sizeof(struct bt_mesh_gen_power_last_status);
  254. break;
  255. }
  256. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS: {
  257. struct bt_mesh_gen_power_default_status *status = NULL;
  258. if (buf->len != 2) {
  259. BT_ERR("Invalid Generic Power Default Status length %d", buf->len);
  260. return;
  261. }
  262. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_default_status));
  263. if (!status) {
  264. BT_ERR("%s, Out of memory", __func__);
  265. return;
  266. }
  267. status->power = net_buf_simple_pull_le16(buf);
  268. val = (uint8_t *)status;
  269. len = sizeof(struct bt_mesh_gen_power_default_status);
  270. break;
  271. }
  272. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS: {
  273. struct bt_mesh_gen_power_range_status *status = NULL;
  274. if (buf->len != 5) {
  275. BT_ERR("Invalid Generic Power Range Status length %d", buf->len);
  276. return;
  277. }
  278. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_range_status));
  279. if (!status) {
  280. BT_ERR("%s, Out of memory", __func__);
  281. return;
  282. }
  283. status->status_code = net_buf_simple_pull_u8(buf);
  284. status->range_min = net_buf_simple_pull_le16(buf);
  285. status->range_max = net_buf_simple_pull_le16(buf);
  286. val = (uint8_t *)status;
  287. len = sizeof(struct bt_mesh_gen_power_range_status);
  288. break;
  289. }
  290. case BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS: {
  291. struct bt_mesh_gen_battery_status *status = NULL;
  292. if (buf->len != 8) {
  293. BT_ERR("Invalid Generic Battery Status length %d", buf->len);
  294. return;
  295. }
  296. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_battery_status));
  297. if (!status) {
  298. BT_ERR("%s, Out of memory", __func__);
  299. return;
  300. }
  301. uint32_t value = 0;
  302. value = net_buf_simple_pull_le32(buf);
  303. status->battery_level = (uint8_t)value;
  304. status->time_to_discharge = (value >> 8);
  305. value = net_buf_simple_pull_le32(buf);
  306. status->time_to_charge = (value & 0xffffff);
  307. status->flags = (uint8_t)(value >> 24);
  308. val = (uint8_t *)status;
  309. len = sizeof(struct bt_mesh_gen_battery_status);
  310. break;
  311. }
  312. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS: {
  313. struct bt_mesh_gen_loc_global_status *status = NULL;
  314. if (buf->len != 10) {
  315. BT_ERR("Invalid Generic Location Global Status length %d", buf->len);
  316. return;
  317. }
  318. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_loc_global_status));
  319. if (!status) {
  320. BT_ERR("%s, Out of memory", __func__);
  321. return;
  322. }
  323. status->global_latitude = net_buf_simple_pull_le32(buf);
  324. status->global_longitude = net_buf_simple_pull_le32(buf);
  325. status->global_altitude = net_buf_simple_pull_le16(buf);
  326. val = (uint8_t *)status;
  327. len = sizeof(struct bt_mesh_gen_loc_global_status);
  328. break;
  329. }
  330. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS: {
  331. struct bt_mesh_gen_loc_local_status *status = NULL;
  332. if (buf->len != 9) {
  333. BT_ERR("Invalid Generic Location Local Status length %d", buf->len);
  334. return;
  335. }
  336. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_loc_local_status));
  337. if (!status) {
  338. BT_ERR("%s, Out of memory", __func__);
  339. return;
  340. }
  341. status->local_north = net_buf_simple_pull_le16(buf);
  342. status->local_east = net_buf_simple_pull_le16(buf);
  343. status->local_altitude = net_buf_simple_pull_le16(buf);
  344. status->floor_number = net_buf_simple_pull_u8(buf);
  345. status->uncertainty = net_buf_simple_pull_le16(buf);
  346. val = (uint8_t *)status;
  347. len = sizeof(struct bt_mesh_gen_loc_local_status);
  348. break;
  349. }
  350. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: {
  351. struct bt_mesh_gen_user_properties_status *status = NULL;
  352. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_user_properties_status));
  353. if (!status) {
  354. BT_ERR("%s, Out of memory", __func__);
  355. return;
  356. }
  357. status->user_property_ids = bt_mesh_alloc_buf(buf->len);
  358. if (!status->user_property_ids) {
  359. BT_ERR("%s, Out of memory", __func__);
  360. bt_mesh_free(status);
  361. return;
  362. }
  363. net_buf_simple_add_mem(status->user_property_ids, buf->data, buf->len);
  364. val = (uint8_t *)status;
  365. len = sizeof(struct bt_mesh_gen_user_properties_status);
  366. break;
  367. }
  368. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: {
  369. struct bt_mesh_gen_user_property_status *status = NULL;
  370. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_user_property_status));
  371. if (!status) {
  372. BT_ERR("%s, Out of memory", __func__);
  373. return;
  374. }
  375. status->user_property_id = net_buf_simple_pull_le16(buf);
  376. if (buf->len) {
  377. status->op_en = true;
  378. status->user_access = net_buf_simple_pull_u8(buf);
  379. status->user_property_value = bt_mesh_alloc_buf(buf->len);
  380. if (!status->user_property_value) {
  381. BT_ERR("%s, Out of memory", __func__);
  382. bt_mesh_free(status);
  383. return;
  384. }
  385. net_buf_simple_add_mem(status->user_property_value, buf->data, buf->len);
  386. }
  387. val = (uint8_t *)status;
  388. len = sizeof(struct bt_mesh_gen_user_property_status);
  389. break;
  390. }
  391. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: {
  392. struct bt_mesh_gen_admin_properties_status *status = NULL;
  393. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_admin_properties_status));
  394. if (!status) {
  395. BT_ERR("%s, Out of memory", __func__);
  396. return;
  397. }
  398. status->admin_property_ids = bt_mesh_alloc_buf(buf->len);
  399. if (!status->admin_property_ids) {
  400. BT_ERR("%s, Out of memory", __func__);
  401. bt_mesh_free(status);
  402. return;
  403. }
  404. net_buf_simple_add_mem(status->admin_property_ids, buf->data, buf->len);
  405. val = (uint8_t *)status;
  406. len = sizeof(struct bt_mesh_gen_admin_properties_status);
  407. break;
  408. }
  409. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: {
  410. struct bt_mesh_gen_admin_property_status *status = NULL;
  411. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_admin_property_status));
  412. if (!status) {
  413. BT_ERR("%s, Out of memory", __func__);
  414. return;
  415. }
  416. status->admin_property_id = net_buf_simple_pull_le16(buf);
  417. if (buf->len) {
  418. status->op_en = true;
  419. status->admin_user_access = net_buf_simple_pull_u8(buf);
  420. status->admin_property_value = bt_mesh_alloc_buf(buf->len);
  421. if (!status->admin_property_value) {
  422. BT_ERR("%s, Out of memory", __func__);
  423. bt_mesh_free(status);
  424. return;
  425. }
  426. net_buf_simple_add_mem(status->admin_property_value, buf->data, buf->len);
  427. }
  428. val = (uint8_t *)status;
  429. len = sizeof(struct bt_mesh_gen_admin_property_status);
  430. break;
  431. }
  432. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS: {
  433. struct bt_mesh_gen_manu_properties_status *status = NULL;
  434. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_manu_properties_status));
  435. if (!status) {
  436. BT_ERR("%s, Out of memory", __func__);
  437. return;
  438. }
  439. status->manu_property_ids = bt_mesh_alloc_buf(buf->len);
  440. if (!status->manu_property_ids) {
  441. BT_ERR("%s, Out of memory", __func__);
  442. bt_mesh_free(status);
  443. return;
  444. }
  445. net_buf_simple_add_mem(status->manu_property_ids, buf->data, buf->len);
  446. val = (uint8_t *)status;
  447. len = sizeof(struct bt_mesh_gen_manu_properties_status);
  448. break;
  449. }
  450. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS: {
  451. struct bt_mesh_gen_manu_property_status *status = NULL;
  452. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_manu_property_status));
  453. if (!status) {
  454. BT_ERR("%s, Out of memory", __func__);
  455. return;
  456. }
  457. status->manu_property_id = net_buf_simple_pull_le16(buf);
  458. if (buf->len) {
  459. status->op_en = true;
  460. status->manu_user_access = net_buf_simple_pull_u8(buf);
  461. status->manu_property_value = bt_mesh_alloc_buf(buf->len);
  462. if (!status->manu_property_value) {
  463. BT_ERR("%s, Out of memory", __func__);
  464. bt_mesh_free(status);
  465. return;
  466. }
  467. net_buf_simple_add_mem(status->manu_property_value, buf->data, buf->len);
  468. }
  469. val = (uint8_t *)status;
  470. len = sizeof(struct bt_mesh_gen_manu_property_status);
  471. break;
  472. }
  473. case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: {
  474. struct bt_mesh_gen_client_properties_status *status = NULL;
  475. status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_client_properties_status));
  476. if (!status) {
  477. BT_ERR("%s, Out of memory", __func__);
  478. return;
  479. }
  480. status->client_property_ids = bt_mesh_alloc_buf(buf->len);
  481. if (!status->client_property_ids) {
  482. BT_ERR("%s, Out of memory", __func__);
  483. bt_mesh_free(status);
  484. return;
  485. }
  486. net_buf_simple_add_mem(status->client_property_ids, buf->data, buf->len);
  487. val = (uint8_t *)status;
  488. len = sizeof(struct bt_mesh_gen_client_properties_status);
  489. break;
  490. }
  491. default:
  492. BT_ERR("Invalid Generic Status opcode 0x%04x", ctx->recv_op);
  493. return;
  494. }
  495. buf->data = val;
  496. buf->len = len;
  497. bt_mesh_generic_client_lock();
  498. node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true);
  499. if (!node) {
  500. BT_DBG("Unexpected Generic Status 0x%04x", ctx->recv_op);
  501. } else {
  502. switch (node->opcode) {
  503. case BLE_MESH_MODEL_OP_GEN_ONOFF_GET:
  504. case BLE_MESH_MODEL_OP_GEN_LEVEL_GET:
  505. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET:
  506. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET:
  507. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET:
  508. case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET:
  509. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET:
  510. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET:
  511. case BLE_MESH_MODEL_OP_GEN_BATTERY_GET:
  512. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET:
  513. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET:
  514. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET:
  515. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET:
  516. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET:
  517. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET:
  518. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET:
  519. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET:
  520. case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET:
  521. evt = BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE;
  522. break;
  523. case BLE_MESH_MODEL_OP_GEN_ONOFF_SET:
  524. case BLE_MESH_MODEL_OP_GEN_LEVEL_SET:
  525. case BLE_MESH_MODEL_OP_GEN_DELTA_SET:
  526. case BLE_MESH_MODEL_OP_GEN_MOVE_SET:
  527. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET:
  528. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET:
  529. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET:
  530. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET:
  531. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET:
  532. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET:
  533. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET:
  534. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET:
  535. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET:
  536. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET:
  537. evt = BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE;
  538. break;
  539. default:
  540. break;
  541. }
  542. if (!k_delayed_work_free(&node->timer)) {
  543. uint32_t opcode = node->opcode;
  544. bt_mesh_client_free_node(node);
  545. bt_mesh_generic_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len);
  546. }
  547. }
  548. bt_mesh_generic_client_unlock();
  549. switch (ctx->recv_op) {
  550. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: {
  551. struct bt_mesh_gen_user_properties_status *status;
  552. status = (struct bt_mesh_gen_user_properties_status *)val;
  553. bt_mesh_free_buf(status->user_property_ids);
  554. break;
  555. }
  556. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: {
  557. struct bt_mesh_gen_user_property_status *status;
  558. status = (struct bt_mesh_gen_user_property_status *)val;
  559. bt_mesh_free_buf(status->user_property_value);
  560. break;
  561. }
  562. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: {
  563. struct bt_mesh_gen_admin_properties_status *status;
  564. status = (struct bt_mesh_gen_admin_properties_status *)val;
  565. bt_mesh_free_buf(status->admin_property_ids);
  566. break;
  567. }
  568. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: {
  569. struct bt_mesh_gen_admin_property_status *status;
  570. status = (struct bt_mesh_gen_admin_property_status *)val;
  571. bt_mesh_free_buf(status->admin_property_value);
  572. break;
  573. }
  574. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS: {
  575. struct bt_mesh_gen_manu_properties_status *status;
  576. status = (struct bt_mesh_gen_manu_properties_status *)val;
  577. bt_mesh_free_buf(status->manu_property_ids);
  578. break;
  579. }
  580. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS: {
  581. struct bt_mesh_gen_manu_property_status *status;
  582. status = (struct bt_mesh_gen_manu_property_status *)val;
  583. bt_mesh_free_buf(status->manu_property_value);
  584. break;
  585. }
  586. case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: {
  587. struct bt_mesh_gen_client_properties_status *status;
  588. status = (struct bt_mesh_gen_client_properties_status *)val;
  589. bt_mesh_free_buf(status->client_property_ids);
  590. break;
  591. }
  592. default:
  593. break;
  594. }
  595. bt_mesh_free(val);
  596. return;
  597. }
  598. const struct bt_mesh_model_op bt_mesh_gen_onoff_cli_op[] = {
  599. { BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, 1, generic_status },
  600. BLE_MESH_MODEL_OP_END,
  601. };
  602. const struct bt_mesh_model_op bt_mesh_gen_level_cli_op[] = {
  603. { BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS, 2, generic_status },
  604. BLE_MESH_MODEL_OP_END,
  605. };
  606. const struct bt_mesh_model_op bt_mesh_gen_def_trans_time_cli_op[] = {
  607. { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS, 1, generic_status },
  608. BLE_MESH_MODEL_OP_END,
  609. };
  610. const struct bt_mesh_model_op bt_mesh_gen_power_onoff_cli_op[] = {
  611. { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS, 1, generic_status },
  612. BLE_MESH_MODEL_OP_END,
  613. };
  614. const struct bt_mesh_model_op bt_mesh_gen_power_level_cli_op[] = {
  615. { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS, 2, generic_status },
  616. { BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS, 2, generic_status },
  617. { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS, 2, generic_status },
  618. { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS, 5, generic_status },
  619. BLE_MESH_MODEL_OP_END,
  620. };
  621. const struct bt_mesh_model_op bt_mesh_gen_battery_cli_op[] = {
  622. { BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS, 8, generic_status },
  623. BLE_MESH_MODEL_OP_END,
  624. };
  625. const struct bt_mesh_model_op bt_mesh_gen_location_cli_op[] = {
  626. { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS, 10, generic_status },
  627. { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS, 9, generic_status },
  628. BLE_MESH_MODEL_OP_END,
  629. };
  630. const struct bt_mesh_model_op bt_mesh_gen_property_cli_op[] = {
  631. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS, 2, generic_status },
  632. { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS, 2, generic_status },
  633. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS, 2, generic_status },
  634. { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS, 2, generic_status },
  635. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS, 2, generic_status },
  636. { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS, 2, generic_status },
  637. { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS, 2, generic_status },
  638. BLE_MESH_MODEL_OP_END,
  639. };
  640. static int gen_get_state(bt_mesh_client_common_param_t *common, void *value)
  641. {
  642. NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_GEN_GET_STATE_MSG_LEN);
  643. bt_mesh_model_msg_init(&msg, common->opcode);
  644. if (value) {
  645. switch (common->opcode) {
  646. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: {
  647. struct bt_mesh_gen_user_property_get *get;
  648. get = (struct bt_mesh_gen_user_property_get *)value;
  649. net_buf_simple_add_le16(&msg, get->user_property_id);
  650. break;
  651. }
  652. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: {
  653. struct bt_mesh_gen_admin_property_get *get;
  654. get = (struct bt_mesh_gen_admin_property_get *)value;
  655. net_buf_simple_add_le16(&msg, get->admin_property_id);
  656. break;
  657. }
  658. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: {
  659. struct bt_mesh_gen_manu_property_get *get;
  660. get = (struct bt_mesh_gen_manu_property_get *)value;
  661. net_buf_simple_add_le16(&msg, get->manu_property_id);
  662. break;
  663. }
  664. case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: {
  665. struct bt_mesh_gen_client_properties_get *get;
  666. get = (struct bt_mesh_gen_client_properties_get *)value;
  667. net_buf_simple_add_le16(&msg, get->client_property_id);
  668. break;
  669. }
  670. default:
  671. BT_DBG("No parameters for Generic Get 0x%04x", common->opcode);
  672. break;
  673. }
  674. }
  675. return bt_mesh_client_send_msg(common, &msg, true, timeout_handler);
  676. }
  677. static int gen_set_state(bt_mesh_client_common_param_t *common,
  678. void *value, uint16_t value_len, bool need_ack)
  679. {
  680. struct net_buf_simple *msg = NULL;
  681. int err = 0;
  682. msg = bt_mesh_alloc_buf(value_len);
  683. if (!msg) {
  684. BT_ERR("%s, Out of memory", __func__);
  685. return -ENOMEM;
  686. }
  687. bt_mesh_model_msg_init(msg, common->opcode);
  688. switch (common->opcode) {
  689. case BLE_MESH_MODEL_OP_GEN_ONOFF_SET:
  690. case BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: {
  691. struct bt_mesh_gen_onoff_set *set;
  692. set = (struct bt_mesh_gen_onoff_set *)value;
  693. net_buf_simple_add_u8(msg, set->onoff);
  694. net_buf_simple_add_u8(msg, set->tid);
  695. if (set->op_en) {
  696. net_buf_simple_add_u8(msg, set->trans_time);
  697. net_buf_simple_add_u8(msg, set->delay);
  698. }
  699. break;
  700. }
  701. case BLE_MESH_MODEL_OP_GEN_LEVEL_SET:
  702. case BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK: {
  703. struct bt_mesh_gen_level_set *set;
  704. set = (struct bt_mesh_gen_level_set *)value;
  705. net_buf_simple_add_le16(msg, set->level);
  706. net_buf_simple_add_u8(msg, set->tid);
  707. if (set->op_en) {
  708. net_buf_simple_add_u8(msg, set->trans_time);
  709. net_buf_simple_add_u8(msg, set->delay);
  710. }
  711. break;
  712. }
  713. case BLE_MESH_MODEL_OP_GEN_DELTA_SET:
  714. case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: {
  715. struct bt_mesh_gen_delta_set *set;
  716. set = (struct bt_mesh_gen_delta_set *)value;
  717. net_buf_simple_add_le32(msg, set->delta_level);
  718. net_buf_simple_add_u8(msg, set->tid);
  719. if (set->op_en) {
  720. net_buf_simple_add_u8(msg, set->trans_time);
  721. net_buf_simple_add_u8(msg, set->delay);
  722. }
  723. break;
  724. }
  725. case BLE_MESH_MODEL_OP_GEN_MOVE_SET:
  726. case BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK: {
  727. struct bt_mesh_gen_move_set *set;
  728. set = (struct bt_mesh_gen_move_set *)value;
  729. net_buf_simple_add_le16(msg, set->delta_level);
  730. net_buf_simple_add_u8(msg, set->tid);
  731. if (set->op_en) {
  732. net_buf_simple_add_u8(msg, set->trans_time);
  733. net_buf_simple_add_u8(msg, set->delay);
  734. }
  735. break;
  736. }
  737. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET:
  738. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK: {
  739. struct bt_mesh_gen_def_trans_time_set *set;
  740. set = (struct bt_mesh_gen_def_trans_time_set *)value;
  741. net_buf_simple_add_u8(msg, set->trans_time);
  742. break;
  743. }
  744. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET:
  745. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK: {
  746. struct bt_mesh_gen_onpowerup_set *set;
  747. set = (struct bt_mesh_gen_onpowerup_set *)value;
  748. net_buf_simple_add_u8(msg, set->onpowerup);
  749. break;
  750. }
  751. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET:
  752. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK: {
  753. struct bt_mesh_gen_power_level_set *set;
  754. set = (struct bt_mesh_gen_power_level_set *)value;
  755. net_buf_simple_add_le16(msg, set->power);
  756. net_buf_simple_add_u8(msg, set->tid);
  757. if (set->op_en) {
  758. net_buf_simple_add_u8(msg, set->trans_time);
  759. net_buf_simple_add_u8(msg, set->delay);
  760. }
  761. break;
  762. }
  763. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET:
  764. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK: {
  765. struct bt_mesh_gen_power_default_set *set;
  766. set = (struct bt_mesh_gen_power_default_set *)value;
  767. net_buf_simple_add_le16(msg, set->power);
  768. break;
  769. }
  770. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET:
  771. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK: {
  772. struct bt_mesh_gen_power_range_set *set;
  773. set = (struct bt_mesh_gen_power_range_set *)value;
  774. net_buf_simple_add_le16(msg, set->range_min);
  775. net_buf_simple_add_le16(msg, set->range_max);
  776. break;
  777. }
  778. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET:
  779. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK: {
  780. struct bt_mesh_gen_loc_global_set *set;
  781. set = (struct bt_mesh_gen_loc_global_set *)value;
  782. net_buf_simple_add_le32(msg, set->global_latitude);
  783. net_buf_simple_add_le32(msg, set->global_longitude);
  784. net_buf_simple_add_le16(msg, set->global_altitude);
  785. break;
  786. }
  787. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET:
  788. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK: {
  789. struct bt_mesh_gen_loc_local_set *set;
  790. set = (struct bt_mesh_gen_loc_local_set *)value;
  791. net_buf_simple_add_le16(msg, set->local_north);
  792. net_buf_simple_add_le16(msg, set->local_east);
  793. net_buf_simple_add_le16(msg, set->local_altitude);
  794. net_buf_simple_add_u8(msg, set->floor_number);
  795. net_buf_simple_add_le16(msg, set->uncertainty);
  796. break;
  797. }
  798. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET:
  799. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: {
  800. struct bt_mesh_gen_user_property_set *set;
  801. set = (struct bt_mesh_gen_user_property_set *)value;
  802. net_buf_simple_add_le16(msg, set->user_property_id);
  803. net_buf_simple_add_mem(msg, set->user_property_value->data, set->user_property_value->len);
  804. break;
  805. }
  806. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET:
  807. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: {
  808. struct bt_mesh_gen_admin_property_set *set;
  809. set = (struct bt_mesh_gen_admin_property_set *)value;
  810. net_buf_simple_add_le16(msg, set->admin_property_id);
  811. net_buf_simple_add_u8(msg, set->admin_user_access);
  812. net_buf_simple_add_mem(msg, set->admin_property_value->data, set->admin_property_value->len);
  813. break;
  814. }
  815. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET:
  816. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK: {
  817. struct bt_mesh_gen_manu_property_set *set;
  818. set = (struct bt_mesh_gen_manu_property_set *)value;
  819. net_buf_simple_add_le16(msg, set->manu_property_id);
  820. net_buf_simple_add_u8(msg, set->manu_user_access);
  821. break;
  822. }
  823. default:
  824. BT_ERR("Invalid Generic Set opcode 0x%04x", common->opcode);
  825. err = -EINVAL;
  826. goto end;
  827. }
  828. err = bt_mesh_client_send_msg(common, msg, need_ack, timeout_handler);
  829. end:
  830. bt_mesh_free_buf(msg);
  831. return err;
  832. }
  833. int bt_mesh_generic_client_get_state(bt_mesh_client_common_param_t *common, void *get)
  834. {
  835. bt_mesh_generic_client_t *client = NULL;
  836. if (!common || !common->model) {
  837. BT_ERR("%s, Invalid parameter", __func__);
  838. return -EINVAL;
  839. }
  840. client = (bt_mesh_generic_client_t *)common->model->user_data;
  841. if (!client || !client->internal_data) {
  842. BT_ERR("Invalid Generic client data");
  843. return -EINVAL;
  844. }
  845. switch (common->opcode) {
  846. case BLE_MESH_MODEL_OP_GEN_ONOFF_GET:
  847. case BLE_MESH_MODEL_OP_GEN_LEVEL_GET:
  848. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET:
  849. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET:
  850. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET:
  851. case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET:
  852. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET:
  853. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET:
  854. case BLE_MESH_MODEL_OP_GEN_BATTERY_GET:
  855. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET:
  856. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET:
  857. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET:
  858. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET:
  859. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET:
  860. break;
  861. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET:
  862. if (!get) {
  863. BT_ERR("Invalid Generic User Property Get");
  864. return -EINVAL;
  865. }
  866. break;
  867. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET:
  868. if (!get) {
  869. BT_ERR("Invalid Generic Admin Property Get");
  870. return -EINVAL;
  871. }
  872. break;
  873. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET:
  874. if (!get) {
  875. BT_ERR("Invalid Generic Manu Property Get");
  876. return -EINVAL;
  877. }
  878. break;
  879. case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET:
  880. if (!get) {
  881. BT_ERR("Invalid Generic Client Properties Get");
  882. return -EINVAL;
  883. }
  884. break;
  885. default:
  886. BT_ERR("Invalid Generic Get opcode 0x%04x", common->opcode);
  887. return -EINVAL;
  888. }
  889. return gen_get_state(common, get);
  890. }
  891. int bt_mesh_generic_client_set_state(bt_mesh_client_common_param_t *common, void *set)
  892. {
  893. bt_mesh_generic_client_t *client = NULL;
  894. uint16_t length = 0U;
  895. bool need_ack = false;
  896. if (!common || !common->model || !set) {
  897. BT_ERR("%s, Invalid parameter", __func__);
  898. return -EINVAL;
  899. }
  900. client = (bt_mesh_generic_client_t *)common->model->user_data;
  901. if (!client || !client->internal_data) {
  902. BT_ERR("Invalid Generic client data");
  903. return -EINVAL;
  904. }
  905. switch (common->opcode) {
  906. case BLE_MESH_MODEL_OP_GEN_ONOFF_SET:
  907. need_ack = true;
  908. case BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: {
  909. struct bt_mesh_gen_onoff_set *value;
  910. value = (struct bt_mesh_gen_onoff_set *)set;
  911. if (value->op_en) {
  912. if ((value->trans_time & 0x3F) > 0x3E) {
  913. BT_ERR("Invalid Generic OnOff Set transition time");
  914. return -EINVAL;
  915. }
  916. }
  917. length = BLE_MESH_GEN_ONOFF_SET_MSG_LEN;
  918. break;
  919. }
  920. case BLE_MESH_MODEL_OP_GEN_LEVEL_SET:
  921. need_ack = true;
  922. case BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK: {
  923. struct bt_mesh_gen_level_set *value;
  924. value = (struct bt_mesh_gen_level_set *)set;
  925. if (value->op_en) {
  926. if ((value->trans_time & 0x3F) > 0x3E) {
  927. BT_ERR("Invalid Generic Level Set transition time");
  928. return -EINVAL;
  929. }
  930. }
  931. length = BLE_MESH_GEN_LEVEL_SET_MSG_LEN;
  932. break;
  933. }
  934. case BLE_MESH_MODEL_OP_GEN_DELTA_SET:
  935. need_ack = true;
  936. case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: {
  937. struct bt_mesh_gen_delta_set *value;
  938. value = (struct bt_mesh_gen_delta_set *)set;
  939. if (value->op_en) {
  940. if ((value->trans_time & 0x3F) > 0x3E) {
  941. BT_ERR("Invalid Generic Delta Set transition time");
  942. return -EINVAL;
  943. }
  944. }
  945. length = BLE_MESH_GEN_DELTA_SET_MSG_LEN;
  946. break;
  947. }
  948. case BLE_MESH_MODEL_OP_GEN_MOVE_SET:
  949. need_ack = true;
  950. case BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK: {
  951. struct bt_mesh_gen_move_set *value;
  952. value = (struct bt_mesh_gen_move_set *)set;
  953. if (value->op_en) {
  954. if ((value->trans_time & 0x3F) > 0x3E) {
  955. BT_ERR("Invalid Generic Move Set transition time");
  956. return -EINVAL;
  957. }
  958. }
  959. length = BLE_MESH_GEN_MOVE_SET_MSG_LEN;
  960. break;
  961. }
  962. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET:
  963. need_ack = true;
  964. case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK: {
  965. uint8_t value = *(uint8_t *)set;
  966. if ((value & 0x3F) > 0x3E) {
  967. BT_ERR("Invalid Generic Default Trans Time Set transition time");
  968. return -EINVAL;
  969. }
  970. length = BLE_MESH_GEN_DEF_TRANS_TIME_SET_MSG_LEN;
  971. break;
  972. }
  973. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET:
  974. need_ack = true;
  975. case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK:
  976. length = BLE_MESH_GEN_ONPOWERUP_SET_MSG_LEN;
  977. break;
  978. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET:
  979. need_ack = true;
  980. case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK: {
  981. struct bt_mesh_gen_power_level_set *value;
  982. value = (struct bt_mesh_gen_power_level_set *)set;
  983. if (value->op_en) {
  984. if ((value->trans_time & 0x3F) > 0x3E) {
  985. BT_ERR("Invalid Generic Power Level Set transition time");
  986. return -EINVAL;
  987. }
  988. }
  989. length = BLE_MESH_GEN_POWER_LEVEL_SET_MSG_LEN;
  990. break;
  991. }
  992. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET:
  993. need_ack = true;
  994. case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK:
  995. length = BLE_MESH_GEN_POWER_DEFAULT_SET_MSG_LEN;
  996. break;
  997. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET:
  998. need_ack = true;
  999. case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK: {
  1000. struct bt_mesh_gen_power_range_set *value;
  1001. value = (struct bt_mesh_gen_power_range_set *)set;
  1002. if (value->range_min > value->range_max) {
  1003. BT_ERR("Generic Power Level Set range min is greater than range max");
  1004. return -EINVAL;
  1005. }
  1006. length = BLE_MESH_GEN_POWER_RANGE_SET_MSG_LEN;
  1007. break;
  1008. }
  1009. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET:
  1010. need_ack = true;
  1011. case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK:
  1012. length = BLE_MESH_GEN_LOC_GLOBAL_SET_MSG_LEN;
  1013. break;
  1014. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET:
  1015. need_ack = true;
  1016. case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK:
  1017. length = BLE_MESH_GEN_LOC_LOCAL_SET_MSG_LEN;
  1018. break;
  1019. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET:
  1020. need_ack = true;
  1021. case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: {
  1022. struct bt_mesh_gen_user_property_set *value;
  1023. value = (struct bt_mesh_gen_user_property_set *)set;
  1024. if (!value->user_property_value) {
  1025. BT_ERR("Invalid Generic User Property value");
  1026. return -EINVAL;
  1027. }
  1028. length = (1 + 2 + value->user_property_value->len + 4);
  1029. break;
  1030. }
  1031. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET:
  1032. need_ack = true;
  1033. case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: {
  1034. struct bt_mesh_gen_admin_property_set *value;
  1035. value = (struct bt_mesh_gen_admin_property_set *)set;
  1036. if (!value->admin_property_value) {
  1037. BT_ERR("Invalid Generic Admin Property value");
  1038. return -EINVAL;
  1039. }
  1040. length = (1 + 2 + 1 + value->admin_property_value->len + 4);
  1041. break;
  1042. }
  1043. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET:
  1044. need_ack = true;
  1045. case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK:
  1046. length = BLE_MESH_GEN_MANU_PROPERTY_SET_MSG_LEN;
  1047. break;
  1048. default:
  1049. BT_ERR("Invalid Generic Set opcode 0x%04x", common->opcode);
  1050. return -EINVAL;
  1051. }
  1052. return gen_set_state(common, set, length, need_ack);
  1053. }
  1054. static int generic_client_init(struct bt_mesh_model *model)
  1055. {
  1056. generic_internal_data_t *internal = NULL;
  1057. bt_mesh_generic_client_t *client = NULL;
  1058. if (!model) {
  1059. BT_ERR("Invalid Generic client model");
  1060. return -EINVAL;
  1061. }
  1062. client = (bt_mesh_generic_client_t *)model->user_data;
  1063. if (!client) {
  1064. BT_ERR("No Generic client context provided");
  1065. return -EINVAL;
  1066. }
  1067. if (!client->internal_data) {
  1068. internal = bt_mesh_calloc(sizeof(generic_internal_data_t));
  1069. if (!internal) {
  1070. BT_ERR("%s, Out of memory", __func__);
  1071. return -ENOMEM;
  1072. }
  1073. sys_slist_init(&internal->queue);
  1074. client->model = model;
  1075. client->op_pair_size = ARRAY_SIZE(gen_op_pair);
  1076. client->op_pair = gen_op_pair;
  1077. client->internal_data = internal;
  1078. } else {
  1079. bt_mesh_client_clear_list(client->internal_data);
  1080. }
  1081. bt_mesh_generic_client_mutex_new();
  1082. return 0;
  1083. }
  1084. #if CONFIG_BLE_MESH_DEINIT
  1085. static int generic_client_deinit(struct bt_mesh_model *model)
  1086. {
  1087. bt_mesh_generic_client_t *client = NULL;
  1088. if (!model) {
  1089. BT_ERR("Invalid Generic client model");
  1090. return -EINVAL;
  1091. }
  1092. client = (bt_mesh_generic_client_t *)model->user_data;
  1093. if (!client) {
  1094. BT_ERR("No Generic client context provided");
  1095. return -EINVAL;
  1096. }
  1097. if (client->internal_data) {
  1098. /* Remove items from the list */
  1099. bt_mesh_client_clear_list(client->internal_data);
  1100. /* Free the allocated internal data */
  1101. bt_mesh_free(client->internal_data);
  1102. client->internal_data = NULL;
  1103. }
  1104. bt_mesh_generic_client_mutex_free();
  1105. return 0;
  1106. }
  1107. #endif /* CONFIG_BLE_MESH_DEINIT */
  1108. const struct bt_mesh_model_cb bt_mesh_generic_client_cb = {
  1109. .init = generic_client_init,
  1110. #if CONFIG_BLE_MESH_DEINIT
  1111. .deinit = generic_client_deinit,
  1112. #endif /* CONFIG_BLE_MESH_DEINIT */
  1113. };
  1114. #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */