summaryrefslogtreecommitdiffstats
path: root/gps/libloc_api_50001/loc_eng_msg.h
blob: 0728653fee44cee58d9c50173fe04aed3776f818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#ifndef LOC_ENG_MSG_H
#define LOC_ENG_MSG_H


#include <hardware/gps.h>
#include <stdlib.h>
#include <string.h>
#include "log_util.h"
#include "loc.h"
#include <loc_eng_log.h>
#include "loc_eng_msg_id.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

struct LocPosMode
{
    LocPositionMode mode;
    GpsPositionRecurrence recurrence;
    uint32_t min_interval;
    uint32_t preferred_accuracy;
    uint32_t preferred_time;
    char credentials[14];
    char provider[8];
    LocPosMode(LocPositionMode m, GpsPositionRecurrence recr,
               uint32_t gap, uint32_t accu, uint32_t time,
               const char* cred, const char* prov) :
        mode(m), recurrence(recr),
        min_interval(gap < MIN_POSSIBLE_FIX_INTERVAL ? MIN_POSSIBLE_FIX_INTERVAL : gap),
        preferred_accuracy(accu), preferred_time(time) {
        memset(credentials, 0, sizeof(credentials));
        memset(provider, 0, sizeof(provider));
        if (NULL != cred) {
            memcpy(credentials, cred, sizeof(credentials)-1);
        }
        if (NULL != prov) {
            memcpy(provider, prov, sizeof(provider)-1);
        }
    }

    LocPosMode() :
        mode(LOC_POSITION_MODE_MS_BASED), recurrence(GPS_POSITION_RECURRENCE_PERIODIC),
        min_interval(MIN_POSSIBLE_FIX_INTERVAL), preferred_accuracy(50), preferred_time(120000) {
        memset(credentials, 0, sizeof(credentials));
        memset(provider, 0, sizeof(provider));
    }

    inline bool equals(const LocPosMode &anotherMode) const
    {
        return anotherMode.mode == mode &&
            anotherMode.recurrence == recurrence &&
            anotherMode.min_interval == min_interval &&
            anotherMode.preferred_accuracy == preferred_accuracy &&
            anotherMode.preferred_time == preferred_time &&
            !strncmp(anotherMode.credentials, credentials, sizeof(credentials)-1) &&
            !strncmp(anotherMode.provider, provider, sizeof(provider)-1);
    }

    inline void logv() const
    {
        LOC_LOGV ("Position mode: %s\n  Position recurrence: %s\n  min interval: %d\n  preferred accuracy: %d\n  preferred time: %d\n  credentials: %s  provider: %s",
                  loc_get_position_mode_name(mode),
                  loc_get_position_recurrence_name(recurrence),
                  min_interval,
                  preferred_accuracy,
                  preferred_time,
                  credentials,
                  provider);
    }
};

/** Flags to indicate which values are valid in a GpsLocationExtended. */
typedef uint16_t GpsLocationExtendedFlags;
/** GpsLocationExtended has valid pdop, hdop, vdop. */
#define GPS_LOCATION_EXTENDED_HAS_DOP 0x0001
/** GpsLocationExtended has valid altitude mean sea level. */
#define GPS_LOCATION_EXTENDED_HAS_ALTITUDE_MEAN_SEA_LEVEL 0x0002
/** GpsLocation has valid magnetic deviation. */
#define GPS_LOCATION_EXTENDED_HAS_MAG_DEV 0x0004
/** GpsLocation has valid mode indicator. */
#define GPS_LOCATION_EXTENDED_HAS_MODE_IND 0x0008

/** Represents gps location extended. */
typedef struct {
    /** set to sizeof(GpsLocationExtended) */
    size_t          size;
    /** Contains GpsLocationExtendedFlags bits. */
    uint16_t        flags;
    /** Contains the Altitude wrt mean sea level */
    float           altitudeMeanSeaLevel;
    /** Contains Position Dilusion of Precision. */
    float           pdop;
    /** Contains Horizontal Dilusion of Precision. */
    float           hdop;
    /** Contains Vertical Dilusion of Precision. */
    float           vdop;
    /** Contains Magnetic Deviation. */
    float           magneticDeviation;
} GpsLocationExtended;

typedef enum {
  LOC_ENG_IF_REQUEST_TYPE_SUPL = 0,
  LOC_ENG_IF_REQUEST_TYPE_WIFI,
  LOC_ENG_IF_REQUEST_TYPE_ANY
} loc_if_req_type_e_type;

