AtClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2017 by Slavey Karadzhov
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * AtClient.h
8  *
9  ****/
10 
16 #pragma once
17 
18 #include "HardwareSerial.h"
19 #include "FIFO.h"
20 #include "Timer.h"
21 
22 #define AT_REPLY_OK "OK"
23 #ifndef AT_TIMEOUT
24 #define AT_TIMEOUT 2000
25 #endif
26 
27 class AtClient;
28 
30 // ^ If the callback returns true then this means that we have
31 // finished successfully processing the command
33 // ^ If the callback returns true then this means that we have
34 // finished successfully processing the command
35 
36 typedef struct {
39  unsigned timeout;
40  unsigned retries;
41  bool breakOnError = true;
44 } AtCommand;
45 
46 typedef enum { eAtOK = 0, eAtRunning, eAtError } AtState;
47 
51 class AtClient
52 {
53 public:
54  AtClient(HardwareSerial* stream);
55 
56  virtual ~AtClient()
57  {
58  }
59 
67  void send(const String& text, const String& altResponse = nullptr, uint32_t timeoutMs = AT_TIMEOUT,
68  unsigned retries = 0);
69 
77  void send(const String& text, AtReceiveCallback onReceive, uint32_t timeoutMs = AT_TIMEOUT, unsigned retries = 0);
78 
86  void send(const String& text, AtCompleteCallback onComplete, uint32_t timeoutMs = AT_TIMEOUT, unsigned retries = 0);
87 
88  // Low Level Functions
89 
96  void send(AtCommand command);
97 
102  void sendDirect(AtCommand command);
103 
109  {
110  return state;
111  }
112 
113  /*
114  * @brief Repeats the execution of the current command
115  * Useful if the current State is not eAtOK
116  */
117  void resend();
118 
119  /*
120  * @brief Replaces the current command with the next on in the queue
121  */
122  void next();
123 
125 
126 protected:
130  virtual void processor(Stream& source, char arrivedChar, uint16_t availableCharsCount);
131 
132 private:
133  FIFO<AtCommand, 10> queue;
134  HardwareSerial* stream = nullptr;
135  Timer commandTimer;
136  AtState state = eAtOK;
137 
141  void ticker();
142 };
143 
virtual ~AtClient()
Definition: AtClient.h:56
AtReceiveCallback onReceive
if set you can process manually all incoming data in a callback
Definition: AtClient.h:42
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:38
The String class.
Definition: WString.h:136
Definition: AtClient.h:46
AtCommand currentCommand
The current command.
Definition: AtClient.h:124
void resend()
Definition: AtClient.h:46
Delegate< bool(AtClient &atClient, String &reply)> AtCompleteCallback
Definition: AtClient.h:32
AtState getState()
Returns the current state.
Definition: AtClient.h:108
AtClient(HardwareSerial *stream)
#define AT_TIMEOUT
Definition: AtClient.h:24
Definition: AtClient.h:36
Hardware serial class.
Definition: HardwareSerial.h:100
AtState
Definition: AtClient.h:46
virtual void processor(Stream &source, char arrivedChar, uint16_t availableCharsCount)
Processes response data.
unsigned retries
number of retries before giving up
Definition: AtClient.h:40
unsigned timeout
timeout in milliseconds
Definition: AtClient.h:39
Class that facilitates the communication with an AT device.
Definition: AtClient.h:51
Callback timer class.
Definition: Timer.h:255
String text
the actual AT command
Definition: AtClient.h:37
Delegate< bool(AtClient &atClient, Stream &source)> AtReceiveCallback
Definition: AtClient.h:27
AtCompleteCallback onComplete
if set then you can process the complete response manually
Definition: AtClient.h:43
void next()
Base Stream class.
Definition: Stream.h:32
Definition: AtClient.h:46