• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "sysinit/sysinit.h"
23 #include "host/ble_hs.h"
24 #include "services/gatt/ble_svc_gatt.h"
25 
26 static uint16_t ble_svc_gatt_changed_val_handle;
27 static uint16_t ble_svc_gatt_start_handle;
28 static uint16_t ble_svc_gatt_end_handle;
29 
30 static int ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
31     struct ble_gatt_access_ctxt *ctxt, void *arg);
32 
33 static const struct ble_gatt_svc_def ble_svc_gatt_defs[] = {
34     {
35         /*** Service: GATT */
36         .type = BLE_GATT_SVC_TYPE_PRIMARY,
37         .uuid = BLE_UUID16_DECLARE(BLE_GATT_SVC_UUID16),
38         .characteristics = (struct ble_gatt_chr_def[])
39         { {
40             .uuid = BLE_UUID16_DECLARE(BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16),
41             .access_cb = ble_svc_gatt_access,
42             .val_handle = &ble_svc_gatt_changed_val_handle,
43             .flags = BLE_GATT_CHR_F_INDICATE,
44             }, {
45                 0, /* No more characteristics in this service. */
46             }
47         },
48     },
49 
50     {
51         0, /* No more services. */
52     },
53 };
54 
ble_svc_gatt_access(uint16_t conn_handle,uint16_t attr_handle,struct ble_gatt_access_ctxt * ctxt,void * arg)55 static int ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
56                                struct ble_gatt_access_ctxt *ctxt, void *arg)
57 {
58     uint8_t *u8p;
59     /* The only operation allowed for this characteristic is indicate.  This
60      * access callback gets called by the stack when it needs to read the
61      * characteristic value to populate the outgoing indication command.
62      * Therefore, this callback should only get called during an attempt to
63      * read the characteristic.
64      */
65     assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
66     assert(ctxt->chr == &ble_svc_gatt_defs[0].characteristics[0]);
67     u8p = os_mbuf_extend(ctxt->om, 4); // 4:len
68     if (u8p == NULL) {
69         return BLE_HS_ENOMEM;
70     }
71 
72     put_le16(u8p + 0, ble_svc_gatt_start_handle);
73     put_le16(u8p + 2, ble_svc_gatt_end_handle); // 2:byte alignment
74     return 0;
75 }
76 
77 /**
78  * Indicates a change in attribute assignment to all subscribed peers.
79  * Unconnected bonded peers receive an indication when they next connect.
80  *
81  * @param start_handle          The start of the affected handle range.
82  * @param end_handle            The end of the affected handle range.
83  */
ble_svc_gatt_changed(uint16_t start_handle,uint16_t end_handle)84 void ble_svc_gatt_changed(uint16_t start_handle, uint16_t end_handle)
85 {
86     ble_svc_gatt_start_handle = start_handle;
87     ble_svc_gatt_end_handle = end_handle;
88     ble_gatts_chr_updated(ble_svc_gatt_changed_val_handle);
89 }
90 
ble_svc_gatt_init(void)91 void ble_svc_gatt_init(void)
92 {
93     int rc;
94     /* Ensure this function only gets called by sysinit. */
95     SYSINIT_ASSERT_ACTIVE();
96     rc = ble_gatts_count_cfg(ble_svc_gatt_defs);
97     SYSINIT_PANIC_ASSERT(rc == 0);
98     rc = ble_gatts_add_svcs(ble_svc_gatt_defs);
99     SYSINIT_PANIC_ASSERT(rc == 0);
100 }