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"
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
31 #include <hardware/bluetooth.h>
32 #include <hardware/bt_sock.h>
34 #include "bt_target.h"
36 #include "bta_jv_api.h"
37 #include "bta_jv_co.h"
38 #include "btif_common.h"
39 #include "btif_sock_sdp.h"
40 #include "btif_sock_thread.h"
41 #include "btif_sock_util.h"
42 #include "btif_util.h"
48 #include "osi/include/compat.h"
49 #include "osi/include/list.h"
50 #include "osi/include/log.h"
51 #include "osi/include/osi.h"
55 /* The JV interface can have only one user, hence we need to call a few
56 * L2CAP functions from this file. */
57 #include "btif_sock_l2cap.h"
59 #define MAX_RFC_CHANNEL 30 // Maximum number of RFCOMM channels (1-30 inclusive).
60 #define MAX_RFC_SESSION 7 // Maximum number of devices we can have an RFCOMM connection with.
63 int outgoing_congest : 1;
64 int pending_sdp_request : 1;
65 int doing_sdp_request : 1;
73 uint32_t id; // Non-zero indicates a valid (in-use) slot.
75 int scn; // Server channel number
78 int is_service_uuid_valid;
79 uint8_t service_uuid[16];
80 char service_name[256];
82 int app_fd; // Temporary storage for the half of the socketpair that's sent back to upper layers.
89 list_t *incoming_queue;
92 static rfc_slot_t rfc_slots[MAX_RFC_CHANNEL];
93 static uint32_t rfc_slot_id;
94 static volatile int pth = -1; // poll thread handle
95 static pthread_mutex_t slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
97 static rfc_slot_t *find_free_slot(void);
98 static void cleanup_rfc_slot(rfc_slot_t *rs);
99 static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data);
100 static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data);
101 static bool send_app_scn(rfc_slot_t *rs);
103 static bool is_init_done(void) {
107 bt_status_t btsock_rfc_init(int poll_thread_handle) {
108 pth = poll_thread_handle;
110 memset(rfc_slots, 0, sizeof(rfc_slots));
111 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
112 rfc_slots[i].scn = -1;
113 rfc_slots[i].sdp_handle = 0;
114 rfc_slots[i].fd = INVALID_FD;
115 rfc_slots[i].app_fd = INVALID_FD;
116 rfc_slots[i].incoming_queue = list_new(GKI_freebuf);
117 assert(rfc_slots[i].incoming_queue != NULL);
120 BTA_JvEnable(jv_dm_cback);
122 return BT_STATUS_SUCCESS;
125 void btsock_rfc_cleanup(void) {
128 pthread_mutex_lock(&slot_lock);
129 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
131 cleanup_rfc_slot(&rfc_slots[i]);
132 list_free(rfc_slots[i].incoming_queue);
134 pthread_mutex_unlock(&slot_lock);
137 static rfc_slot_t *find_free_slot(void) {
138 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
139 if (rfc_slots[i].fd == INVALID_FD)
140 return &rfc_slots[i];
144 static rfc_slot_t *find_rfc_slot_by_id(uint32_t id) {
147 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
148 if (rfc_slots[i].id == id)
149 return &rfc_slots[i];
151 LOG_ERROR(LOG_TAG, "%s unable to find RFCOMM slot id: %d", __func__, id);
155 static rfc_slot_t *find_rfc_slot_by_pending_sdp(void) {
156 uint32_t min_id = UINT32_MAX;
158 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
159 if (rfc_slots[i].id && rfc_slots[i].f.pending_sdp_request && rfc_slots[i].id < min_id) {
160 min_id = rfc_slots[i].id;
164 return (slot == -1) ? NULL : &rfc_slots[slot];
167 static bool is_requesting_sdp(void) {
168 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
169 if (rfc_slots[i].id && rfc_slots[i].f.doing_sdp_request)
174 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) {
176 if(flags & BTSOCK_FLAG_ENCRYPT)
177 security |= server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT;
178 if(flags & BTSOCK_FLAG_AUTH)
179 security |= server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE;
180 if(flags & BTSOCK_FLAG_AUTH_MITM)
181 security |= server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM;
182 if(flags & BTSOCK_FLAG_AUTH_16_DIGIT)
183 security |= BTM_SEC_IN_MIN_16_DIGIT_PIN;
185 rfc_slot_t *slot = find_free_slot();
187 LOG_ERROR(LOG_TAG, "%s unable to find free RFCOMM slot.", __func__);
191 int fds[2] = { INVALID_FD, INVALID_FD };
192 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) == -1) {
193 LOG_ERROR(LOG_TAG, "%s error creating socketpair: %s", __func__, strerror(errno));
197 // Increment slot id and make sure we don't use id=0.
198 if (++rfc_slot_id == 0)
202 slot->app_fd = fds[1];
203 slot->security = security;
206 if(!is_uuid_empty(uuid)) {
207 memcpy(slot->service_uuid, uuid, sizeof(slot->service_uuid));
208 slot->is_service_uuid_valid = true;
210 memset(slot->service_uuid, 0, sizeof(slot->service_uuid));
211 slot->is_service_uuid_valid = false;
214 strlcpy(slot->service_name, name, sizeof(slot->service_name));
216 memset(slot->service_name, 0, sizeof(slot->service_name));
221 slot->id = rfc_slot_id;
222 slot->f.server = server;
227 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) {
228 rfc_slot_t *accept_rs = alloc_rfc_slot(addr, srv_rs->service_name, srv_rs->service_uuid, srv_rs->scn, 0, false);
230 LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__);
234 accept_rs->f.server = false;
235 accept_rs->f.connected = true;
236 accept_rs->security = srv_rs->security;
237 accept_rs->mtu = srv_rs->mtu;
238 accept_rs->role = srv_rs->role;
239 accept_rs->rfc_handle = open_handle;
240 accept_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(open_handle);
242 srv_rs->rfc_handle = new_listen_handle;
243 srv_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(new_listen_handle);
245 assert(accept_rs->rfc_port_handle != srv_rs->rfc_port_handle);
247 // now swap the slot id
248 uint32_t new_listen_id = accept_rs->id;
249 accept_rs->id = srv_rs->id;
250 srv_rs->id = new_listen_id;
255 bt_status_t btsock_rfc_listen(const char *service_name, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) {
256 assert(sock_fd != NULL);
257 assert((service_uuid != NULL)
258 || (channel >= 1 && channel <= MAX_RFC_CHANNEL)
259 || ((flags & BTSOCK_FLAG_NO_SDP) != 0));
261 *sock_fd = INVALID_FD;
263 // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call
264 // functions on RFCOMM sockets before initializing the module. Probably should be an assert.
266 return BT_STATUS_NOT_READY;
268 if((flags & BTSOCK_FLAG_NO_SDP) == 0) {
269 if(is_uuid_empty(service_uuid)) {
270 APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_uuid not set AND "
271 "BTSOCK_FLAG_NO_SDP is not set - changing to SPP");
272 service_uuid = UUID_SPP; // Use serial port profile to listen to specified channel
274 //Check the service_uuid. overwrite the channel # if reserved
275 int reserved_channel = get_reserved_rfc_channel(service_uuid);
276 if (reserved_channel > 0) {
277 channel = reserved_channel;
282 int status = BT_STATUS_FAIL;
283 pthread_mutex_lock(&slot_lock);
285 rfc_slot_t *slot = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true);
287 LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__);
290 APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_name: %s - channel: %d", service_name, channel);
291 BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, UINT_TO_PTR(slot->id), channel);
292 *sock_fd = slot->app_fd; // Transfer ownership of fd to caller.
294 * We are leaking one of the app_fd's - either the listen socket, or the connection socket.
295 * WE need to close this in native, as the FD might belong to another process
296 - This is the server socket FD
297 - For accepted connections, we close the FD after passing it to JAVA.
298 - Try to simply remove the = -1 to free the FD at rs cleanup.*/
299 // close(rs->app_fd);
300 slot->app_fd = INVALID_FD; // Drop our reference to the fd.
301 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, slot->id);
303 status = BT_STATUS_SUCCESS;
306 pthread_mutex_unlock(&slot_lock);
310 bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) {
311 assert(sock_fd != NULL);
312 assert(service_uuid != NULL || (channel >= 1 && channel <= MAX_RFC_CHANNEL));
314 *sock_fd = INVALID_FD;
316 // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call
317 // functions on RFCOMM sockets before initializing the module. Probably should be an assert.
319 return BT_STATUS_NOT_READY;
321 int status = BT_STATUS_FAIL;
322 pthread_mutex_lock(&slot_lock);
324 rfc_slot_t *slot = alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false);
326 LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__);
330 if (is_uuid_empty(service_uuid)) {
331 tBTA_JV_STATUS ret = BTA_JvRfcommConnect(slot->security, slot->role, slot->scn, slot->addr.address, rfcomm_cback, (void *)(uintptr_t)slot->id);
332 if (ret != BTA_JV_SUCCESS) {
333 LOG_ERROR(LOG_TAG, "%s unable to initiate RFCOMM connection: %d", __func__, ret);
334 cleanup_rfc_slot(slot);
338 if (!send_app_scn(slot)) {
339 LOG_ERROR(LOG_TAG, "%s unable to send channel number.", __func__);
340 cleanup_rfc_slot(slot);
346 memcpy(sdp_uuid.uu.uuid128, service_uuid, sizeof(sdp_uuid.uu.uuid128));
348 if (!is_requesting_sdp()) {
349 BTA_JvStartDiscovery((uint8_t *)bd_addr->address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id);
350 slot->f.pending_sdp_request = false;
351 slot->f.doing_sdp_request = true;
353 slot->f.pending_sdp_request = true;
354 slot->f.doing_sdp_request = false;
358 *sock_fd = slot->app_fd; // Transfer ownership of fd to caller.
359 slot->app_fd = INVALID_FD; // Drop our reference to the fd.
360 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
361 status = BT_STATUS_SUCCESS;
364 pthread_mutex_unlock(&slot_lock);
368 static int create_server_sdp_record(rfc_slot_t *slot) {
372 slot->sdp_handle = add_rfc_sdp_rec(slot->service_name, slot->service_uuid, slot->scn);
373 return (slot->sdp_handle > 0);
376 static void free_rfc_slot_scn(rfc_slot_t *slot) {
380 if(slot->f.server && !slot->f.closing && slot->rfc_handle) {
381 BTA_JvRfcommStopServer(slot->rfc_handle, (void *)(uintptr_t)slot->id);
382 slot->rfc_handle = 0;
386 BTM_FreeSCN(slot->scn);
390 static void cleanup_rfc_slot(rfc_slot_t *slot) {
391 if (slot->fd != INVALID_FD) {
392 shutdown(slot->fd, SHUT_RDWR);
394 slot->fd = INVALID_FD;
397 if (slot->app_fd != INVALID_FD) {
399 slot->app_fd = INVALID_FD;
402 if (slot->sdp_handle > 0) {
403 del_rfc_sdp_rec(slot->sdp_handle);
404 slot->sdp_handle = 0;
407 if (slot->rfc_handle && !slot->f.closing && !slot->f.server) {
408 BTA_JvRfcommClose(slot->rfc_handle, (void *)(uintptr_t)slot->id);
409 slot->rfc_handle = 0;
412 free_rfc_slot_scn(slot);
413 list_clear(slot->incoming_queue);
415 slot->rfc_port_handle = 0;
416 memset(&slot->f, 0, sizeof(slot->f));
418 slot->scn_notified = false;
421 static bool send_app_scn(rfc_slot_t *slot) {
422 if(slot->scn_notified == true) {
423 //already send, just return success.
426 slot->scn_notified = true;
427 return sock_send_all(slot->fd, (const uint8_t*)&slot->scn, sizeof(slot->scn)) == sizeof(slot->scn);
430 static bool send_app_connect_signal(int fd, const bt_bdaddr_t* addr, int channel, int status, int send_fd) {
431 sock_connect_signal_t cs;
432 cs.size = sizeof(cs);
434 cs.channel = channel;
436 cs.max_rx_packet_size = 0; // not used for RFCOMM
437 cs.max_tx_packet_size = 0; // not used for RFCOMM
438 if (send_fd == INVALID_FD)
439 return sock_send_all(fd, (const uint8_t *)&cs, sizeof(cs)) == sizeof(cs);
441 return sock_send_fd(fd, (const uint8_t *)&cs, sizeof(cs), send_fd) == sizeof(cs);
444 static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT *p_init, uint32_t id) {
445 pthread_mutex_lock(&slot_lock);
447 rfc_slot_t *slot = find_rfc_slot_by_id(id);
451 if (p_init->status == BTA_JV_SUCCESS)
452 slot->rfc_handle = p_init->handle;
454 cleanup_rfc_slot(slot);
457 pthread_mutex_unlock(&slot_lock);
460 static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START *p_start, uint32_t id) {
461 pthread_mutex_lock(&slot_lock);
463 rfc_slot_t *slot = find_rfc_slot_by_id(id);
467 if (p_start->status == BTA_JV_SUCCESS) {
468 slot->rfc_handle = p_start->handle;
470 cleanup_rfc_slot(slot);
473 pthread_mutex_unlock(&slot_lock);
476 static uint32_t on_srv_rfc_connect(tBTA_JV_RFCOMM_SRV_OPEN *p_open, uint32_t id) {
477 uint32_t new_listen_slot_id = 0;
478 pthread_mutex_lock(&slot_lock);
480 rfc_slot_t *srv_rs = find_rfc_slot_by_id(id);
484 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);
488 // Start monitoring the socket.
489 btsock_thread_add_fd(pth, srv_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, srv_rs->id);
490 btsock_thread_add_fd(pth, accept_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, accept_rs->id);
491 send_app_connect_signal(srv_rs->fd, &accept_rs->addr, srv_rs->scn, 0, accept_rs->app_fd);
492 accept_rs->app_fd = INVALID_FD; // Ownership of the application fd has been transferred.
493 new_listen_slot_id = srv_rs->id;
496 pthread_mutex_unlock(&slot_lock);
497 return new_listen_slot_id;
500 static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN *p_open, uint32_t id) {
501 pthread_mutex_lock(&slot_lock);
503 rfc_slot_t *slot = find_rfc_slot_by_id(id);
507 if (p_open->status != BTA_JV_SUCCESS) {
508 cleanup_rfc_slot(slot);
512 slot->rfc_port_handle = BTA_JvRfcommGetPortHdl(p_open->handle);
513 memcpy(slot->addr.address, p_open->rem_bda, 6);
515 if (send_app_connect_signal(slot->fd, &slot->addr, slot->scn, 0, -1))
516 slot->f.connected = true;
518 LOG_ERROR(LOG_TAG, "%s unable to send connect completion signal to caller.", __func__);
521 pthread_mutex_unlock(&slot_lock);
524 static void on_rfc_close(UNUSED_ATTR tBTA_JV_RFCOMM_CLOSE *p_close, uint32_t id) {
525 pthread_mutex_lock(&slot_lock);
527 // rfc_handle already closed when receiving rfcomm close event from stack.
528 rfc_slot_t *slot = find_rfc_slot_by_id(id);
530 cleanup_rfc_slot(slot);
532 pthread_mutex_unlock(&slot_lock);
535 static void on_rfc_write_done(UNUSED_ATTR tBTA_JV_RFCOMM_WRITE *p, uint32_t id) {
536 pthread_mutex_lock(&slot_lock);
538 rfc_slot_t *slot = find_rfc_slot_by_id(id);
539 if (slot && !slot->f.outgoing_congest)
540 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
542 pthread_mutex_unlock(&slot_lock);
545 static void on_rfc_outgoing_congest(tBTA_JV_RFCOMM_CONG *p, uint32_t id) {
546 pthread_mutex_lock(&slot_lock);
548 rfc_slot_t *slot = find_rfc_slot_by_id(id);
550 slot->f.outgoing_congest = p->cong ? 1 : 0;
551 if (!slot->f.outgoing_congest)
552 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
555 pthread_mutex_unlock(&slot_lock);
558 static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
559 void *new_user_data = NULL;
562 case BTA_JV_RFCOMM_START_EVT:
563 on_srv_rfc_listen_started(&p_data->rfc_start, (uintptr_t)user_data);
566 case BTA_JV_RFCOMM_CL_INIT_EVT:
567 on_cl_rfc_init(&p_data->rfc_cl_init, (uintptr_t)user_data);
570 case BTA_JV_RFCOMM_OPEN_EVT:
571 BTA_JvSetPmProfile(p_data->rfc_open.handle,BTA_JV_PM_ID_1,BTA_JV_CONN_OPEN);
572 on_cli_rfc_connect(&p_data->rfc_open, (uintptr_t)user_data);
575 case BTA_JV_RFCOMM_SRV_OPEN_EVT:
576 BTA_JvSetPmProfile(p_data->rfc_srv_open.handle,BTA_JV_PM_ALL,BTA_JV_CONN_OPEN);
577 new_user_data = (void *)(uintptr_t)on_srv_rfc_connect(&p_data->rfc_srv_open, (uintptr_t)user_data);
580 case BTA_JV_RFCOMM_CLOSE_EVT:
581 APPL_TRACE_DEBUG("BTA_JV_RFCOMM_CLOSE_EVT: user_data:%d", (uintptr_t)user_data);
582 on_rfc_close(&p_data->rfc_close, (uintptr_t)user_data);
585 case BTA_JV_RFCOMM_WRITE_EVT:
586 on_rfc_write_done(&p_data->rfc_write, (uintptr_t)user_data);
589 case BTA_JV_RFCOMM_CONG_EVT:
590 on_rfc_outgoing_congest(&p_data->rfc_cong, (uintptr_t)user_data);
593 case BTA_JV_RFCOMM_READ_EVT:
594 case BTA_JV_RFCOMM_DATA_IND_EVT:
599 LOG_ERROR(LOG_TAG, "%s unhandled event %d, slot id: %zi", __func__, event, (uintptr_t)user_data);
602 return new_user_data;
605 static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
606 uint32_t id = PTR_TO_UINT(user_data);
608 case BTA_JV_GET_SCN_EVT:
610 pthread_mutex_lock(&slot_lock);
611 rfc_slot_t* rs = find_rfc_slot_by_id(id);
612 int new_scn = p_data->scn;
614 if(rs && (new_scn != 0))
617 /* BTA_JvCreateRecordByUser will only create a record if a UUID is specified,
618 * else it just allocate a RFC channel and start the RFCOMM thread - needed
620 * layer to get a RFCOMM channel.
621 * If uuid is null the create_sdp_record() will be called from Java when it
622 * has received the RFCOMM and L2CAP channel numbers through the sockets.*/
624 // Send channel ID to java layer
625 if(!send_app_scn(rs)){
627 APPL_TRACE_DEBUG("send_app_scn() failed, close rs->id:%d", rs->id);
628 cleanup_rfc_slot(rs);
630 if(rs->is_service_uuid_valid == true) {
631 // We already have data for SDP record, create it (RFC-only profiles)
632 BTA_JvCreateRecordByUser(UINT_TO_PTR(rs->id));
634 APPL_TRACE_DEBUG("is_service_uuid_valid==false - don't set SDP-record, "
635 "just start the RFCOMM server", rs->id);
636 //now start the rfcomm server after sdp & channel # assigned
637 BTA_JvRfcommStartServer(rs->security, rs->role, rs->scn, MAX_RFC_SESSION,
638 rfcomm_cback, UINT_TO_PTR(rs->id));
642 APPL_TRACE_ERROR("jv_dm_cback: Error: allocate channel %d, slot found:%p", rs->scn, rs);
643 cleanup_rfc_slot(rs);
645 pthread_mutex_unlock(&slot_lock);
648 case BTA_JV_GET_PSM_EVT:
650 APPL_TRACE_DEBUG("Received PSM: 0x%04x", p_data->psm);
651 on_l2cap_psm_assigned(id, p_data->psm);
654 case BTA_JV_CREATE_RECORD_EVT: {
655 pthread_mutex_lock(&slot_lock);
657 rfc_slot_t *slot = find_rfc_slot_by_id(id);
658 if (slot && create_server_sdp_record(slot)) {
659 // Start the rfcomm server after sdp & channel # assigned.
660 BTA_JvRfcommStartServer(slot->security, slot->role, slot->scn, MAX_RFC_SESSION, rfcomm_cback, (void *)(uintptr_t)slot->id);
662 APPL_TRACE_ERROR("jv_dm_cback: cannot start server, slot found:%p", slot);
663 cleanup_rfc_slot(slot);
666 pthread_mutex_unlock(&slot_lock);
670 case BTA_JV_DISCOVERY_COMP_EVT: {
671 pthread_mutex_lock(&slot_lock);
672 rfc_slot_t *slot = find_rfc_slot_by_id(id);
673 if (p_data->disc_comp.status == BTA_JV_SUCCESS && p_data->disc_comp.scn) {
674 if (slot && slot->f.doing_sdp_request) {
675 // Establish the connection if we successfully looked up a channel number to connect to.
676 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) {
677 slot->scn = p_data->disc_comp.scn;
678 slot->f.doing_sdp_request = false;
679 if (!send_app_scn(slot))
680 cleanup_rfc_slot(slot);
682 cleanup_rfc_slot(slot);
685 // TODO(sharvil): this is really a logic error and we should probably assert.
686 LOG_ERROR(LOG_TAG, "%s SDP response returned but RFCOMM slot %d did not request SDP record.", __func__, id);
689 cleanup_rfc_slot(slot);
692 // Find the next slot that needs to perform an SDP request and service it.
693 slot = find_rfc_slot_by_pending_sdp();
697 memcpy(sdp_uuid.uu.uuid128, slot->service_uuid, sizeof(sdp_uuid.uu.uuid128));
698 BTA_JvStartDiscovery((uint8_t *)slot->addr.address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id);
699 slot->f.pending_sdp_request = false;
700 slot->f.doing_sdp_request = true;
703 pthread_mutex_unlock(&slot_lock);
708 APPL_TRACE_DEBUG("unhandled event:%d, slot id:%d", event, id);
720 static sent_status_t send_data_to_app(int fd, BT_HDR *p_buf) {
724 ssize_t sent = send(fd, p_buf->data + p_buf->offset, p_buf->len, MSG_DONTWAIT);
727 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
729 LOG_ERROR(LOG_TAG, "%s error writing RFCOMM data back to app: %s", __func__, strerror(errno));
736 if (sent == p_buf->len)
739 p_buf->offset += sent;
744 static bool flush_incoming_que_on_wr_signal(rfc_slot_t *slot) {
745 while (!list_is_empty(slot->incoming_queue)) {
746 BT_HDR *p_buf = list_front(slot->incoming_queue);
747 switch (send_data_to_app(slot->fd, p_buf)) {
750 //monitor the fd to get callback when app is ready to receive data
751 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id);
755 list_remove(slot->incoming_queue, p_buf);
759 list_remove(slot->incoming_queue, p_buf);
764 //app is ready to receive data, tell stack to start the data flow
765 //fix me: need a jv flow control api to serialize the call in stack
766 APPL_TRACE_DEBUG("enable data flow, rfc_handle:0x%x, rfc_port_handle:0x%x, user_id:%d",
767 slot->rfc_handle, slot->rfc_port_handle, slot->id);
768 extern int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable);
769 PORT_FlowControl_MaxCredit(slot->rfc_port_handle, true);
773 void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) {
774 pthread_mutex_lock(&slot_lock);
776 rfc_slot_t *slot = find_rfc_slot_by_id(user_id);
780 bool need_close = false;
782 // Data available from app, tell stack we have outgoing data.
783 if (flags & SOCK_THREAD_FD_RD && !slot->f.server) {
784 if (slot->f.connected) {
785 // Make sure there's data pending in case the peer closed the socket.
787 if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl(slot->fd, FIONREAD, &size) == 0 && size))
788 //unlock before BTA_JvRfcommWrite to avoid deadlock on concurrnet multi rfcomm connectoins
789 //concurrnet multi rfcomm connectoins
790 pthread_mutex_unlock(&slot_lock);
791 BTA_JvRfcommWrite(slot->rfc_handle, slot->id);
793 LOG_ERROR(LOG_TAG, "%s socket signaled for read while disconnected, slot: %d, channel: %d", __func__, slot->id, slot->scn);
798 if (flags & SOCK_THREAD_FD_WR) {
799 // App is ready to receive more data, tell stack to enable data flow.
800 if (!slot->f.connected || !flush_incoming_que_on_wr_signal(slot)) {
801 LOG_ERROR(LOG_TAG, "%s socket signaled for write while disconnected (or write failure), slot: %d, channel: %d", __func__, slot->id, slot->scn);
806 if (need_close || (flags & SOCK_THREAD_FD_EXCEPTION)) {
807 // Clean up if there's no data pending.
809 if (need_close || ioctl(slot->fd, FIONREAD, &size) != 0 || !size)
810 cleanup_rfc_slot(slot);
814 pthread_mutex_unlock(&slot_lock);
817 int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) {
818 pthread_mutex_lock(&slot_lock);
821 uint32_t id = (uintptr_t)user_data;
822 rfc_slot_t *slot = find_rfc_slot_by_id(id);
826 if (list_is_empty(slot->incoming_queue)) {
827 switch (send_data_to_app(slot->fd, p_buf)) {
830 list_append(slot->incoming_queue, p_buf);
831 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id);
836 ret = 1; // Enable data flow.
841 cleanup_rfc_slot(slot);
845 list_append(slot->incoming_queue, p_buf);
849 pthread_mutex_unlock(&slot_lock);
850 return ret; // Return 0 to disable data flow.
853 int bta_co_rfc_data_outgoing_size(void *user_data, int *size) {
854 pthread_mutex_lock(&slot_lock);
856 uint32_t id = (uintptr_t)user_data;
859 rfc_slot_t *slot = find_rfc_slot_by_id(id);
863 if (ioctl(slot->fd, FIONREAD, size) == 0) {
866 LOG_ERROR(LOG_TAG, "%s unable to determine bytes remaining to be read on fd %d: %s", __func__, slot->fd, strerror(errno));
867 cleanup_rfc_slot(slot);
871 pthread_mutex_unlock(&slot_lock);
875 int bta_co_rfc_data_outgoing(void *user_data, uint8_t *buf, uint16_t size) {
876 pthread_mutex_lock(&slot_lock);
878 uint32_t id = (uintptr_t)user_data;
880 rfc_slot_t *slot = find_rfc_slot_by_id(id);
884 int received = recv(slot->fd, buf, size, 0);
885 if(received == size) {
888 LOG_ERROR(LOG_TAG, "%s error receiving RFCOMM data from app: %s", __func__, strerror(errno));
889 cleanup_rfc_slot(slot);
893 pthread_mutex_unlock(&slot_lock);