locator.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * locator.c
  3. *
  4. * Device Locator server using UDP
  5. *
  6. */
  7. /*
  8. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  25. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  27. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  30. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. * OF SUCH DAMAGE.
  32. *
  33. * This file is part of the lwIP TCP/IP stack.
  34. *
  35. * Author: Adam Dunkels <adam@sics.se>
  36. *
  37. */
  38. /* Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  39. * ALL RIGHTS RESERVED
  40. */
  41. #include "locator.h"
  42. #include "lwiplib.h"
  43. /******************************************************************************
  44. ** INTERNAL MACRO DEFINITIONS
  45. *******************************************************************************/
  46. #define TAG_CMD (0xff)
  47. #define TAG_STATUS (0xfe)
  48. #define CMD_DISCOVER_TARGET (0x02)
  49. /******************************************************************************
  50. ** INTERNAL VARIABLE DEFINITIONS
  51. *******************************************************************************/
  52. /*
  53. ** An array that contains the device locator response data. The format of the
  54. ** data is as follows:
  55. **
  56. ** Byte Description
  57. ** -------- ------------------------
  58. ** 0 TAG_STATUS
  59. ** 1 packet length
  60. ** 2 CMD_DISCOVER_TARGET
  61. ** 3 board type
  62. ** 4 board ID
  63. ** 5..8 client IP address
  64. ** 9..14 MAC address
  65. ** 15..18 firmware version
  66. ** 19..82 application title
  67. ** 83 checksum
  68. */
  69. static unsigned char locatorData[84];
  70. /******************************************************************************
  71. ** INTERNAL FUNCTION PROTOTYPES
  72. *******************************************************************************/
  73. static void LocatorReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  74. struct ip_addr *addr, u16_t port);
  75. /******************************************************************************
  76. ** FUNCTION DEFINITIONS
  77. *******************************************************************************/
  78. /*
  79. ** This function is called by the lwIP TCP/IP stack when it receives a UDP
  80. ** packet from the discovery port. It produces the response packet, which is
  81. ** sent back to the querying client.
  82. */
  83. static void LocatorReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  84. struct ip_addr *addr, u16_t port)
  85. {
  86. unsigned char *pucData;
  87. unsigned long ulIdx;
  88. /* Validate the contents of the datagram.*/
  89. pucData = p->payload;
  90. if((p->len != 4) || (pucData[0] != TAG_CMD) || (pucData[1] != 4) ||
  91. (pucData[2] != CMD_DISCOVER_TARGET) ||
  92. (pucData[3] != ((0 - TAG_CMD - 4 - CMD_DISCOVER_TARGET) & 0xff)))
  93. {
  94. pbuf_free(p);
  95. return;
  96. }
  97. pbuf_free(p);
  98. p = pbuf_alloc(PBUF_TRANSPORT, sizeof(locatorData), PBUF_RAM);
  99. if(p == NULL)
  100. {
  101. return;
  102. }
  103. /* Calcuate and fill in the checksum on the response packet.*/
  104. for(ulIdx = 0, locatorData[sizeof(locatorData) - 1] = 0;
  105. ulIdx < (sizeof(locatorData) - 1); ulIdx++)
  106. {
  107. locatorData[sizeof(locatorData) - 1] -=
  108. locatorData[ulIdx];
  109. }
  110. /* Copy the response packet data into the pbuf. */
  111. pucData = p->payload;
  112. for(ulIdx = 0; ulIdx < sizeof(locatorData); ulIdx++)
  113. {
  114. pucData[ulIdx] = locatorData[ulIdx];
  115. }
  116. /* Send the response.*/
  117. udp_sendto(pcb, p, addr, port);
  118. pbuf_free(p);
  119. }
  120. /*
  121. ** Initializes the locator service. Prepares the locator service to
  122. ** handle device discovery requests. .
  123. */
  124. void LocatorConfig(unsigned char *macArray, const char *appTitle)
  125. {
  126. unsigned int idx;
  127. void *pcb;
  128. /* Clear out the response data.*/
  129. for(idx = 0; idx < 84; idx++)
  130. {
  131. locatorData[idx] = 0;
  132. }
  133. /* Fill in the header for the response data.*/
  134. locatorData[0] = TAG_STATUS;
  135. locatorData[1] = sizeof(locatorData);
  136. locatorData[2] = CMD_DISCOVER_TARGET;
  137. /* Fill in the MAC address for the response data. */
  138. locatorData[9] = macArray[0];
  139. locatorData[10] = macArray[1];
  140. locatorData[11] = macArray[2];
  141. locatorData[12] = macArray[3];
  142. locatorData[13] = macArray[4];
  143. locatorData[14] = macArray[5];
  144. /* Create a new UDP port for listening to device locator requests.*/
  145. pcb = udp_new();
  146. udp_recv(pcb, LocatorReceive, NULL);
  147. udp_bind(pcb, IP_ADDR_ANY, 23);
  148. udp_connect(pcb, IP_ADDR_ANY, 23);
  149. /* Copy the application title string into the response data. */
  150. for(idx = 0; (idx < 64) && *appTitle; idx++)
  151. {
  152. locatorData[idx + 19] = *appTitle++;
  153. }
  154. /* Zero-fill the remainder of the space in the response data (if any).*/
  155. for(; idx < 64; idx++)
  156. {
  157. locatorData[idx + 19] = 0;
  158. }
  159. }
  160. /***************************** End Of File ***********************************/