| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | extern "C" | 
|  | 18 | void *ril_socket_process_requests_loop(void *arg); | 
|  | 19 |  | 
|  | 20 | #include "RilSocket.h" | 
|  | 21 | #include <cutils/sockets.h> | 
|  | 22 | #include <log/mtk_log.h> | 
|  | 23 | #include <assert.h> | 
|  | 24 | #include <unistd.h> | 
|  | 25 | #define SOCKET_LISTEN_BACKLOG 0 | 
|  | 26 |  | 
|  | 27 | int RilSocket::socketInit(void) { | 
|  | 28 | int ret; | 
|  | 29 |  | 
|  | 30 | listenCb = &RilSocket::sSocketListener; | 
|  | 31 | commandCb = &RilSocket::sSocketRequestsHandler; | 
|  | 32 | listenFd = android_get_control_socket(name); | 
|  | 33 |  | 
|  | 34 | //Start listening | 
|  | 35 | ret = listen(listenFd, SOCKET_LISTEN_BACKLOG); | 
|  | 36 |  | 
|  | 37 | if (ret < 0) { | 
|  | 38 | RLOGE("Failed to listen on %s socket '%d': %s", | 
|  | 39 | name, listenFd, strerror(errno)); | 
|  | 40 | return ret; | 
|  | 41 | } | 
|  | 42 | //Add listen event to the event loop | 
|  | 43 | ril_event_set(&listenEvent, listenFd, false, listenCb, this); | 
|  | 44 | rilEventAddWakeup_helper(&listenEvent); | 
|  | 45 | return ret; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | void RilSocket::sSocketListener(int fd, short flags, void *param) { | 
|  | 49 | RilSocket *theSocket = (RilSocket *) param; | 
|  | 50 | MySocketListenParam listenParam; | 
|  | 51 | listenParam.socket = theSocket; | 
|  | 52 | listenParam.sListenParam.type = RIL_SAP_SOCKET; | 
|  | 53 |  | 
|  | 54 | listenCallback_helper(fd, flags, (void*)&listenParam); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | void RilSocket::onNewCommandConnect() { | 
|  | 58 | pthread_attr_t attr; | 
|  | 59 | PthreadPtr pptr = ril_socket_process_requests_loop; | 
|  | 60 | int result; | 
|  | 61 |  | 
|  | 62 | pthread_attr_init(&attr); | 
|  | 63 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 64 |  | 
|  | 65 | //Start socket request processing loop thread | 
|  | 66 | result = pthread_create(&socketThreadId, &attr, pptr, this); | 
|  | 67 | if(result < 0) { | 
|  | 68 | RLOGE("pthread_create failed with result:%d",result); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | RLOGE("New socket command connected and socket request thread started"); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | void RilSocket::sSocketRequestsHandler(int fd, short flags, void *param) { | 
|  | 75 | socketClient *sc = (socketClient *) param; | 
|  | 76 | RilSocket *theSocket = sc->socketPtr; | 
|  | 77 | RecordStream *rs = sc->rs; | 
|  | 78 |  | 
|  | 79 | theSocket->socketRequestsHandler(fd, flags, rs); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | void RilSocket::socketRequestsHandler(int fd, short flags, RecordStream *p_rs) { | 
|  | 83 | int ret; | 
|  | 84 | assert(fd == commandFd); | 
|  | 85 | void *p_record; | 
|  | 86 | size_t recordlen; | 
|  | 87 |  | 
|  | 88 | for (;;) { | 
|  | 89 | /* loop until EAGAIN/EINTR, end of stream, or other error */ | 
|  | 90 | ret = record_stream_get_next(p_rs, &p_record, &recordlen); | 
|  | 91 |  | 
|  | 92 | if (ret == 0 && p_record == NULL) { | 
|  | 93 | /* end-of-stream */ | 
|  | 94 | break; | 
|  | 95 | } else if (ret < 0) { | 
|  | 96 | break; | 
|  | 97 | } else if (ret == 0) { | 
|  | 98 | pushRecord(p_record, recordlen); | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) { | 
|  | 103 | /* fatal error or end-of-stream */ | 
|  | 104 | if (ret != 0) { | 
|  | 105 | RLOGE("error on reading command socket errno:%d\n", errno); | 
|  | 106 | } else { | 
|  | 107 | RLOGW("EOS.  Closing command socket."); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | close(commandFd); | 
|  | 111 | commandFd = -1; | 
|  | 112 |  | 
|  | 113 | ril_event_del(&callbackEvent); | 
|  | 114 |  | 
|  | 115 | record_stream_free(p_rs); | 
|  | 116 |  | 
|  | 117 | /* start listening for new connections again */ | 
|  | 118 |  | 
|  | 119 | rilEventAddWakeup_helper(&listenEvent); | 
|  | 120 |  | 
|  | 121 | onCommandsSocketClosed(); | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | void RilSocket::setListenFd(int fd) { | 
|  | 126 | listenFd = fd; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | void RilSocket::setCommandFd(int fd) { | 
|  | 130 | commandFd = fd; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | int RilSocket::getListenFd(void) { | 
|  | 134 | return listenFd; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | int RilSocket::getCommandFd(void) { | 
|  | 138 | return commandFd; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | void RilSocket::setListenCb(ril_event_cb cb) { | 
|  | 142 | listenCb = cb; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | void RilSocket::setCommandCb(ril_event_cb cb) { | 
|  | 146 | commandCb = cb; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | ril_event_cb RilSocket::getListenCb(void) { | 
|  | 150 | return listenCb; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | ril_event_cb RilSocket::getCommandCb(void) { | 
|  | 154 | return commandCb; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | void RilSocket::setListenEvent(ril_event event) { | 
|  | 158 | listenEvent = event; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | void RilSocket::setCallbackEvent(ril_event event) { | 
|  | 162 | callbackEvent = event; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | ril_event* RilSocket::getListenEvent(void)  { | 
|  | 166 | return &listenEvent; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | ril_event* RilSocket::getCallbackEvent(void) { | 
|  | 170 | return &callbackEvent; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | extern "C" | 
|  | 174 | void *ril_socket_process_requests_loop(void *arg) { | 
|  | 175 | RilSocket *socket = (RilSocket *)arg; | 
|  | 176 | socket->processRequestsLoop(); | 
|  | 177 | return NULL; | 
|  | 178 | } |