dlog print error fix
[platform/core/messaging/msg-service.git] / proxy / MsgHandleFilter.cpp
1 /*
2  * Copyright (c) 2014 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 #include "MsgDebug.h"
18 #include "MsgHandle.h"
19 #include "MsgUtilFunction.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22
23
24 /*==================================================================================================
25                                      IMPLEMENTATION OF MsgHandle - Filter Member Functions
26 ==================================================================================================*/
27 msg_error_t MsgHandle::addFilter(const MSG_FILTER_S *pFilter)
28 {
29         /* Allocate Memory to Command Data */
30         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_FILTER_S);
31
32         char cmdBuf[cmdSize];
33         bzero(cmdBuf, cmdSize);
34         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
35
36         /* Set Command Parameters */
37         pCmd->cmdType = MSG_CMD_ADD_FILTER;
38
39         /* Copy Cookie */
40         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
41
42         /* Copy Command Data*/
43         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pFilter, sizeof(MSG_FILTER_S));
44
45         /* Send Command to Messaging FW */
46         char* pEventData = NULL;
47         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
48
49         write((char*)pCmd, cmdSize, &pEventData);
50
51         /* Get Return Data */
52         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
53
54         if (pEvent->eventType != MSG_EVENT_ADD_FILTER) {
55                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
56         }
57
58         return pEvent->result;
59 }
60
61
62 msg_error_t MsgHandle::updateFilter(const MSG_FILTER_S *pFilter)
63 {
64         /* Allocate Memory to Command Data */
65         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_FILTER_S);
66
67         char cmdBuf[cmdSize];
68         bzero(cmdBuf, cmdSize);
69         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
70
71         /* Set Command Parameters */
72         pCmd->cmdType = MSG_CMD_UPDATE_FILTER;
73
74         /* Copy Cookie */
75         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
76
77         /* Copy Command Data */
78         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pFilter, sizeof(MSG_FILTER_S));
79
80         /* Send Command to Messaging FW */
81         char* pEventData = NULL;
82         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
83
84         write((char*)pCmd, cmdSize, &pEventData);
85
86         /* Get Return Data */
87         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
88
89         if (pEvent->eventType != MSG_EVENT_UPDATE_FILTER) {
90                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
91         }
92
93         return pEvent->result;
94 }
95
96
97 msg_error_t MsgHandle::deleteFilter(msg_filter_id_t FilterId)
98 {
99         /* Allocate Memory to Command Data */
100         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_filter_id_t);
101
102         char cmdBuf[cmdSize];
103         bzero(cmdBuf, cmdSize);
104         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
105
106         /* Set Command Parameters */
107         pCmd->cmdType = MSG_CMD_DELETE_FILTER;
108
109         /* Copy Cookie */
110         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
111
112         /* Copy Command Data */
113         memcpy(pCmd->cmdData, &FilterId, sizeof(msg_filter_id_t));
114
115         /* Send Command to Messaging FW */
116         char* pEventData = NULL;
117         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
118
119         write((char*)pCmd, cmdSize, &pEventData);
120
121         /* Get Return Data */
122         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
123
124         if (pEvent->eventType != MSG_EVENT_DELETE_FILTER) {
125                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
126         }
127
128         return pEvent->result;
129 }
130
131
132 msg_error_t MsgHandle::getFilterList(msg_struct_list_s *pFilterList)
133 {
134         /* Allocate Memory to Command Data */
135         int cmdSize = sizeof(MSG_CMD_S);
136
137         char cmdBuf[cmdSize];
138         bzero(cmdBuf, cmdSize);
139         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
140
141         /* Set Command Parameters */
142         pCmd->cmdType = MSG_CMD_GET_FILTERLIST;
143
144         /* Copy Cookie */
145         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
146
147         /* Send Command to Messaging FW */
148         char* pEventData = NULL;
149         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
150
151         write((char*)pCmd, cmdSize, &pEventData);
152
153         /* Get Return Data */
154         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
155
156         if (pEvent->eventType != MSG_EVENT_GET_FILTERLIST) {
157                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
158         }
159
160         if (pEvent->result != MSG_SUCCESS) return pEvent->result;
161
162         /* Decode Return Data */
163         MsgDecodeFilterList(pEvent->data, pFilterList);
164
165         return MSG_SUCCESS;
166 }
167
168
169 msg_error_t MsgHandle::setFilterOperation(bool bSetFlag)
170 {
171         msg_error_t ret = MSG_SUCCESS;
172
173         /* Allocate Memory to Command Data */
174         int cmdSize = sizeof(MSG_CMD_S) + sizeof(bool);
175
176         char cmdBuf[cmdSize];
177         bzero(cmdBuf, cmdSize);
178         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
179
180         /* Set Command Parameters */
181         pCmd->cmdType = MSG_CMD_SET_FILTER_OPERATION;
182
183         /* Copy Cookie */
184         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
185
186         /* Copy Command Data */
187         memcpy(pCmd->cmdData, &bSetFlag, sizeof(bool));
188
189         /* Send Command to Messaging FW */
190         char* pEventData = NULL;
191         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
192
193         write((char*)pCmd, cmdSize, &pEventData);
194
195         /* Get Return Data */
196         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
197
198         if (pEvent->eventType != MSG_EVENT_SET_FILTER_OPERATION) {
199                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
200         }
201
202         ret = pEvent->result;
203
204         return ret;
205 }
206
207
208 msg_error_t MsgHandle::getFilterOperation(bool *pSetFlag)
209 {
210         msg_error_t ret = MSG_SUCCESS;
211
212         /* Allocate Memory to Command Data */
213         int cmdSize = sizeof(MSG_CMD_S);
214
215         char cmdBuf[cmdSize];
216         bzero(cmdBuf, cmdSize);
217         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
218
219         /* Set Command Parameters */
220         pCmd->cmdType = MSG_CMD_GET_FILTER_OPERATION;
221
222         /* Copy Cookie */
223         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
224
225         /* Send Command to Messaging FW */
226         char* pEventData = NULL;
227         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
228
229         write((char*)pCmd, cmdSize, &pEventData);
230
231         /* Get Return Data */
232         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
233
234         if (pEvent->eventType != MSG_EVENT_GET_FILTER_OPERATION) {
235                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
236         }
237
238         ret = pEvent->result;
239
240         /* Decode Return Data */
241         if (ret == MSG_SUCCESS) {
242                 MsgDecodeFilterFlag(pEvent->data, pSetFlag);
243                 MSG_DEBUG("Flag : %d", *pSetFlag);
244         }
245
246         return ret;
247 }
248
249
250 msg_error_t MsgHandle::setFilterActivation(msg_filter_id_t filter_id, bool active)
251 {
252         /* Allocate Memory to Command Data */
253         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_filter_id_t) + sizeof(bool);
254
255         char cmdBuf[cmdSize];
256         bzero(cmdBuf, cmdSize);
257         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
258
259         /* Set Command Parameters */
260         pCmd->cmdType = MSG_CMD_SET_FILTER_ACTIVATION;
261
262         /* Copy Cookie */
263         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
264
265         /* Copy Command Data */
266         memcpy(pCmd->cmdData, &filter_id, sizeof(msg_filter_id_t));
267         memcpy(pCmd->cmdData+sizeof(msg_filter_id_t), &active, sizeof(bool));
268
269         /* Send Command to Messaging FW */
270         char* pEventData = NULL;
271         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
272
273         write((char*)pCmd, cmdSize, &pEventData);
274
275         /* Get Return Data */
276         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
277
278         if (pEvent->eventType != MSG_EVENT_SET_FILTER_ACTIVATION) {
279                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
280         }
281
282         return pEvent->result;
283 }