arduino-emulator
Loading...
Searching...
No Matches
WiFi.h
1/*
2 WiFi.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#pragma once
20
21#include "WiFiClient.h"
22#include "WiFiClientSecure.h"
23#include "WiFiServer.h"
24
25#include <string.h>
26#if defined(_WIN32)
27# include <winsock2.h>
28# include <ws2tcpip.h>
29#else
30# include <netdb.h>
31# include <netinet/in.h>
32# include <sys/socket.h>
33#endif
34
41
42namespace arduino {
43
44typedef enum { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 } WiFiMode_t; // For ESP compatibility
45
46enum wifi_ps_type_t { WIFI_PS_NONE, WIFI_PS_MIN_MODEM, WIFI_PS_MAX_MODEM };
47
48class WifiMock {
49 public:
50 virtual void begin(const char* name, const char* pwd) {
51 // nothing to be done
52 }
53
54 // we assume the network to be available on a desktop or host machine
55 wl_status_t status() { return WL_CONNECTED; }
56
57 IPAddress& localIP() {
58 SocketImpl sock;
59 adress.fromString(sock.getIPAddress());
60 return adress;
61 }
62
63 void setSleep(bool) {}
64
65 void setSleep(wifi_ps_type_t) {}
66
67 int macAddress() { return mac; }
68
69 void setClientInsecure() {}
70
71 // WIFI_STA is the best approximation to interface enabled
72 void mode(WiFiMode_t m) { wifi_mode = m; }
73 WiFiMode_t getMode() { return wifi_mode; };
74
77 bool softAP(const char* ssid, const char* passphrase = nullptr,
78 int channel = 1, int ssid_hidden = 0, int max_connection = 4) {
79 wifi_mode = WIFI_AP;
80 return true;
81 }
82 bool softAPdisconnect(bool wifioff = false) {
83 wifi_mode = WIFI_STA;
84 return true;
85 }
86 IPAddress& softAPIP() { return localIP(); }
87
89 int hostByName(const char* host, IPAddress& result) {
90 if (host == nullptr) return 0;
91 if (result.fromString(host)) return 1;
92 struct addrinfo hints;
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_family = AF_INET;
95 struct addrinfo* info = nullptr;
96 if (getaddrinfo(host, nullptr, &hints, &info) != 0 || info == nullptr) return 0;
97 const uint8_t* ip = reinterpret_cast<const uint8_t*>(
98 &reinterpret_cast<struct sockaddr_in*>(info->ai_addr)->sin_addr.s_addr);
99 result = IPAddress(ip[0], ip[1], ip[2], ip[3]);
100 freeaddrinfo(info);
101 return 1;
102 }
103
104 protected:
105 IPAddress adress;
106 int mac = 0;
107 WiFiMode_t wifi_mode = WIFI_STA;
108};
109
110extern WifiMock WiFi;
111
112} // namespace arduino
Definition IPAddress.h:43
Definition IPAddress.h:43
Definition SocketImpl.h:30
Definition WiFi.h:48
int hostByName(const char *host, IPAddress &result)
Resolves a host name (or an IP address in text form) to an IPv4 address.
Definition WiFi.h:89
bool softAP(const char *ssid, const char *passphrase=nullptr, int channel=1, int ssid_hidden=0, int max_connection=4)
Definition WiFi.h:77
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31