app_mesh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #ifndef min
  41. #define min(a, b) ((a) < (b) ? (a) : (b))
  42. #endif
  43. #ifndef max
  44. #define max(a, b) ((a) > (b) ? (a) : (b))
  45. #endif
  46. static bool has_reg_fault = true;
  47. static int
  48. fault_get_cur(struct bt_mesh_model *model,
  49. uint8_t *test_id,
  50. uint16_t *company_id,
  51. uint8_t *faults,
  52. uint8_t *fault_count)
  53. {
  54. uint8_t reg_faults[FAULT_ARR_SIZE] = { [0 ... FAULT_ARR_SIZE - 1] = 0xff };
  55. ESP_LOGI(tag, "fault_get_cur() has_reg_fault %u", has_reg_fault);
  56. *test_id = recent_test_id;
  57. *company_id = CID_VENDOR;
  58. *fault_count = min(*fault_count, sizeof(reg_faults));
  59. memcpy(faults, reg_faults, *fault_count);
  60. return 0;
  61. }
  62. static int
  63. fault_get_reg(struct bt_mesh_model *model,
  64. uint16_t company_id,
  65. uint8_t *test_id,
  66. uint8_t *faults,
  67. uint8_t *fault_count)
  68. {
  69. if (company_id != CID_VENDOR) {
  70. return -BLE_HS_EINVAL;
  71. }
  72. ESP_LOGI(tag, "fault_get_reg() has_reg_fault %u", has_reg_fault);
  73. *test_id = recent_test_id;
  74. if (has_reg_fault) {
  75. uint8_t reg_faults[FAULT_ARR_SIZE] = { [0 ... FAULT_ARR_SIZE - 1] = 0xff };
  76. *fault_count = min(*fault_count, sizeof(reg_faults));
  77. memcpy(faults, reg_faults, *fault_count);
  78. } else {
  79. *fault_count = 0;
  80. }
  81. return 0;
  82. }
  83. static int
  84. fault_clear(struct bt_mesh_model *model, uint16_t company_id)
  85. {
  86. if (company_id != CID_VENDOR) {
  87. return -BLE_HS_EINVAL;
  88. }
  89. has_reg_fault = false;
  90. return 0;
  91. }
  92. static int
  93. fault_test(struct bt_mesh_model *model, uint8_t test_id, uint16_t company_id)
  94. {
  95. if (company_id != CID_VENDOR) {
  96. return -BLE_HS_EINVAL;
  97. }
  98. if (test_id != STANDARD_TEST_ID && test_id != TEST_ID) {
  99. return -BLE_HS_EINVAL;
  100. }
  101. recent_test_id = test_id;
  102. has_reg_fault = true;
  103. bt_mesh_fault_update(bt_mesh_model_elem(model));
  104. return 0;
  105. }
  106. static const struct bt_mesh_health_srv_cb health_srv_cb = {
  107. .fault_get_cur = &fault_get_cur,
  108. .fault_get_reg = &fault_get_reg,
  109. .fault_clear = &fault_clear,
  110. .fault_test = &fault_test,
  111. };
  112. static struct bt_mesh_health_srv health_srv = {
  113. .cb = &health_srv_cb,
  114. };
  115. static struct bt_mesh_model_pub health_pub;
  116. static void
  117. health_pub_init(void)
  118. {
  119. health_pub.msg = BT_MESH_HEALTH_FAULT_MSG(0);
  120. }
  121. static struct bt_mesh_model_pub gen_level_pub;
  122. static struct bt_mesh_model_pub gen_onoff_pub;
  123. static uint8_t gen_on_off_state;
  124. static int16_t gen_level_state;
  125. static void gen_onoff_status(struct bt_mesh_model *model,
  126. struct bt_mesh_msg_ctx *ctx)
  127. {
  128. struct os_mbuf *msg = NET_BUF_SIMPLE(3);
  129. uint8_t *status;
  130. ESP_LOGI(tag, "#mesh-onoff STATUS");
  131. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_2(0x82, 0x04));
  132. status = net_buf_simple_add(msg, 1);
  133. *status = gen_on_off_state;
  134. if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  135. ESP_LOGI(tag, "#mesh-onoff STATUS: send status failed");
  136. }
  137. os_mbuf_free_chain(msg);
  138. }
  139. static void gen_onoff_get(struct bt_mesh_model *model,
  140. struct bt_mesh_msg_ctx *ctx,
  141. struct os_mbuf *buf)
  142. {
  143. ESP_LOGI(tag, "#mesh-onoff GET");
  144. gen_onoff_status(model, ctx);
  145. }
  146. static void gen_onoff_set(struct bt_mesh_model *model,
  147. struct bt_mesh_msg_ctx *ctx,
  148. struct os_mbuf *buf)
  149. {
  150. ESP_LOGI(tag, "#mesh-onoff SET");
  151. gen_on_off_state = buf->om_data[0];
  152. gen_onoff_status(model, ctx);
  153. }
  154. static void gen_onoff_set_unack(struct bt_mesh_model *model,
  155. struct bt_mesh_msg_ctx *ctx,
  156. struct os_mbuf *buf)
  157. {
  158. ESP_LOGI(tag, "#mesh-onoff SET-UNACK");
  159. gen_on_off_state = buf->om_data[0];
  160. }
  161. static const struct bt_mesh_model_op gen_onoff_op[] = {
  162. { BT_MESH_MODEL_OP_2(0x82, 0x01), 0, (void *)gen_onoff_get },
  163. { BT_MESH_MODEL_OP_2(0x82, 0x02), 2, (void *)gen_onoff_set },
  164. { BT_MESH_MODEL_OP_2(0x82, 0x03), 2, (void *)gen_onoff_set_unack },
  165. BT_MESH_MODEL_OP_END,
  166. };
  167. static void gen_level_status(struct bt_mesh_model *model,
  168. struct bt_mesh_msg_ctx *ctx)
  169. {
  170. struct os_mbuf *msg = NET_BUF_SIMPLE(4);
  171. ESP_LOGI(tag, "#mesh-level STATUS");
  172. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_2(0x82, 0x08));
  173. net_buf_simple_add_le16(msg, gen_level_state);
  174. if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  175. ESP_LOGI(tag, "#mesh-level STATUS: send status failed");
  176. }
  177. os_mbuf_free_chain(msg);
  178. }
  179. static void gen_level_get(struct bt_mesh_model *model,
  180. struct bt_mesh_msg_ctx *ctx,
  181. struct os_mbuf *buf)
  182. {
  183. ESP_LOGI(tag, "#mesh-level GET");
  184. gen_level_status(model, ctx);
  185. }
  186. static void gen_level_set(struct bt_mesh_model *model,
  187. struct bt_mesh_msg_ctx *ctx,
  188. struct os_mbuf *buf)
  189. {
  190. int16_t level;
  191. level = (int16_t) net_buf_simple_pull_le16(buf);
  192. ESP_LOGI(tag, "#mesh-level SET: level=%d", level);
  193. gen_level_status(model, ctx);
  194. gen_level_state = level;
  195. ESP_LOGI(tag, "#mesh-level: level=%d", gen_level_state);
  196. }
  197. static void gen_level_set_unack(struct bt_mesh_model *model,
  198. struct bt_mesh_msg_ctx *ctx,
  199. struct os_mbuf *buf)
  200. {
  201. int16_t level;
  202. level = (int16_t) net_buf_simple_pull_le16(buf);
  203. ESP_LOGI(tag, "#mesh-level SET-UNACK: level=%d", level);
  204. gen_level_state = level;
  205. ESP_LOGI(tag, "#mesh-level: level=%d", gen_level_state);
  206. }
  207. static void gen_delta_set(struct bt_mesh_model *model,
  208. struct bt_mesh_msg_ctx *ctx,
  209. struct os_mbuf *buf)
  210. {
  211. int16_t delta_level;
  212. delta_level = (int16_t) net_buf_simple_pull_le16(buf);
  213. ESP_LOGI(tag, "#mesh-level DELTA-SET: delta_level=%d", delta_level);
  214. gen_level_status(model, ctx);
  215. gen_level_state += delta_level;
  216. ESP_LOGI(tag, "#mesh-level: level=%d", gen_level_state);
  217. }
  218. static void gen_delta_set_unack(struct bt_mesh_model *model,
  219. struct bt_mesh_msg_ctx *ctx,
  220. struct os_mbuf *buf)
  221. {
  222. int16_t delta_level;
  223. delta_level = (int16_t) net_buf_simple_pull_le16(buf);
  224. ESP_LOGI(tag, "#mesh-level DELTA-SET: delta_level=%d", delta_level);
  225. gen_level_state += delta_level;
  226. ESP_LOGI(tag, "#mesh-level: level=%d", gen_level_state);
  227. }
  228. static void gen_move_set(struct bt_mesh_model *model,
  229. struct bt_mesh_msg_ctx *ctx,
  230. struct os_mbuf *buf)
  231. {
  232. }
  233. static void gen_move_set_unack(struct bt_mesh_model *model,
  234. struct bt_mesh_msg_ctx *ctx,
  235. struct os_mbuf *buf)
  236. {
  237. }
  238. static const struct bt_mesh_model_op gen_level_op[] = {
  239. { BT_MESH_MODEL_OP_2(0x82, 0x05), 0, (void *)gen_level_get },
  240. { BT_MESH_MODEL_OP_2(0x82, 0x06), 3, (void *)gen_level_set },
  241. { BT_MESH_MODEL_OP_2(0x82, 0x07), 3, (void *)gen_level_set_unack },
  242. { BT_MESH_MODEL_OP_2(0x82, 0x09), 5, (void *)gen_delta_set },
  243. { BT_MESH_MODEL_OP_2(0x82, 0x0a), 5, (void *)gen_delta_set_unack },
  244. { BT_MESH_MODEL_OP_2(0x82, 0x0b), 3, (void *)gen_move_set },
  245. { BT_MESH_MODEL_OP_2(0x82, 0x0c), 3, (void *)gen_move_set_unack },
  246. BT_MESH_MODEL_OP_END,
  247. };
  248. static struct bt_mesh_model root_models[] = {
  249. BT_MESH_MODEL_CFG_SRV,
  250. BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub),
  251. BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_ONOFF_SRV, gen_onoff_op,
  252. &gen_onoff_pub, NULL),
  253. BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_LEVEL_SRV, gen_level_op,
  254. &gen_level_pub, NULL),
  255. };
  256. static struct bt_mesh_model_pub vnd_model_pub;
  257. static void vnd_model_recv(struct bt_mesh_model *model,
  258. struct bt_mesh_msg_ctx *ctx,
  259. struct os_mbuf *buf)
  260. {
  261. struct os_mbuf *msg = NET_BUF_SIMPLE(3);
  262. console_printf("#vendor-model-recv\n");
  263. console_printf("data:%s len:%d\n", bt_hex(buf->om_data, buf->om_len),
  264. buf->om_len);
  265. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_3(0x01, CID_VENDOR));
  266. os_mbuf_append(msg, buf->om_data, buf->om_len);
  267. if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  268. console_printf("#vendor-model-recv: send rsp failed\n");
  269. }
  270. os_mbuf_free_chain(msg);
  271. }
  272. static struct bt_mesh_model_op vnd_model_op[] = {
  273. { BT_MESH_MODEL_OP_3(0x01, CID_VENDOR), 0, (void *)vnd_model_recv },
  274. BT_MESH_MODEL_OP_END,
  275. };
  276. static struct bt_mesh_model vnd_models[] = {
  277. BT_MESH_MODEL_VND(CID_VENDOR, BT_MESH_MODEL_ID_GEN_ONOFF_SRV, vnd_model_op,
  278. &vnd_model_pub, NULL),
  279. };
  280. static struct bt_mesh_elem elements[] = {
  281. BT_MESH_ELEM(0, root_models, vnd_models),
  282. };
  283. static const struct bt_mesh_comp comp = {
  284. .cid = CID_VENDOR,
  285. .elem = elements,
  286. .elem_count = ARRAY_SIZE(elements),
  287. };
  288. static int output_number(bt_mesh_output_action_t action, uint32_t number)
  289. {
  290. ESP_LOGI(tag, "OOB Number: %" PRIu32, number);
  291. return 0;
  292. }
  293. static void prov_complete(uint16_t net_idx, uint16_t addr)
  294. {
  295. ESP_LOGI(tag, "Local node provisioned, primary address 0x%04x", addr);
  296. }
  297. static const uint8_t dev_uuid[16] = MYNEWT_VAL(BLE_MESH_DEV_UUID);
  298. static const struct bt_mesh_prov prov = {
  299. .uuid = dev_uuid,
  300. .output_size = 4,
  301. .output_actions = BT_MESH_DISPLAY_NUMBER | BT_MESH_BEEP | BT_MESH_VIBRATE | BT_MESH_BLINK,
  302. .output_number = output_number,
  303. .complete = prov_complete,
  304. };
  305. static void
  306. blemesh_on_reset(int reason)
  307. {
  308. BLE_HS_LOG(ERROR, "Resetting state; reason=%d\n", reason);
  309. }
  310. static void
  311. blemesh_on_sync(void)
  312. {
  313. int err;
  314. ble_addr_t addr;
  315. ESP_LOGI(tag, "Bluetooth initialized");
  316. /* Use NRPA */
  317. err = ble_hs_id_gen_rnd(1, &addr);
  318. assert(err == 0);
  319. err = ble_hs_id_set_rnd(addr.val);
  320. assert(err == 0);
  321. err = bt_mesh_init(addr.type, &prov, &comp);
  322. if (err) {
  323. ESP_LOGI(tag, "Initializing mesh failed (err %d)", err);
  324. return;
  325. }
  326. #if (MYNEWT_VAL(BLE_MESH_SHELL))
  327. shell_register_default_module("mesh");
  328. #endif
  329. ESP_LOGI(tag, "Mesh initialized");
  330. if (IS_ENABLED(CONFIG_SETTINGS)) {
  331. settings_load();
  332. }
  333. if (bt_mesh_is_provisioned()) {
  334. ESP_LOGI(tag, "Mesh network restored from flash");
  335. }
  336. }
  337. void blemesh_host_task(void *param)
  338. {
  339. ble_hs_cfg.reset_cb = blemesh_on_reset;
  340. ble_hs_cfg.sync_cb = blemesh_on_sync;
  341. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  342. health_pub_init();
  343. nimble_port_run();
  344. nimble_port_freertos_deinit();
  345. }
  346. void app_main(void)
  347. {
  348. /* Initialize NVS — it is used to store PHY calibration data */
  349. esp_err_t ret = nvs_flash_init();
  350. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  351. ESP_ERROR_CHECK(nvs_flash_erase());
  352. ret = nvs_flash_init();
  353. }
  354. ESP_ERROR_CHECK(ret);
  355. ret = nimble_port_init();
  356. if (ret != ESP_OK) {
  357. ESP_LOGE(tag, "Failed to init nimble %d ", ret);
  358. return;
  359. }
  360. ble_svc_gap_init();
  361. ble_svc_gatt_init();
  362. bt_mesh_register_gatt();
  363. /* XXX Need to have template for store */
  364. ble_store_config_init();
  365. nimble_port_freertos_init(blemesh_host_task);
  366. }