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