From 715e5666dcb9fe8a0e7ac351658d4c359fdb9354 Mon Sep 17 00:00:00 2001 From: ihuerner Date: Thu, 9 Aug 2012 11:05:42 +0000 Subject: [PATCH 1/1] Initial version of persistence client library on sourceforge (rev. 1281) --- AUTHORS | 0 COPYING | 12 + ChangeLog | 0 Makefile.am | 12 + NEWS | 0 README | 0 configure.ac | 83 +++++ data/Data.tar.gz | Bin 0 -> 2026 bytes include/persistence_client_library_file.h | 178 +++++++++++ include/persistence_client_library_key.h | 196 ++++++++++++ persistence_client_library.pc.in | 13 + src/Makefile.am | 30 ++ src/crc32.c | 135 ++++++++ src/crc32.h | 42 +++ src/persistence_client_library.c | 291 ++++++++++++++++++ src/persistence_client_library.h | 213 +++++++++++++ src/persistence_client_library_data_access.c | 229 ++++++++++++++ src/persistence_client_library_data_access.h | 135 ++++++++ src/persistence_client_library_dbus_service.c | 407 +++++++++++++++++++++++++ src/persistence_client_library_dbus_service.h | 74 +++++ src/persistence_client_library_file.c | 213 +++++++++++++ src/persistence_client_library_handle.c | 73 +++++ src/persistence_client_library_handle.h | 63 ++++ src/persistence_client_library_key.c | 278 +++++++++++++++++ src/persistence_client_library_lc_interface.c | 351 +++++++++++++++++++++ src/persistence_client_library_lc_interface.h | 50 +++ src/persistence_client_library_pas_interface.c | 298 ++++++++++++++++++ src/persistence_client_library_pas_interface.h | 71 +++++ test/Makefile.am | 15 + test/persistence_client_library_dbus_test.c | 31 ++ test/persistence_client_library_test.c | 259 ++++++++++++++++ 31 files changed, 3752 insertions(+) create mode 100644 AUTHORS create mode 100644 COPYING create mode 100644 ChangeLog create mode 100644 Makefile.am create mode 100644 NEWS create mode 100644 README create mode 100644 configure.ac create mode 100644 data/Data.tar.gz create mode 100644 include/persistence_client_library_file.h create mode 100644 include/persistence_client_library_key.h create mode 100644 persistence_client_library.pc.in create mode 100644 src/Makefile.am create mode 100644 src/crc32.c create mode 100644 src/crc32.h create mode 100644 src/persistence_client_library.c create mode 100644 src/persistence_client_library.h create mode 100644 src/persistence_client_library_data_access.c create mode 100644 src/persistence_client_library_data_access.h create mode 100644 src/persistence_client_library_dbus_service.c create mode 100644 src/persistence_client_library_dbus_service.h create mode 100644 src/persistence_client_library_file.c create mode 100644 src/persistence_client_library_handle.c create mode 100644 src/persistence_client_library_handle.h create mode 100644 src/persistence_client_library_key.c create mode 100644 src/persistence_client_library_lc_interface.c create mode 100644 src/persistence_client_library_lc_interface.h create mode 100644 src/persistence_client_library_pas_interface.c create mode 100644 src/persistence_client_library_pas_interface.h create mode 100644 test/Makefile.am create mode 100644 test/persistence_client_library_dbus_test.c create mode 100644 test/persistence_client_library_test.c diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e69de29 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..755c92e --- /dev/null +++ b/COPYING @@ -0,0 +1,12 @@ +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..4d8d24d --- /dev/null +++ b/Makefile.am @@ -0,0 +1,12 @@ +SUBDIRS=src + +if WANT_TESTS +SUBDIRS+=test +endif + +#pkgconfigdir=$(libdir)/pkgconfig +#pkgconfig_DATA=persistence_clientlib.pc + +MAINTAINERCLEANFILES = \ + Makefile.in aclocal.m4 compile config.guess config.h.in config.sub \ + configure depcomp install-sh ltmain.sh persistence_clientlib.pc.in config.h.in~ Makefile missing diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e69de29 diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..2fe5356 --- /dev/null +++ b/configure.ac @@ -0,0 +1,83 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_INIT([Persistence Client Library], [0.1], [not-valid@xse.de]) +AC_CONFIG_SRCDIR([Makefile.am]) +AM_INIT_AUTOMAKE([dist-bzip2]) +AC_GNU_SOURCE() + +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +# Checks for programs. +AC_PROG_CC +AM_PROG_CC_C_O +AC_PROG_LIBTOOL +PKG_PROG_PKG_CONFIG + +AC_SUBST(CFLAGS) +AC_SUBST(CPPFLAGS) +AC_SUBST(LDFLAGS) + + +# Checks for header files. +AC_CHECK_HEADERS([fcntl.h limits.h stdlib.h string.h unistd.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_TYPE_UID_T +AC_TYPE_MODE_T +AC_TYPE_OFF_T +AC_TYPE_SIZE_T +AC_TYPE_UINT32_T +AC_TYPE_UINT64_T + +AC_ARG_ENABLE([tests], + [AS_HELP_STRING([--enable-tests],[Enable tests])], + [enable_tests=$enableval],[enable_tests="no"]) + +AM_CONDITIONAL([WANT_TESTS], [test x"$enable_tests" = "xyes"]) + + +# Checks for library functions. +AC_FUNC_CHOWN +AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK +AC_FUNC_MMAP +AC_CHECK_FUNCS([fdatasync ftruncate mkdir munmap rmdir strerror utime dlopen]) + +PKG_CHECK_MODULES(DEPS, + automotive-dlt + dbus-1 + dconf + glib-2.0) + +AC_SUBST(DEPS_CFLAGS) +AC_SUBST(DEPS_LIBS) + +AC_ARG_WITH([localcheck], + [AS_HELP_STRING([--with-localcheck],[Path to local check])], + [localcheck=$withval],[localcheck=""]) + +if test "x$enable_tests" != "xno" ; then + if test -z "$localcheck" ; then + PKG_CHECK_MODULES(CHECK, check) + else + CHECK_CFLAGS="-I$localcheck/include" + CHECK_LIBS="-L$localcheck/lib -lcheck" + fi + AC_SUBST(CHECK_CFLAGS) + AC_SUBST(CHECK_LIBS) + + AC_DEFINE_UNQUOTED([TESTS_ENABLED], [1], [tests enabled]) +else + AC_DEFINE_UNQUOTED([TESTS_ENABLED], [0], [tests enabled]) +fi +AC_MSG_NOTICE([Tests enabled: $enable_tests]) +AC_MSG_NOTICE([Local check enabled: $localcheck]) + +#AC_CONFIG_FILES([Makefile +# persistence_client_library.pc +# src/Makefile +# test/Makefile]) +AC_CONFIG_FILES([Makefile + src/Makefile + test/Makefile]) +AC_OUTPUT diff --git a/data/Data.tar.gz b/data/Data.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..c8d11a23357715fdbca304ff4be5794d7319102b GIT binary patch literal 2026 zcmZvYdmxl)8^*`^(99}HI?hyzvgEKUwelig>60D8Rt{-Z#$nNHGFkGDA=wD!LyqH6 z4sB+#DbiucmQ;4e%CRyeMmr9*WKQqA_m2Jk{Jy{L=eeHi`rY?)lQVP)bq2a#1dZt+ zkMy;EM$?@gZqHm(Tu95FFQZcmXXO0{(sg&$j_6#w5Jo z1EQMxgfTuZDNrBhc*KXX;{AlsF6cFDn%I8#Drz_{LyRr%6WpYX1M6fA%VJqgYb%y* zj9J%OH0!5M@FLZrdWn52w)_|qLLKj!q4!dm;#JJDuJ>#oXYCO5prBhhIpI0>zuGn z&hN7<1JlD@5W?UL9`29u%0yp6RJ%x+cujIDysliy)CH?(+GtMQjggT z#VuWPY)xA%V+-LOZh+l}T<0|OA6q=`<8lMfRu4Y0^>7$o+>A%=vJ%3EWM3t<4dJ7P z?!91y1-wMGuKkJXN#4QNr?c%lXsg@c=ou$63;h^WC7nCAQfdQuvgB_}G` z=nCg_wB1A6GywZ4d=&R1*x;rAK~?L@5h1XRX#$;cgUqX_{{6BO18`_8I!YB76qz81vkK)0#S9~sT7R;-_V4r z&VttzBtn5o`QP zx12zSCgM@J?xKdDHe1}?oT|G^vIJETAHg1$`Xc%$n5o`-<%?wVR-Ql5@cmvRb4Y}e zJqtN1j_S}4`@NXx9h@&i^z=V+`=AgtG?HR9{;FT_7E=d7Bt$FzheKJwvyao`lj@wz z&Js-r&ZkCeLfZIG4~6LIb(>_kPLK|MW;6G!mc0W|A5L8$@zzP!3w5IShiyxm?J`E& zJ&(L5PG=?GB@H1f7iL=2TH!vg(YJ_&+k^$gK@w@qAxZb$5&=r^gwK42b=KJ1rHGD_ zA=pyA{ZSfy?QF7?ESP1cIGq~p@GQD71CJ@$h!te3wKls)`WY5R0LO#w^fk%Dcv!|%);yb)JlKKzk3@x1eM zc29-x#Xmf6#xAeT2tj7{=KUTb;PicSC)#4Nbny8Vva6$?AmT*~L%HSKhJ3OPHFk(T z5T3tMe1Trz6F1x1wO8Tp8!q?0Ichy<;B-T~V-5YJ)l_WOSJXxYwVfmV{F8~B_sNPL z{TqWR^U|#2g=Gt|Or2EPk!(pgH`aK_d#&8tC@kORV@Od_!Xq(XC(nNeaXIOk`YkqX zwReB8ZV~AXF8g+O?U!+X)EqwVvMD-tsPogpkk3kfcp9D_>X}zl`sv)zQR0BTX7!7f zZ-$XFn|W-3f5@=DrhCP{R~sGTyFxXZ&0Pjh_0+vsxc4Wqva2;;pZ026%SL%YT-QU2 zY&$W<|2m~}XjJBybSn)POG{^WhHo}Lxa#O~>-J2hyq z2GaQBA^Y!%8SHp@uz;9keWN`931IXmqNnR9RioWtBR%CJ|Tm zFD-$CTIFs^ntvc%W{MFGXio5a)y%yi-l`up9g$}*YW7y9POv>tgL56KkJH4k{u=9p z1UajU9J`7a*eZo(Pwt+mFOXDB#PI#8w>=6&k8^g|-M!gnS7^4Dl@#u*D6pMI$#ahz zPDVN9M#0zf!Ys>3$1LwLbWiYL>S@ zEYJ;v)5`4*aDG> 8); + } + rval = crc ^ ~0U; + } + + return rval; +} + + + + diff --git a/src/crc32.h b/src/crc32.h new file mode 100644 index 0000000..ea785a3 --- /dev/null +++ b/src/crc32.h @@ -0,0 +1,42 @@ +#ifndef CRC32_H +#define CRC32_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file crc32.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Header of crc32 checksum generation + * @see + */ + + +#include + +const unsigned int crc32(unsigned int crc, const unsigned char *buf, size_t theSize); + + +#endif /* CRC32_H */ diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c new file mode 100644 index 0000000..a732ae6 --- /dev/null +++ b/src/persistence_client_library.c @@ -0,0 +1,291 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIcacheON WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library. + * Library provides an API to access persistent data + * @see + */ + + +#include "persistence_client_library.h" +#include "persistence_client_library_lc_interface.h" +#include "persistence_client_library_pas_interface.h" +#include "persistence_client_library_dbus_service.h" +#include "persistence_client_library_handle.h" + +#include +#include +#include +#include + +#include +#include + + +/// pointer to resource table database +static GvdbTable* gResource_table = NULL; + +/// debug log and trace (DLT) setup +DLT_DECLARE_CONTEXT(persClientLibCtx); + +/// library constructor +void pers_library_init(void) __attribute__((constructor)); +/// library deconstructor +void pers_library_destroy(void) __attribute__((destructor)); + + + + + +void pers_library_init(void) +{ + DLT_REGISTER_APP("Persistence Client Library","persClientLib"); + DLT_REGISTER_CONTEXT(persClientLibCtx,"persClientLib","Context for Logging"); + + DLT_LOG(persClientLibCtx, DLT_LOG_ERROR, DLT_STRING("Initialize Persistence Client Library!!!!")); + + setup_dbus_mainloop(); + + // register for lifecycle and persistence admin service dbus messages + register_lifecycle(); + register_pers_admin_service(); + + // clear the open file descriptor array + memset(gOpenFdArray, maxPersHandle, sizeof(int)); + + printf("A p p l i c a t i o n n a m e : %s \n", program_invocation_short_name); // TODO: only temp solution for application name + strncpy(gAppId, program_invocation_short_name, maxAppNameLen); +} + + + +void pers_library_destroy(void) +{ + // dereference opend database + if(gResource_table != NULL) + { + gvdb_table_unref(gResource_table); + } + + // unregister for lifecycle and persistence admin service dbus messages + unregister_lifecycle(); + unregister_pers_admin_service(); + + + DLT_UNREGISTER_CONTEXT(persClientLibCtx); + DLT_UNREGISTER_APP(); + dlt_free(); +} + + + +// status: OK +int get_db_context(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no, + unsigned int isFile, char dbKey[], char dbPath[]) +{ + unsigned char cached = 0; + GVariant* dbValue = NULL; + // get resource configuration table + GvdbTable* resource_table = get_resource_cfg_table(); + + // check if resouce id is in write through table + dbValue = gvdb_table_get_value(resource_table, resource_id); + if(dbValue != NULL) + { + cached = storeWt; // it's a write through value + } + else + { + cached = storeCached; // must be a cached value + } + return get_db_path_and_key(ldbid, resource_id, user_no, seat_no, isFile, dbKey, dbPath, cached); +} + + + +// status: OK +int get_db_path_and_key(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no, + unsigned int isFile, char dbKey[], char dbPath[], unsigned char cached_resource) +{ + int rval = -1; + + // + // create resource database key + // + if((ldbid < 0x80) || (ldbid == 0xFF) ) + { + // The LDBID is used to find the DBID in the resource table. + if((user_no == 0) && (seat_no == 0)) + { + // Node is added in front of the resource ID as the key string. + snprintf(dbKey, dbKeyMaxLen, "%s%s", gNode, resource_id); + rval = 0; + } + else + { + if(seat_no == 0) + { + // /User/ is added in front of the resource ID as the key string. + snprintf(dbKey, dbKeyMaxLen, "%s%d%s", gUser, user_no, resource_id); + rval = 0; + } + else + { + // /User//Seat/ is added in front of the resource ID as the key string. + snprintf(dbKey, dbKeyMaxLen, "%s%d%s%d%s", gUser, user_no, gSeat, seat_no, resource_id); + rval = 0; + } + } + } + + if((ldbid >= 0x80) && ( ldbid != 0xFF)) + { + // The LDBID is used to find the DBID in the resource table. + // / is added in front of the resource ID as the key string. + // Rational: Creates a namespace within one data base. + // Rational: Reduction of number of databases -> reduction of maintenance costs + // /User/ and /Seat/ are add after / if there are different than 0. + + if(seat_no != 0) + { + snprintf(dbKey, dbKeyMaxLen, "/%x%s%d%s%d%s", ldbid, gUser, user_no, gSeat, seat_no, resource_id); + } + else + { + snprintf(dbKey, dbKeyMaxLen, "/%x%s%d%s", ldbid, gUser, user_no, resource_id); + } + rval = 0; + } + + + // + // create resource database path + // + if(ldbid < 0x80) + { + // ------------------------------------- + // shared database + // ------------------------------------- + + if(ldbid != 0) + { + // Additionally /GROUP/ shall be added inside of the database path listed in the resource table. (Off target) + // Rational: To ensure data separation using the Linux user right policy (different data base files in different locations). + + if(storeCached == cached_resource) + { + if(isFile == resIsNoFile) + snprintf(dbPath, dbPathMaxLen, gSharedCachePath, ldbid, gSharedCached); + else + snprintf(dbPath, dbPathMaxLen, gSharedCachePath, ldbid, dbKey); + } + else if(storeWt == cached_resource) + { + if(isFile == resIsNoFile) + snprintf(dbPath, dbPathMaxLen, gSharedWtPath, ldbid, gSharedWt); + else + snprintf(dbPath, dbPathMaxLen, gSharedWtPath, ldbid, dbKey); + } + } + else + { + // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target) + + if(storeCached == cached_resource) + { + if(isFile == resIsNoFile) + snprintf(dbPath, dbPathMaxLen, gSharedPublicCachePath, gSharedCached); + else + snprintf(dbPath, dbPathMaxLen, gSharedPublicCachePath, dbKey); + } + else if(storeWt == cached_resource) + { + if(isFile == resIsNoFile) + snprintf(dbPath, dbPathMaxLen, gSharedPublicWtPath, gSharedWt); + else + snprintf(dbPath, dbPathMaxLen, gSharedPublicWtPath, dbKey); + } + } + + rval = dbShared; // we have a shared database + } + else + { + // ------------------------------------- + // local database + // ------------------------------------- + + if(storeCached == cached_resource) + { + if(isFile == resIsNoFile) + snprintf(dbPath, dbPathMaxLen, gLocalCachePath, gAppId, gLocalCached); + else + snprintf(dbPath, dbPathMaxLen, gLocalCachePath, gAppId, dbKey); + } + else if(storeWt == cached_resource) + { + if(isFile == resIsNoFile) + snprintf(dbPath, dbPathMaxLen, gLocalWtPath, gAppId, gLocalWt); + else + snprintf(dbPath, dbPathMaxLen, gLocalWtPath, gAppId, dbKey); + } + + rval = dbLocal; // we have a local database + } + + printf("dbKey : [key ]: %s \n", dbKey); + printf("dbPath : [path]: %s\n\n", dbPath); + + return rval; +} + + +// status: OK +GvdbTable* get_resource_cfg_table() +{ + if(gResource_table == NULL) // check if database is already open + { + GError* error = NULL; + char filename[dbPathMaxLen]; + snprintf(filename, dbPathMaxLen, gLocalWtPath, gAppId, gResTableCfg); + gResource_table = gvdb_table_new(filename, TRUE, &error); + + if(gResource_table == NULL) + { + printf("Database error: %s\n", error->message); + g_error_free(error); + error = NULL; + } + } + + return gResource_table; +} + + + + diff --git a/src/persistence_client_library.h b/src/persistence_client_library.h new file mode 100644 index 0000000..6f9ce91 --- /dev/null +++ b/src/persistence_client_library.h @@ -0,0 +1,213 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_H +#define PERSISTENCE_CLIENT_LIBRARY_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Header of the persistence client library. + * Library provides an API to access persistent data + * @see + */ + +#include "persistence_client_library_lc_interface.h" +#include +#include "crc32.h" + +#include +#include + + +/// constant definitions +enum persClientLibConstantDef +{ + + storeWt = 0, /// flag for write through storage policy + dbLocal = 0, /// flag for local storage location + resIsNoFile = 0, /// flag to identify that resource a not file + storeCached = 1, /// flag for cached storage policy + dbShared = 1, /// flag for shared storage location + resIsFile = 1, /// flag to identify that resource a file + accessLocked = 1, /// flag to indicate that access is locked + + FileClosed = 0, + FileOpen = 1, + + NsmShutdownNormal = 1, /// lifecycle shutdown normal + NsmErrorStatus_OK = 1, + NsmErrorStatus_Fail = -1, + + PasMsg_Block = 1, /// persistence administration service block access + PasMsg_WriteBack = 2, /// persistence administration service write_back + PasMsg_Unblock = 4, /// persistence administration service unblock access + PasErrorStatus_RespPend = 88, /// persistence administration service msg return status + PasErrorStatus_OK = 100, /// persistence administration service msg return status + PasErrorStatus_FAIL = -1, /// persistence administration service msg return status + + dbKeyMaxLen = 128, /// max database key length + dbPathMaxLen = 128, /// max database path length + maxAppNameLen = 128, /// max application name + maxPersHandle = 256 /// max number of parallel open persistence handles +}; + +/// resource configuration table name +static const char* gResTableCfg = "/resource-table-cfg.gvdb"; + +/// shared cached default database +//static const char* gSharedCachedDefault = "cached-default.dconf"; +/// shared cached database +static const char* gSharedCached = "/cached.dconf"; +/// shared write through default database +//static const char* gSharedWtDefault = "wt-default.dconf"; +/// shared write through database +static const char* gSharedWt = "/wt.dconf"; + +/// local cached default database +//static const char* gLocalCachedDefault = "cached-default.gvdb"; +/// local cached default database +static const char* gLocalCached = "/cached.gvdb"; +/// local write through default database +//static const char* gLocalWtDefault = "wt-default.gvdb"; +/// local write through default database +static const char* gLocalWt = "/wt.gvdb"; + + +/// directory structure node name defintion +static const char* gNode = "/Node"; +/// directory structure user name defintion +static const char* gUser = "/User/"; +/// directory structure seat name defintion +static const char* gSeat = "/Seat/"; + + +/// path prefic for local cached database: /Data/mnt_c// +static const char* gLocalCachePath = "/Data/mnt-c/%s%s"; +/// path prefic for local write through database /Data/mnt_wt// +static const char* gLocalWtPath = "/Data/mnt-wt/%s%s"; +/// path prefic for shared cached database: /Data/mnt_c/Shared/Group// +static const char* gSharedCachePath = "/Data/mnt-c/Shared/Group/%x%s"; +/// path prefic for shared write through database: /Data/mnt_wt/Shared/Group// +static const char* gSharedWtPath = "/Data/mnt-wt/Shared/Group/%x%s"; +/// path prefic for shared public cached database: /Data/mnt_c/Shared/Public// +static const char* gSharedPublicCachePath = "/Data/mnt-c/Shared/Public%s"; +/// path prefic for shared public write through database: /Data/mnt_wt/Shared/Public/ +static const char* gSharedPublicWtPath = "/Data/mnt-wt/Shared/Public%s"; + + +/// application id +static char gAppId[maxAppNameLen]; + + +/** enumerator used to identify the policy to manage the data */ +typedef enum _PersistencePolicy_e +{ + PersistencePolicy_wc = 0, /**< the data is managed write cached */ + PersistencePolicy_wt = 1, /**< the data is managed write through */ + PersistencePolicy_na = 2, /**< the data is not applicable */ + + /** insert new entries here ... */ + PersistencePolicy_LastEntry /**< last entry */ + +} PersistencePolicy_e; + + +/** enumerator used to identify the persistence storage to manage the data */ +typedef enum _PersistenceStorage_e +{ + PersistenceStorage_local = 0, /**< the data is managed local: gvdb */ + PersistenceStorage_shared = 1, /**< the data is managed shared: dconf */ + PersistenceStorage_custom = 2, /**< the data is managed over custom client implementation */ + + /** insert new entries here ... */ + PersistenceStoragePolicy_LastEntry /**< last entry */ + +} PersistenceStorage_e; + + +/** structure used to manage the persistence configuration for a key */ +typedef struct _PersistenceConfigurationKey_s +{ + PersistencePolicy_e policy; /**< policy */ + PersistenceStorage_e storage; /**< definition of storage to use */ + unsigned int permission; /**< access right, corresponds to UNIX */ + long max_size; /**< max size expected for the key */ + char * reponsible; /**< name of responsible application */ + char * custom_name; /**< name of the customer plugin */ +} PersistenceConfigurationKey_s; + + + +/** + * @brief Create database search key and database location path + * + * @param ldbid logical database id + * @param resource_id the resource id + * @param user_no user identification + * @param seat_no seat identifier + * @param isFile identifier if this resource is a file + * @param dbKey the array where the database key will be stored + * @param dbPath the array where the database location path will be stored + * @param cached_resource flag to identify if the resource is cached (value 1)or write through (value 0) + * + * @return -1 if error : 1 if shared database and 0 if local database + */ +int get_db_path_and_key(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no, + unsigned int isFile, char dbKey[], char dbPath[], unsigned char cached_resource); + + + +/** + * Create database search key and database location path + * + * @param ldbid logical database id + * @param resource_id the resource id + * @param user_no user identification + * @param seat_no seat identifier + * @param isFile identifier if this resource is a file + * @param dbKey the array where the database key will be stored + * @param dbPath the array where the database location path will be stored + * @param cached_resource flag to identify if the resource is cached (value 1)or write through (value 0) + * + * @return -1 if error : 1 if shared database and 0 if local database + */ +int get_db_context(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no, + unsigned int isFile, char dbKey[], char dbPath[]); + + + +/** + * @brief get the resource configuration table gvbd database + * + * @return pointer to the gvdb database table + */ +GvdbTable* get_resource_cfg_table(); + + + + +#endif /* PERSISTENCY_CLIENT_LIBRARY_H */ + diff --git a/src/persistence_client_library_data_access.c b/src/persistence_client_library_data_access.c new file mode 100644 index 0000000..9d8eac6 --- /dev/null +++ b/src/persistence_client_library_data_access.c @@ -0,0 +1,229 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_data_access.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of persistence data access + * Library provides an API to access persistent data + * @see + */ + +#include "persistence_client_library_data_access.h" + +#include +#include +#include + + + + +int get_value_from_table(GvdbTable* database, char* key, unsigned char* buffer, unsigned long buffer_size) +{ + int read_size = 0; + gconstpointer valuePtr = NULL; + GVariant* dbValue = NULL; + + dbValue = gvdb_table_get_value(database, key); + + if(dbValue != NULL) + { + read_size = g_variant_get_size(dbValue); + valuePtr = g_variant_get_data(dbValue); // get the "data" part from GVariant + + if( (valuePtr != NULL)) + { + if(read_size > buffer_size) + { + read_size = buffer_size; // truncate data size to buffer size + } + memcpy(buffer, valuePtr, read_size-1); + } + else + { + printf("get_value_from_table: E R R O R getting size and/or data for key: %s \n", key); + } + } + else + { + printf("get_value_from_table: E R R O R getting value for key: %s \n", key); + } + + return read_size; +} + + + +int get_size_from_table(GvdbTable* database, char* key) +{ + int read_size = 0; + GVariant* dbValue = gvdb_table_get_value(database, key); + + if(dbValue != NULL) + { + read_size = g_variant_get_size(dbValue); + } + else + { + printf("get_size_from_table: E R R O R getting value for key: %s \n", key); + } + + return read_size; +} + + + +int persistence_get_data(char* dbPath, char* key, int shared, unsigned char* buffer, unsigned long buffer_size) +{ + int read_size = -1; + + printf("key : %s \n", key); + printf("path: %s \n", dbPath); + if(dbShared == shared) // check if shared data (dconf) + { + printf(" S H A R E D D A T A => not implemented yet\n"); + //DConfClient* dconf_client_new(const gchar *profile, DConfWatchFunc watch_func, gpointer user_data, GDestroyNotify notify); + + //GVariant* dconf_client_read(DConfClient *client, const gchar *key); + + + } + else if(dbLocal == shared) // it is local data (gvdb) + { + GError *error = NULL; + GvdbTable* database = gvdb_table_new(dbPath, TRUE, &error);; + gvdb_table_ref(database); + if(database != NULL) + { + read_size = get_value_from_table(database, key, buffer, buffer_size-1); + gvdb_table_unref(database); + } + else + { + printf("Database E R R O R: %s\n", error->message); + g_error_free(error); + error = NULL; + } + } + return read_size; +} + + + +int persistence_set_data(char* dbPath, char* key, int shared, unsigned char* buffer, unsigned long buffer_size) +{ + int write_size = -1; + + printf("key : %s \n", key); + printf("path: %s \n", dbPath); + if(dbShared == shared) // check if shared data (dconf) + { + //GVariant *value = NULL; + //gboolean ok = FALSE; + + printf(" S H A R E D D A T A => NOW IMPLEMENTING implemented yet\n"); + //DConfDBusClient *dcdbc = dconf_dbus_client_new("/com/canonical/indicator/power/", NULL, NULL); + //ok = dconf_dbus_client_write(dcdbc, key, value); + + } + else if(dbLocal == shared) // it is local data (gvdb) + { + GError *error = NULL; + GHashTable* parent = NULL; + GHashTable* hash_table = NULL; + const gchar *name_in_parent = "persDB"; + + hash_table = gvdb_hash_table_new(parent, name_in_parent); + + if(hash_table != NULL) + { + write_size = set_value_to_table(hash_table, key, buffer, buffer_size); + gvdb_table_write_contents(hash_table, dbPath, FALSE, &error); + } + else + { + printf("Database E R R O R: %s\n", error->message); + g_error_free(error); + error = NULL; + } + } + return write_size; +} + + + +int persistence_get_data_size(char* dbPath, char* key, int shared) +{ + int read_size = -1; + + if(dbShared == shared) // check if shared data (dconf) + { + printf("S H A R E D D A T A => not implemented yet\n"); + } + else if(dbLocal == shared) // it is local data (gvdb) + { + GError *error = NULL; + GvdbTable* database = gvdb_table_new(dbPath, TRUE, &error);; + + if(database != 0) + { + read_size = get_size_from_table(database, key); + gvdb_table_unref(database); + } + else + { + printf("Database E R R O R: %s\n", error->message); + g_error_free(error); + error = NULL; + } + } + return read_size; + +} + + +int set_value_to_table(GHashTable* database, char* key, unsigned char* buffer, unsigned long buffer_size) +{ + int size_written = -1; + gvdb_hash_table_insert_string(database, key, (const gchar*)buffer); + return size_written; +} + + + +int persistence_reg_notify_on_change(char* dbPath, char* key) +{ + int rval = -1; + + return rval; +} + + + + + + + + diff --git a/src/persistence_client_library_data_access.h b/src/persistence_client_library_data_access.h new file mode 100644 index 0000000..536a38c --- /dev/null +++ b/src/persistence_client_library_data_access.h @@ -0,0 +1,135 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_DATA_ACCESS_H +#define PERSISTENCE_CLIENT_LIBRARY_DATA_ACCESS_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_data_access.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Header of the persistence client library data access. + * Library provides an API to access persistent data + * @see + */ + +#include "persistence_client_library.h" +#include "gvdb-builder.h" + + + +/** + * @brief write data to a key + * + * @param dbPath the path to the database where the key is in + * @param key the database key + * @param shared flag if key is a shared key + * (use dbShared for shared key or dbLocal if the key is local) + * @return + */ +int persistence_set_data(char* dbPath, char* key, int shared, unsigned char* buffer, unsigned long buffer_size); + + + +/** + * @brief get data of a key + * + * @param dbPath the path to the database where the key is in + * @param key the database key + * @param shared flag if key is a shared key + * (use dbShared for shared key or dbLocal if the key is local) + * + * @return + */ +int persistence_get_data(char* dbPath, char* key, int shared, unsigned char* buffer, unsigned long buffer_size); + + + +/** + * @brief get the size of the data from a given key + * + * @param dbPath the path to the database where the key is in + * @param key the database key + * @param shared flag if key is a shared key + * (use dbShared for shared key or dbLocal if the key is local) + * + * @return size of data in bytes read from the key + */ +int persistence_get_data_size(char* dbPath, char* key, int shared); + + + +/** + * @brief register for change notifications of a key + * + * @param dbPath the path to the database where the key is in + * @param key the database key to register on + * + * @return 0 of registration was successfull; -1 if registration failes + */ +int persistence_reg_notify_on_change(char* dbPath, char* key); + + + +/** + * @brief read data from a key + * + * @param database pointer to the database + * @param key the database key to read data from + * @param buffer the data + * @param buffer_size the size of the buffer in bytes + * + * @return size of data in bytes read from the key + */ +int get_value_from_table(GvdbTable* database, char* key, unsigned char* buffer, unsigned long buffer_size); + + + +/** + * @brief write data to a key + * + * @param database pointer to the database + * @param key the database key to write data + * @param buffer the data + * @param buffer_size the size of the buffer in bytes + * + * @return size of data in bytes written to the key + */ +int set_value_to_table(GHashTable* database, char* key, unsigned char* buffer, unsigned long buffer_size); + + + +/** + * @brief get the size of the data from a key + * + * @param database pointer to the database + * @param key the database key to get the size form + * + * @return size of data + */ +int get_size_from_table(GvdbTable* database, char* key); + + + +#endif /* PERSISTENCY_CLIENT_LIBRARY_DATA_ACCESS_H */ diff --git a/src/persistence_client_library_dbus_service.c b/src/persistence_client_library_dbus_service.c new file mode 100644 index 0000000..ff1b03e --- /dev/null +++ b/src/persistence_client_library_dbus_service.c @@ -0,0 +1,407 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_dbus_service.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library dbus service. + * @see + */ + +//#include "persistence_client_service_dbus_service.h" + +#include "persistence_client_library_dbus_service.h" +#include "persistence_client_library_lc_interface.h" +#include "persistence_client_library_pas_interface.h" + +#include +#include +#include +#include +#include +#include + + +/// polling structure +typedef struct SPollInfo +{ + int nfds; + struct pollfd fds[10]; + DBusWatch * watches[10]; +} tPollInfo; + + +/// polling information +static tPollInfo gPollInfo; + + +/// dbus connection +DBusConnection* gDbusConn = NULL; + + +DBusConnection* get_dbus_connection() +{ + return gDbusConn; +} + +//------------------------------------------------------------------------ +// debugging only until "correct" exit of main loop is possible!!!!! +//------------------------------------------------------------------------ +#include "signal.h" +static int endLoop = 0; + +void sigHandler(int signo) +{ + endLoop = 1; +} +//------------------------------------------------------------------------ + +//const char* gPersDbusAdminInterface = "org.genivi.persistence.admin"; +//const char* gPersDbusAdminPath = "/org/genivi/persistence/admin"; + + + +#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) + + +/* function to unregister ojbect path message handler */ +static void unregisterMessageHandler(DBusConnection *connection, void *user_data) +{ + printf("unregisterObjectPath\n"); +} + +/* catches messages not directed to any registered object path ("garbage collector") */ +static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data) +{ + printf("handleObjectPathMessageFallback '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message) ); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + + + +static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data) +{ + printf("unregisterObjectPathFallback\n"); +} + + + +void* run_mainloop(void* dataPtr) +{ + // persistence admin message + static const struct DBusObjectPathVTable vtablePersAdmin + = {unregisterMessageHandler, checkPersAdminMsg, NULL, }; + + // lifecycle message + static const struct DBusObjectPathVTable vtableLifecycle + = {unregisterMessageHandler, checkLifecycleMsg, NULL, }; + + // fallback + static const struct DBusObjectPathVTable vtableFallback + = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, }; + + // setup the dbus + mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr); + + printf("Exit dbus main loop!!!!\n"); + + return NULL; +} + + + +int setup_dbus_mainloop() +{ + int rval = 0; + pthread_t thread; + DBusError err; + const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS"); + dbus_error_init(&err); + + // Connect to the bus and check for errors + if(pAddress != NULL) + { + printf("Use specific dbus address: %s\n !", pAddress); + gDbusConn = dbus_connection_open(pAddress, &err); + + if(gDbusConn != NULL) + { + if(!dbus_bus_register(gDbusConn, &err)) + { + printf("dbus_bus_register() Error %s\n", err.message); + dbus_error_free (&err); + return -1; + } + else + { + printf("Registered connection successfully !\n"); + } + } + else + { + printf("dbus_connection_open() Error %s\n",err.message); + dbus_error_free(&err); + } + } + else + { + printf("Use default dbus bus!!!!!!\n"); + gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); + } + + // create here the dbus connection and pass to main loop + rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn); + + if (rval) + { + fprintf(stderr, "Server: - ERROR! pthread_create( run_mainloop ) returned: %d\n", rval); + } + return rval; +} + + + + + +static dbus_bool_t addWatch(DBusWatch *watch, void *data) +{ + dbus_bool_t result = FALSE; + + //printf("addWatch called @%08x flags: %08x enabled: %c\n", (unsigned int)watch, dbus_watch_get_flags(watch), TRUE==dbus_watch_get_enabled(watch)?'x':'-'); + + if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds) + { + int flags = dbus_watch_get_flags(watch); + + gPollInfo.watches[gPollInfo.nfds] = watch; + + gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch); + + if (TRUE==dbus_watch_get_enabled(watch)) + { + if (flags&DBUS_WATCH_READABLE) + { + gPollInfo.fds[gPollInfo.nfds].events |= POLLIN; + } + if (flags&DBUS_WATCH_WRITABLE) + { + gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT; + } + + ++gPollInfo.nfds; + + static const int cmd = CMD_REQUEST_NAME; + if (sizeof(int)!=write(gPipefds[1], &cmd, sizeof(int))) + { + printf("write failed w/ errno %d\n", errno); + } + } + + result = TRUE; + } + + return result; +} + + + +static void removeWatch(DBusWatch *watch, void *data) +{ + printf("removeWatch called @0x%08x\n", (int)watch); +} + + + +static void watchToggled(DBusWatch *watch, void *data) +{ + printf("watchToggled called @0x%08x\n", (int)watch); +} + + + +int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2, + DBusObjectPathVTable vtableFallback, void* userData) +{ + DBusError err; + + signal(SIGTERM, sigHandler); + signal(SIGQUIT, sigHandler); + signal(SIGINT, sigHandler); + + DBusConnection* conn = (DBusConnection*)userData; + + dbus_error_init(&err); + + if (dbus_error_is_set(&err)) + { + printf("Connection Error (%s)\n", err.message); + dbus_error_free(&err); + } + else if (NULL != conn) + { + dbus_connection_set_exit_on_disconnect (conn, FALSE); + + printf("connected as '%s'\n", dbus_bus_get_unique_name(conn)); + + if (0!=pipe(gPipefds)) + { + printf("pipe() failed w/ errno %d\n", errno); + } + else + { + int ret; + int bContinue = 0; + memset(&gPollInfo, 0 , sizeof(gPollInfo)); + + gPollInfo.nfds = 1; + gPollInfo.fds[0].fd = gPipefds[0]; + gPollInfo.fds[0].events = POLLIN; + + if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData)) + && (TRUE==dbus_connection_register_object_path(conn, "/com/contiautomotive/NodeStateManager/LifecycleConsumer", &vtable2, userData)) + && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) ) + { + if (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL)) + { + printf("dbus_connection_set_watch_functions() failed\n"); + } + else + { + char buf[64]; + + do + { + bContinue = 0; /* assume error */ + + while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn)); + + while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, 500)))&&(EINTR==errno)); + + if(0>ret) + { + printf("poll() failed w/ errno %d\n", errno); + } + else + { + int i; + bContinue = 1; + + for (i=0; gPollInfo.nfds>i; ++i) + { + if (0!=gPollInfo.fds[i].revents) + { + if (gPollInfo.fds[i].fd==gPipefds[0]) + { + if (0!=(gPollInfo.fds[i].revents & POLLIN)) + { + bContinue = TRUE; + while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno)); + if (0>ret) + { + printf("read() failed w/ errno %d\n", errno); + } + else if (sizeof(int)==ret) + { + switch (buf[0]) + { + case CMD_REQUEST_NAME: + if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER + != dbus_bus_request_name(conn, "org.genivi.persistence.adminconsumer", DBUS_NAME_FLAG_DO_NOT_QUEUE, &err)) + { + printf("*** Cannot acquire name '%s' (%s). Bailing out!\n", "org.genivi.persistence.admin", err.message); + dbus_error_free(&err); + bContinue = FALSE; + } + if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER + != dbus_bus_request_name(conn, "com.contiautomotive.NodeStateManager.LifecycleConsumer", DBUS_NAME_FLAG_DO_NOT_QUEUE, &err)) + { + printf("*** Cannot acquire name '%s' (%s). Bailing out!\n", "com.contiautomotive.NodeStateManager.LifecycleConsumer", err.message); + dbus_error_free(&err); + bContinue = FALSE; + } + break; + case CMD_PAS_BLOCK_AND_WRITE_BACK: + process_block_and_write_data_back(); + break; + case CMD_LC_PREPARE_SHUTDOWN: + process_prepare_shutdown(buf[1]); + break; + case CMD_QUIT: + bContinue = FALSE; + break; + default: + printf("command %d not handled!\n", buf[0]); + break; + } + } + else + { + printf("read() returned %d (%s)\n", ret, buf); + } + } + } + else + { + int flags = 0; + if (0!=(gPollInfo.fds[i].revents & POLLIN)) + { + flags |= DBUS_WATCH_READABLE; + } + if (0!=(gPollInfo.fds[i].revents & POLLOUT)) + { + flags |= DBUS_WATCH_WRITABLE; + } + if (0!=(gPollInfo.fds[i].revents & POLLERR)) + { + flags |= DBUS_WATCH_ERROR; + } + if (0!=(gPollInfo.fds[i].revents & POLLHUP)) + { + flags |= DBUS_WATCH_HANGUP; + } + //printf("handle watch @0x%08x flags: %04x\n", (int)gPollInfo.watches[i], flags); + bContinue = dbus_watch_handle(gPollInfo.watches[i], flags); + } + } + } + } + if(endLoop == 1) + break; + } + while (0!=bContinue); + } + dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer"); + //dbus_connection_unregister_object_path(conn, "/com/"); + dbus_connection_unregister_object_path(conn, "/"); + } + close(gPipefds[1]); + close(gPipefds[0]); + } + dbus_connection_unref(conn); + dbus_shutdown(); + } + return 0; +} + diff --git a/src/persistence_client_library_dbus_service.h b/src/persistence_client_library_dbus_service.h new file mode 100644 index 0000000..bb69077 --- /dev/null +++ b/src/persistence_client_library_dbus_service.h @@ -0,0 +1,74 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_DBUS_SERVICE_H_ +#define PERSISTENCE_CLIENT_LIBRARY_DBUS_SERVICE_H_ + +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_dbus_service.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Header of the persistence client library dbus service. + * @see + */ + +#include +#include + +/// command definitions for main loop +typedef enum ECmd +{ + CMD_NONE = 0, /// command none + CMD_REQUEST_NAME, /// command request dbus name + CMD_PAS_BLOCK_AND_WRITE_BACK, /// command block access and write data back + CMD_LC_PREPARE_SHUTDOWN, /// command to prepare shutdown + CMD_QUIT /// quit command +} tCmd; + + +/// pipe file descriptors +int gPipefds[2]; + + + + +/// returns the dbus connection +DBusConnection* get_dbus_connection(); + + + +/** + * Main loop to dispatch events and dbus messages + * + */ +int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2, + DBusObjectPathVTable vtableFallback, void* userData); + + + +/// dbus main loop +int setup_dbus_mainloop(); + + +#endif /* PERSISTENCE_CLIENT_LIBRARY_DBUS_SERVICE_H_ */ diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c new file mode 100644 index 0000000..d8b6b50 --- /dev/null +++ b/src/persistence_client_library_file.c @@ -0,0 +1,213 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_file.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library. + * Library provides an API to access persistent data + * @see + */ + +#include "persistence_client_library_file.h" +#include "persistence_client_library.h" +#include "persistence_client_library_data_access.h" +#include "persistence_client_library_pas_interface.h" +#include "persistence_client_library_handle.h" + +#include // for open flags +#include +#include +#include +#include + + + +int file_close(int fd) +{ + int rval = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + rval = close(fd); + if(fd < maxPersHandle) + { + __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag + } + } + return rval; +} + + + +int file_get_size(int fd) +{ + int rval = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + struct stat buf; + int ret = 0; + ret = fstat(fd, &buf); + + if(ret != -1) + { + rval = buf.st_size; + } + } + return rval; +} + + + +void* file_map_data(void* addr, long size, long offset, int fd) +{ + void* ptr = 0; + int mapFlag = PROT_WRITE | PROT_READ; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + ptr = mmap(addr,size, mapFlag, MAP_SHARED, fd, offset); + } + return ptr; +} + + + +int file_open(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no) +{ + int handle = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + int shared_DB = 0, + flags = O_RDWR; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // get database context: database path and database key + shared_DB = get_db_context(ldbid, resource_id, user_no, seat_no, resIsFile, dbKey, dbPath); + + if(shared_DB != -1) // check valid database context + { + handle = open(dbPath, flags); + + if(handle == -1) + { + printf("file_open ERROR: %s \n", strerror(errno) ); + } + else + { + if(handle < maxPersHandle) + { + __sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag + } + } + } + } + + return handle; +} + + + +int file_read_data(int fd, void * buffer, unsigned long buffer_size) +{ + int size = -1; + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + size = read(fd, buffer, buffer_size); + } + return size; +} + + + +int file_remove(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no) +{ + int rval = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + int shared_DB = 0; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // get database context: database path and database key + shared_DB = get_db_context(ldbid, resource_id, user_no, seat_no, resIsFile, dbKey, dbPath); + + if(shared_DB != -1) // check valid database context + { + rval = remove(dbPath); + if(rval == -1) + printf("file_remove ERROR: %s \n", strerror(errno) ); + } + } + + return rval; +} + + + +int file_seek(int fd, long int offset, int whence) +{ + int rval = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + rval = lseek(fd, offset, whence); + } + return rval; +} + + + +int file_unmap_data(void* address, long size) +{ + int rval = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + rval = munmap(address, size); + } + return rval; +} + + + +int file_write_data(int fd, const void * buffer, unsigned long buffer_size) +{ + int size = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + size = write(fd, buffer, buffer_size); + } + return size; +} + + diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c new file mode 100644 index 0000000..8aefb96 --- /dev/null +++ b/src/persistence_client_library_handle.c @@ -0,0 +1,73 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_handle.c + * @ingroup Persistence client library handle + * @author Ingo Huerner + * @brief Implementation of the persistence client library handle + * Library provides an API to access persistent data + * @see + */ + + +#include "persistence_client_library_handle.h" + +/// handle index +static int gHandleIdx = 0; +static int gHandleFreeIdx = -1; + + + +/// get persistence handle +int get_persistence_handle_idx() +{ + int handle = -1; + + if(gHandleFreeIdx != -1) // check if we have a free spot in the array before the current max + { + handle = gHandleIdx = gHandleFreeIdx; + gHandleFreeIdx = -1; + } + else + { + if((gHandleIdx + 1) < maxPersHandle) + { + handle = ++gHandleIdx; // no free sport before current max, increment handle index + } + else + { + printf("Reached maximum of open handles: %d \n", maxPersHandle); + } + } + + return handle; +} + + +/// close persistence handle +void set_persistence_handle_close_idx(int handle) +{ + gHandleFreeIdx = handle; +} diff --git a/src/persistence_client_library_handle.h b/src/persistence_client_library_handle.h new file mode 100644 index 0000000..aa178e6 --- /dev/null +++ b/src/persistence_client_library_handle.h @@ -0,0 +1,63 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_HANDLE_H +#define PERSISTENCE_CLIENT_LIBRARY_HANDLE_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_handle.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Header of the persistence client library handle. + * Library provides an API to access persistent data + * @see + */ + +#include "persistence_client_library.h" + +typedef struct _PersistenceHandle_s +{ + int shared_DB; /// is a shared resource + char dbPath[dbPathMaxLen]; /// path to the database + char dbKey[dbKeyMaxLen]; /// database key +} +PersistenceHandle_s; + +/// persistence handle array +static PersistenceHandle_s gHandleArray[maxPersHandle]; + + +/// open file descriptor handle array +static int gOpenFdArray[maxPersHandle]; + + +/// get persistence handle +int get_persistence_handle_idx(); + +/// close persistence handle +void set_persistence_handle_close_idx(int handle); + + +#endif /* PERSISTENCY_CLIENT_LIBRARY_HANDLE_H */ + diff --git a/src/persistence_client_library_key.c b/src/persistence_client_library_key.c new file mode 100644 index 0000000..1a563b4 --- /dev/null +++ b/src/persistence_client_library_key.c @@ -0,0 +1,278 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_key.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library. + * Library provides an API to access persistent data + * @see + */ + + +#include "persistence_client_library_key.h" +#include "persistence_client_library.h" +#include "persistence_client_library_handle.h" +#include "persistence_client_library_data_access.h" +#include "persistence_client_library_pas_interface.h" + + + + +// ------------------------------------------------------------------ +// function with handle +// ------------------------------------------------------------------ + +int key_handle_open(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no) +{ + int handle = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + int shared_DB = 0; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // get database context: database path and database key + shared_DB = get_db_context(ldbid, resource_id, user_no, seat_no, resIsNoFile, dbKey, dbPath); + + if(shared_DB != -1) // check valid database context + { + handle = get_persistence_handle_idx(); + + // remember data in handle array + strncpy(gHandleArray[handle].dbPath, dbPath, dbPathMaxLen); + strncpy(gHandleArray[handle].dbKey, dbKey, dbKeyMaxLen); + gHandleArray[handle].shared_DB = shared_DB; + } + } + + return handle; +} + + + +int key_handle_close(int key_handle) +{ + int rval = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + // invalidate entries + strncpy(gHandleArray[key_handle].dbPath, "", dbPathMaxLen); + strncpy(gHandleArray[key_handle].dbKey ,"", dbKeyMaxLen); + gHandleArray[key_handle].shared_DB = -1; + + set_persistence_handle_close_idx(key_handle); + } + + return rval; +} + + + +int key_handle_get_size(int key_handle) +{ + int size = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + if(key_handle < maxPersHandle) + size = persistence_get_data_size(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey, gHandleArray[key_handle].shared_DB); + } + + return size; +} + + + +int key_handle_read_data(int key_handle, unsigned char* buffer, unsigned long buffer_size) +{ + int size = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + if(key_handle < maxPersHandle) + size = persistence_get_data(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey, gHandleArray[key_handle].shared_DB, buffer, buffer_size); + } + + return size; +} + + + +int key_handle_register_notify_on_change(int key_handle) +{ + int rval = -1; + + return rval; +} + + + +int key_handle_write_data(int key_handle, unsigned char* buffer, unsigned long buffer_size) +{ + int size = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + if(key_handle < maxPersHandle) + size = persistence_set_data(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey, gHandleArray[key_handle].shared_DB, buffer, buffer_size); + } + + return size; +} + + + + +// ------------------------------------------------------------------ +// functions to be used directly without a handle +// ------------------------------------------------------------------ + +int key_delete(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no) +{ + int rval = 0; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + // TODO + } + + return rval; +} + + + +// status: OK +int key_get_size(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no) +{ + int data_size = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + int shared_DB = 0; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // get database context: database path and database key + shared_DB = get_db_context(ldbid, resource_id, user_no, seat_no, resIsNoFile, dbKey, dbPath); + + if(shared_DB != -1) // check if database context is valid + { + data_size = persistence_get_data_size(dbPath, dbKey, shared_DB); + } + } + + return data_size; +} + + + +// status: OK +int key_read_data(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no, unsigned char* buffer, unsigned long buffer_size) +{ + int data_size = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + int shared_DB = 0; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // get database context: database path and database key + shared_DB = get_db_context(ldbid, resource_id, user_no, seat_no, resIsNoFile, dbKey, dbPath); + + if(shared_DB != -1) // check if database context is valid + { + data_size = persistence_get_data(dbPath, dbKey, shared_DB, buffer, buffer_size); + } + } + return data_size; +} + + + +// status: TODO +int key_write_data(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no, unsigned char* buffer, unsigned long buffer_size) +{ + int data_size = -1; + + if(accessLocked == isAccessLocked() ) // check if access to persistent data is locked + { + int shared_DB = 0; + unsigned int hash_val_data = 0; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // get database context: database path and database key + shared_DB = get_db_context(ldbid, resource_id, user_no, seat_no, resIsNoFile, dbKey, dbPath); + + // get hash value of data to verify storing + hash_val_data = crc32(hash_val_data, buffer, buffer_size); + + // store data + if(shared_DB != -1) // check if database context is valid + { + data_size = persistence_set_data(dbPath, dbKey, shared_DB, buffer, buffer_size); + } + } + + + return data_size; +} + + + +// status: TODO +int key_register_notify_on_change(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no) +{ + int rval = 0; + +// unsigned int hash_val_data = 0; + + char dbKey[dbKeyMaxLen]; // database key + char dbPath[dbPathMaxLen]; // database location + + // registration is only on shared key possible + if(dbShared == get_db_context(ldbid, resource_id, user_no, seat_no, resIsNoFile, dbKey, dbPath)) + { + rval = persistence_reg_notify_on_change(dbPath, dbKey); + } + + return rval; +} + + + + + + + + diff --git a/src/persistence_client_library_lc_interface.c b/src/persistence_client_library_lc_interface.c new file mode 100644 index 0000000..3be07da --- /dev/null +++ b/src/persistence_client_library_lc_interface.c @@ -0,0 +1,351 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_lc_interface.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library lifecycle interface. + * @see + */ + +#include "persistence_client_library_lc_interface.h" +#include "persistence_client_library_pas_interface.h" +#include "persistence_client_library_dbus_service.h" +#include "persistence_client_library.h" +#include "persistence_client_library_handle.h" + +#include +#include +#include +#include + + +int check_lc_request(int request) +{ + int rval = 0; + + switch(request) + { + case NsmShutdownNormal: + { + // add command and data to queue + unsigned long cmd = ( (request << 8) | CMD_LC_PREPARE_SHUTDOWN); + + if(sizeof(int)!=write(gPipefds[1], &cmd, sizeof(unsigned long))) + { + printf("write failed w/ errno %d\n", errno); + rval = NsmErrorStatus_Fail; + } + else + { + rval = NsmErrorStatus_OK; + } + } + break; + default: + printf("Unknown lifecycle message!\n"); + } + + return rval; +} + + +int msg_lifecycleRequest(DBusConnection *connection, DBusMessage *message) +{ + int request = 0, + requestId = 0, + msgReturn = 0; + + DBusMessage *reply; + DBusError error; + dbus_error_init (&error); + + if (!dbus_message_get_args (message, &error, DBUS_TYPE_UINT32, &request, + DBUS_TYPE_UINT32, &requestId, + DBUS_TYPE_INVALID)) + { + reply = dbus_message_new_error(message, error.name, error.message); + + if (reply == 0) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + if (!dbus_connection_send(connection, reply, 0)) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + dbus_message_unref (reply); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + msgReturn = check_lc_request(request); + + reply = dbus_message_new_method_return(message); + + if (reply == 0) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &msgReturn, DBUS_TYPE_INVALID)) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + if (!dbus_connection_send(connection, reply, 0)) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + dbus_connection_flush(connection); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; +} + + + + +DBusHandlerResult checkLifecycleMsg(DBusConnection * connection, DBusMessage * message, void * user_data) +{ + DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + //printf("handleObjectPathMessage '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message)); + if((0==strncmp("com.contiautomotive.NodeStateManager.LifecycleConsumer", dbus_message_get_interface(message), 20))) + { + if((0==strncmp("LifecycleRequest", dbus_message_get_member(message), 18))) + { + result = msg_lifecycleRequest(connection, message); + } + else + { + printf("checkLifecycleMsg -> unknown message '%s'\n", dbus_message_get_interface(message)); + } + } + return result; +} + + + + + + +int send_lifecycle_register(const char* method, const char* busname, const char* objName, + int shutdownMode, int TimeoutMs) +{ + int rval = 0; + + DBusError error; + dbus_error_init (&error); + + DBusConnection* conn = get_dbus_connection(); + + DBusMessage* message = dbus_message_new_method_call("com.contiautomotive.NodeStateManager.Consumer", // destination + "/com/contiautomotive/NodeStateManager/Consumer", // path + "com.contiautomotive.NodeStateManager.Consumer", // interface + method); // method + if(message != NULL) + { + dbus_message_append_args(message, DBUS_TYPE_STRING, &busname, + DBUS_TYPE_STRING, &objName, + DBUS_TYPE_INT32, &shutdownMode, + DBUS_TYPE_INT32, &TimeoutMs, + DBUS_TYPE_INVALID); + + if(conn != NULL) + { + if(!dbus_connection_send(conn, message, 0)) + { + fprintf(stderr, "send_lifecycle ==> Access denied: %s \n", error.message); + } + + dbus_connection_flush(conn); + } + else + { + fprintf(stderr, "send_lifecycle ==> ERROR: Invalid connection!! \n"); + } + dbus_message_unref(message); + } + else + { + fprintf(stderr, "send_lifecycle ==> ERROR: Invalid message!! \n"); + } + + return rval; +} + + + +int send_lifecycle_un_register(const char* method, const char* busname, const char* objName, int shutdownMode) +{ + int rval = 0; + + DBusError error; + dbus_error_init (&error); + + DBusConnection* conn = get_dbus_connection(); + + DBusMessage* message = dbus_message_new_method_call("com.contiautomotive.NodeStateManager.Consumer", // destination + "/com/contiautomotive/NodeStateManager/Consumer", // path + "com.contiautomotive.NodeStateManager.Consumer", // interface + method); // method + if(message != NULL) + { + dbus_message_append_args(message, DBUS_TYPE_STRING, &busname, + DBUS_TYPE_STRING, &objName, + DBUS_TYPE_INT32, &shutdownMode, + DBUS_TYPE_INVALID); + + if(conn != NULL) + { + if(!dbus_connection_send(conn, message, 0)) + { + fprintf(stderr, "send_lifecycle ==> Access denied: %s \n", error.message); + } + + dbus_connection_flush(conn); + } + else + { + fprintf(stderr, "send_lifecycle ==> ERROR: Invalid connection!! \n"); + } + dbus_message_unref(message); + } + else + { + fprintf(stderr, "send_lifecycle ==> ERROR: Invalid message!! \n"); + } + + return rval; +} + +int send_lifecycle_request(const char* method, int requestId, int status) +{ + int rval = 0; + + DBusError error; + dbus_error_init (&error); + + DBusConnection* conn = get_dbus_connection(); + + DBusMessage* message = dbus_message_new_method_call("com.contiautomotive.NodeStateManager.Consumer", // destination + "/com/contiautomotive/NodeStateManager/Consumer", // path + "com.contiautomotive.NodeStateManager.Consumer", // interface + method); // method + if(message != NULL) + { + dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId, + DBUS_TYPE_INT32, &status, + DBUS_TYPE_INVALID); + + if(conn != NULL) + { + if(!dbus_connection_send(conn, message, 0)) + { + fprintf(stderr, "send_lifecycle ==> Access denied: %s \n", error.message); + } + + dbus_connection_flush(conn); + } + else + { + fprintf(stderr, "send_lifecycle ==> ERROR: Invalid connection!! \n"); + } + dbus_message_unref(message); + } + else + { + fprintf(stderr, "send_lifecycle ==> ERROR: Invalid message!! \n"); + } + + return rval; +} + + + +int register_lifecycle() +{ + const char* objName = "objName"; + int shutdownMode = 88; + int TimeoutMs = 500; + return send_lifecycle_register("RegisterShutdownClient", + dbus_bus_get_unique_name(get_dbus_connection()), objName, shutdownMode, TimeoutMs); +} + + + +int unregister_lifecycle() +{ + const char* objName = "objName"; + int shutdownMode = 88; + + return send_lifecycle_un_register("UnRegisterShutdownClient", + dbus_bus_get_unique_name(get_dbus_connection()), objName, shutdownMode); +} + + +int send_prepare_shutdown_complete(int requestId) +{ + int status = 1; + + return send_lifecycle_request("LifecycleRequestComplete", requestId, status); +} + + + + +void process_prepare_shutdown(unsigned char requestId) +{ + int i = 0; + + // block write + pers_lock_access(); + + // flush open files to disk + for(i=0; i + +DBusHandlerResult checkLifecycleMsg(DBusConnection * connection, DBusMessage * message, void * user_data); + +int register_lifecycle(); + +int unregister_lifecycle(); + +void process_prepare_shutdown(unsigned char requestId); + + + + + +#endif /* PERSISTENCE_CLIENT_LIBRARY_LC_INTERFACE_H */ diff --git a/src/persistence_client_library_pas_interface.c b/src/persistence_client_library_pas_interface.c new file mode 100644 index 0000000..cd6614e --- /dev/null +++ b/src/persistence_client_library_pas_interface.c @@ -0,0 +1,298 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_pas_interface.c + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library persistence + * administration service interface. + * @see + */ + +#include "persistence_client_library_pas_interface.h" +#include "persistence_client_library_dbus_service.h" +#include "persistence_client_library.h" + +#include +#include + + +/// flag if access is locked +static int gLockAccess = 0; + + +int pers_data_sync() +{ + return 1; +} + +void pers_lock_access() +{ + __sync_fetch_and_add(&gLockAccess,1); +} + +void pers_unlock_access() +{ + __sync_fetch_and_sub(&gLockAccess,1); +} + +int isAccessLocked() +{ + return gLockAccess; +} + + +int check_pas_request(int request) +{ + int rval = 0; + + switch(request) + { + case (PasMsg_Block|PasMsg_WriteBack): + { + // add command to queue + static const int cmd = CMD_PAS_BLOCK_AND_WRITE_BACK; + if(sizeof(int)!=write(gPipefds[1], &cmd, sizeof(int))) + { + printf("write failed w/ errno %d\n", errno); + rval = PasErrorStatus_FAIL; + } + else + { + rval = PasErrorStatus_RespPend; + } + break; + } + case PasMsg_Unblock: + { + pers_unlock_access(); + rval = PasErrorStatus_OK; + break; + } + default: + rval = PasErrorStatus_FAIL; + } + return rval; +} + + +int msg_persAdminRequest(DBusConnection *connection, DBusMessage *message) +{ + int request = 0, + msgReturn = 0; + + DBusMessage *reply; + DBusError error; + dbus_error_init (&error); + + if (!dbus_message_get_args (message, &error, DBUS_TYPE_UINT16 , &request, DBUS_TYPE_INVALID)) + { + reply = dbus_message_new_error(message, error.name, error.message); + + if (reply == 0) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + if (!dbus_connection_send(connection, reply, 0)) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + dbus_message_unref (reply); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + msgReturn = check_pas_request(request); + + reply = dbus_message_new_method_return(message); + + if (reply == 0) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &msgReturn, DBUS_TYPE_INVALID)) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + if (!dbus_connection_send(connection, reply, 0)) + { + //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory")); + printf("DBus No memory\n"); + } + + dbus_connection_flush(connection); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; +} + + + + + +DBusHandlerResult checkPersAdminMsg(DBusConnection * connection, DBusMessage * message, void * user_data) +{ + DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + //printf("handleObjectPathMessage '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message)); + if((0==strncmp("org.genivi.persistence.admin", dbus_message_get_interface(message), 20))) + { + if((0==strncmp("PersistenceAdminRequest", dbus_message_get_member(message), 14))) + { + result = msg_persAdminRequest(connection, message); + } + else + { + printf("checkPersAdminMsg -> unknown message '%s'\n", dbus_message_get_interface(message)); + } + } + return result; +} + + + + +int send_pas_register(const char* method, const char* appname) +{ + int rval = 0; + + DBusError error; + dbus_error_init (&error); + DBusConnection* conn = get_dbus_connection(); + + DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin", // destination + "/org/genivi/persistence/admin", // path + "org.genivi.persistence.admin", // interface + method); // method + if(message != NULL) + { + dbus_message_append_args(message, DBUS_TYPE_STRING, &appname, DBUS_TYPE_INVALID); + + + if(conn != NULL) + { + if(!dbus_connection_send(conn, message, 0)) + { + fprintf(stderr, "send_pers_admin_service ==> Access denied: %s \n", error.message); + } + + dbus_connection_flush(conn); + } + else + { + fprintf(stderr, "send_pers_admin_service ==> ERROR: Invalid connection!! \n"); + } + dbus_message_unref(message); + } + else + { + fprintf(stderr, "send_pers_admin_service ==> ERROR: Invalid message!! \n"); + } + + return rval; +} + + + +int send_pas_request(const char* method, int blockStatus) +{ + int rval = 0; + + DBusError error; + dbus_error_init (&error); + DBusConnection* conn = get_dbus_connection(); + + DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin", // destination + "/org/genivi/persistence/admin", // path + "org.genivi.persistence.admin", // interface + method); // method + if(message != NULL) + { + dbus_message_append_args(message, DBUS_TYPE_UINT32, &blockStatus, DBUS_TYPE_INVALID); + + + if(conn != NULL) + { + if(!dbus_connection_send(conn, message, 0)) + { + fprintf(stderr, "send_pers_admin_service ==> Access denied: %s \n", error.message); + } + + dbus_connection_flush(conn); + } + else + { + fprintf(stderr, "send_pers_admin_service ==> ERROR: Invalid connection!! \n"); + } + dbus_message_unref(message); + } + else + { + fprintf(stderr, "send_pers_admin_service ==> ERROR: Invalid message!! \n"); + } + + return rval; +} + + + + +int register_pers_admin_service() +{ + return send_pas_register("RegisterPersAdminNotification", gAppId); +} + + +int unregister_pers_admin_service() +{ + return send_pas_register("UnRegisterPersAdminNotification", gAppId); +} + + +int pers_admin_service_data_sync_complete() +{ + return send_pas_request("PersistenceAdminRequestCompleted", 1); +} + + +void process_block_and_write_data_back() +{ + // lock persistence data access + pers_lock_access(); + // sync data back to memory device + pers_data_sync(); + // send complete notification + pers_admin_service_data_sync_complete(); +} + + diff --git a/src/persistence_client_library_pas_interface.h b/src/persistence_client_library_pas_interface.h new file mode 100644 index 0000000..7eabbe2 --- /dev/null +++ b/src/persistence_client_library_pas_interface.h @@ -0,0 +1,71 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_PAS_INTERFACE_H +#define PERSISTENCE_CLIENT_LIBRARY_PAS_INTERFACE_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + /** + * @file persistence_client_library_pas_interface.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Definition of the persistence client library persistence + * administration service interface. + * @see + */ + +#include + + + + +DBusHandlerResult checkPersAdminMsg(DBusConnection * connection, DBusMessage * message, void * user_data); + +/// synchronize data back to memory device +int pers_data_sync(); + +/// lock access to persistence data +void pers_lock_access(); + + +/// unlock access to persistent data +void pers_unlock_access(); + + +/// check if access to persistent data is locked +int isAccessLocked(); + + + +/// block persistence access and write data back to device +void process_block_and_write_data_back(); + + + +int register_pers_admin_service(); + +int unregister_pers_admin_service(); + + + +#endif /* PERSISTENCE_CLIENT_LIBRARY_PAS_INTERFACE_H */ diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..e15e2fa --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,15 @@ +AUTOMAKE_OPTIONS = foreign + +AM_CFLAGS =$(DEPS_CFLAGS) $(CHECK_CFLAGS) +#AM_CFLAGS = -fprofile-arcs -ftest-coverage $(DEPS_CFLAGS) $(CHECK_CFLAGS) + +noinst_PROGRAMS = persistence_client_library_test persistence_client_library_dbus_test +persistence_client_library_test_SOURCES = persistence_client_library_test.c +persistence_client_library_test_LDADD = $(DEPS_LIBS) $(CHECK_LIBS) \ + $(top_srcdir)/src/libpersistence_client_library.la + + +persistence_client_library_dbus_test_SOURCES = persistence_client_library_dbus_test.c +persistence_client_library_dbus_test_LDADD = $(DEPS_LIBS) $(top_srcdir)/src/libpersistence_client_library.la + +TESTS=persistence_client_library_test diff --git a/test/persistence_client_library_dbus_test.c b/test/persistence_client_library_dbus_test.c new file mode 100644 index 0000000..ebd8155 --- /dev/null +++ b/test/persistence_client_library_dbus_test.c @@ -0,0 +1,31 @@ +/* + * persistence_client_library_dbus_test.c + * + * Created on: Aug 8, 2012 + * Author: ihuerner + */ + + + +#include "../include/persistence_client_library_key.h" +#include "../include/persistence_client_library_file.h" + + +#include + + +int main(int argc, char *argv[]) +{ + int ret = 0; + char buffer[128]; + + printf("Dbus interface test application\n"); + + ret = key_read_data(0, "/language/current_language", 3, 0, buffer, 128); + + getchar(); + + + printf("By\n"); + return 1; +} diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c new file mode 100644 index 0000000..e0f2bcd --- /dev/null +++ b/test/persistence_client_library_test.c @@ -0,0 +1,259 @@ +/****************************************************************************** + * Project Persistency + * (c) copyright 2012 + * Company XS Embedded GmbH + *****************************************************************************/ +/****************************************************************************** +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + /** + * @file persistence_client_library_test.c + * @ingroup Persistence client library test + * @author Ingo Huerner + * @brief Test of persistence client library + * @see + */ + +#include +#include +#include +#include +#include /* exit */ +#include +#include + +#include "../include/persistence_client_library_key.h" +#include "../include/persistence_client_library_file.h" + + +#define BUF_SIZE 64 +#define NUM_OF_FILES 3 +#define READ_SIZE 256 + +char* dayOfWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; + +START_TEST (test_persGetData) +{ + int ret = 0; + unsigned char* buffer = NULL; + char sysTimeBuffer[128]; + buffer = malloc(READ_SIZE); + + struct tm *locTime; + time_t t = time(0); + + locTime = localtime(&t); + + snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900), + locTime->tm_hour, locTime->tm_min, locTime->tm_sec); + printf("\n\n"); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0xFF, "/pos/last_position", 0, 0, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/Node/pos/last position" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0, "/language/current_language", 3, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Shared/Public/wt.dconf" => "/User/3/language/current_language" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0xFF, "/status/open_document", 3, 2, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/User/3/Seat/2/status/open_document" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0x20, "/address/home_address", 4, 0, buffer, READ_SIZE); // "/Data/mnt-c/Shared/Group/20/cached.dconf" => "/User/4/address/home_address" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0xFF, "/pos/last satellites", 0, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-1/wt.gvdb" => "/Node/pos/last satellites" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0x84, "/links/last link", 2, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/links/last link" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0x84, "/links/last link", 2, 1, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/links/last link" + printf("T E S T Data: %s \n\n", buffer); + + memset(buffer, 0, READ_SIZE); + ret = key_write_data(0x84, "/links/last link", 2, 1, sysTimeBuffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/Seat/1/links/last link" + printf("T E S T Data: %s \n\n", sysTimeBuffer); + + memset(buffer, 0, READ_SIZE); + ret = key_read_data(0x84, "/links/last link", 2, 1, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/Seat/1/links/last link" + printf("T E S T Data: %s \n\n", buffer); + + + + free(buffer); +} +END_TEST + + + +START_TEST (test_persGetDataHandle) +{ + int ret = 0, handlePos = 0, handleLang = 0, handleStatus = 0, handleHome = 0; + unsigned char* buffer = NULL; + char sysTimeBuffer[128]; + buffer = malloc(READ_SIZE); + + struct tm *locTime; + time_t t = time(0); + + locTime = localtime(&t); + + snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900), + locTime->tm_hour, locTime->tm_min, locTime->tm_sec); + printf("\n\n"); + + memset(buffer, 0, READ_SIZE); + handlePos = key_handle_open(0xFF, "/posHandle/last_position", 0, 0); + ret = key_handle_read_data(handlePos, buffer, READ_SIZE); + printf("T E S T handle: %d | Data: %s \n\n", handlePos, buffer); + + memset(buffer, 0, READ_SIZE); + handleLang = key_handle_open(0, "/languageHandle/current_language", 3, 0); + ret = key_handle_read_data(handleLang, buffer, READ_SIZE); + printf("T E S T handle: %d | Data: %s \n\n", handleLang, buffer); + + memset(buffer, 0, READ_SIZE); + handleStatus = key_handle_open(0xFF, "/statusHandle/open_documen", 3, 2); + ret = key_handle_read_data(handleStatus, buffer, READ_SIZE); + printf("T E S T handle: %d | Data: %s \n\n", handleStatus, buffer); + + // close handle + key_handle_close(handleLang); + + memset(buffer, 0, READ_SIZE); + handleHome = key_handle_open(0x20, "/address/home_address", 4, 0); + ret = key_handle_read_data(handleHome, buffer, READ_SIZE); + printf("T E S T handle: %d | Data: %s \n\n", handleHome, buffer); + + free(buffer); +} +END_TEST + + + +START_TEST(test_persSetData) +{ + int ret = 0; + unsigned char* buffer = NULL; + buffer = malloc(READ_SIZE); + memset(buffer, 0, READ_SIZE); + + + free(buffer); +} +END_TEST + + + +START_TEST(test_persSetSharedData) +{ + int ret = 0; + unsigned char* buffer = NULL; + buffer = malloc(READ_SIZE); + + + memset(buffer, 0, READ_SIZE); + ret = key_write_data(0x20, "/address/home_address", 4, 0, buffer, READ_SIZE); + printf("T E S T Data: %s \n\n", buffer); + + free(buffer); +} +END_TEST + + + +START_TEST(test_persGetData_file) +{ + int ret = 0; + unsigned char* buffer = NULL; + buffer = malloc(READ_SIZE); + + + memset(buffer, 0, READ_SIZE); + + ret = file_open(0xFF, "/media/mediaDB.db", 1, 1); + printf("T E S T Data: %s \n\n", buffer); + + free(buffer); +} +END_TEST + + + + +static Suite * persistencyClientLib_suite() +{ + Suite * s = suite_create("Persistency client library"); + + TCase * tc_persGetData = tcase_create("persGetData"); + tcase_add_test(tc_persGetData, test_persGetData); + + TCase * tc_persGetDataHandle = tcase_create("persGetDataHandle"); + tcase_add_test(tc_persGetDataHandle, test_persGetDataHandle); + + TCase * tc_persSetData = tcase_create("persSetData"); + tcase_add_test(tc_persSetData, test_persSetData); + + TCase * tc_persSetSharedData = tcase_create("persSetSharedData"); + tcase_add_test(tc_persSetSharedData, test_persSetSharedData); + + TCase * tc_persGetData_file = tcase_create("persGetData_file"); + tcase_add_test(tc_persGetData_file, test_persGetData_file); + + suite_add_tcase(s, tc_persGetData); + suite_add_tcase(s, tc_persGetDataHandle); + suite_add_tcase(s, tc_persSetData); + suite_add_tcase(s, tc_persSetSharedData); + suite_add_tcase(s, tc_persGetData_file); + + return s; +} + + + + +int main(int argc, char *argv[]) +{ + int nr_failed; + + Suite * s = persistencyClientLib_suite(); + SRunner * sr = srunner_create(s); + srunner_run_all(sr, CK_NORMAL); + nr_failed = srunner_ntests_failed(sr); + srunner_free(sr); + + + getchar(); + + return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE; + +} + -- 2.7.4