Skip to content

Commit

Permalink
ESP32 platform
Browse files Browse the repository at this point in the history
This is enough for all the test app features to work on ESP32 without
SSL.
  • Loading branch information
lws-team committed Feb 18, 2017
1 parent 936bf08 commit c7c4ae0
Show file tree
Hide file tree
Showing 16 changed files with 776 additions and 59 deletions.
27 changes: 25 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ option(LWS_WITH_GENERIC_SESSIONS "With the Generic Sessions plugin" OFF)
option(LWS_WITH_SQLITE3 "Require SQLITE3 support" OFF)
option(LWS_WITH_SMTP "Provide SMTP support" OFF)
option(LWS_WITH_ESP8266 "Build for ESP8266" OFF)
option(LWS_WITH_ESP32 "Build for ESP32" OFF)
option(LWS_PLAT_OPTEE "Build for OPTEE" OFF)
option(LWS_WITH_NO_LOGS "Disable all logging from being compiled in" OFF)
option(LWS_STATIC_PIC "Build the static version of the library with position-independent code" OFF)
Expand Down Expand Up @@ -176,6 +177,22 @@ if (LWS_WITH_ESP8266)
set(LWS_HAVE_GETIFADDRS 1)
endif()

if (LWS_WITH_ESP32)
set(LWS_WITH_SHARED OFF)
set(LWS_WITH_SSL OFF)
set(LWS_WITH_ZLIB OFF)
# set(LWS_WITHOUT_CLIENT ON)
set(LWS_WITHOUT_TESTAPPS ON)
set(LWS_WITHOUT_EXTENSIONS ON)
set(LWS_WITH_PLUGINS OFF)
set(LWS_WITH_RANGES ON)
# this implies no pthreads in the lib
set(LWS_MAX_SMP 1)
set(LWS_HAVE_MALLOC 1)
set(LWS_HAVE_REALLOC 1)
set(LWS_HAVE_GETIFADDRS 1)
endif()


if (WIN32)
# this implies no pthreads in the lib
Expand Down Expand Up @@ -609,8 +626,13 @@ else()
list(APPEND SOURCES
lib/lws-plat-optee.c)
else()
list(APPEND SOURCES
lib/lws-plat-unix.c)
if (LWS_WITH_ESP32)
list(APPEND SOURCES
lib/lws-plat-esp32.c)
else()
list(APPEND SOURCES
lib/lws-plat-unix.c)
endif()
endif()
endif()
endif()
Expand Down Expand Up @@ -1657,6 +1679,7 @@ message(" LWS_WITH_GENERIC_SESSIONS = ${LWS_WITH_GENERIC_SESSIONS}")
message(" LWS_STATIC_PIC = ${LWS_STATIC_PIC}")
message(" LWS_WITH_RANGES = ${LWS_WITH_RANGES}")
message(" LWS_PLAT_OPTEE = ${LWS_PLAT_OPTEE}")
message(" LWS_WITH_ESP32 = ${LWS_WITH_ESP32}")

message("---------------------------------------------------------------------")

Expand Down
9 changes: 9 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
menu "Libwebsockets"

config LWS
bool "Enable Libwebsockets"
default n
help
Enable Libwebsockets Library

endmenu
37 changes: 37 additions & 0 deletions component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
COMPONENT_ADD_INCLUDEDIRS := ../../../../../../../../../$(COMPONENT_BUILD_DIR)/include-ext

COMPONENT_OWNBUILDTARGET:= 1

CROSS_PATH1:=$(shell which xtensa-esp32-elf-gcc )
CROSS_PATH:= $(shell dirname $(CROSS_PATH1) )/..

#-DLWS_USE_BORINGSSL=1 \
# -DOPENSSL_ROOT_DIR="${PWD}/../../boringssl" \
# -DOPENSSL_LIBRARIES="${PWD}/../../boringssl/build/ssl/libssl.a;${PWD}/../../boringssl/build/crypto/libcrypto.a" \
# -DOPENSSL_INCLUDE_DIRS="${PWD}/../../boringssl/include" \
.PHONY: build
build:
cd $(COMPONENT_BUILD_DIR) ; \
echo "doing lws cmake" ; \
cmake $(COMPONENT_PATH) -DLWS_C_FLAGS="$(CFLAGS)" \
-DCROSS_PATH=$(CROSS_PATH) \
-DCOMPONENT_PATH=$(COMPONENT_PATH) \
-DCMAKE_TOOLCHAIN_FILE=$(COMPONENT_PATH)/cross-esp32.cmake \
-DCMAKE_BUILD_TYPE=RELEASE \
-DLWS_WITH_NO_LOGS=0 \
-DLWS_WITH_ESP32=1 ;\
mkdir -p $(COMPONENT_BUILD_DIR)/include-ext ; \
cp $(COMPONENT_PATH)/lib/libwebsockets.h \
$(COMPONENT_BUILD_DIR)/lws_config.h \
$(COMPONENT_BUILD_DIR)/include-ext ; \
make VERBOSE=1 && \
cp ${COMPONENT_BUILD_DIR}/lib/libwebsockets.a ${COMPONENT_BUILD_DIR}/liblibwebsockets.a

