1/*
2 * NFC PN531 routines for Wi-Fi Protected Setup
3 * Copyright (c) 2009-2012, Masashi Honma <masashi.honma@gmail.com>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include "common.h"
11
12#include "wps/wps.h"
13#include "wps_i.h"
14
15#include "WpsNfcType.h"
16#include "WpsNfc.h"
17
18
19static int init_nfc_pn531(char *path)
20{
21	u32 ret;
22
23	ret = WpsNfcInit();
24	if (ret != WPS_NFCLIB_ERR_SUCCESS) {
25		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to initialize "
26			   "NFC Library: 0x%08x", ret);
27		return -1;
28	}
29
30	ret = WpsNfcOpenDevice((int8 *) path);
31	if (ret != WPS_NFCLIB_ERR_SUCCESS) {
32		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to open "
33			   "NFC Device(%s): 0x%08x", path, ret);
34		goto fail;
35	}
36
37	ret = WpsNfcTokenDiscovery();
38	if (ret != WPS_NFCLIB_ERR_SUCCESS) {
39		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to discover "
40			   "token: 0x%08x", ret);
41		WpsNfcCloseDevice();
42		goto fail;
43	}
44
45	return 0;
46
47fail:
48	WpsNfcDeinit();
49	return -1;
50}
51
52
53static void * read_nfc_pn531(size_t *size)
54{
55	uint32 len;
56	u32 ret;
57	int8 *data;
58
59	ret = WpsNfcRawReadToken(&data, &len);
60	if (ret != WPS_NFCLIB_ERR_SUCCESS) {
61		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to read: 0x%08x",
62			   ret);
63		return NULL;
64	}
65
66	*size = len;
67	return data;
68}
69
70
71static int write_nfc_pn531(void *data, size_t len)
72{
73	u32 ret;
74
75	ret = WpsNfcRawWriteToken(data, len);
76	if (ret != WPS_NFCLIB_ERR_SUCCESS) {
77		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to write: 0x%08x",
78			   ret);
79		return -1;
80	}
81
82	return 0;
83}
84
85
86static void deinit_nfc_pn531(void)
87{
88	u32 ret;
89
90	ret = WpsNfcCloseDevice();
91	if (ret != WPS_NFCLIB_ERR_SUCCESS)
92		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to close "
93			   "NFC Device: 0x%08x", ret);
94
95	ret = WpsNfcDeinit();
96	if (ret != WPS_NFCLIB_ERR_SUCCESS)
97		wpa_printf(MSG_ERROR, "WPS (PN531): Failed to deinitialize "
98			   "NFC Library: 0x%08x", ret);
99}
100
101
102struct oob_nfc_device_data oob_nfc_pn531_device_data = {
103	.init_func	= init_nfc_pn531,
104	.read_func	= read_nfc_pn531,
105	.write_func	= write_nfc_pn531,
106	.deinit_func	= deinit_nfc_pn531,
107};
108