Lines Matching full:sock
89 static void btsock_l2cap_server_listen(l2cap_socket* sock);
115 static char packet_get_head_l(l2cap_socket* sock, uint8_t** data,
117 struct packet* p = sock->first_packet;
121 if (data) *data = sock->first_packet->data;
122 if (len) *len = sock->first_packet->len;
123 sock->first_packet = p->next;
124 if (sock->first_packet)
125 sock->first_packet->prev = NULL;
127 sock->last_packet = NULL;
129 if (len) sock->bytes_buffered -= *len;
147 static char packet_put_head_l(l2cap_socket* sock, const void* data,
161 p->next = sock->first_packet;
162 sock->first_packet = p;
166 sock->last_packet = p;
168 sock->bytes_buffered += len;
174 static char packet_put_tail_l(l2cap_socket* sock, const void* data,
176 if (sock->bytes_buffered >= L2CAP_MAX_RX_BUFFER) {
183 p->prev = sock->last_packet;
184 sock->last_packet = p;
188 sock->first_packet = p;
190 sock->bytes_buffered += len;
202 l2cap_socket* sock = socks;
204 while (sock && sock->id != id) sock = sock->next;
206 return sock;
209 static void btsock_l2cap_free_l(l2cap_socket* sock) {
213 while (t && t != sock) t = t->next;
218 if (sock->next) sock->next->prev = sock->prev;
220 if (sock->prev)
221 sock->prev->next = sock->next;
223 socks = sock->next;
225 shutdown(sock->our_fd, SHUT_RDWR);
226 close(sock->our_fd);
227 if (sock->app_fd != -1) {
228 close(sock->app_fd);
230 LOG(ERROR) << "SOCK_LIST: free(id = " << sock->id << ") - NO app_fd!";
233 while (packet_get_head_l(sock, &buf, NULL)) osi_free(buf);
236 if (sock->is_le_coc) {
238 if (sock->handle >= 0 && (!sock->server)) {
239 BTA_JvL2capClose(sock->handle);
241 if ((sock->channel >= 0) && (sock->server)) {
242 BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP_LE);
246 if ((sock->handle >= 0) && (!sock->server)) {
247 if (sock->fixed_chan)
248 BTA_JvL2capCloseLE(sock->handle);
250 BTA_JvL2capClose(sock->handle);
252 if ((sock->channel >= 0) && (sock->server)) {
253 if (sock->fixed_chan)
254 BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP_LE);
256 BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP);
258 if (!sock->fixed_chan) {
260 << sock->channel;
261 BTA_JvL2capStopServer(sock->channel, sock->id);
266 DVLOG(2) << __func__ << ": free id:" << sock->id;
267 osi_free(sock);
275 l2cap_socket* sock = (l2cap_socket*)osi_calloc(sizeof(*sock));
291 sock->our_fd = fds[0];
292 sock->app_fd = fds[1];
293 sock->security = security;
294 sock->server = is_server;
295 sock->connected = false;
296 sock->handle = 0;
297 sock->server_psm_sent = false;
298 sock->app_uid = -1;
300 if (name) strncpy(sock->name, name, sizeof(sock->name) - 1);
301 if (addr) sock->addr = *addr;
303 sock->first_packet = NULL;
304 sock->last_packet = NULL;
306 sock->tx_mtu = L2CAP_LE_MIN_MTU;
308 sock->next = socks;
309 sock->prev = NULL;
310 if (socks) socks->prev = sock;
311 sock->id = (socks ? socks->id : 0) + 1;
312 socks = sock;
318 while (t && t->id != sock->id) {
321 if (!t && sock->id) /* non-zeor handle is unique -> we're done */
324 if (!++sock->id) /* no zero IDs allowed */
325 sock->id++;
327 DVLOG(2) << __func__ << " SOCK_LIST: alloc id:" << sock->id;
328 return sock;
331 osi_free(sock);
351 static inline bool send_app_psm_or_chan_l(l2cap_socket* sock) {
352 DVLOG(2) << __func__ << ": channel: " << sock->channel;
353 return sock_send_all(sock->our_fd, (const uint8_t*)&sock->channel,
354 sizeof(sock->channel)) == sizeof(sock->channel);
382 l2cap_socket* sock;
385 sock = btsock_l2cap_find_by_id_l(id);
386 if (!sock) return;
391 btsock_l2cap_free_l(sock);
395 sock->handle = p_start->handle;
396 DVLOG(2) << __func__ << ": sock->handle: " << sock->handle
397 << ", id: " << sock->id;
399 if (!sock->server_psm_sent) {
400 if (!send_app_psm_or_chan_l(sock)) {
402 DVLOG(2) << "send_app_psm() failed, close rs->id: " << sock->id;
403 btsock_l2cap_free_l(sock);
405 sock->server_psm_sent = true;
411 l2cap_socket* sock;
414 sock = btsock_l2cap_find_by_id_l(id);
415 if (!sock) return;
418 btsock_l2cap_free_l(sock);
422 sock->handle = p_init->handle;
426 * Here we allocate a new sock instance to mimic the BluetoothSocket. The socket
427 * will be a clone of the sock representing the BluetoothServerSocket.
430 l2cap_socket* sock) {
433 btsock_l2cap_alloc_l(sock->name, &p_open->rem_bda, false, 0);
435 accept_rs->security = sock->security;
436 accept_rs->fixed_chan = sock->fixed_chan;
437 accept_rs->channel = sock->channel;
438 accept_rs->handle = sock->handle;
439 accept_rs->app_uid = sock->app_uid;
440 sock->handle =
442 accept_rs->is_le_coc = sock->is_le_coc;
443 accept_rs->tx_mtu = sock->tx_mtu = p_open->tx_mtu;
448 accept_rs->id = sock->id;
449 sock->id = new_listen_id;
452 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP,
453 SOCK_THREAD_FD_EXCEPTION, sock->id);
458 DVLOG(2) << "server fd: << " << sock->our_fd << ", scn:" << sock->channel;
459 send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0,
460 accept_rs->app_fd, sock->rx_mtu, p_open->tx_mtu);
465 btsock_l2cap_server_listen(sock);
469 l2cap_socket* sock) {
472 btsock_l2cap_alloc_l(sock->name, &p_open->rem_bda, false, 0);
477 accept_rs->id = sock->id;
478 sock->id = new_listen_id;
482 accept_rs->security = sock->security;
483 accept_rs->fixed_chan = sock->fixed_chan;
484 accept_rs->channel = sock->channel;
485 accept_rs->app_uid = sock->app_uid;
486 accept_rs->tx_mtu = sock->tx_mtu = p_open->tx_mtu;
493 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP,
494 SOCK_THREAD_FD_EXCEPTION, sock->id);
499 DVLOG(2) << "server fd: << " << sock->our_fd << ", scn:" << sock->channel;
500 send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0,
501 accept_rs->app_fd, sock->rx_mtu, p_open->tx_mtu);
506 l2cap_socket* sock) {
507 sock->addr = p_open->rem_bda;
508 sock->tx_mtu = p_open->tx_mtu;
510 if (!send_app_psm_or_chan_l(sock)) {
515 if (!send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1,
516 sock->rx_mtu, p_open->tx_mtu)) {
522 DVLOG(2) << " connect signal sent, slot id: " << sock->id
523 << ", chan: " << sock->channel << ", server: " << sock->server;
524 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD,
525 sock->id);
526 sock->connected = true;
530 l2cap_socket* sock) {
531 sock->addr = p_open->rem_bda;
532 sock->tx_mtu = p_open->tx_mtu;
534 if (!send_app_psm_or_chan_l(sock)) {
539 if (!send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1,
540 sock->rx_mtu, p_open->tx_mtu)) {
546 DVLOG(2) << " connect signal sent, slot id: " << sock->id
547 << ", chan: " << sock->channel << ", server: " << sock->server;
548 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD,
549 sock->id);
550 sock->connected = true;
554 l2cap_socket* sock;
559 sock = btsock_l2cap_find_by_id_l(id);
560 if (!sock) {
565 sock->tx_mtu = le_open->tx_mtu;
566 if (sock->fixed_chan && le_open->status == BTA_JV_SUCCESS) {
567 if (!sock->server)
568 on_cl_l2cap_le_connect_l(le_open, sock);
570 on_srv_l2cap_le_connect_l(le_open, sock);
571 } else if (!sock->fixed_chan && psm_open->status == BTA_JV_SUCCESS) {
572 if (!sock->server)
573 on_cl_l2cap_psm_connect_l(psm_open, sock);
575 on_srv_l2cap_psm_connect_l(psm_open, sock);
577 btsock_l2cap_free_l(sock);
581 l2cap_socket* sock;
584 sock = btsock_l2cap_find_by_id_l(id);
585 if (!sock) return;
587 DVLOG(2) << __func__ << ": slot id: " << sock->id << ", fd: " << sock->our_fd
588 << (sock->fixed_chan ? ", fixed_chan:" : ", PSM: ") << sock->channel
589 << ", server:" << sock->server;
592 if (!sock->fixed_chan && (sock->server)) {
593 BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP);
595 btsock_l2cap_free_l(sock);
599 l2cap_socket* sock;
602 sock = btsock_l2cap_find_by_id_l(id);
603 if (!sock) return;
605 sock->outgoing_congest = p->cong ? 1 : 0;
607 if (!sock->outgoing_congest) {
609 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD,
610 sock->id);
616 l2cap_socket* sock = btsock_l2cap_find_by_id_l(id);
617 if (!sock) return;
619 int app_uid = sock->app_uid;
620 if (!sock->outgoing_congest) {
623 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD,
624 sock->id);
631 l2cap_socket* sock;
637 sock = btsock_l2cap_find_by_id_l(id);
638 if (!sock) return;
640 app_uid = sock->app_uid;
642 if (sock->fixed_chan) { /* we do these differently */
648 if (packet_put_tail_l(sock, data, p_buf->len)) {
650 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR,
651 sock->id);
655 BTA_JvL2capCloseLE(sock->handle);
656 btsock_l2cap_free_l(sock);
662 if (BTA_JvL2capReady(sock->handle, &count) == BTA_JV_SUCCESS) {
664 if (BTA_JvL2capRead(sock->handle, sock->id, buffer.data(), count) ==
666 if (packet_put_tail_l(sock, buffer.data(), count)) {
668 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP,
669 SOCK_THREAD_FD_WR, sock->id);
673 BTA_JvL2capClose(sock->handle);
674 btsock_l2cap_free_l(sock);
762 l2cap_socket* sock = btsock_l2cap_find_by_id_l(id);
763 if (!sock) {
764 LOG(ERROR) << __func__ << ": sock is null";
768 sock->channel = psm;
770 btsock_l2cap_server_listen(sock);
773 static void btsock_l2cap_server_listen(l2cap_socket* sock) {
774 DVLOG(2) << __func__ << ": fixed_chan: " << sock->fixed_chan
775 << ", channel: " << sock->channel
776 << ", is_le_coc: " << sock->is_le_coc;
778 if (sock->fixed_chan) {
779 BTA_JvL2capStartServerLE(sock->channel, btsock_l2cap_cbk, sock->id);
784 sock->is_le_coc ? BTA_JV_CONN_TYPE_L2CAP_LE : BTA_JV_CONN_TYPE_L2CAP;
788 if (sock->channel <= 0) {
789 BTA_JvGetChannelId(connection_type, sock->id, 0);
798 if (!sock->is_le_coc) {
802 BTA_JvL2capStartServer(connection_type, sock->security, 0,
803 std::move(ertm_info), sock->channel, sock->rx_mtu,
804 std::move(cfg), btsock_l2cap_cbk, sock->id);
831 l2cap_socket* sock = btsock_l2cap_alloc_l(name, addr, listen, flags);
832 if (!sock) {
836 sock->fixed_chan = fixed_chan;
837 sock->channel = channel;
838 sock->app_uid = app_uid;
839 sock->is_le_coc = is_le_coc;
840 sock->rx_mtu = is_le_coc ? L2CAP_SDU_LENGTH_LE_MAX : L2CAP_SDU_LENGTH_MAX;
844 btsock_l2cap_server_listen(sock);
847 BTA_JvL2capConnectLE(channel, sock->addr, btsock_l2cap_cbk, sock->id);
850 sock->is_le_coc ? BTA_JV_CONN_TYPE_L2CAP_LE : BTA_JV_CONN_TYPE_L2CAP;
857 if (!sock->is_le_coc) {
862 connection_type, sock->security, 0, std::move(ertm_info), channel,
863 sock->rx_mtu, std::move(cfg), sock->addr, btsock_l2cap_cbk, sock->id);
867 *sock_fd = sock->app_fd;
871 sock->app_fd = -1;
874 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP,
875 SOCK_THREAD_FD_EXCEPTION, sock->id);
896 static bool flush_incoming_que_on_wr_signal_l(l2cap_socket* sock) {
900 while (packet_get_head_l(sock, &buf, &len)) {
902 OSI_NO_INTR(sent = send(sock->our_fd, buf, len, MSG_DONTWAIT));
908 packet_put_head_l(sock, buf + sent, len - sent);
913 packet_put_head_l(sock, buf, len);
941 l2cap_socket* sock = btsock_l2cap_find_by_id_l(user_id);
942 if (!sock) return;
944 if ((flags & SOCK_THREAD_FD_RD) && !sock->server) {
946 if (sock->connected) {
948 bool ioctl_success = ioctl(sock->our_fd, FIONREAD, &size) == 0;
956 size = std::min(size, (int)sock->tx_mtu);
964 if (count > sock->tx_mtu) {
968 count = sock->tx_mtu;
978 if (sock->fixed_chan) {
980 BTA_JvL2capWriteFixed(sock->channel, sock->addr, PTR_TO_UINT(buffer),
984 BTA_JvL2capWrite(sock->handle, PTR_TO_UINT(buffer), buffer, user_id);
992 if (flush_incoming_que_on_wr_signal_l(sock) && sock->connected)
993 btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR,
994 sock->id);
998 if (drop_it || ioctl(sock->our_fd, FIONREAD, &size) != 0 || size == 0)
999 btsock_l2cap_free_l(sock);