SmartOS/Drivers/Esp8266/Esp8266.h

145 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __Esp8266_H__
#define __Esp8266_H__
#include "Device\Port.h"
#include "Net\Socket.h"
#include "Net\NetworkInterface.h"
#include "Net\ITransport.h"
#include "Message\DataStore.h"
#include "Message\Pair.h"
// 最好打开 Soket 前 不注册中断以免AT指令乱入到中断里面去 然后信息不对称
// 安信可 ESP8266 模块固件版本 v1.3.0.2
class Esp8266 : public WiFiInterface
{
public:
ushort MinSize; // 数据包最小大小
ushort MaxSize; // 数据包最大大小
ITransport* Port; // 传输口
NetworkType WorkMode; // 工作模式
IDataPort* Led; // 指示灯
Esp8266(ITransport* port, Pin power = P0, Pin rst = P0);
Esp8266(COM idx, Pin power = P0, Pin rst = P0);
virtual ~Esp8266();
void Init(ITransport* port, Pin power = P0, Pin rst = P0);
virtual bool Config();
void SetLed(Pin led);
void SetLed(OutputPort& led);
void RemoveLed();
//virtual const String ToString() const { return String("Esp8266"); }
virtual Socket* CreateSocket(NetType type);
// 启用DNS
virtual bool EnableDNS();
// 启用DHCP
virtual bool EnableDHCP();
/******************************** 基础AT指令 ********************************/
bool Test();
bool Reset(bool soft);
String GetVersion();
bool Sleep(uint ms);
bool Echo(bool open);
// 恢复出厂设置将擦写所有保存到Flash的参数恢复为默认参数。会导致模块重启
bool Restore();
/******************************** WiFi功能指令 ********************************/
// 获取模式
NetworkType GetMode();
// 设置模式。需要重启
bool SetMode(NetworkType mode);
// 连接AP相关
String GetJoinAP();
bool JoinAP(const String& ssid, const String& pass);
bool UnJoinAP();
bool SetAutoConn(bool enable);
String LoadAPs();
String GetAP();
bool SetAP(const String& ssid, const String& pass, byte channel, byte ecn = 0, byte maxConnect = 4, bool hidden = false);
// 查询连接到AP的Stations信息。无法查询DHCP接入
String LoadStations();
bool GetDHCP(bool* sta, bool* ap);
bool SetDHCP(NetworkType mode, bool enable);
MacAddress GetMAC(bool sta);
bool SetMAC(bool sta, const MacAddress& mac);
IPAddress GetIP(bool sta);
/******************************** TCP/IP ********************************/
String GetStatus();
bool GetMux();
bool SetMux(bool enable);
bool Update();
bool Ping(const IPAddress& ip);
bool SetIPD(bool enable);
/******************************** 发送指令 ********************************/
// 发送指令,在超时时间内等待返回期望字符串,然后返回内容
String Send(const String& cmd, cstring expect, cstring expect2 = nullptr, uint msTimeout = 1000);
// 发送命令,自动检测并加上\r\n等待响应OK
bool SendCmd(const String& cmd, uint msTimeout = 1000);
bool WaitForCmd(cstring expect, uint msTimeout);
/******************************** 发送指令 ********************************/
// 设置无线组网密码。匹配令牌协议
bool SetWiFi(const Pair& args, Stream& result);
// 获取无线名称。
bool GetWiFi(const Pair& args, Stream& result);
// 获取可用APs
String* APs;
void GetAPsTask();
bool GetAPs(const Pair& args, Stream& result);
private:
OutputPort _power;
OutputPort _rst;
uint _task; // 调度任务
ByteArray _Buffer; // 待处理数据包
IPEndPoint _Remote; // 当前数据包远程地址
// 打开与关闭
virtual bool OnOpen();
virtual void OnClose();
// 检测连接
virtual bool OnLink(uint retry);
bool CheckReady();
void OpenAP();
//static void LoopTask(void* param);
// 处理收到的数据包
void Process();
void* _Expect; // 等待内容
// 多个硬件socket
int* _sockets[5];
// 分析+IPD接收数据。返回被用掉的字节数
uint ParseReceive(const Buffer& bs);
// 分析关键字。返回被用掉的字节数
uint ParseReply(const Buffer& bs);
// 引发数据到达事件
uint OnReceive(Buffer& bs, void* param);
static uint OnPortReceive(ITransport* sender, Buffer& bs, void* param, void* param2);
};
#endif