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