2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vconf/vconf.h>
26 #include "stc-monitor.h"
27 #include "stc-plugin-tether.h"
29 static GSList *station_list = NULL;
30 static GDBusConnection *connection = NULL;
31 static GCancellable *cancellable = NULL;
32 static int g_mobileap_signal_sub_id = 0;
34 static int _compare_sta_by_mac_func(gconstpointer a,
37 tether_sta_info_s *si = (tether_sta_info_s *)a;
38 return g_ascii_strcasecmp(si->mac, (const char *)b);
41 static int _get_station_info(gconstpointer data, GCompareFunc func,
42 tether_sta_info_s **si)
44 GSList *list = station_list;
45 tether_sta_info_s *info = NULL;
47 if (func == NULL || si == NULL)
53 list = g_slist_find_custom(list, data, func);
62 static void _remove_station_info(gconstpointer data, GCompareFunc func)
64 GSList *list = station_list;
65 tether_sta_info_s *info = NULL;
72 list = g_slist_find_custom(list, data, func);
76 info = (tether_sta_info_s *)list->data;
77 STC_LOGI("STA-REMOVED: (%s) (%s) (%s)", info->ip, info->mac, info->name);
78 g_free(info->station_id);
81 station_list = g_slist_delete_link(station_list, list);
84 static void _add_station_info(tether_sta_info_s *info)
86 tether_sta_info_s *tmp = NULL;
88 STC_LOGE("info is NULL");
92 if (_get_station_info(info->mac, _compare_sta_by_mac_func, &tmp) == 0) {
93 if (!g_strcmp0(tmp->name, info->name) && !g_strcmp0(tmp->ip, info->ip))
96 //Remove the station if dhcp info changed.
97 _remove_station_info(info->mac, _compare_sta_by_mac_func);
100 station_list = g_slist_prepend(station_list, info);
101 STC_LOGI("STA-ADDED: (%s) (%s) (%s)", info->ip, info->mac, info->name);
102 info->station_id = g_strdup_printf("%s_%s", info->mac, info->name);
105 static void _mobileap_signal_cb(GDBusConnection *conn,
106 const gchar *name, const gchar *path,
107 const gchar *interface, const gchar *sig,
108 GVariant *param, gpointer user_data)
114 char *hostname = NULL;
116 tether_sta_info_s *sta = NULL;
118 ret_msg_if(sig == NULL, "signal name NULL");
119 ret_msg_if(param == NULL, "param NULL");
121 STC_LOGI("%s interface(%s)", sig, interface);
123 sta = (tether_sta_info_s *)g_malloc0(sizeof(tether_sta_info_s));
125 STC_LOGE("g_malloc0 failed");
129 g_variant_get(param, "(susssu)", &state, &type, &ip, &mac, &hostname, &tm);
130 STC_LOGI("%s: ip(%s) mac(%s) name(%s) tm(%d)", state, ip, mac, hostname, tm);
132 if (!g_strcmp0(state, "DhcpConnected")) {
133 g_strlcpy(sta->ip, ip, STATION_STR_INFO_LEN);
134 g_strlcpy(sta->mac, mac, STATION_STR_INFO_LEN);
135 g_strlcpy(sta->name, hostname, STATION_STR_HOSTNAME_LEN);
136 _add_station_info(sta);
137 } else if (!g_strcmp0(state, "DhcpLeaseDeleted")) {
138 _remove_station_info(mac, _compare_sta_by_mac_func);
147 stc_error_e tether_plugin_status_changed(void)
149 return STC_ERROR_NONE;
152 int tether_plugin_init(void)
154 GError *error = NULL;
159 connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
165 cancellable = g_cancellable_new();
167 g_mobileap_signal_sub_id = g_dbus_connection_signal_subscribe(connection,
169 TETHERING_SERVICE_INTERFACE,
170 SIGNAL_NAME_DHCP_STATUS,
172 G_DBUS_SIGNAL_FLAGS_NONE,
176 STC_LOGI("tether plugin initialised");
180 void tether_plugin_deinit(void)
185 g_object_unref(connection);
186 g_object_unref(cancellable);
189 STC_LOGI("tether plugin deinitialised");
192 /* Tether Plugin APIs */
193 API stc_plugin_tether_s tether_plugin = {
194 .init = tether_plugin_init,
195 .deinit = tether_plugin_deinit,
196 .status_changed = tether_plugin_status_changed