Add new error (ACR-1257)
[platform/core/api/connection.git] / src / libnetwork_mptcp.c
1 /*
2  * Copyright (c) 2011-2013 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 <glib.h>
18
19 #include "net_connection_private.h"
20 #include "connection_extension.h"
21 #include "net_connection_mptcp_private.h"
22
23 static const char* __convert_mptcp_path_manager_enum_to_str(connection_mptcp_path_manager_e pm)
24 {
25         switch (pm) {
26         case CONNECTION_MPTCP_PM_UNKNOWN:
27                 return NULL;
28         case CONNECTION_MPTCP_PM_DEFAULT:
29                 return "default";
30         case CONNECTION_MPTCP_PM_FULLMESH:
31                 return "fullmesh";
32         }
33         return NULL;
34 }
35
36 static connection_mptcp_path_manager_e __convert_mptcp_path_manager_str_to_enum(char* pm)
37 {
38         if (g_strcmp0(pm, "default") == 0)
39                 return CONNECTION_MPTCP_PM_DEFAULT;
40         if (g_strcmp0(pm, "fullmesh") == 0)
41                 return CONNECTION_MPTCP_PM_FULLMESH;
42         return CONNECTION_MPTCP_PM_UNKNOWN;
43 }
44
45 static const char* __convert_mptcp_scheduler_enum_to_str(connection_mptcp_scheduler_e scheduler)
46 {
47         switch (scheduler) {
48         case CONNECTION_MPTCP_SCHEDULER_UNKNOWN:
49                 return NULL;
50         case CONNECTION_MPTCP_SCHEDULER_DEFAULT:
51                 return "default";
52         case CONNECTION_MPTCP_SCHEDULER_ROUNDROBIN:
53                 return "roundrobin";
54         }
55         return NULL;
56 }
57
58 static connection_mptcp_scheduler_e __convert_mptcp_scheduler_str_to_enum(char *scheduler)
59 {
60         if (g_strcmp0(scheduler, "default") == 0)
61                 return CONNECTION_MPTCP_SCHEDULER_DEFAULT;
62         if (g_strcmp0(scheduler, "roundrobin") == 0)
63                 return CONNECTION_MPTCP_SCHEDULER_ROUNDROBIN;
64         return CONNECTION_MPTCP_SCHEDULER_UNKNOWN;
65 }
66
67 gboolean _connection_libnet_mptcp_supported()
68 {
69         int rv = 0;
70         gboolean support = false;
71
72         rv = net_mptcp_supported(&support);
73         if (rv == NET_ERR_ACCESS_DENIED) {
74                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
75                 return false;
76         } else if (rv != NET_ERR_NONE) {
77                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable MPTCP [%d]", rv);
78                 return false;
79         }
80
81         return support;
82 }
83
84 int _connection_libnet_mptcp_enable(connection_mptcp_enable_e enable)
85 {
86         int rv = 0;
87
88         rv = net_mptcp_set_enable((int)enable);
89         if (rv == NET_ERR_ACCESS_DENIED) {
90                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
91                 return CONNECTION_ERROR_PERMISSION_DENIED;
92         } else if (rv != NET_ERR_NONE) {
93                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable MPTCP [%d]", rv);
94                 return CONNECTION_ERROR_OPERATION_FAILED;
95         }
96
97         return CONNECTION_ERROR_NONE;
98 }
99
100 int _connection_libnet_mptcp_get_enabled(connection_mptcp_enable_e* enable)
101 {
102         int rv = 0;
103         int result = 0;
104
105         rv = net_mptcp_get_enabled(&result);
106         if (rv == NET_ERR_ACCESS_DENIED) {
107                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
108                 return CONNECTION_ERROR_PERMISSION_DENIED;
109         } else if (rv != NET_ERR_NONE) {
110                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MPTCP enabled state [%d]", rv);
111                 return CONNECTION_ERROR_OPERATION_FAILED;
112         }
113
114         *enable = (connection_mptcp_enable_e)result;
115         return CONNECTION_ERROR_NONE;
116 }
117
118
119 int _connection_libnet_mptcp_set_path_manager(connection_mptcp_path_manager_e pm)
120 {
121         int rv = 0;
122         const char* str = __convert_mptcp_path_manager_enum_to_str(pm);
123         CONNECTION_LOG(CONNECTION_ERROR, "set path manager %s", str);
124
125         if (str == NULL) {
126                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid Parameter");
127                 return CONNECTION_ERROR_INVALID_PARAMETER;
128         }
129
130         rv = net_mptcp_set_path_manager(str);
131         if (rv == NET_ERR_ACCESS_DENIED) {
132                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
133                 return CONNECTION_ERROR_PERMISSION_DENIED;
134         } else if (rv != NET_ERR_NONE) {
135                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set MPTCP path manager [%d]", rv);
136                 return CONNECTION_ERROR_OPERATION_FAILED;
137         }
138
139         return CONNECTION_ERROR_NONE;
140 }
141
142 int _connection_libnet_mptcp_get_path_manager(connection_mptcp_path_manager_e* pm)
143 {
144         int rv = 0;
145         char* result = NULL;
146
147         rv = net_mptcp_get_path_manager(&result);
148         if (rv == NET_ERR_ACCESS_DENIED) {
149                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
150                 return CONNECTION_ERROR_PERMISSION_DENIED;
151         } else if (rv != NET_ERR_NONE) {
152                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MPTCP path manager [%d]", rv);
153                 return CONNECTION_ERROR_OPERATION_FAILED;
154         }
155
156         CONNECTION_LOG(CONNECTION_ERROR, "get path manager %s", result);
157         *pm = __convert_mptcp_path_manager_str_to_enum(result);
158         g_free(result);
159         return CONNECTION_ERROR_NONE;
160 }
161
162
163 int _connection_libnet_mptcp_set_scheduler(connection_mptcp_scheduler_e scheduler)
164 {
165         int rv = 0;
166         const char *str = __convert_mptcp_scheduler_enum_to_str(scheduler);
167         CONNECTION_LOG(CONNECTION_ERROR, "set scheduler %s", str);
168
169         if (str == NULL) {
170                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid Parameter");
171                 return CONNECTION_ERROR_INVALID_PARAMETER;
172         }
173
174         rv = net_mptcp_set_scheduler(str);
175         if (rv == NET_ERR_ACCESS_DENIED) {
176                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
177                 return CONNECTION_ERROR_PERMISSION_DENIED;
178         } else if (rv != NET_ERR_NONE) {
179                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set MPTCP scheduler [%d]", rv);
180                 return CONNECTION_ERROR_OPERATION_FAILED;
181         }
182
183         return CONNECTION_ERROR_NONE;
184 }
185
186 int _connection_libnet_mptcp_get_scheduler(connection_mptcp_scheduler_e* scheduler)
187 {
188         int rv = 0;
189         char* result = NULL;
190
191         rv = net_mptcp_get_scheduler(&result);
192         if (rv == NET_ERR_ACCESS_DENIED) {
193                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
194                 return CONNECTION_ERROR_PERMISSION_DENIED;
195         } else if (rv != NET_ERR_NONE) {
196                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MPTCP scheduler [%d]", rv);
197                 return CONNECTION_ERROR_OPERATION_FAILED;
198         }
199
200         CONNECTION_LOG(CONNECTION_ERROR, "get scheduler %s", result);
201         *scheduler = __convert_mptcp_scheduler_str_to_enum(result);
202         g_free(result);
203         return CONNECTION_ERROR_NONE;
204 }