typedef enum {
  LOC_ENG_IF_REQUEST_SENDER_ID_QUIPC = 0,
  LOC_ENG_IF_REQUEST_SENDER_ID_MSAPM,
  LOC_ENG_IF_REQUEST_SENDER_ID_MSAPU,
  LOC_ENG_IF_REQUEST_SENDER_ID_GPSONE_DAEMON,
  LOC_ENG_IF_REQUEST_SENDER_ID_MODEM,
  LOC_ENG_IF_REQUEST_SENDER_ID_UNKNOWN
} loc_if_req_sender_id_e_type;

struct loc_eng_msg {
    const void* owner;
    const int msgid;
    inline loc_eng_msg(void* instance, int id) :
        owner(instance), msgid(id)
    {
        LOC_LOGV("creating msg %s", loc_get_msg_name(msgid));
        LOC_LOGV("creating msg ox%x", msgid);
    }
    virtual ~loc_eng_msg()
    {
        LOC_LOGV("deleting msg %s", loc_get_msg_name(msgid));
        LOC_LOGV("deleting msg ox%x", msgid);
    }
};

struct loc_eng_msg_suple_version : public loc_eng_msg {
    const int supl_version;
    inline loc_eng_msg_suple_version(void* instance, int version) :
        loc_eng_msg(instance, LOC_ENG_MSG_SUPL_VERSION),
        supl_version(version)
        {
            LOC_LOGV("SUPL Version: %d", version);
        }
};

struct loc_eng_msg_lpp_config : public loc_eng_msg {
    const int lpp_config;
    inline loc_eng_msg_lpp_config(void *instance, int profile) :
        loc_eng_msg(instance, LOC_ENG_MSG_LPP_CONFIG),
        lpp_config(profile)
        {
            LOC_LOGV("lpp profile: %d", profile);
        }
};

struct loc_eng_msg_ext_power_config : public loc_eng_msg {
    const int isBatteryCharging;
    inline loc_eng_msg_ext_power_config(void* instance, int isBattCharging) :
            loc_eng_msg(instance, LOC_ENG_MSG_EXT_POWER_CONFIG),
            isBatteryCharging(isBattCharging)
        {
            LOC_LOGV("isBatteryCharging: %d", isBatteryCharging);
        }
};

struct loc_eng_msg_sensor_control_config : public loc_eng_msg {
    const int sensorsDisabled;
    inline loc_eng_msg_sensor_control_config(void* instance, int disabled) :
            loc_eng_msg(instance, LOC_ENG_MSG_SET_SENSOR_CONTROL_CONFIG),
            sensorsDisabled(disabled)
        {
            LOC_LOGV("Sensors Disabled: %d", disabled);
        }
};

struct loc_eng_msg_sensor_properties : public loc_eng_msg {
    const bool gyroBiasVarianceRandomWalk_valid;
    const float gyroBiasVarianceRandomWalk;
    const bool accelRandomWalk_valid;
    const float accelRandomWalk;
    const bool angleRandomWalk_valid;
    const float angleRandomWalk;
    const bool rateRandomWalk_valid;
    const float rateRandomWalk;
    const bool velocityRandomWalk_valid;
    const float velocityRandomWalk;
    inline loc_eng_msg_sensor_properties(void* instance, bool gyroBiasRandomWalk_valid, float gyroBiasRandomWalk,
                                          bool accelRandomWalk_valid, float accelRandomWalk,
                                          bool angleRandomWalk_valid, float angleRandomWalk,
                                          bool rateRandomWalk_valid, float rateRandomWalk,
                                          bool velocityRandomWalk_valid, float velocityRandomWalk) :
            loc_eng_msg(instance, LOC_ENG_MSG_SET_SENSOR_PROPERTIES),
            gyroBiasVarianceRandomWalk_valid(gyroBiasRandomWalk_valid),
            gyroBiasVarianceRandomWalk(gyroBiasRandomWalk),
            accelRandomWalk_valid(accelRandomWalk_valid),
            accelRandomWalk(accelRandomWalk),
            angleRandomWalk_valid(angleRandomWalk_valid),
            angleRandomWalk(angleRandomWalk),
            rateRandomWalk_valid(rateRandomWalk_valid),
            rateRandomWalk(rateRandomWalk),
            velocityRandomWalk_valid(velocityRandomWalk_valid),
            velocityRandomWalk(velocityRandomWalk)
        {
            LOC_LOGV("Sensor properties validity, Gyro Random walk: %d Accel Random Walk: %d "
                     "Angle Random Walk: %d Rate Random Walk: %d "
                     "Velocity Random Walk: %d",
                     gyroBiasRandomWalk_valid,
                     accelRandomWalk_valid,
                     angleRandomWalk_valid,
                     rateRandomWalk_valid,
                     velocityRandomWalk_valid
                     );
            LOC_LOGV("Sensor properties, Gyro Random walk: %f Accel Random Walk: %f "
                     "Angle Random Walk: %f Rate Random Walk: %f "
                     "Velocity Random Walk: %f",
                     gyroBiasRandomWalk,
                     accelRandomWalk,
                     angleRandomWalk,
                     rateRandomWalk,
                     velocityRandomWalk
                     );
        }
};

