samples: net: civetweb: Refractor code structure

move all CivetWeb samples to one directory
add directory with CivetWeb common includes
update cmake files
move civetweb sample folder to samples/net

Signed-off-by: Alexander Kozhinov <AlexanderKozhinov@yandex.com>
This commit is contained in:
Alexander Kozhinov 2020-11-21 22:25:24 +01:00 committed by Jukka Rissanen
commit e35e8913f9
27 changed files with 46 additions and 387 deletions

View file

@ -1,4 +1,5 @@
/*
* Copyright (c) 2019 Antmicro Ltd
* Copyright (c) 2020 Alexander Kozhinov
* Mail: AlexanderKozhinov@yandex.com
*

View file

@ -1,4 +1,5 @@
/*
* Copyright (c) 2019 Antmicro Ltd
* Copyright (c) 2020 Alexander Kozhinov
* Mail: AlexanderKozhinov@yandex.com
*

View file

@ -0,0 +1,23 @@
# Copyright (c) 2020 Alexander Kozhinov
# Mail: AlexanderKozhinov@yandex.com
#
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
set(common_dir ${CMAKE_CURRENT_SOURCE_DIR}/../common)
set(common_src_dir ${common_dir}/src)
set(common_include_dir ${common_dir}/include)
include_directories(
${common_include_dir}
)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(civetweb_http_server)
target_sources(app PRIVATE
src/main.c
${common_src_dir}/libc_extensions.c
)

View file

@ -1,4 +1,4 @@
.. _sockets-civetweb-sample:
.. _civetweb-http_server-sample:
Civetweb sample
###############
@ -10,7 +10,7 @@ This sample application uses the HTTP APIs provided by the external `Civetweb <h
The Civetweb module is available as a west :ref:`module <modules>`.
The source code for this sample application can be found at:
:zephyr_file:`samples/net/sockets/civetweb`.
:zephyr_file:`samples/net/civetweb/http_server`.
Requirements
************
@ -26,7 +26,7 @@ This sample was tested on the Atmel SAM E70 Xplained board, so this is the recom
Build it with:
.. zephyr-app-commands::
:zephyr-app: samples/net/sockets/civetweb
:zephyr-app: samples/net/civetweb/http_server
:board: sam_e70_xplained
:goals: build
:compact:

View file

@ -1,6 +1,6 @@
sample:
description: Civetweb HTTP API sample
name: civetweb
name: http_server
tests:
sample.net.sockets.civetweb:
sample.net.civetweb.http_server:
platform_allow: sam_e70_xplained

View file

@ -5,10 +5,17 @@
cmake_minimum_required(VERSION 3.13.1)
set(common_dir ${CMAKE_CURRENT_SOURCE_DIR}/../common)
set(common_src_dir ${common_dir}/src)
set(common_include_dir ${common_dir}/include)
set(inc_dir ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(src_dir ${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${inc_dir})
include_directories(
${inc_dir}
${common_include_dir}
)
option(CIVETWEB_ENABLE_WEBSOCKETS "Enable websockets connections" ON)
@ -17,7 +24,7 @@ project(civetweb_websocket_server)
target_sources(app PRIVATE
${src_dir}/main.c
${src_dir}/libc_extensions.c
${common_src_dir}/libc_extensions.c
${src_dir}/http_server_handlers.c
${src_dir}/websocket_server_handlers.c)

View file

@ -1,4 +1,4 @@
.. _sockets-civetweb-websocket-server-sample:
.. _civetweb-websocket-server-sample:
Civetweb WebSocket Server sample
################################
@ -12,7 +12,7 @@ server demonstrating selected Civetweb features.
The Civetweb module is available as a west :ref:`module <modules>`.
The source code for this sample application can be found at:
:zephyr_file:`samples/net/sockets/civetweb_websocket_server`.
:zephyr_file:`samples/net/civetweb/websocket_server`.
Requirements
************
@ -28,7 +28,7 @@ This sample was tested on the NUCLEO H745ZI-Q board, so this is the recommended
Build it with:
.. zephyr-app-commands::
:zephyr-app: samples/net/sockets/civetweb_websocket_server
:zephyr-app: samples/net/civetweb/websocket_server
:board: nucleo_h745zi_q_m7
:goals: build
:compact:
@ -37,8 +37,8 @@ The sample application uses a static IP configuration.
After flashing the board, the server can be accessed with the web browser
of your choice (preferably Chrome) under ``192.0.2.1`` IPv4 address.
The IP address can be changed in :zephyr_file:`samples/net/sockets/civetweb_websocket_server/prj.conf`
The port number can be changed in :zephyr_file:`samples/net/sockets/civetweb_websocket_server/main.c`
The IP address can be changed in :zephyr_file:`samples/net/civetweb/websocket_server/prj.conf`
The port number can be changed in :zephyr_file:`samples/net/civetweb/websocket_server/main.c`
This sample application consists of two main parts:

View file

@ -1,6 +1,6 @@
sample:
description: Civetweb WebSocket Server sample
name: civetweb_websocket_server
name: websocket_server
tests:
sample.net.sockets.civetweb_websocket_server:
sample.net.civetweb.websocket_server:
platform_allow: nucleo_h745zi_q_m7

View file

@ -1,8 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(civetweb)
target_sources(app PRIVATE src/main.c src/libc_extensions.c)

View file

@ -1,28 +0,0 @@
/*
* Copyright (c) 2019 Antmicro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
static void log_access(const struct mg_connection *conn)
{
const struct mg_request_info *ri;
char src_addr[IP_ADDR_STR_LEN];
if (!conn || !conn->dom_ctx) {
return;
}
ri = &conn->request_info;
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
printf("%s - \"%s %s%s%s HTTP/%s\" %d\n",
src_addr,
ri->request_method ? ri->request_method : "-",
ri->request_uri ? ri->request_uri : "-",
ri->query_string ? "?" : "",
ri->query_string ? ri->query_string : "",
ri->http_version,
conn->status_code);
}

View file

@ -1,18 +0,0 @@
/*
* Copyright (c) 2019 Antmicro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
static void mg_cry_internal_impl(const struct mg_connection *conn,
const char *func,
unsigned line,
const char *fmt,
va_list ap)
{
(void)conn;
printf("[INTERNAL ERROR]: %s @ %d\n", func, line);
vprintf(fmt, ap);
printf("\n");
}

View file

@ -1,247 +0,0 @@
/*
* Copyright (c) 2019 Antmicro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "libc_extensions.h"
#define FN_MISSING() printf("[IMPLEMENTATION MISSING : %s]\n", __func__)
int iscntrl(int c)
{
/* All the characters placed before the space on the ASCII table
* and the 0x7F character (DEL) are control characters.
*/
return (int)(c < ' ' || c == 0x7F);
}
size_t strftime(char *dst, size_t dst_size,
const char *fmt,
const struct tm *tm)
{
FN_MISSING();
return 0;
}
double difftime(time_t end, time_t beg)
{
return end - beg;
}
struct __strerr_wrap {
int err;
const char *errstr;
};
/* Implementation suggested by @rakons in #16527 */
#define STRERR_DEFINE(e) {e, #e}
static const struct __strerr_wrap error_strings[] = {
STRERR_DEFINE(EILSEQ),
STRERR_DEFINE(EDOM),
STRERR_DEFINE(ERANGE),
STRERR_DEFINE(ENOTTY),
STRERR_DEFINE(EACCES),
STRERR_DEFINE(EPERM),
STRERR_DEFINE(ENOENT),
STRERR_DEFINE(ESRCH),
STRERR_DEFINE(EEXIST),
STRERR_DEFINE(ENOSPC),
STRERR_DEFINE(ENOMEM),
STRERR_DEFINE(EBUSY),
STRERR_DEFINE(EINTR),
STRERR_DEFINE(EAGAIN),
STRERR_DEFINE(ESPIPE),
STRERR_DEFINE(EXDEV),
STRERR_DEFINE(EROFS),
STRERR_DEFINE(ENOTEMPTY),
STRERR_DEFINE(ECONNRESET),
STRERR_DEFINE(ETIMEDOUT),
STRERR_DEFINE(ECONNREFUSED),
STRERR_DEFINE(EHOSTDOWN),
STRERR_DEFINE(EHOSTUNREACH),
STRERR_DEFINE(EADDRINUSE),
STRERR_DEFINE(EPIPE),
STRERR_DEFINE(EIO),
STRERR_DEFINE(ENXIO),
STRERR_DEFINE(ENOTBLK),
STRERR_DEFINE(ENODEV),
STRERR_DEFINE(ENOTDIR),
STRERR_DEFINE(EISDIR),
STRERR_DEFINE(ETXTBSY),
STRERR_DEFINE(ENOEXEC),
STRERR_DEFINE(EINVAL),
STRERR_DEFINE(E2BIG),
STRERR_DEFINE(ELOOP),
STRERR_DEFINE(ENAMETOOLONG),
STRERR_DEFINE(ENFILE),
STRERR_DEFINE(EMFILE),
STRERR_DEFINE(EBADF),
STRERR_DEFINE(ECHILD),
STRERR_DEFINE(EFAULT),
STRERR_DEFINE(EFBIG),
STRERR_DEFINE(EMLINK),
STRERR_DEFINE(ENOLCK),
STRERR_DEFINE(EDEADLK),
STRERR_DEFINE(ECANCELED),
STRERR_DEFINE(ENOSYS),
STRERR_DEFINE(ENOMSG),
STRERR_DEFINE(ENOSTR),
STRERR_DEFINE(ENODATA),
STRERR_DEFINE(ETIME),
STRERR_DEFINE(ENOSR),
STRERR_DEFINE(EPROTO),
STRERR_DEFINE(EBADMSG),
STRERR_DEFINE(ENOTSOCK),
STRERR_DEFINE(EDESTADDRREQ),
STRERR_DEFINE(EMSGSIZE),
STRERR_DEFINE(EPROTOTYPE),
STRERR_DEFINE(ENOPROTOOPT),
STRERR_DEFINE(EPROTONOSUPPORT),
STRERR_DEFINE(ESOCKTNOSUPPORT),
STRERR_DEFINE(ENOTSUP),
STRERR_DEFINE(EPFNOSUPPORT),
STRERR_DEFINE(EAFNOSUPPORT),
STRERR_DEFINE(EADDRNOTAVAIL),
STRERR_DEFINE(ENETDOWN),
STRERR_DEFINE(ENETUNREACH),
STRERR_DEFINE(ENETRESET),
STRERR_DEFINE(ECONNABORTED),
STRERR_DEFINE(ENOBUFS),
STRERR_DEFINE(EISCONN),
STRERR_DEFINE(ENOTCONN),
STRERR_DEFINE(ESHUTDOWN),
STRERR_DEFINE(EALREADY),
STRERR_DEFINE(EINPROGRESS),
};
static char *strerr_unknown = "UNKNOWN";
char *strerror(int err)
{
int i;
for (i = 0; i < ARRAY_SIZE(error_strings); ++i) {
if (error_strings[i].err == err) {
return (char *)error_strings[i].errstr;
}
}
return strerr_unknown;
}
int sscanf(const char *s, const char *format, ...)
{
FN_MISSING();
return 0;
}
double atof(const char *str)
{
/* XXX good enough for civetweb uses */
return (double)atoi(str);
}
long long int strtoll(const char *str, char **endptr, int base)
{
/* XXX good enough for civetweb uses */
return (long long int)strtol(str, endptr, base);
}
time_t time(time_t *t)
{
return 0;
}
/*
* Most of the wrappers below are copies of the wrappers in net/sockets.h,
* but they are available only if CONFIG_NET_SOCKETS_POSIX_NAMES is enabled
* which is impossible here.
*/
int getsockname(int sock, struct sockaddr *addr,
socklen_t *addrlen)
{
return zsock_getsockname(sock, addr, addrlen);
}
int poll(struct zsock_pollfd *fds, int nfds, int timeout)
{
return zsock_poll(fds, nfds, timeout);
}
int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
char *host, socklen_t hostlen,
char *serv, socklen_t servlen, int flags)
{
return zsock_getnameinfo(addr, addrlen, host, hostlen,
serv, servlen, flags);
}
ssize_t send(int sock, const void *buf, size_t len, int flags)
{
return zsock_send(sock, buf, len, flags);
}
ssize_t recv(int sock, void *buf, size_t max_len, int flags)
{
return zsock_recv(sock, buf, max_len, flags);
}
int socket(int family, int type, int proto)
{
return zsock_socket(family, type, proto);
}
int getaddrinfo(const char *host, const char *service,
const struct zsock_addrinfo *hints,
struct zsock_addrinfo **res)
{
return zsock_getaddrinfo(host, service, hints, res);
}
void freeaddrinfo(struct zsock_addrinfo *ai)
{
zsock_freeaddrinfo(ai);
}
int connect(int sock, const struct sockaddr *addr,
socklen_t addrlen)
{
return zsock_connect(sock, addr, addrlen);
}
int getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen)
{
return zsock_getsockopt(sock, level, optname, optval, optlen);
}
int setsockopt(int sock, int level, int optname,
const void *optval, socklen_t optlen)
{
return zsock_setsockopt(sock, level, optname, optval, optlen);
}
int listen(int sock, int backlog)
{
return zsock_listen(sock, backlog);
}
int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
{
return zsock_bind(sock, addr, addrlen);
}
int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
{
return zsock_accept(sock, addr, addrlen);
}

