Exemplo n.º 1
0
static void handle_hci_event(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
    if (packet_type != HCI_EVENT_PACKET) return;
    
    bd_addr_t address;
    uint8_t event = packet[0];
    switch (event) {
        case BTSTACK_EVENT_STATE:
            if (packet[2] != HCI_STATE_WORKING) break;
            le_central_set_scan_parameters(0,0x0030, 0x0030);
            le_central_start_scan();
            break;
            
        case GAP_LE_ADVERTISING_REPORT:{
            advertisement_received = 1;
            memcpy(advertisement_packet, packet, size);
            
            bt_flip_addr(address, &packet[4]);
            le_central_connect(address, (bd_addr_type_t)packet[3]);
            break;
        }
        case HCI_EVENT_LE_META:
            // wait for connection complete
            if (packet[2] !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
            connected = 1;
            break;
        case HCI_EVENT_DISCONNECTION_COMPLETE:
            exit(0);
            break;
        default:
            break;
    }
}
Exemplo n.º 2
0
static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
    if (packet_type != HCI_EVENT_PACKET) return;
    
    switch (packet[0]) {
        case BTSTACK_EVENT_STATE:
            // BTstack activated, get started
            if (packet[2] == HCI_STATE_WORKING) {
                printf("BTstack activated, start scaning!\n");
                le_central_set_scan_parameters(0,0x0030, 0x0030);
                le_central_start_scan(); 
            }
            break;
        case GAP_LE_ADVERTISING_REPORT:{
            int pos = 2;
            uint8_t event_type = packet[pos++];
            uint8_t address_type = packet[pos++];
            bd_addr_t address;
            bt_flip_addr(address, &packet[pos]);
            pos += 6;
            uint8_t rssi = packet[pos++];
            uint8_t length = packet[pos++];
            uint8_t * data = &packet[pos];
            
            printf("Advertisement event: evt-type %u, addr-type %u, addr %s, rssi %u, data[%u] ", event_type,
               address_type, bd_addr_to_str(address), rssi, length);
            printf_hexdump(data, length);
            dump_advertisement_data(data, length);
            break;
        }
        default:
            break;
    }
}
Exemplo n.º 3
0
static void handle_hci_event(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
    if (packet_type != HCI_EVENT_PACKET) return;
    advertising_report_t report;
    
    uint8_t event = packet[0];
    switch (event) {
        case BTSTACK_EVENT_STATE:
            // BTstack activated, get started
            if (packet[2] != HCI_STATE_WORKING) break;
            if (cmdline_addr_found){
                printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr));
                state = TC_W4_CONNECT;
                le_central_connect(&cmdline_addr, 0);
                break;
            }
            printf("BTstack activated, start scanning!\n");
            state = TC_W4_SCAN_RESULT;
            le_central_set_scan_parameters(0,0x0030, 0x0030);
            le_central_start_scan();
            break;
        case GAP_LE_ADVERTISING_REPORT:
            if (state != TC_W4_SCAN_RESULT) return;
            fill_advertising_report_from_packet(&report, packet);
            // stop scanning, and connect to the device
            // state = TC_W4_CONNECT;
            //le_central_stop_scan();
            //le_central_connect(&report.address,report.address_type);
            break;
        case HCI_EVENT_LE_META:
            // wait for connection complete
            if (packet[2] !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
            if (state != TC_W4_CONNECT) return;
            gc_handle = READ_BT_16(packet, 4);
            // query primary services
            state = TC_W4_SERVICE_RESULT;
            gatt_client_discover_primary_services(gc_handle);
            break;
        case HCI_EVENT_DISCONNECTION_COMPLETE:
            printf("\ntest client - DISCONNECTED\n");
            exit(0);
            break;
        default:
            break;
    }
}