Skip to content

Commit ba30f68

Browse files
committed
Fix for issue with new client metrics
Fixes #115
1 parent 9a9f190 commit ba30f68

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/aws_iot_mqtt_client.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@
3939
extern "C" {
4040
#endif
4141

42+
#include <string.h>
43+
4244
#include "aws_iot_log.h"
4345
#include "aws_iot_mqtt_client_interface.h"
46+
#include "aws_iot_version.h"
47+
48+
#if !DISABLE_METRICS
49+
#define SDK_METRICS_LEN 25
50+
#define SDK_METRICS_TEMPLATE "?SDK=C&Version=%d.%d.%d"
51+
static char pUsernameTemp[SDK_METRICS_LEN] = {0};
52+
#endif
4453

4554
#ifdef _ENABLE_THREAD_SUPPORT_
4655
#include "threads_interface.h"
@@ -136,8 +145,16 @@ IoT_Error_t aws_iot_mqtt_set_connect_params(AWS_IoT_Client *pClient, IoT_Client_
136145
pClient->clientData.options.MQTTVersion = pNewConnectParams->MQTTVersion;
137146
pClient->clientData.options.pClientID = pNewConnectParams->pClientID;
138147
pClient->clientData.options.clientIDLen = pNewConnectParams->clientIDLen;
148+
#if !DISABLE_METRICS
149+
if (0 == strlen(pUsernameTemp)) {
150+
snprintf(pUsernameTemp, SDK_METRICS_LEN, SDK_METRICS_TEMPLATE, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
151+
}
152+
pClient->clientData.options.pUsername = (char*)&pUsernameTemp[0];
153+
pClient->clientData.options.usernameLen = strlen(pUsernameTemp);
154+
#else
139155
pClient->clientData.options.pUsername = pNewConnectParams->pUsername;
140156
pClient->clientData.options.usernameLen = pNewConnectParams->usernameLen;
157+
#endif
141158
pClient->clientData.options.pPassword = pNewConnectParams->pPassword;
142159
pClient->clientData.options.passwordLen = pNewConnectParams->passwordLen;
143160
pClient->clientData.options.will.pTopicName = pNewConnectParams->will.pTopicName;

src/aws_iot_mqtt_client_connect.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ extern "C" {
4444
#include <aws_iot_mqtt_client.h>
4545
#include "aws_iot_mqtt_client_interface.h"
4646
#include "aws_iot_mqtt_client_common_internal.h"
47-
#include "aws_iot_version.h"
48-
49-
#if !DISABLE_METRICS
50-
#define SDK_METRICS_LEN 25
51-
#define SDK_METRICS_TEMPLATE "?SDK=C&Version=%d.%d.%d"
52-
#endif
5347

5448
typedef union {
5549
uint8_t all; /**< all connect flags */
@@ -464,9 +458,6 @@ static IoT_Error_t _aws_iot_mqtt_internal_connect(AWS_IoT_Client *pClient, IoT_C
464458
IoT_Error_t aws_iot_mqtt_connect(AWS_IoT_Client *pClient, IoT_Client_Connect_Params *pConnectParams) {
465459
IoT_Error_t rc, disconRc;
466460
ClientState clientState;
467-
#if !DISABLE_METRICS
468-
char pUsernameTemp[SDK_METRICS_LEN] = {0};
469-
#endif
470461
FUNC_ENTRY;
471462

472463
if(NULL == pClient) {
@@ -483,13 +474,6 @@ IoT_Error_t aws_iot_mqtt_connect(AWS_IoT_Client *pClient, IoT_Client_Connect_Par
483474

484475
aws_iot_mqtt_set_client_state(pClient, clientState, CLIENT_STATE_CONNECTING);
485476

486-
#if !DISABLE_METRICS
487-
if (NULL != pConnectParams) {
488-
pConnectParams->usernameLen = snprintf(pUsernameTemp, SDK_METRICS_LEN, SDK_METRICS_TEMPLATE, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
489-
pConnectParams->pUsername = (char*)&pUsernameTemp;
490-
}
491-
#endif
492-
493477
rc = _aws_iot_mqtt_internal_connect(pClient, pConnectParams);
494478

495479
if(SUCCESS != rc) {

tests/unit/src/aws_iot_tests_unit_helper_functions.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
#include "aws_iot_mqtt_client.h"
2424
#include "aws_iot_tests_unit_mock_tls_params.h"
2525
#include "aws_iot_tests_unit_helper_functions.h"
26+
#include "aws_iot_version.h"
27+
28+
#if !DISABLE_METRICS
29+
#define SDK_METRICS_LEN 25
30+
#define SDK_METRICS_TEMPLATE "?SDK=C&Version=%d.%d.%d"
31+
static char pUsernameTemp[SDK_METRICS_LEN] = {0};
32+
#endif
2633

2734
#define CONNACK_SUBACK_PACKET_SIZE 9
2835
#define PUBACK_PACKET_SIZE 4
@@ -402,7 +409,11 @@ bool isConnectTxBufFlagCorrect(IoT_Client_Connect_Params *settings, ConnectBuffe
402409
bool ret = true;
403410
int i;
404411
unsigned char myByte[8]; // Construct our own connect flag byte according to the settings
412+
#if !DISABLE_METRICS
413+
myByte[0] = (unsigned char) (1); // User Name Flag
414+
#else
405415
myByte[0] = (unsigned char) (settings->pUsername == NULL ? 0 : 1); // User Name Flag
416+
#endif
406417
myByte[1] = (unsigned char) (settings->pPassword == NULL ? 0 : 1); // Password Flag
407418
myByte[2] = 0; // Will Retain
408419
// QoS
@@ -433,7 +444,15 @@ bool isConnectTxBufPayloadCorrect(IoT_Client_Connect_Params *settings, unsigned
433444
unsigned int ClientIDLen = (unsigned int) strlen(settings->pClientID);
434445
unsigned int WillTopicLen = (unsigned int) strlen(settings->will.pTopicName);
435446
unsigned int WillMsgLen = (unsigned int) strlen(settings->will.pMessage);
447+
#if !DISABLE_METRICS
448+
if (0 == strlen(pUsernameTemp)) {
449+
snprintf(pUsernameTemp, SDK_METRICS_LEN, SDK_METRICS_TEMPLATE,
450+
VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
451+
}
452+
unsigned int UsernameLen = (unsigned int)strlen(pUsernameTemp);
453+
#else
436454
unsigned int UsernameLen = (unsigned int) settings->usernameLen;
455+
#endif
437456
unsigned int PasswordLen = (unsigned int) settings->passwordLen;
438457
unsigned int myPayloadLen = ClientIDLen + 2 + WillTopicLen + 2 + WillMsgLen + UsernameLen + 2 + PasswordLen;
439458
char *myPayload = (char *) malloc(sizeof(char) * (myPayloadLen + 1)); // reserve 1 byte for '\0'
@@ -468,12 +487,20 @@ bool isConnectTxBufPayloadCorrect(IoT_Client_Connect_Params *settings, unsigned
468487
}
469488
}
470489
// Username
490+
#if !DISABLE_METRICS
491+
for(i = 0; i < strlen(pUsernameTemp); i++)
492+
{
493+
*op = pUsernameTemp[i];
494+
op++;
495+
}
496+
#else
471497
if(NULL != settings->pUsername) {
472498
for(i = 0; i < UsernameLen; i++) {
473499
*op = settings->pUsername[i];
474500
op++;
475501
}
476502
}
503+
#endif
477504
// PasswordLen + Password
478505
if(NULL != settings->pPassword) {
479506
*op = (char) (PasswordLen & 0x0FF00); // MSB

0 commit comments

Comments
 (0)