summaryrefslogtreecommitdiff
path: root/src/wifi-aware-peer.c
blob: 416982658dc14c59ab6035cc76df3d75f3dd4d57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Wi-Fi Aware
 *
 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <glib.h>

#include "wifi-aware-log.h"
#include "wifi-aware-private.h"
#include "wifi-aware-peer.h"
#include "wifi-aware-utils.h"

typedef struct {
	unsigned int id;
	unsigned char mac[WIFI_AWARE_MAC_ADDRESS_LEN];
} wifi_aware_peer_s;

int _wifi_aware_peer_create(wifi_aware_peer_h *peer, unsigned int peer_id)
{
	wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)g_try_malloc0(sizeof(wifi_aware_peer_s));
	RET_VAL_IF(peer_handle == NULL, WIFI_AWARE_ERROR_OUT_OF_MEMORY, "g_try_malloc0 failed");

	peer_handle->id = peer_id;
	*peer = peer_handle;
	return WIFI_AWARE_ERROR_NONE;
}

void _wifi_aware_peer_destroy(wifi_aware_peer_h peer)
{
	g_free((wifi_aware_peer_s *)peer);
}

int _wifi_aware_peer_clone(wifi_aware_peer_h *cloned, wifi_aware_peer_h origin)
{
	wifi_aware_peer_s *cloned_handle = (wifi_aware_peer_s *)g_try_malloc0(sizeof(wifi_aware_peer_s));
	RET_VAL_IF(cloned_handle == NULL, WIFI_AWARE_ERROR_OUT_OF_MEMORY, "g_try_malloc0 failed");

	memcpy(cloned_handle, origin, sizeof(wifi_aware_peer_s));
	*cloned = cloned_handle;
	return WIFI_AWARE_ERROR_NONE;
}

int _wifi_aware_peer_add(GHashTable *peer_map, wifi_aware_peer_h peer)
{
	wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)peer;
	RET_VAL_IF(peer_handle == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "peer is NULL");

	if (g_hash_table_contains(peer_map, GINT_TO_POINTER(peer_handle->id))) {
		WIFI_AWARE_LOGE("peer_id %d alread exists", peer_handle->id);
		return WIFI_AWARE_ERROR_INVALID_PARAMETER;
	}

	g_hash_table_insert(peer_map, GINT_TO_POINTER(peer_handle->id), peer);

	return WIFI_AWARE_ERROR_NONE;
}

int _wifi_aware_peer_remove(GHashTable *peer_map, wifi_aware_peer_h peer)
{
	wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)peer;
	RET_VAL_IF(peer_handle == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "peer is NULL");

	if (peer_handle == NULL) {
		WIFI_AWARE_LOGE("peer is NULL");
		return WIFI_AWARE_ERROR_INVALID_PARAMETER;
	}

	if (!g_hash_table_remove(peer_map, GINT_TO_POINTER(peer_handle->id))) {
		WIFI_AWARE_LOGE("peer %d doesn't exist", peer_handle->id);
	}

	return WIFI_AWARE_ERROR_NONE;
}

wifi_aware_peer_h _wifi_aware_get_peer(GHashTable *peer_map, unsigned int peer_id)
{
	wifi_aware_peer_s *peer = NULL;

	peer = g_hash_table_lookup(peer_map, GINT_TO_POINTER(peer_id));
	return peer;
}

unsigned int _wifi_aware_peer_get_id(wifi_aware_peer_h peer)
{
	wifi_aware_peer_s *handle = (wifi_aware_peer_s *)peer;
	return handle->id;
}

int _wifi_aware_peer_get_mac(wifi_aware_peer_h peer, unsigned char **mac)
{
	*mac = (unsigned char *)g_try_malloc0(sizeof(unsigned char *) * WIFI_AWARE_MAC_ADDRESS_LEN);
	RET_VAL_IF(mac == NULL, WIFI_AWARE_ERROR_OUT_OF_MEMORY, "g_try_malloc0 failed");
	wifi_aware_peer_s *handle = (wifi_aware_peer_s *)peer;
	memcpy(*mac, handle->mac, WIFI_AWARE_MAC_ADDRESS_LEN);
	return WIFI_AWARE_ERROR_NONE;
}

void _wifi_aware_peer_set_mac(wifi_aware_peer_h  peer, unsigned char *mac)
{
	wifi_aware_peer_s *handle = (wifi_aware_peer_s *)peer;
	memcpy(handle->mac, mac, WIFI_AWARE_MAC_ADDRESS_LEN);
}