generic_client.c 46 KB

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