Senet Packet API

senet_packet.h

Committer:
shaunkrnelson
Date:
2016-03-05
Revision:
0:08689149c8e3
Child:
1:c4435fed9eb9

File content as of revision 0:08689149c8e3:

/*
 * =====================================================================================
 *
 *       Filename:  senet_packet.h
 *
 *    Description: Senet Packet types 
 *
 *        Version:  1.0
 *        Created:  03/05/2016 03:13:20 PM
 *       Revision:  1
 *
 *         Author:  Shaun Nelson, coder extraodinaire
 *        Company:  Senet, Inc  
 *
 * =====================================================================================
 */
#include <stdint.h>
#include <assert.h>
#include <string.h>

#define ASSERT(_expr) assert(_expr)

// Senet packet types
enum SenetPacketT
{
    SELF_ID_PACKET      = 0,
    RF_PACKET           = 1,
    GPS_PACKET          = 2,
    CONTROL_PACKET      = 3,
    BOOT_INFO_PACKET    = 4,
    SENSOR_PACKET       = 8,
    OCTET_STRING_PACKET = 126,
    UTF_8_STRING        = 127
};


/*
 * =====================================================================================
 *        Class:  SenetLoRaPacket
 *  Description:  Senet Packet Base class 
 * =====================================================================================
 */
struct SenetLoRaPacket
{
    static const uint32_t MAX_FRAME_SIZE = 242;
    static const uint8_t  VERSION        = 1;


    // Common packet header
    struct PacketHeader
    {
        uint8_t version; // packet format versioni
        uint8_t type;    // Senet packet type

        PacketHeader(uint8_t _type=0)
        {
            version = VERSION;
            type    = _type;
        }

        int32_t serialize  (uint8_t *frame, int32_t len);
        bool    deserialize(uint8_t *frame, int32_t len);
    } header;

    uint8_t pkt[MAX_FRAME_SIZE]; // serialized packet buffer
    uint8_t pktLen;              // serialized packet length

    SenetLoRaPacket(uint8_t senetPktType)
    {
        header.type    = senetPktType;
        header.version = VERSION;
        pktLen         = 0;
    }

    /*
     *--------------------------------------------------------------------------------------
     *       Class:  SenetLoRaPacket
     *      Method:  serialize
     * Description:  Packet serializer 
     *--------------------------------------------------------------------------------------
     */
    virtual int32_t serialize()
    {
        pktLen = header.serialize(pkt, MAX_FRAME_SIZE);

        int32_t payloadLen = serializeData(pkt + pktLen, MAX_FRAME_SIZE - pktLen);
        if(payloadLen > 0)
        {
            pktLen += payloadLen;
            return pktLen;
        }
        return -1;
    }

    
    /*
     *--------------------------------------------------------------------------------------
     *       Class:  SenetLoRaPacket
     *      Method:  serializeData
     * Description:  Each unique packet type implements this to serialize its data   
     *--------------------------------------------------------------------------------------
     */
    virtual int32_t serializeData(uint8_t *frame, int32_t len) {return 0;}

};


/*
 * =====================================================================================
 *        Class:  BootInfoPacket
 *  Description:  Device Boot information packet
 * =====================================================================================
 */
struct BootInfoPacket : public SenetLoRaPacket
{
    uint16_t bootCount;       // number of device boots
    uint16_t resetCount;      // number of device resets
    uint32_t lastBootReason;  // last boot reason

    BootInfoPacket() :
        SenetLoRaPacket(BOOT_INFO_PACKET)
    {
        bootCount      = 0; 
        resetCount     = 0;
        lastBootReason = 0;
    }

    protected:
    /*
     *--------------------------------------------------------------------------------------
     *       Class:  BootInfoPacket
     *      Method:  serializeData
     * Description:  Serialize packet data
     *--------------------------------------------------------------------------------------
     */
    int32_t serializeData(uint8_t *frame, int32_t len); 
};


/*
 * =====================================================================================
 *        Class:  ControlWordPacket
 *  Description:  Packet to configure device
 * =====================================================================================
 */
struct ConfigWordPacket : public SenetLoRaPacket
{
    ConfigWordPacket() :
        SenetLoRaPacket(CONTROL_PACKET) { config = 0; mask = 0; authKey = 0; }

    uint32_t config;  // configuration word
    uint32_t mask;    // valid bit mask applied to configuration word
    uint8_t  authKey; // Downlink authentication key 

    /*
     *--------------------------------------------------------------------------------------
     *       Class:  ConfigWordPacket 
     *      Method:  serializeData
     * Description:  Serialize packet data
     *--------------------------------------------------------------------------------------
     */
    int32_t serializeData(uint8_t *frame, int32_t len);
};


/*
 * =====================================================================================
 *        Class:  GpsPacket
 *  Description:  Transmit device location in Decimal degress (http://www.en.wikipedia.org/wiki/Decimal_degrees)
 * =====================================================================================
 */
struct GpsPacket : public SenetLoRaPacket
{
           bool setCoordinates(uint32_t latitude, uint32_t longitude, uint16_t elevation);
    inline void setTxPower(uint8_t dBm) { txPower = dBm; }

