diff --git a/README.md b/README.md index cff6429..507d90c 100644 --- a/README.md +++ b/README.md @@ -77,8 +77,9 @@ arduino/ ├─ / Arduino Core for specific platform │ ├─ cores/ Core files │ ├─ libraries/ Supported built-in libraries -├─ libretuya-api/ -│ ├─ / Library interfaces (.h) for LibreTuya Arduino cores +├─ libretuya/ +│ ├─ api/ Library interfaces (.h) for LibreTuya Arduino cores +│ ├─ .cpp Built-in platform-independent libraries boards/ ├─ / Board-specific code │ ├─ variant.cpp Arduino variant initialization diff --git a/arduino/descript.ion b/arduino/descript.ion index 14d6396..722627e 100644 --- a/arduino/descript.ion +++ b/arduino/descript.ion @@ -1,2 +1,2 @@ realtek-ambz Realtek AmebaZ Arduino Core -libretuya-api Interfaces for LibreTuya Arduino cores +libretuya Interfaces for LibreTuya Arduino cores diff --git a/arduino/libretuya/api/Flash.h b/arduino/libretuya/api/Flash.h index 381684e..0b00949 100644 --- a/arduino/libretuya/api/Flash.h +++ b/arduino/libretuya/api/Flash.h @@ -13,12 +13,13 @@ typedef struct { class IFlashClass { public: IFlashClass() {} + ~IFlashClass() {} virtual FlashId getChipId() = 0; - virtual uint32_t getSize() = 0; + virtual uint32_t getSize() = 0; - virtual bool eraseSector(uint32_t sector) = 0; - virtual bool readBlock(uint32_t offset, uint8_t *data, size_t size) = 0; + virtual bool eraseSector(uint32_t sector) = 0; + virtual bool readBlock(uint32_t offset, uint8_t *data, size_t size) = 0; virtual bool writeBlock(uint32_t offset, uint8_t *data, size_t size) = 0; }; diff --git a/arduino/libretuya/api/Preferences.h b/arduino/libretuya/api/Preferences.h index b14f7fb..c353e18 100644 --- a/arduino/libretuya/api/Preferences.h +++ b/arduino/libretuya/api/Preferences.h @@ -36,6 +36,7 @@ typedef enum { class IPreferences { public: IPreferences() {} + ~IPreferences() {} bool begin(const char *name, bool readOnly = false, const char *partition_label = NULL); diff --git a/arduino/libretuya/api/WiFi.h b/arduino/libretuya/api/WiFi.h new file mode 100644 index 0000000..e1cf6c1 --- /dev/null +++ b/arduino/libretuya/api/WiFi.h @@ -0,0 +1,173 @@ +/* + WiFi.h - esp32 Wifi support. + Based on WiFi.h from Arduino WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include + +#include +#include +#include + +#include "WiFiClient.h" +#include "WiFiServer.h" +#include "WiFiType.h" +#include "WiFiUdp.h" + +// TODO wifi events +// TODO WiFiMulti library + +class IWiFi { + public: + void printDiag(Print &dest); + + // WiFiGenericClass + int32_t channel(void); + static bool mode(WiFiMode mode); + static WiFiMode getMode(); + + bool enableSTA(bool enable); + bool enableAP(bool enable); + + bool setSleep(bool enable); + bool getSleep(); + + bool setTxPower(int power); + int getTxPower(); + + static int hostByName(const char *aHostname, IPAddress &aResult); + + static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet); + static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet); + static uint8_t calculateSubnetCIDR(IPAddress subnetMask); + + // WiFiSTAClass + WiFiStatus begin( + const char *ssid, + const char *passphrase = NULL, + int32_t channel = 0, + const uint8_t *bssid = NULL, + bool connect = true, + ); + WiFiStatus + begin(char *ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t *bssid = NULL, bool connect = true, ); + WiFiStatus begin(); + + bool config( + IPAddress local_ip, + IPAddress gateway, + IPAddress subnet, + IPAddress dns1 = (uint32_t)0x00000000, + IPAddress dns2 = (uint32_t)0x00000000, + ); + + bool reconnect(); + bool disconnect(bool wifioff = false, bool eraseap = false); + + bool isConnected(); + + bool setAutoConnect(bool autoConnect); + bool getAutoConnect(); + + bool setAutoReconnect(bool autoReconnect); + bool getAutoReconnect(); + + uint8_t waitForConnectResult(); + + IPAddress localIP(); + uint8_t *macAddress(uint8_t *mac); + String macAddress(); + IPAddress subnetMask(); + IPAddress gatewayIP(); + IPAddress dnsIP(uint8_t dns_no = 0); + IPAddress broadcastIP(); + IPAddress networkID(); + uint8_t subnetCIDR(); + bool enableIpV6(); + IPv6Address localIPv6(); + const char *getHostname(); + bool setHostname(const char *hostname); + + bool hostname(const String &aHostname) { + return setHostname(aHostname.c_str()); + } + + static WiFiStatus status(); + String SSID() const; + String psk() const; + uint8_t *BSSID(); + String BSSIDstr(); + int8_t RSSI(); + + // WiFiScanClass + int16_t scanNetworks( + bool async = false, + bool show_hidden = false, + bool passive = false, + uint32_t max_ms_per_chan = 300, + uint8_t channel = 0, + ); + bool getNetworkInfo( + uint8_t networkItem, + String &ssid, + WiFiAuthMode &encryptionType, + int32_t &RSSI, + uint8_t *&BSSID, + int32_t &channel, + ); + + int16_t scanComplete(); + void scanDelete(); + + String SSID(uint8_t networkItem); + WiFiAuthMode encryptionType(uint8_t networkItem); + int32_t RSSI(uint8_t networkItem); + uint8_t *BSSID(uint8_t networkItem); + String BSSIDstr(uint8_t networkItem); + int32_t channel(uint8_t networkItem); + + static void *getScanInfoByIndex(int i) { + return _getScanInfoByIndex(i); + }; + + // WiFiAPClass + bool softAP( + const char *ssid, const char *passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4 + ); + bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + bool softAPdisconnect(bool wifioff = false); + + uint8_t softAPgetStationNum(); + + IPAddress softAPIP(); + IPAddress softAPBroadcastIP(); + IPAddress softAPNetworkID(); + uint8_t softAPSubnetCIDR(); + bool softAPenableIpV6(); + IPv6Address softAPIPv6(); + const char *softAPgetHostname(); + bool softAPsetHostname(const char *hostname); + uint8_t *softAPmacAddress(uint8_t *mac); + String softAPmacAddress(void); + String softAPSSID(void) const; +}; + +extern WiFiClass WiFi; diff --git a/arduino/libretuya/api/WiFiClient.h b/arduino/libretuya/api/WiFiClient.h new file mode 100644 index 0000000..0691b43 --- /dev/null +++ b/arduino/libretuya/api/WiFiClient.h @@ -0,0 +1,65 @@ +/* + Client.h - Base class that provides Client + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include +#include +#include + +class IWiFiClient : public Client { + public: + WiFiClient(int fd); + + int connect(IPAddress ip, uint16_t port, int32_t timeout); + int connect(const char *host, uint16_t port, int32_t timeout); + + size_t write(Stream &stream); + + int fd() const; + int socket(); + int setTimeout(uint32_t seconds); + + WiFiClient &operator=(const WiFiClient &other); + + bool operator==(const bool value) { + return bool() == value; + } + + bool operator!=(const bool value) { + return bool() != value; + } + + bool operator==(const WiFiClient &); + + bool operator!=(const WiFiClient &rhs) { + return !this->operator==(rhs); + }; + + IPAddress remoteIP() const; + IPAddress remoteIP(int fd) const; + uint16_t remotePort() const; + uint16_t remotePort(int fd) const; + IPAddress localIP() const; + IPAddress localIP(int fd) const; + uint16_t localPort() const; + uint16_t localPort(int fd) const; + + using Print::write; +}; diff --git a/arduino/libretuya/api/WiFiClientSecure.h b/arduino/libretuya/api/WiFiClientSecure.h new file mode 100644 index 0000000..c05d1b9 --- /dev/null +++ b/arduino/libretuya/api/WiFiClientSecure.h @@ -0,0 +1,63 @@ +/* + WiFiClientSecure.h - Base class that provides Client SSL to ESP32 + Copyright (c) 2011 Adrian McEwen. All right reserved. + Additions Copyright (C) 2017 Evandro Luis Copercini. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include + +#include "WiFi.h" + +class IWiFiClientSecure : public IWiFiClient { + public: + int connect(IPAddress ip, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key); + int connect(const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key); + int connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey); + int connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey); + + int lastError(char *buf, const size_t size); + void setInsecure(); // Don't validate the chain, just accept whatever is given. VERY INSECURE! + void setPreSharedKey(const char *pskIdent, const char *psKey); // psKey in Hex + void setCACert(const char *rootCA); + void setCertificate(const char *client_ca); + void setPrivateKey(const char *private_key); + bool loadCACert(Stream &stream, size_t size); + bool loadCertificate(Stream &stream, size_t size); + bool loadPrivateKey(Stream &stream, size_t size); + bool verify(const char *fingerprint, const char *domain_name); + void setHandshakeTimeout(unsigned long handshake_timeout); + + WiFiClientSecure &operator=(const WiFiClientSecure &other); + + bool operator==(const bool value) { + return bool() == value; + } + + bool operator!=(const bool value) { + return bool() != value; + } + + bool operator==(const WiFiClientSecure &); + + bool operator!=(const WiFiClientSecure &rhs) { + return !this->operator==(rhs); + }; + + using Print::write; +}; diff --git a/arduino/libretuya/api/WiFiServer.h b/arduino/libretuya/api/WiFiServer.h new file mode 100644 index 0000000..66ac3a7 --- /dev/null +++ b/arduino/libretuya/api/WiFiServer.h @@ -0,0 +1,62 @@ +/* + Server.h - Server class for Raspberry Pi + Copyright (c) 2016 Hristo Gochkov All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include +#include + +#include "WiFiClient.h" + +class IWiFiServer : public Server { + public: + void listenOnLocalhost() {} + + WiFiServer(uint16_t port = 80, uint8_t max_clients = 4) {} + + ~WiFiServer() { + end(); + } + + WiFiClient available(); + + WiFiClient accept() { + return available(); + } + + virtual operator bool() = 0; + + void begin(uint16_t port = 0); + void begin(uint16_t port, int reuse_enable); + void end(); + void close(); + void stop(); + + int setTimeout(uint32_t seconds); + void stopAll(); + void setNoDelay(bool nodelay); + bool getNoDelay(); + bool hasClient(); + + size_t write(uint8_t data) { + return write(&data, 1); + } + + using Print::write; +}; diff --git a/arduino/libretuya/api/WiFiType.h b/arduino/libretuya/api/WiFiType.h new file mode 100644 index 0000000..c85c076 --- /dev/null +++ b/arduino/libretuya/api/WiFiType.h @@ -0,0 +1,66 @@ +/* + ESP8266WiFiType.h - esp8266 Wifi support. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + Reworked by Markus Sattler, December 2015 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#define WIFI_SCAN_RUNNING (-1) +#define WIFI_SCAN_FAILED (-2) + +#define WiFiMode_t wl_mode_t +#define WiFiMode wl_mode_t +#define WiFiStatus wl_status_t +#define WiFiAuthMode wl_auth_mode_t + +#define WIFI_OFF WIFI_MODE_NULL +#define WIFI_STA WIFI_MODE_STA +#define WIFI_AP WIFI_MODE_AP +#define WIFI_AP_STA WIFI_MODE_APSTA + +typedef enum { + WIFI_MODE_NULL = 0, + WIFI_MODE_STA, + WIFI_MODE_AP, + WIFI_MODE_APSTA, + WIFI_MODE_MAX, +} wl_mode_t; + +typedef enum { + WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library + WL_IDLE_STATUS = 0, + WL_NO_SSID_AVAIL = 1, + WL_SCAN_COMPLETED = 2, + WL_CONNECTED = 3, + WL_CONNECT_FAILED = 4, + WL_CONNECTION_LOST = 5, + WL_DISCONNECTED = 6, +} wl_status_t; + +typedef enum { + WIFI_AUTH_INVALID = 255, + WIFI_AUTH_AUTO = 200, + WIFI_AUTH_OPEN_SYSTEM = 0, + WIFI_AUTH_SHARED_KEY = 1, + WIFI_AUTH_WPA = 10, + WIFI_AUTH_WPA2 = 11, + WIFI_AUTH_WPA_PSK = 2, + WIFI_AUTH_WPA2_PSK = 3, + WIFI_AUTH_WPA_WPA2_PSK = 4, +} wl_auth_mode_t; diff --git a/arduino/libretuya/api/WiFiUdp.h b/arduino/libretuya/api/WiFiUdp.h new file mode 100644 index 0000000..77d97cc --- /dev/null +++ b/arduino/libretuya/api/WiFiUdp.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +class IWiFiUDP : public UDP { + public: + uint8_t beginMulticast(IPAddress ip, uint16_t port); + int beginMulticastPacket(); + int beginPacket(); +}; diff --git a/arduino/realtek-ambz/libraries/Flash/Flash.cpp b/arduino/realtek-ambz/libraries/Flash/Flash.cpp index 787cf51..5c6f01a 100644 --- a/arduino/realtek-ambz/libraries/Flash/Flash.cpp +++ b/arduino/realtek-ambz/libraries/Flash/Flash.cpp @@ -24,8 +24,8 @@ FlashId FlashClass::getChipId() { uint8_t idBytes[3]; flash_read_id(flash, idBytes, 3); id.manufacturerId = idBytes[0]; - id.chipId = idBytes[1]; - id.chipSizeId = idBytes[2]; + id.chipId = idBytes[1]; + id.chipSizeId = idBytes[2]; return id; }