Skip to content

Commit 3efbcb6

Browse files
Switch to the new wifi library
This removes the old wifi code entirely and instead links to the new library at https://github.com/Pinoccio/library-gainspan-s2w. To use this library after this commit, you should include that library in the IDE libraries path as well. Additionally, this new library is initialized to use SPI to talk to the backpacks (which requires the SPI version of the firmware on the gainspan module). To use UART for modules that still have the UART firmware, see the comments in WifiBackpack::setup(). This commit disabled the wifi verbose mode for now, since the new wifi library only has some compile-time verbosity switches. Furthermore, the new wifi library does not yet set the L4 (TCP) retry count for the HQ connection and does not wait for the association to complete at startup.
1 parent 513a6c5 commit 3efbcb6

16 files changed

+101
-2426
lines changed

ScoutHandler.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ PinoccioScoutHandler::~PinoccioScoutHandler() { }
1515

1616
void PinoccioScoutHandler::setup() {
1717
if (Scout.isLeadScout()) {
18-
Gainspan.connectEventHandler = hqConnectHandler;
19-
Gainspan.disconnectEventHandler = hqDisconnectHandler;
2018

2119
Scout.enableBackpackVcc();
2220
Serial.print("Wi-Fi backpack connecting...");
2321
Scout.wifi.setup();
24-
Scout.wifi.init();
22+
Scout.wifi.autoConnect();
2523
Serial.println("Done");
2624
RgbLed.blinkGreen();
2725

@@ -49,14 +47,6 @@ void PinoccioScoutHandler::loop() {
4947
}
5048
}
5149

52-
static void hqConnectHandler(uint8_t cid) {
53-
leadHQConnect(cid);
54-
}
55-
56-
static void hqDisconnectHandler(uint8_t cid) {
57-
Serial.println("Disconnected from HQ");
58-
}
59-
6050
static bool fieldCommands(NWK_DataInd_t *ind) {
6151
int total, ret;
6252
RgbLed.blinkGreen(200);
@@ -230,7 +220,7 @@ static void leadAnnouncementSend(int chan, int from, char *message) {
230220
////////////////////
231221
// lead scout stuff
232222

233-
void leadHQConnect(uint8_t cid) {
223+
void leadHQConnect() {
234224
char auth[256], token[33];
235225

236226
if (Scout.wifi.client.connected()) {

ScoutHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void leadAnnouncementSend(int chan, int from, char *message);
6363
static void leadHQ(void);
6464
static void leadSignal(char *json);
6565
static void leadIncoming(char *packet, unsigned short *index);
66-
static void leadHQConnect(uint8_t cid);
66+
void leadHQConnect();
6767

6868
// this is called on the main loop to try to (re)connect to the HQ
6969
static void leadHQHandle(void);

Shell.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -592,58 +592,53 @@ static numvar setEventVerbose(void) {
592592
\****************************/
593593
static numvar wifiReport(void) {
594594
if (getarg(0) > 0 && getarg(1) == 1) {
595-
Scout.wifi.printProfiles();
595+
Scout.wifi.printProfiles(Serial);
596596
} else {
597-
Serial.print("Wi-Fi App Version: ");
598-
Serial.println(Gainspan.getAppVersion());
599-
Serial.print("Wi-Fi GEPS Version: ");
600-
Serial.println(Gainspan.getGepsVersion());
601-
Serial.print("Wi-Fi Wlan Version: ");
602-
Serial.println(Gainspan.getWlanVersion());
603-
Scout.wifi.printCurrentNetworkStatus();
597+
Serial.print("Wi-Fi Versions: ");
598+
Scout.wifi.printFirmwareVersions(Serial);
599+
Scout.wifi.printCurrentNetworkStatus(Serial);
604600
}
605601
}
606602

607603
static numvar wifiList(void) {
608-
Scout.wifi.printAPs();
604+
Scout.wifi.printAPs(Serial);
609605
}
610606

611607
static numvar wifiConfig(void) {
612-
String port = String(getarg(4));
613-
if (!Scout.wifi.apConfig((const char *)getstringarg(1), (const char *)getstringarg(2), (const char *)getstringarg(3), port)) {
608+
if (!Scout.wifi.autoConfig((const char *)getstringarg(1), (const char *)getstringarg(2), (const char *)getstringarg(3), getarg(4))) {
614609
Serial.println("Error: saving Scout.wifi.configuration data failed");
615610
}
616611
}
617612

618613
static numvar wifiConnect(void) {
619614
Serial.print("Wi-Fi backpack connecting...");
620-
if (!Scout.wifi.apConnect()) {
615+
if (!Scout.wifi.autoConnect()) {
621616
Serial.println("Error: unable to connect");
622617
} else {
623618
Serial.println("Done");
624619
}
625620
}
626621

627622
static numvar wifiCommand(void) {
628-
if (!Scout.wifi.runDirectCommand((const char *)getstringarg(1))) {
623+
if (!Scout.wifi.runDirectCommand(Serial, (const char *)getstringarg(1))) {
629624
Serial.println("Error: Wi-Fi direct command failed");
630625
}
631626
}
632627

633628
static numvar wifiPing(void) {
634-
if (!Scout.wifi.ping((const char *)getstringarg(1))) {
629+
if (!Scout.wifi.ping(Serial, (const char *)getstringarg(1))) {
635630
Serial.println("Error: Wi-Fi ping command failed");
636631
}
637632
}
638633

639634
static numvar wifiDNSLookup(void) {
640-
if (!Scout.wifi.dnsLookup((const char *)getstringarg(1))) {
635+
if (!Scout.wifi.dnsLookup(Serial, (const char *)getstringarg(1))) {
641636
Serial.println("Error: Wi-Fi DNS lookup command failed");
642637
}
643638
}
644639

645640
static numvar wifiGetTime(void) {
646-
if (!Scout.wifi.getTime()) {
641+
if (!Scout.wifi.printTime(Serial)) {
647642
Serial.println("Error: Wi-Fi NTP time lookup command failed");
648643
}
649644
}
@@ -661,7 +656,7 @@ static numvar wifiWakeup(void) {
661656
}
662657

663658
static numvar wifiVerbose(void) {
664-
Gainspan.debugAutoConnect = getarg(1);
659+
// TODO
665660
}
666661

667662
/****************************\

examples/Begin/Begin.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <SPI.h>
22
#include <Wire.h>
33
#include <Scout.h>
4+
#include <GS.h>
45

56
/* MANUAL PROVISIONING BITLASH COMMANDS
67
mesh.config(<Scout ID>, <Troop ID>)

utility/WiFiBackpack.cpp

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,111 @@
11
#include <Arduino.h>
2+
#include <SPI.h>
23
#include <utility/WiFiBackpack.h>
4+
#include "../ScoutHandler.h"
35

4-
//#define P_BACKPACK_WIFI_DEBUG
5-
#ifdef P_BACKPACK_WIFI_DEBUG
6-
# define WD(x) x
7-
#else
8-
# define WD(x)
9-
#endif
6+
static void print_line(const uint8_t *buf, uint16_t len, void *data) {
7+
static_cast<Print*>(data)->write(buf, len);
8+
static_cast<Print*>(data)->println();
9+
}
1010

11-
WiFiBackpack::WiFiBackpack() { }
11+
WiFiBackpack::WiFiBackpack() : client(gs) { }
1212

1313
WiFiBackpack::~WiFiBackpack() { }
1414

1515
bool WiFiBackpack::setup() {
1616
Backpack::setup();
1717

18-
WD(Serial.println("WiFiBackpack::setup 1"));
19-
20-
if (!Gainspan.setup()) {
21-
WD(Serial.println("FAIL: Setup failed"));
22-
return 0;
23-
}
24-
25-
WD(Serial.println("WiFiBackpack::setup 2"));
26-
return 1;
27-
}
28-
29-
bool WiFiBackpack::init() {
18+
// Alternatively, use the UART for Wifi backpacks that still have the
19+
// UART firmware running on them
20+
// Serial1.begin(115200);
21+
// return gs.begin(Serial1);
3022

31-
WD(Serial.println("WiFiBackpack::init 1"));
23+
SPI.begin();
24+
SPI.setClockDivider(SPI_CLOCK_DIV8);
3225

33-
if (!Gainspan.init()) {
34-
WD(Serial.println("Error: no response from Wi-Fi backpack"));
35-
return 0;
36-
}
37-
WD(Serial.println("WiFiBackpack::init 2"));
38-
client.autoConnect();
39-
WD(Serial.println("WiFiBackpack::init 3"));
40-
41-
return 1;
26+
return gs.begin(7);
4227
}
4328

4429
void WiFiBackpack::loop() {
4530
Backpack::loop();
46-
Gainspan.process();
31+
client = gs.getNcmCid();
32+
if (!client.connected()) {
33+
hqConnected = false;
34+
} else if (!hqConnected) {
35+
leadHQConnect();
36+
hqConnected = true;
37+
}
38+
// TODO: Don't call leadHQConnect directly
39+
// TODO: There is a race condition here: If a disconnect and connect
40+
// happen quickly before we can notice the disconnect, leadHqConnect
41+
// will not be called for the new connection.
4742
}
4843

49-
bool WiFiBackpack::apConfig(const char *ssid, const char *passphrase, String host, String port) {
50-
String ip;
51-
if (client.connected()) {
52-
ip = Gainspan.dnsLookup(host);
53-
} else {
54-
ip = host;
55-
}
44+
bool WiFiBackpack::autoConfig(const char *ssid, const char *passphrase, const String& host, uint16_t port) {
45+
bool ok = true;
46+
ok = ok && gs.setSecurity(GSModule::GS_SECURITY_AUTO);
47+
ok = ok && gs.setWpaPassphrase(passphrase);
48+
ok = ok && gs.setAutoAssociate(ssid);
49+
ok = ok && gs.setAutoConnectClient(host.c_str(), port);
50+
// Remember these settings through a reboot
51+
ok = ok && gs.saveProfile(0);
52+
ok = ok && gs.setDefaultProfile(0);
53+
return ok;
54+
}
5655

57-
Gainspan.autoConfigure(ssid, passphrase, ip, port);
56+
bool WiFiBackpack::autoConnect() {
57+
// Try to disable the NCM in case it's already running
58+
gs.setNcm(false);
59+
return gs.setNcm(/* enable */ true, /* associate_only */ false, /* remember */ false);
5860
}
5961

60-
bool WiFiBackpack::apConnect() {
61-
Gainspan.autoConnect();
62+
void WiFiBackpack::printAPs(Print& p) {
63+
runDirectCommand(p, "AT+WS");
6264
}
6365

64-
void WiFiBackpack::printAPs() {
65-
Gainspan.send_cmd_w_resp(CMD_LIST_SSIDS);
66+
void WiFiBackpack::printProfiles(Print& p) {
67+
runDirectCommand(p, "AT&V");
6668
}
6769

68-
void WiFiBackpack::printProfiles() {
69-
Gainspan.send_cmd_w_resp(CMD_PROFILEGET);
70+
void WiFiBackpack::printCurrentNetworkStatus(Print& p) {
71+
runDirectCommand(p, "AT+NSTAT=?");
72+
runDirectCommand(p, "AT+CID=?");
7073
}
7174

72-
void WiFiBackpack::printCurrentNetworkStatus() {
73-
Gainspan.send_cmd_w_resp(CMD_NET_STATUS);
74-
Gainspan.send_cmd_w_resp(CMD_CURCID);
75+
void WiFiBackpack::printFirmwareVersions(Print& p) {
76+
runDirectCommand(p, "AT+VER=?");
7577
}
7678

77-
bool WiFiBackpack::dnsLookup(const char *host) {
78-
Serial.println(Gainspan.dnsLookup(host));
79+
80+
bool WiFiBackpack::dnsLookup(Print& p, const char *host) {
81+
// TODO
82+
return false;
7983
}
8084

81-
bool WiFiBackpack::ping(const char *host) {
82-
Gainspan.ping(host);
85+
bool WiFiBackpack::ping(Print &p, const char *host) {
86+
// TODO
87+
return false;
8388
}
8489

85-
bool WiFiBackpack::runDirectCommand(const char *command) {
86-
Gainspan.send_raw_cmd_w_resp(command);
90+
bool WiFiBackpack::runDirectCommand(Print &p, const char *command) {
91+
gs.writeCommand("%s", command);
92+
return gs.readResponse(print_line, &p);
8793
}
8894

8995
bool WiFiBackpack::goToSleep() {
90-
Gainspan.send_cmd(CMD_PSDPSLEEP);
96+
// TODO
97+
//Gainspan.send_cmd(CMD_PSDPSLEEP);
98+
return false;
9199
}
92100

93101
bool WiFiBackpack::wakeUp() {
94-
Gainspan.send_cmd_w_resp(CMD_AT);
102+
// TODO
103+
// Gainspan.send_cmd_w_resp(CMD_AT);
104+
return false;
95105
}
96106

97-
bool WiFiBackpack::getTime() {
98-
Gainspan.send_cmd_w_resp(CMD_GETTIME);
107+
bool WiFiBackpack::printTime(Print &p) {
108+
return runDirectCommand(p, "AT+GETTIME=?");
99109
}
100110

101111

utility/WiFiBackpack.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33

44
#include <Pinoccio.h>
55
#include <utility/Backpack.h>
6-
7-
#include "utility/webGainspan.h"
8-
#include "utility/webWifi.h"
9-
#include "utility/webWifiServer.h"
10-
#include "utility/webWifiClient.h"
11-
#include "utility/Flash.h"
6+
#include <GS.h>
127

138
class WiFiBackpack : public Backpack {
149

@@ -20,25 +15,29 @@ class WiFiBackpack : public Backpack {
2015
bool init();
2116
void loop();
2217

23-
bool apConfig(const char *ssid, const char *passphrase, String ip, String port);
24-
bool apConnect();
25-
void printAPs();
26-
void printProfiles();
27-
void printCurrentNetworkStatus();
18+
bool autoConfig(const char *ssid, const char *passphrase, const String &host, uint16_t port);
19+
bool autoConnect();
20+
21+
void printAPs(Print& p);
22+
void printProfiles(Print& p);
23+
void printCurrentNetworkStatus(Print& p);
24+
bool printTime(Print& p);
25+
void printFirmwareVersions(Print& p);
2826

29-
bool dnsLookup(const char *host);
30-
bool ping(const char *host);
27+
bool dnsLookup(Print &p, const char *host);
28+
bool ping(Print &p, const char *host);
3129

32-
bool runDirectCommand(const char *command);
30+
/** Run a command and print the results */
31+
bool runDirectCommand(Print& p, const char *command);
3332

3433
bool goToSleep();
3534
bool wakeUp();
3635

37-
bool getTime();
38-
39-
PinoccioWifiClient client;
40-
36+
GSClient client;
4137
protected:
38+
GSModule gs;
39+
// Was leadHQConnect already called?
40+
bool hqConnected = false;
4241
};
4342

44-
#endif // LIB_PINOCCIO_WIFI_BACKPACK_H_
43+
#endif // LIB_PINOCCIO_WIFI_BACKPACK_H_

0 commit comments

Comments
 (0)