clean up: devied files, remove router
[platform/core/pim/pims-ipc.git] / src / pims-ipc-data.c
1 /*
2  * PIMS IPC
3  *
4  * Copyright (c) 2012 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26
27 #include "pims-internal.h"
28 #include "pims-ipc-data.h"
29
30 /*
31  * Structure of data with type(4 bytes order)
32  * +------------------------------------------------------------------+
33  * | type | size | data        | pad | type | size | data       | pad |
34  * +------------------------------------------------------------------+
35  * 4      4     size         0-3          (Size of bytes)
36  *
37  * Structure of data without type(4 bytes order)
38  * +----------------------------------------------------+
39  * | size | data        | pad | size | data       | pad |
40  * +----------------------------------------------------+
41  * 4     size         0-3        (Size of bytes)
42  */
43
44 #define _get_used_size_with_type(data_size) \
45         (sizeof(int) + sizeof(int) + data_size + ((sizeof(int) - (data_size % sizeof(int)))%sizeof(int)))
46
47 #define _get_used_size(data_size) \
48         (sizeof(int) + data_size + ((sizeof(int) - (data_size % sizeof(int)))%sizeof(int)))
49
50 API pims_ipc_data_h pims_ipc_data_create_with_size(unsigned int size, int flags)
51 {
52         pims_ipc_data_s *handle = NULL;
53
54         handle = calloc(1, sizeof(pims_ipc_data_s));
55         if (NULL == handle) {
56                 ERR("calloc() Fail");
57                 return NULL;
58         }
59
60         if (flags & PIMS_IPC_DATA_FLAGS_WITH_TYPE) {
61                 ERR("Not supported with-type mode");
62                 free(handle);
63                 return NULL;
64         }
65
66         handle->alloc_size = size;
67         handle->free_size = size;
68         handle->buf_size = 0;
69         handle->buf = calloc(1, size);
70         if (NULL == handle->buf) {
71                 ERR("calloc() Fail");
72                 free(handle);
73                 return NULL;
74         }
75         handle->pos = handle->buf;
76         handle->created = 1;
77         handle->buf_alloced = 1;
78
79         return handle;
80 }
81
82 API void pims_ipc_data_destroy(pims_ipc_data_h data)
83 {
84         pims_ipc_data_s *handle = data;
85         if (NULL == handle)
86                 return;
87
88         if (handle->buf_alloced)
89                 free(handle->buf);
90
91         free(handle);
92 }
93
94 API int pims_ipc_data_put(pims_ipc_data_h data, void *buf, unsigned int size)
95 {
96         pims_ipc_data_s *handle = data;
97         unsigned int dsize = size;
98         unsigned int used_size = 0;
99
100         if (handle->created == 0) {
101                 ERR("This handle isn't create mode.");
102                 return -1;
103         }
104
105         if (dsize > 0 && buf == NULL) {
106                 ERR("Invalid argument");
107                 return -1;
108         }
109
110         used_size = _get_used_size(dsize);
111         if (handle->free_size < used_size) {
112                 int new_size = 0;
113                 new_size = handle->alloc_size * 2;
114
115                 while (new_size < handle->buf_size + used_size)
116                         new_size *= 2;
117                 handle->buf = realloc(handle->buf, new_size);
118                 if (NULL == handle->buf) {
119                         ERR("realloc() Fail");
120                         return -1;
121                 }
122                 handle->alloc_size = new_size;
123                 handle->free_size = handle->alloc_size - handle->buf_size;
124                 handle->pos = handle->buf;
125                 handle->pos += handle->buf_size;
126                 VERBOSE("free_size [%d] dsize [%d]", handle->free_size, dsize);
127         }
128
129         *(int*)(handle->pos) = dsize;
130         if (dsize > 0) {
131                 memcpy((handle->pos+sizeof(int)), buf, dsize);
132                 int pad_size = used_size-dsize-sizeof(int);
133                 if (pad_size > 0)
134                         memset((handle->pos+sizeof(int)+dsize), 0x0, pad_size);
135         }
136
137         handle->pos += used_size;
138         handle->buf_size += used_size;
139         handle->free_size -= used_size;
140
141         VERBOSE("data put size [%u] data[%p]", dsize, buf);
142
143         return 0;
144 }
145
146 API void* pims_ipc_data_get(pims_ipc_data_h data, unsigned int *size)
147 {
148         pims_ipc_data_s *handle = data;
149         unsigned int dsize = 0;
150         unsigned int used_size = 0;
151         void *buf = NULL;
152
153         if (handle->created == 1) {
154                 ERR("This handle is create mode.");
155                 return NULL;
156         }
157         if (handle->buf_size == 0) {
158                 ERR("Remain buffer size is 0.");
159                 return NULL;
160         }
161
162         dsize = *(int*)(handle->pos);
163         buf = (handle->pos+sizeof(int));
164
165         if (dsize == 0) /* current position is next size field becasue data size is 0 */
166                 buf = NULL;
167
168         used_size = _get_used_size(dsize);
169         handle->pos += used_size;
170         handle->buf_size -= used_size;
171         handle->free_size += used_size;
172
173         VERBOSE("data get size [%u] data[%p]", dsize, buf);
174         *size = dsize;
175         return buf;
176 }
177
178 pims_ipc_data_h pims_ipc_data_steal_unmarshal(void *buf, unsigned int size)
179 {
180         pims_ipc_data_s *handle = NULL;
181
182         VERBOSE("size : %d", size);
183         handle = calloc(1, sizeof(pims_ipc_data_s));
184         if (NULL == handle) {
185                 ERR("calloc() handle Fail");
186                 return NULL;
187         }
188         handle->buf = calloc(1, size + 1);
189         if (NULL == handle->buf) {
190                 ERR("calloc() buf Fail");
191                 free(handle);
192                 return NULL;
193         }
194         memcpy(handle->buf, buf, size);
195
196         handle->alloc_size = size;
197         handle->free_size = 0;
198         handle->buf_size = handle->alloc_size;
199         handle->pos = handle->buf;
200         handle->created = 0;
201         handle->buf_alloced = 1;
202
203         VERBOSE("handle[%p]", handle);
204
205         return handle;
206 }
207