wps_nfc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * NFC 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. #include "includes.h"
  15. #include "common.h"
  16. #include "wps/wps.h"
  17. #include "wps_i.h"
  18. struct wps_nfc_data {
  19. struct oob_nfc_device_data *oob_nfc_dev;
  20. };
  21. static void * init_nfc(struct wps_context *wps,
  22. struct oob_device_data *oob_dev, int registrar)
  23. {
  24. struct oob_nfc_device_data *oob_nfc_dev;
  25. struct wps_nfc_data *data;
  26. oob_nfc_dev = wps_get_oob_nfc_device(oob_dev->device_name);
  27. if (oob_nfc_dev == NULL) {
  28. wpa_printf(MSG_ERROR, "WPS (NFC): Unknown NFC device (%s)",
  29. oob_dev->device_name);
  30. return NULL;
  31. }
  32. if (oob_nfc_dev->init_func(oob_dev->device_path) < 0)
  33. return NULL;
  34. data = os_zalloc(sizeof(*data));
  35. if (data == NULL) {
  36. wpa_printf(MSG_ERROR, "WPS (NFC): Failed to allocate "
  37. "nfc data area");
  38. return NULL;
  39. }
  40. data->oob_nfc_dev = oob_nfc_dev;
  41. return data;
  42. }
  43. static struct wpabuf * read_nfc(void *priv)
  44. {
  45. struct wps_nfc_data *data = priv;
  46. struct wpabuf *wifi, *buf;
  47. char *raw_data;
  48. size_t len;
  49. raw_data = data->oob_nfc_dev->read_func(&len);
  50. if (raw_data == NULL)
  51. return NULL;
  52. wifi = wpabuf_alloc_copy(raw_data, len);
  53. os_free(raw_data);
  54. if (wifi == NULL) {
  55. wpa_printf(MSG_ERROR, "WPS (NFC): Failed to allocate "
  56. "nfc read area");
  57. return NULL;
  58. }
  59. buf = ndef_parse_wifi(wifi);
  60. wpabuf_free(wifi);
  61. if (buf == NULL)
  62. wpa_printf(MSG_ERROR, "WPS (NFC): Failed to unwrap");
  63. return buf;
  64. }
  65. static int write_nfc(void *priv, struct wpabuf *buf)
  66. {
  67. struct wps_nfc_data *data = priv;
  68. struct wpabuf *wifi;
  69. int ret;
  70. wifi = ndef_build_wifi(buf);
  71. if (wifi == NULL) {
  72. wpa_printf(MSG_ERROR, "WPS (NFC): Failed to wrap");
  73. return -1;
  74. }
  75. ret = data->oob_nfc_dev->write_func(wpabuf_mhead(wifi),
  76. wpabuf_len(wifi));
  77. wpabuf_free(wifi);
  78. return ret;
  79. }
  80. static void deinit_nfc(void *priv)
  81. {
  82. struct wps_nfc_data *data = priv;
  83. data->oob_nfc_dev->deinit_func();
  84. os_free(data);
  85. }
  86. struct oob_device_data oob_nfc_device_data = {
  87. .device_name = NULL,
  88. .device_path = NULL,
  89. .init_func = init_nfc,
  90. .read_func = read_nfc,
  91. .write_func = write_nfc,
  92. .deinit_func = deinit_nfc,
  93. };