clean: myclean

myclean:
rm -rf ./build

INCLUDES := $(INCLUDES) -I build/

27 changes: 27 additions & 0 deletions cross-esp32.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# CMake Toolchain file for crosscompiling on ARM.
#
# This can be used when running cmake in the following way:
# cd build/
# cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake
#

# Target operating system name.
set(CMAKE_SYSTEM_NAME Linux)

# Name of C compiler.
set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/xtensa-esp32-elf-gcc")

SET(CMAKE_C_FLAGS "-nostdlib -Wall -Werror -I${COMPONENT_PATH}/../lwip/include/lwip/posix -I${COMPONENT_PATH}/../lwip/include/lwip -I${COMPONENT_PATH}/../lwip/include/lwip/port -I${COMPONENT_PATH}/../esp32/include/ ${LWS_C_FLAGS} -I${COMPONENT_PATH}/../nvs_flash/test_nvs_host -I${COMPONENT_PATH}/../freertos/include -Os" CACHE STRING "" FORCE)

# Where to look for the target environment. (More paths can be added here)
set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}")

# Adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment only.
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search headers and libraries in the target environment only.
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

62 changes: 35 additions & 27 deletions lib/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ lws_vhost_protocol_options(struct lws_vhost *vh, const char *name)
* inform every vhost that hasn't already done it, that
* his protocols are initializing
*/
int
LWS_VISIBLE int
lws_protocol_init(struct lws_context *context)
{
struct lws_vhost *vh = context->vhost_list;
Expand Down Expand Up @@ -301,11 +301,12 @@ static const struct lws_protocols protocols_dummy[] = {
lws_callback_http_dummy, /* callback */
0, /* per_session_data_size */
0, /* max frame size / rx buffer */
0, NULL
},
/*
* the other protocols are provided by lws plugins
*/
{ NULL, NULL, 0, 0 } /* terminator */
{ NULL, NULL, 0, 0, 0, NULL} /* terminator */
};

