|
| 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 | + |
| 6 | +/** |
| 7 | + * @file SerialTransport.h |
| 8 | + * |
| 9 | + * Serial remote capability plugin. This file is a plugin file and should not be directly edited, |
| 10 | + * it will be replaced each time the project is built. If you want to edit this file in place, |
| 11 | + * make sure to rename it first. |
| 12 | + */ |
| 13 | + |
| 14 | +#ifndef _TCMENU_SERIALTRANSPORT_H_ |
| 15 | +#define _TCMENU_SERIALTRANSPORT_H_ |
| 16 | + |
| 17 | +#include <Arduino.h> |
| 18 | +#include <RemoteConnector.h> |
| 19 | +#include <MessageProcessors.h> |
| 20 | +#include <tcUtil.h> |
| 21 | +#include <RemoteAuthentication.h> |
| 22 | + |
| 23 | +/** |
| 24 | + * Serial transport is an implementation of TagValueTransport that works over a serial port |
| 25 | + */ |
| 26 | +class SerialTagValueTransport : public TagValueTransport { |
| 27 | +private: |
| 28 | + HardwareSerial* serialPort; |
| 29 | +public: |
| 30 | + SerialTagValueTransport(); |
| 31 | + virtual ~SerialTagValueTransport() {} |
| 32 | + void setStream(HardwareSerial* stream) {this->serialPort = stream; } |
| 33 | + |
| 34 | + void flush() override {serialPort->flush();} |
| 35 | + int writeChar(char data) override; |
| 36 | + int writeStr(const char* data) override; |
| 37 | + |
| 38 | + uint8_t readByte() override { return serialPort->read(); } |
| 39 | + bool readAvailable() override { return serialPort->available(); } |
| 40 | + bool available() override { return serialPort->availableForWrite() != 0;} |
| 41 | + bool connected() override { return true;} |
| 42 | + |
| 43 | + void close() override; |
| 44 | + |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * SerialTagValServer is the implementation of remote communication that provides |
| 49 | + * remote menu capability for Serial streams. |
| 50 | + */ |
| 51 | +class SerialTagValServer { |
| 52 | +private: |
| 53 | + SerialTagValueTransport serPort; |
| 54 | + TagValueRemoteConnector connector; |
| 55 | + CombinedMessageProcessor messageProcessor; |
| 56 | +public: |
| 57 | + /** Empty constructor - configured in begin */ |
| 58 | + SerialTagValServer(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets the mode of authentication used with your remote, if you don't call this the system will |
| 62 | + * default to no authentication; which is probably fine for serial / bluetooth serial. |
| 63 | + * |
| 64 | + * This should always be called before begin(), to ensure this in your code always ensure this |
| 65 | + * is called BEFORE setupMenu(). |
| 66 | + * |
| 67 | + * @param authManager a reference to an authentication manager. |
| 68 | + */ |
| 69 | + void setAuthenticator(AuthenticationManager* authManager) { connector.setAuthManager(authManager); } |
| 70 | + |
| 71 | + /** |
| 72 | + * Begins serial communication on the given port. You must call begin on the stream first. |
| 73 | + * @param portStream the stream upon which to communicate, it must be already opened. |
| 74 | + * @param namePgm the local name of the application (may be program memory on AVR use safeCopyStr) |
| 75 | + */ |
| 76 | + void begin(HardwareSerial* portStream, const ConnectorLocalInfo* localInfo); |
| 77 | + |
| 78 | + /** |
| 79 | + * Arranged internally don't call yourself. |
| 80 | + */ |
| 81 | + void runLoop(); |
| 82 | + |
| 83 | + /** @return the remote connector associated with the connection */ |
| 84 | + TagValueRemoteConnector* getRemoteConnector(int /*num*/) {return &connector;} |
| 85 | + |
| 86 | + /** @return the transport that's associated with the connection */ |
| 87 | + SerialTagValueTransport* getTransport(int /*num*/) {return &serPort;} |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * the global instance of the SerialTagValServer |
| 92 | + */ |
| 93 | +extern SerialTagValServer remoteServer; |
| 94 | + |
| 95 | +#endif /* _TCMENU_SERIALTRANSPORT_H_ */ |
0 commit comments