mesh_node_demo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.h"
  47. const char * device_uuid_string = "001BDC0810210B0E0A0C000B0E0A0C00";
  48. // general
  49. #define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER 0x0000u
  50. static mesh_model_t mesh_vendor_model;
  51. static mesh_model_t mesh_generic_on_off_server_model;
  52. static mesh_generic_on_off_state_t mesh_generic_on_off_state;
  53. static char gap_name_buffer[30];
  54. static char gap_name_prefix[] = "Mesh ";
  55. static btstack_packet_callback_registration_t hci_event_callback_registration;
  56. #ifdef ENABLE_MESH_GATT_BEARER
  57. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  58. UNUSED(channel);
  59. UNUSED(size);
  60. bd_addr_t addr;
  61. switch (packet_type) {
  62. case HCI_EVENT_PACKET:
  63. switch (hci_event_packet_get_type(packet)) {
  64. case BTSTACK_EVENT_STATE:
  65. if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
  66. // setup gap name
  67. gap_local_bd_addr(addr);
  68. strcpy(gap_name_buffer, gap_name_prefix);
  69. strcat(gap_name_buffer, bd_addr_to_str(addr));
  70. break;
  71. default:
  72. break;
  73. }
  74. break;
  75. }
  76. }
  77. 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){
  78. UNUSED(connection_handle);
  79. if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){
  80. return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, strlen(gap_name_buffer), offset, buffer, buffer_size);
  81. }
  82. return 0;
  83. }
  84. #endif
  85. static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  86. UNUSED(packet_type);
  87. UNUSED(channel);
  88. UNUSED(size);
  89. if (packet_type != HCI_EVENT_PACKET) return;
  90. switch(packet[0]){
  91. case HCI_EVENT_MESH_META:
  92. switch(packet[2]){
  93. case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
  94. printf("Provisioner link opened");
  95. break;
  96. case MESH_SUBEVENT_ATTENTION_TIMER:
  97. printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet));
  98. break;
  99. case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
  100. printf("Provisioner link close");
  101. break;
  102. case MESH_SUBEVENT_PB_PROV_COMPLETE:
  103. printf("Provisioning complete\n");
  104. break;
  105. default:
  106. break;
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  114. UNUSED(channel);
  115. UNUSED(size);
  116. if (packet_type != HCI_EVENT_PACKET) return;
  117. switch(packet[0]){
  118. case HCI_EVENT_MESH_META:
  119. switch(packet[2]){
  120. case MESH_SUBEVENT_STATE_UPDATE_BOOL:
  121. printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n",
  122. mesh_subevent_state_update_bool_get_model_identifier(packet),
  123. mesh_subevent_state_update_bool_get_state_identifier(packet),
  124. mesh_subevent_state_update_bool_get_reason(packet),
  125. mesh_subevent_state_update_bool_get_value(packet));
  126. break;
  127. default:
  128. break;
  129. }
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. static void show_usage(void){
  136. bd_addr_t iut_address;
  137. gap_local_bd_addr(iut_address);
  138. printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address));
  139. printf("8 - Delete provisioning data\n");
  140. printf("g - Generic ON/OFF Server Toggle Value\n");
  141. printf("\n");
  142. }
  143. static void stdin_process(char cmd){
  144. switch (cmd){
  145. case '8':
  146. mesh_node_reset();
  147. printf("Mesh Node Reset!\n");
  148. mesh_proxy_start_advertising_unprovisioned_device();
  149. break;
  150. case 'g':
  151. printf("Generic ON/OFF Server Toggle Value\n");
  152. 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);
  153. break;
  154. case ' ':
  155. show_usage();
  156. break;
  157. default:
  158. printf("Command: '%c' not implemented\n", cmd);
  159. show_usage();
  160. break;
  161. }
  162. }
  163. static int scan_hex_byte(const char * byte_string){
  164. int upper_nibble = nibble_for_char(*byte_string++);
  165. if (upper_nibble < 0) return -1;
  166. int lower_nibble = nibble_for_char(*byte_string);
  167. if (lower_nibble < 0) return -1;
  168. return (upper_nibble << 4) | lower_nibble;
  169. }
  170. static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
  171. int i;
  172. for (i = 0; i < len; i++) {
  173. int single_byte = scan_hex_byte(string);
  174. if (single_byte < 0) return 0;
  175. string += 2;
  176. buffer[i] = (uint8_t)single_byte;
  177. // don't check seperator after last byte
  178. if (i == len - 1) {
  179. return 1;
  180. }
  181. // optional seperator
  182. char separator = *string;
  183. if (separator == ':' && separator == '-' && separator == ' ') {
  184. string++;
  185. }
  186. }
  187. return 1;
  188. }
  189. int btstack_main(void);
  190. int btstack_main(void)
  191. {
  192. #ifdef HAVE_BTSTACK_STDIN
  193. // console
  194. btstack_stdin_setup(stdin_process);
  195. #endif
  196. // crypto
  197. btstack_crypto_init();
  198. #ifdef ENABLE_MESH_GATT_BEARER
  199. // l2cap
  200. l2cap_init();
  201. // setup le device db
  202. le_device_db_init();
  203. // setup ATT server
  204. att_server_init(profile_data, &att_read_callback, NULL);
  205. //
  206. sm_init();
  207. #endif
  208. #ifdef ENABLE_MESH_GATT_BEARER
  209. // register for HCI events
  210. hci_event_callback_registration.callback = &packet_handler;
  211. hci_add_event_handler(&hci_event_callback_registration);
  212. #endif
  213. // mesh
  214. mesh_init();
  215. #ifdef ENABLE_MESH_GATT_BEARER
  216. // setup connectable advertisments
  217. bd_addr_t null_addr;
  218. memset(null_addr, 0, 6);
  219. uint8_t adv_type = 0; // AFV_IND
  220. uint16_t adv_int_min = 0x0030;
  221. uint16_t adv_int_max = 0x0030;
  222. adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  223. #endif
  224. // Track Provisioning as device role
  225. mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler);
  226. // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
  227. mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103);
  228. // Setup Generic On/Off model
  229. mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER);
  230. mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations();
  231. mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state;
  232. mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler);
  233. mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model);
  234. // Setup our custom model
  235. mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER);
  236. mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model);
  237. // Enable Output OOB
  238. provisioning_device_set_output_oob_actions(0x08, 0x08);
  239. // Enable PROXY
  240. mesh_foundation_gatt_proxy_set(1);
  241. #if defined(ENABLE_MESH_ADV_BEARER)
  242. // setup scanning when supporting ADV Bearer
  243. gap_set_scan_parameters(0, 0x300, 0x300);
  244. gap_start_scan();
  245. #endif
  246. uint8_t device_uuid[16];
  247. btstack_parse_hex(device_uuid_string, 16, device_uuid);
  248. mesh_node_set_device_uuid(device_uuid);
  249. // turn on!
  250. hci_power_control(HCI_POWER_ON);
  251. return 0;
  252. }
  253. /* EXAMPLE_END */