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>
This commit is contained in:
parent
b2e71a2fa2
commit
a845c57bd7
2 changed files with 111 additions and 45 deletions
|
@ -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,6 +27,10 @@ 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"
|
||||
|
||||
|
@ -34,6 +38,9 @@ LOG_MODULE_REGISTER(net_echo_client_sample, LOG_LEVEL_DBG);
|
|||
|
||||
#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,6 +123,93 @@ static void wait(void)
|
|||
}
|
||||
}
|
||||
|
||||
static void start_udp_and_tcp(struct k_work *work)
|
||||
{
|
||||
int ret;
|
||||
|
||||
LOG_INF("Starting...");
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_TCP)) {
|
||||
ret = start_tcp();
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_UDP)) {
|
||||
ret = start_udp();
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
prepare_fds();
|
||||
|
||||
while (true) {
|
||||
if (IS_ENABLED(CONFIG_NET_TCP)) {
|
||||
ret = process_tcp();
|
||||
if (ret < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_UDP)) {
|
||||
ret = process_udp();
|
||||
if (ret < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wait();
|
||||
}
|
||||
}
|
||||
|
||||
static void stop_udp_and_tcp(struct k_work *work)
|
||||
{
|
||||
LOG_INF("Stopping...");
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_UDP)) {
|
||||
stop_udp();
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_TCP)) {
|
||||
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);
|
||||
|
@ -128,57 +224,26 @@ static void init_app(void)
|
|||
}
|
||||
#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)
|
||||
{
|
||||
int ret;
|
||||
|
||||
init_app();
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_TCP)) {
|
||||
ret = start_tcp();
|
||||
if (ret < 0) {
|
||||
goto quit;
|
||||
}
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_UDP)) {
|
||||
ret = start_udp();
|
||||
if (ret < 0) {
|
||||
goto quit;
|
||||
}
|
||||
}
|
||||
|
||||
prepare_fds();
|
||||
|
||||
while (true) {
|
||||
if (IS_ENABLED(CONFIG_NET_TCP)) {
|
||||
ret = process_tcp();
|
||||
if (ret < 0) {
|
||||
goto quit;
|
||||
}
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_UDP)) {
|
||||
ret = process_udp();
|
||||
if (ret < 0) {
|
||||
goto quit;
|
||||
}
|
||||
}
|
||||
|
||||
wait();
|
||||
}
|
||||
|
||||
quit:
|
||||
LOG_INF("Stopping...");
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_UDP)) {
|
||||
stop_udp();
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_NET_TCP)) {
|
||||
stop_tcp();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue