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

Moves WiFi tester from system-part2 into user module #1378

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hal/inc/hal_dynalib_dct.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#ifdef DYNALIB_EXPORT
#include "dct_hal.h"
#include "device_code.h"
#endif

DYNALIB_BEGIN(hal_dct)
Expand All @@ -13,5 +14,6 @@ DYNALIB_FN(1, hal_dct, dct_read_app_data_copy, int(uint32_t, void*, size_t))
DYNALIB_FN(2, hal_dct, dct_read_app_data_lock, const void*(uint32_t))
DYNALIB_FN(3, hal_dct, dct_read_app_data_unlock, int(uint32_t))
DYNALIB_FN(4, hal_dct, dct_write_app_data, int(const void*, uint32_t, uint32_t))
DYNALIB_FN(5, hal_dct, fetch_or_generate_setup_ssid, bool(device_code_t*))

DYNALIB_END(hal_dct)
2 changes: 2 additions & 0 deletions system/inc/system_dynalib.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "system_version.h"
#include "system_control.h"
#include "system_led_signal.h"
#include "system_setup.h"
#endif

DYNALIB_BEGIN(system)
Expand Down Expand Up @@ -84,6 +85,7 @@ DYNALIB_FN(BASE_IDX + 3, system, led_set_signal_theme, int(const LEDSignalThemeD
DYNALIB_FN(BASE_IDX + 4, system, led_get_signal_theme, int(LEDSignalThemeData*, int, void*))
DYNALIB_FN(BASE_IDX + 5, system, led_signal_status, const LEDStatusData*(int, void*))
DYNALIB_FN(BASE_IDX + 6, system, led_pattern_period, uint16_t(int, int, void*))
DYNALIB_FN(BASE_IDX + 7, system, system_set_tester_handlers, int(system_tester_handlers_t*, void*))

DYNALIB_FN(BASE_IDX + 7, system, system_format_diag_data, int(const uint16_t*, size_t, unsigned, appender_fn, void*, void*))

Expand Down
24 changes: 21 additions & 3 deletions system/inc/system_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ typedef int (*ConnectCallback2)(void* data,
bool dry_run);


class WiFiTester;

struct SystemSetupConsoleConfig
{

Expand Down Expand Up @@ -99,7 +97,8 @@ template<typename Config> class SystemSetupConsole
#if SETUP_OVER_SERIAL1
bool serial1Enabled;
uint8_t magicPos; // how far long the magic key we are
WiFiTester* tester;
// Opaque pointer
void* tester;
#endif
};

Expand Down Expand Up @@ -150,3 +149,22 @@ class CellularSetupConsole : public SystemSetupConsole<CellularSetupConsoleConfi
};

#endif

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

typedef struct {
uint16_t version;
uint16_t size;
void* (*create)(void* reserved);
int (*destroy)(void* tester, void* reserved);
int (*setup)(void* tester, bool useSerial1, void* reserved);
int (*loop)(void* tester, int c, void* reserved);
} system_tester_handlers_t;

int system_set_tester_handlers(system_tester_handlers_t* handlers, void* reserved);

#ifdef __cplusplus
}
#endif /* __cplusplus */
66 changes: 1 addition & 65 deletions system/inc/wifitester.h
Original file line number Diff line number Diff line change
@@ -1,65 +1 @@
/**
******************************************************************************
* @file wifitester.h
* @authors mat
* @date 27 January 2015
******************************************************************************
Copyright (c) 2015 Particle Industries, Inc. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/

#ifndef WIFITESTER_H
#define WIFITESTER_H

class WiFiTester
{
bool useSerial1;

uint8_t notifiedAboutDHCP;
int state;
int wifi_testing;
int dhcp_notices;
unsigned cmd_index;
static const unsigned cmd_length = 256;
char command[cmd_length];
bool power_state = false;


void checkWifiSerial(char c);
void wifiScan();
void printInfo();
void printItem(const char* name, const char* value);
void tester_connect(char *ssid, char *pass);
void tokenizeCommand(char *cmd, char* parts[], unsigned max_parts);
bool isPowerOn();

public:
WiFiTester() {
memset(this, 0, sizeof(*this));
}

void setup(bool useSerial1);
void loop(int c);

uint8_t serialAvailable();
int32_t serialRead();
void serialPrintln(const char* s);
void serialPrint(const char* s);
};


#endif /* WIFITESTER_H */

