Skip to content

Commit

Permalink
Tests and prop API
Browse files Browse the repository at this point in the history
  • Loading branch information
embhorn committed Aug 8, 2018
1 parent 8e2e449 commit 506349c
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 14 deletions.
11 changes: 11 additions & 0 deletions commit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 12 additions & 12 deletions examples/mqttclient/mqttclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion scripts/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions scripts/wiot.test
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions src/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions src/mqtt_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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++) {
Expand Down
23 changes: 23 additions & 0 deletions wolfmqtt/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion wolfmqtt/mqtt_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 506349c

Please sign in to comment.