From 8a27f5b0642025e6d53e15f0edfd06dadb32c808 Mon Sep 17 00:00:00 2001 From: lucas Date: Wed, 17 Apr 2024 16:21:02 +0200 Subject: [PATCH] import http sample --- .../zephyr/simple-http/CMakeLists.txt | 188 ++++++++++++++ .../platforms/zephyr/simple-http/README.md | 16 ++ .../simple-http/boards/nucleo_f767zi.conf | 4 + .../simple-http/boards/qemu_cortex_a53.conf | 4 + .../platforms/zephyr/simple-http/prj.conf | 7 + .../zephyr/simple-http/src/http_get.h | 1 + .../platforms/zephyr/simple-http/src/main.c | 231 ++++++++++++++++++ .../zephyr/simple-http/src/test_wasm.h | 46 ++++ .../simple-http/src/test_wasm_riscv64.h | 41 ++++ .../zephyr/simple-http/to_c_header.py | 25 ++ .../zephyr/simple-http/wamr_sdk_config.cmake | 9 + .../zephyr/simple-http/wasm-apps/http_get.c | 127 ++++++++++ .../zephyr/simple-http/wasm-apps/inc/.gitkeep | 0 .../simple-http/wasm-apps/inc/pthread.h | 91 +++++++ 14 files changed, 790 insertions(+) create mode 100644 product-mini/platforms/zephyr/simple-http/CMakeLists.txt create mode 100644 product-mini/platforms/zephyr/simple-http/README.md create mode 100644 product-mini/platforms/zephyr/simple-http/boards/nucleo_f767zi.conf create mode 100644 product-mini/platforms/zephyr/simple-http/boards/qemu_cortex_a53.conf create mode 100644 product-mini/platforms/zephyr/simple-http/prj.conf create mode 100644 product-mini/platforms/zephyr/simple-http/src/http_get.h create mode 100644 product-mini/platforms/zephyr/simple-http/src/main.c create mode 100644 product-mini/platforms/zephyr/simple-http/src/test_wasm.h create mode 100644 product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h create mode 100644 product-mini/platforms/zephyr/simple-http/to_c_header.py create mode 100644 product-mini/platforms/zephyr/simple-http/wamr_sdk_config.cmake create mode 100644 product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c create mode 100644 product-mini/platforms/zephyr/simple-http/wasm-apps/inc/.gitkeep create mode 100644 product-mini/platforms/zephyr/simple-http/wasm-apps/inc/pthread.h diff --git a/product-mini/platforms/zephyr/simple-http/CMakeLists.txt b/product-mini/platforms/zephyr/simple-http/CMakeLists.txt new file mode 100644 index 000000000..505f01b40 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/CMakeLists.txt @@ -0,0 +1,188 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required(VERSION 3.8.2) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(wamr) + +enable_language (ASM) + +set (WAMR_BUILD_PLATFORM "zephyr") + +############################################################################################################################ +# __ __ __ __ _____ _____ ____ _ _ ______ _____ _____ _ _ _____ _______ _____ ____ _ _ # +# \ \ / /\ | \/ | __ \ / ____/ __ \| \ | | ____|_ _/ ____| | | | __ \ /\|__ __|_ _/ __ \| \ | | # +# \ \ /\ / / \ | \ / | |__) | | | | | | | \| | |__ | || | __| | | | |__) | / \ | | | || | | | \| | # +# \ \/ \/ / /\ \ | |\/| | _ / | | | | | | . ` | __| | || | |_ | | | | _ / / /\ \ | | | || | | | . ` | # +# \ /\ / ____ \| | | | | \ \ | |___| |__| | |\ | | _| || |__| | |__| | | \ \ / ____ \| | _| || |__| | |\ | # +# \/ \/_/ \_\_| |_|_| \_\ \_____\____/|_| \_|_| |_____\_____|\____/|_| \_\/_/ \_\_| |_____\____/|_| \_| # +############################################################################################################################ + +# Build as X86_32 by default, change to "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", "MIPS" or "XTENSA" +# if we want to support arm, thumb, mips or xtensa + +set (WAMR_BUILD_TARGET "THUMB") +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_LIBC_BUILTIN 1) +set (WAMR_BUILD_LIBC_WASI 1) # 1 to use sockets +set (WAMR_BUILD_LIB_PTHREAD 0) # 1 to use sockets => cause errors +set (WAMR_BUILD_GLOBAL_HEAP_POOL 1) +set (WAMR_BUILD_GLOBAL_HEAP_SIZE 131072) # 128 KB + +################################################### +# _____ ______ _______ ______ _ ___ __ # +# / ____| ____|__ __| | ____| \ | \ \ / / # +# | (___ | |__ | | | |__ | \| |\ \ / / # +# \___ \| __| | | | __| | . ` | \ \/ / # +# ____) | |____ | | | |____| |\ | \ / # +# |_____/|______| |_| |______|_| \_| \/ # +################################################### + +# Check if WAMR_ROOT_DIR is set +if(DEFINED ENV{WAMR_ROOT_DIR}) + set(WAMR_ROOT_DIR $ENV{WAMR_ROOT_DIR}) +else() + message(FATAL_ERROR "'WAMR_ROOT_DIR' need to be specified") +endif() +message("wasi-sdk was found at ${WAMR_ROOT_DIR}") + +# Check if WASI_SDK_PATH is set +if(NOT $ENV{WASI_SDK_PATH} STREQUAL "") + set(WASI_SDK_PATH $ENV{WASI_SDK_PATH}) +else() + find_program(WASM_C_COMPILER clang /opt/wasi-sdk/bin NO_DEFAULT_PATH) + if(NOT WASM_C_COMPILER) + message(FATAL_ERROR "'wasi-sdk' not found, please ensure wasi-sdk is installed.\ + You can download and install it from\ + https://github.com/WebAssembly/wasi-sdk/releases") + else() + set(WASI_SDK_PATH /opt/wasi-sdk) + endif() +endif() +message("wasi-sdk was found at ${WASI_SDK_PATH}") + +# Check if WAMR_APP_FRAMEWORK_DIR is set +if (DEFINED ENV{WAMR_APP_FRAMEWORK_DIR}) + set(WAMR_APP_FRAMEWORK_DIR $ENV{WAMR_APP_FRAMEWORK_DIR}) +else() + message(FATAL_ERROR "'wamr-app-framework' not found, please ensure they are installed.\ + You can download and install them from\ + https://github.com/bytecodealliance/wamr-app-framework") +endif() +message("wamr-app-framework was found at ${WAMR_APP_FRAMEWORK_DIR}") + +# set the WAMR_SDK_DIR with the path specified in the environment variable +set(WAMR_SDK_DIR + ${WAMR_APP_FRAMEWORK_DIR}/wamr-sdk +) + +# set the WAMR_LIBC_BUILTIN_DIR with the path specified in the environment variable +set(WAMR_LIBC_BUILTIN_DIR + ${WAMR_SDK_DIR}/wamr-sdk/app/libc-builtin-sysroot +) + +# set the WAMR_SDK_PACKAGE_OUT_DIR +set(WAMR_SDK_PACKAGE_OUT_DIR + ${CMAKE_CURRENT_BINARY_DIR}/wamr-sdk/app-sdk/wamr-app-framework +) + +# temp +set (PYTHON_EXECUTABLE + /usr/bin/python +) +# ~temp + +# # Reset linker flags +# set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") +# set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") + +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) +#include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) # in socket-api sample + +# Build the WAMR runtime +target_sources(app PRIVATE + ${WAMR_RUNTIME_LIB_SOURCE} + src/main.c) + +#################################################################################################### +# ____ _ _ _____ _ _____ __ __ _____ __ __ __ __ ____ _____ +# | _ \| | | |_ _| | | __ \ \ \ / /\ / ____| \/ | | \/ |/ __ \| __ \ +# | |_) | | | | | | | | | | | | \ \ /\ / / \ | (___ | \ / | | \ / | | | | | | | +# | _ <| | | | | | | | | | | | \ \/ \/ / /\ \ \___ \| |\/| | | |\/| | | | | | | | +# | |_) | |__| |_| |_| |____| |__| | \ /\ / ____ \ ____) | | | | | | | | |__| | |__| | +# |____/ \____/|_____|______|_____/ \/ \/_/ \_\_____/|_| |_| |_| |_|\____/|_____/ +#################################################################################################### + +add_dependencies(app wamr-sdk) + +include(ExternalProject) +# include(${WAMR_ROOT_DIR}/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake) +ExternalProject_Add(wamr-sdk + SOURCE_DIR + ${WAMR_SDK_DIR}/app + BINARY_DIR + ${CMAKE_CURRENT_BINARY_DIR}/wamr-sdk + CONFIGURE_COMMAND + ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ${WAMR_SDK_DIR}/app + -DWAMR_BUILD_SDK_PROFILE=simple + -DCONFIG_PATH=${CMAKE_CURRENT_SOURCE_DIR}/wamr_sdk_config.cmake + -DCMAKE_TOOLCHAIN_FILE=${WAMR_SDK_DIR}/app/wamr_toolchain.cmake + -DOUT_DIR=${CMAKE_CURRENT_BINARY_DIR}/wamr-sdk + -DWASI_SDK_DIR=${WASI_SDK_PATH} + BUILD_COMMAND + ${CMAKE_COMMAND} --build . + INSTALL_COMMAND + "" + BUILD_BYPRODUCTS + ${WAMR_SDK_PACKAGE_OUT_DIR}/include/wasm_app.h + ${WAMR_SDK_PACKAGE_OUT_DIR}/lib/libapp_framework.a + USES_TERMINAL +) + +set(WASM_APPS + http_get +) + +foreach(WASM_APP ${WASM_APPS}) + # Build a wasm module + add_custom_target(${WASM_APP} ALL + COMMENT + "Building ${WASM_APP}.wasm .." + COMMAND + ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps + COMMAND + ${WASI_SDK_PATH}/bin/clang -O3 + -I${WAMR_LIBC_BUILTIN_DIR}/include + -I${WAMR_SDK_PACKAGE_OUT_DIR}/include + -I/usr/include/ # temp + -L${WAMR_SDK_PACKAGE_OUT_DIR}/lib + -lapp_framework + -z stack-size=8192 -Wl,--initial-memory=65536 + -Wl,--no-entry -nostdlib -Wl,--allow-undefined + -Wl,--export=__heap_base,--export=__data_end + -Wl,--export=main + -o ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps/${WASM_APP}.wasm + ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps/${WASM_APP}.c + DEPENDS + wamr-sdk + ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps/${WASM_APP}.c + BYPRODUCTS + ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps/${WASM_APP}.wasm + USES_TERMINAL + ) + + + # Run python script to generate the header file + add_custom_command( + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/${WASM_APP}.h + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/to_c_header.py + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps/${WASM_APP}.wasm + COMMENT "Generating .h file from .wasm file..." + ) + + # Add the generated header file as a target + add_custom_target(${WASM_APP}.h ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/${WASM_APP}.h) + +endforeach() \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/README.md b/product-mini/platforms/zephyr/simple-http/README.md new file mode 100644 index 000000000..004acbdd0 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/README.md @@ -0,0 +1,16 @@ +# Simple http +This is a simple http client that make a GET request to a server. + +## Setup +TODO + +## Run Command +Replace `nucleo_h743zi` with your board name. + +```bash +ZEPHYR_BASE=~/zephyrproject/zephyr \ +WAMR_ROOT_DIR=~/wasm-micro-runtime \ +WASI_SDK_PATH=~/wasi-sdk-21.0 \ +WAMR_APP_FRAMEWORK_DIR=~/wamr-app-framework \ +west build . -b nucleo_h563zi -p always -- -DWAMR_BUILD_TARGET=THUMBV8 -DCONFIG_LOG_MODE_IMMEDIATE=y +``` \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/boards/nucleo_f767zi.conf b/product-mini/platforms/zephyr/simple-http/boards/nucleo_f767zi.conf new file mode 100644 index 000000000..c920e6fd0 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/boards/nucleo_f767zi.conf @@ -0,0 +1,4 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +CONFIG_ARM_MPU=y diff --git a/product-mini/platforms/zephyr/simple-http/boards/qemu_cortex_a53.conf b/product-mini/platforms/zephyr/simple-http/boards/qemu_cortex_a53.conf new file mode 100644 index 000000000..2cc2dd4b4 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/boards/qemu_cortex_a53.conf @@ -0,0 +1,4 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +CONFIG_ARM_MMU=n diff --git a/product-mini/platforms/zephyr/simple-http/prj.conf b/product-mini/platforms/zephyr/simple-http/prj.conf new file mode 100644 index 000000000..70444d4fa --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/prj.conf @@ -0,0 +1,7 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +CONFIG_STACK_SENTINEL=y +CONFIG_PRINTK=y +CONFIG_LOG=y +CONFIG_POSIX_API=n \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/src/http_get.h b/product-mini/platforms/zephyr/simple-http/src/http_get.h new file mode 100644 index 000000000..e921e7b31 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/src/http_get.h @@ -0,0 +1 @@ +unsigned char __aligned(4) wasm_test_file[] = { 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x23, 0x06, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x00, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x00, 0x02, 0x98, 0x01, 0x0b, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x00, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x70, 0x75, 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x67, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x07, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x73, 0x65, 0x6e, 0x64, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x72, 0x65, 0x63, 0x76, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x07, 0x70, 0x75, 0x74, 0x63, 0x68, 0x61, 0x72, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x05, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x10, 0x5f, 0x5f, 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x5f, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x04, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x65, 0x78, 0x69, 0x74, 0x00, 0x05, 0x03, 0x02, 0x01, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x15, 0x03, 0x7f, 0x01, 0x41, 0xd0, 0xd3, 0x00, 0x0b, 0x7f, 0x00, 0x41, 0xd0, 0x13, 0x0b, 0x7f, 0x00, 0x41, 0xd0, 0xd3, 0x00, 0x0b, 0x07, 0x2c, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x0b, 0x0a, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x03, 0x01, 0x0b, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x0a, 0xc6, 0x03, 0x01, 0xc3, 0x03, 0x02, 0x03, 0x7f, 0x01, 0x7e, 0x02, 0x7f, 0x23, 0x00, 0x41, 0x80, 0x01, 0x6b, 0x22, 0x00, 0x24, 0x00, 0x41, 0xce, 0x08, 0x10, 0x01, 0x1a, 0x41, 0xb4, 0x0b, 0x42, 0x82, 0x80, 0x80, 0x80, 0x10, 0x37, 0x02, 0x00, 0x20, 0x00, 0x41, 0xbe, 0x08, 0x41, 0xc9, 0x08, 0x41, 0xb0, 0x0b, 0x20, 0x00, 0x41, 0xfc, 0x00, 0x6a, 0x10, 0x02, 0x22, 0x01, 0x36, 0x02, 0x70, 0x41, 0xf7, 0x09, 0x20, 0x00, 0x41, 0xf0, 0x00, 0x6a, 0x10, 0x00, 0x1a, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x01, 0x04, 0x40, 0x41, 0x83, 0x08, 0x10, 0x01, 0x1a, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x28, 0x02, 0x7c, 0x22, 0x01, 0x29, 0x02, 0x08, 0x21, 0x05, 0x20, 0x01, 0x28, 0x02, 0x04, 0x21, 0x02, 0x20, 0x01, 0x28, 0x02, 0x14, 0x22, 0x03, 0x2f, 0x01, 0x02, 0x21, 0x04, 0x20, 0x00, 0x41, 0xe0, 0x00, 0x6a, 0x20, 0x03, 0x2f, 0x01, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00, 0x41, 0xe4, 0x00, 0x6a, 0x20, 0x04, 0x36, 0x02, 0x00, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x50, 0x20, 0x00, 0x20, 0x02, 0x36, 0x02, 0x54, 0x20, 0x00, 0x20, 0x05, 0x37, 0x03, 0x58, 0x41, 0x85, 0x09, 0x20, 0x00, 0x41, 0xd0, 0x00, 0x6a, 0x10, 0x00, 0x1a, 0x41, 0x02, 0x41, 0x01, 0x41, 0x06, 0x10, 0x03, 0x22, 0x01, 0x41, 0x7f, 0x46, 0x0d, 0x01, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x40, 0x41, 0xec, 0x09, 0x20, 0x00, 0x41, 0x40, 0x6b, 0x10, 0x00, 0x1a, 0x20, 0x01, 0x20, 0x00, 0x28, 0x02, 0x7c, 0x22, 0x02, 0x28, 0x02, 0x14, 0x20, 0x02, 0x28, 0x02, 0x10, 0x10, 0x04, 0x41, 0x7f, 0x46, 0x0d, 0x02, 0x20, 0x01, 0x41, 0x86, 0x0b, 0x41, 0x24, 0x41, 0x00, 0x10, 0x05, 0x41, 0x7f, 0x46, 0x0d, 0x03, 0x41, 0x8f, 0x0a, 0x10, 0x01, 0x1a, 0x02, 0x40, 0x20, 0x01, 0x41, 0xd0, 0x0b, 0x41, 0xff, 0x07, 0x41, 0x00, 0x10, 0x06, 0x22, 0x02, 0x41, 0x00, 0x4e, 0x04, 0x40, 0x03, 0x40, 0x20, 0x02, 0x45, 0x0d, 0x02, 0x20, 0x02, 0x41, 0xd0, 0x0b, 0x6a, 0x41, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x00, 0x41, 0xd0, 0x0b, 0x36, 0x02, 0x30, 0x41, 0x80, 0x08, 0x20, 0x00, 0x41, 0x30, 0x6a, 0x10, 0x00, 0x1a, 0x20, 0x01, 0x41, 0xd0, 0x0b, 0x41, 0xff, 0x07, 0x41, 0x00, 0x10, 0x06, 0x22, 0x02, 0x41, 0x00, 0x4e, 0x0d, 0x00, 0x0b, 0x0b, 0x41, 0xa7, 0x08, 0x10, 0x01, 0x1a, 0x0c, 0x01, 0x0b, 0x41, 0x0a, 0x10, 0x07, 0x1a, 0x20, 0x01, 0x10, 0x08, 0x1a, 0x0b, 0x20, 0x00, 0x41, 0x80, 0x01, 0x6a, 0x24, 0x00, 0x41, 0x00, 0x0c, 0x03, 0x0b, 0x20, 0x00, 0x10, 0x09, 0x28, 0x02, 0x00, 0x36, 0x02, 0x00, 0x41, 0xdc, 0x09, 0x20, 0x00, 0x10, 0x00, 0x1a, 0x41, 0x01, 0x10, 0x0a, 0x00, 0x0b, 0x20, 0x00, 0x10, 0x09, 0x28, 0x02, 0x00, 0x36, 0x02, 0x10, 0x41, 0x9a, 0x0a, 0x20, 0x00, 0x41, 0x10, 0x6a, 0x10, 0x00, 0x1a, 0x41, 0x01, 0x10, 0x0a, 0x00, 0x0b, 0x20, 0x00, 0x10, 0x09, 0x28, 0x02, 0x00, 0x36, 0x02, 0x20, 0x41, 0xd2, 0x0a, 0x20, 0x00, 0x41, 0x20, 0x6a, 0x10, 0x00, 0x1a, 0x41, 0x01, 0x10, 0x0a, 0x00, 0x0b, 0x0b, 0x0b, 0xb2, 0x03, 0x01, 0x00, 0x41, 0x80, 0x08, 0x0b, 0xaa, 0x03, 0x25, 0x73, 0x00, 0x55, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x72, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x2c, 0x20, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x72, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x00, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x00, 0x38, 0x30, 0x30, 0x30, 0x00, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x48, 0x54, 0x54, 0x50, 0x20, 0x47, 0x45, 0x54, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x3a, 0x38, 0x30, 0x30, 0x30, 0x2f, 0x00, 0x61, 0x64, 0x64, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x40, 0x25, 0x70, 0x3a, 0x20, 0x61, 0x69, 0x5f, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x61, 0x69, 0x5f, 0x73, 0x6f, 0x63, 0x6b, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x61, 0x69, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x73, 0x61, 0x5f, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x73, 0x69, 0x6e, 0x5f, 0x70, 0x6f, 0x72, 0x74, 0x3d, 0x25, 0x78, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x73, 0x6f, 0x63, 0x6b, 0x0a, 0x00, 0x73, 0x6f, 0x63, 0x6b, 0x20, 0x3d, 0x20, 0x25, 0x64, 0x0a, 0x00, 0x67, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3a, 0x20, 0x25, 0x64, 0x0a, 0x00, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x3a, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x28, 0x73, 0x6f, 0x63, 0x6b, 0x2c, 0x20, 0x72, 0x65, 0x73, 0x2d, 0x3e, 0x61, 0x69, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x2c, 0x20, 0x72, 0x65, 0x73, 0x2d, 0x3e, 0x61, 0x69, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x6c, 0x65, 0x6e, 0x29, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x28, 0x73, 0x6f, 0x63, 0x6b, 0x2c, 0x20, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x2c, 0x20, 0x53, 0x53, 0x54, 0x52, 0x4c, 0x45, 0x4e, 0x28, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x29, 0x2c, 0x20, 0x30, 0x29, 0x0a, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x26, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6c, 0x61, 0x6e, 0x67, 0x06, 0x31, 0x37, 0x2e, 0x30, 0x2e, 0x36, 0x00, 0x2c, 0x0f, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x02, 0x2b, 0x0f, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2d, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x73, 0x2b, 0x08, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x65, 0x78, 0x74 }; \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/src/main.c b/product-mini/platforms/zephyr/simple-http/src/main.c new file mode 100644 index 000000000..f275236f4 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/src/main.c @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include "bh_platform.h" +#include "bh_assert.h" +#include "bh_log.h" +#include "wasm_export.h" +#include "http_get.h" + +// For libc_wasi_parse_context_t +// #if WASM_ENABLE_LIBC_WASI != 0 +// #include "libc_wasi.c" +// #endif + +#define CONFIG_GLOBAL_HEAP_BUF_SIZE WASM_GLOBAL_HEAP_SIZE +#define CONFIG_APP_STACK_SIZE 8192 +#define CONFIG_APP_HEAP_SIZE 8192 + +#ifdef CONFIG_NO_OPTIMIZATIONS +#define CONFIG_MAIN_THREAD_STACK_SIZE 8192 +#else +#define CONFIG_MAIN_THREAD_STACK_SIZE 4096 +#endif + +static int app_argc; +static char **app_argv; + +/** + * Find the unique main function from a WASM module instance + * and execute that function. + * + * @param module_inst the WASM module instance + * @param argc the number of arguments + * @param argv the arguments array + * + * @return true if the main function is called, false otherwise. + */ +bool +wasm_application_execute_main(wasm_module_inst_t module_inst, int argc, + char *argv[]); + +static void * +app_instance_main(wasm_module_inst_t module_inst) +{ + const char *exception; + wasm_function_inst_t func; + wasm_exec_env_t exec_env; + unsigned argv[2] = { 0 }; + + // main fuction called on_init in module + if (wasm_runtime_lookup_function(module_inst, "main") + || wasm_runtime_lookup_function(module_inst, "__main_argc_argv")) { + LOG_VERBOSE("Calling main function\n"); + wasm_application_execute_main(module_inst, app_argc, app_argv); + } + else if ((func = wasm_runtime_lookup_function(module_inst, "app_main"))) { + exec_env = + wasm_runtime_create_exec_env(module_inst, CONFIG_APP_HEAP_SIZE); + if (!exec_env) { + os_printf("Create exec env failed\n"); + return NULL; + } + + LOG_VERBOSE("Calling app_main function\n"); + wasm_runtime_call_wasm(exec_env, func, 0, argv); + + if (!wasm_runtime_get_exception(module_inst)) { + os_printf("result: 0x%x\n", argv[0]); + } + + wasm_runtime_destroy_exec_env(exec_env); + } + else { + os_printf("Failed to lookup function main or app_main to call\n"); + return NULL; + } + + if ((exception = wasm_runtime_get_exception(module_inst))) + os_printf("%s\n", exception); + + return NULL; +} + +#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0 +static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 }; +#endif + +void +iwasm_main(void *arg1, void *arg2, void *arg3) +{ + // time + int start, end; + start = k_uptime_get_32(); + // .wasm file + uint8 *wasm_file_buf = NULL; + uint32 wasm_file_size; + // Runtime args + wasm_module_t wasm_module = NULL; + wasm_module_inst_t wasm_module_inst = NULL; + RuntimeInitArgs init_args; + char error_buf[128]; + +#if WASM_ENABLE_LOG != 0 + int log_verbose_level = 2; +#endif + + (void)arg1; + (void)arg2; + (void)arg3; + + memset(&init_args, 0, sizeof(RuntimeInitArgs)); + +#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0 + init_args.mem_alloc_type = Alloc_With_Pool; + init_args.mem_alloc_option.pool.heap_buf = global_heap_buf; + init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf); +#elif (defined(CONFIG_COMMON_LIBC_MALLOC) \ + && CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE != 0) \ + || defined(CONFIG_NEWLIB_LIBC) + init_args.mem_alloc_type = Alloc_With_System_Allocator; +#else +#error "memory allocation scheme is not defined." +#endif + +// Use the address pool in wasm-micro-runtime +// #if WASM_ENABLE_LIBC_WASI != 0 +// #define HUMAN_READABLE_ADDRESS "192.0.2.10\\24" +// libc_wasi_parse_context_t wasi_parse_ctx; +// memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx)); + +// libc_wasi_parse_result_t result = libc_wasi_parse(HUMAN_READABLE_ADDRESS, &wasi_parse_ctx); +// switch (result) { +// case LIBC_WASI_PARSE_RESULT_OK: +// continue; +// case LIBC_WASI_PARSE_RESULT_NEED_HELP: +// return; +// case LIBC_WASI_PARSE_RESULT_BAD_PARAM: +// return; +// } +// libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx); +// #endif + + /* initialize runtime environment */ + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); + return; + } + +#if WASM_ENABLE_LOG != 0 + bh_log_set_verbose_level(log_verbose_level); +#endif + + /* load WASM byte buffer from byte buffer of include file */ + wasm_file_buf = (uint8 *)wasm_test_file; + wasm_file_size = sizeof(wasm_test_file); + + /* load WASM module */ + if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, + error_buf, sizeof(error_buf)))) { + printf("%s\n", error_buf); + goto fail1; + } + + /* instantiate the module */ + if (!(wasm_module_inst = wasm_runtime_instantiate( + wasm_module, CONFIG_APP_STACK_SIZE, CONFIG_APP_HEAP_SIZE, + error_buf, sizeof(error_buf)))) { + printf("%s\n", error_buf); + goto fail2; + } + + /* invoke the main function */ + app_instance_main(wasm_module_inst); + +// +// #if WASM_ENABLE_LIBC_WASI != 0 +// if (ret == 0) { +// /* propagate wasi exit code. */ +// ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst); +// } +// #endif + + /* destroy the module instance */ + wasm_runtime_deinstantiate(wasm_module_inst); + +fail2: + /* unload the module */ + wasm_runtime_unload(wasm_module); + +fail1: + /* destroy runtime environment */ + wasm_runtime_destroy(); + + end = k_uptime_get_32(); + + printf("elapsed: %d\n", (end - start)); +} + +#define MAIN_THREAD_STACK_SIZE (CONFIG_MAIN_THREAD_STACK_SIZE) +#define MAIN_THREAD_PRIORITY 5 + +K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE); +static struct k_thread iwasm_main_thread; + +bool +iwasm_init(void) +{ + k_tid_t tid = k_thread_create( + &iwasm_main_thread, iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE, + iwasm_main, NULL, NULL, NULL, MAIN_THREAD_PRIORITY, 0, K_NO_WAIT); + return tid ? true : false; +} + +#if KERNEL_VERSION_NUMBER < 0x030400 /* version 3.4.0 */ +void +main(void) +{ + iwasm_init(); +} +#else +int +main(void) +{ + iwasm_init(); + return 0; +} +#endif diff --git a/product-mini/platforms/zephyr/simple-http/src/test_wasm.h b/product-mini/platforms/zephyr/simple-http/src/test_wasm.h new file mode 100644 index 000000000..a729cadef --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/src/test_wasm.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +/** + * The byte array buffer is the file content of a test wasm binary file, + * which is compiled by wasi-sdk toolchain from C source file of: + * product-mini/app-samples/hello-world/main.c. + */ +unsigned char __aligned(4) wasm_test_file[] = { + 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, + 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, + 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, + 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, + 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, + 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, + 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, + 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x13, 0x03, + 0x7F, 0x01, 0x41, 0xC0, 0x28, 0x0B, 0x7F, 0x00, 0x41, 0xBA, 0x08, 0x0B, + 0x7F, 0x00, 0x41, 0xC0, 0x28, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, + 0x6D, 0x6F, 0x72, 0x79, 0x02, 0x00, 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, + 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, + 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x04, 0x6D, 0x61, + 0x69, 0x6E, 0x00, 0x04, 0x0A, 0xB2, 0x01, 0x01, 0xAF, 0x01, 0x01, 0x03, + 0x7F, 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, + 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x88, 0x80, 0x80, 0x00, + 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, + 0x80, 0x08, 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, + 0x41, 0xA8, 0x88, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, + 0x1A, 0x41, 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, + 0x36, 0x02, 0x10, 0x41, 0x80, 0x88, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, + 0x10, 0x6A, 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, + 0x04, 0x20, 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x88, + 0x80, 0x80, 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, + 0x8D, 0x88, 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, + 0x36, 0x02, 0x00, 0x41, 0x93, 0x88, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, + 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, + 0x80, 0x00, 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, + 0x80, 0x00, 0x20, 0x04, 0x0B, 0x0B, 0x41, 0x01, 0x00, 0x41, 0x80, 0x08, + 0x0B, 0x3A, 0x62, 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, + 0x70, 0x0A, 0x00, 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, + 0x3A, 0x20, 0x25, 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, + 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, + 0x20, 0x62, 0x75, 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 +}; diff --git a/product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h b/product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h new file mode 100644 index 000000000..1b45211d7 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +unsigned char __aligned(4) wasm_test_file[] = { + 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, + 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, + 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, + 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, + 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, + 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, + 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, + 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x12, 0x03, + 0x7F, 0x01, 0x41, 0xC0, 0x01, 0x0B, 0x7F, 0x00, 0x41, 0x3A, 0x0B, 0x7F, + 0x00, 0x41, 0xC0, 0x01, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, 0x6D, + 0x6F, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x04, + 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, + 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, + 0x65, 0x03, 0x02, 0x0A, 0xB1, 0x01, 0x01, 0xAE, 0x01, 0x01, 0x03, 0x7F, + 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, 0x24, + 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x80, 0x80, 0x80, 0x00, 0x10, + 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, 0x10, + 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, 0x41, 0xA8, + 0x80, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, + 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, + 0x10, 0x41, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, 0x10, 0x6A, + 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, 0x04, 0x20, + 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x80, 0x80, 0x80, + 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, 0x8D, 0x80, + 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, + 0x00, 0x41, 0x93, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, 0x82, 0x80, + 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, 0x80, 0x00, + 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, + 0x20, 0x04, 0x0B, 0x0B, 0x40, 0x01, 0x00, 0x41, 0x00, 0x0B, 0x3A, 0x62, + 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, 0x70, 0x0A, 0x00, + 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, 0x3A, 0x20, 0x25, + 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, + 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, 0x20, 0x62, 0x75, + 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 +}; diff --git a/product-mini/platforms/zephyr/simple-http/to_c_header.py b/product-mini/platforms/zephyr/simple-http/to_c_header.py new file mode 100644 index 000000000..11f10b7cf --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/to_c_header.py @@ -0,0 +1,25 @@ +# Python script to convert wasm file to byte array in a .h file +import os + + +CWD = os.getcwd() + +CMAKE_CURRENT_BINARY_DIR = os.getenv('CMAKE_CURRENT_BINARY_DIR', CWD) +CMAKE_CURRENT_SOURCE_DIR = os.getenv('CMAKE_CURRENT_SOURCE_DIR', f'{CWD}/../src') + +print('CMAKE_CURRENT_BINARY_DIR:', CMAKE_CURRENT_BINARY_DIR) +print('CMAKE_CURRENT_SOURCE_DIR:', CMAKE_CURRENT_SOURCE_DIR) + +# Open the wasm file in binary mode and read the data +with open(f'{CMAKE_CURRENT_BINARY_DIR}/wasm-apps/http_get.wasm', 'rb') as f: + wasm_bytes = f.read() + +# Convert the bytes to a comma-separated string of hex values +byte_array = ', '.join(f'0x{byte:02x}' for byte in wasm_bytes) + +# Create the output string +output = f'unsigned char __aligned(4) wasm_test_file[] = {{ {byte_array} }};' + +# Write the output string to the .h file +with open(f'{CMAKE_CURRENT_SOURCE_DIR}/http_get.h', 'w') as f: + f.write(output) \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/wamr_sdk_config.cmake b/product-mini/platforms/zephyr/simple-http/wamr_sdk_config.cmake new file mode 100644 index 000000000..7a9065ab5 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/wamr_sdk_config.cmake @@ -0,0 +1,9 @@ +set (WAMR_BUILD_PLATFORM "linux") +set (WAMR_BUILD_TARGET X86_64) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_JIT 0) +set (WAMR_BUILD_LIBC_BUILTIN 1) +set (WAMR_BUILD_LIBC_WASI 1) +set (WAMR_BUILD_APP_FRAMEWORK 1) +set (WAMR_BUILD_APP_LIST WAMR_APP_BUILD_BASE WAMR_APP_BUILD_SENSOR WAMR_APP_BUILD_CONNECTION) diff --git a/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c b/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c new file mode 100644 index 000000000..ca377189b --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2017 Linaro Limited + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#if !defined(__ZEPHYR__) || defined(CONFIG_POSIX_API) + +#include +#include +#include +#include +#include +#include +#else +#include +#include +#endif + +// #ifdef __wasi__ +// #include +// #endif + + +/* HTTP server to connect to */ +#define HTTP_HOST "192.0.2.10" +/* Port to connect to, as string */ +#define HTTP_PORT "8000" +/* HTTP path to request */ +#define HTTP_PATH "/" + + +#define SSTRLEN(s) (sizeof(s) - 1) +#define CHECK(r) { if (r == -1) { printf("Error %d: " #r "\n", errno); exit(1); } } + +#define REQUEST "GET " HTTP_PATH " HTTP/1.0\r\nHost: " HTTP_HOST "\r\n\r\n" + +static char response[1024]; + +void dump_addrinfo(const struct addrinfo *ai) +{ + printf("addrinfo @%p: ai_family=%d, ai_socktype=%d, ai_protocol=%d, " + "sa_family=%d, sin_port=%x\n", + ai, ai->ai_family, ai->ai_socktype, ai->ai_protocol, + ai->ai_addr->sa_family, + ((struct sockaddr_in *)ai->ai_addr)->sin_port); +} + +int main(void) +{ + static struct addrinfo hints; + struct addrinfo *res; + int st, sock; + + printf("Preparing HTTP GET request for http://" HTTP_HOST + ":" HTTP_PORT HTTP_PATH "\n"); + + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + st = getaddrinfo(HTTP_HOST, HTTP_PORT, &hints, &res); + printf("getaddrinfo status: %d\n", st); + + if (st != 0) { + printf("Unable to resolve address, quitting\n"); + return 0; + } + + dump_addrinfo(res); + + //sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + CHECK(sock); + printf("sock = %d\n", sock); + + CHECK(connect(sock, res->ai_addr, res->ai_addrlen)); + // int rc = 0; + // for(int i = 0; i < 10; i++){ + // rc = connect(sock, res->ai_addr, res->ai_addrlen); + // if (rc == 0) { + // break; + // } + // else{ + // printf("[Debug] Connect try %d: status %d\n", i+1, errno); + // close(sock); + // k_sleep(K_MSEC(100)); // 10 mil seconds + // sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + // if (sock < 0) { + // printf("[Error] Unable to create socket, exiting..."); + // exit(1); + // } + // } + // k_sleep(K_MSEC(1000 * 5)); // 5 seconds + // } + // if(rc){ + // printf("[Error] Unable to Connect exiting..."); + // exit(1); + // } + + CHECK(send(sock, REQUEST, SSTRLEN(REQUEST), 0)); + + printf("Response:\n\n"); + + while (1) { + int len = recv(sock, response, sizeof(response) - 1, 0); + + if (len < 0) { + printf("Error reading response\n"); + return 0; + } + + if (len == 0) { + break; + } + + response[len] = 0; + printf("%s", response); + } + + printf("\n"); + + (void)close(sock); + return 0; +} diff --git a/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/.gitkeep b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/pthread.h b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/pthread.h new file mode 100644 index 000000000..10b3978e9 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/pthread.h @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _WAMR_LIB_PTHREAD_H +#define _WAMR_LIB_PTHREAD_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Data type define of pthread, mutex, cond and key */ +typedef unsigned int pthread_t; +typedef unsigned int pthread_mutex_t; +typedef unsigned int pthread_cond_t; +typedef unsigned int pthread_key_t; + +/* Thread APIs */ +int +pthread_create(pthread_t *thread, const void *attr, + void *(*start_routine)(void *), void *arg); + +int +pthread_join(pthread_t thread, void **retval); + +int +pthread_detach(pthread_t thread); + +int +pthread_cancel(pthread_t thread); + +pthread_t +pthread_self(void); + +void +pthread_exit(void *retval); + +/* Mutex APIs */ +int +pthread_mutex_init(pthread_mutex_t *mutex, const void *attr); + +int +pthread_mutex_lock(pthread_mutex_t *mutex); + +int +pthread_mutex_unlock(pthread_mutex_t *mutex); + +int +pthread_mutex_destroy(pthread_mutex_t *mutex); + +/* Cond APIs */ +int +pthread_cond_init(pthread_cond_t *cond, const void *attr); + +int +pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); + +int +pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + uint64_t useconds); + +int +pthread_cond_signal(pthread_cond_t *cond); + +int +pthread_cond_broadcast(pthread_cond_t *cond); + +int +pthread_cond_destroy(pthread_cond_t *cond); + +/* Pthread key APIs */ +int +pthread_key_create(pthread_key_t *key, void (*destructor)(void *)); + +int +pthread_setspecific(pthread_key_t key, const void *value); + +void * +pthread_getspecific(pthread_key_t key); + +int +pthread_key_delete(pthread_key_t key); + +#ifdef __cplusplus +} +#endif + +#endif /* end of _WAMR_LIB_PTHREAD_H */