2 * Network Configuration Module
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements wifi direct peer functions.
23 * @file wifi-direct-peer.c
24 * @author Gibyoung Kim (lastkgb.kim@samsung.com)
35 #include <wifi-direct.h>
37 #include "wifi-direct-ipc.h"
38 #include "wifi-direct-manager.h"
39 #include "wifi-direct-oem.h"
40 #include "wifi-direct-util.h"
41 #include "wifi-direct-peer.h"
42 #include "wifi-direct-session.h"
43 #include "wifi-direct-log.h"
45 void wfd_peer_destroy(wfd_device_s *peer)
59 wfd_device_s *wfd_add_peer(void *data, unsigned char *dev_addr, char *dev_name)
61 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
62 wfd_manager_s *manager = (wfd_manager_s*) data;
63 wfd_device_s *peer = NULL;
65 if (!data || !dev_addr || !dev_name) {
66 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
67 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
71 peer = wfd_peer_find_by_dev_addr(manager, dev_addr);
73 WDS_LOGD("Peer already exist[" MACSECSTR "]", MAC2SECSTR(dev_addr));//LCOV_EXCL_LINE
74 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
78 peer = (wfd_device_s*) g_try_malloc0(sizeof(wfd_device_s));
80 WDS_LOGE("Failed to allocate memory for peer[" MACSTR "]", MAC2STR(dev_addr));//LCOV_EXCL_LINE
81 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
84 memcpy(peer->dev_addr, dev_addr, MACADDR_LEN);
85 g_strlcpy(peer->dev_name, dev_name, DEV_NAME_LEN + 1);
88 manager->peers = g_list_prepend(manager->peers, peer);
89 manager->peer_count++;
90 WDS_LOGD("peer_count[%d]", manager->peer_count);
91 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
95 int wfd_remove_peer(void *data, unsigned char *dev_addr)
97 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
98 wfd_manager_s *manager = (wfd_manager_s*) data;
99 wfd_device_s *peer = NULL;
101 if (!data || !dev_addr) {
102 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
103 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
107 peer = wfd_peer_find_by_dev_addr(manager, dev_addr);
109 WDS_LOGE("Failed to find peer device");//LCOV_EXCL_LINE
110 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
114 manager->peers = g_list_remove(manager->peers, peer);
115 manager->peer_count--;
117 wfd_peer_destroy(peer);
119 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
123 int wfd_update_peer_time(void*data, unsigned char *peer_addr)
125 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
126 wfd_manager_s *manager = (wfd_manager_s*) data;
127 wfd_device_s *peer = NULL;
129 if (!manager || !peer_addr) {
130 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
134 peer = wfd_peer_find_by_dev_addr(manager, peer_addr);
136 WDS_LOGD("Peer not found [" MACSECSTR "]", MAC2SECSTR(peer_addr));//LCOV_EXCL_LINE
140 #if !(__GNUC__ <= 4 && __GNUC_MINOR__ < 8)
141 wfd_util_get_current_time(&peer->time);
144 gettimeofday(&tval, NULL);
145 peer->time = tval.tv_sec;
148 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
152 int wfd_update_peer(void *data, wfd_device_s *peer)
154 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
155 wfd_manager_s *manager = (wfd_manager_s*) data;
156 wfd_oem_device_s *oem_dev = NULL;
160 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
165 //wfd_update_peer takes responsibility of the oem_dev' memory ownership.
166 res = wfd_oem_get_peer_info(manager->oem_ops, peer->dev_addr, &oem_dev);
167 if (res < 0 || !oem_dev) {
168 WDS_LOGE("Failed to get peer information");//LCOV_EXCL_LINE
172 if (oem_dev->age > 30 && peer->state == WFD_PEER_STATE_DISCOVERED) {
173 WDS_LOGE("Too old age to update peer");//LCOV_EXCL_LINE
175 g_free(oem_dev->vsie);
179 g_strlcpy(peer->dev_name, oem_dev->dev_name, DEV_NAME_LEN + 1);
180 if (!ISZEROMACADDR(oem_dev->intf_addr))
181 memcpy(peer->intf_addr, oem_dev->intf_addr, MACADDR_LEN);
182 memcpy(peer->go_dev_addr, oem_dev->go_dev_addr, MACADDR_LEN);
183 peer->channel = oem_dev->channel;
184 peer->dev_role = oem_dev->dev_role;
185 peer->config_methods = oem_dev->config_methods;
186 peer->pri_dev_type = oem_dev->pri_dev_type;
187 peer->sec_dev_type = oem_dev->sec_dev_type;
188 peer->dev_flags = oem_dev->dev_flags;
189 peer->group_flags = oem_dev->group_flags;
190 peer->wps_mode = oem_dev->wps_mode;
192 if (manager->is_wifi_display_supported)
193 memcpy(&(peer->display), &(oem_dev->display), sizeof(wfd_display_s));
196 g_free(oem_dev->vsie);
200 #if !(__GNUC__ <= 4 && __GNUC_MINOR__ < 8)
201 wfd_util_get_current_time(&peer->time);
204 gettimeofday(&tval, NULL);
205 peer->time = tval.tv_sec;
208 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
212 int wfd_peer_clear_all(void *data)
214 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
215 wfd_manager_s *manager = (wfd_manager_s*) data;
216 wfd_device_s *peer = NULL;
217 wfd_session_s *session = NULL;
223 if (manager->peer_count == 0) {
224 WDS_LOGD("Peer not exist");//LCOV_EXCL_LINE
228 temp = g_list_first(manager->peers);
230 peer = (wfd_device_s*) temp->data;
231 wfd_peer_destroy(peer);
232 temp = g_list_next(temp);
233 manager->peer_count--;
236 if (manager->peers) {
237 g_list_free(manager->peers);
238 manager->peers = NULL;
241 if (manager->peer_count) {
242 WDS_LOGE("Peer count is not synced. left count=%d", manager->peer_count);//LCOV_EXCL_LINE
243 manager->peer_count = 0;
247 session = manager->session;
249 session->peer = NULL;
251 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
255 wfd_device_s *wfd_peer_find_by_dev_addr(void *data, unsigned char *dev_addr)
257 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
258 wfd_manager_s *manager = (wfd_manager_s*) data;
259 wfd_device_s *peer = NULL;
262 if (!data || !dev_addr) {
263 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
267 if (manager->peer_count == 0) {
268 WDS_LOGE("There is no peer data");//LCOV_EXCL_LINE
272 temp = g_list_first(manager->peers);
275 if (!memcmp(peer->dev_addr, dev_addr, MACADDR_LEN)) {
276 WDS_LOGD("Peer device found[" MACSECSTR "]", MAC2SECSTR(dev_addr));//LCOV_EXCL_LINE
279 temp = g_list_next(temp);
283 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
288 wfd_device_s *wfd_peer_find_by_intf_addr(void *data, unsigned char *intf_addr)
290 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
291 wfd_manager_s *manager = (wfd_manager_s*) data;
292 wfd_device_s *peer = NULL;
295 if (!data || !intf_addr) {
296 WDS_LOGE("Invalid parameter");
300 if (manager->peer_count == 0) {
301 WDS_LOGE("There is no peer data");
305 temp = g_list_first(manager->peers);
308 if (!memcmp(peer->intf_addr, intf_addr, MACADDR_LEN)) {
309 WDS_LOGD("Peer device found[" MACSECSTR "]", MAC2SECSTR(intf_addr));
312 temp = g_list_next(temp);
316 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
321 wfd_device_s *wfd_peer_find_by_addr(void *data, unsigned char *addr)
323 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
324 wfd_manager_s *manager = (wfd_manager_s*) data;
325 wfd_device_s *peer = NULL;
328 if (!data || !addr) {
329 WDS_LOGE("Invalid parameter");//LCOV_EXCL_LINE
333 if (manager->peer_count == 0) {
334 WDS_LOGE("There is no peer data");//LCOV_EXCL_LINE
338 temp = g_list_first(manager->peers);
341 if (!memcmp(peer->dev_addr, addr, MACADDR_LEN) ||
342 !memcmp(peer->intf_addr, addr, MACADDR_LEN)) {
343 WDS_LOGD("Peer device found[" MACSECSTR "]", MAC2SECSTR(addr));//LCOV_EXCL_LINE
346 temp = g_list_next(temp);
350 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
355 wfd_device_s *wfd_peer_find_current_peer(void *data)
357 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
358 wfd_manager_s *manager = (wfd_manager_s*) data;
360 WDS_LOGE("Invalid parameter");
364 wfd_session_s *session = manager->session;
366 WDS_LOGE("Session not found");
370 if (!session->peer) {
371 WDS_LOGE("Peer not found");
375 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
376 return session->peer;
379 int wfd_peer_set_data(unsigned char *dev_addr, int type, int data)
381 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
382 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
386 int wfd_peer_get_data(unsigned char *dev_addr, int type, int data)
388 __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
389 __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE