164#include "mmhal_app.h"
165#include "mmhal_core.h"
168#include "mmagic_controller.h"
173#define SSID MorseMicro
177#ifndef SAE_PASSPHRASE
180#define SAE_PASSPHRASE 12345678
184#define _STRINGIFY(x) #x
186#define STRINGIFY(x) _STRINGIFY(x)
189#define LINK_STATE_TIMEOUT_MS 20000
192#define TCP_ECHCO_SERVER_PORT 5000
203 struct mmosal_semb *started = (
struct mmosal_semb *)arg;
204 printf(
"Agent start notification received\n");
221 printf(
"\n\n#### Example WLAN Connect using MMAGIC Controller ####\n\n");
224 printf(
"Attempting to connect to %s with passphrase %s\n",
STRINGIFY(
SSID),
226 printf(
"This may take some time (~10 seconds)\n");
228 if (status != MMAGIC_STATUS_OK)
230 printf(
"Error %d setting the wlan ssid\n", status);
235 if (status != MMAGIC_STATUS_OK)
237 printf(
"Error %d setting the wlan password\n", status);
245 if (status != MMAGIC_STATUS_OK)
247 printf(
"Error %d after sending the connect command\n", status);
256 if ((
status == MMAGIC_STATUS_OK) &&
260 printf(
"Link is up %s. Time: %lu ms, ",
286 printf(
"\n\n#### Example check WLAN connection using MMAGIC Controller ####\n\n");
289 printf(
"Checking SSID and password match expected values\n");
293 if (status != MMAGIC_STATUS_OK)
295 printf(
"Error %d getting the wlan ssid\n", status);
300 printf(
"SSID mismatch\n");
307 if (status != MMAGIC_STATUS_OK)
309 printf(
"Error %d getting the wlan password\n", status);
315 printf(
"Password mismatch\n");
319 printf(
"Checking STA connection status\n");
322 if (status != MMAGIC_STATUS_OK)
324 printf(
"Error %d getting wlan sta status\n", status);
327 if (rsp_args.
sta_status != MMAGIC_STA_STATE_CONNECTED)
329 printf(
"STA not connected\n");
333 printf(
"Checking link status\n");
335 for (
int attempts = 2; attempts > 0; --attempts)
338 if ((
status == MMAGIC_STATUS_OK) &&
341 printf(
"Link is up %s. Time: %lu ms, ",
353 printf(
"Link Down\n");
381 while (offset < args->vendor_ies.len)
389 printf(
"Beacon IEs malformed!\n");
400 printf(
"Beacon IEs malformed!\n");
404 printf(
" IE type 0x%02x, IE len 0x%02x, Contents: ", ie_type, ie_len);
405 for (ii = 0; ii < ie_len; ii++)
424 printf(
"\n\n#### Example Beacon Monitor using MMAGIC Controller ####\n\n");
434 if (status != MMAGIC_STATUS_OK)
436 printf(
"Error %d enabling beacon monitor\n", status);
439 printf(
"Enabled beacon monitor for OUI %02x:%02x:%02x\n",
454 struct mmosal_semb *rx_ready_semb)
456 printf(
"\n\n#### Example TCP Client using MMAGIC Controller ####\n\n");
460 .
url = {.
data =
"google.com", .len = strlen(
"google.com")},
462 .protocol = MMAGIC_SOCKET_PROTO_TCP,
466 if (status != MMAGIC_STATUS_OK)
468 printf(
"Error %d establishing TCP connection\n", status);
471 uint8_t tcp_stream_id = tcp_rsp_args.
stream_id;
472 printf(
"Opened TCP socket on stream_id %u\n", tcp_stream_id);
479 bool rx_ready_event_enabled =
false;
480 if (status == MMAGIC_STATUS_OK)
482 rx_ready_event_enabled =
true;
484 else if (status == MMAGIC_STATUS_NOT_SUPPORTED)
486 printf(
"RX ready event not supported.\n");
490 printf(
"Error %d enabling rx ready event\n", status);
495 .buffer = {.data =
"GET /\n\n", .len = strlen(
"GET /\n\n")}
498 if (status != MMAGIC_STATUS_OK)
500 printf(
"Error %d whilst sending data\n", status);
503 printf(
"Successfully sent GET request\n");
505 if (rx_ready_event_enabled)
507 printf(
"Waiting for RX ready event...\n");
518 if (status != MMAGIC_STATUS_OK)
520 printf(
"Error %d whilst receiving data\n", status);
524 printf(
"Received: %.12s, Length %u\n", tcp_recv_rsp_args.
buffer.
data,
530 if (status != MMAGIC_STATUS_OK)
532 printf(
"Error %d whilst closing tcp socket\n", status);
535 printf(
"Closed TCP Socket\n");
559 struct mmagic_controller *controller = thread_args->
controller;
560 uint8_t stream_id = thread_args->
stream_id;
562 printf(
"Accepted a TCP connection on stream_id %u\n", stream_id);
578 if (status == MMAGIC_STATUS_TIMEOUT)
582 else if (status == MMAGIC_STATUS_CLOSED)
584 printf(
"Connection closed by the other side!\n");
587 else if (status != MMAGIC_STATUS_OK)
589 printf(
"Error %d whilst receiving data\n", status);
592 printf(
"Received: %d bytes, echoing.\n", tcp_recv_rsp_args.
buffer.
len);
598 if (status != MMAGIC_STATUS_OK)
600 printf(
"Error %d whilst sending data\n", status);
610 if (status != MMAGIC_STATUS_OK)
612 printf(
"Error %u whilst trying to close the TCP socket on stream_id %u\n",
615 printf(
"Closed TCP socket\n");
629 printf(
"\n\n#### Example TCP Echo Server using MMAGIC Controller ####\n\n");
636 if (status != MMAGIC_STATUS_OK)
638 printf(
"Error %u whilst opening the tcp socket\n", status);
641 uint8_t tcp_socket_stream_id = tcp_bind_rsp_args.
stream_id;
642 printf(
"Opened listening socket (Port %u) on stream_id %u\n",
643 tcp_bind_cmd_args.
port, tcp_socket_stream_id);
652 &tcp_accept_cmd_args,
653 &tcp_accept_rsp_args);
654 if (status != MMAGIC_STATUS_OK)
656 printf(
"Error %u whilst trying to accept a TCP connection\n", status);
672 if (status != MMAGIC_STATUS_OK)
674 printf(
"Error %u whilst trying to close the listening socket on stream_id %u\n",
675 status, tcp_socket_stream_id);
679 printf(
"Closed listening socket\n");
695 printf(
"Socket RX ready event\n");
696 struct mmosal_semb *rx_ready_semb = (
struct mmosal_semb *)arg;
717 AGENT_ACTION_TIMEOUT_MS = 1000,
720 bool agent_already_running =
false;
722 printf(
"M2M Controller enabled. Awaiting Agent start\n");
728 printf(
"No agent start notification, agent may already be running.\n");
729 printf(
"Attempting sync to recover connection.\n");
731 if (status == MMAGIC_STATUS_OK)
733 agent_already_running =
true;
737 printf(
"WLAN connection reattached\n");
738 goto agent_connected;
743 printf(
"Sync failed with status %d, attempting LLC agent reset.\n", status);
750 printf(
"LLC reset failed with status %d. Please hard reset the agent.\n", status);
756 if (status != MMAGIC_STATUS_OK)
758 printf(
"Get version failed with status %d\n", status);
762 printf(
"-----------------------------------\n");
768 printf(
"-----------------------------------\n");
772 printf(
"Failed to connect\n");
775 printf(
"WLAN connection established\n");
787 if ((status == MMAGIC_STATUS_SOCKET_LISTEN_FAILED) && agent_already_running)
791 printf(
"TCP echo server ended with status %d\n", status);
801 printf(
"\n\nM2M Controller Example (Built " __DATE__
" " __TIME__
")\n\n");
enum MM_PACKED mmagic_status
Enumeration of return status codes.
struct mmagic_controller * mmagic_controller_init(const struct mmagic_controller_init_args *args)
Initialize the Controller.
#define MMAGIC_CONTROLLER_ARGS_INIT
Initializer for mmagic_controller_init_args.
enum mmagic_status mmagic_controller_agent_sync(struct mmagic_controller *controller, uint32_t timeout_ms)
Sends a sync request to the agent and waits for a sync response.
enum mmagic_status mmagic_controller_request_agent_reset(struct mmagic_controller *controller)
Sends a reset request to the agent.
static enum mmagic_status mmagic_controller_ip_status(struct mmagic_controller *controller, struct mmagic_core_ip_status_rsp_args *rsp_args)
Gets the status of the IP stack.
static enum mmagic_status mmagic_controller_socket_connect(struct mmagic_controller *controller, struct mmagic_core_socket_connect_cmd_args *cmd_args, struct mmagic_core_socket_connect_rsp_args *rsp_args)
Opens a client socket and returns its stream ID.
static enum mmagic_status mmagic_controller_socket_recv(struct mmagic_controller *controller, struct mmagic_core_socket_recv_cmd_args *cmd_args, struct mmagic_core_socket_recv_rsp_args *rsp_args)
Reads from a socket.
void mmagic_controller_register_socket_rx_ready_handler(struct mmagic_controller *controller, mmagic_socket_rx_ready_event_handler_t handler, void *arg)
Register a handler for the socket-rx_ready event.
static enum mmagic_status mmagic_controller_socket_close(struct mmagic_controller *controller, struct mmagic_core_socket_close_cmd_args *cmd_args)
Closes and frees the socket.
static enum mmagic_status mmagic_controller_socket_set_rx_ready_evt_enabled(struct mmagic_controller *controller, struct mmagic_core_socket_set_rx_ready_evt_enabled_cmd_args *cmd_args)
Sets whether the RX ready event should be enabled for the given socket.
static enum mmagic_status mmagic_controller_socket_accept(struct mmagic_controller *controller, struct mmagic_core_socket_accept_cmd_args *cmd_args, struct mmagic_core_socket_accept_rsp_args *rsp_args)
Waits for an incoming socket connection and returns a new stream ID.
static enum mmagic_status mmagic_controller_socket_send(struct mmagic_controller *controller, struct mmagic_core_socket_send_cmd_args *cmd_args)
Writes to a socket.
static enum mmagic_status mmagic_controller_socket_bind(struct mmagic_controller *controller, struct mmagic_core_socket_bind_cmd_args *cmd_args, struct mmagic_core_socket_bind_rsp_args *rsp_args)
Opens a server socket and returns its stream ID.
static enum mmagic_status mmagic_controller_sys_get_version(struct mmagic_controller *controller, struct mmagic_core_sys_get_version_rsp_args *rsp_args)
Gets the device firmware and hardware versions.
static enum mmagic_status mmagic_controller_set_wlan_ssid(struct mmagic_controller *controller, const char *var)
Sets ssid setting for module wlan.
static enum mmagic_status mmagic_controller_set_wlan_password(struct mmagic_controller *controller, const char *var)
Sets password setting for module wlan.
static enum mmagic_status mmagic_controller_get_wlan_ssid(struct mmagic_controller *controller, struct string32 *var)
Gets ssid setting for module wlan.
static enum mmagic_status mmagic_controller_get_wlan_password(struct mmagic_controller *controller, struct string100 *var)
Gets password setting for module wlan.
static enum mmagic_status mmagic_controller_wlan_beacon_monitor_enable(struct mmagic_controller *controller, struct mmagic_core_wlan_beacon_monitor_enable_cmd_args *cmd_args)
Enable beacon monitoring with the given filter settings.
static enum mmagic_status mmagic_controller_wlan_get_sta_status(struct mmagic_controller *controller, struct mmagic_core_wlan_get_sta_status_rsp_args *rsp_args)
Retrieves the STA status of the WLAN interface.
void mmagic_controller_register_wlan_beacon_rx_handler(struct mmagic_controller *controller, mmagic_wlan_beacon_rx_event_handler_t handler, void *arg)
Register a handler for the wlan-beacon_rx event.
static enum mmagic_status mmagic_controller_wlan_connect(struct mmagic_controller *controller, struct mmagic_core_wlan_connect_cmd_args *cmd_args)
Brings up the WLAN interface and connects to the AP with configured parameters.
void mmhal_set_deep_sleep_veto(uint8_t veto_id)
Sets a deep sleep veto that will prevent the device from entering deep sleep.
uint32_t mmhal_random_u32(uint32_t min, uint32_t max)
Generate a random 32 bit integer within the given range.
@ MMHAL_VETO_ID_APP_MIN
Start of deep sleep veto ID range that is available for application use.
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms)
Wait for a counting semaphore.
struct mmosal_semb * mmosal_semb_create(const char *name)
Create a new binary semaphore.
bool mmosal_semb_give(struct mmosal_semb *semb)
Give a binary semaphore.
struct mmosal_task * mmosal_task_create(mmosal_task_fn_t task_fn, void *argument, enum mmosal_task_priority priority, unsigned stack_size_u32, const char *name)
Create a new task.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
@ MMOSAL_TASK_PRI_LOW
Low priority.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
static bool mmosal_time_lt(uint32_t a, uint32_t b)
Check if time a is less than time b, taking into account wrapping.
#define MM_UNUSED(_x)
Casts the given expression to void to avoid "unused" warnings from the compiler.
static void tcp_echo_server_task(void *args)
This task handles a single incoming TCP connection.
static void run_examples_task(void *args)
Runs the examples.
static struct mmosal_semb * agent_started_semb
Binary semaphore used to indicate when an agent start notification has been received.
static bool wlan_connect(struct mmagic_controller *controller)
This function illustrates how to establish a wlan connection using the mmagic_controller interface.
#define TCP_ECHCO_SERVER_PORT
Port the the TCP echo server will bind to.
void beacon_monitor_rx_handler(const struct mmagic_wlan_beacon_rx_event_args *args, void *arg)
Handler for the beacon monitor event handler callback.
static enum mmagic_status tcp_echo_server_start(struct mmagic_controller *controller, uint16_t port)
This function illustrates how to start a TCP server and listen for a connection.
#define STRINGIFY(x)
Convert the content of the given macro to a string.
#define SAE_PASSPHRASE
Passphrase of the AP (ignored if security type is not SAE).
static bool is_wlan_connected(struct mmagic_controller *controller)
This function illustrates how to check if the agent already has an active connection.
#define LINK_STATE_TIMEOUT_MS
Duration to wait for the link to be established after WLAN reports connected.
#define SSID
SSID of the AP to connect to.
static void socket_rx_ready_event_handler(const struct mmagic_socket_rx_ready_event_args *event_args, void *arg)
Handler for data receive ready event callback.
void app_init(void)
Main entry point to the application.
void agent_start_handler(struct mmagic_controller *controller, void *arg)
Handler for the "Agent start" callback.
static void tcp_client_example(struct mmagic_controller *controller, struct mmosal_semb *rx_ready_semb)
This function illustrates how to open a tcp client, send and receive some data and close the connecti...
static void beacon_monitor_example_start(struct mmagic_controller *controller)
This function illustrates how to subscribe to and receive custom vendor IEs from beacons.
Arguments for beacon monitor task.
uint8_t stream_id
The stream ID.
struct mmagic_controller * controller
The controller reference.
Initialization structure for mmagic_controller.
mmagic_controller_agent_start_cb_t agent_start_cb
Callback function to executed any time a event that the agent has started is received.
void * agent_start_arg
User argument that will be passed when the agent_start_cb is executed.
Response arguments structure for ip_status.
struct struct_ip_status status
Reference to the struct to return the IP status retrieved from the network stack.
Command arguments structure for socket_accept.
uint8_t stream_id
Stream ID of the bound socket.
Response arguments structure for socket_accept.
uint8_t stream_id
Stream ID of the new incoming connection.
Command arguments structure for socket_bind.
uint16_t port
Port to listen on.
Response arguments structure for socket_bind.
uint8_t stream_id
Stream ID of the opened socket.
Command arguments structure for socket_close.
uint8_t stream_id
Stream ID of the socket to close.
Command arguments structure for socket_connect.
struct string254 url
URL of the server to connect to.
Response arguments structure for socket_connect.
uint8_t stream_id
Stream ID of the opened socket.
Command arguments structure for socket_recv.
uint8_t stream_id
Stream ID of the socket to receive from.
Response arguments structure for socket_recv.
struct raw1536 buffer
Buffer of read data.
Command arguments structure for socket_send.
struct raw1536 buffer
Buffer to send.
uint8_t stream_id
Stream ID of the socket to send on.
Command arguments structure for socket_set_rx_ready_evt_enabled.
uint8_t stream_id
Stream ID of the socket to close.
Response arguments structure for sys_get_version.
struct struct_version_info results
Reference to structure to store the version information.
Command arguments structure for wlan_beacon_monitor_enable.
struct struct_oui_list oui_filter
OUIs to monitor.
Command arguments structure for wlan_connect.
uint32_t timeout
Duration in milliseconds to wait for connection establish, if connection does not get established an ...
Response arguments structure for wlan_get_sta_status.
enum mmagic_sta_state sta_status
The current STA status.
Event arguments structure for socket_rx_ready.
Event arguments structure for wlan_beacon_rx.
struct raw1536 vendor_ies
Raw octet string of Vendor Information Elements contained in the beacon.
uint8_t data[1536]
The actual data buffer.
uint16_t len
Length of data.
String type with maximum length of 100 (excluding null terminator).
uint8_t len
Length of string contents (excluding null terminator).
char data[100+1]
The string contents.
char data[254+1]
The string contents.
String type with maximum length of 32 (excluding null terminator).
uint8_t len
Length of string contents (excluding null terminator).
char data[32+1]
The string contents.
char addr[48]
Array containing the IP string.
struct struct_ip_addr netmask
Current IP network mask.
struct struct_ip_addr ip_addr
Current IP address.
bool dhcp_enabled
Whether or not dhcp is enabled.
enum mmagic_ip_link_state link_state
Current link state.
struct struct_ip_addr gateway
Current IP gateway.
struct struct_oui ouis[5]
The OUI data.
uint8_t count
The number of OUIs in the list.
uint8_t oui[3]
The 3 octet OUI.
struct string32 application_version
Version of the application software.
struct string32 morse_hardware_version
Version of the Morse hardware.
struct string32 user_hardware_version
Version of the user hardware.
struct string32 morse_firmware_version
Version of the Morse firmware.
struct string32 morselib_version
Version of the Morse IoT SDK library.
Arguments for TCP echo server task.
uint8_t stream_id
The stream ID.
struct mmagic_controller * controller
The controller reference.