confnet.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file net/confnet.c
  36. * \brief Persistent storage of network configuration.
  37. *
  38. * \verbatim
  39. * $Id: confnet.c 4473 2012-08-20 15:12:45Z haraldkipp $
  40. * \endverbatim
  41. */
  42. #include <string.h>
  43. #include <arpa/inet.h>
  44. #include <netinet/if_ether.h>
  45. #include <sys/confnet.h>
  46. #include <dev/nvmem.h>
  47. #ifdef CONFNET_VIRGIN_MAC
  48. #define VIRGIN_MAC ether_aton(CONFNET_VIRGIN_MAC)
  49. #else
  50. static uint8_t virgin_mac[6] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
  51. #define VIRGIN_MAC virgin_mac
  52. #endif
  53. #ifdef CONFNET_VIRGIN_NETMASK
  54. #define VIRGIN_NETMASK inet_addr(CONFNET_VIRGIN_NETMASK)
  55. #else
  56. #define VIRGIN_NETMASK 0x00FFFFFFUL
  57. #endif
  58. /*!
  59. * \addtogroup xgConfNet
  60. */
  61. /*@{*/
  62. /*!
  63. * \brief Global network configuration structure.
  64. *
  65. * Contains the current network configuration. Nut/Net will load
  66. * this structure from non-volatile memory during initialization.
  67. */
  68. CONFNET confnet;
  69. /*!
  70. * \brief Load network configuration from non-volatile memory.
  71. *
  72. * If no configuration is available in EEPROM, all configuration
  73. * parameters are cleared to zero. Except the MAC address, which
  74. * is set to the locally administered address 02-00-00-00-00-00.
  75. *
  76. * \param name Name of the device.
  77. *
  78. * \return 0 if configuration has been read. Otherwise the
  79. * return value is -1.
  80. */
  81. int NutNetLoadConfig(const char *name)
  82. {
  83. #ifndef CONFNET_HARDCODED
  84. /* Read non-volatile memory. */
  85. if (NutNvMemLoad(CONFNET_EE_OFFSET, &confnet, sizeof(CONFNET)) == 0) {
  86. /* Sanity check. */
  87. if (confnet.cd_size == sizeof(CONFNET) && strcmp(confnet.cd_name, name) == 0) {
  88. /* Got a (hopefully) valid configuration. */
  89. return 0;
  90. }
  91. }
  92. #endif
  93. memset(&confnet, 0, sizeof(CONFNET));
  94. memcpy(confnet.cdn_mac, VIRGIN_MAC, sizeof(confnet.cdn_mac));
  95. /* Set local IP and the gate's IP, if configured. */
  96. #ifdef CONFNET_VIRGIN_IP
  97. confnet.cdn_cip_addr = inet_addr(CONFNET_VIRGIN_IP);
  98. #endif
  99. #ifdef CONFNET_VIRGIN_GATE
  100. confnet.cdn_gateway = inet_addr(CONFNET_VIRGIN_GATE);
  101. #endif
  102. #ifdef CONFNET_HARDCODED
  103. /* If hard coded, set the default mask and return success. */
  104. confnet.cdn_ip_mask = VIRGIN_NETMASK;
  105. return 0;
  106. #else
  107. /* Reading from non-volatile memory failed. */
  108. return -1;
  109. #endif
  110. }
  111. /*!
  112. * \brief Save network configuration in non-volatile memory.
  113. *
  114. * \return 0 if OK, -1 on failures.
  115. */
  116. int NutNetSaveConfig(void)
  117. {
  118. #if !defined (__NUT_EMULATION__) && !defined (CONFNET_HARDCODED)
  119. confnet.cd_size = sizeof(CONFNET);
  120. if (NutNvMemSave(CONFNET_EE_OFFSET, &confnet, sizeof(CONFNET))) {
  121. return -1;
  122. }
  123. #endif
  124. return 0;
  125. }
  126. /*@}*/