xref: /nrf52832-nimble/packages/NimBLE-latest/porting/examples/linux/ble.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 #include "nimble/nimble_port.h"
25 #include "host/ble_hs.h"
26 #include "host/util/util.h"
27 #include "services/gap/ble_svc_gap.h"
28 
29 static const char gap_name[] = "nimble";
30 
31 static uint8_t own_addr_type;
32 
33 static void start_advertise(void);
34 
35 static void
put_ad(uint8_t ad_type,uint8_t ad_len,const void * ad,uint8_t * buf,uint8_t * len)36 put_ad(uint8_t ad_type, uint8_t ad_len, const void *ad, uint8_t *buf,
37        uint8_t *len)
38 {
39     buf[(*len)++] = ad_len + 1;
40     buf[(*len)++] = ad_type;
41 
42     memcpy(&buf[*len], ad, ad_len);
43 
44     *len += ad_len;
45 }
46 
47 static void
update_ad(void)48 update_ad(void)
49 {
50     uint8_t ad[BLE_HS_ADV_MAX_SZ];
51     uint8_t ad_len = 0;
52     uint8_t ad_flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
53 
54     put_ad(BLE_HS_ADV_TYPE_FLAGS, 1, &ad_flags, ad, &ad_len);
55     put_ad(BLE_HS_ADV_TYPE_COMP_NAME, sizeof(gap_name), gap_name, ad, &ad_len);
56 
57     ble_gap_adv_set_data(ad, ad_len);
58 }
59 
60 static int
gap_event_cb(struct ble_gap_event * event,void * arg)61 gap_event_cb(struct ble_gap_event *event, void *arg)
62 {
63     switch (event->type) {
64     case BLE_GAP_EVENT_CONNECT:
65         if (event->connect.status) {
66             start_advertise();
67         }
68         break;
69 
70     case BLE_GAP_EVENT_DISCONNECT:
71         start_advertise();
72         break;
73     }
74 
75     return 0;
76 }
77 
78 static void
start_advertise(void)79 start_advertise(void)
80 {
81     struct ble_gap_adv_params advp;
82     int rc;
83 
84     update_ad();
85 
86     memset(&advp, 0, sizeof advp);
87     advp.conn_mode = BLE_GAP_CONN_MODE_UND;
88     advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
89     rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
90                            &advp, gap_event_cb, NULL);
91     assert(rc == 0);
92 }
93 
94 static void
app_ble_sync_cb(void)95 app_ble_sync_cb(void)
96 {
97     int rc;
98 
99     rc = ble_hs_util_ensure_addr(0);
100     assert(rc == 0);
101 
102     rc = ble_hs_id_infer_auto(0, &own_addr_type);
103     assert(rc == 0);
104 
105     start_advertise();
106 }
107 
108 void
nimble_host_task(void * param)109 nimble_host_task(void *param)
110 {
111     ble_hs_cfg.sync_cb = app_ble_sync_cb;
112 
113     ble_svc_gap_device_name_set(gap_name);
114 
115     nimble_port_run();
116 }
117