1 /*
2  * Copyright (C) 2020 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 #include <log/log.h>
18 #include <fcntl.h>
19 #include <qemud.h>
20 #include <qemu_pipe_bp.h>
21 #include <sys/epoll.h>
22 #include <sys/socket.h>
23 #include "gnss_hw_conn.h"
24 #include "gnss_hw_listener.h"
25 
26 namespace {
27 constexpr char kCMD_QUIT = 'q';
28 constexpr char kCMD_START = 'a';
29 constexpr char kCMD_STOP = 'o';
30 
epollCtlAdd(int epollFd,int fd)31 int epollCtlAdd(int epollFd, int fd) {
32     int ret;
33 
34     /* make the fd non-blocking */
35     ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
36     if (ret < 0) {
37         return ret;
38     }
39     ret = TEMP_FAILURE_RETRY(fcntl(fd, F_SETFL, ret | O_NONBLOCK));
40     if (ret < 0) {
41         return ret;
42     }
43 
44     struct epoll_event ev;
45     ev.events  = EPOLLIN;
46     ev.data.fd = fd;
47 
48     return TEMP_FAILURE_RETRY(epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &ev));
49 }
50 }  // namespace
51 
52 namespace goldfish {
53 
GnssHwConn(const DataSink * sink)54 GnssHwConn::GnssHwConn(const DataSink* sink) {
55     m_devFd.reset(qemu_pipe_open_ns("qemud", "gps", O_RDWR));
56     if (!m_devFd.ok()) {
57         ALOGE("%s:%d: qemu_pipe_open_ns failed", __PRETTY_FUNCTION__, __LINE__);
58         return;
59     }
60 
61     if (!::android::base::Socketpair(AF_LOCAL, SOCK_STREAM, 0,
62                                      &m_callersFd, &m_threadsFd)) {
63         ALOGE("%s:%d: Socketpair failed", __PRETTY_FUNCTION__, __LINE__);
64         m_devFd.reset();
65         return;
66     }
67 
68     m_thread = std::thread([this, sink]() {
69         sink->gnssStatus(ahg10::IGnssCallback::GnssStatusValue::ENGINE_ON);
70         workerThread(m_devFd.get(), m_threadsFd.get(), sink);
71         sink->gnssStatus(ahg10::IGnssCallback::GnssStatusValue::ENGINE_OFF);
72     });
73 }
74 
~GnssHwConn()75 GnssHwConn::~GnssHwConn() {
76     if (m_thread.joinable()) {
77         sendWorkerThreadCommand(kCMD_QUIT);
78         m_thread.join();
79     }
80 }
81 
ok() const82 bool GnssHwConn::ok() const {
83     return m_thread.joinable();
84 }
85 
start()86 bool GnssHwConn::start() {
87     return ok() && sendWorkerThreadCommand(kCMD_START);
88 }
89 
stop()90 bool GnssHwConn::stop() {
91     return ok() && sendWorkerThreadCommand(kCMD_STOP);
92 }
93 
workerThread(int devFd,int threadsFd,const DataSink * sink)94 void GnssHwConn::workerThread(int devFd, int threadsFd, const DataSink* sink) {
95     const unique_fd epollFd(epoll_create1(0));
96     if (!epollFd.ok()) {
97         ALOGE("%s:%d: epoll_create1 failed", __PRETTY_FUNCTION__, __LINE__);
98         ::abort();
99     }
100 
101     epollCtlAdd(epollFd.get(), devFd);
102     epollCtlAdd(epollFd.get(), threadsFd);
103 
104     GnssHwListener listener(sink);
105     bool running = false;
106 
107     while (true) {
108         struct epoll_event events[2];
109         const int kTimeoutMs = 60000;
110         const int n = TEMP_FAILURE_RETRY(epoll_wait(epollFd.get(),
111                                                     events, 2,
112                                                     kTimeoutMs));
113         if (n < 0) {
114             ALOGE("%s:%d: epoll_wait failed with '%s'",
115                   __PRETTY_FUNCTION__, __LINE__, strerror(errno));
116             continue;
117         }
118 
119         for (int i = 0; i < n; ++i) {
120             const struct epoll_event* ev = &events[i];
121             const int fd = ev->data.fd;
122             const int ev_events = ev->events;
123 
124             if (fd == devFd) {
125                 if (ev_events & (EPOLLERR | EPOLLHUP)) {
126                     ALOGE("%s:%d: epoll_wait: devFd has an error, ev_events=%x",
127                           __PRETTY_FUNCTION__, __LINE__, ev_events);
128                     ::abort();
129                 } else if (ev_events & EPOLLIN) {
130                     char buf[64];
131                     while (true) {
132                         int n = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf)));
133                         if (n > 0) {
134                             if (running) {
135                                 for (int i = 0; i < n; ++i) {
136                                     listener.consume(buf[i]);
137                                 }
138                             }
139                         } else {
140                             break;
141                         }
142                     }
143                 }
144             } else if (fd == threadsFd) {
145                 if (ev_events & (EPOLLERR | EPOLLHUP)) {
146                     ALOGE("%s:%d: epoll_wait: threadsFd has an error, ev_events=%x",
147                           __PRETTY_FUNCTION__, __LINE__, ev_events);
148                     ::abort();
149                 } else if (ev_events & EPOLLIN) {
150                     const int cmd = workerThreadRcvCommand(fd);
151                     switch (cmd) {
152                         case kCMD_QUIT:
153                             return;
154 
155                         case kCMD_START:
156                             if (!running) {
157                                 listener.reset();
158                                 sink->gnssStatus(ahg10::IGnssCallback::GnssStatusValue::SESSION_BEGIN);
159                                 running = true;
160                             }
161                             break;
162 
163                         case kCMD_STOP:
164                             if (running) {
165                                 running = false;
166                                 sink->gnssStatus(ahg10::IGnssCallback::GnssStatusValue::SESSION_END);
167                             }
168                             break;
169 
170                         default:
171                             ALOGE("%s:%d: workerThreadRcvCommand returned unexpected command, cmd=%d",
172                                   __PRETTY_FUNCTION__, __LINE__, cmd);
173                             ::abort();
174                             break;
175                     }
176                 }
177             } else {
178                 ALOGE("%s:%d: epoll_wait() returned unexpected fd",
179                       __PRETTY_FUNCTION__, __LINE__);
180             }
181         }
182     }
183 }
184 
workerThreadRcvCommand(const int fd)185 int GnssHwConn::workerThreadRcvCommand(const int fd) {
186     char buf;
187     if (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) {
188         return buf;
189     } else {
190         return -1;
191     }
192 }
193 
sendWorkerThreadCommand(char cmd) const194 bool GnssHwConn::sendWorkerThreadCommand(char cmd) const {
195     return TEMP_FAILURE_RETRY(write(m_callersFd.get(), &cmd, 1)) == 1;
196 }
197 
198 }  // namespace goldfish
199