forked from u-blox/ubxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_gnss_private.c
2645 lines (2427 loc) · 117 KB
/
u_gnss_private.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 functions that are private to GNSS.
* IMPORTANT: this code is changing a lot at the moment as we move
* towards a more generic, streamed, approach - beware!
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // memmove(), strstr()
#include "ctype.h"
#include "u_cfg_os_platform_specific.h"
#include "u_cfg_sw.h"
#include "u_error_common.h"
#include "u_assert.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_uart.h"
#include "u_port_i2c.h"
#include "u_port_spi.h"
#include "u_port_debug.h"
#include "u_hex_bin_convert.h"
#include "u_at_client.h"
#include "u_ubx_protocol.h"
#include "u_device_shared.h"
#include "u_device_serial.h"
#include "u_network_shared.h"
#include "u_short_range_module_type.h"
#include "u_short_range.h" // For uShortRangeAtClientHandleGet()
#include "u_cell_module_type.h"
#include "u_cell.h" // For uCellAtClientHandleGet()
#include "u_gnss_module_type.h"
#include "u_gnss_type.h"
#include "u_gnss.h"
#include "u_gnss_private.h"
#include "u_gnss_msg.h"
#include "u_gnss_msg_private.h"
#include "u_gnss_cfg.h"
#include "u_gnss_cfg_val_key.h"
#include "u_gnss_cfg_private.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#ifndef U_GNSS_AT_BUFFER_LENGTH_BYTES
/** The length of a temporary buffer store a hex-encoded UBX-format
* message when receiving responses over an AT interface.
*/
# define U_GNSS_AT_BUFFER_LENGTH_BYTES ((U_GNSS_MAX_UBX_PROTOCOL_MESSAGE_BODY_LENGTH_BYTES + \
U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES) * 2)
#endif
#ifndef U_GNSS_PRIVATE_SPI_READ_LENGTH_MIN_BYTES
/** The minimum number of bytes to read on an SPI transport when
* trying to determine if there's anything valid to read.
*/
# define U_GNSS_PRIVATE_SPI_READ_LENGTH_MIN_BYTES 1
#endif
// Do some cross-checking
#if U_GNSS_PRIVATE_SPI_READ_LENGTH_MIN_BYTES > U_GNSS_DEFAULT_SPI_FILL_THRESHOLD
# error U_GNSS_PRIVATE_SPI_READ_LENGTH_MIN_BYTES must be less than or equal to U_GNSS_DEFAULT_SPI_FILL_THRESHOLD
#endif
#if U_GNSS_PRIVATE_SPI_READ_LENGTH_MIN_BYTES > U_GNSS_SPI_FILL_THRESHOLD_MAX
# error U_GNSS_PRIVATE_SPI_READ_LENGTH_MIN_BYTES must be less than or equal to U_GNSS_SPI_FILL_THRESHOLD_MAX
#endif
#if U_GNSS_DEFAULT_SPI_FILL_THRESHOLD > U_GNSS_SPI_BUFFER_LENGTH_BYTES
# error U_GNSS_DEFAULT_SPI_FILL_THRESHOLD must be less than or equal to U_GNSS_SPI_BUFFER_LENGTH_BYTES
#endif
#if U_GNSS_DEFAULT_SPI_FILL_THRESHOLD > U_GNSS_SPI_FILL_THRESHOLD_MAX
# error U_GNSS_DEFAULT_SPI_FILL_THRESHOLD must be less than or equal to U_GNSS_SPI_FILL_THRESHOLD_MAX
#endif
#ifndef U_GNSS_PRIVATE_STREAMED_POS_ENSURE_SETTINGS_RETRIES
/** Sometimes, when position has been streamed, restoring save settings
* for measurement rate and message rate can fail; this is the number
* of retries to perform when it fails.
*/
# define U_GNSS_PRIVATE_STREAMED_POS_ENSURE_SETTINGS_RETRIES 2
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** Structure to hold a received UBX-format message.
*/
typedef struct {
int32_t cls;
int32_t id;
char **ppBody; /**< a pointer to a pointer that the received
message body will be written to. If *ppBody is NULL
memory will be allocated. If ppBody is NULL
then the response is not captured (but this
structure may still be used for cls/id matching). */
size_t bodySize; /**< the number of bytes of storage at *ppBody;
must be zero if ppBody is NULL or
*ppBody is NULL. If non-zero it MUST be
large enough to fit the body in or the
CRC calculation will fail. */
} uGnssPrivateUbxReceiveMessage_t;
/* ----------------------------------------------------------------
* VARIABLES THAT ARE SHARED THROUGHOUT THE GNSS IMPLEMENTATION
* -------------------------------------------------------------- */
/** Root for the linked list of instances.
*/
uGnssPrivateInstance_t *gpUGnssPrivateInstanceList = NULL;
/** Mutex to protect the linked list.
*/
uPortMutexHandle_t gUGnssPrivateMutex = NULL;
/** The characteristics of the modules supported by this driver,
* compiled into the driver. Order is important: uGnssModuleType_t
* is used to index into this array.
*/
const uGnssPrivateModule_t gUGnssPrivateModuleList[] = {
{
U_GNSS_MODULE_TYPE_M8,
(1UL << (int32_t) U_GNSS_PRIVATE_FEATURE_OLD_CFG_API) /* features */
},
{
U_GNSS_MODULE_TYPE_M9,
((1UL << (int32_t) U_GNSS_PRIVATE_FEATURE_CFGVALXXX) |
(1UL << (int32_t) U_GNSS_PRIVATE_FEATURE_OLD_CFG_API) |
(1UL << (int32_t) U_GNSS_PRIVATE_FEATURE_GEOFENCE) /* features */
)
},
{
U_GNSS_MODULE_TYPE_M10,
((1UL << (int32_t) U_GNSS_PRIVATE_FEATURE_CFGVALXXX) |
(1UL << (int32_t) U_GNSS_PRIVATE_FEATURE_RXM_MEAS_50_20_C12_D12) /* features */
)
},
// Add new module types here, before the U_GNSS_MODULE_TYPE_ANY entry (since
// the uGnssModuleType_t value is used as an index into this array).
{
U_GNSS_MODULE_TYPE_ANY,
// The module attributes set here should be such that they help
// in identifying the actual module type.
(0x00UL) /* features */
}
};
/** Number of items in the gUGnssPrivateModuleList array, has to be
* done in this file and externed or GCC complains about asking
* for the size of a partially defined type.
*/
const size_t gUGnssPrivateModuleListSize = sizeof(gUGnssPrivateModuleList) /
sizeof(gUGnssPrivateModuleList[0]);
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/** Table to convert a GNSS transport type into a streaming transport type.
*/
static const int32_t gGnssPrivateTransportTypeToStream[] = {
U_GNSS_PRIVATE_STREAM_TYPE_NONE, // U_GNSS_TRANSPORT_NONE
U_GNSS_PRIVATE_STREAM_TYPE_UART, // U_GNSS_TRANSPORT_UART or U_GNSS_TRANSPORT_UART_1
U_ERROR_COMMON_INVALID_PARAMETER, // U_GNSS_TRANSPORT_AT
U_GNSS_PRIVATE_STREAM_TYPE_I2C, // U_GNSS_TRANSPORT_I2C
U_GNSS_PRIVATE_STREAM_TYPE_SPI, // U_GNSS_TRANSPORT_SPI
U_GNSS_PRIVATE_STREAM_TYPE_VIRTUAL_SERIAL, // U_GNSS_TRANSPORT_VIRTUAL_SERIAL
U_GNSS_PRIVATE_STREAM_TYPE_UART // U_GNSS_TRANSPORT_UART_2
};
/** Table to convert a port number to the UBX-CFG-VAL group ID that
* configures that port number for output protocol. */
static const uGnssCfgValKeyGroupId_t gPortToCfgValGroupIdOutProt[] = {
U_GNSS_CFG_VAL_KEY_GROUP_ID_I2COUTPROT, // 0: I2C
U_GNSS_CFG_VAL_KEY_GROUP_ID_UART1OUTPROT, // 1: UART/UART1
U_GNSS_CFG_VAL_KEY_GROUP_ID_UART2OUTPROT, // 2: UART/UART2
U_GNSS_CFG_VAL_KEY_GROUP_ID_USBOUTPROT, // 3: USB
U_GNSS_CFG_VAL_KEY_GROUP_ID_SPIOUTPROT // 4: SPI
};
/** Table to convert an output protocol type to the UBX-CFG-VAL item ID
* for that output protocol type. */
static const uint8_t gProtocolTypeToCfgValItemIdOutProt[] = {
1, // 0: U_GNSS_PROTOCOL_UBX
2, // 1: U_GNSS_PROTOCOL_NMEA
4 // 2: U_GNSS_PROTOCOL_RTCM
};
/** Table of the key IDs of the UBX-CFG-VAL group CFG-RATE.
*/
static const uint32_t gCfgValKeyIdCfgRate[] = {
U_GNSS_CFG_VAL_KEY_ID_RATE_MEAS_U2, // Measurement rate in ms
U_GNSS_CFG_VAL_KEY_ID_RATE_NAV_U2, // Navigation count
U_GNSS_CFG_VAL_KEY_ID_RATE_TIMEREF_E1 // Time system
};
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: MESSAGE RELATED
* -------------------------------------------------------------- */
// Match an NMEA ID with the wanted NMEA ID. Both pointers must be
// to null-terminated strings.
static bool nmeaIdMatch(const char *pNmeaIdActual, const char *pNmeaIdWanted)
{
bool match = true;
if (pNmeaIdWanted != NULL) { // A wanted string of NULL matches anything
match = false; // An actual string of NULL matches nothing (except NULL)
if (pNmeaIdActual != NULL) {
match = true;
while ((*pNmeaIdWanted != 0) && match) {
if ((*pNmeaIdActual == 0) ||
((*pNmeaIdWanted != '?') && (*pNmeaIdWanted != *pNmeaIdActual))) {
match = false;
}
pNmeaIdActual++;
pNmeaIdWanted++;
}
}
}
return match;
}
// Match an UBX ID with the wanted UXB ID, allowing ALL wildcards 0xFF.
static bool ubxIdMatch(uint16_t ubxIdActual, uint16_t ubxIdWanted)
{
if ((ubxIdWanted & U_GNSS_UBX_MESSAGE_ID_ALL) == U_GNSS_UBX_MESSAGE_ID_ALL) {
ubxIdActual |= U_GNSS_UBX_MESSAGE_ID_ALL;
}
if ((ubxIdWanted & (U_GNSS_UBX_MESSAGE_CLASS_ALL << 8)) ==
(U_GNSS_UBX_MESSAGE_CLASS_ALL << 8)) {
ubxIdActual |= (U_GNSS_UBX_MESSAGE_CLASS_ALL << 8);
}
return ubxIdActual == ubxIdWanted;
}
// Match an RTCM ID with the wanted RTCM ID, allowing ALL wildcard 0xFFFF.
static bool rtcmIdMatch(uint16_t rtcmIdActual, uint16_t rtcmIdWanted)
{
return (rtcmIdActual == rtcmIdWanted) || (rtcmIdWanted == U_GNSS_RTCM_MESSAGE_ID_ALL);
}
#ifdef U_GNSS_PRIVATE_DEBUG_PARSING
// Print out a message ID, only used when debugging message parsing.
static void printId(uGnssPrivateMessageId_t *pId)
{
if (pId->type == U_GNSS_PROTOCOL_UBX) {
uPortLog("UBX %04x", pId->id.ubx);
} else if (pId->type == U_GNSS_PROTOCOL_NMEA) {
uPortLog("NMEA %s", pId->id.nmea);
} else if (pId->type == U_GNSS_PROTOCOL_RTCM) {
uPortLog("RTCM %d", pId->id.rtcm);
} else if (pId->type == U_GNSS_PROTOCOL_UNKNOWN) {
uPortLog("UNKNOWN");
} else {
uPortLog("ERROR");
}
}
#endif
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: STREAMING TRANSPORT ONLY
* -------------------------------------------------------------- */
// Read or peek-at the data in the internal ring buffer.
static int32_t streamGetFromRingBuffer(uGnssPrivateInstance_t *pInstance,
int32_t readHandle,
char *pBuffer, size_t size,
size_t offset,
int32_t maxTimeMs,
bool andRemove)
{
int32_t errorCodeOrLength = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
size_t receiveSize;
size_t totalSize = 0;
int32_t x;
size_t leftToRead = size;
int32_t startTimeMs;
if (pInstance != NULL) {
startTimeMs = uPortGetTickTimeMs();
errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT;
while ((leftToRead > 0) &&
(uPortGetTickTimeMs() - startTimeMs < maxTimeMs)) {
if (andRemove) {
receiveSize = (int32_t) uRingBufferReadHandle(&(pInstance->ringBuffer),
readHandle,
pBuffer, leftToRead);
} else {
receiveSize = (int32_t) uRingBufferPeekHandle(&(pInstance->ringBuffer),
readHandle,
pBuffer, leftToRead,
offset);
offset += receiveSize;
}
leftToRead -= receiveSize;
totalSize += receiveSize;
if (pBuffer != NULL) {
pBuffer += receiveSize;
}
if (receiveSize == 0) {
x = uGnssPrivateStreamFillRingBuffer(pInstance,
U_GNSS_RING_BUFFER_MIN_FILL_TIME_MS,
maxTimeMs / 10);
if (x < 0) {
errorCodeOrLength = x;
}
}
}
if (totalSize > 0) {
errorCodeOrLength = (int32_t) totalSize;
}
}
return errorCodeOrLength;
}
// Send a message over UART or I2C or SPI.
static int32_t sendMessageStream(uGnssPrivateInstance_t *pInstance,
const char *pMessage,
size_t messageLengthBytes, bool printIt)
{
int32_t errorCodeOrSentLength = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
int32_t privateStreamTypeOrError;
privateStreamTypeOrError = uGnssPrivateGetStreamType(pInstance->transportType);
switch (privateStreamTypeOrError) {
case U_GNSS_PRIVATE_STREAM_TYPE_UART: {
errorCodeOrSentLength = uPortUartWrite(pInstance->transportHandle.uart,
pMessage, messageLengthBytes);
}
break;
case U_GNSS_PRIVATE_STREAM_TYPE_I2C: {
errorCodeOrSentLength = uPortI2cControllerSend(pInstance->transportHandle.i2c,
pInstance->i2cAddress,
pMessage, messageLengthBytes, false);
if (errorCodeOrSentLength == 0) {
errorCodeOrSentLength = messageLengthBytes;
}
}
break;
case U_GNSS_PRIVATE_STREAM_TYPE_SPI: {
char spiBuffer[U_GNSS_SPI_FILL_THRESHOLD_MAX] = {0}; // Zero'ed to keep Valgrind happy
size_t offset = 0;
size_t thisLength;
// In the SPI case we are always necessarily receiving while
// we send, so we have to capture that data and store it in
// our internal SPI buffer so as not to lose anything; we
// don't want to allocate another receive buffer here though,
// so we send in chunks of length up to our SPI fill-checking
// buffer ('cos it's a convenient length).
errorCodeOrSentLength = 0;
for (size_t x = 0; (offset < messageLengthBytes) && (errorCodeOrSentLength >= 0); x++) {
thisLength = messageLengthBytes - offset;
if (thisLength > U_GNSS_SPI_FILL_THRESHOLD_MAX) {
thisLength = U_GNSS_SPI_FILL_THRESHOLD_MAX;
}
errorCodeOrSentLength = uPortSpiControllerSendReceiveBlock(pInstance->transportHandle.spi,
pMessage + offset,
thisLength,
spiBuffer,
thisLength);
if (errorCodeOrSentLength > 0) {
offset += errorCodeOrSentLength;
// This will add any non-fill SPI received data to the
// internal SPI ring buffer
uGnssPrivateSpiAddReceivedData(pInstance, spiBuffer, errorCodeOrSentLength);
}
}
if (errorCodeOrSentLength >= 0) {
errorCodeOrSentLength = offset;
}
}
break;
case U_GNSS_PRIVATE_STREAM_TYPE_VIRTUAL_SERIAL: {
uDeviceSerial_t *pDeviceSerial = pInstance->transportHandle.pDeviceSerial;
if (pDeviceSerial != NULL) {
errorCodeOrSentLength = pDeviceSerial->write(pDeviceSerial,
pMessage, messageLengthBytes);
}
}
break;
default:
break;
}
if (printIt && (errorCodeOrSentLength == messageLengthBytes)) {
uPortLog("U_GNSS: sent command");
uGnssPrivatePrintBuffer(pMessage, messageLengthBytes);
uPortLog(".\n");
}
return errorCodeOrSentLength;
}
// Receive a UBX format message over UART or I2C or SPI.
// On entry pResponse should be set to the message class and ID of the
// expected response, wild cards permitted. On success it will
// be set to the message ID received and the UBX message body length
// will be returned.
static int32_t receiveUbxMessageStream(uGnssPrivateInstance_t *pInstance,
uGnssPrivateUbxReceiveMessage_t *pResponse,
int32_t timeoutMs, bool printIt)
{
int32_t errorCodeOrLength = 0; // Deliberate choice to return 0 if pResponse
uGnssPrivateMessageId_t privateMessageId; // indicates that no response is required
char *pBuffer = NULL;
if ((pInstance != NULL) && (pResponse != NULL) && (pResponse->ppBody != NULL)) {
// Convert uGnssPrivateUbxReceiveMessage_t into uGnssPrivateMessageId_t
privateMessageId.type = U_GNSS_PROTOCOL_UBX;
privateMessageId.id.ubx = (U_GNSS_UBX_MESSAGE_CLASS_ALL << 8) | U_GNSS_UBX_MESSAGE_ID_ALL;
if (pResponse->cls >= 0) {
privateMessageId.id.ubx = (privateMessageId.id.ubx & 0x00ff) | (uint16_t) (((
uint16_t) pResponse->cls) << 8);
}
if (pResponse->id >= 0) {
privateMessageId.id.ubx = (privateMessageId.id.ubx & 0xff00) | (uint16_t) pResponse->id;
}
// Now wait for the message, allowing a buffer to be allocated by
// the message receive function
errorCodeOrLength = uGnssPrivateReceiveStreamMessage(pInstance,
&privateMessageId,
pInstance->ringBufferReadHandlePrivate,
&pBuffer, 0,
timeoutMs, NULL);
if (errorCodeOrLength >= U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES) {
// Convert uGnssPrivateMessageId_t into uGnssPrivateUbxReceiveMessage_t
pResponse->cls = privateMessageId.id.ubx >> 8;
pResponse->id = privateMessageId.id.ubx & 0xFF;
// Remove the protocol overhead from the length, we just want the body
errorCodeOrLength -= U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES;
// Copy the body of the message into the response
if (*(pResponse->ppBody) == NULL) {
*(pResponse->ppBody) = (char *) pUPortMalloc(errorCodeOrLength);
} else {
if (errorCodeOrLength > (int32_t) pResponse->bodySize) {
errorCodeOrLength = (int32_t) pResponse->bodySize;
}
}
if (*(pResponse->ppBody) != NULL) {
memcpy(*(pResponse->ppBody), pBuffer + U_UBX_PROTOCOL_HEADER_LENGTH_BYTES, errorCodeOrLength);
if (printIt) {
uPortLog("U_GNSS: decoded UBX response 0x%02x 0x%02x",
privateMessageId.id.ubx >> 8, privateMessageId.id.ubx & 0xff);
if (errorCodeOrLength > 0) {
uPortLog(":");
uGnssPrivatePrintBuffer(*(pResponse->ppBody), errorCodeOrLength);
}
uPortLog(" [body %d byte(s)].\n", errorCodeOrLength);
}
} else {
errorCodeOrLength = (int32_t) U_ERROR_COMMON_NO_MEMORY;
}
} else if (printIt && (errorCodeOrLength == (int32_t) U_GNSS_ERROR_NACK)) {
uPortLog("U_GNSS: got Nack for 0x%02x 0x%02x.\n",
pResponse->cls, pResponse->id);
}
// Free memory
uPortFree(pBuffer);
}
return errorCodeOrLength;
}
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: AT TRANSPORT ONLY
* -------------------------------------------------------------- */
// Send a UBX format message over an AT interface and receive
// the response. No matching of message ID or class for
// the response is performed as it is not possible to get other
// responses when using an AT command.
static int32_t sendReceiveUbxMessageAt(const uAtClientHandle_t atHandle,
const char *pSend,
size_t sendLengthBytes,
uGnssPrivateUbxReceiveMessage_t *pResponse,
int32_t timeoutMs,
bool printIt)
{
int32_t errorCodeOrLength = (int32_t) U_ERROR_COMMON_NO_MEMORY;
int32_t x;
size_t bytesToSend;
char *pBuffer;
bool bufferReuse = false;
int32_t bytesRead;
size_t captureSize;
int32_t clsNack = 0x05;
int32_t idNack = 0x00;
char ackBody[2] = {0};
bool atPrintOn = uAtClientPrintAtGet(atHandle);
bool atDebugPrintOn = uAtClientDebugGet(atHandle);
U_ASSERT(pResponse != NULL);
// Need a buffer to hex encode the message into
// and receive the response into
x = (int32_t) (sendLengthBytes * 2) + 1; // +1 for terminator
if (x < U_GNSS_AT_BUFFER_LENGTH_BYTES + 1) {
x = U_GNSS_AT_BUFFER_LENGTH_BYTES + 1;
}
pBuffer = (char *) pUPortMalloc(x);
if (pBuffer != NULL) {
errorCodeOrLength = (int32_t) U_GNSS_ERROR_TRANSPORT;
bytesToSend = uBinToHex(pSend, sendLengthBytes, pBuffer);
if (!printIt) {
// Switch off the AT command printing if we've been
// told not to print stuff; particularly important
// on platforms where the C library leaks memory
// when called from dynamically created tasks and this
// is being called for the GNSS asynchronous API
uAtClientPrintAtSet(atHandle, false);
uAtClientDebugSet(atHandle, false);
}
// Add terminator
*(pBuffer + bytesToSend) = 0;
uAtClientLock(atHandle);
uAtClientTimeoutSet(atHandle, timeoutMs);
uAtClientCommandStart(atHandle, "AT+UGUBX=");
uAtClientWriteString(atHandle, pBuffer, true);
// Read the response
uAtClientCommandStop(atHandle);
if (printIt) {
uPortLog("U_GNSS: sent UBX command");
uGnssPrivatePrintBuffer(pSend, sendLengthBytes);
uPortLog(".\n");
}
uAtClientResponseStart(atHandle, "+UGUBX:");
// Read the hex-coded response back into pBuffer
bytesRead = uAtClientReadString(atHandle, pBuffer, x, false);
uAtClientResponseStop(atHandle);
if ((uAtClientUnlock(atHandle) == 0) && (bytesRead >= 0) &&
(pResponse->ppBody != NULL)) {
// Decode the hex into the same buffer
x = (int32_t) uHexToBin(pBuffer, bytesRead, pBuffer);
if (x > 0) {
// Deal with the output buffer
captureSize = x;
if (*(pResponse->ppBody) != NULL) {
if (captureSize > pResponse->bodySize) {
captureSize = pResponse->bodySize;
}
} else {
// We can just re-use the buffer we already have
*(pResponse->ppBody) = pBuffer;
bufferReuse = true;
}
errorCodeOrLength = (int32_t) captureSize;
if (captureSize > 0) {
// First check if we received a NACK
if ((uUbxProtocolDecode(pBuffer, x, &clsNack, &idNack,
ackBody, sizeof(ackBody),
NULL) == 2) &&
(ackBody[0] == pResponse->cls) &&
(ackBody[1] == pResponse->id)) {
// We got a NACK for the message class
// and ID we are monitoring
errorCodeOrLength = (int32_t) U_GNSS_ERROR_NACK;
} else {
// No NACK, we can decode the message body, noting
// that it is safe to decode back into the same buffer
errorCodeOrLength = uUbxProtocolDecode(pBuffer, x,
&(pResponse->cls),
&(pResponse->id),
*(pResponse->ppBody),
captureSize, NULL);
if (errorCodeOrLength > (int32_t) captureSize) {
errorCodeOrLength = (int32_t) captureSize;
}
}
}
if (printIt) {
if (errorCodeOrLength >= 0) {
uPortLog("U_GNSS: decoded UBX response 0x%02x 0x%02x",
pResponse->cls, pResponse->id);
if (errorCodeOrLength > 0) {
uPortLog(":");
uGnssPrivatePrintBuffer(*(pResponse->ppBody), errorCodeOrLength);
}
uPortLog(" [body %d byte(s)].\n", errorCodeOrLength);
} else if (errorCodeOrLength == (int32_t) U_GNSS_ERROR_NACK) {
uPortLog("U_GNSS: got Nack for 0x%02x 0x%02x.\n",
pResponse->cls, pResponse->id);
}
}
}
}
uAtClientPrintAtSet(atHandle, atPrintOn);
uAtClientDebugSet(atHandle, atDebugPrintOn);
if (!bufferReuse) {
uPortFree(pBuffer);
}
}
return errorCodeOrLength;
}
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: ANY TRANSPORT
* -------------------------------------------------------------- */
// Send a UBX format message to the GNSS module and receive
// the response.
static int32_t sendReceiveUbxMessage(uGnssPrivateInstance_t *pInstance,
int32_t messageClass,
int32_t messageId,
const char *pMessageBody,
size_t messageBodyLengthBytes,
uGnssPrivateUbxReceiveMessage_t *pResponse)
{
int32_t errorCodeOrResponseLength = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
int32_t privateStreamTypeOrError;
int32_t bytesToSend = 0;
char *pBuffer;
bool keepTrying = true;
if ((pInstance != NULL) &&
(((pMessageBody == NULL) && (messageBodyLengthBytes == 0)) ||
(messageBodyLengthBytes > 0)) &&
((pResponse->bodySize == 0) || (pResponse->ppBody != NULL))) {
errorCodeOrResponseLength = (int32_t) U_ERROR_COMMON_NO_MEMORY;
// Allocate a buffer big enough to encode the outgoing message
pBuffer = (char *) pUPortMalloc(messageBodyLengthBytes + U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES);
if (pBuffer != NULL) {
errorCodeOrResponseLength = (int32_t) U_GNSS_ERROR_TRANSPORT;
bytesToSend = uUbxProtocolEncode(messageClass, messageId,
pMessageBody, messageBodyLengthBytes,
pBuffer);
if (bytesToSend > 0) {
U_PORT_MUTEX_LOCK(pInstance->transportMutex);
privateStreamTypeOrError = uGnssPrivateGetStreamType(pInstance->transportType);
if ((pResponse != NULL) && (pResponse->ppBody != NULL) &&
(privateStreamTypeOrError >= 0)) {
// For a streaming transport, if we're going to wait for
// a response, make sure that any historical data is
// cleared from our handle in the ring buffer so that
// we don't pick it up instead, and lock our read
// pointer before we do the send so that we are sure
// we won't lose the response
uGnssPrivateStreamFillRingBuffer(pInstance,
U_GNSS_RING_BUFFER_MIN_FILL_TIME_MS,
U_GNSS_RING_BUFFER_MAX_FILL_TIME_MS);
uRingBufferLockReadHandle(&(pInstance->ringBuffer),
pInstance->ringBufferReadHandlePrivate);
uRingBufferFlushHandle(&(pInstance->ringBuffer),
pInstance->ringBufferReadHandlePrivate);
}
for (int32_t x = 0; (x <= pInstance->retriesOnNoResponse) && keepTrying; x++) {
if (privateStreamTypeOrError >= 0) {
errorCodeOrResponseLength = sendMessageStream(pInstance, pBuffer, bytesToSend,
pInstance->printUbxMessages);
if (errorCodeOrResponseLength >= 0) {
errorCodeOrResponseLength = receiveUbxMessageStream(pInstance, pResponse,
pInstance->timeoutMs,
pInstance->printUbxMessages);
}
} else {
// Not a stream, we're on AT
//lint -e{1773} Suppress attempt to cast away const: I'm not!
errorCodeOrResponseLength = sendReceiveUbxMessageAt((const uAtClientHandle_t)
pInstance->transportHandle.pAt,
pBuffer, bytesToSend,
pResponse, pInstance->timeoutMs,
pInstance->printUbxMessages);
}
if ((errorCodeOrResponseLength >= 0) ||
(errorCodeOrResponseLength == (int32_t) U_GNSS_ERROR_NACK)) {
keepTrying = false;
} else if (pInstance->retriesOnNoResponse > 0) {
uPortTaskBlock(U_GNSS_RETRY_ON_NO_RESPONSE_DELAY_MS);
}
}
// Make sure the read handle is always unlocked afterwards
uRingBufferUnlockReadHandle(&(pInstance->ringBuffer), pInstance->ringBufferReadHandlePrivate);
U_PORT_MUTEX_UNLOCK(pInstance->transportMutex);
}
// Free memory
uPortFree(pBuffer);
}
}
return errorCodeOrResponseLength;
}
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: MESSAGE PARSERS
* -------------------------------------------------------------- */
/** UBX Parser function.
*
* @param parseHandle the parse handle of the ring buffer to read from.
* @param[in] pUserParam the user parameter passed to uRingBufferParseHandle().
* @return negative error or success code.
*/
static int32_t parseUbx(uParseHandle_t parseHandle, void *pUserParam)
{
uGnssPrivateMessageId_t *pMsgId = (uGnssPrivateMessageId_t *) pUserParam;
uint8_t by = 0;
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (0xB5 != by) {
return U_ERROR_COMMON_NOT_FOUND; // = µ, 0xB5
}
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (0x62 != by) {
return U_ERROR_COMMON_NOT_FOUND; // = b
}
if (4 > uRingBufferBytesAvailableUnprotected(parseHandle)) {
return U_ERROR_COMMON_TIMEOUT;
}
uint8_t ckb = 0;
uint8_t cka = 0;
uint8_t cls, id;
uRingBufferGetByteUnprotected(parseHandle, &cls); // cls
cka += cls;
ckb += cka;
uRingBufferGetByteUnprotected(parseHandle, &id); // id
cka += id;
ckb += cka;
pMsgId->id.ubx = (uint16_t) ((((uint16_t) cls) << 8) + id);
uRingBufferGetByteUnprotected(parseHandle, &by); // len low
cka += by;
ckb += cka;
uint16_t l = by;
uRingBufferGetByteUnprotected(parseHandle, &by); // len high
cka += by;
ckb += cka;
l += (((uint16_t) by) << 8);
if (l > uRingBufferBytesAvailableUnprotected(parseHandle)) {
return U_ERROR_COMMON_TIMEOUT;
}
while (l --) {
uRingBufferGetByteUnprotected(parseHandle, &by);
cka += by;
ckb += cka;
}
cka = cka & 0xFF;
ckb = ckb & 0xFF;
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (by != cka) {
return U_ERROR_COMMON_NOT_FOUND;
}
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (by != ckb) {
return U_ERROR_COMMON_NOT_FOUND;
}
// We can only claim this as a UBX-format message if
// there was nothing that needed discarding first.
if (uRingBufferBytesDiscardUnprotected(parseHandle) == 0) {
pMsgId->type = U_GNSS_PROTOCOL_UBX;
}
return U_ERROR_COMMON_SUCCESS;
}
/** NMEA Parser function.
*
* @param parseHandle the parse handle of the ring buffer to read from.
* @param[in] pUserParam the user parameter passed to uRingBufferParseHandle().
* @return negative error or success code.
*/
static int32_t parseNmea(uParseHandle_t parseHandle, void *pUserParam)
{
uGnssPrivateMessageId_t *pMsgId = (uGnssPrivateMessageId_t *) pUserParam;
char ch = 0;
const char *hex = "0123456789ABCDEF";
if (!uRingBufferGetByteUnprotected(parseHandle, &ch)) {
return U_ERROR_COMMON_TIMEOUT;
}
if ('$' != ch) {
return U_ERROR_COMMON_NOT_FOUND;
}
char crc = 0;
int i = 0;
while (uRingBufferGetByteUnprotected(parseHandle, &ch)) {
crc ^= ch;
if (',' == ch) {
break;
}
if (i >= U_GNSS_NMEA_MESSAGE_MATCH_LENGTH_CHARACTERS) {
return U_ERROR_COMMON_NOT_FOUND;
}
if (('0' > ch) || ('Z' < ch) || (('9' < ch) && ('A' > ch))) {
return U_ERROR_COMMON_NOT_FOUND; // A-Z, 0-9
}
pMsgId->id.nmea[i++] = ch;
}
pMsgId->id.nmea[i] = '\0';
while (uRingBufferGetByteUnprotected(parseHandle, &ch)) {
if ((' ' > ch) || ('~' < ch)) {
return U_ERROR_COMMON_NOT_FOUND; // not in printable range 32 - 126
}
if ('*' == ch) {
break; // *
}
crc ^= ch;
}
if (!uRingBufferGetByteUnprotected(parseHandle, &ch)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (hex[(crc >> 4) & 0xF] != ch) {
return U_ERROR_COMMON_NOT_FOUND;
}
if (!uRingBufferGetByteUnprotected(parseHandle, &ch)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (hex[crc & 0xF] != ch) {
return U_ERROR_COMMON_NOT_FOUND;
}
if (!uRingBufferGetByteUnprotected(parseHandle, &ch)) {
return U_ERROR_COMMON_TIMEOUT;
}
if ('\r' != ch) {
return U_ERROR_COMMON_NOT_FOUND;
}
if (!uRingBufferGetByteUnprotected(parseHandle, &ch)) {
return U_ERROR_COMMON_TIMEOUT;
}
if ('\n' != ch) {
return U_ERROR_COMMON_NOT_FOUND;
}
// We can only claim this as an NMEA-format message if
// there was nothing that needed discarding first.
if (uRingBufferBytesDiscardUnprotected(parseHandle) == 0) {
pMsgId->type = U_GNSS_PROTOCOL_NMEA;
}
return U_ERROR_COMMON_SUCCESS;
}
/** RTCM Parser function.
*
* @param parseHandle the parse handle of the ring buffer to read from.
* @param[in] pUserParam the user parameter passed to uRingBufferParseHandle().
* @return negative error or success code.
*/
static int32_t parseRtcm(uParseHandle_t parseHandle, void *pUserParam)
{
uGnssPrivateMessageId_t *pMsgId = (uGnssPrivateMessageId_t *) pUserParam;
uint8_t by;
uint32_t crc = 0;
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
if (0xD3 != by) {
return U_ERROR_COMMON_NOT_FOUND;
}
// CRC24Q check
const uint32_t _crc24qTable[] = {
/* 00 */ 0x000000, 0x864cfb, 0x8ad50d, 0x0c99f6, 0x93e6e1, 0x15aa1a, 0x1933ec, 0x9f7f17,
/* 08 */ 0xa18139, 0x27cdc2, 0x2b5434, 0xad18cf, 0x3267d8, 0xb42b23, 0xb8b2d5, 0x3efe2e,
/* 10 */ 0xc54e89, 0x430272, 0x4f9b84, 0xc9d77f, 0x56a868, 0xd0e493, 0xdc7d65, 0x5a319e,
/* 18 */ 0x64cfb0, 0xe2834b, 0xee1abd, 0x685646, 0xf72951, 0x7165aa, 0x7dfc5c, 0xfbb0a7,
/* 20 */ 0x0cd1e9, 0x8a9d12, 0x8604e4, 0x00481f, 0x9f3708, 0x197bf3, 0x15e205, 0x93aefe,
/* 28 */ 0xad50d0, 0x2b1c2b, 0x2785dd, 0xa1c926, 0x3eb631, 0xb8faca, 0xb4633c, 0x322fc7,
/* 30 */ 0xc99f60, 0x4fd39b, 0x434a6d, 0xc50696, 0x5a7981, 0xdc357a, 0xd0ac8c, 0x56e077,
/* 38 */ 0x681e59, 0xee52a2, 0xe2cb54, 0x6487af, 0xfbf8b8, 0x7db443, 0x712db5, 0xf7614e,
/* 40 */ 0x19a3d2, 0x9fef29, 0x9376df, 0x153a24, 0x8a4533, 0x0c09c8, 0x00903e, 0x86dcc5,
/* 48 */ 0xb822eb, 0x3e6e10, 0x32f7e6, 0xb4bb1d, 0x2bc40a, 0xad88f1, 0xa11107, 0x275dfc,
/* 50 */ 0xdced5b, 0x5aa1a0, 0x563856, 0xd074ad, 0x4f0bba, 0xc94741, 0xc5deb7, 0x43924c,
/* 58 */ 0x7d6c62, 0xfb2099, 0xf7b96f, 0x71f594, 0xee8a83, 0x68c678, 0x645f8e, 0xe21375,
/* 60 */ 0x15723b, 0x933ec0, 0x9fa736, 0x19ebcd, 0x8694da, 0x00d821, 0x0c41d7, 0x8a0d2c,
/* 68 */ 0xb4f302, 0x32bff9, 0x3e260f, 0xb86af4, 0x2715e3, 0xa15918, 0xadc0ee, 0x2b8c15,
/* 70 */ 0xd03cb2, 0x567049, 0x5ae9bf, 0xdca544, 0x43da53, 0xc596a8, 0xc90f5e, 0x4f43a5,
/* 78 */ 0x71bd8b, 0xf7f170, 0xfb6886, 0x7d247d, 0xe25b6a, 0x641791, 0x688e67, 0xeec29c,
/* 80 */ 0x3347a4, 0xb50b5f, 0xb992a9, 0x3fde52, 0xa0a145, 0x26edbe, 0x2a7448, 0xac38b3,
/* 88 */ 0x92c69d, 0x148a66, 0x181390, 0x9e5f6b, 0x01207c, 0x876c87, 0x8bf571, 0x0db98a,
/* 90 */ 0xf6092d, 0x7045d6, 0x7cdc20, 0xfa90db, 0x65efcc, 0xe3a337, 0xef3ac1, 0x69763a,
/* 98 */ 0x578814, 0xd1c4ef, 0xdd5d19, 0x5b11e2, 0xc46ef5, 0x42220e, 0x4ebbf8, 0xc8f703,
/* a0 */ 0x3f964d, 0xb9dab6, 0xb54340, 0x330fbb, 0xac70ac, 0x2a3c57, 0x26a5a1, 0xa0e95a,
/* a8 */ 0x9e1774, 0x185b8f, 0x14c279, 0x928e82, 0x0df195, 0x8bbd6e, 0x872498, 0x016863,
/* b0 */ 0xfad8c4, 0x7c943f, 0x700dc9, 0xf64132, 0x693e25, 0xef72de, 0xe3eb28, 0x65a7d3,
/* b8 */ 0x5b59fd, 0xdd1506, 0xd18cf0, 0x57c00b, 0xc8bf1c, 0x4ef3e7, 0x426a11, 0xc426ea,
/* c0 */ 0x2ae476, 0xaca88d, 0xa0317b, 0x267d80, 0xb90297, 0x3f4e6c, 0x33d79a, 0xb59b61,
/* c8 */ 0x8b654f, 0x0d29b4, 0x01b042, 0x87fcb9, 0x1883ae, 0x9ecf55, 0x9256a3, 0x141a58,
/* d0 */ 0xefaaff, 0x69e604, 0x657ff2, 0xe33309, 0x7c4c1e, 0xfa00e5, 0xf69913, 0x70d5e8,
/* d8 */ 0x4e2bc6, 0xc8673d, 0xc4fecb, 0x42b230, 0xddcd27, 0x5b81dc, 0x57182a, 0xd154d1,
/* e0 */ 0x26359f, 0xa07964, 0xace092, 0x2aac69, 0xb5d37e, 0x339f85, 0x3f0673, 0xb94a88,
/* e8 */ 0x87b4a6, 0x01f85d, 0x0d61ab, 0x8b2d50, 0x145247, 0x921ebc, 0x9e874a, 0x18cbb1,
/* f0 */ 0xe37b16, 0x6537ed, 0x69ae1b, 0xefe2e0, 0x709df7, 0xf6d10c, 0xfa48fa, 0x7c0401,
/* f8 */ 0x42fa2f, 0xc4b6d4, 0xc82f22, 0x4e63d9, 0xd11cce, 0x575035, 0x5bc9c3, 0xdd8538
};
#define RTCM_CRC(crc, by) (crc << 8) ^ _crc24qTable[(by ^ (crc >> 16)) & 0xff]
// CRC is over the entire message, 0xD3 included
crc = RTCM_CRC(crc, by);
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
if ((0xFC & by) != 0) {
return U_ERROR_COMMON_NOT_FOUND;
}
uint16_t l = (uint16_t) ((by & 0x3) << 8);
crc = RTCM_CRC(crc, by);
if (!uRingBufferGetByteUnprotected(parseHandle, &by)) {
return U_ERROR_COMMON_TIMEOUT;
}
l += by;
// Length includes the two-byte message ID and the message
// body, i.e. up to the start of the 3-byte CRC, i.e.
// the total message length - 6.
if (l > uRingBufferBytesAvailableUnprotected(parseHandle) + 3) {
return U_ERROR_COMMON_TIMEOUT;
}
crc = RTCM_CRC(crc, by);
uint8_t idLo, idHi;
if (!uRingBufferGetByteUnprotected(parseHandle, &idLo)) {
return U_ERROR_COMMON_TIMEOUT;
}
l--;
crc = RTCM_CRC(crc, idLo);
if (!uRingBufferGetByteUnprotected(parseHandle, &idHi)) {
return U_ERROR_COMMON_TIMEOUT;
}
l--;
crc = RTCM_CRC(crc, idHi);
pMsgId->id.rtcm = (idHi >> 4) + (uint16_t) (idLo << 4);
while (l--) {
uRingBufferGetByteUnprotected(parseHandle, &by);
crc = RTCM_CRC(crc, by);
}
// Compare CRC
for (int32_t x = 2; (x >= 0) && uRingBufferGetByteUnprotected(parseHandle, &by); x--) {
if (by != (uint8_t) (crc >> (8 * x))) {
return U_ERROR_COMMON_TIMEOUT;
}
}
// We can only claim this as an RTCM-format message if
// there was nothing that needed discarding first.
if (uRingBufferBytesDiscardUnprotected(parseHandle) == 0) {
pMsgId->type = U_GNSS_PROTOCOL_RTCM;
}
return U_ERROR_COMMON_SUCCESS;
}
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: RATE CONFIGURATION
* -------------------------------------------------------------- */
// Get the navigation rate old-style, with UBX-CFG-RATE.
int32_t getRateUbxCfgRate(uGnssPrivateInstance_t *pInstance,
int32_t *pMeasurementPeriodMs,
int32_t *pNavigationCount,
uGnssTimeSystem_t *pTimeSystem)
{