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 #define HEARTBEAT_INTERVAL 1500
27 // when debugging you can increase the heartbeat time to reduce disconnects (below gives 90 seconds)
28 //#define HEARTBEAT_INTERVAL 30000
29 #define HEARTBEAT_INTERVAL_TICKS (HEARTBEAT_INTERVAL / TICK_INTERVAL)
30 #define PAIRING_TIMEOUT_TICKS (15000 / TICK_INTERVAL)
31 
44 enum FieldValueType : uint8_t {
67 };
68 
77 struct FieldAndValue {
78  FieldValueType fieldType;
79  uint16_t msgType;
80  uint16_t field;
81  char value[MAX_VALUE_LEN];
82  uint8_t len;
83 };
84 
89 #define COMMSERR_OK 0
90 #define COMMSERR_WRITE_NOT_CONNECTED 1
91 #define COMMSERR_PROTOCOL_ERROR 2
92 #define COMMSERR_CONNECTED 3
93 #define COMMSERR_DISCONNECTED 4
94 
96  uint16_t remoteNo: 4;
97  uint16_t connected: 1;
98  uint16_t errorMode: 8;
99 };
100 
106 
107 // forward references.
110 
116 protected:
117  FieldAndValue currentField;
118 public:
120  virtual ~TagValueTransport() {}
121 
122  void startMsg(uint16_t msgType);
123  void writeField(uint16_t field, const char* value);
124  void writeFieldInt(uint16_t field, int value);
125  void writeFieldLong(uint16_t field, long value);
126  FieldAndValue* fieldIfAvailable();
127  void clearFieldStatus(FieldValueType ty = FVAL_PROCESSING);
128 
129  virtual void flush() = 0;
130  virtual int writeChar(char data) = 0;
131  virtual int writeStr(const char* data) = 0;
132  virtual uint8_t readByte()=0;
133  virtual bool readAvailable()=0;
134 
135  virtual bool available() = 0;
136  virtual bool connected() = 0;
137  virtual void close() = 0;
138  virtual void endMsg();
139 
140 private:
141  bool findNextMessageStart();
142  bool processMsgKey();
143  bool processValuePart();
144 };
145 
146 #define FLAG_CURRENTLY_CONNECTED 0
147 #define FLAG_BOOTSTRAP_MODE 1
148 #define FLAG_BOOTSTRAP_COMPLETE 2
149 #define FLAG_AUTHENTICATED 3
150 #define FLAG_PAIRING_MODE 4
151 #define FLAG_FULLY_JOINED_RX 5
152 #define FLAG_FULLY_JOINED_TX 6
153 
159 private:
160  const ConnectorLocalInfo* localInfoPgm;
161  uint16_t ticksLastSend;
162  uint16_t ticksLastRead;
163  CombinedMessageProcessor* processor;
164  TagValueTransport* transport;
165  CommsCallbackFn commsCallback;
166  AuthenticationManager* authManager;
167 
168  // used by bootstrapping and writing out messages
169  MenuItemIterator iterator;
170  MenuItemTypePredicate bootPredicate;
171  RemoteNoMenuItemPredicate remotePredicate;
172 
173  // the remote connection details take 16 bytes
174  char remoteName[16];
175  uint8_t remoteMajorVer, remoteMinorVer;
176  ApiPlatform remotePlatform;
177  uint8_t flags;
178  uint8_t remoteNo;
179 public:
185  TagValueRemoteConnector(uint8_t remoteNo = 0);
186 
194  void initialise(TagValueTransport* transport, CombinedMessageProcessor* processor, const ConnectorLocalInfo* localInfoPgm);
195 
196  void setAuthManager(AuthenticationManager* mgr) { authManager = mgr; }
197 
202  void setCommsNotificationCallback(CommsCallbackFn callback) { this->commsCallback = callback; }
203 
207  bool isTransportAvailable() { return transport->available(); }
208 
209  void provideAuthentication(const char* auth);
210 
214  bool isTransportConnected() { return transport->connected(); }
215 
220  void encodeJoin();
221 
230  void encodeDialogMsg(uint8_t mode, uint8_t btn1, uint8_t btn2, const char* hdrPgm, const char* buffer);
231 
236  void encodeBootstrap(bool isComplete);
237 
242  void encodeHeartbeat(HeartbeatMode restartConnection);
243 
250  void encodeAnalogItem(int parentId, AnalogMenuItem* item);
251 
258  void encodeSubMenu(int parentId, SubMenuItem* item);
259 
266  void encodeBooleanMenu(int parentId, BooleanMenuItem* item);
267 
274  void encodeEnumMenu(int parentId, EnumMenuItem* item);
275 
282  void encodeActionMenu(int parentId, ActionMenuItem* item);
283 
290  void encodeFloatMenu(int parentId, FloatMenuItem* item);
291 
298  void encodeRuntimeMenuItem(int parentId, RuntimeMenuItem* item);
299 
306  void encodeMultiEditMenu(int parentId, RuntimeMenuItem* item);
307 
313  void encodeLargeNumberMenuItem(int parentId, EditableLargeNumberMenuItem* item);
314 
321  void encodeChangeValue(MenuItem* theItem);
322 
329  void encodeAcknowledgement(uint32_t correlation, AckResponseStatus status);
330 
336  void encodeColorMenuItem(int id, Rgb32MenuItem *pItem);
337 
343  void encodeScrollMenuItem(int id, ScrollChoiceMenuItem *pItem);
344 
349  void tick();
350 
360  void pairingRequest(const char* name, const char* uuid);
361 
365  void initiateBootstrap();
366 
370  uint8_t getRemoteNo() {return remoteNo;}
371 
375  const char* getRemoteName() {return remoteName;}
376 
380  uint8_t getRemoteMajorVer() {return remoteMajorVer;}
381 
385  uint8_t getRemoteMinorVer() {return remoteMinorVer;}
386 
390  ApiPlatform getRemotePlatform() {return remotePlatform;}
391 
396  bool isConnected() { return bitRead(flags, FLAG_CURRENTLY_CONNECTED); }
397 
401  void setRemoteName(const char* name);
402 
406  void setRemoteConnected(uint8_t major, uint8_t minor, ApiPlatform platform);
407 
413  void commsNotify(uint16_t commsEventType);
414 
416  void close();
417 
419  bool isAuthenticated() { return bitRead(flags, FLAG_AUTHENTICATED); }
420  AuthenticationManager* getAuthManager() { return authManager; }
421 private:
422  void encodeBaseMenuFields(int parentId, MenuItem* item);
423  bool prepareWriteMsg(uint16_t msgType);
424  void nextBootstrap();
425  void performAnyWrites();
426  void dealWithHeartbeating();
433  void setConnected(bool conn);
434 
435  bool isBootstrapMode() { return bitRead(flags, FLAG_BOOTSTRAP_MODE); }
436  void setBootstrapMode(bool mode) { bitWrite(flags, FLAG_BOOTSTRAP_MODE, mode); }
437 
438  bool isBootstrapComplete() { return bitRead(flags, FLAG_BOOTSTRAP_COMPLETE); }
439  void setBootstrapComplete(bool mode) { bitWrite(flags, FLAG_BOOTSTRAP_COMPLETE, mode); }
440 
441  void setAuthenticated(bool auth) { bitWrite(flags, FLAG_AUTHENTICATED, auth); }
442 
443  bool isPairing() { return bitRead(flags, FLAG_PAIRING_MODE); }
444  void setPairing(bool pair) { bitWrite(flags, FLAG_PAIRING_MODE, pair); }
445 
446  bool isConnectionFullyEstablished() {
447  return isConnected() && (bitRead(flags, FLAG_FULLY_JOINED_RX) != 0) && (bitRead(flags, FLAG_FULLY_JOINED_TX) != 0);
448  }
449 
450  void setFullyJoinedRx(bool joinRx) { bitWrite(flags, FLAG_FULLY_JOINED_RX, joinRx); }
451 
452  void setFullyJoinedTx(bool joinTx) { bitWrite(flags, FLAG_FULLY_JOINED_TX, joinTx); }
453 };
454 
455 #endif /* _TCMENU_REMOTECONNECTOR_H_ */
Definition: MenuItems.h:647
void(* CommsCallbackFn)(CommunicationInfo)
Definition: RemoteConnector.h:105
Definition: MenuItems.h:324
Definition: MenuIterator.h:169
Definition: RemoteConnector.h:66
uint8_t getRemoteMajorVer()
Definition: RemoteConnector.h:380
Definition: RemoteConnector.h:77
Definition: MenuItems.h:480
Definition: MenuIterator.h:141
Definition: RemoteConnector.h:54
Definition: RuntimeMenuItem.h:125
Definition: RemoteConnector.h:56
Definition: MenuItems.h:612
Definition: RemoteConnector.h:62
Definition: RemoteConnector.h:64
bool isTransportConnected()
Definition: RemoteConnector.h:214
FieldValueType
Definition: RemoteConnector.h:44
Definition: ScrollChoiceMenuItem.h:33
Definition: MessageProcessors.h:115
ApiPlatform getRemotePlatform()
Definition: RemoteConnector.h:390
Definition: MenuItems.h:553
ApiPlatform
Definition: RemoteTypes.h:138
HeartbeatMode
Definition: RemoteTypes.h:148
Definition: RemoteConnector.h:46
Definition: MenuIterator.h:105
Definition: RemoteConnector.h:50
Definition: MenuItems.h:584
void setCommsNotificationCallback(CommsCallbackFn callback)
Definition: RemoteConnector.h:202
bool isAuthenticated()
Definition: RemoteConnector.h:419
Definition: RemoteConnector.h:115
Definition: RemoteConnector.h:58
Definition: RemoteConnector.h:60
Definition: RemoteConnector.h:48
Definition: RuntimeMenuItem.h:73
uint8_t getRemoteMinorVer()
Definition: RemoteConnector.h:385
bool isTransportAvailable()
Definition: RemoteConnector.h:207
Definition: RemoteAuthentication.h:29
const char * getRemoteName()
Definition: RemoteConnector.h:375
bool isConnected()
Definition: RemoteConnector.h:396
Definition: RemoteConnector.h:158
Definition: tcUtil.h:45
Definition: RemoteConnector.h:95
Definition: RemoteConnector.h:52
uint8_t getRemoteNo()
Definition: RemoteConnector.h:370
Definition: ScrollChoiceMenuItem.h:176
Definition: EditableLargeNumberMenuItem.h:166