ssdp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2012-2013 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. #include <sys/thread.h>
  35. #include <pro/ssdp.h>
  36. #include <string.h>
  37. /* Receiver thread stack size. */
  38. #ifndef SSDP_RECEIVER_STACK
  39. #ifndef NUT_THREAD_SSDPSTACK
  40. #define NUT_THREAD_SSDPSTACK 384
  41. #endif
  42. #define SSDP_RECEIVER_STACK (NUT_THREAD_SSDPSTACK * NUT_THREAD_STACK_MULT + NUT_THREAD_STACK_ADD)
  43. #endif
  44. const char ct_uuid_[] = "uuid:";
  45. const char ct_upnp_rootdevice[] = "upnp:rootdevice";
  46. const char ct_239_255_255_250[] = "239.255.255.250";
  47. static HANDLE rx_thread;
  48. static SSDP_LISTENER_FUNCTION notification_listener;
  49. static SSDP_RESPONDER_FUNCTION search_listener;
  50. static THREAD(SsdpReceiver, arg)
  51. {
  52. HTTPU_SESSION *s;
  53. int timer = 0;
  54. s = HttpuSessionCreate(1900);
  55. for (;;) {
  56. if (HttpuReceive(s, 1000) > 0) {
  57. const char *req = HttpuGetHeader(&s->s_rcvhdr, NULL);
  58. if (strncasecmp(req, "M-SEARCH", 8) == 0) {
  59. if (search_listener) {
  60. (*search_listener)(s);
  61. }
  62. }
  63. else if (strncasecmp(req, "NOTIFY", 6) == 0) {
  64. if (notification_listener) {
  65. (*notification_listener)(&s->s_rcvhdr);
  66. }
  67. }
  68. }
  69. else if (timer-- <= 0) {
  70. timer = SsdpSendNotifications(s, "ssdp:alive");
  71. }
  72. }
  73. }
  74. int SsdpRegisterListener(SSDP_LISTENER_FUNCTION callback)
  75. {
  76. notification_listener = callback;
  77. if (rx_thread == NULL) {
  78. rx_thread = NutThreadCreate("ssdpd", SsdpReceiver, NULL, SSDP_RECEIVER_STACK);
  79. }
  80. return rx_thread ? 0 : -1;
  81. }
  82. int SsdpRegisterResponder(SSDP_RESPONDER_FUNCTION callback)
  83. {
  84. search_listener = callback;
  85. if (rx_thread == NULL) {
  86. rx_thread = NutThreadCreate("ssdpd", SsdpReceiver, NULL, SSDP_RECEIVER_STACK);
  87. }
  88. return rx_thread ? 0 : -1;
  89. }
  90. void SsdpUuidSetMac(char *uuid, const uint8_t *mac)
  91. {
  92. static const char hexdigit[] = "0123456789abcdef";
  93. uint_fast8_t i;
  94. uuid = strrchr(uuid, '-');
  95. if (uuid) {
  96. for (i = 0; i < 6; i++) {
  97. *++uuid = hexdigit[(mac[i] >> 4) & 15];
  98. *++uuid = hexdigit[mac[i] & 15];
  99. }
  100. }
  101. }
  102. void SsdpSplitWords(char *str, char delim, char **words, int n)
  103. {
  104. int cnt;
  105. char *cp;
  106. for (cnt = 0; cnt < n; cnt++) {
  107. words[cnt] = str;
  108. if (*str) {
  109. cp = strchr(str, delim);
  110. if (cp == NULL) {
  111. while (*++str);
  112. } else {
  113. str = cp;
  114. *str++ = '\0';
  115. }
  116. }
  117. }
  118. }