app_mesh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include "esp_log.h"
  20. #include "nvs_flash.h"
  21. #include "freertos/FreeRTOSConfig.h"
  22. /* BLE */
  23. #include "nimble/nimble_port.h"
  24. #include "nimble/nimble_port_freertos.h"
  25. #include "host/ble_hs.h"
  26. #include "host/util/util.h"
  27. #include "console/console.h"
  28. #include "services/gap/ble_svc_gap.h"
  29. #include "services/gatt/ble_svc_gatt.h"
  30. #include "mesh/mesh.h"
  31. static const char *tag = "NimBLE_MESH";
  32. void ble_store_config_init(void);
  33. #define BT_DBG_ENABLED (MYNEWT_VAL(BLE_MESH_DEBUG))
  34. /* Company ID */
  35. #define CID_VENDOR 0x05C3
  36. #define STANDARD_TEST_ID 0x00
  37. #define TEST_ID 0x01
  38. static int recent_test_id = STANDARD_TEST_ID;
  39. #define FAULT_ARR_SIZE 2
  40. static bool has_reg_fault = true;
  41. static int
  42. fault_get_cur(struct bt_mesh_model *model,
  43. uint8_t *test_id,
  44. uint16_t *company_id,
  45. uint8_t *faults,
  46. uint8_t *fault_count)
  47. {
  48. uint8_t reg_faults[FAULT_ARR_SIZE] = { [0 ... FAULT_ARR_SIZE - 1] = 0xff };
  49. ESP_LOGI(tag, "fault_get_cur() has_reg_fault %u\n", has_reg_fault);
  50. *test_id = recent_test_id;
  51. *company_id = CID_VENDOR;
  52. *fault_count = min(*fault_count, sizeof(reg_faults));
  53. memcpy(faults, reg_faults, *fault_count);
  54. return 0;
  55. }
  56. static int
  57. fault_get_reg(struct bt_mesh_model *model,
  58. uint16_t company_id,
  59. uint8_t *test_id,
  60. uint8_t *faults,
  61. uint8_t *fault_count)
  62. {
  63. if (company_id != CID_VENDOR) {
  64. return -BLE_HS_EINVAL;
  65. }
  66. ESP_LOGI(tag, "fault_get_reg() has_reg_fault %u\n", has_reg_fault);
  67. *test_id = recent_test_id;
  68. if (has_reg_fault) {
  69. uint8_t reg_faults[FAULT_ARR_SIZE] = { [0 ... FAULT_ARR_SIZE - 1] = 0xff };
  70. *fault_count = min(*fault_count, sizeof(reg_faults));
  71. memcpy(faults, reg_faults, *fault_count);
  72. } else {
  73. *fault_count = 0;
  74. }
  75. return 0;
  76. }
  77. static int
  78. fault_clear(struct bt_mesh_model *model, uint16_t company_id)
  79. {
  80. if (company_id != CID_VENDOR) {
  81. return -BLE_HS_EINVAL;
  82. }
  83. has_reg_fault = false;
  84. return 0;
  85. }
  86. static int
  87. fault_test(struct bt_mesh_model *model, uint8_t test_id, uint16_t company_id)
  88. {
  89. if (company_id != CID_VENDOR) {
  90. return -BLE_HS_EINVAL;
  91. }
  92. if (test_id != STANDARD_TEST_ID && test_id != TEST_ID) {
  93. return -BLE_HS_EINVAL;
  94. }
  95. recent_test_id = test_id;
  96. has_reg_fault = true;
  97. bt_mesh_fault_update(bt_mesh_model_elem(model));
  98. return 0;
  99. }
  100. static const struct bt_mesh_health_srv_cb health_srv_cb = {
  101. .fault_get_cur = &fault_get_cur,
  102. .fault_get_reg = &fault_get_reg,
  103. .fault_clear = &fault_clear,
  104. .fault_test = &fault_test,
  105. };
  106. static struct bt_mesh_health_srv health_srv = {
  107. .cb = &health_srv_cb,
  108. };
  109. static struct bt_mesh_model_pub health_pub;
  110. static void
  111. health_pub_init(void)
  112. {
  113. health_pub.msg = BT_MESH_HEALTH_FAULT_MSG(0);
  114. }
  115. static struct bt_mesh_model_pub gen_level_pub;
  116. static struct bt_mesh_model_pub gen_onoff_pub;
  117. static uint8_t gen_on_off_state;
  118. static int16_t gen_level_state;
  119. static void gen_onoff_status(struct bt_mesh_model *model,
  120. struct bt_mesh_msg_ctx *ctx)
  121. {
  122. struct os_mbuf *msg = NET_BUF_SIMPLE(3);
  123. uint8_t *status;
  124. ESP_LOGI(tag, "#mesh-onoff STATUS\n");
  125. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_2(0x82, 0x04));
  126. status = net_buf_simple_add(msg, 1);
  127. *status = gen_on_off_state;
  128. if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  129. ESP_LOGI(tag, "#mesh-onoff STATUS: send status failed\n");
  130. }
  131. os_mbuf_free_chain(msg);
  132. }
  133. static void gen_onoff_get(struct bt_mesh_model *model,
  134. struct bt_mesh_msg_ctx *ctx,
  135. struct os_mbuf *buf)
  136. {
  137. ESP_LOGI(tag, "#mesh-onoff GET\n");
  138. gen_onoff_status(model, ctx);
  139. }
  140. static void gen_onoff_set(struct bt_mesh_model *model,
  141. struct bt_mesh_msg_ctx *ctx,
  142. struct os_mbuf *buf)
  143. {
  144. ESP_LOGI(tag, "#mesh-onoff SET\n");
  145. gen_on_off_state = buf->om_data[0];
  146. gen_onoff_status(model, ctx);
  147. }
  148. static void gen_onoff_set_unack(struct bt_mesh_model *model,
  149. struct bt_mesh_msg_ctx *ctx,
  150. struct os_mbuf *buf)
  151. {
  152. ESP_LOGI(tag, "#mesh-onoff SET-UNACK\n");
  153. gen_on_off_state = buf->om_data[0];
  154. }
  155. static const struct bt_mesh_model_op gen_onoff_op[] = {
  156. { BT_MESH_MODEL_OP_2(0x82, 0x01), 0, gen_onoff_get },
  157. { BT_MESH_MODEL_OP_2(0x82, 0x02), 2, gen_onoff_set },
  158. { BT_MESH_MODEL_OP_2(0x82, 0x03), 2, gen_onoff_set_unack },
  159. BT_MESH_MODEL_OP_END,
  160. };
  161. static void gen_level_status(struct bt_mesh_model *model,
  162. struct bt_mesh_msg_ctx *ctx)
  163. {
  164. struct os_mbuf *msg = NET_BUF_SIMPLE(4);
  165. ESP_LOGI(tag, "#mesh-level STATUS\n");
  166. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_2(0x82, 0x08));
  167. net_buf_simple_add_le16(msg, gen_level_state);
  168. if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  169. ESP_LOGI(tag, "#mesh-level STATUS: send status failed\n");
  170. }
  171. os_mbuf_free_chain(msg);
  172. }
  173. static void gen_level_get(struct bt_mesh_model *model,
  174. struct bt_mesh_msg_ctx *ctx,
  175. struct os_mbuf *buf)
  176. {
  177. ESP_LOGI(tag, "#mesh-level GET\n");
  178. gen_level_status(model, ctx);
  179. }
  180. static void gen_level_set(struct bt_mesh_model *model,
  181. struct bt_mesh_msg_ctx *ctx,
  182. struct os_mbuf *buf)
  183. {
  184. int16_t level;
  185. level = (int16_t) net_buf_simple_pull_le16(buf);
  186. ESP_LOGI(tag, "#mesh-level SET: level=%d\n", level);
  187. gen_level_status(model, ctx);
  188. gen_level_state = level;
  189. ESP_LOGI(tag, "#mesh-level: level=%d\n", gen_level_state);
  190. }
  191. static void gen_level_set_unack(struct bt_mesh_model *model,
  192. struct bt_mesh_msg_ctx *ctx,
  193. struct os_mbuf *buf)
  194. {
  195. int16_t level;
  196. level = (int16_t) net_buf_simple_pull_le16(buf);
  197. ESP_LOGI(tag, "#mesh-level SET-UNACK: level=%d\n", level);
  198. gen_level_state = level;
  199. ESP_LOGI(tag, "#mesh-level: level=%d\n", gen_level_state);
  200. }
  201. static void gen_delta_set(struct bt_mesh_model *model,
  202. struct bt_mesh_msg_ctx *ctx,
  203. struct os_mbuf *buf)
  204. {
  205. int16_t delta_level;
  206. delta_level = (int16_t) net_buf_simple_pull_le16(buf);
  207. ESP_LOGI(tag, "#mesh-level DELTA-SET: delta_level=%d\n", delta_level);
  208. gen_level_status(model, ctx);
  209. gen_level_state += delta_level;
  210. ESP_LOGI(tag, "#mesh-level: level=%d\n", gen_level_state);
  211. }
  212. static void gen_delta_set_unack(struct bt_mesh_model *model,
  213. struct bt_mesh_msg_ctx *ctx,
  214. struct os_mbuf *buf)
  215. {
  216. int16_t delta_level;
  217. delta_level = (int16_t) net_buf_simple_pull_le16(buf);
  218. ESP_LOGI(tag, "#mesh-level DELTA-SET: delta_level=%d\n", delta_level);
  219. gen_level_state += delta_level;
  220. ESP_LOGI(tag, "#mesh-level: level=%d\n", gen_level_state);
  221. }
  222. static void gen_move_set(struct bt_mesh_model *model,
  223. struct bt_mesh_msg_ctx *ctx,
  224. struct os_mbuf *buf)
  225. {
  226. }
  227. static void gen_move_set_unack(struct bt_mesh_model *model,
  228. struct bt_mesh_msg_ctx *ctx,
  229. struct os_mbuf *buf)
  230. {
  231. }
  232. static const struct bt_mesh_model_op gen_level_op[] = {
  233. { BT_MESH_MODEL_OP_2(0x82, 0x05), 0, gen_level_get },
  234. { BT_MESH_MODEL_OP_2(0x82, 0x06), 3, gen_level_set },
  235. { BT_MESH_MODEL_OP_2(0x82, 0x07), 3, gen_level_set_unack },
  236. { BT_MESH_MODEL_OP_2(0x82, 0x09), 5, gen_delta_set },
  237. { BT_MESH_MODEL_OP_2(0x82, 0x0a), 5, gen_delta_set_unack },
  238. { BT_MESH_MODEL_OP_2(0x82, 0x0b), 3, gen_move_set },
  239. { BT_MESH_MODEL_OP_2(0x82, 0x0c), 3, gen_move_set_unack },
  240. BT_MESH_MODEL_OP_END,
  241. };
  242. static struct bt_mesh_model root_models[] = {
  243. BT_MESH_MODEL_CFG_SRV,
  244. BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub),
  245. BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_ONOFF_SRV, gen_onoff_op,
  246. &gen_onoff_pub, NULL),
  247. BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_LEVEL_SRV, gen_level_op,
  248. &gen_level_pub, NULL),
  249. };
  250. static struct bt_mesh_model_pub vnd_model_pub;
  251. static void vnd_model_recv(struct bt_mesh_model *model,
  252. struct bt_mesh_msg_ctx *ctx,
  253. struct os_mbuf *buf)
  254. {
  255. struct os_mbuf *msg = NET_BUF_SIMPLE(3);
  256. console_printf("#vendor-model-recv\n");
  257. console_printf("data:%s len:%d\n", bt_hex(buf->om_data, buf->om_len),
  258. buf->om_len);
  259. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_3(0x01, CID_VENDOR));
  260. os_mbuf_append(msg, buf->om_data, buf->om_len);
  261. if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  262. console_printf("#vendor-model-recv: send rsp failed\n");
  263. }
  264. os_mbuf_free_chain(msg);
  265. }
  266. static const struct bt_mesh_model_op vnd_model_op[] = {
  267. { BT_MESH_MODEL_OP_3(0x01, CID_VENDOR), 0, vnd_model_recv },
  268. BT_MESH_MODEL_OP_END,
  269. };
  270. static struct bt_mesh_model vnd_models[] = {
  271. BT_MESH_MODEL_VND(CID_VENDOR, BT_MESH_MODEL_ID_GEN_ONOFF_SRV, vnd_model_op,
  272. &vnd_model_pub, NULL),
  273. };
  274. static struct bt_mesh_elem elements[] = {
  275. BT_MESH_ELEM(0, root_models, vnd_models),
  276. };
  277. static const struct bt_mesh_comp comp = {
  278. .cid = CID_VENDOR,
  279. .elem = elements,
  280. .elem_count = ARRAY_SIZE(elements),
  281. };
  282. static int output_number(bt_mesh_output_action_t action, uint32_t number)
  283. {
  284. ESP_LOGI(tag, "OOB Number: %u\n", number);
  285. return 0;
  286. }
  287. static void prov_complete(uint16_t net_idx, uint16_t addr)
  288. {
  289. ESP_LOGI(tag, "Local node provisioned, primary address 0x%04x\n", addr);
  290. }
  291. static const uint8_t dev_uuid[16] = MYNEWT_VAL(BLE_MESH_DEV_UUID);
  292. static const struct bt_mesh_prov prov = {
  293. .uuid = dev_uuid,
  294. .output_size = 4,
  295. .output_actions = BT_MESH_DISPLAY_NUMBER | BT_MESH_BEEP | BT_MESH_VIBRATE | BT_MESH_BLINK,
  296. .output_number = output_number,
  297. .complete = prov_complete,
  298. };
  299. static void
  300. blemesh_on_reset(int reason)
  301. {
  302. BLE_HS_LOG(ERROR, "Resetting state; reason=%d\n", reason);
  303. }
  304. static void
  305. blemesh_on_sync(void)
  306. {
  307. int err;
  308. ble_addr_t addr;
  309. ESP_LOGI(tag, "Bluetooth initialized\n");
  310. /* Use NRPA */
  311. err = ble_hs_id_gen_rnd(1, &addr);
  312. assert(err == 0);
  313. err = ble_hs_id_set_rnd(addr.val);
  314. assert(err == 0);
  315. err = bt_mesh_init(addr.type, &prov, &comp);
  316. if (err) {
  317. ESP_LOGI(tag, "Initializing mesh failed (err %d)\n", err);
  318. return;
  319. }
  320. #if (MYNEWT_VAL(BLE_MESH_SHELL))
  321. shell_register_default_module("mesh");
  322. #endif
  323. ESP_LOGI(tag, "Mesh initialized\n");
  324. if (IS_ENABLED(CONFIG_SETTINGS)) {
  325. settings_load();
  326. }
  327. if (bt_mesh_is_provisioned()) {
  328. ESP_LOGI(tag, "Mesh network restored from flash\n");
  329. }
  330. }
  331. void blemesh_host_task(void *param)
  332. {
  333. ble_hs_cfg.reset_cb = blemesh_on_reset;
  334. ble_hs_cfg.sync_cb = blemesh_on_sync;
  335. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  336. health_pub_init();
  337. nimble_port_run();
  338. nimble_port_freertos_deinit();
  339. }
  340. void app_main(void)
  341. {
  342. /* Initialize NVS — it is used to store PHY calibration data */
  343. esp_err_t ret = nvs_flash_init();
  344. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  345. ESP_ERROR_CHECK(nvs_flash_erase());
  346. ret = nvs_flash_init();
  347. }
  348. ESP_ERROR_CHECK(ret);
  349. nimble_port_init();
  350. ble_svc_gap_init();
  351. ble_svc_gatt_init();
  352. bt_mesh_register_gatt();
  353. /* XXX Need to have template for store */
  354. ble_store_config_init();
  355. nimble_port_freertos_init(blemesh_host_task);
  356. }