File backups on seperate partition; Fixed bug 146; created performance benchmark...
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_dbus_cmd.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
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 ******************************************************************************/
11  /**
12  * @file           persistence_client_library_dbus_cmd.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of the persistence client library dbus commands.
16  * @see
17  */
18
19 #include "persistence_client_library_dbus_cmd.h"
20
21 #include "persistence_client_library_handle.h"
22 #include "persistence_client_library_itzam_errors.h"
23 #include "persistence_client_library_custom_loader.h"
24 #include "persistence_client_library_prct_access.h"
25
26 #include "../include_protected/persistence_client_library_data_organization.h"
27 #include "../include_protected/persistence_client_library_db_access.h"
28
29 #include <itzam.h>
30
31
32 // function prototype
33 void msg_pending_func(DBusPendingCall *call, void *data);
34
35
36
37 void process_reg_notification_signal(DBusConnection* conn)
38 {
39    char ruleChanged[DbusMatchRuleSize] = {0};
40    char ruleDeleted[DbusMatchRuleSize] = {0};
41    char ruleCreated[DbusMatchRuleSize] = {0};
42
43    // add match for  c h a n g e
44    snprintf(ruleChanged, DbusMatchRuleSize,
45             "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResChange',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
46             gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
47
48    // add match for  d e l e t e
49    snprintf(ruleDeleted, DbusMatchRuleSize,
50             "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResDelete',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
51             gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
52
53    // add match for  c r e a t e
54    snprintf(ruleCreated, DbusMatchRuleSize,
55             "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResCreate',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
56             gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
57
58    if(gNotifyPolicy == Notify_register)
59    {
60       dbus_bus_add_match(conn, ruleChanged, NULL);
61       dbus_bus_add_match(conn, ruleDeleted, NULL);
62       dbus_bus_add_match(conn, ruleCreated, NULL);
63    }
64    else if(gNotifyPolicy == Notify_unregister)
65    {
66       dbus_bus_remove_match(conn, ruleChanged, NULL);
67       dbus_bus_remove_match(conn, ruleDeleted, NULL);
68       dbus_bus_remove_match(conn, ruleCreated, NULL);
69    }
70
71    dbus_connection_flush(conn);  // flush the connection to add the match
72 }
73
74
75
76 void process_send_notification_signal(DBusConnection* conn)
77 {
78    dbus_bool_t ret;
79    DBusMessage* message;
80    const char* notifyReason = NULL;
81
82    char ldbidArray[DbusSubMatchSize] = {0};
83    char userArray[DbusSubMatchSize]  = {0};
84    char seatArray[DbusSubMatchSize]  = {0};
85    char* pldbidArra = ldbidArray;
86    char* puserArray = userArray;
87    char* pseatArray = seatArray;
88    char* pnotifyKey = gNotifykey;
89
90    switch(gNotifyReason)
91    {
92       case pclNotifyStatus_deleted:
93          notifyReason = gDeleteSignal;
94          break;
95       case  pclNotifyStatus_created:
96          notifyReason = gCreateSignal;
97          break;
98       case pclNotifyStatus_changed:
99          notifyReason = gChangeSignal;
100          break;
101       default:
102          notifyReason = NULL;
103          break;
104    }
105
106    if(notifyReason != NULL)
107    {
108       // dbus_bus_add_match is used for the notification mechanism,
109       // and this works only for type DBUS_TYPE_STRING as message arguments
110       // this is the reason to use string instead of integer types directly
111       snprintf(ldbidArray, DbusSubMatchSize, "%u", gNotifyLdbid);
112       snprintf(userArray,  DbusSubMatchSize, "%u", gNotifyUserNo);
113       snprintf(seatArray,  DbusSubMatchSize, "%u", gNotifySeatNo);
114
115       //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", gNotifykey, gLdbid, gUserNo, gSeatNo, gReason);
116       message = dbus_message_new_signal("/org/genivi/persistence/adminconsumer",    // const char *path,
117                                         "org.genivi.persistence.adminconsumer",     // const char *interface,
118                                         notifyReason);                                 // const char *name
119
120       ret = dbus_message_append_args(message, DBUS_TYPE_STRING, &pnotifyKey,
121                                               DBUS_TYPE_STRING, &pldbidArra,
122                                               DBUS_TYPE_STRING, &puserArray,
123                                               DBUS_TYPE_STRING, &pseatArray,
124                                               DBUS_TYPE_INVALID);
125       if(ret == TRUE)
126       {
127          // Send the signal
128          if(conn != NULL)
129          {
130             if(dbus_connection_send(conn, message, 0) == TRUE)
131             {
132                // Free the signal now we have finished with it
133                dbus_message_unref(message);
134             }
135             else
136             {
137                DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
138             }
139          }
140          else
141          {
142             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> E R R O R  C O N E C T I O N  NULL!!"));
143          }
144       }
145       else
146       {
147          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
148       }
149    }
150    else
151    {
152       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR invalid notification reason"));
153    }
154 }
155
156
157
158 void process_block_and_write_data_back(unsigned int requestID, unsigned int status)
159 {
160    // lock persistence data access
161    pers_lock_access();
162    // sync data back to memory device
163    pers_data_sync();
164    // send complete notification
165    //pers_admin_service_data_sync_complete(requestID, status);
166 }
167
168
169
170 void process_prepare_shutdown(unsigned char requestId, unsigned int status)
171 {
172    int i = 0;
173
174    // block write
175    pers_lock_access();
176
177    // flush open files to disk
178    for(i=0; i<MaxPersHandle; i++)
179    {
180       int tmp = i;
181       if(gOpenFdArray[tmp] == FileOpen)
182       {
183          fsync(tmp);
184          close(tmp);
185       }
186    }
187
188    // close all opend rct
189    pers_rct_close_all();
190
191    // close opend database
192    pers_db_close_all();
193
194
195    // unload custom client libraries
196    for(i=0; i<PersCustomLib_LastEntry; i++)
197    {
198       if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
199       {
200          // deinitialize plugin
201          gPersCustomFuncs[i].custom_plugin_deinit();
202          // close library handle
203          dlclose(gPersCustomFuncs[i].handle);
204
205          invalidate_custom_plugin(i);
206       }
207    }
208 }
209
210
211
212 void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int status)
213 {
214    DBusError error;
215    dbus_error_init (&error);
216    int rval = 0;
217
218    DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",       // destination
219                                                       "/org/genivi/persistence/admin",       // path
220                                                        "org.genivi.persistence.admin",       // interface
221                                                        "PersistenceAdminRequestCompleted");  // method
222    if(conn != NULL)
223    {
224       if(message != NULL)
225       {
226          dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestID,
227                                            DBUS_TYPE_INT32,  &status,
228                                            DBUS_TYPE_INVALID);
229
230          if(!dbus_connection_send(conn, message, 0))
231          {
232             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
233             rval = -1;
234          }
235
236          dbus_connection_flush(conn);
237          dbus_message_unref(message);
238       }
239       else
240       {
241          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
242       }
243    }
244    else
245    {
246       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
247    }
248 }
249
250
251 void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag)
252 {
253    DBusError error;
254    dbus_error_init (&error);
255    DBusPendingCall* pending = NULL;
256
257    char* method = NULL;
258
259    if(regType == 0)
260       method = "UnRegisterPersAdminNotification";
261    else if(regType == 1)
262       method = "RegisterPersAdminNotification";
263
264    if(conn != NULL)
265    {
266       const char* objName = "/org/genivi/persistence/adminconsumer";
267       const char* busName = dbus_bus_get_unique_name(conn);
268
269       if(busName != NULL)
270       {
271          DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",    // destination
272                                                             "/org/genivi/persistence/admin",    // path
273                                                              "org.genivi.persistence.admin",    // interface
274                                                              method);                           // method
275
276          if(message != NULL)
277          {
278             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,  // bus name
279                                               DBUS_TYPE_STRING, &objName,
280                                               DBUS_TYPE_INT32,  &notificationFlag,
281                                               DBUS_TYPE_UINT32, &gTimeoutMs,
282                                               DBUS_TYPE_INVALID);
283
284             dbus_connection_send_with_reply(conn,           //    the connection
285                                             message,        // the message to write
286                                             &pending,       // pending
287                                             gTimeoutMs);    // timeout in milliseconds or -1 for default
288
289             dbus_connection_flush(conn);
290
291             if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
292             {
293                DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n") );
294             }
295             dbus_pending_call_unref(pending);
296          }
297          else
298          {
299             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid message") );
300          }
301          dbus_message_unref(message);
302       }
303       else
304       {
305          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid busname") );
306       }
307    }
308    else
309    {
310       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid connection") );
311    }
312 }
313
314
315 void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
316 {
317    int rval = 0;
318
319    DBusError error;
320    dbus_error_init (&error);
321
322    char* method = NULL;
323
324    if(regType == 1)
325       method = "RegisterShutdownClient";
326    else if(regType == 0)
327       method = "UnRegisterShutdownClient";
328
329    if(conn != NULL)
330    {
331       const char* objName = "/org/genivi/NodeStateManager/LifeCycleConsumer";
332       const char* busName = dbus_bus_get_unique_name(conn);
333
334       DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
335                                                           "/org/genivi/NodeStateManager/Consumer", // path
336                                                           "org.genivi.NodeStateManager.Consumer",  // interface
337                                                           method);                                 // method
338       if(message != NULL)
339       {
340          if(regType == 1)   // register
341          {
342             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
343                                               DBUS_TYPE_STRING, &objName,
344                                               DBUS_TYPE_UINT32, &shutdownMode,
345                                               DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
346          }
347          else           // unregister
348          {
349             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
350                                               DBUS_TYPE_STRING, &objName,
351                                               DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
352          }
353
354                    if(!dbus_connection_send(conn, message, 0))
355                    {
356                       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
357                       rval = -1;
358                    }
359                    dbus_connection_flush(conn);
360          dbus_message_unref(message);
361       }
362       else
363       {
364          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
365       }
366    }
367    else
368    {
369       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
370    }
371 }
372
373
374
375 void process_send_lifecycle_request(DBusConnection* conn, int requestId, int status)
376 {
377    int rval = 0;
378    DBusError error;
379    dbus_error_init (&error);
380
381    if(conn != NULL)
382    {
383       DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
384                                                          "/org/genivi/NodeStateManager/Consumer",  // path
385                                                           "org.genivi.NodeStateManager.Consumer",  // interface
386                                                           "LifecycleRequestComplete");             // method
387       if(message != NULL)
388       {
389          dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId,
390                                            DBUS_TYPE_INT32, &status,
391                                            DBUS_TYPE_INVALID);
392
393
394          if(!dbus_connection_send(conn, message, 0))
395          {
396             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
397             rval = -1;
398           }
399
400           dbus_connection_flush(conn);
401           dbus_message_unref(message);
402       }
403       else
404       {
405          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
406          rval = -1;
407       }
408    }
409    else
410    {
411       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
412       rval = -1;
413    }
414 }
415
416
417
418 void msg_pending_func(DBusPendingCall *call, void *data)
419 {
420    int replyArg = -1;
421    DBusError err;
422    dbus_error_init(&err);
423
424    DBusMessage *message = dbus_pending_call_steal_reply(call);
425
426    if (dbus_set_error_from_message(&err, message))
427    {
428       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> Access denied") );
429    }
430    else
431    {
432       //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("msg_pending_func ==> UNlock mutex") );
433       dbus_message_get_args(message, &err, DBUS_TYPE_INT32, &replyArg, DBUS_TYPE_INVALID);
434    }
435
436    gDbusPendingRvalue = replyArg;   // set the return value
437    dbus_message_unref(message);
438
439    // unlock the mutex because we have received the reply to the dbus message
440    pthread_mutex_unlock(&gDbusPendingRegMtx);
441 }
442
443
444
445