dialog.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software GmbH
  3. * Copyright (C) 2010 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. */
  36. /*
  37. * $Id: dialog.c 2953 2010-04-03 10:10:20Z haraldkipp $
  38. */
  39. #include "utils.h"
  40. #include "uart.h"
  41. #include "dialog.h"
  42. static char buf[9];
  43. static char hexdigit[] = "0123456789ABCDEF";
  44. static char inbuff[60];
  45. int PutString(char *cp)
  46. {
  47. int rc = 0;
  48. for (; *cp; cp++) {
  49. if (*cp == '\n') {
  50. UartTx('\r');
  51. rc++;
  52. }
  53. UartTx(*cp);
  54. rc++;
  55. }
  56. return rc;
  57. }
  58. int GetLine(char *line, int size)
  59. {
  60. int cnt;
  61. unsigned char ch;
  62. cnt = PutString(line);
  63. line += cnt;
  64. for (;;) {
  65. if (cnt > size - 1)
  66. break;
  67. while ((ch = UartRx()) == 0 || ch == 32);
  68. if (ch == 8) {
  69. if (cnt) {
  70. UartTx(ch);
  71. UartTx(' ');
  72. UartTx(ch);
  73. cnt--;
  74. line--;
  75. }
  76. }
  77. else if (ch < 32) {
  78. break;
  79. }
  80. else {
  81. UartTx(ch);
  82. *line++ = ch;
  83. cnt++;
  84. }
  85. }
  86. *line = 0;
  87. PutString("\n");
  88. return cnt;
  89. }
  90. /*
  91. * Get a line of input.
  92. */
  93. void GetIP(char *prompt, char *value)
  94. {
  95. for (;;) {
  96. PutString(prompt);
  97. PutString(": ");
  98. strcpy_(inbuff, value);
  99. if (GetLine(inbuff, sizeof(inbuff)) == 0)
  100. break;
  101. if (inet_addr(inbuff) != (unsigned long) (-1L)) {
  102. strcpy_(value, inbuff);
  103. break;
  104. }
  105. PutString("Bad IP address ");
  106. PutString(inbuff);
  107. PutString("\n");
  108. }
  109. }
  110. void PutHex(unsigned char val)
  111. {
  112. register unsigned char i;
  113. for (i = 2; i-- > 0;) {
  114. buf[i] = hexdigit[val & 0x0F];
  115. val >>= 4;
  116. }
  117. buf[2] = 0;
  118. PutString(buf);
  119. }
  120. void PutShortHex(unsigned short val)
  121. {
  122. register unsigned char i;
  123. for (i = 4; i-- > 0;) {
  124. buf[i] = hexdigit[val & 0x0F];
  125. val >>= 4;
  126. }
  127. buf[4] = 0;
  128. PutString(buf);
  129. }
  130. void PutLongHex(unsigned long val)
  131. {
  132. register unsigned char i;
  133. for (i = 8; i-- > 0;) {
  134. buf[i] = hexdigit[val & 0x0F];
  135. val >>= 4;
  136. }
  137. buf[8] = 0;
  138. PutString(buf);
  139. }
  140. void GetMac(unsigned char *mac)
  141. {
  142. int i;
  143. int n;
  144. for (;;) {
  145. PutString("\nMAC address: ");
  146. for (i = 0; i < 6; i++) {
  147. inbuff[i * 2] = hexdigit[mac[i] >> 4];
  148. inbuff[i * 2 + 1] = hexdigit[mac[i] & 0x0F];
  149. }
  150. inbuff[i * 2] = 0;
  151. n = GetLine(inbuff, sizeof(inbuff));
  152. if (n == 0) {
  153. break;
  154. }
  155. for (i = 0; i < n; i++) {
  156. if (inbuff[i] < '0' || (inbuff[i] > '9' && inbuff[i] < 'A') || (inbuff[i] > 'F' && inbuff[i] < 'a') || inbuff[i] > 'f') {
  157. n = 13;
  158. }
  159. }
  160. if (n <= 12 && (n & 1) == 0) {
  161. n >>= 1;
  162. for (i = 0; i < n; i++) {
  163. mac[6 - n + i] = hex2bin(inbuff[i * 2]) * 16 + hex2bin(inbuff[i * 2 + 1]);
  164. }
  165. break;
  166. }
  167. PutString("Bad MAC address");
  168. }
  169. }