CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_ADSB/sagetech-sdk/sgDecodeAck.c
Views: 1799
1
/**
2
* @copyright Copyright (c) 2021 Sagetech, Inc. All rights reserved.
3
*
4
* @file sgDecodeAck.c
5
* @author jimb
6
*
7
* @date Feb 10, 2021
8
*
9
* This file receives a raw Acknowledge message buffer and
10
* parses the payload into a data struct.
11
*/
12
13
#include <string.h>
14
#include <stdbool.h>
15
#include <math.h>
16
17
#include "sg.h"
18
#include "sgUtil.h"
19
20
#define SG_ACK_XPNDR_FAIL 0x01
21
#define SG_ACK_SYSTEM_FAIL 0x02
22
#define SG_ACK_CRYPTO_FAIL 0x04
23
#define SG_ACK_WOW 0x08
24
#define SG_ACK_MAINT 0x10
25
#define SG_ACK_ALT_SOURCE 0x20
26
#define SG_ACK_OP_MODE 0xC0
27
28
typedef struct __attribute__((packed))
29
{
30
uint8_t start;
31
uint8_t type;
32
uint8_t id;
33
uint8_t payloadLen;
34
uint8_t ackType;
35
uint8_t ackId;
36
uint8_t state;
37
uint8_t alt[3];
38
uint8_t checksum;
39
} ack_t;
40
41
/*
42
* Documented in the header file.
43
*/
44
bool sgDecodeAck(uint8_t *buffer, sg_ack_t *ack)
45
{
46
ack_t sgAck;
47
memcpy(&sgAck, buffer, sizeof(ack_t));
48
49
ack->ackType = sgAck.ackType;
50
ack->ackId = sgAck.ackId;
51
ack->failXpdr = (sgAck.state & SG_ACK_XPNDR_FAIL) > 0;
52
ack->failSystem = (sgAck.state & SG_ACK_SYSTEM_FAIL) > 0;
53
ack->failCrypto = (sgAck.state & SG_ACK_CRYPTO_FAIL) > 0;
54
ack->wow = (sgAck.state & SG_ACK_WOW) > 0;
55
ack->maint = (sgAck.state & SG_ACK_MAINT) > 0;
56
ack->isHostAlt = (sgAck.state & SG_ACK_ALT_SOURCE) > 0;
57
ack->opMode = (sgAck.state & SG_ACK_OP_MODE) >> 6;
58
59
int32_t int24 = toInt32(sgAck.alt);
60
61
// Bitmask altitude field to determine if alt = invalid
62
if ((int24 & 0x00FFFFFF) == 0x00800000)
63
{
64
ack->alt = 0;
65
ack->altValid = false;
66
}
67
else
68
{
69
ack->alt = int24;
70
ack->altValid = true;
71
}
72
73
return true;
74
}
75
76