2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
21 /* SLP library header */
27 namespace smartcard_service_api
44 ByteArray Message::serialize()
47 unsigned int length = 0;
48 unsigned int dataLength = 0;
49 unsigned char *buffer = NULL;
51 length = sizeof(message) + sizeof(param1) + sizeof(param2) + sizeof(error) + sizeof(caller) + sizeof(callback) + sizeof(userParam);
52 if (data.getLength() > 0)
54 dataLength = data.getLength();
55 length += sizeof(dataLength) + data.getLength();
58 buffer = new unsigned char[length];
61 unsigned int current = 0;
63 memset(buffer, 0, length);
65 memcpy(buffer + current, &message, sizeof(message));
66 current += sizeof(message);
68 memcpy(buffer + current, ¶m1, sizeof(param1));
69 current += sizeof(param1);
71 memcpy(buffer + current, ¶m2, sizeof(param2));
72 current += sizeof(param2);
74 memcpy(buffer + current, &error, sizeof(error));
75 current += sizeof(error);
77 memcpy(buffer + current, &caller, sizeof(caller));
78 current += sizeof(caller);
80 memcpy(buffer + current, &callback, sizeof(callback));
81 current += sizeof(callback);
83 memcpy(buffer + current, &userParam, sizeof(userParam));
84 current += sizeof(userParam);
86 if (data.getLength() > 0)
88 memcpy(buffer + current, &dataLength, sizeof(dataLength));
89 current += sizeof(dataLength);
91 memcpy(buffer + current, data.getBuffer(), dataLength);
92 current += data.getLength();
95 result.setBuffer(buffer, length);
101 SCARD_DEBUG_ERR("allocation failed");
107 void Message::deserialize(ByteArray buffer)
109 deserialize(buffer.getBuffer(), buffer.getLength());
112 void Message::deserialize(unsigned char *buffer, unsigned int length)
114 unsigned int current = 0;
115 unsigned int dataLength = 0;
117 // SCARD_DEBUG("buffer [%p], length [%d]", buffer, length);
119 memcpy(&message, buffer + current, sizeof(message));
120 current += sizeof(message);
122 // SCARD_DEBUG("message [%d]", message);
124 memcpy(¶m1, buffer + current, sizeof(param1));
125 current += sizeof(param1);
127 // SCARD_DEBUG("param1 [%d]", param1);
129 memcpy(¶m2, buffer + current, sizeof(param2));
130 current += sizeof(param2);
132 // SCARD_DEBUG("param2 [%d]", param2);
134 memcpy(&error, buffer + current, sizeof(error));
135 current += sizeof(error);
137 memcpy(&caller, buffer + current, sizeof(caller));
138 current += sizeof(caller);
140 memcpy(&callback, buffer + current, sizeof(callback));
141 current += sizeof(callback);
143 memcpy(&userParam, buffer + current, sizeof(userParam));
144 current += sizeof(userParam);
146 // SCARD_DEBUG("userContext [%p]", userContext);
148 if (current + sizeof(dataLength) < length)
150 memcpy(&dataLength, buffer + current, sizeof(dataLength));
151 current += sizeof(dataLength);
153 // SCARD_DEBUG("dataLength [%d]", dataLength);
155 data.setBuffer(buffer + current, dataLength);
156 current += dataLength;
160 const char *Message::toString()
162 const char *msg = NULL;
164 memset(&text, 0, sizeof(text));
168 case MSG_REQUEST_READERS :
169 msg = "MSG_REQUEST_READERS";
172 // case MSG_REQUEST_READER_NAME :
173 // msg = "MSG_REQUEST_READER_NAME";
176 case MSG_REQUEST_OPEN_SESSION :
177 msg = "MSG_REQUEST_OPEN_SESSION";
180 case MSG_REQUEST_CLOSE_SESSION :
181 msg = "MSG_REQUEST_CLOSE_CHANNEL";
184 case MSG_REQUEST_OPEN_CHANNEL :
185 msg = "MSG_REQUEST_OPEN_CHANNEL";
188 case MSG_REQUEST_CLOSE_CHANNEL :
189 msg = "MSG_REQUEST_CLOSE_CHANNEL";
192 case MSG_REQUEST_GET_ATR :
193 msg = "MSG_REQUEST_GET_ATR";
196 case MSG_REQUEST_TRANSMIT :
197 msg = "MSG_REQUEST_TRANSMIT";
200 case MSG_REQUEST_GET_CHANNEL_COUNT :
201 msg = "MSG_REQUEST_GET_CHANNEL_COUNT";
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());
211 return (const char *)text;
214 } /* namespace smartcard_service_api */