1 /*
2 * Copyright (C) 2017 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 #pragma once
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/epoll.h>
21
22 #include <hardware/gps.h>
23
24 #define GPS_DEBUG 0
25 #define GPS_DATA_BUFFER_MAX_SIZE 256
26
27 #define DEFAULT_GPS_LOCATION_FLAG \
28 (GPS_LOCATION_HAS_LAT_LONG | GPS_LOCATION_HAS_ALTITUDE | \
29 GPS_LOCATION_HAS_BEARING | GPS_LOCATION_HAS_SPEED | \
30 GPS_LOCATION_HAS_ACCURACY)
31
32 #if GPS_DEBUG
33 #define D(...) ALOGD(__VA_ARGS__)
34 #else
35 #define D(...) ((void)0)
36 #endif
37
38 // Control commands to GPS thread
39 enum { CMD_QUIT = 0, CMD_START = 1, CMD_STOP = 2 };
40
41 // GPS HAL's state
42 typedef struct {
43 int init;
44 int fd;
45 int control[2];
46 pthread_t thread;
47 GpsCallbacks callbacks;
48 } GpsState;
49
50 typedef struct {
51 GpsLocation fix;
52 gps_location_callback callback;
53 char buffer[GPS_DATA_BUFFER_MAX_SIZE + 1];
54 int index;
55 } GpsDataReader;
56
57 void gps_state_thread(void* arg);
58
epoll_register(int epoll_fd,int fd)59 static inline int epoll_register(int epoll_fd, int fd) {
60 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
61
62 struct epoll_event ev;
63 ev.events = EPOLLIN;
64 ev.data.fd = fd;
65
66 int ret;
67 do {
68 ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
69 } while (ret < 0 && errno == EINTR);
70 return ret;
71 }
72
epoll_deregister(int epoll_fd,int fd)73 static inline int epoll_deregister(int epoll_fd, int fd) {
74 int ret;
75 do {
76 ret = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, NULL);
77 } while (ret < 0 && errno == EINTR);
78 return ret;
79 }
80