summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_dbus_cmd.c
blob: c1c732286bebb1c30c048ce9853b15ef768f7ff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/******************************************************************************
 * Project         Persistency
 * (c) copyright   2012
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_dbus_cmd.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of the persistence client library dbus commands.
 * @see
 */

#include "persistence_client_library_dbus_cmd.h"

#include "persistence_client_library_handle.h"
#include "persistence_client_library_custom_loader.h"
#include "persistence_client_library_pas_interface.h"
#include "persistence_client_library_db_access.h"
#include "persistence_client_library_file.h"


#if USE_FILECACHE
   #include <persistence_file_cache.h>


	/**
	 * write back from cache to non volatile memory device
	 * ATTENTION:
	 * THIS FUNCTION IS NOT INTENDED TO BE USED BY A NORMAL APPLICATION.
	 * ONLY SPECIAL APPLICATION ARE ALLOWED TO USING USE THIS FUNCTION
	 **/
	 extern int pfcWriteBackAndSync(int handle);
#endif

#include <errno.h>
#include <dlfcn.h>
#include <dlt.h>

DLT_IMPORT_CONTEXT(gPclDLTContext);

/// change signal string
static const char* gChangeSignal = "PersistenceResChange";
/// delete signal string
static const char* gDeleteSignal = "PersistenceResDelete";
/// create signal string
static const char* gCreateSignal = "PersistenceResCreate";

/// dbus timeout
static int gTimeoutMs = 5000;

// function prototype
static void msg_pending_func(DBusPendingCall *call, void *data);



void process_reg_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
                                                           unsigned int notifySeatNo, unsigned int notifyPolicy, const char* notifyKey)
{
   char ruleChanged[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
   char ruleDeleted[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
   char ruleCreated[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};

   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("process notification - User:"), DLT_UINT(notifyUserNo), DLT_STRING("- Seat:"), DLT_UINT(notifySeatNo));


   // add match for  c h a n g e
   snprintf(ruleChanged, DbusMatchRuleSize,
            "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResChange',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
            notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);

   // add match for  d e l e t e
   snprintf(ruleDeleted, DbusMatchRuleSize,
            "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResDelete',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
            notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);

   // add match for  c r e a t e
   snprintf(ruleCreated, DbusMatchRuleSize,
            "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResCreate',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
            notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);

   if(notifyPolicy == Notify_register)
   {
      dbus_bus_add_match(conn, ruleChanged, NULL);
      dbus_bus_add_match(conn, ruleDeleted, NULL);
      dbus_bus_add_match(conn, ruleCreated, NULL);
      DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Reg for change notify:"), DLT_STRING(ruleChanged));
   }
   else if(notifyPolicy == Notify_unregister)
   {
      dbus_bus_remove_match(conn, ruleChanged, NULL);
      dbus_bus_remove_match(conn, ruleDeleted, NULL);
      dbus_bus_remove_match(conn, ruleCreated, NULL);
      DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("unREg for change notify:"), DLT_STRING(ruleChanged));
   }

   dbus_connection_flush(conn);  // flush the connection to add the match
}



