Peter Ferland / Mbed OS Dot-AT-Firmware-USB

Dependencies:   MTS-Serial libmDot-mbed5

Fork of Dot-AT-Firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CommandTerminal.h Source File

CommandTerminal.h

00001 /**
00002  ******************************************************************************
00003  * File Name          : command.h
00004  * Date               : 18/04/2014 10:57:12
00005  * Description        : This file provides code for command line prompt
00006  ******************************************************************************
00007  *
00008  * COPYRIGHT(c) 2014 MultiTech Systems, Inc.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *   1. Redistributions of source code must retain the above copyright notice,
00013  *      this list of conditions and the following disclaimer.
00014  *   2. Redistributions in binary form must reproduce the above copyright notice,
00015  *      this list of conditions and the following disclaimer in the documentation
00016  *      and/or other materials provided with the distribution.
00017  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00018  *      may be used to endorse or promote products derived from this software
00019  *      without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00027  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  *
00032  ******************************************************************************
00033  */
00034 
00035 #include "mbed.h"
00036 #include "ATSerial.h"
00037 #include "MTSSerial.h"
00038 #include "MTSSerialFlowControl.h"
00039 #include "Commands.h"
00040 #include "mDot.h"
00041 #include "mDotEvent.h"
00042 
00043 /* Define to prevent recursive inclusion -------------------------------------*/
00044 #ifndef __command_terminal_H__
00045 #define __command_terminal_H__
00046 
00047 typedef uint32_t (*action_ptr_t)(std::vector<std::string> args);
00048 typedef bool (*verify_ptr_t)(std::vector<std::string> args);
00049 
00050 class Command;
00051 
00052 class CommandTerminal {
00053 
00054     class RadioEvent : public mDotEvent {
00055 
00056         public:
00057         RadioEvent() : _sendAck(false) {}
00058 
00059         virtual ~RadioEvent() {}
00060 
00061         virtual void TxDone(uint8_t dr) {
00062             mDotEvent::TxDone(dr);
00063 
00064             logDebug("RadioEvent - TxDone");
00065         }
00066 
00067         virtual void TxTimeout(void) {
00068             mDotEvent::TxTimeout();
00069 
00070             logDebug("RadioEvent - TxTimeout");
00071         }
00072 
00073         virtual void JoinAccept(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
00074             mDotEvent::JoinAccept(payload, size, rssi, snr);
00075 
00076             logDebug("RadioEvent - JoinAccept");
00077         }
00078 
00079         virtual void JoinFailed(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
00080             mDotEvent::JoinFailed(payload, size, rssi, snr);
00081 
00082             logDebug("RadioEvent - JoinFailed");
00083         }
00084 
00085         virtual void PacketRx(uint8_t port, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot, uint8_t retries = 0);
00086 
00087         virtual void RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot) {
00088             mDotEvent::RxDone(payload, size, rssi, snr, ctrl, slot);
00089 
00090             logDebug("RadioEvent - RxDone");
00091         }
00092 
00093         virtual void Pong(int16_t m_rssi, int8_t m_snr, int16_t s_rssi, int8_t s_snr) {
00094             mDotEvent::Pong(m_rssi, m_snr, s_rssi, s_snr);
00095 
00096             logDebug("RadioEvent - Pong");
00097         }
00098 
00099         virtual void NetworkLinkCheck(int16_t m_rssi, int8_t m_snr, int8_t s_snr, uint8_t s_gateways) {
00100             mDotEvent::NetworkLinkCheck(m_rssi, m_snr, s_snr, s_gateways);
00101 
00102             logDebug("RadioEvent - NetworkLinkCheck");
00103         }
00104 
00105         virtual void RxTimeout(uint8_t slot) {
00106             mDotEvent::RxTimeout(slot);
00107 
00108             // logDebug("RadioEvent - RxTimeout");
00109         }
00110 
00111         virtual void RxError(uint8_t slot) {
00112             mDotEvent::RxError(slot);
00113 
00114             logDebug("RadioEvent - RxError");
00115         }
00116 
00117         virtual uint8_t MeasureBattery(void) {
00118             return 255;
00119         }
00120 
00121         virtual void MissedAck(uint8_t retries) {
00122             mDotEvent::MissedAck(retries);
00123         }
00124 
00125         bool SendAck() {
00126             bool val = _sendAck;
00127             _sendAck = false;
00128             return val;
00129         }
00130 
00131         bool _sendAck;
00132     };
00133 
00134     public:
00135 
00136     enum WaitType {
00137         WAIT_JOIN,
00138         WAIT_RECV,
00139         WAIT_LINK,
00140         WAIT_SEND,
00141         WAIT_NA
00142     };
00143 
00144     CommandTerminal(mts::ATSerial& serial);
00145     virtual ~CommandTerminal();
00146 
00147     void init();
00148 
00149     // Command prompt text...
00150     static const char banner[];
00151     static const char helpline[];
00152     static const char prompt[];
00153 
00154     // Command error text...
00155     static const char command_error[];
00156 
00157     // Response texts...
00158     static const char help[];
00159     static const char cmd_error[];
00160     static const char newline[];
00161     static const char connect[];
00162     static const char no_carrier[];
00163     static const char done[];
00164     static const char error[];
00165 
00166     // Escape sequence
00167     static const char escape_sequence[];
00168 
00169     static std::string formatPacketData(const std::vector<uint8_t>& data, const uint8_t& format);
00170     static bool waitForEscape(int timeout, mDot* dot=NULL, WaitType wait=WAIT_NA);
00171 
00172     void start();
00173 
00174     static mts::ATSerial* Serial() {return _serialp;}
00175     static mDot* Dot() {return _dot;}
00176     static mDot* _dot;
00177 
00178     static void setErrorMessage(const char* message);
00179     static void setErrorMessage(const std::string& message);
00180 
00181     protected:
00182 
00183     static std::string _errorMessage;
00184 
00185     private:
00186 
00187     mts::ATSerial& _serial;
00188     static mts::ATSerial* _serialp;
00189 
00190     static CommandTerminal::RadioEvent* _events;
00191     mDot::Mode _mode;
00192 
00193     static const Command _commands[NO_OF_COMMANDS];
00194     static const verify_ptr_t _verify[NO_OF_COMMANDS];
00195     static const action_ptr_t _action[NO_OF_COMMANDS];
00196 
00197     bool _sleep_standby;
00198     DigitalOut _xbee_on_sleep;
00199     bool _autoOTAEnabled;
00200 
00201     void serialLoop();
00202     bool autoJoinCheck();
00203 
00204     void printHelp();
00205 
00206     static bool readable();
00207     static bool writeable();
00208     static char read();
00209     static void write(const char* message);
00210     static void writef(const char* format, ... );
00211 
00212     void sleep(bool standby);
00213     void wakeup(void);
00214 
00215 };
00216 
00217 #endif // __command_terminal_H__