mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-29 10:17:30 +02:00
feat(sockutls): Add initial support for socket helpers
This commit is contained in:
8
components/sock_utils/examples/simple/CMakeLists.txt
Normal file
8
components/sock_utils/examples/simple/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(sock_utils_example)
|
30
components/sock_utils/examples/simple/README.md
Normal file
30
components/sock_utils/examples/simple/README.md
Normal file
@ -0,0 +1,30 @@
|
||||
# Simple example
|
||||
|
||||
This example demonstrates using socket helpers on ESP_PLATFORM
|
||||
|
||||
The functionality of this example is meant to be very generic, so the application is compilable on linux without any IDF dependencies:
|
||||
```bash
|
||||
gcc main/app.c -lpthread
|
||||
```
|
||||
|
||||
## Example output on ESP_PLATFORM
|
||||
|
||||
```
|
||||
I (8073) esp_netif_handlers: example_netif_sta ip: 192.168.0.32, mask: 255.255.255.0, gw: 192.168.0.1
|
||||
I (8073) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 192.168.0.32
|
||||
I (8073) example_common: Connected to example_netif_sta
|
||||
I (8083) example_common: - IPv4 address: 192.168.0.32,
|
||||
I (8093) example_common: - IPv6 address: fe80:0000:0000:0000:XXXX, type: ESP_IP6_ADDR_IS_LINK_LOCAL
|
||||
I (8103) sock_utils_example: Received signal: IP4
|
||||
I (8103) sock_utils_example: IPv4 address of interface "sta": 192.168.0.32
|
||||
I (8113) main_task: Returned from app_main()
|
||||
```
|
||||
|
||||
## Example output on linux platform
|
||||
|
||||
```
|
||||
I[sock_utils_example]: Received signal: IP4
|
||||
I[sock_utils_example]: IPv4 address of interface "lo": 127.0.0.1
|
||||
I[sock_utils_example]: IPv4 address of interface "en0": 192.168.0.28
|
||||
I[sock_utils_example]: IPv4 address of interface "docker0": 172.17.0.1
|
||||
```
|
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "app.c"
|
||||
INCLUDE_DIRS ".")
|
123
components/sock_utils/examples/simple/main/app.c
Normal file
123
components/sock_utils/examples/simple/main/app.c
Normal file
@ -0,0 +1,123 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <net/if.h>
|
||||
#include <ifaddrs.h>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_log.h"
|
||||
#include "protocol_examples_common.h"
|
||||
#else
|
||||
// Provides simplified support for building on linux (whithout official IDF linux target)
|
||||
// in order to check with the actual host functionality
|
||||
// use `gcc main/app.c -lpthread`
|
||||
#define SIMPLE_LOG( lev, tag, format, ... ) do { printf("%s[%s]: ", lev, tag); printf(format, ##__VA_ARGS__); printf("\n"); } while(0)
|
||||
#define ESP_LOGE(tag, format, ... ) SIMPLE_LOG("E", tag, format, ##__VA_ARGS__)
|
||||
#define ESP_LOGI(tag, format, ... ) SIMPLE_LOG("I", tag, format, ##__VA_ARGS__)
|
||||
#define ESP_ERROR_CHECK(action) action
|
||||
static void example_connect(void) { }
|
||||
#endif
|
||||
|
||||
static const char *TAG = "sock_utils_example";
|
||||
|
||||
static void *reader_thread(void *arg)
|
||||
{
|
||||
int *pipe_fds = (int *)arg;
|
||||
char buffer[64];
|
||||
int len;
|
||||
while ((len = read(pipe_fds[0], buffer, sizeof(buffer))) < 0) {
|
||||
if (errno != EAGAIN) {
|
||||
ESP_LOGE(TAG, "Failed reading from pipe: %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
usleep(100000);
|
||||
}
|
||||
buffer[len] = '\0';
|
||||
ESP_LOGI(TAG, "Received signal: %s", buffer);
|
||||
if (strcmp(buffer, "IP4") != 0) {
|
||||
ESP_LOGE(TAG, "Unknown signal");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ifaddrs *addresses, *addr;
|
||||
if (getifaddrs(&addresses) == -1) {
|
||||
ESP_LOGE(TAG, "getifaddrs() failed");
|
||||
return NULL;
|
||||
}
|
||||
addr = addresses;
|
||||
|
||||
while (addr) {
|
||||
if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) { // look for IP4 addresses
|
||||
struct sockaddr_in *sock_addr = (struct sockaddr_in *) addr->ifa_addr;
|
||||
if ((addr->ifa_flags & IFF_UP) == 0) {
|
||||
ESP_LOGI(TAG, "Network interface \"%s\" is down", addr->ifa_name);
|
||||
} else {
|
||||
if (getnameinfo((struct sockaddr *)sock_addr, sizeof(*sock_addr),
|
||||
buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST) != 0) {
|
||||
freeifaddrs(addresses);
|
||||
return NULL;
|
||||
}
|
||||
ESP_LOGI(TAG, "IPv4 address of interface \"%s\": %s", addr->ifa_name, buffer);
|
||||
}
|
||||
}
|
||||
addr = addr->ifa_next;
|
||||
}
|
||||
freeifaddrs(addresses);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void simple_pipe_example(void)
|
||||
{
|
||||
int pipe_fds[2];
|
||||
if (pipe(pipe_fds) == -1) {
|
||||
ESP_LOGE(TAG, "Failed to create pipe");
|
||||
return;
|
||||
}
|
||||
|
||||
// creates reader thread to wait for a pipe signal
|
||||
// and print out our IPv4 addresses
|
||||
pthread_t reader;
|
||||
pthread_create(&reader, NULL, reader_thread, pipe_fds);
|
||||
|
||||
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||
* examples/protocols/README.md for more information about this function.
|
||||
*/
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
|
||||
// at this point we should have received an IP address -> send signal to the reader thread
|
||||
const char signal[] = "IP4";
|
||||
write(pipe_fds[1], signal, sizeof(signal));
|
||||
pthread_join(reader, NULL);
|
||||
|
||||
// Close pipe file descriptors
|
||||
close(pipe_fds[0]);
|
||||
close(pipe_fds[1]);
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
simple_pipe_example();
|
||||
}
|
||||
#else
|
||||
int main(void)
|
||||
{
|
||||
simple_pipe_example();
|
||||
}
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
dependencies:
|
||||
idf:
|
||||
version: ">=5.0"
|
||||
sock_utils:
|
||||
version: "*"
|
||||
override_path: '../../../'
|
||||
protocol_examples_common:
|
||||
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
|
13
components/sock_utils/examples/simple/pytest_sockutls.py
Normal file
13
components/sock_utils/examples/simple/pytest_sockutls.py
Normal file
@ -0,0 +1,13 @@
|
||||
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
|
||||
def test_sockutls(dut):
|
||||
"""
|
||||
steps:
|
||||
1. Start the example
|
||||
2. Check the resultant IPv4 address from station netif
|
||||
"""
|
||||
# Signal from the pipe simple implementation
|
||||
dut.expect('Received signal: IP4')
|
||||
# and the IPv4 address of the connected netif
|
||||
dut.expect('IPv4 address of interface "en1"')
|
2
components/sock_utils/examples/simple/sdkconfig.ci
Normal file
2
components/sock_utils/examples/simple/sdkconfig.ci
Normal file
@ -0,0 +1,2 @@
|
||||
CONFIG_EXAMPLE_CONNECT_WIFI=n
|
||||
CONFIG_EXAMPLE_CONNECT_ETHERNET=y
|
Reference in New Issue
Block a user