Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: adding sample to communicate with aws iot #23469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions samples/net/cloud/aws_mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the commit subject to samples: net: cloud: Add AWS IoT sample
You also need to add something in commit body. In this case it could just be something like

Adding a network sample to communicate with Amazon cloud.

Please also go through style issues that the checkpatch complains about.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do i need to push a new commit or create a new pull request, i am unaware about the process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just squash your changes together (as you had only 1 commit in this PR) and then force push to this branch related to this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to confirm, make all the changes in another commit and push to the branch,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to confirm, make all the changes in another commit and push to the branch,

no, just squash the changes in the same branch and force push to this PR


cmake_minimum_required(VERSION 3.13.1)

include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(mqtt-azure)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
70 changes: 70 additions & 0 deletions samples/net/cloud/aws_mqtt/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
CONFIG_NETWORKING=y

# Disable IPv6 support
CONFIG_NET_IPV6=n

# Enable IPv4 support
CONFIG_NET_IPV4=y
CONFIG_NET_IF_MAX_IPV4_COUNT=2
CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=3

# Enable TCP support
CONFIG_NET_TCP=y

# Enable Sockets support
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y

# Enable DHCPv4 support
CONFIG_NET_DHCPV4=y

# Enable SOCKS5 proxy support
CONFIG_SOCKS=n

# Enable MQTT Lib support
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=y

# Network configuration
CONFIG_NET_CONFIG_SETTINGS=y

# Network connection manager
CONFIG_NET_CONNECTION_MANAGER=y

CONFIG_NET_MGMT_EVENT_STACK_SIZE=1024
CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=5
CONFIG_NET_MGMT_EVENT_LOG_LEVEL_DBG=n

CONFIG_DNS_RESOLVER=y
CONFIG_DNS_SERVER_IP_ADDRESSES=y
CONFIG_DNS_SERVER1="8.8.8.8"
CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=2
CONFIG_NEWLIB_LIBC=y

CONFIG_INIT_STACKS=y
CONFIG_NET_SHELL=y

CONFIG_MAIN_STACK_SIZE=4096

# Enable Logging support
CONFIG_LOG_IMMEDIATE=y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the immediate logging, it can slow down the system and is useful only in limited use cases.

CONFIG_NET_LOG=y
CONFIG_MQTT_LOG_LEVEL_DBG=y
CONFIG_SOCKS_LOG_LEVEL_DBG=n
CONFIG_NET_CONNECTION_MANAGER_LOG_LEVEL_DBG=n
CONFIG_DNS_RESOLVER_LOG_LEVEL_DBG=n
CONFIG_NET_CONTEXT_LOG_LEVEL_DBG=n
CONFIG_SOC_PART_NUMBER_SAME70Q21B=y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove board specific stuff from this file. If you need to enable something board specific, create boards directory and put board file there (see example in various networking samples what this means).

CONFIG_SNTP=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_MQTT_LIB_TLS=y

# Enable Mbed TLS configuration
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=100000
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=10240
CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y
CONFIG_MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED=y
8 changes: 8 additions & 0 deletions samples/net/cloud/aws_mqtt/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sample:
description: MQTT sample app to AWS IoT cloud
name: aws-mqtt
tests:
sample.net.cloud.aws_mqtt:
harness: net
platform_whitelist: sam_e70_xplained
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the whitelisting and put instead minimum ram requirement. That way the sample can be run any device with network connectivity and enough ram.

tags: net mqtt cloud
16 changes: 16 additions & 0 deletions samples/net/cloud/aws_mqtt/src/certificates.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
* openssl x509 -in <filename>.pem -inform pem -noout -C
*/

#error "Fill this before generating the build"

unsigned char amazon_certificate[] = {
Copy link
Member

@jukkar jukkar Mar 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that we do things here similar way as in other samples where some file needs to be embedded into the binary. So please write this as

unsigned char amazon_certificate[] = {
#include "amazon.cert"
}

if the cert is in ascii format. If you have the cert in binary format (DER), there is automatic way to create a file that can be included here (search generate_inc_file in networking samples for an example).
The point here is that the amazon.cert file is not put to version control and this will not compile if user does not provide the file. Some nice error message can be generated of course.
Edit: we probably need to provide some sample cert files so that the sample compiles, otherwise we cannot build test this. The certs do not need to have proper credentials of course.

};


unsigned char private_key[] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this key expected to be filled in by someone? Please look at the samples/net/cloud/google_iot_mqtt to see how the keys are handled there. Right now, it isn't obvious how this would be loaded (or that it needs to be).

};

unsigned char server_cert[862] = {
};
16 changes: 16 additions & 0 deletions samples/net/cloud/aws_mqtt/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __CONFIG_H__
#define __CONFIG_H__

#define ALIVE_TIME (MSEC_PER_SEC * 60U)
#define APP_MQTT_BUFFER_SIZE 128
#error "Fill the Host Name and compile"
#define CONFIG_AWS_HOSTNAME ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hostname must be provided via Kconfig file in this case. A sample app can have a Kconfig file of its own, see other networking samples how to do it.
Probably also port and mqtt client id should be put to kconfig file too as these can change for different users.

#define CONFIG_AWS_PORT 8883
#define MQTT_CLIENTID "zephyr_publisher"

#endif /* __CONFIG_H__ */

66 changes: 66 additions & 0 deletions samples/net/cloud/aws_mqtt/src/dhcp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* DHCPv4 client startup. */

/*
* Copyright (c) 2018 Linaro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <logging/log.h>

LOG_MODULE_DECLARE(aws_mqtt, LOG_LEVEL_DBG);

#include <zephyr.h>

#include <net/net_if.h>
#include <net/net_core.h>
#include <net/net_context.h>
#include <net/net_mgmt.h>

static struct net_mgmt_event_callback mgmt_cb;

/* Semaphore to indicate a lease has been acquired. */
static K_SEM_DEFINE(got_address, 0, 1);

static void handler(struct net_mgmt_event_callback *cb,
u32_t mgmt_event,
struct net_if *iface)
{
int i;
bool notified = false;

if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
return;
}

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {

if (iface->config.ip.ipv4->unicast[i].addr_type !=
NET_ADDR_DHCP) {
continue;
}

if (!notified) {
k_sem_give(&got_address);
notified = true;
}
break;
}
}

/**
* Start a DHCP client, and wait for a lease to be acquired.
*/
void app_dhcpv4_startup(void)
{
LOG_INF("starting DHCPv4");

net_mgmt_init_event_callback(&mgmt_cb, handler,
NET_EVENT_IPV4_ADDR_ADD);
net_mgmt_add_event_callback(&mgmt_cb);

net_dhcpv4_start(net_if_get_default());

/* Wait for a lease. */
k_sem_take(&got_address, K_FOREVER);
}
14 changes: 14 additions & 0 deletions samples/net/cloud/aws_mqtt/src/dhcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* DHCPv4 client startup. */

/*
* Copyright (c) 2018 Linaro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __DHCP_H__
#define __DHCP_H__

void app_dhcpv4_startup(void);

#endif /* not __DHCP_H__ */
Loading