    GpsPacket():
        SenetLoRaPacket(GPS_PACKET)
    {
        latitude   = 0;
        longitude  = 0;
        elevation  = 0;
        txPower    = 0;
    }

    protected:
    uint32_t latitude;
    uint32_t longitude;
    uint16_t elevation;
    uint8_t  txPower;

    /*
     *--------------------------------------------------------------------------------------
     *       Class:  GpsPacket
     *      Method:  serializeData
     * Description:  Serialize the data 
     *--------------------------------------------------------------------------------------
     */
    int32_t serializeData(uint8_t *frame, int32_t len); 
};


/*
 * =====================================================================================
 *        Class:  OctetStringPacket
 *  Description:  Variable length Octet String packet 
 * =====================================================================================
 */
struct OctetStringPacket : public SenetLoRaPacket
{
    bool setOctetString(uint8_t *os, uint8_t len);

    OctetStringPacket(uint8_t size) 
        : SenetLoRaPacket(OCTET_STRING_PACKET)
    { 
        osptr = new uint8_t[size];
        ASSERT(osptr != NULL);
        maxSize = size;
        oslen   = 0;
    }

    protected:
    uint8_t *osptr;
    uint8_t  maxSize;
    uint8_t  oslen;

    virtual int32_t serializeData(uint8_t *frame, int32_t len);
};


/*
 * =====================================================================================
 *        Class:  RFDataPacket
 *  Description: Radio Data packet 
 * =====================================================================================
 */
struct RFDataPacket : public SenetLoRaPacket
{
    uint8_t  channel;   //  The channel the device sent on
    uint8_t  txpower;   //  The transmit power in dBm used by the device
    uint8_t  datarate;  //  The datarate used by the device
    uint8_t  snr;       //  Signal to Noise ratio of the last frame received
    uint8_t  rssi;      //  RSSI of the last frame received
    uint32_t timestamp; //  The device's current timestamp

    RFDataPacket():
        SenetLoRaPacket(RF_PACKET)
    { 
        channel   = 0;
        txpower   = 0;
        datarate  = 0;
        snr       = 0;
        rssi      = 0;
        timestamp = 0;
    }

    /*
     *--------------------------------------------------------------------------------------
     *       Class:  RFDataPacket
     *      Method:  serializeData
     * Description:  Serialize the data 
     *--------------------------------------------------------------------------------------
     */
    int32_t serializeData(uint8_t *frame, int32_t len);
};


/*
 * =====================================================================================
 *        Class:  SelfIdPacket
 *  Description:  
 * =====================================================================================
 */
struct SelfIdPacket : public SenetLoRaPacket
{
    const static uint8_t EXT_POWER_SUPPLY_ID_MAX = 2;
    const static uint8_t EXT_POWER_SUPPLY_1      = 1;
    const static uint8_t EXT_POWER_SUPPLY_2      = 2;
    const static uint8_t BATTERY_LEVEL_MAX       = 7;

    bool setDeviceType          (uint32_t model, uint8_t revision);
    bool setSwVersion           (uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developer);
    void setBatteryFailState    (bool failed);
    bool setBatteryLevel        (uint8_t level);
    bool setExtPowerSupplyState (uint8_t id, bool isPresent);

    SelfIdPacket() :
        SenetLoRaPacket(SELF_ID_PACKET) { deviceType = 0; swVersion = 0; powerMask = 0; }

    protected:
    uint32_t deviceType;
    uint32_t swVersion;
    uint8_t  powerMask;

    /*
     *--------------------------------------------------------------------------------------
     *       Class:  SelfIdPacket 
     *      Method:  serializeData
     * Description:  Serialize the data 
     *--------------------------------------------------------------------------------------
     */
    virtual int32_t serializeData(uint8_t *frame, int32_t len); 
};


/*
 * =====================================================================================
 *        Class:  SensorPacket
 *  Description:  
 * =====================================================================================
 */
struct SensorPacket : public SenetLoRaPacket
{
    bool setPrimarySensor(uint16_t value) { return addSensorValue(0,1, value);};
    bool setTemperature  (uint16_t value) { return addSensorValue(1,2, value);}
    void reset();

    SensorPacket() :
        SenetLoRaPacket(SENSOR_PACKET) {}

    protected:
    static const uint8_t MAX_SENSOR_VALUES = 2;

    struct SensorValue
    {
        uint8_t  type;
        uint16_t value;
        bool     isSet;

        SensorValue() { type  = 0; value = 0; isSet = false;}

        int32_t serialize(uint8_t *frame, int32_t len)
        {
            if(len < 3)
                return -1;

            frame[0] = type;
            frame[1] = (value >> 8) & 0xff;
            frame[2] = value & 0xff;
            return 3;
        }
    } sensorValue[MAX_SENSOR_VALUES];

            bool    addSensorValue(uint8_t position, uint8_t type, uint16_t value);

    /*
     *--------------------------------------------------------------------------------------
     *       Class:  SelfIdPacket 
     *      Method:  serializeData
     * Description:  Serialize the data 
     *--------------------------------------------------------------------------------------
     */
    virtual int32_t serializeData(uint8_t *frame, int32_t len);

};