1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License a
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26 #include "ocpayload.h"
28 #include "oic_malloc.h"
29 #include "oic_string.h"
33 #define TAG "OIC_RD_DATABASE"
34 #define RD_PATH "RD.db"
36 static sqlite3 *gRDDB = NULL;
38 static const uint8_t device_index = 2;
39 static const uint8_t ttl_index = 3;
40 static const uint8_t address_index = 4;
42 static const uint8_t uri_index = 2;
43 static const uint8_t p_index = 5;
44 static const uint8_t mt_index = 7;
45 static const uint8_t d_index = 8;
47 static const uint8_t rt_value_index = 1;
48 static const uint8_t rt_link_id_index = 2;
50 static const uint8_t if_value_index = 1;
51 static const uint8_t if_link_id_index = 2;
53 #define VERIFY_SQLITE(arg) \
54 if (SQLITE_OK != (arg)) \
56 OIC_LOG_V(ERROR, TAG, "Error in " #arg ", Error Message: %s", sqlite3_errmsg(gRDDB)); \
57 sqlite3_exec(gRDDB, "ROLLBACK", NULL, NULL, NULL); \
58 return OC_STACK_ERROR; \
61 #define CHECK_DATABASE_INIT \
64 OIC_LOG(ERROR, TAG, "Database is not initialized."); \
65 return OC_STACK_ERROR; \
69 #define XSTR(a) STR(a)
72 "create table RD_DEVICE_LIST(ID INTEGER PRIMARY KEY AUTOINCREMENT, " \
73 XSTR(OC_RSRVD_DEVICE_ID) " UNIQUE NOT NULL, " \
74 XSTR(OC_RSRVD_TTL) " NOT NULL, " \
78 "create table RD_DEVICE_LINK_LIST("XSTR(OC_RSRVD_INS)" INTEGER PRIMARY KEY AUTOINCREMENT, " \
79 XSTR(OC_RSRVD_HREF) "," \
80 XSTR(OC_RSRVD_REL) "," \
81 XSTR(OC_RSRVD_TITLE) "," \
82 XSTR(OC_RSRVD_BITMAP)"," \
83 XSTR(OC_RSRVD_TTL) "," \
84 XSTR(OC_RSRVD_MEDIA_TYPE) "," \
85 "DEVICE_ID INT NOT NULL, " \
86 "FOREIGN KEY(DEVICE_ID) REFERENCES RD_DEVICE_LIST(ID) ON DELETE CASCADE );"
89 "create table RD_LINK_RT(" XSTR(OC_RSRVD_RESOURCE_TYPE) " NOT NULL, " \
90 "LINK_ID INT NOT NULL, "\
91 "FOREIGN KEY("XSTR(LINK_ID)") REFERENCES RD_DEVICE_LINK_LIST("XSTR(OC_RSRVD_INS)") " \
95 "create table RD_LINK_IF(" XSTR(OC_RSRVD_INTERFACE) " NOT NULL, " \
96 "LINK_ID INT NOT NULL, "\
97 "FOREIGN KEY("XSTR(LINK_ID)") REFERENCES RD_DEVICE_LINK_LIST("XSTR(OC_RSRVD_INS)") " \
100 static void errorCallback(void *arg, int errCode, const char *errMsg)
105 OIC_LOG_V(ERROR, TAG, "SQLLite Error: %s : %d", errMsg, errCode);
108 OCStackResult OCRDDatabaseInit(const char *path)
110 if (SQLITE_OK == sqlite3_config(SQLITE_CONFIG_LOG, errorCallback))
112 OIC_LOG_V(INFO, TAG, "SQLite debugging log initialized.");
116 sqlRet = sqlite3_open_v2(!path ? RD_PATH : path, &gRDDB, SQLITE_OPEN_READWRITE, NULL);
117 if (SQLITE_OK != sqlRet)
119 OIC_LOG(DEBUG, TAG, "RD database file did not open, as no table exists.");
120 OIC_LOG(DEBUG, TAG, "RD creating new table.");
121 sqlRet = sqlite3_open_v2(!path ? RD_PATH : path, &gRDDB,
122 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
123 if (SQLITE_OK == sqlRet)
125 VERIFY_SQLITE(sqlite3_exec(gRDDB, RD_TABLE, NULL, NULL, NULL));
126 OIC_LOG(DEBUG, TAG, "RD created RD_DEVICE_LIST table.");
128 VERIFY_SQLITE(sqlite3_exec(gRDDB, RD_LL_TABLE, NULL, NULL, NULL));
129 OIC_LOG(DEBUG, TAG, "RD created RD_DEVICE_LINK_LIST table.");
131 VERIFY_SQLITE(sqlite3_exec(gRDDB, RD_RT_TABLE, NULL, NULL, NULL));
132 OIC_LOG(DEBUG, TAG, "RD created RD_LINK_RT table.");
134 VERIFY_SQLITE(sqlite3_exec(gRDDB, RD_IF_TABLE, NULL, NULL, NULL));
135 OIC_LOG(DEBUG, TAG, "RD created RD_LINK_IF table.");
140 if (sqlRet == SQLITE_OK)
142 sqlite3_stmt *stmt = 0;
143 VERIFY_SQLITE(sqlite3_prepare_v2 (gRDDB, "PRAGMA foreign_keys = ON;", -1, &stmt, NULL));
145 if (SQLITE_DONE != sqlite3_step(stmt))
147 sqlite3_finalize(stmt);
148 return OC_STACK_ERROR;
151 VERIFY_SQLITE(sqlite3_finalize(stmt));
157 OCStackResult OCRDDatabaseClose()
160 VERIFY_SQLITE(sqlite3_close_v2(gRDDB));
164 static int storeResourceType(char **link, size_t size, uint8_t rowid)
167 VERIFY_SQLITE(sqlite3_exec(gRDDB, "BEGIN TRANSACTION", NULL, NULL, NULL));
169 const char *insertRT = "INSERT INTO RD_LINK_RT VALUES(?, ?)";
170 sqlite3_stmt *stmtRT = 0;
172 for (size_t i = 0; i < size; i++)
174 VERIFY_SQLITE(sqlite3_prepare_v2(gRDDB, insertRT, strlen(insertRT) + 1, &stmtRT, NULL));
177 VERIFY_SQLITE(sqlite3_bind_text(stmtRT, rt_value_index, link[i],
178 strlen(link[i]), SQLITE_STATIC));
180 VERIFY_SQLITE(sqlite3_bind_int(stmtRT, rt_link_id_index, rowid));
182 if (sqlite3_step(stmtRT) != SQLITE_DONE)
184 sqlite3_exec(gRDDB, "ROLLBACK", NULL, NULL, NULL);
185 sqlite3_finalize(stmtRT);
190 VERIFY_SQLITE(sqlite3_finalize(stmtRT));
192 VERIFY_SQLITE(sqlite3_exec(gRDDB, "COMMIT", NULL, NULL, NULL));
199 static int storeInterfaceType(char **link, size_t size, uint8_t rowid)
203 VERIFY_SQLITE(sqlite3_exec(gRDDB, "BEGIN TRANSACTION", NULL, NULL, NULL));
205 const char *insertIF = "INSERT INTO RD_LINK_IF VALUES(?, ?)";
206 sqlite3_stmt *stmtIF = 0;
208 for (size_t i = 0; i < size; i++)
210 VERIFY_SQLITE(sqlite3_prepare_v2(gRDDB, insertIF, strlen(insertIF) + 1, &stmtIF, NULL));
214 VERIFY_SQLITE(sqlite3_bind_text(stmtIF, if_value_index, link[i], strlen(link[i]), SQLITE_STATIC));
215 VERIFY_SQLITE(sqlite3_bind_int(stmtIF, if_link_id_index, rowid));
217 if (sqlite3_step(stmtIF) != SQLITE_DONE)
219 sqlite3_exec(gRDDB, "ROLLBACK", NULL, NULL, NULL);
220 res = sqlite3_finalize(stmtIF);
224 VERIFY_SQLITE(sqlite3_finalize(stmtIF));
226 VERIFY_SQLITE(sqlite3_exec(gRDDB, "COMMIT", NULL, NULL, NULL));
232 static int storeLinkPayload(OCRepPayload *rdPayload, int64_t rowid)
238 * Iterate over the properties manually rather than OCRepPayloadGetPropObjectArray to avoid
239 * the clone since we want to insert the 'ins' values into the payload.
241 OCRepPayloadValue *links;
242 for (links = rdPayload->values; links; links = links->next)
244 if (0 == strcmp(links->name, OC_RSRVD_LINKS))
246 if (links->type != OCREP_PROP_ARRAY || links->arr.type != OCREP_PROP_OBJECT)
255 const char *insertDeviceLLList = "INSERT INTO RD_DEVICE_LINK_LIST VALUES(?,?,?,?,?,?,?,?)";
256 sqlite3_stmt *stmt = 0;
258 for (size_t i = 0; i < links->arr.dimensions[0]; i++)
260 VERIFY_SQLITE(sqlite3_prepare_v2(gRDDB, insertDeviceLLList, strlen(insertDeviceLLList) + 1, &stmt, NULL));
261 VERIFY_SQLITE(sqlite3_exec(gRDDB, "BEGIN TRANSACTION", NULL, NULL, NULL));
263 OCRepPayload *link = links->arr.objArray[i];
265 if (OCRepPayloadGetPropString(link, OC_RSRVD_HREF, &uri))
267 VERIFY_SQLITE(sqlite3_bind_text(stmt, uri_index, uri, strlen(uri), SQLITE_STATIC));
270 OCRepPayload *p = NULL;
271 if (OCRepPayloadGetPropObject(link, OC_RSRVD_POLICY, &p))
274 if (OCRepPayloadGetPropInt(p, OC_RSRVD_BITMAP, &bm))
276 VERIFY_SQLITE(sqlite3_bind_int(stmt, p_index, bm));
280 size_t mtDim[MAX_REP_ARRAY_DEPTH] = {0};
281 char **mediaType = NULL;
282 if (OCRepPayloadGetStringArray(link, OC_RSRVD_MEDIA_TYPE, &mediaType, mtDim))
284 VERIFY_SQLITE(sqlite3_bind_text(stmt, mt_index, mediaType[0], strlen(mediaType[0]), SQLITE_STATIC));
287 VERIFY_SQLITE(sqlite3_bind_int(stmt, d_index, rowid));
289 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {0};
291 OCRepPayloadGetStringArray(link, OC_RSRVD_RESOURCE_TYPE, &rt, rtDim);
293 size_t itfDim[MAX_REP_ARRAY_DEPTH] = {0};
295 OCRepPayloadGetStringArray(link, OC_RSRVD_INTERFACE, &itf, itfDim);
297 if (sqlite3_step(stmt) != SQLITE_DONE)
299 sqlite3_exec(gRDDB, "ROLLBACK", NULL, NULL, NULL);
300 sqlite3_finalize(stmt);
303 VERIFY_SQLITE(sqlite3_exec(gRDDB, "COMMIT", NULL, NULL, NULL));
305 int64_t ins = sqlite3_last_insert_rowid(gRDDB);
306 if (!OCRepPayloadSetPropInt(link, OC_RSRVD_INS, ins))
308 OIC_LOG_V(ERROR, TAG, "Error setting 'ins' value");
309 return OC_STACK_ERROR;
311 VERIFY_SQLITE(storeResourceType(rt, rtDim[0], ins));
312 VERIFY_SQLITE(storeInterfaceType(itf, itfDim[0], ins));
314 OCPayloadDestroy((OCPayload *)p);
315 for (j = 0; j < mtDim[0]; j++)
317 OICFree(mediaType[j]);
321 for (j = 0; j < rtDim[0]; j++)
327 for (j = 0; j < itfDim[0]; j++)
335 VERIFY_SQLITE(sqlite3_finalize(stmt));
341 OCStackResult OCRDDatabaseStoreResources(OCRepPayload *payload, const OCDevAddr *address)
345 VERIFY_SQLITE(sqlite3_exec(gRDDB, "BEGIN TRANSACTION", NULL, NULL, NULL));
346 const char *insertDeviceList = "INSERT INTO RD_DEVICE_LIST VALUES(?,?,?,?)";
347 sqlite3_stmt *stmt = 0;
348 VERIFY_SQLITE(sqlite3_prepare_v2(gRDDB, insertDeviceList, strlen(insertDeviceList) + 1, &stmt, NULL));
350 char *deviceid = NULL;
351 if (OCRepPayloadGetPropString(payload, OC_RSRVD_DEVICE_ID, &deviceid))
353 VERIFY_SQLITE(sqlite3_bind_text(stmt, device_index, deviceid, strlen(deviceid), SQLITE_STATIC));
357 if (OCRepPayloadGetPropInt(payload, OC_RSRVD_DEVICE_TTL, &ttl))
359 VERIFY_SQLITE(sqlite3_bind_int(stmt, ttl_index, ttl));
362 char rdAddress[MAX_URI_LENGTH];
363 snprintf(rdAddress, MAX_URI_LENGTH, "%s:%d", address->addr, address->port);
364 OIC_LOG_V(DEBUG, TAG, "Address: %s", rdAddress);
365 VERIFY_SQLITE(sqlite3_bind_text(stmt, address_index, rdAddress, strlen(rdAddress), SQLITE_STATIC));
367 if (sqlite3_step(stmt) != SQLITE_DONE)
369 sqlite3_exec(gRDDB, "ROLLBACK", NULL, NULL, NULL);
370 sqlite3_finalize(stmt);
371 return OC_STACK_ERROR;
373 VERIFY_SQLITE(sqlite3_finalize(stmt));
375 VERIFY_SQLITE(sqlite3_exec(gRDDB, "COMMIT", NULL, NULL, NULL));
377 int64_t rowid = sqlite3_last_insert_rowid(gRDDB);
380 VERIFY_SQLITE(storeLinkPayload(payload, rowid));
386 OCStackResult OCRDDatabaseDeleteResources(const char *deviceId, const uint8_t *instanceIds, uint8_t nInstanceIds)
390 VERIFY_SQLITE(sqlite3_exec(gRDDB, "BEGIN TRANSACTION", NULL, NULL, NULL));
391 sqlite3_stmt *stmt = 0;
392 if (!instanceIds || !nInstanceIds)
394 char *delDevice = "DELETE FROM RD_DEVICE_LIST WHERE "XSTR(OC_RSRVD_DEVICE_ID)" = ?";
395 VERIFY_SQLITE(sqlite3_prepare_v2(gRDDB, delDevice, strlen(delDevice) + 1, &stmt, NULL));
397 VERIFY_SQLITE(sqlite3_bind_text(stmt, 1, deviceId, strlen(deviceId), SQLITE_STATIC));
401 const char pre[] = "DELETE FROM RD_DEVICE_LINK_LIST "
403 "SELECT RD_DEVICE_LINK_LIST.ins FROM RD_DEVICE_LINK_LIST "
404 "INNER JOIN RD_DEVICE_LIST ON RD_DEVICE_LINK_LIST.DEVICE_ID=RD_DEVICE_LIST.ID "
405 "WHERE RD_DEVICE_LIST.di = ? AND RD_DEVICE_LINK_LIST.ins IN (";
406 size_t inLen = nInstanceIds + (nInstanceIds - 1);
407 const char post[] = "))";
408 size_t len = sizeof(pre) + inLen + sizeof(post);
409 char *delResource = OICCalloc(len, 1);
412 OIC_LOG(ERROR, TAG, "SQL query is NULL");
413 return OC_STACK_ERROR;
415 OICStrcat(delResource, len, pre);
416 OICStrcat(delResource, len, "?");
417 for (uint8_t i = 1; i < nInstanceIds; ++i)
419 OICStrcat(delResource, len, ",?");
421 OICStrcat(delResource, len, post);
422 int prepareResult = sqlite3_prepare_v2(gRDDB, delResource, strlen(delResource) + 1, &stmt, NULL);
423 OICFree(delResource);
424 VERIFY_SQLITE(prepareResult);
426 VERIFY_SQLITE(sqlite3_bind_text(stmt, 1, deviceId, strlen(deviceId), SQLITE_STATIC));
427 for (uint8_t i = 0; i < nInstanceIds; ++i)
429 VERIFY_SQLITE(sqlite3_bind_int(stmt, 2 + i, instanceIds[i]));
432 if (sqlite3_step(stmt) != SQLITE_DONE)
434 sqlite3_exec(gRDDB, "ROLLBACK", NULL, NULL, NULL);
435 sqlite3_finalize(stmt);
436 return OC_STACK_ERROR;
438 VERIFY_SQLITE(sqlite3_finalize(stmt));
439 VERIFY_SQLITE(sqlite3_exec(gRDDB, "COMMIT", NULL, NULL, NULL));