forked from u-blox/ubxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_gnss_pos.c
1046 lines (952 loc) · 45.8 KB
/
u_gnss_pos.c
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Implementation of the GNSS APIs to read position.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "limits.h" // INT_MIN
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // memcpy()
#include "u_cfg_sw.h"
#include "u_cfg_os_platform_specific.h"
#include "u_error_common.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_debug.h"
#include "u_time.h"
#include "u_at_client.h"
#include "u_ubx_protocol.h"
#include "u_linked_list.h"
#include "u_geofence.h"
#include "u_geofence_shared.h"
#include "u_gnss_module_type.h"
#include "u_gnss_type.h"
#include "u_gnss_private.h"
#include "u_gnss_cfg_val_key.h"
#include "u_gnss_cfg.h"
#include "u_gnss_cfg_private.h"
#include "u_gnss_msg.h"
#include "u_gnss_msg_private.h"
#include "u_gnss_geofence.h"
#include "u_geofence_shared.h"
#include "u_gnss_pos.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#ifndef U_GNSS_POS_CALLBACK_TASK_STACK_SIZE_BYTES
/** The stack size for the position establishment task. The limiting
* factor is ESP-IDF, and in particular on Arduino, which seems to
* require the most stack, and if power saving may be on then
* additional stack will be used by the AT client.
*/
# define U_GNSS_POS_CALLBACK_TASK_STACK_SIZE_BYTES (1024 * 5)
#endif
#ifndef U_GNSS_POS_CALLBACK_TASK_PRIORITY
/** The task priority for the position establishment task.
*/
# define U_GNSS_POS_CALLBACK_TASK_PRIORITY (U_CFG_OS_PRIORITY_MIN + 2)
#endif
#ifndef U_GNSS_POS_CALLBACK_TASK_STACK_DELAY_SECONDS
/** The delay between position attempts in the asynchronous task.
*/
# define U_GNSS_POS_CALLBACK_TASK_STACK_DELAY_SECONDS 5
#endif
#ifndef U_GNSS_POS_RRLP_HEADER_SIZE_BYTES
/** The number of bytes of UBX protocol header that
* will be added to the front of the raw RRLP binary data.
*/
#define U_GNSS_POS_RRLP_HEADER_SIZE_BYTES (U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES - 2)
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** Parameters to pass to the asynchronous position establishment task.
*/
typedef struct {
uDeviceHandle_t gnssHandle;
uGnssPrivateInstance_t *pInstance;
void (*pCallback) (uDeviceHandle_t gnssHandle,
int32_t errorCode,
int32_t latitudeX1e7,
int32_t longitudeX1e7,
int32_t altitudeMillimetres,
int32_t radiusMillimetres,
int32_t speedMillimetresPerSecond,
int32_t svs,
int64_t timeUtc);
} uGnssPosGetTaskParameters_t;
/* ----------------------------------------------------------------
* STATIC VARIABLES
* -------------------------------------------------------------- */
/** Table to convert U_GNSS_RRLP_MODE_MEASxxx into a message class
* of UBX-RXM-MEASxxx.
*/
static const int32_t gRrlpModeToUbxRxmMessageClass[] = {
0x14, // UBX-RXM-MEASX
0x86, // UBX_RXM_MEAS50
0x84, // UBX_RXM_MEAS20
0x82, // UBX_RXM_MEASC12
0x80 // UBX_RXM_MEASD12
};
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// Decode the contents of a UBX-NAV-PVT message. pMessage must
// be a pointer to the 92 byte body of a UBX-NAV-PVT message.
static int32_t posDecode(char *pMessage,
int32_t *pLatitudeX1e7, int32_t *pLongitudeX1e7,
int32_t *pAltitudeMillimetres,
int32_t *pRadiusMillimetres,
int32_t *pAltitudeUncertaintyMillimetres,
int32_t *pSpeedMillimetresPerSecond,
int32_t *pSvs, int64_t *pTimeUtc, bool printIt)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT;
int32_t months;
int32_t year;
int32_t y;
int64_t t = -1;
if ((*(pMessage + 11) & 0x03) == 0x03) {
// Time and date are valid; we don't indicate
// success based on this but we report it anyway
// if it is valid
t = 0;
// Year is 1999-2099, so need to adjust to get year since 1970
year = ((int32_t) uUbxProtocolUint16Decode(pMessage + 4) - 1999) + 29;
// Month (1 to 12), so take away 1 to make it zero-based
months = *(pMessage + 6) - 1;
months += year * 12;
// Work out the number of seconds due to the year/month count
t += uTimeMonthsToSecondsUtc(months);
// Day (1 to 31)
t += ((int32_t) * (pMessage + 7) - 1) * 3600 * 24;
// Hour (0 to 23)
t += ((int32_t) * (pMessage + 8)) * 3600;
// Minute (0 to 59)
t += ((int32_t) * (pMessage + 9)) * 60;
// Second (0 to 60)
t += *(pMessage + 10);
if (printIt) {
uPortLog("U_GNSS_POS: UTC time = %d.\n", (int32_t) t);
}
}
if (pTimeUtc != NULL) {
*pTimeUtc = t;
}
// From here onwards Lint complains about accesses
// into message[] and it doesn't seem to be possible
// to suppress those warnings with -esym(690, message)
// or even -e(690), hence do it the blunt way
//lint -save -e690
if ((t >= 0) && (*(pMessage + 21) & 0x01)) {
if (printIt) {
uPortLog("U_GNSS_POS: %dD fix achieved.\n", *(pMessage + 20));
}
y = (int32_t) * (pMessage + 23);
if (printIt) {
uPortLog("U_GNSS_POS: satellite(s) = %d.\n", y);
}
if (pSvs != NULL) {
*pSvs = y;
}
y = (int32_t) uUbxProtocolUint32Decode(pMessage + 24);
if (printIt) {
uPortLog("U_GNSS_POS: longitude = %d (degrees * 10^7).\n", y);
}
if (pLongitudeX1e7 != NULL) {
*pLongitudeX1e7 = y;
}
y = (int32_t) uUbxProtocolUint32Decode(pMessage + 28);
if (printIt) {
uPortLog("U_GNSS_POS: latitude = %d (degrees * 10^7).\n", y);
}
if (pLatitudeX1e7 != NULL) {
*pLatitudeX1e7 = y;
}
y = INT_MIN;
if (*(pMessage + 20) == 0x03) {
y = (int32_t) uUbxProtocolUint32Decode(pMessage + 36);
if (printIt) {
uPortLog("U_GNSS_POS: altitude = %d (mm).\n", y);
}
}
if (pAltitudeMillimetres != NULL) {
*pAltitudeMillimetres = y;
}
y = (int32_t) uUbxProtocolUint32Decode(pMessage + 40);
if (printIt) {
uPortLog("U_GNSS_POS: radius = %d (mm).\n", y);
}
if (pRadiusMillimetres != NULL) {
*pRadiusMillimetres = y;
}
y = (int32_t) uUbxProtocolUint32Decode(pMessage + 44);
if (printIt) {
uPortLog("U_GNSS_POS: altitude uncertainty = %d (mm).\n", y);
}
if (pAltitudeUncertaintyMillimetres != NULL) {
*pAltitudeUncertaintyMillimetres = y;
}
y = (int32_t) uUbxProtocolUint32Decode(pMessage + 60);
if (printIt) {
uPortLog("U_GNSS_POS: speed = %d (mm/s).\n", y);
}
if (pSpeedMillimetresPerSecond != NULL) {
*pSpeedMillimetresPerSecond = y;
}
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
//lint -restore
}
return errorCode;
}
// Establish position.
static int32_t posGet(uGnssPrivateInstance_t *pInstance,
int32_t *pLatitudeX1e7, int32_t *pLongitudeX1e7,
int32_t *pAltitudeMillimetres,
int32_t *pRadiusMillimetres,
int32_t *pAltitudeUncertaintyMillimetres,
int32_t *pSpeedMillimetresPerSecond,
int32_t *pSvs, int64_t *pTimeUtc, bool printIt)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT;
// Enough room for the body of the UBX-NAV-PVT message
char message[92] = {0};
errorCode = uGnssPrivateSendReceiveUbxMessage(pInstance,
0x01, 0x07, NULL, 0,
message, sizeof(message));
if (errorCode == sizeof(message)) {
// Got the correct message body length, process it
errorCode = posDecode(message,
pLatitudeX1e7, pLongitudeX1e7,
pAltitudeMillimetres,
pRadiusMillimetres,
pAltitudeUncertaintyMillimetres,
pSpeedMillimetresPerSecond,
pSvs, pTimeUtc, printIt);
} else {
if (errorCode >= 0) {
errorCode = (int32_t) U_ERROR_COMMON_DEVICE_ERROR;
}
}
return errorCode;
}
// Establish position as a task.
// IMPORTANT: this does NOT lock gUGnssPrivateMutex and hence it
// is important that it is stopped before a pInstance is released.
static void posGetTask(void *pParameter)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT;
uGnssPosGetTaskParameters_t taskParameters;
int64_t startTime;
int32_t latitudeX1e7 = INT_MIN;
int32_t longitudeX1e7 = INT_MIN;
int32_t altitudeMillimetres = INT_MIN;
int32_t radiusMillimetres = -1;
int32_t altitudeUncertaintyMillimetres = 0;
int32_t speedMillimetresPerSecond = INT_MIN;
int32_t svs = -1;
int64_t timeUtc = -1;
// Copy the parameter into our local variable and free it
memcpy(&taskParameters, pParameter, sizeof(taskParameters));
uPortFree(pParameter);
// Lock the mutex to indicate that we're running
U_PORT_MUTEX_LOCK(taskParameters.pInstance->posMutex);
startTime = uPortGetTickTimeMs();
taskParameters.pInstance->posTaskFlags |= U_GNSS_POS_TASK_FLAG_HAS_RUN;
while ((taskParameters.pInstance->posTaskFlags & U_GNSS_POS_TASK_FLAG_KEEP_GOING) &&
(errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) &&
((uPortGetTickTimeMs() - startTime) / 1000 < U_GNSS_POS_TIMEOUT_SECONDS)) {
// Call posGet() to do the work
errorCode = posGet(taskParameters.pInstance,
&latitudeX1e7,
&longitudeX1e7,
&altitudeMillimetres,
&radiusMillimetres,
&altitudeUncertaintyMillimetres,
&speedMillimetresPerSecond,
&svs,
&timeUtc, false);
if (errorCode != 0) {
uPortTaskBlock(U_GNSS_POS_CALLBACK_TASK_STACK_DELAY_SECONDS * 1000);
}
}
// Call the callback
taskParameters.pCallback(taskParameters.gnssHandle, errorCode, latitudeX1e7,
longitudeX1e7, altitudeMillimetres, radiusMillimetres,
speedMillimetresPerSecond, svs, timeUtc);
if (errorCode == 0) {
// As well as the above, test the position against any
// fences associated with the instance, which may result
// in further callbacks being called and if GEODESIC is
// employed, may consume an additional ~5 kbytes of stack
uGeofenceContextTest(taskParameters.gnssHandle,
(uGeofenceContext_t *) taskParameters.pInstance->pFenceContext,
U_GEOFENCE_TEST_TYPE_NONE, false,
((int64_t) latitudeX1e7) * 100,
((int64_t) longitudeX1e7) * 100,
altitudeMillimetres,
radiusMillimetres,
altitudeUncertaintyMillimetres);
}
U_PORT_MUTEX_UNLOCK(taskParameters.pInstance->posMutex);
// Delete ourselves
uPortTaskDelete(NULL);
}
// Callback that should receive a UBX-NAV-PVT message.
static void messageCallback(uDeviceHandle_t gnssHandle,
const uGnssMessageId_t *pMessageId,
int32_t errorCodeOrLength,
void *pCallbackParam)
{
uGnssPrivateInstance_t *pInstance = (uGnssPrivateInstance_t *) pCallbackParam;
char message[92 + U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES] = {0};
int32_t latitudeX1e7 = INT_MIN;
int32_t longitudeX1e7 = INT_MIN;
int32_t altitudeMillimetres = INT_MIN;
int32_t radiusMillimetres = -1;
int32_t altitudeUncertaintyMillimetres = 0;
int32_t speedMillimetresPerSecond = INT_MIN;
int32_t svs = -1;
int64_t timeUtc = -1;
(void) pMessageId;
if (errorCodeOrLength > 0) {
if (errorCodeOrLength > sizeof(message)) {
errorCodeOrLength = sizeof(message);
}
// Get the while UBX-NAV-PVT message
uGnssMsgReceiveCallbackRead(gnssHandle,
message,
errorCodeOrLength);
// Decode the body
errorCodeOrLength = posDecode(message + U_UBX_PROTOCOL_HEADER_LENGTH_BYTES,
&latitudeX1e7, &longitudeX1e7,
&altitudeMillimetres,
&radiusMillimetres,
&altitudeUncertaintyMillimetres,
&speedMillimetresPerSecond,
&svs, &timeUtc, false);
// Call the callback
// Note: there can be two handles involved here, e.g. if
// GNSS is inside a cellular device, hence we make sure
// we pass back the one that came in
pInstance->pStreamedPosition->pCallback(pInstance->pStreamedPosition->gnssHandle,
errorCodeOrLength,
latitudeX1e7,
longitudeX1e7,
altitudeMillimetres,
radiusMillimetres,
speedMillimetresPerSecond,
svs,
timeUtc);
if (errorCodeOrLength == 0) {
// As well as the above, test the position against any
// fences associated with the instance, which may result
// in further callbacks being called and if GEODESIC is
// employed, may consume an additional ~5 kbytes of stack
uGeofenceContextTest(gnssHandle,
(uGeofenceContext_t *) pInstance->pFenceContext,
U_GEOFENCE_TEST_TYPE_NONE, false,
((int64_t) latitudeX1e7) * 100,
((int64_t) longitudeX1e7) * 100,
altitudeMillimetres,
radiusMillimetres,
altitudeUncertaintyMillimetres);
}
}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: WORKAROUND FOR LINKER ISSUE
* -------------------------------------------------------------- */
void uGnssPosPrivateLink()
{
//dummy
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
// Get the current position.
int32_t uGnssPosGet(uDeviceHandle_t gnssHandle,
int32_t *pLatitudeX1e7, int32_t *pLongitudeX1e7,
int32_t *pAltitudeMillimetres,
int32_t *pRadiusMillimetres,
int32_t *pSpeedMillimetresPerSecond,
int32_t *pSvs, int64_t *pTimeUtc,
bool (*pKeepGoingCallback) (uDeviceHandle_t))
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uGnssPrivateInstance_t *pInstance;
int32_t latitudeX1e7 = INT_MIN;
int32_t longitudeX1e7 = INT_MIN;
int32_t altitudeMillimetres = INT_MIN;
int32_t radiusMillimetres = -1;
int32_t altitudeUncertaintyMillimetres = 0;
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
uint8_t message[4]; // Room for the body of a UBX-CFG-ANT message
#endif
int64_t startTime;
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if (pInstance != NULL) {
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
if ((pInstance->transportType == U_GNSS_TRANSPORT_AT) ||
(uGnssPrivateGetIntermediateAtHandle(pInstance) != NULL)) {
// Temporary change: on prototype versions of the
// SARA-R510M8S module (production week (printed on the
// module label, upper right) earlier than 20/27)
// the LNA in the GNSS chip is not automatically switched
// on by the firmware in the cellular module, so we need
// to switch it on ourselves by sending UBX-CFG-ANT
// with contents 02000f039
message[0] = 0x02;
message[1] = 0;
message[2] = 0xf0;
message[3] = 0x39;
uGnssPrivateSendUbxMessage(pInstance, 0x06, 0x13,
(const char *) message, 4);
}
#endif
startTime = uPortGetTickTimeMs();
errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT;
while ((errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) &&
(((pKeepGoingCallback == NULL) &&
(uPortGetTickTimeMs() - startTime) / 1000 < U_GNSS_POS_TIMEOUT_SECONDS) ||
((pKeepGoingCallback != NULL) && pKeepGoingCallback(gnssHandle)))) {
// Call posGet() to do the work
errorCode = posGet(pInstance,
&latitudeX1e7,
&longitudeX1e7,
&altitudeMillimetres,
&radiusMillimetres,
&altitudeUncertaintyMillimetres,
pSpeedMillimetresPerSecond,
pSvs, pTimeUtc, true);
if (errorCode == 0) {
// As well as the above, test the position against any
// fences associated with the instance, which may result
// in further callbacks being called and if GEODESIC is
// employed, may consume an additional ~5 kbytes of stack
uGeofenceContextTest(gnssHandle,
(uGeofenceContext_t *) pInstance->pFenceContext,
U_GEOFENCE_TEST_TYPE_NONE, false,
((int64_t) latitudeX1e7) * 100,
((int64_t) longitudeX1e7) * 100,
altitudeMillimetres,
radiusMillimetres,
altitudeUncertaintyMillimetres);
}
if (pLatitudeX1e7 != NULL) {
*pLatitudeX1e7 = latitudeX1e7;
}
if (pLongitudeX1e7 != NULL) {
*pLongitudeX1e7 = longitudeX1e7;
}
if (pAltitudeMillimetres != NULL) {
*pAltitudeMillimetres = altitudeMillimetres;
}
if (pRadiusMillimetres != NULL) {
*pRadiusMillimetres = radiusMillimetres;
}
}
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
return errorCode;
}
// Get the current position, non-blocking version.
int32_t uGnssPosGetStart(uDeviceHandle_t gnssHandle,
void (*pCallback) (uDeviceHandle_t gnssHandle,
int32_t errorCode,
int32_t latitudeX1e7,
int32_t longitudeX1e7,
int32_t altitudeMillimetres,
int32_t radiusMillimetres,
int32_t speedMillimetresPerSecond,
int32_t svs,
int64_t timeUtc))
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uGnssPrivateInstance_t *pInstance;
uGnssPosGetTaskParameters_t *pParameters;
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
uint8_t message[4]; // Room for the body of a UBX-CFG-ANT message
#endif
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if ((pInstance != NULL) && (pCallback != NULL)) {
errorCode = (int32_t) U_ERROR_COMMON_NO_MEMORY;
if (pInstance->posTaskFlags & U_GNSS_POS_TASK_FLAG_HAS_RUN) {
uGnssPrivateCleanUpPosTask(pInstance);
}
if (pInstance->posTaskFlags == 0) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
// Create a mutex to allow us to monitor whether the
// task is running, if there isn't already one
// sitting around from a previous run
if (pInstance->posMutex == NULL) {
errorCode = uPortMutexCreate(&(pInstance->posMutex));
}
if (errorCode == 0) {
// Malloc memory to copy the parameters into:
// this memory will be free'd by the task
// once it has started
pParameters = (uGnssPosGetTaskParameters_t *) pUPortMalloc(sizeof(*pParameters));
if (pParameters != NULL) {
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
if ((pInstance->transportType == U_GNSS_TRANSPORT_AT) ||
(uGnssPrivateGetIntermediateAtHandle(pInstance) != NULL)) {
// Temporary change: on prototype versions of the
// SARA-R510M8S module (production week (printed on the
// module label, upper right) earlier than 20/27)
// the LNA in the GNSS chip is not automatically switched
// on by the firmware in the cellular module, so we need
// to switch it on ourselves by sending UBX-CFG-ANT
// with contents 02000f039
message[0] = 0x02;
message[1] = 0;
message[2] = 0xf0;
message[3] = 0x39;
uGnssPrivateSendUbxMessage(pInstance, 0x06, 0x13,
(const char *) message, 4);
}
#endif
// Fill in the callback and start a task
// that will establish position (or not)
// and call it
memset(pParameters, 0, sizeof(*pParameters));
pInstance->posTaskFlags |= U_GNSS_POS_TASK_FLAG_KEEP_GOING;
pParameters->gnssHandle = gnssHandle;
pParameters->pInstance = pInstance;
pParameters->pCallback = pCallback;
errorCode = uPortTaskCreate(posGetTask,
"gnssPosCallback",
U_GNSS_POS_CALLBACK_TASK_STACK_SIZE_BYTES,
(void *) pParameters,
U_GNSS_POS_CALLBACK_TASK_PRIORITY,
&(pInstance->posTask));
if (errorCode >= 0) {
while (!(pInstance->posTaskFlags & U_GNSS_POS_TASK_FLAG_HAS_RUN)) {
// Make sure the task has run before we
// exit so that stopping it works properly
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
} else {
// If we couldn't create the task, clean the memory
// for the parameters and the mutex and re-zero
// posTaskFlags.
uPortFree(pParameters);
uPortMutexDelete(pInstance->posMutex);
pInstance->posMutex = NULL;
pInstance->posTaskFlags = 0;
}
} else {
// If we couldn't get memory for the parameters,
// clean up the mutex
uPortMutexDelete(pInstance->posMutex);
pInstance->posMutex = NULL;
}
}
}
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
return errorCode;
}
// Cancel a uGnssPosGetStart().
void uGnssPosGetStop(uDeviceHandle_t gnssHandle)
{
uGnssPrivateInstance_t *pInstance;
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if (pInstance != NULL) {
uGnssPrivateCleanUpPosTask(pInstance);
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
}
// Get position readings constantly streamed to a callback.
int32_t uGnssPosGetStreamedStart(uDeviceHandle_t gnssHandle,
int32_t rateMs,
void (*pCallback) (uDeviceHandle_t gnssHandle,
int32_t errorCode,
int32_t latitudeX1e7,
int32_t longitudeX1e7,
int32_t altitudeMillimetres,
int32_t radiusMillimetres,
int32_t speedMillimetresPerSecond,
int32_t svs,
int64_t timeUtc))
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uGnssPrivateInstance_t *pInstance;
uGnssPrivateStreamedPosition_t *pStreamedPosition;
int32_t measurementPeriodMs = -1;
int32_t navigationCount = -1;
int32_t messageRate = -1;
uGnssPrivateMessageId_t ubxNavPvtMessageId = {.type = U_GNSS_PROTOCOL_UBX,
.id.ubx = 0x0107
};
uint32_t keyId;
uGnssCfgVal_t *pCfgVal = NULL;
uGnssCfgVal_t cfgVal;
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
uint8_t message[4]; // Room for the body of a UBX-CFG-ANT message
#endif
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if ((pInstance != NULL) && (pCallback != NULL) && (rateMs != 0)) {
errorCode = (int32_t) U_ERROR_COMMON_NOT_SUPPORTED;
if (uGnssPrivateGetStreamType(pInstance->transportType) >= 0) {
// The keyId for the msgout rates is port dependent but, neatly,
// it is always the I2C value plus the port number (uGnssPort_t)
keyId = U_GNSS_CFG_VAL_KEY_ID_MSGOUT_UBX_NAV_PVT_I2C_U1 + pInstance->portNumber;
cfgVal.keyId = keyId;
cfgVal.value = 1;
bool temp = pInstance->printUbxMessages;
pInstance->printUbxMessages = true;
pStreamedPosition = pInstance->pStreamedPosition;
if (pStreamedPosition != NULL) {
// Stop the previous streamed position
uGnssPrivateCleanUpStreamedPos(pInstance);
}
// Malloc memory to copy the parameters into:
// this memory will be free'd when
// uGnssPosGetStreamedStop() is called
errorCode = (int32_t) U_ERROR_COMMON_NO_MEMORY;
pStreamedPosition = (uGnssPrivateStreamedPosition_t *) pUPortMalloc(sizeof(*pStreamedPosition));
if (pStreamedPosition != NULL) {
memset(pStreamedPosition, 0, sizeof(*pStreamedPosition));
// Put defaults in place so that we know
// to change things back only if necessary
pStreamedPosition->measurementPeriodMs = -1;
pStreamedPosition->navigationCount = -1;
pStreamedPosition->messageRate = -1;
pStreamedPosition->asyncHandle = -1;
pStreamedPosition->pCallback = pCallback;
pInstance->pStreamedPosition = pStreamedPosition;
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
if (rateMs >= 0) {
// Get the existing measurement/navigation rate
// and, if it is not rateMs, set it to rateMs
if (uGnssPrivateGetRate(pInstance,
&measurementPeriodMs,
&navigationCount,
NULL) != rateMs) {
// Set the measurement rate, with a navigation count of 1
// and leaving the time system unchanged
errorCode = uGnssPrivateSetRate(pInstance, rateMs, 1,
U_GNSS_TIME_SYSTEM_NONE);
if (errorCode == 0) {
pStreamedPosition->measurementPeriodMs = measurementPeriodMs;
pStreamedPosition->navigationCount = navigationCount;
}
}
}
if (errorCode == 0) {
// Make sure that the UBX-NAV-PVT message
// is enabled at once per measurement
if (U_GNSS_PRIVATE_HAS(pInstance->pModule,
U_GNSS_PRIVATE_FEATURE_OLD_CFG_API)) {
messageRate = uGnssPrivateGetMsgRate(pInstance,
&ubxNavPvtMessageId);
if (messageRate != 1) {
errorCode = uGnssPrivateSetMsgRate(pInstance,
&ubxNavPvtMessageId, 1);
if (errorCode == 0) {
pStreamedPosition->messageRate = messageRate;
}
}
} else {
if (uGnssCfgPrivateValGetListAlloc(pInstance,
&keyId, 1,
&pCfgVal,
U_GNSS_CFG_VAL_LAYER_RAM) == 1) {
messageRate = (int32_t) pCfgVal->value;
uPortFree(pCfgVal);
}
if (messageRate != (int32_t) cfgVal.value) {
errorCode = uGnssCfgPrivateValSetList(pInstance, &cfgVal, 1,
U_GNSS_CFG_VAL_TRANSACTION_NONE,
U_GNSS_CFG_LAYERS_SET);
if (errorCode == 0) {
pStreamedPosition->messageRate = messageRate;
}
}
}
}
if (errorCode == 0) {
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
if (uGnssPrivateGetIntermediateAtHandle(pInstance) != NULL) {
// Temporary change: on prototype versions of the
// SARA-R510M8S module (production week (printed on the
// module label, upper right) earlier than 20/27)
// the LNA in the GNSS chip is not automatically switched
// on by the firmware in the cellular module, so we need
// to switch it on ourselves by sending UBX-CFG-ANT
// with contents 02000f039
message[0] = 0x02;
message[1] = 0;
message[2] = 0xf0;
message[3] = 0x39;
uGnssPrivateSendUbxMessage(pInstance, 0x06, 0x13,
(const char *) message, 4);
}
#endif
pInstance->printUbxMessages = temp;
// Start a message received for the UBX-NAV-PVT message,
// which will ultimately call pCallback
errorCode = uGnssMsgPrivateReceiveStart(pInstance,
&ubxNavPvtMessageId,
messageCallback,
pInstance);
if (errorCode >= 0) {
// And we're off
pStreamedPosition->gnssHandle = gnssHandle;
pStreamedPosition->asyncHandle = errorCode;
errorCode = 0;
} else {
// If we couldn't create the asynchronous
// message receiver, clean up
uGnssPrivateCleanUpStreamedPos(pInstance);
}
} else {
// If we couldn't set the rate, clean up
uGnssPrivateCleanUpStreamedPos(pInstance);
}
}
pInstance->printUbxMessages = temp;
}
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
return errorCode;
}
// Cancel a uGnssPosGetStreamedStart().
void uGnssPosGetStreamedStop(uDeviceHandle_t gnssHandle)
{
uGnssPrivateInstance_t *pInstance;
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if (pInstance != NULL) {
uGnssPrivateCleanUpStreamedPos(pInstance);
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
}
// Set the mode for uGnssPosGetRrlp().
int32_t uGnssPosSetRrlpMode(uDeviceHandle_t gnssHandle, uGnssRrlpMode_t mode)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uGnssPrivateInstance_t *pInstance;
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if ((pInstance != NULL) &&
(mode < sizeof(gRrlpModeToUbxRxmMessageClass) / sizeof(gRrlpModeToUbxRxmMessageClass[0]))) {
errorCode = (int32_t) U_ERROR_COMMON_NOT_SUPPORTED;
if ((mode == U_GNSS_RRLP_MODE_MEASX) ||
U_GNSS_PRIVATE_HAS(pInstance->pModule,
U_GNSS_PRIVATE_FEATURE_RXM_MEAS_50_20_C12_D12)) {
pInstance->rrlpMode = mode;
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
return errorCode;
}
// Get the mode for uGnssPosGetRrlp().
int32_t uGnssPosGetRrlpMode(uDeviceHandle_t gnssHandle)
{
int32_t errorCodeOrRrlpMode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uGnssPrivateInstance_t *pInstance;
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
errorCodeOrRrlpMode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if (pInstance != NULL) {
errorCodeOrRrlpMode = (int32_t) pInstance->rrlpMode;
}
U_PORT_MUTEX_UNLOCK(gUGnssPrivateMutex);
}
return errorCodeOrRrlpMode;
}
// Get RRLP information from the GNSS chip.
int32_t uGnssPosGetRrlp(uDeviceHandle_t gnssHandle, char *pBuffer,
size_t sizeBytes, int32_t svsThreshold,
int32_t cNoThreshold,
int32_t multipathIndexLimit,
int32_t pseudorangeRmsErrorIndexLimit,
bool (*pKeepGoingCallback) (uDeviceHandle_t))
{
int32_t errorCodeOrLength = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uGnssPrivateInstance_t *pInstance;
int32_t messageClass;
int64_t startTime;
int32_t svs;
int32_t numBytes;
int32_t z;
int32_t numMeetingCriteria;
bool goodSatellite;
int32_t ca = 0;
int32_t cb = 0;
// Access the buffer as a uint8_t to avoid maths funnies with
// chars being signed or unsigned
uint8_t *pBufferUint8 = (uint8_t *) pBuffer;
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
uint8_t message[4]; // Room for the body of a UBX-CFG-ANT message
#endif
if (gUGnssPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUGnssPrivateMutex);
errorCodeOrLength = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pInstance = pUGnssPrivateGetInstance(gnssHandle);
if ((pInstance != NULL) && (pBufferUint8 != NULL) &&
(sizeBytes >= U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES)) {
#ifdef U_CFG_SARA_R5_M8_WORKAROUND
if ((pInstance->transportType == U_GNSS_TRANSPORT_AT) ||
(uGnssPrivateGetIntermediateAtHandle(pInstance) != NULL)) {
// Temporary change: on prototype versions of the
// SARA-R510M8S module (production week (printed on the
// module label, upper right) earlier than 20/27)
// the LNA in the GNSS chip is not automatically switched
// on by the firmware in the cellular module, so we need
// to switch it on ourselves by sending UBX-CFG-ANT
// with contents 02000f039
message[0] = 0x02;
message[1] = 0;
message[2] = 0xf0;
message[3] = 0x39;
uGnssPrivateSendUbxMessage(pInstance, 0x06, 0x13,
(const char *) message, 4);
}
#endif
messageClass = gRrlpModeToUbxRxmMessageClass[pInstance->rrlpMode];
startTime = uPortGetTickTimeMs();
errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT;
while ((errorCodeOrLength == (int32_t) U_ERROR_COMMON_TIMEOUT) &&
(((pKeepGoingCallback == NULL) &&
(uPortGetTickTimeMs() - startTime) / 1000 < U_GNSS_POS_TIMEOUT_SECONDS) ||
((pKeepGoingCallback != NULL) && pKeepGoingCallback(gnssHandle)))) {
numBytes = uGnssPrivateSendReceiveUbxMessage(pInstance,
0x02, messageClass, NULL, 0,
pBuffer + U_GNSS_POS_RRLP_HEADER_SIZE_BYTES,
sizeBytes - U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES);
if ((numBytes > 0) && (pInstance->rrlpMode == U_GNSS_RRLP_MODE_MEASX)) {
// Got something; when using MEASX we need to check if it is good enough.
// 34 since that's the furthest we need to read to check on the number of satellites
if ((((svsThreshold >= 0) || (cNoThreshold >= 0) ||
(multipathIndexLimit >= 0) || (pseudorangeRmsErrorIndexLimit >= 0)) && (numBytes >= 34))) {
// The number of satellites is at offset 34
svs = *(pBufferUint8 + U_GNSS_POS_RRLP_HEADER_SIZE_BYTES + 34);
uPortLog("U_GNSS_POS: RRLP information for %d satellite(s).\n", svs);
if ((svsThreshold >= 0) && (svs < svsThreshold)) {
// Not enough satellites in the first place
numBytes = -1;
}
if ((numBytes > 0) &&
((cNoThreshold >= 0) || (multipathIndexLimit >= 0) || (pseudorangeRmsErrorIndexLimit >= 0))) {
numMeetingCriteria = svs;
// 65 since that's the furthest we need to check on the criteria
for (int8_t x = 0; (x < svs) && (numBytes >= 65 + (x * 24)); x++) {
goodSatellite = true;
// Carrier to noise ratio is at offset 46 + (x * 24)
if (cNoThreshold >= 0) {
z = *(pBufferUint8 + U_GNSS_POS_RRLP_HEADER_SIZE_BYTES + 46 + (x * 24));
uPortLog("U_GNSS_POS: RRLP CNo for satellite %d is %d.\n", x + 1, z);
if (z < cNoThreshold) {
goodSatellite = false;
}
}
// Multipath index is at offset 47 + (x * 24)
if (goodSatellite && (multipathIndexLimit >= 0)) {
z = *(pBufferUint8 + U_GNSS_POS_RRLP_HEADER_SIZE_BYTES + 47 + (x * 24));
uPortLog("U_GNSS_POS: RRLP multipath for satellite %d is %d.\n", x + 1, z);
if (z > multipathIndexLimit) {
goodSatellite = false;
}
}
// Pseudorange RMS error index is at offset 65 + (x * 24)
if (goodSatellite && (pseudorangeRmsErrorIndexLimit >= 0)) {
z = *(pBufferUint8 + U_GNSS_POS_RRLP_HEADER_SIZE_BYTES + 65 + (x * 24));
uPortLog("U_GNSS_POS: pseudorange RMS error index for satellite %d is %d.\n",
x + 1, z);
if (z > pseudorangeRmsErrorIndexLimit) {
goodSatellite = false;
}
}
if (!goodSatellite) {
numMeetingCriteria--;
uPortLog("U_GNSS_POS: only up to %d satellite(s) meet the criteria.\n",
numMeetingCriteria);
if (numMeetingCriteria < svsThreshold) {