/******************************************************************************* ** ** Function GAP_BleAttrDBUpdate ** ** Description GAP ATT database update. ** ** Returns void. ** *******************************************************************************/ void GAP_BleAttrDBUpdate(UINT16 attr_uuid, tGAP_BLE_ATTR_VALUE *p_value) { tGAP_ATTR *p_db_attr = gap_cb.gatt_attr; UINT8 i = 0; GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x", attr_uuid); for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++) { if (p_db_attr->uuid == attr_uuid) { GAP_TRACE_EVENT("Found attr_uuid=0x%04x", attr_uuid); switch (attr_uuid) { case GATT_UUID_GAP_ICON: p_db_attr->attr_value.icon = p_value->icon; break; case GATT_UUID_GAP_PREF_CONN_PARAM: memcpy((void *)&p_db_attr->attr_value.conn_param, (const void *)&p_value->conn_param, sizeof(tGAP_BLE_PREF_PARAM)); break; case GATT_UUID_GAP_DEVICE_NAME: BTM_SetLocalDeviceName((char *)p_value->p_dev_name); break; } break; } } return; }
/******************************************************************************* ** ** Function gap_ble_c_cmpl_cback ** ** Description Client operation complete callback. ** ** Returns void ** *******************************************************************************/ static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data) { tGAP_CLCB *p_clcb = gap_ble_find_clcb_by_conn_id(conn_id); UINT16 op_type; UINT16 min, max, latency, tout; UINT16 len; UINT8 *pp; if (p_clcb == NULL) return; op_type = p_clcb->cl_op_uuid; GAP_TRACE_EVENT ("gap_ble_c_cmpl_cback() - op_code: 0x%02x status: 0x%02x read_type: 0x%04x", op, status, op_type); /* Currently we only issue read commands */ if (op != GATTC_OPTYPE_READ) return; if (status != GATT_SUCCESS) { gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL); return; } pp = p_data->att_value.value; switch (op_type) { case GATT_UUID_GAP_PREF_CONN_PARAM: GAP_TRACE_EVENT ("GATT_UUID_GAP_PREF_CONN_PARAM"); /* Extract the peripheral preferred connection parameters and save them */ STREAM_TO_UINT16 (min, pp); STREAM_TO_UINT16 (max, pp); STREAM_TO_UINT16 (latency, pp); STREAM_TO_UINT16 (tout, pp); BTM_BleSetPrefConnParams (p_clcb->bda, min, max, latency, tout); /* release the connection here */ gap_ble_cl_op_cmpl(p_clcb, TRUE, 0, NULL); break; case GATT_UUID_GAP_DEVICE_NAME: GAP_TRACE_EVENT ("GATT_UUID_GAP_DEVICE_NAME"); len = (UINT16)strlen((char *)pp); if (len > GAP_CHAR_DEV_NAME_SIZE) len = GAP_CHAR_DEV_NAME_SIZE; gap_ble_cl_op_cmpl(p_clcb, TRUE, len, pp); break; case GATT_UUID_GAP_ICON: break; } }
/******************************************************************************* ** ** Function GAP_BleCancelReadPeerDevName ** ** Description Cancel reading a peripheral's device name. ** ** Returns TRUE if request accepted ** *******************************************************************************/ BOOLEAN GAP_BleCancelReadPeerDevName (BD_ADDR peer_bda) { tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (peer_bda); GAP_TRACE_EVENT ("GAP_BleCancelReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x", (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3], (peer_bda[4]<<8)+peer_bda[5], (p_clcb == NULL)? 0 : p_clcb->cl_op_uuid); if (p_clcb == NULL || p_clcb->cl_op_uuid != GATT_UUID_GAP_DEVICE_NAME) { GAP_TRACE_ERROR ("Cannot cancel current op is not get dev name"); return FALSE; } if (!p_clcb->connected) { if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, TRUE)) { GAP_TRACE_ERROR ("Cannot cancel where No connection id"); return FALSE; } } gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL); return(TRUE); }
/******************************************************************************* ** ** Function GAP_GetRxQueueCnt ** ** Description This function return number of bytes on the rx queue. ** ** Parameters: handle - Handle returned in the GAP_ConnOpen ** p_rx_queue_count - Pointer to return queue count in. ** ** *******************************************************************************/ int GAP_GetRxQueueCnt (UINT16 handle, UINT32 *p_rx_queue_count) { tGAP_CCB *p_ccb; int rc = BT_PASS; /* Check that handle is valid */ if (handle < GAP_MAX_CONNECTIONS) { p_ccb = &gap_cb.conn.ccb_pool[handle]; if (p_ccb->con_state == GAP_CCB_STATE_CONNECTED) { *p_rx_queue_count = p_ccb->rx_queue_size; } else rc = GAP_INVALID_HANDLE; } else rc = GAP_INVALID_HANDLE; GAP_TRACE_EVENT ("GAP_GetRxQueueCnt - rc = 0x%04x, rx_queue_count=%d", rc , *p_rx_queue_count); return (rc); }
/******************************************************************************* ** ** Function gap_congestion_ind ** ** Description This is a callback function called by L2CAP when ** data L2CAP congestion status changes ** *******************************************************************************/ static void gap_congestion_ind (UINT16 lcid, BOOLEAN is_congested) { tGAP_CCB *p_ccb; UINT16 event; BT_HDR *p_buf; UINT8 status; GAP_TRACE_EVENT ("GAP_CONN - Rcvd L2CAP Is Congested (%d), CID: 0x%x", is_congested, lcid); /* Find CCB based on CID */ if ((p_ccb = gap_find_ccb_by_cid (lcid)) == NULL) return; p_ccb->is_congested = is_congested; event = (is_congested) ? GAP_EVT_CONN_CONGESTED : GAP_EVT_CONN_UNCONGESTED; p_ccb->p_callback (p_ccb->gap_handle, event); if (!is_congested) { while ((p_buf = (BT_HDR *)GKI_dequeue (&p_ccb->tx_queue)) != NULL) { status = L2CA_DATA_WRITE (p_ccb->connection_id, p_buf); if (status == L2CAP_DW_CONGESTED) { p_ccb->is_congested = TRUE; break; } else if (status != L2CAP_DW_SUCCESS) break; } } }
/******************************************************************************* ** ** Function GAP_ConnGetRemoteAddr ** ** Description This function is called to get the remote BD address ** of a connection. ** ** Parameters: handle - Handle of the connection returned by GAP_ConnOpen ** ** Returns BT_PASS - closed OK ** GAP_ERR_BAD_HANDLE - invalid handle ** *******************************************************************************/ UINT8 *GAP_ConnGetRemoteAddr (UINT16 gap_handle) { tGAP_CCB *p_ccb = gap_find_ccb_by_handle (gap_handle); GAP_TRACE_EVENT ("GAP_ConnGetRemoteAddr gap_handle = %d", gap_handle); if ((p_ccb) && (p_ccb->con_state > GAP_CCB_STATE_LISTENING)) { GAP_TRACE_EVENT("GAP_ConnGetRemoteAddr bda :0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n", \ p_ccb->rem_dev_address[0],p_ccb->rem_dev_address[1],p_ccb->rem_dev_address[2], p_ccb->rem_dev_address[3],p_ccb->rem_dev_address[4],p_ccb->rem_dev_address[5]); return (p_ccb->rem_dev_address); } else { GAP_TRACE_EVENT ("GAP_ConnGetRemoteAddr return Error "); return (NULL); } }
/****************************************************************************** ** ** Function gap_ble_s_attr_request_cback ** ** Description GAP ATT server attribute access request callback. ** ** Returns void. ** *******************************************************************************/ void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type, tGATTS_DATA *p_data) { UINT8 status = GATT_INVALID_PDU; tGATTS_RSP rsp_msg; BOOLEAN ignore = FALSE; GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type); memset(&rsp_msg, 0, sizeof(tGATTS_RSP)); switch (type) { case GATTS_REQ_TYPE_READ: status = gap_proc_read(type, &p_data->read_req, &rsp_msg); break; case GATTS_REQ_TYPE_WRITE: if (!p_data->write_req.need_rsp) ignore = TRUE; status = gap_proc_write_req(type, &p_data->write_req); break; case GATTS_REQ_TYPE_WRITE_EXEC: ignore = TRUE; GAP_TRACE_EVENT("Ignore GATTS_REQ_TYPE_WRITE_EXEC" ); break; case GATTS_REQ_TYPE_MTU: GAP_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu); ignore = TRUE; break; default: GAP_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type); break; } if (!ignore) GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg); }
/******************************************************************************* ** ** Function gap_ble_cl_op_cmpl ** ** Description GAP client operation complete callback ** ** Returns void ** *******************************************************************************/ void gap_ble_cl_op_cmpl(tGAP_CLCB *p_clcb, BOOLEAN status, UINT16 len, UINT8 *p_name) { tGAP_BLE_DEV_NAME_CBACK *p_dev_name_cback = (tGAP_BLE_DEV_NAME_CBACK *)(p_clcb->p_cback); UINT16 op = p_clcb->cl_op_uuid; GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status); p_clcb->cl_op_uuid = 0; p_clcb->p_cback=NULL; if (p_dev_name_cback) { GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl"); if (op == GATT_UUID_GAP_DEVICE_NAME) (* p_dev_name_cback)(status, p_clcb->bda, len, (char *)p_name); } }
/******************************************************************************* ** ** Function gap_checks_con_flags ** ** Description This function processes the L2CAP configuration indication ** event. ** ** Returns void ** *******************************************************************************/ static void gap_checks_con_flags (tGAP_CCB *p_ccb) { GAP_TRACE_EVENT ("gap_checks_con_flags conn_flags:0x%x, ", p_ccb->con_flags); /* if all the required con_flags are set, report the OPEN event now */ if ((p_ccb->con_flags & GAP_CCB_FLAGS_CONN_DONE) == GAP_CCB_FLAGS_CONN_DONE) { p_ccb->con_state = GAP_CCB_STATE_CONNECTED; p_ccb->p_callback (p_ccb->gap_handle, GAP_EVT_CONN_OPENED); } }
/******************************************************************************* ** GAP Attributes Database Request callback *******************************************************************************/ tGATT_STATUS gap_read_attr_value (UINT16 handle, tGATT_VALUE *p_value, BOOLEAN is_long) { tGAP_ATTR *p_db_attr = gap_cb.gatt_attr; UINT8 *p = p_value->value, i; UINT16 offset = p_value->offset; UINT8 *p_dev_name = NULL; for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++) { if (handle == p_db_attr->handle) { if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME && is_long == TRUE) return GATT_NOT_LONG; switch (p_db_attr->uuid) { case GATT_UUID_GAP_DEVICE_NAME: BTM_ReadLocalDeviceName((char **)&p_dev_name); if (strlen ((char *)p_dev_name) > GATT_MAX_ATTR_LEN) p_value->len = GATT_MAX_ATTR_LEN; else p_value->len = (UINT16)strlen ((char *)p_dev_name); if (offset > p_value->len) return GATT_INVALID_OFFSET; else { p_value->len -= offset; p_dev_name += offset; ARRAY_TO_STREAM(p, p_dev_name, p_value->len); GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME len=0x%04x", p_value->len); } break; case GATT_UUID_GAP_ICON: UINT16_TO_STREAM(p, p_db_attr->attr_value.icon); p_value->len = 2; break; case GATT_UUID_GAP_PREF_CONN_PARAM: UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_min); /* int_min */ UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_max); /* int_max */ UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.latency); /* latency */ UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.sp_tout); /* sp_tout */ p_value->len =8; break; } return GATT_SUCCESS; } } return GATT_NOT_FOUND; }
/******************************************************************************* ** ** Function GAP_ConnReadData ** ** Description Normally not GKI aware application will call this function ** after receiving GAP_EVT_RXDATA event. ** ** Parameters: handle - Handle of the connection returned in the Open ** p_data - Data area ** max_len - Byte count requested ** p_len - Byte count received ** ** Returns BT_PASS - data read ** GAP_ERR_BAD_HANDLE - invalid handle ** GAP_NO_DATA_AVAIL - no data available ** *******************************************************************************/ UINT16 GAP_ConnReadData (UINT16 gap_handle, UINT8 *p_data, UINT16 max_len, UINT16 *p_len) { tGAP_CCB *p_ccb = gap_find_ccb_by_handle (gap_handle); BT_HDR *p_buf; UINT16 copy_len; if (!p_ccb) return (GAP_ERR_BAD_HANDLE); *p_len = 0; p_buf = (BT_HDR *)GKI_getfirst (&p_ccb->rx_queue); if (!p_buf) return (GAP_NO_DATA_AVAIL); GKI_disable(); while (max_len && p_buf) { copy_len = (p_buf->len > max_len)?max_len:p_buf->len; max_len -= copy_len; *p_len += copy_len; if (p_data) { memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, copy_len); p_data += copy_len; } if (p_buf->len > copy_len) { p_buf->offset += copy_len; p_buf->len -= copy_len; break; } else { if (max_len) { p_buf = (BT_HDR *)GKI_getnext (p_buf); } GKI_freebuf (GKI_dequeue (&p_ccb->rx_queue)); } } p_ccb->rx_queue_size -= *p_len; GKI_enable(); GAP_TRACE_EVENT ("GAP_ConnReadData - rx_queue_size left=%d, *p_len=%d", p_ccb->rx_queue_size, *p_len); return (BT_PASS); }
/******************************************************************************* ** ** Function gap_disconnect_ind ** ** Description This function handles a disconnect event from L2CAP. If ** requested to, we ack the disconnect before dropping the CCB ** ** Returns void ** *******************************************************************************/ static void gap_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed) { tGAP_CCB *p_ccb; GAP_TRACE_EVENT ("GAP_CONN - Rcvd L2CAP disc, CID: 0x%x", l2cap_cid); /* Find CCB based on CID */ if ((p_ccb = gap_find_ccb_by_cid (l2cap_cid)) == NULL) return; if (ack_needed) L2CA_DISCONNECT_RSP (l2cap_cid); p_ccb->p_callback (p_ccb->gap_handle, GAP_EVT_CONN_CLOSED); gap_release_ccb (p_ccb); }
/******************************************************************************* ** ** Function GAP_BleReadPeerDevName ** ** Description Start a process to read a connected peripheral's device name. ** ** Returns TRUE if request accepted ** *******************************************************************************/ BOOLEAN GAP_BleReadPeerDevName (BD_ADDR peer_bda, tGAP_BLE_DEV_NAME_CBACK *p_cback) { tGAP_CLCB *p_clcb = NULL; BOOLEAN status = TRUE; if (p_cback == NULL) return(FALSE); if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL) { if ((p_clcb = gap_clcb_alloc(0, peer_bda)) == NULL) { GAP_TRACE_ERROR("GAP_BleReadPeerDevName max connection reached"); return FALSE; } p_clcb->connected = FALSE; } GAP_TRACE_EVENT ("GAP_BleReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x", (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3], (peer_bda[4]<<8)+peer_bda[5], p_clcb->cl_op_uuid); /* For now we only handle one at a time */ if (p_clcb->cl_op_uuid != 0) return(FALSE); /* hold the link here */ if( BTM_IsInquiryActive()|| BTM_IsRnrActive() ) { status = GATT_Connect(gap_cb.gatt_if, p_clcb->bda, TRUE, BT_TRANSPORT_LE); } if(status) { if (p_clcb->connected) { return gap_ble_cl_read_request(p_clcb, GATT_UUID_GAP_DEVICE_NAME, (void *)p_cback); } p_clcb->p_cback = (void *)p_cback; /* Mark currently active operation */ p_clcb->cl_op_uuid = GATT_UUID_GAP_DEVICE_NAME; } return FALSE; }
/******************************************************************************* ** ** Function GAP_ConnClose ** ** Description This function is called to close a connection. ** ** Parameters: handle - Handle of the connection returned by GAP_ConnOpen ** ** Returns BT_PASS - closed OK ** GAP_ERR_BAD_HANDLE - invalid handle ** *******************************************************************************/ UINT16 GAP_ConnClose (UINT16 gap_handle) { tGAP_CCB *p_ccb = gap_find_ccb_by_handle (gap_handle); GAP_TRACE_EVENT ("GAP_CONN - close handle: 0x%x", gap_handle); if (p_ccb) { /* Check if we have a connection ID */ if (p_ccb->con_state != GAP_CCB_STATE_LISTENING) L2CA_DISCONNECT_REQ (p_ccb->connection_id); gap_release_ccb (p_ccb); return (BT_PASS); } return (GAP_ERR_BAD_HANDLE); }
/******************************************************************************* ** ** Function gap_connect_ind ** ** Description This function handles an inbound connection indication ** from L2CAP. This is the case where we are acting as a ** server. ** ** Returns void ** *******************************************************************************/ static void gap_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id) { UINT16 xx; tGAP_CCB *p_ccb; /* See if we have a CCB listening for the connection */ for (xx = 0, p_ccb = gap_cb.conn.ccb_pool; xx < GAP_MAX_CONNECTIONS; xx++, p_ccb++) { if ((p_ccb->con_state == GAP_CCB_STATE_LISTENING) && (p_ccb->psm == psm) && ((p_ccb->rem_addr_specified == FALSE) || (!memcmp (bd_addr, p_ccb->rem_dev_address, BD_ADDR_LEN)))) break; } if (xx == GAP_MAX_CONNECTIONS) { GAP_TRACE_WARNING("*******"); GAP_TRACE_WARNING("WARNING: GAP Conn Indication for Unexpected Bd Addr...Disconnecting"); GAP_TRACE_WARNING("*******"); /* Disconnect because it is an unexpected connection */ L2CA_DISCONNECT_REQ (l2cap_cid); return; } /* Transition to the next appropriate state, waiting for config setup. */ p_ccb->con_state = GAP_CCB_STATE_CFG_SETUP; /* Save the BD Address and Channel ID. */ memcpy (&p_ccb->rem_dev_address[0], bd_addr, BD_ADDR_LEN); p_ccb->connection_id = l2cap_cid; /* Send response to the L2CAP layer. */ L2CA_CONNECT_RSP (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK, &p_ccb->ertm_info); GAP_TRACE_EVENT("GAP_CONN - Rcvd L2CAP conn ind, CID: 0x%x", p_ccb->connection_id); /* Send a Configuration Request. */ L2CA_CONFIG_REQ (l2cap_cid, &p_ccb->cfg); }
/******************************************************************************* ** ** Function gap_get_conn_id_if_connected ** ** Description This function returns a connecttion handle to a ATT server ** if the server is already connected ** ** Parameters client_if: client interface. ** bd_addr: peer device address. ** ** Returns Connection handle or invalid handle value ** *******************************************************************************/ UINT16 gap_get_conn_id_if_connected (BD_ADDR bd_addr) { tGAP_CLCB *p_clcb; UINT16 i; GAP_TRACE_EVENT ("gap_get_conn_id_if_connected() - BDA: %08x%04x ", (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3], (bd_addr[4]<<8)+bd_addr[5]); for (i = 0, p_clcb = gap_cb.clcb; i < GAP_MAX_CL; i++, p_clcb++) { if (p_clcb->in_use && p_clcb->connected && !memcmp(p_clcb->bda, bd_addr, BD_ADDR_LEN) ) { return(p_clcb->conn_id); } } /* If here, failed to allocate a client control block */ GATT_TRACE_DEBUG ("gap_get_conn_id_if_connected: not connected"); return(GATT_INVALID_CONN_ID); }
/******************************************************************************* ** ** Function gap_sec_check_complete ** ** Description The function called when Security Manager finishes ** verification of the service side connection ** ** Returns void ** *******************************************************************************/ static void gap_sec_check_complete (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res) { tGAP_CCB *p_ccb = (tGAP_CCB *)p_ref_data; UNUSED(bd_addr); UNUSED (transport); GAP_TRACE_EVENT ("gap_sec_check_complete conn_state:%d, conn_flags:0x%x, status:%d", p_ccb->con_state, p_ccb->con_flags, res); if (p_ccb->con_state == GAP_CCB_STATE_IDLE) return; if (res == BTM_SUCCESS) { p_ccb->con_flags |= GAP_CCB_FLAGS_SEC_DONE; gap_checks_con_flags (p_ccb); } else { /* security failed - disconnect the channel */ L2CA_DISCONNECT_REQ (p_ccb->connection_id); } }
/******************************************************************************* ** ** Function btm_ble_att_db_init ** ** Description GAP ATT database initalization. ** ** Returns void. ** *******************************************************************************/ void gap_attr_db_init(void) { tBT_UUID app_uuid = {LEN_UUID_128,{0}}; tBT_UUID uuid = {LEN_UUID_16,{UUID_SERVCLASS_GAP_SERVER}}; UINT16 service_handle; tGAP_ATTR *p_db_attr = &gap_cb.gatt_attr[0]; tGATT_STATUS status; /* Fill our internal UUID with a fixed pattern 0x82 */ memset (&app_uuid.uu.uuid128, 0x82, LEN_UUID_128); memset(gap_cb.gatt_attr, 0, sizeof(tGAP_ATTR) *GAP_MAX_CHAR_NUM); gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback); GATT_StartIf(gap_cb.gatt_if); /* Create a GAP service */ service_handle = GATTS_CreateService (gap_cb.gatt_if, &uuid, 0, GAP_MAX_ATTR_NUM, TRUE); GAP_TRACE_EVENT ("gap_attr_db_init service_handle = %d", service_handle); /* add Device Name Characteristic */ uuid.len = LEN_UUID_16; uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_DEVICE_NAME; p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ); p_db_attr ++; /* add Icon characteristic */ uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_ICON; p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ); p_db_attr ++; #if BTM_PERIPHERAL_ENABLED == TRUE /* Only needed for peripheral testing */ /* add preferred connection parameter characteristic */ uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_PREF_CONN_PARAM; p_db_attr->attr_value.conn_param.int_max = GAP_PREFER_CONN_INT_MAX; /* 6 */ p_db_attr->attr_value.conn_param.int_min = GAP_PREFER_CONN_INT_MIN; /* 0 */ p_db_attr->attr_value.conn_param.latency = GAP_PREFER_CONN_LATENCY; /* 0 */ p_db_attr->attr_value.conn_param.sp_tout = GAP_PREFER_CONN_SP_TOUT; /* 2000 */ p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ); p_db_attr ++; #endif /* start service now */ memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128); status = GATTS_StartService(gap_cb.gatt_if, service_handle, GAP_TRANSPORT_SUPPORTED ); GAP_TRACE_EVENT ("GAP App gatt_if: %d s_hdl = %d start_status=%d", gap_cb.gatt_if, service_handle, status); }
/******************************************************************************* ** ** Function gap_ble_c_connect_cback ** ** Description Client connection callback. ** ** Returns void ** *******************************************************************************/ static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected, tGATT_DISCONN_REASON reason, tGATT_TRANSPORT transport) { tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (bda); UINT16 cl_op_uuid; UNUSED(gatt_if); UNUSED(transport); GAP_TRACE_EVENT ("gap_ble_c_connect_cback: from %08x%04x connected:%d conn_id=%d reason = 0x%04x", (bda[0]<<24)+(bda[1]<<16)+(bda[2]<<8)+bda[3], (bda[4]<<8)+bda[5], connected, conn_id, reason); if (connected) { if (p_clcb == NULL) { if ((p_clcb = gap_clcb_alloc(conn_id, bda))== NULL) { GAP_TRACE_ERROR ("gap_ble_c_connect_cback: no_resource"); return; } } p_clcb->conn_id = conn_id; p_clcb->connected = TRUE; } else { if (p_clcb != NULL) p_clcb->connected = FALSE; } if (p_clcb) { cl_op_uuid = p_clcb->cl_op_uuid; GAP_TRACE_EVENT ("cl_op_uuid=0x%04x", cl_op_uuid ); if (p_clcb->connected) { p_clcb->cl_op_uuid = 0; if (cl_op_uuid == GATT_UUID_GAP_DEVICE_NAME) { GAP_BleReadPeerDevName (bda, (tGAP_BLE_DEV_NAME_CBACK *)p_clcb->p_cback); } else if (cl_op_uuid == GATT_UUID_GAP_PREF_CONN_PARAM) { GAP_BleReadPeerPrefConnParams(bda); } } /* current link disconnect */ else { gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL); memset(p_clcb, 0, sizeof(tGAP_CLCB)); } } }
/******************************************************************************* ** ** Function GAP_ConnWriteData ** ** Description Normally not GKI aware application will call this function ** to send data to the connection. ** ** Parameters: handle - Handle of the connection returned in the Open ** p_data - Data area ** max_len - Byte count requested ** p_len - Byte count received ** ** Returns BT_PASS - data read ** GAP_ERR_BAD_HANDLE - invalid handle ** GAP_ERR_BAD_STATE - connection not established ** GAP_CONGESTION - system is congested ** *******************************************************************************/ UINT16 GAP_ConnWriteData (UINT16 gap_handle, UINT8 *p_data, UINT16 max_len, UINT16 *p_len) { tGAP_CCB *p_ccb = gap_find_ccb_by_handle (gap_handle); BT_HDR *p_buf; *p_len = 0; if (!p_ccb) return (GAP_ERR_BAD_HANDLE); if (p_ccb->con_state != GAP_CCB_STATE_CONNECTED) return (GAP_ERR_BAD_STATE); while (max_len) { if (p_ccb->cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) { if ((p_buf = (BT_HDR *)GKI_getpoolbuf (p_ccb->ertm_info.user_tx_pool_id)) == NULL) return (GAP_ERR_CONGESTED); } else { if ((p_buf = (BT_HDR *)GKI_getpoolbuf (GAP_DATA_POOL_ID)) == NULL) return (GAP_ERR_CONGESTED); } p_buf->offset = L2CAP_MIN_OFFSET; p_buf->len = (p_ccb->rem_mtu_size < max_len) ? p_ccb->rem_mtu_size : max_len; p_buf->event = BT_EVT_TO_BTU_SP_DATA; memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, p_buf->len); *p_len += p_buf->len; max_len -= p_buf->len; p_data += p_buf->len; GAP_TRACE_EVENT ("GAP_WriteData %d bytes", p_buf->len); GKI_enqueue (&p_ccb->tx_queue, p_buf); } if (p_ccb->is_congested) { return (BT_PASS); } /* Send the buffer through L2CAP */ #if (GAP_CONN_POST_EVT_INCLUDED == TRUE) gap_send_event (gap_handle); #else while ((p_buf = (BT_HDR *)GKI_dequeue (&p_ccb->tx_queue)) != NULL) { UINT8 status = L2CA_DATA_WRITE (p_ccb->connection_id, p_buf); if (status == L2CAP_DW_CONGESTED) { p_ccb->is_congested = TRUE; break; } else if (status != L2CAP_DW_SUCCESS) return (GAP_ERR_BAD_STATE); } #endif return (BT_PASS); }
/******************************************************************************* ** ** Function GAP_ConnOpen ** ** Description This function is called to open an L2CAP connection. ** ** Parameters: is_server - If TRUE, the connection is not created ** but put into a "listen" mode waiting for ** the remote side to connect. ** ** service_id - Unique service ID from ** BTM_SEC_SERVICE_FIRST_EMPTY (6) ** to BTM_SEC_MAX_SERVICE_RECORDS (32) ** ** p_rem_bda - Pointer to remote BD Address. ** If a server, and we don't care about the ** remote BD Address, then NULL should be passed. ** ** psm - the PSM used for the connection ** ** p_config - Optional pointer to configuration structure. ** If NULL, the default GAP configuration will ** be used. ** ** security - security flags ** chan_mode_mask - (GAP_FCR_CHAN_OPT_BASIC, GAP_FCR_CHAN_OPT_ERTM, ** GAP_FCR_CHAN_OPT_STREAM) ** ** p_cb - Pointer to callback function for events. ** ** Returns handle of the connection if successful, else GAP_INVALID_HANDLE ** *******************************************************************************/ UINT16 GAP_ConnOpen (char *p_serv_name, UINT8 service_id, BOOLEAN is_server, BD_ADDR p_rem_bda, UINT16 psm, tL2CAP_CFG_INFO *p_cfg, tL2CAP_ERTM_INFO *ertm_info, UINT16 security, UINT8 chan_mode_mask, tGAP_CONN_CALLBACK *p_cb) { tGAP_CCB *p_ccb; UINT16 cid; GAP_TRACE_EVENT ("GAP_CONN - Open Request"); /* Allocate a new CCB. Return if none available. */ if ((p_ccb = gap_allocate_ccb()) == NULL) return (GAP_INVALID_HANDLE); /* If caller specified a BD address, save it */ if (p_rem_bda) { /* the bd addr is not BT_BD_ANY, then a bd address was specified */ if (memcmp (p_rem_bda, BT_BD_ANY, BD_ADDR_LEN)) p_ccb->rem_addr_specified = TRUE; memcpy (&p_ccb->rem_dev_address[0], p_rem_bda, BD_ADDR_LEN); } else if (!is_server) { /* remore addr is not specified and is not a server -> bad */ return (GAP_INVALID_HANDLE); } /* A client MUST have specified a bd addr to connect with */ if (!p_ccb->rem_addr_specified && !is_server) { gap_release_ccb (p_ccb); GAP_TRACE_ERROR ("GAP ERROR: Client must specify a remote BD ADDR to connect to!"); return (GAP_INVALID_HANDLE); } /* Check if configuration was specified */ if (p_cfg) p_ccb->cfg = *p_cfg; p_ccb->p_callback = p_cb; /* If originator, use a dynamic PSM */ #if AMP_INCLUDED == TRUE if (!is_server) gap_cb.conn.reg_info.pAMP_ConnectInd_Cb = NULL; else gap_cb.conn.reg_info.pAMP_ConnectInd_Cb = gap_connect_ind; #else if (!is_server) gap_cb.conn.reg_info.pL2CA_ConnectInd_Cb = NULL; else gap_cb.conn.reg_info.pL2CA_ConnectInd_Cb = gap_connect_ind; #endif /* Register the PSM with L2CAP */ if ((p_ccb->psm = L2CA_REGISTER (psm, &gap_cb.conn.reg_info, AMP_AUTOSWITCH_ALLOWED|AMP_USE_AMP_IF_POSSIBLE)) == 0) { GAP_TRACE_ERROR ("GAP_ConnOpen: Failure registering PSM 0x%04x", psm); gap_release_ccb (p_ccb); return (GAP_INVALID_HANDLE); } /* Register with Security Manager for the specific security level */ p_ccb->service_id = service_id; if (!BTM_SetSecurityLevel ((UINT8)!is_server, p_serv_name, p_ccb->service_id, security, p_ccb->psm, 0, 0)) { GAP_TRACE_ERROR ("GAP_CONN - Security Error"); gap_release_ccb (p_ccb); return (GAP_INVALID_HANDLE); } /* Fill in eL2CAP parameter data */ if( p_ccb->cfg.fcr_present ) { if(ertm_info == NULL) { p_ccb->ertm_info.preferred_mode = p_ccb->cfg.fcr.mode; p_ccb->ertm_info.user_rx_pool_id = GAP_DATA_POOL_ID; p_ccb->ertm_info.user_tx_pool_id = GAP_DATA_POOL_ID; p_ccb->ertm_info.fcr_rx_pool_id = L2CAP_DEFAULT_ERM_POOL_ID; p_ccb->ertm_info.fcr_tx_pool_id = L2CAP_DEFAULT_ERM_POOL_ID; } else { p_ccb->ertm_info = *ertm_info; } } /* optional FCR channel modes */ if(ertm_info != NULL) { p_ccb->ertm_info.allowed_modes = (chan_mode_mask) ? chan_mode_mask : (UINT8)L2CAP_FCR_CHAN_OPT_BASIC; } if (is_server) { p_ccb->con_flags |= GAP_CCB_FLAGS_SEC_DONE; /* assume btm/l2cap would handle it */ p_ccb->con_state = GAP_CCB_STATE_LISTENING; return (p_ccb->gap_handle); } else { /* We are the originator of this connection */ p_ccb->con_flags = GAP_CCB_FLAGS_IS_ORIG; /* Transition to the next appropriate state, waiting for connection confirm. */ p_ccb->con_state = GAP_CCB_STATE_CONN_SETUP; /* mark security done flag, when security is not required */ if ((security & (BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT) ) == 0) p_ccb->con_flags |= GAP_CCB_FLAGS_SEC_DONE; /* Check if L2CAP started the connection process */ if (p_rem_bda && ((cid = L2CA_CONNECT_REQ (p_ccb->psm, p_rem_bda, &p_ccb->ertm_info)) != 0)) { p_ccb->connection_id = cid; return (p_ccb->gap_handle); } else { gap_release_ccb (p_ccb); return (GAP_INVALID_HANDLE); } } }