diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ec36aa..0a769f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - ECC_SLOT1-32 numbering chabged to ECC_SLOT_0-31 - lt_handshake() function renamed to lt_session_start() -- Unit tests organization was updated +- Unit tests and platform tests organization was updated - Included lt_l2_api_structs.h and lt_l3_api_structs.h are automatically generated - SH0 pairing keypair now reflects first batch of TROPIC01 devices - Changed logging format in examples. @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - libtropic API: lt_update_mode() - Example folder and test folder is reworked - option LT_ADD_EXAMPLES controls if example's folder code is a part of compilation +- port/ support and tests/platform/ example for Raspberry Pi ### Fixed diff --git a/examples/lt_ex_test_reversible.c b/examples/lt_ex_test_reversible.c index b83604a..90fed1e 100644 --- a/examples/lt_ex_test_reversible.c +++ b/examples/lt_ex_test_reversible.c @@ -8,6 +8,7 @@ * @license For the license see file LICENSE.txt file in the root directory of this source tree. */ +#include "inttypes.h" #include "string.h" #include "libtropic.h" @@ -190,7 +191,7 @@ void lt_ex_test_reversible(void) LT_ASSERT(LT_OK, read_whole_R_config(&h, &r_config_read)); // Print r config for (int i=0; i<27;i++) { - LT_LOG(" %s, %08lX", get_conf_desc(i), r_config_read.obj[i]); + LT_LOG(" %s, %08" PRIX32, get_conf_desc(i), r_config_read.obj[i]); } LT_LOG("%s", "write_whole_R_config()"); @@ -206,7 +207,7 @@ void lt_ex_test_reversible(void) LT_ASSERT(LT_OK, read_whole_R_config(&h, &r_config_read)); // Print r config for (int i=0; i<27;i++) { - LT_LOG(" %s, %08lX", get_conf_desc(i), r_config_read.obj[i]); + LT_LOG(" %s, %08" PRIX32, get_conf_desc(i), r_config_read.obj[i]); } LT_LOG("%s", "lt_ping() "); @@ -298,7 +299,7 @@ void lt_ex_test_reversible(void) LT_ASSERT(LT_OK, read_whole_R_config(&h, &r_config_read)); // Print r config for (int i=0; i<27;i++) { - LT_LOG(" %s, %08lX", get_conf_desc(i), r_config_read.obj[i]); + LT_LOG(" %s, %08" PRIX32, get_conf_desc(i), r_config_read.obj[i]); } LT_LOG("lt_session_abort()"); diff --git a/hal/port/unix/lt_port_raspberrypi_wiringpi.c b/hal/port/unix/lt_port_raspberrypi_wiringpi.c new file mode 100644 index 0000000..0bb0ce1 --- /dev/null +++ b/hal/port/unix/lt_port_raspberrypi_wiringpi.c @@ -0,0 +1,122 @@ +/** + * @file lt_port_raspberrypi_spi.c + * @author Tropic Square s.r.o. + * @brief L1 implementation for raspberrypi. Expects wiringPi library to be + * installed on Raspberry Pi, for more info about this library check https://github.com/WiringPi/WiringPi/tree/master + * + * @license For the license see file LICENSE.txt file in the root directory of this source tree. + */ + +#include +#include +#include +#include +#include +#include /// for GPIO control +#include /// For SPI control + +#include "libtropic_common.h" +#include "libtropic_port.h" + +// CS is controlled separately +#define GPIO_CS 14 +#define SPI_SPEED_HZ 5000000 + +// File descriptor, used in init and deinit +int fd = 0; + +//#define NDEBUG +#ifdef NDEBUG +# define LOG_OUT(f_, ...) printf("[TCP] "f_, ##__VA_ARGS__) +# define LOG_ERR(f_, ...) fprintf(stderr, "ERROR: " f_, ##__VA_ARGS__) +# define LOG_U8_ARRAY(arr, len) \ + for (int i = 0; i < len; i++) \ + { \ + printf("%02x ", arr[i]); \ + } \ + printf("\r\n"); +#else +# define LOG_OUT(...) +# define LOG_ERR(...) +# define LOG_U8_ARRAY(...) +#endif + +lt_ret_t lt_port_delay (lt_handle_t *h, uint32_t wait_time_msecs) +{ + UNUSED(h); + LOG_OUT("-- Waiting for the target.\n"); + + usleep(wait_time_msecs*1000); + + return LT_OK; +} + +lt_ret_t lt_port_random_bytes(uint32_t *buff, uint16_t len) { + + for(int i=0; il2_buff + offset, tx_data_length); + + return LT_OK; +} + +lt_ret_t lt_port_spi_csn_low (lt_handle_t *h) +{ + UNUSED(h); + LOG_OUT("-- Driving Chip Select to Low.\n"); + + digitalWrite(GPIO_CS, LOW); + + return LT_OK; +} + +lt_ret_t lt_port_spi_csn_high (lt_handle_t *h) +{ + UNUSED(h); + LOG_OUT("-- Driving Chip Select to High.\n"); + + digitalWrite(GPIO_CS, HIGH); + + return LT_OK; +} diff --git a/hal/port/unix/lt_port_unix.c b/hal/port/unix/lt_port_unix_tcp.c similarity index 100% rename from hal/port/unix/lt_port_unix.c rename to hal/port/unix/lt_port_unix_tcp.c diff --git a/tests/platforms/libtropic-raspberrypi/CMakeLists.txt b/tests/platforms/libtropic-raspberrypi/CMakeLists.txt new file mode 100644 index 0000000..e54f56d --- /dev/null +++ b/tests/platforms/libtropic-raspberrypi/CMakeLists.txt @@ -0,0 +1,93 @@ +cmake_minimum_required(VERSION 3.21.0) + + +########################################################################### +# # +# Paths and setup # +# # +########################################################################### + +#set(CMAKE_BUILD_TYPE Debug) + +set(PATH_TREZOR_CRYPTO "../../vendor/trezor_crypto/") +set(PATH_LIBTROPIC "../../../") + + +########################################################################### +# # +# Define project's name # +# # +########################################################################### + +project(libtropic_unix_spi_example + VERSION 0.1.0 + DESCRIPTION "Example of libtropic's usage in Unix environment" + LANGUAGES C) + + +########################################################################### +# # +# Sources defined by this project # +# # +########################################################################### + +add_executable(run main.c + ${CMAKE_CURRENT_SOURCE_DIR}/${PATH_LIBTROPIC}hal/port/unix/lt_port_raspberrypi_wiringpi.c +) + + +########################################################################### +# # +# Define what examples will be compiled into main(). # +# If any of examples is enabled, libtropic is compiled with # +# examples exposed. # +# # +########################################################################### + +# -DLT_EX_HW_WALLET=1 +if(LT_EX_HW_WALLET) + set(LT_ADD_EXAMPLES ON) + target_compile_definitions(run PRIVATE LT_EX_HW_WALLET) +endif() +# -DLT_EX_HELLO_WORLD=1 +if(LT_EX_HELLO_WORLD) + set(LT_ADD_EXAMPLES ON) + target_compile_definitions(run PRIVATE LT_EX_HELLO_WORLD) +endif() +# -DLT_EX_TEST_REVERSIBLE=1 +if(LT_EX_TEST_REVERSIBLE) + set(LT_ADD_EXAMPLES ON) + target_compile_definitions(run PRIVATE LT_EX_TEST_REVERSIBLE) +endif() +# --DLT_EX_TEST_IREVERSIBLE=1 +if(LT_EX_TEST_IREVERSIBLE) + set(LT_ADD_EXAMPLES ON) + target_compile_definitions(run PRIVATE LT_EX_TEST_IREVERSIBLE) +endif() + +########################################################################### +# # +# Add libtropic and set it up # +# # +########################################################################### + +# Use trezor crypto as a source of backend cryptography code +set(LT_USE_TREZOR_CRYPTO ON) + +# Add path to libtropic's repository root folder +add_subdirectory(${PATH_LIBTROPIC} "libtropic") + +# Customize libtropic's compilation +target_compile_options(tropic PRIVATE -Wall) +target_compile_options(tropic PRIVATE -ffunction-sections -fdata-sections) +target_compile_options(tropic PRIVATE -Wno-implicit-function-declaration) + + +########################################################################### +# # +# Link executable # +# # +########################################################################### + +target_link_options(run PRIVATE -Wl,--gc-sections) +target_link_libraries(run PRIVATE tropic wiringPi) diff --git a/tests/platforms/libtropic-raspberrypi/README.md b/tests/platforms/libtropic-raspberrypi/README.md new file mode 100644 index 0000000..063cb34 --- /dev/null +++ b/tests/platforms/libtropic-raspberrypi/README.md @@ -0,0 +1,78 @@ +# Unix-spi example + +Wire chip like this: + +``` + Connector J8 on Raspberry Pi: + 3v3 --> 3V3 (1) (2) 5V + GPIO2 (3) (4) 5V + GPIO3 (5) (6) GND + GPIO4 (7) (8) GPIO14 <-- CS + GND --> GND (9) (10) GPIO15 + GPIO17 (11) (12) GPIO18 + GPIO27 (13) (14) GND + GPIO22 (15) (16) GPIO23 + 3V3 (17) (18) GPIO24 + MOSI --> GPIO10 (19) (20) GND + MISO --> GPIO9 (21) (22) GPIO25 + CLK --> GPIO11 (23) (24) GPIO8 + GND (25) (26) GPIO7 + GPIO0 (27) (28) GPIO1 + GPIO5 (29) (30) GND + GPIO6 (31) (32) GPIO12 + GPIO13 (33) (34) GND + GPIO19 (35) (36) GPIO16 + GPIO26 (37) (38) GPIO20 + GND (39) (40) GPIO21 + +``` + +This example is ready to be compiled on Raspberry Pi with official raspbian image. Hardware spi0 is used, but chip select pin is controlled separately. + +# Dependencies + +On raspberry pi install wiringPi from released package + +```bash +$ wget https://github.com/WiringPi/WiringPi/releases/download/3.14/wiringpi_3.14_arm64.deb + +$ sudo apt install ./wiringpi_3.14_arm64.deb +``` + +Check out installed version: + +```bash +$ gpio -v +gpio version: 3.14 +Copyright (c) 2012-2025 Gordon Henderson and contributors +This is free software with ABSOLUTELY NO WARRANTY. +For details type: gpio -warranty + +Hardware details: + Type: Pi 3, Revision: 02, Memory: 1024MB, Maker: Sony UK + +System details: + * Device tree present. + Model: Raspberry Pi 3 Model B Rev 1.2 + * Supports full user-level GPIO access via memory. + * Supports basic user-level GPIO access via /dev/gpiomem. + * Supports basic user-level GPIO access via /dev/gpiochip (slow). + +``` + +# Compile + +Use switches as described here TODO to control what examples will be compiled in. + +```bash +$ cd ./libtropic/tests/platforms/unix-spi + +$ mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DLT_EX_TEST_REVERSIBLE=1 .. && make + +``` + +# Execute + +```bash +$ ./libtropic-test +``` \ No newline at end of file diff --git a/tests/platforms/libtropic-raspberrypi/main.c b/tests/platforms/libtropic-raspberrypi/main.c new file mode 100644 index 0000000..0d31178 --- /dev/null +++ b/tests/platforms/libtropic-raspberrypi/main.c @@ -0,0 +1,34 @@ +/** + * @file main.c + * @author Tropic Square s.r.o. + * + * @license For the license see file LICENSE.txt file in the root directory of this source tree. + */ + +#include +#include "string.h" +#include "libtropic_examples.h" + +// rm -rf build/ && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DLT_EX_HELLO_WORLD=1 .. && make && cd ../ +int main(void) +{ + // Test routines +#ifdef LT_EX_TEST_REVERSIBLE + lt_ex_test_reversible(); +#endif +#ifdef LT_EX_TEST_IREVERSIBLE + lt_ex_test_ireversible(); +#endif + + // Full examples +#ifdef LT_FEATURES_FWUPDATE + lt_ex_fwupdate(); +#endif +#ifdef LT_EX_HELLO_WORLD + /*int*/ lt_ex_hello_world(); +#endif +#ifdef LT_EX_HW_WALLET + /*int*/ lt_ex_hardware_wallet(); +#endif + +} diff --git a/tests/platforms/unix/CMakeLists.txt b/tests/platforms/libtropic-unix/CMakeLists.txt similarity index 87% rename from tests/platforms/unix/CMakeLists.txt rename to tests/platforms/libtropic-unix/CMakeLists.txt index 78cba44..d018711 100644 --- a/tests/platforms/unix/CMakeLists.txt +++ b/tests/platforms/libtropic-unix/CMakeLists.txt @@ -31,8 +31,8 @@ project(libtropic_unix_example # # ########################################################################### -add_executable(integration_tests main.c - ${CMAKE_CURRENT_SOURCE_DIR}/${PATH_LIBTROPIC}hal/port/unix/lt_port_unix.c +add_executable(run main.c + ${CMAKE_CURRENT_SOURCE_DIR}/${PATH_LIBTROPIC}hal/port/unix/lt_port_unix_tcp.c ) @@ -47,22 +47,22 @@ add_executable(integration_tests main.c # -DLT_EX_HW_WALLET=1 if(LT_EX_HW_WALLET) set(LT_ADD_EXAMPLES ON) - target_compile_definitions(integration_tests PRIVATE LT_EX_HW_WALLET) + target_compile_definitions(run PRIVATE LT_EX_HW_WALLET) endif() # -DLT_EX_HELLO_WORLD=1 if(LT_EX_HELLO_WORLD) set(LT_ADD_EXAMPLES ON) - target_compile_definitions(integration_tests PRIVATE LT_EX_HELLO_WORLD) + target_compile_definitions(run PRIVATE LT_EX_HELLO_WORLD) endif() # -DLT_EX_TEST_REVERSIBLE=1 if(LT_EX_TEST_REVERSIBLE) set(LT_ADD_EXAMPLES ON) - target_compile_definitions(integration_tests PRIVATE LT_EX_TEST_REVERSIBLE) + target_compile_definitions(run PRIVATE LT_EX_TEST_REVERSIBLE) endif() # --DLT_EX_TEST_IREVERSIBLE=1 if(LT_EX_TEST_IREVERSIBLE) set(LT_ADD_EXAMPLES ON) - target_compile_definitions(integration_tests PRIVATE LT_EX_TEST_IREVERSIBLE) + target_compile_definitions(run PRIVATE LT_EX_TEST_IREVERSIBLE) endif() ########################################################################### @@ -89,5 +89,5 @@ target_compile_options(tropic PRIVATE -Wno-implicit-function-declaration) # # ########################################################################### -target_link_options(integration_tests PRIVATE -Wl,--gc-sections) -target_link_libraries(integration_tests PRIVATE tropic) +target_link_options(run PRIVATE -Wl,--gc-sections) +target_link_libraries(run PRIVATE tropic) diff --git a/tests/platforms/unix/integration_tests.md b/tests/platforms/libtropic-unix/integration_tests.md similarity index 100% rename from tests/platforms/unix/integration_tests.md rename to tests/platforms/libtropic-unix/integration_tests.md diff --git a/tests/platforms/unix/main.c b/tests/platforms/libtropic-unix/main.c similarity index 100% rename from tests/platforms/unix/main.c rename to tests/platforms/libtropic-unix/main.c