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 -----
49 static char database_file[MAX_PATH];
51 // ----- Binding's declarations -----
52 static int ll_database_binding_init();
53 static void verb_insert(struct afb_req req);
54 static void verb_update(struct afb_req req);
55 static void verb_delete(struct afb_req req);
56 static void verb_read(struct afb_req req);
58 // ----- Binding's implementations -----
61 * @brief Initialize the binding.
62 * @return Exit code, zero if success.
64 static int ll_database_binding_init()
67 struct passwd* result;
72 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
73 if (bufsize == -1 || bufsize > MAX_PATH) bufsize = MAX_PATH;
75 ret = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
78 if (ret == 0) AFB_ERROR("User not found");
79 else AFB_ERROR("getpwuid_r failed with %d code", ret);
83 memset(database_file, 0, MAX_PATH);
84 strcat(database_file, result->pw_dir);
85 strcat(database_file, DBFILE);
87 AFB_INFO("The database file is '%s'", database_file);
89 if ((ret = db_create(&database, NULL, 0)) != 0)
91 AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
95 if ((ret = database->open(database, NULL, database_file, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
97 AFB_ERROR("Failed to open the '%s' database: %s.", database_file, db_strerror(ret));
98 database->close(database, 0);
106 * Returns the database key for the 'req'
108 static int get_key(struct afb_req req, DBT *key)
113 size_t ljkey, lappid, size;
115 struct json_object* args;
116 struct json_object* item;
119 args = afb_req_json(req);
120 if (!json_object_object_get_ex(args, "key", &item))
122 afb_req_fail(req, "no-key", NULL);
126 || !(jkey = json_object_get_string(item))
127 || !(ljkey = strlen(jkey)))
129 afb_req_fail(req, "bad-key", NULL);
134 appid = afb_req_get_application_id(req);
137 afb_req_fail(req, "bad-context", NULL);
141 /* make the db-key */
142 lappid = strlen(appid);
143 size = lappid + ljkey + 2;
144 data = realloc(appid, size);
148 afb_req_fail(req, "out-of-memory", NULL);
152 memcpy(&data[lappid + 1], jkey, ljkey + 1);
156 key->size = (uint32_t)size;
160 static void put(struct afb_req req, int replace)
168 struct json_object* args;
169 struct json_object* item;
171 args = afb_req_json(req);
173 if (!json_object_object_get_ex(args, "value", &item))
175 afb_req_fail(req, "no-value", NULL);
179 value = json_object_to_json_string_ext(item, TO_STRING_FLAGS);
182 afb_req_fail(req, "out-of-memory", NULL);
186 if (get_key(req, &key))
189 AFB_INFO("put: key=%s, value=%s", (char*)key.data, value);
191 data.data = (void*)value;
192 data.size = (uint32_t)strlen(value);
194 ret = database->put(database, NULL, &key, &data, replace ? 0 : DB_NOOVERWRITE);
196 afb_req_success(req, NULL, NULL);
199 AFB_ERROR("can't %s key %s with %s", replace ? "replace" : "insert", (char*)key.data, (char*)data.data);
200 afb_req_fail_f(req, "failed", "%s", db_strerror(ret));
205 static void verb_insert(struct afb_req req)
210 static void verb_update(struct afb_req req)
215 static void verb_delete(struct afb_req req)
220 if (get_key(req, &key))
223 AFB_INFO("delete: key=%s", (char*)key.data);
225 if ((ret = database->del(database, NULL, &key, 0)) == 0)
226 afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
228 afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
233 static void verb_read(struct afb_req req)
241 struct json_object* result;
242 struct json_object* val;
245 if (get_key(req, &key))
248 AFB_INFO("read: key=%s", (char*)key.data);
252 data.flags = DB_DBT_USERMEM;
254 if ((ret = database->get(database, NULL, &key, &data, 0)) == 0)
256 result = json_object_new_object();
257 val = json_tokener_parse((char*)data.data);
258 json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));
260 afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
263 afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
268 // ----- Binding's configuration -----
270 static const struct afb_auth ll_database_binding_auths[] = {
274 static const afb_verb_v2 ll_database_binding_verbs[]= {
275 REGISTER_VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
276 REGISTER_VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
277 REGISTER_VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
278 REGISTER_VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
282 const struct afb_binding_v2 afbBindingV2 = {
283 .api = "ll-database",
284 .specification = NULL,
285 .verbs = ll_database_binding_verbs,
287 .init = ll_database_binding_init,