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 <string.h>
22 #include "sysinit/sysinit.h"
23 #include "syscfg/syscfg.h"
24 #include "host/ble_hs.h"
25 #include "services/tps/ble_svc_tps.h"
26
27 /* XXX: We shouldn't be including the host's private header files. The host
28 * API needs to be updated with a function to query the advertising transmit
29 * power.
30 */
31 #include "../src/ble_hs_hci_priv.h"
32
33 int8_t ble_svc_tps_tx_power_level;
34
35 /* Access function */
36 static int
37 ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle,
38 struct ble_gatt_access_ctxt *ctxt, void *arg);
39
40 static const struct ble_gatt_svc_def ble_svc_tps_defs[] = {
41 {
42 /*** Service: Tx Power Service. */
43 .type = BLE_GATT_SVC_TYPE_PRIMARY,
44 .uuid = BLE_UUID16_DECLARE(BLE_SVC_TPS_UUID16),
45 .characteristics = (struct ble_gatt_chr_def[]) { {
46 /*** Characteristic: Tx Power Level. */
47 .uuid = BLE_UUID16_DECLARE(BLE_SVC_TPS_CHR_UUID16_TX_POWER_LEVEL),
48 .access_cb = ble_svc_tps_access,
49 .flags = BLE_GATT_CHR_F_READ,
50 }, {
51 0, /* No more characteristics in this service. */
52 } },
53 },
54
55 {
56 0, /* No more services. */
57 },
58 };
59
60 /**
61 * Simple read access callback for the tx power level
62 * characteristic.
63 */
64 static int
ble_svc_tps_access(uint16_t conn_handle,uint16_t attr_handle,struct ble_gatt_access_ctxt * ctxt,void * arg)65 ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle,
66 struct ble_gatt_access_ctxt *ctxt, void *arg)
67 {
68 int rc;
69
70 assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]);
71
72 switch (ctxt->op) {
73 case BLE_GATT_ACCESS_OP_READ_CHR:
74 rc = ble_hs_hci_util_read_adv_tx_pwr(&ble_svc_tps_tx_power_level);
75 if (rc != 0) {
76 return BLE_ATT_ERR_UNLIKELY;
77 }
78
79 rc = os_mbuf_append(ctxt->om, &ble_svc_tps_tx_power_level,
80 sizeof ble_svc_tps_tx_power_level);
81 return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
82
83 default:
84 assert(0);
85 return BLE_ATT_ERR_UNLIKELY;
86 }
87
88 return 0;
89 }
90
91 /**
92 * Initialize the TPS
93 */
94 void
ble_svc_tps_init(void)95 ble_svc_tps_init(void)
96 {
97 int rc;
98
99 /* Ensure this function only gets called by sysinit. */
100 SYSINIT_ASSERT_ACTIVE();
101
102 rc = ble_gatts_count_cfg(ble_svc_tps_defs);
103 SYSINIT_PANIC_ASSERT(rc == 0);
104
105 rc = ble_gatts_add_svcs(ble_svc_tps_defs);
106 SYSINIT_PANIC_ASSERT(rc == 0);
107 }
108