Line data Source code
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 3 : bool sgDecodeAck(uint8_t *buffer, sg_ack_t *ack) 45 : { 46 : ack_t sgAck; 47 3 : memcpy(&sgAck, buffer, sizeof(ack_t)); 48 : 49 3 : ack->ackType = sgAck.ackType; 50 3 : ack->ackId = sgAck.ackId; 51 3 : ack->failXpdr = (sgAck.state & SG_ACK_XPNDR_FAIL) > 0; 52 3 : ack->failSystem = (sgAck.state & SG_ACK_SYSTEM_FAIL) > 0; 53 3 : ack->failCrypto = (sgAck.state & SG_ACK_CRYPTO_FAIL) > 0; 54 3 : ack->wow = (sgAck.state & SG_ACK_WOW) > 0; 55 3 : ack->maint = (sgAck.state & SG_ACK_MAINT) > 0; 56 3 : ack->isHostAlt = (sgAck.state & SG_ACK_ALT_SOURCE) > 0; 57 3 : ack->opMode = (sgAck.state & SG_ACK_OP_MODE) >> 6; 58 : 59 3 : int32_t int24 = toInt32(sgAck.alt); 60 : 61 : // Bitmask altitude field to determine if alt = invalid 62 3 : if ((int24 & 0x00FFFFFF) == 0x00800000) 63 : { 64 0 : ack->alt = 0; 65 0 : ack->altValid = false; 66 : } 67 : else 68 : { 69 3 : ack->alt = int24; 70 3 : ack->altValid = true; 71 : } 72 : 73 3 : return true; 74 : }