Morse Micro IoT SDK  2.10.4
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 "mmregdb.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#ifndef APPLICATION_VERSION
33#error Please define APPLICATION_VERSION to an appropriate value.
34#endif
35
37struct mmagic_cli *mmagic_cli_ctx;
38
46void cli_uart_rx_handler(const uint8_t *data, size_t length, void *arg)
47{
48 MM_UNUSED(arg);
49 if (mmagic_cli_ctx != NULL)
50 {
51 mmagic_cli_rx(mmagic_cli_ctx, (const char *)data, length);
52 }
53}
54
62void cli_tx_handler(const char *data, size_t length, void *arg)
63{
64 MM_UNUSED(arg);
65 mmhal_uart_tx((const uint8_t *)data, length);
66}
67
77{
78 enum mmhal_uart_deep_sleep_mode mode_uart;
79
80 MM_UNUSED(arg);
81
82 switch (mode)
83 {
86 break;
87
90 /* 1ms delay to flush out the LF that follows a CR - or else this will wake us up */
92 break;
93
94 default:
95 return false;
96 }
97
98 return mmhal_uart_set_deep_sleep_mode(mode_uart);
99}
100
105void app_init(void)
106{
107 char cli_mode[4] = "cli";
108 (void)mmconfig_read_string("cli.mode", cli_mode, sizeof(cli_mode));
109
111
112 /* Initialize mbedTLS threading (required if MBEDTLS_THREADING_ALT is defined) */
113#ifdef MBEDTLS_THREADING_ALT
114 mbedtls_platform_threading_init();
115#endif
116
117 /* If the cli.mode config variable is set to m2m then use MMAGIC in binary machine-to-machine
118 * mode, otherwise use it in interactive CLI mode. */
119 if (strcasecmp(cli_mode, "m2m"))
120 {
121 const struct mmagic_cli_init_args init_args = {
122 .app_version = APPLICATION_VERSION,
123 .tx_cb = cli_tx_handler,
124 .set_deep_sleep_mode_cb = cli_set_deep_sleep_mode_handler,
125 .reg_db = get_regulatory_db(),
126 };
127 mmagic_cli_ctx = mmagic_cli_init(&init_args);
128 printf("CLI interface enabled\n");
129 }
130 else
131 {
132 const struct mmagic_m2m_agent_init_args init_args = {
133 .app_version = APPLICATION_VERSION,
134 .reg_db = get_regulatory_db(),
135 };
136 struct mmagic_m2m_agent *mmagic_m2m_agent = mmagic_m2m_agent_init(&init_args);
137 MM_UNUSED(mmagic_m2m_agent);
138 printf("M2M interface enabled\n");
139 }
140}
struct mmagic_cli * mmagic_cli_ctx
Pointer to context for CLI receive callback.
Definition: cli.c:37
void cli_uart_rx_handler(const uint8_t *data, size_t length, void *arg)
Handler for the UART receive callback.
Definition: cli.c:46
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:76
void cli_tx_handler(const char *data, size_t length, void *arg)
Handler for the CLI transmit callback.
Definition: cli.c:62
void app_init(void)
Main entry point to the application.
Definition: cli.c:105
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:70
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:74
@ MMHAL_UART_DEEP_SLEEP_DISABLED
Deep sleep mode is disabled.
Definition: mmhal_uart.h:72
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:70
CLI initialization arguments structure.
Definition: mmagic.h:251
char app_version[MMAGIC_SYS_MAX_APP_VERSION_LENGTH+1]
Application version string.
Definition: mmagic.h:253
M2M initialization args.
Definition: mmagic.h:294
char app_version[MMAGIC_SYS_MAX_APP_VERSION_LENGTH+1]
Application version string.
Definition: mmagic.h:296