Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow definition of custom network interfaces #921

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

moritz89
Copy link
Contributor

@moritz89 moritz89 commented Dec 15, 2024

Why:

  • Allow user-defined network interfaces to switch been WiFi and mobile modems during run-time
  • Allow custom classes to be used as the network, ssl and server classes

This change addresses the need by:

  • Creating a new custom WEBSOCKETS_NETWORK_TYPE that expects the user to set network classes
    • WEBSOCKETS_NETWORK_CLASS
    • WEBSOCKETS_NETWORK_SSL_CLASS
    • WEBSOCKETS_NETWORK_SERVER_CLASS

Related issues: #779 #910

@moritz89
Copy link
Contributor Author

moritz89 commented Dec 15, 2024

An open questions are:

  1. which number should be chosen as the NETWORK_CUSTOM define? The next number (10) or something like 99 or 255?
  2. Is NETWORK_CUSTOM the right name?

I would also like to test it with a POC class that toggles between WiFi and TinyGSM, like that published by @Naheel-Azawy : ESP32GSMNetClient before marking this MR as ready.

@Links2004
Copy link
Owner

using the next free number is fine.

not sure if setting some custom defines via compiler option will work, since WEBSOCKETS_NETWORK_TYPE does also switch some code around and handles include of h files.

will be intresting to see how you solve this.
all of this would be much easier if upstream arduino did provide some interface classes for the network stuff, but thats not the case sadly.

@moritz89
Copy link
Contributor Author

moritz89 commented Dec 16, 2024

One option I found that compiles, but isn't pretty is the following. One thing that won't work is using defines to set include paths. Looking into 1-2 other approaches.

lib/arduinoWebSockets/src/WebSockets.h

...
#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_CUSTOM)
#include <../../../src/CustomNetworkClient.h>
#else
#error "no network type selected!"
#endif
...
src/CustomNetworkClient.h

#pragma once

#include <WiFi.h>

#include "managers/network_client.h"

#define SSL_AXTLS
#define WEBSOCKETS_NETWORK_CLASS inamata::NetworkClient
#define WEBSOCKETS_NETWORK_SSL_CLASS inamata::NetworkClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
src/managers/network_client.h

#pragma once

#include <Client.h>
#include <WiFiClient.h>

namespace inamata {

class NetworkClient : public Client {
 public:
  NetworkClient();
  NetworkClient(WiFiClient wifi_client);
  virtual ~NetworkClient() = default;

  int connect(IPAddress ip, uint16_t port) final;
  int connect(const char *host, uint16_t port) final;
  int connect(const char *host, uint16_t port, int32_t timeout);
  size_t write(uint8_t) final;
  size_t write(const uint8_t *buf, size_t size) final;
  size_t write(const char *str);
  int available() final;
  int read() final;
  int read(uint8_t *buf, size_t size) final;
  int peek() final;
  void flush() final;
  void stop() final;
  uint8_t connected() final;
  operator bool() final;

  void setCACert(const char *rootCA);
  void setCACertBundle(const uint8_t *bundle);
  void setInsecure();
  bool verify(const char *fingerprint, const char *domain_name);
};

}  // namespace inamata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants