Skip to content

Commit

Permalink
implemented receiving of credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
elpinjo committed Apr 10, 2020
1 parent b516cc6 commit b7278e1
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 12 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name=Cumulocity IoT client
version=0.0.1
version=0.0.4
author=Misja Heuveling <misja.heuveling@softwareag.com>
maintainer=Misja Heuveling <misja.heuveling@softwareag.com>
sentence=A client library to connect your Arduino to Cumulocity IoT cloud over MQTT.
paragraph=Supports MQTT to connect to Cumulocity IoT to send measurement and receive commands.
category=Communication
url=https://softwareag.cloud/
url=https://www.softwareag.cloud/site/product/cumulocity-iot.html#/
architectures=avr,esp32
includes=CumulocityClient.h
depends=WiFiClient, PubSubClient, ESP8266WiFi
190 changes: 182 additions & 8 deletions src/CumulocityClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Arduino.h"
#include "CumulocityClient.h"
#include <stdlib.h>
#include <string.h>

CumulocityClient::CumulocityClient(PubSubClient client, char* tenant, char* user, char* password, const char* deviceId) {

Expand Down Expand Up @@ -42,7 +44,7 @@ bool CumulocityClient::connectClient() {
}
);

char* user = (char*) malloc(strlen(_credentials.tenant) + strlen(_credentials.username) + 2);
char user[512];// = (char*) malloc(strlen(_credentials.tenant) + strlen(_credentials.username) + 2);

sprintf(user, "%s/%s",_credentials.tenant, _credentials.username);

Expand All @@ -56,13 +58,15 @@ bool CumulocityClient::connectClient() {
Serial.println("Connected to cumulocity.");
}

free(user);
//free(user);
//Serial.println("Freed the user string");

return success;
}

void CumulocityClient::disconnect() {

Serial.print("disconnect()");
_client.disconnect();
free(_clientId);
}
Expand Down Expand Up @@ -91,21 +95,27 @@ Credentials CumulocityClient::getCredentials() {
return _credentials;
}

void CumulocityClient::setDeviceCredentials(char* user, char* password) {
void CumulocityClient::setDeviceCredentials(char* tenant, char* user, char* password) {
_credentials.tenant = tenant;
_credentials.username = user;
_credentials.password = password;
}

void CumulocityClient::registerDevice(char* deviceName, char* deviceType){
char* mqttMessage = (char*) malloc(strlen(_deviceId) + strlen(deviceType)+6);

Serial.printf("registerDevice(%s, %s)\n", deviceName, deviceType);

char mqttMessage[1024];
// = (char*) malloc(strlen(_deviceId) + strlen(deviceType)+6);
sprintf(mqttMessage, "100,%s,%s", deviceName, deviceType);

_client.publish("s/us", mqttMessage);
}

void CumulocityClient::createMeasurement(char* fragment, char* series, char* value) {
void CumulocityClient::createMeasurement(char* fragment, char* series, char* value, char* unit) {

char* mqttMessage;
sprintf(mqttMessage, "200,%s,%s,%s", fragment, series, value);
char mqttMessage[2048];// = (char*) malloc(8+ strlen(fragment)+ strlen(series) + strlen(value) + strlen(unit));
sprintf(mqttMessage, "200,%s,%s,%s,%s", fragment, series, value, unit);
_client.publish("s/us", mqttMessage);

}
Expand All @@ -130,8 +140,172 @@ void CumulocityClient::callbackHandler(const char* topic, byte* payload, unsigne
if (strcmp(topic, "s/dcr") == 0 && length > 0) {
Serial.print("Device credentials received: ");
Serial.println(myPayload);
parseCredentials(myPayload);
_credentialsReceived = true;
}

}


void CumulocityClient::parseCredentials(char* payload) {
Serial.println("parseCredentials()");

char** elements = parseCSV(payload);
Serial.printf("tenant before update: %s\n", _credentials.tenant);
free(elements[0]);
_credentials.tenant = elements[1];
Serial.println("Pointing tenant to element 1");
_credentials.username = elements[2];
_credentials.password = elements[3];
Serial.println("copied credentials");
free(elements);
}

char** CumulocityClient::parseCSV(char* payload) {
char **buf, **bptr, *tmp, *tptr;
const char *ptr;
int fieldcnt, fQuote, fEnd;

fieldcnt = countFields( payload );

if ( fieldcnt == -1 ) {
return NULL;
}

buf = (char**) malloc( sizeof(char*) * (fieldcnt+1) );

if ( !buf ) {
return NULL;
}

tmp = (char*) malloc( strlen(payload) + 1 );

if ( !tmp )
{
free( buf );
return NULL;
}

bptr = buf;

for ( ptr = payload, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0; ; ptr++ )
{
if ( fQuote )
{
if ( !*ptr ) {
break;
}

if ( *ptr == '\"' )
{
if ( ptr[1] == '\"' )
{
*tptr++ = '\"';
ptr++;
continue;
}
fQuote = 0;
}
else {
*tptr++ = *ptr;
}

continue;
}

switch( *ptr )
{
case '\"':
fQuote = 1;
continue;
case '\0':
fEnd = 1;
case ',':
*tptr = '\0';
*bptr = strdup( tmp );

if ( !*bptr )
{
for ( bptr--; bptr >= buf; bptr-- ) {
free( *bptr );
}
free( buf );
free( tmp );

return NULL;
}

bptr++;
tptr = tmp;

if ( fEnd ) {
break;
} else {
continue;
}

default:
*tptr++ = *ptr;
continue;
}

if ( fEnd ) {
break;
}
}

*bptr = NULL;
free( tmp );
return buf;
}

void CumulocityClient::freeCSVElements( char **parsed )
{
char **ptr;

for ( ptr = parsed; *ptr; ptr++ ) {
free( *ptr );
}

free( parsed );
}

int CumulocityClient::countFields( const char *line )
{
const char *ptr;
int cnt, fQuote;

for ( cnt = 1, fQuote = 0, ptr = line; *ptr; ptr++ )
{
if ( fQuote )
{
if ( *ptr == '\"' )
{
if ( ptr[1] == '\"' )
{
ptr++;
continue;
}
fQuote = 0;
}
continue;
}

switch( *ptr )
{
case '\"':
fQuote = 1;
continue;
case ',':
cnt++;
continue;
default:
continue;
}
}

if ( fQuote ) {
return -1;
}

return cnt;
}
8 changes: 6 additions & 2 deletions src/CumulocityClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ class CumulocityClient {
bool connect();
bool connect(char* defaultTemplate);
void disconnect();
void setDeviceCredentials(char* user, char* password);
void setDeviceCredentials(char* tenant, char* user, char* password);
void registerDevice(char* deviceName, char* deviceType);
void retrieveDeviceCredentials();
Credentials getCredentials();
bool checkCredentialsReceived();
void createMeasurement(char* fragment, char* series, char* value);
void createMeasurement(char* fragment, char* series, char* value, char* unit);
void loop();

private:
bool connectClient();
void callbackHandler(const char* topic, byte* payload, unsigned int length);
void parseCredentials(char* payload);
char** parseCSV(char* payload);
void freeCSVElements(char **parsed);
int countFields( const char *line );

PubSubClient _client;

Expand Down

0 comments on commit b7278e1

Please sign in to comment.