diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-10-25 11:27:15 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-10-25 11:27:15 +0200 |
commit | e8e37bb6e3cdc85dbab4e6cd97981340e50aa10c (patch) | |
tree | b874a996729455ff70ba07556fb787e19dfef40d | |
parent | 8fde31a4e6760e6ef5cee75c736898901015184f (diff) |
database: simplify
Change-Id: I061051c5f8db1680368a2fd0089f554cd5969d73
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | ll-database-binding/src/ll-database-binding.c | 33 | ||||
-rw-r--r-- | ll-database-binding/src/utils.h | 72 |
2 files changed, 20 insertions, 85 deletions
diff --git a/ll-database-binding/src/ll-database-binding.c b/ll-database-binding/src/ll-database-binding.c index 924bb89..14b8b20 100644 --- a/ll-database-binding/src/ll-database-binding.c +++ b/ll-database-binding/src/ll-database-binding.c @@ -2,6 +2,7 @@ * Copyright 2017 IoT.bzh * * author: Loïc Collignon <loic.collignon@iot.bzh> + * author: Jose Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,14 +24,13 @@ #include <stdio.h> #include <string.h> #include <linux/limits.h> + #include <json-c/json.h> #include <db.h> #define AFB_BINDING_VERSION 2 #include <afb/afb-binding.h> -#include "utils.h" - #ifndef MAX_PATH #define MAX_PATH 1024 #endif @@ -299,20 +299,27 @@ static const struct afb_auth ll_database_binding_auths[] = { }; */ +#define VERB(name_,auth_,info_,sess_) {\ + .verb = #name_, \ + .callback = verb_##name_, \ + .auth = auth_, \ + .info = info_, \ + .session = sess_ } + static const afb_verb_v2 ll_database_binding_verbs[]= { - REGISTER_VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2), - REGISTER_VERB(update, NULL, NULL, AFB_SESSION_NONE_V2), - REGISTER_VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2), - REGISTER_VERB(read, NULL, NULL, AFB_SESSION_NONE_V2), + VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2), + VERB(update, NULL, NULL, AFB_SESSION_NONE_V2), + VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2), + VERB(read, NULL, NULL, AFB_SESSION_NONE_V2), { .verb = NULL} }; const struct afb_binding_v2 afbBindingV2 = { - .api = "ll-database", - .specification = NULL, - .verbs = ll_database_binding_verbs, - .preinit = NULL, - .init = ll_database_binding_init, - .onevent = NULL, - .noconcurrency = 0 + .api = "ll-database", + .specification = NULL, + .verbs = ll_database_binding_verbs, + .preinit = NULL, + .init = ll_database_binding_init, + .onevent = NULL, + .noconcurrency = 0 }; diff --git a/ll-database-binding/src/utils.h b/ll-database-binding/src/utils.h deleted file mode 100644 index 093d591..0000000 --- a/ll-database-binding/src/utils.h +++ /dev/null @@ -1,72 +0,0 @@ -/* -* Copyright 2017 IoT.bzh -* -* author: Loïc Collignon <loic.collignon@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. -*/ - -#ifndef _BINDING_UTILS_H_ -#define _BINDING_UTILS_H_ - -#include <string.h> -#include <json-c/json.h> - -#ifndef AFB_BINDING_VERSION -#define AFB_BINDING_VERSION 2 -#endif -#include <afb/afb-binding.h> - -#define REGISTER_VERB(n, a, i, s) { .verb = #n, .callback = verb_##n, .auth = a, .info = i, .session = s } - -/** - * @brief Get a string from a json object. - * @param[in] obj Json object from wich the string is queried. - * @param[in] name Name of the string to get. - * @return The string value. - */ -static inline const char* get_json_string(struct json_object* obj, const char* name) -{ - struct json_object* item = NULL; - if (!obj || !name || !strlen(name)) return NULL; - if (!json_object_object_get_ex(obj, name, &item) || !item) return NULL; - return json_object_get_string(item); -} - -/** - * @brief Add a string key/value to a json object. - * @param[in] obj The json object to which the key/value is added. - * @param[in] key The key to add. - * @param[in] value The value to add. - */ -static inline void json_object_add_string(struct json_object* obj, const char* key, const char* value) -{ - json_object_object_add(obj, key, json_object_new_string(value)); -} - -/** - * @brief Send an @c event with the specified @c message then fail with the same @c message. - * @param[in] req The query to fail. - * @param[in] event The event to push. - * @param[in] message The message to push with the event and use as a fail message. - */ -static inline void afb_fail_ex(struct afb_req req, struct afb_event event, const char* message) -{ - struct json_object* result = json_object_new_object(); - json_object_add_string(result, "message", message); - afb_event_push(event, result); - - afb_req_fail(req, message, NULL); -} - -#endif // _BINDING_UTILS_H_ |