2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
23 #include "restriction.h"
26 static void AsyncReadyCallback(GObject *source_object,
27 GAsyncResult *res, gpointer user_data)
29 MainLoop *M = (MainLoop *)user_data;
30 GLOGD("Succeeded to response async callback");
36 this->m_AppID[0] = '\0';
37 this->m_IfaceName[0] = '\0';
38 this->m_IfaceType = IFACE_ALL;
39 this->m_DataLimit = 0;
40 this->m_DataWarnLimit = 0;
41 this->m_RoamingType = ROAMING_DISABLED;
42 this->m_SubscriberID[0] = '\0';
49 Restriction::Restriction()
54 Restriction::~Restriction()
59 error_e Restriction::SetRule(const char *app_id, const char *iface_name,
60 const char *subscriber_id, iface_type_e iface_type,
61 int64_t data_limit, int64_t data_warn_limit,
62 roaming_type_e roaming_type, const char *mac)
64 if (app_id == NULL || strlen(app_id) == 0)
65 this->m_Rule.m_AppID[0] = '\0';
67 g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN);
69 if (iface_name == NULL || strlen(iface_name) == 0)
70 this->m_Rule.m_IfaceName[0] = '\0';
72 g_strlcpy(this->m_Rule.m_IfaceName, iface_name, IFNAME_LEN);
74 if (subscriber_id == NULL || strlen(subscriber_id) == 0)
75 this->m_Rule.m_SubscriberID[0] = '\0';
77 g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
79 if (mac == NULL || strlen(mac) == 0)
80 this->m_Rule.m_Mac[0] = '\0';
82 g_strlcpy(this->m_Rule.m_Mac, mac, MAC_LEN);
91 this->m_Rule.m_IfaceType = iface_type;
94 return ERROR_INVALID_PARAMETER;
97 switch(roaming_type) {
100 case ROAMING_DISABLED:
101 this->m_Rule.m_RoamingType = roaming_type;
104 return ERROR_INVALID_PARAMETER;
107 this->m_Rule.m_DataLimit = data_limit;
108 this->m_Rule.m_DataWarnLimit = data_warn_limit;
113 void Restriction::MakeRuleParams(GVariant **params)
115 GVariantBuilder *builder;
117 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
119 g_variant_builder_add(builder, "{sv}",
120 RESTRICTION_RULE_APP_ID,
121 g_variant_new_string(this->m_Rule.m_AppID));
123 g_variant_builder_add(builder, "{sv}",
124 RESTRICTION_RULE_IFNAME,
125 g_variant_new_string(this->m_Rule.m_IfaceName));
127 g_variant_builder_add(builder, "{sv}",
128 RESTRICTION_RULE_IFTYPE,
129 g_variant_new_uint16(this->m_Rule.m_IfaceType));
131 g_variant_builder_add(builder, "{sv}",
132 RESTRICTION_RULE_ROAMING,
133 g_variant_new_uint16(this->m_Rule.m_RoamingType));
135 g_variant_builder_add(builder, "{sv}",
136 RESTRICTION_RULE_SUBSCRIBER_ID,
137 g_variant_new_string(this->m_Rule.m_SubscriberID));
139 g_variant_builder_add(builder, "{sv}",
140 RESTRICTION_RULE_DATA_LIMIT,
141 g_variant_new_int64(this->m_Rule.m_DataLimit));
143 g_variant_builder_add(builder, "{sv}",
144 RESTRICTION_RULE_DATA_WARN_LIMIT,
145 g_variant_new_int64(this->m_Rule.m_DataWarnLimit));
147 g_variant_builder_add(builder, "{sv}",
148 RESTRICTION_RULE_MAC,
149 g_variant_new_string(this->m_Rule.m_Mac));
152 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
153 g_variant_builder_unref(builder);
156 void Restriction::MakeGetParams(GVariant **params)
158 *params = g_variant_new("(s)", this->m_Rule.m_AppID);
161 void Restriction::MakeStatusParams(GVariant **params)
163 *params = g_variant_new("(si)",
164 this->m_Rule.m_AppID, this->m_Rule.m_IfaceType);
167 error_e Restriction::SetRstriction(void)
169 GVariant *message = NULL;
170 GVariant *params = NULL;
171 error_e error = ERROR_NONE;
174 MakeRuleParams(¶ms);
176 message = InvokeMethod(STC_MGR_SERVICE,
177 STC_MGR_RESTRICTION_PATH,
178 STC_MGR_RESTRICTION_INTERFACE,
179 STC_MGR_METHOD_RESTRICTION_SET,
183 if (message == NULL) {
184 GLOGD("Failed to invoke dbus method");
188 g_variant_get(message, "(i)", &result);
189 GLOGD("Succeeded to set restriction [%d]", result);
190 g_variant_unref(message);
195 error_e Restriction::ExcludeRstriction(void)
197 GVariant *message = NULL;
198 GVariant *params = NULL;
199 error_e error = ERROR_NONE;
202 MakeRuleParams(¶ms);
204 message = InvokeMethod(STC_MGR_SERVICE,
205 STC_MGR_RESTRICTION_PATH,
206 STC_MGR_RESTRICTION_INTERFACE,
207 STC_MGR_METHOD_RESTRICTION_EXCLUDE,
211 if (message == NULL) {
212 GLOGD("Failed to invoke dbus method");
216 g_variant_get(message, "(i)", &result);
217 GLOGD("Succeeded to exclude restriction [%d]", result);
218 g_variant_unref(message);
223 error_e Restriction::UnsetRstriction(void)
225 GVariant *message = NULL;
226 GVariant *params = NULL;
227 error_e error = ERROR_NONE;
230 MakeRuleParams(¶ms);
232 message = InvokeMethod(STC_MGR_SERVICE,
233 STC_MGR_RESTRICTION_PATH,
234 STC_MGR_RESTRICTION_INTERFACE,
235 STC_MGR_METHOD_RESTRICTION_UNSET,
239 if (message == NULL) {
240 GLOGD("Failed to invoke dbus method");
244 g_variant_get(message, "(i)", &result);
245 GLOGD("Succeeded to unset restriction [%d]", result);
246 g_variant_unref(message);
251 error_e Restriction::GetRstrictionStatus(restriction_status_e *status)
253 GVariant *message = NULL;
254 GVariant *params = NULL;
255 error_e error = ERROR_NONE;
258 MakeStatusParams(¶ms);
260 message = InvokeMethod(STC_MGR_SERVICE,
261 STC_MGR_RESTRICTION_PATH,
262 STC_MGR_RESTRICTION_INTERFACE,
263 STC_MGR_METHOD_RESTRICTION_GET_STATUS,
267 if (message == NULL) {
268 GLOGD("Failed to invoke dbus method");
272 g_variant_get(message, "(ii)", &result, status);
273 GLOGD("Succeeded to get restriction status [%d:%d]", result, *status);
274 g_variant_unref(message);
279 error_e Restriction::GetRstriction(void)
282 GVariant *params = NULL;
283 error_e error = ERROR_NONE;
285 MakeGetParams(¶ms);
287 error = InvokeMethodNonblock(STC_MGR_SERVICE,
288 STC_MGR_RESTRICTION_PATH,
289 STC_MGR_RESTRICTION_INTERFACE,
290 STC_MGR_METHOD_RESTRICTION_GET_PER_APP_ID,
296 if (error != ERROR_NONE) {
297 GLOGD("Failed to invoke dbus method nonblock");
301 GLOGD("Succeeded to get restriction per app ID");
307 error_e Restriction::GetAllRstriction(void)
310 error_e error = ERROR_NONE;
312 error = InvokeMethodNonblock(STC_MGR_SERVICE,
313 STC_MGR_RESTRICTION_PATH,
314 STC_MGR_RESTRICTION_INTERFACE,
315 STC_MGR_METHOD_RESTRICTION_GET_ALL,
321 if (error != ERROR_NONE) {
322 GLOGD("Failed to invoke dbus method nonblock");
326 GLOGD("Succeeded to get all restriction");