├── .gitignore ├── .gitmodules ├── Dockerfile ├── README.md ├── docs ├── .keep ├── _config.yml ├── assets │ ├── css │ │ └── style.scss │ └── images │ │ └── logo.jpg ├── index.md └── nftables.rules ├── example ├── CMakeLists.txt ├── main │ ├── CMakeLists.txt │ └── main.c ├── sdkconfig.defaults └── simulator │ └── CMakeLists.txt └── src ├── CMakeLists.txt ├── esp_err.c ├── esp_event.c ├── esp_heap_caps.c ├── esp_log.c ├── freertos └── CMakeLists.txt ├── gui ├── DisplayWidget.cpp ├── DisplayWidget.h ├── gui.pro └── main.cpp ├── include ├── arch │ └── cc.h ├── esp32 │ └── rom │ │ └── ets_sys.h ├── esp_event.h ├── esp_event_internal.h ├── esp_event_private.h ├── esp_heap_caps.h ├── esp_rom_sys.h ├── esp_system.h ├── esp_wifi.h ├── freertos │ ├── FreeRTOS.h │ ├── StackMacros.h │ ├── event_groups.h │ ├── list.h │ ├── message_buffer.h │ ├── portable.h │ ├── portmacro.h │ ├── projdefs.h │ ├── queue.h │ ├── semphr.h │ ├── stack_macros.h │ ├── stream_buffer.h │ ├── task.h │ └── timers.h └── lwipopts.h ├── lwip ├── CMakeLists.txt └── extra │ ├── lwip │ └── sys.h │ └── tapif.c ├── mbedtls └── CMakeLists.txt └── startup.c /.gitignore: -------------------------------------------------------------------------------- 1 | /example/sdkconfig 2 | /example/sdkconfig.old 3 | /example/build 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "src/freertos/src"] 2 | path = src/freertos/src 3 | url = https://github.com/mireq/Posix_GCC_Simulator.git 4 | [submodule "src/lwip/src"] 5 | path = src/lwip/src 6 | url = https://git.savannah.nongnu.org/git/lwip.git 7 | [submodule "src/lwip/contrib"] 8 | path = src/lwip/contrib 9 | url = https://git.savannah.nongnu.org/git/lwip/lwip-contrib.git 10 | [submodule "src/mbedtls/src"] 11 | path = src/mbedtls/src 12 | url = https://github.com/ARMmbed/mbedtls.git 13 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:24.04 2 | 3 | ENV PRECONFIGURED_TAPIF=tap0 4 | 5 | COPY ./docs/nftables.rules /etc/nftables.rules 6 | 7 | RUN apt-get -y update && \ 8 | apt install -y git wget flex bison gperf python3 python3-pip python3-setuptools python3-serial python3-click python3-cryptography python3-future python3-pyparsing python3-pyelftools python3-venv cmake ninja-build ccache libffi-dev libssl-dev libusb-1.0-0 iproute2 nftables dfu-util 9 | RUN cd ~ &&\ 10 | git clone -b release/v5.2 --recursive https://github.com/espressif/esp-idf.git && \ 11 | ./esp-idf/install.sh && \ 12 | echo ". `pwd`/esp-idf/export.sh" >> ~/.bashrc && \ 13 | echo "ip tuntap add dev tap0 mode tap;\nip link set tap0 up;\nip addr add 10.0.0.1/24 dev tap0;\nnft -f /etc/nftables.rules;\ntail -f /dev/null" > /usr/bin/container_start && \ 14 | chmod +x /usr/bin/container_start 15 | 16 | CMD "/usr/bin/container_start" 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | docs/index.md -------------------------------------------------------------------------------- /docs/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mireq/esp32-simulator/883f10b46055396d3ff8c78e4a257db990a2db9b/docs/.keep -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | title: ESP32 simulator 3 | description: for linux 4 | -------------------------------------------------------------------------------- /docs/assets/css/style.scss: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | @import "{{ site.theme }}"; 5 | 6 | .page-header { 7 | position: relative; 8 | background: rgba(0, 0, 0, .5); 9 | } 10 | 11 | .page-header:before { 12 | content: ''; 13 | position: absolute; 14 | top: 0; 15 | left: 0; 16 | right: 0; 17 | bottom: 0; 18 | background: url(../images/logo.jpg) no-repeat center center; 19 | background-size: cover; 20 | z-index: -1; 21 | } 22 | 23 | .site-footer { 24 | display: none 25 | } 26 | -------------------------------------------------------------------------------- /docs/assets/images/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mireq/esp32-simulator/883f10b46055396d3ff8c78e4a257db990a2db9b/docs/assets/images/logo.jpg -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | This repository contains ESP32 simulator for linux. 2 | 3 | # How does it work? 4 | 5 | ESP-IDF contains small amount of hardware specific code, FreeRTOS and few 6 | multi-platform libraries. 7 | 8 | This project uses FreeRTOS port for POSIX, LWIP for TCP/IP layer and mbedtls for 9 | cryptography functions. 10 | 11 | In addition, several useful APIs from esp-idf are implemented eg. logging. 12 | 13 | With this project you can write a program, which will be runnable and testable 14 | on Linux. 15 | 16 | # Running 17 | 18 | ## Network 19 | 20 | This project requires tun kernel module and configured tap0 interface to work. 21 | To configure tap0 run following commands: 22 | 23 | ``` 24 | sudo ip tuntap add dev tap0 mode tap user `whoami` 25 | sudo ip link set tap0 up 26 | sudo ip addr add 10.0.0.1/24 dev tap0 27 | export PRECONFIGURED_TAPIF=tap0 28 | ``` 29 | 30 | Simulator can be connected to internet using masquerade. First we need to enable 31 | forwarding. 32 | 33 | ``` 34 | sudo echo 1 > /proc/sys/net/ipv4/ip_forward 35 | ``` 36 | 37 | Simple nftables forwarding rules: 38 | 39 | ``` 40 | #!/sbin/nft -f 41 | 42 | flush ruleset 43 | 44 | 45 | table inet filter { 46 | chain input { 47 | type filter hook input priority 0; policy drop; 48 | ct state invalid counter drop 49 | ct state {established, related} counter accept 50 | iif lo accept 51 | iif != lo ip daddr 127.0.0.1/8 counter drop 52 | iif != lo ip6 daddr ::1/128 counter drop 53 | ip protocol icmp counter accept 54 | ip6 nexthdr icmpv6 counter accept 55 | iifname tap0 accept 56 | } 57 | 58 | chain forward { 59 | type filter hook forward priority 0; policy accept; 60 | } 61 | 62 | chain output { 63 | type filter hook output priority 0; policy accept; 64 | } 65 | } 66 | 67 | 68 | table ip nat { 69 | chain input { 70 | type nat hook input priority 0; policy accept; 71 | ip protocol icmp accept 72 | } 73 | 74 | chain prerouting { 75 | type nat hook prerouting priority 0; policy accept; 76 | } 77 | 78 | chain postrouting { 79 | type nat hook postrouting priority 100; policy accept; 80 | ip daddr != 10.0.0.0/24 ip saddr 10.0.0.0/24 masquerade; 81 | } 82 | 83 | chain output { 84 | type nat hook output priority 0; policy accept; 85 | } 86 | } 87 | ``` 88 | 89 | ## Using ubuntu 90 | 91 | ``` 92 | sudo apt install -y git wget flex bison gperf python3 python3-pip python3-setuptools python3-serial python3-click python3-cryptography python3-future python3-pyparsing python3-pyelftools cmake ninja-build ccache libffi-dev libssl-dev libusb-1.0-0 93 | mkdir esp 94 | cd esp 95 | git clone -b release/v5.2 --recursive https://github.com/espressif/esp-idf.git 96 | ./esp-idf/install.sh 97 | . ./esp-idf/export.sh 98 | git clone --recursive https://github.com/mireq/esp32-simulator 99 | cd simulator/example 100 | idf.py build 101 | ./build/example 102 | ``` 103 | 104 | ## Inside docker 105 | 106 | ``` 107 | docker build -t esp32-simulator . 108 | docker run --cap-add=NET_ADMIN --device /dev/net/tun:/dev/net/tun --name esp32-simulator -v `pwd`:/root/simulator -d esp32-simulator 109 | 110 | docker exec -i -t esp32-simulator bash 111 | cd ~/simulator/example 112 | idf.py build 113 | ./build/example 114 | ``` 115 | -------------------------------------------------------------------------------- /docs/nftables.rules: -------------------------------------------------------------------------------- 1 | #!/sbin/nft -f 2 | 3 | flush ruleset 4 | 5 | 6 | table inet filter { 7 | chain input { 8 | type filter hook input priority 0; policy drop; 9 | ct state invalid counter drop 10 | ct state {established, related} counter accept 11 | iif lo accept 12 | iif != lo ip daddr 127.0.0.1/8 counter drop 13 | iif != lo ip6 daddr ::1/128 counter drop 14 | ip protocol icmp counter accept 15 | ip6 nexthdr icmpv6 counter accept 16 | iifname tap0 accept 17 | } 18 | 19 | chain forward { 20 | type filter hook forward priority 0; policy accept; 21 | } 22 | 23 | chain output { 24 | type filter hook output priority 0; policy accept; 25 | } 26 | } 27 | 28 | 29 | table ip nat { 30 | chain input { 31 | type nat hook input priority 0; policy accept; 32 | ip protocol icmp accept 33 | } 34 | 35 | chain prerouting { 36 | type nat hook prerouting priority 0; policy accept; 37 | } 38 | 39 | chain postrouting { 40 | type nat hook postrouting priority 100; policy accept; 41 | ip daddr != 10.0.0.0/24 ip saddr 10.0.0.0/24 masquerade; 42 | } 43 | 44 | chain output { 45 | type nat hook output priority 0; policy accept; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | 3 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 4 | project(example) 5 | 6 | idf_build_get_property(config_dir CONFIG_DIR) 7 | set(sdkconfig_header ${config_dir}/sdkconfig.h) 8 | set(project_elf ${CMAKE_PROJECT_NAME}.elf) 9 | ExternalProject_Add( 10 | linux 11 | SOURCE_DIR "${CMAKE_SOURCE_DIR}/simulator" 12 | INSTALL_COMMAND "" 13 | TEST_COMMAND "" 14 | BUILD_ALWAYS TRUE 15 | DEPENDS ${project_elf} 16 | CMAKE_ARGS -DCMAKE_ROOT_BUILD_DIR=${CMAKE_BINARY_DIR} -DESP_CONFIG_DIR=${config_dir} 17 | ) 18 | -------------------------------------------------------------------------------- /example/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register( 2 | SRCS 3 | "main.c" 4 | ) 5 | -------------------------------------------------------------------------------- /example/main/main.c: -------------------------------------------------------------------------------- 1 | #include "freertos/FreeRTOS.h" 2 | 3 | #include "lwip/dns.h" 4 | #include "lwip/err.h" 5 | #include "lwip/netdb.h" 6 | #include "lwip/sockets.h" 7 | #include "lwip/sys.h" 8 | 9 | #include "esp_event.h" 10 | #include "esp_log.h" 11 | #include "esp_system.h" 12 | #include "esp_wifi.h" 13 | 14 | 15 | static const char *TAG = "simulator"; 16 | 17 | 18 | // Declare events 19 | typedef enum { 20 | NETWORK_DISCONNECTED, 21 | NETWORK_CONNECTED, 22 | } network_event_t; 23 | 24 | ESP_EVENT_DECLARE_BASE(NETWORK_EVENT); 25 | ESP_EVENT_DEFINE_BASE(NETWORK_EVENT); 26 | 27 | esp_event_loop_handle_t event_loop; 28 | 29 | 30 | static void example_network_access() { 31 | const struct addrinfo hints = { 32 | .ai_family = AF_INET, 33 | .ai_socktype = SOCK_STREAM, 34 | }; 35 | 36 | ESP_LOGI(TAG, "DNS lookup. Address=github.com"); 37 | 38 | struct addrinfo *res; 39 | int err = getaddrinfo("github.com", "443", &hints, &res); 40 | 41 | if (err) { 42 | ESP_LOGE(TAG, "Cannot resolve: %d", err); 43 | if (res) { 44 | freeaddrinfo(res); 45 | } 46 | return; 47 | } 48 | 49 | char address[100]; 50 | inet_ntop(res->ai_family, &((struct sockaddr_in *)res->ai_addr)->sin_addr, address, sizeof(address)); 51 | ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", address); 52 | } 53 | 54 | 55 | static void on_network_event(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { 56 | switch (event_id) { 57 | case NETWORK_DISCONNECTED: 58 | ESP_LOGI(TAG, "Disconnected"); 59 | break; 60 | case NETWORK_CONNECTED: 61 | ESP_LOGI(TAG, "Connected"); 62 | example_network_access(); 63 | break; 64 | } 65 | } 66 | 67 | 68 | #ifdef SIMULATOR 69 | static void generate_fake_events_task(void *args) { 70 | // Simulate events 71 | vTaskDelay(100 / portTICK_PERIOD_MS); 72 | esp_event_post_to(event_loop, NETWORK_EVENT, NETWORK_CONNECTED, NULL, 0, portMAX_DELAY); 73 | vTaskDelay(5000 / portTICK_PERIOD_MS); 74 | esp_event_post_to(event_loop, NETWORK_EVENT, NETWORK_DISCONNECTED, NULL, 0, portMAX_DELAY); 75 | // Destroy unused task 76 | vTaskDelete(NULL); 77 | } 78 | #endif 79 | 80 | 81 | void app_main(void) 82 | { 83 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 84 | #ifdef SIMULATOR 85 | // Task for sending fake events 86 | xTaskCreate(generate_fake_events_task, "fake_init", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL); 87 | #endif 88 | 89 | // New event loop 90 | esp_event_loop_args_t loop_args = { 91 | .queue_size = 1, 92 | .task_name = "event_loop", 93 | .task_priority = 1, 94 | .task_stack_size = configMINIMAL_STACK_SIZE, 95 | .task_core_id = 0, 96 | }; 97 | ESP_ERROR_CHECK(esp_event_loop_create(&loop_args, &event_loop)); 98 | ESP_ERROR_CHECK(esp_event_handler_register_with(event_loop, NETWORK_EVENT, ESP_EVENT_ANY_ID, on_network_event, NULL)); 99 | 100 | for (;;) { 101 | vTaskDelay(1000 / portTICK_PERIOD_MS); 102 | ESP_LOGI(TAG, "Hello"); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /example/sdkconfig.defaults: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mireq/esp32-simulator/883f10b46055396d3ff8c78e4a257db990a2db9b/example/sdkconfig.defaults -------------------------------------------------------------------------------- /example/simulator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | 3 | project(example) 4 | 5 | add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../src ${CMAKE_CURRENT_BINARY_DIR}/simulator) 6 | 7 | include_directories( 8 | ${ESP_SIMULATOR_INCLUDE_DIRECTORIES} 9 | ) 10 | 11 | add_executable( 12 | ${PROJECT_NAME} 13 | ../main/main.c 14 | ) 15 | 16 | target_link_libraries( 17 | ${PROJECT_NAME} 18 | simulator 19 | ) 20 | 21 | add_definitions(-D_GNU_SOURCE -DSIMULATOR -g3 -ggdb) 22 | set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 11) 23 | set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_ROOT_BUILD_DIR}") 24 | set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "example") 25 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | 3 | project(simulator) 4 | 5 | set(ESP_SIMULATOR_INCLUDE_DIRECTORIES 6 | "${CMAKE_CURRENT_SOURCE_DIR}/include/" 7 | "${CMAKE_CURRENT_SOURCE_DIR}/freertos/src/FreeRTOS_Posix" 8 | "${CMAKE_CURRENT_SOURCE_DIR}/freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include" 9 | "${CMAKE_CURRENT_SOURCE_DIR}/freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/portable/GCC/Posix" 10 | "${CMAKE_CURRENT_SOURCE_DIR}/lwip/src/src/include" 11 | "${CMAKE_CURRENT_SOURCE_DIR}/lwip/contrib/ports/freertos/include" 12 | "${CMAKE_CURRENT_SOURCE_DIR}/lwip/contrib/ports/unix/port/include" 13 | "${CMAKE_CURRENT_SOURCE_DIR}/mbedtls/src/include" 14 | "${CMAKE_CURRENT_SOURCE_DIR}/mbedtls/src/crypto/include" 15 | "$ENV{IDF_PATH}/components/log/include/" 16 | "$ENV{IDF_PATH}/components/esp_common/include/" 17 | "$ENV{IDF_PATH}/components/esp_event/include/" 18 | "$ENV{IDF_PATH}/components/" 19 | "${CMAKE_BINARY_DIR}/../../../config/" 20 | ) 21 | set(ESP_SIMULATOR_INCLUDE_DIRECTORIES ${ESP_SIMULATOR_INCLUDE_DIRECTORIES} PARENT_SCOPE) 22 | 23 | add_subdirectory("freertos") 24 | add_subdirectory("lwip") 25 | add_subdirectory("mbedtls") 26 | 27 | include_directories( 28 | ${ESP_SIMULATOR_INCLUDE_DIRECTORIES} 29 | ) 30 | 31 | add_library(simulator STATIC 32 | esp_err.c 33 | esp_event.c 34 | esp_heap_caps.c 35 | esp_log.c 36 | startup.c 37 | "$ENV{IDF_PATH}/components/esp_common/src/esp_err_to_name.c" 38 | "$ENV{IDF_PATH}/components/esp_event/esp_event.c" 39 | ) 40 | 41 | add_definitions(-D_GNU_SOURCE -DSIMULATOR -g3 -ggdb) 42 | 43 | target_link_libraries( 44 | ${PROJECT_NAME} 45 | freertos 46 | lwip 47 | mbedtls 48 | pthread 49 | rt 50 | ) 51 | set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 11) 52 | -------------------------------------------------------------------------------- /src/esp_err.c: -------------------------------------------------------------------------------- 1 | #include "esp_err.h" 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | 5 | #define ets_printf printf 6 | 7 | 8 | static void esp_error_check_failed_print(const char *msg, esp_err_t rc, const char *file, int line, const char *function, const char *expression) 9 | { 10 | ets_printf("%s failed: esp_err_t 0x%x", msg, rc); 11 | #ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP 12 | ets_printf(" (%s)", esp_err_to_name(rc)); 13 | #endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP 14 | } 15 | 16 | void __attribute__((noreturn)) invoke_abort() { 17 | portEXIT_CRITICAL(); 18 | for (;;) { 19 | } 20 | } 21 | 22 | void __attribute__((noreturn)) _esp_error_check_failed(esp_err_t rc, const char *file, int line, const char *function, const char *expression) 23 | { 24 | esp_error_check_failed_print("ESP_ERROR_CHECK", rc, file, line, function, expression); 25 | invoke_abort(); 26 | } 27 | -------------------------------------------------------------------------------- /src/esp_event.c: -------------------------------------------------------------------------------- 1 | #include "esp_event.h" 2 | 3 | esp_err_t esp_event_loop_create_default(void) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/esp_heap_caps.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "esp_heap_caps.h" 4 | 5 | 6 | void *heap_caps_malloc(size_t size, uint32_t caps) { 7 | return malloc(size); 8 | } 9 | 10 | void heap_caps_free(void *ptr) { 11 | free(ptr); 12 | } 13 | -------------------------------------------------------------------------------- /src/esp_log.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "esp_log.h" 7 | 8 | 9 | uint32_t esp_log_timestamp(void) { 10 | struct timeval tv; 11 | gettimeofday(&tv, NULL); 12 | return (uint32_t)tv.tv_sec*1000 + (uint32_t)tv.tv_usec/1000; 13 | } 14 | 15 | void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) { 16 | va_list arglist; 17 | va_start(arglist, format); 18 | vprintf(format, arglist); 19 | va_end(arglist); 20 | } 21 | -------------------------------------------------------------------------------- /src/freertos/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(FREERTOS_SOURCES 2 | "croutine.c" 3 | "list.c" 4 | "queue.c" 5 | "tasks.c" 6 | "timers.c" 7 | "portable/GCC/Posix/port.c" 8 | "portable/MemMang/heap_3.c" 9 | ) 10 | include_directories( 11 | ${ESP_SIMULATOR_INCLUDE_DIRECTORIES} 12 | "${CMAKE_CURRENT_SOURCE_DIR}/src/FreeRTOS_Posix/FreeRTOS_Kernel/portable/GCC/Posix/" 13 | ) 14 | list(TRANSFORM FREERTOS_SOURCES PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/src/FreeRTOS_Posix/FreeRTOS_Kernel/) 15 | add_library(freertos STATIC ${FREERTOS_SOURCES}) 16 | add_definitions(-g3 -ggdb) 17 | -------------------------------------------------------------------------------- /src/gui/DisplayWidget.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "DisplayWidget.h" 12 | 13 | 14 | DisplayWidget::DisplayWidget(QWidget *parent): 15 | QOpenGLWidget(parent), 16 | framebuffer(nullptr) 17 | { 18 | int framebufferFd = shm_open("/simulator_fb", O_RDONLY, S_IRUSR | S_IWUSR); 19 | if (framebufferFd == -1) { 20 | qWarning("Framebuffer not opened"); 21 | } 22 | else { 23 | framebuffer = static_cast(mmap(NULL, FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT * 4, PROT_READ, MAP_SHARED, framebufferFd, 0)); 24 | if (framebuffer == MAP_FAILED || framebuffer == nullptr) { 25 | qWarning("Framebuffer map failed"); 26 | } 27 | ::close(framebufferFd); 28 | } 29 | 30 | 31 | this->setFixedSize(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT); 32 | 33 | QTimer *timer = new QTimer(this); 34 | timer->setSingleShot(false); 35 | connect(timer, &QTimer::timeout, this, qOverload<>(&QWidget::update)); 36 | if (format().swapInterval() == -1) { 37 | timer->setInterval(17); 38 | } 39 | else { 40 | timer->setInterval(0); 41 | } 42 | timer->start(); 43 | } 44 | 45 | DisplayWidget::~DisplayWidget() 46 | { 47 | if (framebuffer != MAP_FAILED && framebuffer != nullptr) { 48 | munmap(const_cast(static_cast(framebuffer)), FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT * 4); 49 | } 50 | } 51 | 52 | void DisplayWidget::initializeGL() 53 | { 54 | QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); 55 | f->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 56 | } 57 | 58 | void DisplayWidget::paintGL() 59 | { 60 | if (framebuffer) { 61 | QImage img(framebuffer, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, QImage::Format_RGBA8888); 62 | QPainter p(this); 63 | p.drawImage(QPoint(0, 0), img); 64 | } 65 | } 66 | 67 | void DisplayWidget::resizeGL(int w, int h) 68 | { 69 | QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); 70 | f->glViewport(0, 0, (GLint)w, (GLint)h); 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/gui/DisplayWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DISPLAYWIDGET_H_NXVA5J03 2 | #define DISPLAYWIDGET_H_NXVA5J03 3 | 4 | #include 5 | 6 | 7 | #define FRAMEBUFFER_WIDTH 1008 8 | #define FRAMEBUFFER_HEIGHT 256 9 | 10 | 11 | class DisplayWidget: public QOpenGLWidget 12 | { 13 | Q_OBJECT 14 | public: 15 | DisplayWidget(QWidget *parent = 0); 16 | virtual ~DisplayWidget(); 17 | 18 | protected: 19 | void initializeGL() override; 20 | void paintGL() override; 21 | void resizeGL(int w, int h) override; 22 | 23 | private: 24 | const uchar *framebuffer; 25 | }; /* ----- end of class DisplayWidget ----- */ 26 | 27 | #endif /* end of include guard: DISPLAYWIDGET_H_NXVA5J03 */ 28 | -------------------------------------------------------------------------------- /src/gui/gui.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | TARGET = gui 3 | INCLUDEPATH += . 4 | QT += opengl 5 | LIBS += -lrt 6 | SOURCES += main.cpp \ 7 | DisplayWidget.cpp 8 | HEADERS += DisplayWidget.h 9 | -------------------------------------------------------------------------------- /src/gui/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DisplayWidget.h" 3 | 4 | 5 | int main(int argc, char* argv[]) 6 | { 7 | QApplication app(argc, argv); 8 | DisplayWidget w; 9 | w.show(); 10 | return app.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /src/include/arch/cc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. The name of the author may not be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | * 27 | * This file is part of the lwIP TCP/IP stack. 28 | * 29 | * Author: Adam Dunkels 30 | * 31 | */ 32 | #ifndef LWIP_ARCH_CC_H 33 | #define LWIP_ARCH_CC_H 34 | 35 | /* Include some files for defining library routines */ 36 | #include 37 | #include 38 | #include 39 | 40 | /* see https://sourceforge.net/p/predef/wiki/OperatingSystems/ */ 41 | #if defined __ANDROID__ 42 | #define LWIP_UNIX_ANDROID 43 | #elif defined __linux__ 44 | #define LWIP_UNIX_LINUX 45 | #elif defined __APPLE__ 46 | #define LWIP_UNIX_MACH 47 | #elif defined __OpenBSD__ 48 | #define LWIP_UNIX_OPENBSD 49 | #elif defined __CYGWIN__ 50 | #define LWIP_UNIX_CYGWIN 51 | #endif 52 | 53 | #define LWIP_TIMEVAL_PRIVATE 0 54 | #include 55 | 56 | #define LWIP_ERRNO_INCLUDE 57 | 58 | #define LWIP_RAND() ((u32_t)rand()) 59 | 60 | /* prototypes for printf() and abort() */ 61 | #include 62 | #include 63 | /* Plaform specific diagnostic output */ 64 | #define LWIP_PLATFORM_DIAG(x) do {printf x;} while(0) 65 | 66 | #ifdef LWIP_UNIX_EMPTY_ASSERT 67 | #define LWIP_PLATFORM_ASSERT(x) 68 | #else 69 | #define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion \"%s\" failed at line %d in %s\n", \ 70 | x, __LINE__, __FILE__); fflush(NULL); abort();} while(0) 71 | #endif 72 | 73 | #if defined(LWIP_UNIX_ANDROID) && defined(FD_SET) 74 | typedef __kernel_fd_set fd_set; 75 | #endif 76 | 77 | #if defined(LWIP_UNIX_MACH) 78 | /* sys/types.h and signal.h bring in Darwin byte order macros. pull the 79 | header here and disable LwIP's version so that apps still can get 80 | the macros via LwIP headers and use system headers */ 81 | #include 82 | #define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS 83 | #endif 84 | 85 | struct sio_status_s; 86 | typedef struct sio_status_s sio_status_t; 87 | #define sio_fd_t sio_status_t* 88 | #define __sio_fd_t_defined 89 | 90 | #endif /* LWIP_ARCH_CC_H */ 91 | -------------------------------------------------------------------------------- /src/include/esp32/rom/ets_sys.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mireq/esp32-simulator/883f10b46055396d3ff8c78e4a257db990a2db9b/src/include/esp32/rom/ets_sys.h -------------------------------------------------------------------------------- /src/include/esp_event.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | 7 | #ifndef ESP_EVENT_H_ 8 | #define ESP_EVENT_H_ 9 | 10 | #include "esp_err.h" 11 | 12 | #include "freertos/FreeRTOS.h" 13 | #include "freertos/task.h" 14 | #include "freertos/queue.h" 15 | #include "freertos/semphr.h" 16 | 17 | #include "esp_event_base.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | /// Configuration for creating event loops 24 | typedef struct { 25 | int32_t queue_size; /**< size of the event loop queue */ 26 | const char *task_name; /**< name of the event loop task; if NULL, 27 | a dedicated task is not created for event loop*/ 28 | UBaseType_t task_priority; /**< priority of the event loop task, ignored if task name is NULL */ 29 | uint32_t task_stack_size; /**< stack size of the event loop task, ignored if task name is NULL */ 30 | BaseType_t task_core_id; /**< core to which the event loop task is pinned to, 31 | ignored if task name is NULL */ 32 | } esp_event_loop_args_t; 33 | 34 | /** 35 | * @brief Create a new event loop. 36 | * 37 | * @param[in] event_loop_args configuration structure for the event loop to create 38 | * @param[out] event_loop handle to the created event loop 39 | * 40 | * @return 41 | * - ESP_OK: Success 42 | * - ESP_ERR_INVALID_ARG: event_loop_args or event_loop was NULL 43 | * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list 44 | * - ESP_FAIL: Failed to create task loop 45 | * - Others: Fail 46 | */ 47 | esp_err_t esp_event_loop_create(const esp_event_loop_args_t *event_loop_args, esp_event_loop_handle_t *event_loop); 48 | 49 | /** 50 | * @brief Delete an existing event loop. 51 | * 52 | * @param[in] event_loop event loop to delete, must not be NULL 53 | * 54 | * @return 55 | * - ESP_OK: Success 56 | * - Others: Fail 57 | */ 58 | esp_err_t esp_event_loop_delete(esp_event_loop_handle_t event_loop); 59 | 60 | /** 61 | * @brief Create default event loop 62 | * 63 | * @return 64 | * - ESP_OK: Success 65 | * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list 66 | * - ESP_ERR_INVALID_STATE: Default event loop has already been created 67 | * - ESP_FAIL: Failed to create task loop 68 | * - Others: Fail 69 | */ 70 | esp_err_t esp_event_loop_create_default(void); 71 | 72 | /** 73 | * @brief Delete the default event loop 74 | * 75 | * @return 76 | * - ESP_OK: Success 77 | * - Others: Fail 78 | */ 79 | esp_err_t esp_event_loop_delete_default(void); 80 | 81 | /** 82 | * @brief Dispatch events posted to an event loop. 83 | * 84 | * This function is used to dispatch events posted to a loop with no dedicated task, i.e. task name was set to NULL 85 | * in event_loop_args argument during loop creation. This function includes an argument to limit the amount of time 86 | * it runs, returning control to the caller when that time expires (or some time afterwards). There is no guarantee 87 | * that a call to this function will exit at exactly the time of expiry. There is also no guarantee that events have 88 | * been dispatched during the call, as the function might have spent all the allotted time waiting on the event queue. 89 | * Once an event has been dequeued, however, it is guaranteed to be dispatched. This guarantee contributes to not being 90 | * able to exit exactly at time of expiry as (1) blocking on internal mutexes is necessary for dispatching the dequeued 91 | * event, and (2) during dispatch of the dequeued event there is no way to control the time occupied by handler code 92 | * execution. The guaranteed time of exit is therefore the allotted time + amount of time required to dispatch 93 | * the last dequeued event. 94 | * 95 | * In cases where waiting on the queue times out, ESP_OK is returned and not ESP_ERR_TIMEOUT, since it is 96 | * normal behavior. 97 | * 98 | * @param[in] event_loop event loop to dispatch posted events from, must not be NULL 99 | * @param[in] ticks_to_run number of ticks to run the loop 100 | * 101 | * @note encountering an unknown event that has been posted to the loop will only generate a warning, not an error. 102 | * 103 | * @return 104 | * - ESP_OK: Success 105 | * - Others: Fail 106 | */ 107 | esp_err_t esp_event_loop_run(esp_event_loop_handle_t event_loop, TickType_t ticks_to_run); 108 | 109 | /** 110 | * @brief Register an event handler to the system event loop (legacy). 111 | * 112 | * This function can be used to register a handler for either: (1) specific events, 113 | * (2) all events of a certain event base, or (3) all events known by the system event loop. 114 | * 115 | * - specific events: specify exact event_base and event_id 116 | * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id 117 | * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id 118 | * 119 | * Registering multiple handlers to events is possible. Registering a single handler to multiple events is 120 | * also possible. However, registering the same handler to the same event multiple times would cause the 121 | * previous registrations to be overwritten. 122 | * 123 | * @param[in] event_base the base ID of the event to register the handler for 124 | * @param[in] event_id the ID of the event to register the handler for 125 | * @param[in] event_handler the handler function which gets called when the event is dispatched 126 | * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called 127 | * 128 | * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should 129 | * ensure that event_handler_arg still points to a valid location by the time the handler gets called 130 | * 131 | * @return 132 | * - ESP_OK: Success 133 | * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler 134 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 135 | * - Others: Fail 136 | */ 137 | esp_err_t esp_event_handler_register(esp_event_base_t event_base, 138 | int32_t event_id, 139 | esp_event_handler_t event_handler, 140 | void *event_handler_arg); 141 | 142 | /** 143 | * @brief Register an event handler to a specific loop (legacy). 144 | * 145 | * This function behaves in the same manner as esp_event_handler_register, except the additional 146 | * specification of the event loop to register the handler to. 147 | * 148 | * @param[in] event_loop the event loop to register this handler function to, must not be NULL 149 | * @param[in] event_base the base ID of the event to register the handler for 150 | * @param[in] event_id the ID of the event to register the handler for 151 | * @param[in] event_handler the handler function which gets called when the event is dispatched 152 | * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called 153 | * 154 | * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should 155 | * ensure that event_handler_arg still points to a valid location by the time the handler gets called 156 | * 157 | * @return 158 | * - ESP_OK: Success 159 | * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler 160 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 161 | * - Others: Fail 162 | */ 163 | esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop, 164 | esp_event_base_t event_base, 165 | int32_t event_id, 166 | esp_event_handler_t event_handler, 167 | void *event_handler_arg); 168 | 169 | /** 170 | * @brief Register an instance of event handler to a specific loop. 171 | * 172 | * This function can be used to register a handler for either: (1) specific events, 173 | * (2) all events of a certain event base, or (3) all events known by the system event loop. 174 | * 175 | * - specific events: specify exact event_base and event_id 176 | * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id 177 | * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id 178 | * 179 | * Besides the error, the function returns an instance object as output parameter to identify each registration. 180 | * This is necessary to remove (unregister) the registration before the event loop is deleted. 181 | * 182 | * Registering multiple handlers to events, registering a single handler to multiple events as well as registering 183 | * the same handler to the same event multiple times is possible. 184 | * Each registration yields a distinct instance object which identifies it over the registration 185 | * lifetime. 186 | * 187 | * @param[in] event_loop the event loop to register this handler function to, must not be NULL 188 | * @param[in] event_base the base ID of the event to register the handler for 189 | * @param[in] event_id the ID of the event to register the handler for 190 | * @param[in] event_handler the handler function which gets called when the event is dispatched 191 | * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called 192 | * @param[out] instance An event handler instance object related to the registered event handler and data, can be NULL. 193 | * This needs to be kept if the specific callback instance should be unregistered before deleting the whole 194 | * event loop. Registering the same event handler multiple times is possible and yields distinct instance 195 | * objects. The data can be the same for all registrations. 196 | * If no unregistration is needed, but the handler should be deleted when the event loop is deleted, 197 | * instance can be NULL. 198 | * 199 | * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should 200 | * ensure that event_handler_arg still points to a valid location by the time the handler gets called 201 | * 202 | * @return 203 | * - ESP_OK: Success 204 | * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler 205 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID or instance is NULL 206 | * - Others: Fail 207 | */ 208 | esp_err_t esp_event_handler_instance_register_with(esp_event_loop_handle_t event_loop, 209 | esp_event_base_t event_base, 210 | int32_t event_id, 211 | esp_event_handler_t event_handler, 212 | void *event_handler_arg, 213 | esp_event_handler_instance_t *instance); 214 | 215 | /** 216 | * @brief Register an instance of event handler to the default loop. 217 | * 218 | * This function does the same as esp_event_handler_instance_register_with, except that it registers the 219 | * handler to the default event loop. 220 | * 221 | * @param[in] event_base the base ID of the event to register the handler for 222 | * @param[in] event_id the ID of the event to register the handler for 223 | * @param[in] event_handler the handler function which gets called when the event is dispatched 224 | * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called 225 | * @param[out] instance An event handler instance object related to the registered event handler and data, can be NULL. 226 | * This needs to be kept if the specific callback instance should be unregistered before deleting the whole 227 | * event loop. Registering the same event handler multiple times is possible and yields distinct instance 228 | * objects. The data can be the same for all registrations. 229 | * If no unregistration is needed, but the handler should be deleted when the event loop is deleted, 230 | * instance can be NULL. 231 | * 232 | * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should 233 | * ensure that event_handler_arg still points to a valid location by the time the handler gets called 234 | * 235 | * @return 236 | * - ESP_OK: Success 237 | * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler 238 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID or instance is NULL 239 | * - Others: Fail 240 | */ 241 | esp_err_t esp_event_handler_instance_register(esp_event_base_t event_base, 242 | int32_t event_id, 243 | esp_event_handler_t event_handler, 244 | void *event_handler_arg, 245 | esp_event_handler_instance_t *instance); 246 | 247 | /** 248 | * @brief Unregister a handler with the system event loop (legacy). 249 | * 250 | * Unregisters a handler, so it will no longer be called during dispatch. 251 | * Handlers can be unregistered for any combination of event_base and event_id which were previously registered. 252 | * To unregister a handler, the event_base and event_id arguments must match exactly the arguments passed to 253 | * esp_event_handler_register() when that handler was registered. Passing ESP_EVENT_ANY_BASE and/or ESP_EVENT_ANY_ID 254 | * will only unregister handlers that were registered with the same wildcard arguments. 255 | * 256 | * @note When using ESP_EVENT_ANY_ID, handlers registered to specific event IDs using the same base will not be 257 | * unregistered. When using ESP_EVENT_ANY_BASE, events registered to specific bases will also not be 258 | * unregistered. This avoids accidental unregistration of handlers registered by other users or components. 259 | * 260 | * @param[in] event_base the base of the event with which to unregister the handler 261 | * @param[in] event_id the ID of the event with which to unregister the handler 262 | * @param[in] event_handler the handler to unregister 263 | * 264 | * @return ESP_OK success 265 | * @return ESP_ERR_INVALID_ARG invalid combination of event base and event ID 266 | * @return others fail 267 | */ 268 | esp_err_t esp_event_handler_unregister(esp_event_base_t event_base, 269 | int32_t event_id, 270 | esp_event_handler_t event_handler); 271 | 272 | /** 273 | * @brief Unregister a handler from a specific event loop (legacy). 274 | * 275 | * This function behaves in the same manner as esp_event_handler_unregister, except the additional specification of 276 | * the event loop to unregister the handler with. 277 | * 278 | * @param[in] event_loop the event loop with which to unregister this handler function, must not be NULL 279 | * @param[in] event_base the base of the event with which to unregister the handler 280 | * @param[in] event_id the ID of the event with which to unregister the handler 281 | * @param[in] event_handler the handler to unregister 282 | * 283 | * @return 284 | * - ESP_OK: Success 285 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 286 | * - Others: Fail 287 | */ 288 | esp_err_t esp_event_handler_unregister_with(esp_event_loop_handle_t event_loop, 289 | esp_event_base_t event_base, 290 | int32_t event_id, 291 | esp_event_handler_t event_handler); 292 | 293 | /** 294 | * @brief Unregister a handler instance from a specific event loop. 295 | * 296 | * Unregisters a handler instance, so it will no longer be called during dispatch. 297 | * Handler instances can be unregistered for any combination of event_base and event_id which were previously 298 | * registered. To unregister a handler instance, the event_base and event_id arguments must match exactly the 299 | * arguments passed to esp_event_handler_instance_register() when that handler instance was registered. 300 | * Passing ESP_EVENT_ANY_BASE and/or ESP_EVENT_ANY_ID will only unregister handler instances that were registered 301 | * with the same wildcard arguments. 302 | * 303 | * @note When using ESP_EVENT_ANY_ID, handlers registered to specific event IDs using the same base will not be 304 | * unregistered. When using ESP_EVENT_ANY_BASE, events registered to specific bases will also not be 305 | * unregistered. This avoids accidental unregistration of handlers registered by other users or components. 306 | * 307 | * @param[in] event_loop the event loop with which to unregister this handler function, must not be NULL 308 | * @param[in] event_base the base of the event with which to unregister the handler 309 | * @param[in] event_id the ID of the event with which to unregister the handler 310 | * @param[in] instance the instance object of the registration to be unregistered 311 | * 312 | * @return 313 | * - ESP_OK: Success 314 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 315 | * - Others: Fail 316 | */ 317 | esp_err_t esp_event_handler_instance_unregister_with(esp_event_loop_handle_t event_loop, 318 | esp_event_base_t event_base, 319 | int32_t event_id, 320 | esp_event_handler_instance_t instance); 321 | 322 | /** 323 | * @brief Unregister a handler from the system event loop. 324 | * 325 | * This function does the same as esp_event_handler_instance_unregister_with, except that it unregisters the 326 | * handler instance from the default event loop. 327 | * 328 | * @param[in] event_base the base of the event with which to unregister the handler 329 | * @param[in] event_id the ID of the event with which to unregister the handler 330 | * @param[in] instance the instance object of the registration to be unregistered 331 | * 332 | * @return 333 | * - ESP_OK: Success 334 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 335 | * - Others: Fail 336 | */ 337 | esp_err_t esp_event_handler_instance_unregister(esp_event_base_t event_base, 338 | int32_t event_id, 339 | esp_event_handler_instance_t instance); 340 | 341 | /** 342 | * @brief Posts an event to the system default event loop. The event loop library keeps a copy of event_data and manages 343 | * the copy's lifetime automatically (allocation + deletion); this ensures that the data the 344 | * handler receives is always valid. 345 | * 346 | * @param[in] event_base the event base that identifies the event 347 | * @param[in] event_id the event ID that identifies the event 348 | * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler 349 | * @param[in] event_data_size the size of the event data 350 | * @param[in] ticks_to_wait number of ticks to block on a full event queue 351 | * 352 | * @return 353 | * - ESP_OK: Success 354 | * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired, 355 | * queue full when posting from ISR 356 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 357 | * - Others: Fail 358 | */ 359 | esp_err_t esp_event_post(esp_event_base_t event_base, 360 | int32_t event_id, 361 | const void *event_data, 362 | size_t event_data_size, 363 | TickType_t ticks_to_wait); 364 | 365 | /** 366 | * @brief Posts an event to the specified event loop. The event loop library keeps a copy of event_data and manages 367 | * the copy's lifetime automatically (allocation + deletion); this ensures that the data the 368 | * handler receives is always valid. 369 | * 370 | * This function behaves in the same manner as esp_event_post_to, except the additional specification of the event loop 371 | * to post the event to. 372 | * 373 | * @param[in] event_loop the event loop to post to, must not be NULL 374 | * @param[in] event_base the event base that identifies the event 375 | * @param[in] event_id the event ID that identifies the event 376 | * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler 377 | * @param[in] event_data_size the size of the event data 378 | * @param[in] ticks_to_wait number of ticks to block on a full event queue 379 | * 380 | * @return 381 | * - ESP_OK: Success 382 | * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired, 383 | * queue full when posting from ISR 384 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID 385 | * - Others: Fail 386 | */ 387 | esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop, 388 | esp_event_base_t event_base, 389 | int32_t event_id, 390 | const void *event_data, 391 | size_t event_data_size, 392 | TickType_t ticks_to_wait); 393 | 394 | #if CONFIG_ESP_EVENT_POST_FROM_ISR 395 | /** 396 | * @brief Special variant of esp_event_post for posting events from interrupt handlers. 397 | * 398 | * @param[in] event_base the event base that identifies the event 399 | * @param[in] event_id the event ID that identifies the event 400 | * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler 401 | * @param[in] event_data_size the size of the event data; max is 4 bytes 402 | * @param[out] task_unblocked an optional parameter (can be NULL) which indicates that an event task with 403 | * higher priority than currently running task has been unblocked by the posted event; 404 | * a context switch should be requested before the interrupt is existed. 405 | * 406 | * @note this function is only available when CONFIG_ESP_EVENT_POST_FROM_ISR is enabled 407 | * @note when this function is called from an interrupt handler placed in IRAM, this function should 408 | * be placed in IRAM as well by enabling CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR 409 | * 410 | * @return 411 | * - ESP_OK: Success 412 | * - ESP_FAIL: Event queue for the default event loop full 413 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID, 414 | * data size of more than 4 bytes 415 | * - Others: Fail 416 | */ 417 | esp_err_t esp_event_isr_post(esp_event_base_t event_base, 418 | int32_t event_id, 419 | const void *event_data, 420 | size_t event_data_size, 421 | BaseType_t *task_unblocked); 422 | 423 | /** 424 | * @brief Special variant of esp_event_post_to for posting events from interrupt handlers 425 | * 426 | * @param[in] event_loop the event loop to post to, must not be NULL 427 | * @param[in] event_base the event base that identifies the event 428 | * @param[in] event_id the event ID that identifies the event 429 | * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler 430 | * @param[in] event_data_size the size of the event data 431 | * @param[out] task_unblocked an optional parameter (can be NULL) which indicates that an event task with 432 | * higher priority than currently running task has been unblocked by the posted event; 433 | * a context switch should be requested before the interrupt is existed. 434 | * 435 | * @note this function is only available when CONFIG_ESP_EVENT_POST_FROM_ISR is enabled 436 | * @note when this function is called from an interrupt handler placed in IRAM, this function should 437 | * be placed in IRAM as well by enabling CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR 438 | * 439 | * @return 440 | * - ESP_OK: Success 441 | * - ESP_FAIL: Event queue for the loop full 442 | * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID, 443 | * data size of more than 4 bytes 444 | * - Others: Fail 445 | */ 446 | esp_err_t esp_event_isr_post_to(esp_event_loop_handle_t event_loop, 447 | esp_event_base_t event_base, 448 | int32_t event_id, 449 | const void *event_data, 450 | size_t event_data_size, 451 | BaseType_t *task_unblocked); 452 | #endif 453 | 454 | /** 455 | * @brief Dumps statistics of all event loops. 456 | * 457 | * Dumps event loop info in the format: 458 | * 459 | @verbatim 460 | event loop 461 | handler 462 | handler 463 | ... 464 | event loop 465 | handler 466 | handler 467 | ... 468 | 469 | where: 470 | 471 | event loop 472 | format: address,name rx:total_received dr:total_dropped 473 | where: 474 | address - memory address of the event loop 475 | name - name of the event loop, 'none' if no dedicated task 476 | total_received - number of successfully posted events 477 | total_dropped - number of events unsuccessfully posted due to queue being full 478 | 479 | handler 480 | format: address ev:base,id inv:total_invoked run:total_runtime 481 | where: 482 | address - address of the handler function 483 | base,id - the event specified by event base and ID this handler executes 484 | total_invoked - number of times this handler has been invoked 485 | total_runtime - total amount of time used for invoking this handler 486 | 487 | @endverbatim 488 | * 489 | * @param[in] file the file stream to output to 490 | * 491 | * @note this function is a noop when CONFIG_ESP_EVENT_LOOP_PROFILING is disabled 492 | * 493 | * @return 494 | * - ESP_OK: Success 495 | * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list 496 | * - Others: Fail 497 | */ 498 | esp_err_t esp_event_dump(FILE *file); 499 | 500 | #ifdef __cplusplus 501 | } // extern "C" 502 | #endif 503 | 504 | #endif // #ifndef ESP_EVENT_H_ 505 | -------------------------------------------------------------------------------- /src/include/esp_event_internal.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef ESP_EVENT_INTERNAL_H_ 16 | #define ESP_EVENT_INTERNAL_H_ 17 | 18 | #include "sys/queue.h" 19 | #include 20 | #include "esp_event.h" 21 | #include "stdatomic.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | typedef SLIST_HEAD(base_nodes, base_node) base_nodes_t; 28 | 29 | typedef struct esp_event_handler_context { 30 | esp_event_handler_t handler; /**< event handler function*/ 31 | void* arg; 32 | } esp_event_handler_instance_context_t; /**< event handler argument */ 33 | 34 | /// Event handler 35 | typedef struct esp_event_handler_node { 36 | esp_event_handler_instance_context_t* handler_ctx; /**< event handler context*/ 37 | #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING 38 | uint32_t invoked; /**< number of times this handler has been invoked */ 39 | int64_t time; /**< total runtime of this handler across all calls */ 40 | #endif 41 | SLIST_ENTRY(esp_event_handler_node) next; /**< next event handler in the list */ 42 | } esp_event_handler_node_t; 43 | 44 | typedef SLIST_HEAD(esp_event_handler_instances, esp_event_handler_node) esp_event_handler_nodes_t; 45 | 46 | /// Event 47 | typedef struct esp_event_id_node { 48 | int32_t id; /**< id number of the event */ 49 | esp_event_handler_nodes_t handlers; /**< list of handlers to be executed when 50 | this event is raised */ 51 | SLIST_ENTRY(esp_event_id_node) next; /**< pointer to the next event node on the linked list */ 52 | } esp_event_id_node_t; 53 | 54 | typedef SLIST_HEAD(esp_event_id_nodes, esp_event_id_node) esp_event_id_nodes_t; 55 | 56 | typedef struct esp_event_base_node { 57 | esp_event_base_t base; /**< base identifier of the event */ 58 | esp_event_handler_nodes_t handlers; /**< event base level handlers, handlers for 59 | all events with this base */ 60 | esp_event_id_nodes_t id_nodes; /**< list of event ids with this base */ 61 | SLIST_ENTRY(esp_event_base_node) next; /**< pointer to the next base node on the linked list */ 62 | } esp_event_base_node_t; 63 | 64 | typedef SLIST_HEAD(esp_event_base_nodes, esp_event_base_node) esp_event_base_nodes_t; 65 | 66 | typedef struct esp_event_loop_node { 67 | esp_event_handler_nodes_t handlers; /** event loop level handlers */ 68 | esp_event_base_nodes_t base_nodes; /** list of event bases registered to the loop */ 69 | SLIST_ENTRY(esp_event_loop_node) next; /** pointer to the next loop node containing 70 | event loop level handlers and the rest of 71 | event bases registered to the loop */ 72 | } esp_event_loop_node_t; 73 | 74 | typedef SLIST_HEAD(esp_event_loop_nodes, esp_event_loop_node) esp_event_loop_nodes_t; 75 | 76 | /// Event loop 77 | typedef struct esp_event_loop_instance { 78 | const char* name; /**< name of this event loop */ 79 | QueueHandle_t queue; /**< event queue */ 80 | TaskHandle_t task; /**< task that consumes the event queue */ 81 | TaskHandle_t running_task; /**< for loops with no dedicated task, the 82 | task that consumes the queue */ 83 | SemaphoreHandle_t mutex; /**< mutex for updating the events linked list */ 84 | esp_event_loop_nodes_t loop_nodes; /**< set of linked lists containing the 85 | registered handlers for the loop */ 86 | #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING 87 | atomic_uint_least32_t events_recieved; /**< number of events successfully posted to the loop */ 88 | atomic_uint_least32_t events_dropped; /**< number of events dropped due to queue being full */ 89 | SemaphoreHandle_t profiling_mutex; /**< mutex used for profiliing */ 90 | SLIST_ENTRY(esp_event_loop_instance) next; /**< next event loop in the list */ 91 | #endif 92 | } esp_event_loop_instance_t; 93 | 94 | #if CONFIG_ESP_EVENT_POST_FROM_ISR 95 | typedef union esp_event_post_data { 96 | uint32_t val; 97 | void *ptr; 98 | } esp_event_post_data_t; 99 | #else 100 | typedef void* esp_event_post_data_t; 101 | #endif 102 | 103 | /// Event posted to the event queue 104 | typedef struct esp_event_post_instance { 105 | #if CONFIG_ESP_EVENT_POST_FROM_ISR 106 | bool data_allocated; /**< indicates whether data is allocated from heap */ 107 | bool data_set; /**< indicates if data is null */ 108 | #endif 109 | esp_event_base_t base; /**< the event base */ 110 | int32_t id; /**< the event id */ 111 | esp_event_post_data_t data; /**< data associated with the event */ 112 | } esp_event_post_instance_t; 113 | 114 | #ifdef __cplusplus 115 | } // extern "C" 116 | #endif 117 | 118 | #endif // #ifndef ESP_EVENT_INTERNAL_H_ 119 | -------------------------------------------------------------------------------- /src/include/esp_event_private.h: -------------------------------------------------------------------------------- 1 | #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 2 | for ((var) = SLIST_FIRST((head)); \ 3 | (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 4 | (var) = (tvar)) 5 | -------------------------------------------------------------------------------- /src/include/esp_heap_caps.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define MALLOC_CAP_SPIRAM 0 7 | #define MALLOC_CAP_DEFAULT 0 8 | 9 | 10 | void *heap_caps_malloc(size_t size, uint32_t caps); 11 | void heap_caps_free(void *ptr); 12 | -------------------------------------------------------------------------------- /src/include/esp_rom_sys.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mireq/esp32-simulator/883f10b46055396d3ff8c78e4a257db990a2db9b/src/include/esp_rom_sys.h -------------------------------------------------------------------------------- /src/include/esp_system.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mireq/esp32-simulator/883f10b46055396d3ff8c78e4a257db990a2db9b/src/include/esp_system.h -------------------------------------------------------------------------------- /src/include/esp_wifi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /src/include/freertos/FreeRTOS.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/FreeRTOS.h" 4 | -------------------------------------------------------------------------------- /src/include/freertos/StackMacros.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/StackMacros.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/event_groups.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/event_groups.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/list.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/list.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/message_buffer.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/message_buffer.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/portable.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/portable.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/portmacro.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/portable/GCC/Posix/portmacro.h" 2 | //#include 3 | // 4 | //typedef unsigned portBASE_TYPE UBaseType_t; 5 | //typedef portBASE_TYPE BaseType_t; 6 | //typedef uint32_t TickType_t; 7 | //typedef void * QueueHandle_t; 8 | //typedef void * TaskHandle_t; 9 | //typedef void * SemaphoreHandle_t; 10 | -------------------------------------------------------------------------------- /src/include/freertos/projdefs.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/projdefs.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/queue.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/queue.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/semphr.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/semphr.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/stack_macros.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/stack_macros.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/stream_buffer.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/stream_buffer.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/task.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/task.h" 2 | -------------------------------------------------------------------------------- /src/include/freertos/timers.h: -------------------------------------------------------------------------------- 1 | #include "../../freertos/src/FreeRTOS_Posix/FreeRTOS_Kernel/include/timers.h" 2 | -------------------------------------------------------------------------------- /src/include/lwipopts.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. The name of the author may not be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | * 27 | * This file is part of the lwIP TCP/IP stack. 28 | * 29 | * Author: Adam Dunkels 30 | * 31 | */ 32 | #ifndef LWIP_LWIPOPTS_H 33 | #define LWIP_LWIPOPTS_H 34 | 35 | #include "arch/cc.h" 36 | #include "sdkconfig.h" 37 | 38 | #define LWIP_IPV4 1 39 | #define LWIP_IPV6 1 40 | 41 | #define LWIP_DBG_MIN_LEVEL 0 42 | #define LWIP_COMPAT_SOCKETS 1 43 | #define TAPIF_DEBUG LWIP_DBG_ON 44 | #define TUNIF_DEBUG LWIP_DBG_OFF 45 | #define UNIXIF_DEBUG LWIP_DBG_OFF 46 | #define DELIF_DEBUG LWIP_DBG_OFF 47 | #define SIO_FIFO_DEBUG LWIP_DBG_OFF 48 | #define TCPDUMP_DEBUG LWIP_DBG_ON 49 | 50 | #define SLIP_DEBUG LWIP_DBG_OFF 51 | #define PPP_DEBUG LWIP_DBG_ON 52 | #define MEM_DEBUG LWIP_DBG_OFF 53 | #define MEMP_DEBUG LWIP_DBG_OFF 54 | #define PBUF_DEBUG LWIP_DBG_OFF 55 | #define API_LIB_DEBUG LWIP_DBG_ON 56 | #define API_MSG_DEBUG LWIP_DBG_ON 57 | #define TCPIP_DEBUG LWIP_DBG_ON 58 | #define NETIF_DEBUG LWIP_DBG_ON 59 | #define SOCKETS_DEBUG LWIP_DBG_ON 60 | #define DEMO_DEBUG LWIP_DBG_ON 61 | #define IP_DEBUG LWIP_DBG_ON 62 | #define IP_REASS_DEBUG LWIP_DBG_ON 63 | #define RAW_DEBUG LWIP_DBG_ON 64 | #define ICMP_DEBUG LWIP_DBG_ON 65 | #define UDP_DEBUG LWIP_DBG_ON 66 | #define TCP_DEBUG LWIP_DBG_ON 67 | #define TCP_INPUT_DEBUG LWIP_DBG_ON 68 | #define TCP_OUTPUT_DEBUG LWIP_DBG_ON 69 | #define TCP_RTO_DEBUG LWIP_DBG_ON 70 | #define TCP_CWND_DEBUG LWIP_DBG_ON 71 | #define TCP_WND_DEBUG LWIP_DBG_ON 72 | #define TCP_FR_DEBUG LWIP_DBG_ON 73 | #define TCP_QLEN_DEBUG LWIP_DBG_ON 74 | #define TCP_RST_DEBUG LWIP_DBG_ON 75 | 76 | /*#define SIO_DEBUG LWIP_DBG_ON*/ 77 | 78 | #define TCPIP_MBOX_SIZE CONFIG_LWIP_TCPIP_RECVMBOX_SIZE 79 | #define DEFAULT_TCP_RECVMBOX_SIZE CONFIG_LWIP_TCP_RECVMBOX_SIZE 80 | #define DEFAULT_UDP_RECVMBOX_SIZE CONFIG_LWIP_UDP_RECVMBOX_SIZE 81 | #define DEFAULT_ACCEPTMBOX_SIZE 6 82 | 83 | extern unsigned char debug_flags; 84 | #define LWIP_DBG_TYPES_ON debug_flags 85 | 86 | #define NO_SYS 0 87 | #define LWIP_SOCKET (NO_SYS==0) 88 | #define LWIP_NETCONN (NO_SYS==0) 89 | #define SO_REUSE 1 90 | #define IP_SOF_BROADCAST_RECV 1 91 | #define IP_SOF_BROADCAST 1 92 | #define SO_REUSE_RXTOALL 1 93 | 94 | /* ---------- Memory options ---------- */ 95 | /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which 96 | lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2 97 | byte alignment -> define MEM_ALIGNMENT to 2. */ 98 | /* MSVC port: intel processors don't need 4-byte alignment, 99 | but are faster that way! */ 100 | #define MEM_ALIGNMENT 4 101 | 102 | /* MEM_SIZE: the size of the heap memory. If the application will send 103 | a lot of data that needs to be copied, this should be set high. */ 104 | #define MEM_SIZE 10240 105 | 106 | /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application 107 | sends a lot of data out of ROM (or other static memory), this 108 | should be set high. */ 109 | #define MEMP_NUM_PBUF 16 110 | /* MEMP_NUM_RAW_PCB: the number of UDP protocol control blocks. One 111 | per active RAW "connection". */ 112 | #define MEMP_NUM_RAW_PCB CONFIG_LWIP_MAX_RAW_PCBS 113 | /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One 114 | per active UDP "connection". */ 115 | #define MEMP_NUM_UDP_PCB CONFIG_LWIP_MAX_UDP_PCBS 116 | /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP 117 | connections. */ 118 | #define MEMP_NUM_TCP_PCB CONFIG_LWIP_MAX_ACTIVE_TCP 119 | /* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP 120 | connections. */ 121 | #define MEMP_NUM_TCP_PCB_LISTEN CONFIG_LWIP_MAX_LISTENING_TCP 122 | /* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP 123 | segments. */ 124 | #define MEMP_NUM_TCP_SEG 16 125 | /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active 126 | timeouts. */ 127 | #define MEMP_NUM_SYS_TIMEOUT 12 128 | 129 | /* The following four are used only with the sequential API and can be 130 | set to 0 if the application only will use the raw API. */ 131 | /* MEMP_NUM_NETBUF: the number of struct netbufs. */ 132 | #define MEMP_NUM_NETBUF 2 133 | /* MEMP_NUM_NETCONN: the number of struct netconns. */ 134 | #define MEMP_NUM_NETCONN CONFIG_LWIP_MAX_SOCKETS 135 | /* MEMP_NUM_TCPIP_MSG_*: the number of struct tcpip_msg, which is used 136 | for sequential API communication and incoming packets. Used in 137 | src/api/tcpip.c. */ 138 | #define MEMP_NUM_TCPIP_MSG_API 16 139 | #define MEMP_NUM_TCPIP_MSG_INPKT 16 140 | 141 | /* ---------- Pbuf options ---------- */ 142 | /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ 143 | #define PBUF_POOL_SIZE 200 144 | 145 | /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */ 146 | /*#define PBUF_POOL_BUFSIZE 128*/ 147 | #define PBUF_POOL_BUFSIZE 332 148 | 149 | /* PBUF_LINK_HLEN: the number of bytes that should be allocated for a 150 | link level header. */ 151 | #define PBUF_LINK_HLEN 16 152 | 153 | /** SYS_LIGHTWEIGHT_PROT 154 | * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection 155 | * for certain critical regions during buffer allocation, deallocation and memory 156 | * allocation and deallocation. 157 | */ 158 | #define SYS_LIGHTWEIGHT_PROT 1 159 | 160 | #define LWIP_TCPIP_TIMEOUT 1 161 | 162 | /* ---------- TCP options ---------- */ 163 | #define LWIP_TCP 1 164 | #define TCP_TTL 255 165 | 166 | #define TCP_LISTEN_BACKLOG 1 167 | 168 | /* Controls if TCP should queue segments that arrive out of 169 | order. Define to 0 if your device is low on memory. */ 170 | #define TCP_QUEUE_OOSEQ CONFIG_LWIP_TCP_QUEUE_OOSEQ 171 | 172 | /* TCP Maximum segment size. */ 173 | #define TCP_MSS CONFIG_LWIP_TCP_MSS 174 | 175 | /* TCP sender buffer space (bytes). */ 176 | #define TCP_SND_BUF (4 * TCP_MSS) 177 | 178 | /* TCP sender buffer space (pbufs). This must be at least = 2 * 179 | TCP_SND_BUF/TCP_MSS for things to work. */ 180 | #define TCP_SND_QUEUELEN (4 * TCP_SND_BUF/TCP_MSS) 181 | 182 | /* TCP writable space (bytes). This must be less than or equal 183 | to TCP_SND_BUF. It is the amount of space which must be 184 | available in the tcp snd_buf for select to return writable */ 185 | #define TCP_SNDLOWAT (TCP_SND_BUF/2) 186 | 187 | /* TCP receive window. */ 188 | #define TCP_WND (4 * TCP_MSS) 189 | 190 | /* Maximum number of retransmissions of data segments. */ 191 | #define TCP_MAXRTX 12 192 | 193 | /* Maximum number of retransmissions of SYN segments. */ 194 | #define TCP_SYNMAXRTX CONFIG_LWIP_TCP_SYNMAXRTX 195 | 196 | /* ---------- ARP options ---------- */ 197 | #define LWIP_ARP 1 198 | #define ARP_TABLE_SIZE 10 199 | #define ARP_QUEUEING 1 200 | 201 | /* ---------- IP options ---------- */ 202 | /* Define IP_FORWARD to 1 if you wish to have the ability to forward 203 | IP packets across network interfaces. If you are going to run lwIP 204 | on a device with only one network interface, define this to 0. */ 205 | #define IP_FORWARD 1 206 | 207 | 208 | /* IP reassembly and segmentation.These are orthogonal even 209 | * if they both deal with IP fragments */ 210 | #define IP_REASSEMBLY CONFIG_LWIP_IP_REASSEMBLY 211 | #define IP_REASS_MAX_PBUFS 10 212 | #define MEMP_NUM_REASSDATA 10 213 | #define IP_FRAG CONFIG_LWIP_IP_FRAG 214 | #define IPV6_FRAG_COPYHEADER 1 215 | 216 | #define LWIP_IGMP 1 217 | 218 | /* ---------- ICMP options ---------- */ 219 | #define ICMP_TTL 255 220 | 221 | /* ---------- DHCP options ---------- */ 222 | /* Define LWIP_DHCP to 1 if you want DHCP configuration of 223 | interfaces. */ 224 | #define LWIP_DHCP 0 225 | 226 | #define LWIP_DHCP_GET_NTP_SRV (LWIP_DHCP) 227 | 228 | /* 1 if you want to do an ARP check on the offered address 229 | (recommended if using DHCP). */ 230 | #define DHCP_DOES_ARP_CHECK (LWIP_DHCP) 231 | 232 | /* ---------- AUTOIP options ------- */ 233 | #define LWIP_AUTOIP (LWIP_DHCP) 234 | 235 | /* ---------- SNTP options --------- */ 236 | extern void sntp_set_system_time(uint32_t sec); 237 | #define SNTP_SET_SYSTEM_TIME(s) sntp_set_system_time(s) 238 | 239 | /* ---------- SNMP options ---------- */ 240 | #define LWIP_SNMP 1 241 | #define MIB2_STATS LWIP_SNMP 242 | #define SNMP_USE_NETCONN LWIP_NETCONN 243 | #define SNMP_USE_RAW (!LWIP_NETCONN) 244 | 245 | /* ---------- DNS options ---------- */ 246 | #define LWIP_DNS 1 247 | 248 | /* ---------- UDP options ---------- */ 249 | #define LWIP_UDP 1 250 | #define UDP_TTL 255 251 | 252 | /* ---------- RAW options ---------- */ 253 | #define LWIP_RAW 1 254 | #define RAW_TTL 255 255 | 256 | /* ---------- Statistics options ---------- */ 257 | /* individual STATS options can be turned off by defining them to 0 258 | * (e.g #define TCP_STATS 0). All of them are turned off if LWIP_STATS 259 | * is 0 260 | * */ 261 | 262 | #define LWIP_STATS 1 263 | 264 | #define LWIP_NETIF_API 1 265 | #define LWIP_NETIF_STATUS_CALLBACK 1 266 | #define LWIP_NETIF_HOSTNAME 0 267 | 268 | /* ---------- SLIP options ---------- */ 269 | 270 | #define LWIP_HAVE_SLIPIF 1 /* Set > 0 for SLIP */ 271 | 272 | /* Maximum packet size that is received by this netif */ 273 | #define SLIP_MAX_SIZE 1500 274 | #define sio_tryread sio_read 275 | 276 | /* ---------- 6LoWPAN options ---------- */ 277 | #define LWIP_6LOWPAN 1 278 | 279 | /* ---------- PPP options ---------- */ 280 | 281 | #define PPP_SUPPORT 1 /* Set > 0 for PPP */ 282 | #define MPPE_SUPPORT PPP_SUPPORT 283 | #define PPPOE_SUPPORT PPP_SUPPORT 284 | #define PPPOL2TP_SUPPORT PPP_SUPPORT 285 | #define PPPOS_SUPPORT PPP_SUPPORT 286 | 287 | #if PPP_SUPPORT > 0 288 | 289 | #define NUM_PPP 1 /* Max PPP sessions. */ 290 | 291 | 292 | /* Select modules to enable. Ideally these would be set in the makefile but 293 | * we're limited by the command line length so you need to modify the settings 294 | * in this file. 295 | */ 296 | #define PAP_SUPPORT 1 /* Set > 0 for PAP. */ 297 | #define CHAP_SUPPORT 1 /* Set > 0 for CHAP. */ 298 | #define MSCHAP_SUPPORT 0 /* Set > 0 for MSCHAP (NOT FUNCTIONAL!) */ 299 | #define CBCP_SUPPORT 0 /* Set > 0 for CBCP (NOT FUNCTIONAL!) */ 300 | #define CCP_SUPPORT 0 /* Set > 0 for CCP (NOT FUNCTIONAL!) */ 301 | #define VJ_SUPPORT 1 /* Set > 0 for VJ header compression. */ 302 | #define MD5_SUPPORT 1 /* Set > 0 for MD5 (see also CHAP) */ 303 | 304 | 305 | /* 306 | * Timeouts. 307 | */ 308 | #define FSM_DEFTIMEOUT 6 /* Timeout time in seconds */ 309 | #define FSM_DEFMAXTERMREQS 2 /* Maximum Terminate-Request transmissions */ 310 | #define FSM_DEFMAXCONFREQS 10 /* Maximum Configure-Request transmissions */ 311 | #define FSM_DEFMAXNAKLOOPS 5 /* Maximum number of nak loops */ 312 | 313 | #define UPAP_DEFTIMEOUT 6 /* Timeout (seconds) for retransmitting req */ 314 | #define UPAP_DEFREQTIME 30 /* Time to wait for auth-req from peer */ 315 | 316 | #define CHAP_DEFTIMEOUT 6 /* Timeout time in seconds */ 317 | #define CHAP_DEFTRANSMITS 10 /* max # times to send challenge */ 318 | 319 | 320 | /* Interval in seconds between keepalive echo requests, 0 to disable. */ 321 | #if 1 322 | #define LCP_ECHOINTERVAL 0 323 | #else 324 | #define LCP_ECHOINTERVAL 10 325 | #endif 326 | 327 | /* Number of unanswered echo requests before failure. */ 328 | #define LCP_MAXECHOFAILS 3 329 | 330 | /* Max Xmit idle time (in jiffies) before resend flag char. */ 331 | #define PPP_MAXIDLEFLAG 100 332 | 333 | /* 334 | * Packet sizes 335 | * 336 | * Note - lcp shouldn't be allowed to negotiate stuff outside these 337 | * limits. See lcp.h in the pppd directory. 338 | * (XXX - these constants should simply be shared by lcp.c instead 339 | * of living in lcp.h) 340 | */ 341 | #define PPP_MTU 1500 /* Default MTU (size of Info field) */ 342 | #if 0 343 | #define PPP_MAXMTU 65535 - (PPP_HDRLEN + PPP_FCSLEN) 344 | #else 345 | #define PPP_MAXMTU 1500 /* Largest MTU we allow */ 346 | #endif 347 | #define PPP_MINMTU 64 348 | #define PPP_MRU 1500 /* default MRU = max length of info field */ 349 | #define PPP_MAXMRU 1500 /* Largest MRU we allow */ 350 | #define PPP_DEFMRU 296 /* Try for this */ 351 | #define PPP_MINMRU 128 /* No MRUs below this */ 352 | 353 | 354 | #define MAXNAMELEN 256 /* max length of hostname or name for auth */ 355 | #define MAXSECRETLEN 256 /* max length of password or secret */ 356 | 357 | #endif /* PPP_SUPPORT > 0 */ 358 | 359 | 360 | #define LWIP_FREERTOS_CHECK_CORE_LOCKING 1 361 | #define LWIP_TCPIP_CORE_LOCKING 1 362 | 363 | #endif /* LWIP_LWIPOPTS_H */ 364 | -------------------------------------------------------------------------------- /src/lwip/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LWIP_SOURCES 2 | "core/altcp_alloc.c" 3 | "core/altcp.c" 4 | "core/altcp_tcp.c" 5 | "core/def.c" 6 | "core/dns.c" 7 | "core/inet_chksum.c" 8 | "core/init.c" 9 | "core/ip.c" 10 | "core/ipv4" 11 | "core/ipv6" 12 | "core/mem.c" 13 | "core/memp.c" 14 | "core/netif.c" 15 | "core/pbuf.c" 16 | "core/raw.c" 17 | "core/stats.c" 18 | "core/sys.c" 19 | "core/tcp.c" 20 | "core/tcp_in.c" 21 | "core/tcp_out.c" 22 | "core/timeouts.c" 23 | "core/udp.c" 24 | 25 | "core/ipv4/autoip.c" 26 | "core/ipv4/dhcp.c" 27 | "core/ipv4/etharp.c" 28 | "core/ipv4/icmp.c" 29 | "core/ipv4/igmp.c" 30 | "core/ipv4/ip4_addr.c" 31 | "core/ipv4/ip4.c" 32 | "core/ipv4/ip4_frag.c" 33 | 34 | "core/ipv6/dhcp6.c" 35 | "core/ipv6/ethip6.c" 36 | "core/ipv6/icmp6.c" 37 | "core/ipv6/inet6.c" 38 | "core/ipv6/ip6_addr.c" 39 | "core/ipv6/ip6.c" 40 | "core/ipv6/ip6_frag.c" 41 | "core/ipv6/mld6.c" 42 | "core/ipv6/nd6.c" 43 | 44 | "api/api_lib.c" 45 | "api/api_msg.c" 46 | "api/err.c" 47 | "api/if_api.c" 48 | "api/netbuf.c" 49 | "api/netdb.c" 50 | "api/netifapi.c" 51 | "api/sockets.c" 52 | "api/tcpip.c" 53 | 54 | "netif/bridgeif.c" 55 | "netif/bridgeif_fdb.c" 56 | "netif/ethernet.c" 57 | "netif/lowpan6_ble.c" 58 | "netif/lowpan6.c" 59 | "netif/lowpan6_common.c" 60 | "netif/slipif.c" 61 | "netif/zepif.c" 62 | 63 | "netif/ppp/auth.c" 64 | "netif/ppp/ccp.c" 65 | "netif/ppp/demand.c" 66 | "netif/ppp/eap.c" 67 | "netif/ppp/ecp.c" 68 | "netif/ppp/eui64.c" 69 | "netif/ppp/fsm.c" 70 | "netif/ppp/chap-md5.c" 71 | "netif/ppp/chap_ms.c" 72 | "netif/ppp/chap-new.c" 73 | "netif/ppp/ipcp.c" 74 | "netif/ppp/ipv6cp.c" 75 | "netif/ppp/lcp.c" 76 | "netif/ppp/magic.c" 77 | "netif/ppp/mppe.c" 78 | "netif/ppp/multilink.c" 79 | "netif/ppp/pppapi.c" 80 | "netif/ppp/ppp.c" 81 | "netif/ppp/pppcrypt.c" 82 | "netif/ppp/pppoe.c" 83 | "netif/ppp/pppol2tp.c" 84 | "netif/ppp/pppos.c" 85 | "netif/ppp/upap.c" 86 | "netif/ppp/utils.c" 87 | "netif/ppp/vj.c" 88 | 89 | "netif/ppp/polarssl/arc4.c" 90 | "netif/ppp/polarssl/des.c" 91 | "netif/ppp/polarssl/md4.c" 92 | "netif/ppp/polarssl/md5.c" 93 | "netif/ppp/polarssl/sha1.c" 94 | ) 95 | set(LWIP_CONTRIB 96 | "ports/freertos/sys_arch.c" 97 | "ports/unix/port/netif/sio.c" 98 | ) 99 | include_directories( 100 | "${CMAKE_CURRENT_SOURCE_DIR}/extra" 101 | "${CMAKE_CURRENT_SOURCE_DIR}/src/src/include" 102 | "${CMAKE_CURRENT_SOURCE_DIR}/contrib/ports/freertos/include" 103 | "${CMAKE_CURRENT_SOURCE_DIR}/contrib/ports/unix/port/include" 104 | ${ESP_SIMULATOR_INCLUDE_DIRECTORIES} 105 | "${ESP_CONFIG_DIR}" 106 | ) 107 | 108 | list(TRANSFORM LWIP_SOURCES PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/src/src/) 109 | list(TRANSFORM LWIP_CONTRIB PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/contrib/) 110 | add_definitions(-DDEFAULT_THREAD_STACKSIZE=1024 -DTCPIP_THREAD_STACKSIZE=1024 -g3 -ggdb) 111 | add_library(lwip STATIC 112 | ${LWIP_SOURCES} 113 | ${LWIP_CONTRIB} 114 | "${CMAKE_CURRENT_SOURCE_DIR}/extra/tapif.c" 115 | ) 116 | -------------------------------------------------------------------------------- /src/lwip/extra/lwip/sys.h: -------------------------------------------------------------------------------- 1 | #include "../../src/src/include/lwip/sys.h" 2 | 3 | 4 | extern sys_mutex_t lock_tcpip_core; 5 | -------------------------------------------------------------------------------- /src/lwip/extra/tapif.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. The name of the author may not be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | * 27 | * This file is part of the lwIP TCP/IP stack. 28 | * 29 | * Author: Adam Dunkels 30 | * 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | #include "lwip/opt.h" 47 | 48 | #include "lwip/debug.h" 49 | #include "lwip/def.h" 50 | #include "lwip/ip.h" 51 | #include "lwip/mem.h" 52 | #include "lwip/stats.h" 53 | #include "lwip/snmp.h" 54 | #include "lwip/pbuf.h" 55 | #include "lwip/sys.h" 56 | #include "lwip/tcpip.h" 57 | #include "lwip/timeouts.h" 58 | #include "netif/etharp.h" 59 | #include "lwip/ethip6.h" 60 | 61 | #include "netif/tapif.h" 62 | 63 | #define IFCONFIG_BIN "/sbin/ifconfig " 64 | 65 | #if defined(LWIP_UNIX_LINUX) 66 | #include 67 | #include 68 | #include 69 | /* 70 | * Creating a tap interface requires special privileges. If the interfaces 71 | * is created in advance with `tunctl -u ` it can be opened as a regular 72 | * user. The network must already be configured. If DEVTAP_IF is defined it 73 | * will be opened instead of creating a new tap device. 74 | * 75 | * You can also use PRECONFIGURED_TAPIF environment variable to do so. 76 | */ 77 | #ifndef DEVTAP_DEFAULT_IF 78 | #define DEVTAP_DEFAULT_IF "tap0" 79 | #endif 80 | #ifndef DEVTAP 81 | #define DEVTAP "/dev/net/tun" 82 | #endif 83 | #define NETMASK_ARGS "netmask %d.%d.%d.%d" 84 | #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS 85 | #elif defined(LWIP_UNIX_OPENBSD) 86 | #define DEVTAP "/dev/tun0" 87 | #define NETMASK_ARGS "netmask %d.%d.%d.%d" 88 | #define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d " NETMASK_ARGS " link0" 89 | #else /* others */ 90 | #define DEVTAP "/dev/tap0" 91 | #define NETMASK_ARGS "netmask %d.%d.%d.%d" 92 | #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS 93 | #endif 94 | 95 | /* Define those to better describe your network interface. */ 96 | #define IFNAME0 't' 97 | #define IFNAME1 'p' 98 | 99 | #ifndef TAPIF_DEBUG 100 | #define TAPIF_DEBUG LWIP_DBG_OFF 101 | #endif 102 | 103 | struct tapif { 104 | /* Add whatever per-interface state that is needed here. */ 105 | int fd; 106 | }; 107 | 108 | /* Forward declarations. */ 109 | static void tapif_input(struct netif *netif); 110 | #if !NO_SYS 111 | static void tapif_thread(void *arg); 112 | #endif /* !NO_SYS */ 113 | 114 | /*-----------------------------------------------------------------------------------*/ 115 | static void 116 | low_level_init(struct netif *netif) 117 | { 118 | struct tapif *tapif; 119 | #if LWIP_IPV4 120 | int ret; 121 | char buf[1024]; 122 | #endif /* LWIP_IPV4 */ 123 | char *preconfigured_tapif = getenv("PRECONFIGURED_TAPIF"); 124 | 125 | tapif = (struct tapif *)netif->state; 126 | 127 | /* Obtain MAC address from network interface. */ 128 | 129 | /* (We just fake an address...) */ 130 | netif->hwaddr[0] = 0x02; 131 | netif->hwaddr[1] = 0x12; 132 | netif->hwaddr[2] = 0x34; 133 | netif->hwaddr[3] = 0x56; 134 | netif->hwaddr[4] = 0x78; 135 | netif->hwaddr[5] = 0xab; 136 | netif->hwaddr_len = 6; 137 | 138 | /* device capabilities */ 139 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP; 140 | 141 | tapif->fd = open(DEVTAP, O_RDWR); 142 | LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd)); 143 | if (tapif->fd == -1) { 144 | #ifdef LWIP_UNIX_LINUX 145 | perror("tapif_init: try running \"modprobe tun\" or rebuilding your kernel with CONFIG_TUN; cannot open "DEVTAP); 146 | #else /* LWIP_UNIX_LINUX */ 147 | perror("tapif_init: cannot open "DEVTAP); 148 | #endif /* LWIP_UNIX_LINUX */ 149 | exit(1); 150 | } 151 | 152 | #ifdef LWIP_UNIX_LINUX 153 | { 154 | struct ifreq ifr; 155 | memset(&ifr, 0, sizeof(ifr)); 156 | 157 | if (preconfigured_tapif) { 158 | strncpy(ifr.ifr_name, preconfigured_tapif, sizeof(ifr.ifr_name)); 159 | } else { 160 | strncpy(ifr.ifr_name, DEVTAP_DEFAULT_IF, sizeof(ifr.ifr_name)); 161 | } 162 | ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; /* ensure \0 termination */ 163 | 164 | ifr.ifr_flags = IFF_TAP|IFF_NO_PI; 165 | if (ioctl(tapif->fd, TUNSETIFF, (void *) &ifr) < 0) { 166 | perror("tapif_init: "DEVTAP" ioctl TUNSETIFF"); 167 | exit(1); 168 | } 169 | } 170 | #endif /* LWIP_UNIX_LINUX */ 171 | 172 | netif_set_link_up(netif); 173 | 174 | if (preconfigured_tapif == NULL) { 175 | #if LWIP_IPV4 176 | snprintf(buf, 1024, IFCONFIG_BIN IFCONFIG_ARGS, 177 | ip4_addr1(netif_ip4_gw(netif)), 178 | ip4_addr2(netif_ip4_gw(netif)), 179 | ip4_addr3(netif_ip4_gw(netif)), 180 | ip4_addr4(netif_ip4_gw(netif)) 181 | #ifdef NETMASK_ARGS 182 | , 183 | ip4_addr1(netif_ip4_netmask(netif)), 184 | ip4_addr2(netif_ip4_netmask(netif)), 185 | ip4_addr3(netif_ip4_netmask(netif)), 186 | ip4_addr4(netif_ip4_netmask(netif)) 187 | #endif /* NETMASK_ARGS */ 188 | ); 189 | 190 | LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: system(\"%s\");\n", buf)); 191 | ret = system(buf); 192 | if (ret < 0) { 193 | perror("ifconfig failed"); 194 | exit(1); 195 | } 196 | if (ret != 0) { 197 | printf("ifconfig returned %d\n", ret); 198 | } 199 | #else /* LWIP_IPV4 */ 200 | perror("todo: support IPv6 support for non-preconfigured tapif"); 201 | exit(1); 202 | #endif /* LWIP_IPV4 */ 203 | } 204 | 205 | #if !NO_SYS 206 | sys_thread_new("tapif_thread", tapif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); 207 | #endif /* !NO_SYS */ 208 | } 209 | /*-----------------------------------------------------------------------------------*/ 210 | /* 211 | * low_level_output(): 212 | * 213 | * Should do the actual transmission of the packet. The packet is 214 | * contained in the pbuf that is passed to the function. This pbuf 215 | * might be chained. 216 | * 217 | */ 218 | /*-----------------------------------------------------------------------------------*/ 219 | 220 | static err_t 221 | low_level_output(struct netif *netif, struct pbuf *p) 222 | { 223 | struct tapif *tapif = (struct tapif *)netif->state; 224 | char buf[1518]; /* max packet size including VLAN excluding CRC */ 225 | ssize_t written; 226 | 227 | #if 0 228 | if (((double)rand()/(double)RAND_MAX) < 0.2) { 229 | printf("drop output\n"); 230 | return ERR_OK; /* ERR_OK because we simulate packet loss on cable */ 231 | } 232 | #endif 233 | 234 | if (p->tot_len > sizeof(buf)) { 235 | MIB2_STATS_NETIF_INC(netif, ifoutdiscards); 236 | perror("tapif: packet too large"); 237 | return ERR_IF; 238 | } 239 | 240 | /* initiate transfer(); */ 241 | pbuf_copy_partial(p, buf, p->tot_len, 0); 242 | 243 | /* signal that packet should be sent(); */ 244 | written = write(tapif->fd, buf, p->tot_len); 245 | if (written < p->tot_len) { 246 | MIB2_STATS_NETIF_INC(netif, ifoutdiscards); 247 | perror("tapif: write"); 248 | return ERR_IF; 249 | } else { 250 | MIB2_STATS_NETIF_ADD(netif, ifoutoctets, (u32_t)written); 251 | return ERR_OK; 252 | } 253 | } 254 | /*-----------------------------------------------------------------------------------*/ 255 | /* 256 | * low_level_input(): 257 | * 258 | * Should allocate a pbuf and transfer the bytes of the incoming 259 | * packet from the interface into the pbuf. 260 | * 261 | */ 262 | /*-----------------------------------------------------------------------------------*/ 263 | static struct pbuf * 264 | low_level_input(struct netif *netif) 265 | { 266 | struct pbuf *p; 267 | u16_t len; 268 | ssize_t readlen; 269 | char buf[1518]; /* max packet size including VLAN excluding CRC */ 270 | struct tapif *tapif = (struct tapif *)netif->state; 271 | 272 | /* Obtain the size of the packet and put it into the "len" 273 | variable. */ 274 | readlen = read(tapif->fd, buf, sizeof(buf)); 275 | if (readlen < 0) { 276 | perror("read returned -1"); 277 | exit(1); 278 | } 279 | len = (u16_t)readlen; 280 | 281 | MIB2_STATS_NETIF_ADD(netif, ifinoctets, len); 282 | 283 | #if 0 284 | if (((double)rand()/(double)RAND_MAX) < 0.2) { 285 | printf("drop\n"); 286 | return NULL; 287 | } 288 | #endif 289 | 290 | /* We allocate a pbuf chain of pbufs from the pool. */ 291 | p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 292 | if (p != NULL) { 293 | pbuf_take(p, buf, len); 294 | /* acknowledge that packet has been read(); */ 295 | } else { 296 | /* drop packet(); */ 297 | MIB2_STATS_NETIF_INC(netif, ifindiscards); 298 | LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: could not allocate pbuf\n")); 299 | } 300 | 301 | return p; 302 | } 303 | 304 | /*-----------------------------------------------------------------------------------*/ 305 | /* 306 | * tapif_input(): 307 | * 308 | * This function should be called when a packet is ready to be read 309 | * from the interface. It uses the function low_level_input() that 310 | * should handle the actual reception of bytes from the network 311 | * interface. 312 | * 313 | */ 314 | /*-----------------------------------------------------------------------------------*/ 315 | static void 316 | tapif_input(struct netif *netif) 317 | { 318 | struct pbuf *p = low_level_input(netif); 319 | 320 | if (p == NULL) { 321 | #if LINK_STATS 322 | LINK_STATS_INC(link.recv); 323 | #endif /* LINK_STATS */ 324 | LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: low_level_input returned NULL\n")); 325 | return; 326 | } 327 | 328 | if (netif->input(p, netif) != ERR_OK) { 329 | LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: netif input error\n")); 330 | pbuf_free(p); 331 | } 332 | } 333 | /*-----------------------------------------------------------------------------------*/ 334 | /* 335 | * tapif_init(): 336 | * 337 | * Should be called at the beginning of the program to set up the 338 | * network interface. It calls the function low_level_init() to do the 339 | * actual setup of the hardware. 340 | * 341 | */ 342 | /*-----------------------------------------------------------------------------------*/ 343 | err_t 344 | tapif_init(struct netif *netif) 345 | { 346 | struct tapif *tapif = (struct tapif *)mem_malloc(sizeof(struct tapif)); 347 | 348 | if (tapif == NULL) { 349 | LWIP_DEBUGF(NETIF_DEBUG, ("tapif_init: out of memory for tapif\n")); 350 | return ERR_MEM; 351 | } 352 | netif->state = tapif; 353 | MIB2_INIT_NETIF(netif, snmp_ifType_other, 100000000); 354 | 355 | netif->name[0] = IFNAME0; 356 | netif->name[1] = IFNAME1; 357 | #if LWIP_IPV4 358 | netif->output = etharp_output; 359 | #endif /* LWIP_IPV4 */ 360 | #if LWIP_IPV6 361 | netif->output_ip6 = ethip6_output; 362 | #endif /* LWIP_IPV6 */ 363 | netif->linkoutput = low_level_output; 364 | netif->mtu = 1500; 365 | 366 | low_level_init(netif); 367 | 368 | return ERR_OK; 369 | } 370 | 371 | 372 | /*-----------------------------------------------------------------------------------*/ 373 | void 374 | tapif_poll(struct netif *netif) 375 | { 376 | tapif_input(netif); 377 | } 378 | 379 | #if NO_SYS 380 | 381 | int 382 | tapif_select(struct netif *netif) 383 | { 384 | fd_set fdset; 385 | int ret; 386 | struct timeval tv; 387 | struct tapif *tapif; 388 | u32_t msecs = sys_timeouts_sleeptime(); 389 | 390 | tapif = (struct tapif *)netif->state; 391 | 392 | tv.tv_sec = msecs / 1000; 393 | tv.tv_usec = (msecs % 1000) * 1000; 394 | 395 | FD_ZERO(&fdset); 396 | FD_SET(tapif->fd, &fdset); 397 | 398 | ret = select(tapif->fd + 1, &fdset, NULL, NULL, &tv); 399 | if (ret > 0) { 400 | tapif_input(netif); 401 | } 402 | return ret; 403 | } 404 | 405 | #else /* NO_SYS */ 406 | 407 | static void 408 | tapif_thread(void *arg) 409 | { 410 | struct netif *netif; 411 | struct tapif *tapif; 412 | fd_set fdset; 413 | int ret; 414 | 415 | netif = (struct netif *)arg; 416 | tapif = (struct tapif *)netif->state; 417 | 418 | while(1) { 419 | FD_ZERO(&fdset); 420 | FD_SET(tapif->fd, &fdset); 421 | 422 | /* Wait for a packet to arrive. */ 423 | ret = select(tapif->fd + 1, &fdset, NULL, NULL, NULL); 424 | 425 | if(ret == 1) { 426 | /* Handle incoming packet. */ 427 | LOCK_TCPIP_CORE(); 428 | tapif_input(netif); 429 | UNLOCK_TCPIP_CORE(); 430 | } else if(ret == -1) { 431 | if (errno != EINTR) { /* Ignore signals */ 432 | perror("tapif_thread: select"); 433 | } 434 | } 435 | } 436 | } 437 | 438 | #endif /* NO_SYS */ 439 | -------------------------------------------------------------------------------- /src/mbedtls/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(MBEDTLS_SOURCES 2 | "aes.c" 3 | "aesce.c" 4 | "aesce.h" 5 | "aesni.c" 6 | "aesni.h" 7 | "alignment.h" 8 | "aria.c" 9 | "asn1parse.c" 10 | "asn1write.c" 11 | "base64.c" 12 | "bignum.c" 13 | "bignum_core.c" 14 | "bignum_core.h" 15 | "bignum_mod.c" 16 | "bignum_mod.h" 17 | "bignum_mod_raw.c" 18 | "bignum_mod_raw.h" 19 | "bignum_mod_raw_invasive.h" 20 | "bn_mul.h" 21 | "camellia.c" 22 | "ccm.c" 23 | "cipher.c" 24 | "cipher_wrap.c" 25 | "cipher_wrap.h" 26 | "cmac.c" 27 | "CMakeLists.txt" 28 | "common.h" 29 | "constant_time.c" 30 | "constant_time_internal.h" 31 | "constant_time_invasive.h" 32 | "ctr_drbg.c" 33 | "debug.c" 34 | "des.c" 35 | "dhm.c" 36 | "ecdh.c" 37 | "ecdsa.c" 38 | "ecjpake.c" 39 | "ecp.c" 40 | "ecp_curves.c" 41 | "ecp_internal_alt.h" 42 | "ecp_invasive.h" 43 | "entropy.c" 44 | "entropy_poll.c" 45 | "entropy_poll.h" 46 | "error.c" 47 | "gcm.c" 48 | "hash_info.c" 49 | "hash_info.h" 50 | "hkdf.c" 51 | "hmac_drbg.c" 52 | "chachapoly.c" 53 | "chacha20.c" 54 | "check_crypto_config.h" 55 | "lmots.c" 56 | "lmots.h" 57 | "lms.c" 58 | "Makefile" 59 | "md.c" 60 | "md_wrap.h" 61 | "md5.c" 62 | "memory_buffer_alloc.c" 63 | "mps_common.h" 64 | "mps_error.h" 65 | "mps_reader.c" 66 | "mps_reader.h" 67 | "mps_trace.c" 68 | "mps_trace.h" 69 | "net_sockets.c" 70 | "nist_kw.c" 71 | "oid.c" 72 | "padlock.c" 73 | "padlock.h" 74 | "pem.c" 75 | "pk.c" 76 | "pkcs12.c" 77 | "pkcs5.c" 78 | "pkcs7.c" 79 | "pkparse.c" 80 | "pk_wrap.c" 81 | "pk_wrap.h" 82 | "pkwrite.c" 83 | "pkwrite.h" 84 | "platform.c" 85 | "platform_util.c" 86 | "poly1305.c" 87 | "psa_crypto_aead.c" 88 | "psa_crypto_aead.h" 89 | "psa_crypto.c" 90 | "psa_crypto_cipher.c" 91 | "psa_crypto_cipher.h" 92 | "psa_crypto_client.c" 93 | "psa_crypto_core.h" 94 | "psa_crypto_driver_wrappers.c" 95 | "psa_crypto_driver_wrappers.h" 96 | "psa_crypto_ecp.c" 97 | "psa_crypto_ecp.h" 98 | "psa_crypto_hash.c" 99 | "psa_crypto_hash.h" 100 | "psa_crypto_invasive.h" 101 | "psa_crypto_its.h" 102 | "psa_crypto_mac.c" 103 | "psa_crypto_mac.h" 104 | "psa_crypto_pake.c" 105 | "psa_crypto_pake.h" 106 | "psa_crypto_random_impl.h" 107 | "psa_crypto_rsa.c" 108 | "psa_crypto_rsa.h" 109 | "psa_crypto_se.c" 110 | "psa_crypto_se.h" 111 | "psa_crypto_slot_management.c" 112 | "psa_crypto_slot_management.h" 113 | "psa_crypto_storage.c" 114 | "psa_crypto_storage.h" 115 | "psa_its_file.c" 116 | "psa_util.c" 117 | "ripemd160.c" 118 | "rsa_alt_helpers.c" 119 | "rsa_alt_helpers.h" 120 | "rsa.c" 121 | "sha1.c" 122 | "sha256.c" 123 | "sha512.c" 124 | "ssl_cache.c" 125 | "ssl_ciphersuites.c" 126 | "ssl_client.c" 127 | "ssl_client.h" 128 | "ssl_cookie.c" 129 | "ssl_debug_helpers_generated.c" 130 | "ssl_debug_helpers.h" 131 | "ssl_misc.h" 132 | "ssl_msg.c" 133 | "ssl_ticket.c" 134 | "ssl_tls.c" 135 | "ssl_tls12_client.c" 136 | "ssl_tls12_server.c" 137 | "ssl_tls13_client.c" 138 | "ssl_tls13_generic.c" 139 | "ssl_tls13_invasive.h" 140 | "ssl_tls13_keys.c" 141 | "ssl_tls13_keys.h" 142 | "ssl_tls13_server.c" 143 | "threading.c" 144 | "timing.c" 145 | "version.c" 146 | "version_features.c" 147 | "x509.c" 148 | "x509_create.c" 149 | "x509_crl.c" 150 | "x509_crt.c" 151 | "x509_csr.c" 152 | "x509write_crt.c" 153 | "x509write_csr.c" 154 | ) 155 | include_directories( 156 | "${CMAKE_CURRENT_SOURCE_DIR}/src/include" 157 | ) 158 | list(TRANSFORM MBEDTLS_SOURCES PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/src/library/) 159 | add_library(mbedtls STATIC 160 | ${MBEDTLS_SOURCES} 161 | ) 162 | -------------------------------------------------------------------------------- /src/startup.c: -------------------------------------------------------------------------------- 1 | #include "esp_log.h" 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/semphr.h" 4 | #include "freertos/task.h" 5 | #include "lwip/dns.h" 6 | #include "lwip/init.h" 7 | #include "lwip/ip_addr.h" 8 | #include "lwip/netif.h" 9 | #include "lwip/tcpip.h" 10 | #include "lwip/timeouts.h" 11 | #include "netif/etharp.h" 12 | #include "netif/tapif.h" 13 | 14 | 15 | static const char *TAG = "startup"; 16 | 17 | 18 | static SemaphoreHandle_t wait_for_tcpip_semaphore; 19 | 20 | 21 | void app_main(); 22 | static void run_main(void *data); 23 | static void exit_app(void *data); 24 | 25 | 26 | static void tcpip_init_done_signal(void *params) { 27 | xSemaphoreGive(wait_for_tcpip_semaphore); 28 | } 29 | 30 | static void setup_network(void *parameters) { 31 | static struct netif netif; 32 | static ip4_addr_t ipaddr, netmask, gw; 33 | ESP_LOGI(TAG, "initializing tcpip"); 34 | wait_for_tcpip_semaphore = xSemaphoreCreateBinary(); 35 | volatile s32_t tcpipdone = 0; 36 | tcpip_init(tcpip_init_done_signal, NULL); 37 | xSemaphoreTake(wait_for_tcpip_semaphore, portMAX_DELAY); 38 | vSemaphoreDelete(wait_for_tcpip_semaphore); 39 | 40 | IP4_ADDR(&gw, 10, 0, 0, 1); 41 | IP4_ADDR(&ipaddr, 10, 0, 0, 2); 42 | IP4_ADDR(&netmask, 255, 255, 255, 0); 43 | 44 | if (!netif_add(&netif, &ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input)) { 45 | LWIP_ASSERT("Net interface failed to initialize\r\n", 0); 46 | } 47 | 48 | ip_addr_t dnsserver; 49 | IP_ADDR4(&dnsserver, 8, 8, 8, 8); 50 | dns_setserver(0, &dnsserver); 51 | 52 | netif_set_default(&netif); 53 | netif_set_up(&netif); 54 | 55 | ESP_LOGI(TAG, "done initializing tcpip"); 56 | 57 | xTaskCreate(&run_main, "run_main", 2048, NULL, 5, NULL); 58 | 59 | vTaskDelete(NULL); 60 | } 61 | 62 | 63 | int main(void) { 64 | xTaskCreate(setup_network, "setup_network", configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 1), (xTaskHandle *) NULL); 65 | vTaskStartScheduler(); 66 | return 0; 67 | } 68 | 69 | 70 | void vApplicationIdleHook(void) { 71 | sleep(1); 72 | } 73 | 74 | 75 | void vMainQueueSendPassed(void) { 76 | } 77 | 78 | 79 | static void exit_app(void *data) { 80 | vTaskEndScheduler(); 81 | } 82 | 83 | 84 | static void run_main(void *data) { 85 | app_main(); 86 | vTaskDelete(NULL); 87 | } 88 | --------------------------------------------------------------------------------