xref: /btstack/test/mesh/mesh_pts.c (revision 3c4cc6427fe05577c00b7d2593f58c7abcf9eab7)
1 /*
2  * Copyright (C) 2014 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  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "mesh_pts.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 #include "mesh_pts.h"
47 
48 // general
49 static void show_usage(void);
50 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
51 static void mesh_pts_dump_mesh_options(void);
52 
53 #define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER   0x0000u
54 
55 static btstack_packet_callback_registration_t hci_event_callback_registration;
56 static int provisioned;
57 
58 static mesh_model_t                 mesh_vendor_model;
59 
60 static mesh_model_t                 mesh_generic_on_off_server_model;
61 static mesh_generic_on_off_state_t  mesh_generic_on_off_state;
62 
63 static char gap_name_buffer[30];
64 static char gap_name_prefix[] = "Mesh ";
65 
66 // pts add-on
67 #define PTS_DEFAULT_TTL 10
68 
69 static int      pts_type;
70 
71 static mesh_virtual_address_t * pts_virtual_addresss;
72 
73 const char * pts_device_uuid_string = "001BDC0810210B0E0A0C000B0E0A0C00";
74 
75 static uint8_t      prov_static_oob_data[16];
76 static const char * prov_static_oob_string = "00000000000000000102030405060708";
77 
78 static uint8_t      prov_public_key_data[64];
79 static const char * prov_public_key_string = "F465E43FF23D3F1B9DC7DFC04DA8758184DBC966204796ECCF0D6CF5E16500CC0201D048BCBBD899EEEFC424164E33C201C2B010CA6B4D43A8A155CAD8ECB279";
80 static uint8_t      prov_private_key_data[32];
81 static const char * prov_private_key_string = "529AA0670D72CD6497502ED473502B037E8803B5C60829A5A3CAA219505530BA";
82 
83 static mesh_transport_key_t pts_application_key;
84 
85 // pin entry (pts)
86 static int ui_chars_for_pin;
87 static uint8_t ui_pin[17];
88 static int ui_pin_offset;
89 
90 static mesh_publication_model_t generic_on_off_server_publication;
91 
92 static void mesh_provisioning_dump(const mesh_provisioning_data_t * data){
93     mesh_network_key_t * key = data->network_key;
94     printf("UnicastAddr:   0x%02x\n", data->unicast_address);
95     printf("DevKey:        "); printf_hexdump(data->device_key, 16);
96     printf("IV Index:      0x%08x\n", data->iv_index);
97     printf("Flags:               0x%02x\n", data->flags);
98     printf("NetKey:        "); printf_hexdump(key->net_key, 16);
99     printf("-- Derived from NetKey --\n");
100     printf("NID:           0x%02x\n", key->nid);
101     printf("NetworkID:     "); printf_hexdump(key->network_id, 8);
102     printf("BeaconKey:     "); printf_hexdump(key->beacon_key, 16);
103     printf("EncryptionKey: "); printf_hexdump(key->encryption_key, 16);
104     printf("PrivacyKey:    "); printf_hexdump(key->privacy_key, 16);
105     printf("IdentityKey:   "); printf_hexdump(key->identity_key, 16);
106 }
107 
108 
109 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
110     UNUSED(channel);
111     UNUSED(size);
112     bd_addr_t addr;
113     int i;
114 
115     switch (packet_type) {
116         case HCI_EVENT_PACKET:
117             switch (hci_event_packet_get_type(packet)) {
118                 case BTSTACK_EVENT_STATE:
119                     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
120                     // dump bd_addr in pts format
121                     gap_local_bd_addr(addr);
122                     printf("Local addr: %s - ", bd_addr_to_str(addr));
123                     for (i=0;i<6;i++) {
124                         printf("%02x", addr[i]);
125                     }
126                     printf("\n");
127 
128                     // setup gap name
129                     strcpy(gap_name_buffer, gap_name_prefix);
130                     strcat(gap_name_buffer, bd_addr_to_str(addr));
131 
132                     // dump PTS MeshOptions.ini
133                     mesh_pts_dump_mesh_options();
134                     break;
135                 default:
136                     break;
137             }
138             break;
139     }
140 }
141 
142 static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
143     mesh_provisioning_data_t provisioning_data;
144 
145     switch(packet[0]){
146         case HCI_EVENT_MESH_META:
147             switch(packet[2]){
148                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
149                     printf("Provisioner link opened");
150                     break;
151                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
152                     printf("Provisioner link close");
153                     break;
154                 case MESH_SUBEVENT_ATTENTION_TIMER:
155                     printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet));
156                     break;
157                 case MESH_SUBEVENT_PB_PROV_INPUT_OOB_REQUEST:
158                     printf("Enter passphrase: ");
159                     fflush(stdout);
160                     ui_chars_for_pin = 1;
161                     ui_pin_offset = 0;
162                     break;
163                 case MESH_SUBEVENT_PB_PROV_COMPLETE:
164                     printf("Provisioning complete\n");
165                     // dump provisioning data
166                     provisioning_device_data_get(&provisioning_data);
167                     mesh_provisioning_dump(&provisioning_data);
168                     provisioned = 1;
169                     break;
170                 default:
171                     break;
172             }
173             break;
174         default:
175             break;
176     }
177 }
178 
179 static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
180     if (packet_type != HCI_EVENT_PACKET) return;
181 
182     switch(packet[0]){
183         case HCI_EVENT_MESH_META:
184             switch(packet[2]){
185                 case MESH_SUBEVENT_STATE_UPDATE_BOOL:
186                     printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n",
187                         mesh_subevent_state_update_bool_get_model_identifier(packet),
188                         mesh_subevent_state_update_bool_get_state_identifier(packet),
189                         mesh_subevent_state_update_bool_get_reason(packet),
190                         mesh_subevent_state_update_bool_get_value(packet));
191                     break;
192                 default:
193                     break;
194             }
195             break;
196         default:
197             break;
198     }
199 }
200 
201 // PTS
202 
203 // helper network layer, temp
204 static void mesh_pts_received_network_message(mesh_network_callback_type_t callback_type, mesh_network_pdu_t *network_pdu){
205     switch (callback_type){
206         case MESH_NETWORK_PDU_RECEIVED:
207             printf("Received network message. SRC %04x, DST %04x, SEQ %04x\n",
208                    mesh_network_src(network_pdu), mesh_network_dst(network_pdu),  mesh_network_seq(network_pdu));
209             printf_hexdump(mesh_network_pdu_data(network_pdu), mesh_network_pdu_len(network_pdu));
210             mesh_network_message_processed_by_higher_layer(network_pdu);
211             break;
212         default:
213             break;
214     }
215 }
216 
217 static uint8_t mesh_network_send(uint8_t ttl, uint16_t dest, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len){
218 
219     uint16_t netkey_index = 0;
220     uint8_t  ctl = 0;
221     uint16_t src = mesh_node_get_primary_element_address();
222     uint32_t seq = mesh_sequence_number_next();
223 
224     // "3.4.5.2: The output filter of the interface connected to advertising or GATT bearers shall drop all messages with TTL value set to 1."
225     // if (ttl <= 1) return 0;
226 
227     // TODO: check transport_pdu_len depending on ctl
228 
229     // lookup network by netkey_index
230     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
231     if (!network_key) return 0;
232 
233     // allocate network_pdu
234     mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
235     if (!network_pdu) return 0;
236 
237     // setup network_pdu
238     mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, ctl, ttl, seq, src, dest, transport_pdu_data, transport_pdu_len);
239 
240     // send network_pdu
241     mesh_lower_transport_send_pdu((mesh_pdu_t *) network_pdu);
242     return 0;
243 }
244 
245 static void printf_hex(const uint8_t * data, uint16_t len){
246     while (len){
247         printf("%02x", *data);
248         data++;
249         len--;
250     }
251     printf("\n");
252 }
253 
254 static void mesh_pts_dump_mesh_options(void){
255     printf("\nMeshOptions.ini - into 'My Decoders' of Bluetooth Protocol Viewer\n");
256 
257     printf("[mesh]\n");
258 
259     printf("{IVindex}\n");
260     printf("%08x\n", mesh_get_iv_index());
261 
262     mesh_network_key_t * network_key = mesh_network_key_list_get(0);
263     if (network_key){
264         printf("{NetKey}\n");
265         printf_hex(network_key->net_key, 16);
266     }
267 
268     mesh_transport_key_t * transport_key = mesh_transport_key_get(0);
269     if (transport_key){
270         printf("{AppKey}\n");
271         printf_hex(transport_key->key, 16);
272     }
273 
274     mesh_transport_key_t * device_key = mesh_transport_key_get(MESH_DEVICE_KEY_INDEX);
275     if (device_key){
276         printf("{DevKey}\n");
277         printf_hex(device_key->key, 16);
278     }
279     printf("\n");
280 }
281 
282 static void mesh_unprovisioned_beacon_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
283     if (packet_type != MESH_BEACON_PACKET) return;
284     uint8_t  device_uuid[16];
285     uint16_t oob;
286     memcpy(device_uuid, &packet[1], 16);
287     oob = big_endian_read_16(packet, 17);
288     printf("received unprovisioned device beacon, oob data %x, device uuid: ", oob);
289     printf_hexdump(device_uuid, 16);
290     pb_adv_create_link(device_uuid);
291 }
292 
293 static int scan_hex_byte(const char * byte_string){
294     int upper_nibble = nibble_for_char(*byte_string++);
295     if (upper_nibble < 0) return -1;
296     int lower_nibble = nibble_for_char(*byte_string);
297     if (lower_nibble < 0) return -1;
298     return (upper_nibble << 4) | lower_nibble;
299 }
300 
301 static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
302     int i;
303     for (i = 0; i < len; i++) {
304         int single_byte = scan_hex_byte(string);
305         if (single_byte < 0) return 0;
306         string += 2;
307         buffer[i] = (uint8_t)single_byte;
308         // don't check seperator after last byte
309         if (i == len - 1) {
310             return 1;
311         }
312         // optional seperator
313         char separator = *string;
314         if (separator == ':' && separator == '-' && separator == ' ') {
315             string++;
316         }
317     }
318     return 1;
319 }
320 
321 static void btstack_print_hex(const uint8_t * data, uint16_t len, char separator){
322     int i;
323     for (i=0;i<len;i++){
324         printf("%02x", data[i]);
325         if (separator){
326             printf("%c", separator);
327         }
328     }
329     printf("\n");
330 }
331 
332 static void load_pts_app_key(void){
333     // PTS app key
334     btstack_parse_hex("3216D1509884B533248541792B877F98", 16, pts_application_key.key);
335     pts_application_key.aid = 0x38;
336     pts_application_key.internal_index = mesh_transport_key_get_free_index();
337     mesh_transport_key_add(&pts_application_key);
338     printf("PTS Application Key (AID %02x): ", 0x38);
339     printf_hexdump(pts_application_key.key, 16);
340 }
341 static void send_pts_network_messsage(const char * dst_type, uint16_t dst_addr, int ttl_type){
342     uint8_t ttl;
343     switch (ttl_type){
344         case 0:
345             ttl = 0;
346             break;
347         case 1:
348             ttl = PTS_DEFAULT_TTL;
349             break;
350         default:
351             ttl = 0x7f;
352             break;
353     }
354     printf("%s dst %04x, ttl %u\n", dst_type, dst_addr, ttl);
355     int lower_transport_pdu_len = 16;
356     uint8_t lower_transport_pdu_data[16];
357     memset(lower_transport_pdu_data, 0x55, lower_transport_pdu_len);
358     mesh_network_send(ttl, dst_addr, lower_transport_pdu_data, lower_transport_pdu_len);
359 }
360 
361 static void send_pts_unsegmented_access_messsage(void){
362     uint8_t access_pdu_data[16];
363 
364     load_pts_app_key();
365 
366     uint16_t src = mesh_node_get_primary_element_address();
367     uint16_t dest = 0x0001;
368     uint8_t  ttl = PTS_DEFAULT_TTL;
369 
370     int access_pdu_len = 1;
371     memset(access_pdu_data, 0x55, access_pdu_len);
372     uint16_t netkey_index = 0;
373     uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX;
374 
375     // send as unsegmented access pdu
376     mesh_pdu_t * pdu = (mesh_pdu_t*) mesh_network_pdu_get();
377     int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
378     if (status) return;
379     mesh_access_send_unacknowledged_pdu(pdu);
380 }
381 
382 static void send_pts_segmented_access_messsage_unicast(void){
383     uint8_t access_pdu_data[20];
384 
385     load_pts_app_key();
386 
387     uint16_t src = mesh_node_get_primary_element_address();
388     uint16_t dest = 0x0001;
389     uint8_t  ttl = PTS_DEFAULT_TTL;
390 
391     int access_pdu_len = 20;
392     memset(access_pdu_data, 0x55, access_pdu_len);
393     uint16_t netkey_index = 0;
394     uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX;
395 
396     // send as segmented access pdu
397     mesh_pdu_t * pdu = (mesh_pdu_t *) mesh_transport_pdu_get();
398     int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
399     if (status) return;
400     mesh_access_send_unacknowledged_pdu(pdu);
401 }
402 
403 static void send_pts_segmented_access_messsage_group(void){
404     uint8_t access_pdu_data[20];
405 
406     load_pts_app_key();
407 
408     uint16_t src = mesh_node_get_primary_element_address();
409     uint16_t dest = 0xd000;
410     uint8_t  ttl = PTS_DEFAULT_TTL;
411 
412     int access_pdu_len = 20;
413     memset(access_pdu_data, 0x55, access_pdu_len);
414     uint16_t netkey_index = 0;
415     uint16_t appkey_index = 0;
416 
417     // send as segmented access pdu
418     mesh_pdu_t * pdu = (mesh_pdu_t *) mesh_transport_pdu_get();
419     int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
420     if (status) return;
421     mesh_access_send_unacknowledged_pdu(pdu);
422 }
423 
424 static void send_pts_segmented_access_messsage_virtual(void){
425     uint8_t access_pdu_data[20];
426 
427     load_pts_app_key();
428 
429     uint16_t src = mesh_node_get_primary_element_address();
430     uint16_t dest = pts_virtual_addresss->pseudo_dst;
431     uint8_t  ttl = PTS_DEFAULT_TTL;
432 
433     int access_pdu_len = 20;
434     memset(access_pdu_data, 0x55, access_pdu_len);
435     uint16_t netkey_index = 0;
436     uint16_t appkey_index = 0;
437 
438     // send as segmented access pdu
439     mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
440     int status = mesh_upper_transport_setup_access_pdu((mesh_pdu_t*) transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
441     if (status) return;
442     mesh_access_send_unacknowledged_pdu((mesh_pdu_t*) transport_pdu);
443 }
444 
445 static void show_usage(void){
446     bd_addr_t      iut_address;
447     gap_local_bd_addr(iut_address);
448     printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address));
449     printf("0      - Send Network Message Unicast\n");
450     printf("1      - Send Network Message Virtual 9779\n");
451     printf("2      - Send Network Message Group   D000\n");
452     printf("3      - Send Network Message All Proxies\n");
453     printf("4      - Send Network Message All Friends\n");
454     printf("5      - Send Network Message All Relays\n");
455     printf("6      - Send Network Message Nodes\n");
456     printf("7      - Dump Network Messages\n");
457     printf("?      - Send Unsegmented Access Message\n");
458     printf("?      - Send Segmented Access Message - Unicast\n");
459     printf("?      - Send Segmented Access Message - Group   D000\n");
460     printf("?      - Send Segmented Access Message - Virtual 9779\n");
461     printf("?      - Clear Replay Protection List\n");
462     printf("?      - Load PTS App key\n");
463     printf("8      - Delete provisioning data\n");
464     printf("p      - Enable Public Key OOB \n");
465     printf("o      - Enable Output OOB \n");
466     printf("i      - Input  Output OOB \n");
467     printf("s      - Static Output OOB \n");
468     printf("b      - Set Secure Network Beacon %s\n", mesh_foundation_beacon_get() ? "Off" : "On");
469     printf("n      - Start Advertising with Node Identity\n");
470     printf("N      - Stop Advertising with Node Identity\n");
471     printf("g      - Generic ON/OFF Server Toggle Value\n");
472     printf("\n");
473 }
474 
475 static void stdin_process(char cmd){
476     if (ui_chars_for_pin){
477         printf("%c", cmd);
478         fflush(stdout);
479         if (cmd == '\n'){
480             printf("\nSending Pin '%s'\n", ui_pin);
481             provisioning_device_input_oob_complete_alphanumeric(1, ui_pin, ui_pin_offset);
482             ui_chars_for_pin = 0;
483         } else {
484             ui_pin[ui_pin_offset++] = cmd;
485         }
486         return;
487     }
488     switch (cmd){
489         case '0':
490             send_pts_network_messsage("Unicast", 0x0001, pts_type++);
491             break;
492         case '1':
493             send_pts_network_messsage("Virtual", pts_virtual_addresss->hash, pts_type++);
494             break;
495         case '2':
496             send_pts_network_messsage("Group", 0xd000, pts_type++);
497             break;
498         case '3':
499             send_pts_network_messsage("All Proxies", MESH_ADDRESS_ALL_PROXIES, pts_type++);
500             break;
501         case '4':
502             send_pts_network_messsage("All Friends", MESH_ADDRESS_ALL_FRIENDS, pts_type++);
503             break;
504         case '5':
505             send_pts_network_messsage("All Relays", MESH_ADDRESS_ALL_RELAYS, pts_type++);
506             break;
507         case '6':
508             send_pts_network_messsage("All Nodes", MESH_ADDRESS_ALL_NODES, pts_type++);
509             break;
510         case '7':
511             printf("Dump Network packets\n");
512             mesh_network_set_higher_layer_handler(&mesh_pts_received_network_message);
513             break;
514         case '8':
515             mesh_node_reset();
516             printf("Mesh Node Reset!\n");
517 #ifdef ENABLE_MESH_PROXY_SERVER
518             mesh_proxy_start_advertising_unprovisioned_device();
519 #endif
520             break;
521         case 'p':
522             printf("+ Public Key OOB Enabled\n");
523             btstack_parse_hex(prov_public_key_string, 64, prov_public_key_data);
524             btstack_parse_hex(prov_private_key_string, 32, prov_private_key_data);
525             provisioning_device_set_public_key_oob(prov_public_key_data, prov_private_key_data);
526             break;
527         case 'o':
528             printf("+ Output OOB Enabled\n");
529             provisioning_device_set_output_oob_actions(0x08, 0x08);
530             break;
531         case 'i':
532             printf("+ Input OOB Enabled\n");
533             provisioning_device_set_input_oob_actions(0x08, 0x08);
534             break;
535         case 's':
536             printf("+ Static OOB Enabled\n");
537             btstack_parse_hex(prov_static_oob_string, 16, prov_static_oob_data);
538             provisioning_device_set_static_oob(16, prov_static_oob_data);
539             break;
540         case 'g':
541             printf("Generic ON/OFF Server Toggle Value\n");
542             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);
543             break;
544         case 'b':
545             printf("Turn Secure Network Beacon %s\n", mesh_foundation_beacon_get() ? "Off" : "On");
546             mesh_foundation_beacon_set(1 - mesh_foundation_beacon_get());
547             mesh_foundation_state_store();
548             break;
549         case 'n':
550             printf("Start Advertising with Node ID\n");
551             mesh_proxy_start_advertising_with_node_id();
552             break;
553         case 'N':
554             printf("Stop Advertising with Node ID\n");
555             mesh_proxy_start_advertising_with_node_id();
556             break;
557         case ' ':
558             show_usage();
559             break;
560         default:
561             printf("Command: '%c' not implemented\n", cmd);
562             show_usage();
563             break;
564     }
565 }
566 
567 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){
568     UNUSED(connection_handle);
569     if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){
570         return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, strlen(gap_name_buffer), offset, buffer, buffer_size);
571     }
572     return 0;
573 }
574 
575 int btstack_main(void);
576 int btstack_main(void)
577 {
578     // console
579     btstack_stdin_setup(stdin_process);
580 
581     // crypto
582     btstack_crypto_init();
583 
584     // l2cap
585     l2cap_init();
586 
587     // setup le device db
588     le_device_db_init();
589 
590     // setup ATT server
591     att_server_init(profile_data, &att_read_callback, NULL);
592 
593     //
594     sm_init();
595 
596 
597     // mesh
598     mesh_init();
599 
600     // setup connectable advertisments
601     bd_addr_t null_addr;
602     memset(null_addr, 0, 6);
603     uint8_t adv_type = 0;   // AFV_IND
604     uint16_t adv_int_min = 0x0030;
605     uint16_t adv_int_max = 0x0030;
606     adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
607 
608     // Provisioning in device role
609     mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler);
610 
611     // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
612     mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103);
613 
614     // Setup Generic On/Off model
615     mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER);
616     mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations();
617     mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state;
618     mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler);
619     mesh_generic_on_off_server_set_publication_model(&mesh_generic_on_off_server_model, &generic_on_off_server_publication);
620     mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model);
621 
622     // Setup our custom model
623     mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER);
624     mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model);
625 
626     // Enable PROXY
627     mesh_foundation_gatt_proxy_set(1);
628 
629     // register for HCI events
630     hci_event_callback_registration.callback = &packet_handler;
631     hci_add_event_handler(&hci_event_callback_registration);
632 
633 #if defined(ENABLE_MESH_ADV_BEARER)
634     // setup scanning when supporting ADV Bearer
635     gap_set_scan_parameters(0, 0x300, 0x300);
636     gap_start_scan();
637 #endif
638 
639     //  PTS add-on
640 
641 #ifdef ENABLE_MESH_ADV_BEARER
642     // Register for Unprovisioned Device Beacons provisioner
643     beacon_register_for_unprovisioned_device_beacons(&mesh_unprovisioned_beacon_handler);
644 #endif
645 
646     // Set PTS Device UUID
647     uint8_t pts_device_uuid[16];
648     btstack_parse_hex(pts_device_uuid_string, 16, pts_device_uuid);
649     mesh_node_set_device_uuid(pts_device_uuid);
650     btstack_print_hex(pts_device_uuid, 16, 0);
651 
652     // PTS Virtual Address Label UUID - without Config Model, PTS uses our device uuid
653     uint8_t label_uuid[16];
654     btstack_parse_hex("001BDC0810210B0E0A0C000B0E0A0C00", 16, label_uuid);
655     pts_virtual_addresss = mesh_virtual_address_register(label_uuid, 0x9779);
656 
657     printf("TSPX_iut_model_id    0x1000 (Generic OnOff Server)\n");
658     printf("TSPX_vendor_model_id 0x%08x\n", mesh_vendor_model.model_identifier);
659     // turn on!
660 	hci_power_control(HCI_POWER_ON);
661 
662     return 0;
663 }
664 /* EXAMPLE_END */
665