xref: /btstack/src/mesh/beacon.c (revision 01122b73eef352aeca7441c8b29c15365f0d0177)
1 /*
2  * Copyright (C) 2017 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__ "beacon.c"
39 
40 #include <string.h>
41 
42 #include "mesh/beacon.h"
43 #include "mesh/adv_bearer.h"
44 #include "mesh/gatt_bearer.h"
45 #include "mesh_foundation.h"
46 #include "ble/core.h"
47 #include "bluetooth.h"
48 #include "bluetooth_data_types.h"
49 #include "btstack_debug.h"
50 #include "btstack_util.h"
51 #include "btstack_run_loop.h"
52 #include "btstack_event.h"
53 #include "gap.h"
54 #include "mesh_keys.h"
55 
56 #define BEACON_TYPE_UNPROVISIONED_DEVICE 0
57 #define BEACON_TYPE_SECURE_NETWORK 1
58 
59 #define UNPROVISIONED_BEACON_INTERVAL_MS 5000
60 #define UNPROVISIONED_BEACON_LEN      23
61 
62 #define SECURE_NETWORK_BEACON_INTERVAL_MIN_MS  10000
63 #define SECURE_NETWORK_BEACON_INTERVAL_MAX_MS 600000
64 #define SECURE_NETWORK_BEACON_LEN                 22
65 
66 // prototypes
67 static void mesh_secure_network_beacon_run(btstack_timer_source_t * ts);
68 
69 // bearers
70 #ifdef ENABLE_MESH_GATT_BEARER
71 static hci_con_handle_t gatt_bearer_con_handle;
72 #endif
73 
74 // beacon
75 static uint8_t mesh_beacon_data[29];
76 static uint8_t mesh_beacon_len;
77 static btstack_timer_source_t   beacon_timer;
78 
79 // unprovisioned device beacon
80 #ifdef ENABLE_MESH_ADV_BEARER
81 static const uint8_t * beacon_device_uuid;
82 static       uint16_t  beacon_oob_information;
83 static       uint32_t  beacon_uri_hash;
84 static int             beacon_send_device_beacon;
85 #endif
86 
87 static btstack_packet_handler_t unprovisioned_device_beacon_handler;
88 
89 // secure network beacon
90 static btstack_crypto_aes128_cmac_t        mesh_secure_network_beacon_cmac_request;
91 static uint8_t                             mesh_secure_network_beacon_auth_value[16];
92 static btstack_packet_handler_t            mesh_secure_network_beacon_handler;
93 static int                                 mesh_secure_network_beacon_active;
94 static uint8_t                             mesh_secure_network_beacon_validate_buffer[SECURE_NETWORK_BEACON_LEN];
95 
96 #ifdef ENABLE_MESH_ADV_BEARER
97 static void beacon_timer_handler(btstack_timer_source_t * ts){
98     // restart timer
99     btstack_run_loop_set_timer(ts, UNPROVISIONED_BEACON_INTERVAL_MS);
100     btstack_run_loop_add_timer(ts);
101 
102     // setup beacon
103     mesh_beacon_len = UNPROVISIONED_BEACON_LEN;
104     mesh_beacon_data[0] = BEACON_TYPE_UNPROVISIONED_DEVICE;
105     memcpy(&mesh_beacon_data[1], beacon_device_uuid, 16);
106     big_endian_store_16(mesh_beacon_data, 17, beacon_oob_information);
107     big_endian_store_32(mesh_beacon_data, 19, beacon_uri_hash);
108 
109     // request to send
110     beacon_send_device_beacon = 1;
111     adv_bearer_request_can_send_now_for_beacon();
112 }
113 #endif
114 
115 static void mesh_secure_network_beacon_auth_value_calculated(void * arg){
116     mesh_network_key_t * mesh_network_key = (mesh_network_key_t *) arg;
117 
118     memcpy(&mesh_beacon_data[14], mesh_secure_network_beacon_auth_value, 8);
119     mesh_beacon_len = SECURE_NETWORK_BEACON_LEN;
120 
121     printf("Secure Network Beacon\n");
122     printf("- ");
123     printf_hexdump(mesh_beacon_data, mesh_beacon_len);
124 
125     mesh_network_key->beacon_state = MESH_SECURE_NETWORK_BEACON_AUTH_VALUE;
126 
127     mesh_secure_network_beacon_run(NULL);
128 }
129 
130 static uint8_t mesh_secure_network_beacon_get_flags(mesh_network_key_t * mesh_network_key){
131     uint8_t mesh_flags = 0;
132     if (mesh_network_key->key_refresh != MESH_KEY_REFRESH_NOT_ACTIVE){
133         mesh_flags |= 1;
134     }
135 
136     // TODO: set bit 1 if IV Update is active
137     return mesh_flags;
138 }
139 
140 static void mesh_secure_network_beacon_setup(mesh_network_key_t * mesh_network_key){
141     mesh_beacon_data[0] = BEACON_TYPE_SECURE_NETWORK;
142     mesh_beacon_data[1] = mesh_secure_network_beacon_get_flags(mesh_network_key);
143     memcpy(&mesh_beacon_data[2], mesh_network_key->network_id, 8);
144     big_endian_store_32(mesh_beacon_data, 10, mesh_get_iv_index());
145     btstack_crypto_aes128_cmac_message(&mesh_secure_network_beacon_cmac_request, mesh_network_key->beacon_key, 13,
146         &mesh_beacon_data[1], mesh_secure_network_beacon_auth_value, &mesh_secure_network_beacon_auth_value_calculated, mesh_network_key);
147 }
148 
149 static void mesh_secure_network_beacon_update_interval(mesh_network_key_t * subnet){
150     uint32_t min_observation_period_ms = 2 * subnet->beacon_interval_ms;
151     uint32_t actual_observation_period = btstack_time_delta(btstack_run_loop_get_time_ms(), subnet->beacon_observation_start_ms);
152 
153     // The Observation Period in seconds should typically be double the typical Beacon Interval.
154     if (actual_observation_period < min_observation_period_ms) return;
155 
156     // Expected Number of Beacons (1 beacon per 10 seconds)
157     uint16_t expected_number_of_beacons = actual_observation_period / SECURE_NETWORK_BEACON_INTERVAL_MIN_MS;
158 
159     // Beacon Interval = Observation Period * (Observed Number of Beacons + 1) / Expected Number of Beacons
160     uint32_t new_beacon_interval  =  actual_observation_period * (subnet->beacon_observation_counter + 1) / expected_number_of_beacons;
161 
162     if (new_beacon_interval > SECURE_NETWORK_BEACON_INTERVAL_MAX_MS){
163         new_beacon_interval = SECURE_NETWORK_BEACON_INTERVAL_MAX_MS;
164     }
165     else if (new_beacon_interval < SECURE_NETWORK_BEACON_INTERVAL_MIN_MS){
166         new_beacon_interval = SECURE_NETWORK_BEACON_INTERVAL_MAX_MS;
167     }
168     subnet->beacon_interval_ms = new_beacon_interval;
169     log_info("New beacon interval %u seconds", (int) (subnet->beacon_interval_ms / 1000));
170 }
171 
172 static void mesh_secure_network_beacon_run(btstack_timer_source_t * ts){
173     UNUSED(ts);
174 
175     uint32_t next_timeout_ms = 0;
176 
177     // iterate over all networks
178     mesh_network_key_iterator_t it;
179     mesh_network_key_iterator_init(&it);
180     while (mesh_network_key_iterator_has_more(&it)){
181         mesh_network_key_t * subnet = mesh_network_key_iterator_get_next(&it);
182         switch (subnet->beacon_state){
183             case MESH_SECURE_NETWORK_BEACON_W4_INTERVAL:
184                 // update beacon interval
185                 mesh_secure_network_beacon_update_interval(subnet);
186 
187                 // send new beacon
188                 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE;
189 
190                 /** Explict Fall-through */
191 
192             case MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE:
193                 if (mesh_secure_network_beacon_active){
194                     // just try again in 10 ms
195                     next_timeout_ms = 10;
196                     break;
197                 }
198                 subnet->beacon_state  = MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE;
199                 mesh_secure_network_beacon_active = 1;
200                 mesh_secure_network_beacon_setup(subnet);
201                 break;
202 
203             case MESH_SECURE_NETWORK_BEACON_AUTH_VALUE:
204 
205 #ifdef ENABLE_MESH_ADV_BEARER
206                 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV;
207                 adv_bearer_request_can_send_now_for_beacon();
208                 break;
209 #endif
210                 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_ADV_SENT;
211 
212                 /** Explict Fall-through */
213 
214             case MESH_SECURE_NETWORK_BEACON_ADV_SENT:
215 
216 #ifdef ENABLE_MESH_GATT_BEARER
217                 if (gatt_bearer_con_handle != HCI_CON_HANDLE_INVALID && mesh_foundation_gatt_proxy_get() != 0){
218                     subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT;
219                     gatt_bearer_request_can_send_now_for_beacon();
220                     break;
221                 }
222 #endif
223                 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_GATT_SENT;
224 
225                 /** Explict Fall-through */
226 
227             case MESH_SECURE_NETWORK_BEACON_GATT_SENT:
228                 // now, start listening for beacons
229                 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W4_INTERVAL;
230                 // and request timeout
231                 if (next_timeout_ms == 0 || next_timeout_ms > subnet->beacon_interval_ms){
232                     next_timeout_ms = subnet->beacon_interval_ms;
233                 }
234                 break;
235 
236             default:
237                 break;
238         }
239     }
240 
241     // setup next run
242     if (next_timeout_ms == 0) return;
243 
244     btstack_run_loop_set_timer(&beacon_timer, next_timeout_ms);
245     btstack_run_loop_set_timer_handler(&beacon_timer, mesh_secure_network_beacon_run);
246     btstack_run_loop_add_timer(&beacon_timer);
247 }
248 
249 static void beacon_handle_secure_beacon_auth_value_calculated(void * arg){
250     UNUSED(arg);
251 
252     // pass on, if auth value checks out
253     if (memcmp(&mesh_secure_network_beacon_validate_buffer[14], mesh_secure_network_beacon_auth_value, 8) == 0) {
254         if (mesh_secure_network_beacon_handler){
255             (*mesh_secure_network_beacon_handler)(MESH_BEACON_PACKET, 0, mesh_secure_network_beacon_validate_buffer, SECURE_NETWORK_BEACON_LEN);
256         }
257     }
258 
259     // done
260     mesh_secure_network_beacon_active = 0;
261     mesh_secure_network_beacon_run(NULL);
262 }
263 
264 static void beacon_handle_secure_beacon(uint8_t * packet, uint16_t size){
265     if (size != SECURE_NETWORK_BEACON_LEN) return;
266 
267     // lookup subnet by network id
268     uint8_t * beacon_network_id = &packet[2];
269     mesh_network_key_iterator_t it;
270     mesh_network_key_iterator_init(&it);
271     mesh_network_key_t * subnet = NULL;
272     while (mesh_network_key_iterator_has_more(&it)){
273         mesh_network_key_t * item = mesh_network_key_iterator_get_next(&it);
274         if (memcmp(item->network_id, beacon_network_id, 8) != 0 ) continue;
275         subnet = item;
276         break;
277     }
278     if (subnet == NULL) return;
279 
280     // count beacon
281     subnet->beacon_observation_counter++;
282 
283     // check if new flags are set
284     uint8_t current_flags = mesh_secure_network_beacon_get_flags(subnet);
285     uint8_t new_flags = packet[1] & (~current_flags);
286 
287     if (new_flags == 0) return;
288 
289     // validate beacon - if crytpo ready
290     if (mesh_secure_network_beacon_active) return;
291 
292     mesh_secure_network_beacon_active = 1;
293     memcpy(mesh_secure_network_beacon_validate_buffer, &packet[0], SECURE_NETWORK_BEACON_LEN);
294 
295     btstack_crypto_aes128_cmac_message(&mesh_secure_network_beacon_cmac_request, subnet->beacon_key, 13,
296         &mesh_secure_network_beacon_validate_buffer[1], mesh_secure_network_beacon_auth_value, &beacon_handle_secure_beacon_auth_value_calculated, subnet);
297 }
298 
299 static void beacon_handle_beacon_packet(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
300     log_info("beacon type %u", packet[0]);
301     switch (packet[0]){
302         case BEACON_TYPE_UNPROVISIONED_DEVICE:
303             if (unprovisioned_device_beacon_handler){
304                 (*unprovisioned_device_beacon_handler)(packet_type, channel, packet, size);
305             }
306             break;
307         case BEACON_TYPE_SECURE_NETWORK:
308             beacon_handle_secure_beacon(packet, size);
309             break;
310         default:
311             break;
312     }
313 }
314 
315 #ifdef ENABLE_MESH_ADV_BEARER
316 static void beacon_adv_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
317     mesh_network_key_iterator_t it;
318     switch (packet_type){
319         case HCI_EVENT_PACKET:
320             switch(packet[0]){
321                 case HCI_EVENT_MESH_META:
322                     switch(packet[2]){
323                         case MESH_SUBEVENT_CAN_SEND_NOW:
324                             if (beacon_send_device_beacon){
325                                 beacon_send_device_beacon = 0;
326                                 adv_bearer_send_beacon(mesh_beacon_data, mesh_beacon_len);
327                                 break;
328                             }
329                             // secure beacon state machine
330                             mesh_network_key_iterator_init(&it);
331                             while (mesh_network_key_iterator_has_more(&it)){
332                                 mesh_network_key_t * subnet = mesh_network_key_iterator_get_next(&it);
333                                 switch (subnet->beacon_state){
334                                     case MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV:
335                                         adv_bearer_send_beacon(mesh_beacon_data, mesh_beacon_len);
336                                         subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_ADV_SENT;
337                                         mesh_secure_network_beacon_run(NULL);
338                                         break;
339                                     default:
340                                         break;
341                                 }
342                             }
343                             break;
344                         default:
345                             break;
346                     }
347                     break;
348                 default:
349                     break;
350             }
351             break;
352         case MESH_BEACON_PACKET:
353             beacon_handle_beacon_packet(packet_type, channel, packet, size);
354             break;
355         default:
356             break;
357     }
358 }
359 #endif
360 
361 #ifdef ENABLE_MESH_GATT_BEARER
362 // handle MESH_SUBEVENT_PROXY_DISCONNECTED and MESH_SUBEVENT_CAN_SEND_NOW
363 static void beacon_gatt_handle_mesh_event(uint8_t mesh_subevent){
364     mesh_network_key_iterator_t it;
365     mesh_network_key_iterator_init(&it);
366     while (mesh_network_key_iterator_has_more(&it)){
367         mesh_network_key_t * subnet = mesh_network_key_iterator_get_next(&it);
368         switch (subnet->beacon_state){
369             case MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT:
370                 // skip send on MESH_SUBEVENT_PROXY_DISCONNECTED
371                 if (mesh_subevent == MESH_SUBEVENT_CAN_SEND_NOW){
372                     gatt_bearer_send_beacon(mesh_beacon_data, mesh_beacon_len);
373                 }
374                 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_GATT_SENT;
375                 mesh_secure_network_beacon_run(NULL);
376                 break;
377             default:
378                 break;
379         }
380     }
381 
382 }
383 
384 static void beacon_gatt_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
385     uint8_t mesh_subevent;
386     switch (packet_type){
387         case HCI_EVENT_PACKET:
388             switch(packet[0]){
389                 case HCI_EVENT_MESH_META:
390                     mesh_subevent = packet[2];
391                     switch(mesh_subevent){
392                         case MESH_SUBEVENT_PROXY_CONNECTED:
393                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
394                             break;
395                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
396                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
397                             beacon_gatt_handle_mesh_event(mesh_subevent);
398                             break;
399                         case MESH_SUBEVENT_CAN_SEND_NOW:
400                             beacon_gatt_handle_mesh_event(mesh_subevent);
401                             break;
402                         default:
403                             break;
404                     }
405                     break;
406                 default:
407                     break;
408             }
409             break;
410         case MESH_BEACON_PACKET:
411             beacon_handle_beacon_packet(packet_type, channel, packet, size);
412             break;
413         default:
414             break;
415     }
416 }
417 #endif
418 
419 void beacon_init(void){
420 #ifdef ENABLE_MESH_ADV_BEARER
421     adv_bearer_register_for_beacon(&beacon_adv_packet_handler);
422 #endif
423 #ifdef ENABLE_MESH_GATT_BEARER
424     gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
425     gatt_bearer_register_for_beacon(&beacon_gatt_packet_handler);
426 #endif
427 }
428 
429 /**
430  * Start Unprovisioned Device Beacon
431  */
432 void beacon_unprovisioned_device_start(const uint8_t * device_uuid, uint16_t oob_information){
433 #ifdef ENABLE_MESH_ADV_BEARER
434     beacon_oob_information = oob_information;
435     if (device_uuid){
436         beacon_device_uuid = device_uuid;
437         beacon_timer.process = &beacon_timer_handler;
438         beacon_timer_handler(&beacon_timer);
439     }
440 #endif
441 }
442 
443 /**
444  * Stop Unprovisioned Device Beacon
445  */
446 void beacon_unprovisioned_device_stop(void){
447 #ifdef ENABLE_MESH_ADV_BEARER
448     btstack_run_loop_remove_timer(&beacon_timer);
449 #endif
450 }
451 
452 // secure network beacons
453 
454 void beacon_secure_network_start(mesh_network_key_t * mesh_network_key){
455     // default interval
456     mesh_network_key->beacon_interval_ms = SECURE_NETWORK_BEACON_INTERVAL_MIN_MS;
457     mesh_network_key->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE;
458     mesh_network_key->beacon_observation_start_ms = btstack_run_loop_get_time_ms();
459     mesh_network_key->beacon_observation_counter = 0;
460 
461     // start sending
462     mesh_secure_network_beacon_run(NULL);
463 }
464 
465 // register handler
466 void beacon_register_for_unprovisioned_device_beacons(btstack_packet_handler_t packet_handler){
467     unprovisioned_device_beacon_handler = packet_handler;
468 }
469 
470 void beacon_register_for_secure_network_beacons(btstack_packet_handler_t packet_handler){
471     mesh_secure_network_beacon_handler = packet_handler;
472 }
473