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.
18 /* standard library header */
23 /* SLP library header */
29 namespace smartcard_service_api
46 const ByteArray Message::serialize() const
49 unsigned int length = 0;
50 unsigned int dataLength = 0;
51 unsigned char *buffer = NULL;
53 length = sizeof(message) + sizeof(param1) + sizeof(param2) + sizeof(error) + sizeof(caller) + sizeof(callback) + sizeof(userParam);
56 dataLength = data.size();
57 length += sizeof(dataLength) + data.size();
60 buffer = new unsigned char[length];
63 unsigned int current = 0;
65 memset(buffer, 0, length);
67 memcpy(buffer + current, &message, sizeof(message));
68 current += sizeof(message);
70 memcpy(buffer + current, ¶m1, sizeof(param1));
71 current += sizeof(param1);
73 memcpy(buffer + current, ¶m2, sizeof(param2));
74 current += sizeof(param2);
76 memcpy(buffer + current, &error, sizeof(error));
77 current += sizeof(error);
79 memcpy(buffer + current, &caller, sizeof(caller));
80 current += sizeof(caller);
82 memcpy(buffer + current, &callback, sizeof(callback));
83 current += sizeof(callback);
85 memcpy(buffer + current, &userParam, sizeof(userParam));
86 current += sizeof(userParam);
90 memcpy(buffer + current, &dataLength, sizeof(dataLength));
91 current += sizeof(dataLength);
93 memcpy(buffer + current, data.getBuffer(), dataLength);
94 current += data.size();
97 result.assign(buffer, length);
103 _ERR("allocation failed");
109 void Message::deserialize(const ByteArray &buffer)
111 deserialize(buffer.getBuffer(), buffer.size());
114 void Message::deserialize(const unsigned char *buffer, unsigned int length)
116 unsigned int current = 0;
117 unsigned int dataLength = 0;
119 // _DBG("buffer [%p], length [%d]", buffer, length);
121 memcpy(&message, buffer + current, sizeof(message));
122 current += sizeof(message);
124 // _DBG("message [%d]", message);
126 memcpy(¶m1, buffer + current, sizeof(param1));
127 current += sizeof(param1);
129 // _DBG("param1 [%d]", param1);
131 memcpy(¶m2, buffer + current, sizeof(param2));
132 current += sizeof(param2);
134 // _DBG("param2 [%d]", param2);
136 memcpy(&error, buffer + current, sizeof(error));
137 current += sizeof(error);
139 memcpy(&caller, buffer + current, sizeof(caller));
140 current += sizeof(caller);
142 memcpy(&callback, buffer + current, sizeof(callback));
143 current += sizeof(callback);
145 memcpy(&userParam, buffer + current, sizeof(userParam));
146 current += sizeof(userParam);
148 // _DBG("userContext [%p]", userContext);
150 if (current + sizeof(dataLength) < length)
152 memcpy(&dataLength, buffer + current, sizeof(dataLength));
153 current += sizeof(dataLength);
155 // _DBG("dataLength [%d]", dataLength);
157 data.assign(buffer + current, dataLength);
158 current += dataLength;
162 const string Message::toString() const
165 const char *msg = NULL;
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 ss << "Message [" << msg << ", " << message << "], param1 [" << param1 << "], param2 [" << param2 << "], error [" << error << "], caller [" << "], callback [" << callback << "], userParam [" << userParam << "], data length [" << data.size() << "]";
215 } /* namespace smartcard_service_api */