24#if !defined(MBEDTLS_CONFIG_FILE)
25#include "mbedtls/mbedtls_config.h"
27#include MBEDTLS_CONFIG_FILE
29#include "mbedtls/net.h"
35#define DEFAULT_TARGET_ADDR "192.168.1.1"
37#define DEFAULT_PORT "1337"
39#define DEFAULT_IP_PROTOCOL MBEDTLS_NET_PROTO_TCP
42#define DEFAULT_IP_PROTOCOL_STR "tcp"
45#define DEFAULT_PACKET_COUNT 5
47#define DEFAULT_PACKET_INTERVAL_MS 10000
50#define MAX_DATA_SIZE_BYTES (uint32_t)1500
52#define DEFAULT_DATA_SIZE_BYTES 500
55#define PACKET_DATA_FORMAT "Packet index %ld, len=%ld: "
58#define PACKET_SEND_DURATION_MS 200
60#define CONNECT_MAX_ATTEMPTS 5
62#if defined(ENABLE_PWR_MEAS) && ENABLE_PWR_MEAS
64#define PWR_MEAS_DELAY_MS(delay_ms) mmosal_task_sleep(delay_ms)
66#define SET_DEBUG_STATE(state) mmhal_set_debug_pins(MMHAL_ALL_DEBUG_PINS, state);
70#define PWR_MEAS_DELAY_MS(delay_ms) MM_UNUSED(delay_ms)
72#define SET_DEBUG_STATE(state) MM_UNUSED(state)
118 if (strncmp(
"tcp", protocol, len) == 0)
120 return MBEDTLS_NET_PROTO_TCP;
122 else if (strncmp(
"udp", protocol, len) == 0)
124#if defined(ipconfigUSE_IPv4) || defined(ipconfigUSE_IPv6)
125 printf(
"WARNING: The FreeRTOS+TCP socket implementation for Mbed TLS dos not currently "
129 return MBEDTLS_NET_PROTO_UDP;
146 buf[i] =
'0' + (i % 10);
158 printf(
"\n\nSockets Example (Built " __DATE__
" " __TIME__
")\n\n");
180 mbedtls_net_context server_fd;
181 mbedtls_net_init(&server_fd);
190 printf(
"Selected data size (socket.data_size_bytes) too large. Using the maximum of %ld "
204 printf(
"Failed to retrieve IP config\n");
213 char protocol_str[4];
219 printf(
"Failed to parse socket.protocol config. Defaulting to %s\n", protocol_str);
224 printf(
"Connecting to %s server at %s:%s...\n", protocol_str, target_ip, target_port);
228 while ((ret = mbedtls_net_connect(&server_fd, target_ip, target_port, ip_protocol)) &&
231 printf(
"Failed to connect to server. Status: %d\n", ret);
235 printf(
"Successfully connected to server.\n");
241 mbedtls_net_send(&server_fd, NULL, 0);
249 for (uint32_t packet_idx = 0; packet_idx < packet_count; packet_idx++)
253 printf(
"Sending packet number %ld... ", packet_idx);
254 if ((ret = mbedtls_net_send(&server_fd,
buf, data_size_bytes)) < 0)
256 printf(
"Packet failed to send. Return code: %d\n", ret);
258 else if ((uint32_t)ret < data_size_bytes)
260 printf(
"Packet not fully sent: %d of %ld\n", ret, data_size_bytes);
264 printf(
"Packet successfully sent\n");
266#if defined(ENABLE_PWR_MEAS) && ENABLE_PWR_MEAS
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.
mmipal_status
Enumeration of status codes returned by MMIPAL functions.
enum mmipal_status mmipal_get_ip_config(struct mmipal_ip_config *config)
Get the IP configurations.
@ MMIPAL_SUCCESS
Completed successfully.
@ MMIPAL_DISABLED
Disabled.
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
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.
debug_state
Enumeration of debug states that will be reflected on debug pins.
#define DEFAULT_DATA_SIZE_BYTES
Default data size for TCP/UDP packets.
#define DEFAULT_PACKET_COUNT
Default number of packets to send.
#define SET_DEBUG_STATE(state)
Disable GPIO writing if not measuring power consumption.
#define DEFAULT_PORT
Default port to use for contacting server.
#define MAX_DATA_SIZE_BYTES
Arbitrary maximum data size for TCP/UDP packets.
#define PACKET_DATA_FORMAT
Format of string to place at start of TCP/UDP packet.
int parse_protocol_str(const char *protocol, uint8_t len)
Parse the protocol from a string to an MBEDTLS protocol integer.
#define PACKET_SEND_DURATION_MS
Length of time in ms to wait for packet to send.
#define DEFAULT_IP_PROTOCOL_STR
Default protocol in string form.
#define PWR_MEAS_DELAY_MS(delay_ms)
Disable delays which are only useful for power consumption accuracy.
#define CONNECT_MAX_ATTEMPTS
Number of times to attempt mbedtls connect.
#define DEFAULT_PACKET_INTERVAL_MS
Default period between transmissions in ms.
#define DEFAULT_IP_PROTOCOL
Default protocol to use as an mbedtls int code.
void app_init(void)
Main entry point to the application.
uint8_t buf[MAX_DATA_SIZE_BYTES]
Buffer holding UDP/TCP packet data.
#define DEFAULT_TARGET_ADDR
Default IP address of target server.
@ DEBUG_STATE_INIT
Initial state at startup.
@ DEBUG_STATE_MBEDTLS_CONNECTING
Indicates we are connecting to the TCP server on the AP.
@ DEBUG_STATE_BOOTING_CHIP
Indicates that we are booting the MM chip (note that this will also include the host MCU startup time...
@ DEBUG_STATE_MBEDTLS_SEND
Indicates we are sending a TCP packet to the AP.
@ DEBUG_STATE_CONNECTED
Indicates we are connected to the AP.
@ DEBUG_STATE_MBEDTLS_SEND_DONE
Indicates we have sent a TCP packet to the AP and are waiting for communications to cool down.
@ DEBUG_STATE_MBEDTLS_INIT
Indicates we are initializing the mbedtls client.
@ DEBUG_STATE_TERMINATING
Indicates that we are disconnecting from the AP.
@ DEBUG_STATE_CONNECTED_IDLE
Indicates that we have connected to the AP, but have not connected to the TCP server.
@ DEBUG_STATE_CONNECTING
Indicates we are connecting to the AP.
void generate_packet_data(uint32_t packet_idx, uint32_t len, unsigned char *buf)
Handle a report at the end of an iperf transfer.
IPv4 configuration structure.
enum mmipal_addr_mode mode
IP address allocation mode.
mmipal_ip_addr_t gateway_addr
Gateway address.