![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a SLIP interface for the STM32F446RE Nucleo Board. It is designed to work specifically with the esp-link software for the ESP8266. The program is an example of a rest command.
STMClient.h@4:31bed73a0d08, 2016-08-09 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Tue Aug 09 22:42:31 2016 +0000
- Revision:
- 4:31bed73a0d08
- Parent:
- 2:20ea1be14e4b
- Child:
- 8:6a3b7c5d9ba7
added interrupt driven interface;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ShaneKirkbride | 0:70a6082c1bf7 | 1 | #ifndef _STM_CLIENT_H_ |
ShaneKirkbride | 0:70a6082c1bf7 | 2 | #define _STM_CLIENT_H_ |
ShaneKirkbride | 0:70a6082c1bf7 | 3 | |
ShaneKirkbride | 0:70a6082c1bf7 | 4 | #include "mbed.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 5 | #include "STMClientResponse.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 6 | #include "FP.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 7 | |
ShaneKirkbride | 0:70a6082c1bf7 | 8 | #define ESP_TIMEOUT 2000 |
ShaneKirkbride | 2:20ea1be14e4b | 9 | #define PGM_P const char * |
ShaneKirkbride | 0:70a6082c1bf7 | 10 | |
ShaneKirkbride | 0:70a6082c1bf7 | 11 | // Enumeration of commands supported by esp-link, this needs to match the definition in |
ShaneKirkbride | 0:70a6082c1bf7 | 12 | // esp-link! |
ShaneKirkbride | 0:70a6082c1bf7 | 13 | typedef enum { |
ShaneKirkbride | 0:70a6082c1bf7 | 14 | CMD_NULL = 0, // null, mainly to prevent 0 from doing something bad |
ShaneKirkbride | 0:70a6082c1bf7 | 15 | CMD_SYNC, // synchronize, starts the protocol |
ShaneKirkbride | 0:70a6082c1bf7 | 16 | CMD_RESP_V, // response with a value |
ShaneKirkbride | 0:70a6082c1bf7 | 17 | CMD_RESP_CB, // response with a callback |
ShaneKirkbride | 0:70a6082c1bf7 | 18 | CMD_WIFI_STATUS, // get the wifi status |
ShaneKirkbride | 0:70a6082c1bf7 | 19 | CMD_CB_ADD, // add a custom callback |
ShaneKirkbride | 0:70a6082c1bf7 | 20 | CMD_CB_EVENTS, // ??? |
ShaneKirkbride | 0:70a6082c1bf7 | 21 | CMD_GET_TIME, // get current time in seconds since the unix epoch |
ShaneKirkbride | 0:70a6082c1bf7 | 22 | //CMD_GET_INFO, |
ShaneKirkbride | 0:70a6082c1bf7 | 23 | |
ShaneKirkbride | 0:70a6082c1bf7 | 24 | CMD_MQTT_SETUP = 10, |
ShaneKirkbride | 0:70a6082c1bf7 | 25 | CMD_MQTT_PUBLISH, |
ShaneKirkbride | 0:70a6082c1bf7 | 26 | CMD_MQTT_SUBSCRIBE, |
ShaneKirkbride | 0:70a6082c1bf7 | 27 | CMD_MQTT_LWT, |
ShaneKirkbride | 0:70a6082c1bf7 | 28 | CMD_MQTT_EVENTS, |
ShaneKirkbride | 0:70a6082c1bf7 | 29 | |
ShaneKirkbride | 0:70a6082c1bf7 | 30 | CMD_REST_SETUP = 20, |
ShaneKirkbride | 0:70a6082c1bf7 | 31 | CMD_REST_REQUEST, |
ShaneKirkbride | 0:70a6082c1bf7 | 32 | CMD_REST_SETHEADER, |
ShaneKirkbride | 0:70a6082c1bf7 | 33 | CMD_REST_EVENTS |
ShaneKirkbride | 0:70a6082c1bf7 | 34 | |
ShaneKirkbride | 0:70a6082c1bf7 | 35 | } CmdName; |
ShaneKirkbride | 0:70a6082c1bf7 | 36 | |
ShaneKirkbride | 0:70a6082c1bf7 | 37 | enum WIFI_STATUS { |
ShaneKirkbride | 0:70a6082c1bf7 | 38 | STATION_IDLE = 0, |
ShaneKirkbride | 0:70a6082c1bf7 | 39 | STATION_CONNECTING, |
ShaneKirkbride | 0:70a6082c1bf7 | 40 | STATION_WRONG_PASSWORD, |
ShaneKirkbride | 0:70a6082c1bf7 | 41 | STATION_NO_AP_FOUND, |
ShaneKirkbride | 0:70a6082c1bf7 | 42 | STATION_CONNECT_FAIL, |
ShaneKirkbride | 0:70a6082c1bf7 | 43 | STATION_GOT_IP |
ShaneKirkbride | 0:70a6082c1bf7 | 44 | }; |
ShaneKirkbride | 0:70a6082c1bf7 | 45 | |
ShaneKirkbride | 0:70a6082c1bf7 | 46 | typedef struct { |
ShaneKirkbride | 0:70a6082c1bf7 | 47 | uint8_t* buf; |
ShaneKirkbride | 0:70a6082c1bf7 | 48 | uint16_t bufSize; |
ShaneKirkbride | 0:70a6082c1bf7 | 49 | uint16_t dataLen; |
ShaneKirkbride | 0:70a6082c1bf7 | 50 | uint8_t isEsc; |
ShaneKirkbride | 0:70a6082c1bf7 | 51 | } STMClientProtocol; |
ShaneKirkbride | 0:70a6082c1bf7 | 52 | |
ShaneKirkbride | 0:70a6082c1bf7 | 53 | class STMClient { |
ShaneKirkbride | 0:70a6082c1bf7 | 54 | public: |
ShaneKirkbride | 0:70a6082c1bf7 | 55 | //Make the serial process interrupt driven |
ShaneKirkbride | 0:70a6082c1bf7 | 56 | |
ShaneKirkbride | 0:70a6082c1bf7 | 57 | // Create an esp-link client based on a stream and with a specified debug output stream. |
ShaneKirkbride | 0:70a6082c1bf7 | 58 | STMClient(Serial* serial, Serial* debug); |
ShaneKirkbride | 0:70a6082c1bf7 | 59 | |
ShaneKirkbride | 0:70a6082c1bf7 | 60 | // Create an esp-link client based on a stream with no debug output |
ShaneKirkbride | 0:70a6082c1bf7 | 61 | STMClient(Serial* serial); |
ShaneKirkbride | 0:70a6082c1bf7 | 62 | |
ShaneKirkbride | 0:70a6082c1bf7 | 63 | Serial* _debug; |
ShaneKirkbride | 0:70a6082c1bf7 | 64 | |
ShaneKirkbride | 0:70a6082c1bf7 | 65 | //== Requests |
ShaneKirkbride | 0:70a6082c1bf7 | 66 | // Start a request. cmd is the command to execute, value is either the address of a function |
ShaneKirkbride | 0:70a6082c1bf7 | 67 | // to call with a response or a first argument to the command if there is no CB. |
ShaneKirkbride | 0:70a6082c1bf7 | 68 | // Argc is the number of additional arguments |
ShaneKirkbride | 0:70a6082c1bf7 | 69 | void Request(uint16_t cmd, uint32_t value, uint16_t argc); |
ShaneKirkbride | 0:70a6082c1bf7 | 70 | |
ShaneKirkbride | 0:70a6082c1bf7 | 71 | // Add a data block as argument to a request |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | void Request(const void* data, uint16_t len); |
ShaneKirkbride | 0:70a6082c1bf7 | 73 | |
ShaneKirkbride | 0:70a6082c1bf7 | 74 | // Add a data block from flash as argument to a request |
ShaneKirkbride | 0:70a6082c1bf7 | 75 | //void Request(const __FlashStringHelper* data, uint16_t len); //commented out for now... |
ShaneKirkbride | 0:70a6082c1bf7 | 76 | |
ShaneKirkbride | 0:70a6082c1bf7 | 77 | // Finish a request |
ShaneKirkbride | 0:70a6082c1bf7 | 78 | void Request(void); |
ShaneKirkbride | 0:70a6082c1bf7 | 79 | |
ShaneKirkbride | 0:70a6082c1bf7 | 80 | //== Responses |
ShaneKirkbride | 0:70a6082c1bf7 | 81 | // Process the input stream, call this in loop() to dispatch call-back based responses. |
ShaneKirkbride | 0:70a6082c1bf7 | 82 | // Callbacks are invoked with an STMClientResponse pointer as argument. |
ShaneKirkbride | 0:70a6082c1bf7 | 83 | // Returns the STMClientPacket if a non-callback response was received, typically this is |
ShaneKirkbride | 0:70a6082c1bf7 | 84 | // used to create an STMClientResponse. Returns NULL if no response needs to be processed. |
ShaneKirkbride | 0:70a6082c1bf7 | 85 | STMClientPacket *Process(void); |
ShaneKirkbride | 0:70a6082c1bf7 | 86 | |
ShaneKirkbride | 0:70a6082c1bf7 | 87 | // Busy wait for a response with a timeout in milliseconds, returns an STMClientPacket |
ShaneKirkbride | 0:70a6082c1bf7 | 88 | // if a response was recv'd and NULL otherwise. The STMClientPacket is typically used to |
ShaneKirkbride | 0:70a6082c1bf7 | 89 | // create an STMClientResponse. |
ShaneKirkbride | 0:70a6082c1bf7 | 90 | STMClientPacket *WaitReturn(uint32_t timeout=ESP_TIMEOUT); |
ShaneKirkbride | 0:70a6082c1bf7 | 91 | |
ShaneKirkbride | 0:70a6082c1bf7 | 92 | //== Commands built-into STMClient |
ShaneKirkbride | 0:70a6082c1bf7 | 93 | // Initialize and synchronize communication with esp-link with a timeout in milliseconds, |
ShaneKirkbride | 0:70a6082c1bf7 | 94 | // and remove all existing callbacks. Registers the wifiCb and returns true on success |
ShaneKirkbride | 0:70a6082c1bf7 | 95 | bool Sync(uint32_t timeout=ESP_TIMEOUT); |
ShaneKirkbride | 0:70a6082c1bf7 | 96 | // Request the wifi status |
ShaneKirkbride | 0:70a6082c1bf7 | 97 | void GetWifiStatus(void); |
ShaneKirkbride | 0:70a6082c1bf7 | 98 | |
ShaneKirkbride | 0:70a6082c1bf7 | 99 | // Callback for wifi status changes that must be attached before calling Sync |
ShaneKirkbride | 0:70a6082c1bf7 | 100 | FP<void, void*> wifiCb; |
ShaneKirkbride | 0:70a6082c1bf7 | 101 | |
ShaneKirkbride | 4:31bed73a0d08 | 102 | private: |
ShaneKirkbride | 0:70a6082c1bf7 | 103 | Serial* _serial; |
ShaneKirkbride | 0:70a6082c1bf7 | 104 | bool _debugEn; |
ShaneKirkbride | 0:70a6082c1bf7 | 105 | uint16_t crc; |
ShaneKirkbride | 0:70a6082c1bf7 | 106 | STMClientProtocol _proto; |
ShaneKirkbride | 0:70a6082c1bf7 | 107 | uint8_t _protoBuf[128]; |
ShaneKirkbride | 0:70a6082c1bf7 | 108 | |
ShaneKirkbride | 0:70a6082c1bf7 | 109 | void init(); |
ShaneKirkbride | 0:70a6082c1bf7 | 110 | void DBG(const char* info); |
ShaneKirkbride | 0:70a6082c1bf7 | 111 | STMClientPacket *protoCompletedCb(void); |
ShaneKirkbride | 0:70a6082c1bf7 | 112 | void write(uint8_t data); |
ShaneKirkbride | 0:70a6082c1bf7 | 113 | void write(void* data, uint16_t len); |
ShaneKirkbride | 0:70a6082c1bf7 | 114 | uint16_t crc16Add(unsigned char b, uint16_t acc); |
ShaneKirkbride | 0:70a6082c1bf7 | 115 | uint16_t crc16Data(const unsigned char *data, uint16_t len, uint16_t acc); |
ShaneKirkbride | 0:70a6082c1bf7 | 116 | }; |
ShaneKirkbride | 0:70a6082c1bf7 | 117 | #endif // _EL_CLIENT_H_ |