diff --git a/.vscode/tasks.json b/.vscode/tasks.json index bc0c110f22bd80f21518fc833ebf2740f1373509..3d2eb922660db276e3f0b095b598d1b74a624259 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -21,7 +21,7 @@ { "label": "Build app evk", "type": "shell", - "command": "./scripts/docker_run.sh west build -b esp32_devkitc_wroom debug-app/app --build-dir .build-debug-app-evk", + "command": "./scripts/docker_run.sh west build -b esp32_devkitc_wroom debug-app/app --build-dir .build-debug-app-evk -- -DDTC_OVERLAY_FILE=$(pwd)/debug-app/boards/esp32.overlay", "problemMatcher": [ "$gcc" ], diff --git a/debug-app/app/prj.conf b/debug-app/app/prj.conf index bc5e9b7a21c22a4fbfbe65c8a248b56581c140f2..52583b602a2147ed016cbae83049d458868398c8 100644 --- a/debug-app/app/prj.conf +++ b/debug-app/app/prj.conf @@ -35,6 +35,27 @@ CONFIG_ZBUS_CHANNEL_NAME=y CONFIG_BT=y CONFIG_BT_OBSERVER=y +#wifi +CONFIG_WIFI=y +CONFIG_INIT_STACKS=y +CONFIG_NET_L2_WIFI_MGMT=y + +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_NET_IPV6=y +CONFIG_NET_TCP=y +CONFIG_NET_SOCKETS=y +# Use DHCP for IPv4 +CONFIG_NET_DHCPV4=y + +# Or assign a static IP address (useful for testing) +# Following line must be enabled, otherwise WiFi connection fails with -1. +CONFIG_NET_CONFIG_SETTINGS=y +CONFIG_NET_L2_ETHERNET=y +# CONFIG_NET_CONFIG_NEED_IPV4=y +# CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.8.135" +# CONFIG_NET_CONFIG_MY_IPV4_GW="192.168.8.255" + # PM # CONFIG_PM=y # CONFIG_PM_DEVICE=y diff --git a/debug-app/app/src/main.cpp b/debug-app/app/src/main.cpp index 923ebbec54fd9a465c4acf90a1f4375350d60280..0cfd64ae0722d789bc3c010a3198344e7e53cff5 100644 --- a/debug-app/app/src/main.cpp +++ b/debug-app/app/src/main.cpp @@ -7,6 +7,7 @@ #include "driver-hook.h" #include "ble-scanner.h" #include "device-parser.h" +#include "wifi-controller.h" #include "utilities.h" @@ -50,6 +51,7 @@ ZBUS_CHAN_DEFINE(stateChannel, /* Name */ int main() { + WifiController wifiController; MainHandler mainHandler; DriverHook driverHook; DebugHandler debugHanddler; diff --git a/debug-app/boards/esp32.overlay b/debug-app/boards/esp32.overlay new file mode 100644 index 0000000000000000000000000000000000000000..5256637eaac1d5df33f0d13f922bad538813a842 --- /dev/null +++ b/debug-app/boards/esp32.overlay @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + &wifi { + status = "okay"; +}; \ No newline at end of file diff --git a/debug-app/include/debug-handler.h b/debug-app/include/debug-handler.h index 602293c9958baeeee893689edfc5e39735360efb..4a7b440eb0ba046d71030ae450176457dab69aed 100644 --- a/debug-app/include/debug-handler.h +++ b/debug-app/include/debug-handler.h @@ -1,4 +1,16 @@ #include <zephyr/kernel.h> +#include <vector> + +typedef enum DataType{ + DEBUG_STATE, + DEBUG_BLE, + DEBUG_DRIVER +} DataType_e; + +typedef struct DebugMessage { + DataType_e flag; + std::vector<uint8_t> value; +} DebugMessage_t; class DebugHandler { public: @@ -23,6 +35,7 @@ public: private: static void DebugThread(DebugHandler *context); + int SendPacket(uint8_t *buf, uint16_t size); }; \ No newline at end of file diff --git a/debug-app/include/wifi-controller.h b/debug-app/include/wifi-controller.h new file mode 100644 index 0000000000000000000000000000000000000000..ebcc467062e8fb04a4320cd434567b8b5cc3a0e5 --- /dev/null +++ b/debug-app/include/wifi-controller.h @@ -0,0 +1,22 @@ +#include <zephyr/kernel.h> +#include <zephyr/net/wifi.h> +#include <zephyr/net/wifi_mgmt.h> + +class WifiController { +public: + /** Constructor. + * + **/ + WifiController(); + + /** Destructor. + * + **/ + ~WifiController(); + + static void WifiEventCallback( struct net_mgmt_event_callback *cb, uint32_t mgmt_event, struct net_if *iface ); + +private: + + +}; \ No newline at end of file diff --git a/debug-app/module/CMakeLists.txt b/debug-app/module/CMakeLists.txt index 6ccbd8e6592b007ef207c9e028063ebe1b3de769..f2e924501f7802b5ce36c43ce5755892f0cefe4a 100644 --- a/debug-app/module/CMakeLists.txt +++ b/debug-app/module/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(driver-hook) add_subdirectory(handler) -add_subdirectory(ble-scanner) \ No newline at end of file +add_subdirectory(ble-scanner) +add_subdirectory(wifi-controller) \ No newline at end of file diff --git a/debug-app/module/handler/debug-handler/src/debug-handler.cpp b/debug-app/module/handler/debug-handler/src/debug-handler.cpp index 181ab9b23c10aba3e29a74f1d6e4a0122e79d153..39bab71858f92599400fa14df270a99c02592d39 100644 --- a/debug-app/module/handler/debug-handler/src/debug-handler.cpp +++ b/debug-app/module/handler/debug-handler/src/debug-handler.cpp @@ -2,6 +2,8 @@ #include <zephyr/logging/log.h> #include <zephyr/zbus/zbus.h> +#include <zephyr/net/socket.h> + #include <stdio.h> #include <string.h> @@ -11,6 +13,10 @@ #define DEBUG_THREAD_STACK_SIZE 2048 #define DEBUG_THREAD_PRIORITY 5 +#define PORT 12345 + +#define PASS 0x41, 0x42, 0x43 + LOG_MODULE_REGISTER(Debug_Handler); ZBUS_CHAN_DECLARE(driverResponseChannel, stateChannel, bleScanChannel); @@ -21,6 +27,11 @@ struct k_thread debugThreadHandler; k_tid_t debugThreadId; static DebugHandler *context; +// TCP +int serv; +int client; +bool isDebugMode = false; + ZBUS_LISTENER_DEFINE(debugDriverListener, context->DriverResponseListener); ZBUS_LISTENER_DEFINE(stateListener, context->StateListener); ZBUS_LISTENER_DEFINE(bleListener, context->BleListener); @@ -30,8 +41,7 @@ DebugHandler::DebugHandler(){ debugThreadId = k_thread_create(&debugThreadHandler, debugThreadStack, DEBUG_THREAD_STACK_SIZE, (k_thread_entry_t) DebugThread, this, NULL, NULL, - DEBUG_THREAD_PRIORITY, 0, K_MSEC(10)); - k_thread_suspend(debugThreadId); + DEBUG_THREAD_PRIORITY, 0, K_NO_WAIT); zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT); zbus_chan_add_obs(&stateChannel, &stateListener, K_NO_WAIT); zbus_chan_add_obs(&bleScanChannel, &bleListener, K_NO_WAIT); @@ -46,14 +56,78 @@ int DebugHandler::Deinit(){ } void DebugHandler::DebugThread(DebugHandler *context){ - + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT); + addr.sin_addr = INADDR_ANY_INIT; + + serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (serv < 0) { + LOG_ERR("error: socket: %d\n", errno); + return; + } + + if (bind(serv, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + LOG_ERR("error: bind: %d\n", errno); + return; + } + + if (listen(serv, 5) < 0) { + LOG_ERR("error: listen: %d\n", errno); + return; + } + + while (1) { + struct sockaddr_in client_addr; + socklen_t client_addr_len = sizeof(client_addr); + char addr_str[32]; + client = accept(serv, (struct sockaddr *)&client_addr, + &client_addr_len); + + if (client < 0) { + LOG_ERR("error: accept: %d\n", errno); + continue; + } + + inet_ntop(client_addr.sin_family, &client_addr.sin_addr, + addr_str, sizeof(addr_str)); + LOG_INF("Connection from %s\n", addr_str); + + while (1) { + char buf[128], *p; + int len = recv(client, buf, sizeof(buf), 0); + int out_len; + + if (len <= 0) { + if (len < 0) { + LOG_ERR("error: recv: %d\n", errno); + } + break; + } + + char pass[] = {PASS}; + if(memcmp(pass, buf, sizeof(pass)) == 0){ + LOG_INF("DEBUG ACTIVE"); + isDebugMode = true; + } + + LOG_HEXDUMP_INF(buf, (uint32_t)len, "DATA TCP: "); + } + + close(client); + isDebugMode = false; + LOG_INF("Connection to %s close", addr_str); + } } void DebugHandler::DriverResponseListener(const struct zbus_channel *chan){ const DriverMessage_t *driver; if (&driverResponseChannel == chan) { driver = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access - LOG_INF("From listener -> driver =%d, value 0=%d", driver->number, driver->value[0]); + // LOG_INF("From listener -> driver =%d, value 0=%d", driver->number, driver->value[0]); + // if(isDebugMode){ + // send(client, p, len, 0); + // } } } @@ -61,7 +135,15 @@ void DebugHandler::StateListener(const struct zbus_channel *chan){ const StateMessage_t *state; if (&stateChannel == chan) { state = (StateMessage_t *)zbus_chan_const_msg(chan); // Direct message access - LOG_INF("From listener -> state =%d, value 0=%d", state->flag, state->value[0]); + // LOG_INF("From listener -> state =%d, value 0=%d", state->flag, state->value[0]); + if(isDebugMode){ + std::vector<uint8_t> dataSent; + dataSent.push_back(DEBUG_STATE); + dataSent.push_back((state->value.size() >> 8) & 0xff); + dataSent.push_back(state->value.size() & 0xff); + copy(state->value.begin(), state->value.end(), back_inserter(dataSent)); + context->SendPacket((uint8_t *)dataSent.data(), dataSent.size()); + } } } @@ -69,7 +151,30 @@ void DebugHandler::BleListener(const struct zbus_channel *chan){ const BleData_t *ble; if (&bleScanChannel == chan) { ble = (BleData_t *)zbus_chan_const_msg(chan); // Direct message access - LOG_INF("Ble -> address =%s", ble->address.c_str()); - LOG_HEXDUMP_INF(ble->data.data(), ble->data.size(), "data raw"); + // LOG_INF("Ble -> address =%s", ble->address.c_str()); + // LOG_HEXDUMP_INF(ble->data.data(), ble->data.size(), "data raw"); + if(isDebugMode){ + std::vector<uint8_t> dataSent; + dataSent.push_back(DEBUG_BLE); + dataSent.push_back((ble->data.size() >> 8) & 0xff); + dataSent.push_back(ble->data.size() & 0xff); + copy(ble->data.begin(), ble->data.end(), back_inserter(dataSent)); + context->SendPacket((uint8_t *)dataSent.data(), dataSent.size()); + } } +} + +int DebugHandler::SendPacket(uint8_t *buf, uint16_t size){ + int sentlength, recvLength; + recvLength = size; + do { + sentlength = send(client, buf, recvLength, 0); + if (sentlength < 0) { + LOG_ERR("error: send: %d\n", errno); + break; + } + buf += sentlength; + recvLength-= sentlength; + } while (recvLength); + return recvLength; } \ No newline at end of file diff --git a/debug-app/module/handler/main-handler/src/main-handler.cpp b/debug-app/module/handler/main-handler/src/main-handler.cpp index 50ed4ee44066d0cf9ae58b2f9f095e3be41b6acf..9032059e996c15a55b6d64744bcde0314ca93b45 100644 --- a/debug-app/module/handler/main-handler/src/main-handler.cpp +++ b/debug-app/module/handler/main-handler/src/main-handler.cpp @@ -83,7 +83,7 @@ void MainHandler::MainThread(MainHandler *context){ configVector.number = SENSOR_1; configVector.value.push_back(SENSOR_1); if(context->DriverRequest(&configVector, &responseVector, K_MSEC(200))){ - LOG_INF("get sensor data %d", responseVector.value[0]); + LOG_INF("get sensor data %d", responseVector.number); } else{ @@ -111,7 +111,7 @@ void MainHandler::DriverResponseListener(const struct zbus_channel *chan){ if (&driverResponseChannel == chan) { driver = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access if(driver->number == context->requestedResponse){ - k_sem_give(&responseWait); + // k_sem_give(&responseWait); context->driverResponse_g = *driver; } } diff --git a/debug-app/module/wifi-controller/CMakeLists.txt b/debug-app/module/wifi-controller/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..04472fc3eaabc0b23995f93b0424e4ffd7f514aa --- /dev/null +++ b/debug-app/module/wifi-controller/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(src/wifi-controller.cpp) \ No newline at end of file diff --git a/debug-app/module/wifi-controller/src/wifi-controller.cpp b/debug-app/module/wifi-controller/src/wifi-controller.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57f001e88d7b2011aef08a5ea27be18ed2ff8bab --- /dev/null +++ b/debug-app/module/wifi-controller/src/wifi-controller.cpp @@ -0,0 +1,85 @@ +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +#include <stdio.h> +#include <string.h> + +#include "utilities.h" +#include "wifi-controller.h" +#include <zephyr/net/net_ip.h> +#include <zephyr/net/net_if.h> +#include <zephyr/net/net_event.h> +#include <zephyr/net/wifi_mgmt.h> +#include <zephyr/net/wifi_utils.h> +#include <zephyr/net/net_mgmt.h> +// #include <zephyr/posix/unistd.h> +#include <zephyr/sys/slist.h> +#include <zephyr/init.h> + +#define AUTO_CONNECT_SSID "tselhome-605E" +#define AUTO_CONNECT_SSID_PSK "Sapiular0" + +#define WIFI_MGMT_EVENTS (NET_EVENT_WIFI_SCAN_RESULT | \ + NET_EVENT_WIFI_SCAN_DONE | \ + NET_EVENT_WIFI_CONNECT_RESULT | \ + NET_EVENT_WIFI_DISCONNECT_RESULT) + +LOG_MODULE_REGISTER(WifiController); +// ZBUS_CHAN_DECLARE(driverRequestChannel, bleScanChannel); + +static WifiController *context; +static struct k_sem net_cb_sem; +static struct net_mgmt_event_callback cb; + + +WifiController::WifiController(){ + context = this; + struct wifi_connect_req_params wifi_args; + + wifi_args.security = WIFI_SECURITY_TYPE_PSK; + wifi_args.channel = WIFI_CHANNEL_ANY; + wifi_args.psk = (const uint8_t *)AUTO_CONNECT_SSID_PSK; + wifi_args.psk_length = strlen(AUTO_CONNECT_SSID_PSK); + wifi_args.ssid = (const uint8_t *)AUTO_CONNECT_SSID; + wifi_args.ssid_length = strlen(AUTO_CONNECT_SSID); + + // Init semaphore + k_sem_init(&net_cb_sem, 0, 1); + + // Configure Callback + net_mgmt_init_event_callback(&cb, WifiEventCallback, WIFI_MGMT_EVENTS ); + net_mgmt_add_event_callback(&cb); + + // Connect interface to network + struct net_if *iface = net_if_get_default(); + if(iface == NULL){ + LOG_ERR("iface NULL"); + return; + } + + if(iface->if_dev == NULL){ + LOG_ERR("iface->if_dev NULL"); + return; + } + int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &wifi_args, sizeof(wifi_args)); + if(ret != 0) { + LOG_ERR("Failed to request connection to SSID %s", AUTO_CONNECT_SSID); + } + + // Wait for connection..... + LOG_INF("Wait for connection....."); + k_sem_take(&net_cb_sem, K_FOREVER ); + LOG_INF("SUCCESFULLY CONNECTED"); +} + +void WifiController::WifiEventCallback( struct net_mgmt_event_callback *cb, uint32_t mgmt_event, struct net_if *iface ){ + switch( mgmt_event ) { + case NET_EVENT_WIFI_CONNECT_RESULT: + const struct wifi_status *status = (const struct wifi_status *) cb->info; + if (!status->status) { + // Connected + k_sem_give(&net_cb_sem); + } + break; + } +} \ No newline at end of file