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)
46 #define USERNAME "agl"
47 #define APPNAME "firefox"
49 // ----- Globals -----
51 static char database_file[MAX_PATH];
53 // ----- Binding's declarations -----
54 static int ll_database_binding_init();
55 static void verb_insert(struct afb_req req);
56 static void verb_update(struct afb_req req);
57 static void verb_delete(struct afb_req req);
58 static void verb_read(struct afb_req req);
60 // ----- Binding's implementations -----
63 * @brief Initialize the binding.
64 * @return Exit code, zero if success.
66 static int ll_database_binding_init()
69 struct passwd* result;
74 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
75 if (bufsize == -1 || bufsize > MAX_PATH) bufsize = MAX_PATH;
77 ret = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
80 if (ret == 0) AFB_ERROR("User not found");
81 else AFB_ERROR("getpwuid_r failed with %d code", ret);
85 memset(database_file, 0, MAX_PATH);
86 strcat(database_file, result->pw_dir);
87 strcat(database_file, DBFILE);
89 AFB_INFO("The database file is '%s'", database_file);
91 if ((ret = db_create(&database, NULL, 0)) != 0)
93 AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
97 if ((ret = database->open(database, NULL, database_file, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
99 AFB_ERROR("Failed to open the '%s' database: %s.", database_file, db_strerror(ret));
100 database->close(database, 0);
108 * Returns the database key for the 'req'
110 static int get_key(struct afb_req req, DBT *key)
115 size_t ljkey, lappid, size;
117 struct json_object* args;
118 struct json_object* item;
121 args = afb_req_json(req);
122 if (!json_object_object_get_ex(args, "key", &item))
124 afb_req_fail(req, "no-key", NULL);
128 || !(jkey = json_object_get_string(item))
129 || !(ljkey = strlen(jkey)))
131 afb_req_fail(req, "bad-key", NULL);
136 appid = afb_req_get_application_id(req);
139 afb_req_fail(req, "bad-context", NULL);
143 /* make the db-key */
144 lappid = strlen(appid);
145 size = lappid + ljkey + 2;
146 data = realloc(appid, size);
150 afb_req_fail(req, "out-of-memory", NULL);
154 memcpy(&data[lappid + 1], jkey, ljkey + 1);
158 key->size = (uint32_t)size;
162 static void put(struct afb_req req, int replace)
170 struct json_object* args;
171 struct json_object* item;
173 args = afb_req_json(req);
175 if (!json_object_object_get_ex(args, "value", &item))
177 afb_req_fail(req, "no-value", NULL);
181 value = json_object_to_json_string_ext(item, TO_STRING_FLAGS);
184 afb_req_fail(req, "out-of-memory", NULL);
188 if (get_key(req, &key))
191 AFB_INFO("put: key=%s, value=%s", (char*)key.data, value);
193 data.data = (void*)value;
194 data.size = (uint32_t)strlen(value);
196 ret = database->put(database, NULL, &key, &data, replace ? 0 : DB_NOOVERWRITE);
198 afb_req_success(req, NULL, NULL);
201 AFB_ERROR("can't %s key %s with %s", replace ? "replace" : "insert", (char*)key.data, (char*)data.data);
202 afb_req_fail_f(req, "failed", "%s", db_strerror(ret));
207 static void verb_insert(struct afb_req req)
212 static void verb_update(struct afb_req req)
217 static void verb_delete(struct afb_req req)
222 if (get_key(req, &key))
225 AFB_INFO("delete: key=%s", (char*)key.data);
227 if ((ret = database->del(database, NULL, &key, 0)) == 0)
228 afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
230 afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
235 static void verb_read(struct afb_req req)
243 struct json_object* result;
244 struct json_object* val;
247 if (get_key(req, &key))
250 AFB_INFO("read: key=%s", (char*)key.data);
254 data.flags = DB_DBT_USERMEM;
256 if ((ret = database->get(database, NULL, &key, &data, 0)) == 0)
258 result = json_object_new_object();
259 val = json_tokener_parse((char*)data.data);
260 json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));
262 afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
265 afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
270 // ----- Binding's configuration -----
272 static const struct afb_auth ll_database_binding_auths[] = {
276 static const afb_verb_v2 ll_database_binding_verbs[]= {
277 REGISTER_VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
278 REGISTER_VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
279 REGISTER_VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
280 REGISTER_VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
284 const struct afb_binding_v2 afbBindingV2 = {
285 .api = "ll-database",
286 .specification = NULL,
287 .verbs = ll_database_binding_verbs,
289 .init = ll_database_binding_init,