summaryrefslogtreecommitdiff
path: root/popup-wifidirect/src/wfd-app-client.c
blob: ca6b6f97d2c901ab49c0052e227bf34981fec1e4 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * This file is part of org.tizen.wifi-direct-popup
 * Written by Sungsik Jang <sungsik.jang@samsung.com>
 *            Dongwook Lee <dwmax.lee@samsung.com>
 *
 * PROPRIETARY/CONFIDENTIAL
 *
 * This software is the confidential and proprietary information of
 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with SAMSUNG ELECTRONICS. 
 *
 * SAMSUNG make no representations or warranties about the suitability
 * of the software, either express or implied, including but not limited
 * to the implied warranties of merchantability, fitness for a particular
 * purpose, or non-infringement. SAMSUNG shall not be liable for any
 * damages suffered by licensee as a result of using, modifying or
 * distributing this software or its derivatives.
 *
 */

/**
 * This file implements wifi direct application client  functions.
 *
 * @file    wfd-app-client.c
 * @author  Sungsik Jang (sungsik.jang@samsung.com)
 * @version 0.1
 */

#include <stdio.h>
#include <string.h>
#include "wifi-direct.h"
#include "wfd-app.h"
#include "wfd-app-util.h"


void _cb_activation(int error_code, wifi_direct_device_state_e device_state,
                    void *user_data)
{
    __WFD_APP_FUNC_ENTER__;

    wfd_appdata_t *ad = (wfd_appdata_t *) user_data;

    switch (device_state)
    {
    case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
        WFD_APP_LOG(WFD_APP_LOG_LOW,
                    "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
        break;

    case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
        WFD_APP_LOG(WFD_APP_LOG_LOW,
                    "event ------------------ WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
        WFD_APP_LOG(WFD_APP_LOG_LOW,
                    "Termination process of wifi-direct popup begins...\n");
        elm_exit();
        break;

    default:
        break;
    }

    __WFD_APP_FUNC_EXIT__;

}


static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data,
	                                                  const char *mac_address)
{
	__WFD_APP_FUNC_ENTER__;

	wfd_appdata_t *ad = (wfd_appdata_t *) data;
	
	int i;

	if (ad == NULL)
	{
		WFD_APP_LOG(WFD_APP_LOG_LOW, "Incorrect parameter(NULL)\n");
		return NULL;
	}

	WFD_APP_LOG(WFD_APP_LOG_LOW, "find peer by MAC [%s] \n", mac_address);

	for (i = 0; i < ad->discovered_peer_count; i++)
	{
		WFD_APP_LOG(WFD_APP_LOG_LOW, "check %dth peer\n", i);
		
		if (!strncmp(mac_address, (const char *) ad->discovered_peers[i].mac_address, 18))
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "found peer. [%d]\n", i);
			__WFD_APP_FUNC_EXIT__;
			return &ad->discovered_peers[i];
		}
	}
    
	__WFD_APP_FUNC_EXIT__;

    return NULL;
}


bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s * peer,
                            void *user_data)
{
	__WFD_APP_FUNC_ENTER__;

	wfd_appdata_t *ad = (wfd_appdata_t *) user_data;

	if (NULL != peer->ssid)
	{
		WFD_APP_LOG(WFD_APP_LOG_LOW, "discovered peer ssid[%s]\n", peer->ssid);
		strncpy(ad->discovered_peers[ad->discovered_peer_count].ssid, peer->ssid, 32);
	}
	else
	{
		WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's ssid is NULL\n");
	}

	if (NULL != peer->mac_address)
	{
		WFD_APP_LOG(WFD_APP_LOG_LOW, "discovered peer mac[%s]\n", peer->mac_address);
		strncpy(ad->discovered_peers[ad->discovered_peer_count].mac_address, peer->mac_address, 18);
	}
	else
	{
		WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's mac is NULL\n");
	}
	
	ad->discovered_peer_count++;

	__WFD_APP_FUNC_EXIT__;
	
	return TRUE;

}


void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state,
                  void *user_data)
{
    __WFD_APP_FUNC_ENTER__;

    wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
    int ret;

    switch (discovery_state)
    {
	case WIFI_DIRECT_DISCOVERY_STARTED:
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
		}
		break;

	case WIFI_DIRECT_ONLY_LISTEN_STARTED:
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
		}
		break;

	case WIFI_DIRECT_DISCOVERY_FINISHED:
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
		}
		break;

	case WIFI_DIRECT_DISCOVERY_FOUND:
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");

			if (NULL != ad->discovered_peers)
				free(ad->discovered_peers);

			ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
			ad->discovered_peer_count = 0;

			ret = wifi_direct_foreach_discovered_peers(_wfd_app_discoverd_peer_cb, (void *) ad);
			if (ret != WIFI_DIRECT_ERROR_NONE)
 				WFD_APP_LOG(WFD_APP_LOG_LOW, "get discovery result failed: %d\n", ret);
 		}
		break;

	default:
		break;
    }

    __WFD_APP_FUNC_EXIT__;

}

