aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-11-07 10:54:01 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2018-11-28 17:34:38 +0100
commit99914a58a2d5b2638e2c1fdac5ca74a2d247e6fa (patch)
tree18661bc142ff60e6d1e596ae6d4fc5193774f07c /src
parentb0899d3e0d83457403fa6195a3448d9d6610adb6 (diff)
First simple platform-info API implementationguppy_6.99.2guppy/6.99.26.99.2
Bug-AGL: SPEC-1917 Change-Id: I300155302c9d69c6549ddabc6af53e2ad55eb18d Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt33
-rw-r--r--src/platform-info-apidef.h64
-rw-r--r--src/platform-info-apidef.json50
-rw-r--r--src/platform-info-binding.c125
-rw-r--r--src/platform-info-binding.h27
5 files changed, 299 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..afa714f
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,33 @@
+###########################################################################
+# Copyright 2015, 2016, 2017 IoT.bzh
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+###########################################################################
+
+# Add target to project dependency list
+PROJECT_TARGET_ADD(platform-info)
+
+ # Define project Targets
+ ADD_LIBRARY(${TARGET_NAME} MODULE platform-info-binding.c)
+
+ # Binder exposes a unique public entry point
+ SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
+ PREFIX "afb-"
+ LABELS "BINDING"
+ OUTPUT_NAME ${TARGET_NAME}
+ )
+
+ TARGET_LINK_LIBRARIES(${TARGET_NAME}
+ afb-helpers
+ ${link_libraries}
+ )
diff --git a/src/platform-info-apidef.h b/src/platform-info-apidef.h
new file mode 100644
index 0000000..c09da63
--- /dev/null
+++ b/src/platform-info-apidef.h
@@ -0,0 +1,64 @@
+static const struct afb_auth _afb_auths_platform_info[] = {
+ { .type = afb_auth_Permission, .text = "urn:AGL:permission::platform:info:get " },
+ { .type = afb_auth_Permission, .text = "urn:AGL:permission::platform:info:set " }
+};
+
+ void afv_get(afb_req_t req);
+ void afv_set(afb_req_t req);
+ void afv_subscribe(afb_req_t req);
+ void afv_unsubscribe(afb_req_t req);
+
+static const struct afb_verb_v3 _afb_verbs_platform_info[] = {
+ {
+ .verb = "get",
+ .callback = afv_get,
+ .auth = &_afb_auths_platform_info[0],
+ .info = "Get a platform data.",
+ .session = AFB_SESSION_NONE
+ },
+ {
+ .verb = "set",
+ .callback = afv_set,
+ .auth = &_afb_auths_platform_info[1],
+ .info = "Set a platform data.",
+ .session = AFB_SESSION_NONE
+ },
+ {
+ .verb = "unsubscribe",
+ .callback = afv_unsubscribe,
+ .auth = NULL,
+ .info = "Unsubscribe to changes (hotplug event, failures, ...)",
+ .session = AFB_SESSION_NONE
+ },
+ {
+ .verb = "subscribe",
+ .callback = afv_subscribe,
+ .auth = NULL,
+ .info = "Subscribe to changes (hotplug event, failures, ...)",
+ .session = AFB_SESSION_NONE
+ },
+ {
+ .verb = NULL,
+ .callback = NULL,
+ .auth = NULL,
+ .info = NULL,
+ .session = 0
+ }
+};
+
+int init(afb_api_t api);
+
+const struct afb_binding_v3 afbBindingV3 = {
+ .api = "platform-info",
+ .specification = NULL,
+ .info = "",
+ .verbs = _afb_verbs_platform_info,
+ .preinit = NULL,
+ .init = init,
+ .onevent = NULL,
+ .userdata = NULL,
+ .provide_class = NULL,
+ .require_class = NULL,
+ .require_api = NULL,
+ .noconcurrency = 0
+};
diff --git a/src/platform-info-apidef.json b/src/platform-info-apidef.json
new file mode 100644
index 0000000..fb4b863
--- /dev/null
+++ b/src/platform-info-apidef.json
@@ -0,0 +1,50 @@
+{
+ "afbidl": "0.0",
+ "info": {
+ "description": "Provide platform information to other bindings",
+ "title": "agl-service-platform-info",
+ "version": "1.0"
+ },
+ "c-generator": {
+ "version": 3,
+ "prefix": "afv_",
+ "postfix": "",
+ "preinit": null,
+ "init": "init",
+ "onevent": null,
+ "scope": "static",
+ "private": true
+ },
+ "api": {
+ "name": "platform-info",
+ "verbs": {
+ "get": {
+ "description": "Get a platform data.",
+ "permissions": { "session": "check" },
+ "request": { "$ref": "#/schemas/get-request" },
+ "reply": { "$ref": "#/schemas/any" }
+ },
+ "subscribe": {
+ "description": "Subscribe to changes (hotplug event, failures, ...)"
+ },
+ "unsubscribe": {
+ "description": "Unsubscribe to changes (hotplug event, failures, ...)"
+ }
+ }
+ },
+ "schemas": {
+ "any": {
+ "title": "Any value",
+ "type": [ "null", "boolean", "object", "array", "number", "string" ]
+ },
+ "get-request": {
+ "type": "object",
+ "properties": {
+ "oneOf": [
+ { "type": "string"},
+ { "type": "array", "items": { "type": "string"} }
+ ]
+ }
+ }
+ }
+}
diff --git a/src/platform-info-binding.c b/src/platform-info-binding.c
new file mode 100644
index 0000000..c6bc9fd
--- /dev/null
+++ b/src/platform-info-binding.c
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2016, 2018 "IoT.bzh"
+ *
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+
+#include <afb/afb-binding.h>
+#include "platform-info-binding.h"
+
+#ifndef PLATFORM_INFO_DIR
+#define PLATFORM_INFO_DIR "/etc/platform-info"
+#endif
+
+#ifndef HOTPLUG_NS
+#define HOTPLUG_NS "hotplug"
+#endif
+
+void afv_get(afb_req_t req) {
+ char *json_path = NULL, *full_json_path = NULL;
+ json_object *platform_info = (json_object*) afb_api_get_userdata(req->api);
+ json_object *args = afb_req_json(req), *result = NULL;
+
+ switch (json_object_get_type(args)) {
+ case json_type_null:
+ result = platform_info;
+ break;
+ case json_type_string:
+ full_json_path = strdupa(json_object_get_string(args));
+ result = platform_info;
+ for(json_path = strtok(full_json_path, "."); json_path && *json_path; json_path = strtok(NULL, ".")) {
+ if(! json_object_object_get_ex(result, json_path, &result)) {
+ afb_req_fail(req, "A key hasn't been found in JSON path.", json_path);
+ return;
+ }
+ }
+ break;
+ default:
+ afb_req_fail(req, "Type error", "Argument type is unknown, you must provide a string only");
+ return;
+ }
+
+ afb_req_success(req, result, NULL);
+}
+
+void afv_set(afb_req_t req) {
+ json_object *platform_info = (json_object*) afb_api_get_userdata(req->api);
+ json_object *args = afb_req_json(req);
+
+#if (JSON_C_VERSION_MAJOR_VERSION == 0 && JSON_C_VERSION_MINOR_VERSION >= 13)
+ if(json_object_object_add(platform_info, HOTPLUG_NS, args)) {
+ afb_req_fail(req, "Addition fails", NULL);
+ return;
+ }
+#else
+ json_object_object_add(platform_info, HOTPLUG_NS, args);
+#endif
+
+ afb_req_success(req, NULL, NULL);
+}
+
+// TODO RFOR: interface with inotify and udev
+void afv_unsubscribe(afb_req_t req) {
+ afb_req_success(req, NULL, NULL);
+}
+
+void afv_subscribe(afb_req_t req) {
+ afb_req_success(req, NULL, NULL);
+}
+
+int init(afb_api_t api) {
+ struct dirent* dir_ent = NULL;
+ DIR* dir_handle = opendir(PLATFORM_INFO_DIR);
+ json_object *json_file = json_object_new_object(), *current_file = NULL;
+
+ while( (dir_ent = readdir(dir_handle)) != NULL) {
+ if(dir_ent->d_type == DT_REG && dir_ent->d_name[0] != '.') {
+ size_t filepath_len = strlen(PLATFORM_INFO_DIR) + strlen(dir_ent->d_name) + 2;
+ char *filepath = alloca(filepath_len);
+
+ filepath = strncpy(filepath, PLATFORM_INFO_DIR, filepath_len);
+ filepath = strncat(filepath, "/", filepath_len - strlen(filepath) - 1);
+ filepath = strncat(filepath, dir_ent->d_name, filepath_len - strlen(filepath) - 1);
+
+ AFB_DEBUG("File in processing: %s", filepath);
+ current_file = json_object_from_file(filepath);
+#if (JSON_C_VERSION_MAJOR_VERSION == 0 && JSON_C_VERSION_MINOR_VERSION >= 13)
+ const char *error = NULL;
+ if( (error = json_util_get_last_err()) ) {
+ AFB_NOTICE("This file '%s' isn't a JSON file or have error: %s. len: %ld", filepath, error, filepath_len);
+ continue;
+ }
+#else
+ if( current_file == NULL ) {
+ AFB_NOTICE("This file '%s' isn't a JSON file or have error.", filepath);
+ continue;
+ }
+#endif
+ wrap_json_object_add(json_file, current_file);
+ AFB_DEBUG("JSON loaded: %s", json_object_get_string(json_file));
+ }
+ }
+
+ // Initializing the platform_info binding object and associated it to
+ // the api
+ afb_api_set_userdata(api, (void*)json_file);
+
+ return 0;
+}
diff --git a/src/platform-info-binding.h b/src/platform-info-binding.h
new file mode 100644
index 0000000..183fa86
--- /dev/null
+++ b/src/platform-info-binding.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2016, 2018 "IoT.bzh"
+ *
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <wrap-json.h>
+#include "platform-info-apidef.h"
+
+struct key_search_t {
+ afb_api_t api;
+ json_object **result;
+};
+
+json_object *platform_info;