summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorFlavio Santes <flavio.santes@intel.com>2017-02-12 00:43:34 -0600
committerJukka Rissanen <jukka.rissanen@linux.intel.com>2017-02-14 08:30:34 +0200
commitf40bdd67ac1cdc963a25840b3801fc5b7090c8c4 (patch)
tree58ec981f0f80c720120566092d1b208238cc7ced /samples
parent79a064fae814d04fb615bc60a3975b934f4853c2 (diff)
net/http: Improve network configuration routines
Modify the network setup routine to accept the following parameters: - network context - accept callback - server IP address - server port This change will allow us to reuse this routine for TLS. Change-Id: I1fdbaa908783c69f87863cbc597b42f39358762c Signed-off-by: Flavio Santes <flavio.santes@intel.com>
Diffstat (limited to 'samples')
-rw-r--r--samples/net/http_server/src/http_server.c26
-rw-r--r--samples/net/http_server/src/http_server.h4
-rw-r--r--samples/net/http_server/src/main.c99
3 files changed, 64 insertions, 65 deletions
diff --git a/samples/net/http_server/src/http_server.c b/samples/net/http_server/src/http_server.c
index 3355a4ce0..a94c59ede 100644
--- a/samples/net/http_server/src/http_server.c
+++ b/samples/net/http_server/src/http_server.c
@@ -21,6 +21,32 @@
NET_BUF_POOL_DEFINE(http_msg_pool, HTTP_BUF_CTR, HTTP_BUF_SIZE, 0, NULL);
+void http_accept_cb(struct net_context *net_ctx, struct sockaddr *addr,
+ socklen_t addr_len, int status, void *data)
+{
+ struct http_server_ctx *http_ctx = NULL;
+
+ ARG_UNUSED(addr_len);
+ ARG_UNUSED(data);
+
+ if (status != 0) {
+ net_context_put(net_ctx);
+ return;
+ }
+
+ print_client_banner(addr);
+
+ http_ctx = http_ctx_get();
+ if (!http_ctx) {
+ net_context_put(net_ctx);
+ return;
+ }
+
+ http_ctx_set(http_ctx, net_ctx);
+
+ net_context_recv(net_ctx, http_rx_tx, K_NO_WAIT, http_ctx);
+}
+
/**
* @brief http_ctx_release Releases an HTTP context
* @return 0, future versions may return error codes
diff --git a/samples/net/http_server/src/http_server.h b/samples/net/http_server/src/http_server.h
index 45751c56f..5e00279d3 100644
--- a/samples/net/http_server/src/http_server.h
+++ b/samples/net/http_server/src/http_server.h
@@ -9,6 +9,10 @@
#include <net/net_context.h>
+/* Callback executed when a new connection is accepted */
+void http_accept_cb(struct net_context *net_ctx, struct sockaddr *addr,
+ socklen_t addr_len, int status, void *data);
+
/**
* @brief http_rx_tx Reads the HTTP request from the `rx` buffer
* and writes an HTTP 1.1 200 OK response with client
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;
}