2 * Copyright 2017 IoT.bzh
4 * author: Loïc Collignon <loic.collignon@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <sys/types.h>
25 #include <linux/limits.h>
26 #include <json-c/json.h>
29 #define AFB_BINDING_VERSION 2
30 #include <afb/afb-binding.h>
38 #define DBFILE "ll-database-binding.db"
40 #if !defined(TO_STRING_FLAGS)
41 # if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
42 # define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4)
44 # define TO_STRING_FLAGS (JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE)
47 // ----- Globals -----
50 // ----- Binding's declarations -----
51 static int ll_database_binding_init();
52 static void verb_insert(struct afb_req req);
53 static void verb_update(struct afb_req req);
54 static void verb_delete(struct afb_req req);
55 static void verb_read(struct afb_req req);
57 // ----- Binding's implementations -----
60 * @brief Get the path to the database
62 static int get_database_path(char *buffer, size_t size)
64 static const char dbfile[] = DBFILE;
69 config = secure_getenv("XDG_CONFIG_HOME");
71 rc = snprintf(buffer, size, "%s/%s", config, dbfile);
74 home = secure_getenv("HOME");
76 rc = snprintf(buffer, size, "%s/.config/%s", home, dbfile);
79 struct passwd *pwd = getpwuid(getuid());
81 rc = snprintf(buffer, size, "%s/.config/%s", pwd->pw_dir, dbfile);
83 rc = snprintf(buffer, size, "/home/%d/.config/%s", (int)getuid(), dbfile);
90 * @brief Initialize the binding.
91 * @return Exit code, zero if success.
93 static int ll_database_binding_init()
98 ret = get_database_path(path, sizeof path);
99 if (ret < 0 || (int)ret >= (int)(sizeof path))
101 AFB_ERROR("Can't compute the database filename");
105 AFB_INFO("opening database %s", path);
107 if ((ret = db_create(&database, NULL, 0)) != 0)
109 AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
113 if ((ret = database->open(database, NULL, path, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
115 AFB_ERROR("Failed to open the '%s' database: %s.", path, db_strerror(ret));
116 database->close(database, 0);
124 * Returns the database key for the 'req'
126 static int get_key(struct afb_req req, DBT *key)
131 size_t ljkey, lappid, size;
133 struct json_object* args;
134 struct json_object* item;
137 args = afb_req_json(req);
138 if (!json_object_object_get_ex(args, "key", &item))
140 afb_req_fail(req, "no-key", NULL);
144 || !(jkey = json_object_get_string(item))
145 || !(ljkey = strlen(jkey)))
147 afb_req_fail(req, "bad-key", NULL);
152 appid = afb_req_get_application_id(req);
155 appid = strdup("#UNKNOWN-APP#");
159 afb_req_fail(req, "bad-context", NULL);
163 /* make the db-key */
164 lappid = strlen(appid);
165 size = lappid + ljkey + 2;
166 data = realloc(appid, size);
170 afb_req_fail(req, "out-of-memory", NULL);
174 memcpy(&data[lappid + 1], jkey, ljkey + 1);
178 key->size = (uint32_t)size;
182 static void put(struct afb_req req, int replace)
190 struct json_object* args;
191 struct json_object* item;
193 args = afb_req_json(req);
195 if (!json_object_object_get_ex(args, "value", &item))
197 afb_req_fail(req, "no-value", NULL);
201 value = json_object_to_json_string_ext(item, TO_STRING_FLAGS);
204 afb_req_fail(req, "out-of-memory", NULL);
208 if (get_key(req, &key))
211 AFB_INFO("put: key=%s, value=%s", (char*)key.data, value);
213 data.data = (void*)value;
214 data.size = (uint32_t)strlen(value);
216 ret = database->put(database, NULL, &key, &data, replace ? 0 : DB_NOOVERWRITE);
218 afb_req_success(req, NULL, NULL);
221 AFB_ERROR("can't %s key %s with %s", replace ? "replace" : "insert", (char*)key.data, (char*)data.data);
222 afb_req_fail_f(req, "failed", "%s", db_strerror(ret));
227 static void verb_insert(struct afb_req req)
232 static void verb_update(struct afb_req req)
237 static void verb_delete(struct afb_req req)
242 if (get_key(req, &key))
245 AFB_INFO("delete: key=%s", (char*)key.data);
247 if ((ret = database->del(database, NULL, &key, 0)) == 0)
248 afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
250 afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
255 static void verb_read(struct afb_req req)
263 struct json_object* result;
264 struct json_object* val;
267 if (get_key(req, &key))
270 AFB_INFO("read: key=%s", (char*)key.data);
274 data.flags = DB_DBT_USERMEM;
276 if ((ret = database->get(database, NULL, &key, &data, 0)) == 0)
278 result = json_object_new_object();
279 val = json_tokener_parse((char*)data.data);
280 json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));
282 afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
285 afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
290 // ----- Binding's configuration -----
292 static const struct afb_auth ll_database_binding_auths[] = {
296 static const afb_verb_v2 ll_database_binding_verbs[]= {
297 REGISTER_VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
298 REGISTER_VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
299 REGISTER_VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
300 REGISTER_VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
304 const struct afb_binding_v2 afbBindingV2 = {
305 .api = "ll-database",
306 .specification = NULL,
307 .verbs = ll_database_binding_verbs,
309 .init = ll_database_binding_init,