summaryrefslogtreecommitdiff
path: root/samples/net/http_server/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'samples/net/http_server/src/main.c')
-rw-r--r--samples/net/http_server/src/main.c99
1 files changed, 34 insertions, 65 deletions
diff --git a/samples/net/http_server/src/main.c b/samples/net/http_server/src/main.c
index c01d789bb..e5ebd16e8 100644
--- a/samples/net/http_server/src/main.c
+++ b/samples/net/http_server/src/main.c
@@ -15,82 +15,49 @@
#include "http_write_utils.h"
#include "config.h"
-static void network_setup(void);
+/* Sets the network parameters */
+static
+int network_setup(struct net_context **net_ctx, net_tcp_accept_cb_t accept_cb,
+ const char *addr, uint16_t port);
void main(void)
{
+ struct net_context *net_ctx = NULL;
+
http_ctx_init();
http_url_default_handler(http_write_soft_404_not_found);
http_url_add("/headers", HTTP_URL_STANDARD, http_write_header_fields);
http_url_add("/index.html", HTTP_URL_STANDARD, http_write_it_works);
- network_setup();
+ network_setup(&net_ctx, http_accept_cb, ZEPHYR_ADDR, ZEPHYR_PORT);
}
-/**
- * @brief connection_cb Callback executed when a connection is accepted
- * @param ctx Network context
- * @param addr Client's address
- * @param addr_len Address length
- * @param status Status code, 0 on success, < 0 otherwise
- * @param data User-provided data
- */
-void connection_cb(struct net_context *net_ctx, struct sockaddr *addr,
- socklen_t addr_len, int status, void *data)
+static
+int network_setup(struct net_context **net_ctx, net_tcp_accept_cb_t accept_cb,
+ const char *addr, uint16_t port)
{
- struct http_server_ctx *http_ctx = NULL;
-
- ARG_UNUSED(addr_len);
- ARG_UNUSED(data);
-
- if (status != 0) {
- printk("Status code: %d\n", status);
- net_context_put(net_ctx);
- return;
- }
-
- print_client_banner(addr);
-
- http_ctx = http_ctx_get();
- if (!http_ctx) {
- printk("Unable to get free HTTP context\n");
- net_context_put(net_ctx);
- return;
- }
-
- http_ctx_set(http_ctx, net_ctx);
-
- net_context_recv(net_ctx, http_rx_tx, 0, http_ctx);
-}
-
-/**
- * @brief network_setup This routine configures the HTTP server.
- * It puts the application in listening mode and
- * installs the accept connection callback.
- */
-static void network_setup(void)
-{
- struct net_context *ctx = NULL;
struct sockaddr local_sock;
void *ptr;
int rc;
+ *net_ctx = NULL;
+
#ifdef CONFIG_NET_IPV6
- net_sin6(&local_sock)->sin6_port = htons(ZEPHYR_PORT);
- sock_addr.family = AF_INET6;
+ net_sin6(&local_sock)->sin6_port = htons(port);
+ local_sock.family = AF_INET6;
ptr = &(net_sin6(&local_sock)->sin6_addr);
- rc = net_addr_pton(AF_INET6, ZEPHYR_ADDR, ptr);
+ rc = net_addr_pton(AF_INET6, addr, ptr);
#else
- net_sin(&local_sock)->sin_port = htons(ZEPHYR_PORT);
+ net_sin(&local_sock)->sin_port = htons(port);
local_sock.family = AF_INET;
ptr = &(net_sin(&local_sock)->sin_addr);
- rc = net_addr_pton(AF_INET, ZEPHYR_ADDR, ptr);
+ rc = net_addr_pton(AF_INET, addr, ptr);
#endif
if (rc) {
- printk("Invalid IP address/Port: %s, %d\n",
- ZEPHYR_ADDR, ZEPHYR_PORT);
+ printk("Invalid IP address/Port: %s, %d\n", addr, port);
+ return rc;
}
#ifdef CONFIG_NET_IPV6
@@ -102,41 +69,43 @@ static void network_setup(void)
&net_sin(&local_sock)->sin_addr,
NET_ADDR_MANUAL, 0);
#endif
- print_server_banner(&local_sock);
#if defined(CONFIG_NET_IPV6)
- rc = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &ctx);
+ rc = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, net_ctx);
#else
- rc = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
+ rc = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, net_ctx);
#endif
if (rc != 0) {
printk("net_context_get error\n");
- return;
+ return rc;
}
- rc = net_context_bind(ctx, (const struct sockaddr *)&local_sock,
+ rc = net_context_bind(*net_ctx, (const struct sockaddr *)&local_sock,
sizeof(local_sock));
if (rc != 0) {
printk("net_context_bind error\n");
goto lb_error;
}
- rc = net_context_listen(ctx, 0);
+ rc = net_context_listen(*net_ctx, 0);
if (rc != 0) {
- printk("[%s:%d] net_context_listen %d, <%s>\n",
- __func__, __LINE__, rc, RC_STR(rc));
+ printk("net_context_listen error\n");
goto lb_error;
}
- rc = net_context_accept(ctx, connection_cb, 0, NULL);
+ rc = net_context_accept(*net_ctx, accept_cb, 0, NULL);
if (rc != 0) {
- printk("[%s:%d] net_context_accept %d, <%s>\n",
- __func__, __LINE__, rc, RC_STR(rc));
+ printk("net_context_accept error\n");
goto lb_error;
}
- return;
+ print_server_banner(&local_sock);
+
+ return 0;
lb_error:
- net_context_put(ctx);
+ net_context_put(*net_ctx);
+ *net_ctx = NULL;
+
+ return rc;
}