BOOL btadp_jsr82_session_task_ongoing(BT_ADP_JSR82_SESSION *session, BT_JSR82_TASK_CODE code) { ListEntry *header = NULL; ListEntry *next = NULL; BT_Jsr82_Task *task = NULL; BOOL bRet = FALSE; ASSERT(session); session_mutex_lock(session->mutex); header = &session->pending_task; next = &session->pending_task; if(IsListEmpty(header)) { goto exit; } do { next = GetNextNode(next); task = (BT_Jsr82_Task *)next; if (task->code == code) { bRet = TRUE; break; } } while (next != header); exit: session_mutex_unlock(session->mutex); return bRet; }
BOOL btadp_jsr82_session_add_task(BT_ADP_JSR82_SESSION *session, BT_JSR82_TASK_CODE code, void *context) { BT_Jsr82_Task *task = NULL; if (NULL == session || BT_ADP_JSR82_STATE_IDLE == session->conn_state) { return FALSE; } if (session->pending_task_num == 0xFF) { OS_Report("[JSR82] task list is full"); return FALSE; } task = (BT_Jsr82_Task *)malloc (sizeof(BT_Jsr82_Task)); if (NULL == task) { return FALSE; } memset(task, 0x0, sizeof(BT_Jsr82_Task)); task->code = code; task->context = context; session_mutex_lock(session->mutex); InitializeListEntry(&task->node); InsertTailList(&session->pending_task, &task->node); session->pending_task_num ++; session_mutex_unlock(session->mutex); return TRUE; }
void btmtk_jbt_deinit(void) { if (FALSE == jbt_jsr82.init) { return; } session_mutex_lock(jbt_jsr82.mutex); //double check if (FALSE == jbt_jsr82.init) { goto exit; } jbt_l2cap_deinit(&jbt_jsr82.l2cap); jbt_spp_deinit(&jbt_jsr82.spp); jbt_session_stop_monitor(jbt_jsr82.monitorid); btmtk_jsr82_sdp_deinit(); jbt_jsr82.init = FALSE; exit: session_mutex_unlock(jbt_jsr82.mutex); }
//send_data_to_lower void btadp_jsr82_session_send(BT_ADP_JSR82_SESSION *session) { int free_space_size = 0; int size = 0; int len = 0; kal_int16 sentLen = 0; ASSERT(session); OS_Report("[JSR82]btadp_jsr82_session_send"); if (session->conn_state != BT_ADP_JSR82_STATE_CONNECTED) { OS_Report("[JSR82]SR_E_INVALID_PARAM"); return; } //this should not happen if (BT_JSR82_SESSION_STATE_ATTACHED != session->attach_state) { if (FALSE == btadp_jsr82_session_add_task(session, JSR82_TASK_SEND, NULL)) { OS_Report("[JSR82]fail to add pending task"); } goto exit; } session_mutex_lock(session->mutex); free_space_size = jsr82_session_BufFreeSpace(session->ps_type, session->index, session->l2cap_id, JBT_SESSION_TX_BUF_TYPE); if(free_space_size <= 0) { OS_Report("[JSR82]no free space in TX buffer: %d", free_space_size); goto exit; } len = bt_session_read(session->sessionid, session->txbuffer, free_space_size); if (len <= 0) { OS_Report("[JSR82]read fail %d", len); goto exit; } session->txlength = len; sentLen = btadp_jsr82_session_send_data(session); if (sentLen < len) { OS_Report("[JSR82][warning]only partial data is sent to lower layer"); } //TODO: store remained data to buffer session->txlength = sentLen; exit: session_mutex_unlock(session->mutex); return; }
void btadp_jsr82_session_process_task(BT_ADP_JSR82_SESSION *session) { BT_Jsr82_Task *task = NULL; U8 original_task_num = session->pending_task_num; if (NULL == session || BT_ADP_JSR82_STATE_IDLE == session->conn_state) { return; } session_mutex_lock(session->mutex); while ((original_task_num > 0) && FALSE == IsListEmpty(&session->pending_task)) { task = (BT_Jsr82_Task *)RemoveHeadList(&session->pending_task); //release lock and permit to process pending task session_mutex_unlock(session->mutex); switch (task->code) { case JSR82_TASK_CHANNEL_CREATED: btadp_jsr82_channel_connected(session, NULL, 0); break; case JSR82_TASK_RECEIVE: btadp_jsr82_session_receive(session); break; default: OS_Report("[JSR82][ADP]unexpected event %d", task->code); } //free resource free(task); session_mutex_lock(session->mutex); //check the list node number with node record original_task_num --; } session_mutex_unlock(session->mutex); }
/***************************************************************************** * FUNCTION * jbt_session_service_deregistration * DESCRIPTION * * PARAMETERS * ps_type [IN] * transaction_id [IN] * con_id [IN] * RETURNS * *****************************************************************************/ BT_BOOL btmtk_jsr82_session_service_deregistration(kal_uint8 ps_type, kal_uint32 transaction_id, kal_uint8 con_id) { /*----------------------------------------------------------------*/ /* Local Variables */ /*----------------------------------------------------------------*/ ilm_struct ilm; ilm_struct *ilmptr; jbt_list *existing_entry = NULL; jbt_session_info *session_entry = NULL; BT_BOOL jret = FALSE; /*----------------------------------------------------------------*/ /* Code Body */ /*----------------------------------------------------------------*/ bt_ext_log("[JSR82][JBT] JBT jbt_session_service_deregistration"); //lock context if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); return FALSE; } session_mutex_lock(jbt_jsr82.mutex); //lock context again if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); jret = FALSE; goto exit; } session_entry = jbt_search_existing_session_entry(ps_type, JBT_LIST_SEARCH_TYPE_BY_INX, con_id); if (session_entry == NULL) { bt_ext_log("[JSR82][JBT] JBT can not find an existing entry with type=%d and value=%d", JBT_LIST_SEARCH_TYPE_BY_INX, con_id); jret = FALSE; goto exit; } session_entry->transaction_id = transaction_id; jret = jbt_session_general_service_deregistration(session_entry); exit: session_mutex_unlock(jbt_jsr82.mutex); return jret; }
void btadp_remove_all_tasks(BT_ADP_JSR82_SESSION *session) { BT_Jsr82_Task *task = NULL; if (NULL == session || BT_ADP_JSR82_STATE_IDLE == session->conn_state) { return; } session_mutex_lock(session->mutex); while (FALSE == IsListEmpty(&session->pending_task)) { task = (BT_Jsr82_Task *)RemoveHeadList(&session->pending_task); free(task); } session_mutex_unlock(session->mutex); }
void bt_session_attached(U32 id) { BT_ADP_JSR82_SESSION *session = NULL; OS_Report("[JSR82]bt_session_attached: id[%8x]",id); //todo: add mutex protection session_mutex_lock(jsr82_adp_cntx.mutex); session = btadp_jsr82_find_session_by_id(id); if (NULL == session) { session = btadp_jsr82_find_available_session(); if (NULL != session) { btadp_jsr82_session_init(session); session->sessionid = id; session->conn_state = BT_ADP_JSR82_STATE_ACTIVE; } else { OS_Report("[JSR82] No resource"); } } else { //session attached } session_mutex_unlock(jsr82_adp_cntx.mutex); if (NULL == session) { bt_session_destroy(id); } session->attach_state = BT_JSR82_SESSION_STATE_ATTACHED; btadp_jsr82_session_process_task(session); }
/***************************************************************************** * FUNCTION * jbt_session_disconnect_req * DESCRIPTION * * PARAMETERS * transaction_id [IN] * ps_type [IN] * con_id [IN] * l2cap_id [IN] * RETURNS * *****************************************************************************/ BT_BOOL btmtk_jsr82_session_disconnect_req(kal_uint32 transaction_id, kal_uint8 ps_type, U8 con_id, kal_uint16 l2cap_id) { /*----------------------------------------------------------------*/ /* Local Variables */ /*----------------------------------------------------------------*/ ilm_struct ilm; ilm_struct *ilmptr; jbt_list *existing_entry = NULL; jbt_session_info *session_entry = NULL; jbt_subsession *subsession_entry = NULL; BT_BOOL jret = FALSE; /*----------------------------------------------------------------*/ /* Code Body */ /*----------------------------------------------------------------*/ bt_ext_log("[JSR82][JBT] JBT jbt_session_disconnect_req"); //lock context if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); return FALSE; } session_mutex_lock(jbt_jsr82.mutex); //lock context again if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); jret = FALSE; goto exit; } session_entry = jbt_search_existing_session_entry(ps_type, JBT_LIST_SEARCH_TYPE_BY_INX, con_id); if (session_entry == NULL) { bt_ext_log("[JSR82][JBT] JBT can not find an existing entry with type=%d and value=%d", JBT_LIST_SEARCH_TYPE_BY_INX, con_id); jret = FALSE; goto exit; } subsession_entry = jbt_search_existing_subsession_entry(session_entry, JBT_LIST_SEARCH_TYPE_BY_INX, l2cap_id); ASSERT(l2cap_id == subsession_entry->subsession_id); session_entry->transaction_id = transaction_id; session_entry->ps_type = ps_type; if (subsession_entry->subsession_state == JBT_SESSION_STATE_CONNECTED) { subsession_entry->subsession_state = JBT_SESSION_STATE_DISCONNECTING; } else { /* return FALSE: due to error session state */ jret = FALSE; goto exit; } jret = jbt_session_general_disconnect(session_entry, subsession_entry); exit: session_mutex_unlock(jbt_jsr82.mutex); return jret; }
/***************************************************************************** * FUNCTION * jbt_session_connect_req * DESCRIPTION * * PARAMETERS * transaction_id [IN] * bd_addr [IN] * ps_type [IN] * psm_channel [IN] * mtu [IN] * security_value [IN] * RETURNS * *****************************************************************************/ BT_BOOL btmtk_jsr82_session_connect_req( kal_uint32 transaction_id, kal_uint8 *bd_addr, kal_uint8 *uuid128, kal_uint8 ps_type, kal_uint16 psm_channel, kal_uint16 mtu, kal_uint8 security_value, kal_int32* fd) { /*----------------------------------------------------------------*/ /* Local Variables */ /*----------------------------------------------------------------*/ ilm_struct ilm; ilm_struct *ilmptr; jbt_list *free_entry = NULL; jbt_session_info *session_entry = NULL; jbt_subsession *subsession_entry = NULL; kal_uint8 block_inx = 0; kal_uint8 list_type = 0; U32 sessionid; SESSION_RESULT sret = SR_NOERROR; BT_BOOL jret = FALSE; /*----------------------------------------------------------------*/ /* Code Body */ /*----------------------------------------------------------------*/ bt_ext_log("[JSR82][JBT] JBT jbt_session_connect_req"); //lock context if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); return FALSE; } session_mutex_lock(jbt_jsr82.mutex); //lock context again if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); jret = FALSE; goto exit; } ASSERT((ps_type == JSR82_SESSION_PS_RFCOMM) || (ps_type == JSR82_SESSION_PS_L2CAP)); // TODO: We should allow multi-connection scenario usage. // RFCOMM do NOT allow 2 connections with the same channel between two devices. // L2CAP: 2 connections in the same PSM channel from a device is acceptable. /* Check if this bd_address and its channel is in activeList or not: 2007-1213 */ if (JSR82_SESSION_PS_RFCOMM == ps_type) { if (jbt_check_already_connect_chnl_and_addr(bd_addr, ps_type, psm_channel)) { jret = FALSE; goto exit; } } session_entry = jbt_allocate_one_available_session_entry(ps_type); if (session_entry == NULL) { bt_ext_log("[JSR82][JBT] JBT can not allocate a free entry"); jret = FALSE; goto exit; } transaction_id = get_transaction_id(); subsession_entry = jbt_allocate_one_available_subsession_entry(session_entry); if (subsession_entry != NULL) { if (subsession_entry->subsession_state == JBT_SESSION_STATE_IDLE) { subsession_entry->subsession_state = JBT_SESSION_STATE_CONNECTING; subsession_entry->transaction_id = transaction_id; } else { /* session_state is in an error state */ bt_ext_err("[JSR82] the subsession state is abnormal %d", subsession_entry->subsession_state); jret = FALSE; goto exit; } } /* * Modify: if the bd_addr is sent to btstack via array format, it must be from low byte to high byte [0] to [5] * This function invoked by JVM passes the addr format from high to low ([0] to [6]) * The bd_addr array returned to JVM is also fro, high to low ([0] to [6]) */ jbt_ConvertBdAddrOrder(subsession_entry->bd_addr,bd_addr); // memcpy(subsession_entry->bd_addr, bd_addr, 6); session_entry->transaction_id = transaction_id; session_entry->ps_type = ps_type; /* Add 2007-1213 */ session_entry->psm_channel = psm_channel; session_entry->initiator = JBT_SESSION_INITIATOR_TRUE; session_entry->con_req_op.operation_state = JBT_OPERATION_STATE_ONGOING; session_entry->transaction_id = transaction_id; subsession_entry->transaction_id = transaction_id; session_entry->security_value = security_value; session_entry->mtu = mtu; sret = jbt_session_attach(transaction_id, &sessionid,(int *)fd); if (SR_NOERROR != sret) { jbt_reset_session_entry(session_entry); jret = FALSE; goto exit; } session_entry->session_id = sessionid; subsession_entry->subsession_id = 0; memcpy(session_entry->uuid_128, uuid128, 16); if (FALSE == jbt_session_general_connect(session_entry, subsession_entry)) { bt_ext_log("[JSR82][JBT] fail to connect"); sret = bt_session_lock(session_entry->session_id, FALSE); jbt_reset_session_entry(session_entry); jret = FALSE; } else { jret = TRUE; } exit: session_mutex_unlock(jbt_jsr82.mutex); return jret; }
/***************************************************************************** * FUNCTION * jbt_session_service_registration * DESCRIPTION * * PARAMETERS * ps_type [IN] * mtu [IN] * security [IN] * ==> Authentication or Encryption is necessary: 0x01 * else: 0x00 * transaction_id [IN] * RETURNS * *****************************************************************************/ BT_BOOL btmtk_jsr82_session_service_registration( kal_uint8 ps_type, kal_uint8 *uuid128, kal_uint8 *name, kal_uint32 namesize, kal_uint16 mtu, kal_uint8 security, kal_uint32 transaction_id, kal_int32 *fd) { /*----------------------------------------------------------------*/ /* Local Variables */ /*----------------------------------------------------------------*/ jbt_list *free_entry = NULL; jbt_session_info *session_entry = NULL; kal_uint16 max_mtu = JSR82_SESSION_PS_RFCOMM_MTU; kal_uint8 list_type = 0; SESSION_RESULT sret = SR_NOERROR; int fds[2]; U32 sessionids[2]; BT_BOOL jret = FALSE; /*----------------------------------------------------------------*/ /* Code Body */ /*----------------------------------------------------------------*/ bt_ext_log("[JSR82][JBT] JBT jbt_session_service_registration"); //lock context if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); return FALSE; } session_mutex_lock(jbt_jsr82.mutex); //lock context again if (FALSE == btmtk_jbt_check_context_active()) { bt_ext_err("[JSR82][JBT] JSR82 does not init"); jret = FALSE; goto exit; } /* Modify from or to and condition: 2007-1102 */ if ((security != 0) && ((security != 1))) { bt_ext_log("[JSR82][JBT] JBT Command Reject B"); jret = FALSE; goto exit; } session_entry = jbt_allocate_one_available_session_entry(ps_type); if (session_entry == NULL) { bt_ext_log("[JSR82][JBT] JBT can not allocate a free entry"); jret = FALSE; goto exit; } list_type = jbt_convert_list_type(ps_type); /* The actual service channel registered by this service is dependend on Stack's cureent channel number usage: 2007-1107 */ session_entry->psm_channel = 0; if (mtu > max_mtu) { mtu = max_mtu; } transaction_id = get_transaction_id(); sret = jbt_iternal_session_attach(sessionids, fds); if (SR_NOERROR != sret) { bt_ext_log("[JSR82][JBT] fail to attach session"); jbt_free_one_existing_entry(list_type, (jbt_list *)session_entry); jret = FALSE; goto exit; } //fds[0] --> app //fds[1] keep it for self *fd = fds[0]; session_entry->fd = fds[1]; session_entry->session_id = sessionids[1]; session_entry->twin_session_id = sessionids[0]; //sdp info memcpy(session_entry->sdpinfo.uuid, uuid128, BTMTK_SDP_UUID_128_BIT_SIZE); strncpy((char *)session_entry->sdpinfo.name, (char *)name, SDP_REGISTRATION_NAME_MAX_LEN); session_entry->sdpinfo.identify = sessionids[1]; session_entry->sdpinfo.callback = btmtk_jsr82_sdp_register_callback; session_entry->sdpinfo.context = session_entry; session_entry->transaction_id = transaction_id; session_entry->ps_type = ps_type; session_entry->initiator = JBT_SESSION_INITIATOR_FALSE; session_entry->index = 0xFF; session_entry->mtu = mtu; session_entry->security_value = security; if(jbt_session_general_service_registration(session_entry)) { jret = TRUE; goto exit; } else { bt_ext_log("[JSR82][JBT] fail to register service"); sret = bt_session_lock(session_entry->session_id, FALSE); sret = bt_session_lock(session_entry->twin_session_id, FALSE); jbt_reset_session_entry(session_entry); jret = FALSE; goto exit; } exit: session_mutex_unlock(jbt_jsr82.mutex); return jret; }