#include "spark_wiring_wifitester.h"
130 changes: 76 additions & 54 deletions system/src/system_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,24 @@
#define SETUP_LISTEN_MAGIC 1
void loop_wifitester(int c);
#include "spark_wiring_usartserial.h"
#include "wifitester.h"
#endif

static system_tester_handlers_t s_tester_handlers = {0};

int system_set_tester_handlers(system_tester_handlers_t* handlers, void* reserved) {
memset(&s_tester_handlers, 0, sizeof(s_tester_handlers));
if (handlers != nullptr) {
memcpy(&s_tester_handlers, handlers, std::min(static_cast<uint16_t>(sizeof(s_tester_handlers)), handlers->size));
}
return 0;
}

#else /* SETUP_OVER_SERIAL1 */

int system_set_tester_handlers(system_tester_handlers_t* handlers, void* reserved) {
return 1;
}

#endif /* SETUP_OVER_SERIAL1 */

#ifndef SETUP_LISTEN_MAGIC
#define SETUP_LISTEN_MAGIC 0
Expand Down Expand Up @@ -89,9 +105,9 @@ class StreamAppender : public Appender

#if SETUP_OVER_SERIAL1
inline bool setup_serial1() {
uint8_t value = 0;
system_get_flag(SYSTEM_FLAG_WIFITESTER_OVER_SERIAL1, &value, nullptr);
return value;
uint8_t value = 0;
system_get_flag(SYSTEM_FLAG_WIFITESTER_OVER_SERIAL1, &value, nullptr);
return value;
}
#endif

Expand All @@ -107,17 +123,19 @@ template <typename Config> SystemSetupConsole<Config>::SystemSetupConsole(Config
serial1Enabled = false;
magicPos = 0;
if (setup_serial1()) {
//WITH_LOCK(SETUP_SERIAL);
SETUP_SERIAL.begin(9600);
//WITH_LOCK(SETUP_SERIAL);
SETUP_SERIAL.begin(9600);
}
this->tester = NULL;
this->tester = nullptr;
#endif
}

template <typename Config> SystemSetupConsole<Config>::~SystemSetupConsole()
{
#if SETUP_OVER_SERIAL1
delete this->tester;
if (tester != nullptr && s_tester_handlers.destroy) {
s_tester_handlers.destroy(this->tester, nullptr);
}
#endif
}

Expand All @@ -126,36 +144,40 @@ template<typename Config> void SystemSetupConsole<Config>::loop(void)
#if SETUP_OVER_SERIAL1
//TRY_LOCK(SETUP_SERIAL)
{
if (setup_serial1()) {
int c = -1;
if (SETUP_SERIAL.available()) {
c = SETUP_SERIAL.read();
}
if (SETUP_LISTEN_MAGIC) {
static uint8_t magic_code[] = { 0xe1, 0x63, 0x57, 0x3f, 0xe7, 0x87, 0xc2, 0xa6, 0x85, 0x20, 0xa5, 0x6c, 0xe3, 0x04, 0x9e, 0xa0 };
if (!serial1Enabled) {
if (c>=0) {
if (c==magic_code[magicPos++]) {
serial1Enabled = magicPos==sizeof(magic_code);
if (serial1Enabled) {
if (tester==NULL)
tester = new WiFiTester();
tester->setup(SETUP_OVER_SERIAL1);
}
}
else {
magicPos = 0;
}
c = -1;
}
}
else {
if (tester)
tester->loop(c);
}
}
}
}
if (setup_serial1() && s_tester_handlers.size != 0) {
int c = -1;
if (SETUP_SERIAL.available()) {
c = SETUP_SERIAL.read();
}
if (SETUP_LISTEN_MAGIC) {
static uint8_t magic_code[] = { 0xe1, 0x63, 0x57, 0x3f, 0xe7, 0x87, 0xc2, 0xa6, 0x85, 0x20, 0xa5, 0x6c, 0xe3, 0x04, 0x9e, 0xa0 };
if (!serial1Enabled) {
if (c>=0) {
if (c==magic_code[magicPos++]) {
serial1Enabled = magicPos==sizeof(magic_code);
if (serial1Enabled) {
if (tester == nullptr && s_tester_handlers.create != nullptr) {
tester = s_tester_handlers.create(nullptr);
}
if (tester != nullptr && s_tester_handlers.setup) {
s_tester_handlers.setup(tester, SETUP_OVER_SERIAL1, nullptr);
}
}
}
else {
magicPos = 0;
}
c = -1;
}
}
else {
if (tester != nullptr && s_tester_handlers.loop) {
s_tester_handlers.loop(tester, c, nullptr);
}
}
}
}
}
#endif

