int btstack_main(int argc, const char * argv[]){ // init L2CAP l2cap_init(); l2cap_register_packet_handler(packet_handler); // init RFCOMM rfcomm_init(); rfcomm_register_packet_handler(packet_handler); rfcomm_register_service_with_initial_credits_internal(NULL, rfcomm_channel_nr, 0xffff, 1); // reserved channel, mtu limited by l2cap, 1 credit // init SDP, create record for SPP and register with SDP sdp_init(); memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer; sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, 1, "SPP Counter"); printf("SDP service buffer size: %u\n\r", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record))); sdp_register_service_internal(NULL, service_record_item); // set one-shot timer heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); puts("SPP FlowControl Demo: simulates processing on received data...\n\r"); gap_set_local_name("BTstack SPP Flow Control"); hci_discoverable_control(1); // turn on! hci_power_control(HCI_POWER_ON); return 0; }
int main(void) { printf("BTstack LE Peripheral starting up...\n"); setup(); setup_cli(); gap_random_address_set_update_period(300000); gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE); strcpy(gap_device_name, "BTstack"); sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); sm_io_capabilities = "IO_CAPABILITY_NO_INPUT_NO_OUTPUT"; sm_set_authentication_requirements(0); sm_register_oob_data_callback(get_oob_data_callback); sm_set_encryption_key_size_range(sm_min_key_size, 16); sm_test_set_irk(test_irk); // set one-shot timer heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); // turn on! hci_power_control(HCI_POWER_ON); // go! run_loop_execute(); // happy compiler! return 0; }
static void ehcill_sleep_ack_timer_setup(void){ // setup timer ehcill_sleep_ack_timer.process = &ehcill_sleep_ack_timer_handler; run_loop_set_timer(&ehcill_sleep_ack_timer, 50); run_loop_add_timer(&ehcill_sleep_ack_timer); embedded_trigger(); }
int btstack_main(int argc, const char * argv[]){ // init L2CAP l2cap_init(); l2cap_register_packet_handler(packet_handler); // init RFCOMM rfcomm_init(); rfcomm_register_packet_handler(packet_handler); rfcomm_register_service_internal(NULL, rfcomm_channel_nr, 100); // reserved channel, mtu=100 // init SDP, create record for SPP and register with SDP sdp_init(); memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer; sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, 1, "SPP Counter"); printf("SDP service buffer size: %u\n\r", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record))); sdp_register_service_internal(NULL, service_record_item); // set one-shot timer timer_source_t heartbeat; heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); // set local name gap_set_local_name("BlueMSP-Demo"); // make discoverable hci_discoverable_control(1); printf("Run...\n\r"); // turn on! hci_power_control(HCI_POWER_ON); return 0; }
static void heartbeat_handler(struct timer *ts){ if (rfcomm_send_credit){ rfcomm_grant_credits(rfcomm_channel_id, 1); rfcomm_send_credit = 0; } run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); run_loop_add_timer(ts); }
static void heartbeat_handler(struct timer *ts){ // restart timer run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); run_loop_add_timer(ts); counter++; update_client = 1; app_run(); }
static void start_power_off_timer(void){ #ifdef USE_POWER_OFF_TIMER stop_power_off_timer(); run_loop_set_timer(&timeout, DAEMON_NO_ACTIVE_CLIENT_TIMEOUT); run_loop_add_timer(&timeout); timeout_active = 1; #else hci_power_control(HCI_POWER_OFF); #endif }
static void h4_enforce_wake_on(void) { if (!enforce_wake_device) return; if (!enforce_wake_fd) { enforce_wake_fd = open(enforce_wake_device, O_RDWR); usleep(HCI_WAKE_DURATION); // wait until device is ready } run_loop_remove_timer(&hci_transport_h4->sleep_timer); run_loop_set_timer(&hci_transport_h4->sleep_timer, HCI_WAKE_TIMER_MS); hci_transport_h4->sleep_timer.process = h4_enforce_wake_timeout; run_loop_add_timer(&hci_transport_h4->sleep_timer); }
int btstack_main(void) { hci_discoverable_control(1); l2cap_init(); l2cap_register_packet_handler(packet_handler); rfcomm_init(); rfcomm_register_packet_handler(packet_handler); rfcomm_register_service_internal(NULL, RFCOMM_SERVER_CHANNEL, 0xffff); // init SDP, create record for SPP and register with SDP sdp_init(); memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); /* LISTING_PAUSE */ #ifdef EMBEDDED /* LISTING_RESUME */ service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer; sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, RFCOMM_SERVER_CHANNEL, "SPP Counter"); printf("SDP service buffer size: %u\n", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record))); sdp_register_service_internal(NULL, service_record_item); /* LISTING_PAUSE */ #else sdp_create_spp_service( spp_service_buffer, RFCOMM_SERVER_CHANNEL, "SPP Counter"); printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); sdp_register_service_internal(NULL, spp_service_buffer); #endif /* LISTING_RESUME */ hci_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); // setup le device db le_device_db_init(); // setup SM: Display only sm_init(); // setup ATT server att_server_init(profile_data, att_read_callback, att_write_callback); att_dump_attributes(); // set one-shot timer heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); // turn on! hci_power_control(HCI_POWER_ON); return 0; }
void heartbeat_handler(struct timer *ts){ if (rfcomm_channel_id){ static int counter = 0; char lineBuffer[30]; sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter); puts(lineBuffer); if (rfcomm_can_send_packet_now(rfcomm_channel_id)) { int err = rfcomm_send_internal(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer)); if (err) printf("rfcomm_send_internal -> error 0X%02x", err); } } run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); run_loop_add_timer(ts); }
int att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len){ if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS; if (!l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; // track indication att_handle_value_indication_handle = handle; run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout); run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); run_loop_add_timer(&att_handle_value_indication_timer); l2cap_reserve_packet_buffer(); uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); uint16_t size = att_prepare_handle_value_indication(&att_connection, handle, value, value_len, packet_buffer); l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); return 0; }
int btstack_main(int argc, const char * argv[]){ printf("BTstack LE Peripheral starting up...\n"); // set up l2cap_le l2cap_init(); // setup le device db le_device_db_init(); // setup SM: Display only sm_init(); sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); sm_set_authentication_requirements( SM_AUTHREQ_BONDING | SM_AUTHREQ_MITM_PROTECTION); // setup ATT server att_server_init(profile_data, att_read_callback, att_write_callback); att_write_queue_init(); att_attributes_init(); att_server_register_packet_handler(app_packet_handler); att_dump_attributes(); btstack_stdin_setup(stdin_process); gap_random_address_set_update_period(300000); gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE); strcpy(gap_device_name, "BTstack"); sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); sm_io_capabilities = "IO_CAPABILITY_NO_INPUT_NO_OUTPUT"; sm_set_authentication_requirements(0); sm_register_oob_data_callback(get_oob_data_callback); sm_set_encryption_key_size_range(sm_min_key_size, 16); sm_test_set_irk(test_irk); // set one-shot timer heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); // turn on! hci_power_control(HCI_POWER_ON); return 0; }
static void hci_connection_timeout_handler(timer_source_t *timer){ hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); #ifdef HAVE_TIME struct timeval tv; gettimeofday(&tv, NULL); if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { // connections might be timed out hci_emit_l2cap_check_timeout(connection); } #endif #ifdef HAVE_TICK if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ // connections might be timed out hci_emit_l2cap_check_timeout(connection); } #endif run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); run_loop_add_timer(timer); }
// main == setup int main(void) { setup(); // set one-shot timer timer_source_t heartbeat; heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); // turn on! hci_power_control(HCI_POWER_ON); // go! run_loop_execute(); // happy compiler! return 0; }
int btstack_main(int argc, const char * argv[]){ hci_discoverable_control(1); l2cap_init(); l2cap_register_packet_handler(packet_handler); rfcomm_init(); rfcomm_register_packet_handler(packet_handler); rfcomm_register_service_internal(NULL, RFCOMM_SERVER_CHANNEL, 0xffff); // reserved channel, mtu limited by l2cap // init SDP, create record for SPP and register with SDP sdp_init(); memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); #ifdef EMBEDDED service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer; sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, RFCOMM_SERVER_CHANNEL, "SPP Counter"); printf("SDP service buffer size: %u\n", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record))); sdp_register_service_internal(NULL, service_record_item); #else sdp_create_spp_service( spp_service_buffer, RFCOMM_SERVER_CHANNEL, "SPP Counter"); printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); sdp_register_service_internal(NULL, spp_service_buffer); #endif hci_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); // set one-shot timer timer_source_t heartbeat; heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); // turn on! hci_power_control(HCI_POWER_ON); // go! run_loop_execute(); // happy compiler! return 0; }
/* LISTING_START(heartbeat): Combined Heartbeat handler */ static void heartbeat_handler(struct timer *ts){ counter++; counter_string_len = sprintf(counter_string, "BTstack counter %04u\n", counter); // printf("%s", counter_string); if (rfcomm_channel_id){ if (rfcomm_can_send_packet_now(rfcomm_channel_id)){ int err = rfcomm_send_internal(rfcomm_channel_id, (uint8_t*) counter_string, counter_string_len); if (err) { log_error("rfcomm_send_internal -> error 0X%02x", err); } } } if (le_notification_enabled) { att_server_notify(ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len); } run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); run_loop_add_timer(ts); }
static void usb_process_ts(timer_source_t *timer) { // log_info("in usb_process_ts"); // timer is deactive, when timer callback gets called usb_timer_active = 0; if (libusb_state != LIB_USB_TRANSFERS_ALLOCATED) return; // actually handled the packet in the pollfds function usb_process_ds((struct data_source *) NULL); // Get the amount of time until next event is due long msec = AYSNC_POLLING_INTERVAL_MS; // Activate timer run_loop_set_timer(&usb_timer, msec); run_loop_add_timer(&usb_timer); usb_timer_active = 1; return; }
static void one_shot_timer_setup(){ // set one-shot timer heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); }
static void event_handler(uint8_t *packet, int size){ bd_addr_t addr; uint8_t link_type; hci_con_handle_t handle; hci_connection_t * conn; int i; switch (packet[0]) { case HCI_EVENT_COMMAND_COMPLETE: // get num cmd packets // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); hci_stack.num_cmd_packets = packet[2]; if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ // from offset 5 // status // "The HC_ACL_Data_Packet_Length return parameter will be used to determine the size of the L2CAP segments contained in ACL Data Packets" hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); // ignore: SCO data packet len (8) hci_stack.total_num_acl_packets = packet[9]; // ignore: total num SCO packets if (hci_stack.state == HCI_STATE_INITIALIZING){ // determine usable ACL payload size if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; } // determine usable ACL packet types hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); } } if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ hci_emit_discoverable_enabled(hci_stack.discoverable); } break; case HCI_EVENT_COMMAND_STATUS: // get num cmd packets // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); hci_stack.num_cmd_packets = packet[3]; break; case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: for (i=0; i<packet[2];i++){ handle = READ_BT_16(packet, 3 + 2*i); uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); conn = connection_for_handle(handle); if (!conn){ log_error("hci_number_completed_packet lists unused con handle %u\n", handle); continue; } conn->num_acl_packets_sent -= num_packets; // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); } break; case HCI_EVENT_CONNECTION_REQUEST: bt_flip_addr(addr, &packet[2]); // TODO: eval COD 8-10 link_type = packet[11]; log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); if (link_type == 1) { // ACL conn = connection_for_address(addr); if (!conn) { conn = create_connection_for_addr(addr); } if (!conn) { // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) hci_stack.decline_reason = 0x0d; BD_ADDR_COPY(hci_stack.decline_addr, addr); break; } conn->state = RECEIVED_CONNECTION_REQUEST; hci_run(); } else { // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) hci_stack.decline_reason = 0x0a; BD_ADDR_COPY(hci_stack.decline_addr, addr); } break; case HCI_EVENT_CONNECTION_COMPLETE: // Connection management bt_flip_addr(addr, &packet[5]); log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); conn = connection_for_address(addr); if (conn) { if (!packet[2]){ conn->state = OPEN; conn->con_handle = READ_BT_16(packet, 3); #ifdef HAVE_TICK // restart timer run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); run_loop_add_timer(&conn->timeout); #endif log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); hci_emit_nr_connections_changed(); } else { // connection failed, remove entry linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); btstack_memory_hci_connection_free( conn ); // if authentication error, also delete link key if (packet[2] == 0x05) { hci_drop_link_key_for_bd_addr(&addr); } } } break; case HCI_EVENT_LINK_KEY_REQUEST: log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); if (!hci_stack.remote_device_db) break; hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); hci_run(); // request already answered return; case HCI_EVENT_LINK_KEY_NOTIFICATION: hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); if (!hci_stack.remote_device_db) break; bt_flip_addr(addr, &packet[2]); hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); // still forward event to allow dismiss of pairing dialog break; case HCI_EVENT_PIN_CODE_REQUEST: hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); break; #ifndef EMBEDDED case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: if (!hci_stack.remote_device_db) break; if (packet[2]) break; // status not ok bt_flip_addr(addr, &packet[3]); // fix for invalid remote names - terminate on 0xff for (i=0; i<248;i++){ if (packet[9+i] == 0xff){ packet[9+i] = 0; break; } } memset(&device_name, 0, sizeof(device_name_t)); strncpy((char*) device_name, (char*) &packet[9], 248); hci_stack.remote_device_db->put_name(&addr, &device_name); break; case HCI_EVENT_INQUIRY_RESULT: case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: if (!hci_stack.remote_device_db) break; // first send inq result packet hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); // then send cached remote names for (i=0; i<packet[2];i++){ bt_flip_addr(addr, &packet[3+i*6]); if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ hci_emit_remote_name_cached(&addr, &device_name); } } return; #endif case HCI_EVENT_DISCONNECTION_COMPLETE: if (!packet[2]){ handle = READ_BT_16(packet, 3); hci_connection_t * conn = connection_for_handle(handle); if (conn) { hci_shutdown_connection(conn); } } break; case HCI_EVENT_HARDWARE_ERROR: if(hci_stack.control->hw_error){ (*hci_stack.control->hw_error)(); } break; #ifdef HAVE_BLE case HCI_EVENT_LE_META: switch (packet[2]) { case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: // Connection management bt_flip_addr(addr, &packet[8]); log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); // LE connections are auto-accepted, so just create a connection if there isn't one already conn = connection_for_address(addr); if (packet[3]){ if (conn){ // outgoing connection failed, remove entry linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); btstack_memory_hci_connection_free( conn ); } // if authentication error, also delete link key if (packet[3] == 0x05) { hci_drop_link_key_for_bd_addr(&addr); } break; } if (!conn){ conn = create_connection_for_addr(addr); } if (!conn){ // no memory break; } conn->state = OPEN; conn->con_handle = READ_BT_16(packet, 4); // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock // restart timer // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); // run_loop_add_timer(&conn->timeout); log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); hci_emit_nr_connections_changed(); break; default: break; } break; #endif default: break; } // handle BT initialization if (hci_stack.state == HCI_STATE_INITIALIZING){ // handle H4 synchronization loss on restart // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ // hci_stack.substate = 0; // } // handle normal init sequence if (hci_stack.substate % 2){ // odd: waiting for event if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ hci_stack.substate++; } } } // help with BT sleep if (hci_stack.state == HCI_STATE_FALLING_ASLEEP && hci_stack.substate == 1 && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ hci_stack.substate++; } hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); // execute main loop hci_run(); }
static void hsp_ringing_timeout_handler(timer_source_t * timer){ ag_ring = 1; run_loop_set_timer(&hs_timeout, 2000); // 2 seconds timeout run_loop_add_timer(&hs_timeout); }
// main int main(void) { // stop watchdog timer WDTCTL = WDTPW + WDTHOLD; //Initialize clock and peripherals halBoardInit(); halBoardStartXT1(); halBoardSetSystemClock(SYSCLK_16MHZ); // init debug UART halUsbInit(); // init LEDs LED_PORT_OUT |= LED_1 | LED_2; LED_PORT_DIR |= LED_1 | LED_2; /// GET STARTED with BTstack /// btstack_memory_init(); run_loop_init(RUN_LOOP_EMBEDDED); // init HCI hci_transport_t * transport = hci_transport_h4_dma_instance(); bt_control_t * control = bt_control_cc256x_instance(); hci_uart_config_t * config = hci_uart_config_cc256x_instance(); remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; hci_init(transport, config, control, remote_db); // use eHCILL bt_control_cc256x_enable_ehcill(1); // init L2CAP l2cap_init(); l2cap_register_packet_handler(packet_handler); // init RFCOMM rfcomm_init(); rfcomm_register_packet_handler(packet_handler); rfcomm_register_service_with_initial_credits_internal(NULL, rfcomm_channel_nr, 100, 1); // reserved channel, mtu=100, 1 credit // init SDP, create record for SPP and register with SDP sdp_init(); memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); service_record_item_t * service_record_item = (service_record_item_t *) spp_service_buffer; sdp_create_spp_service( (uint8_t*) &service_record_item->service_record, 1, "SPP Counter"); printf("SDP service buffer size: %u\n\r", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record))); sdp_register_service_internal(NULL, service_record_item); // set one-shot timer timer_source_t heartbeat; heartbeat.process = &heartbeat_handler; run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); run_loop_add_timer(&heartbeat); puts("SPP FlowControl Demo: simulates processing on received data...\n\r"); // ready - enable irq used in h4 task __enable_interrupt(); // turn on! hci_power_control(HCI_POWER_ON); // go! run_loop_execute(); // happy compiler! return 0; }
static void hsp_ringing_timer_start(void){ run_loop_remove_timer(&hs_timeout); run_loop_set_timer_handler(&hs_timeout, hsp_ringing_timeout_handler); run_loop_set_timer(&hs_timeout, 2000); // 2 seconds timeout run_loop_add_timer(&hs_timeout); }
void timer_handler(struct timer *ts){ bt_send_cmd(&hci_read_bd_addr); run_loop_set_timer(&timer, 3000); run_loop_add_timer(&timer); };
void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ bd_addr_t event_addr; uint16_t handle; uint16_t psm; uint16_t local_cid; char pin[20]; int i; switch (packet_type) { case L2CAP_DATA_PACKET: // measure data rate break; case HCI_EVENT_PACKET: switch (packet[0]) { case BTSTACK_EVENT_POWERON_FAILED: printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n"); exit(1); break; case BTSTACK_EVENT_STATE: // bt stack activated, get started if (packet[2] == HCI_STATE_WORKING) { if (serverMode) { printf("Waiting for incoming L2CAP connection on PSM %04x...\n", PSM_TEST); timer.process = timer_handler; run_loop_set_timer(&timer, 3000); // run_loop_add_timer(&timer); } else { bt_send_cmd(&hci_write_authentication_enable, 0); } } break; case HCI_EVENT_COMMAND_COMPLETE: // use pairing yes/no if ( COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable) ) { bt_send_cmd(&hci_write_class_of_device, 0x38010c); } if ( COMMAND_COMPLETE_EVENT(packet, hci_write_class_of_device) ) { bt_send_cmd(&l2cap_create_channel_mtu, addr, PSM_TEST, PACKET_SIZE); } break; case L2CAP_EVENT_INCOMING_CONNECTION: // data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) bt_flip_addr(event_addr, &packet[2]); handle = READ_BT_16(packet, 8); psm = READ_BT_16(packet, 10); local_cid = READ_BT_16(packet, 12); // remote_cid = READ_BT_16(packet, 14); printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x\n", bd_addr_to_str(event_addr), handle, psm, local_cid); // accept bt_send_cmd(&l2cap_accept_connection, local_cid); break; case HCI_EVENT_LINK_KEY_REQUEST: // link key request bt_flip_addr(event_addr, &packet[2]); bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr); break; case HCI_EVENT_PIN_CODE_REQUEST: // inform about pin code request printf("Please enter PIN here: "); fgets(pin, 20, stdin); i = strlen(pin); if( pin[i-1] == '\n' || pin[i-1] == '\r') { pin[i-1] = '\0'; i--; } printf("PIN (%u)= '%s'\n", i, pin); bt_flip_addr(event_addr, &packet[2]); bt_send_cmd(&hci_pin_code_request_reply, &event_addr, i, pin); break; case L2CAP_EVENT_CHANNEL_OPENED: // inform about new l2cap connection bt_flip_addr(event_addr, &packet[3]); psm = READ_BT_16(packet, 11); local_cid = READ_BT_16(packet, 13); handle = READ_BT_16(packet, 9); if (packet[2] == 0) { printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", bd_addr_to_str(event_addr), handle, psm, local_cid, READ_BT_16(packet, 15)); } else { printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_addr), packet[2]); } break; case HCI_EVENT_DISCONNECTION_COMPLETE: printf("Basebank connection closed\n"); break; case L2CAP_EVENT_CREDITS: if (!serverMode) { // can send! (assuming single credits are handet out) update_packet(); local_cid = READ_BT_16(packet, 2); bt_send_l2cap( local_cid, packet, PACKET_SIZE); } break; default: // other event break; } break; default: // other packet type break; } }
static void run_loop_register_timer(timer_source_t *timer, uint16_t period){ run_loop_set_timer(timer, period); run_loop_add_timer(timer); }
static int usb_open(void *transport_config){ int r; sco_state_machine_init(); sco_ring_init(); handle_packet = NULL; // default endpoint addresses event_in_addr = 0x81; // EP1, IN interrupt acl_in_addr = 0x82; // EP2, IN bulk acl_out_addr = 0x02; // EP2, OUT bulk sco_in_addr = 0x83; // EP3, IN isochronous sco_out_addr = 0x03; // EP3, OUT isochronous // USB init r = libusb_init(NULL); if (r < 0) return -1; libusb_state = LIB_USB_OPENED; // configure debug level libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_WARNING); #ifdef HAVE_USB_VENDOR_ID_AND_PRODUCT_ID // Use a specified device log_info("Want vend: %04x, prod: %04x", USB_VENDOR_ID, USB_PRODUCT_ID); handle = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_ID, USB_PRODUCT_ID); if (!handle){ log_error("libusb_open_device_with_vid_pid failed!"); usb_close(handle); return -1; } log_info("libusb open %d, handle %p", r, handle); r = prepare_device(handle); if (r < 0){ usb_close(handle); return -1; } #else // Scan system for an appropriate devices libusb_device **devs; ssize_t cnt; log_info("Scanning for USB Bluetooth device"); cnt = libusb_get_device_list(NULL, &devs); if (cnt < 0) { usb_close(handle); return -1; } int startIndex = 0; dev = NULL; while (1){ int deviceIndex = scan_for_bt_device(devs, startIndex); if (deviceIndex < 0){ break; } startIndex = deviceIndex+1; log_info("USB Bluetooth device found, index %u", deviceIndex); handle = NULL; r = libusb_open(devs[deviceIndex], &handle); if (r < 0) { log_error("libusb_open failed!"); handle = NULL; continue; } log_info("libusb open %d, handle %p", r, handle); // reset device libusb_reset_device(handle); if (r < 0) { log_error("libusb_reset_device failed!"); libusb_close(handle); handle = NULL; continue; } // device found r = prepare_device(handle); if (r < 0){ continue; } libusb_state = LIB_USB_INTERFACE_CLAIMED; break; } libusb_free_device_list(devs, 1); if (handle == 0){ log_error("No USB Bluetooth device found"); return -1; } scan_for_bt_endpoints(); #endif // allocate transfer handlers int c; for (c = 0 ; c < ASYNC_BUFFERS ; c++) { event_in_transfer[c] = libusb_alloc_transfer(0); // 0 isochronous transfers Events acl_in_transfer[c] = libusb_alloc_transfer(0); // 0 isochronous transfers ACL in if ( !event_in_transfer[c] || !acl_in_transfer[c]) { usb_close(handle); return LIBUSB_ERROR_NO_MEM; } } command_out_transfer = libusb_alloc_transfer(0); acl_out_transfer = libusb_alloc_transfer(0); // TODO check for error libusb_state = LIB_USB_TRANSFERS_ALLOCATED; #ifdef HAVE_SCO // incoming for (c = 0 ; c < ASYNC_BUFFERS ; c++) { sco_in_transfer[c] = libusb_alloc_transfer(NUM_ISO_PACKETS); // isochronous transfers SCO in log_info("Alloc iso transfer"); if (!sco_in_transfer[c]) { usb_close(handle); return LIBUSB_ERROR_NO_MEM; } // configure sco_in handlers libusb_fill_iso_transfer(sco_in_transfer[c], handle, sco_in_addr, hci_sco_in_buffer[c], SCO_PACKET_SIZE, NUM_ISO_PACKETS, async_callback, NULL, 0); libusb_set_iso_packet_lengths(sco_in_transfer[c], ISO_PACKET_SIZE); r = libusb_submit_transfer(sco_in_transfer[c]); log_info("Submit iso transfer res = %d", r); if (r) { log_error("Error submitting isochronous in transfer %d", r); usb_close(handle); return r; } } // outgoing for (c=0; c < SCO_RING_BUFFER_COUNT ; c++){ sco_ring_transfers[c] = libusb_alloc_transfer(NUM_ISO_PACKETS); // 1 isochronous transfers SCO out - up to 3 parts } #endif for (c = 0 ; c < ASYNC_BUFFERS ; c++) { // configure event_in handlers libusb_fill_interrupt_transfer(event_in_transfer[c], handle, event_in_addr, hci_event_in_buffer[c], HCI_ACL_BUFFER_SIZE, async_callback, NULL, 0) ; r = libusb_submit_transfer(event_in_transfer[c]); if (r) { log_error("Error submitting interrupt transfer %d", r); usb_close(handle); return r; } // configure acl_in handlers libusb_fill_bulk_transfer(acl_in_transfer[c], handle, acl_in_addr, hci_acl_in_buffer[c] + HCI_INCOMING_PRE_BUFFER_SIZE, HCI_ACL_BUFFER_SIZE, async_callback, NULL, 0) ; r = libusb_submit_transfer(acl_in_transfer[c]); if (r) { log_error("Error submitting bulk in transfer %d", r); usb_close(handle); return r; } } // Check for pollfds functionality doing_pollfds = libusb_pollfds_handle_timeouts(NULL); // NOTE: using pollfds doesn't work on Linux, so it is disable until further investigation here doing_pollfds = 0; if (doing_pollfds) { log_info("Async using pollfds:"); const struct libusb_pollfd ** pollfd = libusb_get_pollfds(NULL); for (num_pollfds = 0 ; pollfd[num_pollfds] ; num_pollfds++); pollfd_data_sources = malloc(sizeof(data_source_t) * num_pollfds); if (!pollfd_data_sources){ log_error("Cannot allocate data sources for pollfds"); usb_close(handle); return 1; } for (r = 0 ; r < num_pollfds ; r++) { data_source_t *ds = &pollfd_data_sources[r]; ds->fd = pollfd[r]->fd; ds->process = usb_process_ds; run_loop_add_data_source(ds); log_info("%u: %p fd: %u, events %x", r, pollfd[r], pollfd[r]->fd, pollfd[r]->events); } free(pollfd); } else { log_info("Async using timers:"); usb_timer.process = usb_process_ts; run_loop_set_timer(&usb_timer, AYSNC_POLLING_INTERVAL_MS); run_loop_add_timer(&usb_timer); usb_timer_active = 1; } return 0; }