Fix the prevent problem.
[framework/system/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 /* standard library header */
18 #include <stdio.h>
19 #include <string.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "Message.h"
26
27 namespace smartcard_service_api
28 {
29         Message::Message()
30         {
31                 message = 0;
32                 param1 = 0;
33                 param2 = 0;
34                 error = 0;
35                 caller = NULL;
36                 callback = NULL;
37                 userParam = NULL;
38         }
39
40         Message::~Message()
41         {
42         }
43
44         ByteArray Message::serialize()
45         {
46                 ByteArray result;
47                 unsigned int length = 0;
48                 unsigned int dataLength = 0;
49                 unsigned char *buffer = NULL;
50
51                 length = sizeof(message) + sizeof(param1) + sizeof(param2) + sizeof(error) + sizeof(caller) + sizeof(callback) + sizeof(userParam);
52                 if (data.getLength() > 0)
53                 {
54                         dataLength = data.getLength();
55                         length += sizeof(dataLength) + data.getLength();
56                 }
57
58                 buffer = new unsigned char[length];
59                 if (buffer != NULL)
60                 {
61                         unsigned int current = 0;
62
63                         memset(buffer, 0, length);
64
65                         memcpy(buffer + current, &message, sizeof(message));
66                         current += sizeof(message);
67
68                         memcpy(buffer + current, &param1, sizeof(param1));
69                         current += sizeof(param1);
70
71                         memcpy(buffer + current, &param2, sizeof(param2));
72                         current += sizeof(param2);
73
74                         memcpy(buffer + current, &error, sizeof(error));
75                         current += sizeof(error);
76
77                         memcpy(buffer + current, &caller, sizeof(caller));
78                         current += sizeof(caller);
79
80                         memcpy(buffer + current, &callback, sizeof(callback));
81                         current += sizeof(callback);
82
83                         memcpy(buffer + current, &userParam, sizeof(userParam));
84                         current += sizeof(userParam);
85
86                         if (data.getLength() > 0)
87                         {
88                                 memcpy(buffer + current, &dataLength, sizeof(dataLength));
89                                 current += sizeof(dataLength);
90
91                                 memcpy(buffer + current, data.getBuffer(), dataLength);
92                                 current += data.getLength();
93                         }
94
95                         result.setBuffer(buffer, length);
96
97                         delete []buffer;
98                 }
99                 else
100                 {
101                         SCARD_DEBUG_ERR("allocation failed");
102                 }
103
104                 return result;
105         }
106
107         void Message::deserialize(ByteArray buffer)
108         {
109                 deserialize(buffer.getBuffer(), buffer.getLength());
110         }
111
112         void Message::deserialize(unsigned char *buffer, unsigned int length)
113         {
114                 unsigned int current = 0;
115                 unsigned int dataLength = 0;
116
117 //              SCARD_DEBUG("buffer [%p], length [%d]", buffer, length);
118
119                 memcpy(&message, buffer + current, sizeof(message));
120                 current += sizeof(message);
121
122 //              SCARD_DEBUG("message [%d]", message);
123
124                 memcpy(&param1, buffer + current, sizeof(param1));
125                 current += sizeof(param1);
126
127 //              SCARD_DEBUG("param1 [%d]", param1);
128
129                 memcpy(&param2, buffer + current, sizeof(param2));
130                 current += sizeof(param2);
131
132 //              SCARD_DEBUG("param2 [%d]", param2);
133
134                 memcpy(&error, buffer + current, sizeof(error));
135                 current += sizeof(error);
136
137                 memcpy(&caller, buffer + current, sizeof(caller));
138                 current += sizeof(caller);
139
140                 memcpy(&callback, buffer + current, sizeof(callback));
141                 current += sizeof(callback);
142
143                 memcpy(&userParam, buffer + current, sizeof(userParam));
144                 current += sizeof(userParam);
145
146 //              SCARD_DEBUG("userContext [%p]", userContext);
147
148                 if (current + sizeof(dataLength) < length)
149                 {
150                         memcpy(&dataLength, buffer + current, sizeof(dataLength));
151                         current += sizeof(dataLength);
152
153 //                      SCARD_DEBUG("dataLength [%d]", dataLength);
154
155                         data.setBuffer(buffer + current, dataLength);
156                         current += dataLength;
157                 }
158         }
159
160         const char *Message::toString()
161         {
162                 const char *msg = NULL;
163
164                 memset(&text, 0, sizeof(text));
165
166                 switch (message)
167                 {
168                 case MSG_REQUEST_READERS :
169                         msg = "MSG_REQUEST_READERS";
170                         break;
171
172 //              case MSG_REQUEST_READER_NAME :
173 //                      msg = "MSG_REQUEST_READER_NAME";
174 //                      break;
175 //
176                 case MSG_REQUEST_OPEN_SESSION :
177                         msg = "MSG_REQUEST_OPEN_SESSION";
178                         break;
179
180                 case MSG_REQUEST_CLOSE_SESSION :
181                         msg = "MSG_REQUEST_CLOSE_CHANNEL";
182                         break;
183
184                 case MSG_REQUEST_OPEN_CHANNEL :
185                         msg = "MSG_REQUEST_OPEN_CHANNEL";
186                         break;
187
188                 case MSG_REQUEST_CLOSE_CHANNEL :
189                         msg = "MSG_REQUEST_CLOSE_CHANNEL";
190                         break;
191
192                 case MSG_REQUEST_GET_ATR :
193                         msg = "MSG_REQUEST_GET_ATR";
194                         break;
195
196                 case MSG_REQUEST_TRANSMIT :
197                         msg = "MSG_REQUEST_TRANSMIT";
198                         break;
199
200                 case MSG_REQUEST_GET_CHANNEL_COUNT :
201                         msg = "MSG_REQUEST_GET_CHANNEL_COUNT";
202                         break;
203
204                 default :
205                         msg = "Unknown";
206                         break;
207                 }
208
209                 snprintf(text, sizeof(text), "Message [%s, %d], param1 [%ld], param2 [%ld], error [%ld], caller [%p], callback [%p], userParam [%p], data length [%d]", msg, message, param1, param2, error, caller, callback, userParam, data.getLength());
210
211                 return (const char *)text;
212         }
213
214 } /* namespace smartcard_service_api */