LocalDevices.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * unsigned LocalDevices (struct channel * channel, struct message * message, void * memory, size_t extent);
  44. *
  45. * plc.h
  46. *
  47. * populate a memory region with consecutive Ethernet addresses;
  48. * the addresses belong to Atheros powerline devices connected to
  49. * the host interface specified in struct channel; each bridge may
  50. * be connected to one or more HomePlug AV devices via powerline;
  51. *
  52. * each powerline bridge normally belongs to a different powerline
  53. * network but is is possible for multiple bridges to belong to the
  54. * same powerline network thus leading to confusing configurations;
  55. *
  56. * use function NetworkDevices() to discover all devices on each
  57. * powerline network;
  58. *
  59. * although this function accepts a channel structure, it ignores
  60. * the channel peer address and sends a VS_SW_VER request message
  61. * to the Local Management Address; this causes all local devices
  62. * to respond; the list is a collection of source addresses taken
  63. * from all responses;
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier
  68. * Matthieu Poullet
  69. *
  70. *--------------------------------------------------------------------*/
  71. #ifndef NETWORKBRIDGES_SOURCE
  72. #define NETWORKBRIDGES_SOURCE
  73. #include <memory.h>
  74. #include <errno.h>
  75. #include "../plc/plc.h"
  76. #include "../ether/channel.h"
  77. #include "../tools/types.h"
  78. #include "../tools/error.h"
  79. unsigned LocalDevices (struct channel const * channel, struct message * message, void * memory, size_t extent)
  80. {
  81. extern const byte localcast [ETHER_ADDR_LEN];
  82. struct vs_sw_ver_request
  83. {
  84. struct ethernet_hdr ethernet;
  85. struct qualcomm_hdr qualcomm;
  86. }
  87. * request = (struct vs_sw_ver_request *)(message);
  88. uint8_t * origin = (uint8_t *)(memory);
  89. uint8_t * offset = (uint8_t *)(memory);
  90. ssize_t packetsize;
  91. memset (memory, 0, extent);
  92. memset (message, 0, sizeof (* message));
  93. EthernetHeader (&request->ethernet, localcast, channel->host, channel->type);
  94. QualcommHeader (&request->qualcomm, 0, (VS_SW_VER | MMTYPE_REQ));
  95. if (sendpacket (channel, message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) <= 0)
  96. {
  97. return (0);
  98. }
  99. while ((packetsize = readpacket (channel, message, sizeof (* message))) > 0)
  100. {
  101. if (UnwantedMessage (message, packetsize, 0, (VS_SW_VER | MMTYPE_CNF)))
  102. {
  103. continue;
  104. }
  105. if (extent >= sizeof (message->ethernet.OSA))
  106. {
  107. memcpy (offset, message->ethernet.OSA, sizeof (message->ethernet.OSA));
  108. offset += sizeof (message->ethernet.OSA);
  109. extent -= sizeof (message->ethernet.OSA);
  110. }
  111. }
  112. return ((unsigned)(offset - origin) / ETHER_ADDR_LEN);
  113. }
  114. #endif