Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
AtClient.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2017 by Slavey Karadzhov
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * AtClient.h
8  *
9  ****/
10 
16 #ifndef _SMING_CORE_ATCLIENT_H_
17 #define _SMING_CORE_ATCLIENT_H_
18 
19 #include "HardwareSerial.h"
20 #include "FILO.h"
21 #include "Delegate.h"
22 #include "Timer.h"
23 
24 #define AT_REPLY_OK "OK"
25 #ifndef AT_TIMEOUT
26 #define AT_TIMEOUT 2000
27 #endif
28 
29 class AtClient;
30 
32 // ^ If the callback returns true then this means that we have
33 // finished successfully processing the command
35 // ^ If the callback returns true then this means that we have
36 // finished successfully processing the command
37 
38 typedef struct {
41  unsigned timeout;
42  unsigned retries;
43  bool breakOnError = true;
44  AtReceiveCallback onReceive = 0;
45  AtCompleteCallback onComplete = 0;
46 } AtCommand;
47 
48 typedef enum { eAtOK = 0, eAtRunning, eAtError } AtState;
49 
53 class AtClient
54 {
55 public:
56  AtClient(HardwareSerial* stream);
57 
58  virtual ~AtClient()
59  {
60  }
61 
69  void send(const String& text, const String& altResponse = nullptr, uint32_t timeoutMs = AT_TIMEOUT,
70  unsigned retries = 0);
71 
79  void send(const String& text, AtReceiveCallback onReceive, uint32_t timeoutMs = AT_TIMEOUT, unsigned retries = 0);
80 
88  void send(const String& text, AtCompleteCallback onComplete, uint32_t timeoutMs = AT_TIMEOUT, unsigned retries = 0);
89 
90  // Low Level Functions
91 
98  void send(AtCommand command);
99 
104  void sendDirect(AtCommand command);
105 
110  AtState getState()
111  {
112  return state;
113  }
114 
115  /*
116  * @brief Repeats the execution of the current command
117  * Useful if the current State is not eAtOK
118  */
119  void resend();
120 
121  /*
122  * @brief Replaces the current command with the next on in the queue
123  */
124  void next();
125 
127 
128 protected:
132  virtual void processor(Stream& source, char arrivedChar, uint16_t availableCharsCount);
133 
134 private:
135  FIFO<AtCommand, 10> queue;
136  HardwareSerial* stream = nullptr;
137  Timer commandTimer;
138  AtState state = eAtOK;
139 
143  void ticker();
144 };
145 
155 #endif /* _SMING_CORE_ATCLIENT_H_ */
void sendDirect(AtCommand command)
Executes directly (does not queue it) a command.
void send(const String &text, const String &altResponse=nullptr, uint32_t timeoutMs=AT_TIMEOUT, unsigned retries=0)
Sends AT command.
String response2
alternative successful response
Definition: AtClient.h:40
The string class.
Definition: WString.h:104
AtCommand currentCommand
The current command.
Definition: AtClient.h:126
AtState getState()
Returns the current state.
Definition: AtClient.h:110
Definition: AtClient.h:38
Hardware serial class.
Definition: HardwareSerial.h:100
virtual void processor(Stream &source, char arrivedChar, uint16_t availableCharsCount)
Processes response data.
unsigned retries
number of retries before giving up
Definition: AtClient.h:42
unsigned timeout
timeout in milliseconds
Definition: AtClient.h:41
Class that facilitates the communication with an AT device.
Definition: AtClient.h:53
Definition: Timer.h:26
String text
the actual AT command
Definition: AtClient.h:39
Definition: Stream.h:30