Skip to content

Commit

Permalink
sample: net: mqtt: add ALPN Sample code for aws
Browse files Browse the repository at this point in the history
due to MQTT now have ALPN support
the example code of using ALPN to connect MQTT
over port 443 need to be added

Signed-off-by: sukrit buddeewong <sukrit.omu@gmail.com>
  • Loading branch information
ZYNQHRONIZE authored and dleach02 committed Mar 14, 2024
1 parent 3ad8d32 commit ddb147d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
6 changes: 6 additions & 0 deletions samples/net/cloud/aws_iot_mqtt/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ config AWS_ENDPOINT
Endpoint (hostname) of the AWS MQTT broker.
Note that the endpoint is different when using AWS Device Advisor.

config AWS_MQTT_PORT
int "MQTT Port"
default 8883
help
Set port of AWS MQTT broker.

config AWS_THING_NAME
string "AWS Thing name"
default "myThingName"
Expand Down
1 change: 1 addition & 0 deletions samples/net/cloud/aws_iot_mqtt/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Core region, thing, and device advisor configuration:

- :kconfig:option:`CONFIG_AWS_ENDPOINT`: The AWS IoT Core broker endpoint, found in the AWS IoT Core
console. This will be specific if running a test suite using device advisor.
- :kconfig:option:`CONFIG_AWS_MQTT_PORT`: Port number for AWS IoT Core MQTT broker.
- :kconfig:option:`CONFIG_AWS_THING_NAME`: The name of the thing created in AWS IoT Core. Associated
with the certificate it will be used as the client id. We will use
``zephyr_sample`` in this example.
Expand Down
3 changes: 3 additions & 0 deletions samples/net/cloud/aws_iot_mqtt/prj.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CONFIG_AWS_ENDPOINT="a31gokdeokxhl8-ats.iot.eu-west-1.amazonaws.com"
CONFIG_AWS_MQTT_PORT=8883
CONFIG_AWS_THING_NAME="zephyr_sample"
CONFIG_AWS_PUBLISH_TOPIC="zephyr_sample/data"
CONFIG_AWS_SUBSCRIBE_TOPIC="zephyr_sample/downlink"
Expand Down Expand Up @@ -45,6 +46,7 @@ CONFIG_NET_BUF_TX_COUNT=32
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=y
CONFIG_MQTT_KEEPALIVE=60
CONFIG_MQTT_LIB_TLS_USE_ALPN=y

# TLS
CONFIG_MBEDTLS=y
Expand All @@ -58,3 +60,4 @@ CONFIG_MBEDTLS_AES_ROM_TABLES=y
CONFIG_MBEDTLS_TLS_VERSION_1_2=y
CONFIG_MBEDTLS_MEMORY_DEBUG=y
CONFIG_MBEDTLS_HAVE_TIME_DATE=y
CONFIG_MBEDTLS_SSL_ALPN=y
2 changes: 1 addition & 1 deletion samples/net/cloud/aws_iot_mqtt/src/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void handler(struct net_mgmt_event_callback *cb,
}

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
if (iface->config.ip.ipv4->unicast[i].addr_type !=
if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type !=
NET_ADDR_DHCP) {
continue;
}
Expand Down
14 changes: 12 additions & 2 deletions samples/net/cloud/aws_iot_mqtt/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LOG_MODULE_REGISTER(aws, LOG_LEVEL_DBG);

#define SNTP_SERVER "0.pool.ntp.org"

#define AWS_BROKER_PORT "8883"
#define AWS_BROKER_PORT CONFIG_AWS_MQTT_PORT

#define MQTT_BUFFER_SIZE 256u
#define APP_BUFFER_SIZE 4096u
Expand All @@ -54,6 +54,10 @@ static uint32_t messages_received_counter;
static bool do_publish; /* Trigger client to publish */
static bool do_subscribe; /* Trigger client to subscribe */

#if (CONFIG_AWS_MQTT_PORT == 443 && !defined(CONFIG_MQTT_LIB_WEBSOCKET))
static const char * const alpn_list[] = {"x-amzn-mqtt-ca"};
#endif

#define TLS_TAG_DEVICE_CERTIFICATE 1
#define TLS_TAG_DEVICE_PRIVATE_KEY 1
#define TLS_TAG_AWS_CA_CERTIFICATE 2
Expand Down Expand Up @@ -266,6 +270,10 @@ static void aws_client_setup(void)
tls_config->sec_tag_count = ARRAY_SIZE(sec_tls_tags);
tls_config->hostname = CONFIG_AWS_ENDPOINT;
tls_config->cert_nocopy = TLS_CERT_NOCOPY_NONE;
#if (CONFIG_AWS_MQTT_PORT == 443 && !defined(CONFIG_MQTT_LIB_WEBSOCKET))
tls_config->alpn_protocol_name_list = alpn_list;
tls_config->alpn_protocol_name_count = ARRAY_SIZE(alpn_list);
#endif
}

struct backoff_context {
Expand Down Expand Up @@ -443,8 +451,10 @@ static int resolve_broker_addr(struct sockaddr_in *broker)
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0,
};
char port_string[6] = {0};

ret = zsock_getaddrinfo(CONFIG_AWS_ENDPOINT, AWS_BROKER_PORT, &hints, &ai);
sprintf(port_string, "%d", AWS_BROKER_PORT);
ret = zsock_getaddrinfo(CONFIG_AWS_ENDPOINT, port_string, &hints, &ai);
if (ret == 0) {
char addr_str[INET_ADDRSTRLEN];

Expand Down

0 comments on commit ddb147d

Please sign in to comment.