forked from sxjack/unix_rid_capture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
opendroneid.c
1497 lines (1376 loc) · 47.7 KB
/
opendroneid.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 (C) 2019 Intel Corporation
SPDX-License-Identifier: Apache-2.0
Open Drone ID C Library
Maintainer:
Gabriel Cox
*/
#include "opendroneid.h"
#include <math.h>
#include <stdio.h>
#define ENABLE_DEBUG 1
const float SPEED_DIV[2] = {0.25f, 0.75f};
const float VSPEED_DIV = 0.5f;
const int32_t LATLON_MULT = 10000000;
const float ALT_DIV = 0.5f;
const int ALT_ADDER = 1000;
static char *safe_dec_copyfill(char *dstStr, const char *srcStr, int dstSize);
static int intRangeMax(int64_t inValue, int startRange, int endRange);
static int intInRange(int inValue, int startRange, int endRange);
/**
* Initialize basic ID data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initBasicIDData(ODID_BasicID_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_BasicID_data));
}
/**
* Initialize location data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initLocationData(ODID_Location_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_Location_data));
data->Direction = INV_DIR;
data->SpeedHorizontal = INV_SPEED_H;
data->SpeedVertical = INV_SPEED_V;
data->AltitudeBaro = INV_ALT;
data->AltitudeGeo = INV_ALT;
data->Height = INV_ALT;
}
/**
* Initialize authorization data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initAuthData(ODID_Auth_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_Auth_data));
}
/**
* Initialize self ID data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initSelfIDData(ODID_SelfID_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_SelfID_data));
}
/**
* Initialize system data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initSystemData(ODID_System_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_System_data));
data->AreaCount = 1;
data->AreaCeiling = INV_ALT;
data->AreaFloor = INV_ALT;
data->OperatorAltitudeGeo = INV_ALT;
}
/**
* Initialize operator ID data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initOperatorIDData(ODID_OperatorID_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_OperatorID_data));
}
/**
* Initialize message pack data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initMessagePackData(ODID_MessagePack_data *data)
{
if (!data)
return;
memset(data, 0, sizeof(ODID_MessagePack_data));
data->SingleMessageSize = ODID_MESSAGE_SIZE;
}
/**
* Initialize UAS data fields to their default values
*
* @param data (non encoded/packed) structure
*/
void odid_initUasData(ODID_UAS_Data *data)
{
if (!data)
return;
for (int i = 0; i < ODID_BASIC_ID_MAX_MESSAGES; i++) {
data->BasicIDValid[i] = 0;
odid_initBasicIDData(&data->BasicID[i]);
}
data->LocationValid = 0;
odid_initLocationData(&data->Location);
for (int i = 0; i < ODID_AUTH_MAX_PAGES; i++) {
data->AuthValid[i] = 0;
odid_initAuthData(&data->Auth[i]);
}
data->SelfIDValid = 0;
odid_initSelfIDData(&data->SelfID);
data->SystemValid = 0;
odid_initSystemData(&data->System);
data->OperatorIDValid = 0;
odid_initOperatorIDData(&data->OperatorID);
}
/**
* Encode direction as defined by Open Drone ID
*
* The encoding method uses 8 bits for the direction in degrees and
* one extra bit for indicating the East/West direction.
*
* @param Direcction in degrees. 0 <= x < 360. Route course based on true North
* @param EWDirection Bit flag indicating whether the direction is towards
East (0 - 179 degrees) or West (180 - 359)
* @return Encoded Direction in a single byte
*/
static uint8_t encodeDirection(float Direction, uint8_t *EWDirection)
{
unsigned int direction_int = (unsigned int) roundf(Direction);
if (direction_int == 360)
direction_int = 0;
if (direction_int < 180) {
*EWDirection = 0;
} else {
*EWDirection = 1;
direction_int -= 180;
}
return (uint8_t) intRangeMax(direction_int, 0, UINT8_MAX);
}
/**
* Encode speed into units defined by Open Drone ID
*
* The quantization algorithm allows for speed to be stored in units of 0.25 m/s
* on the low end of the scale and 0.75 m/s on the high end of the scale.
* This allows for more precise speeds to be represented in a single Uint8 byte
* rather than using a large float value.
*
* @param Speed_data Speed (and decimal) in m/s
* @param mult a (write only) value that sets the multiplier flag
* @return Encoded Speed in a single byte or max speed if over max encoded speed.
*/
static uint8_t encodeSpeedHorizontal(float Speed_data, uint8_t *mult)
{
if (Speed_data <= UINT8_MAX * SPEED_DIV[0]) {
*mult = 0;
return (uint8_t) (Speed_data / SPEED_DIV[0]);
} else {
*mult = 1;
int big_value = (int) ((Speed_data - (UINT8_MAX * SPEED_DIV[0])) / SPEED_DIV[1]);
return (uint8_t) intRangeMax(big_value, 0, UINT8_MAX);
}
}
/**
* Encode Vertical Speed into a signed Integer ODID format
*
* @param SpeedVertical_data vertical speed (in m/s)
* @return Encoded vertical speed
*/
static int8_t encodeSpeedVertical(float SpeedVertical_data)
{
int encValue = (int) (SpeedVertical_data / VSPEED_DIV);
return (int8_t) intRangeMax(encValue, INT8_MIN, INT8_MAX);
}
/**
* Encode Latitude or Longitude value into a signed Integer ODID format
*
* This encodes a 64bit double into a 32 bit integer yet still maintains
* 10^7 of a degree of accuracy (about 1cm)
*
* @param LatLon_data Either Lat or Lon double float value
* @return Encoded Lat or Lon
*/
static int32_t encodeLatLon(double LatLon_data)
{
return (int32_t) intRangeMax((int64_t) (LatLon_data * LATLON_MULT), -180 * LATLON_MULT, 180 * LATLON_MULT);
}
/**
* Encode Altitude value into an int16 ODID format
*
* This encodes a 32bit floating point altitude into an uint16 compressed
* scale that starts at -1000m.
*
* @param Alt_data Altitude to encode (in meters)
* @return Encoded Altitude
*/
static uint16_t encodeAltitude(float Alt_data)
{
return (uint16_t) intRangeMax( (int) ((Alt_data + (float) ALT_ADDER) / ALT_DIV), 0, UINT16_MAX);
}
/**
* Encode timestamp data in ODID format
*
* This encodes a fractional seconds value into a 2 byte int16
* on a scale of tenths of seconds since after the hour.
*
* @param Seconds_data Seconds (to at least 1 decimal place) since the hour
* @return Encoded timestamp (Tenths of seconds since the hour)
*/
static uint16_t encodeTimeStamp(float Seconds_data)
{
if (Seconds_data == INV_TIMESTAMP)
return INV_TIMESTAMP;
else
return (uint16_t) intRangeMax((int64_t) roundf(Seconds_data*10), 0, MAX_TIMESTAMP * 10);
}
/**
* Encode area radius data in ODID format
*
* This encodes the area radius in meters into a 1 byte value
*
* @param Radius The radius of the drone area/swarm
* @return Encoded area radius
*/
static uint8_t encodeAreaRadius(uint16_t Radius)
{
return (uint8_t) intRangeMax(Radius / 10, 0, 255);
}
/**
* Encode Basic ID message (packed, ready for broadcast)
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeBasicIDMessage(ODID_BasicID_encoded *outEncoded, ODID_BasicID_data *inData)
{
if (!outEncoded || !inData ||
!intInRange(inData->IDType, 0, 15) ||
!intInRange(inData->UAType, 0, 15))
return ODID_FAIL;
outEncoded->MessageType = ODID_MESSAGETYPE_BASIC_ID;
outEncoded->ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->IDType = inData->IDType;
outEncoded->UAType = inData->UAType;
switch (inData->IDType)
{
case ODID_IDTYPE_SERIAL_NUMBER:
case ODID_IDTYPE_CAA_REGISTRATION_ID:
memset(outEncoded->UASID, 0, sizeof(outEncoded->UASID));
strncpy(outEncoded->UASID, inData->UASID, sizeof(outEncoded->UASID));
break;
default:
memcpy(outEncoded->UASID, inData->UASID, sizeof(outEncoded->UASID));
break;
}
memset(outEncoded->Reserved, 0, sizeof(outEncoded->Reserved));
return ODID_SUCCESS;
}
/**
* Encode Location message (packed, ready for broadcast)
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeLocationMessage(ODID_Location_encoded *outEncoded, ODID_Location_data *inData)
{
uint8_t bitflag;
if (!outEncoded || !inData ||
!intInRange(inData->Status, 0, 15) ||
!intInRange(inData->HeightType, 0, 1) ||
!intInRange(inData->HorizAccuracy, 0, 15) ||
!intInRange(inData->VertAccuracy, 0, 15) ||
!intInRange(inData->BaroAccuracy, 0, 15) ||
!intInRange(inData->SpeedAccuracy, 0, 15) ||
!intInRange(inData->TSAccuracy, 0, 15))
return ODID_FAIL;
if (inData->Direction < MIN_DIR || inData->Direction > INV_DIR ||
(inData->Direction > MAX_DIR && inData->Direction < INV_DIR))
return ODID_FAIL;
if (inData->SpeedHorizontal < MIN_SPEED_H || inData->SpeedHorizontal > INV_SPEED_H ||
(inData->SpeedHorizontal > MAX_SPEED_H && inData->SpeedHorizontal < INV_SPEED_H))
return ODID_FAIL;
if (inData->SpeedVertical < MIN_SPEED_V || inData->SpeedVertical > INV_SPEED_V ||
(inData->SpeedVertical > MAX_SPEED_V && inData->SpeedVertical < INV_SPEED_V))
return ODID_FAIL;
if (inData->Latitude < MIN_LAT || inData->Latitude > MAX_LAT ||
inData->Longitude < MIN_LON || inData->Longitude > MAX_LON)
return ODID_FAIL;
if (inData->AltitudeBaro < MIN_ALT || inData->AltitudeBaro > MAX_ALT ||
inData->AltitudeGeo < MIN_ALT || inData->AltitudeGeo > MAX_ALT ||
inData->Height < MIN_ALT || inData->Height > MAX_ALT)
return ODID_FAIL;
if (inData->TimeStamp < 0 ||
(inData->TimeStamp > MAX_TIMESTAMP && inData->TimeStamp != INV_TIMESTAMP))
return ODID_FAIL;
outEncoded->MessageType = ODID_MESSAGETYPE_LOCATION;
outEncoded->ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->Status = inData->Status;
outEncoded->Reserved = 0;
outEncoded->Direction = encodeDirection(inData->Direction, &bitflag);
outEncoded->EWDirection = bitflag;
outEncoded->SpeedHorizontal = encodeSpeedHorizontal(inData->SpeedHorizontal, &bitflag);
outEncoded->SpeedMult = bitflag;
outEncoded->SpeedVertical = encodeSpeedVertical(inData->SpeedVertical);
outEncoded->Latitude = encodeLatLon(inData->Latitude);
outEncoded->Longitude = encodeLatLon(inData->Longitude);
outEncoded->AltitudeBaro = encodeAltitude(inData->AltitudeBaro);
outEncoded->AltitudeGeo = encodeAltitude(inData->AltitudeGeo);
outEncoded->HeightType = inData->HeightType;
outEncoded->Height = encodeAltitude(inData->Height);
outEncoded->HorizAccuracy = inData->HorizAccuracy;
outEncoded->VertAccuracy = inData->VertAccuracy;
outEncoded->BaroAccuracy = inData->BaroAccuracy;
outEncoded->SpeedAccuracy = inData->SpeedAccuracy;
outEncoded->TSAccuracy = inData->TSAccuracy;
outEncoded->Reserved2 = 0;
outEncoded->TimeStamp = encodeTimeStamp(inData->TimeStamp);
outEncoded->Reserved3 = 0;
return ODID_SUCCESS;
}
/**
* Encode Auth message (packed, ready for broadcast)
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeAuthMessage(ODID_Auth_encoded *outEncoded, ODID_Auth_data *inData)
{
if (!outEncoded || !inData || !intInRange(inData->AuthType, 0, 15))
return ODID_FAIL;
if (inData->DataPage >= ODID_AUTH_MAX_PAGES)
return ODID_FAIL;
if (inData->DataPage == 0) {
if (inData->LastPageIndex >= ODID_AUTH_MAX_PAGES)
return ODID_FAIL;
#if (MAX_AUTH_LENGTH < UINT8_MAX)
if (inData->Length > MAX_AUTH_LENGTH)
return ODID_FAIL;
#endif
int len = ODID_AUTH_PAGE_ZERO_DATA_SIZE +
inData->LastPageIndex * ODID_AUTH_PAGE_NONZERO_DATA_SIZE;
if (len < inData->Length)
return ODID_FAIL;
}
outEncoded->page_zero.MessageType = ODID_MESSAGETYPE_AUTH;
outEncoded->page_zero.ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->page_zero.AuthType = inData->AuthType;
outEncoded->page_zero.DataPage = inData->DataPage;
if (inData->DataPage == 0) {
outEncoded->page_zero.LastPageIndex = inData->LastPageIndex;
outEncoded->page_zero.Length = inData->Length;
outEncoded->page_zero.Timestamp = inData->Timestamp;
memcpy(outEncoded->page_zero.AuthData, inData->AuthData,
sizeof(outEncoded->page_zero.AuthData));
} else {
memcpy(outEncoded->page_non_zero.AuthData, inData->AuthData,
sizeof(outEncoded->page_non_zero.AuthData));
}
return ODID_SUCCESS;
}
/**
* Encode Self ID message (packed, ready for broadcast)
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeSelfIDMessage(ODID_SelfID_encoded *outEncoded, ODID_SelfID_data *inData)
{
if (!outEncoded || !inData || !intInRange(inData->DescType, 0, 255))
return ODID_FAIL;
outEncoded->MessageType = ODID_MESSAGETYPE_SELF_ID;
outEncoded->ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->DescType = inData->DescType;
strncpy(outEncoded->Desc, inData->Desc, sizeof(outEncoded->Desc));
return ODID_SUCCESS;
}
/**
* Encode System message (packed, ready for broadcast)
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeSystemMessage(ODID_System_encoded *outEncoded, ODID_System_data *inData)
{
if (!outEncoded || !inData ||
!intInRange(inData->OperatorLocationType, 0, 3) ||
!intInRange(inData->ClassificationType, 0, 7) ||
!intInRange(inData->CategoryEU, 0, 15) ||
!intInRange(inData->ClassEU, 0, 15))
return ODID_FAIL;
if (inData->OperatorLatitude < MIN_LAT || inData->OperatorLatitude > MAX_LAT ||
inData->OperatorLongitude < MIN_LON || inData->OperatorLongitude > MAX_LON)
return ODID_FAIL;
if (inData->AreaRadius > MAX_AREA_RADIUS)
return ODID_FAIL;
if (inData->AreaCeiling < MIN_ALT || inData->AreaCeiling > MAX_ALT ||
inData->AreaFloor < MIN_ALT || inData->AreaFloor > MAX_ALT ||
inData->OperatorAltitudeGeo < MIN_ALT || inData->OperatorAltitudeGeo > MAX_ALT)
return ODID_FAIL;
outEncoded->MessageType = ODID_MESSAGETYPE_SYSTEM;
outEncoded->ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->Reserved = 0;
outEncoded->OperatorLocationType = inData->OperatorLocationType;
outEncoded->ClassificationType = inData->ClassificationType;
outEncoded->OperatorLatitude = encodeLatLon(inData->OperatorLatitude);
outEncoded->OperatorLongitude = encodeLatLon(inData->OperatorLongitude);
outEncoded->AreaCount = inData->AreaCount;
outEncoded->AreaRadius = encodeAreaRadius(inData->AreaRadius);
outEncoded->AreaCeiling = encodeAltitude(inData->AreaCeiling);
outEncoded->AreaFloor = encodeAltitude(inData->AreaFloor);
outEncoded->CategoryEU = inData->CategoryEU;
outEncoded->ClassEU = inData->ClassEU;
outEncoded->OperatorAltitudeGeo = encodeAltitude(inData->OperatorAltitudeGeo);
outEncoded->Timestamp = inData->Timestamp;
outEncoded->Reserved2 = 0;
return ODID_SUCCESS;
}
/**
* Encode Operator ID message (packed, ready for broadcast)
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeOperatorIDMessage(ODID_OperatorID_encoded *outEncoded, ODID_OperatorID_data *inData)
{
if (!outEncoded || !inData || !intInRange(inData->OperatorIdType, 0, 255))
return ODID_FAIL;
outEncoded->MessageType = ODID_MESSAGETYPE_OPERATOR_ID;
outEncoded->ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->OperatorIdType = inData->OperatorIdType;
strncpy(outEncoded->OperatorId, inData->OperatorId, sizeof(outEncoded->OperatorId));
memset(outEncoded->Reserved, 0, sizeof(outEncoded->Reserved));
return ODID_SUCCESS;
}
/**
* Check whether the data fields of a pack structure are valid
*
* @param msgs Pointer to the buffer containing the messages
* @param amount The amount of messages in the pack
* @return ODID_SUCCESS or ODID_FAIL;
*/
static int checkPackContent(ODID_Message_encoded *msgs, int amount)
{
if (amount <= 0 || amount > ODID_PACK_MAX_MESSAGES)
return ODID_FAIL;
int numMessages[6] = { 0 }; // Counters for relevant parts of ODID_messagetype_t
for (int i = 0; i < amount; i++) {
uint8_t MessageType = decodeMessageType(msgs[i].rawData[0]);
// Check for illegal content. This also avoids recursive calls between
// decodeOpenDroneID() and decodeMessagePack()/checkPackContent()
if (MessageType <= ODID_MESSAGETYPE_OPERATOR_ID)
numMessages[MessageType]++;
else
return ODID_FAIL;
}
// Allow max one of each message except Basic ID and Authorization.
if (numMessages[ODID_MESSAGETYPE_BASIC_ID] > ODID_BASIC_ID_MAX_MESSAGES ||
numMessages[ODID_MESSAGETYPE_LOCATION] > 1 ||
numMessages[ODID_MESSAGETYPE_AUTH] > ODID_AUTH_MAX_PAGES ||
numMessages[ODID_MESSAGETYPE_SELF_ID] > 1 ||
numMessages[ODID_MESSAGETYPE_SYSTEM] > 1 ||
numMessages[ODID_MESSAGETYPE_OPERATOR_ID] > 1)
return ODID_FAIL;
return ODID_SUCCESS;
}
/**
* Encode message pack. I.e. a collection of multiple encoded messages
*
* @param outEncoded Output (encoded/packed) structure
* @param inData Input data (non encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int encodeMessagePack(ODID_MessagePack_encoded *outEncoded, ODID_MessagePack_data *inData)
{
if (!outEncoded || !inData || inData->SingleMessageSize != ODID_MESSAGE_SIZE)
return ODID_FAIL;
if (checkPackContent(inData->Messages, inData->MsgPackSize) != ODID_SUCCESS)
return ODID_FAIL;
outEncoded->MessageType = ODID_MESSAGETYPE_PACKED;
outEncoded->ProtoVersion = ODID_PROTOCOL_VERSION;
outEncoded->SingleMessageSize = inData->SingleMessageSize;
outEncoded->MsgPackSize = inData->MsgPackSize;
for (int i = 0; i < inData->MsgPackSize; i++)
memcpy(&outEncoded->Messages[i], &inData->Messages[i], ODID_MESSAGE_SIZE);
return ODID_SUCCESS;
}
/**
* Dencode direction from Open Drone ID packed message
*
* @param Direction_enc encoded direction
* @param EWDirection East/West direction flag
* @return direction in degrees (0 - 359)
*/
static float decodeDirection(uint8_t Direction_enc, uint8_t EWDirection)
{
if (EWDirection)
return (float) Direction_enc + 180;
else
return (float) Direction_enc;
}
/**
* Dencode speed from Open Drone ID packed message
*
* @param Speed_enc encoded speed
* @param mult multiplier flag
* @return decoded speed in m/s
*/
static float decodeSpeedHorizontal(uint8_t Speed_enc, uint8_t mult)
{
if (mult)
return ((float) Speed_enc * SPEED_DIV[1]) + (UINT8_MAX * SPEED_DIV[0]);
else
return (float) Speed_enc * SPEED_DIV[0];
}
/**
* Decode Vertical Speed from Open Drone ID Packed Message
*
* @param SpeedVertical_enc Encoded Vertical Speed
* @return decoded Vertical Speed in m/s
*/
static float decodeSpeedVertical(int8_t SpeedVertical_enc)
{
return (float) SpeedVertical_enc * VSPEED_DIV;
}
/**
* Decode Latitude or Longitude value into a signed Integer ODID format
*
* @param LatLon_enc Either Lat or Lon ecoded int value
* @return decoded (double) Lat or Lon
*/
static double decodeLatLon(int32_t LatLon_enc)
{
return (double) LatLon_enc / LATLON_MULT;
}
/**
* Decode Altitude from ODID packed format
*
* @param Alt_enc Encoded Altitude to decode
* @return decoded Altitude (in meters)
*/
static float decodeAltitude(uint16_t Alt_enc)
{
return (float) Alt_enc * ALT_DIV - (float) ALT_ADDER;
}
/**
* Decode timestamp data from ODID packed format
*
* @param Seconds_enc Encoded Timestamp
* @return Decoded timestamp (seconds since the hour)
*/
static float decodeTimeStamp(uint16_t Seconds_enc)
{
if (Seconds_enc == INV_TIMESTAMP)
return INV_TIMESTAMP;
else
return (float) Seconds_enc / 10;
}
/**
* Decode area radius data from ODID format
*
* This decodes a 1 byte value to the area radius in meters
*
* @param Radius_enc Encoded area radius
* @return The radius of the drone area/swarm in meters
*/
static uint16_t decodeAreaRadius(uint8_t Radius_enc)
{
return (uint16_t) ((int) Radius_enc * 10);
}
/**
* Get the ID type of the basic ID message
*
* @param inEncoded Input message (encoded/packed) structure
* @param idType Output: The ID type of this basic ID message
* @return ODID_SUCCESS or ODID_FAIL;
*/
int getBasicIDType(ODID_BasicID_encoded *inEncoded, enum ODID_idtype *idType)
{
if (!inEncoded || !idType || inEncoded->MessageType != ODID_MESSAGETYPE_BASIC_ID)
return ODID_FAIL;
*idType = (enum ODID_idtype) inEncoded->IDType;
return ODID_SUCCESS;
}
/**
* Decode Basic ID data from packed message
*
* @param outData Output: decoded message
* @param inEncoded Input message (encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeBasicIDMessage(ODID_BasicID_data *outData, ODID_BasicID_encoded *inEncoded)
{
if (!outData || !inEncoded ||
inEncoded->MessageType != ODID_MESSAGETYPE_BASIC_ID ||
!intInRange(inEncoded->IDType, 0, 15) ||
!intInRange(inEncoded->UAType, 0, 15))
return ODID_FAIL;
outData->IDType = (ODID_idtype_t) inEncoded->IDType;
outData->UAType = (ODID_uatype_t) inEncoded->UAType;
switch (inEncoded->IDType)
{
case ODID_IDTYPE_SERIAL_NUMBER:
case ODID_IDTYPE_CAA_REGISTRATION_ID:
safe_dec_copyfill(outData->UASID, inEncoded->UASID, sizeof(outData->UASID));
break;
default:
memcpy(outData->UASID, inEncoded->UASID, sizeof(outData->UASID));
break;
}
return ODID_SUCCESS;
}
/**
* Decode Location data from packed message
*
* @param outData Output: decoded message
* @param inEncoded Input message (encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeLocationMessage(ODID_Location_data *outData, ODID_Location_encoded *inEncoded)
{
if (!outData || !inEncoded ||
inEncoded->MessageType != ODID_MESSAGETYPE_LOCATION ||
!intInRange(inEncoded->Status, 0, 15))
return ODID_FAIL;
outData->Status = (ODID_status_t) inEncoded->Status;
outData->Direction = decodeDirection(inEncoded->Direction, inEncoded-> EWDirection);
outData->SpeedHorizontal = decodeSpeedHorizontal(inEncoded->SpeedHorizontal, inEncoded->SpeedMult);
outData->SpeedVertical = decodeSpeedVertical(inEncoded->SpeedVertical);
outData->Latitude = decodeLatLon(inEncoded->Latitude);
outData->Longitude = decodeLatLon(inEncoded->Longitude);
outData->AltitudeBaro = decodeAltitude(inEncoded->AltitudeBaro);
outData->AltitudeGeo = decodeAltitude(inEncoded->AltitudeGeo);
outData->HeightType = (ODID_Height_reference_t) inEncoded->HeightType;
outData->Height = decodeAltitude(inEncoded->Height);
outData->HorizAccuracy = (ODID_Horizontal_accuracy_t) inEncoded->HorizAccuracy;
outData->VertAccuracy = (ODID_Vertical_accuracy_t) inEncoded->VertAccuracy;
outData->BaroAccuracy = (ODID_Vertical_accuracy_t) inEncoded->BaroAccuracy;
outData->SpeedAccuracy = (ODID_Speed_accuracy_t) inEncoded->SpeedAccuracy;
outData->TSAccuracy = (ODID_Timestamp_accuracy_t) inEncoded->TSAccuracy;
outData->TimeStamp = decodeTimeStamp(inEncoded->TimeStamp);
return ODID_SUCCESS;
}
/**
* Get the page number of the authorization message
*
* @param inEncoded Input message (encoded/packed) structure
* @param pageNum Output: The page number of this authorization message
* @return ODID_SUCCESS or ODID_FAIL;
*/
int getAuthPageNum(ODID_Auth_encoded *inEncoded, int *pageNum)
{
if (!inEncoded || !pageNum ||
inEncoded->page_zero.MessageType != ODID_MESSAGETYPE_AUTH ||
!intInRange(inEncoded->page_zero.AuthType, 0, 15) ||
!intInRange(inEncoded->page_zero.DataPage, 0, ODID_AUTH_MAX_PAGES - 1))
return ODID_FAIL;
*pageNum = inEncoded->page_zero.DataPage;
return ODID_SUCCESS;
}
/**
* Decode Auth data from packed message
*
* @param outData Output: decoded message
* @param inEncoded Input message (encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeAuthMessage(ODID_Auth_data *outData, ODID_Auth_encoded *inEncoded)
{
if (!outData || !inEncoded ||
inEncoded->page_zero.MessageType != ODID_MESSAGETYPE_AUTH ||
!intInRange(inEncoded->page_zero.AuthType, 0, 15) ||
!intInRange(inEncoded->page_zero.DataPage, 0, ODID_AUTH_MAX_PAGES - 1))
return ODID_FAIL;
if (inEncoded->page_zero.DataPage == 0) {
if (inEncoded->page_zero.LastPageIndex >= ODID_AUTH_MAX_PAGES)
return ODID_FAIL;
#if (MAX_AUTH_LENGTH < UINT8_MAX)
if (inEncoded->page_zero.Length > MAX_AUTH_LENGTH)
return ODID_FAIL;
#endif
int len = ODID_AUTH_PAGE_ZERO_DATA_SIZE +
inEncoded->page_zero.LastPageIndex * ODID_AUTH_PAGE_NONZERO_DATA_SIZE;
if (len < inEncoded->page_zero.Length)
return ODID_FAIL;
}
outData->AuthType = (ODID_authtype_t) inEncoded->page_zero.AuthType;
outData->DataPage = inEncoded->page_zero.DataPage;
if (inEncoded->page_zero.DataPage == 0) {
outData->LastPageIndex = inEncoded->page_zero.LastPageIndex;
outData->Length = inEncoded->page_zero.Length;
outData->Timestamp = inEncoded->page_zero.Timestamp;
memset(outData->AuthData, 0, sizeof(outData->AuthData));
memcpy(outData->AuthData, inEncoded->page_zero.AuthData,
ODID_AUTH_PAGE_ZERO_DATA_SIZE);
} else {
memset(outData->AuthData, 0, sizeof(outData->AuthData));
memcpy(outData->AuthData, inEncoded->page_non_zero.AuthData,
ODID_AUTH_PAGE_NONZERO_DATA_SIZE);
}
return ODID_SUCCESS;
}
/**
* Decode Self ID data from packed message
*
* @param outData Output: decoded message
* @param inEncoded Input message (encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeSelfIDMessage(ODID_SelfID_data *outData, ODID_SelfID_encoded *inEncoded)
{
if (!outData || !inEncoded ||
inEncoded->MessageType != ODID_MESSAGETYPE_SELF_ID)
return ODID_FAIL;
outData->DescType = (ODID_desctype_t) inEncoded->DescType;
safe_dec_copyfill(outData->Desc, inEncoded->Desc, sizeof(outData->Desc));
return ODID_SUCCESS;
}
/**
* Decode System data from packed message
*
* @param outData Output: decoded message
* @param inEncoded Input message (encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeSystemMessage(ODID_System_data *outData, ODID_System_encoded *inEncoded)
{
if (!outData || !inEncoded ||
inEncoded->MessageType != ODID_MESSAGETYPE_SYSTEM)
return ODID_FAIL;
outData->OperatorLocationType =
(ODID_operator_location_type_t) inEncoded->OperatorLocationType;
outData->ClassificationType =
(ODID_classification_type_t) inEncoded->ClassificationType;
outData->OperatorLatitude = decodeLatLon(inEncoded->OperatorLatitude);
outData->OperatorLongitude = decodeLatLon(inEncoded->OperatorLongitude);
outData->AreaCount = inEncoded->AreaCount;
outData->AreaRadius = decodeAreaRadius(inEncoded->AreaRadius);
outData->AreaCeiling = decodeAltitude(inEncoded->AreaCeiling);
outData->AreaFloor = decodeAltitude(inEncoded->AreaFloor);
outData->CategoryEU = (ODID_category_EU_t) inEncoded->CategoryEU;
outData->ClassEU = (ODID_class_EU_t) inEncoded->ClassEU;
outData->OperatorAltitudeGeo = decodeAltitude(inEncoded->OperatorAltitudeGeo);
outData->Timestamp = inEncoded->Timestamp;
return ODID_SUCCESS;
}
/**
* Decode Operator ID data from packed message
*
* @param outData Output: decoded message
* @param inEncoded Input message (encoded/packed) structure
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeOperatorIDMessage(ODID_OperatorID_data *outData, ODID_OperatorID_encoded *inEncoded)
{
if (!outData || !inEncoded ||
inEncoded->MessageType != ODID_MESSAGETYPE_OPERATOR_ID)
return ODID_FAIL;
outData->OperatorIdType = (ODID_operatorIdType_t) inEncoded->OperatorIdType;
safe_dec_copyfill(outData->OperatorId, inEncoded->OperatorId, sizeof(outData->OperatorId));
return ODID_SUCCESS;
}
/**
* Decode Message Pack from packed message
*
* The various Valid flags in uasData are set true whenever a message has been
* decoded and the corresponding data structure has been filled. The caller must
* clear these flags before calling decodeMessagePack().
*
* @param uasData Output: Structure containing buffers for all message data
* @param pack Pointer to an encoded packed message
* @return ODID_SUCCESS or ODID_FAIL;
*/
int decodeMessagePack(ODID_UAS_Data *uasData, ODID_MessagePack_encoded *pack)
{
if (!uasData || !pack || pack->MessageType != ODID_MESSAGETYPE_PACKED)
return ODID_FAIL;
if (pack->SingleMessageSize != ODID_MESSAGE_SIZE)
return ODID_FAIL;
if (checkPackContent(pack->Messages, pack->MsgPackSize) != ODID_SUCCESS)
return ODID_FAIL;
for (int i = 0; i < pack->MsgPackSize; i++) {
decodeOpenDroneID(uasData, pack->Messages[i].rawData);
}
return ODID_SUCCESS;
}
/**
* Decodes the message type of a packed Open Drone ID message
*
* @param byte The first byte of the message
* @return The message type: ODID_messagetype_t
*/
ODID_messagetype_t decodeMessageType(uint8_t byte)
{
switch (byte >> 4)
{
case ODID_MESSAGETYPE_BASIC_ID:
return ODID_MESSAGETYPE_BASIC_ID;
case ODID_MESSAGETYPE_LOCATION:
return ODID_MESSAGETYPE_LOCATION;
case ODID_MESSAGETYPE_AUTH:
return ODID_MESSAGETYPE_AUTH;
case ODID_MESSAGETYPE_SELF_ID:
return ODID_MESSAGETYPE_SELF_ID;
case ODID_MESSAGETYPE_SYSTEM:
return ODID_MESSAGETYPE_SYSTEM;
case ODID_MESSAGETYPE_OPERATOR_ID:
return ODID_MESSAGETYPE_OPERATOR_ID;
case ODID_MESSAGETYPE_PACKED:
return ODID_MESSAGETYPE_PACKED;
default:
return ODID_MESSAGETYPE_INVALID;
}
}
/**
* Parse encoded Open Drone ID data to identify the message type. Then decode
* from Open Drone ID packed format into the appropriate Open Drone ID structure
*
* This function assumes that msgData points to a buffer containing all
* ODID_MESSAGE_SIZE bytes of an Open Drone ID message.
*
* The various Valid flags in uasData are set true whenever a message has been
* decoded and the corresponding data structure has been filled. The caller must
* clear these flags before calling decodeOpenDroneID().
*
* @param uasData Structure containing buffers for all message data
* @param msgData Pointer to a buffer containing a full encoded Open Drone ID
* message
* @return The message type: ODID_messagetype_t
*/
ODID_messagetype_t decodeOpenDroneID(ODID_UAS_Data *uasData, uint8_t *msgData)
{
if (!uasData || !msgData)
return ODID_MESSAGETYPE_INVALID;
switch (decodeMessageType(msgData[0]))
{
case ODID_MESSAGETYPE_BASIC_ID: {
ODID_BasicID_encoded *basicId = (ODID_BasicID_encoded *) msgData;
enum ODID_idtype idType;
if (getBasicIDType(basicId, &idType) == ODID_SUCCESS) {
// Find a free slot to store the current message in or overwrite old data of the same type.
for (int i = 0; i < ODID_BASIC_ID_MAX_MESSAGES; i++) {
enum ODID_idtype storedType = uasData->BasicID[i].IDType;
if (storedType == ODID_IDTYPE_NONE || storedType == idType) {
if (decodeBasicIDMessage(&uasData->BasicID[i], basicId) == ODID_SUCCESS) {
uasData->BasicIDValid[i] = 1;
return ODID_MESSAGETYPE_BASIC_ID;
}
}
}
}
break;
}
case ODID_MESSAGETYPE_LOCATION: {
ODID_Location_encoded *location = (ODID_Location_encoded *) msgData;
if (decodeLocationMessage(&uasData->Location, location) == ODID_SUCCESS) {
uasData->LocationValid = 1;
return ODID_MESSAGETYPE_LOCATION;
}
break;
}
case ODID_MESSAGETYPE_AUTH: {
ODID_Auth_encoded *auth = (ODID_Auth_encoded *) msgData;
int pageNum;
if (getAuthPageNum(auth, &pageNum) == ODID_SUCCESS) {
ODID_Auth_data *authData = &uasData->Auth[pageNum];
if (decodeAuthMessage(authData, auth) == ODID_SUCCESS) {
uasData->AuthValid[pageNum] = 1;
return ODID_MESSAGETYPE_AUTH;
}
}
break;
}
case ODID_MESSAGETYPE_SELF_ID: {
ODID_SelfID_encoded *selfId = (ODID_SelfID_encoded *) msgData;
if (decodeSelfIDMessage(&uasData->SelfID, selfId) == ODID_SUCCESS) {
uasData->SelfIDValid = 1;
return ODID_MESSAGETYPE_SELF_ID;
}
break;
}