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"
23 #include <hardware/bluetooth.h>
24 #include <hardware/bt_sock.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
31 #include "bt_target.h"
32 #include "bta_jv_api.h"
33 #include "bta_jv_co.h"
34 #include "btif_common.h"
35 #include "btif_sock_sdp.h"
36 #include "btif_sock_thread.h"
37 #include "btif_sock_util.h"
38 #include "btif_util.h"
44 #include "osi/include/list.h"
45 #include "osi/include/osi.h"
46 #include "osi/include/log.h"
50 #define MAX_RFC_CHANNEL 30 // Maximum number of RFCOMM channels (1-30 inclusive).
51 #define MAX_RFC_SESSION 7 // Maximum number of devices we can have an RFCOMM connection with.
54 int outgoing_congest : 1;
55 int pending_sdp_request : 1;
56 int doing_sdp_request : 1;
64 uint32_t id; // Non-zero indicates a valid (in-use) slot.
66 int scn; // Server channel number
68 uint8_t service_uuid[16];
69 char service_name[256];
71 int app_fd; // Temporary storage for the half of the socketpair that's sent back to upper layers.
78 list_t *incoming_queue;
81 static rfc_slot_t rfc_slots[MAX_RFC_CHANNEL];
82 static uint32_t rfc_slot_id;
83 static volatile int pth = -1; // poll thread handle
84 static pthread_mutex_t slot_lock;
86 static rfc_slot_t *find_free_slot(void);
87 static void cleanup_rfc_slot(rfc_slot_t *rs);
88 static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data);
89 static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data);
90 static bool send_app_scn(const rfc_slot_t *rs);
92 static bool is_init_done(void) {
96 bt_status_t btsock_rfc_init(int poll_thread_handle) {
97 pth = poll_thread_handle;
99 memset(rfc_slots, 0, sizeof(rfc_slots));
100 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
101 rfc_slots[i].scn = -1;
102 rfc_slots[i].sdp_handle = 0;
103 rfc_slots[i].fd = INVALID_FD;
104 rfc_slots[i].app_fd = INVALID_FD;
105 rfc_slots[i].incoming_queue = list_new(GKI_freebuf);
106 assert(rfc_slots[i].incoming_queue != NULL);
109 BTA_JvEnable(jv_dm_cback);
110 init_slot_lock(&slot_lock);
112 return BT_STATUS_SUCCESS;
115 void btsock_rfc_cleanup(void) {
118 lock_slot(&slot_lock);
119 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
121 cleanup_rfc_slot(&rfc_slots[i]);
122 list_free(rfc_slots[i].incoming_queue);
124 unlock_slot(&slot_lock);
127 static rfc_slot_t *find_free_slot(void) {
128 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
129 if (rfc_slots[i].fd == INVALID_FD)
130 return &rfc_slots[i];
134 static rfc_slot_t *find_rfc_slot_by_id(uint32_t id) {
137 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
138 if (rfc_slots[i].id == id)
139 return &rfc_slots[i];
141 LOG_ERROR("%s unable to find RFCOMM slot id: %d", __func__, id);
145 static rfc_slot_t *find_rfc_slot_by_pending_sdp(void) {
146 uint32_t min_id = UINT32_MAX;
148 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
149 if (rfc_slots[i].id && rfc_slots[i].f.pending_sdp_request && rfc_slots[i].id < min_id) {
150 min_id = rfc_slots[i].id;
154 return (slot == -1) ? NULL : &rfc_slots[slot];
157 static bool is_requesting_sdp(void) {
158 for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i)
159 if (rfc_slots[i].id && rfc_slots[i].f.doing_sdp_request)
164 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) {
166 if(flags & BTSOCK_FLAG_ENCRYPT)
167 security |= server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT;
168 if(flags & BTSOCK_FLAG_AUTH)
169 security |= server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE;
171 rfc_slot_t *slot = find_free_slot();
173 LOG_ERROR("%s unable to find free RFCOMM slot.", __func__);
177 int fds[2] = { INVALID_FD, INVALID_FD };
178 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) == -1) {
179 LOG_ERROR("%s error creating socketpair: %s", __func__, strerror(errno));
183 // Increment slot id and make sure we don't use id=0.
184 if (++rfc_slot_id == 0)
188 slot->app_fd = fds[1];
189 slot->security = security;
193 memcpy(slot->service_uuid, uuid, sizeof(slot->service_uuid));
195 memset(&slot->service_uuid, 0, sizeof(slot->service_uuid));
198 strlcpy(slot->service_name, name, sizeof(slot->service_name));
203 slot->id = rfc_slot_id;
204 slot->f.server = server;
209 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) {
210 rfc_slot_t *accept_rs = alloc_rfc_slot(addr, srv_rs->service_name, srv_rs->service_uuid, srv_rs->scn, 0, false);
212 LOG_ERROR("%s unable to allocate RFCOMM slot.", __func__);
216 accept_rs->f.server = false;
217 accept_rs->f.connected = true;
218 accept_rs->security = srv_rs->security;
219 accept_rs->mtu = srv_rs->mtu;
220 accept_rs->role = srv_rs->role;
221 accept_rs->rfc_handle = open_handle;
222 accept_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(open_handle);
224 srv_rs->rfc_handle = new_listen_handle;
225 srv_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(new_listen_handle);
227 assert(accept_rs->rfc_port_handle != srv_rs->rfc_port_handle);
229 // now swap the slot id
230 uint32_t new_listen_id = accept_rs->id;
231 accept_rs->id = srv_rs->id;
232 srv_rs->id = new_listen_id;
237 bt_status_t btsock_rfc_listen(const char *service_name, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) {
238 assert(sock_fd != NULL);
239 assert(service_uuid != NULL || (channel >= 1 && channel <= MAX_RFC_CHANNEL));
241 *sock_fd = INVALID_FD;
243 // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call
244 // functions on RFCOMM sockets before initializing the module. Probably should be an assert.
246 return BT_STATUS_NOT_READY;
248 if (!is_uuid_empty(service_uuid)) {
249 // Use a pre-defined channel # if the UUID is reserved.
250 int reserved_channel = get_reserved_rfc_channel(service_uuid);
251 if (reserved_channel != -1)
252 channel = reserved_channel;
254 service_uuid = UUID_SPP; // Use serial port profile to listen to specified channel
257 int status = BT_STATUS_FAIL;
258 lock_slot(&slot_lock);
260 rfc_slot_t *slot = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true);
262 LOG_ERROR("%s unable to allocate RFCOMM slot.", __func__);
266 BTA_JvCreateRecordByUser((void *)(uintptr_t)slot->id);
267 *sock_fd = slot->app_fd; // Transfer ownership of fd to caller.
268 slot->app_fd = INVALID_FD; // Drop our reference to the fd.
269 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, slot->id);
271 status = BT_STATUS_SUCCESS;
274 unlock_slot(&slot_lock);
278 bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t *service_uuid, int channel, int *sock_fd, int flags) {
279 assert(sock_fd != NULL);
280 assert(service_uuid != NULL || (channel >= 1 && channel <= MAX_RFC_CHANNEL));
282 *sock_fd = INVALID_FD;
284 // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call
285 // functions on RFCOMM sockets before initializing the module. Probably should be an assert.
287 return BT_STATUS_NOT_READY;
289 int status = BT_STATUS_FAIL;
290 lock_slot(&slot_lock);
292 rfc_slot_t *slot = alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false);
294 LOG_ERROR("%s unable to allocate RFCOMM slot.", __func__);
298 if (is_uuid_empty(service_uuid)) {
299 tBTA_JV_STATUS ret = BTA_JvRfcommConnect(slot->security, slot->role, slot->scn, slot->addr.address, rfcomm_cback, (void *)(uintptr_t)slot->id);
300 if (ret != BTA_JV_SUCCESS) {
301 LOG_ERROR("%s unable to initiate RFCOMM connection: %d", __func__, ret);
302 cleanup_rfc_slot(slot);
306 if (!send_app_scn(slot)) {
307 LOG_ERROR("%s unable to send channel number.", __func__);
308 cleanup_rfc_slot(slot);
314 memcpy(sdp_uuid.uu.uuid128, service_uuid, sizeof(sdp_uuid.uu.uuid128));
316 if (!is_requesting_sdp()) {
317 BTA_JvStartDiscovery((uint8_t *)bd_addr->address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id);
318 slot->f.pending_sdp_request = false;
319 slot->f.doing_sdp_request = true;
321 slot->f.pending_sdp_request = true;
322 slot->f.doing_sdp_request = false;
326 *sock_fd = slot->app_fd; // Transfer ownership of fd to caller.
327 slot->app_fd = INVALID_FD; // Drop our reference to the fd.
328 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
329 status = BT_STATUS_SUCCESS;
332 unlock_slot(&slot_lock);
336 static int create_server_sdp_record(rfc_slot_t *slot) {
338 if (!BTM_TryAllocateSCN(slot->scn)) {
339 LOG_ERROR("%s attempting to allocate fixed channel %d which is already in use.", __func__, slot->scn);
342 } else if ((slot->scn = BTM_AllocateSCN()) == 0) {
343 LOG_ERROR("%s unable to allocate RFCOMM server channel.", __func__);
347 slot->sdp_handle = add_rfc_sdp_rec(slot->service_name, slot->service_uuid, slot->scn);
348 return (slot->sdp_handle > 0);
351 static void free_rfc_slot_scn(rfc_slot_t *slot) {
355 if(slot->f.server && !slot->f.closing && slot->rfc_handle) {
356 BTA_JvRfcommStopServer(slot->rfc_handle, (void *)(uintptr_t)slot->id);
357 slot->rfc_handle = 0;
361 BTM_FreeSCN(slot->scn);
365 static void cleanup_rfc_slot(rfc_slot_t *slot) {
366 if (slot->fd != INVALID_FD) {
367 shutdown(slot->fd, SHUT_RDWR);
369 slot->fd = INVALID_FD;
372 if (slot->app_fd != INVALID_FD) {
374 slot->app_fd = INVALID_FD;
377 if (slot->sdp_handle > 0) {
378 del_rfc_sdp_rec(slot->sdp_handle);
379 slot->sdp_handle = 0;
382 if (slot->rfc_handle && !slot->f.closing && !slot->f.server) {
383 BTA_JvRfcommClose(slot->rfc_handle, (void *)(uintptr_t)slot->id);
384 slot->rfc_handle = 0;
387 free_rfc_slot_scn(slot);
388 list_clear(slot->incoming_queue);
390 slot->rfc_port_handle = 0;
391 memset(&slot->f, 0, sizeof(slot->f));
395 static bool send_app_scn(const rfc_slot_t *slot) {
396 return sock_send_all(slot->fd, (const uint8_t*)&slot->scn, sizeof(slot->scn)) == sizeof(slot->scn);
399 static bool send_app_connect_signal(int fd, const bt_bdaddr_t* addr, int channel, int status, int send_fd) {
400 sock_connect_signal_t cs;
401 cs.size = sizeof(cs);
403 cs.channel = channel;
406 if (send_fd == INVALID_FD)
407 return sock_send_all(fd, (const uint8_t *)&cs, sizeof(cs)) == sizeof(cs);
409 return sock_send_fd(fd, (const uint8_t *)&cs, sizeof(cs), send_fd) == sizeof(cs);
412 static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT *p_init, uint32_t id) {
413 lock_slot(&slot_lock);
415 rfc_slot_t *slot = find_rfc_slot_by_id(id);
419 if (p_init->status == BTA_JV_SUCCESS)
420 slot->rfc_handle = p_init->handle;
422 cleanup_rfc_slot(slot);
425 unlock_slot(&slot_lock);
428 static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START *p_start, uint32_t id) {
429 lock_slot(&slot_lock);
431 rfc_slot_t *slot = find_rfc_slot_by_id(id);
435 if (p_start->status == BTA_JV_SUCCESS) {
436 slot->rfc_handle = p_start->handle;
438 if (!send_app_scn(slot)) {
439 LOG_ERROR("%s unable to send server channel number for slot %d.", __func__, slot->id);
440 cleanup_rfc_slot(slot);
443 cleanup_rfc_slot(slot);
446 unlock_slot(&slot_lock);
449 static uint32_t on_srv_rfc_connect(tBTA_JV_RFCOMM_SRV_OPEN *p_open, uint32_t id) {
450 uint32_t new_listen_slot_id = 0;
451 lock_slot(&slot_lock);
453 rfc_slot_t *srv_rs = find_rfc_slot_by_id(id);
457 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);
461 // Start monitoring the socket.
462 btsock_thread_add_fd(pth, srv_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, srv_rs->id);
463 btsock_thread_add_fd(pth, accept_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, accept_rs->id);
464 send_app_connect_signal(srv_rs->fd, &accept_rs->addr, srv_rs->scn, 0, accept_rs->app_fd);
465 accept_rs->app_fd = INVALID_FD; // Ownership of the application fd has been transferred.
466 new_listen_slot_id = srv_rs->id;
469 unlock_slot(&slot_lock);
470 return new_listen_slot_id;
473 static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN *p_open, uint32_t id) {
474 lock_slot(&slot_lock);
476 rfc_slot_t *slot = find_rfc_slot_by_id(id);
480 if (p_open->status != BTA_JV_SUCCESS) {
481 cleanup_rfc_slot(slot);
485 slot->rfc_port_handle = BTA_JvRfcommGetPortHdl(p_open->handle);
486 memcpy(slot->addr.address, p_open->rem_bda, 6);
488 if (send_app_connect_signal(slot->fd, &slot->addr, slot->scn, 0, -1))
489 slot->f.connected = true;
491 LOG_ERROR("%s unable to send connect completion signal to caller.", __func__);
494 unlock_slot(&slot_lock);
497 static void on_rfc_close(UNUSED_ATTR tBTA_JV_RFCOMM_CLOSE *p_close, uint32_t id) {
498 lock_slot(&slot_lock);
500 // rfc_handle already closed when receiving rfcomm close event from stack.
501 rfc_slot_t *slot = find_rfc_slot_by_id(id);
503 cleanup_rfc_slot(slot);
505 unlock_slot(&slot_lock);
508 static void on_rfc_write_done(UNUSED_ATTR tBTA_JV_RFCOMM_WRITE *p, uint32_t id) {
509 lock_slot(&slot_lock);
511 rfc_slot_t *slot = find_rfc_slot_by_id(id);
512 if (slot && !slot->f.outgoing_congest)
513 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
515 unlock_slot(&slot_lock);
518 static void on_rfc_outgoing_congest(tBTA_JV_RFCOMM_CONG *p, uint32_t id) {
519 lock_slot(&slot_lock);
521 rfc_slot_t *slot = find_rfc_slot_by_id(id);
523 slot->f.outgoing_congest = p->cong ? 1 : 0;
524 if (!slot->f.outgoing_congest)
525 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
528 unlock_slot(&slot_lock);
531 static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
532 void *new_user_data = NULL;
535 case BTA_JV_RFCOMM_START_EVT:
536 on_srv_rfc_listen_started(&p_data->rfc_start, (uintptr_t)user_data);
539 case BTA_JV_RFCOMM_CL_INIT_EVT:
540 on_cl_rfc_init(&p_data->rfc_cl_init, (uintptr_t)user_data);
543 case BTA_JV_RFCOMM_OPEN_EVT:
544 BTA_JvSetPmProfile(p_data->rfc_open.handle,BTA_JV_PM_ID_1,BTA_JV_CONN_OPEN);
545 on_cli_rfc_connect(&p_data->rfc_open, (uintptr_t)user_data);
548 case BTA_JV_RFCOMM_SRV_OPEN_EVT:
549 BTA_JvSetPmProfile(p_data->rfc_srv_open.handle,BTA_JV_PM_ALL,BTA_JV_CONN_OPEN);
550 new_user_data = (void *)(uintptr_t)on_srv_rfc_connect(&p_data->rfc_srv_open, (uintptr_t)user_data);
553 case BTA_JV_RFCOMM_CLOSE_EVT:
554 APPL_TRACE_DEBUG("BTA_JV_RFCOMM_CLOSE_EVT: user_data:%d", (uintptr_t)user_data);
555 on_rfc_close(&p_data->rfc_close, (uintptr_t)user_data);
558 case BTA_JV_RFCOMM_WRITE_EVT:
559 on_rfc_write_done(&p_data->rfc_write, (uintptr_t)user_data);
562 case BTA_JV_RFCOMM_CONG_EVT:
563 on_rfc_outgoing_congest(&p_data->rfc_cong, (uintptr_t)user_data);
566 case BTA_JV_RFCOMM_READ_EVT:
567 case BTA_JV_RFCOMM_DATA_IND_EVT:
572 LOG_ERROR("%s unhandled event %d, slot id: %d", __func__, event, (uintptr_t)user_data);
575 return new_user_data;
578 static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
579 uint32_t id = (uintptr_t)user_data;
581 case BTA_JV_CREATE_RECORD_EVT: {
582 lock_slot(&slot_lock);
584 rfc_slot_t *slot = find_rfc_slot_by_id(id);
585 if (slot && create_server_sdp_record(slot)) {
586 // Start the rfcomm server after sdp & channel # assigned.
587 BTA_JvRfcommStartServer(slot->security, slot->role, slot->scn, MAX_RFC_SESSION, rfcomm_cback, (void *)(uintptr_t)slot->id);
589 APPL_TRACE_ERROR("jv_dm_cback: cannot start server, slot found:%p", slot);
590 cleanup_rfc_slot(slot);
593 unlock_slot(&slot_lock);
597 case BTA_JV_DISCOVERY_COMP_EVT: {
598 lock_slot(&slot_lock);
599 rfc_slot_t *slot = find_rfc_slot_by_id(id);
600 if (p_data->disc_comp.status == BTA_JV_SUCCESS && p_data->disc_comp.scn) {
601 if (slot && slot->f.doing_sdp_request) {
602 // Establish the connection if we successfully looked up a channel number to connect to.
603 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) {
604 slot->scn = p_data->disc_comp.scn;
605 slot->f.doing_sdp_request = false;
606 if (!send_app_scn(slot))
607 cleanup_rfc_slot(slot);
609 cleanup_rfc_slot(slot);
612 // TODO(sharvil): this is really a logic error and we should probably assert.
613 LOG_ERROR("%s SDP response returned but RFCOMM slot %d did not request SDP record.", __func__, id);
616 cleanup_rfc_slot(slot);
619 // Find the next slot that needs to perform an SDP request and service it.
620 slot = find_rfc_slot_by_pending_sdp();
624 memcpy(sdp_uuid.uu.uuid128, slot->service_uuid, sizeof(sdp_uuid.uu.uuid128));
625 BTA_JvStartDiscovery((uint8_t *)slot->addr.address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id);
626 slot->f.pending_sdp_request = false;
627 slot->f.doing_sdp_request = true;
630 unlock_slot(&slot_lock);
635 APPL_TRACE_DEBUG("unhandled event:%d, slot id:%d", event, id);
647 static sent_status_t send_data_to_app(int fd, BT_HDR *p_buf) {
651 ssize_t sent = send(fd, p_buf->data + p_buf->offset, p_buf->len, MSG_DONTWAIT);
654 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
656 LOG_ERROR("%s error writing RFCOMM data back to app: %s", __func__, strerror(errno));
663 if (sent == p_buf->len)
666 p_buf->offset += sent;
671 static bool flush_incoming_que_on_wr_signal(rfc_slot_t *slot) {
672 while (!list_is_empty(slot->incoming_queue)) {
673 BT_HDR *p_buf = list_front(slot->incoming_queue);
674 switch (send_data_to_app(slot->fd, p_buf)) {
677 //monitor the fd to get callback when app is ready to receive data
678 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id);
682 list_remove(slot->incoming_queue, p_buf);
686 list_remove(slot->incoming_queue, p_buf);
691 //app is ready to receive data, tell stack to start the data flow
692 //fix me: need a jv flow control api to serialize the call in stack
693 APPL_TRACE_DEBUG("enable data flow, rfc_handle:0x%x, rfc_port_handle:0x%x, user_id:%d",
694 slot->rfc_handle, slot->rfc_port_handle, slot->id);
695 extern int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable);
696 PORT_FlowControl_MaxCredit(slot->rfc_port_handle, true);
700 void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) {
701 lock_slot(&slot_lock);
703 rfc_slot_t *slot = find_rfc_slot_by_id(user_id);
707 bool need_close = false;
709 // Data available from app, tell stack we have outgoing data.
710 if (flags & SOCK_THREAD_FD_RD && !slot->f.server) {
711 if (slot->f.connected) {
712 // Make sure there's data pending in case the peer closed the socket.
714 if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl(slot->fd, FIONREAD, &size) == 0 && size))
715 BTA_JvRfcommWrite(slot->rfc_handle, slot->id);
717 LOG_ERROR("%s socket signaled for read while disconnected, slot: %d, channel: %d", __func__, slot->id, slot->scn);
722 if (flags & SOCK_THREAD_FD_WR) {
723 // App is ready to receive more data, tell stack to enable data flow.
724 if (!slot->f.connected || !flush_incoming_que_on_wr_signal(slot)) {
725 LOG_ERROR("%s socket signaled for write while disconnected (or write failure), slot: %d, channel: %d", __func__, slot->id, slot->scn);
730 if (need_close || (flags & SOCK_THREAD_FD_EXCEPTION)) {
731 // Clean up if there's no data pending.
733 if (need_close || ioctl(slot->fd, FIONREAD, &size) != 0 || !size)
734 cleanup_rfc_slot(slot);
738 unlock_slot(&slot_lock);
741 int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) {
742 lock_slot(&slot_lock);
745 uint32_t id = (uintptr_t)user_data;
746 rfc_slot_t *slot = find_rfc_slot_by_id(id);
750 if (list_is_empty(slot->incoming_queue)) {
751 switch (send_data_to_app(slot->fd, p_buf)) {
754 list_append(slot->incoming_queue, p_buf);
755 btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id);
760 ret = 1; // Enable data flow.
765 cleanup_rfc_slot(slot);
769 list_append(slot->incoming_queue, p_buf);
773 unlock_slot(&slot_lock);
774 return ret; // Return 0 to disable data flow.
777 int bta_co_rfc_data_outgoing_size(void *user_data, int *size) {
778 lock_slot(&slot_lock);
780 uint32_t id = (uintptr_t)user_data;
783 rfc_slot_t *slot = find_rfc_slot_by_id(id);
787 if (ioctl(slot->fd, FIONREAD, size) == 0) {
790 LOG_ERROR("%s unable to determine bytes remaining to be read on fd %d: %s", __func__, slot->fd, strerror(errno));
791 cleanup_rfc_slot(slot);
795 unlock_slot(&slot_lock);
799 int bta_co_rfc_data_outgoing(void *user_data, uint8_t *buf, uint16_t size) {
800 lock_slot(&slot_lock);
802 uint32_t id = (uintptr_t)user_data;
804 rfc_slot_t *slot = find_rfc_slot_by_id(id);
808 int received = recv(slot->fd, buf, size, 0);
809 if(received == size) {
812 LOG_ERROR("%s error receiving RFCOMM data from app: %s", __func__, strerror(errno));
813 cleanup_rfc_slot(slot);
817 unlock_slot(&slot_lock);