From 506349cde739d78e247dc16be48b39f7c1789711 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Wed, 8 Aug 2018 11:53:30 -0500 Subject: [PATCH] Tests and prop API --- commit-tests.sh | 11 +++++++++++ examples/mqttclient/mqttclient.c | 24 ++++++++++++------------ scripts/include.am | 3 ++- scripts/wiot.test | 16 ++++++++++++++++ src/mqtt_client.c | 11 +++++++++++ src/mqtt_packet.c | 18 ++++++++++++++++++ wolfmqtt/mqtt_client.h | 23 +++++++++++++++++++++++ wolfmqtt/mqtt_packet.h | 2 +- 8 files changed, 94 insertions(+), 14 deletions(-) create mode 100755 scripts/wiot.test diff --git a/commit-tests.sh b/commit-tests.sh index 09b6880b6..37fbc4aa3 100755 --- a/commit-tests.sh +++ b/commit-tests.sh @@ -51,4 +51,15 @@ make -j 8 test; RESULT=$? [ $RESULT -ne 0 ] && echo -e "\n\nTest './configure --enable-nonblock --enable-tls' make test failed " && exit 1 + +# make sure mqtt5 with property callback is okay +echo -e "\n\nTesting mqtt5 with property callback config additionally...\n\n" +./configure --enable-mqtt5 --enable-propcb; +RESULT=$? +[ $RESULT -ne 0 ] && echo -e "\n\nTest './configure --enable-mqtt5 --enable-propcb' failed" && exit 1 + +make -j 8 test; +RESULT=$? +[ $RESULT -ne 0 ] && echo -e "\n\nTest './configure --enable-mqtt5 --enable-propcb' make test failed " && exit 1 + exit 0 diff --git a/examples/mqttclient/mqttclient.c b/examples/mqttclient/mqttclient.c index ac2a4fbd9..a6593aff4 100644 --- a/examples/mqttclient/mqttclient.c +++ b/examples/mqttclient/mqttclient.c @@ -318,32 +318,32 @@ int mqttclient_test(MQTTCtx *mqttCtx) { /* Enhanced authentication */ /* Add property: Authentication Method */ - MqttProp* prop = MqttProps_Add(&mqttCtx->connect.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props); prop->type = MQTT_PROP_AUTH_METHOD; prop->data_str.str = (char*)DEFAULT_AUTH_METHOD; prop->data_str.len = strlen(prop->data_str.str); } { /* Request Response Information */ - MqttProp* prop = MqttProps_Add(&mqttCtx->connect.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props); prop->type = MQTT_PROP_REQ_RESP_INFO; prop->data_byte = 1; } { /* Request Problem Information */ - MqttProp* prop = MqttProps_Add(&mqttCtx->connect.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props); prop->type = MQTT_PROP_REQ_PROB_INFO; prop->data_byte = 1; } { /* Maximum Packet Size */ - MqttProp* prop = MqttProps_Add(&mqttCtx->connect.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props); prop->type = MQTT_PROP_MAX_PACKET_SZ; prop->data_int = (word32)mqttCtx->max_packet_size; } { /* Topic Alias Maximum */ - MqttProp* prop = MqttProps_Add(&mqttCtx->connect.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props); prop->type = MQTT_PROP_TOPIC_ALIAS_MAX; prop->data_short = mqttCtx->topic_alias_max; } @@ -375,7 +375,7 @@ int mqttclient_test(MQTTCtx *mqttCtx) #ifdef WOLFMQTT_V5 if (mqttCtx->connect.props != NULL) { /* Release the allocated properties */ - MqttProps_Free(mqttCtx->connect.props); + MqttClient_PropsFree(mqttCtx->connect.props); } #endif @@ -404,7 +404,7 @@ int mqttclient_test(MQTTCtx *mqttCtx) if (mqttCtx->subId_not_avail != 1) { /* Subscription Identifier */ mqttCtx->topics[i].sub_id = i + 1; /* Sub ID starts at 1 */ - MqttProp* prop = MqttProps_Add(&mqttCtx->subscribe.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->subscribe.props); prop->type = MQTT_PROP_SUBSCRIPTION_ID; prop->data_int = mqttCtx->topics[i].sub_id; } @@ -430,7 +430,7 @@ int mqttclient_test(MQTTCtx *mqttCtx) #ifdef WOLFMQTT_V5 if (mqttCtx->subscribe.props != NULL) { /* Release the allocated properties */ - MqttProps_Free(mqttCtx->subscribe.props); + MqttClient_PropsFree(mqttCtx->subscribe.props); } #endif @@ -460,13 +460,13 @@ int mqttclient_test(MQTTCtx *mqttCtx) #ifdef WOLFMQTT_V5 { /* Payload Format Indicator */ - MqttProp* prop = MqttProps_Add(&mqttCtx->publish.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->publish.props); prop->type = MQTT_PROP_PLAYLOAD_FORMAT_IND; prop->data_int = 1; } { /* Content Type */ - MqttProp* prop = MqttProps_Add(&mqttCtx->publish.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->publish.props); prop->type = MQTT_PROP_CONTENT_TYPE; prop->data_str.str = (char*)"wolf_type"; prop->data_str.len = strlen(prop->data_str.str); @@ -475,7 +475,7 @@ int mqttclient_test(MQTTCtx *mqttCtx) (mqttCtx->topic_alias > 0) && (mqttCtx->topic_alias < mqttCtx->topic_alias_max)) { /* Topic Alias */ - MqttProp* prop = MqttProps_Add(&mqttCtx->publish.props); + MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->publish.props); prop->type = MQTT_PROP_TOPIC_ALIAS; prop->data_short = mqttCtx->topic_alias; } @@ -500,7 +500,7 @@ int mqttclient_test(MQTTCtx *mqttCtx) #ifdef WOLFMQTT_V5 if (mqttCtx->connect.props != NULL) { /* Release the allocated properties */ - MqttProps_Free(mqttCtx->publish.props); + MqttClient_PropsFree(mqttCtx->publish.props); } #endif diff --git a/scripts/include.am b/scripts/include.am index e59860b4c..b28f56593 100644 --- a/scripts/include.am +++ b/scripts/include.am @@ -6,5 +6,6 @@ if BUILD_EXAMPLES dist_noinst_SCRIPTS += scripts/client.test \ scripts/firmware.test \ scripts/azureiothub.test \ - scripts/awsiot.test + scripts/awsiot.test \ + scripts/wiot.test endif diff --git a/scripts/wiot.test b/scripts/wiot.test new file mode 100755 index 000000000..1c515e723 --- /dev/null +++ b/scripts/wiot.test @@ -0,0 +1,16 @@ +#!/bin/sh + +# Watson IoT Client test + +# Check for application +[ ! -x ./examples/wiot/wiot ] && echo -e "\n\nWatson IoT MQTT Client doesn't exist" && exit 1 + +def_args="-T" + +# Run + +./examples/wiot/wiot $def_args $1 +RESULT=$? +[ $RESULT -lt 0 ] && echo -e "\n\nWatson IoT MQTT Client failed! TLS=On, QoS=0" && exit 1 + +exit 0 diff --git a/src/mqtt_client.c b/src/mqtt_client.c index 54818d8f9..c67da7267 100644 --- a/src/mqtt_client.c +++ b/src/mqtt_client.c @@ -859,6 +859,17 @@ int MqttClient_Auth(MqttClient *client, MqttAuth* auth) return rc; } + +MqttProp* MqttClient_PropsAdd(MqttProp **head) +{ + return(MqttProps_Add(head)); +} + +void MqttClient_PropsFree(MqttProp *head) +{ + return(MqttProps_Free(head)); +} + #endif /* WOLFMQTT_V5 */ int MqttClient_WaitMessage(MqttClient *client, int timeout_ms) diff --git a/src/mqtt_packet.c b/src/mqtt_packet.c index 666addda4..cea326e2f 100755 --- a/src/mqtt_packet.c +++ b/src/mqtt_packet.c @@ -1171,6 +1171,9 @@ int MqttEncode_Unsubscribe(byte *tx_buf, int tx_buf_len, int header_len, remain_len, i; byte *tx_payload; MqttTopic *topic; +#ifdef WOLFMQTT_V5 + word32 props_len = 0; +#endif /* Validate required arguments */ if (tx_buf == NULL || unsubscribe == NULL) { @@ -1183,6 +1186,13 @@ int MqttEncode_Unsubscribe(byte *tx_buf, int tx_buf_len, topic = &unsubscribe->topics[i]; remain_len += (int)XSTRLEN(topic->topic_filter) + MQTT_DATA_LEN_SIZE; } +#ifdef WOLFMQTT_V5 + /* Determine length of properties */ + remain_len += props_len = MqttEncode_Props(MQTT_PACKET_TYPE_UNSUBSCRIBE, unsubscribe->props, NULL); + + /* Determine the length of the "property length" */ + remain_len += MqttEncode_Vbi(NULL, props_len); +#endif /* Encode fixed header */ header_len = MqttEncode_FixedHeader(tx_buf, tx_buf_len, remain_len, @@ -1194,6 +1204,14 @@ int MqttEncode_Unsubscribe(byte *tx_buf, int tx_buf_len, /* Encode variable header */ tx_payload += MqttEncode_Num(&tx_buf[header_len], unsubscribe->packet_id); +#ifdef WOLFMQTT_V5 + /* Encode the property length */ + tx_payload += MqttEncode_Vbi(tx_payload, props_len); + + /* Encode properties */ + tx_payload += MqttEncode_Props(MQTT_PACKET_TYPE_UNSUBSCRIBE, unsubscribe->props, tx_payload); +#endif + /* Encode payload */ for (i = 0; i < unsubscribe->topic_count; i++) { diff --git a/wolfmqtt/mqtt_client.h b/wolfmqtt/mqtt_client.h index 56bd1c03c..effe0efe8 100644 --- a/wolfmqtt/mqtt_client.h +++ b/wolfmqtt/mqtt_client.h @@ -265,6 +265,7 @@ WOLFMQTT_API int MqttClient_Unsubscribe( WOLFMQTT_API int MqttClient_Ping( MqttClient *client); + #ifdef WOLFMQTT_V5 /*! \brief Encodes and sends the MQTT Authentication Request packet and waits for the Ping Response packet @@ -276,8 +277,29 @@ WOLFMQTT_API int MqttClient_Ping( WOLFMQTT_API int MqttClient_Auth( MqttClient *client, MqttAuth *auth); + + +/*! \brief Add a new property + * \discussion Allocate a property structure and add it to the head of the list + pointed to by head. To be used prior to calling packet command. + * \param head Pointer-pointer to a property structure + * \return MQTT_CODE_SUCCESS or MQTT_CODE_ERROR_BAD_ARG + */ +WOLFMQTT_API MqttProp* MqttClient_PropsAdd( + MqttProp **head); + + +/*! \brief Free property list + * \discussion Deallocate the list pointed to by head. Must be used after the + packet command that used MqttClient_Prop_Add. + * \param head Pointer-pointer to a property structure + * \return Pointer to newly allocated property structure or NULL + */ +WOLFMQTT_API void MqttClient_PropsFree( + MqttProp *head); #endif + /*! \brief Encodes and sends the MQTT Disconnect packet (no response) * \discussion This is a non-blocking function that will try and send using MqttNet.write @@ -343,6 +365,7 @@ WOLFMQTT_API const char* MqttClient_ReturnCodeToString( "no support for error strings built in" #endif /* WOLFMQTT_NO_ERROR_STRINGS */ + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/wolfmqtt/mqtt_packet.h b/wolfmqtt/mqtt_packet.h index db7779d64..9d1e83e69 100644 --- a/wolfmqtt/mqtt_packet.h +++ b/wolfmqtt/mqtt_packet.h @@ -558,7 +558,7 @@ WOLFMQTT_LOCAL int MqttEncode_Disconnect(byte *tx_buf, int tx_buf_len, MqttDisconnect* disconnect); #ifdef WOLFMQTT_V5 -int MqttDecode_Disconnect(byte *rx_buf, int rx_buf_len, MqttDisconnect *disc); +WOLFMQTT_LOCAL int MqttDecode_Disconnect(byte *rx_buf, int rx_buf_len, MqttDisconnect *disc); WOLFMQTT_LOCAL int MqttDecode_Auth(byte *rx_buf, int rx_buf_len, MqttAuth *auth); WOLFMQTT_LOCAL int MqttEncode_Auth(byte *tx_buf, int tx_buf_len,