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 <stdio.h>
22 #include <string.h>
23 #include "host/ble_hs.h"
24 #include "host/ble_uuid.h"
25 #include "bleprph.h"
26
27 /**
28 * The vendor specific security test service consists of two characteristics:
29 * o random-number-generator: generates a random 32-bit number each time
30 * it is read. This characteristic can only be read over an encrypted
31 * connection.
32 * o static-value: a single-byte characteristic that can always be read,
33 * but can only be written over an encrypted connection.
34 */
35
36 /* 59462f12-9543-9999-12c8-58b459a2712d */
37 static const ble_uuid128_t gatt_svr_svc_sec_test_uuid =
38 BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
39 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
40
41 /* 5c3a659e-897e-45e1-b016-007107c96df6 */
42 static const ble_uuid128_t gatt_svr_chr_sec_test_rand_uuid =
43 BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
44 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
45
46 /* 5c3a659e-897e-45e1-b016-007107c96df7 */
47 static const ble_uuid128_t gatt_svr_chr_sec_test_static_uuid =
48 BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
49 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
50
51 static uint8_t gatt_svr_sec_test_static_val;
52
53 static int
54 gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
55 struct ble_gatt_access_ctxt *ctxt,
56 void *arg);
57
58 static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
59 {
60 /*** Service: Security test. */
61 .type = BLE_GATT_SVC_TYPE_PRIMARY,
62 .uuid = &gatt_svr_svc_sec_test_uuid.u,
63 .characteristics = (struct ble_gatt_chr_def[]) { {
64 /*** Characteristic: Random number generator. */
65 .uuid = &gatt_svr_chr_sec_test_rand_uuid.u,
66 .access_cb = gatt_svr_chr_access_sec_test,
67 .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
68 }, {
69 /*** Characteristic: Static value. */
70 .uuid = &gatt_svr_chr_sec_test_static_uuid.u,
71 .access_cb = gatt_svr_chr_access_sec_test,
72 .flags = BLE_GATT_CHR_F_READ |
73 BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
74 }, {
75 0, /* No more characteristics in this service. */
76 } },
77 },
78
79 {
80 0, /* No more services. */
81 },
82 };
83
84 static int
gatt_svr_chr_write(struct os_mbuf * om,uint16_t min_len,uint16_t max_len,void * dst,uint16_t * len)85 gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
86 void *dst, uint16_t *len)
87 {
88 uint16_t om_len;
89 int rc;
90
91 om_len = OS_MBUF_PKTLEN(om);
92 if (om_len < min_len || om_len > max_len) {
93 return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
94 }
95
96 rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
97 if (rc != 0) {
98 return BLE_ATT_ERR_UNLIKELY;
99 }
100
101 return 0;
102 }
103
104 static int
gatt_svr_chr_access_sec_test(uint16_t conn_handle,uint16_t attr_handle,struct ble_gatt_access_ctxt * ctxt,void * arg)105 gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
106 struct ble_gatt_access_ctxt *ctxt,
107 void *arg)
108 {
109 const ble_uuid_t *uuid;
110 int rand_num;
111 int rc;
112
113 uuid = ctxt->chr->uuid;
114
115 /* Determine which characteristic is being accessed by examining its
116 * 128-bit UUID.
117 */
118
119 if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_rand_uuid.u) == 0) {
120 assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
121
122 /* Respond with a 32-bit random number. */
123 rand_num = rand();
124 rc = os_mbuf_append(ctxt->om, &rand_num, sizeof rand_num);
125 return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
126 }
127
128 if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0) {
129 switch (ctxt->op) {
130 case BLE_GATT_ACCESS_OP_READ_CHR:
131 rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val,
132 sizeof gatt_svr_sec_test_static_val);
133 return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
134
135 case BLE_GATT_ACCESS_OP_WRITE_CHR:
136 rc = gatt_svr_chr_write(ctxt->om,
137 sizeof gatt_svr_sec_test_static_val,
138 sizeof gatt_svr_sec_test_static_val,
139 &gatt_svr_sec_test_static_val, NULL);
140 return rc;
141
142 default:
143 assert(0);
144 return BLE_ATT_ERR_UNLIKELY;
145 }
146 }
147
148 /* Unknown characteristic; the nimble stack should not have called this
149 * function.
150 */
151 assert(0);
152 return BLE_ATT_ERR_UNLIKELY;
153 }
154
155 void
gatt_svr_register_cb(struct ble_gatt_register_ctxt * ctxt,void * arg)156 gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
157 {
158 char buf[BLE_UUID_STR_LEN];
159
160 switch (ctxt->op) {
161 case BLE_GATT_REGISTER_OP_SVC:
162 MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
163 ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
164 ctxt->svc.handle);
165 break;
166
167 case BLE_GATT_REGISTER_OP_CHR:
168 MODLOG_DFLT(DEBUG, "registering characteristic %s with "
169 "def_handle=%d val_handle=%d\n",
170 ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
171 ctxt->chr.def_handle,
172 ctxt->chr.val_handle);
173 break;
174
175 case BLE_GATT_REGISTER_OP_DSC:
176 MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
177 ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
178 ctxt->dsc.handle);
179 break;
180
181 default:
182 assert(0);
183 break;
184 }
185 }
186
187 int
gatt_svr_init(void)188 gatt_svr_init(void)
189 {
190 int rc;
191
192 rc = ble_gatts_count_cfg(gatt_svr_svcs);
193 if (rc != 0) {
194 return rc;
195 }
196
197 rc = ble_gatts_add_svcs(gatt_svr_svcs);
198 if (rc != 0) {
199 return rc;
200 }
201
202 return 0;
203 }
204