tcMenu
RemoteConnector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
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 BINARY_GZ_PROTOCOL 0x02
25 #define START_OF_MESSAGE 0x01
26 #define TICK_INTERVAL 1
27 
28 // This sets the DEFAULT heartbeat interval when no other interval has been set on a connection. IE if you create a
29 // connection and do not call setHeartbeatInterval(millis), this is what you'll get.
30 #ifndef HEARTBEAT_INTERVAL
31 # define HEARTBEAT_INTERVAL 1500
32 #endif
33 
34 // Pairing is different to a normal connection, in that we just need to provide the user with reasonable time to
35 // accept the pairing request on a device, usually around 15 seconds is enough. You can redefine if needed.
36 #ifndef PAIRING_TIMEOUT_TICKS
37 #define PAIRING_TIMEOUT_TICKS (15000 / TICK_INTERVAL)
38 #endif
39 
54 enum FieldValueType : uint8_t {
77 };
78 
90 struct FieldAndValue {
91  FieldValueType fieldType;
92  uint16_t msgType;
93  uint16_t field;
94  char value[MAX_VALUE_LEN];
95  uint8_t len;
96 };
97 
102 #define COMMSERR_OK 0
103 #define COMMSERR_WRITE_NOT_CONNECTED 1
104 #define COMMSERR_PROTOCOL_ERROR 2
105 #define COMMSERR_CONNECTED 3
106 #define COMMSERR_DISCONNECTED 4
107 
109  uint16_t remoteNo: 4;
110  uint16_t connected: 1;
111  uint16_t errorMode: 8;
112 };
113 
119 
120 // forward references.
123 
135 };
136 
142 protected:
143  FieldAndValue currentField;
144  TagValueTransportType transportType;
145  uint8_t protocolUsed;
146 public:
148  virtual ~TagValueTransport() = default;
149 
150  virtual void startMsg(uint16_t msgType);
151  void startBinMsg(uint16_t msgType, uint16_t byteLen);
152  void writeField(uint16_t field, const char* value);
153  void writeFieldInt(uint16_t field, int value);
154  void writeFieldLong(uint16_t field, long value);
155  FieldAndValue* fieldIfAvailable();
156  void clearFieldStatus(FieldValueType ty = FVAL_PROCESSING);
157  TagValueTransportType getTransportType() { return transportType; }
158 
159  virtual void flush() = 0;
160  virtual int writeChar(char data) = 0;
161  virtual int writeStr(const char* data) = 0;
162  virtual uint8_t readByte()=0;
163  virtual bool readAvailable()=0;
164 
165  virtual bool available() = 0;
166  virtual bool connected() = 0;
167  virtual void close() = 0;
168  virtual void endMsg();
169 private:
170  bool findNextMessageStart();
171  bool processMsgKey();
172  bool processValuePart();
173 };
174 
175 #define FLAG_CURRENTLY_CONNECTED 0
176 #define FLAG_BOOTSTRAP_MODE 1
177 #define FLAG_BOOTSTRAP_COMPLETE 2
178 #define FLAG_AUTHENTICATED 3
179 #define FLAG_PAIRING_MODE 4
180 #define FLAG_FULLY_JOINED_RX 5
181 #define FLAG_FULLY_JOINED_TX 6
182 
188 private:
189  const ConnectorLocalInfo* localInfoPgm;
190  uint16_t ticksLastSend;
191  uint16_t ticksLastRead;
192  uint16_t hbTimeoutTicks;
193  CombinedMessageProcessor* processor;
194  TagValueTransport* transport;
195  CommsCallbackFn commsCallback;
196  AuthenticationManager* authManager;
197 
198  // used by bootstrapping and writing out messages
199  MenuItemIterator iterator;
200  MenuItemTypePredicate bootPredicate;
201  RemoteNoMenuItemPredicate remotePredicate;
202 
203  // the remote connection details take 16 bytes
204  char remoteName[16];
205  uint8_t remoteMajorVer, remoteMinorVer;
206  ApiPlatform remotePlatform;
207  uint8_t flags;
208  uint8_t remoteNo;
209 public:
214  explicit TagValueRemoteConnector(uint8_t remoteNo = 0);
215 
224  void initialise(TagValueTransport* transport, CombinedMessageProcessor* processor, const ConnectorLocalInfo* localInfoPgm, uint8_t remoteNo);
225 
226  void setAuthManager(AuthenticationManager* mgr) { authManager = mgr; }
227 
232  void setCommsNotificationCallback(CommsCallbackFn callback) { this->commsCallback = callback; }
233 
237  bool isTransportAvailable() { return transport->available(); }
238 
239  void provideAuthentication(const char* auth);
240 
244  bool isTransportConnected() { return transport->connected(); }
245 
250  void encodeJoin();
251 
271  void encodeCustomTagValMessage(uint16_t msgType, void (*msgWriter)(TagValueTransport*, void*), void*);
272 
289  void encodeCustomBinaryMessage(uint16_t msgType, uint16_t len, void (*msgWriter)(TagValueTransport*, void*, size_t), void* data = nullptr);
290 
299  void encodeDialogMsg(uint8_t mode, uint8_t btn1, uint8_t btn2, const char* hdrPgm, const char* buffer);
300 
305  void encodeBootstrap(bool isComplete);
306 
311  void encodeHeartbeat(HeartbeatMode restartConnection);
312 
319  void encodeAnalogItem(int parentId, AnalogMenuItem* item);
320 
327  void encodeSubMenu(int parentId, SubMenuItem* item);
328 
335  void encodeBooleanMenu(int parentId, BooleanMenuItem* item);
336 
343  void encodeEnumMenu(int parentId, EnumMenuItem* item);
344 
351  void encodeActionMenu(int parentId, ActionMenuItem* item);
352 
359  void encodeFloatMenu(int parentId, FloatMenuItem* item);
360 
367  void encodeRuntimeMenuItem(int parentId, RuntimeMenuItem* item);
368 
375  void encodeMultiEditMenu(int parentId, RuntimeMenuItem* item);
376 
382  void encodeLargeNumberMenuItem(int parentId, EditableLargeNumberMenuItem* item);
383 
390  void encodeChangeValue(MenuItem* theItem);
391 
398  void encodeAcknowledgement(uint32_t correlation, AckResponseStatus status);
399 
405  void encodeColorMenuItem(int id, Rgb32MenuItem *pItem);
406 
412  void encodeScrollMenuItem(int id, ScrollChoiceMenuItem *pItem);
413 
418  void tick();
419 
429  void pairingRequest(const char* name, const char* uuid);
430 
434  void initiateBootstrap();
435 
439  uint8_t getRemoteNo() const {return remoteNo;}
440 
444  const char* getRemoteName() {return remoteName;}
445 
449  uint8_t getRemoteMajorVer() const {return remoteMajorVer;}
450 
454  uint8_t getRemoteMinorVer() const {return remoteMinorVer;}
455 
459  ApiPlatform getRemotePlatform() {return remotePlatform;}
460 
465  bool isConnected() { return bitRead(flags, FLAG_CURRENTLY_CONNECTED); }
466 
470  void setRemoteName(const char* name);
471 
475  void setRemoteConnected(uint8_t major, uint8_t minor, ApiPlatform platform);
476 
482  void commsNotify(uint16_t commsEventType);
483 
485  void close();
486 
488  bool isAuthenticated() { return bitRead(flags, FLAG_AUTHENTICATED); }
489  AuthenticationManager* getAuthManager() { return authManager; }
490 
491  void setHeartbeatTimeout(uint16_t milli) { hbTimeoutTicks = milli / TICK_INTERVAL; }
492 private:
493  void encodeBaseMenuFields(int parentId, MenuItem* item);
494  bool prepareWriteMsg(uint16_t msgType);
495  void nextBootstrap();
496  void performAnyWrites();
497  void dealWithHeartbeating();
504  void setConnected(bool conn);
505 
506  bool isBootstrapMode() { return bitRead(flags, FLAG_BOOTSTRAP_MODE); }
507  void setBootstrapMode(bool mode) { bitWrite(flags, FLAG_BOOTSTRAP_MODE, mode); }
508 
509  bool isBootstrapComplete() { return bitRead(flags, FLAG_BOOTSTRAP_COMPLETE); }
510  void setBootstrapComplete(bool mode) { bitWrite(flags, FLAG_BOOTSTRAP_COMPLETE, mode); }
511 
512  void setAuthenticated(bool auth) { bitWrite(flags, FLAG_AUTHENTICATED, auth); }
513 
514  bool isPairing() { return bitRead(flags, FLAG_PAIRING_MODE); }
515  void setPairing(bool pair) { bitWrite(flags, FLAG_PAIRING_MODE, pair); }
516 
517  bool isConnectionFullyEstablished() {
518  return isConnected() && (bitRead(flags, FLAG_FULLY_JOINED_RX) != 0) && (bitRead(flags, FLAG_FULLY_JOINED_TX) != 0);
519  }
520 
521  void setFullyJoinedRx(bool joinRx) { bitWrite(flags, FLAG_FULLY_JOINED_RX, joinRx); }
522 
523  void setFullyJoinedTx(bool joinTx) { bitWrite(flags, FLAG_FULLY_JOINED_TX, joinTx); }
524 
525  bool beginEncodeChange(MenuItem *item);
526 };
527 
528 #endif /* _TCMENU_REMOTECONNECTOR_H_ */
In TcMenu, MenuItem storage is shared between program memory and RAM. Usually each MenuItem has assoc...
Provides a number of utility functions for the processing of menu item structures.
This file contains the default processors that can deal with incoming messages turning them into even...
void(* CommsCallbackFn)(CommunicationInfo)
Definition: RemoteConnector.h:118
FieldValueType
Definition: RemoteConnector.h:54
@ FVAL_END_MSG
Definition: RemoteConnector.h:58
@ FVAL_PROCESSING_MSGTYPE_HI
Definition: RemoteConnector.h:74
@ FVAL_NEW_MSG
Definition: RemoteConnector.h:56
@ FVAL_PROCESSING_VALUE
Definition: RemoteConnector.h:68
@ FVAL_ERROR_PROTO
Definition: RemoteConnector.h:62
@ FVAL_PROCESSING_PROTOCOL
Definition: RemoteConnector.h:72
@ FVAL_PROCESSING_MSGTYPE_LO
Definition: RemoteConnector.h:76
@ FVAL_FIELD
Definition: RemoteConnector.h:60
@ FVAL_PROCESSING_WAITEQ
Definition: RemoteConnector.h:66
@ FVAL_PROCESSING_AWAITINGMSG
Definition: RemoteConnector.h:70
@ FVAL_PROCESSING
Definition: RemoteConnector.h:64
TagValueTransportType
Definition: RemoteConnector.h:128
@ TVAL_BUFFERED
Definition: RemoteConnector.h:132
@ TVAL_UNBUFFERED
Definition: RemoteConnector.h:130
@ TVAL_BUFFERED_DELEGATE_ENCRYPT
Definition: RemoteConnector.h:134
Definition: RemoteConnector.h:108
Definition: RemoteConnector.h:90
contains the definitions of each message and field.
HeartbeatMode
Definition: RemoteTypes.h:149
ApiPlatform
Definition: RemoteTypes.h:139
Contains definitions of menu items that can be fully defined at runtime with no need for prog mem str...
contains the menu item definition for scrolling choice types, and also for RGB items
Definition: MenuItems.h:672
Definition: MenuItems.h:476
Definition: RemoteAuthentication.h:38
Definition: MenuItems.h:597
Definition: MessageProcessors.h:146
Definition: EditableLargeNumberMenuItem.h:182
Definition: MenuItems.h:566
Definition: MenuItems.h:625
Definition: MenuItems.h:329
Definition: MenuIterator.h:173
Definition: MenuIterator.h:145
Definition: MenuIterator.h:103
Definition: ScrollChoiceMenuItem.h:229
Definition: RuntimeMenuItem.h:75
Definition: ScrollChoiceMenuItem.h:34
Definition: RuntimeMenuItem.h:149
Definition: RemoteConnector.h:187
bool isConnected()
Definition: RemoteConnector.h:465
const char * getRemoteName()
Definition: RemoteConnector.h:444
bool isTransportAvailable()
Definition: RemoteConnector.h:237
void encodeAnalogItem(int parentId, AnalogMenuItem *item)
Definition: RemoteConnector.cpp:433
void setRemoteName(const char *name)
Definition: RemoteConnector.cpp:68
void encodeRuntimeMenuItem(int parentId, RuntimeMenuItem *item)
Definition: RemoteConnector.cpp:537
uint8_t getRemoteMajorVer() const
Definition: RemoteConnector.h:449
void encodeSubMenu(int parentId, SubMenuItem *item)
Definition: RemoteConnector.cpp:587
void encodeMultiEditMenu(int parentId, RuntimeMenuItem *item)
Definition: RemoteConnector.cpp:483
void encodeEnumMenu(int parentId, EnumMenuItem *item)
Definition: RemoteConnector.cpp:552
void encodeBootstrap(bool isComplete)
Definition: RemoteConnector.cpp:380
void tick()
Definition: RemoteConnector.cpp:174
bool isTransportConnected()
Definition: RemoteConnector.h:244
void setRemoteConnected(uint8_t major, uint8_t minor, ApiPlatform platform)
Definition: RemoteConnector.cpp:73
void pairingRequest(const char *name, const char *uuid)
Definition: RemoteConnector.cpp:127
void encodeAcknowledgement(uint32_t correlation, AckResponseStatus status)
Definition: RemoteConnector.cpp:567
void encodeLargeNumberMenuItem(int parentId, EditableLargeNumberMenuItem *item)
Definition: RemoteConnector.cpp:472
void initialise(TagValueTransport *transport, CombinedMessageProcessor *processor, const ConnectorLocalInfo *localInfoPgm, uint8_t remoteNo)
Definition: RemoteConnector.cpp:56
uint8_t getRemoteMinorVer() const
Definition: RemoteConnector.h:454
bool isAuthenticated()
Definition: RemoteConnector.h:488
TagValueRemoteConnector(uint8_t remoteNo=0)
Definition: RemoteConnector.cpp:41
void encodeColorMenuItem(int id, Rgb32MenuItem *pItem)
Definition: RemoteConnector.cpp:462
void encodeCustomBinaryMessage(uint16_t msgType, uint16_t len, void(*msgWriter)(TagValueTransport *, void *, size_t), void *data=nullptr)
Definition: RemoteConnector.cpp:407
void encodeBooleanMenu(int parentId, BooleanMenuItem *item)
Definition: RemoteConnector.cpp:579
void close()
Definition: RemoteConnector.cpp:160
void encodeChangeValue(MenuItem *theItem)
Definition: RemoteConnector.cpp:605
void encodeDialogMsg(uint8_t mode, uint8_t btn1, uint8_t btn2, const char *hdrPgm, const char *buffer)
Definition: RemoteConnector.cpp:340
void setCommsNotificationCallback(CommsCallbackFn callback)
Definition: RemoteConnector.h:232
void encodeActionMenu(int parentId, ActionMenuItem *item)
Definition: RemoteConnector.cpp:593
void encodeCustomTagValMessage(uint16_t msgType, void(*msgWriter)(TagValueTransport *, void *), void *)
Definition: RemoteConnector.cpp:358
void initiateBootstrap()
Definition: RemoteConnector.cpp:270
void commsNotify(uint16_t commsEventType)
Definition: RemoteConnector.cpp:150
void encodeScrollMenuItem(int id, ScrollChoiceMenuItem *pItem)
Definition: RemoteConnector.cpp:447
uint8_t getRemoteNo() const
Definition: RemoteConnector.h:439
void encodeFloatMenu(int parentId, FloatMenuItem *item)
Definition: RemoteConnector.cpp:517
void encodeHeartbeat(HeartbeatMode restartConnection)
Definition: RemoteConnector.cpp:386
void encodeJoin()
Definition: RemoteConnector.cpp:365
ApiPlatform getRemotePlatform()
Definition: RemoteConnector.h:459
Definition: RemoteConnector.h:141
A series of utilities that used throughout tcMenu.
Definition: tcUtil.h:25