Morse Micro IoT SDK  2.9.7
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.h"
23#include "mmosal.h"
24#include "mmwlan.h"
25#include "mmconfig.h"
26#include "mmping.h"
27
28#include "mmipal.h"
29
30#include "mm_app_common.h"
31
32/* Ping configurations. */
33#ifndef PING_COUNT
35#define PING_COUNT 10
36#endif
37#ifndef PING_DATA_SIZE
39#define PING_DATA_SIZE 56
40#endif
41#ifndef PING_INTERVAL_MS
43#define PING_INTERVAL_MS 1000
44#endif
45#ifndef POST_PING_DELAY_MS
47#define POST_PING_DELAY_MS 10000
48#endif
49#ifndef UPDATE_INTERVAL_MS
52#define UPDATE_INTERVAL_MS (5000)
53#endif
54
62{
82};
83
89static void set_debug_state(enum debug_state state)
90{
92}
93
98void app_init(void)
99{
101 struct mmping_args args = MMPING_ARGS_DEFAULT;
102
104
105 printf("\n\nMorse Ping Demo (Built " __DATE__ " " __TIME__ ")\n\n");
106
107 /* Initialize and connect to Wi-Fi, blocks till connected */
109
111
113
115
116 /* Delay to allow communications to settle so we measure only idle current */
118
120
121 mmosal_task_sleep(1000);
122
124
125 /* Get the target IP */
127 enum mmipal_status status = mmipal_get_ip_config(&ip_config);
128 if (status == MMIPAL_SUCCESS)
129 {
130 memcpy(args.ping_target, ip_config.gateway_addr, sizeof(ip_config.gateway_addr));
131 }
132 else
133 {
134 printf("Failed to retrieve IP config\n");
135 }
136 /* If ping.target is set, we use it as an override */
137 (void)mmconfig_read_string("ping.target", args.ping_target, sizeof(args.ping_target));
138
139 status = mmipal_get_local_addr(args.ping_src, args.ping_target);
140 if (status != MMIPAL_SUCCESS)
141 {
142 printf("Failed to get local address for PING\n");
143 }
144
145 args.ping_count = PING_COUNT;
146 mmconfig_read_uint32("ping.count", &args.ping_count);
147
149 mmconfig_read_uint32("ping.size", &args.ping_size);
150
152 mmconfig_read_uint32("ping.interval", &args.ping_interval_ms);
153
154 mmping_start(&args);
155 printf("\nPing %s %lu(%lu) bytes of data.\n", args.ping_target, args.ping_size,
157
158 struct mmping_stats stats;
159 uint32_t next_update_time_ms = mmosal_get_time_ms() + UPDATE_INTERVAL_MS;
160 unsigned last_ping_recv_count = 0;
161 mmping_stats(&stats);
162 while (stats.ping_is_running)
163 {
165 mmping_stats(&stats);
166 if (stats.ping_recv_count != last_ping_recv_count ||
167 mmosal_time_has_passed(next_update_time_ms))
168 {
169 printf("(%s) packets transmitted/received = %lu/%lu, "
170 "round-trip min/avg/max = %lu/%lu/%lu ms\n",
173 last_ping_recv_count = stats.ping_recv_count;
174 next_update_time_ms = mmosal_get_time_ms() + UPDATE_INTERVAL_MS;
175 }
176 }
177
179
180 uint32_t loss = 0;
181 if (stats.ping_total_count == 0)
182 {
183 loss = 0;
184 }
185 else
186 {
187 loss = (1000 * (stats.ping_total_count - stats.ping_recv_count) * 100 /
188 stats.ping_total_count);
189 }
190 printf("\n--- %s ping statistics ---\n%lu packets transmitted, %lu packets received, ",
192 printf("%lu.%03lu%% packet loss\nround-trip min/avg/max = %lu/%lu/%lu ms\n",
193 loss/1000, loss%1000, stats.ping_min_time_ms, stats.ping_avg_time_ms,
194 stats.ping_max_time_ms);
195
196 /* Delay to allow communications to settle so we measure only idle current */
198
200
201 uint32_t post_ping_delay_ms = POST_PING_DELAY_MS;
202 mmconfig_read_uint32("ping.post_ping_delay_ms", &post_ping_delay_ms);
203 mmosal_task_sleep(post_ping_delay_ms);
204
206
207 /* Disconnect from Wi-Fi */
209}
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 MMHAL_ALL_DEBUG_PINS
Bit mask with all debug pins selected.
Definition: mmhal.h:217
void mmhal_set_debug_pins(uint32_t mask, uint32_t values)
Set the value one or more debug pins.
#define MMIPAL_IP_CONFIG_DEFAULT
Initializer for mmipal_ip_config.
Definition: mmipal.h:100
mmipal_status
Enumeration of status codes returned by MMIPAL functions.
Definition: mmipal.h:37
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:39
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:725
#define MMPING_ICMP_ECHO_HDR_LEN
Length of the ICMP echo header in octets.
Definition: mmping.h:64
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:95
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:35
static void set_debug_state(enum debug_state state)
Perform necessary operation (i.e., setting GPIO pins) upon entering the given debug state.
Definition: ping.c:89
#define POST_PING_DELAY_MS
Delay in ms to wait before terminating connection on completion of ping.
Definition: ping.c:47
#define PING_INTERVAL_MS
Interval between successive ping requests.
Definition: ping.c:43
#define PING_DATA_SIZE
Size of the ping request data, excluding 8-byte ICMP header.
Definition: ping.c:39
#define UPDATE_INTERVAL_MS
Interval (in milliseconds) at which to provide updates when the receive count has not changed.
Definition: ping.c:52
void app_init(void)
Main entry point to the application.
Definition: ping.c:98
debug_state
Enumeration of debug states that will be reflected on debug pins.
Definition: ping.c:62
@ DEBUG_STATE_INIT
Initial state at startup.
Definition: ping.c:64
@ 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:67
@ DEBUG_STATE_CONNECTED
Indicates we are connected to the AP.
Definition: ping.c:71
@ DEBUG_STATE_PINGING
Indicates that the ping is in progress.
Definition: ping.c:75
@ DEBUG_STATE_PING_DONE
Indicates that the ping has completed.
Definition: ping.c:77
@ DEBUG_STATE_IDLE
Indicates that we are idling with WLAN still on.
Definition: ping.c:79
@ DEBUG_STATE_TERMINATING
Indicates that we are disconnecting from the AP.
Definition: ping.c:81
@ DEBUG_STATE_CONNECTED_IDLE
Indicates that we have connected to the AP, but have not started the ping yet.
Definition: ping.c:73
@ DEBUG_STATE_CONNECTING
Indicates we are connecting to the AP.
Definition: ping.c:69
IPv4 configuration structure.
Definition: mmipal.h:88
mmipal_ip_addr_t gateway_addr
Gateway address.
Definition: mmipal.h:96
Ping request arguments data structure.
Definition: mmping.h:77
uint32_t ping_size
Specifies the data packet size in bytes excluding 8 bytes ICMP header.
Definition: mmping.h:91
char ping_src[MMPING_IPADDR_MAXLEN]
String representation of the local IP address.
Definition: mmping.h:79
char ping_target[MMPING_IPADDR_MAXLEN]
String representation of the IP address of the ping target.
Definition: mmping.h:81
uint32_t ping_interval_ms
The time interval between ping requests (in milliseconds)
Definition: mmping.h:83
uint32_t ping_count
This specifies the number of ping requests to send before terminating the session.
Definition: mmping.h:89
Data structure to store ping results.
Definition: mmping.h:105
uint32_t ping_avg_time_ms
The average latency in ms between request sent and response received.
Definition: mmping.h:115
bool ping_is_running
Stores the ping running status.
Definition: mmping.h:119
uint32_t ping_min_time_ms
The minimum latency in ms between request sent and response received.
Definition: mmping.h:113
uint32_t ping_max_time_ms
The maximum latency in ms between request sent and response received.
Definition: mmping.h:117
char ping_receiver[MMPING_IPADDR_MAXLEN]
String representation of the IP address of the ping receiver.
Definition: mmping.h:107
uint32_t ping_recv_count
The number of ping responses received.
Definition: mmping.h:111
uint32_t ping_total_count
Total number of requests sent.
Definition: mmping.h:109