struct loc_eng_msg_sensor_perf_control_config : public loc_eng_msg {
    const int controlMode;
    const int accelSamplesPerBatch;
    const int accelBatchesPerSec;
    const int gyroSamplesPerBatch;
    const int gyroBatchesPerSec;
    const int accelSamplesPerBatchHigh;
    const int accelBatchesPerSecHigh;
    const int gyroSamplesPerBatchHigh;
    const int gyroBatchesPerSecHigh;
    const int algorithmConfig;
    inline loc_eng_msg_sensor_perf_control_config(void* instance, int controlMode,
                                                  int accelSamplesPerBatch, int accelBatchesPerSec,
                                                  int gyroSamplesPerBatch, int gyroBatchesPerSec,
                                                  int accelSamplesPerBatchHigh, int accelBatchesPerSecHigh,
                                                  int gyroSamplesPerBatchHigh, int gyroBatchesPerSecHigh,
                                                  int algorithmConfig) :
            loc_eng_msg(instance, LOC_ENG_MSG_SET_SENSOR_PERF_CONTROL_CONFIG),
            controlMode(controlMode),
            accelSamplesPerBatch(accelSamplesPerBatch),
            accelBatchesPerSec(accelBatchesPerSec),
            gyroSamplesPerBatch(gyroSamplesPerBatch),
            gyroBatchesPerSec(gyroBatchesPerSec),
            accelSamplesPerBatchHigh(accelSamplesPerBatchHigh),
            accelBatchesPerSecHigh(accelBatchesPerSecHigh),
            gyroSamplesPerBatchHigh(gyroSamplesPerBatchHigh),
            gyroBatchesPerSecHigh(gyroBatchesPerSecHigh),
            algorithmConfig(algorithmConfig)
        {
            LOC_LOGV("Sensor Perf Control Config (performanceControlMode)(%u) "
                "accel(#smp,#batches) (%u,%u) gyro(#smp,#batches) (%u,%u), "
                "accel_high(#smp,#batches) (%u,%u) gyro_high(#smp,#batches) (%u,%u), "
                "algorithmConfig(%u)\n",
                controlMode,
                accelSamplesPerBatch,
                accelBatchesPerSec,
                gyroSamplesPerBatch,
                gyroBatchesPerSec,
                accelSamplesPerBatchHigh,
                accelBatchesPerSecHigh,
                gyroSamplesPerBatchHigh,
                gyroBatchesPerSecHigh,
                algorithmConfig
                );
        }
};


struct loc_eng_msg_position_mode : public loc_eng_msg {
    const LocPosMode pMode;
    inline loc_eng_msg_position_mode(void* instance,
                                     LocPosMode &mode) :
        loc_eng_msg(instance, LOC_ENG_MSG_SET_POSITION_MODE),
        pMode(mode)
    {
        pMode.logv();
    }
};

struct loc_eng_msg_set_time : public loc_eng_msg {
    const GpsUtcTime time;
    const int64_t timeReference;
    const int uncertainty;
    inline loc_eng_msg_set_time(void* instance,
                                GpsUtcTime t,
                                int64_t tf,
                                int unc) :
        loc_eng_msg(instance, LOC_ENG_MSG_SET_TIME),
        time(t), timeReference(tf), uncertainty(unc)
    {
        LOC_LOGV("time: %lld\n  timeReference: %lld\n  uncertainty: %d",
                 time, timeReference, uncertainty);
    }
};

struct loc_eng_msg_inject_location : public loc_eng_msg {
    const double latitude;
    const double longitude;
    const float accuracy;
    inline loc_eng_msg_inject_location(void* instance, double lat,
                                       double longi, float accur) :
        loc_eng_msg(instance, LOC_ENG_MSG_INJECT_LOCATION),
        latitude(lat), longitude(longi), accuracy(accur)
    {
        LOC_LOGV("latitude: %f\n  longitude: %f\n  accuracy: %f",
                 latitude, longitude, accuracy);
    }
};

