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