Skip to content

Commit

Permalink
Cross-demo code sync; post-lint changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Smith committed Jul 24, 2024
1 parent e245fa7 commit ff0524e
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 93 deletions.
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ FROM ubuntu:20.04 AS builder
ARG USERNAME=mvisor
ARG UID=1000
ARG GID=1000
RUN groupadd -g $GID -o $USERNAME
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $USERNAME
RUN groupadd -g ${GID} -o ${USERNAME}
RUN useradd -m -u ${UID} -g ${GID} -o -s /bin/bash ${USERNAME}

# Dependencies for elf generation
RUN apt-get update -yqq && apt-get install -yqq apt-utils
RUN apt-get update -yqq && apt-get install -yqq apt-utils && apt-get clean
RUN DEBIAN_FRONTEND=noninteractive apt-get install -o APT::Immediate-Configure=0 -yqq \
cmake gcc-arm-none-eabi jq curl ca-certificates gnupg
cmake gcc-arm-none-eabi jq curl ca-certificates gnupg \
&& apt-get clean

# Twilio CLI for bundle generation via npm
# as a binary debian package is not yet available for Apple Silicon
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update -yqq && DEBIAN_FRONTEND=noninteractive apt-get install -yqq nodejs
RUN apt-get update -yqq && DEBIAN_FRONTEND=noninteractive apt-get install -yqq nodejs && apt-get clean
RUN npm install -g twilio-cli

# Install dependencies for this repo ONLY
RUN apt-get install -yqq uuid-runtime gdb-multiarch
RUN apt-get install -yqq uuid-runtime gdb-multiarch && apt-get clean

WORKDIR /home/${USERNAME}/

Expand Down
3 changes: 2 additions & 1 deletion demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14)

# Set application data
set(APP "Microvisor Remote Debug Demo")
set(VERSION_NUMBER "3.1.4")
set(VERSION_NUMBER "3.2.0")
set(BUILD_NUMBER "1")

message("Building ${APP} ${VERSION_NUMBER} build ${BUILD_NUMBER}")
Expand All @@ -14,6 +14,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})

