Senet Packet API

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers senet_packet.h Source File

senet_packet.h

00001 /*
00002  * =====================================================================================
00003  *
00004  *       Filename:  senet_packet.h
00005  *
00006  *    Description: Senet Packet types 
00007  *
00008  *        Version:  1.0
00009  *        Created:  03/05/2016 03:13:20 PM
00010  *       Revision:  1
00011  *
00012  *         Author:  Shaun Nelson, coder extraodinaire
00013  *        Company:  Senet, Inc  
00014  *
00015  * =====================================================================================
00016  */
00017 
00018 #ifndef __SENET_PACKET__
00019 #define __SENET_PACKET__
00020 
00021 #include <stdint.h>
00022 #include <string.h>
00023 
00024 
00025 // Senet packet types
00026 enum SenetPacketT
00027 {
00028     SELF_ID_PACKET      = 0,
00029     RF_PACKET           = 1,
00030     GPS_PACKET          = 2,
00031     CONTROL_PACKET      = 3,
00032     BOOT_INFO_PACKET    = 4,
00033     SENSOR_PACKET       = 8,
00034     OCTET_STRING_PACKET = 126,
00035     UTF_8_STRING        = 127
00036 };
00037 
00038 
00039 /*
00040  * =====================================================================================
00041  *        Class:  SenetPacket
00042  *  Description:  Senet Packet Base class 
00043  * =====================================================================================
00044  */
00045 struct SenetPacket
00046 {
00047     static const uint32_t MAX_FRAME_SIZE = 242;
00048     static const uint8_t  VERSION        = 1;
00049 
00050     /*
00051      *--------------------------------------------------------------------------------------
00052      *       Class:  SenetPacket
00053      *      Method:  serialize
00054      * Description:  Packet serializer 
00055      *--------------------------------------------------------------------------------------
00056      */
00057     int32_t serialize();
00058 
00059     inline const uint8_t* payload() { return buffer;}
00060                  uint8_t  length () { return pktLen; }
00061 
00062     protected:
00063     // Common packet header
00064     struct PacketHeader
00065     {
00066         static const uint8_t HEADER_SIZE = 2;
00067 
00068         uint8_t version; // packet format versioni
00069         uint8_t type;    // Senet packet type
00070 
00071         PacketHeader(uint8_t _type=0)
00072         {
00073             version = VERSION;
00074             type    = _type;
00075         }
00076 
00077         int32_t serialize  (uint8_t *frame, int32_t len);
00078         bool    deserialize(uint8_t *frame, int32_t len);
00079     } header;
00080 
00081     uint8_t  pktLen;   
00082     uint8_t *buffer;
00083     uint8_t  bufferLen;
00084     bool     ownBuffer;
00085 
00086     SenetPacket(uint8_t senetPktType, uint8_t *_buffer=NULL, uint8_t _buflen=0);
00087    ~SenetPacket();
00088 
00089     /*
00090      *--------------------------------------------------------------------------------------
00091      *       Class:  SenetPacket
00092      *      Method:  serializePayload
00093      * Description:  Each unique packet type implements this to serialize its payload   
00094      *--------------------------------------------------------------------------------------
00095      */
00096     virtual int32_t serializePayload(uint8_t *frame, int32_t len) = 0; 
00097 
00098 };
00099 
00100 
00101 /*
00102  * =====================================================================================
00103  *        Class:  BootInfoPacket
00104  *  Description:  Device Boot information packet
00105  * =====================================================================================
00106  */
00107 struct BootInfoPacket : public SenetPacket
00108 {
00109     static const uint8_t BOOT_PAYLOAD_LENGTH = 8;
00110 
00111     uint16_t bootCount;       // number of device boots
00112     uint16_t resetCount;      // number of device resets
00113     uint32_t lastBootReason;  // last boot reason
00114 
00115     BootInfoPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00116         SenetPacket(BOOT_INFO_PACKET, _buffer, _buflen)
00117     {
00118         bootCount      = 0; 
00119         resetCount     = 0;
00120         lastBootReason = 0;
00121     }
00122 
00123     protected:
00124     /*
00125      *--------------------------------------------------------------------------------------
00126      *       Class:  BootInfoPacket
00127      *      Method:  serializePayload
00128      * Description:  Serialize packet data
00129      *--------------------------------------------------------------------------------------
00130      */
00131     virtual int32_t serializePayload(uint8_t *frame, int32_t len); 
00132 };
00133 
00134 
00135 /*
00136  * =====================================================================================
00137  *        Class:  ControlWordPacket
00138  *  Description:  Packet to configure device
00139  * =====================================================================================
00140  */
00141 struct ConfigWordPacket : public SenetPacket
00142 {
00143     static const uint8_t CONTROL_PAYLOAD_LENGTH = 9;
00144 
00145     ConfigWordPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00146         SenetPacket(CONTROL_PACKET, _buffer, _buflen) { config = 0; mask = 0; authKey = 0; }
00147 
00148     uint32_t config;  // configuration word
00149     uint32_t mask;    // valid bit mask applied to configuration word
00150     uint8_t  authKey; // Downlink authentication key 
00151 
00152     /*
00153      *--------------------------------------------------------------------------------------
00154      *       Class:  ConfigWordPacket 
00155      *      Method:  serializePayload
00156      * Description:  Serialize packet data
00157      *--------------------------------------------------------------------------------------
00158      */
00159     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00160 };
00161 
00162 
00163 /*
00164  * =====================================================================================
00165  *        Class:  GpsPacket
00166  *  Description:  Transmit device location in Decimal degress (http://www.en.wikipedia.org/wiki/Decimal_degrees)
00167  * =====================================================================================
00168  */
00169 struct GpsPacket : public SenetPacket
00170 {
00171     static const uint8_t GPS_PAYLOAD_LENGTH = 9;
00172 
00173            bool setCoordinates(uint32_t latitude, uint32_t longitude, uint16_t elevation);
00174     inline void setTxPower(uint8_t dBm) { txPower = dBm; }
00175 
00176 
00177     GpsPacket(uint8_t* _buffer=NULL, uint8_t _buflen=0):
00178         SenetPacket(GPS_PACKET, _buffer, _buflen)
00179     {
00180         latitude   = 0;
00181         longitude  = 0;
00182         elevation  = 0;
00183         txPower    = 0;
00184     }
00185 
00186     protected:
00187     uint32_t latitude;
00188     uint32_t longitude;
00189     uint16_t elevation;
00190     uint8_t  txPower;
00191 
00192     /*
00193      *--------------------------------------------------------------------------------------
00194      *       Class:  GpsPacket
00195      *      Method:  serializePayload
00196      * Description:  Serialize the data 
00197      *--------------------------------------------------------------------------------------
00198      */
00199     virtual int32_t serializePayload(uint8_t *frame, int32_t len); 
00200 };
00201 
00202 
00203 /*
00204  * =====================================================================================
00205  *        Class:  OctetStringPacket
00206  *  Description:  Variable length Octet String packet 
00207  * =====================================================================================
00208  */
00209 struct OctetStringPacket : public SenetPacket
00210 {
00211     bool setOctetString(uint8_t *os, uint8_t len);
00212 
00213     OctetStringPacket(uint8_t size); 
00214 
00215     protected:
00216     uint8_t  oslen;
00217     uint8_t  max;
00218 
00219     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00220 };
00221 
00222 /*
00223  * =====================================================================================
00224  *        Class:  RFDataPacket
00225  *  Description: Radio Data packet 
00226  * =====================================================================================
00227  */
00228 struct RFDataPacket : public SenetPacket
00229 {
00230     static const uint8_t RFDATA_PAYLOAD_LEN = 8;
00231 
00232     uint8_t  channel;   //  The channel the device sent on
00233     uint8_t  txpower;   //  The transmit power in dBm used by the device
00234     uint8_t  datarate;  //  The datarate used by the device
00235     uint8_t  snr;       //  Signal to Noise ratio of the last frame received
00236     uint8_t  rssi;      //  RSSI of the last frame received
00237     uint32_t timestamp; //  The device's current timestamp
00238 
00239     RFDataPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0):
00240         SenetPacket(RF_PACKET, _buffer, _buflen)
00241     { 
00242         channel   = 0;
00243         txpower   = 0;
00244         datarate  = 0;
00245         snr       = 0;
00246         rssi      = 0;
00247         timestamp = 0;
00248     }
00249 
00250     /*
00251      *--------------------------------------------------------------------------------------
00252      *       Class:  RFDataPacket
00253      *      Method:  serializePayload
00254      * Description:  Serialize the data 
00255      *--------------------------------------------------------------------------------------
00256      */
00257     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00258 };
00259 
00260 
00261 /*
00262  * =====================================================================================
00263  *        Class:  SelfIdPacket
00264  *  Description:  
00265  * =====================================================================================
00266  */
00267 struct SelfIdPacket : public SenetPacket
00268 {
00269     const static uint8_t EXT_POWER_SUPPLY_ID_MAX = 2;
00270     const static uint8_t EXT_POWER_SUPPLY_1      = 1;
00271     const static uint8_t EXT_POWER_SUPPLY_2      = 2;
00272     const static uint8_t BATTERY_LEVEL_MAX       = 7;
00273     const static uint8_t SELFID_PAYLOAD_LEN      = 8;
00274 
00275     bool setDeviceType          (uint32_t model, uint8_t revision);
00276     bool setSwVersion           (uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developer);
00277     void setBatteryFailState    (bool failed);
00278     bool setBatteryLevel        (uint8_t level);
00279     bool setExtPowerSupplyState (uint8_t id, bool isPresent);
00280 
00281     SelfIdPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00282         SenetPacket(SELF_ID_PACKET, _buffer, _buflen) { deviceType = 0; swVersion = 0; powerMask = 0; }
00283 
00284     protected:
00285     uint32_t deviceType;
00286     uint32_t swVersion;
00287     uint8_t  powerMask;
00288 
00289     /*
00290      *--------------------------------------------------------------------------------------
00291      *       Class:  SelfIdPacket 
00292      *      Method:  serializePayload
00293      * Description:  Serialize the data 
00294      *--------------------------------------------------------------------------------------
00295      */
00296     virtual int32_t serializePayload(uint8_t *frame, int32_t len); 
00297 };
00298 
00299 
00300 /*
00301  * =====================================================================================
00302  *        Class:  SensorPacket
00303  *  Description:  
00304  * =====================================================================================
00305  */
00306 struct SensorPacket : public SenetPacket
00307 {
00308     bool setPrimarySensor(uint16_t value) { return addSensorValue(0,1, value);};
00309     bool setTemperature  (uint16_t value) { return addSensorValue(1,2, value);}
00310     void reset();
00311 
00312     SensorPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00313         SenetPacket(SENSOR_PACKET, _buffer, _buflen) {}
00314 
00315     protected:
00316     static const uint8_t MAX_SENSOR_VALUES = 2;
00317 
00318     struct SensorValue
00319     {
00320         uint8_t  type;
00321         uint16_t value;
00322         bool     isSet;
00323 
00324         SensorValue() { type  = 0; value = 0; isSet = false;}
00325 
00326         int32_t serialize(uint8_t *frame, int32_t len)
00327         {
00328             if(len < 3)
00329                 return -1;
00330 
00331             frame[0] = type;
00332             frame[1] = (value >> 8) & 0xff;
00333             frame[2] = value & 0xff;
00334             return 3;
00335         }
00336     } sensorValue[MAX_SENSOR_VALUES];
00337 
00338             bool    addSensorValue(uint8_t position, uint8_t type, uint16_t value);
00339 
00340     /*
00341      *--------------------------------------------------------------------------------------
00342      *       Class:  SelfIdPacket 
00343      *      Method:  serializePayload
00344      * Description:  Serialize the data 
00345      *--------------------------------------------------------------------------------------
00346      */
00347     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00348 
00349 };
00350 
00351 #endif // __SENET_PACKET__ 
00352