diff --git a/lib/espMqttClient/src/Helpers.h b/lib/espMqttClient/src/Helpers.h index 05ab136d9..5228156ea 100644 --- a/lib/espMqttClient/src/Helpers.h +++ b/lib/espMqttClient/src/Helpers.h @@ -9,41 +9,51 @@ the LICENSE file. #pragma once #if defined(ARDUINO_ARCH_ESP32) - #include // millis(), ESP.getFreeHeap(); - #include "freertos/FreeRTOS.h" - #include "freertos/task.h" - #include "esp_task_wdt.h" - #define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY) - #define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore) - #define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram()) - #define EMC_YIELD() vTaskDelay(1) - #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp32%06llx", ESP.getEfuseMac()); +#include // millis(), ESP.getFreeHeap(); +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_task_wdt.h" +#define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY) +#define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore) +#define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram()) +#define EMC_YIELD() vTaskDelay(1) +#define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp32%06llx", ESP.getEfuseMac()); #elif defined(ARDUINO_ARCH_ESP8266) - #include // millis(), ESP.getFreeHeap(); - #if EMC_ESP8266_MULTITHREADING - // This lib doesn't run use multithreading on ESP8266 - // _xSemaphore defined as std::atomic - #define EMC_SEMAPHORE_TAKE() while (_xSemaphore) { /*ESP.wdtFeed();*/ } _xSemaphore = true - #define EMC_SEMAPHORE_GIVE() _xSemaphore = false - #else - #define EMC_SEMAPHORE_TAKE() - #define EMC_SEMAPHORE_GIVE() - #endif - #define EMC_GET_FREE_MEMORY() ESP.getMaxFreeBlockSize() - // no need to yield for ESP8266, the Arduino framework does this internally - // yielding in async is forbidden (will crash) - #define EMC_YIELD() - #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp8266%06x", ESP.getChipId()); -#elif defined(__linux__) - #include // NOLINT [build/c++11] - #include // NOLINT [build/c++11] for yield() - #define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() - #define EMC_GET_FREE_MEMORY() 1000000000 - #define EMC_YIELD() std::this_thread::yield() - #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "Client%04d%04d%04d", rand()%10000, rand()%10000, rand()%10000) - #include // NOLINT [build/c++11] - #define EMC_SEMAPHORE_TAKE() mtx.lock(); - #define EMC_SEMAPHORE_GIVE() mtx.unlock(); +#include // millis(), ESP.getFreeHeap(); +#if EMC_ESP8266_MULTITHREADING +// This lib doesn't run use multithreading on ESP8266 +// _xSemaphore defined as std::atomic +#define EMC_SEMAPHORE_TAKE() \ + while (_xSemaphore) { /*ESP.wdtFeed();*/ \ + } \ + _xSemaphore = true +#define EMC_SEMAPHORE_GIVE() _xSemaphore = false #else - #error Target platform not supported +#define EMC_SEMAPHORE_TAKE() +#define EMC_SEMAPHORE_GIVE() +#endif +#define EMC_GET_FREE_MEMORY() ESP.getMaxFreeBlockSize() +// no need to yield for ESP8266, the Arduino framework does this internally +// yielding in async is forbidden (will crash) +#define EMC_YIELD() +#define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp8266%06x", ESP.getChipId()); +#elif defined(__linux__) +#include // NOLINT [build/c++11] +#include // NOLINT [build/c++11] for yield() +#define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() +#define EMC_GET_FREE_MEMORY() 1000000000 +#define EMC_YIELD() std::this_thread::yield() +#define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "Client%04d%04d%04d", rand() % 10000, rand() % 10000, rand() % 10000) +#include // NOLINT [build/c++11] +#define EMC_SEMAPHORE_TAKE() mtx.lock(); +#define EMC_SEMAPHORE_GIVE() mtx.unlock(); +#elif defined(_WIN32) +#include +#define EMC_SEMAPHORE_TAKE() +#define EMC_SEMAPHORE_GIVE() +#define EMC_YIELD() +#define EMC_GET_FREE_MEMORY() 1000 +#define EMC_GENERATE_CLIENTID(x) +#else +#error Target platform not supported #endif diff --git a/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp b/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp index 3386dec85..3bf936d93 100644 --- a/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp +++ b/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp @@ -6,27 +6,27 @@ For a copy, see or the LICENSE file. */ -#if defined(__linux__) +#if defined(__linux__) || defined(_WIN32) #include "ClientPosixIPAddress.h" IPAddress::IPAddress() -: _address(0) { - // empty + : _address(0) { + // empty } IPAddress::IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3) -: _address(0) { - _address = (uint32_t)p0 << 24 | (uint32_t)p1 << 16 | (uint32_t)p2 << 8 | p3; + : _address(0) { + _address = (uint32_t)p0 << 24 | (uint32_t)p1 << 16 | (uint32_t)p2 << 8 | p3; } IPAddress::IPAddress(uint32_t address) -: _address(address) { - // empty + : _address(address) { + // empty } IPAddress::operator uint32_t() { - return _address; + return _address; } #endif diff --git a/lib/espMqttClient/src/espMqttClient.cpp b/lib/espMqttClient/src/espMqttClient.cpp index bbbfd693e..a2cdc5ba7 100644 --- a/lib/espMqttClient/src/espMqttClient.cpp +++ b/lib/espMqttClient/src/espMqttClient.cpp @@ -120,4 +120,9 @@ espMqttClient::espMqttClient() , _client() { _transport = &_client; } +#elif defined(_WIN32) +// Windows +espMqttClient::espMqttClient() + : MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) { +} #endif diff --git a/lib/espMqttClient/src/espMqttClient.h b/lib/espMqttClient/src/espMqttClient.h index 4e448011d..8e42052d2 100644 --- a/lib/espMqttClient/src/espMqttClient.h +++ b/lib/espMqttClient/src/espMqttClient.h @@ -22,59 +22,64 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) class espMqttClient : public MqttClientSetup { - public: - espMqttClient(); + public: + espMqttClient(); - protected: - espMqttClientInternals::ClientSync _client; + protected: + espMqttClientInternals::ClientSync _client; }; class espMqttClientSecure : public MqttClientSetup { - public: - espMqttClientSecure(); - espMqttClientSecure& setInsecure(); - espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]); - espMqttClientSecure& setTrustAnchors(const X509List *ta); - espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk); - espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type); - espMqttClientSecure& setCertStore(CertStoreBase *certStore); + public: + espMqttClientSecure(); + espMqttClientSecure & setInsecure(); + espMqttClientSecure & setFingerprint(const uint8_t fingerprint[20]); + espMqttClientSecure & setTrustAnchors(const X509List * ta); + espMqttClientSecure & setClientRSACert(const X509List * cert, const PrivateKey * sk); + espMqttClientSecure & setClientECCert(const X509List * cert, const PrivateKey * sk, unsigned allowed_usages, unsigned cert_issuer_key_type); + espMqttClientSecure & setCertStore(CertStoreBase * certStore); - protected: - espMqttClientInternals::ClientSecureSync _client; + protected: + espMqttClientInternals::ClientSecureSync _client; }; #endif #if defined(ARDUINO_ARCH_ESP32) class espMqttClient : public MqttClientSetup { - public: - explicit espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask); - explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1); + public: + explicit espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1); - protected: - espMqttClientInternals::ClientSync _client; + protected: + espMqttClientInternals::ClientSync _client; }; class espMqttClientSecure : public MqttClientSetup { - public: - explicit espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask); - explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); - espMqttClientSecure& setInsecure(); - espMqttClientSecure& setCACert(const char* rootCA); - espMqttClientSecure& setCertificate(const char* clientCa); - espMqttClientSecure& setPrivateKey(const char* privateKey); - espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey); + public: + explicit espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); + espMqttClientSecure & setInsecure(); + espMqttClientSecure & setCACert(const char * rootCA); + espMqttClientSecure & setCertificate(const char * clientCa); + espMqttClientSecure & setPrivateKey(const char * privateKey); + espMqttClientSecure & setPreSharedKey(const char * pskIdent, const char * psKey); - protected: - espMqttClientInternals::ClientSecureSync _client; + protected: + espMqttClientInternals::ClientSecureSync _client; }; -#endif -#if defined(__linux__) +#elif defined(__linux__) class espMqttClient : public MqttClientSetup { - public: - espMqttClient(); + public: + espMqttClient(); - protected: - espMqttClientInternals::ClientPosix _client; + protected: + espMqttClientInternals::ClientPosix _client; }; +#elif defined(_WIN32) +class espMqttClient : public MqttClientSetup { + public: + espMqttClient(); +}; + #endif diff --git a/lib_standalone/Arduino.cpp b/lib_standalone/Arduino.cpp index 314f01601..d2975fdcf 100644 --- a/lib_standalone/Arduino.cpp +++ b/lib_standalone/Arduino.cpp @@ -17,7 +17,6 @@ #ifdef EMSESP_STANDALONE - #include #include #include @@ -57,8 +56,11 @@ void ClientLoop(void * arg) { } } +#ifndef UNITY_INCLUDE_CONFIG_H +// we have another main that overrides this when using Unity in test_api.cpp int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused))) { setup(); + std::thread t = std::thread(ClientLoop, nullptr); // while (millis() <= 10 * 1000) { while (1) { @@ -69,10 +71,7 @@ int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused)) t.join(); return EXIT_SUCCESS; } - -// unsigned long millis() { -// return __millis; -// } +#endif int64_t esp_timer_get_time() { return __millis; @@ -139,13 +138,13 @@ uint32_t analogReadMilliVolts(uint8_t pin) { return 0; } -void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation){}; -void analogSetAttenuation(adc_attenuation_t attenuation){}; -void dacWrite(uint8_t pin, uint8_t value){}; +void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation) {}; +void analogSetAttenuation(adc_attenuation_t attenuation) {}; +void dacWrite(uint8_t pin, uint8_t value) {}; double ledcSetup(uint8_t chan, double freq, uint8_t bit_num) { return 0; }; -void ledcAttachPin(uint8_t pin, uint8_t chan){}; -void ledcWrite(uint8_t chan, uint32_t duty){}; +void ledcAttachPin(uint8_t pin, uint8_t chan) {}; +void ledcWrite(uint8_t chan, uint32_t duty) {}; #endif \ No newline at end of file diff --git a/lib_standalone/Arduino.h b/lib_standalone/Arduino.h index b500609ab..12a294793 100644 --- a/lib_standalone/Arduino.h +++ b/lib_standalone/Arduino.h @@ -32,6 +32,10 @@ #include #include "WString.h" +#include "Network.h" + +extern ETHClass ETH; +extern WiFiClass WiFi; typedef double double_t; @@ -86,6 +90,21 @@ int vsnprintf_P(char * str, size_t size, const char * format, va_list ap); #define pgm_read_float(addr) (*(const float *)(addr)) #define pgm_read_ptr(addr) (*(const void **)(addr)) +// #if defined(__linux__) +#include // NOLINT [build/c++11] +#include // NOLINT [build/c++11] for yield() +#define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() +// #endif + +int64_t esp_timer_get_time(); + +void delay(unsigned long millis); + +void yield(void); + +void setup(void); +void loop(void); + class Print; class NativeConsole : public Stream { @@ -94,6 +113,11 @@ class NativeConsole : public Stream { } int available() override { +#if defined(_WIN32) + // added for EMS-ESP + return 1; +#else + if (peek_ != -1) return 1; @@ -107,6 +131,7 @@ class NativeConsole : public Stream { timeout.tv_usec = 1000; return ::select(STDIN_FILENO + 1, &rfds, NULL, NULL, &timeout) > 0 ? 1 : 0; +#endif } int read() override { @@ -163,27 +188,6 @@ class NativeConsole : public Stream { int peek_ = -1; }; -#include "Network.h" - extern NativeConsole Serial; -extern ETHClass ETH; -extern WiFiClass WiFi; - -// unsigned long millis(); - -#if defined(__linux__) -#include // NOLINT [build/c++11] -#include // NOLINT [build/c++11] for yield() -#define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() -#endif - -int64_t esp_timer_get_time(); - -void delay(unsigned long millis); - -void yield(void); - -void setup(void); -void loop(void); #endif diff --git a/lib_standalone/Print.h b/lib_standalone/Print.h index d8f40e25e..7247f045e 100644 --- a/lib_standalone/Print.h +++ b/lib_standalone/Print.h @@ -86,7 +86,12 @@ class Print { return print(str); } size_t println() { + // added for EMS-ESP +#if defined(_WIN32) + return print("\n"); +#else return print("\r\n"); +#endif } size_t println(const char * data) { return print(data) + println(); @@ -106,7 +111,7 @@ class Print { size_t println(unsigned long value) { return print(std::to_string(value).c_str()) + println(); } - virtual void flush(){}; + virtual void flush() {}; private: int err_{0};