1 /*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holders nor the names of
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38 #define BTSTACK_FILE__ "device_information_service_server.c"
39
40 /**
41 * Implementation of the Device Information Service Server
42 *
43 * To use with your application, add `#import <device_information_sevice.gatt>` to your .gatt file
44 * and call all functions below. All strings and blobs need to stay valid after calling the functions.
45 *
46 * @note: instead of calling all setters, you can create a local copy of the .gatt file and remove
47 * all Characteristics that are not relevant for your application.
48 */
49
50
51 #include "btstack_defines.h"
52 #include "ble/att_db.h"
53 #include "ble/att_server.h"
54 #include "btstack_util.h"
55 #include "bluetooth_gatt.h"
56 #include "btstack_debug.h"
57
58 #include "ble/gatt-service/device_information_service_server.h"
59
60 #define DEVICE_INFORMATION_MAX_STRING_LEN 32
61
62 #define UDI_FOR_MEDICAL_DEVICES_BITMASK_LABEL 1
63 #define UDI_FOR_MEDICAL_DEVICES_BITMASK_DEVICE_ID 2
64 #define UDI_FOR_MEDICAL_DEVICES_BITMASK_ISSUER 4
65 #define UDI_FOR_MEDICAL_DEVICES_BITMASK_AUTHORITY 8
66
67 typedef enum {
68 MANUFACTURER_NAME = 0,
69 MODEL_NUMBER,
70 SERIAL_NUMBER,
71 HARDWARE_REVISION,
72 FIRMWARE_REVISION,
73 SOFTWARE_REVISION,
74 SYSTEM_ID,
75 IEEE_REGULATORY_CERTIFICATION,
76 PNP_ID,
77 UDI_FOR_MEDICAL_DEVICES,
78 NUM_INFORMATION_FIELDS
79 } device_information_field_id_t;
80
81 typedef struct {
82 uint8_t * data;
83 uint16_t len;
84 uint16_t value_handle;
85 } device_information_field_t;
86
87 static device_information_field_t device_information_fields[NUM_INFORMATION_FIELDS];
88
89 static uint8_t device_information_system_id[8];
90 static uint8_t device_information_ieee_regulatory_certification[4];
91 static uint8_t device_information_pnp_id[7];
92 static uint8_t device_information_udi_for_medical_devices[1 + 4 * DEVICE_INFORMATION_MAX_STRING_LEN];
93
94 static att_service_handler_t device_information_service;
95
set_string(device_information_field_id_t field_id,const char * text)96 static void set_string(device_information_field_id_t field_id, const char * text){
97 device_information_fields[field_id].data = (uint8_t*) text;
98 device_information_fields[field_id].len = text != NULL ? (uint8_t) strlen(text) : 0;
99 }
100
device_information_service_read_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)101 static uint16_t device_information_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
102 UNUSED(con_handle); // ok: info same for all devices
103 unsigned int i;
104 for (i=0; i < NUM_INFORMATION_FIELDS; i++){
105 if ((device_information_fields[i].value_handle == attribute_handle) && (device_information_fields[i].data != NULL)){
106 return att_read_callback_handle_blob(device_information_fields[i].data, device_information_fields[i].len, offset, buffer, buffer_size);
107 };
108 }
109 return 0;
110 }
111
device_information_service_server_init(void)112 void device_information_service_server_init(void){
113
114 const uint16_t device_information_characteristic_uuids[] = {
115 ORG_BLUETOOTH_CHARACTERISTIC_MANUFACTURER_NAME_STRING,
116 ORG_BLUETOOTH_CHARACTERISTIC_MODEL_NUMBER_STRING,
117 ORG_BLUETOOTH_CHARACTERISTIC_SERIAL_NUMBER_STRING,
118 ORG_BLUETOOTH_CHARACTERISTIC_HARDWARE_REVISION_STRING,
119 ORG_BLUETOOTH_CHARACTERISTIC_FIRMWARE_REVISION_STRING,
120 ORG_BLUETOOTH_CHARACTERISTIC_SOFTWARE_REVISION_STRING,
121 ORG_BLUETOOTH_CHARACTERISTIC_SYSTEM_ID,
122 ORG_BLUETOOTH_CHARACTERISTIC_IEEE_11073_20601_REGULATORY_CERTIFICATION_DATA_LIST,
123 ORG_BLUETOOTH_CHARACTERISTIC_PNP_ID,
124 ORG_BLUETOOTH_CHARACTERISTIC_UDI_FOR_MEDICAL_DEVICES
125 };
126
127
128 // get service handle range
129 uint16_t start_handle = 0;
130 uint16_t end_handle = 0xffff;
131 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_DEVICE_INFORMATION, &start_handle, &end_handle);
132 btstack_assert(service_found != 0);
133 UNUSED(service_found);
134
135 // set length for fixed size characateristics
136 device_information_fields[SYSTEM_ID].data = device_information_system_id;
137 device_information_fields[SYSTEM_ID].len = sizeof(device_information_system_id);
138 device_information_fields[IEEE_REGULATORY_CERTIFICATION].data = device_information_ieee_regulatory_certification;
139 device_information_fields[IEEE_REGULATORY_CERTIFICATION].len = sizeof(device_information_ieee_regulatory_certification);
140 device_information_fields[PNP_ID].data = device_information_pnp_id;
141 device_information_fields[PNP_ID].len = sizeof(device_information_pnp_id);
142 // reserve max space for UDI
143 device_information_fields[UDI_FOR_MEDICAL_DEVICES].data = device_information_udi_for_medical_devices;
144 device_information_fields[UDI_FOR_MEDICAL_DEVICES].len = 0;
145
146 // get characteristic value handles
147 int i;
148 for (i=0; i < NUM_INFORMATION_FIELDS; i++){
149 device_information_fields[i].value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, device_information_characteristic_uuids[i]);
150 }
151
152 // register service with ATT Server
153 device_information_service.start_handle = start_handle;
154 device_information_service.end_handle = end_handle;
155 device_information_service.read_callback = &device_information_service_read_callback;
156 device_information_service.write_callback = NULL;
157 att_server_register_service_handler(&device_information_service);
158 }
159
160 /**
161 * @brief Set Manufacturer Name
162 * @param manufacturer_name
163 */
device_information_service_server_set_manufacturer_name(const char * manufacturer_name)164 void device_information_service_server_set_manufacturer_name(const char * manufacturer_name){
165 set_string(MANUFACTURER_NAME, manufacturer_name);
166 }
167
168 /**
169 * @brief Set Model Number
170 * @param model_number
171 */
device_information_service_server_set_model_number(const char * model_number)172 void device_information_service_server_set_model_number(const char * model_number){
173 set_string(MODEL_NUMBER, model_number);
174 }
175
176 /**
177 * @brief Set Serial Number
178 * @param serial_number
179 */
device_information_service_server_set_serial_number(const char * serial_number)180 void device_information_service_server_set_serial_number(const char * serial_number){
181 set_string(SERIAL_NUMBER, serial_number);
182 }
183
184 /**
185 * @brief Set Hardware Revision
186 * @param hardware_revision
187 */
device_information_service_server_set_hardware_revision(const char * hardware_revision)188 void device_information_service_server_set_hardware_revision(const char * hardware_revision){
189 set_string(HARDWARE_REVISION, hardware_revision);
190 }
191
192 /**
193 * @brief Set Firmware Revision
194 * @param firmware_revision
195 */
device_information_service_server_set_firmware_revision(const char * firmware_revision)196 void device_information_service_server_set_firmware_revision(const char * firmware_revision){
197 set_string(FIRMWARE_REVISION, firmware_revision);
198 }
199
200 /**
201 * @brief Set Software Revision
202 * @param software_revision
203 */
device_information_service_server_set_software_revision(const char * software_revision)204 void device_information_service_server_set_software_revision(const char * software_revision){
205 set_string(SOFTWARE_REVISION, software_revision);
206 }
207
208 /**
209 * @brief Set System ID
210 * @param manufacturer_identifier uint40
211 * @param organizationally_unique_identifier uint24
212 */
device_information_service_server_set_system_id(uint64_t manufacturer_identifier,uint32_t organizationally_unique_identifier)213 void device_information_service_server_set_system_id(uint64_t manufacturer_identifier, uint32_t organizationally_unique_identifier){
214 little_endian_store_32(device_information_system_id, 0, (uint32_t)(manufacturer_identifier & 0xffffffff));
215 device_information_system_id[4] = (uint8_t) (manufacturer_identifier >> 32);
216 little_endian_store_16(device_information_system_id, 5, organizationally_unique_identifier);
217 device_information_system_id[7] = organizationally_unique_identifier >> 16;
218 }
219
220 /**
221 * @brief Set IEEE 11073-20601 regulatory certification data list
222 * @note: format duint16. duint16 is two uint16 values concatenated together.
223 * @param ieee_regulatory_certification
224 */
device_information_service_server_set_ieee_regulatory_certification(uint16_t value_a,uint16_t value_b)225 void device_information_service_server_set_ieee_regulatory_certification(uint16_t value_a, uint16_t value_b){
226 little_endian_store_16(device_information_ieee_regulatory_certification, 0, value_a);
227 little_endian_store_16(device_information_ieee_regulatory_certification, 2, value_b);
228 }
229
device_information_service_server_set_pnp_id(uint8_t vendor_source_id,uint16_t vendor_id,uint16_t product_id,uint16_t product_version)230 void device_information_service_server_set_pnp_id(uint8_t vendor_source_id, uint16_t vendor_id, uint16_t product_id, uint16_t product_version){
231 device_information_pnp_id[0] = vendor_source_id;
232 little_endian_store_16(device_information_pnp_id, 1, vendor_id);
233 little_endian_store_16(device_information_pnp_id, 3, product_id);
234 little_endian_store_16(device_information_pnp_id, 5, product_version);
235 }
236
237
device_information_service_server_set_udi_for_medical_devices(const char * label,const char * device_id,const char * issuer,const char * authority)238 void device_information_service_server_set_udi_for_medical_devices(const char * label, const char * device_id, const char * issuer, const char * authority){
239
240 // suppress CppCheck warnings below. Buffer has size 1 + 4 * DEVICE_INFORMATION_MAX_STRING_LEN
241 uint8_t * data = device_information_fields[UDI_FOR_MEDICAL_DEVICES].data;
242
243 uint16_t bytes_copied;
244 uint16_t pos = 0;
245 data[pos++] = 0; // reserved for flags
246
247
248 // cppcheck-suppress objectIndex
249 if (label != NULL){
250 bytes_copied = btstack_strcpy((char *) &data[pos], DEVICE_INFORMATION_MAX_STRING_LEN, label);
251 pos += bytes_copied;
252 data[0] |= (1 << UDI_FOR_MEDICAL_DEVICES_BITMASK_LABEL);
253 }
254
255 if (device_id != NULL){
256 // cppcheck-suppress objectIndex
257 bytes_copied = btstack_strcpy((char *) &data[pos], DEVICE_INFORMATION_MAX_STRING_LEN, device_id);
258 pos += bytes_copied;
259 data[0] |= (1 << UDI_FOR_MEDICAL_DEVICES_BITMASK_DEVICE_ID);
260 }
261
262 if (issuer != NULL) {
263 // cppcheck-suppress objectIndex
264 bytes_copied = btstack_strcpy((char *) &data[pos], DEVICE_INFORMATION_MAX_STRING_LEN, issuer);
265 pos += bytes_copied;
266 data[0] |= (1 << UDI_FOR_MEDICAL_DEVICES_BITMASK_ISSUER);
267 }
268
269 if (authority != NULL){
270 // cppcheck-suppress objectIndex
271 bytes_copied = btstack_strcpy((char *) &data[pos], DEVICE_INFORMATION_MAX_STRING_LEN, authority);
272 pos += bytes_copied;
273 data[0] |= (1 << UDI_FOR_MEDICAL_DEVICES_BITMASK_AUTHORITY);
274 }
275 device_information_fields[UDI_FOR_MEDICAL_DEVICES].len = pos;
276 }
277
278