this is using the mbed os version 5-13-1
Revision 114:b11bb96c09f3, committed 2019-05-18
- Comitter:
- ocomeni
- Date:
- Sat May 18 15:44:48 2019 +0000
- Branch:
- PassingRegression
- Parent:
- 113:888e262ff0a9
- Child:
- 115:8054dbadfaa0
- Commit message:
- main program refactored:; 1. only atcmd manager started by default; 2. added state in main to allow ATCMD to trigger enabling BLE and WiFi; 3. added main.h to expose trigger functions to ATCMD; 4. added triggers to ATCMD; ; Testing not yet done.
Changed in this revision
--- a/source/ATCmdManager.cpp Sat May 18 10:50:49 2019 +0000
+++ b/source/ATCmdManager.cpp Sat May 18 15:44:48 2019 +0000
@@ -2,6 +2,7 @@
#include "ATCmdManager.h"
#include "common_config.h"
#include "common_types.h"
+#include "main.h"
#include "http_request.h"
#include "mbed_memory_status.h"
//#include "mbed_memory_status.h"
@@ -71,7 +72,7 @@
//_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
//_parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
_parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection));
- _parser.oob("AT+UWSC=0,0,0", callback(this, &ATCmdManager::_oob_ok_hdlr));
+ _parser.oob("AT+UWSC=0,0,0", callback(this, &ATCmdManager::_oob_enable_wifi));
_parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID));
_parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD));
_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity));
@@ -651,6 +652,11 @@
void ATCmdManager::_oob_conn_already(){
}
+void ATCmdManager::_oob_enable_wifi()
+{
+ trigger_start_WiFi();
+ sendAtConfirmation(OK_RESP);
+}
void ATCmdManager::_oob_err(){
}
@@ -772,6 +778,7 @@
}
void ATCmdManager::_oob_ena_ble_peri(){
+ trigger_start_BLE();
dbg_printf(LOG, "\n Received enable BLE Peripheral command!!\n");
sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
}
--- a/source/ATCmdManager.h Sat May 18 10:50:49 2019 +0000
+++ b/source/ATCmdManager.h Sat May 18 15:44:48 2019 +0000
@@ -16,8 +16,8 @@
#define ERROR_RESP "\r\nERROR\r\n"
#define WIFI_BUSY_RESP "\r\nWIFI BUSY\r\n"
#define BOX_UBLOX_DEMO_TESTING
-extern void print_memory_info();
-extern void blinkLEDs();
+//extern void print_memory_info();
+//extern void blinkLEDs();
class ATCmdManager {
public:
ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral,
@@ -82,6 +82,7 @@
void _oob_startup_hdlr();
void _oob_ok_hdlr();
void _oob_bleRole_hdlr();
+ void _oob_enable_wifi();
void _oob_wifiMode_err();
void _oob_conn_already();
void _oob_err();
--- a/source/WiFiManager.cpp Sat May 18 10:50:49 2019 +0000
+++ b/source/WiFiManager.cpp Sat May 18 15:44:48 2019 +0000
@@ -50,6 +50,7 @@
WiFiManager::~WiFiManager()
{
+ delete network;
}
//#define DISABLE_WATCHDOG
@@ -113,7 +114,13 @@
return true;
}
-
+void WiFiManager::getWiFiInstance()
+{
+ network = WiFiInterface::get_default_instance();
+ if (!network) {
+ dbg_printf(LOG, "ERROR: No WiFiInterface found.\n");
+ }
+}
void WiFiManager::runMain(){
nsapi_error_t error;
@@ -417,6 +424,12 @@
nsapi_size_or_error_t WiFiManager::scanNetworks()
{
+ getWiFiInstance();
+ if(network == NULL)
+ {
+ dbg_printf(LOG, "\n [WIFI-MAN] Error instantiating WiFi!! \n");
+ return 0;
+ }
nsapi_error_t error;
dbg_printf(LOG, "\n [WIFI-MAN] About to start scan for WiFi networks\n");
lastScanCount = network->scan(NULL, 0);
@@ -429,6 +442,12 @@
// nsapi_size_t ncount)
nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount)
{
+ getWiFiInstance();
+ if(network == NULL)
+ {
+ dbg_printf(LOG, "\n [WIFI-MAN] Error instantiating WiFi!! \n");
+ return 0;
+ }
WiFiAccessPoint *ap;
nsapi_size_or_error_t count;
count = ncount;
@@ -739,6 +758,12 @@
nsapi_error_t WiFiManager::connect()
{
+ getWiFiInstance();
+ if(network == NULL)
+ {
+ dbg_printf(LOG, "\n [WIFI-MAN] Error instantiating WiFi!! \n");
+ return 0;
+ }
nsapi_error_t error;
dbg_printf(LOG, "\n [WIFI-MAN] About to connect to WiFi network\n");
network->attach(callback(this, &WiFiManager::status_callback));
--- a/source/WiFiManager.h Sat May 18 10:50:49 2019 +0000
+++ b/source/WiFiManager.h Sat May 18 15:44:48 2019 +0000
@@ -91,6 +91,7 @@
void set_WIFI_SECURITY(nsapi_security_t wifi_security);
void set_WIFI_CONFIG();
void set_internet_config();
+ void getWiFiInstance();
nsapi_error_t connect();
nsapi_error_t disconnect();
void createSendHttpsRequest();
--- a/source/common_types.h Sat May 18 10:50:49 2019 +0000
+++ b/source/common_types.h Sat May 18 15:44:48 2019 +0000
@@ -46,6 +46,16 @@
typedef enum
{
+ MAIN_IDLE,
+ START_BLE,
+ STOP_BLE,
+ START_WIFI,
+ STOP_WIFI
+}main_states_t;
+
+
+typedef enum
+{
RESPONSE_OK,
TLS_CONNECTION_FAILED,
HTTP_REQUEST_FAILED
--- a/source/main-https.cpp Sat May 18 10:50:49 2019 +0000
+++ b/source/main-https.cpp Sat May 18 15:44:48 2019 +0000
@@ -27,7 +27,7 @@
DigitalOut led3(LED3);
#define FILE_CODE "main"
-
+main_states_t mainLoop;
static RawSerial *device; // tx, rx
// wifi configuration
@@ -37,6 +37,10 @@
// wifi manager pointer
static WiFiManager *wiFiManager;
+// BLE instance
+//BLE& _ble;
+BLE& _ble = BLE::Instance();
+
// BLE configuration
static ble_config_t ble_config;
const uint8_t pairingPassword[6] = "1101";
@@ -215,21 +219,9 @@
led3 = !led3;
}
-#define PAUSE_SECONDS 0
-#define PAUSE_SECONDS_BLE 0
-int main() {
- device = new RawSerial(USBTX, USBRX, 2*DEFAULT_BAUD_RATE);
- uint8_t debug_level = (LOG | ERR | TXT | DBG);
- initialise_debug(debug_level);
-#ifdef MBED_MAJOR_VERSION
- dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
-#endif
-
- reset_counter++;
- dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
- setupDefaultWiFiConfig();
- setupDefaultBleConfig();
- BLE& _ble = BLE::Instance();
+void start_BLE()
+{
+ //_ble = BLE::Instance();
#if MBED_CONF_APP_FILESYSTEM_SUPPORT
/* if filesystem creation fails or there is no filesystem the security manager
* will fallback to storing the security database in memory */
@@ -242,14 +234,15 @@
peripheral->run();
btle_thread.start(callback(&eventQueue_ble, &EventQueue::dispatch_forever));
- //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
- printWaitAbortKeyPress(120, "Wait after BLE dispatch\r\n");
- int start = Kernel::get_ms_count();
- network = WiFiInterface::get_default_instance();
- if (!network) {
- dbg_printf(LOG, "ERROR: No WiFiInterface found.\n");
- }
- printWaitAbortKeyPress(120, "Wait after WiFi instantiation\r\n");
+}
+
+void stop_BLE()
+{
+ delete peripheral;
+}
+
+void start_WiFi()
+{
wiFiManager = new WiFiManager(wifi_config, network,
eventQueue_wifi,
&aT2WiFimPool, &aT2WiFiCmdQueue,
@@ -266,6 +259,60 @@
initialise_debug(NONE);
// dispatch wifi event queue on event thread
wifi_evt_thread.start(callback(&eventQueue_wifi, &EventQueue::dispatch_forever));
+}
+
+void stop_WiFi()
+{
+ delete network;
+}
+
+
+void trigger_start_BLE()
+{
+ mainLoop = START_BLE;
+}
+
+
+void trigger_stop_BLE()
+{
+ mainLoop = STOP_BLE;
+}
+
+
+void trigger_start_WiFi()
+{
+ mainLoop = START_WIFI;
+}
+
+
+void trigger_stop_WiFi()
+{
+ mainLoop = STOP_WIFI;
+}
+
+
+
+
+#define PAUSE_SECONDS 0
+#define PAUSE_SECONDS_BLE 0
+int main() {
+ device = new RawSerial(USBTX, USBRX, 2*DEFAULT_BAUD_RATE);
+ uint8_t debug_level = (LOG | ERR | TXT | DBG);
+ initialise_debug(debug_level);
+#ifdef MBED_MAJOR_VERSION
+ dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+#endif
+
+ reset_counter++;
+ dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
+ setupDefaultWiFiConfig();
+ setupDefaultBleConfig();
+ //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
+ start_BLE();
+ printWaitAbortKeyPress(120, "Wait after BLE dispatch\r\n");
+ int start = Kernel::get_ms_count();
+ start_WiFi();
+ printWaitAbortKeyPress(120, "Wait after WiFi instantiation\r\n");
// dispatch atcmd event queue on event thread
atcmd_evt_thread.start(callback(&eventQueue_atcmd, &EventQueue::dispatch_forever));
dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
@@ -281,6 +328,30 @@
print_memory_info();
while(1)
{
+ switch(mainLoop)
+ {
+ case MAIN_IDLE:
+ break;
+ case START_BLE:
+ start_BLE();
+ mainLoop = MAIN_IDLE;
+ break;
+ case START_WIFI:
+ start_WiFi();
+ mainLoop = MAIN_IDLE;
+ break;
+ case STOP_BLE:
+ stop_BLE();
+ mainLoop = MAIN_IDLE;
+ break;
+ case STOP_WIFI:
+ stop_WiFi();
+ mainLoop = MAIN_IDLE;
+ break;
+ default:
+ mainLoop = MAIN_IDLE;
+ break;
+ }
wait(0.1);
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main.h Sat May 18 15:44:48 2019 +0000 @@ -0,0 +1,10 @@ +#ifndef __MAIN_H__ +#define __MAIN_H__ +void print_memory_info(); +void blinkLEDs(); +void trigger_start_BLE(); +void trigger_stop_BLE(); +void trigger_start_WiFi(); +void trigger_stop_WiFi(); + +#endif // __MAIN_H__ \ No newline at end of file