summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_dbus_cmd.c
blob: f410e75af88b66282372150ac217e5e701e4f76c (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
/******************************************************************************
 * 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_data_organization.h"
#include "persistence_client_library_db_access.h"

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


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



void process_reg_notification_signal(DBusConnection* conn)
{
   char ruleChanged[DbusMatchRuleSize] = {0};
   char ruleDeleted[DbusMatchRuleSize] = {0};
   char ruleCreated[DbusMatchRuleSize] = {0};

   // 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'",
            gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);

   // 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'",
            gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);

   // 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'",
            gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);

   if(gNotifyPolicy == Notify_register)
   {
      dbus_bus_add_match(conn, ruleChanged, NULL);
      dbus_bus_add_match(conn, ruleDeleted, NULL);
      dbus_bus_add_match(conn, ruleCreated, NULL);
   }
   else if(gNotifyPolicy == Notify_unregister)
   {
      dbus_bus_remove_match(conn, ruleChanged, NULL);
      dbus_bus_remove_match(conn, ruleDeleted, NULL);
      dbus_bus_remove_match(conn, ruleCreated, NULL);
   }

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



void process_send_notification_signal(DBusConnection* conn)
{
   dbus_bool_t ret;
   DBusMessage* message;
   const char* notifyReason = NULL;

   char ldbidArray[DbusSubMatchSize] = {0};
   char userArray[DbusSubMatchSize]  = {0};
   char seatArray[DbusSubMatchSize]  = {0};
   char* pldbidArra = ldbidArray;
   char* puserArray = userArray;
   char* pseatArray = seatArray;
   char* pnotifyKey = gNotifykey;

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

   if(notifyReason != 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", gNotifyLdbid);
      snprintf(userArray,  DbusSubMatchSize, "%u", gNotifyUserNo);
      snprintf(seatArray,  DbusSubMatchSize, "%u", gNotifySeatNo);

      //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", gNotifykey, gLdbid, gUserNo, gSeatNo, gReason);
      message = dbus_message_new_signal("/org/genivi/persistence/adminconsumer",    // const char *path,
                                        "org.genivi.persistence.adminconsumer",     // const char *interface,
                                        notifyReason);                                 // const char *name

      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)
      {
         // Send the signal
         if(conn != NULL)
         {
            if(dbus_connection_send(conn, message, 0) == TRUE)
            {
               // Free the signal now we have finished with it
               dbus_message_unref(message);
            }
            else
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
            }
         }
         else
         {
            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!!"));
         }
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR invalid notification 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
   pers_data_sync();
   // send complete notification
   //pers_admin_service_data_sync_complete(requestID, status);
}



void process_prepare_shutdown(int complete)
{
   int i = 0, rval = 0;

   // block write
   pers_lock_access();

   // flush open files to disk
   for(i=0; i<MaxPersHandle; i++)
   {
      if(gOpenFdArray[i] == FileOpen)
      {
         fsync(i);

#if USE_FILECACHE
         if(complete == Shutdown_Full)
         {
         	rval = pfcCloseFile(i);
         }
         else if(complete == Shutdown_Partial)
         {
         	pfcWriteBackAndSync(i);
         }
#else
         if(complete == Shutdown_Full)
         {
         	rval = close(i);
         }
         else if(complete == Shutdown_Partial)
         {
         	fsync(i);
         }
#endif
         if(rval == -1)
         {
         	DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close file: "), DLT_STRING(strerror(errno)) );
         }

      }
   }

   // close all opend rct
   pers_rct_close_all();

   // close opend database
   database_close_all();

   if(complete > 0)
   {
   	close_all_persistence_handle();
   }


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

				invalidate_custom_plugin(i);
			}
		}
   }
}



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("org.genivi.persistence.admin",       // destination
                                                      "/org/genivi/persistence/admin",       // path
                                                       "org.genivi.persistence.admin",       // 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);

         if(!dbus_connection_send(conn, message, 0))
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
         }

         dbus_connection_flush(conn);
         dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
   }
}


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* objName = "/org/genivi/persistence/adminconsumer";
      const char* busName = dbus_bus_get_unique_name(conn);

      if(busName != NULL)
      {
         DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",    // destination
                                                            "/org/genivi/persistence/admin",    // path
                                                             "org.genivi.persistence.admin",    // interface
                                                             method);                           // method

         if(message != NULL)
         {
            dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,  // bus name
                                              DBUS_TYPE_STRING, &objName,
                                              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("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n") );
            }
            dbus_pending_call_unref(pending);
         }
         else
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid message") );
         }
         dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid busname") );
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid connection") );
   }
}


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* objName = "/org/genivi/NodeStateManager/LifeCycleConsumer";
      const char* busName = dbus_bus_get_unique_name(conn);

      DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
                                                          "/org/genivi/NodeStateManager/Consumer", // path
                                                          "org.genivi.NodeStateManager.Consumer",  // interface
                                                          method);                                 // method
      if(message != NULL)
      {
         if(regType == 1)   // register
         {
            dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
                                              DBUS_TYPE_STRING, &objName,
                                              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, &objName,
                                              DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
         }

		   if(!dbus_connection_send(conn, message, 0))
		   {
		      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
		   }
		   dbus_connection_flush(conn);
         dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
   }
}



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

   if(conn != NULL)
   {
      DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
                                                         "/org/genivi/NodeStateManager/Consumer",  // path
                                                          "org.genivi.NodeStateManager.Consumer",  // interface
                                                          "LifecycleRequestComplete");             // method
      if(message != NULL)
      {
         dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId,
                                           DBUS_TYPE_INT32, &status,
                                           DBUS_TYPE_INVALID);


         if(!dbus_connection_send(conn, message, 0))
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
          }

          dbus_connection_flush(conn);
          dbus_message_unref(message);
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
   }
}



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

   (void)data;

   DBusMessage *message = dbus_pending_call_steal_reply(call);

   if (dbus_set_error_from_message(&err, message))
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> 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
   dbus_message_unref(message);

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