Senet / Senet_Packet

Dependents:   MTDOT-UDKDemo_Senet Senet NAMote mDot-IKS01A1 unh-hackathon-example ... more

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     /*
00060      *--------------------------------------------------------------------------------------
00061      *       Class:  SenetPacket
00062      *      Method:  deserialize
00063      * Description:  Packet deserializer 
00064      *--------------------------------------------------------------------------------------
00065      */
00066     int32_t deserialize(uint8_t *frame, int32_t len);
00067 
00068     inline const uint8_t* payload() { return buffer;}
00069                  uint8_t  length () { return pktLen; }
00070 
00071     protected:
00072     // Common packet header
00073     struct PacketHeader
00074     {
00075         static const uint8_t HEADER_SIZE = 2;
00076 
00077         uint8_t version; // packet format versioni
00078         uint8_t type;    // Senet packet type
00079 
00080         PacketHeader(uint8_t _type=0)
00081         {
00082             version = VERSION;
00083             type    = _type;
00084         }
00085 
00086         int32_t serialize  (uint8_t *frame, int32_t len);
00087         int32_t deserialize(uint8_t *frame, int32_t len);
00088     } header;
00089 
00090     uint8_t  pktLen;   
00091     uint8_t *buffer;
00092     uint8_t  bufferLen;
00093     bool     ownBuffer;
00094 
00095     SenetPacket(uint8_t senetPktType, uint8_t *_buffer=NULL, uint8_t _buflen=0);
00096    ~SenetPacket();
00097 
00098     /*
00099      *--------------------------------------------------------------------------------------
00100      *       Class:  SenetPacket
00101      *      Method:  serializePayload
00102      * Description:  Each unique packet type implements this to serialize its payload   
00103      *--------------------------------------------------------------------------------------
00104      */
00105     virtual int32_t serializePayload(uint8_t *frame, int32_t len) = 0; 
00106 
00107     /*
00108      *--------------------------------------------------------------------------------------
00109      *       Class:  SenetPacket
00110      *      Method:  deserializePayload
00111      * Description:  Derived packet types can implement this to deserialize 
00112      *--------------------------------------------------------------------------------------
00113      */
00114     virtual int32_t deserializePayload(uint8_t *frame, int32_t len) {return 0;}
00115 
00116 };
00117 
00118 
00119 /*
00120  * =====================================================================================
00121  *        Class:  BootInfoPacket
00122  *  Description:  Device Boot information packet
00123  * =====================================================================================
00124  */
00125 struct BootInfoPacket : public SenetPacket
00126 {
00127     static const uint8_t BOOT_PAYLOAD_LENGTH = 9;
00128 
00129     uint16_t bootCount;       // number of device boots
00130     uint16_t resetCount;      // number of device resets
00131     uint32_t lastBootReason;  // last boot reason
00132     uint8_t  authKey;         
00133 
00134     BootInfoPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00135         SenetPacket(BOOT_INFO_PACKET, _buffer, _buflen)
00136     {
00137         bootCount      = 0; 
00138         resetCount     = 0;
00139         lastBootReason = 0;
00140         authKey        = 0;
00141     }
00142 
00143     protected:
00144     /*
00145      *--------------------------------------------------------------------------------------
00146      *       Class:  BootInfoPacket
00147      *      Method:  serializePayload
00148      * Description:  Serialize packet data
00149      *--------------------------------------------------------------------------------------
00150      */
00151     virtual int32_t serializePayload(uint8_t *frame, int32_t len); 
00152 
00153     /*
00154      *--------------------------------------------------------------------------------------
00155      *       Class:  BootInfoPacket 
00156      *      Method:  deserializePayload
00157      * Description:  Deserialize packet data
00158      *--------------------------------------------------------------------------------------
00159      */
00160     virtual int32_t deserializePayload(uint8_t *frame, int32_t len); 
00161 };
00162 
00163 
00164 /*
00165  * =====================================================================================
00166  *        Class:  ConfigWordPacket
00167  *  Description:  Packet to configure device
00168  * =====================================================================================
00169  */
00170 struct ConfigWordPacket : public SenetPacket
00171 {
00172     static const uint8_t CONTROL_PAYLOAD_LENGTH = 9;
00173 
00174     ConfigWordPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00175         SenetPacket(CONTROL_PACKET, _buffer, _buflen) { config = 0; mask = 0; authKey = 0; }
00176 
00177     uint32_t config;  // configuration word
00178     uint32_t mask;    // valid bit mask applied to configuration word
00179     uint8_t  authKey; // Downlink authentication key 
00180 
00181     /*
00182      *--------------------------------------------------------------------------------------
00183      *       Class:  ConfigWordPacket 
00184      *      Method:  serializePayload
00185      * Description:  Serialize packet data
00186      *--------------------------------------------------------------------------------------
00187      */
00188     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00189 
00190     /*
00191      *--------------------------------------------------------------------------------------
00192      *       Class:  ConfigWordPacket 
00193      *      Method:  deserializePayload
00194      * Description:  Deserialize packet data
00195      *--------------------------------------------------------------------------------------
00196      */
00197     virtual int32_t deserializePayload(uint8_t *frame, int32_t len); 
00198 };
00199 
00200 
00201 /*
00202  * =====================================================================================
00203  *        Class:  GpsPacket
00204  *  Description:  Transmit device location in Decimal degress (http://www.en.wikipedia.org/wiki/Decimal_degrees)
00205  * =====================================================================================
00206  */
00207 struct GpsPacket : public SenetPacket
00208 {
00209     static const uint8_t GPS_PAYLOAD_LENGTH = 9;
00210 
00211            bool setCoordinates(int32_t latitude, int32_t longitude, uint16_t elevation);
00212     inline void setTxPower(uint8_t dBm) { txPower = dBm; }
00213 
00214 
00215     GpsPacket(uint8_t* _buffer=NULL, uint8_t _buflen=0):
00216         SenetPacket(GPS_PACKET, _buffer, _buflen)
00217     {
00218         latitude   = 0;
00219         longitude  = 0;
00220         elevation  = 0;
00221         txPower    = 0;
00222     }
00223 
00224     protected:
00225     uint32_t latitude;
00226     uint32_t longitude;
00227     uint16_t elevation;
00228     uint8_t  txPower;
00229 
00230     /*
00231      *--------------------------------------------------------------------------------------
00232      *       Class:  GpsPacket
00233      *      Method:  serializePayload
00234      * Description:  Serialize the data 
00235      *--------------------------------------------------------------------------------------
00236      */
00237     virtual int32_t serializePayload(uint8_t *frame, int32_t len); 
00238 };
00239 
00240 
00241 /*
00242  * =====================================================================================
00243  *        Class:  OctetStringPacket
00244  *  Description:  Variable length Octet String packet 
00245  * =====================================================================================
00246  */
00247 struct OctetStringPacket : public SenetPacket
00248 {
00249     bool setOctetString(uint8_t *os, uint8_t len);
00250 
00251     OctetStringPacket(uint8_t size); 
00252 
00253     protected:
00254     uint8_t  oslen;
00255     uint8_t  max;
00256 
00257     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00258 };
00259 
00260 /*
00261  * =====================================================================================
00262  *        Class:  RFDataPacket
00263  *  Description: Radio Data packet 
00264  * =====================================================================================
00265  */
00266 struct RFDataPacket : public SenetPacket
00267 {
00268     static const uint8_t RFDATA_PAYLOAD_LEN = 9;
00269 
00270     uint8_t  channel;   //  The channel the device sent on
00271     uint8_t  txpower;   //  The transmit power in dBm used by the device
00272     uint8_t  datarate;  //  The datarate used by the device
00273     uint8_t  snr;       //  Signal to Noise ratio of the last frame received
00274     uint8_t  rssi;      //  RSSI of the last frame received
00275     uint32_t timestamp; //  The device's current timestamp
00276 
00277     RFDataPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0):
00278         SenetPacket(RF_PACKET, _buffer, _buflen)
00279     { 
00280         channel   = 0;
00281         txpower   = 0;
00282         datarate  = 0;
00283         snr       = 0;
00284         rssi      = 0;
00285         timestamp = 0;
00286     }
00287 
00288     /*
00289      *--------------------------------------------------------------------------------------
00290      *       Class:  RFDataPacket
00291      *      Method:  serializePayload
00292      * Description:  Serialize the data 
00293      *--------------------------------------------------------------------------------------
00294      */
00295     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00296 };
00297 
00298 
00299 /*
00300  * =====================================================================================
00301  *        Class:  SelfIdPacket
00302  *  Description:  
00303  * =====================================================================================
00304  */
00305 struct SelfIdPacket : public SenetPacket
00306 {
00307     const static uint8_t EXT_POWER_SUPPLY_ID_MAX = 2;
00308     const static uint8_t EXT_POWER_SUPPLY_1      = 1;
00309     const static uint8_t EXT_POWER_SUPPLY_2      = 2;
00310     const static uint8_t BATTERY_LEVEL_MAX       = 7;
00311     const static uint8_t SELFID_PAYLOAD_LEN      = 9;
00312 
00313     bool setDeviceType          (uint32_t model, uint8_t revision);
00314     bool setSwVersion           (uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developer);
00315     void setBatteryFailState    (bool failed);
00316     bool setBatteryLevel        (uint8_t level);
00317     bool setExtPowerSupplyState (uint8_t id, bool isPresent);
00318 
00319     SelfIdPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00320         SenetPacket(SELF_ID_PACKET, _buffer, _buflen) { deviceType = 0; swVersion = 0; powerMask = 0; }
00321 
00322     protected:
00323     uint32_t deviceType;
00324     uint32_t swVersion;
00325     uint8_t  powerMask;
00326 
00327     /*
00328      *--------------------------------------------------------------------------------------
00329      *       Class:  SelfIdPacket 
00330      *      Method:  serializePayload
00331      * Description:  Serialize the data 
00332      *--------------------------------------------------------------------------------------
00333      */
00334     virtual int32_t serializePayload(uint8_t *frame, int32_t len); 
00335 };
00336 
00337 
00338 /*
00339  * =====================================================================================
00340  *        Class:  SensorPacket
00341  *  Description:  
00342  * =====================================================================================
00343  */
00344 struct SensorPacket : public SenetPacket
00345 {
00346     bool setPrimarySensor(uint16_t value) { return addSensorValue(0,1, value);}
00347     bool setTemperature  (uint16_t value) { return addSensorValue(1,2, value);}
00348     bool setPressure     (uint16_t value) { return addSensorValue(2,3, value);}
00349     void reset();
00350 
00351     SensorPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
00352         SenetPacket(SENSOR_PACKET, _buffer, _buflen) {}
00353 
00354     public:
00355     static const uint8_t MAX_SENSOR_VALUES = 3;
00356 
00357     struct SensorValue
00358     {
00359         uint8_t  type;
00360         uint16_t value;
00361         bool     isSet;
00362 
00363         SensorValue() { type  = 0; value = 0; isSet = false;}
00364 
00365         int32_t serialize(uint8_t *frame, int32_t len)
00366         {
00367             if(len < 3)
00368                 return -1;
00369 
00370             frame[0] = type;
00371             frame[1] = (value >> 8) & 0xff;
00372             frame[2] = value & 0xff;
00373             return 3;
00374         }
00375     } sensorValue[MAX_SENSOR_VALUES];
00376 
00377     bool    addSensorValue(uint8_t position, uint8_t type, uint16_t value);
00378 
00379     virtual int32_t serializePayload(uint8_t *frame, int32_t len);
00380     virtual int32_t deserializePayload(uint8_t *frame, int32_t len);
00381 
00382 };
00383 
00384 #endif // __SENET_PACKET__ 
00385