Morse Micro IoT SDK  2.11.2
socket.c File Reference

Detailed Description

Simple TCP/UDP socket demonstration.

Note
It is assumed that you have followed the steps in the Getting Started guide and are therefore familiar with how to build, flash, and monitor an application using the MM-IoT-SDK framework.

This file demonstrates how to send raw UDP/TCP packets using the mbedtls API. To setup a basic TCP server on a linux host use nc -v -l -p 1337. Add the -u flag for UDP.

Definition in file socket.c.

#include "mmconfig.h"
#include "mmosal.h"
#include "mmhal_app.h"
#include "mmipal.h"
#include "mbedtls/mbedtls_config.h"
#include "mbedtls/net.h"
#include "mm_app_common.h"
#include "mmwlan.h"
#include "mmutils.h"
Include dependency graph for socket.c:

Go to the source code of this file.

Macros

#define DEFAULT_TARGET_ADDR   "192.168.1.1"
 Default IP address of target server. More...
 
#define DEFAULT_PORT   "1337"
 Default port to use for contacting server. More...
 
#define DEFAULT_IP_PROTOCOL   MBEDTLS_NET_PROTO_TCP
 Default protocol to use as an mbedtls int code. More...
 
#define DEFAULT_IP_PROTOCOL_STR   "tcp"
 Default protocol in string form. More...
 
#define DEFAULT_PACKET_COUNT   5
 Default number of packets to send. More...
 
#define DEFAULT_PACKET_INTERVAL_MS   10000
 Default period between transmissions in ms. More...
 
#define MAX_DATA_SIZE_BYTES   (uint32_t)1500
 Arbitrary maximum data size for TCP/UDP packets. More...
 
#define DEFAULT_DATA_SIZE_BYTES   500
 Default data size for TCP/UDP packets. More...
 
#define PACKET_DATA_FORMAT   "Packet index %ld, len=%ld: "
 Format of string to place at start of TCP/UDP packet. More...
 
#define PACKET_SEND_DURATION_MS   200
 Length of time in ms to wait for packet to send. More...
 
#define CONNECT_MAX_ATTEMPTS   5
 Number of times to attempt mbedtls connect. More...
 
#define PWR_MEAS_DELAY_MS(delay_ms)   MM_UNUSED(delay_ms)
 Disable delays which are only useful for power consumption accuracy. More...
 
#define SET_DEBUG_STATE(state)   MM_UNUSED(state)
 Disable GPIO writing if not measuring power consumption. More...
 

Enumerations

enum  debug_state {
  DEBUG_STATE_INIT = 0x00 , DEBUG_STATE_BOOTING_CHIP = 0x01 , DEBUG_STATE_CONNECTING = 0x03 , DEBUG_STATE_CONNECTED = 0x02 ,
  DEBUG_STATE_CONNECTED_IDLE = 0x00 , DEBUG_STATE_PINGING = 0x02 , DEBUG_STATE_PING_DONE = 0x03 , DEBUG_STATE_IDLE = 0x01 ,
  DEBUG_STATE_TERMINATING = 0x00 , DEBUG_STATE_INIT = 0x00 , DEBUG_STATE_BOOTING_CHIP = 0x01 , DEBUG_STATE_CONNECTING = 0x03 ,
  DEBUG_STATE_CONNECTED = 0x02 , DEBUG_STATE_CONNECTED_IDLE = 0x00 , DEBUG_STATE_MBEDTLS_INIT = 0x02 , DEBUG_STATE_MBEDTLS_CONNECTING = 0x03 ,
  DEBUG_STATE_MBEDTLS_SEND = 0x01 , DEBUG_STATE_MBEDTLS_SEND_DONE = 0x00 , DEBUG_STATE_TERMINATING = 0x03 , DEBUG_STATE_INIT = 0x00 ,
  DEBUG_STATE_CONNECTING = 0x01 , DEBUG_STATE_CONNECTED = 0x03 , DEBUG_STATE_PINGING_0 = 0x02 , DEBUG_STATE_PING_0_DONE = 0x00 ,
  DEBUG_STATE_WNM_SLEEP = 0x01 , DEBUG_STATE_EXITING_WNM_SLEEP = 0x03 , DEBUG_STATE_WNM_SLEEP_DONE = 0x02 , DEBUG_STATE_PINGING_1 = 0x00 ,
  DEBUG_STATE_PING_1_DONE = 0x01 , DEBUG_STATE_WNM_SLEEP_POWER_DOWN = 0x03 , DEBUG_STATE_EXITING_WNM_SLEEP_POWER_DOWN = 0x02 , DEBUG_STATE_WNM_SLEEP_POWER_DOWN_DONE = 0x00 ,
  DEBUG_STATE_TERMINATING = 0x01
}
 Enumeration of debug states that will be reflected on debug pins. More...
 