struct loc_eng_msg_delete_aiding_data : public loc_eng_msg {
    const GpsAidingData type;
    inline loc_eng_msg_delete_aiding_data(void* instance, GpsAidingData data) :
        loc_eng_msg(instance, LOC_ENG_MSG_DELETE_AIDING_DATA), type(data)
    {
        LOC_LOGV("aiding data msak %d", type);
    }
};

struct loc_eng_msg_report_position : public loc_eng_msg {
    const GpsLocation location;
    const GpsLocationExtended locationExtended;
    const void* locationExt;
    const enum loc_sess_status status;
    const LocPosTechMask technology_mask;
    inline loc_eng_msg_report_position(void* instance, GpsLocation &loc, GpsLocationExtended &locExtended, void* locExt,
                                       enum loc_sess_status st) :
        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_POSITION),
        location(loc), locationExtended(locExtended), locationExt(locExt), status(st), technology_mask(LOC_POS_TECH_MASK_DEFAULT)
    {
        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: %d\n Technology mask: %u",
                 location.flags, location.position_source, location.latitude, location.longitude,
                 location.altitude, location.speed, location.bearing, location.accuracy,
                 location.timestamp, location.rawDataSize, location.rawData,status,technology_mask);
    }
    inline loc_eng_msg_report_position(void* instance, GpsLocation &loc, GpsLocationExtended &locExtended, void* locExt,
                                       enum loc_sess_status st, LocPosTechMask technology) :
        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_POSITION),
        location(loc), locationExtended(locExtended), locationExt(locExt), status(st), technology_mask(technology)
    {
        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: %d\n Technology mask: %u",
                 location.flags, location.position_source, location.latitude, location.longitude,
                 location.altitude, location.speed, location.bearing, location.accuracy,
                 location.timestamp, location.rawDataSize, location.rawData,status,technology_mask);
    }
};

struct loc_eng_msg_report_sv : public loc_eng_msg {
    const GpsSvStatus svStatus;
    const GpsLocationExtended locationExtended;
    const void* svExt;
    inline loc_eng_msg_report_sv(void* instance, GpsSvStatus &sv, GpsLocationExtended &locExtended, void* ext) :
        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_SV), svStatus(sv), locationExtended(locExtended), svExt(ext)
    {
        LOC_LOGV("num sv: %d\n  ephemeris mask: %dxn  almanac mask: %x\n  used in fix mask: %x\n      sv: prn         snr       elevation      azimuth",
                 svStatus.num_svs, svStatus.ephemeris_mask, svStatus.almanac_mask, svStatus.used_in_fix_mask);
        for (int i = 0; i < svStatus.num_svs && i < GPS_MAX_SVS; i++) {
            LOC_LOGV("   %d:   %d    %f    %f    %f\n  ",
                     i,
                     svStatus.sv_list[i].prn,
                     svStatus.sv_list[i].snr,
                     svStatus.sv_list[i].elevation,
                     svStatus.sv_list[i].azimuth);
        }
    }
};

struct loc_eng_msg_report_status : public loc_eng_msg {
    const GpsStatusValue status;
    inline loc_eng_msg_report_status(void* instance, GpsStatusValue engineStatus) :
        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_STATUS), status(engineStatus)
    {
        LOC_LOGV("status: %s", loc_get_gps_status_name(status));
    }
};

struct loc_eng_msg_report_nmea : public loc_eng_msg {
    char* const nmea;
    const int length;
    inline loc_eng_msg_report_nmea(void* instance,
                                   const char* data,
                                   int len) :
        loc_eng_msg(instance, LOC_ENG_MSG_REPORT_NMEA),
        nmea(new char[len]), length(len)
    {
        memcpy((void*)nmea, (void*)data, len);
        LOC_LOGV("length: %d\n  nmea: %p - %c%c%c",
                 length, nmea, nmea[3], nmea[4], nmea[5]);
    }
    inline ~loc_eng_msg_report_nmea()
    {
        delete[] nmea;
    }
};

