1/* Copyright (c) 2011, Code Aurora Forum. 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 Code Aurora Forum, Inc. 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 LOC_ENG_MSG_H
30#define LOC_ENG_MSG_H
31
32
33#include <hardware/gps.h>
34#include <stdlib.h>
35#include <string.h>
36#include "log_util.h"
37#include "loc.h"
38#include "loc_eng_log.h"
39#include "loc_eng_msg_id.h"
40
41
42#ifdef __cplusplus
43extern "C" {
44#endif /* __cplusplus */
45
46struct loc_eng_msg {
47    const void* owner;
48    const int msgid;
49    inline loc_eng_msg(void* instance, int id) :
50        owner(instance), msgid(id)
51    {
52        LOC_LOGV("creating msg %s", loc_get_msg_name(msgid));
53    }
54    virtual ~loc_eng_msg()
55    {
56        LOC_LOGV("deleting msg %s", loc_get_msg_name(msgid));
57    }
58};
59
60struct loc_eng_msg_suple_version : public loc_eng_msg {
61    const int supl_version;
62    inline loc_eng_msg_suple_version(void* instance, int version) :
63        loc_eng_msg(instance, LOC_ENG_MSG_SUPL_VERSION),
64        supl_version(version)
65        {
66            LOC_LOGV("SUPL Version: %d", version);
67        }
68};
69
70struct loc_eng_msg_sensor_control_config : public loc_eng_msg {
71    const int sensorsDisabled;
72    inline loc_eng_msg_sensor_control_config(void* instance, int disabled) :
73            loc_eng_msg(instance, LOC_ENG_MSG_SET_SENSOR_CONTROL_CONFIG),
74            sensorsDisabled(disabled)
75        {
76            LOC_LOGV("Sensors Disabled: %d", disabled);
77        }
78};
79
80struct loc_eng_msg_sensor_properties : public loc_eng_msg {
81    const float gyroBiasVarianceRandomWalk;
82    inline loc_eng_msg_sensor_properties(void* instance, float gyroBiasRandomWalk) :
83            loc_eng_msg(instance, LOC_ENG_MSG_SET_SENSOR_PROPERTIES),
84            gyroBiasVarianceRandomWalk(gyroBiasRandomWalk)
85        {
86            LOC_LOGV("Gyro Bias Random Walk: %f", gyroBiasRandomWalk);
87        }
88};
89
90struct loc_eng_msg_sensor_perf_control_config : public loc_eng_msg {
91    const int controlMode;
92    const int accelSamplesPerBatch;
93    const int accelBatchesPerSec;
94    const int gyroSamplesPerBatch;
95    const int gyroBatchesPerSec;
96    inline loc_eng_msg_sensor_perf_control_config(void* instance, int controlMode,
97                                                  int accelSamplesPerBatch, int accelBatchesPerSec,
98                                                  int gyroSamplesPerBatch, int gyroBatchesPerSec) :
99            loc_eng_msg(instance, LOC_ENG_MSG_SET_SENSOR_PERF_CONTROL_CONFIG),
100            controlMode(controlMode),
101            accelSamplesPerBatch(accelSamplesPerBatch),
102            accelBatchesPerSec(accelBatchesPerSec),
103            gyroSamplesPerBatch(gyroSamplesPerBatch),
104            gyroBatchesPerSec(gyroBatchesPerSec)
105        {
106            LOC_LOGV("Sensor Perf Control Config (performanceControlMode)(%u) "
107                "accel(#smp,#batches) (%u,%u) gyro(#smp,#batches) (%u,%u)\n",
108                controlMode,
109                accelSamplesPerBatch,
110                accelBatchesPerSec,
111                gyroSamplesPerBatch,
112                gyroBatchesPerSec
113                );
114        }
115};
116
117
118struct loc_eng_msg_position_mode : public loc_eng_msg {
119    const LocPositionMode pMode;
120    const GpsPositionRecurrence pRecurrence;
121    const uint32_t minInterval;
122    const uint32_t preferredAccuracy;
123    const uint32_t preferredTime;
124    inline loc_eng_msg_position_mode() :
125        loc_eng_msg(NULL, LOC_ENG_MSG_SET_POSITION_MODE),
126        pMode(LOC_POSITION_MODE_STANDALONE),
127        pRecurrence(0), minInterval(0),
128        preferredAccuracy(0), preferredTime(0) {}
129    inline loc_eng_msg_position_mode(void* instance,
130                                     LocPositionMode mode,
131                                     GpsPositionRecurrence recurrence,
132                                     uint32_t min_interval,
133                                     uint32_t preferred_accuracy,
134                                     uint32_t preferred_time) :
135        loc_eng_msg(instance, LOC_ENG_MSG_SET_POSITION_MODE),
136        pMode(mode), pRecurrence(recurrence), minInterval(min_interval),
137        preferredAccuracy(preferred_accuracy), preferredTime(preferred_time)
138    {
139        LOC_LOGV("Position mode: %s\n  Position recurrence: %s\n  min interval: %d\n  preferred accuracy: %d\n  preferred time: %d",
140                 loc_get_position_mode_name(pMode),
141                 loc_get_position_recurrence_name(pRecurrence),
142                 minInterval,
143                 preferredAccuracy,
144                 preferredTime);
145    }
146};
147
148struct loc_eng_msg_set_time : public loc_eng_msg {
149    const GpsUtcTime time;
150    const int64_t timeReference;
151    const int uncertainty;
152    inline loc_eng_msg_set_time(void* instance,
153                                GpsUtcTime t,
154                                int64_t tf,
155                                int unc) :
156        loc_eng_msg(instance, LOC_ENG_MSG_SET_TIME),
157        time(t), timeReference(tf), uncertainty(unc)
158    {
159        LOC_LOGV("time: %lld\n  timeReference: %lld\n  uncertainty: %d",
160                 time, timeReference, uncertainty);
161    }
162};
163
164struct loc_eng_msg_inject_location : public loc_eng_msg {
165    const double latitude;
166    const double longitude;
167    const float accuracy;
168    inline loc_eng_msg_inject_location(void* instance, double lat,
169                                       double longi, float accur) :
170        loc_eng_msg(instance, LOC_ENG_MSG_INJECT_LOCATION),
171        latitude(lat), longitude(longi), accuracy(accur)
172    {
173        LOC_LOGV("latitude: %f\n  longitude: %f\n  accuracy: %f",
174                 latitude, longitude, accuracy);
175    }
176};
177
178struct loc_eng_msg_delete_aiding_data : public loc_eng_msg {
179    const GpsAidingData type;
180    inline loc_eng_msg_delete_aiding_data(void* instance, GpsAidingData data) :
181        loc_eng_msg(instance, LOC_ENG_MSG_DELETE_AIDING_DATA), type(data)
182    {
183        LOC_LOGV("aiding data msak %d", type);
184    }
185};
186
187struct loc_eng_msg_report_position : public loc_eng_msg {
188    const GpsLocation location;
189    const void* locationExt;
190    const enum loc_sess_status status;
191    inline loc_eng_msg_report_position(void* instance, GpsLocation &loc, void* locExt,
192                                       enum loc_sess_status st) :
193        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_POSITION),
194        location(loc), locationExt(locExt), status(st)
195    {
196#ifdef QCOM_FEATURE_ULP
197        LOC_LOGV("flags: %d\n  source: %d\n  latitude: %f\n  longitude: %f\n  altitude: %f\n  speed: %f\n  bearing: %f\n  accuracy: %f\n  timestamp: %lld\n  rawDataSize: %d\n  rawData: %p\n  Session status: %s",
198                 location.flags, location.position_source, location.latitude, location.longitude,
199                 location.altitude, location.speed, location.bearing, location.accuracy,
200                 location.timestamp, location.rawDataSize, location.rawData,
201                 loc_get_position_sess_status_name(status));
202#else
203        LOC_LOGV("flags: %d\n  latitude: %f\n  longitude: %f\n  altitude: %f\n  speed: %f\n  bearing: %f\n  accuracy: %f\n  timestamp: %lld\n  Session status: %s",
204                 location.flags, location.latitude, location.longitude,
205                 location.altitude, location.speed, location.bearing, location.accuracy,
206                 location.timestamp, loc_get_position_sess_status_name(status));
207#endif
208    }
209};
210
211struct loc_eng_msg_report_sv : public loc_eng_msg {
212    const GpsSvStatus svStatus;
213    const void* svExt;
214    inline loc_eng_msg_report_sv(void* instance, GpsSvStatus &sv, void* ext) :
215        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_SV), svStatus(sv), svExt(ext)
216    {
217        LOC_LOGV("num sv: %d\n  ephemeris mask: %dxn  almanac mask: %x\n  used in fix mask: %x\n      sv: prn         snr       elevation      azimuth",
218                 svStatus.num_svs, svStatus.ephemeris_mask, svStatus.almanac_mask, svStatus.used_in_fix_mask);
219        for (int i = 0; i < svStatus.num_svs && i < GPS_MAX_SVS; i++) {
220            LOC_LOGV("   %d:   %d    %f    %f    %f\n  ",
221                     i,
222                     svStatus.sv_list[i].prn,
223                     svStatus.sv_list[i].snr,
224                     svStatus.sv_list[i].elevation,
225                     svStatus.sv_list[i].azimuth);
226        }
227    }
228};
229
230struct loc_eng_msg_report_status : public loc_eng_msg {
231    const GpsStatusValue status;
232    inline loc_eng_msg_report_status(void* instance, GpsStatusValue engineStatus) :
233        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_STATUS), status(engineStatus)
234    {
235        LOC_LOGV("status: %s", loc_get_gps_status_name(status));
236    }
237};
238
239struct loc_eng_msg_report_nmea : public loc_eng_msg {
240    char* const nmea;
241    const int length;
242    inline loc_eng_msg_report_nmea(void* instance,
243                                   const char* data,
244                                   int len) :
245        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_NMEA),
246        nmea(new char[len]), length(len)
247    {
248        memcpy((void*)nmea, (void*)data, len);
249        LOC_LOGV("length: %d\n  nmea: %p - %c%c%c",
250                 length, nmea, nmea[3], nmea[4], nmea[5]);
251    }
252    inline ~loc_eng_msg_report_nmea()
253    {
254        delete[] nmea;
255    }
256};
257
258struct loc_eng_msg_request_bit : public loc_eng_msg {
259    const unsigned int isSupl;
260    const int ipv4Addr;
261    char* const ipv6Addr;
262    inline loc_eng_msg_request_bit(void* instance,
263                                   unsigned int is_supl,
264                                   int ipv4,
265                                   char* ipv6) :
266        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_BIT),
267        isSupl(is_supl), ipv4Addr(ipv4),
268        ipv6Addr(NULL == ipv6 ? NULL : new char[16])
269    {
270        if (NULL != ipv6Addr)
271            memcpy(ipv6Addr, ipv6, 16);
272        LOC_LOGV("isSupl: %d, ipv4: %d.%d.%d.%d, ipv6: %s", isSupl,
273                 (unsigned char)ipv4>>24,
274                 (unsigned char)ipv4>>16,
275                 (unsigned char)ipv4>>8,
276                 (unsigned char)ipv4,
277                 NULL != ipv6Addr ? ipv6Addr : "");
278    }
279
280    inline ~loc_eng_msg_request_bit()
281    {
282        if (NULL != ipv6Addr) {
283            delete[] ipv6Addr;
284        }
285    }
286};
287
288struct loc_eng_msg_release_bit : public loc_eng_msg {
289    const unsigned int isSupl;
290    const int ipv4Addr;
291    char* const ipv6Addr;
292    inline loc_eng_msg_release_bit(void* instance,
293                                   unsigned int is_supl,
294                                   int ipv4,
295                                   char* ipv6) :
296        loc_eng_msg(instance, LOC_ENG_MSG_RELEASE_BIT),
297        isSupl(is_supl), ipv4Addr(ipv4),
298        ipv6Addr(NULL == ipv6 ? NULL : new char[16])
299    {
300        if (NULL != ipv6Addr)
301            memcpy(ipv6Addr, ipv6, 16);
302        LOC_LOGV("isSupl: %d, ipv4: %d.%d.%d.%d, ipv6: %s", isSupl,
303                 (unsigned char)ipv4>>24,
304                 (unsigned char)ipv4>>16,
305                 (unsigned char)ipv4>>8,
306                 (unsigned char)ipv4,
307                 NULL != ipv6Addr ? ipv6Addr : "");
308    }
309
310    inline ~loc_eng_msg_release_bit()
311    {
312        if (NULL != ipv6Addr) {
313            delete[] ipv6Addr;
314        }
315    }
316};
317
318struct loc_eng_msg_request_atl : public loc_eng_msg {
319    const int handle;
320    const AGpsType type;
321    inline loc_eng_msg_request_atl(void* instance, int hndl,
322                                   AGpsType agps_type) :
323        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_ATL),
324        handle(hndl), type(agps_type)
325    {
326        LOC_LOGV("handle: %d\n  agps type: %s",
327                 handle,
328                 loc_get_agps_type_name(type));
329    }
330};
331
332struct loc_eng_msg_release_atl : public loc_eng_msg {
333    const int handle;
334    inline loc_eng_msg_release_atl(void* instance, int hndl) :
335        loc_eng_msg(instance, LOC_ENG_MSG_RELEASE_ATL), handle(hndl)
336    {
337        LOC_LOGV("handle: %d", handle);
338    }
339};
340
341struct loc_eng_msg_request_ni : public loc_eng_msg {
342    const GpsNiNotification notify;
343    const void *passThroughData;
344    inline loc_eng_msg_request_ni(void* instance,
345                                  GpsNiNotification &notif, const void* data) :
346        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_NI),
347        notify(notif), passThroughData(data)
348    {
349        LOC_LOGV("id: %d\n  type: %s\n  flags: %d\n  time out: %d\n  default response: %s\n  requestor id encoding: %s\n  text encoding: %s\n  passThroughData: %p",
350                 notify.notification_id,
351                 loc_get_ni_type_name(notify.ni_type),
352                 notify.notify_flags,
353                 notify.timeout,
354                 loc_get_ni_response_name(notify.default_response),
355                 loc_get_ni_encoding_name(notify.requestor_id_encoding),
356                 loc_get_ni_encoding_name(notify.text_encoding),
357                 passThroughData);
358    }
359};
360
361struct loc_eng_msg_inform_ni_response : public loc_eng_msg {
362    const GpsUserResponseType response;
363    const void *passThroughData;
364    inline loc_eng_msg_inform_ni_response(void* instance,
365                                          GpsUserResponseType resp,
366                                          const void* data) :
367        loc_eng_msg(instance, LOC_ENG_MSG_INFORM_NI_RESPONSE),
368        response(resp), passThroughData(data)
369    {
370        LOC_LOGV("response: %s\n  passThroughData: %p",
371                 loc_get_ni_response_name(response),
372                 passThroughData);
373    }
374    inline ~loc_eng_msg_inform_ni_response()
375    {
376        // this is a bit weird since passThroughData is not
377        // allocated by this class.  But there is no better way.
378        // passThroughData actually won't be NULL here.
379        // But better safer than sorry.
380        if (NULL != passThroughData) {
381            free((void*)passThroughData);
382        }
383    }
384};
385
386struct loc_eng_msg_set_apn : public loc_eng_msg {
387    char* const apn;
388    inline loc_eng_msg_set_apn(void* instance, const char* name, int len) :
389        loc_eng_msg(instance, LOC_ENG_MSG_SET_APN),
390        apn(new char[len+1])
391    {
392        memcpy((void*)apn, (void*)name, len);
393        apn[len] = 0;
394        LOC_LOGV("apn: %s", apn);
395    }
396    inline ~loc_eng_msg_set_apn()
397    {
398        delete[] apn;
399    }
400};
401
402
403
404struct loc_eng_msg_set_server_ipv4 : public loc_eng_msg {
405    const unsigned int nl_addr;
406    const int port;
407    const LocServerType serverType;
408    inline loc_eng_msg_set_server_ipv4(void* instance,
409                                       unsigned int ip,
410                                       int p,
411                                       LocServerType type) :
412        loc_eng_msg(instance, LOC_ENG_MSG_SET_SERVER_IPV4),
413        nl_addr(ip), port(p), serverType(type)
414    {
415        LOC_LOGV("addr: %x\n  , port: %d\n type: %s", nl_addr, port, loc_get_server_type_name(serverType));
416    }
417};
418
419
420struct loc_eng_msg_set_server_url : public loc_eng_msg {
421    const int len;
422    char* const url;
423    inline loc_eng_msg_set_server_url(void* instance,
424                                      const char* urlString,
425                                      int url_len) :
426        loc_eng_msg(instance, LOC_ENG_MSG_SET_SERVER_URL),
427        len(url_len), url(new char[len+1])
428    {
429        memcpy((void*)url, (void*)urlString, url_len);
430        url[len] = 0;
431        LOC_LOGV("url: %s", url);
432    }
433    inline ~loc_eng_msg_set_server_url()
434    {
435        delete[] url;
436    }
437};
438
439struct loc_eng_msg_inject_xtra_data : public loc_eng_msg {
440    char* const data;
441    const int length;
442    inline loc_eng_msg_inject_xtra_data(void* instance, char* d, int l) :
443        loc_eng_msg(instance, LOC_ENG_MSG_INJECT_XTRA_DATA),
444        data(new char[l]), length(l)
445    {
446        memcpy((void*)data, (void*)d, l);
447        LOC_LOGV("length: %d\n  data: %p", length, data);
448    }
449    inline ~loc_eng_msg_inject_xtra_data()
450    {
451        delete[] data;
452    }
453};
454
455#ifdef QCOM_FEATURE_IPV6
456struct loc_eng_msg_atl_open_success : public loc_eng_msg {
457    const AGpsStatusValue agpsType;
458    const int length;
459    char* const apn;
460    const AGpsBearerType bearerType;
461    inline loc_eng_msg_atl_open_success(void* instance,
462                                        AGpsStatusValue atype,
463                                        const char* name,
464                                        int len,
465                                        AGpsBearerType btype) :
466        loc_eng_msg(instance, LOC_ENG_MSG_ATL_OPEN_SUCCESS),
467        agpsType(atype), length(len),
468        apn(new char[len+1]), bearerType(btype)
469    {
470        memcpy((void*)apn, (void*)name, len);
471        apn[len] = 0;
472        LOC_LOGV("agps type: %s\n  apn: %s\n  bearer type: %s",
473                 loc_get_agps_type_name(agpsType),
474                 apn,
475                 loc_get_agps_bear_name(bearerType));
476    }
477    inline ~loc_eng_msg_atl_open_success()
478    {
479        delete[] apn;
480    }
481};
482#else
483struct loc_eng_msg_atl_open_success : public loc_eng_msg {
484    const int length;
485    char* const apn;
486    inline loc_eng_msg_atl_open_success(void* instance,
487                                        const char* name,
488                                        int len) :
489        loc_eng_msg(instance, LOC_ENG_MSG_ATL_OPEN_SUCCESS),
490        length(len),
491        apn(new char[len+1])
492    {
493        memcpy((void*)apn, (void*)name, len);
494        apn[len] = 0;
495        LOC_LOGV("apn: %s\n",
496                 apn);
497    }
498    inline ~loc_eng_msg_atl_open_success()
499    {
500        delete[] apn;
501    }
502};
503#endif
504
505#ifdef QCOM_FEATURE_IPV6
506struct loc_eng_msg_atl_open_failed : public loc_eng_msg {
507    const AGpsStatusValue agpsType;
508    inline loc_eng_msg_atl_open_failed(void* instance,
509                                       AGpsStatusValue atype) :
510        loc_eng_msg(instance, LOC_ENG_MSG_ATL_OPEN_FAILED),
511        agpsType(atype)
512    {
513        LOC_LOGV("agps type %s",
514                 loc_get_agps_type_name(agpsType));
515    }
516};
517#else
518struct loc_eng_msg_atl_open_failed : public loc_eng_msg {
519    inline loc_eng_msg_atl_open_failed(void* instance) :
520        loc_eng_msg(instance, LOC_ENG_MSG_ATL_OPEN_FAILED)
521    {
522        LOC_LOGV("");
523    }
524};
525#endif
526
527#ifdef QCOM_FEATURE_IPV6
528struct loc_eng_msg_atl_closed : public loc_eng_msg {
529    const AGpsStatusValue agpsType;
530    inline loc_eng_msg_atl_closed(void* instance,
531                                  AGpsStatusValue atype) :
532        loc_eng_msg(instance, LOC_ENG_MSG_ATL_CLOSED),
533        agpsType(atype)
534    {
535        LOC_LOGV("agps type %s",
536                 loc_get_agps_type_name(agpsType));
537    }
538};
539#else
540struct loc_eng_msg_atl_closed : public loc_eng_msg {
541    inline loc_eng_msg_atl_closed(void* instance) :
542        loc_eng_msg(instance, LOC_ENG_MSG_ATL_CLOSED)
543    {
544        LOC_LOGV("");
545    }
546};
547#endif
548
549struct loc_eng_msg_set_data_enable : public loc_eng_msg {
550    const int enable;
551    char* const apn;
552    const int length;
553    inline loc_eng_msg_set_data_enable(void* instance,
554                                       const char* name,
555                                       int len,
556                                       int yes) :
557        loc_eng_msg(instance, LOC_ENG_MSG_ENABLE_DATA),
558        enable(yes), apn(new char[len+1]), length(len)
559    {
560        memcpy((void*)apn, (void*)name, len);
561        apn[len] = 0;
562        LOC_LOGV("apn: %s\n  enable: %d", apn, enable);
563    }
564    inline ~loc_eng_msg_set_data_enable()
565    {
566        delete[] apn;
567    }
568};
569
570void loc_eng_msg_sender(void* loc_eng_data_p, void* msg);
571int loc_eng_msgget(int * p_req_msgq);
572int loc_eng_msgremove(int req_msgq);
573int loc_eng_msgsnd(int msgqid, void * msgp);
574int loc_eng_msgrcv(int msgqid, void ** msgp);
575int loc_eng_msgsnd_raw(int msgqid, void * msgp, unsigned int msgsz);
576int loc_eng_msgrcv_raw(int msgqid, void *msgp, unsigned int msgsz);
577int loc_eng_msgflush(int msgqid);
578int loc_eng_msgunblock(int msgqid);
579
580#ifdef __cplusplus
581}
582#endif /* __cplusplus */
583
584#endif /* LOC_ENG_MSG_H */
585