BayEOSArduino Library
BayEOS.h
1 
50 #ifndef BayEOS_h
51 #define BayEOS_h
52 #define BayEOS_VERSION "1.6"
53 /*
54  * Frame-Types
55  */
56 
57 #define BayEOS_DataFrame 0x1
58 #define BayEOS_Command 0x2 /* [0x2][CMD][ARGS...] */
59 #define BayEOS_CommandResponse 0x3 /* [0x3][CMD][RESPONSE...] */
60 #define BayEOS_Message 0x4
61 #define BayEOS_ErrorMessage 0x5
62 /*
63  * Routed, Delayed, RoutedRSSI, Timestamp Frame:
64  * These frames wrap the original frame. In principle this can be
65  * used several times. However the effective payload is reduced by
66  * five (six with RSSI) byte per hub
67  */
68 #define BayEOS_RoutedFrame 0x6 /* [0x6][MY_ID][PANID][Original Frame] */
69 #define BayEOS_DelayedFrame 0x7 /* [0x7][(unsigned long) delay (msec)][Original Frame] */
70 #define BayEOS_RoutedFrameRSSI 0x8 /* [0x9][MY_ID][PANID][RSSI][Original Frame]
71 Note RSSI is negative but without sign as uint8_t
72 */
73 #define BayEOS_TimestampFrame 0x9 /* [0x9][(unsigned long) timestamp (sec since 2000-01-01 00:00 GMT)][Original Frame] */
74 #define BayEOS_BinaryFrame 0xa /* [0xa][(unsigned long) pos][binary data] */
75 #define BayEOS_OriginFrame 0xb /* [0xb][origin_length][ORIGIN][Original Frame] -> Origin replaces current origin*/
76 #define BayEOS_MillisecondTimestampFrame 0xc /* [0xc][(long long) timestamp (millisec since 1970-01-01 00:00 GMT)][Original Frame] */
77 #define BayEOS_RoutedOriginFrame 0xd /* [0xd][origin_length][ORIGIN][Original Frame] -> Origin is appended to current origin using "/" */
78 
79 /*Send commands to the Gateway via frame/save interface */
80 #define BayEOS_GatewayCommand 0xe /* [0xe][type][ARGS...] */
81 #define BayEOS_GatewayCommand_SetName 0x1 /* [0xe][0x1][Name] */
82 #define BayEOS_GatewayCommand_ApplyTemplate 0x2 /* [0xe][0x2][TemplateName/UUID/URL] */
83 #define BayEOS_GatewayCommand_SetSamplingInt 0x3 /* [0xe][0x3][long] */
84 #define BayEOS_GatewayCommand_SetCheckDelay 0x4 /* [0xe][0x4][long] */
85 
86 //#define BayEOS_GatewayCommand_SetChannelName 0x21 /* [0xe][0x2][NR][Name] */
87 
88 #define BayEOS_ChecksumFrame 0xf /* [0xf][frame][checksum_16bit] */
89 #define BayEOS_DelayedSecondFrame 0x10 /* [0x10][(unsigned long) delay (sec)][Original Frame] */
90 #define BayEOS_RF24Frame 0x11 /* [0x11][uint8_t pipe][Original Frame] */
91 
92 #define BayEOS_Action 0x12 /* [0x12][Key][ARGS...] */
93 #define BayEOS_ActionResponse 0x13 /* [0x13][Key][Status][RESPONSE...] */
94 #define BayEOS_ActionSuccess 0x0
95 #define BayEOS_ActionFailed 0x1
96 
97 
98 
99 /* BayEOS Data Frames */
100 /* [0x1][0x1][offset][[float]]+... */
101 #define BayEOS_Float32le 0x1
102 #define BayEOS_Int32le 0x2
103 #define BayEOS_Int16le 0x3
104 #define BayEOS_UInt8 0x4
105 #define BayEOS_Double64le 0x5
106 #define BayEOS_String 0x6 /* [length (2b)][string] */
107 #define BayEOS_Binary 0x7 /* [offset][length (2b)][data] */
108 
109 /* [0x1][0x21][[float]]+... */
110 #define BayEOS_WithoutOffsetFloat32le 0x21
111 #define BayEOS_WithoutOffsetInt32le 0x22
112 #define BayEOS_WithoutOffsetInt16le 0x23
113 #define BayEOS_WithoutOffsetUInt8 0x24
114 #define BayEOS_WithoutOffsetDouble64le 0x25
115 /* [0x1][0x41][[Channel number][float]]+... */
116 #define BayEOS_ChannelFloat32le 0x41
117 #define BayEOS_ChannelInt32le 0x42
118 #define BayEOS_ChannelInt16le 0x43
119 #define BayEOS_ChannelUInt8 0x44
120 #define BayEOS_ChannelDouble64le 0x45
121  /* [0x1][0x61][[Label length][ChannelLabel][float]]+... */
122 #define BayEOS_LabelledChannelFloat32le 0x61
123 #define BayEOS_LabelledChannelInt32le 0x62
124 #define BayEOS_LabelledChannelInt16le 0x63
125 #define BayEOS_LabelledChannelUInt8 0x64
126 #define BayEOS_LabelledChannelDouble64le 0x65
127 
128 
129 #define BayEOS_WithoutOffset 0x20
130 #define BayEOS_ChannelNumber 0x40
131 #define BayEOS_ChannelLabel 0x60
132 
133 #define BayEOS_DATATYP_MASK 0x0f
134 #define BayEOS_OFFSETTYP_MASK 0xf0
135 
142 #define BayEOS_SetCannelAddress 0x1
143 #define BayEOS_GetCannelAddress 0x2
144 #define BayEOS_SetAutoSearch 0x3
145 #define BayEOS_GetAutoSearch 0x4
146 #define BayEOS_SetPin 0x5
147 #define BayEOS_GetPin 0x6
148 #define BayEOS_GetTime 0x7
149 #define BayEOS_SetTime 0x8
150 #define BayEOS_GetName 0x9
151 #define BayEOS_SetName 0xa
152 #define BayEOS_StartData 0xb
153 #define BayEOS_StopData 0xc
156 #define BayEOS_GetVersion 0xd
157 #define BayEOS_GetSamplingInt 0xe
158 #define BayEOS_SetSamplingInt 0xf
159 #define BayEOS_TimeOfNextFrame 0x10
160 #define BayEOS_StartLiveData 0x11
161 #define BayEOS_ModeStop 0x12 /* renamed from StopLiveData - Should be used to stop DUMP/LIVE/SEND-Mode */
162 #define BayEOS_Seek 0x13
163 #define BayEOS_StartBinaryDump 0x14
175 #define BayEOS_BufferCommand 0x15
176 #define BayEOS_GetBatStatus 0x16 /* [0x2][0x16] -> returns [0x3][0x16][uint16_t mV][uint16_t warning limit] */
177 #define BayEOS_BufferInfo 0x17 /*(v1.5) [0x2][0x17] -> returns [0x3][0x17][uint32_t read][uint32_t write][uint32_t end][uint32_t length][int8_t framesize][uint16_t sampling_int] */
178 #define BayEOS_GetLoggingDisabled 0x18 /*(v1.6) [0x2][0x18] -> returns [0x3][0x18][uint8_t _logging_disabled]*/
179 #define BayEOS_SetLoggingDisabled 0x19 /*(v1.6) [0x2][0x19][uint8_t 0/1] -> returns [0x3][0x19][uint8_t _logging_disabled]*/
180 #define BayEOS_GetChannelMap 0x1a /*(v1.6) [0x2][0x1a] -> returns [0x3][0x1a][char ch1;ch2;ch3;ch4....]*/
181 #define BayEOS_GetUnitMap 0x1b /*(v1.6) [0x2][0x1b] -> returns [0x3][0x1b][char unit1;unit2;....]*/
182 
183 #ifndef BayEOS_MAX_PAYLOAD
184 #define BayEOS_MAX_PAYLOAD 100
185 #endif
186 
187 
188 
189 #include <BayEOSBuffer.h>
190 
191 
192 class BayEOS {
193 public:
197  BayEOS(void) {
198  _buffer=NULL;
199  }
204  virtual uint8_t sendPayload(void)=0;
205 
210  virtual uint8_t readIntoPayload(int timeout=5000){return 0;};
211 
216  virtual int available(void){ return 0;};
217 
221  uint8_t sendError(const char* s);
222 
226  uint8_t sendMessage(const char* s);
227 
231  uint8_t createMessage(const char* s, uint8_t checksum=0, uint8_t frametype=BayEOS_Message);
232 
236  uint8_t createActionResponse(uint8_t key, uint8_t status);
237 
241  void startFrame(void);
242 
246  void startFrame(uint8_t type);
247 
251  void startOriginFrame(const char* o, uint8_t routed=0);
252 
256  void startDataFrame(uint8_t subtype=BayEOS_Float32le,uint8_t checksum=0);
257 
263  void startDataFrameWithOrigin(uint8_t subtype,const char* o,uint8_t checksum=0,uint8_t routed=0);
264 
274  uint8_t addChannelValue(float v, uint8_t channel_number=0);
275  uint8_t addChannelValue(double v, uint8_t channel_number=0);
276  uint8_t addChannelValue(long v, uint8_t channel_number=0);
277  uint8_t addChannelValue(unsigned long v, uint8_t channel_number=0);
278  uint8_t addChannelValue(int v, uint8_t channel_number=0);
279  uint8_t addChannelValue(unsigned int v, uint8_t channel_number=0);
280  uint8_t addChannelValue(int8_t v, uint8_t channel_number=0);
281  uint8_t addChannelValue(uint8_t v, uint8_t channel_number=0);
282 
291  uint8_t addChannelValue(float v, const char* channel_label);
292  uint8_t addChannelValue(double v, const char* channel_label);
293  uint8_t addChannelValue(long v, const char* channel_label);
294  uint8_t addChannelValue(unsigned long v, const char* channel_label);
295  uint8_t addChannelValue(int v, const char* channel_label);
296  uint8_t addChannelValue(unsigned int v, const char* channel_label);
297  uint8_t addChannelValue(int8_t v, const char* channel_label);
298  uint8_t addChannelValue(uint8_t v, const char* channel_label);
299 
300  /*
301  * Add Checksum at end of frame
302  */
303  uint8_t addChecksum(void);
304 
305  /*
306  * tries to validate checksum of payload
307  * returns
308  * 0 on success
309  * 1 on checksum error
310  * 2 on no checksum
311  */
312  uint8_t validateChecksum(void);
313 
317  void startRoutedFrame(uint16_t sourceMyID,uint16_t sourcePANID,uint8_t rssi=0);
318 
322  void startDelayedFrame(unsigned long delay);
323 
327  void startDelayedSecondFrame(unsigned long delay);
331  void startRF24Frame(uint8_t pipe);
332 
336  void startTimestampFrame(unsigned long timestamp);
340  void startCommand(uint8_t cmd_api);
341 
345  void startCommandResponse(uint8_t cmd_api);
346 
352  uint8_t addToPayload(uint8_t b);
353 
357  uint8_t addToPayload(const void* p,uint8_t length);
358 
362  uint8_t addToPayload(const uint8_t* c);
363 
367  uint8_t addToPayload(const char* s);
368 
372  uint8_t addToPayload(float f);
373 
377  uint8_t addToPayload(unsigned long l);
378 
382  uint8_t addToPayload(long l);
383 
387  uint8_t addToPayload(int w);
388 
392  uint8_t addToPayload(uint16_t w);
393 
394 
398  uint8_t getPayloadBytesLeft(void) const;
399 
403  uint8_t getPayloadLength(void) const;
404 
408  uint8_t getPacketLength(void) const;
409 
413  const uint8_t *getPayload(void) const;
414 
415  /*
416  * get a byte from payload
417  */
418  uint8_t getPayload(uint8_t index) const;
419 
420 
424  uint8_t sendOrBuffer(void);
425 
429  uint8_t sendFromBuffer(void);
430 
434  uint8_t writeToBuffer(void);
435 
439  uint8_t readFromBuffer(void);
440 
444  uint8_t readBinaryFromBuffer(unsigned long pos);
445 
449  uint8_t readBinaryFromBuffer(unsigned long pos,unsigned long end, unsigned long vpos);
450 
456  void setBuffer(BayEOSBuffer &buffer, uint16_t max_skip=0) {
457  _buffer = &buffer;
458  _max_skip=max_skip;
459  _skip_counter=0;
460  _failure_counter=0;
461  }
462 
463  uint8_t _payload[BayEOS_MAX_PAYLOAD];
464  uint8_t _next;
465  uint8_t _success;
466  uint8_t _failure_counter; //counts send failures
467  uint16_t _max_skip; //maximum number of packets to write to buffer before next try
468  uint16_t _skip_counter; //skip counter
469  BayEOSBuffer* _buffer;
470 };
471 
472 
473 inline uint8_t BayEOS::addToPayload(uint8_t b){
474  if(_next<BayEOS_MAX_PAYLOAD){
475  _payload[_next]=b;
476  _next++;
477  return 1;
478  }
479  return 0;
480 
481 }
482 #endif
483 
Definition: BayEOSBuffer.h:70
Definition: BayEOS.h:188
void startCommandResponse(uint8_t cmd_api)
Definition: BayEOS.cpp:241
void startDelayedFrame(unsigned long delay)
Definition: BayEOS.cpp:216
void startDelayedSecondFrame(unsigned long delay)
Definition: BayEOS.cpp:221
void startCommand(uint8_t cmd_api)
Definition: BayEOS.cpp:236
uint8_t readBinaryFromBuffer(unsigned long pos)
Definition: BayEOS.cpp:330
virtual uint8_t sendPayload(void)=0
uint8_t sendError(const char *s)
Definition: BayEOS.cpp:289
uint8_t getPacketLength(void) const
Definition: BayEOS.cpp:431
void startDataFrameWithOrigin(uint8_t subtype, const char *o, uint8_t checksum=0, uint8_t routed=0)
Definition: BayEOS.cpp:27
void setBuffer(BayEOSBuffer &buffer, uint16_t max_skip=0)
Definition: BayEOS.h:452
uint8_t createActionResponse(uint8_t key, uint8_t status)
Definition: BayEOS.cpp:313
BayEOS(void)
Definition: BayEOS.h:193
uint8_t addChannelValue(float v, uint8_t channel_number=0)
Definition: BayEOS.cpp:61
uint8_t writeToBuffer(void)
Definition: BayEOS.cpp:322
uint8_t sendMessage(const char *s)
Definition: BayEOS.cpp:295
void startRoutedFrame(uint16_t sourceMyID, uint16_t sourcePANID, uint8_t rssi=0)
Definition: BayEOS.cpp:204
uint8_t sendFromBuffer(void)
Definition: BayEOS.cpp:394
void startFrame(void)
Definition: BayEOS.cpp:8
void startTimestampFrame(unsigned long timestamp)
Definition: BayEOS.cpp:226
void startRF24Frame(uint8_t pipe)
Definition: BayEOS.cpp:231
void startOriginFrame(const char *o, uint8_t routed=0)
Definition: BayEOS.cpp:20
uint8_t createMessage(const char *s, uint8_t checksum=0, uint8_t frametype=BayEOS_Message)
Definition: BayEOS.cpp:301
uint8_t addToPayload(uint8_t b)
Definition: BayEOS.h:469
void startDataFrame(uint8_t subtype=BayEOS_Float32le, uint8_t checksum=0)
Definition: BayEOS.cpp:12
const uint8_t * getPayload(void) const
Definition: BayEOS.cpp:435
uint8_t sendOrBuffer(void)
Definition: BayEOS.cpp:369
virtual uint8_t readIntoPayload(int timeout=5000)
Definition: BayEOS.h:206
uint8_t getPayloadLength(void) const
Definition: BayEOS.cpp:427
uint8_t getPayloadBytesLeft(void) const
Definition: BayEOS.cpp:423
virtual int available(void)
Definition: BayEOS.h:212
uint8_t readFromBuffer(void)
Definition: BayEOS.cpp:345