struct loc_eng_msg_request_bit : public loc_eng_msg {
    const loc_if_req_type_e_type ifType;
    const int ipv4Addr;
    char* const ipv6Addr;
    inline loc_eng_msg_request_bit(void* instance,
                                   loc_if_req_type_e_type type,
                                   int ipv4,
                                   char* ipv6) :
        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_BIT),
        ifType(type), ipv4Addr(ipv4),
        ipv6Addr(NULL == ipv6 ? NULL : new char[16])
    {
        if (NULL != ipv6Addr)
            memcpy(ipv6Addr, ipv6, 16);
        LOC_LOGV("ifType: %d, ipv4: %d.%d.%d.%d, ipv6: %s", ifType,
                 (unsigned char)(ipv4>>24),
                 (unsigned char)(ipv4>>16),
                 (unsigned char)(ipv4>>8),
                 (unsigned char)ipv4,
                 NULL != ipv6Addr ? ipv6Addr : "");
    }

    inline ~loc_eng_msg_request_bit()
    {
        if (NULL != ipv6Addr) {
            delete[] ipv6Addr;
        }
    }
};

struct loc_eng_msg_request_wifi : public loc_eng_msg {
    const loc_if_req_type_e_type ifType;
    const loc_if_req_sender_id_e_type senderId;
    char* const ssid;
    char* const password;
    inline loc_eng_msg_request_wifi(void* instance,
                                   loc_if_req_type_e_type type,
                                   loc_if_req_sender_id_e_type sender_id,
                                   char* s,
                                   char* p) :
        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_WIFI),
        ifType(type), senderId(sender_id),
        ssid(NULL == s ? NULL : new char[SSID_BUF_SIZE]),
        password(NULL == p ? NULL : new char[SSID_BUF_SIZE])
    {
        if (NULL != ssid)
            strlcpy(ssid, s, SSID_BUF_SIZE);
        if (NULL != password)
            strlcpy(password, p, SSID_BUF_SIZE);
        LOC_LOGV("ifType: %d, senderId: %d, ssid: %s, password: %s",
                 ifType,
                 senderId,
                 NULL != ssid ? ssid : "",
                 NULL != password ? password : "");
    }

    inline ~loc_eng_msg_request_wifi()
    {
        if (NULL != ssid) {
            delete[] ssid;
        }
        if (NULL != password) {
            delete[] password;
        }
    }
};

struct loc_eng_msg_release_bit : public loc_eng_msg {
    const loc_if_req_type_e_type ifType;
    const int ipv4Addr;
    char* const ipv6Addr;
    inline loc_eng_msg_release_bit(void* instance,
                                   loc_if_req_type_e_type type,
                                   int ipv4,
                                   char* ipv6) :
        loc_eng_msg(instance, LOC_ENG_MSG_RELEASE_BIT),
        ifType(type), ipv4Addr(ipv4),
        ipv6Addr(NULL == ipv6 ? NULL : new char[16])
    {
        if (NULL != ipv6Addr)
            memcpy(ipv6Addr, ipv6, 16);
        LOC_LOGV("ifType: %d, ipv4: %d.%d.%d.%d, ipv6: %s", ifType,
                 (unsigned char)(ipv4>>24),
                 (unsigned char)(ipv4>>16),
                 (unsigned char)(ipv4>>8),
                 (unsigned char)ipv4,
                 NULL != ipv6Addr ? ipv6Addr : "");
    }

    inline ~loc_eng_msg_release_bit()
    {
        if (NULL != ipv6Addr) {
            delete[] ipv6Addr;
        }
    }
};

struct loc_eng_msg_release_wifi : public loc_eng_msg {
    const loc_if_req_type_e_type ifType;
    const loc_if_req_sender_id_e_type senderId;
    char* const ssid;
    char* const password;
    inline loc_eng_msg_release_wifi(void* instance,
                                   loc_if_req_type_e_type type,
                                   loc_if_req_sender_id_e_type sender_id,
                                   char* s,
                                   char* p) :
        loc_eng_msg(instance, LOC_ENG_MSG_RELEASE_WIFI),
        ifType(type), senderId(sender_id),
        ssid(NULL == s ? NULL : new char[SSID_BUF_SIZE]),
        password(NULL == p ? NULL : new char[SSID_BUF_SIZE])
    {
        if (NULL != s)
            strlcpy(ssid, s, SSID_BUF_SIZE);
        if (NULL != p)
            strlcpy(password, p, SSID_BUF_SIZE);
        LOC_LOGV("ifType: %d, senderId: %d, ssid: %s, password: %s",
                 ifType,
                 senderId,
                 NULL != ssid ? ssid : "",
                 NULL != password ? password : "");
    }