Functions

int parse_protocol_str (const char *protocol, uint8_t len)
 Parse the protocol from a string to an MBEDTLS protocol integer. More...
 
void generate_packet_data (uint32_t packet_idx, uint32_t len, unsigned char *buf)
 Handle a report at the end of an iperf transfer. More...
 
void app_init (void)
 Main entry point to the application. More...
 

Variables

uint8_t buf [MAX_DATA_SIZE_BYTES] = {}
 Buffer holding UDP/TCP packet data. More...
 

Macro Definition Documentation

◆ CONNECT_MAX_ATTEMPTS

#define CONNECT_MAX_ATTEMPTS   5

Number of times to attempt mbedtls connect.

Definition at line 60 of file socket.c.

◆ DEFAULT_DATA_SIZE_BYTES

#define DEFAULT_DATA_SIZE_BYTES   500

Default data size for TCP/UDP packets.

Must be <= MAX_DATA_SIZE_BYTES.

Definition at line 52 of file socket.c.

◆ DEFAULT_IP_PROTOCOL

#define DEFAULT_IP_PROTOCOL   MBEDTLS_NET_PROTO_TCP

Default protocol to use as an mbedtls int code.

Definition at line 39 of file socket.c.

◆ DEFAULT_IP_PROTOCOL_STR

#define DEFAULT_IP_PROTOCOL_STR   "tcp"

Default protocol in string form.

Options: {"tcp", "udp"}. Must coincide with DEFAULT_IP_PROTOCOL

Definition at line 42 of file socket.c.

◆ DEFAULT_PACKET_COUNT

#define DEFAULT_PACKET_COUNT   5

Default number of packets to send.


Definition at line 45 of file socket.c.

◆ DEFAULT_PACKET_INTERVAL_MS

#define DEFAULT_PACKET_INTERVAL_MS   10000

Default period between transmissions in ms.

Must be greater than PACKET_SEND_DURATION_MS.

Definition at line 47 of file socket.c.

◆ DEFAULT_PORT

#define DEFAULT_PORT   "1337"

Default port to use for contacting server.

Definition at line 37 of file socket.c.

◆ DEFAULT_TARGET_ADDR

#define DEFAULT_TARGET_ADDR   "192.168.1.1"

Default IP address of target server.

Definition at line 35 of file socket.c.

◆ MAX_DATA_SIZE_BYTES

#define MAX_DATA_SIZE_BYTES   (uint32_t)1500

Arbitrary maximum data size for TCP/UDP packets.

Determines the size of the buffer. Must be <= INT32_MAX as this is the return type of IP send functions.

Definition at line 50 of file socket.c.

◆ PACKET_DATA_FORMAT

#define PACKET_DATA_FORMAT   "Packet index %ld, len=%ld: "

Format of string to place at start of TCP/UDP packet.

Must be shorter than MAX_DATA_SIZE_BYTES

Definition at line 55 of file socket.c.

◆ PACKET_SEND_DURATION_MS

#define PACKET_SEND_DURATION_MS   200

Length of time in ms to wait for packet to send.

Only used if ENABLE_POWER_MEAS=1. Should be increased for lower link bandwidths or larger packet sizes.

Definition at line 58 of file socket.c.

◆ PWR_MEAS_DELAY_MS

#define PWR_MEAS_DELAY_MS (   delay_ms)    MM_UNUSED(delay_ms)

Disable delays which are only useful for power consumption accuracy.

Definition at line 70 of file socket.c.

◆ SET_DEBUG_STATE

#define SET_DEBUG_STATE (   state)    MM_UNUSED(state)

Disable GPIO writing if not measuring power consumption.

