1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
91
92
93
94
95
96
99
100
103
104
110
111
112
113
114
115
116
117
118
119
120
121
122
136
137
138
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
186
189
190
196
199
/* ... */
#define BTSTACK_FILE__ "nordic_spp_service_server.c"
/* ... */
#include <stdint.h>
#include <string.h>
#include "ble/att_db.h"
#include "ble/att_server.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "ble/gatt-service/nordic_spp_service_server.h"
7 includes
static att_service_handler_t nordic_spp_service;
static btstack_packet_handler_t client_packet_handler;
static btstack_packet_callback_registration_t nordic_spp_service_hci_event_callback_registration;
static uint16_t nordic_spp_rx_value_handle;
static uint16_t nordic_spp_tx_value_handle;
static uint16_t nordic_spp_tx_client_configuration_handle;
static uint16_t nordic_spp_tx_client_configuration_value;
static hci_con_handle_t nordic_spp_con_handle;
static void nordic_spp_service_emit_state(hci_con_handle_t con_handle, bool enabled){
uint8_t event[5];
uint8_t pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = enabled ? GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED : GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED;
little_endian_store_16(event,pos, (uint16_t) con_handle);
pos += 2;
(*client_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
}{ ... }
static uint16_t nordic_spp_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
UNUSED(con_handle);
UNUSED(offset);
UNUSED(buffer_size);
if (attribute_handle == nordic_spp_tx_client_configuration_handle){
if (buffer != NULL){
little_endian_store_16(buffer, 0, nordic_spp_tx_client_configuration_value);
}if (buffer != NULL) { ... }
return 2;
}if (attribute_handle == nordic_spp_tx_client_configuration_handle) { ... }
return 0;
}{ ... }
static int nordic_spp_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){
UNUSED(offset);
UNUSED(buffer_size);
if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
return 0;
}if (transaction_mode != ATT_TRANSACTION_MODE_NONE) { ... }
if (attribute_handle == nordic_spp_rx_value_handle){
(*client_packet_handler)(RFCOMM_DATA_PACKET, (uint16_t) con_handle, buffer, buffer_size);
}if (attribute_handle == nordic_spp_rx_value_handle) { ... }
if (attribute_handle == nordic_spp_tx_client_configuration_handle){
nordic_spp_tx_client_configuration_value = little_endian_read_16(buffer, 0);
bool enabled = (nordic_spp_tx_client_configuration_value != 0);
nordic_spp_con_handle = con_handle;
nordic_spp_service_emit_state(con_handle, enabled);
}if (attribute_handle == nordic_spp_tx_client_configuration_handle) { ... }
return 0;
}{ ... }
static void nordic_spp_service_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
if (packet_type != HCI_EVENT_PACKET) return;
hci_con_handle_t con_handle;
switch (hci_event_packet_get_type(packet)){
case HCI_EVENT_DISCONNECTION_COMPLETE:
con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
if (con_handle == nordic_spp_con_handle){
nordic_spp_con_handle = HCI_CON_HANDLE_INVALID;
if (nordic_spp_tx_client_configuration_value > 0){
nordic_spp_tx_client_configuration_value = 0;
nordic_spp_service_emit_state(con_handle, false);
}if (nordic_spp_tx_client_configuration_value > 0) { ... }
}if (con_handle == nordic_spp_con_handle) { ... }
break;case HCI_EVENT_DISCONNECTION_COMPLETE:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
}{ ... }
/* ... */
void nordic_spp_service_server_init(btstack_packet_handler_t packet_handler){
static const uint8_t nordic_spp_profile_uuid128[] = { 0x6E, 0x40, 0x00, 0x01, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
static const uint8_t nordic_spp_rx_uuid128[] = { 0x6E, 0x40, 0x00, 0x02, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
static const uint8_t nordic_spp_tx_uuid128[] = { 0x6E, 0x40, 0x00, 0x03, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
client_packet_handler = packet_handler;
uint16_t start_handle = 0;
uint16_t end_handle = 0xffff;
int service_found = gatt_server_get_handle_range_for_service_with_uuid128(nordic_spp_profile_uuid128, &start_handle, &end_handle);
btstack_assert(service_found != 0);
UNUSED(service_found);
nordic_spp_rx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_rx_uuid128);
nordic_spp_tx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
nordic_spp_tx_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
log_info("nordic_spp_rx_value_handle 0x%02x", nordic_spp_rx_value_handle);
log_info("nordic_spp_tx_value_handle 0x%02x", nordic_spp_tx_value_handle);
log_info("nordic_spp_tx_client_configuration_handle 0x%02x", nordic_spp_tx_client_configuration_handle);
nordic_spp_service.start_handle = start_handle;
nordic_spp_service.end_handle = end_handle;
nordic_spp_service.read_callback = &nordic_spp_service_read_callback;
nordic_spp_service.write_callback = &nordic_spp_service_write_callback;
att_server_register_service_handler(&nordic_spp_service);
nordic_spp_service_hci_event_callback_registration.callback = &nordic_spp_service_hci_event_handler;
hci_add_event_handler(&nordic_spp_service_hci_event_callback_registration);
nordic_spp_con_handle = HCI_CON_HANDLE_INVALID;
}{ ... }
/* ... */
void nordic_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
att_server_request_to_send_notification(request, con_handle);
}{ ... }
/* ... */
int nordic_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
return att_server_notify(con_handle, nordic_spp_tx_value_handle, data, size);
}{ ... }