codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file secril-client.cpp |
| 3 | * |
| 4 | * @author Myeongcheol Kim (mcmount.kim@samsung.com) |
| 5 | * |
| 6 | * @brief RIL client library for multi-client support |
| 7 | */ |
| 8 | |
| 9 | #define LOG_TAG "RILClient" |
Andreas Schneider | a366011 | 2017-02-06 18:23:19 +0100 | [diff] [blame] | 10 | /*#define LOG_NDEBUG 0*/ |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 11 | |
| 12 | #include <binder/Parcel.h> |
| 13 | #include <telephony/ril.h> |
| 14 | #include <cutils/record_stream.h> |
| 15 | |
| 16 | #include <unistd.h> |
| 17 | #include <errno.h> |
| 18 | #include <cutils/sockets.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <string.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <pthread.h> |
| 25 | #include "secril-client.h" |
| 26 | #include <hardware_legacy/power.h> // For wakelock |
| 27 | |
| 28 | |
| 29 | #define RIL_CLIENT_WAKE_LOCK "client-interface" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | //--------------------------------------------------------------------------- |
| 34 | // Defines |
| 35 | //--------------------------------------------------------------------------- |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 36 | #define RILD_PORT 7777 |
| 37 | #define MULTI_CLIENT_SOCKET_NAME "Multiclient" |
| 38 | #define MULTI_CLIENT_Q_SOCKET_NAME "QMulticlient" |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 39 | #define MULTI_CLIENT_SOCKET_NAME_2 "Multiclient2" |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 40 | |
| 41 | #define MAX_COMMAND_BYTES (8 * 1024) |
| 42 | #define REQ_POOL_SIZE 32 |
| 43 | #define TOKEN_POOL_SIZE 32 |
| 44 | |
| 45 | // Constants for response types |
| 46 | #define RESPONSE_SOLICITED 0 |
| 47 | #define RESPONSE_UNSOLICITED 1 |
| 48 | |
| 49 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 50 | |
| 51 | #define REQ_OEM_HOOK_RAW RIL_REQUEST_OEM_HOOK_RAW |
| 52 | #define REQ_SET_CALL_VOLUME 101 |
| 53 | #define REQ_SET_AUDIO_PATH 102 |
| 54 | #define REQ_SET_CALL_CLOCK_SYNC 103 |
| 55 | #define REQ_SET_CALL_RECORDING 104 |
| 56 | #define REQ_SET_CALL_MUTE 105 |
| 57 | #define REQ_GET_CALL_MUTE 106 |
| 58 | #define REQ_SET_CALL_VT_CTRL 107 |
| 59 | #define REQ_SET_TWO_MIC_CTRL 108 |
| 60 | #define REQ_SET_DHA_CTRL 109 |
| 61 | #define REQ_SET_LOOPBACK 110 |
| 62 | |
| 63 | // OEM request function ID |
| 64 | #define OEM_FUNC_SOUND 0x08 |
| 65 | |
| 66 | // OEM request sub function ID |
| 67 | #define OEM_SND_SET_VOLUME_CTRL 0x03 |
| 68 | #define OEM_SND_SET_AUDIO_PATH 0x05 |
| 69 | #define OEM_SND_GET_AUDIO_PATH 0x06 |
| 70 | #define OEM_SND_SET_VIDEO_CALL_CTRL 0x07 |
| 71 | #define OEM_SND_SET_LOOPBACK_CTRL 0x08 |
| 72 | #define OEM_SND_SET_VOICE_RECORDING_CTRL 0x09 |
| 73 | #define OEM_SND_SET_CLOCK_CTRL 0x0A |
| 74 | #define OEM_SND_SET_MUTE 0x0B |
| 75 | #define OEM_SND_GET_MUTE 0x0C |
| 76 | #define OEM_SND_SET_TWO_MIC_CTL 0x0D |
| 77 | #define OEM_SND_SET_DHA_CTL 0x0E |
| 78 | |
| 79 | #define OEM_SND_TYPE_VOICE 0x01 // Receiver(0x00) + Voice(0x01) |
| 80 | #define OEM_SND_TYPE_SPEAKER 0x11 // SpeakerPhone(0x10) + Voice(0x01) |
| 81 | #define OEM_SND_TYPE_HEADSET 0x31 // Headset(0x30) + Voice(0x01) |
| 82 | #define OEM_SND_TYPE_BTVOICE 0x41 // BT(0x40) + Voice(0x01) |
| 83 | |
Christopher N. Hesse | 57eacca | 2016-03-08 15:50:23 +0100 | [diff] [blame] | 84 | #ifdef SAMSUNG_NEXT_GEN_MODEM |
Andreas Schneider | e005881 | 2017-02-14 17:24:02 +0100 | [diff] [blame] | 85 | #define OEM_SND_AUDIO_PATH_EARPIECE 0x01 |
Andreas Schneider | 6b9cd5f | 2015-01-23 22:41:22 +0100 | [diff] [blame] | 86 | #define OEM_SND_AUDIO_PATH_HEADSET 0x02 |
| 87 | #define OEM_SND_AUDIO_PATH_HFK 0x06 |
| 88 | #define OEM_SND_AUDIO_PATH_BLUETOOTH 0x04 |
| 89 | #define OEM_SND_AUDIO_PATH_STEREO_BLUETOOTH 0x05 |
| 90 | #define OEM_SND_AUDIO_PATH_SPEAKER 0x07 |
| 91 | #define OEM_SND_AUDIO_PATH_HEADPHONE 0x08 |
| 92 | #define OEM_SND_AUDIO_PATH_BT_NSEC_OFF 0x09 |
| 93 | #define OEM_SND_AUDIO_PATH_MIC1 0x0A |
| 94 | #define OEM_SND_AUDIO_PATH_MIC2 0x0B |
| 95 | #define OEM_SND_AUDIO_PATH_BT_WB 0x0C |
| 96 | #define OEM_SND_AUDIO_PATH_BT_WB_NSEC_OFF 0x0D |
| 97 | #else |
Andreas Schneider | e005881 | 2017-02-14 17:24:02 +0100 | [diff] [blame] | 98 | #define OEM_SND_AUDIO_PATH_EARPIECE 0x01 |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 99 | #define OEM_SND_AUDIO_PATH_HEADSET 0x02 |
| 100 | #define OEM_SND_AUDIO_PATH_HFK 0x03 |
| 101 | #define OEM_SND_AUDIO_PATH_BLUETOOTH 0x04 |
| 102 | #define OEM_SND_AUDIO_PATH_STEREO_BLUETOOTH 0x05 |
| 103 | #define OEM_SND_AUDIO_PATH_SPEAKER 0x06 |
| 104 | #define OEM_SND_AUDIO_PATH_HEADPHONE 0x07 |
| 105 | #define OEM_SND_AUDIO_PATH_BT_NSEC_OFF 0x08 |
| 106 | #define OEM_SND_AUDIO_PATH_MIC1 0x09 |
| 107 | #define OEM_SND_AUDIO_PATH_MIC2 0x0A |
| 108 | #define OEM_SND_AUDIO_PATH_BT_WB 0x0B |
| 109 | #define OEM_SND_AUDIO_PATH_BT_WB_NSEC_OFF 0x0C |
Andreas Schneider | 6b9cd5f | 2015-01-23 22:41:22 +0100 | [diff] [blame] | 110 | #endif |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 111 | |
| 112 | //--------------------------------------------------------------------------- |
| 113 | // Type definitions |
| 114 | //--------------------------------------------------------------------------- |
| 115 | typedef struct _ReqHistory { |
| 116 | int token; // token used for request |
| 117 | uint32_t id; // request ID |
| 118 | } ReqHistory; |
| 119 | |
| 120 | typedef struct _ReqRespHandler { |
| 121 | uint32_t id; // request ID |
| 122 | RilOnComplete handler; // handler function |
| 123 | } ReqRespHandler; |
| 124 | |
| 125 | typedef struct _UnsolHandler { |
| 126 | uint32_t id; // unsolicited response ID |
| 127 | RilOnUnsolicited handler; // handler function |
| 128 | } UnsolHandler; |
| 129 | |
| 130 | typedef struct _RilClientPrv { |
| 131 | HRilClient parent; |
| 132 | uint8_t b_connect; // connected to server? |
| 133 | int sock; // socket |
| 134 | int pipefd[2]; |
| 135 | fd_set sock_rfds; // for read with select() |
| 136 | RecordStream *p_rs; |
| 137 | uint32_t token_pool; // each bit in token_pool used for token. |
| 138 | // so, pool size is 32. |
| 139 | pthread_t tid_reader; // socket reader thread id |
| 140 | ReqHistory history[TOKEN_POOL_SIZE]; // request history |
| 141 | ReqRespHandler req_handlers[REQ_POOL_SIZE]; // request response handler list |
| 142 | UnsolHandler unsol_handlers[REQ_POOL_SIZE]; // unsolicited response handler list |
| 143 | RilOnError err_cb; // error callback |
| 144 | void *err_cb_data; // error callback data |
| 145 | uint8_t b_del_handler; |
| 146 | } RilClientPrv; |
| 147 | |
| 148 | |
| 149 | //--------------------------------------------------------------------------- |
| 150 | // Local static function prototypes |
| 151 | //--------------------------------------------------------------------------- |
| 152 | static void * RxReaderFunc(void *param); |
| 153 | static int processRxBuffer(RilClientPrv *prv, void *buffer, size_t buflen); |
| 154 | static uint32_t AllocateToken(uint32_t *token_pool); |
| 155 | static void FreeToken(uint32_t *token_pool, uint32_t token); |
| 156 | static uint8_t IsValidToken(uint32_t *token_pool, uint32_t token); |
| 157 | static void DeallocateToken(uint32_t *token_pool, uint32_t token); |
| 158 | static int blockingWrite(int fd, const void *buffer, size_t len); |
| 159 | static int RecordReqHistory(RilClientPrv *prv, int token, uint32_t id); |
| 160 | static void ClearReqHistory(RilClientPrv *prv, int token); |
| 161 | static RilOnComplete FindReqHandler(RilClientPrv *prv, int token, uint32_t *id); |
| 162 | static RilOnUnsolicited FindUnsolHandler(RilClientPrv *prv, uint32_t id); |
| 163 | static int SendOemRequestHookRaw(HRilClient client, int req_id, char *data, size_t len); |
| 164 | static bool isValidSoundType(SoundType type); |
| 165 | static bool isValidAudioPath(AudioPath path); |
| 166 | static bool isValidSoundClockCondition(SoundClockCondition condition); |
| 167 | static bool isValidCallRecCondition(CallRecCondition condition); |
| 168 | static bool isValidMuteCondition(MuteCondition condition); |
| 169 | static bool isValidTwoMicCtrl(TwoMicSolDevice device, TwoMicSolReport report); |
| 170 | static char ConvertSoundType(SoundType type); |
| 171 | static char ConvertAudioPath(AudioPath path); |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * @fn int RegisterUnsolicitedHandler(HRilClient client, uint32_t id, RilOnUnsolicited handler) |
| 176 | * |
| 177 | * @params client: Client handle. |
| 178 | * id: Unsolicited response ID to which handler is registered. |
| 179 | * handler: Unsolicited handler. NULL for deregistration. |
| 180 | * |
| 181 | * @return 0 on success or error code. |
| 182 | */ |
| 183 | extern "C" |
| 184 | int RegisterUnsolicitedHandler(HRilClient client, uint32_t id, RilOnUnsolicited handler) { |
| 185 | RilClientPrv *client_prv; |
| 186 | int match_slot = -1; |
| 187 | int first_empty_slot = -1; |
| 188 | int i; |
| 189 | |
| 190 | if (client == NULL || client->prv == NULL) |
| 191 | return RIL_CLIENT_ERR_INVAL; |
| 192 | |
| 193 | client_prv = (RilClientPrv *)(client->prv); |
| 194 | |
| 195 | for (i = 0; i < REQ_POOL_SIZE; i++) { |
| 196 | // Check if there is matched handler. |
| 197 | if (id == client_prv->unsol_handlers[i].id) { |
| 198 | match_slot = i; |
| 199 | } |
| 200 | // Find first empty handler slot. |
| 201 | if (first_empty_slot == -1 && client_prv->unsol_handlers[i].id == 0) { |
| 202 | first_empty_slot = i; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (handler == NULL) { // Unregister. |
| 207 | if (match_slot >= 0) { |
| 208 | memset(&(client_prv->unsol_handlers[match_slot]), 0, sizeof(UnsolHandler)); |
| 209 | return RIL_CLIENT_ERR_SUCCESS; |
| 210 | } |
| 211 | else { |
| 212 | return RIL_CLIENT_ERR_SUCCESS; |
| 213 | } |
| 214 | } |
| 215 | else {// Register. |
| 216 | if (match_slot >= 0) { |
| 217 | client_prv->unsol_handlers[match_slot].handler = handler; // Just update. |
| 218 | } |
| 219 | else if (first_empty_slot >= 0) { |
| 220 | client_prv->unsol_handlers[first_empty_slot].id = id; |
| 221 | client_prv->unsol_handlers[first_empty_slot].handler = handler; |
| 222 | } |
| 223 | else { |
| 224 | return RIL_CLIENT_ERR_RESOURCE; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return RIL_CLIENT_ERR_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /** |
| 233 | * @fn int RegisterRequestCompleteHandler(HRilClient client, uint32_t id, RilOnComplete handler) |
| 234 | * |
| 235 | * @params client: Client handle. |
| 236 | * id: Request ID to which handler is registered. |
| 237 | * handler: Request complete handler. NULL for deregistration. |
| 238 | * |
| 239 | * @return 0 on success or error code. |
| 240 | */ |
| 241 | extern "C" |
| 242 | int RegisterRequestCompleteHandler(HRilClient client, uint32_t id, RilOnComplete handler) { |
| 243 | RilClientPrv *client_prv; |
| 244 | int match_slot = -1; |
| 245 | int first_empty_slot = -1; |
| 246 | int i; |
| 247 | |
| 248 | if (client == NULL || client->prv == NULL) |
| 249 | return RIL_CLIENT_ERR_INVAL; |
| 250 | |
| 251 | client_prv = (RilClientPrv *)(client->prv); |
| 252 | |
| 253 | for (i = 0; i < REQ_POOL_SIZE; i++) { |
| 254 | // Check if there is matched handler. |
| 255 | if (id == client_prv->req_handlers[i].id) { |
| 256 | match_slot = i; |
| 257 | } |
| 258 | // Find first empty handler slot. |
| 259 | if (first_empty_slot == -1 && client_prv->req_handlers[i].id == 0) { |
| 260 | first_empty_slot = i; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (handler == NULL) { // Unregister. |
| 265 | if (match_slot >= 0) { |
| 266 | memset(&(client_prv->req_handlers[match_slot]), 0, sizeof(ReqRespHandler)); |
| 267 | return RIL_CLIENT_ERR_SUCCESS; |
| 268 | } |
| 269 | else { |
| 270 | return RIL_CLIENT_ERR_SUCCESS; |
| 271 | } |
| 272 | } |
| 273 | else { // Register. |
| 274 | if (match_slot >= 0) { |
| 275 | client_prv->req_handlers[match_slot].handler = handler; // Just update. |
| 276 | } |
| 277 | else if (first_empty_slot >= 0) { |
| 278 | client_prv->req_handlers[first_empty_slot].id = id; |
| 279 | client_prv->req_handlers[first_empty_slot].handler = handler; |
| 280 | } |
| 281 | else { |
| 282 | return RIL_CLIENT_ERR_RESOURCE; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return RIL_CLIENT_ERR_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * @fn int RegisterErrorCallback(HRilClient client, RilOnError cb, void *data) |
| 292 | * |
| 293 | * @params client: Client handle. |
| 294 | * cb: Error callback. NULL for unregistration. |
| 295 | * data: Callback data. |
| 296 | * |
| 297 | * @return 0 for success or error code. |
| 298 | */ |
| 299 | extern "C" |
| 300 | int RegisterErrorCallback(HRilClient client, RilOnError cb, void *data) { |
| 301 | RilClientPrv *client_prv; |
| 302 | |
| 303 | if (client == NULL || client->prv == NULL) |
| 304 | return RIL_CLIENT_ERR_INVAL; |
| 305 | |
| 306 | client_prv = (RilClientPrv *)(client->prv); |
| 307 | |
| 308 | client_prv->err_cb = cb; |
| 309 | client_prv->err_cb_data = data; |
| 310 | |
| 311 | return RIL_CLIENT_ERR_SUCCESS; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /** |
| 316 | * @fn HRilClient OpenClient_RILD(void) |
| 317 | * |
| 318 | * @params None. |
| 319 | * |
| 320 | * @return Client handle, NULL on error. |
| 321 | */ |
| 322 | extern "C" |
| 323 | HRilClient OpenClient_RILD(void) { |
| 324 | HRilClient client = (HRilClient)malloc(sizeof(struct RilClient)); |
| 325 | if (client == NULL) |
| 326 | return NULL; |
| 327 | |
| 328 | client->prv = (RilClientPrv *)malloc(sizeof(RilClientPrv)); |
| 329 | if (client->prv == NULL) { |
| 330 | free(client); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | memset(client->prv, 0, sizeof(RilClientPrv)); |
| 335 | |
| 336 | ((RilClientPrv *)(client->prv))->parent = client; |
| 337 | ((RilClientPrv *)(client->prv))->sock = -1; |
| 338 | |
| 339 | return client; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /** |
| 344 | * @fn int Connect_RILD(void) |
| 345 | * |
| 346 | * @params client: Client handle. |
| 347 | * |
| 348 | * @return 0, or error code. |
| 349 | */ |
| 350 | extern "C" |
| 351 | int Connect_RILD(HRilClient client) { |
| 352 | RilClientPrv *client_prv; |
| 353 | |
| 354 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 355 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 356 | return RIL_CLIENT_ERR_INVAL; |
| 357 | } |
| 358 | |
| 359 | client_prv = (RilClientPrv *)(client->prv); |
| 360 | |
| 361 | // Open client socket and connect to server. |
| 362 | //client_prv->sock = socket_loopback_client(RILD_PORT, SOCK_STREAM); |
| 363 | client_prv->sock = socket_local_client(MULTI_CLIENT_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM ); |
| 364 | |
| 365 | if (client_prv->sock < 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 366 | RLOGE("%s: Connecting failed. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 367 | return RIL_CLIENT_ERR_CONNECT; |
| 368 | } |
| 369 | |
| 370 | client_prv->b_connect = 1; |
| 371 | |
| 372 | if (fcntl(client_prv->sock, F_SETFL, O_NONBLOCK) < 0) { |
| 373 | close(client_prv->sock); |
| 374 | return RIL_CLIENT_ERR_IO; |
| 375 | } |
| 376 | |
| 377 | client_prv->p_rs = record_stream_new(client_prv->sock, MAX_COMMAND_BYTES); |
| 378 | |
| 379 | if (pipe(client_prv->pipefd) < 0) { |
| 380 | close(client_prv->sock); |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 381 | RLOGE("%s: Creating command pipe failed. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 382 | return RIL_CLIENT_ERR_IO; |
| 383 | } |
| 384 | |
| 385 | if (fcntl(client_prv->pipefd[0], F_SETFL, O_NONBLOCK) < 0) { |
| 386 | close(client_prv->sock); |
| 387 | close(client_prv->pipefd[0]); |
| 388 | close(client_prv->pipefd[1]); |
| 389 | return RIL_CLIENT_ERR_IO; |
| 390 | } |
| 391 | |
| 392 | // Start socket read thread. |
| 393 | if (pthread_create(&(client_prv->tid_reader), NULL, RxReaderFunc, (void *)client_prv) != 0) { |
| 394 | close(client_prv->sock); |
| 395 | close(client_prv->pipefd[0]); |
| 396 | close(client_prv->pipefd[1]); |
| 397 | |
| 398 | memset(client_prv, 0, sizeof(RilClientPrv)); |
| 399 | client_prv->sock = -1; |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 400 | RLOGE("%s: Can't create Reader thread. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 401 | return RIL_CLIENT_ERR_CONNECT; |
| 402 | } |
| 403 | |
| 404 | return RIL_CLIENT_ERR_SUCCESS; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @fn int Connect_QRILD(void) |
| 409 | * |
| 410 | * @params client: Client handle. |
| 411 | * |
| 412 | * @return 0, or error code. |
| 413 | */ |
| 414 | extern "C" |
| 415 | int Connect_QRILD(HRilClient client) { |
| 416 | RilClientPrv *client_prv; |
| 417 | |
| 418 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 419 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 420 | return RIL_CLIENT_ERR_INVAL; |
| 421 | } |
| 422 | |
| 423 | client_prv = (RilClientPrv *)(client->prv); |
| 424 | |
| 425 | // Open client socket and connect to server. |
| 426 | //client_prv->sock = socket_loopback_client(RILD_PORT, SOCK_STREAM); |
| 427 | client_prv->sock = socket_local_client(MULTI_CLIENT_Q_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM ); |
| 428 | |
| 429 | if (client_prv->sock < 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 430 | RLOGE("%s: Connecting failed. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 431 | return RIL_CLIENT_ERR_CONNECT; |
| 432 | } |
| 433 | |
| 434 | client_prv->b_connect = 1; |
| 435 | |
| 436 | if (fcntl(client_prv->sock, F_SETFL, O_NONBLOCK) < 0) { |
| 437 | close(client_prv->sock); |
| 438 | return RIL_CLIENT_ERR_IO; |
| 439 | } |
| 440 | |
| 441 | client_prv->p_rs = record_stream_new(client_prv->sock, MAX_COMMAND_BYTES); |
| 442 | |
| 443 | if (pipe(client_prv->pipefd) < 0) { |
| 444 | close(client_prv->sock); |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 445 | RLOGE("%s: Creating command pipe failed. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 446 | return RIL_CLIENT_ERR_IO; |
| 447 | } |
| 448 | |
| 449 | if (fcntl(client_prv->pipefd[0], F_SETFL, O_NONBLOCK) < 0) { |
| 450 | close(client_prv->sock); |
| 451 | close(client_prv->pipefd[0]); |
| 452 | close(client_prv->pipefd[1]); |
| 453 | return RIL_CLIENT_ERR_IO; |
| 454 | } |
| 455 | |
| 456 | // Start socket read thread. |
| 457 | if (pthread_create(&(client_prv->tid_reader), NULL, RxReaderFunc, (void *)client_prv) != 0) { |
| 458 | close(client_prv->sock); |
| 459 | close(client_prv->pipefd[0]); |
| 460 | close(client_prv->pipefd[1]); |
| 461 | |
| 462 | memset(client_prv, 0, sizeof(RilClientPrv)); |
| 463 | client_prv->sock = -1; |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 464 | RLOGE("%s: Can't create Reader thread. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 465 | return RIL_CLIENT_ERR_CONNECT; |
| 466 | } |
| 467 | |
| 468 | return RIL_CLIENT_ERR_SUCCESS; |
| 469 | } |
| 470 | |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 471 | /** |
| 472 | * @fn int Connect_RILD_Second(void) |
| 473 | * |
| 474 | * @params client: Client handle. |
| 475 | * |
| 476 | * @return 0, or error code. |
| 477 | */ |
| 478 | extern "C" |
| 479 | int Connect_RILD_Second(HRilClient client) { |
| 480 | RilClientPrv *client_prv; |
| 481 | |
| 482 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 483 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 484 | return RIL_CLIENT_ERR_INVAL; |
| 485 | } |
| 486 | |
| 487 | client_prv = (RilClientPrv *)(client->prv); |
| 488 | |
| 489 | // Open client socket and connect to server. |
| 490 | //client_prv->sock = socket_loopback_client(RILD_PORT, SOCK_STREAM); |
| 491 | client_prv->sock = socket_local_client(MULTI_CLIENT_SOCKET_NAME_2, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM ); |
| 492 | |
| 493 | if (client_prv->sock < 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 494 | RLOGE("%s: Connecting failed. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 495 | return RIL_CLIENT_ERR_CONNECT; |
| 496 | } |
| 497 | |
| 498 | client_prv->b_connect = 1; |
| 499 | |
| 500 | if (fcntl(client_prv->sock, F_SETFL, O_NONBLOCK) < 0) { |
| 501 | close(client_prv->sock); |
| 502 | return RIL_CLIENT_ERR_IO; |
| 503 | } |
| 504 | |
| 505 | client_prv->p_rs = record_stream_new(client_prv->sock, MAX_COMMAND_BYTES); |
| 506 | |
| 507 | if (pipe(client_prv->pipefd) < 0) { |
| 508 | close(client_prv->sock); |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 509 | RLOGE("%s: Creating command pipe failed. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 510 | return RIL_CLIENT_ERR_IO; |
| 511 | } |
| 512 | |
| 513 | if (fcntl(client_prv->pipefd[0], F_SETFL, O_NONBLOCK) < 0) { |
| 514 | close(client_prv->sock); |
| 515 | close(client_prv->pipefd[0]); |
| 516 | close(client_prv->pipefd[1]); |
| 517 | return RIL_CLIENT_ERR_IO; |
| 518 | } |
| 519 | |
| 520 | // Start socket read thread. |
| 521 | if (pthread_create(&(client_prv->tid_reader), NULL, RxReaderFunc, (void *)client_prv) != 0) { |
| 522 | close(client_prv->sock); |
| 523 | close(client_prv->pipefd[0]); |
| 524 | close(client_prv->pipefd[1]); |
| 525 | |
| 526 | memset(client_prv, 0, sizeof(RilClientPrv)); |
| 527 | client_prv->sock = -1; |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 528 | RLOGE("%s: Can't create Reader thread. %s(%d)", __FUNCTION__, strerror(errno), errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 529 | return RIL_CLIENT_ERR_CONNECT; |
| 530 | } |
| 531 | |
| 532 | return RIL_CLIENT_ERR_SUCCESS; |
| 533 | } |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * @fn int isConnected_RILD(HRilClient client) |
| 537 | * |
| 538 | * @params client: Client handle. |
| 539 | * |
| 540 | * @return 0, or 1. |
| 541 | */ |
| 542 | extern "C" |
| 543 | int isConnected_RILD(HRilClient client) { |
| 544 | RilClientPrv *client_prv; |
| 545 | |
| 546 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 547 | RLOGE("%s: invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 548 | return RIL_CLIENT_ERR_INVAL; |
| 549 | } |
| 550 | |
| 551 | client_prv = (RilClientPrv *)(client->prv); |
| 552 | |
| 553 | return client_prv->b_connect == 1; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @fn int Disconnect_RILD(HRilClient client) |
| 558 | * |
| 559 | * @params client: Client handle. |
| 560 | * |
| 561 | * @return 0 on success, or error code. |
| 562 | */ |
| 563 | extern "C" |
| 564 | int Disconnect_RILD(HRilClient client) { |
| 565 | RilClientPrv *client_prv; |
| 566 | int ret = 0; |
| 567 | |
| 568 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 569 | RLOGE("%s: invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 570 | return RIL_CLIENT_ERR_INVAL; |
| 571 | } |
| 572 | |
| 573 | client_prv = (RilClientPrv *)(client->prv); |
| 574 | |
| 575 | if (client_prv->sock == -1) |
| 576 | return RIL_CLIENT_ERR_SUCCESS; |
| 577 | |
| 578 | printf("[*] %s(): sock=%d\n", __FUNCTION__, client_prv->sock); |
| 579 | |
| 580 | if (client_prv->sock > 0) { |
| 581 | do { |
| 582 | ret = write(client_prv->pipefd[1], "close", strlen("close")); |
| 583 | } while (ret < 0 && errno == EINTR); |
| 584 | } |
| 585 | |
| 586 | client_prv->b_connect = 0; |
| 587 | |
| 588 | pthread_join(client_prv->tid_reader, NULL); |
| 589 | |
| 590 | return RIL_CLIENT_ERR_SUCCESS; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /** |
| 595 | * @fn int CloseClient_RILD(HRilClient client) |
| 596 | * |
| 597 | * @params client: Client handle. |
| 598 | * |
| 599 | * @return 0 on success, or error code. |
| 600 | */ |
| 601 | extern "C" |
| 602 | int CloseClient_RILD(HRilClient client) { |
| 603 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 604 | RLOGE("%s: invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 605 | return RIL_CLIENT_ERR_INVAL; |
| 606 | } |
| 607 | |
| 608 | Disconnect_RILD(client); |
| 609 | |
| 610 | free(client->prv); |
| 611 | free(client); |
| 612 | |
| 613 | return RIL_CLIENT_ERR_SUCCESS; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | /** |
| 618 | * Set in-call volume. |
| 619 | */ |
| 620 | extern "C" |
| 621 | int SetCallVolume(HRilClient client, SoundType type, int vol_level) { |
| 622 | RilClientPrv *client_prv; |
| 623 | int ret; |
| 624 | char data[6] = {0,}; |
| 625 | |
| 626 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 627 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 628 | return RIL_CLIENT_ERR_INVAL; |
| 629 | } |
| 630 | |
| 631 | client_prv = (RilClientPrv *)(client->prv); |
| 632 | |
| 633 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 634 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 635 | return RIL_CLIENT_ERR_CONNECT; |
| 636 | } |
| 637 | |
| 638 | if (isValidSoundType(type) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 639 | RLOGE("%s: Invalid sound type", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 640 | return RIL_CLIENT_ERR_INVAL; |
| 641 | } |
| 642 | |
| 643 | // Make raw data |
| 644 | data[0] = OEM_FUNC_SOUND; |
| 645 | data[1] = OEM_SND_SET_VOLUME_CTRL; |
| 646 | data[2] = 0x00; // data length |
| 647 | data[3] = 0x06; // data length |
| 648 | data[4] = ConvertSoundType(type); // volume type |
| 649 | data[5] = vol_level; // volume level |
| 650 | |
| 651 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_VOLUME, NULL); |
| 652 | |
| 653 | ret = SendOemRequestHookRaw(client, REQ_SET_CALL_VOLUME, data, sizeof(data)); |
| 654 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 655 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_VOLUME, NULL); |
| 656 | } |
| 657 | |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /** |
| 663 | * Set external sound device path for noise reduction. |
| 664 | */ |
| 665 | extern "C" |
Android-Andi | 40afadb | 2016-01-07 11:51:38 +0100 | [diff] [blame] | 666 | #ifdef RIL_CALL_AUDIO_PATH_EXTRAVOLUME |
Andreas Schneider | 9427510 | 2015-08-24 19:55:42 +0200 | [diff] [blame] | 667 | int SetCallAudioPath(HRilClient client, AudioPath path, ExtraVolume mode) |
| 668 | #else |
| 669 | int SetCallAudioPath(HRilClient client, AudioPath path) |
| 670 | #endif |
| 671 | { |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 672 | RilClientPrv *client_prv; |
| 673 | int ret; |
| 674 | char data[6] = {0,}; |
| 675 | |
| 676 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 677 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 678 | return RIL_CLIENT_ERR_INVAL; |
| 679 | } |
| 680 | |
| 681 | client_prv = (RilClientPrv *)(client->prv); |
| 682 | |
| 683 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 684 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 685 | return RIL_CLIENT_ERR_CONNECT; |
| 686 | } |
| 687 | |
| 688 | if (isValidAudioPath(path) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 689 | RLOGE("%s: Invalid audio path", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 690 | return RIL_CLIENT_ERR_INVAL; |
| 691 | } |
| 692 | |
| 693 | // Make raw data |
| 694 | data[0] = OEM_FUNC_SOUND; |
| 695 | data[1] = OEM_SND_SET_AUDIO_PATH; |
| 696 | data[2] = 0x00; // data length |
| 697 | data[3] = 0x06; // data length |
| 698 | data[4] = ConvertAudioPath(path); // audio path |
Android-Andi | 40afadb | 2016-01-07 11:51:38 +0100 | [diff] [blame] | 699 | #ifdef RIL_CALL_AUDIO_PATH_EXTRAVOLUME |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 700 | data[5] = mode; // ExtraVolume |
Andreas Schneider | 9427510 | 2015-08-24 19:55:42 +0200 | [diff] [blame] | 701 | #endif |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 702 | |
| 703 | RegisterRequestCompleteHandler(client, REQ_SET_AUDIO_PATH, NULL); |
| 704 | |
| 705 | ret = SendOemRequestHookRaw(client, REQ_SET_AUDIO_PATH, data, sizeof(data)); |
| 706 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 707 | RegisterRequestCompleteHandler(client, REQ_SET_AUDIO_PATH, NULL); |
| 708 | } |
| 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | /** |
| 715 | * Set modem clock to master or slave. |
| 716 | */ |
| 717 | extern "C" |
| 718 | int SetCallClockSync(HRilClient client, SoundClockCondition condition) { |
| 719 | RilClientPrv *client_prv; |
| 720 | int ret; |
| 721 | char data[5] = {0,}; |
| 722 | |
| 723 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 724 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 725 | return RIL_CLIENT_ERR_INVAL; |
| 726 | } |
| 727 | |
| 728 | client_prv = (RilClientPrv *)(client->prv); |
| 729 | |
| 730 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 731 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 732 | return RIL_CLIENT_ERR_CONNECT; |
| 733 | } |
| 734 | |
| 735 | if (isValidSoundClockCondition(condition) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 736 | RLOGE("%s: Invalid sound clock condition", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 737 | return RIL_CLIENT_ERR_INVAL; |
| 738 | } |
| 739 | |
| 740 | // Make raw data |
| 741 | data[0] = OEM_FUNC_SOUND; |
| 742 | data[1] = OEM_SND_SET_CLOCK_CTRL; |
| 743 | data[2] = 0x00; // data length |
| 744 | data[3] = 0x05; // data length |
| 745 | data[4] = condition; |
| 746 | |
| 747 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_CLOCK_SYNC, NULL); |
| 748 | |
| 749 | ret = SendOemRequestHookRaw(client, REQ_SET_CALL_CLOCK_SYNC, data, sizeof(data)); |
| 750 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 751 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_CLOCK_SYNC, NULL); |
| 752 | } |
| 753 | |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Set modem VTCall clock to master or slave. |
| 759 | */ |
| 760 | extern "C" |
| 761 | int SetVideoCallClockSync(HRilClient client, SoundClockCondition condition) { |
| 762 | RilClientPrv *client_prv; |
| 763 | int ret; |
| 764 | char data[5] = {0,}; |
| 765 | |
| 766 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 767 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 768 | return RIL_CLIENT_ERR_INVAL; |
| 769 | } |
| 770 | |
| 771 | client_prv = (RilClientPrv *)(client->prv); |
| 772 | |
| 773 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 774 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 775 | return RIL_CLIENT_ERR_CONNECT; |
| 776 | } |
| 777 | |
| 778 | if (isValidSoundClockCondition(condition) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 779 | RLOGE("%s: Invalid sound clock condition", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 780 | return RIL_CLIENT_ERR_INVAL; |
| 781 | } |
| 782 | |
| 783 | // Make raw data |
| 784 | data[0] = OEM_FUNC_SOUND; |
| 785 | data[1] = OEM_SND_SET_VIDEO_CALL_CTRL; |
| 786 | data[2] = 0x00; // data length |
| 787 | data[3] = 0x05; // data length |
| 788 | data[4] = condition; |
| 789 | |
| 790 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_VT_CTRL, NULL); |
| 791 | |
| 792 | ret = SendOemRequestHookRaw(client, REQ_SET_CALL_VT_CTRL, data, sizeof(data)); |
| 793 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 794 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_VT_CTRL, NULL); |
| 795 | } |
| 796 | |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Set voice recording. |
| 802 | */ |
| 803 | extern "C" |
| 804 | int SetCallRecord(HRilClient client, CallRecCondition condition) { |
| 805 | RilClientPrv *client_prv; |
| 806 | int ret; |
| 807 | char data[5] = {0,}; |
| 808 | |
| 809 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 810 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 811 | return RIL_CLIENT_ERR_INVAL; |
| 812 | } |
| 813 | |
| 814 | client_prv = (RilClientPrv *)(client->prv); |
| 815 | |
| 816 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 817 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 818 | return RIL_CLIENT_ERR_CONNECT; |
| 819 | } |
| 820 | |
| 821 | if (isValidCallRecCondition(condition) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 822 | RLOGE("%s: Invalid sound clock condition", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 823 | return RIL_CLIENT_ERR_INVAL; |
| 824 | } |
| 825 | |
| 826 | // Make raw data |
| 827 | data[0] = OEM_FUNC_SOUND; |
| 828 | data[1] = OEM_SND_SET_VOICE_RECORDING_CTRL; |
| 829 | data[2] = 0x00; // data length |
| 830 | data[3] = 0x05; // data length |
| 831 | data[4] = condition; |
| 832 | |
| 833 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_RECORDING, NULL); |
| 834 | |
| 835 | ret = SendOemRequestHookRaw(client, REQ_SET_CALL_RECORDING, data, sizeof(data)); |
| 836 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 837 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_RECORDING, NULL); |
| 838 | } |
| 839 | |
| 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Set mute or unmute. |
| 845 | */ |
| 846 | extern "C" |
| 847 | int SetMute(HRilClient client, MuteCondition condition) { |
| 848 | RilClientPrv *client_prv; |
| 849 | int ret; |
| 850 | char data[5] = {0,}; |
| 851 | |
| 852 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 853 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 854 | return RIL_CLIENT_ERR_INVAL; |
| 855 | } |
| 856 | |
| 857 | client_prv = (RilClientPrv *)(client->prv); |
| 858 | |
| 859 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 860 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 861 | return RIL_CLIENT_ERR_CONNECT; |
| 862 | } |
| 863 | |
| 864 | if (isValidMuteCondition(condition) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 865 | RLOGE("%s: Invalid sound clock condition", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 866 | return RIL_CLIENT_ERR_INVAL; |
| 867 | } |
| 868 | |
| 869 | // Make raw data |
| 870 | data[0] = OEM_FUNC_SOUND; |
| 871 | data[1] = OEM_SND_SET_MUTE; |
| 872 | data[2] = 0x00; // data length |
| 873 | data[3] = 0x05; // data length |
| 874 | data[4] = condition; |
| 875 | |
| 876 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_MUTE, NULL); |
| 877 | |
| 878 | ret = SendOemRequestHookRaw(client, REQ_SET_CALL_MUTE, data, sizeof(data)); |
| 879 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 880 | RegisterRequestCompleteHandler(client, REQ_SET_CALL_MUTE, NULL); |
| 881 | } |
| 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Get mute state. |
| 888 | */ |
| 889 | extern "C" |
| 890 | int GetMute(HRilClient client, RilOnComplete handler) { |
| 891 | RilClientPrv *client_prv; |
| 892 | int ret; |
| 893 | char data[4] = {0,}; |
| 894 | |
| 895 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 896 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 897 | return RIL_CLIENT_ERR_INVAL; |
| 898 | } |
| 899 | |
| 900 | client_prv = (RilClientPrv *)(client->prv); |
| 901 | |
| 902 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 903 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 904 | return RIL_CLIENT_ERR_CONNECT; |
| 905 | } |
| 906 | |
| 907 | client_prv->b_del_handler = 1; |
| 908 | |
| 909 | // Make raw data |
| 910 | data[0] = OEM_FUNC_SOUND; |
| 911 | data[1] = OEM_SND_GET_MUTE; |
| 912 | data[2] = 0x00; // data length |
| 913 | data[3] = 0x04; // data length |
| 914 | |
| 915 | RegisterRequestCompleteHandler(client, REQ_GET_CALL_MUTE, handler); |
| 916 | |
| 917 | ret = SendOemRequestHookRaw(client, REQ_GET_CALL_MUTE, data, sizeof(data)); |
| 918 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 919 | RegisterRequestCompleteHandler(client, REQ_GET_CALL_MUTE, NULL); |
| 920 | } |
| 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | extern "C" |
| 926 | int SetTwoMicControl(HRilClient client, TwoMicSolDevice device, TwoMicSolReport report) { |
| 927 | RilClientPrv *client_prv; |
| 928 | int ret; |
| 929 | char data[6] = {0,}; |
| 930 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 931 | RLOGV(" + %s", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 932 | |
| 933 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 934 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 935 | return RIL_CLIENT_ERR_INVAL; |
| 936 | } |
| 937 | |
| 938 | client_prv = (RilClientPrv *)(client->prv); |
| 939 | |
| 940 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 941 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 942 | return RIL_CLIENT_ERR_CONNECT; |
| 943 | } |
| 944 | |
| 945 | if (isValidTwoMicCtrl(device, report) == false) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 946 | RLOGE("%s: Invalid sound set two params", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 947 | return RIL_CLIENT_ERR_INVAL; |
| 948 | } |
| 949 | |
| 950 | // Make raw data |
| 951 | data[0] = OEM_FUNC_SOUND; |
| 952 | data[1] = OEM_SND_SET_TWO_MIC_CTL; |
| 953 | data[2] = 0x00; // data length |
| 954 | data[3] = 0x06; // data length |
| 955 | data[4] = device; |
| 956 | data[5] = report; |
| 957 | |
| 958 | RegisterRequestCompleteHandler(client, REQ_SET_TWO_MIC_CTRL, NULL); |
| 959 | |
| 960 | ret = SendOemRequestHookRaw(client, REQ_SET_TWO_MIC_CTRL, data, sizeof(data)); |
| 961 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 962 | RegisterRequestCompleteHandler(client, REQ_SET_TWO_MIC_CTRL, NULL); |
| 963 | } |
| 964 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 965 | RLOGV(" - %s", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 966 | |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | extern "C" |
| 971 | int SetDhaSolution(HRilClient client, DhaSolMode mode, DhaSolSelect select, char *parameter) { |
| 972 | RilClientPrv *client_prv; |
| 973 | int ret; |
| 974 | char data[30] = {0,}; |
| 975 | char tempPara[24]={0,}; |
| 976 | |
| 977 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 978 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 979 | return RIL_CLIENT_ERR_INVAL; |
| 980 | } |
| 981 | |
| 982 | client_prv = (RilClientPrv *)(client->prv); |
| 983 | |
| 984 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 985 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 986 | return RIL_CLIENT_ERR_CONNECT; |
| 987 | } |
| 988 | |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 989 | RLOGE("%s: DHA mode=%d, select=%d", __FUNCTION__,mode, select); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 990 | |
| 991 | // Make raw data |
| 992 | data[0] = OEM_FUNC_SOUND; |
| 993 | data[1] = OEM_SND_SET_DHA_CTL; |
| 994 | data[2] = 0x00; // data length |
| 995 | data[3] = 0x1E; // data length |
| 996 | data[4] = mode; |
| 997 | data[5] = select; |
| 998 | |
| 999 | memcpy(tempPara, parameter, 24); |
| 1000 | for(int i=0; i<24; i++) |
| 1001 | data[6+i]= tempPara[i]; |
| 1002 | |
| 1003 | RegisterRequestCompleteHandler(client, REQ_SET_DHA_CTRL, NULL); |
| 1004 | |
| 1005 | ret = SendOemRequestHookRaw(client, REQ_SET_DHA_CTRL, data, sizeof(data)); |
| 1006 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 1007 | RegisterRequestCompleteHandler(client, REQ_SET_DHA_CTRL, NULL); |
| 1008 | } |
| 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * Set LoopbackTest mode, path. |
| 1015 | */ |
| 1016 | extern "C" |
| 1017 | int SetLoopbackTest(HRilClient client, LoopbackMode mode, AudioPath path) { |
| 1018 | RilClientPrv *client_prv; |
| 1019 | int ret; |
| 1020 | char data[6] = {0,}; |
| 1021 | |
| 1022 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1023 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1024 | return RIL_CLIENT_ERR_INVAL; |
| 1025 | } |
| 1026 | |
| 1027 | client_prv = (RilClientPrv *)(client->prv); |
| 1028 | |
| 1029 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1030 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1031 | return RIL_CLIENT_ERR_CONNECT; |
| 1032 | } |
| 1033 | |
| 1034 | // Make raw data |
| 1035 | data[0] = OEM_FUNC_SOUND; |
| 1036 | data[1] = OEM_SND_SET_LOOPBACK_CTRL; |
| 1037 | data[2] = 0x00; // data length |
| 1038 | data[3] = 0x06; // data length |
| 1039 | data[4] = mode; // Loopback Mode |
| 1040 | data[5] = ConvertAudioPath(path); // Loopback path |
| 1041 | |
| 1042 | RegisterRequestCompleteHandler(client, REQ_SET_LOOPBACK, NULL); |
| 1043 | |
| 1044 | ret = SendOemRequestHookRaw(client, REQ_SET_LOOPBACK, data, sizeof(data)); |
| 1045 | if (ret != RIL_CLIENT_ERR_SUCCESS) { |
| 1046 | RegisterRequestCompleteHandler(client, REQ_SET_LOOPBACK, NULL); |
| 1047 | } |
| 1048 | |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | /** |
| 1054 | * @fn int InvokeOemRequestHookRaw(HRilClient client, char *data, size_t len) |
| 1055 | * |
| 1056 | * @params client: Client handle. |
| 1057 | * data: Request data. |
| 1058 | * len: Request data length. |
| 1059 | * |
| 1060 | * @return 0 for success or error code. On receiving RIL_CLIENT_ERR_AGAIN, |
| 1061 | * caller should retry. |
| 1062 | */ |
| 1063 | extern "C" |
| 1064 | int InvokeOemRequestHookRaw(HRilClient client, char *data, size_t len) { |
| 1065 | RilClientPrv *client_prv; |
| 1066 | |
| 1067 | if (client == NULL || client->prv == NULL) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1068 | RLOGE("%s: Invalid client %p", __FUNCTION__, client); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1069 | return RIL_CLIENT_ERR_INVAL; |
| 1070 | } |
| 1071 | |
| 1072 | client_prv = (RilClientPrv *)(client->prv); |
| 1073 | |
| 1074 | if (client_prv->sock < 0 ) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1075 | RLOGE("%s: Not connected.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1076 | return RIL_CLIENT_ERR_CONNECT; |
| 1077 | } |
| 1078 | |
| 1079 | return SendOemRequestHookRaw(client, REQ_OEM_HOOK_RAW, data, len); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | static int SendOemRequestHookRaw(HRilClient client, int req_id, char *data, size_t len) { |
| 1084 | int token = 0; |
| 1085 | int ret = 0; |
| 1086 | uint32_t header = 0; |
| 1087 | android::Parcel p; |
| 1088 | RilClientPrv *client_prv; |
| 1089 | int maxfd = -1; |
| 1090 | |
Basil Gello | 72aac79 | 2017-07-13 19:29:21 +0300 | [diff] [blame] | 1091 | unsigned int check_req_id = req_id; |
| 1092 | |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1093 | client_prv = (RilClientPrv *)(client->prv); |
| 1094 | |
| 1095 | // Allocate a token. |
| 1096 | token = AllocateToken(&(client_prv->token_pool)); |
| 1097 | if (token == 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1098 | RLOGE("%s: No token.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1099 | return RIL_CLIENT_ERR_AGAIN; |
| 1100 | } |
| 1101 | |
| 1102 | // Record token for the request sent. |
| 1103 | if (RecordReqHistory(client_prv, token, req_id) != RIL_CLIENT_ERR_SUCCESS) { |
| 1104 | goto error; |
| 1105 | } |
| 1106 | |
| 1107 | // Make OEM request data. |
| 1108 | p.writeInt32(RIL_REQUEST_OEM_HOOK_RAW); |
| 1109 | p.writeInt32(token); |
| 1110 | p.writeInt32(len); |
| 1111 | p.write((void *)data, len); |
| 1112 | |
| 1113 | // DO TX: header(size). |
| 1114 | header = htonl(p.dataSize()); |
| 1115 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1116 | RLOGV("%s(): token = %d\n", __FUNCTION__, token); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1117 | |
| 1118 | ret = blockingWrite(client_prv->sock, (void *)&header, sizeof(header)); |
| 1119 | if (ret < 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1120 | RLOGE("%s: send request header failed. (%d)", __FUNCTION__, ret); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1121 | goto error; |
| 1122 | } |
| 1123 | |
| 1124 | // Do TX: response data. |
| 1125 | ret = blockingWrite(client_prv->sock, p.data(), p.dataSize()); |
| 1126 | if (ret < 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1127 | RLOGE("%s: send request data failed. (%d)", __FUNCTION__, ret); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1128 | goto error; |
| 1129 | } |
| 1130 | |
Basil Gello | 72aac79 | 2017-07-13 19:29:21 +0300 | [diff] [blame] | 1131 | // check if the handler for specified event is NULL and deregister token |
| 1132 | // to prevent token pool overflow |
| 1133 | if(!FindReqHandler(client_prv, token, &check_req_id)) { |
| 1134 | FreeToken(&(client_prv->token_pool), token); |
| 1135 | ClearReqHistory(client_prv, token); |
| 1136 | } |
| 1137 | |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1138 | return RIL_CLIENT_ERR_SUCCESS; |
| 1139 | |
| 1140 | error: |
| 1141 | FreeToken(&(client_prv->token_pool), token); |
| 1142 | ClearReqHistory(client_prv, token); |
| 1143 | |
Basil Gello | 2a40024 | 2017-07-18 22:00:01 +0300 | [diff] [blame^] | 1144 | if (ret == -EPIPE || ret == -EBADFD) { |
| 1145 | close(client_prv->sock); |
| 1146 | client_prv->sock = -1; |
| 1147 | client_prv->b_connect = 0; |
| 1148 | } |
| 1149 | |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1150 | return RIL_CLIENT_ERR_UNKNOWN; |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | static bool isValidSoundType(SoundType type) { |
| 1155 | return (type >= SOUND_TYPE_VOICE && type <= SOUND_TYPE_BTVOICE); |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | static bool isValidAudioPath(AudioPath path) { |
Andreas Schneider | e005881 | 2017-02-14 17:24:02 +0100 | [diff] [blame] | 1160 | return (path >= SOUND_AUDIO_PATH_EARPIECE && path <= OEM_SND_AUDIO_PATH_BT_WB_NSEC_OFF); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | static bool isValidSoundClockCondition(SoundClockCondition condition) { |
| 1165 | return (condition >= SOUND_CLOCK_STOP && condition <= SOUND_CLOCK_START); |
| 1166 | } |
| 1167 | |
| 1168 | static bool isValidCallRecCondition(CallRecCondition condition) { |
| 1169 | return (condition >= CALL_REC_STOP && condition <= CALL_REC_START); |
| 1170 | } |
| 1171 | |
| 1172 | static bool isValidMuteCondition(MuteCondition condition) { |
| 1173 | return (condition >= TX_UNMUTE && condition <= RXTX_MUTE); |
| 1174 | } |
| 1175 | |
| 1176 | static bool isValidTwoMicCtrl(TwoMicSolDevice device, TwoMicSolReport report) { |
| 1177 | return (device >= AUDIENCE && device <= FORTEMEDIA && report >= TWO_MIC_SOLUTION_OFF && report <= TWO_MIC_SOLUTION_ON ); |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | static char ConvertSoundType(SoundType type) { |
| 1182 | switch (type) { |
| 1183 | case SOUND_TYPE_VOICE: |
| 1184 | return OEM_SND_TYPE_VOICE; |
| 1185 | case SOUND_TYPE_SPEAKER: |
| 1186 | return OEM_SND_TYPE_SPEAKER; |
| 1187 | case SOUND_TYPE_HEADSET: |
| 1188 | return OEM_SND_TYPE_HEADSET; |
| 1189 | case SOUND_TYPE_BTVOICE: |
| 1190 | return OEM_SND_TYPE_BTVOICE; |
| 1191 | default: |
| 1192 | return OEM_SND_TYPE_VOICE; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | static char ConvertAudioPath(AudioPath path) { |
| 1198 | switch (path) { |
Andreas Schneider | e005881 | 2017-02-14 17:24:02 +0100 | [diff] [blame] | 1199 | case SOUND_AUDIO_PATH_EARPIECE: |
| 1200 | return OEM_SND_AUDIO_PATH_EARPIECE; |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1201 | case SOUND_AUDIO_PATH_HEADSET: |
| 1202 | return OEM_SND_AUDIO_PATH_HEADSET; |
| 1203 | case SOUND_AUDIO_PATH_SPEAKER: |
| 1204 | return OEM_SND_AUDIO_PATH_SPEAKER; |
| 1205 | case SOUND_AUDIO_PATH_BLUETOOTH: |
| 1206 | return OEM_SND_AUDIO_PATH_BLUETOOTH; |
| 1207 | case SOUND_AUDIO_PATH_STEREO_BT: |
| 1208 | return OEM_SND_AUDIO_PATH_STEREO_BLUETOOTH; |
| 1209 | case SOUND_AUDIO_PATH_HEADPHONE: |
| 1210 | return OEM_SND_AUDIO_PATH_HEADPHONE; |
| 1211 | case SOUND_AUDIO_PATH_BLUETOOTH_NO_NR: |
| 1212 | return OEM_SND_AUDIO_PATH_BT_NSEC_OFF; |
| 1213 | case SOUND_AUDIO_PATH_MIC1: |
| 1214 | return OEM_SND_AUDIO_PATH_MIC1; |
| 1215 | case SOUND_AUDIO_PATH_MIC2: |
| 1216 | return OEM_SND_AUDIO_PATH_MIC2; |
| 1217 | case SOUND_AUDIO_PATH_BLUETOOTH_WB: |
| 1218 | return OEM_SND_AUDIO_PATH_BT_WB; |
| 1219 | case SOUND_AUDIO_PATH_BLUETOOTH_WB_NO_NR: |
| 1220 | return OEM_SND_AUDIO_PATH_BT_WB_NSEC_OFF; |
| 1221 | |
| 1222 | default: |
Andreas Schneider | e005881 | 2017-02-14 17:24:02 +0100 | [diff] [blame] | 1223 | return OEM_SND_AUDIO_PATH_EARPIECE; |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | static void * RxReaderFunc(void *param) { |
| 1229 | RilClientPrv *client_prv = (RilClientPrv *)param; |
| 1230 | int maxfd = 0; |
| 1231 | int token = 0; |
| 1232 | void *p_record = NULL; |
| 1233 | size_t recordlen = 0; |
| 1234 | int ret = 0; |
| 1235 | int n; |
| 1236 | |
| 1237 | if (client_prv == NULL) |
| 1238 | return NULL; |
| 1239 | |
| 1240 | maxfd = max(client_prv->sock, client_prv->pipefd[0]) + 1; |
| 1241 | |
| 1242 | printf("[*] %s() b_connect=%d, maxfd=%d\n", __FUNCTION__, client_prv->b_connect, maxfd); |
| 1243 | while (client_prv->b_connect) { |
| 1244 | FD_ZERO(&(client_prv->sock_rfds)); |
| 1245 | |
| 1246 | FD_SET(client_prv->sock, &(client_prv->sock_rfds)); |
| 1247 | FD_SET(client_prv->pipefd[0], &(client_prv->sock_rfds)); |
| 1248 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1249 | RLOGV("[*] %s() b_connect=%d\n", __FUNCTION__, client_prv->b_connect); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1250 | if (select(maxfd, &(client_prv->sock_rfds), NULL, NULL, NULL) > 0) { |
| 1251 | if (FD_ISSET(client_prv->sock, &(client_prv->sock_rfds))) { |
| 1252 | // Read incoming data |
| 1253 | for (;;) { |
| 1254 | // loop until EAGAIN/EINTR, end of stream, or other error |
| 1255 | ret = record_stream_get_next(client_prv->p_rs, &p_record, &recordlen); |
| 1256 | if (ret == 0 && p_record == NULL) { // end-of-stream |
| 1257 | break; |
| 1258 | } |
| 1259 | else if (ret < 0) { |
| 1260 | break; |
| 1261 | } |
| 1262 | else if (ret == 0) { // && p_record != NULL |
| 1263 | n = processRxBuffer(client_prv, p_record, recordlen); |
| 1264 | if (n != RIL_CLIENT_ERR_SUCCESS) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1265 | RLOGE("%s: processRXBuffer returns %d", __FUNCTION__, n); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | else { |
| 1269 | printf("[*] %s()\n", __FUNCTION__); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) { |
| 1274 | // fatal error or end-of-stream |
| 1275 | if (client_prv->sock > 0) { |
| 1276 | close(client_prv->sock); |
| 1277 | client_prv->sock = -1; |
| 1278 | client_prv->b_connect = 0; |
| 1279 | } |
| 1280 | |
| 1281 | if (client_prv->p_rs) |
| 1282 | record_stream_free(client_prv->p_rs); |
| 1283 | |
| 1284 | // EOS |
| 1285 | if (client_prv->err_cb) { |
| 1286 | client_prv->err_cb(client_prv->err_cb_data, RIL_CLIENT_ERR_CONNECT); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
| 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | if (FD_ISSET(client_prv->pipefd[0], &(client_prv->sock_rfds))) { |
| 1294 | char end_cmd[10]; |
| 1295 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1296 | RLOGV("%s(): close\n", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1297 | |
| 1298 | if (read(client_prv->pipefd[0], end_cmd, sizeof(end_cmd)) > 0) { |
| 1299 | close(client_prv->sock); |
| 1300 | close(client_prv->pipefd[0]); |
| 1301 | close(client_prv->pipefd[1]); |
| 1302 | |
| 1303 | client_prv->sock = -1; |
| 1304 | client_prv->b_connect = 0; |
| 1305 | } |
| 1306 | } |
Basil Gello | 2a40024 | 2017-07-18 22:00:01 +0300 | [diff] [blame^] | 1307 | } else { |
| 1308 | RLOGE("%s: select() returned %d\n", __FUNCTION__, -errno); |
| 1309 | |
| 1310 | if (client_prv->sock > 0) { |
| 1311 | close(client_prv->sock); |
| 1312 | client_prv->sock = -1; |
| 1313 | client_prv->b_connect = 0; |
| 1314 | } |
| 1315 | |
| 1316 | if (client_prv->p_rs) |
| 1317 | record_stream_free(client_prv->p_rs); |
| 1318 | |
| 1319 | // EOS |
| 1320 | if (client_prv->err_cb) { |
| 1321 | client_prv->err_cb(client_prv->err_cb_data, RIL_CLIENT_ERR_CONNECT); |
| 1322 | return NULL; |
| 1323 | } |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | return NULL; |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | static int processUnsolicited(RilClientPrv *prv, Parcel &p) { |
| 1332 | int32_t resp_id, len; |
| 1333 | status_t status; |
| 1334 | const void *data = NULL; |
| 1335 | RilOnUnsolicited unsol_func = NULL; |
| 1336 | |
| 1337 | status = p.readInt32(&resp_id); |
| 1338 | if (status != NO_ERROR) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1339 | RLOGE("%s: read resp_id failed.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1340 | return RIL_CLIENT_ERR_IO; |
| 1341 | } |
| 1342 | |
| 1343 | status = p.readInt32(&len); |
| 1344 | if (status != NO_ERROR) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1345 | //RLOGE("%s: read length failed. assume zero length.", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1346 | len = 0; |
| 1347 | } |
| 1348 | |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1349 | RLOGD("%s(): resp_id (%d), len(%d)\n", __FUNCTION__, resp_id, len); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1350 | |
| 1351 | if (len) |
| 1352 | data = p.readInplace(len); |
| 1353 | |
| 1354 | // Find unsolicited response handler. |
| 1355 | unsol_func = FindUnsolHandler(prv, (uint32_t)resp_id); |
| 1356 | if (unsol_func) { |
| 1357 | unsol_func(prv->parent, data, len); |
| 1358 | } |
| 1359 | |
| 1360 | return RIL_CLIENT_ERR_SUCCESS; |
| 1361 | } |
| 1362 | |
| 1363 | |
| 1364 | static int processSolicited(RilClientPrv *prv, Parcel &p) { |
| 1365 | int32_t token, err, len; |
| 1366 | status_t status; |
| 1367 | const void *data = NULL; |
| 1368 | RilOnComplete req_func = NULL; |
| 1369 | int ret = RIL_CLIENT_ERR_SUCCESS; |
| 1370 | uint32_t req_id = 0; |
| 1371 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1372 | RLOGV("%s()", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1373 | |
| 1374 | status = p.readInt32(&token); |
| 1375 | if (status != NO_ERROR) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1376 | RLOGE("%s: Read token fail. Status %d\n", __FUNCTION__, status); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1377 | return RIL_CLIENT_ERR_IO; |
| 1378 | } |
| 1379 | |
| 1380 | if (IsValidToken(&(prv->token_pool), token) == 0) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1381 | RLOGE("%s: Invalid Token", __FUNCTION__); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1382 | return RIL_CLIENT_ERR_INVAL; // Invalid token. |
| 1383 | } |
| 1384 | |
| 1385 | status = p.readInt32(&err); |
| 1386 | if (status != NO_ERROR) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1387 | RLOGE("%s: Read err fail. Status %d\n", __FUNCTION__, status); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1388 | ret = RIL_CLIENT_ERR_IO; |
| 1389 | goto error; |
| 1390 | } |
| 1391 | |
| 1392 | // Don't go further for error response. |
| 1393 | if (err != RIL_CLIENT_ERR_SUCCESS) { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1394 | RLOGE("%s: Error %d\n", __FUNCTION__, err); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1395 | if (prv->err_cb) |
| 1396 | prv->err_cb(prv->err_cb_data, err); |
| 1397 | ret = RIL_CLIENT_ERR_SUCCESS; |
| 1398 | goto error; |
| 1399 | } |
| 1400 | |
| 1401 | status = p.readInt32(&len); |
| 1402 | if (status != NO_ERROR) { |
| 1403 | /* no length field */ |
| 1404 | len = 0; |
| 1405 | } |
| 1406 | |
| 1407 | if (len) |
| 1408 | data = p.readInplace(len); |
| 1409 | |
| 1410 | // Find request handler for the token. |
| 1411 | // First, FindReqHandler() searches request history with the token |
| 1412 | // and finds out a request ID. Then, it search request handler table |
| 1413 | // with the request ID. |
| 1414 | req_func = FindReqHandler(prv, token, &req_id); |
| 1415 | if (req_func) |
| 1416 | { |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1417 | RLOGV("[*] Call handler"); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1418 | req_func(prv->parent, data, len); |
| 1419 | |
| 1420 | if(prv->b_del_handler) { |
| 1421 | prv->b_del_handler = 0; |
| 1422 | RegisterRequestCompleteHandler(prv->parent, req_id, NULL); |
| 1423 | } |
| 1424 | } else { |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1425 | RLOGV("%s: No handler for token %d\n", __FUNCTION__, token); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | error: |
| 1429 | FreeToken(&(prv->token_pool), token); |
| 1430 | ClearReqHistory(prv, token); |
| 1431 | return ret; |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | static int processRxBuffer(RilClientPrv *prv, void *buffer, size_t buflen) { |
| 1436 | Parcel p; |
| 1437 | int32_t response_type; |
| 1438 | status_t status; |
| 1439 | int ret = RIL_CLIENT_ERR_SUCCESS; |
| 1440 | |
| 1441 | acquire_wake_lock(PARTIAL_WAKE_LOCK, RIL_CLIENT_WAKE_LOCK); |
| 1442 | |
| 1443 | p.setData((uint8_t *)buffer, buflen); |
| 1444 | |
| 1445 | status = p.readInt32(&response_type); |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1446 | RLOGV("%s: status %d response_type %d", __FUNCTION__, status, response_type); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1447 | |
| 1448 | if (status != NO_ERROR) { |
| 1449 | ret = RIL_CLIENT_ERR_IO; |
| 1450 | goto EXIT; |
| 1451 | } |
| 1452 | |
| 1453 | // FOr unsolicited response. |
| 1454 | if (response_type == RESPONSE_UNSOLICITED) { |
| 1455 | ret = processUnsolicited(prv, p); |
| 1456 | } |
| 1457 | // For solicited response. |
| 1458 | else if (response_type == RESPONSE_SOLICITED) { |
| 1459 | ret = processSolicited(prv, p); |
| 1460 | if (ret != RIL_CLIENT_ERR_SUCCESS && prv->err_cb) { |
| 1461 | prv->err_cb(prv->err_cb_data, ret); |
| 1462 | } |
| 1463 | } |
| 1464 | else { |
| 1465 | ret = RIL_CLIENT_ERR_INVAL; |
| 1466 | } |
| 1467 | |
| 1468 | EXIT: |
| 1469 | release_wake_lock(RIL_CLIENT_WAKE_LOCK); |
| 1470 | return ret; |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | static uint32_t AllocateToken(uint32_t *token_pool) { |
| 1475 | int i; |
| 1476 | |
| 1477 | // Token pool is full. |
| 1478 | if (*token_pool == 0xFFFFFFFF) |
| 1479 | return 0; |
| 1480 | |
| 1481 | for (i = 0; i < 32; i++) { |
| 1482 | uint32_t new_token = 0x00000001 << i; |
| 1483 | |
| 1484 | if ((*token_pool & new_token) == 0) { |
| 1485 | *token_pool |= new_token; |
| 1486 | return new_token; |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | static void FreeToken(uint32_t *token_pool, uint32_t token) { |
| 1495 | *token_pool &= ~token; |
| 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | static uint8_t IsValidToken(uint32_t *token_pool, uint32_t token) { |
| 1500 | if (token == 0) |
| 1501 | return 0; |
| 1502 | |
| 1503 | if ((*token_pool & token) == token) |
| 1504 | return 1; |
| 1505 | else |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | static int RecordReqHistory(RilClientPrv *prv, int token, uint32_t id) { |
| 1511 | int i = 0; |
| 1512 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1513 | RLOGV("[*] %s(): token(%d), ID(%d)\n", __FUNCTION__, token, id); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1514 | for (i = 0; i < TOKEN_POOL_SIZE; i++) { |
| 1515 | if (prv->history[i].token == 0) { |
| 1516 | prv->history[i].token = token; |
| 1517 | prv->history[i].id = id; |
| 1518 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1519 | RLOGV("[*] %s(): token(%d), ID(%d)\n", __FUNCTION__, token, id); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1520 | |
| 1521 | return RIL_CLIENT_ERR_SUCCESS; |
| 1522 | } |
| 1523 | } |
| 1524 | |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1525 | RLOGE("%s: No free record for token %d", __FUNCTION__, token); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1526 | |
| 1527 | return RIL_CLIENT_ERR_RESOURCE; |
| 1528 | } |
| 1529 | |
| 1530 | static void ClearReqHistory(RilClientPrv *prv, int token) { |
| 1531 | int i = 0; |
| 1532 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1533 | RLOGV("[*] %s(): token(%d)\n", __FUNCTION__, token); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1534 | for (i = 0; i < TOKEN_POOL_SIZE; i++) { |
| 1535 | if (prv->history[i].token == token) { |
| 1536 | memset(&(prv->history[i]), 0, sizeof(ReqHistory)); |
| 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | static RilOnUnsolicited FindUnsolHandler(RilClientPrv *prv, uint32_t id) { |
| 1544 | int i; |
| 1545 | |
| 1546 | // Search unsolicited handler table. |
| 1547 | for (i = 0; i < REQ_POOL_SIZE; i++) { |
| 1548 | if (prv->unsol_handlers[i].id == id) |
| 1549 | return prv->unsol_handlers[i].handler; |
| 1550 | } |
| 1551 | |
| 1552 | return (RilOnUnsolicited)NULL; |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | static RilOnComplete FindReqHandler(RilClientPrv *prv, int token, uint32_t *id) { |
| 1557 | int i = 0; |
| 1558 | int j = 0; |
| 1559 | |
Andreas Schneider | 928ddca | 2017-02-08 16:52:54 +0100 | [diff] [blame] | 1560 | RLOGV("[*] %s(): token(%d)\n", __FUNCTION__, token); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1561 | |
| 1562 | // Search request history. |
| 1563 | for (i = 0; i < TOKEN_POOL_SIZE; i++) { |
| 1564 | printf("[*] %s(): history_token(%d)\n", __FUNCTION__, prv->history[i].token); |
| 1565 | if (prv->history[i].token == token) { |
| 1566 | // Search request handler with request ID found. |
| 1567 | for (j = 0; j < REQ_POOL_SIZE; j++) { |
| 1568 | printf("[*] %s(): token(%d), req_id(%d), history_id(%d)\n", __FUNCTION__, token, prv->history[i].id, prv->history[i].id); |
| 1569 | if (prv->req_handlers[j].id == prv->history[i].id) { |
| 1570 | *id = prv->req_handlers[j].id; |
| 1571 | return prv->req_handlers[j].handler; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return NULL; |
| 1578 | } |
| 1579 | |
| 1580 | |
| 1581 | static void DeallocateToken(uint32_t *token_pool, uint32_t token) { |
| 1582 | *token_pool &= !token; |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | static int blockingWrite(int fd, const void *buffer, size_t len) { |
| 1587 | size_t writeOffset = 0; |
| 1588 | const uint8_t *toWrite; |
| 1589 | ssize_t written = 0; |
| 1590 | |
| 1591 | if (buffer == NULL) |
| 1592 | return -1; |
| 1593 | |
| 1594 | toWrite = (const uint8_t *)buffer; |
| 1595 | |
| 1596 | while (writeOffset < len) { |
| 1597 | do |
| 1598 | { |
| 1599 | written = write(fd, toWrite + writeOffset, len - writeOffset); |
| 1600 | } while (written < 0 && errno == EINTR); |
| 1601 | |
| 1602 | if (written >= 0) { |
| 1603 | writeOffset += written; |
| 1604 | } |
| 1605 | else { |
Andreas Schneider | 560e37a | 2017-02-06 18:19:39 +0100 | [diff] [blame] | 1606 | RLOGE ("RIL Response: unexpected error on write errno:%d", errno); |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1607 | close(fd); |
Basil Gello | 338942b | 2017-07-13 19:36:39 +0300 | [diff] [blame] | 1608 | return -errno; |
codeworkx | f1587a3 | 2012-08-03 23:32:29 +0200 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | } // namespace android |
| 1616 | |
| 1617 | // end of file |
| 1618 | |