Add Parcel Library
[platform/core/base/bundle.git] / parcel / api / parcel.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __PARCEL_H__
18 #define __PARCEL_H__
19
20 #include <stdint.h>
21 #include <stddef.h>
22 #include <bundle.h>
23 #include <tizen.h>
24
25 /**
26  * @addtogroup CORE_LIB_PARCEL_MODULE
27  * @{
28  */
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /**
35  * @brief The parcel handle.
36  * @since_tizen 6.5
37  */
38 typedef void *parcel_h;
39
40 /**
41  * @brief The interface for converting data to/from a parcel.
42  * @since_tizen 6.5
43  */
44 typedef struct {
45         void (*to)(parcel_h parcel, void *data);
46         void (*from)(parcel_h parcel, void *data);
47 } parcelable_t;
48
49 /**
50  * @brief Enumeration for error codes for a parcel.
51  * @since_tizen 6.5
52  */
53 typedef enum {
54         PARCEL_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
55         PARCEL_ERROR_ILLEGAL_BYTE_SEQ = TIZEN_ERROR_ILLEGAL_BYTE_SEQ, /**< Illegal byte sequence */
56         PARCEL_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
57         PARCEL_ERROR_NO_DATA = TIZEN_ERROR_NO_DATA, /**< No data available */
58         PARCEL_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
59 } parcel_error_e;
60
61 /**
62  * @brief Creates a parcel handle.
63  * @since_tizen 6.5
64  * @remarks You MUST release @a parcel using parcel_destroy().
65  * @param[out] parcel The parcel handle that is newly created
66  * @return @c 0 on success,
67  *         otherwise a negative error value
68  * @retval #PARCEL_ERROR_NONE Successful
69  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
70  * @retval #PARCEL_ERROR_OUT_OF_MEMORY Out of memory
71  * @see parcel_destroy()
72  */
73 int parcel_create(parcel_h *parcel);
74
75 /**
76  * @brief Destroys a parcel handle.
77  * @since_tizen 6.5
78  * @param[in] parcel The parcel handle
79  * @return @c 0 on success,
80  *         otherwise a negative error value
81  * @retval #PARCEL_ERROR_NONE Successful
82  * @retval #PARCEL_ERROR_INVALID_PARAMETER
83  * @see parcel_create()
84  */
85 int parcel_destroy(parcel_h parcel);
86
87 /**
88  * @brief Creates and returns a copy of the given parcel handle.
89  * @since_tizen 6.5
90  * @remarks A newly created parcel should be destroyed by calling the parcel_destroy() if it is no longer needed.
91  * @param[in] parcel The parcel handle
92  * @param[out] If successful, a newly created parcel handle will be returned
93  * @return @c 0 on success,
94  *         otherwise a negative error value
95  * @retval #PARCEL_ERROR_NONE Successful
96  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
97  * @see parcel_destroy()
98  */
99 int parcel_clone(parcel_h parcel, parcel_h *clone);
100
101 /**
102  * @brief Writes bytes to the parcel handle.
103  * @since_tizen 6.5
104  * @param[in] parcel The parcel handle
105  * @param[in] buf The array buffer to write
106  * @param[in] size The size of bytes to write
107  * @return @c 0 on success,
108  *         otherwise a negative error value
109  * @retval #PARCEL_ERROR_NONE Successful
110  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
111  * @see parcel_burst_read()
112  */
113 int parcel_burst_write(parcel_h parcel, const void *buf, uint32_t size);
114
115 /**
116  * @brief Reads bytes from the parcel handle.
117  * @since_tizen 6.5
118  * @param[in] parcel The parcel handle
119  * @param[out] buf The array buffer to read
120  * @param[in] size The size of bytes to read
121  * @return @c 0 on success,
122  *         otherwise a negative error value
123  * @retval #PARCEL_ERROR_NONE Successful
124  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
125  * @retval #PARCEL_ERROR_NO_DATA No data available
126  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
127  * @see parcel_burst_write()
128  */
129 int parcel_burst_read(parcel_h parcel, void *buf, uint32_t size);
130
131 /**
132  * @brief Writes a boolean value into the parcel handle.
133  * @since_tizen 6.5
134  * @param[in] parcel The parcel handle
135  * @param[in] val The boolean value
136  * @return @c 0 on success,
137  *         otherwise a negative error value
138  * @retval #PARCEL_ERROR_NONE Successful
139  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
140  * @see parcel_read_bool()
141  */
142 int parcel_write_bool(parcel_h parcel, bool val);
143
144 /**
145  * @brief Writes a byte value into the parcel handle.
146  * @since_tizen 6.5
147  * @param[in] parcel The parcel handle
148  * @param[in] val The byte value
149  * @return @c 0 on success,
150  *         otherwise a negative error value
151  * @retval #PARCEL_ERROR_NONE Successful
152  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
153  * @see parcel_read_uint8()
154  */
155 int parcel_write_byte(parcel_h parcel, char val);
156
157 /**
158  * @brief Writes an unsigned short value into the parcel handle.
159  * @since_tizen 6.5
160  * @param[in] parcel The parcel handle
161  * @param[in] val The unsigned short value
162  * @return @c 0 on success,
163  *         otherwise a negative error value
164  * @retval #PARCEL_ERROR_NONE Successful
165  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
166  * @see parcel_read_uint16()
167  */
168 int parcel_write_uint16(parcel_h parcel, uint16_t val);
169
170 /**
171  * @brief Writes an unsigned integer value into the parcel handle.
172  * @since_tizen 6.5
173  * @param[in] parcel The parcel handle
174  * @param[in] val The unsigned integer value
175  * @return @c 0 on success,
176  *         otherwise a negative error value
177  * @retval #PARCEL_ERROR_NONE Successful
178  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
179  * @see parcel_read_uint32()
180  */
181 int parcel_write_uint32(parcel_h parcel, uint32_t val);
182
183 /**
184  * @brief Writes an unsigned long long integer value into the parcel handle.
185  * @since_tizen 6.5
186  * @param[in] parcel The parcel handle
187  * @param[in] val The unsigned long long interger value
188  * @return @c 0 on success,
189  *         otherwise a negative error value
190  * @retval #PARCEL_ERROR_NONE Successful
191  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
192  * @see parcel_read_uint64()
193  */
194 int parcel_write_uint64(parcel_h parcel, uint64_t val);
195
196 /**
197  * @brief Writes a signed short value into the parcel handle.
198  * @since_tizen 6.5
199  * @param[in] parcel The parcel handle
200  * @param[in] val The signed short value
201  * @return @c 0 on success,
202  *         otherwise a negative error value
203  * @retval #PARCEL_ERROR_NONE Successful
204  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
205  * @see parcel_read_int16()
206  */
207 int parcel_write_int16(parcel_h parcel, int16_t val);
208
209 /**
210  * @brief Writes a signed integer value into the parcel handle.
211  * @since_tizen 6.5
212  * @param[in] parcel The parcel handle
213  * @param[in] val The signed integer value
214  * @return @c 0 on success,
215  *         otherwise a negative error value
216  * @retval #PARCEL_ERROR_NONE Successful
217  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
218  * @see parcel_read_int32()
219  */
220 int parcel_write_int32(parcel_h parcel, int32_t val);
221
222 /**
223  * @brief Writes a signed long long integer value into the parcel handle.
224  * @since_tizen 6.5
225  * @param[in] parcel The parcel handle
226  * @param[in] val The signedi long long integer value
227  * @return @c 0 on success,
228  *         otherwise a negative error value
229  * @retval #PARCEL_ERROR_NONE Successful
230  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
231  * @see parcel_read_int64()
232  */
233 int parcel_write_int64(parcel_h parcel, int64_t val);
234
235 /**
236  * @brief Writes a floating point value into the parcel handle.
237  * @since_tizen 6.5
238  * @param[in] parcel The parcel handle
239  * @param[in] val The floating point value
240  * @return @c 0 on success,
241  *         otherwise a negative error value
242  * @retval #PARCEL_ERROR_NONE Successful
243  * @retval #PARCLE_ERROR_INVALID_PARAMETER Invalid parameter
244  * @see parcel_read_float()
245  */
246 int parcel_write_float(parcel_h parcel, float val);
247
248 /**
249  * @brief Writes a double precision floating point value into the parcel handle.
250  * @since_tizen 6.5
251  * @param[in] parcel The parcel handle
252  * @param[in] val The double precision floating point value
253  * @return @c 0 on success,
254  *         otherwise a negative error value
255  * @retval #PARCEL_ERROR_NONE Successful
256  * @retval #PARCLE_ERROR_INVALID_PARAMETER Invalid parameter
257  * @see parcel_read_double()
258  */
259 int parcel_write_double(parcel_h parcel, double val);
260
261 /**
262  * @brief Writes a string data into the parcel handle.
263  * @since_tizen 6.5
264  * @param[in] parcel The parcel handle
265  * @param[in] str The string data
266  * @return @c 0 on success,
267  *         otherwise a negative error value
268  * @retval #PARCEL_ERROR_NONE Successful
269  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
270  * @see parcel_read_string()
271  */
272 int parcel_write_string(parcel_h parcel, const char *str);
273
274 /**
275  * @brief Writes a bundle data into the parcel handle.
276  * @since_tizen 6.5
277  * @param[in] parcel The parcel handle
278  * @param[in] b The bundle data
279  * @return @c 0 on success,
280  *         otherwise a negative error value
281  * @retval #PARCEL_ERROR_NONE Successful
282  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
283  * @see parcel_read_bundle()
284  */
285 int parcel_write_bundle(parcel_h parcel, bundle* b);
286
287 /**
288  * @brief Reads a boolean value from the parcel handle.
289  * @since_tizen 6.5
290  * @param[in] parcel The parcel handle
291  * @param[out] val The boolean value
292  * @return @c 0 on success,
293  *         otherwise a negative error value
294  * @retval #PARCEL_ERROR_NONE Successful
295  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
296  * @retval #PARCEL_ERROR_NO_DATA No data available
297  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
298  * @see parcel_write_bool()
299  */
300 int parcel_read_bool(parcel_h parcel, bool *val);
301
302 /**
303  * @brief Reads a byte value from the parcel handle.
304  * @since_tizen 6.5
305  * @param[in] parcel The parcel handle
306  * @param[out] val The byte value
307  * @return @c 0 on success,
308  *         otherwise a negative error value
309  * @retval #PARCEL_ERROR_NONE Successful
310  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
311  * @retval #PARCEL_ERROR_NO_DATA No data available
312  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
313  * @see parcel_write_uint8()
314  */
315 int parcel_read_byte(parcel_h parcel, char *val);
316
317 /**
318  * @brief Reads an unsigned short value from the parcel handle.
319  * @since_tizen 6.5
320  * @param[in] parcel The parcel handle
321  * @param[out] val The unsigned short value
322  * @return @c 0 on success,
323  *         otherwise a negative error value
324  * @retval #PARCEL_ERROR_NONE Successful
325  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
326  * @retval #PARCEL_ERROR_NO_DATA No data available
327  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
328  * @see parcel_write_uint16()
329  */
330 int parcel_read_uint16(parcel_h parcel, uint16_t *val);
331
332 /**
333  * @brief Reads an unsigned integer value from the parcel handle.
334  * @since_tizen 6.5
335  * @param[in] parcel The parcel handle
336  * @param[out] val The unsigned integer value
337  * @return @c 0 on success,
338  *         otherwise a negative error value
339  * @retval #PARCEL_ERROR_NONE Successful
340  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
341  * @retval #PARCEL_ERROR_NO_DATA No data available
342  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
343  * @see parcel_write_uint32()
344  */
345 int parcel_read_uint32(parcel_h parcel, uint32_t *val);
346
347 /**
348  * @brief Reads an unsigned long long integer value from the parcel handle.
349  * @since_tizen 6.5
350  * @param[in] parcel The parcel handle
351  * @param[out] val The unsigned long long integer value
352  * @return @c 0 on success,
353  *         otherwise a negative error value
354  * @retval #PARCEL_ERROR_NONE Successful
355  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
356  * @retval #PARCEL_ERROR_NO_DATA No data available
357  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
358  * @see parcel_write_uint64()
359  */
360 int parcel_read_uint64(parcel_h parcel, uint64_t *val);
361
362 /**
363  * @brief Reads a signed short value from the parcel handle.
364  * @since_tizen 6.5
365  * @param[in] parcel The parcel handle
366  * @param[out] val The signed short value
367  * @return @c 0 on success,
368  *         otherwise a negative error value
369  * @retval #PARCEL_ERROR_NONE Successful
370  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
371  * @retval #PARCEL_ERROR_NO_DATA No data available
372  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
373  * @see parcel_write_int16()
374  */
375 int parcel_read_int16(parcel_h parcel, int16_t *val);
376
377 /**
378  * @brief Reads a signed integer value from the parcel handle.
379  * @since_tizen 6.5
380  * @param[in] parcel The parcel handle
381  * @param[out] val The signed integer value
382  * @return @c 0 on success,
383  *         otherwise a negative error value
384  * @retval #PARCEL_ERROR_NONE Successful
385  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
386  * @retval #PARCEL_ERROR_NO_DATA No data available
387  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
388  * @see parcel_write_int32()
389  */
390 int parcel_read_int32(parcel_h parcel, int32_t *val);
391
392 /**
393  * @brief Reads a signed long long integer value from the parcel handle.
394  * @since_tizen 6.5
395  * @param[in] parcel The parcel handle
396  * @param[out] val The signed long long integer value
397  * @return @c 0 on success,
398  *         otherwise a negative error value
399  * @retval #PARCEL_ERROR_NONE Successful
400  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
401  * @retval #PARCEL_ERROR_NO_DATA No data available
402  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
403  * @see parcel_write_int64()
404  */
405 int parcel_read_int64(parcel_h parcel, int64_t *val);
406
407 /**
408  * @brief Reads a floating point value from the parcel handle.
409  * @since_tizen 6.5
410  * @param[in] parcel The parcel handle
411  * @param[out] val The floating point value
412  * @return @c 0 on success,
413  *         otherwise a negative error value
414  * @retval #PARCEL_ERROR_NONE Successful
415  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
416  * @retval #PARCEL_ERROR_NO_DATA No data available
417  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
418  * @see parcel_write_uint8()
419  */
420 int parcel_read_float(parcel_h parcel, float *val);
421
422 /**
423  * @brief Reads a double precision floating point value from the parcel handle.
424  * @since_tizen 6.5
425  * @param[in] parcel The parcel handle
426  * @param[out] val The double precision floating point value
427  * @return @c 0 on success,
428  *         otherwise a negative error value
429  * @retval #PARCEL_ERROR_NONE Successful
430  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
431  * @retval #PARCEL_ERROR_NO_DATA No data available
432  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
433  * @see parcel_write_uint8()
434  */
435 int parcel_read_double(parcel_h parcel, double *val);
436
437 /**
438  * @brief Reads a string data from the parcel handle.
439  * @since_tizen 6.5
440  * @param[in] parcel The parcel handle
441  * @param[out] str The string data
442  * @return @c 0 on success,
443  *         otherwise a negative error value
444  * @retval #PARCEL_ERROR_NONE Successful
445  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
446  * @retval #PARCEL_ERROR_NO_DATA No data available
447  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
448  * @retval #PARCEL_ERROR_OUT_OF_MEMORY Out of memory
449  * @see parcel_write_uint8()
450  */
451 int parcel_read_string(parcel_h parcel, char **str);
452
453 /**
454  * @brief Reads a bundle data from the parcel handle.
455  * @since_tizen 6.5
456  * @param[in] parcel The parcel handle
457  * @param[out] val The bundle data
458  * @return @c 0 on success,
459  *         otherwise a negative error value
460  * @retval #PARCEL_ERROR_NONE Successful
461  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
462  * @retval #PARCEL_ERROR_NO_DATA No data available
463  * @retval #PARCEL_ERROR_ILLEGAL_BYTE_SEQ Illegal byte sequence
464  * @retval #PARCEL_ERROR_OUT_OF_MEMORY Out of memory
465  * @see parcel_write_uint8()
466  */
467 int parcel_read_bundle(parcel_h parcel, bundle **b);
468
469 /**
470  * @brief Resets the reader pointer of the parcel handle to the start.
471  * @since_tizen 6.5
472  * @param[in] parcel The parcel handle
473  * @return @c 0 on success,
474  *         otherwise a negative error value
475  * @retval #PARCEL_ERROR_NONE Successful
476  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
477  */
478 int parcel_reset_reader(parcel_h parcel);
479
480 /**
481  * @brief Clears the data of the parcel handle.
482  * @since_tizen 6.5
483  * @param[in] parcel The parcel handle
484  * @return @c 0 on success,
485  *         otherwise a negative error value
486  * @retval #PARCEL_ERROR_NONE Successful
487  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
488  */
489 int parcel_clear(parcel_h parcel);
490
491 /**
492  * @brief Resets the data of the parcel handle.
493  * @since_tizen 6.5
494  * @param[in] parcel The parcel handle
495  * @param[in] buf The array buffer to write
496  * @param[in] size The size of bytes to write
497  * @return @c 0 on success,
498  *         otherwise a negative error value
499  * @retval #PARCEL_ERROR_NONE Successful
500  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
501  */
502 int parcel_reset(parcel_h parcel, const void *buf, uint32_t size);
503
504 /**
505  * @brief Writes the data using @a parcelable to the parcel handle.
506  * @since_tizen 6.5
507  * @param[in] parcel The parcel handle
508  * @param[in] parcelable The interface to write the data into the parcel handle
509  * @param[in] data The user data which writes into the parcel handle
510  * @return @c 0 on success,
511  *         otherwise a negative error value
512  * @retval #PARCEL_ERROR_NONE Successful
513  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
514  * @see parcel_read()
515  */
516 int parcel_write(parcel_h parcel, parcelable_t *parcelable, void *data);
517
518 /**
519  * @brief Reads the data using @a parcelable from the parcel handle.
520  * @since_tizen 6.5
521  * @param[in] parcel The parcel handle
522  * @param[in] parcelable The interface to read the data from the parcel handle
523  * @param[in] data The user data which reads from the parcel handle
524  * @return @c 0 on success,
525  *         otherwise a negative error value
526  * @retval #PARCEL_ERROR_NONE Successful
527  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
528  * @see parcel_write()
529  */
530 int parcel_read(parcel_h parcel, parcelable_t *parcelable, void *data);
531
532 /**
533  * @brief Gets the raw data of the parcel handle.
534  * @since_tizen 6.5
535  * @remarks You MUST NOT release @a raw using free(). It's managed by platform.
536  * @param[in] parcel The parcel handle
537  * @param[out] raw The raw data
538  * @param[out] size The size of the raw data
539  * @return @c 0 on success,
540  *         otherwise a negative error value
541  * @retval #PARCEL_ERROR_NONE Successful
542  * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter
543  */
544 int parcel_get_raw(parcel_h parcel, void **raw, uint32_t *size);
545
546 #ifdef __cplusplus
547 }
548 #endif
549
550 /**
551  * @}
552  */
553
554 #endif /* __PARCEL_H__ */