Instrument Neutral Distributed Interface INDI 2.2.5
pegasus_wifi.h
Go to the documentation of this file.
1/*******************************************************************************
2 Pegasus Astro WiFi settings
3
4 Copyright (C) 2026 Dominik Merkel
5
6 Shared by the drivers of Pegasus Astro devices with the common WiFi command set.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21*******************************************************************************/
22
23#pragma once
24
25#include "indipropertynumber.h"
26#include "indipropertyswitch.h"
27#include "indipropertytext.h"
28
29#include <cstdio>
30#include <functional>
31#include <string>
32#include <sys/types.h>
33
34namespace INDI
35{
36class DefaultDevice;
37}
38
39namespace PegasusAstro
40{
41
54class WiFi
55{
56 public:
58 using LoadConfig = std::function<void(const char *)>;
59 using SaveConfig = std::function<void(INDI::Property &)>;
60
62
64 void initProperties(const char *tab, LoadConfig loadConfig, SaveConfig saveConfig);
65
67 void ISGetProperties();
68
70 void updateProperties(bool connected, int portFD);
71
72 bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n);
73 bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n);
74 bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n);
75
77 void saveConfigItems(FILE *fp);
78
80 bool login(int portFD);
81
86 static void drain(int portFD);
87
89 void updateConnection();
90
95 void allowOpenNetworks(bool allow)
96 {
97 m_AllowOpenNetworks = allow;
98 }
99
101 static constexpr int TCP_PORT {9999};
102
103 private:
105 bool sendCommand(const char *cmd, char *res, int len, int timeout = 3, bool secret = false);
106 bool readHotspotStatus();
107 bool readHotspotCredentials();
108 bool readChannel();
109 bool readConnection();
110 bool scan();
112 static std::string formatScan(const char *reply);
113 bool setHotspotCredentials(const char *ssid, const char *password);
114 bool connectToNetwork(const char *ssid, const char *password);
115 static bool validText(const char *text, size_t maxLength);
116 static void readConnectionLater(void *context);
117
118 INDI::DefaultDevice *m_Device {nullptr};
119 LoadConfig m_LoadConfig;
120 SaveConfig m_SaveConfig;
121 int m_PortFD {-1};
122 bool m_PasswordLoaded {false};
123 bool m_Connected {false};
124 bool m_AllowOpenNetworks {false};
125
126 INDI::PropertySwitch HotspotSP {2};
127 INDI::PropertyText HotspotTP {2};
128 INDI::PropertyNumber ChannelNP {1};
129 INDI::PropertyText ConnectionTP {1};
130 INDI::PropertySwitch ScanSP {1};
131 INDI::PropertyText ScanTP {1};
132 INDI::PropertyText NetworkTP {2};
133 INDI::PropertySwitch ConnectSP {1};
134 INDI::PropertyText PasswordTP {1};
135
136 static constexpr int LEN {128};
137 static constexpr int SCAN_LEN {4096};
139 static constexpr int SCAN_TIMEOUT {16};
140 static constexpr int SCAN_RETRIES {4};
142 static constexpr int CONNECT_CHECK_MS {10000};
143 static constexpr useconds_t SCAN_RETRY_DELAY_US {3000000};
144 static constexpr const char *DEFAULT_PASSWORD {"12345678"};
145 static constexpr const char *OPEN_NETWORK_PASSWORD {"PASSWORD"};
146};
147
148}
hid_device * device
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Provides generic container for INDI properties.
static constexpr int TCP_PORT
Port of the TCP command socket.
void updateConnection()
Read the current WiFi connection again, e.g. on every poll. Does nothing while not connected.
void saveConfigItems(FILE *fp)
Save the TCP password; the device stores its WiFi settings itself.
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
void ISGetProperties()
Define the TCP password and load it once from the configuration. Call from ISGetProperties.
std::function< void(INDI::Property &)> SaveConfig
void initProperties(const char *tab, LoadConfig loadConfig, SaveConfig saveConfig)
Create the properties: the WiFi settings on tab, the TCP password on the connection tab.
static void drain(int portFD)
Discard pending input. tcflush() only works on a serial port, so over TCP a line end left behind by a...
bool login(int portFD)
Log in on the TCP command socket with the hotspot password.
void updateProperties(bool connected, int portFD)
Define the WiFi properties and read the settings, or delete them on disconnect.
bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
std::function< void(const char *)> LoadConfig
WiFi(INDI::DefaultDevice *device)
void allowOpenNetworks(bool allow)
Allow joining an open network with an empty password. The device then gets the placeholder password "...
ISState
Switch state.
Definition indiapi.h:148
*brief user defined string literal for JSON values *sa std::size_t n
Namespace to encapsulate INDI client, drivers, and mediator classes.
uint8_t cmd[4]
Definition pwc-ioctl.h:2