void process_send_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
                                                            unsigned int notifySeatNo, unsigned int notifyReason, const char* notifyKey)
{
   dbus_bool_t ret;
   DBusMessage* message;
   const char* notifyReasonString = NULL;
   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("send notification - User:"), DLT_UINT(notifyUserNo), DLT_STRING("- Seat:"), DLT_UINT(notifySeatNo));

   char ldbidArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
   char userArray[DbusSubMatchSize]  = {[0 ... DbusSubMatchSize-1] = 0};
   char seatArray[DbusSubMatchSize]  = {[0 ... DbusSubMatchSize-1] = 0};
   char* pldbidArra = ldbidArray;
   char* puserArray = userArray;
   char* pseatArray = seatArray;
   char* pnotifyKey = (char*)notifyKey;

   switch(notifyReason)
   {
      case pclNotifyStatus_deleted:
      	notifyReasonString = gDeleteSignal;
         break;
      case  pclNotifyStatus_created:
      	notifyReasonString = gCreateSignal;
         break;
      case pclNotifyStatus_changed:
      	notifyReasonString = gChangeSignal;
         break;
      default:
      	notifyReasonString = NULL;
         break;
   }

   if(notifyReasonString != NULL)
   {
      // dbus_bus_add_match is used for the notification mechanism,
      // and this works only for type DBUS_TYPE_STRING as message arguments
      // this is the reason to use string instead of integer types directly
      snprintf(ldbidArray, DbusSubMatchSize, "%u", notifyLdbid);
      snprintf(userArray,  DbusSubMatchSize, "%u", notifyUserNo);
      snprintf(seatArray,  DbusSubMatchSize, "%u", notifySeatNo);

      //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", notifyKey, notifyLdbid, notifyUserNo, notifySeatNo, notifyReason);
      message = dbus_message_new_signal(gPersAdminConsumerPath, gDbusPersAdminConsInterface, notifyReasonString);

      ret = dbus_message_append_args(message, DBUS_TYPE_STRING, &pnotifyKey,
                                              DBUS_TYPE_STRING, &pldbidArra,
                                              DBUS_TYPE_STRING, &puserArray,
                                              DBUS_TYPE_STRING, &pseatArray, DBUS_TYPE_INVALID);
      if(ret == TRUE)
      {
         if(conn != NULL)  // Send the signal
         {
            if(dbus_connection_send(conn, message, 0) == TRUE)
            {
               dbus_message_unref(message);  // Free the signal now we have finished with it
            }
            else
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendNotifySig - failed to send msg!!"));
            }
         }
         else
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendNotifySig - Con NULL"));
         }
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendNotifySig - _append_args"));
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendNotifySig - Inval noty reason"));
   }
}



void process_block_and_write_data_back(unsigned int requestID, unsigned int status)
{
   (void)requestID;
   (void)status;
   // lock persistence data access
   pers_lock_access();
   // sync data back to memory device
}



void process_prepare_shutdown(unsigned int complete)
{
   int i = 0;

   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("prepShtdwn - writing all changed data / closing all handles"));

   // block write
   pers_lock_access();

   // flush open files to disk

#if USE_FILECACHE
   if(complete == Shutdown_Full)
   {
      rval = pfcCloseFile(i);
   }
   else if(complete == Shutdown_Partial)
   {
      pfcWriteBackAndSync(i);
   }
#else
   if(complete == Shutdown_Full)
   {
      list_iterate(&gOpenFdList, &pclFileClose);
   }
   else if(complete == Shutdown_Partial)
   {
      list_iterate(&gOpenFdList, &fsync);
   }
#endif

   pers_rct_close_all();      // close all opened resource configuration table

   database_close_all();      // close opened database

   if(complete > 0)
   {
      close_all_persistence_handle();

		for(i=0; i<PersCustomLib_LastEntry; i++)  // unload custom client libraries
		{
			if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
			{
				gPersCustomFuncs[i].custom_plugin_deinit();     // deinitialize plugin

				dlclose(gPersCustomFuncs[i].handle);            // close library handle

				invalidate_custom_plugin(i);
			}
		}
   }

   if(gIsNodeStateManager == 0)
      syncfs(gSyncFd);  // finally make sure to commit buffer cache to disk
}



void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int status)
{
   DBusError error;
   dbus_error_init (&error);

   DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface,  			   // destination
   		                                              gDbusPersAdminPath,            	      // path
   		                                              gDbusPersAdminInterface,       	      // interface
                                                       "PersistenceAdminRequestCompleted");  // method
   if(conn != NULL)
   {
      if(message != NULL)
      {
         dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestID,
                                           DBUS_TYPE_INT32,  &status, DBUS_TYPE_INVALID);

         DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("sendPasRequest - pas_request"), DLT_UINT(requestID), DLT_INT(status) );
         if(!dbus_connection_send(conn, message, 0))
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasRequest - Access denied"), DLT_STRING(error.message) );
         }

         dbus_connection_flush(conn);
         dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasRequest - Inval msg") );
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasRequest - Inval con") );
   }
}



