From ec9460b65e1e58ffa80585615e078a98a949e487 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Tue, 23 Jul 2024 17:18:02 -0400 Subject: [PATCH] Refactor nim2 (#24) * dev container * test fixups * print test headers * use docker build * fix nvs_entry_find * fix esp_netif * distinct type for XQueue pointer (helps compile errors) * fix merge --- .../.gitignore | 0 .../CMakeLists.txt | 0 .../Makefile | 0 .../README.md | 0 .../build-all.sh | 0 .../config.nims | 0 .../main/.gitignore | 0 .../main/CMakeLists.txt | 0 .../main/Kconfig.projbuild | 0 .../main/apps.nim | 0 .../main/nim.cfg | 0 .../main/ota_rpc_example.nim | 0 .../main/server.nim | 0 .../main/wifi_example_main.nim | 0 .../rpc_cli_test.nim | 0 .../sdkconfig | 0 .../sdkconfig.old | 0 nesper.nimble | 30 ++++++++++++------- src/nesper/build_utils/tasks.nim | 1 + src/nesper/consts.nim | 7 +++-- src/nesper/esp/net/esp_netif.nim | 1 + src/nesper/ethernet.nim | 4 +-- src/nesper/queues.nim | 2 +- src/nesper/servers/rpc/router.nim | 9 ++---- src/nesper/uarts.nim | 10 +++---- tests/c_headers/mock/esp32/rom/gpio.h | 0 tests/c_headers/mock/soc/gpio_reg.h | 0 tests/exec_tests/trouter_exec.nim | 5 ++-- 28 files changed, 40 insertions(+), 29 deletions(-) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/.gitignore (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/CMakeLists.txt (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/Makefile (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/README.md (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/build-all.sh (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/config.nims (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/.gitignore (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/CMakeLists.txt (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/Kconfig.projbuild (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/apps.nim (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/nim.cfg (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/ota_rpc_example.nim (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/server.nim (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/main/wifi_example_main.nim (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/rpc_cli_test.nim (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/sdkconfig (100%) rename esp-idf-examples/{simplewifi-rpc => simplewifi_rpc}/sdkconfig.old (100%) create mode 100644 tests/c_headers/mock/esp32/rom/gpio.h create mode 100644 tests/c_headers/mock/soc/gpio_reg.h diff --git a/esp-idf-examples/simplewifi-rpc/.gitignore b/esp-idf-examples/simplewifi_rpc/.gitignore similarity index 100% rename from esp-idf-examples/simplewifi-rpc/.gitignore rename to esp-idf-examples/simplewifi_rpc/.gitignore diff --git a/esp-idf-examples/simplewifi-rpc/CMakeLists.txt b/esp-idf-examples/simplewifi_rpc/CMakeLists.txt similarity index 100% rename from esp-idf-examples/simplewifi-rpc/CMakeLists.txt rename to esp-idf-examples/simplewifi_rpc/CMakeLists.txt diff --git a/esp-idf-examples/simplewifi-rpc/Makefile b/esp-idf-examples/simplewifi_rpc/Makefile similarity index 100% rename from esp-idf-examples/simplewifi-rpc/Makefile rename to esp-idf-examples/simplewifi_rpc/Makefile diff --git a/esp-idf-examples/simplewifi-rpc/README.md b/esp-idf-examples/simplewifi_rpc/README.md similarity index 100% rename from esp-idf-examples/simplewifi-rpc/README.md rename to esp-idf-examples/simplewifi_rpc/README.md diff --git a/esp-idf-examples/simplewifi-rpc/build-all.sh b/esp-idf-examples/simplewifi_rpc/build-all.sh similarity index 100% rename from esp-idf-examples/simplewifi-rpc/build-all.sh rename to esp-idf-examples/simplewifi_rpc/build-all.sh diff --git a/esp-idf-examples/simplewifi-rpc/config.nims b/esp-idf-examples/simplewifi_rpc/config.nims similarity index 100% rename from esp-idf-examples/simplewifi-rpc/config.nims rename to esp-idf-examples/simplewifi_rpc/config.nims diff --git a/esp-idf-examples/simplewifi-rpc/main/.gitignore b/esp-idf-examples/simplewifi_rpc/main/.gitignore similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/.gitignore rename to esp-idf-examples/simplewifi_rpc/main/.gitignore diff --git a/esp-idf-examples/simplewifi-rpc/main/CMakeLists.txt b/esp-idf-examples/simplewifi_rpc/main/CMakeLists.txt similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/CMakeLists.txt rename to esp-idf-examples/simplewifi_rpc/main/CMakeLists.txt diff --git a/esp-idf-examples/simplewifi-rpc/main/Kconfig.projbuild b/esp-idf-examples/simplewifi_rpc/main/Kconfig.projbuild similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/Kconfig.projbuild rename to esp-idf-examples/simplewifi_rpc/main/Kconfig.projbuild diff --git a/esp-idf-examples/simplewifi-rpc/main/apps.nim b/esp-idf-examples/simplewifi_rpc/main/apps.nim similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/apps.nim rename to esp-idf-examples/simplewifi_rpc/main/apps.nim diff --git a/esp-idf-examples/simplewifi-rpc/main/nim.cfg b/esp-idf-examples/simplewifi_rpc/main/nim.cfg similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/nim.cfg rename to esp-idf-examples/simplewifi_rpc/main/nim.cfg diff --git a/esp-idf-examples/simplewifi-rpc/main/ota_rpc_example.nim b/esp-idf-examples/simplewifi_rpc/main/ota_rpc_example.nim similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/ota_rpc_example.nim rename to esp-idf-examples/simplewifi_rpc/main/ota_rpc_example.nim diff --git a/esp-idf-examples/simplewifi-rpc/main/server.nim b/esp-idf-examples/simplewifi_rpc/main/server.nim similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/server.nim rename to esp-idf-examples/simplewifi_rpc/main/server.nim diff --git a/esp-idf-examples/simplewifi-rpc/main/wifi_example_main.nim b/esp-idf-examples/simplewifi_rpc/main/wifi_example_main.nim similarity index 100% rename from esp-idf-examples/simplewifi-rpc/main/wifi_example_main.nim rename to esp-idf-examples/simplewifi_rpc/main/wifi_example_main.nim diff --git a/esp-idf-examples/simplewifi-rpc/rpc_cli_test.nim b/esp-idf-examples/simplewifi_rpc/rpc_cli_test.nim similarity index 100% rename from esp-idf-examples/simplewifi-rpc/rpc_cli_test.nim rename to esp-idf-examples/simplewifi_rpc/rpc_cli_test.nim diff --git a/esp-idf-examples/simplewifi-rpc/sdkconfig b/esp-idf-examples/simplewifi_rpc/sdkconfig similarity index 100% rename from esp-idf-examples/simplewifi-rpc/sdkconfig rename to esp-idf-examples/simplewifi_rpc/sdkconfig diff --git a/esp-idf-examples/simplewifi-rpc/sdkconfig.old b/esp-idf-examples/simplewifi_rpc/sdkconfig.old similarity index 100% rename from esp-idf-examples/simplewifi-rpc/sdkconfig.old rename to esp-idf-examples/simplewifi_rpc/sdkconfig.old diff --git a/nesper.nimble b/nesper.nimble index aaa379f..59ae109 100644 --- a/nesper.nimble +++ b/nesper.nimble @@ -1,6 +1,6 @@ # Package -version = "0.7.0" +version = "0.7.1" author = "Jaremy Creechley" description = "Nim wrappers for ESP-IDF (ESP32)" license = "Apache-2.0" @@ -20,39 +20,49 @@ requires "bytesequtils" # Tasks import os, strutils +proc header(msg: string) = + echo "\n\n", msg, "\n" +proc testExec(cmd: string) = + echo "running: ", cmd + exec(cmd) + proc general_tests() = # Regular tests + header "=== Regular Tests ===" for dtest in listFiles("tests/"): if dtest.splitFile()[1].startsWith("t") and dtest.endsWith(".nim"): echo("Testing: " & $dtest) - exec "nim c --compileOnly:on --cincludes:c_headers/mock/ --os:freertos $1" % [dtest] + testExec "nim c --verbosity:0 --compileOnly:on --cincludes:c_headers/mock/ --os:freertos $1" % [dtest] proc driver_tests() = # Driver tests + header "=== Driver Tests ===" for dtest in listFiles("tests/driver/"): if dtest.splitFile()[1].startsWith("t") and dtest.endsWith(".nim"): - echo("Testing: " & $dtest) - exec "nim c --compileOnly:on --cincludes:c_headers/mock/ --os:freertos $1" % [dtest] + echo("\nTesting: " & $dtest) + testExec "nim c --verbosity:0 --compileOnly:on --cincludes:c_headers/mock/ --os:freertos $1" % [dtest] proc storage_tests() = - # Driver tests + # Storage tests + header "=== Storage Tests ===" for dtest in listFiles("tests/storage/"): if dtest.splitFile()[1].startsWith("t") and dtest.endsWith(".nim"): - echo("Testing: " & $dtest) - exec "nim c --compileOnly:on --cincludes:c_headers/mock/ --os:freertos $1" % [dtest] + echo("\nTesting: " & $dtest) + testExec "nim c --verbosity:0 --compileOnly:on --cincludes:c_headers/mock/ --os:freertos $1" % [dtest] proc exec_tests() = # Exec tests + header "=== Exec Tests ===" for dtest in listFiles("tests/exec_tests/"): if dtest.splitFile()[1].startsWith("t") and dtest.endsWith(".nim"): - echo("Testing: " & $dtest) - exec "nim c -r --cincludes:$2/tests/c_headers/mock/ $1" % [dtest, getCurrentDir()] + echo("\nTesting: " & $dtest) + testExec "nim c --verbosity:0 -r --cincludes:$2/tests/c_headers/mock/ $1" % [dtest, getCurrentDir()] task test, "Runs the test suite": general_tests() driver_tests() storage_tests() - # exec_tests() + exec_tests() task test_general, "Runs the test suite": general_tests() diff --git a/src/nesper/build_utils/tasks.nim b/src/nesper/build_utils/tasks.nim index cfbd7e3..25a8e77 100644 --- a/src/nesper/build_utils/tasks.nim +++ b/src/nesper/build_utils/tasks.nim @@ -15,6 +15,7 @@ type args: seq[string] child_args: seq[string] cachedir: string + esp_idf_major: string esp_idf_version: string wifi_args: string debug: bool diff --git a/src/nesper/consts.nim b/src/nesper/consts.nim index a1c917a..cb9aa9e 100644 --- a/src/nesper/consts.nim +++ b/src/nesper/consts.nim @@ -146,9 +146,10 @@ type type ## *< a number that identifies an event with respect to a base - esp_event_handler_t* = proc (event_handler_arg: esp_event_base_t; - event_base: esp_event_base_t; event_id: int32; - event_data: pointer) {.cdecl.} + esp_event_handler_t* = proc (event_handler_arg: pointer; + event_base: esp_event_base_t; + event_id: int32; + event_data: pointer) {.cdecl.} type bits* = distinct int diff --git a/src/nesper/esp/net/esp_netif.nim b/src/nesper/esp/net/esp_netif.nim index f7a8089..b576091 100644 --- a/src/nesper/esp/net/esp_netif.nim +++ b/src/nesper/esp/net/esp_netif.nim @@ -35,6 +35,7 @@ else: export esp_netif_impl export esp_netif_defaults + export esp_netif_impl export esp_wifi_types export esp_netif_ip_addr export esp_netif_types diff --git a/src/nesper/ethernet.nim b/src/nesper/ethernet.nim index fa26b52..501ab67 100644 --- a/src/nesper/ethernet.nim +++ b/src/nesper/ethernet.nim @@ -19,8 +19,8 @@ when defined(ESP_IDF_V4_0): import nesper/esp/net/tcpip_adapter export tcpip_adapter else: - import nesper/esp/net/esp_netif_types - import nesper/esp/net/esp_netif + import esp/net/esp_netif_types + import esp/net/esp_netif export esp_netif_types export esp_netif diff --git a/src/nesper/queues.nim b/src/nesper/queues.nim index d28aa4d..19a52e9 100644 --- a/src/nesper/queues.nim +++ b/src/nesper/queues.nim @@ -44,7 +44,7 @@ import esp/queue export queue type - XQueue*[N] = pointer + XQueue*[N] = distinct pointer template xQueueCreate*(uxQueueLength, uxItemSize: int): untyped = xQueueCreate(UBaseType_t(uxQueueLength), UBaseType_t(uxItemSize)) diff --git a/src/nesper/servers/rpc/router.nim b/src/nesper/servers/rpc/router.nim index cea4e21..d80c4d6 100644 --- a/src/nesper/servers/rpc/router.nim +++ b/src/nesper/servers/rpc/router.nim @@ -130,15 +130,12 @@ proc wrapError*(code: int, msg: string, id: JsonNode, result = %* { "code": code, "id": id, "message": escapeJson(msg), "data": data } if err != nil: result["stacktrace"] = %* err.getStackTraceEntries() - echo "Error generated: ", "result: ", result, " id: ", id -template wrapException(body: untyped) = +template wrapException*(body: untyped) = try: body - except: - let msg = getCurrentExceptionMsg() - echo("control server: invalid input: error: ", msg) - let resp = rpcInvalidRequest(msg) + except Exception as err: + let resp = rpcInvalidRequest(err.msg) return resp diff --git a/src/nesper/uarts.nim b/src/nesper/uarts.nim index 1821b2f..6261f15 100644 --- a/src/nesper/uarts.nim +++ b/src/nesper/uarts.nim @@ -11,7 +11,7 @@ export uart export consts export gpios.gpio_num_t -import bytesequtils +# import bytesequtils const TAG = "uarts" @@ -112,10 +112,10 @@ proc read*(uart: var Uart; result = nb proc write*(uart: var Uart; - data: string): SzBytes {.discardable.} = + data: openArray[byte]): SzBytes {.discardable.} = # // Write data to UART. - let bytes_written = uart_write_bytes(uart.port, data, data.len().csize_t) + let bytes_written = uart_write_bytes(uart.port, cast[cstring](data[0].unsafeAddr), data.len().csize_t) result = bytes_written.SzBytes() @@ -123,6 +123,6 @@ proc write*(uart: var Uart; data: var seq[byte], ): SzBytes {.discardable.} = # var buff = data[0..data.len] - var str = data.toStrBuf() - write(uart, str) + + write(uart, data.toOpenArray(0, data.high())) diff --git a/tests/c_headers/mock/esp32/rom/gpio.h b/tests/c_headers/mock/esp32/rom/gpio.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/c_headers/mock/soc/gpio_reg.h b/tests/c_headers/mock/soc/gpio_reg.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/exec_tests/trouter_exec.nim b/tests/exec_tests/trouter_exec.nim index b13b891..32422c0 100644 --- a/tests/exec_tests/trouter_exec.nim +++ b/tests/exec_tests/trouter_exec.nim @@ -40,7 +40,7 @@ assert res2["result"].getInt() == 3 ## Test Call 3: incorrect arguments ## -echo "\n## Call 3 ##" +echo "\n## Call 3: incorrect arguments ##" let err_res = %* { "jsonrpc": "2.0", "id": 1, @@ -58,7 +58,8 @@ var call3 = %* { } var res3 = rt1.route( call3 ) echo "Result 3: " -echo res3 +echo res3.pretty() +assert res3["error"].kind == JObject delete(res3["error"], "stacktrace") assert res3 == err_res