diff --git a/example/rfcomm-echo.c b/example/rfcomm-echo.c index 100411b25..f12f8940a 100644 --- a/example/rfcomm-echo.c +++ b/example/rfcomm-echo.c @@ -52,6 +52,75 @@ uint16_t con_handle; int rfcomm_channel = 1; char pin[17]; +uint8_t service_buffer[100]; +void create_spp_service(uint8_t *service, int service_id){ + + uint8_t* attribute; + de_create_sequence(service); + + // 0x0001 "Service Class ID List" + de_add_number(service, DE_UINT, DE_SIZE_16, 0x0001); + attribute = de_push_sequence(service); + { + de_add_number(attribute, DE_UUID, DE_SIZE_16, 0x1101 ); + } + de_pop_sequence(service, attribute); + + // 0x0004 "Protocol Descriptor List" + de_add_number(service, DE_UINT, DE_SIZE_16, 0x0004); + attribute = de_push_sequence(service); + { + uint8_t* l2cpProtocol = de_push_sequence(attribute); + { + de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, 0x0100); + } + de_pop_sequence(attribute, l2cpProtocol); + + uint8_t* rfcomm = de_push_sequence(attribute); + { + de_add_number(rfcomm, DE_UUID, DE_SIZE_16, 0x0003); // rfcomm_service + de_add_number(rfcomm, DE_UINT, DE_SIZE_8, service_id); // rfcomm channel + } + de_pop_sequence(attribute, rfcomm); + } + de_pop_sequence(service, attribute); + + // 0x0005 "Public Browse Group" + de_add_number(service, DE_UINT, DE_SIZE_16, 0x0005); // public browse group + attribute = de_push_sequence(service); + { + de_add_number(attribute, DE_UUID, DE_SIZE_16, 0x1002 ); + } + de_pop_sequence(service, attribute); + + // 0x0006 + de_add_number(service, DE_UINT, DE_SIZE_16, SDP_LanguageBaseAttributeIDList); + attribute = de_push_sequence(service); + { + de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e); + de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a); + de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100); + } + de_pop_sequence(service, attribute); + + // 0x0009 "Bluetooth Profile Descriptor List" + de_add_number(service, DE_UINT, DE_SIZE_16, 0x0009); + attribute = de_push_sequence(service); + { + uint8_t *sppProfile = de_push_sequence(attribute); + { + de_add_number(sppProfile, DE_UUID, DE_SIZE_16, 0x1101); + de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0100); + } + de_pop_sequence(attribute, sppProfile); + } + de_pop_sequence(service, attribute); + + // 0x0100 "ServiceName" + de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); + de_add_data(service, DE_STRING, 3, (uint8_t *) "SPP ECHO"); +} + void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ bd_addr_t event_addr; uint16_t mtu; @@ -82,10 +151,23 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint case BTSTACK_EVENT_STATE: // bt stack activated, get started if (packet[2] == HCI_STATE_WORKING) { - bt_send_cmd(&btstack_set_discoverable, 1); + // register RFCOMM channel + bt_send_cmd(&rfcomm_register_service, 0x4711, 100); // random registration ID } break; - + + case RFCOMM_EVENT_SERVICE_REGISTERED: + rfcomm_channel_nr = packet[5]; + printf("RFCOMM channel %u was assigned by BTdaemon\n", rfcomm_channel_nr); + // register SDP for our SPP + create_spp_service(service_buffer, rfcomm_channel_nr); + bt_send_cmd(&sdp_register_service_record, service_buffer); + break; + + case SDP_SERVICE_REGISTERED: + bt_send_cmd(&btstack_set_discoverable, 1); + break; + case HCI_EVENT_PIN_CODE_REQUEST: // inform about pin code request printf("Using PIN 0000\n"); @@ -131,74 +213,6 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint } } -uint8_t service_buffer[100]; -void create_spp_service(uint8_t *service){ - - uint8_t* attribute; - de_create_sequence(service); - - // 0x0001 "Service Class ID List" - de_add_number(service, DE_UINT, DE_SIZE_16, 0x0001); - attribute = de_push_sequence(service); - { - de_add_number(attribute, DE_UUID, DE_SIZE_16, 0x1101 ); - } - de_pop_sequence(service, attribute); - - // 0x0004 "Protocol Descriptor List" - de_add_number(service, DE_UINT, DE_SIZE_16, 0x0004); - attribute = de_push_sequence(service); - { - uint8_t* l2cpProtocol = de_push_sequence(attribute); - { - de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, 0x0100); - } - de_pop_sequence(attribute, l2cpProtocol); - - uint8_t* rfcomm = de_push_sequence(attribute); - { - de_add_number(rfcomm, DE_UUID, DE_SIZE_16, 0x0003); // rfcomm_service - de_add_number(rfcomm, DE_UINT, DE_SIZE_8, 0x0001); // rfcomm channel - } - de_pop_sequence(attribute, rfcomm); - } - de_pop_sequence(service, attribute); - - // 0x0005 "Public Browse Group" - de_add_number(service, DE_UINT, DE_SIZE_16, 0x0005); // public browse group - attribute = de_push_sequence(service); - { - de_add_number(attribute, DE_UUID, DE_SIZE_16, 0x1002 ); - } - de_pop_sequence(service, attribute); - - // 0x0006 - de_add_number(service, DE_UINT, DE_SIZE_16, SDP_LanguageBaseAttributeIDList); - attribute = de_push_sequence(service); - { - de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e); - de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a); - de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100); - } - de_pop_sequence(service, attribute); - - // 0x0009 "Bluetooth Profile Descriptor List" - de_add_number(service, DE_UINT, DE_SIZE_16, 0x0009); - attribute = de_push_sequence(service); - { - uint8_t *sppProfile = de_push_sequence(attribute); - { - de_add_number(sppProfile, DE_UUID, DE_SIZE_16, 0x1101); - de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0100); - } - de_pop_sequence(attribute, sppProfile); - } - de_pop_sequence(service, attribute); - - // 0x0100 "ServiceName" - de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); - de_add_data(service, DE_STRING, 3, (uint8_t *) "SPP ECHO"); -} int main (int argc, const char * argv[]){ @@ -209,13 +223,6 @@ int main (int argc, const char * argv[]){ return 1; } bt_register_packet_handler(packet_handler); - - // register RFCOM channel - bt_send_cmd(&rfcomm_register_service, 1, 100); - - // register SDP for our SPP - create_spp_service(service_buffer); - bt_send_cmd(&sdp_register_service_record, service_buffer); bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); run_loop_execute();