editconf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2011 by egnite 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. /*!
  36. * $Id$
  37. */
  38. /*!
  39. * \example editconf/editconf.c
  40. *
  41. * Network configuration editor.
  42. *
  43. * This application allows to view and modify the network configuration
  44. * that is stored on non-volatile memory. It requires a terminal emulator
  45. * running on the PC and an RS-232 connection between the PC and the
  46. * target board.
  47. *
  48. * Note, that the main UART is used here, not the DEBUG device. On some
  49. * boards this may require a different jumper configuration.
  50. */
  51. #include <dev/board.h>
  52. #include <sys/confnet.h>
  53. #include <string.h>
  54. #include <stdint.h>
  55. #include <stdio.h>
  56. #include <io.h>
  57. #include <arpa/inet.h>
  58. #include <netinet/if_ether.h>
  59. #include "meminfo.h"
  60. /* Reads line from standard input. */
  61. static int EditLine(char *prompt, char *line, int siz)
  62. {
  63. int ch;
  64. int pos = strlen(line);
  65. printf("%s: %s", prompt, line);
  66. for (;;) {
  67. ch = getchar();
  68. if (ch == 8) {
  69. if (pos) {
  70. pos--;
  71. printf("\b \b");
  72. }
  73. }
  74. else if (ch < ' ') {
  75. break;
  76. }
  77. else if (pos + 1 < siz) {
  78. putchar(ch);
  79. line[pos++] = ch;
  80. }
  81. else {
  82. putchar('\a');
  83. }
  84. }
  85. line[pos] = 0;
  86. putchar('\n');
  87. return 0;
  88. }
  89. /* Editor main routine. */
  90. int main(void)
  91. {
  92. uint32_t baud = 115200;
  93. char buf[32];
  94. uint8_t *cp;
  95. uint32_t addr;
  96. char ch;
  97. /* Assign stdin and stdout to the default UART device. */
  98. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  99. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  100. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  101. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  102. puts("\n\nNetwork Configuration Editor - Compiled " __DATE__ " - " __TIME__);
  103. ShowHardwareConfiguration();
  104. for (;;) {
  105. /* Load configuration. */
  106. if (NutNetLoadConfig(DEV_ETHER_NAME)) {
  107. puts("\nNo configuration available");
  108. strcpy(confnet.cd_name, DEV_ETHER_NAME);
  109. } else {
  110. puts("\nConfiguration loaded");
  111. }
  112. /* Edit MAC address. */
  113. do {
  114. strcpy(buf, ether_ntoa(confnet.cdn_mac));
  115. EditLine("MAC Address", buf, 18);
  116. cp = ether_aton(buf);
  117. } while (cp == NULL);
  118. memcpy(confnet.cdn_mac, cp, 6);
  119. /* Edit IP address. */
  120. do {
  121. strcpy(buf, inet_ntoa(confnet.cdn_cip_addr));
  122. EditLine("IP Address", buf, 16);
  123. addr = inet_addr(buf);
  124. } while (addr == -1);
  125. confnet.cdn_cip_addr = addr;
  126. /* Edit IP mask. */
  127. do {
  128. strcpy(buf, inet_ntoa(confnet.cdn_ip_mask));
  129. EditLine("IP Mask", buf, 16);
  130. addr = inet_addr(buf);
  131. } while (addr == -1);
  132. confnet.cdn_ip_mask = addr;
  133. /* Edit IP gate. */
  134. do {
  135. strcpy(buf, inet_ntoa(confnet.cdn_gateway));
  136. EditLine("IP Gate", buf, 16);
  137. addr = inet_addr(buf);
  138. } while (addr == -1);
  139. confnet.cdn_gateway = addr;
  140. /* Prompt for saving. */
  141. printf("\nPress S to save this configuration ");
  142. /* Flush input buffer and read next character. */
  143. while (kbhit()) {
  144. ch = getchar();
  145. }
  146. ch = getchar();
  147. /* Save or discard edited configuration. */
  148. if (ch == 's' || ch == 'S') {
  149. if (NutNetSaveConfig()) {
  150. puts("Failed");
  151. } else {
  152. puts("Saved");
  153. }
  154. } else {
  155. puts("Discarded");
  156. }
  157. }
  158. return 0;
  159. }