tizen 2.3 release
[kernel/api/system-resource.git] / src / network / restriction-helper.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 2013 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 /**
21  * @file restriction-helper.c
22  * @desc Helper restriction functions
23  */
24
25 #include "const.h"
26 #include "data_usage.h"
27 #include "macro.h"
28 #include "trace.h"
29 #include "transmission.h"
30
31 resourced_iface_type get_store_iftype(const u_int32_t app_classid,
32                                       const resourced_iface_type iftype)
33 {
34         /* We need to put RESOURCED_IFACE_ALL type into the database,
35            in case of the "tethering" because it with no iftype */
36         return (app_classid == RESOURCED_TETHERING_APP_CLASSID) ?
37                 RESOURCED_IFACE_ALL : iftype;
38 }
39
40 resourced_restriction_state convert_to_restriction_state(
41         const enum traffic_restriction_type rst_type)
42 {
43         switch (rst_type) {
44         case RST_SET:
45                 return RESOURCED_RESTRICTION_ACTIVATED;
46         case RST_UNSET:
47                 return RESOURCED_RESTRICTION_REMOVED;
48         case RST_EXCLUDE:
49                 return RESOURCED_RESTRICTION_EXCLUDED;
50         default:
51                 return RESOURCED_RESTRICTION_UNKNOWN;
52         }
53 }
54
55 enum traffic_restriction_type convert_to_restriction_type(
56         const resourced_restriction_state rst_state)
57 {
58         switch (rst_state) {
59         case RESOURCED_RESTRICTION_ACTIVATED:
60                 return RST_SET;
61         case RESOURCED_RESTRICTION_REMOVED:
62                 return RST_UNSET;
63         case RESOURCED_RESTRICTION_EXCLUDED:
64                 return RST_EXCLUDE;
65         default:
66                 return RST_UNDEFINDED;
67         }
68 }
69
70 int check_restriction_arguments(const char *appid,
71                                 const resourced_net_restrictions *rst,
72                                 const enum traffic_restriction_type rst_type)
73 {
74         ret_value_secure_msg_if(!appid, RESOURCED_ERROR_INVALID_PARAMETER,
75                                 "appid is required argument\n");
76         ret_value_msg_if(
77                 rst_type <= RST_UNDEFINDED || rst_type >= RST_MAX_VALUE,
78                 RESOURCED_ERROR_INVALID_PARAMETER,
79                 "Invalid restriction_type %d\n", rst_type);
80         ret_value_msg_if(!rst, RESOURCED_ERROR_INVALID_PARAMETER,
81                          "Restriction should be set\n");
82         ret_value_msg_if(rst->iftype <= RESOURCED_IFACE_UNKNOWN ||
83                          rst->iftype >= RESOURCED_IFACE_LAST_ELEM,
84                          RESOURCED_ERROR_INVALID_PARAMETER,
85                          "Invalid restriction network interface type %d\n",
86                          rst->iftype);
87         if (rst_type == RST_SET) {
88                 ret_value_msg_if(rst->send_limit < 0,
89                                  RESOURCED_ERROR_INVALID_PARAMETER,
90                                  "Invalid send_limit %d\n", rst->send_limit);
91                 ret_value_msg_if(rst->rcv_limit < 0,
92                                  RESOURCED_ERROR_INVALID_PARAMETER,
93                                  "Invalid rcv_limit %d\n", rst->rcv_limit);
94                 ret_value_msg_if(rst->snd_warning_limit < 0,
95                                  RESOURCED_ERROR_INVALID_PARAMETER,
96                                  "Invalid snd_warning_limit %d\n",
97                                  rst->snd_warning_limit);
98                 ret_value_msg_if(rst->rcv_warning_limit < 0,
99                                  RESOURCED_ERROR_INVALID_PARAMETER,
100                                  "Invalid rcv_warning_limit %d\n",
101                                  rst->rcv_warning_limit);
102         }
103
104         /* check roaming */
105         ret_value_msg_if(rst->roaming >= RESOURCED_ROAMING_LAST_ELEM,
106                 RESOURCED_ERROR_INVALID_PARAMETER,
107                 "roaming is not valid %d", rst->roaming);
108         return RESOURCED_ERROR_NONE;
109 }