    inline ~loc_eng_msg_release_wifi()
    {
        if (NULL != ssid) {
            delete[] ssid;
        }
        if (NULL != password) {
            delete[] password;
        }
    }
};


struct loc_eng_msg_request_atl : public loc_eng_msg {
    const int handle;
    const AGpsType type;
    inline loc_eng_msg_request_atl(void* instance, int hndl,
                                   AGpsType agps_type) :
        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_ATL),
        handle(hndl), type(agps_type)
    {
        LOC_LOGV("handle: %d\n  agps type: %s",
                 handle,
                 loc_get_agps_type_name(type));
    }
};

struct loc_eng_msg_release_atl : public loc_eng_msg {
    const int handle;
    inline loc_eng_msg_release_atl(void* instance, int hndl) :
        loc_eng_msg(instance, LOC_ENG_MSG_RELEASE_ATL), handle(hndl)
    {
        LOC_LOGV("handle: %d", handle);
    }
};

struct loc_eng_msg_request_ni : public loc_eng_msg {
    const GpsNiNotification notify;
    const void *passThroughData;
    inline loc_eng_msg_request_ni(void* instance,
                                  GpsNiNotification &notif, const void* data) :
        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_NI),
        notify(notif), passThroughData(data)
    {
        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",
                 notify.notification_id,
                 loc_get_ni_type_name(notify.ni_type),
                 notify.notify_flags,
                 notify.timeout,
                 loc_get_ni_response_name(notify.default_response),
                 loc_get_ni_encoding_name(notify.requestor_id_encoding),
                 loc_get_ni_encoding_name(notify.text_encoding),
                 passThroughData);
    }
};

struct loc_eng_msg_inform_ni_response : public loc_eng_msg {
    const GpsUserResponseType response;
    const void *passThroughData;
    inline loc_eng_msg_inform_ni_response(void* instance,
                                          GpsUserResponseType resp,
                                          const void* data) :
        loc_eng_msg(instance, LOC_ENG_MSG_INFORM_NI_RESPONSE),
        response(resp), passThroughData(data)
    {
        LOC_LOGV("response: %s\n  passThroughData: %p",
                 loc_get_ni_response_name(response),
                 passThroughData);
    }
    inline ~loc_eng_msg_inform_ni_response()
    {
        // this is a bit weird since passThroughData is not
        // allocated by this class.  But there is no better way.
        // passThroughData actually won't be NULL here.
        // But better safer than sorry.
        if (NULL != passThroughData) {
            free((void*)passThroughData);
        }
    }
};

struct loc_eng_msg_set_apn : public loc_eng_msg {
    char* const apn;
    inline loc_eng_msg_set_apn(void* instance, const char* name, int len) :
        loc_eng_msg(instance, LOC_ENG_MSG_SET_APN),
        apn(new char[len+1])
    {
        memcpy((void*)apn, (void*)name, len);
        apn[len] = 0;
        LOC_LOGV("apn: %s", apn);
    }
    inline ~loc_eng_msg_set_apn()
    {
        delete[] apn;
    }
};



struct loc_eng_msg_set_server_ipv4 : public loc_eng_msg {
    const unsigned int nl_addr;
    const int port;
    const LocServerType serverType;
    inline loc_eng_msg_set_server_ipv4(void* instance,
                                       unsigned int ip,
                                       int p,
                                       LocServerType type) :
        loc_eng_msg(instance, LOC_ENG_MSG_SET_SERVER_IPV4),
        nl_addr(ip), port(p), serverType(type)
    {
        LOC_LOGV("addr: %x\n  , port: %d\n type: %s", nl_addr, port, loc_get_server_type_name(serverType));
    }
};


struct loc_eng_msg_set_server_url : public loc_eng_msg {
    const int len;
    char* const url;
    inline loc_eng_msg_set_server_url(void* instance,
                                      const char* urlString,
                                      int url_len) :
        loc_eng_msg(instance, LOC_ENG_MSG_SET_SERVER_URL),
        len(url_len), url(new char[len+1])
    {
        memcpy((void*)url, (void*)urlString, url_len);
        url[len] = 0;
        LOC_LOGV("url: %s", url);
    }
    inline ~loc_eng_msg_set_server_url()
    {
        delete[] url;
    }
};

