From f8da2a98ea3bca76c653efb1f2b455c901fe4f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sun, 24 Apr 2022 21:58:05 +0200 Subject: [PATCH] [core] Change Arduino common libs structure --- arduino/libretuya/IPv6Address.cpp | 90 ++++++++++++++++++ arduino/libretuya/IPv6Address.h | 95 +++++++++++++++++++ .../{libretuya-api => libretuya/api}/Flash.h | 5 +- .../api}/Preferences.h | 4 +- arduino/realtek-ambz/libraries/Flash/Flash.h | 2 +- builder/{arduino-api.py => arduino-common.py} | 7 +- builder/frameworks/realtek-ambz-arduino.py | 2 +- 7 files changed, 195 insertions(+), 10 deletions(-) create mode 100644 arduino/libretuya/IPv6Address.cpp create mode 100644 arduino/libretuya/IPv6Address.h rename arduino/{libretuya-api => libretuya/api}/Flash.h (92%) rename arduino/{libretuya-api => libretuya/api}/Preferences.h (98%) rename builder/{arduino-api.py => arduino-common.py} (81%) diff --git a/arduino/libretuya/IPv6Address.cpp b/arduino/libretuya/IPv6Address.cpp new file mode 100644 index 0000000..2f39b37 --- /dev/null +++ b/arduino/libretuya/IPv6Address.cpp @@ -0,0 +1,90 @@ +/* + IPv6Address.cpp - Base class that provides IPv6Address + 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 + */ + +#include +#include +#include + +IPv6Address::IPv6Address() +{ + memset(_address.bytes, 0, sizeof(_address.bytes)); +} + +IPv6Address::IPv6Address(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); +} + +IPv6Address::IPv6Address(const uint32_t *address) +{ + memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes)); +} + +IPv6Address& IPv6Address::operator=(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; +} + +bool IPv6Address::operator==(const uint8_t* addr) const +{ + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; +} + +/* size_t IPv6Address::printTo(Print& p) const +{ + size_t n = 0; + for(int i = 0; i < 16; i+=2) { + if(i){ + n += p.print(':'); + } + n += p.printf("%02x", _address.bytes[i]); + n += p.printf("%02x", _address.bytes[i+1]); + + } + return n; +} */ + +String IPv6Address::toString() const +{ + char szRet[40]; + sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], + _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], + _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], + _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); + return String(szRet); +} + +bool IPv6Address::fromString(const char *address) +{ + //format 0011:2233:4455:6677:8899:aabb:ccdd:eeff + if(strlen(address) != 39){ + return false; + } + char * pos = (char *)address; + size_t i = 0; + for(i = 0; i < 16; i+=2) { + if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){ + return false; + } + pos += 5; + } + return true; +} diff --git a/arduino/libretuya/IPv6Address.h b/arduino/libretuya/IPv6Address.h new file mode 100644 index 0000000..fff5df0 --- /dev/null +++ b/arduino/libretuya/IPv6Address.h @@ -0,0 +1,95 @@ +/* + IPv6Address.h - Base class that provides IPv6Address + 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 + */ + +#ifndef IPv6Address_h +#define IPv6Address_h + +#include +#include +#include + +// A class to make it easier to handle and pass around IP addresses + +class IPv6Address: public Printable +{ +private: + union { + uint8_t bytes[16]; // IPv4 address + uint32_t dword[4]; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() + { + return _address.bytes; + } + +public: + // Constructors + IPv6Address(); + IPv6Address(const uint8_t *address); + IPv6Address(const uint32_t *address); + virtual ~IPv6Address() {} + + bool fromString(const char *address); + bool fromString(const String &address) { return fromString(address.c_str()); } + + operator const uint8_t*() const + { + return _address.bytes; + } + operator const uint32_t*() const + { + return _address.dword; + } + bool operator==(const IPv6Address& addr) const + { + return (_address.dword[0] == addr._address.dword[0]) + && (_address.dword[1] == addr._address.dword[1]) + && (_address.dword[2] == addr._address.dword[2]) + && (_address.dword[3] == addr._address.dword[3]); + } + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const + { + return _address.bytes[index]; + } + uint8_t& operator[](int index) + { + return _address.bytes[index]; + } + + // Overloaded copy operators to allow initialisation of IPv6Address objects from other types + IPv6Address& operator=(const uint8_t *address); + + // TODO implement printTo() + // virtual size_t printTo(Print& p) const; + String toString() const; + + friend class UDP; + friend class Client; + friend class Server; +}; + +#endif diff --git a/arduino/libretuya-api/Flash.h b/arduino/libretuya/api/Flash.h similarity index 92% rename from arduino/libretuya-api/Flash.h rename to arduino/libretuya/api/Flash.h index 1551106..381684e 100644 --- a/arduino/libretuya-api/Flash.h +++ b/arduino/libretuya/api/Flash.h @@ -4,7 +4,6 @@ #include #include - typedef struct { uint8_t manufacturerId; uint8_t chipId; @@ -13,8 +12,8 @@ typedef struct { class IFlashClass { public: - IFlashClass(){}; - ~IFlashClass(){}; + IFlashClass() {} + ~IFlashClass() {} virtual FlashId getChipId() = 0; virtual uint32_t getSize() = 0; diff --git a/arduino/libretuya-api/Preferences.h b/arduino/libretuya/api/Preferences.h similarity index 98% rename from arduino/libretuya-api/Preferences.h rename to arduino/libretuya/api/Preferences.h index a455aca..b14f7fb 100644 --- a/arduino/libretuya-api/Preferences.h +++ b/arduino/libretuya/api/Preferences.h @@ -35,8 +35,8 @@ typedef enum { class IPreferences { public: - IPreferences(){}; - ~IPreferences(){}; + IPreferences() {} + ~IPreferences() {} bool begin(const char *name, bool readOnly = false, const char *partition_label = NULL); void end(); diff --git a/arduino/realtek-ambz/libraries/Flash/Flash.h b/arduino/realtek-ambz/libraries/Flash/Flash.h index 343cfe7..3dd1c08 100644 --- a/arduino/realtek-ambz/libraries/Flash/Flash.h +++ b/arduino/realtek-ambz/libraries/Flash/Flash.h @@ -1,6 +1,6 @@ #pragma once -#include "libretuya-api/Flash.h" +#include "api/Flash.h" #ifdef __cplusplus extern "C" { diff --git a/builder/arduino-api.py b/builder/arduino-common.py similarity index 81% rename from builder/arduino-api.py rename to builder/arduino-common.py index c8d9120..33acecd 100644 --- a/builder/arduino-api.py +++ b/builder/arduino-common.py @@ -7,16 +7,16 @@ platform = env.PioPlatform() board = env.BoardConfig() API_DIR = platform.get_package_dir("framework-arduino-api") -ARDUINO_DIR = join(platform.get_dir(), "arduino") +LT_API_DIR = join(platform.get_dir(), "arduino", "libretuya") assert isdir(API_DIR) -assert isdir(ARDUINO_DIR) +assert isdir(LT_API_DIR) # Includes env.Prepend( CPPPATH=[ # fmt: off join(API_DIR), - join(ARDUINO_DIR), # for libretuya-api + join(LT_API_DIR), # for libretuya-api # fmt: on ] ) @@ -29,6 +29,7 @@ sources_api = [ "+<" + API_DIR + "/api/Print.cpp>", "+<" + API_DIR + "/api/Stream.cpp>", "+<" + API_DIR + "/api/String.cpp>", + "+<" + LT_API_DIR + "/IPv6Address.cpp>", # fmt: on ] diff --git a/builder/frameworks/realtek-ambz-arduino.py b/builder/frameworks/realtek-ambz-arduino.py index 7d4dc18..e31babb 100644 --- a/builder/frameworks/realtek-ambz-arduino.py +++ b/builder/frameworks/realtek-ambz-arduino.py @@ -8,7 +8,7 @@ platform = env.PioPlatform() board = env.BoardConfig() env.SConscript("realtek-ambz-sdk.py", exports="env") -env.SConscript("../arduino-api.py", exports="env") +env.SConscript("../arduino-common.py", exports="env") mcu = board.get("build.mcu").upper() family = board.get("build.family").upper()