diff options
author | ihuerner <ihuerner@b9707d8c-442e-47db-affb-152dabb2260b> | 2012-08-09 11:05:42 +0000 |
---|---|---|
committer | ihuerner <ihuerner@b9707d8c-442e-47db-affb-152dabb2260b> | 2012-08-09 11:05:42 +0000 |
commit | 715e5666dcb9fe8a0e7ac351658d4c359fdb9354 (patch) | |
tree | 9f01147b469561f1bf1cb7645a3430b4638bf9c7 | |
download | persistence-client-library-715e5666dcb9fe8a0e7ac351658d4c359fdb9354.tar.gz |
Initial version of persistence client library on sourceforge (rev. 1281)
31 files changed, 3752 insertions, 0 deletions
@@ -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 --- /dev/null +++ b/ChangeLog 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/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 Binary files differnew file mode 100644 index 0000000..c8d11a2 --- /dev/null +++ b/data/Data.tar.gz diff --git a/include/persistence_client_library_file.h b/include/persistence_client_library_file.h new file mode 100644 index 0000000..0059de6 --- /dev/null +++ b/include/persistence_client_library_file.h @@ -0,0 +1,178 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_FILE_H +#define PERSISTENCE_CLIENT_LIBRARY_FILE_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2011 + * 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.h + * @ingroup Persistence client library + * @author Ingo Huerner (XSe) / Guy Sagnes (Continental) + * @brief Header of the persistence client library. + * Library provides an API to access persistent data + * @see + */ + +#ifdef __cplusplus +extern "C" { +#endif + + +#define PERSIST_FILEAPI_INTERFACE_VERSION (0x01000000U) + + + +/** + * @brief close the given POSIX file descriptor + * + * @param fd the file descriptor to close + * + * @return zero on success. On error, -1 is returned, and errno is set appropriately + */ +int file_close(int fd); + + + +/** + * @brief get the size of the file given by the file descriptor + * + * @param fd the POSIX file descriptor + * + * @return positive value. On error, -1 is returned, and errno is set appropriately + */ +int file_get_size(int fd); + + + +/** + * @brief map a file into the memory + * + * @param addr if NULL, kernel chooses address + * @param size the size in bytes to map into the memory + * @param offset in the file to map + * @param fd the POSIX file descriptor of the file to map + * + * @return a pointer to the mapped area, or on error the value MAP_FAILED + */ +void* file_map_data(void* addr, long size, long offset, int fd); + + + +/** + * @brief open a file + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * + * @return positive value: the POSIX file descriptor; negative value: Error code + */ +int file_open(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no); + + + +/** + * @brief read persistent data from a file + * + * @param fd POSIX file descriptor + * @param buffer buffer to read the data + * @param buffer_size the size buffer for reading + * + * @return positive value: the size read; negative value: error code + */ +int file_read_data(int fd, void * buffer, unsigned long buffer_size); + + + +/** + * @brief remove the file + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * + * @return positive value: success; negative value: error code + * + */ +int file_remove(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no); + + + +/** + * @brief reposition the file descriptor + * + * @param fd the POSIX file descriptor + * @param offset the reposition offset + * @param whence the direction to reposition + SEEK_SET + The offset is set to offset bytes. + SEEK_CUR + The offset is set to its current location plus offset bytes. + SEEK_END + The offset is set to the size of the file plus offset bytes. + * + * @return positive value: resulting offset location; negative value: error code + */ +int file_seek(int fd, long int offset, int whence); + + + +/** + * @brief unmap the file from the memory + * + * @param address the address to unmap + * @param size the size in bytes to unmap + * + * @return on success 0; negative value: error code + */ +int file_unmap_data(void* address, long size); + + + +/** + * @brief write persistent data to file + * + * @param fd the POSIX file descriptor + * @param buffer the buffer to write + * @param buffer_size the size of the buffer to write in bytes + * +* @return positive value: bytes written; negative value: error code + */ +int file_write_data(int fd, const void * buffer, unsigned long buffer_size); + + + + + + + +#ifdef __cplusplus +} +#endif + + +#endif /* PERSISTENCY_CLIENT_LIBRARY_FILE_H */ + diff --git a/include/persistence_client_library_key.h b/include/persistence_client_library_key.h new file mode 100644 index 0000000..febe623 --- /dev/null +++ b/include/persistence_client_library_key.h @@ -0,0 +1,196 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_KEY_H +#define PERSISTENCE_CLIENT_LIBRARY_KEY_H + +/****************************************************************************** + * Project Persistency + * (c) copyright 2011 + * 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.h + * @ingroup Persistence client library + * @author Ingo Huerner (XSe) / Guy Sagnes (Continental) + * @brief Header of the persistence client library. + * Library provides an API to access persistent data + * @see + */ + +#ifdef __cplusplus +extern "C" { +#endif + + +#define PERSIST_KEYVALUEAPI_INTERFACE_VERSION (0x01000000U) + + + +/** + * @brief delete persistent data + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * + * @return positive value: success; negative value: error code + */ +int key_delete(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no); + + + +/** + * @brief gets the size of persistent data in bytes + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * + * @return positive value: the size; negative value: error code + */ +int key_get_size(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no); + +/** + * @brief close the access to a key-value identified by key handle + * + * @param key_handle key value handle return by key_handle_open() + * + * @return positive value: success; negative value: error code + */ +int key_handle_close(int key_handle); + + + +/** + * @brief gets the size of persistent data in bytes identified by key handle + * + * @param key_handle key value handle return by key_handle_open() + * + * @return positive value: the size; negative value: error code + */ +int key_handle_get_size(int key_handle); + + + +/** + * @brief open a key-value + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * + * @return positive value: the key handle to access the value; negative value: Error code + */ +int key_handle_open(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no); + + + +/** + * @brief reads persistent data identified by key handle + * + * @param key_handle key value handle return by key_handle_open() + * @param buffer the buffer for persistent data + * @param buffer_size size of buffer for reading + * + * @return positive value: the bytes read; negative value: error code + */ +int key_handle_read_data(int key_handle, unsigned char* buffer, unsigned long buffer_size); + + + +/** + * @brief register a change notification for persistent data + * + * @param key_handle key value handle return by key_handle_open() + * + * @return positive value: registration OK; negative value: error code + */ +int key_handle_register_notify_on_change(int key_handle); + + + +/** + * @brief writes persistent data identified by key handle + * + * @param key_handle key value handle return by key_handle_open() + * @param buffer the buffer containing the persistent data to write + * @param buffer_size the number of bytes to write + * + * @return positive value: the bytes written; negative value: error code + */ +int key_handle_write_data(int key_handle, unsigned char* buffer, unsigned long buffer_size); + + + +/** + * @brief reads persistent data identified by ldbid and resource_id + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * @param buffer the buffer to read the persistent data + * @param buffer_size size of buffer for reading + * + * @return positive value: the bytes read; negative value: error code + */ +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); + + + +/** + * @brief register a change notification for persistent data + * + * @param ldbid logical database ID of the resource to monitor + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * + * @return positive value: registration OK; negative value: error code + */ +int key_register_notify_on_change(unsigned char ldbid, char* resource_id, unsigned char user_no, unsigned char seat_no); + + + +/** + * @brief writes persistent data identified by ldbid and resource_id + * + * @param ldbid logical database ID + * @param resource_id the resource ID + * @param user_no the user ID + * @param seat_no the seat number (seat 0 to 3) + * @param buffer the buffer containing the persistent data to write + * @param buffer_size the number of bytes to write + * + * @return positive value: the bytes written; negative value: error code + */ +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); + + +#ifdef __cplusplus +} +#endif + + +#endif /* PERSISTENCY_CLIENT_LIBRARY_KEY_H */ + diff --git a/persistence_client_library.pc.in b/persistence_client_library.pc.in new file mode 100644 index 0000000..819fc9a --- /dev/null +++ b/persistence_client_library.pc.in @@ -0,0 +1,13 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ +datarootdir=@datarootdir@ +datadir=@datadir@ + +Name: persistence client library +Description: persistence client library +Version: @VERSION@ +Requires: automotive-dlt glib-2.0 +Libs: -L${libdir} -lmygvdb -lrt -lglib-2.0 -ldl +Cflags: -I${includedir} diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..64c21a1 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,30 @@ +AUTOMAKE_OPTIONS = foreign + +AM_CFLAGS =$(DEPS_CFLAGS) -Wall -I../include \ + -I/usr/include/glib-2.0 \ + -I/usr/include/dbus-1.0 \ + -I/usr/include/dconf-dbus-1 \ + -I/usr/lib/glib-2.0/include + + +include_HEADERS = ../include/persistence_client_library_key.h ../include/persistence_client_library_file.h + + +lib_LTLIBRARIES = libpersistence_client_library.la + +libpersistence_client_library_la_LIBADD = $(DEPS_LIBS) -ldconf -ldl -ldconf-dbus-1 -lmygvdb + +libpersistence_client_library_la_SOURCES = \ + persistence_client_library.c \ + persistence_client_library_key.c \ + persistence_client_library_file.c \ + persistence_client_library_data_access.c \ + persistence_client_library_handle.c \ + persistence_client_library_lc_interface.c \ + persistence_client_library_pas_interface.c \ + persistence_client_library_dbus_service.c \ + crc32.c + +libpersistence_client_library_la_LDFLAGS = -module -export-dynamic $(LDFLAGS) + + diff --git a/src/crc32.c b/src/crc32.c new file mode 100644 index 0000000..b951823 --- /dev/null +++ b/src/crc32.c @@ -0,0 +1,135 @@ +/*- + * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or + * code or tables extracted from it, as desired without restriction. + * + * First, the polynomial itself and its table of feedback terms. The + * polynomial is + * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 + * + * Note that we take it "backwards" and put the highest-order term in + * the lowest-order bit. The X^32 term is "implied"; the LSB is the + * X^31 term, etc. The X^0 term (usually shown as "+1") results in + * the MSB being 1 + * + * Note that the usual hardware shift register implementation, which + * is what we're using (we're merely optimizing it by doing eight-bit + * chunks at a time) shifts bits into the lowest-order term. In our + * implementation, that means shifting towards the right. Why do we + * do it this way? Because the calculated CRC must be transmitted in + * order from highest-order term to lowest-order term. UARTs transmit + * characters in order from LSB to MSB. By storing the CRC this way + * we hand it to the UART in the order low-byte to high-byte; the UART + * sends each low-bit to hight-bit; and the result is transmission bit + * by bit from highest- to lowest-order term without requiring any bit + * shuffling on our part. Reception works similarly + * + * The feedback terms table consists of 256, 32-bit entries. Notes + * + * The table can be generated at runtime if desired; code to do so + * is shown later. It might not be obvious, but the feedback + * terms simply represent the results of eight shift/xor opera + * tions for all combinations of data and CRC register values + * + * The values must be right-shifted by eight bits by the "updcrc + * logic; the shift must be unsigned (bring in zeroes). On some + * hardware you could probably optimize the shift in assembler by + * using byte-swap instructions + * polynomial $edb88320 + * + * + * CRC32 code derived from work by Gary S. Brown. + */ + + +/* + * File has been copied from: + * http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/libkern/crc32.c + */ + +/* + * Modified parts of this files by XS Embedded GmbH, 2012 + */ + + +#include "crc32.h" + + +enum crc32ConstantDefinition +{ + crc32_array_size = 255 +}; + + +static unsigned int crc32_tab[] = +{ + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +const unsigned int crc32(unsigned int crc, const unsigned char *buf, size_t theSize) +{ + const unsigned char *p = 0; + unsigned int rval = 0; + + p = buf; + crc = crc ^ ~0U; + + if(p != 0) + { + while(theSize--) + { + unsigned int index = (crc ^ *p++) & 0xFF; + + if(index < crc32_array_size) + crc = crc32_tab[index] ^ (crc >> 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 <string.h> + +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 <string.h> +#include <errno.h> +#include <dlfcn.h> +#include <stdlib.h> + +#include <dlt/dlt.h> +#include <dlt/dlt_common.h> + + +/// 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/<user_no_parameter> 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/<user_no_parameter>/Seat/<seat_no_parameter> 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. + // /<LDBID parameter> 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/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> 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/<LDBID_parameter> 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 <gvdb-reader.h> +#include "crc32.h" + +#include <string.h> +#include <stdio.h> + + +/// 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/<appId>/<database_name> +static const char* gLocalCachePath = "/Data/mnt-c/%s%s"; +/// path prefic for local write through database /Data/mnt_wt/<appId>/<database_name> +static const char* gLocalWtPath = "/Data/mnt-wt/%s%s"; +/// path prefic for shared cached database: /Data/mnt_c/Shared/Group/<group_no>/<database_name> +static const char* gSharedCachePath = "/Data/mnt-c/Shared/Group/%x%s"; +/// path prefic for shared write through database: /Data/mnt_wt/Shared/Group/<group_no>/<database_name> +static const char* gSharedWtPath = "/Data/mnt-wt/Shared/Group/%x%s"; +/// path prefic for shared public cached database: /Data/mnt_c/Shared/Public//<database_name> +static const char* gSharedPublicCachePath = "/Data/mnt-c/Shared/Public%s"; +/// path prefic for shared public write through database: /Data/mnt_wt/Shared/Public/<database_name> +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 <string.h> +#include <stdio.h> +#include <dconf-dbus-1.h> + + + + +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 <stdio.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> + + +/// 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 <dbus/dbus.h> +#include <poll.h> + +/// 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 <fcntl.h> // for open flags +#include <errno.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> + + + +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 <errno.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + + +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<maxPersHandle; i++) + { + if(gOpenFdArray[i] == FileOpen) + { + fsync(i); + close(i); + } + } + + // close open gvdb and dconf database + // TODO + + // notify lifecycle shutdown OK + send_prepare_shutdown_complete((int)requestId); +} + diff --git a/src/persistence_client_library_lc_interface.h b/src/persistence_client_library_lc_interface.h new file mode 100644 index 0000000..5f2a4e3 --- /dev/null +++ b/src/persistence_client_library_lc_interface.h @@ -0,0 +1,50 @@ +#ifndef PERSISTENCE_CLIENT_LIBRARY_LC_INTERFACE_H +#define PERSISTENCE_CLIENT_LIBRARY_LC_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_lc_interface.h + * @ingroup Persistence client library + * @author Ingo Huerner + * @brief Implementation of the persistence client library lifecycle interface. + * @see + */ + +#include <dbus/dbus.h> + +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 <errno.h> +#include <unistd.h> + + +/// 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 <dbus/dbus.h> + + + + +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 <stdio.h> + + +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 <organization> 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 <COPYRIGHT HOLDER> 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> /* exit */ +#include <check.h> +#include <time.h> + +#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; + +} + |