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_write_bundle(parcel_h parcel, bundle* b) {
215 if (parcel == nullptr || b == nullptr) {
216 _E("Invalid parameter");
217 return PARCEL_ERROR_INVALID_PARAMETER;
220 auto* h = static_cast<Parcel*>(parcel);
222 return PARCEL_ERROR_NONE;
225 extern "C" EXPORT int parcel_read_bool(parcel_h parcel, bool* val) {
226 if (parcel == nullptr || val == nullptr) {
227 _E("Invalid parameter");
228 return PARCEL_ERROR_INVALID_PARAMETER;
231 auto* h = static_cast<Parcel*>(parcel);
232 return h->ReadBool(val);
235 extern "C" EXPORT int parcel_read_byte(parcel_h parcel, char* val) {
236 if (parcel == nullptr || val == nullptr) {
237 _E("Invalid parameter");
238 return PARCEL_ERROR_INVALID_PARAMETER;
241 auto* h = static_cast<Parcel*>(parcel);
242 return h->ReadByte(val);
245 extern "C" EXPORT int parcel_read_uint16(parcel_h parcel, uint16_t* val) {
246 if (parcel == nullptr || val == nullptr) {
247 _E("Invalid parameter");
248 return PARCEL_ERROR_INVALID_PARAMETER;
251 auto* h = static_cast<Parcel*>(parcel);
252 return h->ReadUInt16(val);
255 extern "C" EXPORT int parcel_read_uint32(parcel_h parcel, uint32_t* val) {
256 if (parcel == nullptr || val == nullptr) {
257 _E("Invalid parameter");
258 return PARCEL_ERROR_INVALID_PARAMETER;
261 auto* h = static_cast<Parcel*>(parcel);
262 return h->ReadUInt32(val);
265 extern "C" EXPORT int parcel_read_uint64(parcel_h parcel, uint64_t* val) {
266 if (parcel == nullptr || val == nullptr) {
267 _E("Invalid parameter");
268 return PARCEL_ERROR_INVALID_PARAMETER;
271 auto* h = static_cast<Parcel*>(parcel);
272 return h->ReadUInt64(val);
275 extern "C" EXPORT int parcel_read_int16(parcel_h parcel, int16_t* val) {
276 if (parcel == nullptr || val == nullptr) {
277 _E("Invalid parameter");
278 return PARCEL_ERROR_INVALID_PARAMETER;
281 auto* h = static_cast<Parcel*>(parcel);
282 return h->ReadInt16(val);
285 extern "C" EXPORT int parcel_read_int32(parcel_h parcel, int32_t* val) {
286 if (parcel == nullptr || val == nullptr) {
287 _E("Invalid parameter");
288 return PARCEL_ERROR_INVALID_PARAMETER;
291 auto* h = static_cast<Parcel*>(parcel);
292 return h->ReadInt32(val);
295 extern "C" EXPORT int parcel_read_int64(parcel_h parcel, int64_t* val) {
296 if (parcel == nullptr || val == nullptr) {
297 _E("Invalid parameter");
298 return PARCEL_ERROR_INVALID_PARAMETER;
301 auto* h = static_cast<Parcel*>(parcel);
302 return h->ReadInt64(val);
305 extern "C" EXPORT int parcel_read_float(parcel_h parcel, float* val) {
306 if (parcel == nullptr || val == nullptr) {
307 _E("Invalid parameter");
308 return PARCEL_ERROR_INVALID_PARAMETER;
311 auto* h = static_cast<Parcel*>(parcel);
312 return h->ReadFloat(val);
315 extern "C" EXPORT int parcel_read_double(parcel_h parcel, double* val) {
316 if (parcel == nullptr || val == nullptr) {
317 _E("Invalid parameter");
318 return PARCEL_ERROR_INVALID_PARAMETER;
321 auto* h = static_cast<Parcel*>(parcel);
322 return h->ReadDouble(val);
325 extern "C" EXPORT int parcel_read_string(parcel_h parcel, char** str) {
326 if (parcel == nullptr || str == nullptr) {
327 _E("Invalid parameter");
328 return PARCEL_ERROR_INVALID_PARAMETER;
331 auto* h = static_cast<Parcel*>(parcel);
332 return h->ReadCString(str);
335 extern "C" EXPORT int parcel_read_bundle(parcel_h parcel, bundle** b) {
336 if (parcel == nullptr || b == nullptr) {
337 _E("Invalid parameter");
338 return PARCEL_ERROR_INVALID_PARAMETER;
341 auto* h = static_cast<Parcel*>(parcel);
342 return h->ReadCBundle(b);
345 extern "C" EXPORT int parcel_reset_reader(parcel_h parcel) {
346 if (parcel == nullptr) {
347 _E("Invalid parameter");
348 return PARCEL_ERROR_INVALID_PARAMETER;
351 auto* h = static_cast<Parcel*>(parcel);
353 return PARCEL_ERROR_NONE;
356 extern "C" EXPORT int parcel_clear(parcel_h parcel) {
357 if (parcel == nullptr) {
358 _E("Invalid parameter");
359 return PARCEL_ERROR_INVALID_PARAMETER;
362 auto* h = static_cast<Parcel*>(parcel);
364 return PARCEL_ERROR_NONE;
367 extern "C" EXPORT int parcel_reset(parcel_h parcel, const void* buf,
369 if (parcel == nullptr || buf == nullptr || size == 0) {
370 _E("Invalid parameter");
371 return PARCEL_ERROR_INVALID_PARAMETER;
374 auto* h = static_cast<Parcel*>(parcel);
376 return PARCEL_ERROR_NONE;
379 extern "C" EXPORT int parcel_write(parcel_h parcel, parcelable_t* parcelable,
381 if (parcel == nullptr || parcelable == nullptr || parcelable->to == nullptr) {
382 _E("Invalid parameter");
383 return PARCEL_ERROR_INVALID_PARAMETER;
386 parcelable->to(parcel, data);
387 return PARCEL_ERROR_NONE;
390 extern "C" EXPORT int parcel_read(parcel_h parcel, parcelable_t* parcelable,
392 if (parcel == nullptr || parcelable == nullptr ||
393 parcelable->from == nullptr) {
394 _E("Invalid parameter");
395 return PARCEL_ERROR_INVALID_PARAMETER;
398 parcelable->from(parcel, data);
399 return PARCEL_ERROR_NONE;
402 extern "C" EXPORT int parcel_get_raw(parcel_h parcel, void** raw,
404 if (parcel == nullptr || raw == nullptr || size == nullptr) {
405 _E("Invalid parameter");
406 return PARCEL_ERROR_INVALID_PARAMETER;
409 auto* h = static_cast<Parcel*>(parcel);
410 auto& raw_data = h->GetRaw();
411 *raw = reinterpret_cast<void*>(const_cast<uint8_t*>(&raw_data[0]));
412 *size = raw_data.size();
413 return PARCEL_ERROR_NONE;