bt_status_t btsock_rfc_listen(const char *service_name, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) { assert(sock_fd != NULL); assert((service_uuid != NULL) || (channel >= 1 && channel <= MAX_RFC_CHANNEL) || ((flags & BTSOCK_FLAG_NO_SDP) != 0)); *sock_fd = INVALID_FD; // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call // functions on RFCOMM sockets before initializing the module. Probably should be an assert. if (!is_init_done()) return BT_STATUS_NOT_READY; if((flags & BTSOCK_FLAG_NO_SDP) == 0) { if(is_uuid_empty(service_uuid)) { APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_uuid not set AND " "BTSOCK_FLAG_NO_SDP is not set - changing to SPP"); service_uuid = UUID_SPP; // Use serial port profile to listen to specified channel } else { //Check the service_uuid. overwrite the channel # if reserved int reserved_channel = get_reserved_rfc_channel(service_uuid); if (reserved_channel > 0) { channel = reserved_channel; } } } int status = BT_STATUS_FAIL; pthread_mutex_lock(&slot_lock); rfc_slot_t *slot = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true); if (!slot) { LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__); goto out; } APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_name: %s - channel: %d", service_name, channel); BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, UINT_TO_PTR(slot->id), channel); *sock_fd = slot->app_fd; // Transfer ownership of fd to caller. /*TODO: * We are leaking one of the app_fd's - either the listen socket, or the connection socket. * WE need to close this in native, as the FD might belong to another process - This is the server socket FD - For accepted connections, we close the FD after passing it to JAVA. - Try to simply remove the = -1 to free the FD at rs cleanup.*/ // close(rs->app_fd); slot->app_fd = INVALID_FD; // Drop our reference to the fd. btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, slot->id); status = BT_STATUS_SUCCESS; out:; pthread_mutex_unlock(&slot_lock); return status; }
bt_status_t btsock_rfc_listen(const char* service_name, const uint8_t* service_uuid, int channel, int* sock_fd, int flags) { APPL_TRACE_DEBUG1("btsock_rfc_listen, service_name:%s", service_name); if(sock_fd == NULL || (service_uuid == NULL && (channel < 1 || channel > 30))) { APPL_TRACE_ERROR3("invalid rfc channel:%d or sock_fd:%p, uuid:%p", channel, sock_fd, service_uuid); return BT_STATUS_PARM_INVALID; } *sock_fd = -1; if(!is_init_done()) return BT_STATUS_NOT_READY; if(is_uuid_empty(service_uuid)) service_uuid = UUID_SPP; //use serial port profile to listen to specified channel else { //Check the service_uuid. overwrite the channel # if reserved int reserved_channel = get_reserved_rfc_channel(service_uuid); if(reserved_channel > 0) { channel = reserved_channel; } } int status = BT_STATUS_FAIL; lock_slot(&slot_lock); rfc_slot_t* rs = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, TRUE); if(rs) { APPL_TRACE_DEBUG1("BTA_JvCreateRecordByUser:%s", service_name); BTA_JvCreateRecordByUser((void *)rs->id); *sock_fd = rs->app_fd; rs->app_fd = -1; //the fd ownership is transferred to app status = BT_STATUS_SUCCESS; btsock_thread_add_fd(pth, rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, rs->id); } unlock_slot(&slot_lock); return status; }