Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: yodiwo_helpers.c
- Revision:
- 6:4596aaa1a824
- Parent:
- 4:073e23f6718c
- Child:
- 7:a121e995f5e1
diff -r 7358822a587a -r 4596aaa1a824 yodiwo_helpers.c
--- a/yodiwo_helpers.c Mon Sep 21 06:55:29 2015 +0000
+++ b/yodiwo_helpers.c Thu Sep 24 13:15:40 2015 +0000
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
+#include <string.h>
+#include <malloc.h>
#include "jsmn.h"
#include "yodiwo_api.h"
#include "yodiwo_helpers.h"
@@ -71,6 +73,11 @@
int Parse_String(jsmntok_t* t, char* json, void** result)
{
int s = t->end - t->start;
+ //write NULL for empty strings
+ if (s == 0) {
+ *result = NULL;
+ return 1;
+ }
int escaped = count_escaped(&json[t->start], s);
*result = os_zmalloc(sizeof(char)*(s - escaped + 1));
if (*result != NULL)
@@ -83,7 +90,7 @@
int s = t->end - t->start;
char d[100]; memset(d, 0, s);
memcpy(d, &json[t->start], s);
- *result = atof(d);
+ *(double *)result = atof(d);
return 1;
}
// -----------------------------------------------------------------------------------------------------------------------
@@ -92,7 +99,7 @@
int s = t->end - t->start;
char d[100]; memset(d, 0, s);
memcpy(d, &json[t->start], s);
- *result = atof(d);
+ *(float *)result = atof(d);
return 1;
}
// -----------------------------------------------------------------------------------------------------------------------
@@ -101,7 +108,7 @@
int s = t->end - t->start;
char d[100]; memset(d, 0, s);
memcpy(d, &json[t->start], s);
- *result = atoi(d);
+ *(int32_t *)result = atoi(d);
return 1;
}
// -----------------------------------------------------------------------------------------------------------------------
@@ -115,13 +122,13 @@
if (memcmp(d, "true", 4) == 0 ||
memcmp(d, "True", 4) == 0 ||
memcmp(d, "TRUE", 4) == 0)
- *result = true;
+ *(bool *)result = true;
else
- *result = false;
+ *(bool *)result = false;
}
else
{
- *result = atoi(d) > 0;
+ *(bool *)result = atoi(d) > 0;
}
return 1;
}
@@ -215,3 +222,82 @@
return res;
}
// -----------------------------------------------------------------------------------------------------------------------
+
+int lastIndexOf(char c, char *str)
+{
+ int r = -1;
+ int i = 0;
+ for(r = -1, i = 0; str[i] != '\0'; i++) {
+ if (str[i] == c)
+ r = i;
+ }
+ return r;
+}
+
+// -----------------------------------------------------------------------------------------------------------------------
+int NodeKey_FromString(Yodiwo_Plegma_NodeKey_t *nodeKey, char *str)
+{
+ int len = lastIndexOf(KEY_SEPARATOR, str);
+ if (len <= 0)
+ return -1;
+ nodeKey->UserKey.UserID = (char *)malloc((len + 1) * sizeof(char));
+ memcpy(nodeKey->UserKey.UserID, str, len);
+ nodeKey->UserKey.UserID[len] = '\0';
+ nodeKey->NodeID = atoi(&(str[len + 1]));
+ return 0;
+}
+
+// -----------------------------------------------------------------------------------------------------------------------
+char *make_nextKey_str(char *key, int nextId)
+{
+ int idlen;
+ char id_str[10];
+ char *nextKey;
+ int r;
+
+ idlen = sprintf(id_str, "%d", nextId);
+
+ if (nextId < 0 || idlen < 0)
+ return NULL;
+ r = strlen(key) + idlen + 1 + 1; //separator + null terminationportlen;
+ nextKey = (char *)malloc(r * sizeof(char) + 2);
+ if (nextKey == NULL)
+ return NULL;
+ r = sprintf(nextKey, "%s-%s", key, id_str);
+ if (r < 0) {
+ free(nextKey);
+ return NULL;
+ }
+ return nextKey;
+}
+
+// -----------------------------------------------------------------------------------------------------------------------
+int fill_PortKey(Yodiwo_Plegma_Port_t *port, char *thingKey, int portId)
+{
+ port->PortKey = make_nextKey_str(thingKey, portId);
+ return (port->PortKey != NULL) ? 0 : -1;
+}
+
+// -----------------------------------------------------------------------------------------------------------------------
+int fill_ThingKey(Yodiwo_Plegma_Thing_t *thing, char *nodeKey, int thingId)
+{
+ thing->ThingKey = make_nextKey_str(nodeKey, thingId);
+ return (thing->ThingKey != NULL) ? 0 : -1;
+}
+
+// -----------------------------------------------------------------------------------------------------------------------
+int fill_Thing_Keys(Yodiwo_Plegma_Thing_t *thing, char *nodeKey, int thingId)
+{
+ int i;
+ int r;
+ r = fill_ThingKey(thing, nodeKey, thingId);
+ if (r < 0)
+ return -1;
+ for (i = 0; i < thing->Ports.num; i++) {
+ r = fill_PortKey(&thing->Ports.elems[i], thing->ThingKey, i + 1);
+ if (r < 0)
+ return -1;
+ }
+ return 0;
+}
+