1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 * This Source Code Form is subject to the terms of the
8 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
9 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
12 * @file persistence_client_library_pas_interface.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of the persistence client library persistence
16 * administration service interface.
20 #include "persistence_client_library_pas_interface.h"
21 #include "persistence_client_library_dbus_service.h"
23 #include "persistence_client_library_data_organization.h"
29 /// flag if access is locked
30 static int gLockAccess = 0;
32 void pers_lock_access(void)
34 __sync_fetch_and_add(&gLockAccess,1);
37 void pers_unlock_access(void)
39 __sync_fetch_and_sub(&gLockAccess,1);
42 int isAccessLocked(void)
48 int check_pas_request(unsigned int request, unsigned int requestID)
53 case (PasMsg_Block|PasMsg_WriteBack):
57 data.message.cmd = (uint32_t)CMD_PAS_BLOCK_AND_WRITE_BACK;
58 data.message.params[0] = request;
59 data.message.params[1] = requestID;
60 data.message.string[0] = '\0'; // no string parameter, set to 0
62 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("PCL: check_pas_request; case PasMsg_Block o. PasMsg_WriteBack"));
63 if(-1 == deliverToMainloop_NM(&data))
65 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("write failed w/ errno "), DLT_INT(errno), DLT_STRING(strerror(errno)));
66 rval = PasErrorStatus_FAIL;
70 rval = PasErrorStatus_RespPend;
76 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("PCL: check_pas_request; case PasMsg_Unblock"));
78 rval = PasErrorStatus_OK;
83 rval = PasErrorStatus_FAIL;
94 DBusHandlerResult msg_persAdminRequest(DBusConnection *connection, DBusMessage *message)
96 int request = 0, requestID = 0;
101 dbus_error_init (&error);
104 if (!dbus_message_get_args(message, &error, DBUS_TYPE_INT32 , &request,
105 DBUS_TYPE_INT32 , &requestID,
108 reply = dbus_message_new_error(message, error.name, error.message);
112 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_persAdminRequest => DBus No memory"));
115 if (!dbus_connection_send(connection, reply, 0))
117 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_persAdminRequest => DBus No memory"));
120 dbus_message_unref(reply);
122 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
124 errorReturn = check_pas_request(request, requestID);
127 reply = dbus_message_new_method_return(message);
131 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_persAdminRequest => DBus No memory"));
134 if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &errorReturn, DBUS_TYPE_INVALID))
136 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_persAdminRequest => DBus No memory"));
139 if (!dbus_connection_send(connection, reply, 0))
141 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_persAdminRequest => DBus No memory"));
144 dbus_connection_flush(connection);
145 dbus_message_unref(reply);
147 return DBUS_HANDLER_RESULT_HANDLED;
151 int signal_persModeChange(DBusConnection *connection, DBusMessage *message)
153 int persistenceMode = 0;
158 dbus_error_init (&error);
160 if (!dbus_message_get_args (message, &error, DBUS_TYPE_INT32 , &persistenceMode,
163 reply = dbus_message_new_error(message, error.name, error.message);
167 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("signal_persModeChange => DBus No memory"));
170 if (!dbus_connection_send(connection, reply, 0))
172 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("signal_persModeChange => DBus No memory"));
175 dbus_message_unref(reply);
177 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
180 reply = dbus_message_new_method_return(message);
184 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("signal_persModeChange => DBus No memory"));
187 if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &errorCode, DBUS_TYPE_INVALID))
189 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("signal_persModeChange => DBus No memory"));
192 if (!dbus_connection_send(connection, reply, 0))
194 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("signal_persModeChange => DBus No memory"));
197 dbus_connection_flush(connection);
198 dbus_message_unref(reply);
200 return DBUS_HANDLER_RESULT_HANDLED;
204 DBusHandlerResult checkPersAdminMsg(DBusConnection * connection, DBusMessage * message, void * user_data)
206 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
210 if((0==strcmp(gDbusPersAdminConsInterface, dbus_message_get_interface(message))))
212 if((0==strcmp(gDbusPersAdminConsMsg, dbus_message_get_member(message))))
214 result = msg_persAdminRequest(connection, message);
218 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("checkPersAdminMsg => unknown message"), DLT_STRING(dbus_message_get_member(message)));
223 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("checkPersAdminMsg => unknown message"), DLT_STRING(dbus_message_get_interface(message)));
231 int register_pers_admin_service(void)
237 data.message.cmd = (uint32_t)CMD_SEND_PAS_REGISTER;
238 data.message.params[0] = 1;
239 data.message.params[1] = (PasMsg_Block | PasMsg_WriteBack | PasMsg_Unblock);
240 data.message.string[0] = '\0'; // no string parameter, set to 0
242 if(-1 == deliverToMainloop(&data))
244 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("register_pers_admin_service => failed to write to pipe"), DLT_INT(errno));
249 pthread_mutex_lock(&gDbusPendingRegMtx); // block until pending received
250 rval = gDbusPendingRvalue;
257 int unregister_pers_admin_service(void)
262 data.message.cmd = (uint32_t)CMD_SEND_PAS_REGISTER;
263 data.message.params[0] = 0;
264 data.message.params[1] = (PasMsg_Block | PasMsg_WriteBack | PasMsg_Unblock);
265 data.message.string[0] = '\0'; // no string parameter, set to 0
267 if(-1 == deliverToMainloop(&data))
269 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("unregister_pers_admin_service => failed to write to pipe"), DLT_INT(errno));
274 pthread_mutex_lock(&gDbusPendingRegMtx); // block until pending received
275 rval = gDbusPendingRvalue;