2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 void *ril_socket_process_requests_loop(void *arg);
20 #include "RilSocket.h"
21 #include <cutils/sockets.h>
22 #include <utils/Log.h>
24 #define SOCKET_LISTEN_BACKLOG 0
26 int RilSocket::socketInit(void) {
29 listenCb = &RilSocket::sSocketListener;
30 commandCb = &RilSocket::sSocketRequestsHandler;
31 listenFd = android_get_control_socket(name);
34 ret = listen(listenFd, SOCKET_LISTEN_BACKLOG);
37 RLOGE("Failed to listen on %s socket '%d': %s",
38 name, listenFd, strerror(errno));
41 //Add listen event to the event loop
42 ril_event_set(&listenEvent, listenFd, false, listenCb, this);
43 rilEventAddWakeup_helper(&listenEvent);
47 void RilSocket::sSocketListener(int fd, short flags, void *param) {
48 RilSocket *theSocket = (RilSocket *) param;
49 MySocketListenParam listenParam;
50 listenParam.socket = theSocket;
51 listenParam.sListenParam.type = RIL_SAP_SOCKET;
53 listenCallback_helper(fd, flags, (void*)&listenParam);
56 void RilSocket::onNewCommandConnect() {
58 PthreadPtr pptr = ril_socket_process_requests_loop;
61 pthread_attr_init(&attr);
62 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
64 //Start socket request processing loop thread
65 result = pthread_create(&socketThreadId, &attr, pptr, this);
67 RLOGE("pthread_create failed with result:%d",result);
70 RLOGE("New socket command connected and socket request thread started");
73 void RilSocket::sSocketRequestsHandler(int fd, short flags, void *param) {
74 socketClient *sc = (socketClient *) param;
75 RilSocket *theSocket = sc->socketPtr;
76 RecordStream *rs = sc->rs;
78 theSocket->socketRequestsHandler(fd, flags, rs);
81 void RilSocket::socketRequestsHandler(int fd, short flags, RecordStream *p_rs) {
83 assert(fd == commandFd);
88 /* loop until EAGAIN/EINTR, end of stream, or other error */
89 ret = record_stream_get_next(p_rs, &p_record, &recordlen);
91 if (ret == 0 && p_record == NULL) {
96 } else if (ret == 0) {
97 pushRecord(p_record, recordlen);
101 if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) {
102 /* fatal error or end-of-stream */
104 RLOGE("error on reading command socket errno:%d\n", errno);
106 RLOGW("EOS. Closing command socket.");
112 ril_event_del(&callbackEvent);
114 record_stream_free(p_rs);
116 /* start listening for new connections again */
118 rilEventAddWakeup_helper(&listenEvent);
120 onCommandsSocketClosed();
124 void RilSocket::setListenFd(int fd) {
128 void RilSocket::setCommandFd(int fd) {
132 int RilSocket::getListenFd(void) {
136 int RilSocket::getCommandFd(void) {
140 void RilSocket::setListenCb(ril_event_cb cb) {
144 void RilSocket::setCommandCb(ril_event_cb cb) {
148 ril_event_cb RilSocket::getListenCb(void) {
152 ril_event_cb RilSocket::getCommandCb(void) {
156 void RilSocket::setListenEvent(ril_event event) {
160 void RilSocket::setCallbackEvent(ril_event event) {
161 callbackEvent = event;
164 ril_event* RilSocket::getListenEvent(void) {
168 ril_event* RilSocket::getCallbackEvent(void) {
169 return &callbackEvent;
173 void *ril_socket_process_requests_loop(void *arg) {
174 RilSocket *socket = (RilSocket *)arg;
175 socket->processRequestsLoop();