Commit 97132615 authored by Jozef Kralik's avatar Jozef Kralik

implement upd + tcp stack for esp32

parent 6d209bf7
......@@ -65,6 +65,8 @@ port/contiki/obj_native/
port/contiki/*.native
port/contiki/symbols.*
port/openthread/output
port/esp32/build/
port/esp32/sdkconfig
*.o
*.cmd
*.tmp
......
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(example)
idf_build_get_property(config_dir CONFIG_DIR)
set(sdkconfig_header ${config_dir}/sdkconfig.h)
set(project_elf ${CMAKE_PROJECT_NAME}.elf)
This repository contains ESP32 simulator for linux.
# How does it work?
ESP-IDF contains small amount of hardware specific code, FreeRTOS and few
multi-platform libraries.
# Running
## Using ubuntu
```
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
mkdir esp
cd esp
git clone -b release/v4.1 --recursive https://github.com/espressif/esp-idf.git
./esp-idf/install.sh
. ./esp-idf/export.sh
idf.py build
```
## Inside docker
```
docker build -t esp32-simulator .
docker run --cap-add=NET_ADMIN --device /dev/net/tun:/dev/net/tun --name esp32-simulator -v `pwd`:/root/simulator -d esp32-simulator
docker exec -i -t esp32-simulator bash
cd ~/simulator/example
idf.build
./build/example
```
/*
This code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#ifndef _DEBUG_PRINT_H_
#define _DEBUG_PRINT_H_
#include "oc_network_events.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define APP_PRINT(...) printf(__VA_ARGS__)
/**
* @brief print current all macro information
*
* for easily debug, print all macro configuration
*
* @param[in] no param input
*
* @return noreturn
*
*/
void print_macro_info();
/**
* @brief print detailed struct message content
*
* for easily debug, print more parameter in struct message
*
* @param[in] message: the struct oc_message_t to print
*
* @return noreturn
* */
void print_message_info(oc_message_t *message);
/**
* @brief print the data detail information
*
* print input data, print from data[0] to data[len-1], addtionally add notes string
*
* @param[in] data: input data pointer to print
* @param[in] len: data length
* @param[in] note: notes for read easily
* @param[in] mode: 0x00, 0x01, 0x10, 0x11 to decide the BINARY_SHOW && BYTES_SHOW
*
* @return noreturn
*
*/
void print_debug(const char* data, const unsigned int len, const char* note, int mode);
/**
* @brief print the fatal error information and cycle it
*
* usage: same to printf
*
* @return noreturn
* */
#define print_error(fmt, args...) \
do { \
printf("[error]:");\
printf(fmt, ##args); \
printf(",heap size:%d%s", esp_get_free_heap_size(), "\r\n"); \
vTaskDelay(2000 / portTICK_RATE_MS); \
} while(1)
#if APP_DEBUG
#define APP_LOG(level, ...) \
do { \
APP_PRINT("%s: %s <%s:%d>: ", level, __FILE__, __FUNCTION__, __LINE__); \
APP_PRINT(__VA_ARGS__); \
printf("\n"); \
} while (0)
#define APP_DBG(...) APP_LOG("DEBUG", __VA_ARGS__)
#define APP_WRN(...) APP_LOG("WARNING", __VA_ARGS__)
#define APP_ERR(...) APP_LOG("ERROR", __VA_ARGS__)
#else
#define APP_LOG(...)
#define APP_DBG(...)
#define APP_WRN(...)
#define APP_ERR(...)
#endif // endif APP_DEBUG
#endif // endif _DEBUG_PRINT_H_
#ifndef OC_CONFIG_H
#define OC_CONFIG_H
/* Time resolution */
#include <stdint.h>
#include <time.h>
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef uint64_t oc_clock_time_t;
#define OC_CLOCK_CONF_TICKS_PER_SECOND CLOCKS_PER_SEC
//#define OC_SPEC_VER_OIC
/* Security Layer */
/* Max inactivity timeout before tearing down DTLS connection */
#define OC_DTLS_INACTIVITY_TIMEOUT (600)
/* Maximum wait time for select function */
#define SELECT_TIMEOUT_SEC (1)
/* Add support for passing network up/down events to the app */
//#define OC_NETWORK_MONITOR
/* Add support for passing TCP/TLS/DTLS session connection events to the app */
#define OC_SESSION_EVENTS
/* Add support for software update */
//#define OC_SOFTWARE_UPDATE or run "make" with SWUPDATE=1
/* Add support for the oic.if.create interface in Collections */
//#define OC_COLLECTIONS_IF_CREATE or run "make" with CREATE=1
/* Add support for the maintenance resource */
//#define OC_MNT or run "make" with MNT=1
/* Add batch interface support to /oic/res */
#define OC_RES_BATCH_SUPPORT
/* Add support for dns lookup to the endpoint */
#define OC_DNS_LOOKUP
#define OC_DNS_CACHE
//#define OC_DNS_LOOKUP_IPV6
/* If we selected support for dynamic memory allocation */
#ifdef OC_DYNAMIC_ALLOCATION
#define OC_COLLECTIONS
#define OC_BLOCK_WISE
#else /* OC_DYNAMIC_ALLOCATION */
/* List of constraints below for a build that does not employ dynamic
memory allocation
*/
/* Memory pool sizes */
#define OC_BYTES_POOL_SIZE (1800)
#define OC_INTS_POOL_SIZE (100)
#define OC_DOUBLES_POOL_SIZE (4)
/* Server-side parameters */
/* Maximum number of server resources */
#define OC_MAX_APP_RESOURCES (4)
#define OC_MAX_NUM_COLLECTIONS (1)
/* Common paramters */
/* Prescriptive lower layers MTU size, enable block-wise transfers */
#define OC_BLOCK_WISE_SET_MTU (700)
/* Maximum size of request/response payloads */
#define OC_MAX_APP_DATA_SIZE (2048L)
/* Maximum number of concurrent requests */
#define OC_MAX_NUM_CONCURRENT_REQUESTS (3)
/* Maximum number of nodes in a payload tree structure */
#define OC_MAX_NUM_REP_OBJECTS (150)
/* Number of devices on the OCF platform */
#define OC_MAX_NUM_DEVICES (2)
/* Maximum number of endpoints */
#define OC_MAX_NUM_ENDPOINTS (20)
/* Security layer */
/* Maximum number of authorized clients */
#define OC_MAX_NUM_SUBJECTS (2)
/* Maximum number of concurrent (D)TLS sessions */
#define OC_MAX_TLS_PEERS (1)
/* Maximum number of peer for TCP channel */
#define OC_MAX_TCP_PEERS (CONFIG_LWIP_MAX_ACTIVE_TCP)
/* Maximum number of interfaces for IP adapter */
#define OC_MAX_IP_INTERFACES (2)
/* Maximum number of callbacks for Network interface event monitoring */
#define OC_MAX_NETWORK_INTERFACE_CBS (2)
/* Maximum number of callbacks for connection of session */
#define OC_MAX_SESSION_EVENT_CBS (2)
#endif /* !OC_DYNAMIC_ALLOCATION */
/* library features that require persistent storage */
#ifdef OC_SECURITY
#define OC_STORAGE
#endif
#ifdef OC_IDD_API
#define OC_STORAGE
#endif
#ifdef OC_CLOUD
#define OC_STORAGE
#endif
#ifdef OC_SOFTWARE_UPDATE
#define OC_STORAGE
#endif
#ifdef __cplusplus
}
#endif
#endif /* OC_CONFIG_H */
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#ifndef OC_ENDPOINT_EXT_H
#define OC_ENDPOINT_EXT_H
#include "oc_helpers.h"
#include "port/oc_connectivity.h"
void oc_init_endpoint_list(void);
int oc_add_endpoint_to_list(oc_endpoint_t *endpoint);
oc_endpoint_t *oc_get_endpoint_list(void);
#if 0
void oc_free_endpoint_list(void);
oc_endpoint_t *oc_new_endpoint(void);
void oc_free_endpoint(oc_endpoint_t *endpoint);
int oc_endpoint_to_string(oc_endpoint_t *endpoint, oc_string_t *endpoint_str);
int oc_string_to_endpoint(oc_string_t *endpoint_str, oc_endpoint_t *endpoint,
oc_string_t *uri);
int oc_ipv6_endpoint_is_link_local(oc_endpoint_t *endpoint);
int oc_endpoint_compare(oc_endpoint_t *ep1, oc_endpoint_t *ep2);
int oc_endpoint_compare_address(oc_endpoint_t *ep1, oc_endpoint_t *ep2);
#endif
#endif /* OC_ENDPOINT_EXT_H */
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include "port/oc_assert.h"
#include <stdlib.h>
#include <assert.h>
void
abort_impl(void)
{
assert(NULL);
}
void
exit_impl(int status)
{
(void)status;
assert(NULL);
}
/*
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include "port/oc_clock.h"
#include "port/oc_log.h"
#include <math.h>
#include <time.h>
#include <unistd.h>
void
oc_clock_init(void)
{
}
oc_clock_time_t
oc_clock_time(void)
{
oc_clock_time_t time = 0;
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
time = (oc_clock_time_t)t.tv_sec * OC_CLOCK_SECOND +
(oc_clock_time_t)ceil(t.tv_nsec / (1.e09 / OC_CLOCK_SECOND));
}
return time;
}
unsigned long
oc_clock_seconds(void)
{
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
return t.tv_sec;
}
return 0;
}
void
oc_clock_wait(oc_clock_time_t t)
{
usleep(t * 1.e03);
}
/*
This code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdint.h>
#include "debug_print.h"
#include "port/oc_connectivity.h"
void print_macro_info()
{
printf("\n****************************************\n");
#ifdef OC_DEBUG
printf("OC_DEBUG defined!\n");
#else
printf("OC_DEBUG not defined!\n");
#endif
#ifdef APP_DEBUG
printf("APP_DEBUG defined!\n");
#else
printf("APP_DEBUG not defined!\n");
#endif
#ifdef OC_CLIENT
printf("OC_CLIENT defined!\n");
#else
printf("OC_CLIENT not defined!\n");
#endif
#ifdef OC_SERVER
printf("OC_SERVER defined!\n");
#else
printf("OC_SERVER not defined!\n");
#endif
#ifdef OC_IPV4
printf("OC_IPV4 defined!\n");
#else
printf("OC_IPV4 not defined!\n");
#endif
#ifdef ENABLE_LIGHT
printf("ENABLE_LIGHT defined!\n");
#else
printf("ENABLE_LIGHT not defined!\n");
#endif
#ifdef OC_SECURITY
printf("OC_SECURITY defined!\n");
#else
printf("OC_SECURITY not defined!\n");
#endif
#ifdef OC_DYNAMIC_ALLOCATION
printf("OC_DYNAMIC_ALLOCATION defined!\n");
#else
printf("OC_DYNAMIC_ALLOCATION not defined!\n");
#endif
#ifdef OC_BLOCK_WISE
printf("OC_BLOCK_WISE defined!\n");
#else
printf("OC_BLOCK_WISE not defined!\n");
#endif
printf("\n****************************************\n");
}
void print_message_info(oc_message_t *message)
{
#ifdef OC_DEBUG
printf("\n****************************************\n");
#ifdef OC_IPV4
printf("ipv4 message info:\n------------------\n");
printf("message length:%d ref_count:%d\n", message->length, message->ref_count);
printf("endpoint flags:%d port:%d\naddr:", message->endpoint.flags, message->endpoint.addr.ipv4.port);
for (int i = 0; i < 4; ++i) {
printf("%d ", message->endpoint.addr.ipv4.address[i]);
}
#else
printf("ipv6 message info:\n------------------\n");
printf("message length:%d ref_count:%d\n", message->length, message->ref_count);
printf("endpoint flags:%d port:%d scope:%d\naddr:", message->endpoint.flags, message->endpoint.addr.ipv6.port, message->endpoint.addr.ipv6.scope);
for (int i = 0; i < 16; ++i) {
printf("%d ", message->endpoint.addr.ipv6.address[i]);
}
#endif
printf("\nmessage content:\n");
for (int i = 0; i < message->length; ++i) {
printf("%x ", message->data[i]);
}
printf("\n****************************************\n");
#endif
}
/**
* @brief print the data detail information
*
* print input data, print from data[0] to data[len-1], addtionally add notes string
*
* @param[in] data: input data pointer to print
* @param[in] len: data length
* @param[in] note: notes for read easily
* @param[in] mode: 0x00, 0x01, 0x10, 0x11 to decide the BINARY_SHOW && BYTES_SHOW
*
* @return noreturn
*
*/
void print_debug(const char* data, const unsigned int len, const char* note, int mode)
{
#define BINARY_SHOW 0x10
#define BYTES_SHOW 0x01
printf("\n********** %s [len:%u] start addr:%p **********\n", note, len, data);
int i = 0;
for (i = 0; i < len; ++i) {
if (BINARY_SHOW & mode) {
printf("%02x ",data[i]);
} else {
if(data[i] < 32 || data[i] > 126) { // control || invisible charset
if(i > 0 && (data[i-1] >= 33 && data[i-1] <= 126) )
printf(" ");
printf("%02x ",data[i]);
} else {
printf("%c", data[i]);
}
}
if ((BYTES_SHOW & mode) && ((i + 1) % 32 == 0)) {
printf(" | %d Bytes\n",i + 1);
}
} // end for
printf("\n---------- %s End ----------\n", note);
}
This diff is collapsed.
This diff is collapsed.
/****************************************************************************
*
* Copyright 2018 Samsung Electronics All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************/
#ifndef IPCONTEXT_H
#define IPCONTEXT_H
#include "oc_endpoint.h"
#include <pthread.h>
#include <stdint.h>
#include <sys/select.h>
#include <sys/socket.h>
#include "vfs_pipe.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum {
ADAPTER_STATUS_NONE = 0, /* Nothing happens */
ADAPTER_STATUS_ACCEPT, /* Receiving no meaningful data */
ADAPTER_STATUS_RECEIVE, /* Receiving meaningful data */
ADAPTER_STATUS_ERROR /* Error */
} adapter_receive_state_t;
#ifdef OC_TCP
typedef struct tcp_context_t
{
struct sockaddr_storage server;
int server_sock;
uint16_t port;
#ifdef OC_SECURITY
struct sockaddr_storage secure;
int secure_sock;
uint16_t tls_port;
#endif /* OC_SECURITY */
#ifdef OC_IPV4
struct sockaddr_storage server4;
int server4_sock;
uint16_t port4;
#ifdef OC_SECURITY
struct sockaddr_storage secure4;
int secure4_sock;
uint16_t tls4_port;
#endif /* OC_SECURITY */
#endif /* OC_IPV4 */
int connect_pipe[2];
pthread_mutex_t mutex;
} tcp_context_t;
#endif
typedef struct ip_context_t {
struct ip_context_t *next;
OC_LIST_STRUCT(eps);
struct sockaddr_storage mcast;
struct sockaddr_storage server;
int mcast_sock;
int server_sock;
uint16_t port;
#ifdef OC_SECURITY
struct sockaddr_storage secure;
int secure_sock;
uint16_t dtls_port;
#endif /* OC_SECURITY */
#ifdef OC_IPV4
struct sockaddr_storage mcast4;
struct sockaddr_storage server4;
int mcast4_sock;
int server4_sock;
uint16_t port4;
#ifdef OC_SECURITY
struct sockaddr_storage secure4;
int secure4_sock;
uint16_t dtls4_port;
#endif /* OC_SECURITY */
#endif /* OC_IPV4 */
#ifdef OC_TCP
tcp_context_t tcp;
#endif
pthread_t event_thread;
int terminate;
size_t device;
fd_set rfds;
int shutdown_pipe[2];
} ip_context_t;
#ifdef __cplusplus
}
#endif
#endif /* IPCONTEXT_H */