#ifdef LWS_PLAT_OPTEE
Expand All @@ -322,9 +323,9 @@ lws_create_vhost(struct lws_context *context,
const struct lws_protocol_vhost_options *pvo;
#ifdef LWS_WITH_PLUGINS
struct lws_plugin *plugin = context->plugin_list;
#endif
struct lws_protocols *lwsp;
int m, f = !info->pvo;
#endif
#ifdef LWS_HAVE_GETENV
char *p;
#endif
Expand Down Expand Up @@ -356,29 +357,31 @@ lws_create_vhost(struct lws_context *context,
else
vh->keepalive_timeout = 5;

#ifdef LWS_WITH_PLUGINS
if (plugin) {
/*
* give the vhost a unified list of protocols including the
* ones that came from plugins
*/
lwsp = lws_zalloc(sizeof(struct lws_protocols) *
(vh->count_protocols +
context->plugin_protocol_count + 1));
if (!lwsp)
return NULL;
/*
* give the vhost a unified list of protocols including the
* ones that came from plugins
*/
lwsp = lws_zalloc(sizeof(struct lws_protocols) *
(vh->count_protocols +
context->plugin_protocol_count + 1));
if (!lwsp) {
lwsl_err("OOM\n");
return NULL;
}

m = vh->count_protocols;
memcpy(lwsp, info->protocols,
sizeof(struct lws_protocols) * m);
m = vh->count_protocols;
memcpy(lwsp, info->protocols, sizeof(struct lws_protocols) * m);

/* for compatibility, all protocols enabled on vhost if only
* the default vhost exists. Otherwise only vhosts who ask
* for a protocol get it enabled.
*/
/* for compatibility, all protocols enabled on vhost if only
* the default vhost exists. Otherwise only vhosts who ask
* for a protocol get it enabled.
*/

if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
f = 0;
if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
f = 0;
(void)f;
#ifdef LWS_WITH_PLUGINS
if (plugin) {

while (plugin) {
for (n = 0; n < plugin->caps.count_protocols; n++) {
Expand All @@ -397,9 +400,12 @@ lws_create_vhost(struct lws_context *context,
}
plugin = plugin->list;
}
vh->protocols = lwsp;
} else
}
#endif

if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
vh->protocols = lwsp;
else
vh->protocols = info->protocols;

vh->same_vh_protocol_list = (struct lws **)
Expand Down Expand Up @@ -522,8 +528,10 @@ lws_create_vhost(struct lws_context *context,
goto bail;
if (lws_context_init_client_ssl(info, vh))
goto bail;
if (lws_context_init_server(info, vh))
if (lws_context_init_server(info, vh)) {
lwsl_err("init server failed\n");
goto bail;
}

while (1) {
if (!(*vh1)) {
Expand Down Expand Up @@ -583,7 +591,7 @@ lws_create_context(struct lws_context_creation_info *info)
#else
lwsl_notice("IPV6 not compiled in\n");
#endif
#ifndef LWS_PLAT_OPTEE
#if !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_ESP32)
lws_feature_status_libev(info);
lws_feature_status_libuv(info);
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ lws_read(struct lws *wsi, unsigned char *buf, size_t len)

read_ok:
/* Nothing more to do for now */
lwsl_info("%s: read_ok, used %ld\n", __func__, buf - oldbuf);
lwsl_info("%s: read_ok, used %ld\n", __func__, (long)(buf - oldbuf));

return buf - oldbuf;

Expand Down
19 changes: 13 additions & 6 deletions lib/libwebsockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
wsi->state != LWSS_CLIENT_UNCONNECTED &&
reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
!wsi->socket_is_permanently_unusable) {
lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, wsi->sock, wsi->state);
lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, (int)(long)wsi->sock, wsi->state);
n = shutdown(wsi->sock, SHUT_WR);
if (n)
lwsl_debug("closing: shutdown (state %d) ret %d\n", wsi->state, LWS_ERRNO);
Expand Down Expand Up @@ -638,7 +638,7 @@ lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
return NULL;
}

#if LWS_POSIX
#if LWS_POSIX && !defined(LWS_WITH_ESP32)
LWS_VISIBLE int
interface_to_sa(struct lws_vhost *vh, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
{
Expand Down Expand Up @@ -692,12 +692,12 @@ lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
ai.ai_family = PF_UNSPEC;
ai.ai_socktype = SOCK_STREAM;
ai.ai_flags = AI_CANONNAME;

#if !defined(LWS_WITH_ESP32)
if (getnameinfo((struct sockaddr *)ads,
sizeof(struct sockaddr_in),
name, name_len, NULL, 0, 0))
return -1;

#endif
if (!rip)
return 0;

Expand Down Expand Up @@ -928,6 +928,12 @@ lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
return 0;
}

LWS_VISIBLE LWS_EXTERN void
lws_set_fops(struct lws_context *context, struct lws_plat_file_ops *fops)
{
memcpy(&context->fops, fops, sizeof *fops);
}

/**
* lws_now_secs() - seconds since 1970-1-1
*
Expand Down Expand Up @@ -1580,7 +1586,7 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,

} else
#endif
#ifdef LWS_USE_IPV6
#if defined(LWS_USE_IPV6) && !defined(LWS_WITH_ESP32)
if (LWS_IPV6_ENABLED(vhost)) {
v = (struct sockaddr *)&serv_addr6;
n = sizeof(struct sockaddr_in6);
Expand Down Expand Up @@ -1634,14 +1640,15 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
bzero((char *) &serv_addr4, sizeof(serv_addr4));
serv_addr4.sin_addr.s_addr = INADDR_ANY;
serv_addr4.sin_family = AF_INET;
#if !defined(LWS_WITH_ESP32)

if (iface &&
interface_to_sa(vhost, iface,
(struct sockaddr_in *)v, n) < 0) {
lwsl_err("Unable to find interface %s\n", iface);
return -1;
}

#endif
serv_addr4.sin_port = htons(port);
} /* ipv4 */

Expand Down
Loading

0 comments on commit c7c4ae0

Please sign in to comment.