From 1cbc2ccf13eae07d71ae1e28569a3bbabfe99123 Mon Sep 17 00:00:00 2001 From: DAsamhan <23522019@std.stei.itb.ac.id> Date: Sat, 22 Jun 2024 18:41:40 +0700 Subject: [PATCH] squash from main --- .vscode/tasks.json | 34 +++--- debug-app/app/prj.conf | 6 +- debug-app/app/src/main.cpp | 13 +- debug-app/boards/overlay.conf | 12 ++ debug-app/drivers/CMakeLists.txt | 4 +- .../debug-handler/src/debug-handler.cpp | 43 +++++++ debug-python/debug.py | 89 ++++++++++++++ debug-python/helper.py | 6 + debug-python/socket-run.py | 111 ++++++++++++++++++ scripts/docker_run.sh | 7 +- scripts/run_debug.sh | 6 + 11 files changed, 303 insertions(+), 28 deletions(-) create mode 100644 debug-app/boards/overlay.conf create mode 100644 debug-python/debug.py create mode 100755 debug-python/helper.py create mode 100755 debug-python/socket-run.py create mode 100755 scripts/run_debug.sh diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 3d2eb92..0234c35 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,9 +4,9 @@ "version": "2.0.0", "tasks": [ { - "label": "Build app posix", + "label": "Build app native", "type": "shell", - "command": "./scripts/docker_run.sh west build -b native_sim debug-app/app --build-dir .build-debug-app-native", + "command": "./scripts/docker_run.sh west build -b native_sim debug-app/app --build-dir .build-debug-app-native -- -DCONF_FILE=\"prj.conf ../boards/overlay.conf\"", "problemMatcher": [ "$gcc" ], @@ -48,21 +48,21 @@ "clear": true } }, - // { - // "label": "Run Posix", - // "type": "shell", - // "command": " ./scripts/docker_run.sh west build -t run --build-dir .build-debug-app-qemu", - // "problemMatcher": [ - // "$gcc" - // ], - // "group": { - // "kind": "build", - // "isDefault": true - // }, - // "presentation": { - // "clear": true - // } - // }, + { + "label": "Debug native", + "type": "shell", + "command": "./scripts/docker_run.sh ./scripts/run_debug.sh", + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "clear": true + } + }, { "label": "Clean", "type": "shell", diff --git a/debug-app/app/prj.conf b/debug-app/app/prj.conf index 589d0ce..e55a350 100644 --- a/debug-app/app/prj.conf +++ b/debug-app/app/prj.conf @@ -9,10 +9,11 @@ CONFIG_NEWLIB_LIBC=y # Zephyr CONFIG_MAIN_STACK_SIZE=2048 -CONFIG_HEAP_MEM_POOL_SIZE=512 +CONFIG_HEAP_MEM_POOL_SIZE=65535 CONFIG_THREAD_NAME=y CONFIG_ASSERT=y -CONFIG_NO_OPTIMIZATIONS=n +CONFIG_DEBUG=n +CONFIG_BOOT_BANNER=n # Peripheral # CONFIG_ADC=y @@ -61,6 +62,7 @@ CONFIG_NET_L2_ETHERNET=y # CONFIG_PM_DEVICE=y # CONFIG_PM_DEVICE_RUNTIME=y + # Log CONFIG_LOG=y CONFIG_LOG_PROCESS_THREAD=y diff --git a/debug-app/app/src/main.cpp b/debug-app/app/src/main.cpp index 0cfd64a..7cf10b0 100644 --- a/debug-app/app/src/main.cpp +++ b/debug-app/app/src/main.cpp @@ -2,13 +2,16 @@ #include <zephyr/logging/log.h> #include <zephyr/zbus/zbus.h> +#include "utilities.h" #include "main-handler.h" #include "debug-handler.h" #include "driver-hook.h" -#include "ble-scanner.h" #include "device-parser.h" + +#if !defined(CONFIG_SOC_POSIX) +#include "ble-scanner.h" #include "wifi-controller.h" -#include "utilities.h" +#endif LOG_MODULE_REGISTER(main); @@ -51,11 +54,13 @@ ZBUS_CHAN_DEFINE(stateChannel, /* Name */ int main() { +#if !defined(CONFIG_SOC_POSIX) WifiController wifiController; + BleScanner bleScanner; +#endif + DebugHandler debugHanddler; MainHandler mainHandler; DriverHook driverHook; - DebugHandler debugHanddler; - BleScanner bleScanner; DeviceParser deviceParser; while(true) { diff --git a/debug-app/boards/overlay.conf b/debug-app/boards/overlay.conf new file mode 100644 index 0000000..bbf54be --- /dev/null +++ b/debug-app/boards/overlay.conf @@ -0,0 +1,12 @@ +#ble +CONFIG_BT=n +CONFIG_BT_OBSERVER=n + +#networking +CONFIG_WIFI=n +CONFIG_NETWORKING=n + +#zephyr +CONFIG_NO_OPTIMIZATIONS=y +CONFIG_DEBUG=y +CONFIG_BOOT_BANNER=y diff --git a/debug-app/drivers/CMakeLists.txt b/debug-app/drivers/CMakeLists.txt index 6f943b6..176ae59 100644 --- a/debug-app/drivers/CMakeLists.txt +++ b/debug-app/drivers/CMakeLists.txt @@ -1,5 +1,5 @@ -add_subdirectory(ble-scanner) -add_subdirectory(wifi-controller) +add_subdirectory_ifndef(CONFIG_SOC_POSIX ble-scanner) +add_subdirectory_ifndef(CONFIG_SOC_POSIX wifi-controller) add_subdirectory(ir-driver) add_subdirectory(servo-driver) add_subdirectory(led-driver) \ 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 39bab71..cecf966 100644 --- a/debug-app/module/handler/debug-handler/src/debug-handler.cpp +++ b/debug-app/module/handler/debug-handler/src/debug-handler.cpp @@ -2,7 +2,9 @@ #include <zephyr/logging/log.h> #include <zephyr/zbus/zbus.h> +#ifndef CONFIG_SOC_POSIX #include <zephyr/net/socket.h> +#endif #include <stdio.h> #include <string.h> @@ -32,6 +34,11 @@ int serv; int client; bool isDebugMode = false; +// Debug Client +bool isDriverAvailable = false; +bool isBleAvailable = false; +DebugMessage_t debugData; + ZBUS_LISTENER_DEFINE(debugDriverListener, context->DriverResponseListener); ZBUS_LISTENER_DEFINE(stateListener, context->StateListener); ZBUS_LISTENER_DEFINE(bleListener, context->BleListener); @@ -56,6 +63,7 @@ int DebugHandler::Deinit(){ } void DebugHandler::DebugThread(DebugHandler *context){ +#ifndef CONFIG_SOC_POSIX struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(PORT); @@ -118,6 +126,37 @@ void DebugHandler::DebugThread(DebugHandler *context){ isDebugMode = false; LOG_INF("Connection to %s close", addr_str); } +#else + while(1){ + std::vector<uint8_t> driverData; + std::vector<uint8_t> bleData; + uint8_t bleArray[128]; + uint8_t driverArray[128]; + uint16_t bleLen = 0; + uint16_t driverLen = 0; + + if(isDriverAvailable){ + driverData.insert(driverData.end(), &driverArray[0], &driverArray[driverLen]); + DriverMessage_t sentMessage = { + .number = (DriverNumb_e)0, //temporary + .value = driverData + }; + zbus_chan_pub(&driverResponseChannel, &sentMessage, K_MSEC(200)); + isDriverAvailable = false; + } + + if(isBleAvailable){ + bleData.insert(bleData.end(), &bleArray[0], &bleArray[bleLen]); + BleData_t sentMessage = { + .address = std::string(""), //temporary + .data = bleData + }; + zbus_chan_pub(&bleScanChannel, &sentMessage, K_MSEC(200)); + isBleAvailable = false; + } + k_msleep(100); + } +#endif } void DebugHandler::DriverResponseListener(const struct zbus_channel *chan){ @@ -165,6 +204,7 @@ void DebugHandler::BleListener(const struct zbus_channel *chan){ } int DebugHandler::SendPacket(uint8_t *buf, uint16_t size){ +#ifndef CONFIG_SOC_POSIX int sentlength, recvLength; recvLength = size; do { @@ -177,4 +217,7 @@ int DebugHandler::SendPacket(uint8_t *buf, uint16_t size){ recvLength-= sentlength; } while (recvLength); return recvLength; +#else + return 0; +#endif } \ No newline at end of file diff --git a/debug-python/debug.py b/debug-python/debug.py new file mode 100644 index 0000000..dd8649b --- /dev/null +++ b/debug-python/debug.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +import socket +import select +import gdb +import rpyc + + +TCP_IP = '192.168.8.149' +TCP_PORT = 12345 +BUFFER_SIZE = 1024 +MESSAGE = b'ABC' +received = 0 + +c = rpyc.connect("localhost", 18080) + +def GetState(): + return c.root.getState() + +def GetBle(): + return c.root.getBle() + +def GetDriver(): + return c.root.getDriver() + +class StateBp(gdb.Breakpoint): + def __init__(self): + gdb.Breakpoint.__init__(self, "main-handler.cpp:69") + + def stop(self): + # SocketStart() + state = GetState() + if state >= 0: + print('set var currentState = ' + str(state)) + gdb.execute('set var currentState = ' + str(state)) + return False + +class DriverBp(gdb.Breakpoint): + def __init__(self): + gdb.Breakpoint.__init__(self, "debug-handler.cpp:136") + + def stop(self): + bleData = GetBle() + driverData = GetDriver() + if len(bleData) > 0: + gdb.execute('set var bleLen = ' + str(len(bleData))) + gdb.execute('set var isBleAvailable = true') + # gdb.execute('print bleLen') + # gdb.execute('print isBleAvailable') + print(bleData) + for i, data in enumerate(bleData): + gdb.execute('set var bleArray[' +str(i) + '] =' + str(data)) + # gdb.execute('print bleArray') + + if len(driverData) > 0: + gdb.execute('set var driverLen = ' + str(len(driverData))) + gdb.execute('set var isDriverAvailable = true') + for i, data in enumerate(driverData): + gdb.execute('set var driverArray[' +str(i) + '] =' + str(data)) + + return False + +def SocketStart(): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((TCP_IP, TCP_PORT)) + s.setblocking(False) + s.send(MESSAGE) + + inputs = [s] + outputs = [s] + while inputs: + readable,writable,exceptional = select.select(inputs,outputs,inputs,1) + data = [] + + for s in readable: + data = s.recv(1024) + # logging.info(f'Non Blocking - data: {len(data)}') + # logging.info(f'Non Blocking - closing...') + if(len(data) >0): + if(data[0]==0): + global received + received = data[3] + print(received) + break + + s.close() + +bp1 = StateBp() +bp2 = DriverBp() \ No newline at end of file diff --git a/debug-python/helper.py b/debug-python/helper.py new file mode 100755 index 0000000..6bef84c --- /dev/null +++ b/debug-python/helper.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python + +import rpyc + +c = rpyc.connect("localhost", 18080) +c.root.socketStart() \ No newline at end of file diff --git a/debug-python/socket-run.py b/debug-python/socket-run.py new file mode 100755 index 0000000..def87e5 --- /dev/null +++ b/debug-python/socket-run.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python + +import socket +import select +import rpyc +from rpyc.utils.server import ThreadedServer + +TCP_IP = '192.168.8.149' +TCP_PORT = 12345 +BUFFER_SIZE = 1024 +MESSAGE = b'ABC' +received = 0 + +stateData = [] +bleData = [] +driverData = [] + +class DebugService(rpyc.Service): + def on_connect(self,conn): + print("connected") + # SocketStart() + + def on_disconnect(self,conn): + pass + + def parser(self, packet): + FLAG_BYTE = 0 + LENGTH_MSB = 1 + LENGTH_LSB = 2 + PAYLOAD_BYTE = 3 + + STATE_DATA = 0 + BLE_DATA = 1 + DRIVER_DATA = 2 + + dataType = FLAG_BYTE + dataLength = 0 + dataFlag = 0 + + index = 0 + while index < len(packet): + dataLength = ((packet[index + 1] << 8) + packet[index + 2]) + if(packet[index] == STATE_DATA): + stateData.append(packet[index+3]) + elif(packet[index] == BLE_DATA): + bleData.append(packet[index+3 : index + 3 + dataLength]) + elif(packet[index] == DRIVER_DATA): + driverData.append(packet[index+3 : index + 3 + dataLength]) + index += (3+dataLength) + + + def exposed_socketStart(self): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((TCP_IP, TCP_PORT)) + s.setblocking(False) + s.send(MESSAGE) + + inputs = [s] + outputs = [s] + while inputs: + readable,writable,exceptional = select.select(inputs,outputs,inputs,1) + data = [] + + for s in readable: + data = s.recv(1024) + self.parser(data) + + def exposed_getState(self): + if len(stateData) > 0: + return stateData.pop(0) + else: + return -1 + + def exposed_getDriver(self): + if len(driverData) > 0: + return driverData.pop(0) + else : + return [] + + def exposed_getBle(self): + if len(bleData) > 0: + return bleData.pop(0) + else : + return [] + + +def SocketStart(): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((TCP_IP, TCP_PORT)) + s.setblocking(False) + s.send(MESSAGE) + + inputs = [s] + outputs = [s] + while inputs: + readable,writable,exceptional = select.select(inputs,outputs,inputs,1) + data = [] + + for s in readable: + data = s.recv(1024) + # logging.info(f'Non Blocking - data: {len(data)}') + # logging.info(f'Non Blocking - closing...') + if(len(data) >0): + if(data[0]==0): + print(data) + global received + received = data[3] + +ts = ThreadedServer(DebugService,port=18080) +print('Service started on port 18080') +ts.start() \ No newline at end of file diff --git a/scripts/docker_run.sh b/scripts/docker_run.sh index fc9ef5d..2c2761c 100755 --- a/scripts/docker_run.sh +++ b/scripts/docker_run.sh @@ -8,10 +8,11 @@ DOCKER_ARGS="--privileged \ --rm=true \ --user="$(id --user):$(id --group)" \ -e ZEPHYR_SDK_INSTALL_DIR=/opt/toolchains/zephyr-sdk-0.16.4 \ --e CCACHE_DIR=$(pwd)/.ccache \ +-e CCACHE_DIR=$(pwd)/.ccache -v $(pwd):$(pwd) \ -v /dev/bus/usb:/dev/bus/usb \ --w $(pwd)" -DOCKER_IMAGE=zephyrprojectrtos/ci:v0.26.6 +-w $(pwd) \ +-it" +DOCKER_IMAGE=zephyrprojectrtos/ci:v0.26.6-t docker run $DOCKER_ARGS $DOCKER_IMAGE "$@" diff --git a/scripts/run_debug.sh b/scripts/run_debug.sh new file mode 100755 index 0000000..56787a0 --- /dev/null +++ b/scripts/run_debug.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +python debug-python/socket-run.py& +python debug-python/helper.py& + +gdb --command debug-python/debug.py .build-debug-app-native/zephyr/zephyr.exe \ No newline at end of file -- GitLab