summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_lc_interface.c
blob: 3a9e615765ecc5b437401ddb6fd6464cdbfb8d71 (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
/******************************************************************************
 * 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_lc_interface.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of the persistence client library lifecycle interface.
 * @see
 */


#include "persistence_client_library_lc_interface.h"

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

DLT_IMPORT_CONTEXT(gPclDLTContext);


int check_lc_request(unsigned int request, unsigned int requestID)
{
   int rval = 0;

   switch(request)
   {
      case NsmShutdownNormal:
      {
      	MainLoopData_u data;

      	memset(&data, 0, sizeof(MainLoopData_u));
      	data.cmd = (uint32_t)CMD_LC_PREPARE_SHUTDOWN;
      	data.params[0] = request;
      	data.params[1] = requestID;
      	data.string[0] = '\0'; 	// no string parameter, set to 0

         if(-1 == deliverToMainloop_NM(&data) )
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("lcCechkReq - failed write pipe"), DLT_INT(errno));
            rval = NsmErrorStatus_Fail;
         }
         else
         {
            rval = NsmErrorStatus_OK;
         }
         break;
      }
      default:
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("lcCechkReq - Unknown lcm message"), DLT_UINT(request));
         break;
      }
   }

   return rval;
}


int msg_lifecycleRequest(DBusConnection *connection, DBusMessage *message)
{
   int msgReturn = 0;
   unsigned int request = 0, requestID = 0;

   DBusMessage *reply;
   DBusError error;
   dbus_error_init (&error);

   if (!dbus_message_get_args (message, &error, DBUS_TYPE_UINT32, &request,
                                                DBUS_TYPE_UINT32, &requestID, DBUS_TYPE_INVALID))
   {
      reply = dbus_message_new_error(message, error.name, error.message);

      if (reply == 0)
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgLcReq - DBus No mem"));
      }

      if (!dbus_connection_send(connection, reply, 0))
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgLcReq - DBus No mem"));
      }

      dbus_message_unref(reply);

      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
   }

   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("msg_lifecycleRequest"), DLT_UINT(request), DLT_UINT(requestID) );

   msgReturn = NsmErrorStatus_ResponsePending;  // let NSM know that message response is pending,

   reply = dbus_message_new_method_return(message);

   if (reply == 0)
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgLcReq - DBus No mem"));
   }

   if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &msgReturn, DBUS_TYPE_INVALID))
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgLcReq - DBus No mem"));
   }

   if (!dbus_connection_send(connection, reply, 0))
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgLcReq - DBus No mem"));
   }

   dbus_connection_flush(connection);
   dbus_message_unref(reply);


   // now prepare for shutdown and then send
   if(check_lc_request(request, requestID) == NsmErrorStatus_Fail)
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msgLcReq - Failed write wo queue"));
   }

   return DBUS_HANDLER_RESULT_HANDLED;
}



DBusHandlerResult checkLifecycleMsg(DBusConnection * connection, DBusMessage * message, void * user_data)
{
   DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

   (void)user_data;

   if((0==strncmp(gDbusLcConsterface, dbus_message_get_interface(message), 46)))
   {
   	DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("chLcMsg - Received dbus msg: "), DLT_STRING(dbus_message_get_member(message)));
      if((0==strncmp(gDbusLcConsMsg, dbus_message_get_member(message), 16)))
      {
         result = msg_lifecycleRequest(connection, message);
      }
      else
      {
          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("chLcMsg - unknown msg "), DLT_STRING(dbus_message_get_interface(message)));
      }
   }
   return result;
}



int register_lifecycle(int shutdownMode)
{
	MainLoopData_u data;

   memset(&data, 0, sizeof(MainLoopData_u));
	data.cmd = (uint32_t)CMD_SEND_LC_REGISTER;
	data.params[0] = 1;
	data.params[1] = (uint32_t)shutdownMode;
	data.string[0] = '\0'; 	// no string parameter, set to 0

   return deliverToMainloop(&data);
}



int unregister_lifecycle(int shutdownMode)
{
	MainLoopData_u data;

	memset(&data, 0, sizeof(MainLoopData_u));
	data.cmd = (uint32_t)CMD_SEND_LC_REGISTER;
	data.params[0] = 0;
	data.params[1] = (uint32_t)shutdownMode;
	data.string[0] = '\0'; 	// no string parameter, set to 0

   return deliverToMainloop(&data);
}