1 /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef GPS_EXTENDED_C_H
30 #define GPS_EXTENDED_C_H
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35 
36 #include <ctype.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <hardware/gps.h>
41 
42 /** Location has valid source information. */
43 #define LOCATION_HAS_SOURCE_INFO   0x0020
44 /** GpsLocation has valid "is indoor?" flag */
45 #define GPS_LOCATION_HAS_IS_INDOOR   0x0040
46 /** GpsLocation has valid floor number */
47 #define GPS_LOCATION_HAS_FLOOR_NUMBER   0x0080
48 /** GpsLocation has valid map URL*/
49 #define GPS_LOCATION_HAS_MAP_URL   0x0100
50 /** GpsLocation has valid map index */
51 #define GPS_LOCATION_HAS_MAP_INDEX   0x0200
52 
53 /** Sizes for indoor fields */
54 #define GPS_LOCATION_MAP_URL_SIZE 400
55 #define GPS_LOCATION_MAP_INDEX_SIZE 16
56 
57 /** Position source is ULP */
58 #define ULP_LOCATION_IS_FROM_HYBRID   0x0001
59 /** Position source is GNSS only */
60 #define ULP_LOCATION_IS_FROM_GNSS     0x0002
61 /** Position source is ZPP only */
62 #define ULP_LOCATION_IS_FROM_ZPP      0x0004
63 /** Position is from a Geofence Breach Event */
64 #define ULP_LOCATION_IS_FROM_GEOFENCE 0X0008
65 /** Position is from Hardware FLP */
66 #define ULP_LOCATION_IS_FROM_HW_FLP   0x0010
67 /** Position is from NLP */
68 #define ULP_LOCATION_IS_FROM_NLP      0x0020
69 /** Position is from PIP */
70 #define ULP_LOCATION_IS_FROM_PIP      0x0040
71 
72 #define ULP_MIN_INTERVAL_INVALID 0xffffffff
73 
74 /*Emergency SUPL*/
75 #define GPS_NI_TYPE_EMERGENCY_SUPL    4
76 
77 #define AGPS_CERTIFICATE_MAX_LENGTH 2000
78 #define AGPS_CERTIFICATE_MAX_SLOTS 10
79 
80 enum loc_registration_mask_status {
81     LOC_REGISTRATION_MASK_ENABLED,
82     LOC_REGISTRATION_MASK_DISABLED
83 };
84 
85 typedef struct {
86     /** set to sizeof(UlpLocation) */
87     size_t          size;
88     GpsLocation     gpsLocation;
89     /* Provider indicator for HYBRID or GPS */
90     uint16_t        position_source;
91     /*allows HAL to pass additional information related to the location */
92     int             rawDataSize;         /* in # of bytes */
93     void            * rawData;
94     bool            is_indoor;
95     float           floor_number;
96     char            map_url[GPS_LOCATION_MAP_URL_SIZE];
97     unsigned char   map_index[GPS_LOCATION_MAP_INDEX_SIZE];
98 } UlpLocation;
99 
100 /** AGPS type */
101 typedef int16_t AGpsExtType;
102 #define AGPS_TYPE_INVALID       -1
103 #define AGPS_TYPE_ANY           0
104 #define AGPS_TYPE_SUPL          1
105 #define AGPS_TYPE_C2K           2
106 #define AGPS_TYPE_WWAN_ANY      3
107 #define AGPS_TYPE_WIFI          4
108 #define AGPS_TYPE_SUPL_ES       5
109 
110 /** SSID length */
111 #define SSID_BUF_SIZE (32+1)
112 
113 typedef int16_t AGpsBearerType;
114 #define AGPS_APN_BEARER_INVALID    -1
115 #define AGPS_APN_BEARER_IPV4        0
116 #define AGPS_APN_BEARER_IPV6        1
117 #define AGPS_APN_BEARER_IPV4V6      2
118 
119 /** GPS extended callback structure. */
120 typedef struct {
121     /** set to sizeof(GpsCallbacks) */
122     size_t      size;
123     gps_set_capabilities set_capabilities_cb;
124     gps_acquire_wakelock acquire_wakelock_cb;
125     gps_release_wakelock release_wakelock_cb;
126     gps_create_thread create_thread_cb;
127     gps_request_utc_time request_utc_time_cb;
128 } GpsExtCallbacks;
129 
130 /** Callback to report the xtra server url to the client.
131  *  The client should use this url when downloading xtra unless overwritten
132  *  in the gps.conf file
133  */
134 typedef void (* report_xtra_server)(const char*, const char*, const char*);
135 
136 /** Callback structure for the XTRA interface. */
137 typedef struct {
138     gps_xtra_download_request download_request_cb;
139     gps_create_thread create_thread_cb;
140     report_xtra_server report_xtra_server_cb;
141 } GpsXtraExtCallbacks;
142 
143 /** Represents the status of AGPS. */
144 typedef struct {
145     /** set to sizeof(AGpsExtStatus) */
146     size_t          size;
147 
148     AGpsExtType type;
149     AGpsStatusValue status;
150     uint32_t        ipv4_addr;
151     struct sockaddr_storage addr;
152     char            ssid[SSID_BUF_SIZE];
153     char            password[SSID_BUF_SIZE];
154 } AGpsExtStatus;
155 
156 /** Callback with AGPS status information.
157  *  Can only be called from a thread created by create_thread_cb.
158  */
159 typedef void (* agps_status_extended)(AGpsExtStatus* status);
160 
161 /** Callback structure for the AGPS interface. */
162 typedef struct {
163     agps_status_extended status_cb;
164     gps_create_thread create_thread_cb;
165 } AGpsExtCallbacks;
166 
167 
168 /** GPS NI callback structure. */
169 typedef struct
170 {
171     /**
172      * Sends the notification request from HAL to GPSLocationProvider.
173      */
174     gps_ni_notify_callback notify_cb;
175     gps_create_thread create_thread_cb;
176 } GpsNiExtCallbacks;
177 
178 typedef enum loc_server_type {
179     LOC_AGPS_CDMA_PDE_SERVER,
180     LOC_AGPS_CUSTOM_PDE_SERVER,
181     LOC_AGPS_MPC_SERVER,
182     LOC_AGPS_SUPL_SERVER
183 } LocServerType;
184 
185 typedef enum loc_position_mode_type {
186     LOC_POSITION_MODE_INVALID = -1,
187     LOC_POSITION_MODE_STANDALONE = 0,
188     LOC_POSITION_MODE_MS_BASED,
189     LOC_POSITION_MODE_MS_ASSISTED,
190     LOC_POSITION_MODE_RESERVED_1,
191     LOC_POSITION_MODE_RESERVED_2,
192     LOC_POSITION_MODE_RESERVED_3,
193     LOC_POSITION_MODE_RESERVED_4,
194     LOC_POSITION_MODE_RESERVED_5
195 
196 } LocPositionMode;
197 
198 #define MIN_POSSIBLE_FIX_INTERVAL 1000 /* msec */
199 
200 /** Flags to indicate which values are valid in a GpsLocationExtended. */
201 typedef uint16_t GpsLocationExtendedFlags;
202 /** GpsLocationExtended has valid pdop, hdop, vdop. */
203 #define GPS_LOCATION_EXTENDED_HAS_DOP 0x0001
204 /** GpsLocationExtended has valid altitude mean sea level. */
205 #define GPS_LOCATION_EXTENDED_HAS_ALTITUDE_MEAN_SEA_LEVEL 0x0002
206 /** UlpLocation has valid magnetic deviation. */
207 #define GPS_LOCATION_EXTENDED_HAS_MAG_DEV 0x0004
208 /** UlpLocation has valid mode indicator. */
209 #define GPS_LOCATION_EXTENDED_HAS_MODE_IND 0x0008
210 /** GpsLocationExtended has valid vertical uncertainty */
211 #define GPS_LOCATION_EXTENDED_HAS_VERT_UNC 0x0010
212 /** GpsLocationExtended has valid speed uncertainty */
213 #define GPS_LOCATION_EXTENDED_HAS_SPEED_UNC 0x0020
214 /** GpsLocationExtended has valid heading uncertainty */
215 #define GPS_LOCATION_EXTENDED_HAS_BEARING_UNC 0x0040
216 /** GpsLocationExtended has valid horizontal reliability */
217 #define GPS_LOCATION_EXTENDED_HAS_HOR_RELIABILITY 0x0080
218 /** GpsLocationExtended has valid vertical reliability */
219 #define GPS_LOCATION_EXTENDED_HAS_VERT_RELIABILITY 0x0100
220 
221 typedef enum {
222     LOC_RELIABILITY_NOT_SET = 0,
223     LOC_RELIABILITY_VERY_LOW = 1,
224     LOC_RELIABILITY_LOW = 2,
225     LOC_RELIABILITY_MEDIUM = 3,
226     LOC_RELIABILITY_HIGH = 4
227 }LocReliability;
228 
229 /** Represents gps location extended. */
230 typedef struct {
231     /** set to sizeof(GpsLocationExtended) */
232     size_t          size;
233     /** Contains GpsLocationExtendedFlags bits. */
234     uint16_t        flags;
235     /** Contains the Altitude wrt mean sea level */
236     float           altitudeMeanSeaLevel;
237     /** Contains Position Dilusion of Precision. */
238     float           pdop;
239     /** Contains Horizontal Dilusion of Precision. */
240     float           hdop;
241     /** Contains Vertical Dilusion of Precision. */
242     float           vdop;
243     /** Contains Magnetic Deviation. */
244     float           magneticDeviation;
245     /** vertical uncertainty in meters */
246     float           vert_unc;
247     /** speed uncertainty in m/s */
248     float           speed_unc;
249     /** heading uncertainty in degrees (0 to 359.999) */
250     float           bearing_unc;
251     /** horizontal reliability. */
252     LocReliability  horizontal_reliability;
253     /** vertical reliability. */
254     LocReliability  vertical_reliability;
255 } GpsLocationExtended;
256 
257 enum loc_sess_status {
258     LOC_SESS_SUCCESS,
259     LOC_SESS_INTERMEDIATE,
260     LOC_SESS_FAILURE
261 };
262 
263 typedef uint32_t LocPosTechMask;
264 #define LOC_POS_TECH_MASK_DEFAULT ((LocPosTechMask)0x00000000)
265 #define LOC_POS_TECH_MASK_SATELLITE ((LocPosTechMask)0x00000001)
266 #define LOC_POS_TECH_MASK_CELLID ((LocPosTechMask)0x00000002)
267 #define LOC_POS_TECH_MASK_WIFI ((LocPosTechMask)0x00000004)
268 #define LOC_POS_TECH_MASK_SENSORS ((LocPosTechMask)0x00000008)
269 #define LOC_POS_TECH_MASK_REFERENCE_LOCATION ((LocPosTechMask)0x00000010)
270 #define LOC_POS_TECH_MASK_INJECTED_COARSE_POSITION ((LocPosTechMask)0x00000020)
271 #define LOC_POS_TECH_MASK_AFLT ((LocPosTechMask)0x00000040)
272 #define LOC_POS_TECH_MASK_HYBRID ((LocPosTechMask)0x00000080)
273 
274 typedef enum {
275   LOC_ENG_IF_REQUEST_SENDER_ID_QUIPC = 0,
276   LOC_ENG_IF_REQUEST_SENDER_ID_MSAPM,
277   LOC_ENG_IF_REQUEST_SENDER_ID_MSAPU,
278   LOC_ENG_IF_REQUEST_SENDER_ID_GPSONE_DAEMON,
279   LOC_ENG_IF_REQUEST_SENDER_ID_MODEM,
280   LOC_ENG_IF_REQUEST_SENDER_ID_UNKNOWN
281 } loc_if_req_sender_id_e_type;
282 
283 
284 #define smaller_of(a, b) (((a) > (b)) ? (b) : (a))
285 #define MAX_APN_LEN 100
286 
287 // This will be overridden by the individual adapters
288 // if necessary.
289 #define DEFAULT_IMPL(rtv)                                     \
290 {                                                             \
291     LOC_LOGD("%s: default implementation invoked", __func__); \
292     return rtv;                                               \
293 }
294 
295 enum loc_api_adapter_err {
296     LOC_API_ADAPTER_ERR_SUCCESS             = 0,
297     LOC_API_ADAPTER_ERR_GENERAL_FAILURE     = 1,
298     LOC_API_ADAPTER_ERR_UNSUPPORTED         = 2,
299     LOC_API_ADAPTER_ERR_INVALID_HANDLE      = 4,
300     LOC_API_ADAPTER_ERR_INVALID_PARAMETER   = 5,
301     LOC_API_ADAPTER_ERR_ENGINE_BUSY         = 6,
302     LOC_API_ADAPTER_ERR_PHONE_OFFLINE       = 7,
303     LOC_API_ADAPTER_ERR_TIMEOUT             = 8,
304     LOC_API_ADAPTER_ERR_SERVICE_NOT_PRESENT = 9,
305     LOC_API_ADAPTER_ERR_INTERNAL            = 10,
306 
307     /* equating engine down to phone offline, as they are the same errror */
308     LOC_API_ADAPTER_ERR_ENGINE_DOWN         = LOC_API_ADAPTER_ERR_PHONE_OFFLINE,
309     LOC_API_ADAPTER_ERR_FAILURE             = 101,
310     LOC_API_ADAPTER_ERR_UNKNOWN
311 };
312 
313 enum loc_api_adapter_event_index {
314     LOC_API_ADAPTER_REPORT_POSITION = 0,               // Position report comes in loc_parsed_position_s_type
315     LOC_API_ADAPTER_REPORT_SATELLITE,                  // Satellite in view report
316     LOC_API_ADAPTER_REPORT_NMEA_1HZ,                   // NMEA report at 1HZ rate
317     LOC_API_ADAPTER_REPORT_NMEA_POSITION,              // NMEA report at position report rate
318     LOC_API_ADAPTER_REQUEST_NI_NOTIFY_VERIFY,          // NI notification/verification request
319     LOC_API_ADAPTER_REQUEST_ASSISTANCE_DATA,           // Assistance data, eg: time, predicted orbits request
320     LOC_API_ADAPTER_REQUEST_LOCATION_SERVER,           // Request for location server
321     LOC_API_ADAPTER_REPORT_IOCTL,                      // Callback report for loc_ioctl
322     LOC_API_ADAPTER_REPORT_STATUS,                     // Misc status report: eg, engine state
323     LOC_API_ADAPTER_REQUEST_WIFI,                      //
324     LOC_API_ADAPTER_SENSOR_STATUS,                     //
325     LOC_API_ADAPTER_REQUEST_TIME_SYNC,                 //
326     LOC_API_ADAPTER_REPORT_SPI,                        //
327     LOC_API_ADAPTER_REPORT_NI_GEOFENCE,                //
328     LOC_API_ADAPTER_GEOFENCE_GEN_ALERT,                //
329     LOC_API_ADAPTER_REPORT_GENFENCE_BREACH,            //
330     LOC_API_ADAPTER_PEDOMETER_CTRL,                    //
331     LOC_API_ADAPTER_MOTION_CTRL,                       //
332     LOC_API_ADAPTER_REQUEST_WIFI_AP_DATA,              // Wifi ap data
333     LOC_API_ADAPTER_BATCH_FULL,                        // Batching on full
334     LOC_API_ADAPTER_BATCHED_POSITION_REPORT,           // Batching on fix
335     LOC_API_ADAPTER_BATCHED_GENFENCE_BREACH_REPORT,    //
336     LOC_API_ADAPTER_GDT_UPLOAD_BEGIN_REQ,              // GDT upload start request
337     LOC_API_ADAPTER_GDT_UPLOAD_END_REQ,                // GDT upload end request
338     LOC_API_ADAPTER_GNSS_MEASUREMENT,                  // GNSS Measurement report
339     LOC_API_ADAPTER_REQUEST_TIMEZONE,                  // Timezone injection request
340     LOC_API_ADAPTER_REPORT_GENFENCE_DWELL_REPORT,      // Geofence dwell report
341     LOC_API_ADAPTER_EVENT_MAX
342 };
343 
344 #define LOC_API_ADAPTER_BIT_PARSED_POSITION_REPORT           (1<<LOC_API_ADAPTER_REPORT_POSITION)
345 #define LOC_API_ADAPTER_BIT_SATELLITE_REPORT                 (1<<LOC_API_ADAPTER_REPORT_SATELLITE)
346 #define LOC_API_ADAPTER_BIT_NMEA_1HZ_REPORT                  (1<<LOC_API_ADAPTER_REPORT_NMEA_1HZ)
347 #define LOC_API_ADAPTER_BIT_NMEA_POSITION_REPORT             (1<<LOC_API_ADAPTER_REPORT_NMEA_POSITION)
348 #define LOC_API_ADAPTER_BIT_NI_NOTIFY_VERIFY_REQUEST         (1<<LOC_API_ADAPTER_REQUEST_NI_NOTIFY_VERIFY)
349 #define LOC_API_ADAPTER_BIT_ASSISTANCE_DATA_REQUEST          (1<<LOC_API_ADAPTER_REQUEST_ASSISTANCE_DATA)
350 #define LOC_API_ADAPTER_BIT_LOCATION_SERVER_REQUEST          (1<<LOC_API_ADAPTER_REQUEST_LOCATION_SERVER)
351 #define LOC_API_ADAPTER_BIT_IOCTL_REPORT                     (1<<LOC_API_ADAPTER_REPORT_IOCTL)
352 #define LOC_API_ADAPTER_BIT_STATUS_REPORT                    (1<<LOC_API_ADAPTER_REPORT_STATUS)
353 #define LOC_API_ADAPTER_BIT_REQUEST_WIFI                     (1<<LOC_API_ADAPTER_REQUEST_WIFI)
354 #define LOC_API_ADAPTER_BIT_SENSOR_STATUS                    (1<<LOC_API_ADAPTER_SENSOR_STATUS)
355 #define LOC_API_ADAPTER_BIT_REQUEST_TIME_SYNC                (1<<LOC_API_ADAPTER_REQUEST_TIME_SYNC)
356 #define LOC_API_ADAPTER_BIT_REPORT_SPI                       (1<<LOC_API_ADAPTER_REPORT_SPI)
357 #define LOC_API_ADAPTER_BIT_REPORT_NI_GEOFENCE               (1<<LOC_API_ADAPTER_REPORT_NI_GEOFENCE)
358 #define LOC_API_ADAPTER_BIT_GEOFENCE_GEN_ALERT               (1<<LOC_API_ADAPTER_GEOFENCE_GEN_ALERT)
359 #define LOC_API_ADAPTER_BIT_REPORT_GENFENCE_BREACH           (1<<LOC_API_ADAPTER_REPORT_GENFENCE_BREACH)
360 #define LOC_API_ADAPTER_BIT_BATCHED_GENFENCE_BREACH_REPORT   (1<<LOC_API_ADAPTER_BATCHED_GENFENCE_BREACH_REPORT)
361 #define LOC_API_ADAPTER_BIT_PEDOMETER_CTRL                   (1<<LOC_API_ADAPTER_PEDOMETER_CTRL)
362 #define LOC_API_ADAPTER_BIT_MOTION_CTRL                      (1<<LOC_API_ADAPTER_MOTION_CTRL)
363 #define LOC_API_ADAPTER_BIT_REQUEST_WIFI_AP_DATA             (1<<LOC_API_ADAPTER_REQUEST_WIFI_AP_DATA)
364 #define LOC_API_ADAPTER_BIT_BATCH_FULL                       (1<<LOC_API_ADAPTER_BATCH_FULL)
365 #define LOC_API_ADAPTER_BIT_BATCHED_POSITION_REPORT          (1<<LOC_API_ADAPTER_BATCHED_POSITION_REPORT)
366 #define LOC_API_ADAPTER_BIT_GDT_UPLOAD_BEGIN_REQ             (1<<LOC_API_ADAPTER_GDT_UPLOAD_BEGIN_REQ)
367 #define LOC_API_ADAPTER_BIT_GDT_UPLOAD_END_REQ               (1<<LOC_API_ADAPTER_GDT_UPLOAD_END_REQ)
368 #define LOC_API_ADAPTER_BIT_GNSS_MEASUREMENT                 (1<<LOC_API_ADAPTER_GNSS_MEASUREMENT)
369 #define LOC_API_ADAPTER_BIT_REQUEST_TIMEZONE                 (1<<LOC_API_ADAPTER_REQUEST_TIMEZONE)
370 #define LOC_API_ADAPTER_BIT_REPORT_GENFENCE_DWELL            (1<<LOC_API_ADAPTER_REPORT_GENFENCE_DWELL_REPORT)
371 
372 typedef unsigned int LOC_API_ADAPTER_EVENT_MASK_T;
373 
374 typedef enum loc_api_adapter_msg_to_check_supported {
375     LOC_API_ADAPTER_MESSAGE_LOCATION_BATCHING,               // Batching 1.0
376     LOC_API_ADAPTER_MESSAGE_BATCHED_GENFENCE_BREACH,         // Geofence Batched Breach
377     LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_TRACKING,          // DBT 2.0
378     LOC_API_ADAPTER_MESSAGE_ADAPTIVE_LOCATION_BATCHING,      // Batching 1.5
379     LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_LOCATION_BATCHING, // Batching 2.0
380     LOC_API_ADAPTER_MESSAGE_UPDATE_TBF_ON_THE_FLY,           // Updating Tracking TBF On The Fly
381 
382     LOC_API_ADAPTER_MESSAGE_MAX
383 } LocCheckingMessagesID;
384 
385 typedef uint32_t LOC_GPS_LOCK_MASK;
386 #define isGpsLockNone(lock) ((lock) == 0)
387 #define isGpsLockMO(lock) ((lock) & ((LOC_GPS_LOCK_MASK)1))
388 #define isGpsLockMT(lock) ((lock) & ((LOC_GPS_LOCK_MASK)2))
389 #define isGpsLockAll(lock) (((lock) & ((LOC_GPS_LOCK_MASK)3)) == 3)
390 
391 #ifdef __cplusplus
392 }
393 #endif /* __cplusplus */
394 
395 #endif /* GPS_EXTENDED_C_H */
396