xref: /btstack/src/mesh/gatt-service/mesh_proxy_service_server.c (revision d567aeb32398f2a708611c446bcd9bad85a152cd)
1*d567aeb3SMatthias Ringwald /*
2*d567aeb3SMatthias Ringwald  * Copyright (C) 2018 BlueKitchen GmbH
3*d567aeb3SMatthias Ringwald  *
4*d567aeb3SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*d567aeb3SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*d567aeb3SMatthias Ringwald  * are met:
7*d567aeb3SMatthias Ringwald  *
8*d567aeb3SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*d567aeb3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*d567aeb3SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*d567aeb3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*d567aeb3SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*d567aeb3SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*d567aeb3SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*d567aeb3SMatthias Ringwald  *    from this software without specific prior written permission.
16*d567aeb3SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*d567aeb3SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*d567aeb3SMatthias Ringwald  *    monetary gain.
19*d567aeb3SMatthias Ringwald  *
20*d567aeb3SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*d567aeb3SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*d567aeb3SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*d567aeb3SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*d567aeb3SMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*d567aeb3SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*d567aeb3SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*d567aeb3SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*d567aeb3SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*d567aeb3SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*d567aeb3SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*d567aeb3SMatthias Ringwald  * SUCH DAMAGE.
32*d567aeb3SMatthias Ringwald  *
33*d567aeb3SMatthias Ringwald  * Please inquire about commercial licensing options at
34*d567aeb3SMatthias Ringwald  * [email protected]
35*d567aeb3SMatthias Ringwald  *
36*d567aeb3SMatthias Ringwald  */
37*d567aeb3SMatthias Ringwald 
38*d567aeb3SMatthias Ringwald #define BTSTACK_FILE__ "mesh_proxy_service_server.c"
39*d567aeb3SMatthias Ringwald 
40*d567aeb3SMatthias Ringwald #include "mesh/gatt-service/mesh_proxy_service_server.h"
41*d567aeb3SMatthias Ringwald 
42*d567aeb3SMatthias Ringwald #include "ble/att_db.h"
43*d567aeb3SMatthias Ringwald #include "ble/att_server.h"
44*d567aeb3SMatthias Ringwald #include "bluetooth.h"
45*d567aeb3SMatthias Ringwald #include "bluetooth_gatt.h"
46*d567aeb3SMatthias Ringwald #include "btstack_debug.h"
47*d567aeb3SMatthias Ringwald #include "btstack_defines.h"
48*d567aeb3SMatthias Ringwald #include "btstack_event.h"
49*d567aeb3SMatthias Ringwald #include "btstack_util.h"
50*d567aeb3SMatthias Ringwald #include "hci.h"
51*d567aeb3SMatthias Ringwald 
52*d567aeb3SMatthias Ringwald #include "mesh/provisioning.h"
53*d567aeb3SMatthias Ringwald 
54*d567aeb3SMatthias Ringwald typedef struct {
55*d567aeb3SMatthias Ringwald     hci_con_handle_t con_handle;
56*d567aeb3SMatthias Ringwald 
57*d567aeb3SMatthias Ringwald     uint16_t data_in_client_value_handle;
58*d567aeb3SMatthias Ringwald     uint8_t  data_in_proxy_pdu[MESH_PROV_MAX_PROXY_PDU];
59*d567aeb3SMatthias Ringwald 
60*d567aeb3SMatthias Ringwald     // Mesh Provisioning Data Out
61*d567aeb3SMatthias Ringwald     uint16_t data_out_client_value_handle;
62*d567aeb3SMatthias Ringwald     uint8_t  data_out_proxy_pdu[MESH_PROV_MAX_PROXY_PDU];
63*d567aeb3SMatthias Ringwald     uint16_t data_out_proxy_pdu_size;
64*d567aeb3SMatthias Ringwald 
65*d567aeb3SMatthias Ringwald     // Mesh Provisioning Data Out Notification
66*d567aeb3SMatthias Ringwald     uint16_t data_out_client_configuration_descriptor_handle;
67*d567aeb3SMatthias Ringwald     uint16_t data_out_client_configuration_descriptor_value;
68*d567aeb3SMatthias Ringwald     btstack_context_callback_registration_t data_out_notify_callback;
69*d567aeb3SMatthias Ringwald 
70*d567aeb3SMatthias Ringwald     btstack_context_callback_registration_t  pdu_response_callback;
71*d567aeb3SMatthias Ringwald } mesh_proxy_t;
72*d567aeb3SMatthias Ringwald 
73*d567aeb3SMatthias Ringwald static btstack_packet_handler_t mesh_proxy_service_packet_handler;
74*d567aeb3SMatthias Ringwald static att_service_handler_t mesh_proxy_service;
75*d567aeb3SMatthias Ringwald static mesh_proxy_t mesh_proxy;
76*d567aeb3SMatthias Ringwald 
77*d567aeb3SMatthias Ringwald 
mesh_proxy_service_emit_connected(hci_con_handle_t con_handle)78*d567aeb3SMatthias Ringwald static void mesh_proxy_service_emit_connected(hci_con_handle_t con_handle){
79*d567aeb3SMatthias Ringwald     uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PROXY_CONNECTED};
80*d567aeb3SMatthias Ringwald     little_endian_store_16(event, 4, con_handle);
81*d567aeb3SMatthias Ringwald     mesh_proxy_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
82*d567aeb3SMatthias Ringwald }
83*d567aeb3SMatthias Ringwald 
mesh_proxy_service_emit_disconnected(hci_con_handle_t con_handle)84*d567aeb3SMatthias Ringwald static void mesh_proxy_service_emit_disconnected(hci_con_handle_t con_handle){
85*d567aeb3SMatthias Ringwald     uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PROXY_DISCONNECTED};
86*d567aeb3SMatthias Ringwald     little_endian_store_16(event, 4, con_handle);
87*d567aeb3SMatthias Ringwald     mesh_proxy_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
88*d567aeb3SMatthias Ringwald }
89*d567aeb3SMatthias Ringwald 
mesh_proxy_service_get_instance_for_con_handle(hci_con_handle_t con_handle)90*d567aeb3SMatthias Ringwald static mesh_proxy_t * mesh_proxy_service_get_instance_for_con_handle(hci_con_handle_t con_handle){
91*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = &mesh_proxy;
92*d567aeb3SMatthias Ringwald     if (con_handle == HCI_CON_HANDLE_INVALID) return NULL;
93*d567aeb3SMatthias Ringwald     instance->con_handle = con_handle;
94*d567aeb3SMatthias Ringwald     return instance;
95*d567aeb3SMatthias Ringwald }
96*d567aeb3SMatthias Ringwald 
mesh_proxy_service_read_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)97*d567aeb3SMatthias Ringwald static uint16_t mesh_proxy_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
98*d567aeb3SMatthias Ringwald     UNUSED(con_handle);
99*d567aeb3SMatthias Ringwald     UNUSED(attribute_handle);
100*d567aeb3SMatthias Ringwald     UNUSED(offset);
101*d567aeb3SMatthias Ringwald     UNUSED(buffer_size);
102*d567aeb3SMatthias Ringwald 
103*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle);
104*d567aeb3SMatthias Ringwald     if (!instance){
105*d567aeb3SMatthias Ringwald         log_error("mesh_proxy_service_read_callback: instance is null");
106*d567aeb3SMatthias Ringwald         return 0;
107*d567aeb3SMatthias Ringwald     }
108*d567aeb3SMatthias Ringwald     if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){
109*d567aeb3SMatthias Ringwald         if (buffer && buffer_size >= 2){
110*d567aeb3SMatthias Ringwald             little_endian_store_16(buffer, 0, instance->data_out_client_configuration_descriptor_value);
111*d567aeb3SMatthias Ringwald         }
112*d567aeb3SMatthias Ringwald         return 2;
113*d567aeb3SMatthias Ringwald     }
114*d567aeb3SMatthias Ringwald     log_info("mesh_proxy_service_read_callback: not handled read on handle 0x%02x", attribute_handle);
115*d567aeb3SMatthias Ringwald     return 0;
116*d567aeb3SMatthias Ringwald }
117*d567aeb3SMatthias Ringwald 
mesh_proxy_service_write_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)118*d567aeb3SMatthias Ringwald static int mesh_proxy_service_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
119*d567aeb3SMatthias Ringwald     UNUSED(transaction_mode);
120*d567aeb3SMatthias Ringwald     UNUSED(offset);
121*d567aeb3SMatthias Ringwald     UNUSED(buffer_size);
122*d567aeb3SMatthias Ringwald 
123*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle);
124*d567aeb3SMatthias Ringwald     if (!instance){
125*d567aeb3SMatthias Ringwald         log_error("mesh_proxy_service_write_callback: instance is null");
126*d567aeb3SMatthias Ringwald         return 0;
127*d567aeb3SMatthias Ringwald     }
128*d567aeb3SMatthias Ringwald 
129*d567aeb3SMatthias Ringwald     if (attribute_handle == instance->data_in_client_value_handle){
130*d567aeb3SMatthias Ringwald         if (!mesh_proxy_service_packet_handler) return 0;
131*d567aeb3SMatthias Ringwald         (*mesh_proxy_service_packet_handler)(MESH_PROXY_DATA_PACKET, con_handle, buffer, buffer_size);
132*d567aeb3SMatthias Ringwald         return 0;
133*d567aeb3SMatthias Ringwald     }
134*d567aeb3SMatthias Ringwald 
135*d567aeb3SMatthias Ringwald     if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){
136*d567aeb3SMatthias Ringwald         if (buffer_size < 2){
137*d567aeb3SMatthias Ringwald             return ATT_ERROR_INVALID_OFFSET;
138*d567aeb3SMatthias Ringwald         }
139*d567aeb3SMatthias Ringwald         instance->data_out_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
140*d567aeb3SMatthias Ringwald         log_info("mesh_proxy_service_write_callback: data out notify enabled %d, con handle 0x%02x", instance->data_out_client_configuration_descriptor_value, con_handle);
141*d567aeb3SMatthias Ringwald         if (instance->data_out_client_configuration_descriptor_value){
142*d567aeb3SMatthias Ringwald             mesh_proxy_service_emit_connected(con_handle);
143*d567aeb3SMatthias Ringwald         } else {
144*d567aeb3SMatthias Ringwald             mesh_proxy_service_emit_disconnected(con_handle);
145*d567aeb3SMatthias Ringwald         }
146*d567aeb3SMatthias Ringwald         return 0;
147*d567aeb3SMatthias Ringwald     }
148*d567aeb3SMatthias Ringwald     log_info("mesh_proxy_service_write_callback: not handled write on handle 0x%02x, buffer size %d", attribute_handle, buffer_size);
149*d567aeb3SMatthias Ringwald     return 0;
150*d567aeb3SMatthias Ringwald }
151*d567aeb3SMatthias Ringwald 
mesh_proxy_service_server_init(void)152*d567aeb3SMatthias Ringwald void mesh_proxy_service_server_init(void){
153*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = &mesh_proxy;
154*d567aeb3SMatthias Ringwald     if (!instance){
155*d567aeb3SMatthias Ringwald         log_error("mesh_proxy_service_server_init: instance is null");
156*d567aeb3SMatthias Ringwald         return;
157*d567aeb3SMatthias Ringwald     }
158*d567aeb3SMatthias Ringwald 
159*d567aeb3SMatthias Ringwald     // get service handle range
160*d567aeb3SMatthias Ringwald     uint16_t start_handle = 0;
161*d567aeb3SMatthias Ringwald     uint16_t end_handle   = 0xffff;
162*d567aeb3SMatthias Ringwald     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_MESH_PROXY, &start_handle, &end_handle);
163*d567aeb3SMatthias Ringwald 	btstack_assert(service_found != 0);
164*d567aeb3SMatthias Ringwald 	UNUSED(service_found);
165*d567aeb3SMatthias Ringwald 
166*d567aeb3SMatthias Ringwald     instance->data_in_client_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROXY_DATA_IN);
167*d567aeb3SMatthias Ringwald     instance->data_out_client_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROXY_DATA_OUT);
168*d567aeb3SMatthias Ringwald     instance->data_out_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROXY_DATA_OUT);
169*d567aeb3SMatthias Ringwald 
170*d567aeb3SMatthias Ringwald     log_info("DataIn     value handle 0x%02x", instance->data_in_client_value_handle);
171*d567aeb3SMatthias Ringwald     log_info("DataOut    value handle 0x%02x", instance->data_out_client_value_handle);
172*d567aeb3SMatthias Ringwald     log_info("DataOut CC value handle 0x%02x", instance->data_out_client_configuration_descriptor_handle);
173*d567aeb3SMatthias Ringwald 
174*d567aeb3SMatthias Ringwald     mesh_proxy_service.start_handle   = start_handle;
175*d567aeb3SMatthias Ringwald     mesh_proxy_service.end_handle     = end_handle;
176*d567aeb3SMatthias Ringwald     mesh_proxy_service.read_callback  = &mesh_proxy_service_read_callback;
177*d567aeb3SMatthias Ringwald     mesh_proxy_service.write_callback = &mesh_proxy_service_write_callback;
178*d567aeb3SMatthias Ringwald 
179*d567aeb3SMatthias Ringwald     att_server_register_service_handler(&mesh_proxy_service);
180*d567aeb3SMatthias Ringwald }
181*d567aeb3SMatthias Ringwald 
mesh_proxy_service_server_send_proxy_pdu(uint16_t con_handle,const uint8_t * proxy_pdu,uint16_t proxy_pdu_size)182*d567aeb3SMatthias Ringwald void mesh_proxy_service_server_send_proxy_pdu(uint16_t con_handle, const uint8_t * proxy_pdu, uint16_t proxy_pdu_size){
183*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle);
184*d567aeb3SMatthias Ringwald     if (!instance){
185*d567aeb3SMatthias Ringwald         log_error("mesh_proxy_service_server_data_out_can_send_now: instance is null");
186*d567aeb3SMatthias Ringwald         return;
187*d567aeb3SMatthias Ringwald     }
188*d567aeb3SMatthias Ringwald     att_server_notify(instance->con_handle, instance->data_out_client_value_handle, proxy_pdu, proxy_pdu_size);
189*d567aeb3SMatthias Ringwald }
190*d567aeb3SMatthias Ringwald 
mesh_proxy_service_can_send_now(void * context)191*d567aeb3SMatthias Ringwald static void mesh_proxy_service_can_send_now(void * context){
192*d567aeb3SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
193*d567aeb3SMatthias Ringwald     // notify client
194*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle);
195*d567aeb3SMatthias Ringwald     if (!instance){
196*d567aeb3SMatthias Ringwald         log_error("no instance for handle 0x%02x", con_handle);
197*d567aeb3SMatthias Ringwald         return;
198*d567aeb3SMatthias Ringwald     }
199*d567aeb3SMatthias Ringwald 
200*d567aeb3SMatthias Ringwald     if (!mesh_proxy_service_packet_handler) return;
201*d567aeb3SMatthias Ringwald     uint8_t buffer[5];
202*d567aeb3SMatthias Ringwald     buffer[0] = HCI_EVENT_MESH_META;
203*d567aeb3SMatthias Ringwald     buffer[1] = 3;
204*d567aeb3SMatthias Ringwald     buffer[2] = MESH_SUBEVENT_CAN_SEND_NOW;
205*d567aeb3SMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
206*d567aeb3SMatthias Ringwald     (*mesh_proxy_service_packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
207*d567aeb3SMatthias Ringwald }
208*d567aeb3SMatthias Ringwald 
mesh_proxy_service_server_request_can_send_now(hci_con_handle_t con_handle)209*d567aeb3SMatthias Ringwald void mesh_proxy_service_server_request_can_send_now(hci_con_handle_t con_handle){
210*d567aeb3SMatthias Ringwald     mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle);
211*d567aeb3SMatthias Ringwald     if (!instance){
212*d567aeb3SMatthias Ringwald         log_error("mesh_proxy_service_server_request_can_send_now: instance is null, 0x%2x", con_handle);
213*d567aeb3SMatthias Ringwald         return;
214*d567aeb3SMatthias Ringwald     }
215*d567aeb3SMatthias Ringwald     instance->pdu_response_callback.callback = &mesh_proxy_service_can_send_now;
216*d567aeb3SMatthias Ringwald     instance->pdu_response_callback.context  = (void*) (uintptr_t) con_handle;
217*d567aeb3SMatthias Ringwald     att_server_register_can_send_now_callback(&instance->pdu_response_callback, con_handle);
218*d567aeb3SMatthias Ringwald }
219*d567aeb3SMatthias Ringwald 
mesh_proxy_service_server_register_packet_handler(btstack_packet_handler_t callback)220*d567aeb3SMatthias Ringwald void mesh_proxy_service_server_register_packet_handler(btstack_packet_handler_t callback){
221*d567aeb3SMatthias Ringwald     mesh_proxy_service_packet_handler = callback;
222*d567aeb3SMatthias Ringwald }
223