struct loc_eng_msg_inject_xtra_data : public loc_eng_msg {
    char* const data;
    const int length;
    inline loc_eng_msg_inject_xtra_data(void* instance, char* d, int l) :
        loc_eng_msg(instance, LOC_ENG_MSG_INJECT_XTRA_DATA),
        data(new char[l]), length(l)
    {
        memcpy((void*)data, (void*)d, l);
        LOC_LOGV("length: %d\n  data: %p", length, data);
    }
    inline ~loc_eng_msg_inject_xtra_data()
    {
        delete[] data;
    }
};

struct loc_eng_msg_atl_open_success : public loc_eng_msg {
    const AGpsStatusValue agpsType;
    const int length;
    char* const apn;
    const AGpsBearerType bearerType;
    inline loc_eng_msg_atl_open_success(void* instance,
                                        AGpsStatusValue atype,
                                        const char* name,
                                        int len,
                                        AGpsBearerType btype) :
        loc_eng_msg(instance, LOC_ENG_MSG_ATL_OPEN_SUCCESS),
        agpsType(atype), length(len),
        apn(new char[len+1]), bearerType(btype)
    {
        memcpy((void*)apn, (void*)name, len);
        apn[len] = 0;
        LOC_LOGV("agps type: %s\n  apn: %s\n  bearer type: %s",
                 loc_get_agps_type_name(agpsType),
                 apn,
                 loc_get_agps_bear_name(bearerType));
    }
    inline ~loc_eng_msg_atl_open_success()
    {
        delete[] apn;
    }
};

struct loc_eng_msg_atl_open_failed : public loc_eng_msg {
    const AGpsStatusValue agpsType;
    inline loc_eng_msg_atl_open_failed(void* instance,
                                       AGpsStatusValue atype) :
        loc_eng_msg(instance, LOC_ENG_MSG_ATL_OPEN_FAILED),
        agpsType(atype)
    {
        LOC_LOGV("agps type %s",
                 loc_get_agps_type_name(agpsType));
    }
};

struct loc_eng_msg_atl_closed : public loc_eng_msg {
    const AGpsStatusValue agpsType;
    inline loc_eng_msg_atl_closed(void* instance,
                                  AGpsStatusValue atype) :
        loc_eng_msg(instance, LOC_ENG_MSG_ATL_CLOSED),
        agpsType(atype)
    {
        LOC_LOGV("agps type %s",
                 loc_get_agps_type_name(agpsType));
    }
};

struct loc_eng_msg_set_data_enable : public loc_eng_msg {
    const int enable;
    char* const apn;
    const int length;
    inline loc_eng_msg_set_data_enable(void* instance,
                                       const char* name,
                                       int len,
                                       int yes) :
        loc_eng_msg(instance, LOC_ENG_MSG_ENABLE_DATA),
        enable(yes), apn(new char[len+1]), length(len)
    {
        memcpy((void*)apn, (void*)name, len);
        apn[len] = 0;
        LOC_LOGV("apn: %s\n  enable: %d", apn, enable);
    }
    inline ~loc_eng_msg_set_data_enable()
    {
        delete[] apn;
    }
};

struct loc_eng_msg_request_network_position : public loc_eng_msg {
    const UlpNetworkRequestPos networkPosRequest;
    inline loc_eng_msg_request_network_position (void* instance, UlpNetworkRequestPos networkPosReq) :
        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_NETWORK_POSIITON),
        networkPosRequest(networkPosReq)
    {
        LOC_LOGV("network position request: desired pos source %d\n  request type: %d\n interval ms: %d ",
             networkPosReq.desired_position_source,
             networkPosReq.request_type,
             networkPosReq.interval_ms);
    }
};

struct loc_eng_msg_request_phone_context : public loc_eng_msg {
    const UlpPhoneContextRequest contextRequest;
    inline loc_eng_msg_request_phone_context (void* instance, UlpPhoneContextRequest contextReq) :
        loc_eng_msg(instance, LOC_ENG_MSG_REQUEST_PHONE_CONTEXT),
        contextRequest(contextReq)
    {
        LOC_LOGV("phone context request: request type 0x%x context type: 0x%x ",
             contextRequest.request_type,
             contextRequest.context_type);
    }
};

struct ulp_msg_update_criteria : public loc_eng_msg {
    const UlpLocationCriteria locationCriteria;
    inline ulp_msg_update_criteria (void* instance, UlpLocationCriteria criteria) :
        loc_eng_msg(instance, ULP_MSG_UPDATE_CRITERIA),
        locationCriteria(criteria)
    {
        LOC_LOGV("location criteria: aciton %d\n  valid mask: %d\n provider source: %d\n accuracy %d\n recurrence type %d\n min interval %d\n power consumption %d\n intermediate pos %d ",
             locationCriteria.action,
             locationCriteria.valid_mask,
             locationCriteria.provider_source,
             locationCriteria.preferred_horizontal_accuracy,
             locationCriteria.recurrence_type,
             locationCriteria.min_interval,
             locationCriteria.preferred_power_consumption,
             locationCriteria.intermediate_pos_report_enabled);
    }
};

