Skip to content
Permalink
Browse files

samples: net: echo-client: Start service in correct time

Start to monitor Connected and Disconnect events and then start
and stop the echo service according to system connectivity status.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information...
jukkar committed Apr 14, 2019
1 parent b2e71a2 commit a845c57bd7b76f2d06b9c22523f8ef1ab72f7e56
Showing with 90 additions and 24 deletions.
  1. +1 −0 samples/net/sockets/echo_client/prj.conf
  2. +89 −24 samples/net/sockets/echo_client/src/echo-client.c
@@ -7,6 +7,7 @@ CONFIG_NET_IPV4=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_SOCKETS_POLL_MAX=4
CONFIG_NET_CONNECTION_MANAGER=y

# Kernel options
CONFIG_MAIN_STACK_SIZE=2048
@@ -27,13 +27,20 @@ LOG_MODULE_REGISTER(net_echo_client_sample, LOG_LEVEL_DBG);
#include <net/socket.h>
#include <net/tls_credentials.h>

#include <net/net_mgmt.h>
#include <net/net_event.h>
#include <net/net_conn_mgr.h>

#include "common.h"
#include "ca_certificate.h"

#define APP_BANNER "Run echo client"

#define INVALID_SOCK (-1)

#define EVENT_MASK (NET_EVENT_L4_CONNECTED | \
NET_EVENT_L4_DISCONNECTED)

/* Generated by http://www.lipsum.com/
* 2 paragraphs, 179 words, 1160 bytes of Lorem Ipsum
*/
@@ -77,6 +84,8 @@ struct configs conf = {
struct pollfd fds[4];
int nfds;

static struct net_mgmt_event_callback mgmt_cb;

static void prepare_fds(void)
{
if (conf.ipv4.udp.sock >= 0) {
@@ -114,40 +123,23 @@ static void wait(void)
}
}

static void init_app(void)
{
LOG_INF(APP_BANNER);

#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
int err = tls_credential_add(CA_CERTIFICATE_TAG,
TLS_CREDENTIAL_CA_CERTIFICATE,
ca_certificate,
sizeof(ca_certificate));
if (err < 0) {
LOG_ERR("Failed to register public certificate: %d", err);
}
#endif

init_vlan();
}

void main(void)
static void start_udp_and_tcp(struct k_work *work)
{
int ret;

init_app();
LOG_INF("Starting...");

if (IS_ENABLED(CONFIG_NET_TCP)) {
ret = start_tcp();
if (ret < 0) {
goto quit;
return;
}
}

if (IS_ENABLED(CONFIG_NET_UDP)) {
ret = start_udp();
if (ret < 0) {
goto quit;
return;
}
}

@@ -157,21 +149,23 @@ void main(void)
if (IS_ENABLED(CONFIG_NET_TCP)) {
ret = process_tcp();
if (ret < 0) {
goto quit;
break;
}
}

if (IS_ENABLED(CONFIG_NET_UDP)) {
ret = process_udp();
if (ret < 0) {
goto quit;
break;
}
}

wait();
}
}

quit:
static void stop_udp_and_tcp(struct k_work *work)
{
LOG_INF("Stopping...");

if (IS_ENABLED(CONFIG_NET_UDP)) {
@@ -182,3 +176,74 @@ void main(void)
stop_tcp();
}
}

static void do_service(k_work_handler_t handler)
{
static struct k_work work;

k_work_init(&work, handler);

k_work_submit(&work);
}

static void event_handler(struct net_mgmt_event_callback *cb,
u32_t mgmt_event, struct net_if *iface)
{
if ((mgmt_event & EVENT_MASK) != mgmt_event) {
return;
}

if (mgmt_event == NET_EVENT_L4_CONNECTED) {
LOG_INF("Network connected");

do_service(start_udp_and_tcp);

return;
}

if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
LOG_INF("Network disconnected");

do_service(stop_udp_and_tcp);

return;
}
}

static void init_app(void)
{
LOG_INF(APP_BANNER);

#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
int err = tls_credential_add(CA_CERTIFICATE_TAG,
TLS_CREDENTIAL_CA_CERTIFICATE,
ca_certificate,
sizeof(ca_certificate));
if (err < 0) {
LOG_ERR("Failed to register public certificate: %d", err);
}
#endif

if (IS_ENABLED(CONFIG_NET_CONNECTION_MANAGER)) {
net_mgmt_init_event_callback(&mgmt_cb,
event_handler, EVENT_MASK);
net_mgmt_add_event_callback(&mgmt_cb);

net_conn_mgr_resend_status();
}

init_vlan();
}

void main(void)
{
init_app();

if (!IS_ENABLED(CONFIG_NET_CONNECTION_MANAGER)) {
/* If the config library has not been configured to start the
* app only after we have a connection, then we can start
* it right away.
*/
do_service(start_udp_and_tcp);
}
}

0 comments on commit a845c57

Please sign in to comment.
You can’t perform that action at this time.