2015-09-25 03:16:02 +02:00
|
|
|
|
2015-09-22 15:58:19 +02:00
|
|
|
/** Node type: OMA Next Generation Services Interface 10 (NGSI) (FIWARE context broker)
|
2015-09-19 18:54:27 +02:00
|
|
|
*
|
2015-09-22 15:58:19 +02:00
|
|
|
* This file implements the NGSI context interface. NGSI is RESTful HTTP is specified by
|
|
|
|
* the Open Mobile Alliance (OMA).
|
|
|
|
* It uses the standard operations of the NGSI 10 context information standard.
|
2015-09-19 18:54:27 +02:00
|
|
|
*
|
|
|
|
* @see https://forge.fiware.org/plugins/mediawiki/wiki/fiware/index.php/FI-WARE_NGSI-10_Open_RESTful_API_Specification
|
2015-09-22 15:58:19 +02:00
|
|
|
* @see http://technical.openmobilealliance.org/Technical/Release_Program/docs/NGSI/V1_0-20120529-A/OMA-TS-NGSI_Context_Management-V1_0-20120529-A.pdf
|
2015-09-19 18:54:27 +02:00
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
**********************************************************************************/
|
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-09-19 18:54:27 +02:00
|
|
|
#include <curl/curl.h>
|
2015-09-21 17:13:50 +02:00
|
|
|
#include <uuid/uuid.h>
|
2015-09-19 18:54:27 +02:00
|
|
|
#include <jansson.h>
|
2015-10-14 12:19:01 +02:00
|
|
|
#include <math.h>
|
2015-10-13 16:11:58 +02:00
|
|
|
#include <pthread.h>
|
2015-12-11 12:35:32 +01:00
|
|
|
#include <unistd.h>
|
2015-09-19 18:54:27 +02:00
|
|
|
|
|
|
|
#include "ngsi.h"
|
|
|
|
#include "utils.h"
|
2015-10-14 12:19:01 +02:00
|
|
|
#include "timing.h"
|
2016-01-14 22:59:57 +01:00
|
|
|
#include "pool.h"
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-11-23 16:44:18 +01:00
|
|
|
/* Some global settings */
|
|
|
|
static char *name = NULL;
|
2015-10-11 14:45:54 +02:00
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
#if 0 /* unused at the moment */
|
2015-09-21 17:13:50 +02:00
|
|
|
static json_t * json_uuid()
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-09-21 17:13:50 +02:00
|
|
|
char eid[37];
|
|
|
|
uuid_t uuid;
|
|
|
|
|
|
|
|
uuid_generate_time(uuid);
|
2015-09-22 13:19:44 +02:00
|
|
|
uuid_unparse_lower(uuid, eid);
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
return json_string(eid);
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-09-22 13:19:44 +02:00
|
|
|
static json_t * json_date(struct timespec *ts)
|
|
|
|
{
|
|
|
|
// Example: 2015-09-21T11:42:25+02:00
|
|
|
|
char date[64];
|
2015-10-12 13:45:05 +02:00
|
|
|
strftimespec(date, sizeof(date), "%FT%T.%u%z", ts);
|
2015-09-22 13:19:44 +02:00
|
|
|
|
|
|
|
return json_string(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_t * json_lookup(json_t *array, char *key, char *needle)
|
2015-09-21 17:13:50 +02:00
|
|
|
{
|
|
|
|
size_t ind;
|
|
|
|
json_t *obj;
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
json_array_foreach(array, ind, obj) {
|
|
|
|
json_t *value = json_object_get(obj, key);
|
|
|
|
if (value && json_is_string(value)) {
|
|
|
|
if (!strcmp(json_string_value(value), needle))
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
2015-10-14 12:19:01 +02:00
|
|
|
#endif
|
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
enum ngsi_flags {
|
|
|
|
NGSI_ENTITY_ATTRIBUTES = (1 << 0),
|
|
|
|
NGSI_ENTITY_VALUES = (1 << 1) | NGSI_ENTITY_ATTRIBUTES,
|
|
|
|
NGSI_ENTITY_METADATA = (1 << 2) | NGSI_ENTITY_ATTRIBUTES,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ngsi_metadata {
|
|
|
|
char *name;
|
|
|
|
char *type;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ngsi_response {
|
|
|
|
char *data;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
static json_t* ngsi_build_entity(struct ngsi *i, struct pool *pool, int cnt, int flags)
|
2015-10-14 12:19:01 +02:00
|
|
|
{
|
2015-10-15 17:36:17 +02:00
|
|
|
json_t *entity = json_pack("{ s: s, s: s, s: b }",
|
2015-10-14 12:19:01 +02:00
|
|
|
"id", i->entity_id,
|
|
|
|
"type", i->entity_type,
|
2015-10-15 17:36:17 +02:00
|
|
|
"isPattern", 0
|
2015-10-14 12:19:01 +02:00
|
|
|
);
|
2015-10-15 17:36:17 +02:00
|
|
|
|
|
|
|
if (flags & NGSI_ENTITY_ATTRIBUTES) {
|
|
|
|
json_t *attributes = json_array();
|
|
|
|
list_foreach(struct ngsi_attribute *map, &i->mapping) {
|
|
|
|
json_t *attribute = json_pack("{ s: s, s: s }",
|
|
|
|
"name", map->name,
|
|
|
|
"type", map->type
|
|
|
|
);
|
|
|
|
|
|
|
|
if (flags & NGSI_ENTITY_VALUES) { /* Build value vector */
|
|
|
|
json_t *values = json_array();
|
|
|
|
for (int k = 0; k < cnt; k++) {
|
2016-01-14 22:59:57 +01:00
|
|
|
struct msg *m = pool_getrel(pool, k);
|
2015-10-15 17:36:17 +02:00
|
|
|
|
|
|
|
json_array_append_new(values, json_pack("[ f, f, i ]",
|
|
|
|
time_to_double(&MSG_TS(m)),
|
|
|
|
m->data[map->index].f,
|
|
|
|
m->sequence
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_set(attribute, "value", values);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & NGSI_ENTITY_METADATA) { /* Create Metadata for attribute */
|
|
|
|
json_t *metadatas = json_array();
|
|
|
|
list_foreach(struct ngsi_metadata *meta, &map->metadata) {
|
|
|
|
json_array_append_new(metadatas, json_pack("{ s: s, s: s, s: s }",
|
|
|
|
"name", meta->name,
|
|
|
|
"type", meta->type,
|
|
|
|
"value", meta->value
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_set(attribute, "metadatas", metadatas);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_array_append_new(attributes, attribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_set(entity, "attributes", attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity;
|
2015-10-14 12:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
static int ngsi_parse_entity(json_t *entity, struct ngsi *i, struct pool *pool, int cnt)
|
2015-10-14 12:19:01 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const char *id, *name, *type;
|
|
|
|
|
|
|
|
size_t index;
|
|
|
|
json_t *attribute, *attributes;
|
|
|
|
|
|
|
|
ret = json_unpack(entity, "{ s: s, s: s, s: o }",
|
|
|
|
"id", &id,
|
|
|
|
"type", &type,
|
|
|
|
"attributes", &attributes
|
|
|
|
);
|
|
|
|
if (ret || !json_is_array(attributes))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(id, i->entity_id) || strcmp(type, i->entity_type))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
for (int j = 0; j < cnt; j++) {
|
2016-01-14 22:59:57 +01:00
|
|
|
struct msg *m = pool_getrel(pool, j);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
|
|
|
m->version = MSG_VERSION;
|
2016-01-14 22:59:57 +01:00
|
|
|
m->values = json_array_size(attributes);
|
2015-10-14 12:19:01 +02:00
|
|
|
m->endian = MSG_ENDIAN_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_array_foreach(attributes, index, attribute) {
|
2015-10-15 17:36:17 +02:00
|
|
|
struct ngsi_attribute *map;
|
2015-10-14 12:19:01 +02:00
|
|
|
json_t *metadata, *values, *tuple;
|
|
|
|
|
|
|
|
/* Parse JSON */
|
2015-10-15 17:36:17 +02:00
|
|
|
ret = json_unpack(attribute, "{ s: s, s: s, s: o, s?: o }",
|
2015-10-14 12:19:01 +02:00
|
|
|
"name", &name,
|
|
|
|
"type", &type,
|
|
|
|
"value", &values,
|
|
|
|
"metadatas", &metadata
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
/* Check attribute name and type */
|
|
|
|
map = list_lookup(&i->mapping, name);
|
|
|
|
if (!map || strcmp(map->type, type))
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
/* Check metadata */
|
|
|
|
if (!json_is_array(metadata))
|
|
|
|
return -5;
|
|
|
|
|
|
|
|
/* Check number of values */
|
|
|
|
if (!json_is_array(values) || json_array_size(values) != cnt)
|
|
|
|
return -6;
|
|
|
|
|
|
|
|
size_t index2;
|
|
|
|
json_array_foreach(values, index2, tuple) {
|
2016-01-14 22:59:57 +01:00
|
|
|
struct msg *m = pool_getrel(pool, index2);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
|
|
|
/* Check sample format */
|
|
|
|
if (!json_is_array(tuple) || json_array_size(tuple) != 3)
|
|
|
|
return -7;
|
|
|
|
|
|
|
|
char *end;
|
|
|
|
const char *value, *ts, *seq;
|
|
|
|
ret = json_unpack(tuple, "[ s, s, s ]", &ts, &value, &seq);
|
|
|
|
if (ret)
|
|
|
|
return -8;
|
|
|
|
|
|
|
|
m->sequence = atoi(seq);
|
|
|
|
|
|
|
|
struct timespec tss = time_from_double(strtod(ts, &end));
|
|
|
|
if (ts == end)
|
|
|
|
return -9;
|
|
|
|
|
|
|
|
m->ts.sec = tss.tv_sec;
|
|
|
|
m->ts.nsec = tss.tv_nsec;
|
|
|
|
|
|
|
|
m->data[map->index].f = strtof(value, &end);
|
|
|
|
if (value == end)
|
|
|
|
return -10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
static int ngsi_parse_mapping(struct list *mapping, config_setting_t *cfg)
|
2015-10-15 17:36:17 +02:00
|
|
|
{
|
|
|
|
if (!config_setting_is_array(cfg))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_init(mapping, NULL);
|
|
|
|
|
|
|
|
for (int j = 0; j < config_setting_length(cfg); j++) { INDENT
|
|
|
|
const char *token = config_setting_get_string_elem(cfg, j);
|
|
|
|
if (!token)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
struct ngsi_attribute map = { .index = j };
|
|
|
|
|
|
|
|
/* Parse Attribute: AttributeName(AttributeType) */
|
|
|
|
int bytes;
|
|
|
|
if (sscanf(token, "%m[^(](%m[^)])%n", &map.name, &map.type, &bytes) != 2)
|
|
|
|
cerror(cfg, "Invalid mapping token: '%s'", token);
|
|
|
|
|
|
|
|
token += bytes;
|
|
|
|
|
|
|
|
/* MetadataName(MetadataType)=MetadataValue */
|
|
|
|
list_init(&map.metadata, NULL);
|
|
|
|
struct ngsi_metadata meta;
|
|
|
|
while (sscanf(token, " %m[^(](%m[^)])=%ms%n", &meta.name, &meta.type, &meta.value, &bytes) == 3) { INDENT
|
|
|
|
list_push(&map.metadata, memdup(&meta, sizeof(struct ngsi_metadata)));
|
|
|
|
token += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Static metadata */
|
|
|
|
struct ngsi_metadata source = {
|
|
|
|
.name = "source",
|
|
|
|
.type = "string",
|
2015-11-23 16:44:18 +01:00
|
|
|
.value = name,
|
2015-10-15 17:36:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ngsi_metadata index = {
|
|
|
|
.name = "index",
|
|
|
|
.type = "integer",
|
|
|
|
.value = alloc(8)
|
|
|
|
};
|
|
|
|
snprintf(index.value, 8, "%u", j);
|
|
|
|
|
|
|
|
list_push(&map.metadata, memdup(&index, sizeof(struct ngsi_metadata)));
|
|
|
|
list_push(&map.metadata, memdup(&source, sizeof(struct ngsi_metadata)));
|
|
|
|
|
|
|
|
list_push(mapping, memdup(&map, sizeof(struct ngsi_attribute)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ngsi_parse_context_response(json_t *response, int *code, char **reason, json_t **rentity) {
|
|
|
|
int ret;
|
|
|
|
char *codestr;
|
|
|
|
|
2015-10-15 18:23:57 +02:00
|
|
|
ret = json_unpack(response, "{ s: [ { s: O, s: { s: s, s: s } } ] }",
|
2015-10-15 17:36:17 +02:00
|
|
|
"contextResponses",
|
|
|
|
"contextElement", rentity,
|
|
|
|
"statusCode",
|
|
|
|
"code", &codestr,
|
|
|
|
"reasonPhrase", reason
|
|
|
|
);
|
|
|
|
if (ret) {
|
|
|
|
warn("Failed to find NGSI response code");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*code = atoi(codestr);
|
|
|
|
|
|
|
|
if (*code != 200)
|
|
|
|
warn("NGSI response: %s %s", codestr, *reason);
|
|
|
|
|
2015-10-15 18:23:57 +02:00
|
|
|
return ret;
|
2015-10-15 17:36:17 +02:00
|
|
|
}
|
2015-09-21 17:13:50 +02:00
|
|
|
|
|
|
|
static size_t ngsi_request_writer(void *contents, size_t size, size_t nmemb, void *userp)
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-09-21 17:13:50 +02:00
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
struct ngsi_response *mem = (struct ngsi_response *) userp;
|
|
|
|
|
|
|
|
mem->data = realloc(mem->data, mem->len + realsize + 1);
|
|
|
|
if(mem->data == NULL) /* out of memory! */
|
|
|
|
error("Not enough memory (realloc returned NULL)");
|
|
|
|
|
|
|
|
memcpy(&(mem->data[mem->len]), contents, realsize);
|
|
|
|
mem->len += realsize;
|
|
|
|
mem->data[mem->len] = 0;
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
return realsize;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
static int ngsi_request(CURL *handle, const char *endpoint, const char *operation, json_t *request, json_t **response)
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-09-21 17:13:50 +02:00
|
|
|
struct ngsi_response chunk = { 0 };
|
2015-10-14 12:19:01 +02:00
|
|
|
char *post = json_dumps(request, JSON_INDENT(4));
|
2015-10-15 17:36:17 +02:00
|
|
|
int old;
|
|
|
|
double time;
|
2015-10-09 17:20:58 +02:00
|
|
|
char url[128];
|
2015-10-15 17:36:17 +02:00
|
|
|
json_error_t err;
|
|
|
|
|
2015-10-09 17:20:58 +02:00
|
|
|
snprintf(url, sizeof(url), "%s/v1/%s", endpoint, operation);
|
|
|
|
|
|
|
|
curl_easy_setopt(handle, CURLOPT_URL, url);
|
2015-09-21 17:13:50 +02:00
|
|
|
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ngsi_request_writer);
|
|
|
|
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *) &chunk);
|
|
|
|
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, strlen(post));
|
|
|
|
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, post);
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-10-13 16:11:58 +02:00
|
|
|
debug(18, "Request to context broker: %s\n%s", url, post);
|
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
/* We don't want to leave the handle in an invalid state */
|
2015-10-13 16:11:58 +02:00
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
|
2015-09-21 17:13:50 +02:00
|
|
|
CURLcode ret = curl_easy_perform(handle);
|
2015-10-13 16:11:58 +02:00
|
|
|
pthread_setcancelstate(old, NULL);
|
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
if (ret) {
|
|
|
|
warn("HTTP request failed: %s", curl_easy_strerror(ret));
|
|
|
|
goto out;
|
|
|
|
}
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-09-28 21:32:04 +02:00
|
|
|
curl_easy_getinfo(handle, CURLINFO_TOTAL_TIME, &time);
|
2015-09-21 17:13:50 +02:00
|
|
|
|
2015-10-12 16:16:25 +02:00
|
|
|
debug(16, "Request to context broker completed in %.4f seconds", time);
|
2015-10-14 12:19:01 +02:00
|
|
|
debug(17, "Response from context broker:\n%s", chunk.data);
|
2015-09-22 13:19:44 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
*response = json_loads(chunk.data, 0, &err);
|
|
|
|
if (!*response)
|
|
|
|
warn("Received invalid JSON: %s in %s:%u:%u\n%s", err.text, err.source, err.line, err.column, chunk.data);
|
2015-09-21 17:13:50 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
out: free(post);
|
2015-09-21 17:13:50 +02:00
|
|
|
free(chunk.data);
|
|
|
|
|
2015-10-15 18:23:57 +02:00
|
|
|
return ret;
|
2015-09-21 17:13:50 +02:00
|
|
|
}
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
static int ngsi_request_context_query(CURL *handle, const char *endpoint, json_t *entity, json_t **rentity)
|
|
|
|
{
|
|
|
|
int ret, code;
|
|
|
|
char *reason;
|
|
|
|
|
|
|
|
json_t *response;
|
|
|
|
json_t *request = json_pack("{ s: [ o ] }", "entities", entity);
|
|
|
|
|
|
|
|
ret = ngsi_request(handle, endpoint, "queryContext", request, &response);
|
2015-10-15 18:23:57 +02:00
|
|
|
if (ret)
|
2015-10-15 17:36:17 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = ngsi_parse_context_response(response, &code, &reason, rentity);
|
|
|
|
if (ret)
|
|
|
|
goto out2;
|
|
|
|
|
|
|
|
out2: json_decref(response);
|
|
|
|
out: json_decref(request);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ngsi_request_context_update(CURL *handle, const char *endpoint, const char *action, json_t *entity)
|
|
|
|
{
|
|
|
|
int ret, code;
|
|
|
|
char *reason;
|
|
|
|
|
|
|
|
json_t *response;
|
|
|
|
json_t *request = json_pack("{ s: s, s: [ o ] }",
|
|
|
|
"updateAction", action,
|
|
|
|
"contextElements", entity
|
|
|
|
);
|
|
|
|
|
|
|
|
ret = ngsi_request(handle, endpoint, "updateContext", request, &response);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
json_t *rentity;
|
|
|
|
ret = ngsi_parse_context_response(response, &code, &reason, &rentity);
|
|
|
|
if (ret)
|
|
|
|
goto out2;
|
|
|
|
|
|
|
|
json_decref(rentity);
|
|
|
|
out2: json_decref(response);
|
|
|
|
out: json_decref(request);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:35:32 +01:00
|
|
|
int ngsi_init(int argc, char *argv[], config_setting_t *cfg)
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-12-11 12:35:32 +01:00
|
|
|
const char *tname;
|
|
|
|
if (config_setting_lookup_string(cfg, "name", &tname)) {
|
|
|
|
name = strdup(tname);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
name = alloc(128); /** @todo missing free */
|
|
|
|
gethostname((char *) name, 128);
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
return curl_global_init(CURL_GLOBAL_ALL);
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ngsi_deinit()
|
|
|
|
{
|
2015-11-23 16:44:18 +01:00
|
|
|
free(name);
|
|
|
|
|
2015-09-19 18:54:27 +02:00
|
|
|
curl_global_cleanup();
|
2015-09-21 17:13:50 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int ngsi_parse(struct node *n, config_setting_t *cfg)
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-10-12 15:29:26 +02:00
|
|
|
if (!config_setting_lookup_string(cfg, "access_token", &i->access_token))
|
|
|
|
i->access_token = NULL; /* disabled by default */
|
2015-09-22 15:58:19 +02:00
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
if (!config_setting_lookup_string(cfg, "endpoint", &i->endpoint))
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg, "Missing NGSI endpoint for node %s", node_name(n));
|
2015-10-12 15:29:26 +02:00
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "entity_id", &i->entity_id))
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg, "Missing NGSI entity ID for node %s", node_name(n));
|
2015-10-12 15:29:26 +02:00
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "entity_type", &i->entity_type))
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg, "Missing NGSI entity type for node %s", node_name(n));
|
2015-09-22 15:58:19 +02:00
|
|
|
|
2015-09-22 14:49:42 +02:00
|
|
|
if (!config_setting_lookup_bool(cfg, "ssl_verify", &i->ssl_verify))
|
|
|
|
i->ssl_verify = 1; /* verify by default */
|
2015-09-22 15:58:19 +02:00
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
if (!config_setting_lookup_float(cfg, "timeout", &i->timeout))
|
|
|
|
i->timeout = 1; /* default value */
|
2015-10-14 12:19:01 +02:00
|
|
|
|
|
|
|
if (!config_setting_lookup_float(cfg, "rate", &i->rate))
|
|
|
|
i->rate = 5; /* default value */
|
2015-09-21 17:13:50 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
config_setting_t *cfg_mapping = config_setting_get_member(cfg, "mapping");
|
|
|
|
if (!cfg_mapping)
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg, "Missing mapping for node %s", node_name(n));
|
2015-09-22 15:58:19 +02:00
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
if (ngsi_parse_mapping(&i->mapping, cfg_mapping))
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg_mapping, "Invalid mapping for node %s", node_name(n));
|
2015-09-21 17:13:50 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
char * ngsi_print(struct node *n)
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-09-22 12:58:37 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
return strcatf(&buf, "endpoint=%s, timeout=%.3f secs, #mappings=%zu",
|
|
|
|
i->endpoint, i->timeout, list_length(&i->mapping));
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int ngsi_destroy(struct node *n)
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-11-23 16:42:43 +01:00
|
|
|
|
|
|
|
list_destroy(&i->mapping);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-19 18:54:27 +02:00
|
|
|
int ngsi_open(struct node *n)
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-10-15 17:36:17 +02:00
|
|
|
int ret;
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-09-19 18:54:27 +02:00
|
|
|
i->curl = curl_easy_init();
|
2015-09-21 17:13:50 +02:00
|
|
|
i->headers = NULL;
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-10-12 15:29:26 +02:00
|
|
|
if (i->access_token) {
|
2015-10-14 12:19:01 +02:00
|
|
|
char buf[128];
|
2015-10-12 15:29:26 +02:00
|
|
|
snprintf(buf, sizeof(buf), "Auth-Token: %s", i->access_token);
|
2015-09-19 18:54:27 +02:00
|
|
|
i->headers = curl_slist_append(i->headers, buf);
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
/* Create timer */
|
|
|
|
if (i->timeout > 1 / i->rate)
|
|
|
|
warn("Timeout is to large for given rate: %f", i->rate);
|
|
|
|
|
2016-01-14 22:57:39 +01:00
|
|
|
i->tfd = timerfd_create_rate(i->rate);
|
|
|
|
if (i->tfd < 0)
|
|
|
|
serror("Failed to create timer");
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-09-19 18:54:27 +02:00
|
|
|
i->headers = curl_slist_append(i->headers, "User-Agent: S2SS " VERSION);
|
2015-09-22 15:58:19 +02:00
|
|
|
i->headers = curl_slist_append(i->headers, "Accept: application/json");
|
2015-09-19 18:54:27 +02:00
|
|
|
i->headers = curl_slist_append(i->headers, "Content-Type: application/json");
|
|
|
|
|
2015-09-22 14:49:42 +02:00
|
|
|
curl_easy_setopt(i->curl, CURLOPT_SSL_VERIFYPEER, i->ssl_verify);
|
2015-09-21 17:13:50 +02:00
|
|
|
curl_easy_setopt(i->curl, CURLOPT_TIMEOUT_MS, i->timeout * 1e3);
|
2015-09-19 18:54:27 +02:00
|
|
|
curl_easy_setopt(i->curl, CURLOPT_HTTPHEADER, i->headers);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-09-21 17:13:50 +02:00
|
|
|
/* Create entity and atributes */
|
2016-01-14 22:59:57 +01:00
|
|
|
json_t *entity = ngsi_build_entity(i, NULL, 0, NGSI_ENTITY_METADATA);
|
2015-10-15 17:36:17 +02:00
|
|
|
|
|
|
|
ret = ngsi_request_context_update(i->curl, i->endpoint, "APPEND", entity);
|
|
|
|
if (ret)
|
2015-11-29 22:45:46 +01:00
|
|
|
error("Failed to create NGSI context for node %s", node_name(n));
|
2015-10-15 17:36:17 +02:00
|
|
|
|
|
|
|
json_decref(entity);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
return ret;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ngsi_close(struct node *n)
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-10-14 12:19:01 +02:00
|
|
|
int ret;
|
2015-09-19 18:54:27 +02:00
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
/* Delete complete entity (not just attributes) */
|
2016-01-14 22:59:57 +01:00
|
|
|
json_t *entity = ngsi_build_entity(i, NULL, 0, 0);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
ret = ngsi_request_context_update(i->curl, i->endpoint, "DELETE", entity);
|
|
|
|
|
|
|
|
json_decref(entity);
|
2015-09-22 15:58:19 +02:00
|
|
|
|
2015-09-19 18:54:27 +02:00
|
|
|
curl_easy_cleanup(i->curl);
|
|
|
|
curl_slist_free_all(i->headers);
|
|
|
|
|
2015-10-14 12:19:01 +02:00
|
|
|
return ret;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
int ngsi_read(struct node *n, struct pool *pool, int cnt)
|
2015-10-12 15:29:26 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-10-12 15:29:26 +02:00
|
|
|
int ret;
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2016-01-14 22:57:39 +01:00
|
|
|
if (timerfd_wait(i->tfd) == 0)
|
|
|
|
perror("Failed to wait for timer");
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
json_t *rentity;
|
2016-01-14 22:59:57 +01:00
|
|
|
json_t *entity = ngsi_build_entity(i, NULL, 0, 0);
|
2015-10-12 15:29:26 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
ret = ngsi_request_context_query(i->curl, i->endpoint, entity, &rentity);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
ret = ngsi_parse_entity(rentity, i, pool, cnt);
|
2015-10-15 17:36:17 +02:00
|
|
|
if (ret)
|
|
|
|
goto out2;
|
|
|
|
|
2015-10-15 18:23:57 +02:00
|
|
|
out2: json_decref(rentity);
|
|
|
|
out: json_decref(entity);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-10-15 18:23:57 +02:00
|
|
|
return ret;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
int ngsi_write(struct node *n, struct pool *pool, int cnt)
|
2015-09-19 18:54:27 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct ngsi *i = n->_vd;
|
2015-10-15 17:36:17 +02:00
|
|
|
int ret;
|
2015-10-12 15:29:26 +02:00
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
json_t *entity = ngsi_build_entity(i, pool, cnt, NGSI_ENTITY_VALUES);
|
2015-10-14 12:19:01 +02:00
|
|
|
|
2015-10-15 17:36:17 +02:00
|
|
|
ret = ngsi_request_context_update(i->curl, i->endpoint, "UPDATE", entity);
|
|
|
|
|
|
|
|
json_decref(entity);
|
|
|
|
|
|
|
|
return ret ? 0 : cnt;
|
2015-09-19 18:54:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:44:01 +01:00
|
|
|
static struct node_type vt = {
|
|
|
|
.name = "ngsi",
|
|
|
|
.description = "OMA Next Generation Services Interface 10 (libcurl, libjansson, libuuid)",
|
2016-01-14 22:54:08 +01:00
|
|
|
.vectorize = 0, /* unlimited */
|
2015-11-29 22:47:57 +01:00
|
|
|
.size = sizeof(struct ngsi),
|
2015-11-23 16:44:01 +01:00
|
|
|
.parse = ngsi_parse,
|
|
|
|
.print = ngsi_print,
|
|
|
|
.open = ngsi_open,
|
|
|
|
.close = ngsi_close,
|
|
|
|
.read = ngsi_read,
|
|
|
|
.write = ngsi_write,
|
|
|
|
.init = ngsi_init,
|
|
|
|
.deinit = ngsi_deinit
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_NODE_TYPE(&vt)
|