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