struct ulp_msg_inject_raw_command : public loc_eng_msg {
    const char* rawCommand;
    const int rawCommandLength;
    inline ulp_msg_inject_raw_command (void* instance, char* command, int length) :
        loc_eng_msg(instance, ULP_MSG_INJECT_RAW_COMMAND),
        rawCommand(new char[length]),
        rawCommandLength(length)
    {
      memcpy((void*)rawCommand, (void*)command, length);
      LOC_LOGV("inject raw command: command %s\n  command length: %d\n ",
             rawCommand,
             rawCommandLength);
    }

    inline ~ulp_msg_inject_raw_command()
    {
        delete[] rawCommand;
    }
};

struct ulp_msg_inject_phone_context_settings : public loc_eng_msg {
    const UlpPhoneContextSettings phoneSetting;
    inline ulp_msg_inject_phone_context_settings(void* instance, UlpPhoneContextSettings setting) :
        loc_eng_msg(instance, ULP_MSG_INJECT_PHONE_CONTEXT_SETTINGS),
        phoneSetting(setting)
    {
        LOC_LOGV("context type: %d\n  gps enabled: %d\n network position available %d\n wifi setting enabled %d\n battery charging %d"
                 "is_agps_setting_enabled %d, is_enh_location_services_enabled %d\n",
             phoneSetting.context_type,
             phoneSetting.is_gps_enabled,
             phoneSetting.is_network_position_available,
             phoneSetting.is_wifi_setting_enabled,
             phoneSetting.is_battery_charging,
             phoneSetting.is_agps_enabled,
             phoneSetting.is_enh_location_services_enabled);
    }
};

struct ulp_msg_inject_network_position : public loc_eng_msg {
    const UlpNetworkPositionReport networkPosition;
    inline ulp_msg_inject_network_position(void* instance, UlpNetworkPositionReport networkPos) :
        loc_eng_msg(instance, ULP_MSG_INJECT_NETWORK_POSITION),
        networkPosition(networkPos)
    {
        LOC_LOGV("flags: %d\n  source: %d\n  latitude: %f\n  longitude: %f\n  accuracy %d",
             networkPosition.valid_flag,
             networkPosition.position.pos_source,
             networkPosition.position.latitude,
             networkPosition.position.longitude,
             networkPosition.position.HEPE);
    }
};

struct ulp_msg_report_quipc_position : public loc_eng_msg {
    const GpsLocation location;
    const int  quipc_error_code;
    inline ulp_msg_report_quipc_position(void* instance, GpsLocation &loc,
                                         int  quipc_err) :
        loc_eng_msg(instance, ULP_MSG_REPORT_QUIPC_POSITION),
        location(loc), quipc_error_code(quipc_err)
    {
        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  Quipc error: %d",
                 location.flags, location.position_source, location.latitude, location.longitude,
                 location.altitude, location.speed, location.bearing, location.accuracy,
                 location.timestamp, location.rawDataSize, location.rawData,
                 quipc_error_code);
    }
};

struct loc_eng_msg_privacy : public loc_eng_msg {
    const int8_t privacy_setting;
    inline loc_eng_msg_privacy(void* instance, int8_t privacy_setting) :
        loc_eng_msg(instance, LOC_ENG_MSG_PRIVACY),
        privacy_setting(privacy_setting)
        {
            LOC_LOGV("privacy_setting: %d", privacy_setting);
        }
};

void loc_eng_msg_sender(void* loc_eng_data_p, void* msg);
int loc_eng_msgget(int * p_req_msgq);
int loc_eng_msgremove(int req_msgq);
int loc_eng_msgsnd(int msgqid, void * msgp);
int loc_eng_msgrcv(int msgqid, void ** msgp);
int loc_eng_msgsnd_raw(int msgqid, void * msgp, unsigned int msgsz);
int loc_eng_msgrcv_raw(int msgqid, void *msgp, unsigned int msgsz);
int loc_eng_msgflush(int msgqid);
int loc_eng_msgunblock(int msgqid);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LOC_ENG_MSG_H */