Finished switch to single threaded dbus communication
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_lc_interface.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_lc_interface.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of the persistence client library lifecycle interface.
16  * @see
17  */
18
19
20 #include "persistence_client_library_lc_interface.h"
21
22 #include "../include_protected/persistence_client_library_data_organization.h"
23 #include "../include_protected/persistence_client_library_db_access.h"
24
25 #include "persistence_client_library_handle.h"
26 #include "persistence_client_library_pas_interface.h"
27 #include "persistence_client_library_dbus_service.h"
28 #include "persistence_client_library_custom_loader.h"
29 #include "persistence_client_library_prct_access.h"
30 #include "persistence_client_library_itzam_errors.h"
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <dlfcn.h>
37
38
39 int check_lc_request(int request, int requestID)
40 {
41    int rval = 0;
42
43    switch(request)
44    {
45       case NsmShutdownNormal:
46       {
47          if(-1 == deliverToMainloop(CMD_LC_PREPARE_SHUTDOWN, request, requestID) )
48          {
49             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("check_lc_request => failed to write to pipe"), DLT_INT(errno));
50             rval = NsmErrorStatus_Fail;
51          }
52          else
53          {
54             rval = NsmErrorStatus_OK;
55          }
56          break;
57       }
58       default:
59       {
60          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("check_lc_request => Unknown lifecycle message"), DLT_INT(request));
61          break;
62       }
63    }
64
65    return rval;
66 }
67
68
69 int msg_lifecycleRequest(DBusConnection *connection, DBusMessage *message)
70 {
71    int request   = 0,
72        requestID = 0,
73        msgReturn = 0;
74
75    DBusMessage *reply;
76    DBusError error;
77    dbus_error_init (&error);
78
79    if (!dbus_message_get_args (message, &error, DBUS_TYPE_UINT32, &request,
80                                                 DBUS_TYPE_UINT32, &requestID,
81                                                 DBUS_TYPE_INVALID))
82    {
83       reply = dbus_message_new_error(message, error.name, error.message);
84
85       if (reply == 0)
86       {
87          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_lifecycleRequest => DBus No memory"));
88       }
89
90       if (!dbus_connection_send(connection, reply, 0))
91       {
92          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_lifecycleRequest => DBus No memory"));
93       }
94
95       dbus_message_unref(reply);
96
97       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
98    }
99
100    msgReturn = check_lc_request(request, requestID);
101
102    reply = dbus_message_new_method_return(message);
103
104    if (reply == 0)
105    {
106       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_lifecycleRequest => DBus No memory"));
107    }
108
109    if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &msgReturn, DBUS_TYPE_INVALID))
110    {
111       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_lifecycleRequest => DBus No memory"));
112    }
113
114    if (!dbus_connection_send(connection, reply, 0))
115    {
116       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_lifecycleRequest => DBus No memory"));
117    }
118
119    dbus_connection_flush(connection);
120    dbus_message_unref(reply);
121
122    return DBUS_HANDLER_RESULT_HANDLED;
123 }
124
125
126
127
128 DBusHandlerResult checkLifecycleMsg(DBusConnection * connection, DBusMessage * message, void * user_data)
129 {
130    DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
131
132    if((0==strncmp("org.genivi.NodeStateManager.LifeCycleConsumer", dbus_message_get_interface(message), 20)))
133    {
134       if((0==strncmp("LifecycleRequest", dbus_message_get_member(message), 18)))
135       {
136          result = msg_lifecycleRequest(connection, message);
137       }
138       else
139       {
140           DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("checkLifecycleMsg -> unknown message "), DLT_STRING(dbus_message_get_interface(message)));
141       }
142    }
143    return result;
144 }
145
146
147
148 int register_lifecycle(int shutdownMode)
149 {
150    return deliverToMainloop(CMD_SEND_LC_REGISTER, 1, shutdownMode);
151 }
152
153
154
155 int unregister_lifecycle(int shutdownMode)
156 {
157    return deliverToMainloop(CMD_SEND_LC_REGISTER, 0, shutdownMode);
158 }
159
160
161
162 int send_prepare_shutdown_complete(int requestId, int status)
163 {
164    return deliverToMainloop(CMD_SEND_LC_REQUEST, status, requestId);
165 }
166