net: ppp: Add proper support to receive Echo-Reply message

Currently only net-shell calls net_ppp_ping() command, so make
it return the amount of time that it took to receive Echo-Reply
so the net-shell can print the round trip time value.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2019-06-25 16:29:58 +03:00
parent a62c6b2b04
commit c4a692a85f
4 changed files with 70 additions and 3 deletions

View file

@ -182,6 +182,17 @@ enum ipv6cp_option_type {
IPV6CP_OPTION_INTERFACE_IDENTIFIER = 1,
} __packed;
/**
* @typedef net_ppp_lcp_echo_reply_cb_t
* @brief A callback function that can be called if a Echo-Reply needs to
* be received.
* @param user_data User settable data that is passed to the callback
* function.
* @param user_data_len Length of the user data.
*/
typedef void (*net_ppp_lcp_echo_reply_cb_t)(void *user_data,
size_t user_data_len);
/**
* Generic PPP Finite State Machine
*/
@ -427,6 +438,18 @@ struct ppp_context {
#if defined(CONFIG_NET_SHELL)
struct {
struct {
/** Callback to be called when Echo-Reply is received.
*/
net_ppp_lcp_echo_reply_cb_t cb;
/** User specific data for the callback */
void *user_data;
/** User data length */
size_t user_data_len;
} echo_reply;
/** Used when waiting Echo-Reply */
struct k_sem wait_echo_reply;

View file

@ -3101,6 +3101,15 @@ static int cmd_net_ppp_ping(const struct shell *shell, size_t argc,
} else {
PR_INFO("PPP Echo-Req failed (%d)\n", ret);
}
} else {
if (ret > 1000) {
PR_INFO("%s%d msec\n",
"Received PPP Echo-Reply in ",
ret / 1000);
} else {
PR_INFO("%s%d usec\n",
"Received PPP Echo-Reply in ", ret);
}
}
} else {
PR_INFO("PPP network interface must be given.\n");

View file

@ -1109,7 +1109,16 @@ enum net_verdict ppp_fsm_recv_echo_reply(struct ppp_fsm *fsm,
NET_DBG("[%s/%p] Current state %s (%d)", fsm->name, fsm,
ppp_state_str(fsm->state), fsm->state);
return NET_DROP;
#if defined(CONFIG_NET_SHELL)
struct ppp_context *ctx = CONTAINER_OF(fsm, struct ppp_context,
lcp.fsm);
if (ctx->shell.echo_reply.cb) {
ctx->shell.echo_reply.cb(ctx->shell.echo_reply.user_data,
ctx->shell.echo_reply.user_data_len);
}
#endif /* CONFIG_NET_SHELL */
return NET_OK;
}
enum net_verdict ppp_fsm_recv_discard_req(struct ppp_fsm *fsm,

View file

@ -7,6 +7,7 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(net_l2_ppp, CONFIG_NET_L2_PPP_LOG_LEVEL);
#include <stdlib.h>
#include <net/net_core.h>
#include <net/net_l2.h>
#include <net/net_if.h>
@ -336,6 +337,19 @@ static int get_ppp_context(int idx, struct ppp_context **ctx,
return 0;
}
static void echo_reply_handler(void *user_data, size_t user_data_len)
{
struct ppp_context *ctx = user_data;
u32_t end_time = k_cycle_get_32();
int time_diff;
time_diff = abs(end_time - ctx->shell.echo_req_data);
ctx->shell.echo_req_data =
SYS_CLOCK_HW_CYCLES_TO_NS64(time_diff) / 1000;
k_sem_give(&ctx->shell.wait_echo_reply);
}
int net_ppp_ping(int idx, s32_t timeout)
{
struct ppp_context *ctx;
@ -347,7 +361,10 @@ int net_ppp_ping(int idx, s32_t timeout)
return ret;
}
ctx->shell.echo_req_data = sys_rand32_get();
ctx->shell.echo_req_data = k_cycle_get_32();
ctx->shell.echo_reply.cb = echo_reply_handler;
ctx->shell.echo_reply.user_data = ctx;
ctx->shell.echo_reply.user_data_len = sizeof(ctx);
ret = ppp_send_pkt(&ctx->lcp.fsm, iface, PPP_ECHO_REQ, 0,
UINT_TO_POINTER(ctx->shell.echo_req_data),
@ -356,7 +373,16 @@ int net_ppp_ping(int idx, s32_t timeout)
return ret;
}
return k_sem_take(&ctx->shell.wait_echo_reply, timeout);
ret = k_sem_take(&ctx->shell.wait_echo_reply, timeout);
ctx->shell.echo_reply.cb = NULL;
if (ret < 0) {
return ret;
}
/* Returns amount of microseconds waited */
return ctx->shell.echo_req_data;
}
struct ppp_context *net_ppp_context_get(int idx)