diff --git a/CMakeLists.txt b/CMakeLists.txt index e2add76..e0a1feb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ file(GLOB SRC_FILES "${PROJECT_SOURCE_DIR}/src/*.cpp") add_executable(Tardis ${SRC_FILES}) target_include_directories(Tardis PRIVATE "${PROJECT_SOURCE_DIR}/include") -target_link_libraries(Tardis PRIVATE mbed-os mbed-storage-qspif mbed-usb-msd mbed-storage-littlefs-v2) +target_link_libraries(Tardis PRIVATE mbed-netsocket mbed-os mbed-storage-qspif mbed-usb-msd mbed-storage-littlefs-v2) target_compile_options(Tardis PRIVATE -Wall -Wextra -Wpedantic -Werror) mbed_set_post_build(Tardis) diff --git a/include/NetworkHandler.h b/include/NetworkHandler.h new file mode 100644 index 0000000..ab7d0cf --- /dev/null +++ b/include/NetworkHandler.h @@ -0,0 +1,15 @@ +/* +SPDX-License-Identifier: MIT +Copyright (c) 2023 Casey Reeves and the LuminaSensum contributors +*/ + +#ifndef NETWORKHANDLER_H +#define NETWORKHANDLER_H + +#include "EthernetInterface.h" + +void network_connect(void); +void network_disconnect(void); +void network_init(void); + +#endif diff --git a/mbed_app.json b/mbed_app.json index 5b9c18d..bc84241 100644 --- a/mbed_app.json +++ b/mbed_app.json @@ -1,6 +1,8 @@ { "target_overrides": { "*": { + "lwip.ipv6-enabled": true, + "lwip.ip-ver-pref": 6, "platform.stdio-baud-rate": 115200 } } diff --git a/src/NetworkHandler.cpp b/src/NetworkHandler.cpp new file mode 100644 index 0000000..96e68ac --- /dev/null +++ b/src/NetworkHandler.cpp @@ -0,0 +1,63 @@ +/* +SPDX-License-Identifier: MIT +Copyright (c) 2023 Casey Reeves and the LuminaSensum contributors +*/ + +#include "EthernetInterface.h" +#include "mbed-trace/mbed_trace.h" +#include "mbed.h" +#define TRACE_GROUP "NET" + +// instantiate the network interface + +EthernetInterface eth; + +// create a function to allow other part of the program to connect to the +// network + +void network_connect(void) { eth.connect(); } + +// create a function to disconnect from the network + +void network_disconnect(void) { eth.disconnect(); } + +// set up a status callback function to monitor for changes + +bool is_connected = false; +void status_callback(nsapi_event_t status, intptr_t param) { + tr_debug("Detected a connection status change."); + switch (param) { + case NSAPI_STATUS_CONNECTING: + tr_info("Connecting to the network"); + break; + case NSAPI_STATUS_LOCAL_UP: + tr_debug("Local IP address acquired"); + break; + case NSAPI_STATUS_GLOBAL_UP: + if (!is_connected) { + tr_info("Global IP address acquired.. Networking is " + "now fully operational."); + is_connected = true; + } + break; + case NSAPI_STATUS_DISCONNECTED: + tr_info("No network connection available."); + if (is_connected) { + is_connected = false; + } + break; + default: + tr_info("Networking is not supported."); + break; + } +} + +// create a function to set the network interface in async mode and attach the +// status callback. This should only be called by main. + +void network_init(void) { + eth.set_blocking(false); + tr_debug("Network interface now in async mode."); + eth.attach(&status_callback); + tr_debug("Status callback now attached to monitor changes."); +} diff --git a/src/main.cpp b/src/main.cpp index bd46c6f..fe8f7b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "MainBD.h" #include "MainFilesystem.h" #include "MyUSBMSD.h" +#include "NetworkHandler.h" #include "mbed-trace/mbed_trace.h" #include "mbed.h" #define TRACE_GROUP "APP" @@ -24,7 +25,7 @@ static void trace_mutex_release() { traceMutex.unlock(); } void setup_tracing(void) { - char trace_filters[] = "APP,FS,FLER,SNTP,USBM"; + char trace_filters[] = "APP,FS,FLER,NET,SNTP,USBM"; mbed_trace_mutex_wait_function_set(trace_mutex_wait); mbed_trace_mutex_release_function_set(trace_mutex_release); mbed_trace_init(); @@ -50,6 +51,8 @@ Thread buttonThread; buttonThread.start(buttonTask); + network_init(); + while (true) { int presses = waitForPresses(600s);