1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #define LOG_TAG "bt_btif_sock_rfcomm"
24 #include <hardware/bluetooth.h>
25 #include <hardware/bt_sock.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
33 #include "bt_target.h"
34 #include "bta_jv_api.h"
35 #include "bta_jv_co.h"
36 #include "btif_common.h"
37 #include "btif_sock_sdp.h"
38 #include "btif_sock_thread.h"
39 #include "btif_sock_util.h"
40 /* The JV interface can have only one user, hence we need to call a few
41 * L2CAP functions from this file. */
42 #include "btif_sock_l2cap.h"
45 #include "btif_util.h"
51 #include "osi/include/compat.h"
52 #include "osi/include/list.h"
53 #include "osi/include/osi.h"
54 #include "osi/include/log.h"
58 #define MAX_RFC_CHANNEL 30 // Maximum number of RFCOMM channels (1-30 inclusive).
59 #define MAX_RFC_SESSION 7 // Maximum number of devices we can have an RFCOMM connection with.
62 int outgoing_congest : 1;
63 int pending_sdp_request : 1;
64 int doing_sdp_request : 1;
72 uint32_t id; // Non-zero indicates a valid (in-use) slot.
74 int scn; // Server channel number
77 int is_service_uuid_valid;
78 uint8_t service_uuid[16];
79 char service_name[256];
81 int app_fd; // Temporary storage for the half of the socketpair that's sent back to upper layers.
88 list_t *incoming_queue;
91 static rfc_slot_t rfc_slots[MAX_RFC_CHANNEL];
92 static uint32_t rfc_slot_id;
93 static volatile int pth = -1; // poll thread handle
94 static pthread_mutex_t slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
96 static rfc_slot_t *find_free_slot(void);
97 static void cleanup_rfc_slot(rfc_slot_t *rs);
98 static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data);
99 static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data);
100 static bool send_app_scn(rfc_slot_t *rs);
102 static bool is_init_done(void) {
106 bt_status_t btsock_rfc_init(int poll_thread_handle) {
107 pth = poll_thread_handle;
109 memset(rfc_slots, 0, sizeof(rfc_slots));
110 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
111 rfc_slots[i].scn = -1;
112 rfc_slots[i].sdp_handle = 0;
113 rfc_slots[i].fd = INVALID_FD;
114 rfc_slots[i].app_fd = INVALID_FD;
115 rfc_slots[i].incoming_queue = list_new(GKI_freebuf);
116 assert(rfc_slots[i].incoming_queue != NULL);
119 BTA_JvEnable(jv_dm_cback);
121 return BT_STATUS_SUCCESS;
124 void btsock_rfc_cleanup(void) {
127 pthread_mutex_lock(&slot_lock);
128 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
130 cleanup_rfc_slot(&rfc_slots[i]);
131 list_free(rfc_slots[i].incoming_queue);
133 pthread_mutex_unlock(&slot_lock);
136 static rfc_slot_t *find_free_slot(void) {
137 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
138 if (rfc_slots[i].fd == INVALID_FD)
139 return &rfc_slots[i];
143 static rfc_slot_t *find_rfc_slot_by_id(uint32_t id) {
146 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
147 if (rfc_slots[i].id == id)
148 return &rfc_slots[i];
150 LOG_ERROR("%s unable to find RFCOMM slot id: %d", __func__, id);
154 static rfc_slot_t *find_rfc_slot_by_pending_sdp(void) {
155 uint32_t min_id = UINT32_MAX;
157 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
158 if (rfc_slots[i].id && rfc_slots[i].f.pending_sdp_request && rfc_slots[i].id < min_id) {
159 min_id = rfc_slots[i].id;
163 return (slot == -1) ? NULL : &rfc_slots[slot];
166 static bool is_requesting_sdp(void) {
167 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
168 if (rfc_slots[i].id && rfc_slots[i].f.doing_sdp_request)
173 static rfc_slot_t *alloc_rfc_slot(const bt_bdaddr_t *addr, const char *name, const uint8_t *uuid, int channel, int flags, bool server) {
175 if(flags & BTSOCK_FLAG_ENCRYPT)
176 security |= server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT;
177 if(flags & BTSOCK_FLAG_AUTH)
178 security |= server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE;
179 if(flags & BTSOCK_FLAG_AUTH_MITM)
180 security |= server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM;
181 if(flags & BTSOCK_FLAG_AUTH_16_DIGIT)
182 security |= BTM_SEC_IN_MIN_16_DIGIT_PIN;
184 rfc_slot_t *slot = find_free_slot();
186 LOG_ERROR("%s unable to find free RFCOMM slot.", __func__);
190 int fds[2] = { INVALID_FD, INVALID_FD };
191 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) == -1) {
192 LOG_ERROR("%s error creating socketpair: %s", __func__, strerror(errno));
196 // Increment slot id and make sure we don't use id=0.
197 if (++rfc_slot_id == 0)
201 slot->app_fd = fds[1];
202 slot->security = security;
205 if(!is_uuid_empty(uuid)) {
206 memcpy(slot->service_uuid, uuid, sizeof(slot->service_uuid));
207 slot->is_service_uuid_valid = true;
209 memset(slot->service_uuid, 0, sizeof(slot->service_uuid));
210 slot->is_service_uuid_valid = false;
213 strlcpy(slot->service_name, name, sizeof(slot->service_name));
215 memset(slot->service_name, 0, sizeof(slot->service_name));
220 slot->id = rfc_slot_id;
221 slot->f.server = server;
226 static rfc_slot_t *create_srv_accept_rfc_slot(rfc_slot_t *srv_rs, const bt_bdaddr_t *addr, int open_handle, int new_listen_handle) {
227 rfc_slot_t *accept_rs = alloc_rfc_slot(addr, srv_rs->service_name, srv_rs->service_uuid, srv_rs->scn, 0, false);
229 LOG_ERROR("%s unable to allocate RFCOMM slot.", __func__);
233 accept_rs->f.server = false;
234 accept_rs->f.connected = true;
235 accept_rs->security = srv_rs->security;
236 accept_rs->mtu = srv_rs->mtu;
237 accept_rs->role = srv_rs->role;
238 accept_rs->rfc_handle = open_handle;
239 accept_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(open_handle);
241 srv_rs->rfc_handle = new_listen_handle;
242 srv_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(new_listen_handle);
244 assert(accept_rs->rfc_port_handle != srv_rs->rfc_port_handle);
246 // now swap the slot id
247 uint32_t new_listen_id = accept_rs->id;
248 accept_rs->id = srv_rs->id;
249 srv_rs->id = new_listen_id;
254 bt_status_t btsock_rfc_listen(const char *service_name, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) {
255 assert(sock_fd != NULL);
256 assert((service_uuid != NULL)
257 || (channel >= 1 && channel <= MAX_RFC_CHANNEL)
258 || ((flags & BTSOCK_FLAG_NO_SDP) != 0));
260 *sock_fd = INVALID_FD;
262 // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call
263 // functions on RFCOMM sockets before initializing the module. Probably should be an assert.
265 return BT_STATUS_NOT_READY;
267 if((flags & BTSOCK_FLAG_NO_SDP) == 0) {
268 if(is_uuid_empty(service_uuid)) {
269 APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_uuid not set AND "
270 "BTSOCK_FLAG_NO_SDP is not set - changing to SPP");
271 service_uuid = UUID_SPP; // Use serial port profile to listen to specified channel
273 //Check the service_uuid. overwrite the channel # if reserved
274 int reserved_channel = get_reserved_rfc_channel(service_uuid);
275 if (reserved_channel > 0) {
276 channel = reserved_channel;
281 int status = BT_STATUS_FAIL;
282 pthread_mutex_lock(&slot_lock);
284 rfc_slot_t *slot = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true);
286 LOG_ERROR("%s unable to allocate RFCOMM slot.", __func__);
289 APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_name: %s - channel: %d", service_name, channel);
290 BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, (void*) slot->id, channel);
291 *sock_fd = slot->app_fd; // Transfer ownership of fd to caller.
293 * We are leaking one of the app_fd's - either the listen socket, or the connection socket.
294 * WE need to close this in native, as the FD might belong to another process
295 - This is the server socket FD
296 - For accepted connections, we close the FD after passing it to JAVA.
297 - Try to simply remove the = -1 to free the FD at rs cleanup.*/
298 // close(rs->app_fd);
299 slot->app_fd = INVALID_FD; // Drop our reference to the fd.
300 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, slot->id);
302 status = BT_STATUS_SUCCESS;
305 pthread_mutex_unlock(&slot_lock);
309 bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) {
310 assert(sock_fd != NULL);
311 assert(service_uuid != NULL || (channel >= 1 && channel <= MAX_RFC_CHANNEL));
313 *sock_fd = INVALID_FD;
315 // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call
316 // functions on RFCOMM sockets before initializing the module. Probably should be an assert.
318 return BT_STATUS_NOT_READY;
320 int status = BT_STATUS_FAIL;
321 pthread_mutex_lock(&slot_lock);
323 rfc_slot_t *slot = alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false);
325 LOG_ERROR("%s unable to allocate RFCOMM slot.", __func__);
329 if (is_uuid_empty(service_uuid)) {
330 tBTA_JV_STATUS ret = BTA_JvRfcommConnect(slot->security, slot->role, slot->scn, slot->addr.address, rfcomm_cback, (void *)(uintptr_t)slot->id);
331 if (ret != BTA_JV_SUCCESS) {
332 LOG_ERROR("%s unable to initiate RFCOMM connection: %d", __func__, ret);
333 cleanup_rfc_slot(slot);
337 if (!send_app_scn(slot)) {
338 LOG_ERROR("%s unable to send channel number.", __func__);
339 cleanup_rfc_slot(slot);
345 memcpy(sdp_uuid.uu.uuid128, service_uuid, sizeof(sdp_uuid.uu.uuid128));
347 if (!is_requesting_sdp()) {
348 BTA_JvStartDiscovery((uint8_t *)bd_addr->address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id);
349 slot->f.pending_sdp_request = false;
350 slot->f.doing_sdp_request = true;
352 slot->f.pending_sdp_request = true;
353 slot->f.doing_sdp_request = false;
357 *sock_fd = slot->app_fd; // Transfer ownership of fd to caller.
358 slot->app_fd = INVALID_FD; // Drop our reference to the fd.
359 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
360 status = BT_STATUS_SUCCESS;
363 pthread_mutex_unlock(&slot_lock);
367 static int create_server_sdp_record(rfc_slot_t *slot) {
371 slot->sdp_handle = add_rfc_sdp_rec(slot->service_name, slot->service_uuid, slot->scn);
372 return (slot->sdp_handle > 0);
375 static void free_rfc_slot_scn(rfc_slot_t *slot) {
379 if(slot->f.server && !slot->f.closing && slot->rfc_handle) {
380 BTA_JvRfcommStopServer(slot->rfc_handle, (void *)(uintptr_t)slot->id);
381 slot->rfc_handle = 0;
385 BTM_FreeSCN(slot->scn);
389 static void cleanup_rfc_slot(rfc_slot_t *slot) {
390 if (slot->fd != INVALID_FD) {
391 shutdown(slot->fd, SHUT_RDWR);
393 slot->fd = INVALID_FD;
396 if (slot->app_fd != INVALID_FD) {
398 slot->app_fd = INVALID_FD;
401 if (slot->sdp_handle > 0) {
402 del_rfc_sdp_rec(slot->sdp_handle);
403 slot->sdp_handle = 0;
406 if (slot->rfc_handle && !slot->f.closing && !slot->f.server) {
407 BTA_JvRfcommClose(slot->rfc_handle, (void *)(uintptr_t)slot->id);
408 slot->rfc_handle = 0;
411 free_rfc_slot_scn(slot);
412 list_clear(slot->incoming_queue);
414 slot->rfc_port_handle = 0;
415 memset(&slot->f, 0, sizeof(slot->f));
417 slot->scn_notified = false;
420 static bool send_app_scn(rfc_slot_t *slot) {
421 if(slot->scn_notified == true) {
422 //already send, just return success.
425 slot->scn_notified = true;
426 return sock_send_all(slot->fd, (const uint8_t*)&slot->scn, sizeof(slot->scn)) == sizeof(slot->scn);
429 static bool send_app_connect_signal(int fd, const bt_bdaddr_t* addr, int channel, int status, int send_fd) {
430 sock_connect_signal_t cs;
431 cs.size = sizeof(cs);
433 cs.channel = channel;
435 cs.max_rx_packet_size = 0; // not used for RFCOMM
436 cs.max_tx_packet_size = 0; // not used for RFCOMM
437 if (send_fd == INVALID_FD)
438 return sock_send_all(fd, (const uint8_t *)&cs, sizeof(cs)) == sizeof(cs);
440 return sock_send_fd(fd, (const uint8_t *)&cs, sizeof(cs), send_fd) == sizeof(cs);
443 static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT *p_init, uint32_t id) {
444 pthread_mutex_lock(&slot_lock);
446 rfc_slot_t *slot = find_rfc_slot_by_id(id);
450 if (p_init->status == BTA_JV_SUCCESS)
451 slot->rfc_handle = p_init->handle;
453 cleanup_rfc_slot(slot);
456 pthread_mutex_unlock(&slot_lock);
459 static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START *p_start, uint32_t id) {
460 pthread_mutex_lock(&slot_lock);
462 rfc_slot_t *slot = find_rfc_slot_by_id(id);
466 if (p_start->status == BTA_JV_SUCCESS) {
467 slot->rfc_handle = p_start->handle;
469 cleanup_rfc_slot(slot);
472 pthread_mutex_unlock(&slot_lock);
475 static uint32_t on_srv_rfc_connect(tBTA_JV_RFCOMM_SRV_OPEN *p_open, uint32_t id) {
476 uint32_t new_listen_slot_id = 0;
477 pthread_mutex_lock(&slot_lock);
479 rfc_slot_t *srv_rs = find_rfc_slot_by_id(id);
483 rfc_slot_t *accept_rs = create_srv_accept_rfc_slot(srv_rs, (const bt_bdaddr_t *)p_open->rem_bda, p_open->handle, p_open->new_listen_handle);
487 // Start monitoring the socket.
488 btsock_thread_add_fd(pth, srv_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, srv_rs->id);
489 btsock_thread_add_fd(pth, accept_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, accept_rs->id);
490 send_app_connect_signal(srv_rs->fd, &accept_rs->addr, srv_rs->scn, 0, accept_rs->app_fd);
491 accept_rs->app_fd = INVALID_FD; // Ownership of the application fd has been transferred.
492 new_listen_slot_id = srv_rs->id;
495 pthread_mutex_unlock(&slot_lock);
496 return new_listen_slot_id;
499 static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN *p_open, uint32_t id) {
500 pthread_mutex_lock(&slot_lock);
502 rfc_slot_t *slot = find_rfc_slot_by_id(id);
506 if (p_open->status != BTA_JV_SUCCESS) {
507 cleanup_rfc_slot(slot);
511 slot->rfc_port_handle = BTA_JvRfcommGetPortHdl(p_open->handle);
512 memcpy(slot->addr.address, p_open->rem_bda, 6);
514 if (send_app_connect_signal(slot->fd, &slot->addr, slot->scn, 0, -1))
515 slot->f.connected = true;
517 LOG_ERROR("%s unable to send connect completion signal to caller.", __func__);
520 pthread_mutex_unlock(&slot_lock);
523 static void on_rfc_close(UNUSED_ATTR tBTA_JV_RFCOMM_CLOSE *p_close, uint32_t id) {
524 pthread_mutex_lock(&slot_lock);
526 // rfc_handle already closed when receiving rfcomm close event from stack.
527 rfc_slot_t *slot = find_rfc_slot_by_id(id);
529 cleanup_rfc_slot(slot);
531 pthread_mutex_unlock(&slot_lock);
534 static void on_rfc_write_done(UNUSED_ATTR tBTA_JV_RFCOMM_WRITE *p, uint32_t id) {
535 pthread_mutex_lock(&slot_lock);
537 rfc_slot_t *slot = find_rfc_slot_by_id(id);
538 if (slot && !slot->f.outgoing_congest)
539 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
541 pthread_mutex_unlock(&slot_lock);
544 static void on_rfc_outgoing_congest(tBTA_JV_RFCOMM_CONG *p, uint32_t id) {
545 pthread_mutex_lock(&slot_lock);
547 rfc_slot_t *slot = find_rfc_slot_by_id(id);
549 slot->f.outgoing_congest = p->cong ? 1 : 0;
550 if (!slot->f.outgoing_congest)
551 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
554 pthread_mutex_unlock(&slot_lock);
557 static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
558 void *new_user_data = NULL;
561 case BTA_JV_RFCOMM_START_EVT:
562 on_srv_rfc_listen_started(&p_data->rfc_start, (uintptr_t)user_data);
565 case BTA_JV_RFCOMM_CL_INIT_EVT:
566 on_cl_rfc_init(&p_data->rfc_cl_init, (uintptr_t)user_data);
569 case BTA_JV_RFCOMM_OPEN_EVT:
570 BTA_JvSetPmProfile(p_data->rfc_open.handle,BTA_JV_PM_ID_1,BTA_JV_CONN_OPEN);
571 on_cli_rfc_connect(&p_data->rfc_open, (uintptr_t)user_data);
574 case BTA_JV_RFCOMM_SRV_OPEN_EVT:
575 BTA_JvSetPmProfile(p_data->rfc_srv_open.handle,BTA_JV_PM_ALL,BTA_JV_CONN_OPEN);
576 new_user_data = (void *)(uintptr_t)on_srv_rfc_connect(&p_data->rfc_srv_open, (uintptr_t)user_data);
579 case BTA_JV_RFCOMM_CLOSE_EVT:
580 APPL_TRACE_DEBUG("BTA_JV_RFCOMM_CLOSE_EVT: user_data:%d", (uintptr_t)user_data);
581 on_rfc_close(&p_data->rfc_close, (uintptr_t)user_data);
584 case BTA_JV_RFCOMM_WRITE_EVT:
585 on_rfc_write_done(&p_data->rfc_write, (uintptr_t)user_data);
588 case BTA_JV_RFCOMM_CONG_EVT:
589 on_rfc_outgoing_congest(&p_data->rfc_cong, (uintptr_t)user_data);
592 case BTA_JV_RFCOMM_READ_EVT:
593 case BTA_JV_RFCOMM_DATA_IND_EVT:
598 LOG_ERROR("%s unhandled event %d, slot id: %zi", __func__, event, (uintptr_t)user_data);
601 return new_user_data;
604 static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
605 uint32_t id = (uintptr_t)user_data;
607 case BTA_JV_GET_SCN_EVT:
609 pthread_mutex_lock(&slot_lock);
610 rfc_slot_t* rs = find_rfc_slot_by_id(id);
611 int new_scn = p_data->scn;
613 if(rs && (new_scn != 0))
616 /* BTA_JvCreateRecordByUser will only create a record if a UUID is specified,
617 * else it just allocate a RFC channel and start the RFCOMM thread - needed
619 * layer to get a RFCOMM channel.
620 * If uuid is null the create_sdp_record() will be called from Java when it
621 * has received the RFCOMM and L2CAP channel numbers through the sockets.*/
623 // Send channel ID to java layer
624 if(!send_app_scn(rs)){
626 APPL_TRACE_DEBUG("send_app_scn() failed, close rs->id:%d", rs->id);
627 cleanup_rfc_slot(rs);
629 if(rs->is_service_uuid_valid == true) {
630 // We already have data for SDP record, create it (RFC-only profiles)
631 BTA_JvCreateRecordByUser((void *)rs->id);
633 APPL_TRACE_DEBUG("is_service_uuid_valid==false - don't set SDP-record, "
634 "just start the RFCOMM server", rs->id);
635 //now start the rfcomm server after sdp & channel # assigned
636 BTA_JvRfcommStartServer(rs->security, rs->role, rs->scn, MAX_RFC_SESSION,
637 rfcomm_cback, (void*)rs->id);
641 APPL_TRACE_ERROR("jv_dm_cback: Error: allocate channel %d, slot found:%p", rs->scn, rs);
642 cleanup_rfc_slot(rs);
644 pthread_mutex_unlock(&slot_lock);
647 case BTA_JV_GET_PSM_EVT:
649 APPL_TRACE_DEBUG("Received PSM: 0x%04x", p_data->psm);
650 on_l2cap_psm_assigned(id, p_data->psm);
653 case BTA_JV_CREATE_RECORD_EVT: {
654 pthread_mutex_lock(&slot_lock);
656 rfc_slot_t *slot = find_rfc_slot_by_id(id);
657 if (slot && create_server_sdp_record(slot)) {
658 // Start the rfcomm server after sdp & channel # assigned.
659 BTA_JvRfcommStartServer(slot->security, slot->role, slot->scn, MAX_RFC_SESSION, rfcomm_cback, (void *)(uintptr_t)slot->id);
661 APPL_TRACE_ERROR("jv_dm_cback: cannot start server, slot found:%p", slot);
662 cleanup_rfc_slot(slot);
665 pthread_mutex_unlock(&slot_lock);
669 case BTA_JV_DISCOVERY_COMP_EVT: {
670 pthread_mutex_lock(&slot_lock);
671 rfc_slot_t *slot = find_rfc_slot_by_id(id);
672 if (p_data->disc_comp.status == BTA_JV_SUCCESS && p_data->disc_comp.scn) {
673 if (slot && slot->f.doing_sdp_request) {
674 // Establish the connection if we successfully looked up a channel number to connect to.
675 if (BTA_JvRfcommConnect(slot->security, slot->role, p_data->disc_comp.scn, slot->addr.address, rfcomm_cback, (void *)(uintptr_t)slot->id) == BTA_JV_SUCCESS) {
676 slot->scn = p_data->disc_comp.scn;
677 slot->f.doing_sdp_request = false;
678 if (!send_app_scn(slot))
679 cleanup_rfc_slot(slot);
681 cleanup_rfc_slot(slot);
684 // TODO(sharvil): this is really a logic error and we should probably assert.
685 LOG_ERROR("%s SDP response returned but RFCOMM slot %d did not request SDP record.", __func__, id);
688 cleanup_rfc_slot(slot);
691 // Find the next slot that needs to perform an SDP request and service it.
692 slot = find_rfc_slot_by_pending_sdp();
696 memcpy(sdp_uuid.uu.uuid128, slot->service_uuid, sizeof(sdp_uuid.uu.uuid128));
697 BTA_JvStartDiscovery((uint8_t *)slot->addr.address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id);
698 slot->f.pending_sdp_request = false;
699 slot->f.doing_sdp_request = true;
702 pthread_mutex_unlock(&slot_lock);
707 APPL_TRACE_DEBUG("unhandled event:%d, slot id:%d", event, id);
719 static sent_status_t send_data_to_app(int fd, BT_HDR *p_buf) {
723 ssize_t sent = send(fd, p_buf->data + p_buf->offset, p_buf->len, MSG_DONTWAIT);
726 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
728 LOG_ERROR("%s error writing RFCOMM data back to app: %s", __func__, strerror(errno));
735 if (sent == p_buf->len)
738 p_buf->offset += sent;
743 static bool flush_incoming_que_on_wr_signal(rfc_slot_t *slot) {
744 while (!list_is_empty(slot->incoming_queue)) {
745 BT_HDR *p_buf = list_front(slot->incoming_queue);
746 switch (send_data_to_app(slot->fd, p_buf)) {
749 //monitor the fd to get callback when app is ready to receive data
750 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id);
754 list_remove(slot->incoming_queue, p_buf);
758 list_remove(slot->incoming_queue, p_buf);
763 //app is ready to receive data, tell stack to start the data flow
764 //fix me: need a jv flow control api to serialize the call in stack
765 APPL_TRACE_DEBUG("enable data flow, rfc_handle:0x%x, rfc_port_handle:0x%x, user_id:%d",
766 slot->rfc_handle, slot->rfc_port_handle, slot->id);
767 extern int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable);
768 PORT_FlowControl_MaxCredit(slot->rfc_port_handle, true);
772 void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) {
773 pthread_mutex_lock(&slot_lock);
775 rfc_slot_t *slot = find_rfc_slot_by_id(user_id);
779 bool need_close = false;
781 // Data available from app, tell stack we have outgoing data.
782 if (flags & SOCK_THREAD_FD_RD && !slot->f.server) {
783 if (slot->f.connected) {
784 // Make sure there's data pending in case the peer closed the socket.
786 if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl(slot->fd, FIONREAD, &size) == 0 && size))
787 //unlock before BTA_JvRfcommWrite to avoid deadlock on concurrnet multi rfcomm connectoins
788 //concurrnet multi rfcomm connectoins
789 pthread_mutex_unlock(&slot_lock);
790 BTA_JvRfcommWrite(slot->rfc_handle, slot->id);
792 LOG_ERROR("%s socket signaled for read while disconnected, slot: %d, channel: %d", __func__, slot->id, slot->scn);
797 if (flags & SOCK_THREAD_FD_WR) {
798 // App is ready to receive more data, tell stack to enable data flow.
799 if (!slot->f.connected || !flush_incoming_que_on_wr_signal(slot)) {
800 LOG_ERROR("%s socket signaled for write while disconnected (or write failure), slot: %d, channel: %d", __func__, slot->id, slot->scn);
805 if (need_close || (flags & SOCK_THREAD_FD_EXCEPTION)) {
806 // Clean up if there's no data pending.
808 if (need_close || ioctl(slot->fd, FIONREAD, &size) != 0 || !size)
809 cleanup_rfc_slot(slot);
813 pthread_mutex_unlock(&slot_lock);
816 int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) {
817 pthread_mutex_lock(&slot_lock);
820 uint32_t id = (uintptr_t)user_data;
821 rfc_slot_t *slot = find_rfc_slot_by_id(id);
825 if (list_is_empty(slot->incoming_queue)) {
826 switch (send_data_to_app(slot->fd, p_buf)) {
829 list_append(slot->incoming_queue, p_buf);
830 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id);
835 ret = 1; // Enable data flow.
840 cleanup_rfc_slot(slot);
844 list_append(slot->incoming_queue, p_buf);
848 pthread_mutex_unlock(&slot_lock);
849 return ret; // Return 0 to disable data flow.
852 int bta_co_rfc_data_outgoing_size(void *user_data, int *size) {
853 pthread_mutex_lock(&slot_lock);
855 uint32_t id = (uintptr_t)user_data;
858 rfc_slot_t *slot = find_rfc_slot_by_id(id);
862 if (ioctl(slot->fd, FIONREAD, size) == 0) {
865 LOG_ERROR("%s unable to determine bytes remaining to be read on fd %d: %s", __func__, slot->fd, strerror(errno));
866 cleanup_rfc_slot(slot);
870 pthread_mutex_unlock(&slot_lock);
874 int bta_co_rfc_data_outgoing(void *user_data, uint8_t *buf, uint16_t size) {
875 pthread_mutex_lock(&slot_lock);
877 uint32_t id = (uintptr_t)user_data;
879 rfc_slot_t *slot = find_rfc_slot_by_id(id);
883 int received = recv(slot->fd, buf, size, 0);
884 if(received == size) {
887 LOG_ERROR("%s error receiving RFCOMM data from app: %s", __func__, strerror(errno));
888 cleanup_rfc_slot(slot);
892 pthread_mutex_unlock(&slot_lock);