ble server: added calculate bytes to copy

This commit is contained in:
mila@ringwald.ch 2014-07-04 15:17:38 +00:00
parent ff8c64ca60
commit 41f1a65d77

View File

@ -115,50 +115,50 @@ void hexdump2(void *data, int size){
// enable LE, setup ADV data // enable LE, setup ADV data
static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return;
bd_addr_t addr; bd_addr_t addr;
uint8_t adv_data[] = { 02, 01, 05, 03, 02, 0xf0, 0xff }; uint8_t adv_data[] = { 02, 01, 05, 03, 02, 0xf0, 0xff };
switch (packet_type) {
switch (packet[0]) {
case BTSTACK_EVENT_STATE:
// bt stack activated, get started - set local name
if (packet[2] == HCI_STATE_WORKING) {
printf("Working!\n");
hci_send_cmd(&hci_le_set_advertising_data, sizeof(adv_data), adv_data);
}
break;
case HCI_EVENT_PACKET: case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
switch (packet[0]) { if (packet[2]) {
overwriteLine(4, "CONNECTED");
case BTSTACK_EVENT_STATE: } else {
// bt stack activated, get started - set local name overwriteLine(4, "NOT CONNECTED");
if (packet[2] == HCI_STATE_WORKING) { }
printf("Working!\n"); break;
hci_send_cmd(&hci_le_set_advertising_data, sizeof(adv_data), adv_data);
} case HCI_EVENT_DISCONNECTION_COMPLETE:
break; // restart advertising
hci_send_cmd(&hci_le_set_advertise_enable, 1);
case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED: break;
if (packet[2]) {
overwriteLine(4, "CONNECTED"); case HCI_EVENT_COMMAND_COMPLETE:
} else { if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){
overwriteLine(4, "NOT CONNECTED"); bt_flip_addr(addr, &packet[6]);
} printf("BD ADDR: %s\n", bd_addr_to_str(addr));
break; break;
}
case HCI_EVENT_DISCONNECTION_COMPLETE: if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_data)){
// restart advertising hci_send_cmd(&hci_le_set_scan_response_data, 10, adv_data);
hci_send_cmd(&hci_le_set_advertise_enable, 1); break;
break; }
if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_scan_response_data)){
case HCI_EVENT_COMMAND_COMPLETE: hci_send_cmd(&hci_le_set_advertise_enable, 1);
if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){ break;
bt_flip_addr(addr, &packet[6]); }
printf("BD ADDR: %s\n", bd_addr_to_str(addr)); default:
break; break;
} }
if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_data)){
hci_send_cmd(&hci_le_set_scan_response_data, 10, adv_data);
break;
}
if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_scan_response_data)){
hci_send_cmd(&hci_le_set_advertise_enable, 1);
break;
}
}
}
} }
// test profile // test profile
@ -196,17 +196,23 @@ static uint16_t get_write_att_value_len(uint16_t att_handle){
return value_len; return value_len;
} }
uint16_t att_read_callback(uint16_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ static uint16_t get_bytes_to_copy(uint16_t value_len, uint16_t offset){
printf("READ Callback, handle %04x\n", att_handle);
uint16_t value_len = get_att_read_value_len(att_handle);
if (!buffer) return value_len;
if (value_len <= offset ) return 0; if (value_len <= offset ) return 0;
uint16_t bytes_to_copy = value_len - offset; uint16_t bytes_to_copy = value_len - offset;
if (bytes_to_copy > buffer_size) { if (bytes_to_copy > buffer_size) {
bytes_to_copy = buffer-size; bytes_to_copy = buffer_size;
} }
return bytes_to_copy;
}
uint16_t att_read_callback(uint16_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
printf("READ Callback, handle %04x\n", att_handle);
uint16_t value_len = get_att_read_value_len(att_handle);
if (!buffer) return value_len;
uint16_t bytes_to_copy = get_bytes_to_copy(value_len, offset);
if (!bytes_to_copy) return 0;
switch(att_handle){ switch(att_handle){
case ATT_CHARACTERISTIC_FFF1_01_HANDLE: case ATT_CHARACTERISTIC_FFF1_01_HANDLE:
@ -224,12 +230,8 @@ static int att_write_callback(uint16_t con_handle, uint16_t att_handle, uint16_t
printf("WRITE Callback, handle %04x\n", att_handle); printf("WRITE Callback, handle %04x\n", att_handle);
uint16_t value_len = get_write_att_value_len(att_handle); uint16_t value_len = get_write_att_value_len(att_handle);
if (value_len <= offset ) return ATT_ERROR_INVALID_OFFSET; uint16_t bytes_to_copy = get_bytes_to_copy(value_len, offset);
if (!bytes_to_copy) return ATT_ERROR_INVALID_OFFSET;
uint16_t bytes_to_copy = value_len - offset;
if (bytes_to_copy > buffer_size) {
bytes_to_copy = buffer-size;
}
switch(att_handle){ switch(att_handle){
case ATT_CHARACTERISTIC_FFF1_01_HANDLE: case ATT_CHARACTERISTIC_FFF1_01_HANDLE: