Morse Micro IoT SDK  2.9.7
cli.c
Go to the documentation of this file.
1/*
2 * Copyright 2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
16#include "mmconfig.h"
17#include "mmhal_uart.h"
18#include "mmutils.h"
19
20#include "mmagic.h"
21#include "mm_app_regdb.h"
22
23#if !defined(MBEDTLS_CONFIG_FILE)
24#include "mbedtls/mbedtls_config.h"
25#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28#ifdef MBEDTLS_THREADING_ALT
29#include "threading_alt.h"
30#endif
31
32
33#ifndef APPLICATION_VERSION
34#error Please define APPLICATION_VERSION to an appropriate value.
35#endif
36
38struct mmagic_cli *mmagic_cli_ctx;
39
47void cli_uart_rx_handler(const uint8_t *data, size_t length, void *arg)
48{
49 MM_UNUSED(arg);
50 if (mmagic_cli_ctx != NULL)
51 {
52 mmagic_cli_rx(mmagic_cli_ctx, (const char *)data, length);
53 }
54}
55
56
64void cli_tx_handler(const char *data, size_t length, void *arg)
65{
66 MM_UNUSED(arg);
67 mmhal_uart_tx((const uint8_t *)data, length);
68}
69
79{
80 enum mmhal_uart_deep_sleep_mode mode_uart;
81
82 MM_UNUSED(arg);
83
84 switch (mode)
85 {
88 break;
89
92 /* 1ms delay to flush out the LF that follows a CR - or else this will wake us up */
94 break;
95
96 default:
97 return false;
98 }
99
100 return mmhal_uart_set_deep_sleep_mode(mode_uart);
101}
102
107void app_init(void)
108{
109 char cli_mode[4] = "cli";
110 (void)mmconfig_read_string("cli.mode", cli_mode, sizeof(cli_mode));
111
113
114 /* Initialize mbedTLS threading (required if MBEDTLS_THREADING_ALT is defined) */
115#ifdef MBEDTLS_THREADING_ALT
116 mbedtls_platform_threading_init();
117#endif
118
119 /* If the cli.mode config variable is set to m2m then use MMAGIC in binary machine-to-machine
120 * mode, otherwise use it in interactive CLI mode. */
121 if (strcasecmp(cli_mode, "m2m"))
122 {
123 const struct mmagic_cli_init_args init_args = {
124 .app_version = APPLICATION_VERSION,
125 .tx_cb = cli_tx_handler,
126 .set_deep_sleep_mode_cb = cli_set_deep_sleep_mode_handler,
127 .reg_db = get_regulatory_db(),
128 };
129 mmagic_cli_ctx = mmagic_cli_init(&init_args);
130 printf("CLI interface enabled\n");
131 }
132 else
133 {
134 const struct mmagic_m2m_agent_init_args init_args = {
135 .app_version = APPLICATION_VERSION,
136 .reg_db = get_regulatory_db(),
137 };
138 struct mmagic_m2m_agent *mmagic_m2m_agent = mmagic_m2m_agent_init(&init_args);
139 MM_UNUSED(mmagic_m2m_agent);
140 printf("M2M interface enabled\n");
141 }
142}
struct mmagic_cli * mmagic_cli_ctx
Pointer to context for CLI receive callback.
Definition: cli.c:38
void cli_uart_rx_handler(const uint8_t *data, size_t length, void *arg)
Handler for the UART receive callback.
Definition: cli.c:47
bool cli_set_deep_sleep_mode_handler(enum mmagic_deep_sleep_mode mode, void *arg)
Handler for the CLI set deep sleep mode callback.
Definition: cli.c:78
void cli_tx_handler(const char *data, size_t length, void *arg)
Handler for the CLI transmit callback.
Definition: cli.c:64
void app_init(void)
Main entry point to the application.
Definition: cli.c:107
struct mmagic_m2m_agent * mmagic_m2m_agent_init(const struct mmagic_m2m_agent_init_args *args)
Initialize the M2M agent.
struct mmagic_cli * mmagic_cli_init(const struct mmagic_cli_init_args *args)
Initialize and enable the CLI.
void mmagic_cli_rx(struct mmagic_cli *ctx, const char *buf, size_t len)
This function is used to pass received characters to the MMAGIC CLI.
mmagic_deep_sleep_mode
Deep sleep modes for the agent MCU.
@ MMAGIC_DEEP_SLEEP_MODE_ONE_SHOT
Deep sleep is enabled until activity occurs on the datalink.
@ MMAGIC_DEEP_SLEEP_MODE_DISABLED
Deep sleep is disabled.
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
void mmhal_uart_tx(const uint8_t *data, size_t length)
Transmit data on the UART.
mmhal_uart_deep_sleep_mode
Enumeration of deep sleep modes for the UART HAL.
Definition: mmhal_uart.h:72
void mmhal_uart_init(mmhal_uart_rx_cb_t rx_cb, void *rx_cb_arg)
Initialize the UART HAL and perform any setup necessary.
bool mmhal_uart_set_deep_sleep_mode(enum mmhal_uart_deep_sleep_mode mode)
Set the deep sleep mode for the UART.
@ MMHAL_UART_DEEP_SLEEP_ONE_SHOT
Enable deep sleep until activity occurs on data-link transport.
Definition: mmhal_uart.h:76
@ MMHAL_UART_DEEP_SLEEP_DISABLED
Deep sleep mode is disabled.
Definition: mmhal_uart.h:74
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
#define MM_UNUSED(_x)
Casts the given expression to void to avoid "unused" warnings from the compiler.
Definition: mmutils.h:69
CLI initialization arguments structure.
Definition: mmagic.h:250
char app_version[MMAGIC_SYS_MAX_APP_VERSION_LENGTH+1]
Application version string.
Definition: mmagic.h:252
M2M initialization args.
Definition: mmagic.h:293
char app_version[MMAGIC_SYS_MAX_APP_VERSION_LENGTH+1]
Application version string.
Definition: mmagic.h:295