Merge "Return errors to caller" into tizen_5.5
[platform/core/connectivity/stc-manager.git] / src / stc-manager-plugin-tether.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 <dlfcn.h>
18
19 #include "stc-manager.h"
20 #include "stc-manager-plugin-tether.h"
21
22 static gboolean stc_tether_plugin_enabled = FALSE;
23 static void *tether_plugin_handle;
24 static stc_plugin_tether_s *plugin;
25
26 int stc_plugin_tether_init(void)
27 {
28         __STC_LOG_FUNC_ENTER__;
29
30         tether_plugin_handle = dlopen(STC_PLUGIN_TETHER_FILEPATH, RTLD_NOW);
31         if (!tether_plugin_handle) {
32                 STC_LOGE("Can't load %s: %s", STC_PLUGIN_TETHER_FILEPATH, dlerror());
33                 __STC_LOG_FUNC_EXIT__;
34                 return STC_ERROR_UNINITIALIZED;
35         }
36
37         plugin = dlsym(tether_plugin_handle, "tether_plugin");
38         if (!plugin) {
39                 STC_LOGE("Can't load symbol: %s", dlerror());
40                 dlclose(tether_plugin_handle);
41                 __STC_LOG_FUNC_EXIT__;
42                 return STC_ERROR_UNINITIALIZED;
43         }
44
45         plugin->init();
46         stc_tether_plugin_enabled = TRUE;
47
48         __STC_LOG_FUNC_EXIT__;
49         return STC_ERROR_NONE;
50 }
51
52 int stc_plugin_tether_deinit(void)
53 {
54         __STC_LOG_FUNC_ENTER__;
55
56         if (!stc_tether_plugin_enabled) {
57                 __STC_LOG_FUNC_EXIT__;
58                 return STC_ERROR_UNINITIALIZED;
59         }
60
61         plugin->deinit();
62         stc_tether_plugin_enabled = FALSE;
63         dlclose(tether_plugin_handle);
64
65         __STC_LOG_FUNC_EXIT__;
66         return STC_ERROR_NONE;
67 }
68
69 API int stc_plugin_tether_get_station_ip(const char *mac, char **ipaddr)
70 {
71         char ip[INET_ADDRSTRLEN+1];
72
73         if (!stc_tether_plugin_enabled ||
74                 mac == NULL || ipaddr == NULL) {
75                 if (STC_DEBUG_LOG)
76                         STC_LOGE("invalid args");
77                 return STC_ERROR_INVALID_PARAMETER;
78         }
79
80         memset(ip, 0, sizeof(ip));
81
82         if (plugin->get_station_ip(mac, ip) != STC_ERROR_NONE)
83                 return STC_ERROR_FAIL;
84
85         *ipaddr = g_strdup(ip);
86         STC_LOGI("station ip(%s)", *ipaddr);
87
88         return STC_ERROR_NONE;
89 }
90
91 API int stc_plugin_tether_get_station_by_classid(const int classid, char **mac)
92 {
93         __STC_LOG_FUNC_ENTER__;
94         char mac_addr[STATION_MAC_STR_LEN+1];
95
96         if (!stc_tether_plugin_enabled || mac == NULL) {
97                 if (STC_DEBUG_LOG)
98                         STC_LOGE("invalid args");
99                 return STC_ERROR_INVALID_PARAMETER;
100         }
101
102         memset(mac_addr, 0, sizeof(mac_addr));
103
104         if (plugin->get_station_by_classid(classid, mac_addr) != STC_ERROR_NONE)
105                 return STC_ERROR_FAIL;
106
107         *mac = g_strdup(mac_addr);
108         STC_LOGI("station mac(%s)", *mac);
109
110         return STC_ERROR_NONE;
111 }
112
113 API int stc_plugin_tether_set_station_classid(const char *mac, int classid)
114 {
115         __STC_LOG_FUNC_ENTER__;
116
117         if (!stc_tether_plugin_enabled || mac == NULL) {
118                 if (STC_DEBUG_LOG)
119                         STC_LOGE("invalid args");
120                 return STC_ERROR_INVALID_PARAMETER;
121         }
122
123         if (plugin->set_station_classid(mac, classid) != STC_ERROR_NONE)
124                 return STC_ERROR_FAIL;
125
126         STC_LOGI("classid(%d) for station mac(%s) is set successfully",
127                         classid, mac);
128
129         return STC_ERROR_NONE;
130 }