tcMenu
RemoteConnector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
11 #ifndef _TCMENU_REMOTECONNECTOR_H_
12 #define _TCMENU_REMOTECONNECTOR_H_
13 
14 #include <PlatformDetermination.h>
15 #include "RemoteTypes.h"
16 #include <tcUtil.h>
17 #include "MenuItems.h"
18 #include "RuntimeMenuItem.h"
19 #include "MessageProcessors.h"
20 #include "MenuIterator.h"
21 #include "ScrollChoiceMenuItem.h"
22 
23 #define TAG_VAL_PROTOCOL 0x01
24 #define START_OF_MESSAGE 0x01
25 #define TICK_INTERVAL 1
26 
27 // when debugging to reduce disconnects set the following (give 1 minute timeout): -DHEARTBEAT_INTERVAL=20000
28 #ifndef HEARTBEAT_INTERVAL
29 # define HEARTBEAT_INTERVAL 1500
30 #endif
31 
32 #define HEARTBEAT_INTERVAL_TICKS (HEARTBEAT_INTERVAL / TICK_INTERVAL)
33 #define PAIRING_TIMEOUT_TICKS (15000 / TICK_INTERVAL)
34 
47 enum FieldValueType : uint8_t {
70 };
71 
80 struct FieldAndValue {
81  FieldValueType fieldType;
82  uint16_t msgType;
83  uint16_t field;
84  char value[MAX_VALUE_LEN];
85  uint8_t len;
86 };
87 
92 #define COMMSERR_OK 0
93 #define COMMSERR_WRITE_NOT_CONNECTED 1
94 #define COMMSERR_PROTOCOL_ERROR 2
95 #define COMMSERR_CONNECTED 3
96 #define COMMSERR_DISCONNECTED 4
97 
99  uint16_t remoteNo: 4;
100  uint16_t connected: 1;
101  uint16_t errorMode: 8;
102 };
103 
109 
110 // forward references.
113 
125 };
126 
132 protected:
133  FieldAndValue currentField;
134  TagValueTransportType transportType;
135  uint8_t protocolUsed;
136 public:
138  virtual ~TagValueTransport() = default;
139 
140  void startMsg(uint16_t msgType);
141  void writeField(uint16_t field, const char* value);
142  void writeFieldInt(uint16_t field, int value);
143  void writeFieldLong(uint16_t field, long value);
144  FieldAndValue* fieldIfAvailable();
145  void clearFieldStatus(FieldValueType ty = FVAL_PROCESSING);
146  TagValueTransportType getTransportType() { return transportType; }
147 
148  virtual void flush() = 0;
149  virtual int writeChar(char data) = 0;
150  virtual int writeStr(const char* data) = 0;
151  virtual uint8_t readByte()=0;
152  virtual bool readAvailable()=0;
153 
154  virtual bool available() = 0;
155  virtual bool connected() = 0;
156  virtual void close() = 0;
157  virtual void endMsg();
158 
159 private:
160  bool findNextMessageStart();
161  bool processMsgKey();
162  bool processValuePart();
163 };
164 
165 #define FLAG_CURRENTLY_CONNECTED 0
166 #define FLAG_BOOTSTRAP_MODE 1
167 #define FLAG_BOOTSTRAP_COMPLETE 2
168 #define FLAG_AUTHENTICATED 3
169 #define FLAG_PAIRING_MODE 4
170 #define FLAG_FULLY_JOINED_RX 5
171 #define FLAG_FULLY_JOINED_TX 6
172 
178 private:
179  const ConnectorLocalInfo* localInfoPgm;
180  uint16_t ticksLastSend;
181  uint16_t ticksLastRead;
182  CombinedMessageProcessor* processor;
183  TagValueTransport* transport;
184  CommsCallbackFn commsCallback;
185  AuthenticationManager* authManager;
186 
187  // used by bootstrapping and writing out messages
188  MenuItemIterator iterator;
189  MenuItemTypePredicate bootPredicate;
190  RemoteNoMenuItemPredicate remotePredicate;
191 
192  // the remote connection details take 16 bytes
193  char remoteName[16];
194  uint8_t remoteMajorVer, remoteMinorVer;
195  ApiPlatform remotePlatform;
196  uint8_t flags;
197  uint8_t remoteNo;
198 public:
204  explicit TagValueRemoteConnector(uint8_t remoteNo = 0);
205 
214  void initialise(TagValueTransport* transport, CombinedMessageProcessor* processor, const ConnectorLocalInfo* localInfoPgm, uint8_t remoteNo);
215 
216  void setAuthManager(AuthenticationManager* mgr) { authManager = mgr; }
217 
222  void setCommsNotificationCallback(CommsCallbackFn callback) { this->commsCallback = callback; }
223 
227  bool isTransportAvailable() { return transport->available(); }
228 
229  void provideAuthentication(const char* auth);
230 
234  bool isTransportConnected() { return transport->connected(); }
235 
240  void encodeJoin();
241 
250  void encodeDialogMsg(uint8_t mode, uint8_t btn1, uint8_t btn2, const char* hdrPgm, const char* buffer);
251 
256  void encodeBootstrap(bool isComplete);
257 
262  void encodeHeartbeat(HeartbeatMode restartConnection);
263 
270  void encodeAnalogItem(int parentId, AnalogMenuItem* item);
271 
278  void encodeSubMenu(int parentId, SubMenuItem* item);
279 
286  void encodeBooleanMenu(int parentId, BooleanMenuItem* item);
287 
294  void encodeEnumMenu(int parentId, EnumMenuItem* item);
295 
302  void encodeActionMenu(int parentId, ActionMenuItem* item);
303 
310  void encodeFloatMenu(int parentId, FloatMenuItem* item);
311 
318  void encodeRuntimeMenuItem(int parentId, RuntimeMenuItem* item);
319 
326  void encodeMultiEditMenu(int parentId, RuntimeMenuItem* item);
327 
333  void encodeLargeNumberMenuItem(int parentId, EditableLargeNumberMenuItem* item);
334 
341  void encodeChangeValue(MenuItem* theItem);
342 
349  void encodeAcknowledgement(uint32_t correlation, AckResponseStatus status);
350 
356  void encodeColorMenuItem(int id, Rgb32MenuItem *pItem);
357 
363  void encodeScrollMenuItem(int id, ScrollChoiceMenuItem *pItem);
364 
369  void tick();
370 
380  void pairingRequest(const char* name, const char* uuid);
381 
385  void initiateBootstrap();
386 
390  uint8_t getRemoteNo() const {return remoteNo;}
391 
395  const char* getRemoteName() {return remoteName;}
396 
400  uint8_t getRemoteMajorVer() const {return remoteMajorVer;}
401 
405  uint8_t getRemoteMinorVer() const {return remoteMinorVer;}
406 
410  ApiPlatform getRemotePlatform() {return remotePlatform;}
411 
416  bool isConnected() { return bitRead(flags, FLAG_CURRENTLY_CONNECTED); }
417 
421  void setRemoteName(const char* name);
422 
426  void setRemoteConnected(uint8_t major, uint8_t minor, ApiPlatform platform);
427 
433  void commsNotify(uint16_t commsEventType);
434 
436  void close();
437 
439  bool isAuthenticated() { return bitRead(flags, FLAG_AUTHENTICATED); }
440  AuthenticationManager* getAuthManager() { return authManager; }
441 private:
442  void encodeBaseMenuFields(int parentId, MenuItem* item);
443  bool prepareWriteMsg(uint16_t msgType);
444  void nextBootstrap();
445  void performAnyWrites();
446  void dealWithHeartbeating();
453  void setConnected(bool conn);
454 
455  bool isBootstrapMode() { return bitRead(flags, FLAG_BOOTSTRAP_MODE); }
456  void setBootstrapMode(bool mode) { bitWrite(flags, FLAG_BOOTSTRAP_MODE, mode); }
457 
458  bool isBootstrapComplete() { return bitRead(flags, FLAG_BOOTSTRAP_COMPLETE); }
459  void setBootstrapComplete(bool mode) { bitWrite(flags, FLAG_BOOTSTRAP_COMPLETE, mode); }
460 
461  void setAuthenticated(bool auth) { bitWrite(flags, FLAG_AUTHENTICATED, auth); }
462 
463  bool isPairing() { return bitRead(flags, FLAG_PAIRING_MODE); }
464  void setPairing(bool pair) { bitWrite(flags, FLAG_PAIRING_MODE, pair); }
465 
466  bool isConnectionFullyEstablished() {
467  return isConnected() && (bitRead(flags, FLAG_FULLY_JOINED_RX) != 0) && (bitRead(flags, FLAG_FULLY_JOINED_TX) != 0);
468  }
469 
470  void setFullyJoinedRx(bool joinRx) { bitWrite(flags, FLAG_FULLY_JOINED_RX, joinRx); }
471 
472  void setFullyJoinedTx(bool joinTx) { bitWrite(flags, FLAG_FULLY_JOINED_TX, joinTx); }
473 };
474 
475 #endif /* _TCMENU_REMOTECONNECTOR_H_ */
Definition: MenuItems.h:652
void(* CommsCallbackFn)(CommunicationInfo)
Definition: RemoteConnector.h:108
Definition: RemoteConnector.h:124
Definition: MenuItems.h:329
Definition: MenuIterator.h:175
Definition: RemoteConnector.h:69
uint8_t getRemoteMajorVer() const
Definition: RemoteConnector.h:400
Definition: RemoteConnector.h:80
Definition: MenuItems.h:485
Definition: MenuIterator.h:147
Definition: RemoteConnector.h:57
Definition: RuntimeMenuItem.h:125
Definition: RemoteConnector.h:59
Definition: MenuItems.h:617
Definition: RemoteConnector.h:122
Definition: RemoteConnector.h:65
Definition: RemoteConnector.h:67
bool isTransportConnected()
Definition: RemoteConnector.h:234
uint8_t getRemoteMinorVer() const
Definition: RemoteConnector.h:405
FieldValueType
Definition: RemoteConnector.h:47
Definition: ScrollChoiceMenuItem.h:33
Definition: MessageProcessors.h:115
ApiPlatform getRemotePlatform()
Definition: RemoteConnector.h:410
Definition: MenuItems.h:558
ApiPlatform
Definition: RemoteTypes.h:134
HeartbeatMode
Definition: RemoteTypes.h:144
Definition: RemoteConnector.h:49
Definition: MenuIterator.h:105
Definition: RemoteConnector.h:53
Definition: MenuItems.h:589
uint8_t getRemoteNo() const
Definition: RemoteConnector.h:390
void setCommsNotificationCallback(CommsCallbackFn callback)
Definition: RemoteConnector.h:222
bool isAuthenticated()
Definition: RemoteConnector.h:439
Definition: RemoteConnector.h:131
Definition: RemoteConnector.h:61
Definition: RemoteConnector.h:63
Definition: RemoteConnector.h:51
Definition: RuntimeMenuItem.h:73
bool isTransportAvailable()
Definition: RemoteConnector.h:227
Definition: RemoteAuthentication.h:33
Definition: RemoteConnector.h:120
const char * getRemoteName()
Definition: RemoteConnector.h:395
bool isConnected()
Definition: RemoteConnector.h:416
Definition: RemoteConnector.h:177
Definition: tcUtil.h:45
Definition: RemoteConnector.h:98
TagValueTransportType
Definition: RemoteConnector.h:118
Definition: RemoteConnector.h:55
Definition: ScrollChoiceMenuItem.h:176
Definition: EditableLargeNumberMenuItem.h:166