5f9cae5b27326c1578f18c454235215d4709d23f
[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
18 /* standard library header */
19 #include <stdio.h>
20 #include <string.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "Message.h"
27
28 namespace smartcard_service_api
29 {
30         Message::Message()
31         {
32                 message = 0;
33                 param1 = 0;
34                 param2 = 0;
35                 error = 0;
36                 caller = NULL;
37                 callback = NULL;
38                 userParam = NULL;
39         }
40
41         Message::~Message()
42         {
43         }
44
45         ByteArray Message::serialize()
46         {
47                 ByteArray result;
48                 unsigned int length = 0;
49                 unsigned int dataLength = 0;
50                 unsigned char *buffer = NULL;
51
52                 length = sizeof(message) + sizeof(param1) + sizeof(param2) + sizeof(error) + sizeof(caller) + sizeof(callback) + sizeof(userParam);
53                 if (data.getLength() > 0)
54                 {
55                         dataLength = data.getLength();
56                         length += sizeof(dataLength) + data.getLength();
57                 }
58
59                 buffer = new unsigned char[length];
60                 if (buffer != NULL)
61                 {
62                         unsigned int current = 0;
63
64                         memset(buffer, 0, length);
65
66                         memcpy(buffer + current, &message, sizeof(message));
67                         current += sizeof(message);
68
69                         memcpy(buffer + current, &param1, sizeof(param1));
70                         current += sizeof(param1);
71
72                         memcpy(buffer + current, &param2, sizeof(param2));
73                         current += sizeof(param2);
74
75                         memcpy(buffer + current, &error, sizeof(error));
76                         current += sizeof(error);
77
78                         memcpy(buffer + current, &caller, sizeof(caller));
79                         current += sizeof(caller);
80
81                         memcpy(buffer + current, &callback, sizeof(callback));
82                         current += sizeof(callback);
83
84                         memcpy(buffer + current, &userParam, sizeof(userParam));
85                         current += sizeof(userParam);
86
87                         if (data.getLength() > 0)
88                         {
89                                 memcpy(buffer + current, &dataLength, sizeof(dataLength));
90                                 current += sizeof(dataLength);
91
92                                 memcpy(buffer + current, data.getBuffer(), dataLength);
93                                 current += data.getLength();
94                         }
95
96                         result.setBuffer(buffer, length);
97
98                         delete []buffer;
99                 }
100                 else
101                 {
102                         SCARD_DEBUG_ERR("allocation failed");
103                 }
104
105                 return result;
106         }
107
108         void Message::deserialize(ByteArray buffer)
109         {
110                 deserialize(buffer.getBuffer(), buffer.getLength());
111         }
112
113         void Message::deserialize(unsigned char *buffer, unsigned int length)
114         {
115                 unsigned int current = 0;
116                 unsigned int dataLength = 0;
117
118 //              SCARD_DEBUG("buffer [%p], length [%d]", buffer, length);
119
120                 memcpy(&message, buffer + current, sizeof(message));
121                 current += sizeof(message);
122
123 //              SCARD_DEBUG("message [%d]", message);
124
125                 memcpy(&param1, buffer + current, sizeof(param1));
126                 current += sizeof(param1);
127
128 //              SCARD_DEBUG("param1 [%d]", param1);
129
130                 memcpy(&param2, buffer + current, sizeof(param2));
131                 current += sizeof(param2);
132
133 //              SCARD_DEBUG("param2 [%d]", param2);
134
135                 memcpy(&error, buffer + current, sizeof(error));
136                 current += sizeof(error);
137
138                 memcpy(&caller, buffer + current, sizeof(caller));
139                 current += sizeof(caller);
140
141                 memcpy(&callback, buffer + current, sizeof(callback));
142                 current += sizeof(callback);
143
144                 memcpy(&userParam, buffer + current, sizeof(userParam));
145                 current += sizeof(userParam);
146
147 //              SCARD_DEBUG("userContext [%p]", userContext);
148
149                 if (current + sizeof(dataLength) < length)
150                 {
151                         memcpy(&dataLength, buffer + current, sizeof(dataLength));
152                         current += sizeof(dataLength);
153
154 //                      SCARD_DEBUG("dataLength [%d]", dataLength);
155
156                         data.setBuffer(buffer + current, dataLength);
157                         current += dataLength;
158                 }
159         }
160
161         const char *Message::toString()
162         {
163                 const char *msg = NULL;
164
165                 memset(&text, 0, sizeof(text));
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                 snprintf(text, sizeof(text), "Message [%s, %d], param1 [%d], param2 [%d], error [%d], caller [%p], callback [%p], userParam [%p], data length [%d]", msg, message, param1, param2, error, caller, callback, userParam, data.getLength());
211
212                 return (const char *)text;
213         }
214
215 } /* namespace smartcard_service_api */