Definition at line 72 of file socket.c.

Enumeration Type Documentation

◆ debug_state

Enumeration of debug states that will be reflected on debug pins.

Note that due to limited availability of pins, the values are mapped to 2-bit codes and so are not unique. The code sequence has been chosen to be gray code like in that only one bit changes at a time.

Enumerator
DEBUG_STATE_INIT 

Initial state at startup.

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_CONNECTING 

Indicates we are connecting to the AP.

DEBUG_STATE_CONNECTED 

Indicates we are connected to the AP.

DEBUG_STATE_CONNECTED_IDLE 

Indicates that we have connected to the AP, but have not started the ping yet.

DEBUG_STATE_PINGING 

Indicates that the ping is in progress.

DEBUG_STATE_PING_DONE 

Indicates that the ping has completed.

DEBUG_STATE_IDLE 

Indicates that we are idling with WLAN still on.

DEBUG_STATE_TERMINATING 

Indicates that we are disconnecting from the AP.

DEBUG_STATE_INIT 

Initial state at startup.

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_CONNECTING 

Indicates we are connecting to the AP.

DEBUG_STATE_CONNECTED 

Indicates we are connected to the AP.

DEBUG_STATE_CONNECTED_IDLE 

Indicates that we have connected to the AP, but have not connected to the TCP server.

DEBUG_STATE_MBEDTLS_INIT 

Indicates we are initializing the mbedtls client.

DEBUG_STATE_MBEDTLS_CONNECTING 

Indicates we are connecting to the TCP server on the AP.

DEBUG_STATE_MBEDTLS_SEND 

Indicates we are sending a TCP packet 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_TERMINATING 

Indicates that we are disconnecting from the AP.

DEBUG_STATE_INIT 

Initial state at startup.

DEBUG_STATE_CONNECTING 

Indicates we are connecting to the AP.

DEBUG_STATE_CONNECTED 

Indicates we are connected to the AP.

DEBUG_STATE_PINGING_0 

Indicates that the ping is in progress.

DEBUG_STATE_PING_0_DONE 

Indicates that the ping has completed.

DEBUG_STATE_WNM_SLEEP 

Indicates that WNM sleep is in progress.

DEBUG_STATE_EXITING_WNM_SLEEP 

Indicates that we are exiting WNM sleep.

DEBUG_STATE_WNM_SLEEP_DONE 

Indicates that WNM sleep has completed.

DEBUG_STATE_PINGING_1 

Indicates that the ping is in progress.

DEBUG_STATE_PING_1_DONE 

Indicates that the ping has completed.

DEBUG_STATE_WNM_SLEEP_POWER_DOWN 

Indicates that WNM sleep is in progress with chip powered down.

DEBUG_STATE_EXITING_WNM_SLEEP_POWER_DOWN 

Indicates that we are exiting WNM sleep with chip powered down.

DEBUG_STATE_WNM_SLEEP_POWER_DOWN_DONE 

Indicates that WNM sleep with chip powered down has completed.

DEBUG_STATE_TERMINATING 

Indicates that we are disconnecting from the AP.

Definition at line 83 of file socket.c.

Function Documentation

◆ app_init()

void app_init ( void  )

Main entry point to the application.

This will be invoked in a thread once operating system and hardware initialization has completed. It may return, but it does not have to.

Definition at line 154 of file socket.c.

◆ generate_packet_data()

void generate_packet_data ( uint32_t  packet_idx,
uint32_t  len,
unsigned char *  buf 
)

Handle a report at the end of an iperf transfer.

Parameters
packet_idxIndex of the packet which is included in the packet data.
lenLength of the buffer.
bufBuffer for data to be written into.

Definition at line 141 of file socket.c.

◆ parse_protocol_str()

int parse_protocol_str ( const char *  protocol,
uint8_t  len 
)

Parse the protocol from a string to an MBEDTLS protocol integer.

Parameters
protocolThe protocol string to parse.
lenThe length of the string to parse.
Returns
The mbedtls protocol int code on success, else -1 on error.

Definition at line 116 of file socket.c.

Variable Documentation

◆ buf

uint8_t buf[MAX_DATA_SIZE_BYTES] = {}

Buffer holding UDP/TCP packet data.

Definition at line 76 of file socket.c.