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
31 ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
32 struct ble_gatt_access_ctxt *ctxt, void *arg);
33
34 static const struct ble_gatt_svc_def ble_svc_gatt_defs[] = {
35 {
36 /*** Service: GATT */
37 .type = BLE_GATT_SVC_TYPE_PRIMARY,
38 .uuid = BLE_UUID16_DECLARE(BLE_GATT_SVC_UUID16),
39 .characteristics = (struct ble_gatt_chr_def[]) { {
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 0, /* No more services. */
51 },
52 };
53
54 static int
ble_svc_gatt_access(uint16_t conn_handle,uint16_t attr_handle,struct ble_gatt_access_ctxt * ctxt,void * arg)55 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
60 /* The only operation allowed for this characteristic is indicate. This
61 * access callback gets called by the stack when it needs to read the
62 * characteristic value to populate the outgoing indication command.
63 * Therefore, this callback should only get called during an attempt to
64 * read the characteristic.
65 */
66 assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
67 assert(ctxt->chr == &ble_svc_gatt_defs[0].characteristics[0]);
68
69 u8p = os_mbuf_extend(ctxt->om, 4);
70 if (u8p == NULL) {
71 return BLE_HS_ENOMEM;
72 }
73
74 put_le16(u8p + 0, ble_svc_gatt_start_handle);
75 put_le16(u8p + 2, ble_svc_gatt_end_handle);
76
77 return 0;
78 }
79
80 /**
81 * Indicates a change in attribute assignment to all subscribed peers.
82 * Unconnected bonded peers receive an indication when they next connect.
83 *
84 * @param start_handle The start of the affected handle range.
85 * @param end_handle The end of the affected handle range.
86 */
87 void
ble_svc_gatt_changed(uint16_t start_handle,uint16_t end_handle)88 ble_svc_gatt_changed(uint16_t start_handle, uint16_t end_handle)
89 {
90 ble_svc_gatt_start_handle = start_handle;
91 ble_svc_gatt_end_handle = end_handle;
92 ble_gatts_chr_updated(ble_svc_gatt_changed_val_handle);
93 }
94
95 void
ble_svc_gatt_init(void)96 ble_svc_gatt_init(void)
97 {
98 int rc;
99
100 /* Ensure this function only gets called by sysinit. */
101 SYSINIT_ASSERT_ACTIVE();
102
103 rc = ble_gatts_count_cfg(ble_svc_gatt_defs);
104 SYSINIT_PANIC_ASSERT(rc == 0);
105
106 rc = ble_gatts_add_svcs(ble_svc_gatt_defs);
107 SYSINIT_PANIC_ASSERT(rc == 0);
108 }
109