2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 /* standard library header */
22 /* SLP library header */
28 namespace smartcard_service_api
45 ByteArray Message::serialize()
48 unsigned int length = 0;
49 unsigned int dataLength = 0;
50 unsigned char *buffer = NULL;
52 length = sizeof(message) + sizeof(param1) + sizeof(param2) + sizeof(error) + sizeof(caller) + sizeof(callback) + sizeof(userParam);
53 if (data.getLength() > 0)
55 dataLength = data.getLength();
56 length += sizeof(dataLength) + data.getLength();
59 buffer = new unsigned char[length];
62 unsigned int current = 0;
64 memset(buffer, 0, length);
66 memcpy(buffer + current, &message, sizeof(message));
67 current += sizeof(message);
69 memcpy(buffer + current, ¶m1, sizeof(param1));
70 current += sizeof(param1);
72 memcpy(buffer + current, ¶m2, sizeof(param2));
73 current += sizeof(param2);
75 memcpy(buffer + current, &error, sizeof(error));
76 current += sizeof(error);
78 memcpy(buffer + current, &caller, sizeof(caller));
79 current += sizeof(caller);
81 memcpy(buffer + current, &callback, sizeof(callback));
82 current += sizeof(callback);
84 memcpy(buffer + current, &userParam, sizeof(userParam));
85 current += sizeof(userParam);
87 if (data.getLength() > 0)
89 memcpy(buffer + current, &dataLength, sizeof(dataLength));
90 current += sizeof(dataLength);
92 memcpy(buffer + current, data.getBuffer(), dataLength);
93 current += data.getLength();
96 result.setBuffer(buffer, length);
102 SCARD_DEBUG_ERR("allocation failed");
108 void Message::deserialize(ByteArray buffer)
110 deserialize(buffer.getBuffer(), buffer.getLength());
113 void Message::deserialize(unsigned char *buffer, unsigned int length)
115 unsigned int current = 0;
116 unsigned int dataLength = 0;
118 // SCARD_DEBUG("buffer [%p], length [%d]", buffer, length);
120 memcpy(&message, buffer + current, sizeof(message));
121 current += sizeof(message);
123 // SCARD_DEBUG("message [%d]", message);
125 memcpy(¶m1, buffer + current, sizeof(param1));
126 current += sizeof(param1);
128 // SCARD_DEBUG("param1 [%d]", param1);
130 memcpy(¶m2, buffer + current, sizeof(param2));
131 current += sizeof(param2);
133 // SCARD_DEBUG("param2 [%d]", param2);
135 memcpy(&error, buffer + current, sizeof(error));
136 current += sizeof(error);
138 memcpy(&caller, buffer + current, sizeof(caller));
139 current += sizeof(caller);
141 memcpy(&callback, buffer + current, sizeof(callback));
142 current += sizeof(callback);
144 memcpy(&userParam, buffer + current, sizeof(userParam));
145 current += sizeof(userParam);
147 // SCARD_DEBUG("userContext [%p]", userContext);
149 if (current + sizeof(dataLength) < length)
151 memcpy(&dataLength, buffer + current, sizeof(dataLength));
152 current += sizeof(dataLength);
154 // SCARD_DEBUG("dataLength [%d]", dataLength);
156 data.setBuffer(buffer + current, dataLength);
157 current += dataLength;
161 const char *Message::toString()
163 const char *msg = NULL;
165 memset(&text, 0, sizeof(text));
169 case MSG_REQUEST_READERS :
170 msg = "MSG_REQUEST_READERS";
173 // case MSG_REQUEST_READER_NAME :
174 // msg = "MSG_REQUEST_READER_NAME";
177 case MSG_REQUEST_OPEN_SESSION :
178 msg = "MSG_REQUEST_OPEN_SESSION";
181 case MSG_REQUEST_CLOSE_SESSION :
182 msg = "MSG_REQUEST_CLOSE_CHANNEL";
185 case MSG_REQUEST_OPEN_CHANNEL :
186 msg = "MSG_REQUEST_OPEN_CHANNEL";
189 case MSG_REQUEST_CLOSE_CHANNEL :
190 msg = "MSG_REQUEST_CLOSE_CHANNEL";
193 case MSG_REQUEST_GET_ATR :
194 msg = "MSG_REQUEST_GET_ATR";
197 case MSG_REQUEST_TRANSMIT :
198 msg = "MSG_REQUEST_TRANSMIT";
201 case MSG_REQUEST_GET_CHANNEL_COUNT :
202 msg = "MSG_REQUEST_GET_CHANNEL_COUNT";
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());
212 return (const char *)text;
215 } /* namespace smartcard_service_api */