skaarj1989 / mWebSockets

WebSockets for microcontrollers
https://skaarj1989.github.io/mWebSockets/autobahn-testsuite/servers/
MIT License
108 stars 23 forks source link

Solution to #59: 'strtok_r' was not declared in this scope, 'strcasecmp' was not declared in this scope #62

Closed gigaj0ule closed 1 year ago

gigaj0ule commented 1 year ago

I found a better solution

We must #include at the top of utility.h

It's not guaranteed to be automatically included in every arduino environment

In some, it is. In other environments, it's not.

#pragma once

#include "platform.h"
#include <strings.h>

#define SAFE_DELETE(ptr)                                                       \
  {                                                                            \
    if (ptr != nullptr) {                                                      \
      delete ptr;                                                              \
      ptr = nullptr;                                                           \
    }                                                                          \
  }

#define SAFE_DELETE_ARRAY(ptr)                                                 \
  {                                                                            \
    if (ptr != nullptr) {                                                      \
      delete[] ptr;                                                            \
      ptr = nullptr;                                                           \
    }                                                                          \
  }

#ifdef _DEBUG
#  define __debugOutput printf
#else
#  define __debugOutput(...)
#endif

void printf(const __FlashStringHelper *fmt, ...);

namespace net {

IPAddress fetchRemoteIp(const NetClient &);

} // namespace net
gigaj0ule commented 1 year ago

We can also put it in "platform.h" just under stdint.h

#pragma once

/** @file */

#include <stdint.h>
#include <strings.h>

//
// Board architectures:
//

/** @cond */
#define PLATFORM_ARCHITECTURE_AVR 1
#define PLATFORM_ARCHITECTURE_ESP8266 2
#define PLATFORM_ARCHITECTURE_ESP32 3
#define PLATFORM_ARCHITECTURE_SAM 4
#define PLATFORM_ARCHITECTURE_SAMD21 5
#define PLATFORM_ARCHITECTURE_STM32 6
/** @endcond */

#if defined(__AVR__)
#  define PLATFORM_ARCH PLATFORM_ARCHITECTURE_AVR
#elif defined(ESP8266)
#  define PLATFORM_ARCH PLATFORM_ARCHITECTURE_ESP8266
#elif defined(ESP32)
#  define PLATFORM_ARCH PLATFORM_ARCHITECTURE_ESP32
#elif defined(ARDUINO_ARCH_SAM)
#  define PLATFORM_ARCH PLATFORM_ARCHITECTURE_SAM
#elif defined(ARDUINO_ARCH_SAMD)
#  define PLATFORM_ARCH PLATFORM_ARCHITECTURE_SAMD21
#elif defined(ARDUINO_ARCH_STM32)
#  define PLATFORM_ARCH PLATFORM_ARCHITECTURE_STM32
#else
#  error "Unsupported platform"
#endif

//
// Ethernet/WiFi devices:
//

/** @cond */
#define ETHERNET_CONTROLLER_W5X00 1
#define ETHERNET_CONTROLLER_ENC28J60 2
#define NETWORK_CONTROLLER_WIFI 3
/** @endcond */

#include "config.h"

#if NETWORK_CONTROLLER == ETHERNET_CONTROLLER_W5X00
#  include <Ethernet.h>
/** Specifies maximum number of clients connected to server. */
constexpr uint8_t kMaxConnections{MAX_SOCK_NUM};
#elif NETWORK_CONTROLLER == ETHERNET_CONTROLLER_ENC28J60
#  include <EthernetENC.h>
constexpr uint8_t kMaxConnections{4};
#elif NETWORK_CONTROLLER == NETWORK_CONTROLLER_WIFI
#  if PLATFORM_ARCH == PLATFORM_ARCHITECTURE_ESP8266
#    include <ESP8266WiFi.h>
#  else
#    include <WiFi.h>
#  endif
#  include <WiFiClient.h>
#  include <WiFiServer.h>
constexpr uint8_t kMaxConnections{8};
#else
#  error "Network controller is required!"
#endif

/** @cond */
#if NETWORK_CONTROLLER == NETWORK_CONTROLLER_WIFI
using NetClient = WiFiClient;
using NetServer = WiFiServer;
#else
using NetClient = EthernetClient;
using NetServer = EthernetServer;
#endif
/** @endcond */

/**
 * @def PLATFORM_ARCH
 * @def NETWORK_CONTROLLER
 */
skaarj1989 commented 1 year ago

You mean string.h?

fatal error: strings.h: No such file or directory
 #include <strings.h>
gigaj0ule commented 1 year ago

Huh, now this is interesting...

does not contain strcasecmp, but does https://stackoverflow.com/questions/4291149/difference-between-string-h-and-strings-h is not included in your environment, and that is puzzling since arduino uses GCC under the hood, and is part of the GCC distribution! Maybe there is a -D #if defined() flag we can use to detect the compiler environment and optionally include
skaarj1989 commented 1 year ago

There is a solution (requires C++17)

#if __has_include("strings.h")
# include <strings.h>
#endif
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.