xref: /btstack/src/ble/gatt-service/device_information_service_server.c (revision ff3cc4a5378c2f681cc9b75cf54d154a12a3051e)
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 MATTHIAS
24  * RINGWALD 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 
57 #include "ble/gatt-service/device_information_service_server.h"
58 
59 typedef enum {
60 	MANUFACTURER_NAME = 0,
61 	MODEL_NUMBER,
62 	SERIAL_NUMBER,
63 	HARDWARE_REVISION,
64 	FIRMWARE_REVISION,
65 	SOFTWARE_REVISION,
66 	SYSTEM_ID,
67 	IEEE_REGULATORY_CERTIFICATION,
68 	PNP_ID,
69 	NUM_INFORMATION_FIELDS
70 } device_information_field_id_t;
71 
72 typedef struct {
73 	uint8_t * data;
74 	uint16_t  len;
75 	uint16_t  value_handle;
76 } device_information_field_t;
77 
78 const uint16_t device_information_characteristic_uuids[] = {
79 	ORG_BLUETOOTH_CHARACTERISTIC_MANUFACTURER_NAME_STRING,
80 	ORG_BLUETOOTH_CHARACTERISTIC_MODEL_NUMBER_STRING,
81 	ORG_BLUETOOTH_CHARACTERISTIC_SERIAL_NUMBER_STRING,
82 	ORG_BLUETOOTH_CHARACTERISTIC_HARDWARE_REVISION_STRING,
83 	ORG_BLUETOOTH_CHARACTERISTIC_FIRMWARE_REVISION_STRING,
84 	ORG_BLUETOOTH_CHARACTERISTIC_SOFTWARE_REVISION_STRING,
85 	ORG_BLUETOOTH_CHARACTERISTIC_SYSTEM_ID,
86 	ORG_BLUETOOTH_CHARACTERISTIC_IEEE_11073_20601_REGULATORY_CERTIFICATION_DATA_LIST,
87 	ORG_BLUETOOTH_CHARACTERISTIC_PNP_ID
88 };
89 
90 static device_information_field_t device_information_fields[NUM_INFORMATION_FIELDS];
91 
92 static uint8_t device_information_system_id[8];
93 static uint8_t device_information_ieee_regulatory_certification[4];
94 static uint8_t device_information_pnp_id[7];
95 
96 static att_service_handler_t       device_information_service;
97 
98 static void set_string(device_information_field_id_t field_id, const char * text){
99 	device_information_fields[field_id].data = (uint8_t*) text;
100 	device_information_fields[field_id].len  = strlen(text);
101 }
102 
103 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){
104 	UNUSED(con_handle);	// ok: info same for all devices
105 	int i;
106 	for (i=0;i<NUM_INFORMATION_FIELDS;i++){
107 		if (device_information_fields[i].value_handle != attribute_handle) continue;
108 		if (buffer == NULL){
109 			return device_information_fields[i].len;
110 		}
111 		int bytes_to_copy = btstack_min(device_information_fields[i].len - offset, buffer_size);
112 		(void)memcpy(buffer,
113 			     &device_information_fields[i].data[offset],
114 			     bytes_to_copy);
115 		return bytes_to_copy;
116 	}
117 	return 0;
118 }
119 
120 void device_information_service_server_init(void){
121 
122 	// get service handle range
123 	uint16_t start_handle;
124 	uint16_t end_handle;
125 	int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_DEVICE_INFORMATION, &start_handle, &end_handle);
126 	if (!service_found) return;
127 
128 	// set length for fixed size characateristics
129 	device_information_fields[SYSTEM_ID].data = device_information_system_id;
130 	device_information_fields[SYSTEM_ID].len = 8;
131 	device_information_fields[IEEE_REGULATORY_CERTIFICATION].data = device_information_ieee_regulatory_certification;
132 	device_information_fields[IEEE_REGULATORY_CERTIFICATION].len = 4;
133 	device_information_fields[PNP_ID].data = device_information_pnp_id;
134 	device_information_fields[PNP_ID].len = 7;
135 
136 	// get characteristic value handles
137 	int i;
138 	for (i=0;i<NUM_INFORMATION_FIELDS;i++){
139 		device_information_fields[i].value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, device_information_characteristic_uuids[i]);
140 	}
141 
142 	// register service with ATT Server
143 	device_information_service.start_handle   = start_handle;
144 	device_information_service.end_handle     = end_handle;
145 	device_information_service.read_callback  = &device_information_service_read_callback;
146 	device_information_service.write_callback = NULL;
147 	att_server_register_service_handler(&device_information_service);
148 }
149 
150 /**
151  * @brief Set Manufacturer Name
152  * @param manufacturer_name
153  */
154 void device_information_service_server_set_manufacturer_name(const char * manufacturer_name){
155 	set_string(MANUFACTURER_NAME, manufacturer_name);
156 }
157 
158 /**
159  * @brief Set Model Number
160  * @param model_number
161  */
162 void device_information_service_server_set_model_number(const char * model_number){
163 	set_string(MODEL_NUMBER, model_number);
164 }
165 
166 /**
167  * @brief Set Serial Number
168  * @param serial_number
169  */
170 void device_information_service_server_set_serial_number(const char * serial_number){
171 	set_string(SERIAL_NUMBER, serial_number);
172 }
173 
174 /**
175  * @brief Set Hardware Revision
176  * @param hardware_revision
177  */
178 void device_information_service_server_set_hardware_revision(const char * hardware_revision){
179 	set_string(HARDWARE_REVISION, hardware_revision);
180 }
181 
182 /**
183  * @brief Set Firmware Revision
184  * @param firmware_revision
185  */
186 void device_information_service_server_set_firmware_revision(const char * firmware_revision){
187 	set_string(FIRMWARE_REVISION, firmware_revision);
188 }
189 
190 /**
191  * @brief Set Software Revision
192  * @param software_revision
193  */
194 void device_information_service_server_set_software_revision(const char * software_revision){
195 	set_string(SOFTWARE_REVISION, software_revision);
196 }
197 
198 /**
199  * @brief Set System ID
200  * @param manufacturer_identifier uint40
201  * @param organizationally_unique_identifier uint24
202  */
203 void device_information_service_server_set_system_id(uint64_t manufacturer_identifier, uint32_t organizationally_unique_identifier){
204 	little_endian_store_32(device_information_system_id, 0, manufacturer_identifier);
205 	device_information_system_id[4] = manufacturer_identifier >> 32;
206 	little_endian_store_16(device_information_system_id, 5, organizationally_unique_identifier);
207 	device_information_system_id[7] = organizationally_unique_identifier >> 16;
208 }
209 
210 /**
211  * @brief Set IEEE 11073-20601 regulatory certification data list
212  * @note: format duint16. duint16 is two uint16 values concatenated together.
213  * @param ieee_regulatory_certification
214  */
215 void device_information_service_server_set_ieee_regulatory_certification(uint16_t value_a, uint16_t value_b){
216 	little_endian_store_16(device_information_ieee_regulatory_certification, 0, value_a);
217 	little_endian_store_16(device_information_ieee_regulatory_certification, 2, value_b);
218 }
219 
220 /**
221  * @brief Set PNP ID
222  * @param vendor_source_id
223  * @param vendor_id
224  * @param product_id
225  */
226 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){
227 	device_information_pnp_id[0] = vendor_source_id;
228 	little_endian_store_16(device_information_pnp_id, 1, vendor_id);
229 	little_endian_store_16(device_information_pnp_id, 3, product_id);
230 	little_endian_store_16(device_information_pnp_id, 5, product_version);
231 }
232