void _cb_connection(int error_code,
                    wifi_direct_connection_state_e connection_state,
                    const char *mac_address, void *user_data)
{
    __WFD_APP_FUNC_ENTER__;

    wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
    int result;

    switch (connection_state)
    {
    case WIFI_DIRECT_CONNECTION_RSP:
        {
            WFD_APP_LOG(WFD_APP_LOG_LOW,
                        "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");

            if (error_code == WIFI_DIRECT_ERROR_NONE)
            {
                WFD_APP_LOG(WFD_APP_LOG_LOW, "Link Complete!\n");
                wfd_prepare_popup(WFD_POP_NOTI_CONNECTED, NULL);
            }
            else
            {
                if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT)
                    WFD_APP_LOG(WFD_APP_LOG_LOW,
                                "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
                else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED)
                    WFD_APP_LOG(WFD_APP_LOG_LOW,
                                "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
                else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED)
                    WFD_APP_LOG(WFD_APP_LOG_LOW,
                                "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");

                wfd_prepare_popup(WFD_POP_FAIL_CONNECT, NULL);

                result = wifi_direct_start_discovery(FALSE, 0);
                WFD_APP_LOG(WFD_APP_LOG_LOW,
                            "wifi_direct_start_discovery() result=[%d]\n",
                            result);
            }
        }
        break;

    case WIFI_DIRECT_CONNECTION_WPS_REQ:
        {
            wifi_direct_config_data_s *config = NULL;

            memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));

            WFD_APP_LOG(WFD_APP_LOG_LOW,
                        "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
            result = wifi_direct_get_config_data(&config);
            WFD_APP_LOG(WFD_APP_LOG_LOW,
                        "wifi_direct_client_get_config_data() result=[%d]\n",
                        result);

            if (config->wps_config == WIFI_DIRECT_WPS_PUSHBUTTON)
            {
                WFD_APP_LOG(WFD_APP_LOG_LOW,
                            "wps_config is WFD_WPS_PUSHBUTTON. Ignore it..\n");
            }
            else if (config->wps_config == WIFI_DIRECT_WPS_KEYPAD)
            {
                WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_KEYPAD\n");

                result = wifi_direct_generate_wps_pin();
                WFD_APP_LOG(WFD_APP_LOG_LOW,
                            "wifi_direct_client_generate_wps_pin() result=[%d]\n",
                            result);

                char *pin_number = NULL;
                result = wifi_direct_get_wps_pin(&pin_number);
                WFD_APP_LOG(WFD_APP_LOG_LOW,
                            "wifi_direct_client_get_wps_pin() result=[%d]. pin=[%s]\n",
                            result, ad->pin_number);

                strncpy(ad->pin_number, pin_number, 32);

                result = wifi_direct_accept_connection(ad->peer_mac);
                WFD_APP_LOG(WFD_APP_LOG_LOW,
                            "wifi_direct_accept_connection[%s] result=[%d].\n",
                            ad->peer_mac, result);

                result = wifi_direct_activate_pushbutton();
                wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);

                if (pin_number != NULL)
                    free(pin_number);
            }
            else if (config->wps_config == WIFI_DIRECT_WPS_DISPLAY)
            {
                WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_DISPLAY\n");
                wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD,
                                  (void *) NULL);
            }
            else
            {
                WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");

            }
            if (config != NULL)
                free(config);
        }
        break;

    case WIFI_DIRECT_CONNECTION_REQ:
        {
		WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");

		wifi_direct_config_data_s *config = NULL;
		wfd_device_info_t *peer_info = NULL;

		if (NULL == mac_address)
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "ERROR : incomming_peer_mac is NULL !!\n");
			return;
		}

		WFD_APP_LOG(WFD_APP_LOG_LOW, "Connection Request from MAC[%s]\n", mac_address);
		strncpy(ad->peer_mac, mac_address, strlen(mac_address));

		peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);

		if (NULL != peer_info->ssid)
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "Connection Request from SSID[%s]\n", peer_info->ssid);
			strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
		}
		else
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "incomming_peer SSID is NULL !!\n");
		}

		if (ad->peer_name == NULL || strlen(ad->peer_name) == 0)
			strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));

		result = wifi_direct_get_config_data(&config);
		WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_client_get_config_data() result=[%d]\n", result);

		if (config->wps_config == WIFI_DIRECT_WPS_PUSHBUTTON)
		{
			char pushbutton;
			WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_PUSHBUTTON\n");

			wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
		}
		else if (config->wps_config == WIFI_DIRECT_WPS_DISPLAY)
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_DISPLAY\n");

			result = wifi_direct_generate_wps_pin();
			WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_client_generate_wps_pin() result=[%d]\n", result);

			char *pin_number = NULL;
			result = wifi_direct_get_wps_pin(&pin_number);
			WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_get_wps_pin() result=[%d]\n", result);

			strncpy(ad->pin_number, pin_number, 32);

			wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
			
			if (pin_number != NULL)
				free(pin_number);
		}
		else if (config->wps_config == WIFI_DIRECT_WPS_KEYPAD)
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_KEYPAD\n");
			wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
		}
		else
		{
			WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
		}
		
		if (config != NULL)
			free(config);
        }
        break;

    case WIFI_DIRECT_DISCONNECTION_IND:
        {
            WFD_APP_LOG(WFD_APP_LOG_LOW,
                        "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
        }
        break;

    case WIFI_DIRECT_DISCONNECTION_RSP:
        {
            wfd_destroy_popup();

            result = wifi_direct_start_discovery(FALSE, 0);
            WFD_APP_LOG(WFD_APP_LOG_LOW,
                        "wifi_direct_start_discovery() result=[%d]\n", result);
        }
        break;

    default:
        break;

    }

    __WFD_APP_FUNC_EXIT__;
}


int init_wfd_popup_client(wfd_appdata_t * ad)
{
    __WFD_APP_FUNC_ENTER__;
    int ret;

    ret = wifi_direct_initialize();

    ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *) ad);
    ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *) ad);
    ret =
        wifi_direct_set_connection_state_changed_cb(_cb_connection,
                                                    (void *) ad);

    __WFD_APP_FUNC_EXIT__;

    if (ret)
        return TRUE;
    else
        return FALSE;
}

int deinit_wfd_popup_client(void)
{
    __WFD_APP_FUNC_ENTER__;

    int ret;

    ret = wifi_direct_deinitialize();

    __WFD_APP_FUNC_EXIT__;

    if (ret)
        return TRUE;
    else
        return FALSE;
}