TRY_LOCK(serial)
Expand Down Expand Up @@ -251,25 +273,25 @@ template<typename Config> void SystemSetupConsole<Config>::handle(char c)
}
else if ('c' == c)
{
bool claimed = HAL_IsDeviceClaimed(nullptr);
print("Device claimed: ");
print(claimed ? "yes" : "no");
print("\r\n");
bool claimed = HAL_IsDeviceClaimed(nullptr);
print("Device claimed: ");
print(claimed ? "yes" : "no");
print("\r\n");
}
else if ('C' == c)
{
char code[64];
print("Enter 63-digit claim code: ");
read_line(code, 63);
if (strlen(code)==63) {
HAL_Set_Claim_Code(code);
print("Claim code set to: ");
print(code);
}
else {
print("Sorry, claim code is not 63 characters long. Claim code unchanged.");
char code[64];
print("Enter 63-digit claim code: ");
read_line(code, 63);
if (strlen(code)==63) {
HAL_Set_Claim_Code(code);
print("Claim code set to: ");
print(code);
}
else {
print("Sorry, claim code is not 63 characters long. Claim code unchanged.");
}
print("\r\n");
print("\r\n");
}
else if ('d' == c)
{
Expand Down
1 change: 1 addition & 0 deletions user/applications/tinker/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);

STARTUP(System.enable(SYSTEM_FLAG_WIFITESTER_OVER_SERIAL1));
STARTUP(System.enableFeature(FEATURE_WIFITESTER));

SYSTEM_MODE(AUTOMATIC);

Expand Down
2 changes: 1 addition & 1 deletion user/applications/wifitester/wifitester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

#include "application.h"
#include "wifitester.h"
#include "spark_wiring_wifitester.h"

SYSTEM_MODE(MANUAL);

Expand Down
1 change: 1 addition & 0 deletions user/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);

STARTUP(System.enable(SYSTEM_FLAG_WIFITESTER_OVER_SERIAL1));
STARTUP(System.enableFeature(FEATURE_WIFITESTER));

SYSTEM_MODE(AUTOMATIC);

Expand Down
1 change: 1 addition & 0 deletions user/tests/app/listen_mode/listen_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SYSTEM_MODE(MANUAL);

STARTUP(System.enable(SYSTEM_FLAG_WIFITESTER_OVER_SERIAL1));
STARTUP(System.enableFeature(FEATURE_WIFITESTER));

// SerialLogHandler logHandler(LOG_LEVEL_ALL); // >= 0.6.0
// SerialDebugOutput debugOutput(9600, ALL_LEVEL); // < 0.6.0
Expand Down
6 changes: 6 additions & 0 deletions wiring/inc/spark_wiring_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ enum LoggingFeature {
};
#endif

enum WiFiTesterFeature {
FEATURE_WIFITESTER = 1
};

class SystemClass {
public:

Expand Down Expand Up @@ -209,6 +213,8 @@ class SystemClass {
bool enableFeature(LoggingFeature feature);
#endif

static bool enableFeature(const WiFiTesterFeature feature);

String version()
{
SystemVersionInfo info;
Expand Down
Loading