The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2009-2012 Broadcom Corporation |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 4 | * |
| 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: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | /***************************************************************************** |
| 20 | * |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 21 | * Filename: uipc.cc |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 22 | * |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 23 | * Description: UIPC implementation for fluoride |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 24 | * |
| 25 | *****************************************************************************/ |
| 26 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 27 | #include <errno.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 28 | #include <fcntl.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 29 | #include <signal.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 33 | #include <sys/mman.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 34 | #include <sys/poll.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 35 | #include <sys/prctl.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 36 | #include <sys/select.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/un.h> |
| 40 | #include <unistd.h> |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 41 | #include <mutex> |
Hansong Zhang | f88552c | 2018-02-01 18:02:53 -0800 | [diff] [blame] | 42 | #include <set> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 43 | |
Pavlin Radoslavov | 304ceeb | 2017-04-05 16:18:26 -0700 | [diff] [blame] | 44 | #include "audio_a2dp_hw/include/audio_a2dp_hw.h" |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 45 | #include "bt_common.h" |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 46 | #include "bt_types.h" |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 47 | #include "bt_utils.h" |
Pavlin Radoslavov | d6121a3 | 2016-05-12 11:36:44 -0700 | [diff] [blame] | 48 | #include "osi/include/osi.h" |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 49 | #include "osi/include/socket_utils/sockets.h" |
| 50 | #include "uipc.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 51 | |
| 52 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 53 | * Constants & Macros |
| 54 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 55 | |
| 56 | #define PCM_FILENAME "/data/test.pcm" |
| 57 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 58 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 59 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 60 | #define CASE_RETURN_STR(const) \ |
| 61 | case const: \ |
| 62 | return #const; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 63 | |
| 64 | #define UIPC_DISCONNECTED (-1) |
| 65 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 66 | #define SAFE_FD_ISSET(fd, set) (((fd) == -1) ? false : FD_ISSET((fd), (set))) |
Elliott Hughes | 2408d9e | 2013-10-02 21:28:50 -0700 | [diff] [blame] | 67 | |
Andre Eisenbach | c5916e9 | 2014-11-07 15:46:04 -0800 | [diff] [blame] | 68 | #define UIPC_FLUSH_BUFFER_SIZE 1024 |
| 69 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 70 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 71 | * Local type definitions |
| 72 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 73 | |
| 74 | typedef enum { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 75 | UIPC_TASK_FLAG_DISCONNECT_CHAN = 0x1, |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 76 | } tUIPC_TASK_FLAGS; |
| 77 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 78 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 79 | * Static functions |
| 80 | *****************************************************************************/ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 81 | static int uipc_close_ch_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 82 | |
| 83 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 84 | * Externs |
| 85 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 86 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 87 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 88 | * Helper functions |
| 89 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 90 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 91 | const char* dump_uipc_event(tUIPC_EVENT event) { |
| 92 | switch (event) { |
| 93 | CASE_RETURN_STR(UIPC_OPEN_EVT) |
| 94 | CASE_RETURN_STR(UIPC_CLOSE_EVT) |
| 95 | CASE_RETURN_STR(UIPC_RX_DATA_EVT) |
| 96 | CASE_RETURN_STR(UIPC_RX_DATA_READY_EVT) |
| 97 | CASE_RETURN_STR(UIPC_TX_DATA_READY_EVT) |
| 98 | default: |
| 99 | return "UNKNOWN MSG ID"; |
| 100 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 104 | * socket helper functions |
| 105 | ****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 106 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 107 | static inline int create_server_socket(const char* name) { |
| 108 | int s = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 109 | if (s < 0) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 110 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 111 | BTIF_TRACE_EVENT("create_server_socket %s", name); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 112 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 113 | if (osi_socket_local_server_bind(s, name, |
Jakub Pawlowski | 7da3a4a | 2016-10-13 15:46:30 -0700 | [diff] [blame] | 114 | #if defined(OS_GENERIC) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 115 | ANDROID_SOCKET_NAMESPACE_FILESYSTEM |
| 116 | #else // !defined(OS_GENERIC) |
| 117 | ANDROID_SOCKET_NAMESPACE_ABSTRACT |
Jakub Pawlowski | 7da3a4a | 2016-10-13 15:46:30 -0700 | [diff] [blame] | 118 | #endif // defined(OS_GENERIC) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 119 | ) < 0) { |
| 120 | BTIF_TRACE_EVENT("socket failed to create (%s)", strerror(errno)); |
| 121 | close(s); |
| 122 | return -1; |
| 123 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 124 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 125 | if (listen(s, 5) < 0) { |
| 126 | BTIF_TRACE_EVENT("listen failed", strerror(errno)); |
| 127 | close(s); |
| 128 | return -1; |
| 129 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 130 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 131 | BTIF_TRACE_EVENT("created socket fd %d", s); |
| 132 | return s; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 135 | static int accept_server_socket(int sfd) { |
| 136 | struct sockaddr_un remote; |
| 137 | struct pollfd pfd; |
| 138 | int fd; |
| 139 | socklen_t len = sizeof(struct sockaddr_un); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 140 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 141 | BTIF_TRACE_EVENT("accept fd %d", sfd); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 142 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 143 | /* make sure there is data to process */ |
| 144 | pfd.fd = sfd; |
| 145 | pfd.events = POLLIN; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 146 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 147 | int poll_ret; |
| 148 | OSI_NO_INTR(poll_ret = poll(&pfd, 1, 0)); |
| 149 | if (poll_ret == 0) { |
| 150 | BTIF_TRACE_WARNING("accept poll timeout"); |
| 151 | return -1; |
| 152 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 153 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 154 | // BTIF_TRACE_EVENT("poll revents 0x%x", pfd.revents); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 155 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 156 | OSI_NO_INTR(fd = accept(sfd, (struct sockaddr*)&remote, &len)); |
| 157 | if (fd == -1) { |
| 158 | BTIF_TRACE_ERROR("sock accept failed (%s)", strerror(errno)); |
| 159 | return -1; |
| 160 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 161 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 162 | // match socket buffer size option with client |
| 163 | const int size = AUDIO_STREAM_OUTPUT_BUFFER_SZ; |
| 164 | int ret = |
| 165 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&size, (int)sizeof(size)); |
| 166 | if (ret < 0) { |
| 167 | BTIF_TRACE_ERROR("setsockopt failed (%s)", strerror(errno)); |
| 168 | } |
Andy Hung | 3d02215 | 2016-05-04 15:47:05 -0700 | [diff] [blame] | 169 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 170 | // BTIF_TRACE_EVENT("new fd %d", fd); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 171 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 172 | return fd; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 176 | * |
| 177 | * uipc helper functions |
| 178 | * |
| 179 | ****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 180 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 181 | static int uipc_main_init(tUIPC_STATE& uipc) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 182 | int i; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 183 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 184 | BTIF_TRACE_EVENT("### uipc_main_init ###"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 185 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 186 | uipc.tid = 0; |
| 187 | uipc.running = 0; |
| 188 | memset(&uipc.active_set, 0, sizeof(uipc.active_set)); |
| 189 | memset(&uipc.read_set, 0, sizeof(uipc.read_set)); |
| 190 | uipc.max_fd = 0; |
| 191 | memset(&uipc.signal_fds, 0, sizeof(uipc.signal_fds)); |
| 192 | memset(&uipc.ch, 0, sizeof(uipc.ch)); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 193 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 194 | /* setup interrupt socket pair */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 195 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, uipc.signal_fds) < 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 196 | return -1; |
| 197 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 198 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 199 | FD_SET(uipc.signal_fds[0], &uipc.active_set); |
| 200 | uipc.max_fd = MAX(uipc.max_fd, uipc.signal_fds[0]); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 201 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 202 | for (i = 0; i < UIPC_CH_NUM; i++) { |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 203 | tUIPC_CHAN* p = &uipc.ch[i]; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 204 | p->srvfd = UIPC_DISCONNECTED; |
| 205 | p->fd = UIPC_DISCONNECTED; |
| 206 | p->task_evt_flags = 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 207 | p->cback = NULL; |
| 208 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 209 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 210 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 213 | void uipc_main_cleanup(tUIPC_STATE& uipc) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 214 | int i; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 215 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 216 | BTIF_TRACE_EVENT("uipc_main_cleanup"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 217 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 218 | close(uipc.signal_fds[0]); |
| 219 | close(uipc.signal_fds[1]); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 220 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 221 | /* close any open channels */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 222 | for (i = 0; i < UIPC_CH_NUM; i++) uipc_close_ch_locked(uipc, i); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 223 | } |
| 224 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 225 | /* check pending events in read task */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 226 | static void uipc_check_task_flags_locked(tUIPC_STATE& uipc) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 227 | int i; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 228 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 229 | for (i = 0; i < UIPC_CH_NUM; i++) { |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 230 | if (uipc.ch[i].task_evt_flags & UIPC_TASK_FLAG_DISCONNECT_CHAN) { |
| 231 | uipc.ch[i].task_evt_flags &= ~UIPC_TASK_FLAG_DISCONNECT_CHAN; |
| 232 | uipc_close_ch_locked(uipc, i); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 235 | /* add here */ |
| 236 | } |
| 237 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 238 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 239 | static int uipc_check_fd_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 240 | if (ch_id >= UIPC_CH_NUM) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 241 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 242 | // BTIF_TRACE_EVENT("CHECK SRVFD %d (ch %d)", uipc.ch[ch_id].srvfd, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 243 | // ch_id); |
| 244 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 245 | if (SAFE_FD_ISSET(uipc.ch[ch_id].srvfd, &uipc.read_set)) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 246 | BTIF_TRACE_EVENT("INCOMING CONNECTION ON CH %d", ch_id); |
| 247 | |
Pavlin Radoslavov | 09fa9c5 | 2017-02-22 22:07:17 -0800 | [diff] [blame] | 248 | // Close the previous connection |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 249 | if (uipc.ch[ch_id].fd != UIPC_DISCONNECTED) { |
| 250 | BTIF_TRACE_EVENT("CLOSE CONNECTION (FD %d)", uipc.ch[ch_id].fd); |
| 251 | close(uipc.ch[ch_id].fd); |
| 252 | FD_CLR(uipc.ch[ch_id].fd, &uipc.active_set); |
| 253 | uipc.ch[ch_id].fd = UIPC_DISCONNECTED; |
Pavlin Radoslavov | 09fa9c5 | 2017-02-22 22:07:17 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 256 | uipc.ch[ch_id].fd = accept_server_socket(uipc.ch[ch_id].srvfd); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 257 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 258 | BTIF_TRACE_EVENT("NEW FD %d", uipc.ch[ch_id].fd); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 259 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 260 | if ((uipc.ch[ch_id].fd >= 0) && uipc.ch[ch_id].cback) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 261 | /* if we have a callback we should add this fd to the active set |
| 262 | and notify user with callback event */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 263 | BTIF_TRACE_EVENT("ADD FD %d TO ACTIVE SET", uipc.ch[ch_id].fd); |
| 264 | FD_SET(uipc.ch[ch_id].fd, &uipc.active_set); |
| 265 | uipc.max_fd = MAX(uipc.max_fd, uipc.ch[ch_id].fd); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 266 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 267 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 268 | if (uipc.ch[ch_id].fd < 0) { |
Pavlin Radoslavov | 7b67302 | 2017-09-19 20:29:33 -0700 | [diff] [blame] | 269 | BTIF_TRACE_ERROR("FAILED TO ACCEPT CH %d", ch_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 270 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 271 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 272 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 273 | if (uipc.ch[ch_id].cback) uipc.ch[ch_id].cback(ch_id, UIPC_OPEN_EVT); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 276 | // BTIF_TRACE_EVENT("CHECK FD %d (ch %d)", uipc.ch[ch_id].fd, ch_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 277 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 278 | if (SAFE_FD_ISSET(uipc.ch[ch_id].fd, &uipc.read_set)) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 279 | // BTIF_TRACE_EVENT("INCOMING DATA ON CH %d", ch_id); |
| 280 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 281 | if (uipc.ch[ch_id].cback) |
| 282 | uipc.ch[ch_id].cback(ch_id, UIPC_RX_DATA_READY_EVT); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 283 | } |
| 284 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 287 | static void uipc_check_interrupt_locked(tUIPC_STATE& uipc) { |
| 288 | if (SAFE_FD_ISSET(uipc.signal_fds[0], &uipc.read_set)) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 289 | char sig_recv = 0; |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 290 | OSI_NO_INTR( |
| 291 | recv(uipc.signal_fds[0], &sig_recv, sizeof(sig_recv), MSG_WAITALL)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 292 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 295 | static inline void uipc_wakeup_locked(tUIPC_STATE& uipc) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 296 | char sig_on = 1; |
| 297 | BTIF_TRACE_EVENT("UIPC SEND WAKE UP"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 298 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 299 | OSI_NO_INTR(send(uipc.signal_fds[1], &sig_on, sizeof(sig_on), 0)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 300 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 301 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 302 | static int uipc_setup_server_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, |
| 303 | const char* name, tUIPC_RCV_CBACK* cback) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 304 | int fd; |
| 305 | |
| 306 | BTIF_TRACE_EVENT("SETUP CHANNEL SERVER %d", ch_id); |
| 307 | |
| 308 | if (ch_id >= UIPC_CH_NUM) return -1; |
| 309 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 310 | std::lock_guard<std::recursive_mutex> guard(uipc.mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 311 | |
| 312 | fd = create_server_socket(name); |
| 313 | |
| 314 | if (fd < 0) { |
| 315 | BTIF_TRACE_ERROR("failed to setup %s", name, strerror(errno)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | BTIF_TRACE_EVENT("ADD SERVER FD TO ACTIVE SET %d", fd); |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 320 | FD_SET(fd, &uipc.active_set); |
| 321 | uipc.max_fd = MAX(uipc.max_fd, fd); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 322 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 323 | uipc.ch[ch_id].srvfd = fd; |
| 324 | uipc.ch[ch_id].cback = cback; |
| 325 | uipc.ch[ch_id].read_poll_tmo_ms = DEFAULT_READ_POLL_TMO_MS; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 326 | |
| 327 | /* trigger main thread to update read set */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 328 | uipc_wakeup_locked(uipc); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 329 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 333 | static void uipc_flush_ch_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 334 | char buf[UIPC_FLUSH_BUFFER_SIZE]; |
| 335 | struct pollfd pfd; |
| 336 | |
| 337 | pfd.events = POLLIN; |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 338 | pfd.fd = uipc.ch[ch_id].fd; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 339 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 340 | if (uipc.ch[ch_id].fd == UIPC_DISCONNECTED) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 341 | BTIF_TRACE_EVENT("%s() - fd disconnected. Exiting", __func__); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | while (1) { |
| 346 | int ret; |
| 347 | OSI_NO_INTR(ret = poll(&pfd, 1, 1)); |
| 348 | if (ret == 0) { |
| 349 | BTIF_TRACE_VERBOSE("%s(): poll() timeout - nothing to do. Exiting", |
| 350 | __func__); |
| 351 | return; |
| 352 | } |
| 353 | if (ret < 0) { |
| 354 | BTIF_TRACE_WARNING( |
| 355 | "%s() - poll() failed: return %d errno %d (%s). Exiting", __func__, |
| 356 | ret, errno, strerror(errno)); |
| 357 | return; |
| 358 | } |
| 359 | BTIF_TRACE_VERBOSE("%s() - polling fd %d, revents: 0x%x, ret %d", __func__, |
| 360 | pfd.fd, pfd.revents, ret); |
| 361 | if (pfd.revents & (POLLERR | POLLHUP)) { |
| 362 | BTIF_TRACE_WARNING("%s() - POLLERR or POLLHUP. Exiting", __func__); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | /* read sufficiently large buffer to ensure flush empties socket faster than |
| 367 | it is getting refilled */ |
| 368 | read(pfd.fd, &buf, UIPC_FLUSH_BUFFER_SIZE); |
| 369 | } |
| 370 | } |
| 371 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 372 | static void uipc_flush_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 373 | if (ch_id >= UIPC_CH_NUM) return; |
| 374 | |
| 375 | switch (ch_id) { |
| 376 | case UIPC_CH_ID_AV_CTRL: |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 377 | uipc_flush_ch_locked(uipc, UIPC_CH_ID_AV_CTRL); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case UIPC_CH_ID_AV_AUDIO: |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 381 | uipc_flush_ch_locked(uipc, UIPC_CH_ID_AV_AUDIO); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 386 | static int uipc_close_ch_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 387 | int wakeup = 0; |
| 388 | |
| 389 | BTIF_TRACE_EVENT("CLOSE CHANNEL %d", ch_id); |
| 390 | |
| 391 | if (ch_id >= UIPC_CH_NUM) return -1; |
| 392 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 393 | if (uipc.ch[ch_id].srvfd != UIPC_DISCONNECTED) { |
| 394 | BTIF_TRACE_EVENT("CLOSE SERVER (FD %d)", uipc.ch[ch_id].srvfd); |
| 395 | close(uipc.ch[ch_id].srvfd); |
| 396 | FD_CLR(uipc.ch[ch_id].srvfd, &uipc.active_set); |
| 397 | uipc.ch[ch_id].srvfd = UIPC_DISCONNECTED; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 398 | wakeup = 1; |
| 399 | } |
| 400 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 401 | if (uipc.ch[ch_id].fd != UIPC_DISCONNECTED) { |
| 402 | BTIF_TRACE_EVENT("CLOSE CONNECTION (FD %d)", uipc.ch[ch_id].fd); |
| 403 | close(uipc.ch[ch_id].fd); |
| 404 | FD_CLR(uipc.ch[ch_id].fd, &uipc.active_set); |
| 405 | uipc.ch[ch_id].fd = UIPC_DISCONNECTED; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 406 | wakeup = 1; |
| 407 | } |
| 408 | |
| 409 | /* notify this connection is closed */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 410 | if (uipc.ch[ch_id].cback) uipc.ch[ch_id].cback(ch_id, UIPC_CLOSE_EVT); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 411 | |
| 412 | /* trigger main thread update if something was updated */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 413 | if (wakeup) uipc_wakeup_locked(uipc); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 418 | void uipc_close_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id) { |
| 419 | if (uipc.ch[ch_id].srvfd == UIPC_DISCONNECTED) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 420 | BTIF_TRACE_EVENT("CHANNEL %d ALREADY CLOSED", ch_id); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | /* schedule close on this channel */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 425 | uipc.ch[ch_id].task_evt_flags |= UIPC_TASK_FLAG_DISCONNECT_CHAN; |
| 426 | uipc_wakeup_locked(uipc); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 429 | static void* uipc_read_task(void* arg) { |
| 430 | tUIPC_STATE& uipc = *((tUIPC_STATE*)arg); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 431 | int ch_id; |
| 432 | int result; |
| 433 | |
| 434 | prctl(PR_SET_NAME, (unsigned long)"uipc-main", 0, 0, 0); |
| 435 | |
| 436 | raise_priority_a2dp(TASK_UIPC_READ); |
| 437 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 438 | while (uipc.running) { |
| 439 | uipc.read_set = uipc.active_set; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 440 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 441 | result = select(uipc.max_fd + 1, &uipc.read_set, NULL, NULL, NULL); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 442 | |
| 443 | if (result == 0) { |
| 444 | BTIF_TRACE_EVENT("select timeout"); |
| 445 | continue; |
| 446 | } |
| 447 | if (result < 0) { |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 448 | if (errno != EINTR) { |
| 449 | BTIF_TRACE_EVENT("select failed %s", strerror(errno)); |
| 450 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 451 | continue; |
| 452 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 453 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 454 | { |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 455 | std::lock_guard<std::recursive_mutex> guard(uipc.mutex); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 456 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 457 | /* clear any wakeup interrupt */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 458 | uipc_check_interrupt_locked(uipc); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 459 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 460 | /* check pending task events */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 461 | uipc_check_task_flags_locked(uipc); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 462 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 463 | /* make sure we service audio channel first */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 464 | uipc_check_fd_locked(uipc, UIPC_CH_ID_AV_AUDIO); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 465 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 466 | /* check for other connections */ |
| 467 | for (ch_id = 0; ch_id < UIPC_CH_NUM; ch_id++) { |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 468 | if (ch_id != UIPC_CH_ID_AV_AUDIO) uipc_check_fd_locked(uipc, ch_id); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 469 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 470 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 471 | } |
Pavlin Radoslavov | dbf75f1 | 2016-11-08 00:57:53 +0000 | [diff] [blame] | 472 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 473 | BTIF_TRACE_EVENT("UIPC READ THREAD EXITING"); |
| 474 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 475 | uipc_main_cleanup(uipc); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 476 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 477 | uipc.tid = 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 478 | |
| 479 | BTIF_TRACE_EVENT("UIPC READ THREAD DONE"); |
| 480 | |
| 481 | return nullptr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 484 | int uipc_start_main_server_thread(tUIPC_STATE& uipc) { |
| 485 | uipc.running = 1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 486 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 487 | if (pthread_create(&uipc.tid, (const pthread_attr_t*)NULL, uipc_read_task, |
| 488 | &uipc) < 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 489 | BTIF_TRACE_ERROR("uipc_thread_create pthread_create failed:%d", errno); |
| 490 | return -1; |
| 491 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 492 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 493 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* blocking call */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 497 | void uipc_stop_main_server_thread(tUIPC_STATE& uipc) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 498 | /* request shutdown of read thread */ |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 499 | { |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 500 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
| 501 | uipc.running = 0; |
| 502 | uipc_wakeup_locked(uipc); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 503 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 504 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 505 | /* wait until read thread is fully terminated */ |
| 506 | /* tid might hold pointer value where it's value |
| 507 | is negative vaule with singed bit is set, so |
| 508 | corrected the logic to check zero or non zero */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 509 | if (uipc.tid) pthread_join(uipc.tid, NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /******************************************************************************* |
| 513 | ** |
| 514 | ** Function UIPC_Init |
| 515 | ** |
| 516 | ** Description Initialize UIPC module |
| 517 | ** |
| 518 | ** Returns void |
| 519 | ** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 520 | ******************************************************************************/ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 521 | std::unique_ptr<tUIPC_STATE> UIPC_Init() { |
| 522 | std::unique_ptr<tUIPC_STATE> uipc = std::make_unique<tUIPC_STATE>(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 523 | BTIF_TRACE_DEBUG("UIPC_Init"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 524 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 525 | std::lock_guard<std::recursive_mutex> lock(uipc->mutex); |
| 526 | |
| 527 | uipc_main_init(*uipc); |
| 528 | uipc_start_main_server_thread(*uipc); |
| 529 | |
| 530 | return uipc; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /******************************************************************************* |
| 534 | ** |
| 535 | ** Function UIPC_Open |
| 536 | ** |
| 537 | ** Description Open UIPC interface |
| 538 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 539 | ** Returns true in case of success, false in case of failure. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 540 | ** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 541 | ******************************************************************************/ |
Jakub Pawlowski | 16a6340 | 2018-03-02 02:22:33 -0800 | [diff] [blame] | 542 | bool UIPC_Open(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback, |
| 543 | const char* socket_path) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 544 | BTIF_TRACE_DEBUG("UIPC_Open : ch_id %d, p_cback %x", ch_id, p_cback); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 545 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 546 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 547 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 548 | if (ch_id >= UIPC_CH_NUM) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 549 | return false; |
| 550 | } |
Pavlin Radoslavov | dbf75f1 | 2016-11-08 00:57:53 +0000 | [diff] [blame] | 551 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 552 | if (uipc.ch[ch_id].srvfd != UIPC_DISCONNECTED) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 553 | BTIF_TRACE_EVENT("CHANNEL %d ALREADY OPEN", ch_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Jakub Pawlowski | 16a6340 | 2018-03-02 02:22:33 -0800 | [diff] [blame] | 557 | uipc_setup_server_locked(uipc, ch_id, socket_path, p_cback); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 558 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 559 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /******************************************************************************* |
| 563 | ** |
| 564 | ** Function UIPC_Close |
| 565 | ** |
| 566 | ** Description Close UIPC interface |
| 567 | ** |
| 568 | ** Returns void |
| 569 | ** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 570 | ******************************************************************************/ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 571 | void UIPC_Close(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 572 | BTIF_TRACE_DEBUG("UIPC_Close : ch_id %d", ch_id); |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 573 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 574 | /* special case handling uipc shutdown */ |
| 575 | if (ch_id != UIPC_CH_ID_ALL) { |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 576 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
| 577 | uipc_close_locked(uipc, ch_id); |
Hansong Zhang | f88552c | 2018-02-01 18:02:53 -0800 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 581 | BTIF_TRACE_DEBUG("UIPC_Close : waiting for shutdown to complete"); |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 582 | uipc_stop_main_server_thread(uipc); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 583 | BTIF_TRACE_DEBUG("UIPC_Close : shutdown complete"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /******************************************************************************* |
| 587 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 588 | ** Function UIPC_Send |
| 589 | ** |
| 590 | ** Description Called to transmit a message over UIPC. |
| 591 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 592 | ** Returns true in case of success, false in case of failure. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 593 | ** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 594 | ******************************************************************************/ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 595 | bool UIPC_Send(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, |
| 596 | UNUSED_ATTR uint16_t msg_evt, const uint8_t* p_buf, |
| 597 | uint16_t msglen) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 598 | BTIF_TRACE_DEBUG("UIPC_Send : ch_id:%d %d bytes", ch_id, msglen); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 599 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 600 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 601 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 602 | ssize_t ret; |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 603 | OSI_NO_INTR(ret = write(uipc.ch[ch_id].fd, p_buf, msglen)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 604 | if (ret < 0) { |
| 605 | BTIF_TRACE_ERROR("failed to write (%s)", strerror(errno)); |
| 606 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 607 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 608 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /******************************************************************************* |
| 612 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 613 | ** Function UIPC_Read |
| 614 | ** |
| 615 | ** Description Called to read a message from UIPC. |
| 616 | ** |
| 617 | ** Returns return the number of bytes read. |
| 618 | ** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 619 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 620 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 621 | uint32_t UIPC_Read(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, |
| 622 | UNUSED_ATTR uint16_t* p_msg_evt, uint8_t* p_buf, |
| 623 | uint32_t len) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 624 | if (ch_id >= UIPC_CH_NUM) { |
| 625 | BTIF_TRACE_ERROR("UIPC_Read : invalid ch id %d", ch_id); |
| 626 | return 0; |
| 627 | } |
| 628 | |
Chienyuan | fa62617 | 2019-01-30 11:50:01 +0800 | [diff] [blame] | 629 | int n_read = 0; |
| 630 | int fd = uipc.ch[ch_id].fd; |
| 631 | struct pollfd pfd; |
| 632 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 633 | if (fd == UIPC_DISCONNECTED) { |
| 634 | BTIF_TRACE_ERROR("UIPC_Read : channel %d closed", ch_id); |
| 635 | return 0; |
| 636 | } |
| 637 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 638 | while (n_read < (int)len) { |
| 639 | pfd.fd = fd; |
| 640 | pfd.events = POLLIN | POLLHUP; |
| 641 | |
| 642 | /* make sure there is data prior to attempting read to avoid blocking |
| 643 | a read for more than poll timeout */ |
| 644 | |
| 645 | int poll_ret; |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 646 | OSI_NO_INTR(poll_ret = poll(&pfd, 1, uipc.ch[ch_id].read_poll_tmo_ms)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 647 | if (poll_ret == 0) { |
| 648 | BTIF_TRACE_WARNING("poll timeout (%d ms)", |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 649 | uipc.ch[ch_id].read_poll_tmo_ms); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | if (poll_ret < 0) { |
| 653 | BTIF_TRACE_ERROR("%s(): poll() failed: return %d errno %d (%s)", __func__, |
| 654 | poll_ret, errno, strerror(errno)); |
| 655 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 658 | // BTIF_TRACE_EVENT("poll revents %x", pfd.revents); |
| 659 | |
| 660 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 661 | BTIF_TRACE_WARNING("poll : channel detached remotely"); |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 662 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
| 663 | uipc_close_locked(uipc, ch_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 664 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 667 | ssize_t n; |
| 668 | OSI_NO_INTR(n = recv(fd, p_buf + n_read, len - n_read, 0)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 669 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 670 | // BTIF_TRACE_EVENT("read %d bytes", n); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 671 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 672 | if (n == 0) { |
| 673 | BTIF_TRACE_WARNING("UIPC_Read : channel detached remotely"); |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 674 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
| 675 | uipc_close_locked(uipc, ch_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 676 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 679 | if (n < 0) { |
| 680 | BTIF_TRACE_WARNING("UIPC_Read : read failed (%s)", strerror(errno)); |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | n_read += n; |
| 685 | } |
| 686 | |
| 687 | return n_read; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 691 | * |
| 692 | * Function UIPC_Ioctl |
| 693 | * |
| 694 | * Description Called to control UIPC. |
| 695 | * |
| 696 | * Returns void |
| 697 | * |
| 698 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 699 | |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 700 | extern bool UIPC_Ioctl(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint32_t request, |
| 701 | void* param) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 702 | BTIF_TRACE_DEBUG("#### UIPC_Ioctl : ch_id %d, request %d ####", ch_id, |
| 703 | request); |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 704 | std::lock_guard<std::recursive_mutex> lock(uipc.mutex); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 705 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 706 | switch (request) { |
| 707 | case UIPC_REQ_RX_FLUSH: |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 708 | uipc_flush_locked(uipc, ch_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 709 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 710 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 711 | case UIPC_REG_CBACK: |
| 712 | // BTIF_TRACE_EVENT("register callback ch %d srvfd %d, fd %d", ch_id, |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 713 | // uipc.ch[ch_id].srvfd, uipc.ch[ch_id].fd); |
| 714 | uipc.ch[ch_id].cback = (tUIPC_RCV_CBACK*)param; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 715 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 716 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 717 | case UIPC_REG_REMOVE_ACTIVE_READSET: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 718 | /* user will read data directly and not use select loop */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 719 | if (uipc.ch[ch_id].fd != UIPC_DISCONNECTED) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 720 | /* remove this channel from active set */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 721 | FD_CLR(uipc.ch[ch_id].fd, &uipc.active_set); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 722 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 723 | /* refresh active set */ |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 724 | uipc_wakeup_locked(uipc); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 725 | } |
| 726 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 727 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 728 | case UIPC_SET_READ_POLL_TMO: |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 729 | uipc.ch[ch_id].read_poll_tmo_ms = (intptr_t)param; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 730 | BTIF_TRACE_EVENT("UIPC_SET_READ_POLL_TMO : CH %d, TMO %d ms", ch_id, |
Jakub Pawlowski | 4653f21 | 2018-03-01 16:23:58 -0800 | [diff] [blame] | 731 | uipc.ch[ch_id].read_poll_tmo_ms); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 732 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 733 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 734 | default: |
| 735 | BTIF_TRACE_EVENT("UIPC_Ioctl : request not handled (%d)", request); |
| 736 | break; |
| 737 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 738 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 739 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 740 | } |