LCOV - code coverage report
Current view: top level - AP_ADSB/sagetech-sdk - sgEncodeInstall.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 34 41 82.9 %
Date: 2026-09-09 11:02:08 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @copyright Copyright (c) 2021 Sagetech, Inc. All rights reserved.
       3             :  *
       4             :  * @file   sgEncodeInstall.c
       5             :  * @author Jacob.Garrison
       6             :  *
       7             :  * @date   Feb 23, 2021
       8             :  *
       9             :  * This file receives a populated installation message struct and
      10             :  * converts it into a installation message buffer.
      11             :  */
      12             : 
      13             : #include <ctype.h>
      14             : #include <string.h>
      15             : #include <stdbool.h>
      16             : 
      17             : #include "sg.h"
      18             : #include "sgUtil.h"
      19             : 
      20             : #define SG_PAYLOAD_LEN_INSTALL SG_MSG_LEN_INSTALL - 5 /// the payload length.
      21             : 
      22             : #define PBASE 4             /// the payload offset.
      23             : #define OFFSET_ICAO 0       /// the icao address offset in the payload.
      24             : #define OFFSET_REG 3        /// the registration offset in the payload.
      25             : #define OFFSET_RSVD1 10     /// the first reserved field offset in the payload.
      26             : #define OFFSET_COM0 12      /// the COM port 0 offset in the payload.
      27             : #define OFFSET_COM1 13      /// the COM port 1 offset in the payload.
      28             : #define OFFSET_IP 14        /// the IP address offset in the payload.
      29             : #define OFFSET_MASK 18      /// the net mask offset in the payload.
      30             : #define OFFSET_PORT 22      /// the port number offset in the payload.
      31             : #define OFFSET_GPS 24       /// the GPS integrity offset in the payload.
      32             : #define OFFSET_EMIT_SET 25  /// the emitter category offset in the payload.
      33             : #define OFFSET_EMIT_TYPE 26 /// the emitter type offset in the payload.
      34             : #define OFFSET_SIZE 27      /// the aircraft size offset in the payload.
      35             : #define OFFSET_SPEED 28     /// the maximum airspeed offset in the payload.
      36             : #define OFFSET_ENCODER 29   /// the altitude-encoder-offset offset in the payload.
      37             : #define OFFSET_RSVD2 31     /// the second reserved field offset in the payload.
      38             : #define OFFSET_CONFIG 33    /// the configuration offset in the payload.
      39             : #define OFFSET_RSVD3 34     /// the third reserved field offset in the payload.
      40             : 
      41             : #define REG_LEN 7 /// the registration field length.
      42             : /*
      43             :  * Documented in the header file.
      44             :  */
      45           2 : bool sgEncodeInstall(uint8_t *buffer, sg_install_t *stl, uint8_t msgId)
      46             : {
      47             :     // populate header
      48           2 :     buffer[0] = SG_MSG_START_BYTE;
      49           2 :     buffer[1] = SG_MSG_TYPE_HOST_INSTALL;
      50           2 :     buffer[2] = msgId;
      51           2 :     buffer[3] = SG_PAYLOAD_LEN_INSTALL;
      52             : 
      53             :     // populate icao address
      54           2 :     icao2Buf(&buffer[PBASE + OFFSET_ICAO], stl->icao);
      55             : 
      56             :     // populate aircraft registration
      57           2 :     charArray2Buf(&buffer[PBASE + OFFSET_REG], stl->reg, REG_LEN);
      58             : 
      59             :     // populate reserved fields
      60           2 :     uint162Buf(&buffer[PBASE + OFFSET_RSVD1], 0);
      61             : 
      62             :     // populate COM port 0, correct enum offset
      63           2 :     buffer[PBASE + OFFSET_COM0] = stl->com0;
      64             : 
      65             :     // populate COM port 1, correct enum offset
      66           2 :     buffer[PBASE + OFFSET_COM1] = stl->com1;
      67             : 
      68             :     // populate IP address
      69           2 :     uint322Buf(&buffer[PBASE + OFFSET_IP], stl->eth.ipAddress);
      70             : 
      71             :     // populate net mask
      72           2 :     uint322Buf(&buffer[PBASE + OFFSET_MASK], stl->eth.subnetMask);
      73             : 
      74             :     // populate port number
      75           2 :     uint162Buf(&buffer[PBASE + OFFSET_PORT], stl->eth.portNumber);
      76             : 
      77             :     // populate gps integrity
      78           2 :     buffer[PBASE + OFFSET_GPS] = stl->sil << 4 |
      79           2 :                                  stl->sda;
      80             : 
      81             :     // populate emitter category set and type
      82             :     uint8_t emitSet;
      83             :     uint8_t emitType;
      84           2 :     if (stl->emitter < SG_EMIT_OFFSET_B) // group A
      85             :     {
      86           0 :         emitSet = SG_EMIT_GROUP_A;
      87           0 :         emitType = stl->emitter - SG_EMIT_OFFSET_A;
      88             :     }
      89           2 :     else if (stl->emitter < SG_EMIT_OFFSET_C) // group B
      90             :     {
      91           2 :         emitSet = SG_EMIT_GROUP_B;
      92           2 :         emitType = stl->emitter - SG_EMIT_OFFSET_B;
      93             :     }
      94           0 :     else if (stl->emitter < SG_EMIT_OFFSET_D) // group C
      95             :     {
      96           0 :         emitSet = SG_EMIT_GROUP_C;
      97           0 :         emitType = stl->emitter - SG_EMIT_OFFSET_C;
      98             :     }
      99             :     else // group D
     100             :     {
     101           0 :         emitSet = SG_EMIT_GROUP_D;
     102           0 :         emitType = stl->emitter - SG_EMIT_OFFSET_D;
     103             :     }
     104           2 :     buffer[PBASE + OFFSET_EMIT_SET] = emitSet;
     105           2 :     buffer[PBASE + OFFSET_EMIT_TYPE] = emitType;
     106             : 
     107             :     // populate aircraft size
     108           2 :     buffer[PBASE + OFFSET_SIZE] = stl->size;
     109             : 
     110             :     // populate max airspeed
     111           2 :     buffer[PBASE + OFFSET_SPEED] = stl->maxSpeed;
     112             : 
     113             :     // populate altitude encoder offset
     114           2 :     uint162Buf(&buffer[PBASE + OFFSET_ENCODER], stl->altOffset);
     115             : 
     116             :     // populate reserved fields
     117           2 :     uint162Buf(&buffer[PBASE + OFFSET_RSVD2], 0);
     118             : 
     119             :     // populate install configuration
     120           2 :     buffer[PBASE + OFFSET_CONFIG] = stl->wowConnected << 7 |
     121           2 :                                     stl->heater << 6 |
     122           2 :                                     stl->airspeedTrue << 5 |
     123           2 :                                     stl->hdgTrueNorth << 4 |
     124           2 :                                     stl->altRes100 << 3 |
     125           2 :                                     stl->antenna;
     126             : 
     127             :     // populate reserved fields
     128           2 :     uint162Buf(&buffer[PBASE + OFFSET_RSVD3], 0);
     129             : 
     130             :     // populate checksum
     131           2 :     appendChecksum(buffer, SG_MSG_LEN_INSTALL);
     132             : 
     133           2 :     return true;
     134             : }

Generated by: LCOV version 1.14