Fix the Prevent problems
[platform/core/connectivity/smartcard-service.git] / common / Message.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef USE_GDBUS
18 /* standard library header */
19 #include <cstdio>
20 #include <cstring>
21 #include <sstream>
22
23 /* SLP library header */
24
25 /* local header */
26 #include "Debug.h"
27 #include "Message.h"
28
29 namespace smartcard_service_api
30 {
31         Message::Message()
32         {
33                 message = 0;
34                 param1 = 0;
35                 param2 = 0;
36                 error = 0;
37                 caller = NULL;
38                 callback = NULL;
39                 userParam = NULL;
40         }
41
42         Message::~Message()
43         {
44         }
45
46         const ByteArray Message::serialize() const
47         {
48                 ByteArray result;
49                 unsigned int length = 0;
50                 unsigned int dataLength = 0;
51                 unsigned char *buffer = NULL;
52
53                 length = sizeof(message) + sizeof(param1) + sizeof(param2) + sizeof(error) + sizeof(caller) + sizeof(callback) + sizeof(userParam);
54                 if (data.size() > 0)
55                 {
56                         dataLength = data.size();
57                         length += sizeof(dataLength) + data.size();
58                 }
59
60                 buffer = new unsigned char[length];
61                 if (buffer != NULL)
62                 {
63                         unsigned int current = 0;
64
65                         memset(buffer, 0, length);
66
67                         memcpy(buffer + current, &message, sizeof(message));
68                         current += sizeof(message);
69
70                         memcpy(buffer + current, &param1, sizeof(param1));
71                         current += sizeof(param1);
72
73                         memcpy(buffer + current, &param2, sizeof(param2));
74                         current += sizeof(param2);
75
76                         memcpy(buffer + current, &error, sizeof(error));
77                         current += sizeof(error);
78
79                         memcpy(buffer + current, &caller, sizeof(caller));
80                         current += sizeof(caller);
81
82                         memcpy(buffer + current, &callback, sizeof(callback));
83                         current += sizeof(callback);
84
85                         memcpy(buffer + current, &userParam, sizeof(userParam));
86                         current += sizeof(userParam);
87
88                         if (data.size() > 0)
89                         {
90                                 memcpy(buffer + current, &dataLength, sizeof(dataLength));
91                                 current += sizeof(dataLength);
92
93                                 memcpy(buffer + current, data.getBuffer(), dataLength);
94                                 current += data.size();
95                         }
96
97                         result.assign(buffer, length);
98
99                         delete []buffer;
100                 }
101                 else
102                 {
103                         _ERR("allocation failed");
104                 }
105
106                 return result;
107         }
108
109         void Message::deserialize(const ByteArray &buffer)
110         {
111                 deserialize(buffer.getBuffer(), buffer.size());
112         }
113
114         void Message::deserialize(const unsigned char *buffer, unsigned int length)
115         {
116                 unsigned int current = 0;
117                 unsigned int dataLength = 0;
118
119 //              _DBG("buffer [%p], length [%d]", buffer, length);
120
121                 memcpy(&message, buffer + current, sizeof(message));
122                 current += sizeof(message);
123
124 //              _DBG("message [%d]", message);
125
126                 memcpy(&param1, buffer + current, sizeof(param1));
127                 current += sizeof(param1);
128
129 //              _DBG("param1 [%d]", param1);
130
131                 memcpy(&param2, buffer + current, sizeof(param2));
132                 current += sizeof(param2);
133
134 //              _DBG("param2 [%d]", param2);
135
136                 memcpy(&error, buffer + current, sizeof(error));
137                 current += sizeof(error);
138
139                 memcpy(&caller, buffer + current, sizeof(caller));
140                 current += sizeof(caller);
141
142                 memcpy(&callback, buffer + current, sizeof(callback));
143                 current += sizeof(callback);
144
145                 memcpy(&userParam, buffer + current, sizeof(userParam));
146                 current += sizeof(userParam);
147
148 //              _DBG("userContext [%p]", userContext);
149
150                 if (current + sizeof(dataLength) < length)
151                 {
152                         memcpy(&dataLength, buffer + current, sizeof(dataLength));
153                         current += sizeof(dataLength);
154
155 //                      _DBG("dataLength [%d]", dataLength);
156
157                         data.assign(buffer + current, dataLength);
158                         current += dataLength;
159                 }
160         }
161
162         const string Message::toString() const
163         {
164                 stringstream ss;
165                 const char *msg = NULL;
166
167                 switch (message)
168                 {
169                 case MSG_REQUEST_READERS :
170                         msg = "MSG_REQUEST_READERS";
171                         break;
172
173 //              case MSG_REQUEST_READER_NAME :
174 //                      msg = "MSG_REQUEST_READER_NAME";
175 //                      break;
176 //
177                 case MSG_REQUEST_OPEN_SESSION :
178                         msg = "MSG_REQUEST_OPEN_SESSION";
179                         break;
180
181                 case MSG_REQUEST_CLOSE_SESSION :
182                         msg = "MSG_REQUEST_CLOSE_CHANNEL";
183                         break;
184
185                 case MSG_REQUEST_OPEN_CHANNEL :
186                         msg = "MSG_REQUEST_OPEN_CHANNEL";
187                         break;
188
189                 case MSG_REQUEST_CLOSE_CHANNEL :
190                         msg = "MSG_REQUEST_CLOSE_CHANNEL";
191                         break;
192
193                 case MSG_REQUEST_GET_ATR :
194                         msg = "MSG_REQUEST_GET_ATR";
195                         break;
196
197                 case MSG_REQUEST_TRANSMIT :
198                         msg = "MSG_REQUEST_TRANSMIT";
199                         break;
200
201                 case MSG_REQUEST_GET_CHANNEL_COUNT :
202                         msg = "MSG_REQUEST_GET_CHANNEL_COUNT";
203                         break;
204
205                 default :
206                         msg = "Unknown";
207                         break;
208                 }
209
210                 ss << "Message [" << msg << ", " << message << "], param1 [" << param1 << "], param2 [" << param2 << "], error [" << error << "], caller [" << "], callback [" << callback << "], userParam [" << userParam << "], data length [" << data.size() << "]";
211
212                 return ss.str();
213         }
214
215 } /* namespace smartcard_service_api */
216 #endif