View file

@ -1,72 +0,0 @@
/*
* Copyright (c) 2019 Antmicro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <net/socket.h>
#define pollfd zsock_pollfd
#define fcntl zsock_fcntl
#define POLLIN ZSOCK_POLLIN
#define POLLOUT ZSOCK_POLLOUT
#define addrinfo zsock_addrinfo
#define F_SETFD 2
#define FD_CLOEXEC 1
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
int iscntrl(int c);
double atof(const char *str);
long long int strtoll(const char *str, char **endptr, int base);
int sscanf(const char *s, const char *format, ...);
char *strerror(int err);
unsigned long long int strtoull(const char *str, char **endptr, int base);
time_t time(time_t *t);
struct tm *gmtime(const time_t *ptime);
size_t strftime(char *dst, size_t dst_size, const char *fmt,
const struct tm *tm);
double difftime(time_t end, time_t beg);
struct tm *localtime(const time_t *timer);
int fileno(FILE *stream);
int ferror(FILE *stream);
int fclose(FILE *stream);
int fseeko(FILE *stream, off_t offset, int whence);
FILE *fopen(const char *filename, const char *mode);
char *fgets(char *str, int num, FILE *stream);
size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
int remove(const char *filename);
int getsockname(int sock, struct sockaddr *addr, socklen_t *addrlen);
int poll(struct zsock_pollfd *fds, int nfds, int timeout);
int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
char *host, socklen_t hostlen,
char *serv, socklen_t servlen, int flags);
ssize_t send(int sock, const void *buf, size_t len, int flags);
ssize_t recv(int sock, void *buf, size_t max_len, int flags);
int socket(int family, int type, int proto);
int getaddrinfo(const char *host, const char *service,
const struct zsock_addrinfo *hints,
struct zsock_addrinfo **res);
void freeaddrinfo(struct zsock_addrinfo *ai);
int connect(int sock, const struct sockaddr *addr, socklen_t addrlen);
int getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen);
int setsockopt(int sock, int level, int optname,
const void *optval, socklen_t optlen);
int listen(int sock, int backlog);
int accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
int bind(int sock, const struct sockaddr *addr, socklen_t addrlen);