de1debe6377fc9076644a1efa95ceccf7d168425
[platform/core/pim/contacts-service.git] / common / ipc / ctsvc_ipc_result.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 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 #include "ctsvc_internal.h"
20 #include "ctsvc_ipc_marshal.h"
21 #include "contacts_record.h"
22
23
24 static int __ctsvc_ipc_unmarshal_search_value(pims_ipc_data_h ipc_data,
25                 ctsvc_result_value_s *pvalue)
26 {
27         int ret;
28
29         ret = ctsvc_ipc_unmarshal_int(ipc_data, &pvalue->property_id);
30         if (ret != CONTACTS_ERROR_NONE) {
31                 ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
32                 return CONTACTS_ERROR_INVALID_PARAMETER;
33         }
34         ret = ctsvc_ipc_unmarshal_int(ipc_data, &pvalue->type);
35         if (ret != CONTACTS_ERROR_NONE) {
36                 ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
37                 return CONTACTS_ERROR_INVALID_PARAMETER;
38         }
39
40
41         if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_STR) == true) {
42                 if (ctsvc_ipc_unmarshal_string(ipc_data, &pvalue->value.s) != CONTACTS_ERROR_NONE) {
43                         ERR("ctsvc_ipc_unmarshal_string() Fail");
44                         return CONTACTS_ERROR_INVALID_PARAMETER;
45                 }
46         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_BOOL) == true) {
47                 if (ctsvc_ipc_unmarshal_bool(ipc_data, &pvalue->value.b) != CONTACTS_ERROR_NONE) {
48                         ERR("ctsvc_ipc_unmarshal_bool() Fail");
49                         return CONTACTS_ERROR_INVALID_PARAMETER;
50                 }
51         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_INT) == true) {
52                 if (ctsvc_ipc_unmarshal_int(ipc_data, &pvalue->value.i) != CONTACTS_ERROR_NONE) {
53                         ERR("ctsvc_ipc_unmarshal_int() Fail");
54                         return CONTACTS_ERROR_INVALID_PARAMETER;
55                 }
56         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_DOUBLE) == true) {
57                 return CONTACTS_ERROR_NONE;
58         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_LLI) == true) {
59                 return CONTACTS_ERROR_NONE;
60         } else {
61                 ASSERT_NOT_REACHED("invalid parameter (property:%d)", pvalue->property_id);
62                 return CONTACTS_ERROR_INVALID_PARAMETER;
63         }
64         return CONTACTS_ERROR_NONE;
65 }
66
67 static int __ctsvc_ipc_marshal_search_value(const ctsvc_result_value_s *pvalue,
68                 pims_ipc_data_h ipc_data)
69 {
70         if (ctsvc_ipc_marshal_int(pvalue->property_id, ipc_data) != CONTACTS_ERROR_NONE) {
71                 ERR("ctsvc_ipc_marshal() Fail");
72                 return CONTACTS_ERROR_INVALID_PARAMETER;
73         }
74         if (ctsvc_ipc_marshal_int(pvalue->type, ipc_data) != CONTACTS_ERROR_NONE) {
75                 ERR("ctsvc_ipc_marshal() Fail");
76                 return CONTACTS_ERROR_INVALID_PARAMETER;
77         }
78
79
80
81         if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_STR) == true) {
82                 if (ctsvc_ipc_marshal_string(pvalue->value.s, ipc_data) != CONTACTS_ERROR_NONE) {
83                         ERR("ctsvc_ipc_marshal() Fail");
84                         return CONTACTS_ERROR_INVALID_PARAMETER;
85                 }
86         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_BOOL) == true) {
87                 if (ctsvc_ipc_marshal_bool(pvalue->value.b, ipc_data) != CONTACTS_ERROR_NONE) {
88                         ERR("ctsvc_ipc_marshal() Fail");
89                         return CONTACTS_ERROR_INVALID_PARAMETER;
90                 }
91         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_INT) == true) {
92                 if (ctsvc_ipc_marshal_int(pvalue->value.i, ipc_data) != CONTACTS_ERROR_NONE) {
93                         ERR("ctsvc_ipc_marshal() Fail");
94                         return CONTACTS_ERROR_INVALID_PARAMETER;
95                 }
96         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_DOUBLE) == true) {
97                 return CONTACTS_ERROR_NONE;
98         } else if (CTSVC_VIEW_CHECK_DATA_TYPE(pvalue->property_id, CTSVC_VIEW_DATA_TYPE_LLI) == true) {
99                 return CONTACTS_ERROR_NONE;
100         } else {
101                 ASSERT_NOT_REACHED("invalid parameter (property:%d)", pvalue->property_id);
102                 return CONTACTS_ERROR_INVALID_PARAMETER;
103         }
104         return CONTACTS_ERROR_NONE;
105 }
106
107
108 static int __ctsvc_ipc_unmarshal_result(pims_ipc_data_h ipc_data, const char *view_uri,
109                 contacts_record_h record)
110 {
111         ctsvc_result_s *result_p = (ctsvc_result_s*)record;
112
113         RETV_IF(NULL == ipc_data, CONTACTS_ERROR_NO_DATA);
114         RETV_IF(NULL == record, CONTACTS_ERROR_NO_DATA);
115
116         unsigned int count = 0;
117         if (ctsvc_ipc_unmarshal_unsigned_int(ipc_data, &count) != CONTACTS_ERROR_NONE) {
118                 ERR("ctsvc_ipc_unmarshal_unsigned_int() fail");
119                 return CONTACTS_ERROR_INVALID_PARAMETER;
120         }
121
122         unsigned int i = 0;
123         for (i = 0; i < count; i++) {
124                 ctsvc_result_value_s *value_data = NULL;
125                 value_data = calloc(1, sizeof(ctsvc_result_value_s));
126                 if (value_data == NULL)
127                         return CONTACTS_ERROR_OUT_OF_MEMORY;
128
129                 if (__ctsvc_ipc_unmarshal_search_value(ipc_data, value_data) != CONTACTS_ERROR_NONE) {
130                         ERR("__ctsvc_ipc_unmarshal_search_value() Fail");
131                         free(value_data);
132                         return CONTACTS_ERROR_INVALID_PARAMETER;
133                 }
134                 result_p->values = g_slist_append(result_p->values, value_data);
135         }
136
137         return CONTACTS_ERROR_NONE;
138 }
139
140 static int __ctsvc_ipc_marshal_result(const contacts_record_h record,
141                 pims_ipc_data_h ipc_data)
142 {
143         ctsvc_result_s *result_p = (ctsvc_result_s*)record;
144
145         RETV_IF(NULL == ipc_data, CONTACTS_ERROR_NO_DATA);
146         RETV_IF(result_p == NULL, CONTACTS_ERROR_NO_DATA);
147
148         if (result_p->values) {
149                 unsigned int count = g_slist_length(result_p->values);
150                 if (ctsvc_ipc_marshal_unsigned_int(count, ipc_data) != CONTACTS_ERROR_NONE) {
151                         ERR("ctsvc_ipc_marshal() Fail");
152                         return CONTACTS_ERROR_INVALID_PARAMETER;
153                 }
154
155                 GSList *cursor = result_p->values;
156                 while (cursor) {
157                         ctsvc_result_value_s *value_data = cursor->data;
158                         if (value_data == NULL) {
159                                 cursor = g_slist_next(cursor);
160                                 continue;
161                         }
162                         if (__ctsvc_ipc_marshal_search_value(value_data, ipc_data) != CONTACTS_ERROR_NONE) {
163                                 ERR("ctsvc_ipc_marshal() Fail");
164                                 return CONTACTS_ERROR_INVALID_PARAMETER;
165                         }
166                         cursor = g_slist_next(cursor);
167                 }
168         } else {
169                 if (ctsvc_ipc_marshal_int(0, ipc_data) != CONTACTS_ERROR_NONE) {
170                         ERR("ctsvc_ipc_marshal() Fail");
171                         return CONTACTS_ERROR_INVALID_PARAMETER;
172                 }
173         }
174
175         return CONTACTS_ERROR_NONE;
176 }
177
178 ctsvc_ipc_marshal_record_plugin_cb_s _ctsvc_ipc_record_result_plugin_cb = {
179         .unmarshal_record = __ctsvc_ipc_unmarshal_result,
180         .marshal_record = __ctsvc_ipc_marshal_result
181 };
182