Skip to content

Commit c816e4c

Browse files
authored
SoftAP support for LWiFi library (#67)
This feature adds LWiFi.softAP() method that allows user to switch LinkIt 7697 into a simple Wi-Fi access point. Calling LWiFi.softAPdisconnect() or LWiFi.begin() to switch back to STA mode, which connects to other access points. To do so: * SoftAP implementation using `AP_ONLY` mode in LinkIt SDK. * Patch `middelware/MTK/dhcpd` of LinkIT SDK to prevent include path collision. In Arduino projects, we need to prevent adding "lwip/inc/lwip" directly into compiler include pathes. Failing to do so would cause confusions between Arduino "Udp.h" and lwip "lwip/udp.h". As a result, we change the include path in `dhcpd.h` to `lwip/ipv4.h" * Clean up Wi-Fi initialization routines. BLE, STA and AP all initializes (`wifi_init()`) when necessary, resulting in redundant code. We refactor the Wi-Fi initializtion routines into a common function `init_gloabal_connsys` in `variant.h`. Also clean up some formatting issues and add some more documentation to softAP APIs. Since we always initialize Wi-Fi in STA mode, we have to use `wifi_config_*` APIs to update SoftAP settings, instead of relying on `wifi_init` and `lwip_tcpip_init`. * Add example codes for `softAP()` and `softAPdisconnect()` usages.
1 parent 10d0190 commit c816e4c

File tree

14 files changed

+1056
-435
lines changed

14 files changed

+1056
-435
lines changed

middleware/MTK/dhcpd/inc/dhcpd.h

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/* Copyright Statement:
2+
*
3+
* (C) 2005-2016 MediaTek Inc. All rights reserved.
4+
*
5+
* This software/firmware and related documentation ("MediaTek Software") are
6+
* protected under relevant copyright laws. The information contained herein
7+
* is confidential and proprietary to MediaTek Inc. ("MediaTek") and/or its licensors.
8+
* Without the prior written permission of MediaTek and/or its licensors,
9+
* any reproduction, modification, use or disclosure of MediaTek Software,
10+
* and information contained herein, in whole or in part, shall be strictly prohibited.
11+
* You may only use, reproduce, modify, or distribute (as applicable) MediaTek Software
12+
* if you have agreed to and been bound by the applicable license agreement with
13+
* MediaTek ("License Agreement") and been granted explicit permission to do so within
14+
* the License Agreement ("Permitted User"). If you are not a Permitted User,
15+
* please cease any access or use of MediaTek Software immediately.
16+
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
17+
* THAT MEDIATEK SOFTWARE RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES
18+
* ARE PROVIDED TO RECEIVER ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
19+
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
20+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
21+
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
22+
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
23+
* SUPPLIED WITH MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
24+
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
25+
* THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
26+
* CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
27+
* SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
28+
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
29+
* CUMULATIVE LIABILITY WITH RESPECT TO MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
30+
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE MEDIATEK SOFTWARE AT ISSUE,
31+
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
32+
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
33+
*/
34+
35+
#ifndef __DHCPD_H__
36+
#define __DHCPD_H__
37+
#include "lwip/ip4_addr.h"
38+
#include "syslog.h"
39+
40+
41+
/**
42+
* @addtogroup DHCPD
43+
* @{
44+
* This section introduces the DHCP daemon (DHCPD) APIs including details on how to use this module, structures and functions.
45+
* It's a simple DHCP daemon to operate in soft AP mode for assigning IP addresses to connected Wi-Fi station nodes.
46+
*
47+
* @section DHCPD_Terms_Chapter Terms and acronyms
48+
*
49+
* |Terms |Details |
50+
* |------------------------------|------------------------------------------------------------------------|
51+
* |\b DHCP | Dynamic Host Configuration Protocol. |
52+
* |\b Wi-Fi | Wi-Fi is a local area wireless computer networking technology, https://en.wikipedia.org/wiki/Wi-Fi |
53+
*
54+
* @section DHCPD_Usage_Chapter How to use this module
55+
*
56+
* - Step1: Set DHCPD parameters.
57+
* If you want to use all default DHCPD parameters (DHPCD_DEFAULT_X, such as DHPCD_DEFAULT_SERVER_IP),
58+
* skip this step.
59+
* - Step2: Call #dhcpd_start() to start the service.
60+
* If you want to use all default DHCPD parameters (DHPCD_DEFAULT_X, such as DHPCD_DEFAULT_SERVER_IP),
61+
* just call #dhcpd_start(NULL).
62+
* - Sample code:
63+
* @code
64+
* xTaskHandle dhcpd_task;
65+
* int32_t process_softap(void)
66+
* {
67+
* LOG_I(common, "Now start DHCPD:");
68+
* dhcpd_settings_t dhcpd_settings;
69+
*
70+
* memset(&dhcpd_settings, 0, sizeof(dhcpd_settings_t));
71+
* strncpy(dhcpd_settings.dhcpd_server_address, "10.10.10.2" , IP4ADDR_STRLEN_MAX - 1);
72+
* strncpy(dhcpd_settings.dhcpd_gateway, "10.10.10.1" , IP4ADDR_STRLEN_MAX - 1);
73+
* strncpy(dhcpd_settings.dhcpd_netmask, "255.255.255.0" , IP4ADDR_STRLEN_MAX - 1);
74+
* strncpy(dhcpd_settings.dhcpd_primary_dns, "8.8.8.8" , IP4ADDR_STRLEN_MAX - 1);
75+
* strncpy(dhcpd_settings.dhcpd_secondary_dns, "8.8.4.4" , IP4ADDR_STRLEN_MAX - 1);
76+
* strncpy(dhcpd_settings.dhcpd_ip_pool_start, "10.10.10.3" , IP4ADDR_STRLEN_MAX - 1);
77+
* strncpy(dhcpd_settings.dhcpd_ip_pool_end, "10.10.10.11" , IP4ADDR_STRLEN_MAX - 1);
78+
*
79+
* dhcpd_start(&dhcpd_settings);
80+
*
81+
* return 0;
82+
* }
83+
*
84+
* @endcode
85+
*/
86+
87+
88+
#ifdef __cplusplus
89+
extern "C" {
90+
#endif
91+
92+
93+
/** @defgroup dhcpd_define Define
94+
* @{
95+
*/
96+
97+
98+
/** @brief This macro enables the feature of saving the client configuration when AP is keeping power on.
99+
* Client configuration will not be stored when AP is power off.
100+
*/
101+
#define DHCPD_SAVE_CLIENT_CONFIG_ON_LINE
102+
103+
104+
/** @brief This macro defines default IP lease time in seconds.
105+
*/
106+
#define DHCPD_DEFAULT_LEASE_TIME (64800)
107+
108+
109+
/** @brief This macro defines default server IP for AP.
110+
*/
111+
#define DHPCD_DEFAULT_SERVER_IP ("10.10.10.1")
112+
113+
114+
/** @brief This macro defines default gateway IP for AP.
115+
*/
116+
#define DHPCD_DEFAULT_GATEWAY ("10.10.10.1")
117+
118+
119+
/** @brief This macro defines default netmask for AP.
120+
*/
121+
#define DHPCD_DEFAULT_NETMASK ("255.255.255.0")
122+
123+
124+
/** @brief This macro defines default primary DNS IP for AP.
125+
*/
126+
#define DHPCD_DEFAULT_PRIMARY_DNS ("8.8.8.8")
127+
128+
129+
/** @brief This macro defines default secondary DNS IP for AP.
130+
*/
131+
#define DHPCD_DEFAULT_SECONDARY_DNS ("8.8.4.4")
132+
133+
134+
/** @brief This macro defines default starting IP for IP pool.
135+
*/
136+
#define DHPCD_DEFAULT_IP_POOL_START ("10.10.10.2")
137+
138+
139+
/** @brief This macro defines default ending IP for IP pool.
140+
*/
141+
#define DHPCD_DEFAULT_IP_POOL_END ("10.10.10.10")
142+
143+
/**
144+
* @}
145+
*/
146+
147+
148+
/** @defgroup dhcpd_struct Struct
149+
* @{
150+
*/
151+
152+
/** @brief This structure defines the DHCPD configuration structure. For more information, please refer to #dhcpd_start() */
153+
typedef struct
154+
{
155+
char dhcpd_server_address[IP4ADDR_STRLEN_MAX]; /**< Specify server IP for AP. */
156+
char dhcpd_gateway[IP4ADDR_STRLEN_MAX]; /**< Specify gateway IP for AP. */
157+
char dhcpd_netmask[IP4ADDR_STRLEN_MAX]; /**< Specify netmask for AP. */
158+
char dhcpd_primary_dns[IP4ADDR_STRLEN_MAX]; /**< Specify primary DNS IP for AP. */
159+
char dhcpd_secondary_dns[IP4ADDR_STRLEN_MAX]; /**< Specify secondary DNS IP for AP. */
160+
char dhcpd_ip_pool_start[IP4ADDR_STRLEN_MAX]; /**< Specify starting IP for IP pool. */
161+
char dhcpd_ip_pool_end[IP4ADDR_STRLEN_MAX]; /**< Specify ending IP for IP pool. */
162+
} dhcpd_settings_t;
163+
164+
/**
165+
* @}
166+
*/
167+
168+
169+
/**
170+
* @brief This function is used to start the DHCPD service. It creates a new task for the DHCPD, then returns. The DHCPD task runs until dhcpd_stop() is called.
171+
* @param[in] dhcpd_settings specifies the settings for DHCPD, such as DHCP server IP, gateway, netmask, DNS and IP pool. These settings can be set individually.
172+
* If the setting is not set, default value will be used. Please beware that if both default values and customer configured value are used, they should
173+
* match to each other.
174+
* @return
175+
* @sa #dhcpd_stop();
176+
*/
177+
int dhcpd_start(dhcpd_settings_t *dhcpd_settings);
178+
179+
180+
/**
181+
* @brief This function is used to stop the DHCPD service. It kills the DHCPD task and stops the DHCPD service immediately.
182+
* @return
183+
* @sa #dhcpd_start();
184+
*/
185+
void dhcpd_stop(void);
186+
187+
/**
188+
* @}
189+
*/
190+
191+
#ifdef __cplusplus
192+
}
193+
#endif
194+
195+
#endif /* __DHCPD_H__ */

middleware/third_party/arduino/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ include $(SOURCE_DIR)/middleware/MTK/bluetooth/module.mk
6969

7070
# WiFi files
7171
include $(SOURCE_DIR)/middleware/MTK/wifi_service/combo/module.mk
72+
CXXFLAGS += -I$(SOURCE_DIR)/middleware/MTK/wifi_service/combo/inc
7273

7374
CXXFLAGS += -fno-rtti
7475

middleware/third_party/arduino/hardware/arduino/mt7697/cores/arduino/IPAddress.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,15 @@ size_t IPAddress::printTo(Print& p) const
7272
n += p.print(_address.bytes[3], DEC);
7373
return n;
7474
}
75+
76+
String IPAddress::toString() const {
77+
String p;
78+
size_t n = 0;
79+
for (int i =0; i < 3; i++)
80+
{
81+
p += String(_address.bytes[i], DEC);
82+
p += String('.');
83+
}
84+
p += String(_address.bytes[3], DEC);
85+
return p;
86+
}

middleware/third_party/arduino/hardware/arduino/mt7697/cores/arduino/IPAddress.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define IPAddress_h
2222

2323
#include <stdint.h>
24+
#include "WString.h"
2425
#include "Printable.h"
2526

2627
// A class to make it easier to handle and pass around IP addresses
@@ -60,6 +61,7 @@ class IPAddress : public Printable {
6061
IPAddress& operator=(uint32_t address);
6162

6263
virtual size_t printTo(Print& p) const;
64+
virtual String toString() const;
6365

6466
friend class EthernetClass;
6567
friend class UDP;

middleware/third_party/arduino/hardware/arduino/mt7697/libraries/LBLE/src/utility/ard_ble.c

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,11 @@
2727
#include <lwip/dns.h>
2828
#include <ethernetif.h>
2929

30-
// declared in Arduino core's "variant.h"
31-
extern void set_wifi_ready(void);
32-
extern bool wifi_ready(void);
33-
34-
static int32_t _wifi_event_handler(wifi_event_t event, uint8_t* payload, uint32_t length)
35-
{
36-
switch(event)
37-
{
38-
case WIFI_EVENT_IOT_INIT_COMPLETE:
39-
set_wifi_ready();
40-
pr_debug("event=0x%x, payload=0x%p, length=0x%x", (unsigned int)event, payload, (unsigned int)length);
41-
break;
42-
}
43-
44-
return 1;
45-
}
46-
4730
static void _connsys_workaround()
4831
{
49-
/* Wi-Fi must be initialized for BLE start-up */
50-
if(!wifi_ready())
51-
{
52-
wifi_connection_register_event_handler(WIFI_EVENT_IOT_INIT_COMPLETE, _wifi_event_handler);
53-
54-
wifi_config_t config = {0};
55-
config.opmode = WIFI_MODE_STA_ONLY;
56-
wifi_init(&config, NULL);
57-
58-
lwip_tcpip_config_t tcpip_config = {{0}, {0}, {0}, {0}, {0}, {0}};
59-
lwip_tcpip_init(&tcpip_config, WIFI_MODE_STA_ONLY);
60-
}
32+
// Wi-Fi must be initialized for BLE start-up
33+
// declared in Arduino core's "variant.h"
34+
init_global_connsys();
6135
}
6236

6337
/*
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <LWiFi.h>
2+
#include <WiFiClient.h>
3+
// #include <LWebServer.h>
4+
5+
/* Set these to your desired credentials. */
6+
const char *ssid = "LinkItAP";
7+
const char *password = "yourpassword";
8+
9+
WiFiServer server(80);
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
Serial.println();
14+
Serial.print("Configuring access point...");
15+
16+
/* You can remove the password parameter if you want the AP to be open. */
17+
WiFi.softAP(ssid, password);
18+
IPAddress myIP = WiFi.softAPIP();
19+
Serial.println("AP ready.");
20+
Serial.print("Connect to AP ");
21+
Serial.print(ssid);
22+
Serial.print(" and visit http://");
23+
Serial.println(myIP);
24+
25+
Serial.print("AP MAC=");
26+
Serial.println(WiFi.softAPmacAddress());
27+
28+
server.begin();
29+
}
30+
31+
void loop() {
32+
// server.handleClient();
33+
// listen for incoming clients
34+
WiFiClient client = server.available();
35+
if (client) {
36+
// an http request ends with a blank line
37+
boolean currentLineIsBlank = true;
38+
while (client.connected()) {
39+
if (client.available()) {
40+
char c = client.read();
41+
Serial.write(c);
42+
// if you've gotten to the end of the line (received a newline
43+
// character) and the line is blank, the http request has ended,
44+
// so you can send a reply
45+
if (c == '\n' && currentLineIsBlank) {
46+
// send a standard http response header
47+
client.println("HTTP/1.1 200 OK");
48+
client.println("Content-Type: text/html");
49+
client.println("Connection: close"); // the connection will be closed after completion of the response
50+
client.println();
51+
client.println("<!DOCTYPE HTML>");
52+
client.println("<html>");
53+
client.println("<p>Hello from LinkIt 7697!</p>");
54+
client.println("<p>Wi-Fi SSID: ");
55+
client.print(ssid);
56+
client.println("</p>");
57+
client.print("<p>Connected STA #: ");
58+
client.print((int)WiFi.softAPgetStationNum());
59+
client.println("</p>");
60+
client.println("</html>");
61+
break;
62+
}
63+
if (c == '\n') {
64+
// you're starting a new line
65+
currentLineIsBlank = true;
66+
} else if (c != '\r') {
67+
// you've gotten a character on the current line
68+
currentLineIsBlank = false;
69+
}
70+
}
71+
}
72+
// give the web browser time to receive the data
73+
delay(300);
74+
75+
// close the connection:
76+
client.stop();
77+
Serial.println("client disonnected");
78+
}
79+
}

0 commit comments

Comments
 (0)