Morse Micro IoT SDK  2.10.4
ping.c
Go to the documentation of this file.
1/*
2 * Copyright 2021-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
21#include <string.h>
22#include "mmhal_app.h"
23#include "mmosal.h"
24#include "mmwlan.h"
25#include "mmconfig.h"
26#include "mmping.h"
27#include "mmutils.h"
28
29#include "mmipal.h"
30
31#include "mm_app_common.h"
32
33/* Ping configurations. */
34#ifndef PING_COUNT
36#define PING_COUNT 10
37#endif
38#ifndef PING_DATA_SIZE
40#define PING_DATA_SIZE 56
41#endif
42#ifndef PING_INTERVAL_MS
44#define PING_INTERVAL_MS 1000
45#endif
46#ifndef POST_PING_DELAY_MS
48#define POST_PING_DELAY_MS 10000
49#endif
50#ifndef UPDATE_INTERVAL_MS
53#define UPDATE_INTERVAL_MS (5000)
54#endif
55
56#if defined(ENABLE_PWR_MEAS) && ENABLE_PWR_MEAS
58#define PWR_MEAS_DELAY_MS(delay_ms) mmosal_task_sleep(delay_ms)
60#define SET_DEBUG_STATE(state) mmhal_set_debug_pins(MMHAL_ALL_DEBUG_PINS, state);
61#else
63#define PWR_MEAS_DELAY_MS(delay_ms) MM_UNUSED(delay_ms)
65#define SET_DEBUG_STATE(state) MM_UNUSED(state)
66#endif
67
75{
95};
96
101void app_init(void)
102{
104 struct mmping_args args = MMPING_ARGS_DEFAULT;
105
107
108 printf("\n\nMorse Ping Demo (Built " __DATE__ " " __TIME__ ")\n\n");
109
110 /* Initialize and connect to Wi-Fi, blocks till connected */
112
114
116
118
120
121 /* Delay to allow communications to settle so we measure only idle current */
123
125
126 PWR_MEAS_DELAY_MS(1000);
127
129
130 /* Get the target IP */
132 enum mmipal_status status = mmipal_get_ip_config(&ip_config);
133 if (status == MMIPAL_SUCCESS)
134 {
135 memcpy(args.ping_target, ip_config.gateway_addr, sizeof(ip_config.gateway_addr));
136 }
137 else
138 {
139 printf("Failed to retrieve IP config\n");
140 }
141 /* If ping.target is set, we use it as an override */
142 (void)mmconfig_read_string("ping.target", args.ping_target, sizeof(args.ping_target));
143
144 status = mmipal_get_local_addr(args.ping_src, args.ping_target);
145 if (status != MMIPAL_SUCCESS)
146 {
147 printf("Failed to get local address for PING\n");
148 }
149
150 args.ping_count = PING_COUNT;
151 mmconfig_read_uint32("ping.count", &args.ping_count);
152
154 mmconfig_read_uint32("ping.size", &args.ping_size);
155
157 mmconfig_read_uint32("ping.interval", &args.ping_interval_ms);
158
159 mmping_start(&args);
160 printf("\nPing %s %lu(%lu) bytes of data.\n",
161 args.ping_target,
162 args.ping_size,
164
165 struct mmping_stats stats;
166 uint32_t next_update_time_ms = mmosal_get_time_ms() + UPDATE_INTERVAL_MS;
167 unsigned last_ping_recv_count = 0;
168 mmping_stats(&stats);
169 while (stats.ping_is_running)
170 {
172 mmping_stats(&stats);
173 if (stats.ping_recv_count != last_ping_recv_count ||
174 mmosal_time_has_passed(next_update_time_ms))
175 {
176 printf("(%s) packets transmitted/received = %lu/%lu, "
177 "round-trip min/avg/max = %lu/%lu/%lu ms\n",
178 stats.ping_receiver,
179 stats.ping_total_count,
180 stats.ping_recv_count,
181 stats.ping_min_time_ms,
182 stats.ping_avg_time_ms,
183 stats.ping_max_time_ms);
184 last_ping_recv_count = stats.ping_recv_count;
185 next_update_time_ms = mmosal_get_time_ms() + UPDATE_INTERVAL_MS;
186 }
187 }
188
190
191 uint32_t loss = 0;
192 if (stats.ping_total_count == 0)
193 {
194 loss = 0;
195 }
196 else
197 {
198 loss = (1000 *
199 (stats.ping_total_count - stats.ping_recv_count) *
200 100 /
201 stats.ping_total_count);
202 }
203 printf("\n--- %s ping statistics ---\n%lu packets transmitted, %lu packets received, ",
204 stats.ping_receiver,
205 stats.ping_total_count,
206 stats.ping_recv_count);
207 printf("%lu.%03lu%% packet loss\nround-trip min/avg/max = %lu/%lu/%lu ms\n",
208 loss / 1000,
209 loss % 1000,
210 stats.ping_min_time_ms,
211 stats.ping_avg_time_ms,
212 stats.ping_max_time_ms);
213
214 /* Delay to allow communications to settle so we measure only idle current */
216
218
219 uint32_t post_ping_delay_ms = POST_PING_DELAY_MS;
220 mmconfig_read_uint32("ping.post_ping_delay_ms", &post_ping_delay_ms);
221 mmosal_task_sleep(post_ping_delay_ms);
222
224
225 /* Disconnect from Wi-Fi */
227}
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
int mmconfig_read_uint32(const char *key, uint32_t *value)
Returns the unsigned integer stored in persistent store identified by the key.
#define MMIPAL_IP_CONFIG_DEFAULT
Initializer for mmipal_ip_config.
Definition: mmipal.h:109
mmipal_status
Enumeration of status codes returned by MMIPAL functions.
Definition: mmipal.h:43
enum mmipal_status mmipal_get_ip_config(struct mmipal_ip_config *config)
Get the IP configurations.
enum mmipal_status mmipal_get_local_addr(mmipal_ip_addr_t local_addr, const mmipal_ip_addr_t dest_addr)
Gets the local address for the MMWLAN interface that is appropriate for a given destination address.
@ MMIPAL_SUCCESS
Completed successfully.
Definition: mmipal.h:45
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
static bool mmosal_time_has_passed(uint32_t t)
Check if the given time has already passed.
Definition: mmosal.h:727
#define MMPING_ICMP_ECHO_HDR_LEN
Length of the ICMP echo header in octets.
Definition: mmping.h:65
uint16_t mmping_start(const struct mmping_args *args)
Initialize ping parameters and start ping.
#define MMPING_ARGS_DEFAULT
Initializer for mmping_args.
Definition: mmping.h:96
void mmping_stats(struct mmping_stats *stats)
Get Ping Statistics.
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_stop(void)
Disconnects from Wi-Fi and de-initializes the WLAN interface.
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.
#define PING_COUNT
Number of ping requests to send.
Definition: ping.c:36
#define SET_DEBUG_STATE(state)
Disable GPIO writing if not measuring power consumption.
Definition: ping.c:65
#define PWR_MEAS_DELAY_MS(delay_ms)
Disable delays which are only useful for power consumption accuracy.
Definition: ping.c:63
#define POST_PING_DELAY_MS
Delay in ms to wait before terminating connection on completion of ping.
Definition: ping.c:48
#define PING_INTERVAL_MS
Interval between successive ping requests.
Definition: ping.c:44
#define PING_DATA_SIZE
Size of the ping request data, excluding 8-byte ICMP header.
Definition: ping.c:40
#define UPDATE_INTERVAL_MS
Interval (in milliseconds) at which to provide updates when the receive count has not changed.
Definition: ping.c:53
void app_init(void)
Main entry point to the application.
Definition: ping.c:101
debug_state
Enumeration of debug states that will be reflected on debug pins.
Definition: ping.c:75
@ DEBUG_STATE_INIT
Initial state at startup.
Definition: ping.c:77
@ DEBUG_STATE_BOOTING_CHIP
Indicates that we are booting the MM chip (note that this will also include the host MCU startup time...
Definition: ping.c:80
@ DEBUG_STATE_CONNECTED
Indicates we are connected to the AP.
Definition: ping.c:84
@ DEBUG_STATE_PINGING
Indicates that the ping is in progress.
Definition: ping.c:88
@ DEBUG_STATE_PING_DONE
Indicates that the ping has completed.
Definition: ping.c:90
@ DEBUG_STATE_IDLE
Indicates that we are idling with WLAN still on.
Definition: ping.c:92
@ DEBUG_STATE_TERMINATING
Indicates that we are disconnecting from the AP.
Definition: ping.c:94
@ DEBUG_STATE_CONNECTED_IDLE
Indicates that we have connected to the AP, but have not started the ping yet.
Definition: ping.c:86
@ DEBUG_STATE_CONNECTING
Indicates we are connecting to the AP.
Definition: ping.c:82
IPv4 configuration structure.
Definition: mmipal.h:97
mmipal_ip_addr_t gateway_addr
Gateway address.
Definition: mmipal.h:105
Ping request arguments data structure.
Definition: mmping.h:78
uint32_t ping_size
Specifies the data packet size in bytes excluding 8 bytes ICMP header.
Definition: mmping.h:92
char ping_src[MMPING_IPADDR_MAXLEN]
String representation of the local IP address.
Definition: mmping.h:80
char ping_target[MMPING_IPADDR_MAXLEN]
String representation of the IP address of the ping target.
Definition: mmping.h:82
uint32_t ping_interval_ms
The time interval between ping requests (in milliseconds)
Definition: mmping.h:84
uint32_t ping_count
This specifies the number of ping requests to send before terminating the session.
Definition: mmping.h:90
Data structure to store ping results.
Definition: mmping.h:109
uint32_t ping_avg_time_ms
The average latency in ms between request sent and response received.
Definition: mmping.h:119
bool ping_is_running
Stores the ping running status.
Definition: mmping.h:123
uint32_t ping_min_time_ms
The minimum latency in ms between request sent and response received.
Definition: mmping.h:117
uint32_t ping_max_time_ms
The maximum latency in ms between request sent and response received.
Definition: mmping.h:121
char ping_receiver[MMPING_IPADDR_MAXLEN]
String representation of the IP address of the ping receiver.
Definition: mmping.h:111
uint32_t ping_recv_count
The number of ping responses received.
Definition: mmping.h:115
uint32_t ping_total_count
Total number of requests sent.
Definition: mmping.h:113