# Compile app source code file(s)
add_executable(${PROJECT_NAME}
generic.c
http.c
logging.c
main.c
Expand Down
93 changes: 93 additions & 0 deletions demo/generic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
*
* Microvisor C Demos
*
* Copyright © 2024, KORE Wireless
* Licence: MIT
*
*/
#include "main.h"
#include "app_version.h"


/**
* @brief Get the MV clock value.
*
* @returns The clock value.
*/
uint32_t SECURE_SystemCoreClockUpdate(void) {

uint32_t clock = 0;
mvGetHClk(&clock);
return clock;
}


/**
* @brief System clock configuration.
*/
void system_clock_config(void) {

SystemCoreClockUpdate();
HAL_InitTick(TICK_INT_PRIORITY);
}


/**
* @brief Show basic device info.
*/
void show_wake_reason(void) {

static char* reasons[] = {
"Cold boot or wake-up from shutdown mode",
"Microvisor restart requested via server",
"Application restart requested via server",
"Application restarted by debugger",
"Microvisor kernel crash",
"Microvisor watchdog failure",
"Microvisor out of memory error",
"Unspecified Microvisor error",
"Application crash",
"Application updated",
"Microvisor updated",
"Device option bytes updated",
"Device woken from deep sleep due to check-in period expiration",
"Device woken from deep sleep by application",
"Device woken from deep sleep due to cellular modem interrupt",
"Device woken from deep sleep due to application RTC wakeup",
"Device woken from deep sleep: reason unclear"
};

enum MvWakeReason reason;
if (mvGetWakeReason(&reason) == MV_STATUS_OKAY && reason < 17) {
server_log("Wake reason: %s", reasons[reason]);
return;
}

server_log("Wake reason: Unknown");
}


/**
* @brief Show basic device info.
*/
void log_device_info(void) {

uint8_t buffer[35] = { 0 };
mvGetDeviceId(buffer, 34);
server_log("Device: %s", buffer);
server_log(" App: %s %s-%u", APP_NAME, APP_VERSION, BUILD_NUM);
}


/**
* @brief Enable or disable the Microvisor system LED.
* NOTE If disabled, connection state can not be determined visually.
*
* @param do_enable: `true` to enable the system LED, `false` to disable.
*/
void control_system_led(bool do_enable) {

enum MvStatus status = mvSystemLedEnable(do_enable ? 1 : 0);
assert(status == MV_STATUS_OKAY);
}
32 changes: 32 additions & 0 deletions demo/generic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
*
* Microvisor C Demos
*
* Copyright © 2024, KORE Wireless
* Licence: MIT
*
*/
#ifndef _GENERIC_H_
#define _GENERIC_H_


#ifdef __cplusplus
extern "C" {
#endif


/*
* PROTOTYPES
*/
void system_clock_config(void);
void show_wake_reason(void);
void log_device_info(void);
void control_system_led(bool do_enable);


#ifdef __cplusplus
}
#endif


#endif // _GENERIC_H_
59 changes: 36 additions & 23 deletions demo/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
*/
// Central store for Microvisor resource handles used in this code.
// See `https://www.twilio.com/docs/iot/microvisor/syscalls#handles`
struct {
static struct {
MvNotificationHandle notification;
MvNetworkHandle network;
MvChannelHandle channel;
} http_handles = { 0, 0, 0 };

// Central store for HTTP request management notification records.
// Holds HTTP_NT_BUFFER_SIZE_R records at a time -- each record is 16 bytes in size.
static volatile struct MvNotification http_notification_center[HTTP_NT_BUFFER_SIZE_R] __attribute__((aligned(8)));
static struct MvNotification http_notification_center[HTTP_NT_BUFFER_SIZE_R] __attribute__((aligned(8)));
static volatile uint32_t current_notification_index = 0;

// Defined in `main.c`
Expand All @@ -38,8 +38,8 @@ extern volatile bool channel_was_closed;
bool http_open_channel(void) {

// Set up the HTTP channel's multi-use send and receive buffers
static volatile uint8_t http_rx_buffer[HTTP_RX_BUFFER_SIZE_B] __attribute__((aligned(512)));
static volatile uint8_t http_tx_buffer[HTTP_TX_BUFFER_SIZE_B] __attribute__((aligned(512)));
static uint8_t http_rx_buffer[HTTP_RX_BUFFER_SIZE_B] __attribute__((aligned(512)));
static uint8_t http_tx_buffer[HTTP_TX_BUFFER_SIZE_B] __attribute__((aligned(512)));

// Get the network channel handle.
// NOTE This is set in `logging.c` which puts the network in place
Expand All @@ -49,15 +49,15 @@ bool http_open_channel(void) {
server_log("Network handle: %lu", (uint32_t)http_handles.network);

// Configure the required data channel
struct MvOpenChannelParams channel_config = {
const struct MvOpenChannelParams channel_config = {
.version = 1,
.v1 = {
.notification_handle = http_handles.notification,
.notification_tag = USER_TAG_HTTP_OPEN_CHANNEL,
.network_handle = http_handles.network,
.receive_buffer = (uint8_t*)http_rx_buffer,
.receive_buffer = http_rx_buffer,
.receive_buffer_len = sizeof(http_rx_buffer),
.send_buffer = (uint8_t*)http_tx_buffer,
.send_buffer = http_tx_buffer,
.send_buffer_len = sizeof(http_tx_buffer),
.channel_type = MV_CHANNELTYPE_HTTP,
.endpoint = {
Expand Down Expand Up @@ -100,6 +100,17 @@ void http_close_channel(void) {
}


/**
* @brief Provide the current channel handle.
*
* @returns The channel handle.
*/
MvChannelHandle http_get_handle(void) {

return http_handles.channel;
}


/**
* @brief Configure the channel Notification Center.
*/
Expand All @@ -109,7 +120,7 @@ void http_setup_notification_center(void) {
memset((void *)http_notification_center, 0x00, sizeof(http_notification_center));

// Configure a notification center for network-centric notifications
static struct MvNotificationSetup http_notification_setup = {
const struct MvNotificationSetup http_notification_setup = {
.irq = TIM8_BRK_IRQn,
.buffer = (struct MvNotification *)http_notification_center,
.buffer_size = sizeof(http_notification_center)
Expand All @@ -118,12 +129,12 @@ void http_setup_notification_center(void) {
// Ask Microvisor to establish the notification center
// and confirm that it has accepted the request
enum MvStatus status = mvSetupNotifications(&http_notification_setup, &http_handles.notification);
do_assert(status == MV_STATUS_OKAY, "Could not set up HTTP channel NC");
do_assert(status == MV_STATUS_OKAY, "Could not set up HTTP channel Notification Center");

// Start the notification IRQ
NVIC_ClearPendingIRQ(TIM8_BRK_IRQn);
NVIC_EnableIRQ(TIM8_BRK_IRQn);
server_log("HTTP NC handle: %lu", (uint32_t)http_handles.notification);
server_log("HTTP Notification Center handle: %lu", (uint32_t)http_handles.notification);
}


Expand All @@ -132,34 +143,36 @@ void http_setup_notification_center(void) {
*
* @returns `true` if the request was accepted by Microvisor, otherwise `false`
*/
enum MvStatus http_send_request(void) {
enum MvStatus http_send_request(bool do_reset) {

static uint32_t item_number = 0;
static uint32_t item_number = 1;

// Make sure we have a valid channel handle
if (http_handles.channel == 0) {
// There's no open channel, so open
// one now and try to send again
http_open_channel();
return http_send_request();
return http_send_request(false);
}

server_log("Sending HTTP request");
server_log("Preparing HTTP request");

if (do_reset) item_number = 1;

// Set up the request
const char verb[] = "GET";
const char body[] = "";
char uri[46] = "";
sprintf(uri, "https://jsonplaceholder.typicode.com/todos/%lu", item_number++);
struct MvHttpHeader hdrs[] = {};
struct MvHttpRequest request_config = {
char url[64] = "";
snprintf(url, 64, "https://jsonplaceholder.typicode.com/todos/%lu", item_number++);
const struct MvHttpHeader hdrs[] = {};
const struct MvHttpRequest request_config = {
.method = {
.data = (const uint8_t *)verb,
.length = strlen(verb)
},
.url = {
.data = (const uint8_t *)uri,
.length = strlen(uri)
.data = (const uint8_t *)url,
.length = strlen(url)
},
.num_headers = 0,
.headers = hdrs,
Expand All @@ -173,7 +186,7 @@ enum MvStatus http_send_request(void) {
// Issue the request -- and check its status
enum MvStatus status = mvSendHttpRequest(http_handles.channel, &request_config);
if (status == MV_STATUS_OKAY) {
server_log("Request sent to Microvisor Cloud");
server_log("Request sent to the Microvisor Cloud");
} else if (status == MV_STATUS_CHANNELCLOSED) {
server_error("HTTP channel %lu already closed", (uint32_t)http_handles.channel);
} else {
Expand All @@ -194,7 +207,7 @@ void TIM8_BRK_IRQHandler(void) {

// Check for a suitable event: readable data in the channel
bool got_notification = false;
volatile struct MvNotification notification = http_notification_center[current_notification_index];
struct MvNotification notification = http_notification_center[current_notification_index];
if (notification.event_type == MV_EVENTTYPE_CHANNELDATAREADABLE) {
// Flag we need to access received data and to close the HTTP channel
// when we're back in the main loop. This lets us exit the ISR quickly.
Expand All @@ -217,4 +230,4 @@ void TIM8_BRK_IRQHandler(void) {
// See https://www.twilio.com/docs/iot/microvisor/microvisor-notifications#buffer-overruns
notification.event_type = 0;
}
}
}
3 changes: 2 additions & 1 deletion demo/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ extern "C" {
void http_setup_notification_center(void);
bool http_open_channel(void);
void http_close_channel(void);
enum MvStatus http_send_request(void);
MvChannelHandle http_get_handle(void);
enum MvStatus http_send_request(bool do_reset);


#ifdef __cplusplus
Expand Down
10 changes: 6 additions & 4 deletions demo/logging.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Microvisor Remote Debugging Demo
* Microvisor C Demos
*
* Copyright © 2024, KORE Wireless
* Licence: MIT
Expand All @@ -21,13 +21,13 @@ static void post_log(bool is_err, const char* format_string, va_list args);
* GLOBALS
*/
// Entities for Microvisor application logging
static uint8_t log_buffer[LOG_BUFFER_SIZE_B] __attribute__((aligned(512))) = {0};
static uint8_t log_buffer[LOG_BUFFER_SIZE_B] __attribute__((aligned(512))) = {0};
static uint32_t log_state = USER_HANDLE_LOGGING_OFF;

// Entities for local serial logging
static bool uart_available = false;
// Declared in `uart_logging.c`
extern UART_HandleTypeDef uart;
static bool uart_available = false;


/**
Expand Down Expand Up @@ -105,9 +105,11 @@ void server_error(const char* format_string, ...) {
*/
static void post_log(bool is_err, const char* format_string, va_list args) {

log_start();
static char buffer[LOG_MESSAGE_MAX_LEN_B] = {0};

// Initialize logging if we need to
log_start();

// Write the message type to the message
sprintf(buffer, is_err ? "[ERROR] " : "[DEBUG] ");

Expand Down
Loading

0 comments on commit ff0524e

Please sign in to comment.