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