Skip to content

Commit

Permalink
Save BT status and start at boot if enabled. Fixes 419
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzwald committed Jan 16, 2021
1 parent e09997c commit cc1aca3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
6 changes: 3 additions & 3 deletions include/version.h
Expand Up @@ -10,8 +10,8 @@
#define FN_VERSION_MAJOR 0
#define FN_VERSION_MINOR 5

#define FN_VERSION_BUILD "c379297b"
#define FN_VERSION_BUILD "e09997c5"

#define FN_VERSION_DATE "2021-01-15 23:40:06"
#define FN_VERSION_DATE "2021-01-16 02:23:59"

#define FN_VERSION_FULL "0.5.c379297b"
#define FN_VERSION_FULL "0.5.e09997c5"
38 changes: 38 additions & 0 deletions lib/config/fnConfig.cpp
Expand Up @@ -109,6 +109,12 @@ void fnConfig::store_wifi_passphrase(const char *passphrase_octets, int num_octe
}
}

void fnConfig::store_bt_status(bool status)
{
_bt.bt_status = status;
_dirty = true;
}

std::string fnConfig::get_host_name(uint8_t num)
{
if (num < MAX_HOST_SLOTS)
Expand Down Expand Up @@ -362,6 +368,10 @@ void fnConfig::save()
// TODO: Encrypt passphrase!
ss << "passphrase=" << _wifi.passphrase << LINETERM;

// BLUETOOTH
ss << LINETERM << "[Bluetooth]" LINETERM;
ss << "enabled=" << _bt.bt_status << LINETERM;

// NETWORK
ss << LINETERM << "[Network]" LINETERM;
ss << "sntpserver=" << _network.sntpserver << LINETERM;
Expand Down Expand Up @@ -547,6 +557,9 @@ New behavior: copy from SD first if available, then read SPIFFS.
case SECTION_WIFI:
_read_section_wifi(ss);
break;
case SECTION_BT:
_read_section_bt(ss);
break;
case SECTION_NETWORK:
_read_section_network(ss);
break;
Expand Down Expand Up @@ -656,6 +669,26 @@ void fnConfig::_read_section_wifi(std::stringstream &ss)
}
}

void fnConfig::_read_section_bt(std::stringstream &ss)
{
std::string line;
// Read lines until one starts with '[' which indicates a new section
while (_read_line(ss, line, '[') >= 0)
{
std::string name;
std::string value;
if (_split_name_value(line, name, value))
{
if (strcasecmp(name.c_str(), "enabled") == 0)
{
if (strcasecmp(value.c_str(), "1") == 0)
_bt.bt_status = true;
else
_bt.bt_status = false;
}
}
}}

void fnConfig::_read_section_host(std::stringstream &ss, int index)
{
// Throw out any existing data for this index
Expand Down Expand Up @@ -885,6 +918,11 @@ fnConfig::section_match fnConfig::_find_section_in_line(std::string &line, int &
//Debug_printf("Found WIFI\n");
return SECTION_WIFI;
}
else if (strncasecmp("Bluetooth", s1.c_str(), 9) == 0)
{
//Debug_printf("Found Bluetooth\n");
return SECTION_BT;
}
else if (strncasecmp("General", s1.c_str(), 7) == 0)
{
// Debug_printf("Found General\n");
Expand Down
12 changes: 12 additions & 0 deletions lib/config/fnConfig.h
Expand Up @@ -69,6 +69,10 @@ class fnConfig
void store_wifi_passphrase(const char *passphrase_octets, int num_octets);
void reset_wifi() { _wifi.ssid.clear(); _wifi.passphrase.clear(); };

// BLUETOOTH
void store_bt_status(bool status);
bool get_bt_status() { return _bt.bt_status; };

// HOSTS
std::string get_host_name(uint8_t num);
host_type_t get_host_type(uint8_t num);
Expand Down Expand Up @@ -110,6 +114,7 @@ class fnConfig

void _read_section_general(std::stringstream &ss);
void _read_section_wifi(std::stringstream &ss);
void _read_section_bt(std::stringstream &ss);
void _read_section_network(std::stringstream &ss);
void _read_section_host(std::stringstream &ss, int index);
void _read_section_mount(std::stringstream &ss, int index);
Expand All @@ -122,6 +127,7 @@ class fnConfig
{
SECTION_GENERAL,
SECTION_WIFI,
SECTION_BT,
SECTION_HOST,
SECTION_MOUNT,
SECTION_PRINTER,
Expand Down Expand Up @@ -182,6 +188,11 @@ class fnConfig
std::string passphrase;
};

struct bt_info
{
bool bt_status = false;
};

struct network_info
{
char sntpserver [40];
Expand Down Expand Up @@ -214,6 +225,7 @@ class fnConfig
mount_info _tape_slots[MAX_TAPE_SLOTS];

wifi_info _wifi;
bt_info _bt;
network_info _network;
general_info _general;
modem_info _modem;
Expand Down
10 changes: 9 additions & 1 deletion lib/hardware/keys.cpp
Expand Up @@ -6,6 +6,7 @@
#include "led.h"
#include "keys.h"
#include "sio.h"
#include "fnConfig.h"

#define LONGPRESS_TIME 1500 // 1.5 seconds to detect long press
#define DOUBLETAP_DETECT_TIME 400 // ms to wait to see if it's a single/double tap
Expand Down Expand Up @@ -167,14 +168,21 @@ void KeyManager::_keystate_task(void *param)
fnWiFi.start();
fnWiFi.connect();

// Save Bluetooth status in fnConfig
Config.store_bt_status(false); // Disabled
Config.save();
}
else
{
// Stop WiFi
fnWiFi.stop();

fnLedManager.set(BLUETOOTH_LED, true); // SIO LED always ON in Bluetooth mode
fnLedManager.set(BLUETOOTH_LED, true); // BT LED ON
fnBtManager.start();

// Save Bluetooth status in fnConfig
Config.store_bt_status(true); // Enabled
Config.save();
}
#endif //BLUETOOTH_SUPPORT
break;
Expand Down
17 changes: 13 additions & 4 deletions src/main.cpp
Expand Up @@ -80,10 +80,19 @@ void main_setup()
// Load our stored configuration
Config.load();

// Set up the WiFi adapter
fnWiFi.start();
// Go ahead and try reconnecting to WiFi
fnWiFi.connect();
if ( Config.get_bt_status() )
{
// Start SIO2BT mode if we were in it last shutdown
fnLedManager.set(eLed::LED_BT, true); // BT LED ON
fnBtManager.start();
}
else
{
// Set up the WiFi adapter
fnWiFi.start();
// Go ahead and try reconnecting to WiFi
fnWiFi.connect();
}

theFuji.setup(&SIO);
SIO.addDevice(&theFuji, SIO_DEVICEID_FUJINET); // the FUJINET!
Expand Down

0 comments on commit cc1aca3

Please sign in to comment.