2 * Copyright (c) 2020 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.
17 #include "parcel/api/parcel.h"
18 #include "parcel/common.hh"
19 #include "parcel/log_private.hh"
20 #include "parcel/parcel.hh"
21 #include "parcel/parcelable.hh"
23 using namespace tizen_base;
25 extern "C" EXPORT int parcel_create(parcel_h *parcel) {
26 if (parcel == nullptr) {
27 _E("Invalid parameter");
28 return PARCEL_ERROR_INVALID_PARAMETER;
31 auto* h = new (std::nothrow) Parcel();
34 return PARCEL_ERROR_OUT_OF_MEMORY;
37 *parcel = static_cast<parcel_h>(h);
38 return PARCEL_ERROR_NONE;
41 extern "C" EXPORT int parcel_destroy(parcel_h parcel) {
42 if (parcel == nullptr) {
43 _E("Invalid parameter");
44 return PARCEL_ERROR_INVALID_PARAMETER;
47 auto* h = static_cast<Parcel*>(parcel);
49 return PARCEL_ERROR_NONE;
52 extern "C" EXPORT int parcel_clone(parcel_h parcel, parcel_h* clone) {
53 if (parcel == nullptr || clone == nullptr) {
54 _E("Invalid parameter");
55 return PARCEL_ERROR_INVALID_PARAMETER;
58 auto* h = static_cast<Parcel*>(parcel);
59 auto* c = new (std::nothrow) Parcel(*h);
62 return PARCEL_ERROR_OUT_OF_MEMORY;
65 *clone = static_cast<parcel_h>(c);
66 return PARCEL_ERROR_NONE;
69 extern "C" EXPORT int parcel_burst_write(parcel_h parcel, const void *buf,
71 if (parcel == nullptr || buf == nullptr || size == 0) {
72 _E("Invalid parameter");
73 return PARCEL_ERROR_INVALID_PARAMETER;
76 auto* h = static_cast<Parcel*>(parcel);
78 return PARCEL_ERROR_NONE;
81 extern "C" EXPORT int parcel_burst_read(parcel_h parcel, void *buf,
83 if (parcel == nullptr || buf == nullptr || size == 0) {
84 _E("Invalid parameter");
85 return PARCEL_ERROR_INVALID_PARAMETER;
88 auto* h = static_cast<Parcel*>(parcel);
90 return PARCEL_ERROR_NONE;
93 extern "C" EXPORT int parcel_write_bool(parcel_h parcel, bool val) {
94 if (parcel == nullptr) {
95 _E("Invalid parameter");
96 return PARCEL_ERROR_INVALID_PARAMETER;
99 auto* h = static_cast<Parcel*>(parcel);
101 return PARCEL_ERROR_NONE;
104 extern "C" EXPORT int parcel_write_byte(parcel_h parcel, char val) {
105 if (parcel == nullptr) {
106 _E("Invalid parameter");
107 return PARCEL_ERROR_INVALID_PARAMETER;
110 auto* h = static_cast<Parcel*>(parcel);
112 return PARCEL_ERROR_NONE;
115 extern "C" EXPORT int parcel_write_uint16(parcel_h parcel, uint16_t val) {
116 if (parcel == nullptr) {
117 _E("Invalid parameter");
118 return PARCEL_ERROR_INVALID_PARAMETER;
121 auto* h = static_cast<Parcel*>(parcel);
123 return PARCEL_ERROR_NONE;
126 extern "C" EXPORT int parcel_write_uint32(parcel_h parcel, uint32_t val) {
127 if (parcel == nullptr) {
128 _E("Invalid parameter");
129 return PARCEL_ERROR_INVALID_PARAMETER;
132 auto* h = static_cast<Parcel*>(parcel);
134 return PARCEL_ERROR_NONE;
137 extern "C" EXPORT int parcel_write_uint64(parcel_h parcel, uint64_t val) {
138 if (parcel == nullptr) {
139 _E("Invalid parameter");
140 return PARCEL_ERROR_INVALID_PARAMETER;
143 auto* h = static_cast<Parcel*>(parcel);
145 return PARCEL_ERROR_NONE;
148 extern "C" EXPORT int parcel_write_int16(parcel_h parcel, int16_t val) {
149 if (parcel == nullptr) {
150 _E("Invalid parameter");
151 return PARCEL_ERROR_INVALID_PARAMETER;
154 auto* h = static_cast<Parcel*>(parcel);
156 return PARCEL_ERROR_NONE;
159 extern "C" EXPORT int parcel_write_int32(parcel_h parcel, int32_t val) {
160 if (parcel == nullptr) {
161 _E("Invalid parameter");
162 return PARCEL_ERROR_INVALID_PARAMETER;
165 auto* h = static_cast<Parcel*>(parcel);
167 return PARCEL_ERROR_NONE;
170 extern "C" EXPORT int parcel_write_int64(parcel_h parcel, int64_t val) {
171 if (parcel == nullptr) {
172 _E("Invalid parameter");
173 return PARCEL_ERROR_INVALID_PARAMETER;
176 auto* h = static_cast<Parcel*>(parcel);
178 return PARCEL_ERROR_NONE;
181 extern "C" EXPORT int parcel_write_float(parcel_h parcel, float val) {
182 if (parcel == nullptr) {
183 _E("Invalid parameter");
184 return PARCEL_ERROR_INVALID_PARAMETER;
187 auto* h = static_cast<Parcel*>(parcel);
189 return PARCEL_ERROR_NONE;
192 extern "C" EXPORT int parcel_write_double(parcel_h parcel, double val) {
193 if (parcel == nullptr) {
194 _E("Invalid parameter");
195 return PARCEL_ERROR_INVALID_PARAMETER;
198 auto* h = static_cast<Parcel*>(parcel);
200 return PARCEL_ERROR_NONE;
203 extern "C" EXPORT int parcel_write_string(parcel_h parcel, const char* str) {
204 if (parcel == nullptr || str == nullptr) {
205 _E("Invalid parameter");
206 return PARCEL_ERROR_INVALID_PARAMETER;
209 auto* h = static_cast<Parcel*>(parcel);
210 h->WriteCString(str);
211 return PARCEL_ERROR_NONE;
214 extern "C" EXPORT int parcel_read_bool(parcel_h parcel, bool* val) {
215 if (parcel == nullptr || val == nullptr) {
216 _E("Invalid parameter");
217 return PARCEL_ERROR_INVALID_PARAMETER;
220 auto* h = static_cast<Parcel*>(parcel);
221 return h->ReadBool(val);
224 extern "C" EXPORT int parcel_read_byte(parcel_h parcel, char* val) {
225 if (parcel == nullptr || val == nullptr) {
226 _E("Invalid parameter");
227 return PARCEL_ERROR_INVALID_PARAMETER;
230 auto* h = static_cast<Parcel*>(parcel);
231 return h->ReadByte(val);
234 extern "C" EXPORT int parcel_read_uint16(parcel_h parcel, uint16_t* val) {
235 if (parcel == nullptr || val == nullptr) {
236 _E("Invalid parameter");
237 return PARCEL_ERROR_INVALID_PARAMETER;
240 auto* h = static_cast<Parcel*>(parcel);
241 return h->ReadUInt16(val);
244 extern "C" EXPORT int parcel_read_uint32(parcel_h parcel, uint32_t* val) {
245 if (parcel == nullptr || val == nullptr) {
246 _E("Invalid parameter");
247 return PARCEL_ERROR_INVALID_PARAMETER;
250 auto* h = static_cast<Parcel*>(parcel);
251 return h->ReadUInt32(val);
254 extern "C" EXPORT int parcel_read_uint64(parcel_h parcel, uint64_t* val) {
255 if (parcel == nullptr || val == nullptr) {
256 _E("Invalid parameter");
257 return PARCEL_ERROR_INVALID_PARAMETER;
260 auto* h = static_cast<Parcel*>(parcel);
261 return h->ReadUInt64(val);
264 extern "C" EXPORT int parcel_read_int16(parcel_h parcel, int16_t* val) {
265 if (parcel == nullptr || val == nullptr) {
266 _E("Invalid parameter");
267 return PARCEL_ERROR_INVALID_PARAMETER;
270 auto* h = static_cast<Parcel*>(parcel);
271 return h->ReadInt16(val);
274 extern "C" EXPORT int parcel_read_int32(parcel_h parcel, int32_t* val) {
275 if (parcel == nullptr || val == nullptr) {
276 _E("Invalid parameter");
277 return PARCEL_ERROR_INVALID_PARAMETER;
280 auto* h = static_cast<Parcel*>(parcel);
281 return h->ReadInt32(val);
284 extern "C" EXPORT int parcel_read_int64(parcel_h parcel, int64_t* val) {
285 if (parcel == nullptr || val == nullptr) {
286 _E("Invalid parameter");
287 return PARCEL_ERROR_INVALID_PARAMETER;
290 auto* h = static_cast<Parcel*>(parcel);
291 return h->ReadInt64(val);
294 extern "C" EXPORT int parcel_read_float(parcel_h parcel, float* val) {
295 if (parcel == nullptr || val == nullptr) {
296 _E("Invalid parameter");
297 return PARCEL_ERROR_INVALID_PARAMETER;
300 auto* h = static_cast<Parcel*>(parcel);
301 return h->ReadFloat(val);
304 extern "C" EXPORT int parcel_read_double(parcel_h parcel, double* val) {
305 if (parcel == nullptr || val == nullptr) {
306 _E("Invalid parameter");
307 return PARCEL_ERROR_INVALID_PARAMETER;
310 auto* h = static_cast<Parcel*>(parcel);
311 return h->ReadDouble(val);
314 extern "C" EXPORT int parcel_read_string(parcel_h parcel, char** str) {
315 if (parcel == nullptr || str == nullptr) {
316 _E("Invalid parameter");
317 return PARCEL_ERROR_INVALID_PARAMETER;
320 auto* h = static_cast<Parcel*>(parcel);
321 return h->ReadCString(str);
324 extern "C" EXPORT int parcel_reset_reader(parcel_h parcel) {
325 if (parcel == nullptr) {
326 _E("Invalid parameter");
327 return PARCEL_ERROR_INVALID_PARAMETER;
330 auto* h = static_cast<Parcel*>(parcel);
332 return PARCEL_ERROR_NONE;
335 extern "C" EXPORT int parcel_clear(parcel_h parcel) {
336 if (parcel == nullptr) {
337 _E("Invalid parameter");
338 return PARCEL_ERROR_INVALID_PARAMETER;
341 auto* h = static_cast<Parcel*>(parcel);
343 return PARCEL_ERROR_NONE;
346 extern "C" EXPORT int parcel_reset(parcel_h parcel, const void* buf,
348 if (parcel == nullptr || buf == nullptr || size == 0) {
349 _E("Invalid parameter");
350 return PARCEL_ERROR_INVALID_PARAMETER;
353 auto* h = static_cast<Parcel*>(parcel);
355 return PARCEL_ERROR_NONE;
358 extern "C" EXPORT int parcel_write(parcel_h parcel, parcelable_t* parcelable,
360 if (parcel == nullptr || parcelable == nullptr || parcelable->to == nullptr) {
361 _E("Invalid parameter");
362 return PARCEL_ERROR_INVALID_PARAMETER;
365 parcelable->to(parcel, data);
366 return PARCEL_ERROR_NONE;
369 extern "C" EXPORT int parcel_read(parcel_h parcel, parcelable_t* parcelable,
371 if (parcel == nullptr || parcelable == nullptr ||
372 parcelable->from == nullptr) {
373 _E("Invalid parameter");
374 return PARCEL_ERROR_INVALID_PARAMETER;
377 parcelable->from(parcel, data);
378 return PARCEL_ERROR_NONE;
381 extern "C" EXPORT int parcel_get_raw(parcel_h parcel, void** raw,
383 if (parcel == nullptr || raw == nullptr || size == nullptr) {
384 _E("Invalid parameter");
385 return PARCEL_ERROR_INVALID_PARAMETER;
388 auto* h = static_cast<Parcel*>(parcel);
389 auto& raw_data = h->GetRaw();
390 *raw = reinterpret_cast<void*>(const_cast<uint8_t*>(&raw_data[0]));
391 *size = raw_data.size();
392 return PARCEL_ERROR_NONE;
395 extern "C" EXPORT int parcel_set_byte_order(parcel_h parcel,
397 if (parcel == nullptr) {
398 _E("Invalid parameter");
399 return PARCEL_ERROR_INVALID_PARAMETER;
402 auto* h = static_cast<Parcel*>(parcel);
403 h->SetByteOrder(big_endian);
404 return PARCEL_ERROR_NONE;