Added pclLifecycleSet function
[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_custom_loader.h"
23 #include "persistence_client_library_pas_interface.h"
24
25 #include "persistence_client_library_data_organization.h"
26 #include "persistence_client_library_db_access.h"
27
28 #include <dlfcn.h>
29 #include <errno.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(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
138             }
139          }
140          else
141          {
142             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!!"));
143          }
144       }
145       else
146       {
147          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
148       }
149    }
150    else
151    {
152       DLT_LOG(gPclDLTContext, 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    (void)requestID;
161    (void)status;
162    // lock persistence data access
163    pers_lock_access();
164    // sync data back to memory device
165    pers_data_sync();
166    // send complete notification
167    //pers_admin_service_data_sync_complete(requestID, status);
168 }
169
170
171
172 void process_prepare_shutdown(int complete)
173 {
174    int i = 0, rval = 0;
175
176    // block write
177    pers_lock_access();
178
179    // flush open files to disk
180    for(i=0; i<MaxPersHandle; i++)
181    {
182       int tmp = i;
183       if(gOpenFdArray[tmp] == FileOpen)
184       {
185          fsync(tmp);
186
187 #if USE_FILECACHE
188          rval = pfcCloseFile(tmp);
189 #else
190          if(complete > 0)
191          {
192                 rval = close(tmp);
193          }
194 #endif
195          if(rval == -1)
196          {
197                 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close file: "), DLT_STRING(strerror(errno)) );
198          }
199
200       }
201    }
202
203    // close all opend rct
204    pers_rct_close_all();
205
206    // close opend database
207    database_close_all();
208
209    if(complete > 0)
210    {
211         close_all_persistence_handle();
212    }
213
214
215    if(complete > 0)
216    {
217                 // unload custom client libraries
218                 for(i=0; i<PersCustomLib_LastEntry; i++)
219                 {
220                         if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
221                         {
222                                 // deinitialize plugin
223                                 gPersCustomFuncs[i].custom_plugin_deinit();
224                                 // close library handle
225                                 dlclose(gPersCustomFuncs[i].handle);
226
227                                 invalidate_custom_plugin(i);
228                         }
229                 }
230    }
231 }
232
233
234
235 void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int status)
236 {
237    DBusError error;
238    dbus_error_init (&error);
239
240    DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",       // destination
241                                                       "/org/genivi/persistence/admin",       // path
242                                                        "org.genivi.persistence.admin",       // interface
243                                                        "PersistenceAdminRequestCompleted");  // method
244    if(conn != NULL)
245    {
246       if(message != NULL)
247       {
248          dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestID,
249                                            DBUS_TYPE_INT32,  &status,
250                                            DBUS_TYPE_INVALID);
251
252          if(!dbus_connection_send(conn, message, 0))
253          {
254             DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
255          }
256
257          dbus_connection_flush(conn);
258          dbus_message_unref(message);
259       }
260       else
261       {
262          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
263       }
264    }
265    else
266    {
267       DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
268    }
269 }
270
271
272 void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag)
273 {
274    DBusError error;
275    dbus_error_init (&error);
276    DBusPendingCall* pending = NULL;
277
278    char* method = NULL;
279
280    if(regType == 0)
281       method = "UnRegisterPersAdminNotification";
282    else if(regType == 1)
283       method = "RegisterPersAdminNotification";
284
285    if(conn != NULL)
286    {
287       const char* objName = "/org/genivi/persistence/adminconsumer";
288       const char* busName = dbus_bus_get_unique_name(conn);
289
290       if(busName != NULL)
291       {
292          DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",    // destination
293                                                             "/org/genivi/persistence/admin",    // path
294                                                              "org.genivi.persistence.admin",    // interface
295                                                              method);                           // method
296
297          if(message != NULL)
298          {
299             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,  // bus name
300                                               DBUS_TYPE_STRING, &objName,
301                                               DBUS_TYPE_INT32,  &notificationFlag,
302                                               DBUS_TYPE_UINT32, &gTimeoutMs,
303                                               DBUS_TYPE_INVALID);
304
305             dbus_connection_send_with_reply(conn,           //    the connection
306                                             message,        // the message to write
307                                             &pending,       // pending
308                                             gTimeoutMs);    // timeout in milliseconds or -1 for default
309
310             dbus_connection_flush(conn);
311
312             if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
313             {
314                DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n") );
315             }
316             dbus_pending_call_unref(pending);
317          }
318          else
319          {
320             DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid message") );
321          }
322          dbus_message_unref(message);
323       }
324       else
325       {
326          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid busname") );
327       }
328    }
329    else
330    {
331       DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid connection") );
332    }
333 }
334
335
336 void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
337 {
338    DBusError error;
339    dbus_error_init (&error);
340
341    char* method = NULL;
342
343    if(regType == 1)
344       method = "RegisterShutdownClient";
345    else if(regType == 0)
346       method = "UnRegisterShutdownClient";
347
348    if(conn != NULL)
349    {
350       const char* objName = "/org/genivi/NodeStateManager/LifeCycleConsumer";
351       const char* busName = dbus_bus_get_unique_name(conn);
352
353       DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
354                                                           "/org/genivi/NodeStateManager/Consumer", // path
355                                                           "org.genivi.NodeStateManager.Consumer",  // interface
356                                                           method);                                 // method
357       if(message != NULL)
358       {
359          if(regType == 1)   // register
360          {
361             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
362                                               DBUS_TYPE_STRING, &objName,
363                                               DBUS_TYPE_UINT32, &shutdownMode,
364                                               DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
365          }
366          else           // unregister
367          {
368             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
369                                               DBUS_TYPE_STRING, &objName,
370                                               DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
371          }
372
373                    if(!dbus_connection_send(conn, message, 0))
374                    {
375                       DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
376                    }
377                    dbus_connection_flush(conn);
378          dbus_message_unref(message);
379       }
380       else
381       {
382          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
383       }
384    }
385    else
386    {
387       DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
388    }
389 }
390
391
392
393 void process_send_lifecycle_request(DBusConnection* conn, int requestId, int status)
394 {
395    DBusError error;
396    dbus_error_init (&error);
397
398    if(conn != NULL)
399    {
400       DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
401                                                          "/org/genivi/NodeStateManager/Consumer",  // path
402                                                           "org.genivi.NodeStateManager.Consumer",  // interface
403                                                           "LifecycleRequestComplete");             // method
404       if(message != NULL)
405       {
406          dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId,
407                                            DBUS_TYPE_INT32, &status,
408                                            DBUS_TYPE_INVALID);
409
410
411          if(!dbus_connection_send(conn, message, 0))
412          {
413             DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
414           }
415
416           dbus_connection_flush(conn);
417           dbus_message_unref(message);
418       }
419       else
420       {
421          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
422       }
423    }
424    else
425    {
426       DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
427    }
428 }
429
430
431
432 void msg_pending_func(DBusPendingCall *call, void *data)
433 {
434    int replyArg = -1;
435    DBusError err;
436    dbus_error_init(&err);
437
438    (void)data;
439
440    DBusMessage *message = dbus_pending_call_steal_reply(call);
441
442    if (dbus_set_error_from_message(&err, message))
443    {
444       DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> Access denied") );
445    }
446    else
447    {
448       //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("msg_pending_func ==> UNlock mutex") );
449       dbus_message_get_args(message, &err, DBUS_TYPE_INT32, &replyArg, DBUS_TYPE_INVALID);
450    }
451
452    gDbusPendingRvalue = replyArg;   // set the return value
453    dbus_message_unref(message);
454
455    // unlock the mutex because we have received the reply to the dbus message
456    pthread_mutex_unlock(&gDbusPendingRegMtx);
457 }
458
459
460
461