wps_nfc.c
Go to the documentation of this file.
00001 /*
00002  * NFC routines for Wi-Fi Protected Setup
00003  * Copyright (c) 2009, Masashi Honma <honma@ictec.co.jp>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 #include "common.h"
00017 
00018 #include "wps/wps.h"
00019 #include "wps_i.h"
00020 
00021 
00022 struct wps_nfc_data {
00023         struct oob_nfc_device_data *oob_nfc_dev;
00024 };
00025 
00026 
00027 static void * init_nfc(struct wps_context *wps,
00028                        struct oob_device_data *oob_dev, int registrar)
00029 {
00030         struct oob_nfc_device_data *oob_nfc_dev;
00031         struct wps_nfc_data *data;
00032 
00033         oob_nfc_dev = wps_get_oob_nfc_device(oob_dev->device_name);
00034         if (oob_nfc_dev == NULL) {
00035                 wpa_printf(MSG_ERROR, "WPS (NFC): Unknown NFC device (%s)",
00036                            oob_dev->device_name);
00037                 return NULL;
00038         }
00039 
00040         if (oob_nfc_dev->init_func(oob_dev->device_path) < 0)
00041                 return NULL;
00042 
00043         data = os_zalloc(sizeof(*data));
00044         if (data == NULL) {
00045                 wpa_printf(MSG_ERROR, "WPS (NFC): Failed to allocate "
00046                            "nfc data area");
00047                 return NULL;
00048         }
00049         data->oob_nfc_dev = oob_nfc_dev;
00050         return data;
00051 }
00052 
00053 
00054 static struct wpabuf * read_nfc(void *priv)
00055 {
00056         struct wps_nfc_data *data = priv;
00057         struct wpabuf *wifi, *buf;
00058         char *raw_data;
00059         size_t len;
00060 
00061         raw_data = data->oob_nfc_dev->read_func(&len);
00062         if (raw_data == NULL)
00063                 return NULL;
00064 
00065         wifi = wpabuf_alloc_copy(raw_data, len);
00066         os_free(raw_data);
00067         if (wifi == NULL) {
00068                 wpa_printf(MSG_ERROR, "WPS (NFC): Failed to allocate "
00069                            "nfc read area");
00070                 return NULL;
00071         }
00072 
00073         buf = ndef_parse_wifi(wifi);
00074         wpabuf_free(wifi);
00075         if (buf == NULL)
00076                 wpa_printf(MSG_ERROR, "WPS (NFC): Failed to unwrap");
00077         return buf;
00078 }
00079 
00080 
00081 static int write_nfc(void *priv, struct wpabuf *buf)
00082 {
00083         struct wps_nfc_data *data = priv;
00084         struct wpabuf *wifi;
00085         int ret;
00086 
00087         wifi = ndef_build_wifi(buf);
00088         if (wifi == NULL) {
00089                 wpa_printf(MSG_ERROR, "WPS (NFC): Failed to wrap");
00090                 return -1;
00091         }
00092 
00093         ret = data->oob_nfc_dev->write_func(wpabuf_mhead(wifi),
00094                                             wpabuf_len(wifi));
00095         wpabuf_free(wifi);
00096         return ret;
00097 }
00098 
00099 
00100 static void deinit_nfc(void *priv)
00101 {
00102         struct wps_nfc_data *data = priv;
00103 
00104         data->oob_nfc_dev->deinit_func();
00105 
00106         os_free(data);
00107 }
00108 
00109 
00110 struct oob_device_data oob_nfc_device_data = {
00111         .device_name    = NULL,
00112         .device_path    = NULL,
00113         .init_func      = init_nfc,
00114         .read_func      = read_nfc,
00115         .write_func     = write_nfc,
00116         .deinit_func    = deinit_nfc,
00117 };


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:36