/* Compare function for bd address */ static int prh_rfcomm_entity_bdaddr_cmp(void *comp_input, void *comp_with) { t_bdaddr *bd_addr_ptr=(t_bdaddr *)comp_input; struct prh_rfcomm_entity_pending_conns *pe_conn=(struct prh_rfcomm_entity_pending_conns *)comp_with; return BD_ADDR_CMP(pe_conn->bd_addr, (*bd_addr_ptr)); }
static int prh_tcs_wug_keydb_bdaddr_cmp(void *cmpin, void *compwith) { struct prh_st_tcs_wug_member_link_key *instance=(struct prh_st_tcs_wug_member_link_key *)compwith; t_bdaddr bd_addr=*((t_bdaddr *)cmpin); return (BD_ADDR_CMP(bd_addr, instance->peer)); }
/** * get connection for given address * * @return connection OR NULL, if not found */ static hci_connection_t * connection_for_address(bd_addr_t address){ linked_item_t *it; for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ return (hci_connection_t *) it; } } return NULL; }
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; }
static db_mem_device_t * get_item(linked_list_t list, bd_addr_t bd_addr) { linked_item_t *it; for (it = (linked_item_t *) list; it ; it = it->next){ db_mem_device_t * item = (db_mem_device_t *) it; if (BD_ADDR_CMP(item->bd_addr, bd_addr) == 0) { return item; } } return NULL; }
struct st_t_TCS_TLPeer *prh_tcs_cc_core_getTLPeerByBdAddress(t_bdaddr address) { int i; for(i=0; i<PRH_TCS_MAXTERMINALSPERGATEWAY; i++) { if(TCSInstance.TLPeer[i]) { if(BD_ADDR_CMP(TCSInstance.TLPeer[i]->bdAddress, address)) return TCSInstance.TLPeer[i]; } } return NULL; }
static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ linked_item_t *it; for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ l2cap_channel_t * channel = (l2cap_channel_t *) it; if ( ! BD_ADDR_CMP( channel->address, address) ){ if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { // success, start l2cap handshake channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; channel->handle = handle; channel->local_cid = l2cap_next_local_cid(); } } } // process l2cap_run(); }
static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ linked_item_t *it = (linked_item_t *) &l2cap_channels; while (it->next){ l2cap_channel_t * channel = (l2cap_channel_t *) it->next; if ( ! BD_ADDR_CMP( channel->address, address) ){ if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { // failure, forward error code l2cap_emit_channel_opened(channel, status); // discard channel it->next = it->next->next; btstack_memory_l2cap_channel_free(channel); } } else { it = it->next; } } }
u_int8 prh_tcs_cc_core_freeTLPeerByBdAddress(t_bdaddr address) { int i; for(i=0; i<PRH_TCS_MAXTERMINALSPERGATEWAY; i++) { if(TCSInstance.TLPeer[i]) { if(BD_ADDR_CMP(TCSInstance.TLPeer[i]->bdAddress, address)) { pFree(TCSInstance.TLPeer[i]); TCSInstance.TLPeer[i] = NULL; return 0; } } } return 1; }
static struct apple_pad_connection* btpad_find_connection_for(uint16_t handle, bd_addr_t address) { for (int i = 0; i < MAX_PLAYERS; i ++) { if (!g_connections[i].handle && !g_connections[i].has_address) continue; if (handle && g_connections[i].handle && handle != g_connections[i].handle) continue; if (address && g_connections[i].has_address && (BD_ADDR_CMP(address, g_connections[i].address))) continue; return &g_connections[i]; } return 0; }
static struct btstack_hid_adapter *btpad_find_connection_for( uint16_t handle, bd_addr_t address) { unsigned i; for (i = 0; i < MAX_USERS; i++) { if (!g_connections[i].handle && !g_connections[i].has_address) continue; if (handle && g_connections[i].handle && handle != g_connections[i].handle) continue; if (address && g_connections[i].has_address && (BD_ADDR_CMP(address, g_connections[i].address))) continue; return &g_connections[i]; } return 0; }
/************************************************************************************************* * FUNCTION :- prh_mgr_retrieve_inquiry_entry * * DESCRIPTION :- This function searches the inq db for an entry by the BDAddress. If a device * is found with a matching BDAddress, it is returned, otherwise NULL is returned. * ************************************************************************************************/ struct st_t_BT_InquiryInfo *prh_mgr_retrieve_inquiry_entry(t_bdaddr address) { struct st_t_BT_InquiryInfo *current; current=prh_mgr_inqdb_head; while (current) { if (BD_ADDR_CMP(address,current->bdAddress)) break; current=current->next; } if (current) { pMutexLock(prh_mgr_inqdb_list_lock); current->entryUsage++; pMutexUnlock(prh_mgr_inqdb_list_lock); } #if pDEBUG else { pDebugPrintfEX((pLOGDEBUG,pLOGMANAGER,"prh_mgr_retrieve_inquiry_entry: cannot find entry for device %s\n",prh_mgr_printBDAddress(address))); } #endif return current; }
//03-28-2009, add by tan void rdabt_being_bonding_cb(t_bdaddr address, int status) { bth_dev_struct dev_Info; struct st_t_BT_ConnectionEntry * cur=NULL, *next=NULL; bt_bm_bonding_result_ind_struct *param_ptr=NULL; bt_bm_bonding_cnf_struct * bonding_cnf=NULL; u_int8 flag_present=0; kal_prompt_trace(1,"rdabt_being_bonding_cb status = %d\n",status); //status = 1; if( g_PairingFinished == 0) g_PairingFinished = 1; //7-12-2009, add by tan else return; memset(&dev_Info, 0, sizeof(bth_dev_struct)); param_ptr = (bt_bm_bonding_result_ind_struct*)construct_local_para((kal_uint16)sizeof(bt_bm_bonding_result_ind_struct), TD_CTRL); param_ptr->bd_addr.lap= BDADDR_Get_LAP(&address); param_ptr->bd_addr.uap = BDADDR_Get_UAP(&address); param_ptr->bd_addr.nap = BDADDR_Get_NAP(&address); kal_prompt_trace(1,"rdabt_being_bonding_cb\n"); if (wait_bonding_timer) { kal_prompt_trace(1,"wait_bonding_timer be canceled wait_bonding_timer=%d\n",wait_bonding_timer); pTimerCancel(wait_bonding_timer); wait_bonding_timer=0; } MGR_GetNextConnectionRecord(NULL, &next); while(next) { cur = next; if( BD_ADDR_CMP(next->bdAddress, address)) { if( next->linkKey) { kal_prompt_trace(1,"get the device' s info, link_key:%s and cod:%d\n", print_link_key(next->linkKey),next->cod); dev_Info.bd_addr = param_ptr->bd_addr; dev_Info.KeyType = next->linkKeyType; memcpy( dev_Info.linkKey, next->linkKey, 0x10); dev_Info.cod =param_ptr->cod = next->cod; flag_present=1;//zhou siyou add 20070725 } //break; } //else { MGR_GetNextConnectionRecord(cur, &next); } } //if(next && next->linkKey) if(flag_present) { bth_write_paired_device_info(&dev_Info); status = 0; // kal_prompt_trace(1,"write the device info into a file, status:%d (0 indicates succeed)\n", status_tem); } param_ptr->result = status; pMemcpy(param_ptr->linkKey, dev_Info.linkKey, 16); param_ptr->KeyType = dev_Info.KeyType; bonding_cnf = (bt_bm_bonding_cnf_struct *)construct_local_para((kal_uint16)sizeof(bt_bm_bonding_cnf_struct), TD_CTRL); bonding_cnf->result = status; bonding_cnf->bd_addr = param_ptr->bd_addr; rdabt_send_msg_up(MOD_MMI, MSG_ID_BT_BM_BONDING_RESULT_IND, (void *)param_ptr, NULL); rdabt_send_msg_up(MOD_MMI, MSG_ID_BT_BM_BE_BONDING_CNF, (void*)bonding_cnf, NULL);//zhou siyou add 200728 if(deviceName != NULL) { pFree(deviceName); deviceName = NULL; nameLen = 0; } return ; }
void rdabt_reply_rsp_cb(t_bdaddr address, int status) { LINK_PIN_CODE_RESULT_IND_ST mv1; LINK_PIN_CODE_CNF_ST mv2; bt_bm_service_search_req_struct service_msg; bt_bm_service_search_req_struct *service_msgp=&service_msg; kal_uint8 i; u_int8 flag_preset=0; struct st_t_BT_ConnectionEntry * cur=NULL, *next=NULL; bth_dev_struct dev_Info; bonding_role = 0; pincode_sent=1; if (pin_code_ind_timer)//zhou siyou add 20070725 { pTimerCancel(pin_code_ind_timer); pin_code_ind_timer=0; } memset(&dev_Info, 0, sizeof(bth_dev_struct)); kal_prompt_trace(1,"rdabt_reply_rsp_cb, found linkkey, status:%d\n", status); if( status == 0) { MGR_GetNextConnectionRecord(NULL, &next); while(next) { cur = next; if(BD_ADDR_CMP(next->bdAddress, address)) { if( next->linkKey) { dev_Info.bd_addr.lap= BDADDR_Get_LAP(&address); dev_Info.bd_addr.uap = BDADDR_Get_UAP(&address); dev_Info.bd_addr.nap = BDADDR_Get_NAP(&address); dev_Info.KeyType = next->linkKeyType; dev_Info.cod = next->cod; memcpy( dev_Info.linkKey, next->linkKey, 0x10); kal_prompt_trace(1,"rdabt_reply_rsp_cb, found linkkey, cod:%d\n", next->cod); flag_preset=1; } // break; } //else { MGR_GetNextConnectionRecord(cur, &next); } } //if(next && next->linkKey) if(flag_preset) { mv2.result = bth_write_paired_device_info(&dev_Info); } } mv1.type = LINK_BONDING_RESULT_IND; mv1.result = status; mv1.bd_addr.lap= BDADDR_Get_LAP(&address); mv1.bd_addr.uap = BDADDR_Get_UAP(&address); mv1.bd_addr.nap = BDADDR_Get_NAP(&address); mv1.cod = next?next->cod:0; pMemcpy(mv1.linkKey, dev_Info.linkKey, 16); mv1.KeyType = dev_Info.KeyType; // mmi_trace(g_sw_BT,"rdabt_reply_rsp_cb::status=%d", status); // mmi_trace_hex(g_sw_BT,16, dev_Info.linkKey); rdabt_put_message(EXTERNAL_TASK_MASK,LINK_Q,&mv1); mv2.type = LINK_BONDING_CNF; mv2.result = status; mv2.bd_addr = mv1.bd_addr; rdabt_put_message(EXTERNAL_TASK_MASK,LINK_Q,&mv2); if( mv2.result == 0) //modified by tan { service_msgp->bd_addr=mv1.bd_addr; service_msgp->sdap_len = pin_code_sdap.sdap_len; for (i = 0; i < BTBM_ADP_MAX_SDAP_UUID_NO; i++) { service_msgp->sdap_uuid[i] = pin_code_sdap.sdap_uuid[i]; } rdabt_SDP_serviceSearch(service_msgp); } }
/************************************************************************************************* * FUNCTION :- prh_mgr_store_inquiry_entry * * DESCRIPTION :- This function stores an inquiry result in the inq db. If the device is already * in the database the parameters will be updates otherwise a new entry will be * added to the inq db. * NOTE:inquiryresult passed in is one set of page parameters, there may be many * in a single hci event * ************************************************************************************************/ struct st_t_BT_InquiryInfo *prh_mgr_store_inquiry_entry(u_int8 *inquiryResult) { struct st_t_BT_InquiryInfo *current; extern u_int32 prh_mgr_inqdb_num_entries; // char err_str[40]={0}; if(inquiryResult == NULL) return NULL; pMutexLock(prh_mgr_inqdb_list_lock); current=prh_mgr_inqdb_head; pDebugPrintfEX((pLOGDEBUG,pLOGMANAGER,"prh_mgr_store_inquiry_entry: linklist is NULL? %d\n", prh_mgr_inqdb_head?1:0)); while (current) { if (BD_ADDR_CMP(current->bdAddress,*(t_bdaddr*)(inquiryResult+0))) /* check alignment correctness here */ break; current=current->next; } #if pDEBUG if (!current) { pDebugPrintfEX((pLOGDEBUG,pLOGMANAGER,"prh_mgr_store_inquiry_entry: cannot find existing entry for device %s\n",prh_mgr_printBDAddress(*(t_bdaddr*)(inquiryResult+0)))); } #endif if (current) { if(current->entryUsage) { /* entry already exists, but is in use, so cannot be updated */ pDebugPrintfEX((pLOGDEBUG,pLOGMANAGER,"prh_mgr_store_inquiry_entry: cannot find existing entry for device %s\n",prh_mgr_printBDAddress(*(t_bdaddr*)(inquiryResult+0)))); pMutexUnlock(prh_mgr_inqdb_list_lock); return current; } else { prh_mgr_unlink_inquiry_entry(current); } } else { pDebugPrintfEX((pLOGDEBUG,pLOGMANAGER,"prh_mgr_store_inquiry_entry: prh_mgr_inqdb_num_entries=%d\n", prh_mgr_inqdb_num_entries)); if (prh_mgr_inqdb_num_entries<(u_int32)HOST_INQDB_MAX_DEVICE_ENTRIES) { /* alloc a new record */ current=pMalloc(sizeof(struct st_t_BT_InquiryInfo)); if (!current) { pMutexUnlock(prh_mgr_inqdb_list_lock); return NULL; } current->deviceName = NULL; #if HOST_INQDB_CULL_THRESHOLD!=0 if (!prh_mgr_inqdb_num_entries) /* we are adding to an empty database */ prh_mgr_inqdb_timer_handle=pTimerCreate(HOST_INQDB_CULL_GRANULARITY,prh_mgr_cull_inquiry_db,NULL,pTIMER_ONESHOT); #endif prh_mgr_inqdb_num_entries++; } else { /* find the oldest non-inuse record */ current=prh_mgr_inqdb_tail; while (current) { if (current->entryUsage) current=current->prev; else break; } if (!current) { /* no available entry */ pMutexUnlock(prh_mgr_inqdb_list_lock); pDebugPrintfEX((pLOGNOTICE,pLOGMANAGER,"prh_mgr_store_inquiry_entry: prh_mgr_inqdb_num_entries==HOST_INQDB_MAX_DEVICE_ENTRIES (%d) and all entries in use\n",HOST_INQDB_MAX_DEVICE_ENTRIES)); return NULL; } prh_mgr_unlink_inquiry_entry(current); } current->entryAge=1; /* only modified when an entry did not already exist for this adress */ current->nameRetrieved = 0; } //Lu added /* fill out the parameters */ pMemcpy(¤t->bdAddress,inquiryResult+0,6); current->pageScanRepetitionMode=inquiryResult[6]; current->pageScanPeriodMode=inquiryResult[7]; current->pageScanMode=inquiryResult[8]; current->classOfDevice=inquiryResult[9]|(inquiryResult[10]<<8)|(inquiryResult[11]<<16); current->clockOffset=inquiryResult[12]|(inquiryResult[13]<<8); /* Do not fill in current->deviceName until RemoteNameRequest is complete */ current->entryUsage=1; /* entry starts marked as 'in-use' */ /* add this new record (to the head of the list) */ current->next=prh_mgr_inqdb_head; current->prev=NULL; if (current->next) current->next->prev=current; prh_mgr_inqdb_head=current; if (!prh_mgr_inqdb_tail) prh_mgr_inqdb_tail=current; pDebugPrintfEX((pLOGDEBUG,pLOGMANAGER,"prh_mgr_store_inquiry_entry: current->entryAge=%d, current->classOfDevice=0x%x\n", current->entryAge,current->classOfDevice)); pMutexUnlock(prh_mgr_inqdb_list_lock); return current; }
void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ hci_con_handle_t acl_in; hci_con_handle_t acl_out; switch (packet_type){ case HCI_ACL_DATA_PACKET: acl_in = READ_ACL_CONNECTION_HANDLE(packet); acl_out = 0; if (acl_in == alice_handle) { printf("Alice: "); hexdump( packet, size ); printf("\n\n"); acl_out = bob_handle; } if (acl_in == bob_handle) { printf("Bob: "); hexdump( packet, size ); printf("\n\n"); acl_out = alice_handle; } if (acl_out){ bt_store_16( packet, 0, (READ_BT_16(packet, 0) & 0xf000) | acl_out); bt_send_acl(packet, size); } break; case HCI_EVENT_PACKET: switch(packet[0]){ case BTSTACK_EVENT_STATE: // bt stack activated, get started - set COD if (packet[2] == HCI_STATE_WORKING) { bt_send_cmd(&hci_write_class_of_device, 0x7A020C); // used on iPhone } break; case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: // process EIR responses if (packet[17]) { bt_flip_addr(temp_addr, &packet[3]); if (BD_ADDR_CMP(temp_addr, bob_addr)) { printf("2. Got BOB's EIR. "); int i, k; bzero(bob_EIR, EIR_LEN); for (i=17, k=0;i<EIR_LEN && packet[i]; i += packet[i] + 1, k += bob_EIR[k] + 1){ if (packet[i+1] == 0x09) { // complete name id -- use own bob_EIR[k+0] = 1 + strlen(NAME); bob_EIR[k+1] = 0x09; memcpy(&bob_EIR[k+2], NAME, strlen(NAME)); } else { // vendor specific if (packet[i+1] == 0x0ff ) { bob_got_EIR = 1; } memcpy(&bob_EIR[k], &packet[i], packet[i]+1); } } hexdump(&bob_EIR, k); printf("\n\n"); bob_clock_offset = READ_BT_16(packet, 14); bob_page_scan_repetition_mode = packet[9]; } // stop inquiry // bt_send_cmd(&hci_inquiry_cancel); } break; case HCI_EVENT_CONNECTION_REQUEST: // accept incoming connections bt_flip_addr(temp_addr, &packet[2]); if (BD_ADDR_CMP(temp_addr, bob_addr) ){ printf("-> Connection request from BOB. Denying\n"); // bt_send_cmd(&hci_accept_connection_request, &temp_addr, 1); } else { printf("-> Connection request from Alice. Sending Accept\n"); bt_send_cmd(&hci_accept_connection_request, &temp_addr, 1); } break; case HCI_EVENT_CONNECTION_COMPLETE: // handle connections bt_flip_addr(temp_addr, &packet[5]); if (packet[2] == 0){ hci_con_handle_t incoming_handle = READ_BT_16(packet, 3); if (BD_ADDR_CMP(temp_addr, bob_addr)){ bob_handle = incoming_handle; printf("7. Connected to BOB (handle %u). Relaying data!\n", bob_handle); } else { alice_handle = incoming_handle; printf("6. Alice connected (handle %u). Connecting to BOB.\n", alice_handle); bt_send_cmd(&hci_create_connection, &bob_addr, 0x18, bob_page_scan_repetition_mode, 0, 0x8000 || bob_clock_offset, 0); } } else { printf("Connection complete status %u for connection", packet[2]); print_bd_addr(temp_addr); printf("\n"); } break; case HCI_EVENT_PIN_CODE_REQUEST: // inform about pin code request printf("Please enter PIN 1234 on remote device\n"); break; case HCI_EVENT_DISCONNECTION_COMPLETE: // connection closed -> quit test app printf("Basebank connection closed, exit.\n"); exit(0); break; case HCI_EVENT_COMMAND_COMPLETE: // use pairing yes/no if ( COMMAND_COMPLETE_EVENT(packet, hci_write_class_of_device) ) { bt_send_cmd(&hci_write_authentication_enable, 0); } // allow Extended Inquiry responses if ( COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable) ) { bt_send_cmd(&hci_write_inquiry_mode, 2); } // get all events, including EIRs if ( COMMAND_COMPLETE_EVENT(packet, hci_write_inquiry_mode) ) { bt_send_cmd(&hci_set_event_mask, 0xffffffff, 0x1fffffff); } // fine with us, too if ( COMMAND_COMPLETE_EVENT(packet, hci_set_event_mask) ) { bt_send_cmd(&hci_write_simple_pairing_mode, 1); } // start inquiry if ( COMMAND_COMPLETE_EVENT(packet, hci_write_simple_pairing_mode) ) { // enable capure bt_send_cmd(&btstack_set_acl_capture_mode, 1); printf("1. Started inquiry.\n"); bt_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, 15, 0); } // Connect to BOB if ( COMMAND_COMPLETE_EVENT(packet, hci_write_extended_inquiry_response) ) { printf("5. Waiting for Alice!...\n"); // bt_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan // bt_send_cmd(&hci_create_connection, &addr, 0x18, page_scan_repetition_mode, 0, 0x8000 || clock_offset, 0); } break; default: // Inquiry done, set EIR if (packet[0] == HCI_EVENT_INQUIRY_COMPLETE || COMMAND_COMPLETE_EVENT(packet, hci_inquiry_cancel)){ if (!inquiry_done){ inquiry_done = 1; printf("3. Inquiry Complete\n"); if (bob_got_EIR){ printf("4. Set EIR to Bob's.\n"); bt_send_cmd(&hci_write_extended_inquiry_response, 0, bob_EIR); } else { // failed to get BOB's EIR } } } break; } default: break; } }