mesh_node_demo_tmall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2019 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. /**
  38. * Basic Mesh Node demo
  39. */
  40. #define __BTSTACK_FILE__ "mesh_node_demo.c"
  41. #include <stdint.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "btstack.h"
  46. #include "mesh_node_demo_tmall.h"
  47. #include "rtthread.h"
  48. #include <drv_common.h>
  49. //const char * device_uuid_string = "001BDC0810210B0E0A0C000B0E0A0C00";
  50. const char * device_uuid_string = "A801B1773100002983497AFA28000000";
  51. // general
  52. #define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER 0x0000u
  53. static mesh_model_t mesh_vendor_model;
  54. static mesh_model_t mesh_generic_on_off_server_model;
  55. static mesh_generic_on_off_state_t mesh_generic_on_off_state;
  56. static char gap_name_buffer[30];
  57. static char gap_name_prefix[] = "Mesh ";
  58. static btstack_packet_callback_registration_t hci_event_callback_registration;
  59. #ifdef ENABLE_MESH_GATT_BEARER
  60. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  61. UNUSED(channel);
  62. UNUSED(size);
  63. bd_addr_t addr;
  64. switch (packet_type) {
  65. case HCI_EVENT_PACKET:
  66. switch (hci_event_packet_get_type(packet)) {
  67. case BTSTACK_EVENT_STATE:
  68. if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
  69. // setup gap name
  70. gap_local_bd_addr(addr);
  71. strcpy(gap_name_buffer, gap_name_prefix);
  72. strcat(gap_name_buffer, bd_addr_to_str(addr));
  73. break;
  74. default:
  75. break;
  76. }
  77. break;
  78. }
  79. }
  80. static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
  81. UNUSED(connection_handle);
  82. if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){
  83. return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, strlen(gap_name_buffer), offset, buffer, buffer_size);
  84. }
  85. return 0;
  86. }
  87. #endif
  88. #define MESH_BLUE_LED GET_PIN(I, 8)
  89. static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  90. UNUSED(packet_type);
  91. UNUSED(channel);
  92. UNUSED(size);
  93. if (packet_type != HCI_EVENT_PACKET) return;
  94. switch(packet[0]){
  95. case HCI_EVENT_MESH_META:
  96. switch(packet[2]){
  97. case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
  98. printf("Provisioner link opened");
  99. break;
  100. case MESH_SUBEVENT_ATTENTION_TIMER:
  101. printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet));
  102. break;
  103. case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
  104. printf("Provisioner link close");
  105. break;
  106. case MESH_SUBEVENT_PB_PROV_COMPLETE:
  107. printf("Provisioning complete\n");
  108. break;
  109. default:
  110. break;
  111. }
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  118. UNUSED(channel);
  119. UNUSED(size);
  120. uint8_t led_status;
  121. if (packet_type != HCI_EVENT_PACKET) return;
  122. switch(packet[0]){
  123. case HCI_EVENT_MESH_META:
  124. switch(packet[2]){
  125. case MESH_SUBEVENT_STATE_UPDATE_BOOL:
  126. printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n",
  127. mesh_subevent_state_update_bool_get_model_identifier(packet),
  128. mesh_subevent_state_update_bool_get_state_identifier(packet),
  129. mesh_subevent_state_update_bool_get_reason(packet),
  130. mesh_subevent_state_update_bool_get_value(packet));
  131. led_status = mesh_subevent_state_update_bool_get_value(packet);
  132. if(led_status)
  133. {
  134. printf("\r\n====POWER ON LED===\r\n");
  135. rt_pin_write(MESH_BLUE_LED, 0);
  136. }
  137. else {
  138. printf("\r\n====POWER OFF LED===\r\n");
  139. rt_pin_write(MESH_BLUE_LED, 1);
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. static void show_usage(void){
  151. bd_addr_t iut_address;
  152. gap_local_bd_addr(iut_address);
  153. printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address));
  154. printf("8 - Delete provisioning data\n");
  155. printf("g - Generic ON/OFF Server Toggle Value\n");
  156. printf("\n");
  157. }
  158. static void stdin_process(char cmd){
  159. switch (cmd){
  160. case '8':
  161. mesh_node_reset();
  162. printf("Mesh Node Reset!\n");
  163. mesh_proxy_start_advertising_unprovisioned_device();
  164. break;
  165. case 'g':
  166. printf("Generic ON/OFF Server Toggle Value\n");
  167. mesh_generic_on_off_server_set(&mesh_generic_on_off_server_model, 1-mesh_generic_on_off_server_get(&mesh_generic_on_off_server_model), 0, 0);
  168. break;
  169. case ' ':
  170. show_usage();
  171. break;
  172. default:
  173. printf("Command: '%c' not implemented\n", cmd);
  174. show_usage();
  175. break;
  176. }
  177. }
  178. static int scan_hex_byte(const char * byte_string){
  179. int upper_nibble = nibble_for_char(*byte_string++);
  180. if (upper_nibble < 0) return -1;
  181. int lower_nibble = nibble_for_char(*byte_string);
  182. if (lower_nibble < 0) return -1;
  183. return (upper_nibble << 4) | lower_nibble;
  184. }
  185. static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
  186. int i;
  187. for (i = 0; i < len; i++) {
  188. int single_byte = scan_hex_byte(string);
  189. if (single_byte < 0) return 0;
  190. string += 2;
  191. buffer[i] = (uint8_t)single_byte;
  192. // don't check seperator after last byte
  193. if (i == len - 1) {
  194. return 1;
  195. }
  196. // optional seperator
  197. char separator = *string;
  198. if (separator == ':' && separator == '-' && separator == ' ') {
  199. string++;
  200. }
  201. }
  202. return 1;
  203. }
  204. //static const char * prov_static_oob_string = "00000000000000000102030405060708";
  205. static const char * prov_static_oob_string = "05640E9EBC91EF58500A1B9B0B963CB7";
  206. static uint8_t prov_static_oob_data[16];
  207. int btstack_main(void);
  208. int btstack_main(void)
  209. {
  210. #ifdef HAVE_BTSTACK_STDIN
  211. // console
  212. btstack_stdin_setup(stdin_process);
  213. #endif
  214. // crypto
  215. btstack_crypto_init();
  216. #ifdef ENABLE_MESH_GATT_BEARER
  217. // l2cap
  218. l2cap_init();
  219. // setup le device db
  220. le_device_db_init();
  221. // setup ATT server
  222. att_server_init(profile_data, &att_read_callback, NULL);
  223. //
  224. sm_init();
  225. #endif
  226. #ifdef ENABLE_MESH_GATT_BEARER
  227. // register for HCI events
  228. hci_event_callback_registration.callback = &packet_handler;
  229. hci_add_event_handler(&hci_event_callback_registration);
  230. #endif
  231. // mesh
  232. mesh_init();
  233. #ifdef ENABLE_MESH_GATT_BEARER
  234. // setup connectable advertisments
  235. bd_addr_t null_addr;
  236. memset(null_addr, 0, 6);
  237. uint8_t adv_type = 0; // AFV_IND
  238. uint16_t adv_int_min = 0x0030;
  239. uint16_t adv_int_max = 0x0030;
  240. adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  241. #endif
  242. // Track Provisioning as device role
  243. mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler);
  244. // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
  245. mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103);
  246. // Setup Generic On/Off model
  247. mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER);
  248. mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations();
  249. mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state;
  250. mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler);
  251. mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model);
  252. // Setup our custom model
  253. mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER);
  254. mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model);
  255. // Enable Output OOB
  256. // provisioning_device_set_output_oob_actions(0x08, 0x08);
  257. printf("+ Static OOB Enabled\n");
  258. btstack_parse_hex(prov_static_oob_string, 16, prov_static_oob_data);
  259. provisioning_device_set_static_oob(16, prov_static_oob_data);
  260. // Enable PROXY
  261. mesh_foundation_gatt_proxy_set(1);
  262. #if defined(ENABLE_MESH_ADV_BEARER)
  263. // setup scanning when supporting ADV Bearer
  264. gap_set_scan_parameters(0, 0x300, 0x300);
  265. gap_start_scan();
  266. #endif
  267. uint8_t device_uuid[16];
  268. btstack_parse_hex(device_uuid_string, 16, device_uuid);
  269. mesh_node_set_device_uuid(device_uuid);
  270. // turn on!
  271. hci_power_control(HCI_POWER_ON);
  272. return 0;
  273. }
  274. /* EXAMPLE_END */