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.

Dependencies:   mbed DHT Matrix

Committer:
ShaneKirkbride
Date:
Thu Jul 14 20:38:18 2016 +0000
Revision:
0:70a6082c1bf7
Child:
2:20ea1be14e4b
first commit. Rest example working

Who changed what in which revision?

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