samples: net: mqtt: Add userspace support to mqtt_publisher

Allow mqtt_publisher sample application to be run in user mode.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2020-06-15 12:37:10 +03:00
commit 6cec40e8cf
2 changed files with 72 additions and 26 deletions

View file

@ -1,12 +1,15 @@
sample:
description: TBD
name: TBD
description: MQTT publisher sample application
name: MQTT publisher
common:
harness: net
tags: net mqtt
tests:
sample.net.mqtt_publisher:
harness: net
platform_whitelist: frdm_k64f qemu_x86
tags: net mqtt
sample.net.mqtt_publisher.userspace:
platform_whitelist: frdm_k64f qemu_x86
extra_args: CONFIG_USERSPACE=y
sample.net.mqtt_publisher.bt:
harness: net
platform_whitelist: 96b_nitrogen
tags: net mqtt bluetooth

View file

@ -16,32 +16,43 @@ LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG);
#include "config.h"
#if defined(CONFIG_USERSPACE)
#include <app_memory/app_memdomain.h>
K_APPMEM_PARTITION_DEFINE(app_partition);
struct k_mem_domain app_domain;
#define APP_BMEM K_APP_BMEM(app_partition)
#define APP_DMEM K_APP_DMEM(app_partition)
#else
#define APP_BMEM
#define APP_DMEM
#endif
/* Buffers for MQTT client. */
static uint8_t rx_buffer[APP_MQTT_BUFFER_SIZE];
static uint8_t tx_buffer[APP_MQTT_BUFFER_SIZE];
static APP_BMEM uint8_t rx_buffer[APP_MQTT_BUFFER_SIZE];
static APP_BMEM uint8_t tx_buffer[APP_MQTT_BUFFER_SIZE];
#if defined(CONFIG_MQTT_LIB_WEBSOCKET)
/* Making RX buffer large enough that the full IPv6 packet can fit into it */
#define MQTT_LIB_WEBSOCKET_RECV_BUF_LEN 1280
/* Websocket needs temporary buffer to store partial packets */
static uint8_t temp_ws_rx_buf[MQTT_LIB_WEBSOCKET_RECV_BUF_LEN];
static APP_BMEM uint8_t temp_ws_rx_buf[MQTT_LIB_WEBSOCKET_RECV_BUF_LEN];
#endif
/* The mqtt client struct */
static struct mqtt_client client_ctx;
static APP_BMEM struct mqtt_client client_ctx;
/* MQTT Broker details. */
static struct sockaddr_storage broker;
static APP_BMEM struct sockaddr_storage broker;
#if defined(CONFIG_SOCKS)
static struct sockaddr socks5_proxy;
static APP_BMEM struct sockaddr socks5_proxy;
#endif
static struct pollfd fds[1];
static int nfds;
static APP_BMEM struct pollfd fds[1];
static APP_BMEM int nfds;
static bool connected;
static APP_BMEM bool connected;
#if defined(CONFIG_MQTT_LIB_TLS)
@ -51,7 +62,7 @@ static bool connected;
#define APP_CA_CERT_TAG 1
#define APP_PSK_TAG 2
static sec_tag_t m_sec_tags[] = {
static APP_DMEM sec_tag_t m_sec_tags[] = {
#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(CONFIG_NET_SOCKETS_OFFLOAD)
APP_CA_CERT_TAG,
#endif
@ -204,12 +215,12 @@ void mqtt_evt_handler(struct mqtt_client *const client,
static char *get_mqtt_payload(enum mqtt_qos qos)
{
#if APP_BLUEMIX_TOPIC
static char payload[30];
static APP_BMEM char payload[30];
snprintk(payload, sizeof(payload), "{d:{temperature:%d}}",
(uint8_t)sys_rand32_get());
#else
static char payload[] = "DOORS:OPEN_QoSx";
static APP_DMEM char payload[] = "DOORS:OPEN_QoSx";
payload[strlen(payload) - 1] = '0' + qos;
#endif
@ -470,15 +481,9 @@ static int publisher(void)
return r;
}
void main(void)
static void start_app(void)
{
int r = 0, i = 0;
#if defined(CONFIG_MQTT_LIB_TLS)
int rc;
rc = tls_init();
PRINT_RESULT("tls_init", rc);
#endif
while (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS ||
i++ < CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) {
@ -488,6 +493,44 @@ void main(void)
k_sleep(K_MSEC(5000));
}
}
exit(r);
}
#if defined(CONFIG_USERSPACE)
#define STACK_SIZE 2048
#define THREAD_PRIORITY K_PRIO_COOP(8)
K_THREAD_DEFINE(app_thread, STACK_SIZE,
start_app, NULL, NULL, NULL,
THREAD_PRIORITY, K_USER, -1);
static K_MEM_POOL_DEFINE(app_mem_pool, sizeof(uintptr_t), 1024,
2, sizeof(uintptr_t));
#endif
void main(void)
{
#if defined(CONFIG_MQTT_LIB_TLS)
int rc;
rc = tls_init();
PRINT_RESULT("tls_init", rc);
#endif
#if defined(CONFIG_USERSPACE)
struct k_mem_partition *parts[] = {
#if Z_LIBC_PARTITION_EXISTS
&z_libc_partition,
#endif
&app_partition
};
k_mem_domain_init(&app_domain, ARRAY_SIZE(parts), parts);
k_mem_domain_add_thread(&app_domain, app_thread);
k_thread_resource_pool_assign(app_thread, &app_mem_pool);
k_thread_start(app_thread);
k_thread_join(app_thread, K_FOREVER);
#else
start_app();
#endif
}