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