void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag)
{
   DBusError error;
   dbus_error_init (&error);
   DBusPendingCall* pending = NULL;

   char* method = NULL;

   if(regType == 0)
      method = "UnRegisterPersAdminNotification";
   else if(regType == 1)
      method = "RegisterPersAdminNotification";

   if(conn != NULL)
   {
      const char* busName = dbus_bus_get_unique_name(conn);

      DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("sendPasRegister - Type:"), DLT_INT(regType), DLT_STRING(busName) );

      if(busName != NULL)
      {
         DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface,     // destination
         		                                              gDbusPersAdminPath,    		// path
         		                                              gDbusPersAdminInterface,     // interface
                                                             method);                     // method

         if(message != NULL)
         {
            dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,  // bus name
                                              DBUS_TYPE_STRING, &gPersAdminConsumerPath,
                                              DBUS_TYPE_INT32,  &notificationFlag,
                                              DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);

            dbus_connection_send_with_reply(conn,           // the connection
                                            message,        // the message to write
                                            &pending,       // pending
                                            gTimeoutMs);    // timeout in milliseconds or -1 for default

            dbus_connection_flush(conn);

            if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasReg - _call_set_notify: FAILED\n") );
            }
            dbus_pending_call_unref(pending);
         }
         else
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasReg - Invalid message") );
         }
         dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasReg - Invalid busname") );
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendPasReg - Inval con") );
   }
}



void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
{
   DBusError error;
   dbus_error_init (&error);

   char* method = NULL;

   if(regType == 1)
      method = "RegisterShutdownClient";
   else if(regType == 0)
      method = "UnRegisterShutdownClient";

   if(conn != NULL)
   {
      const char* busName = dbus_bus_get_unique_name(conn);

      DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("sendLcmRegister - Type:"), DLT_INT(regType), DLT_STRING(busName) );

      DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest,       // destination
      		                                              gDbusLcCons,           // path
      		                                              gDbusLcInterface,      // interface
                                                          method);               // method
      if(message != NULL)
      {
         if(regType == 1)   // register
         {
            dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
                                              DBUS_TYPE_STRING, &gDbusLcConsPath,
                                              DBUS_TYPE_UINT32, &shutdownMode,
                                              DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
         }
         else           // unregister
         {
            dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
                                              DBUS_TYPE_STRING, &gDbusLcConsPath,
                                              DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
         }

		   if(!dbus_connection_send(conn, message, 0))
		   {
		      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendLcmReg - Access denied"), DLT_STRING(error.message) );
		   }
		   dbus_connection_flush(conn);
         dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendLcmReg - Invalid msg"));
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendLcmReg - con is NULL"));
   }
}



void process_send_lifecycle_request(DBusConnection* conn, unsigned int requestId, unsigned int status)
{
   DBusError error;
   dbus_error_init (&error);

   if(conn != NULL)
   {
      DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest,             // destination
      		                                              gDbusLcCons,                 // path
      		                                              gDbusLcInterface,            // interface
                                                          "LifecycleRequestComplete"); // method
      if(message != NULL)
      {
         dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestId,
                                           DBUS_TYPE_INT32, &status, DBUS_TYPE_INVALID);

         DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("sendLcmRequest: "), DLT_UINT(requestId), DLT_UINT(status) );
         if(!dbus_connection_send(conn, message, 0))
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendLcmRequest - Access denied"), DLT_STRING(error.message) );
          }

          dbus_connection_flush(conn);
          dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendLcmRequest - Invalid msg"));
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sendLcmRequest - con is NULL"));
   }
}



void msg_pending_func(DBusPendingCall *call, void *data)
{
   int replyArg = -1;
   DBusError err;
   dbus_error_init(&err);

   DBusMessage *message = NULL;

   (void)data;

   pthread_mutex_lock(&gDbusPendingRegMtx);

   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("msgPendFunc - steal_reply: "));
   message = dbus_pending_call_steal_reply(call);

   if(dbus_set_error_from_message(&err, message))
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgPendFunc - Access denied") );
   }
   else
   {
      //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("msg_pending_func ==> UNlock mutex") );
      dbus_message_get_args(message, &err, DBUS_TYPE_INT32, &replyArg, DBUS_TYPE_INVALID);
   }

   gDbusPendingRvalue = replyArg;   // set the return value

   gDbusPendingCondValue = 1;
   pthread_cond_signal(&gDbusPendingCond);

   // unlock the mutex because we have received the reply to the dbus message
   pthread_mutex_unlock(&gDbusPendingRegMtx);

   dbus_message_unref(message);
}