example/hfp_ag: use gap inquiry functionality

This commit is contained in:
Matthias Ringwald 2017-05-22 11:04:15 +02:00
parent 0a51f88b22
commit 6e87f20c5b

View File

@ -104,25 +104,7 @@ static hfp_generic_status_indicator_t hf_indicators[] = {
{2, 1},
};
char cmd;
// GAP INQUIRY
#define MAX_DEVICES 10
enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED };
struct device {
bd_addr_t address;
uint16_t clockOffset;
uint32_t classOfDevice;
uint8_t pageScanRepetitionMode;
uint8_t rssi;
enum DEVICE_STATE state;
};
#define INQUIRY_INTERVAL 5
struct device devices[MAX_DEVICES];
int deviceCount = 0;
enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
enum STATE state = INIT;
@ -151,149 +133,7 @@ static void dump_supported_codecs(void){
}
}
static int getDeviceIndexForAddress( bd_addr_t addr){
int j;
for (j=0; j< deviceCount; j++){
if (bd_addr_cmp(addr, devices[j].address) == 0){
return j;
}
}
return -1;
}
#ifdef HAVE_POSIX_STDIN
static void start_scan(void){
printf("Starting inquiry scan..\n");
hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
}
#endif
static int has_more_remote_name_requests(void){
int i;
for (i=0;i<deviceCount;i++) {
if (devices[i].state == REMOTE_NAME_REQUEST) return 1;
}
return 0;
}
static void do_next_remote_name_request(void){
int i;
for (i=0;i<deviceCount;i++) {
// remote name request
if (devices[i].state == REMOTE_NAME_REQUEST){
devices[i].state = REMOTE_NAME_INQUIRED;
printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address));
hci_send_cmd(&hci_remote_name_request, devices[i].address,
devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000);
return;
}
}
}
static void continue_remote_names(void){
// don't get remote names for testing
if (has_more_remote_name_requests()){
do_next_remote_name_request();
return;
}
// try to find PTS
int i;
for (i=0;i<deviceCount;i++){
if (memcmp(devices[i].address, device_addr, 6) == 0){
printf("Inquiry scan over, successfully found PTS at index %u\nReady to connect to it.\n", i);
return;
}
}
printf("Inquiry scan over but PTS not found :(\n");
}
static void inquiry_packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){
UNUSED(size);
bd_addr_t addr;
int i;
int numResponses;
int index;
// printf("packet_handler: pt: 0x%02x, packet[0]: 0x%02x\n", packet_type, packet[0]);
if (packet_type != HCI_EVENT_PACKET) return;
uint8_t event = packet[0];
switch(event){
case HCI_EVENT_INQUIRY_RESULT:
case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
numResponses = hci_event_inquiry_result_get_num_responses(packet);
int offset = 3;
for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){
reverse_bd_addr(&packet[offset], addr);
offset += 6;
index = getDeviceIndexForAddress(addr);
if (index >= 0) continue; // already in our list
memcpy(devices[deviceCount].address, addr, 6);
devices[deviceCount].pageScanRepetitionMode = packet[offset];
offset += 1;
if (event == HCI_EVENT_INQUIRY_RESULT){
offset += 2; // Reserved + Reserved
devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset);
offset += 3;
devices[deviceCount].clockOffset = little_endian_read_16(packet, offset) & 0x7fff;
offset += 2;
devices[deviceCount].rssi = 0;
} else {
offset += 1; // Reserved
devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset);
offset += 3;
devices[deviceCount].clockOffset = little_endian_read_16(packet, offset) & 0x7fff;
offset += 2;
devices[deviceCount].rssi = packet[offset];
offset += 1;
}
devices[deviceCount].state = REMOTE_NAME_REQUEST;
printf("Device #%u found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n",
deviceCount, bd_addr_to_str(addr),
devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode,
devices[deviceCount].clockOffset, devices[deviceCount].rssi);
deviceCount++;
}
break;
}
case HCI_EVENT_INQUIRY_COMPLETE:
for (i=0;i<deviceCount;i++) {
// retry remote name request
if (devices[i].state == REMOTE_NAME_INQUIRED)
devices[i].state = REMOTE_NAME_REQUEST;
}
continue_remote_names();
break;
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
reverse_bd_addr(&packet[3], addr);
index = getDeviceIndexForAddress(addr);
if (index >= 0) {
if (packet[2] == 0) {
printf("Name: '%s'\n", &packet[9]);
devices[index].state = REMOTE_NAME_FETCHED;
} else {
printf("Failed to get name: page timeout\n");
}
}
continue_remote_names();
break;
default:
break;
}
}
// GAP INQUIRY END
#ifdef HAVE_POSIX_STDIN
// prototypes
static void show_usage(void);
// Testig User Interface
static void show_usage(void){
bd_addr_t iut_address;
@ -340,7 +180,7 @@ static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callbac
UNUSED(ds);
UNUSED(callback_type);
cmd = btstack_stdin_read();
char cmd = btstack_stdin_read();
switch (cmd){
case 'a':
log_info("USER:\'%c\'", cmd);
@ -535,7 +375,8 @@ static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callbac
hfp_ag_join_held_call();
break;
case 'v':
start_scan();
printf("Start scanning...\n");
gap_inquiry_start(INQUIRY_INTERVAL);
break;
case 'w':
log_info("USER:\'%c\'", cmd);
@ -561,15 +402,29 @@ static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callbac
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
UNUSED(channel);
bd_addr_t addr;
switch (packet_type){
case HCI_EVENT_PACKET:
switch (event[0]){
case HCI_EVENT_INQUIRY_RESULT:
case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
case HCI_EVENT_INQUIRY_COMPLETE:
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
inquiry_packet_handler(HCI_EVENT_PACKET, event, event_size);
case GAP_EVENT_INQUIRY_RESULT:
gap_event_inquiry_result_get_bd_addr(event, addr);
// print info
printf("Device found: %s ", bd_addr_to_str(addr));
printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
if (gap_event_inquiry_result_get_rssi_availabe(event)){
printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
}
if (gap_event_inquiry_result_get_name_available(event)){
char name_buffer[240];
int name_len = gap_event_inquiry_result_get_name_len(event);
memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
name_buffer[name_len] = 0;
printf(", name '%s'", name_buffer);
}
printf("\n");
break;
case GAP_EVENT_INQUIRY_COMPLETE:
printf("Inquiry scan complete.\n");
break;
case HCI_EVENT_SCO_CAN_SEND_NOW:
sco_demo_send(sco_handle);