PLCTopology.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. * PLCTopology.c
  44. *
  45. * plc.h
  46. *
  47. *
  48. * Contributor(s):
  49. * Charles Maier
  50. * Matthieu Poullet
  51. *
  52. *--------------------------------------------------------------------*/
  53. #ifndef PLCTOPOLOGY_SOURCE
  54. #define PLCTOPOLOGY_SOURCE
  55. #include <memory.h>
  56. #include <errno.h>
  57. #include "../tools/memory.h"
  58. #include "../tools/symbol.h"
  59. #include "../tools/error.h"
  60. #include "../tools/flags.h"
  61. #include "../plc/plc.h"
  62. /*====================================================================*
  63. *
  64. * signed PLCPlatform (struct channel * channel, struct plcstation * station);
  65. *
  66. * populate plcstation structure with hardware and firmware version
  67. * string using a VS_SW_VER message;
  68. *
  69. * extern char const * chipset [] contains chipset name strings in
  70. * order of chipset code but function chipset() must be called to
  71. * insert the true code into the confirmation message because some
  72. * chipsets return the wrong code; alien technology and voodoo are
  73. * needed;
  74. *
  75. *--------------------------------------------------------------------*/
  76. static signed PLCPlatform (struct channel * channel, struct plcstation * plcstation)
  77. {
  78. struct message message;
  79. ssize_t packetsize;
  80. #ifndef __GNUC__
  81. #pragma pack (push,1)
  82. #endif
  83. struct __packed vs_sw_ver_request
  84. {
  85. struct ethernet_hdr ethernet;
  86. struct qualcomm_hdr qualcomm;
  87. }
  88. * request = (struct vs_sw_ver_request *) (&message);
  89. struct __packed vs_sw_ver_confirm
  90. {
  91. struct ethernet_hdr ethernet;
  92. struct qualcomm_hdr qualcomm;
  93. uint8_t MSTATUS;
  94. uint8_t MDEVICE;
  95. uint8_t MLENGTH;
  96. char MSTRING [0x80];
  97. }
  98. * confirm = (struct vs_sw_ver_confirm *) (&message);
  99. #ifndef __GNUC__
  100. #pragma pack (pop)
  101. #endif
  102. memset (&message, 0, sizeof (message));
  103. EthernetHeader (&request->ethernet, plcstation->MAC, channel->host, channel->type);
  104. QualcommHeader (&request->qualcomm, 0, (VS_SW_VER | MMTYPE_REQ));
  105. if (sendpacket (channel, &message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) > 0)
  106. {
  107. while ((packetsize = readpacket (channel, &message, sizeof (message))) > 0)
  108. {
  109. if (!UnwantedMessage (&message, packetsize, 0, (VS_SW_VER | MMTYPE_CNF)))
  110. {
  111. chipset (confirm);
  112. strncpy (plcstation->hardware, chipsetname (confirm->MDEVICE), sizeof (plcstation->hardware));
  113. strncpy (plcstation->firmware, confirm->MSTRING, sizeof (plcstation->firmware));
  114. return (0);
  115. }
  116. }
  117. }
  118. return (-1);
  119. }
  120. /*====================================================================*
  121. *
  122. * signed PLCIdentity (struct channel * channel, struct plcstation * station);
  123. *
  124. * populate plcstation structure with the device identity using a
  125. * VS_MFG_STRING message;
  126. *
  127. *--------------------------------------------------------------------*/
  128. static signed PLCIdentity (struct channel * channel, struct plcstation * plcstation)
  129. {
  130. struct message message;
  131. ssize_t packetsize;
  132. #ifndef __GNUC__
  133. #pragma pack (push,1)
  134. #endif
  135. struct __packed vs_mfg_string_request
  136. {
  137. struct ethernet_hdr ethernet;
  138. struct qualcomm_hdr qualcomm;
  139. }
  140. * request = (struct vs_mfg_string_request *) (&message);
  141. struct __packed vs_mfg_string_confirm
  142. {
  143. struct ethernet_hdr ethernet;
  144. struct qualcomm_hdr qualcomm;
  145. uint8_t MSTATUS;
  146. uint8_t MLENGTH;
  147. char MSTRING [0x40];
  148. }
  149. * confirm = (struct vs_mfg_string_confirm *) (&message);
  150. #ifndef __GNUC__
  151. #pragma pack (pop)
  152. #endif
  153. memset (&message, 0, sizeof (message));
  154. EthernetHeader (&request->ethernet, plcstation->MAC, channel->host, channel->type);
  155. QualcommHeader (&request->qualcomm, 0, (VS_MFG_STRING | MMTYPE_REQ));
  156. if (sendpacket (channel, &message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) > 0)
  157. {
  158. while ((packetsize = readpacket (channel, &message, sizeof (message))) > 0)
  159. {
  160. if (!UnwantedMessage (&message, packetsize, 0, (VS_MFG_STRING | MMTYPE_CNF)))
  161. {
  162. strncpy (plcstation->identity, confirm->MSTRING, sizeof (plcstation->identity));
  163. return (0);
  164. }
  165. }
  166. }
  167. return (-1);
  168. }
  169. /*====================================================================*
  170. *
  171. * signed PLCTopology (struct channel * channel, struct message * message, struct plctopology * plctopology)
  172. *
  173. * populate a plctopology structure with network information; the
  174. * logic this is elusive due to the way the VS_NW_INFO message is
  175. * structured;
  176. *
  177. * the host can have many interfaces and each interface can have
  178. * many powerline devices connected to it; each powerline device
  179. * can bridge to an independent powerline network having a unique
  180. * set of member devices; alternately, some powerline devices can
  181. * be members of the same powerline network;
  182. *
  183. * INT6x00 chipsets have an 8-bit PHY rate while AR7x00 chipsets
  184. * have a 16-bit PHY rate; this means that AR7x00 PHY rates need
  185. *
  186. *--------------------------------------------------------------------*/
  187. signed PLCTopology (struct channel * channel, struct message * message, struct plctopology * plctopology)
  188. {
  189. ssize_t packetsize;
  190. #if defined(INT6x00)
  191. struct __packed vs_nw_info_request
  192. {
  193. struct ethernet_hdr ethernet;
  194. struct qualcomm_hdr qualcomm;
  195. }
  196. * request = (struct vs_nw_info_request *)(message);
  197. struct __packed vs_nw_info_confirm
  198. {
  199. struct ethernet_hdr ethernet;
  200. struct qualcomm_hdr qualcomm;
  201. uint8_t data [1];
  202. }
  203. * confirm = (struct vs_nw_info_confirm *)(message);
  204. struct __packed station
  205. {
  206. uint8_t MAC [ETHER_ADDR_LEN];
  207. uint8_t TEI;
  208. uint8_t BDA [ETHER_ADDR_LEN];
  209. uint8_t AVGTX;
  210. uint8_t AVGRX;
  211. }
  212. * station;
  213. struct __packed network
  214. {
  215. uint8_t NID [7];
  216. uint8_t SNID;
  217. uint8_t TEI;
  218. uint8_t ROLE;
  219. uint8_t CCO_MAC [ETHER_ADDR_LEN];
  220. uint8_t CCO_TEI;
  221. uint8_t NUMSTAS;
  222. struct station stations [1];
  223. }
  224. * network;
  225. struct __packed networks
  226. {
  227. uint8_t NUMAVLNS;
  228. struct network networks [1];
  229. }
  230. * networks = (struct networks *) (confirm->data);
  231. #elif defined (AR7x00)
  232. struct __packed vs_nw_info_request
  233. {
  234. struct ethernet_hdr ethernet;
  235. struct qualcomm_fmi qualcomm;
  236. }
  237. * request = (struct vs_nw_info_request *)(message);
  238. struct __packed vs_nw_info_confirm
  239. {
  240. struct ethernet_hdr ethernet;
  241. struct qualcomm_fmi qualcomm;
  242. uint8_t SUB_VERSION;
  243. uint8_t Reserved;
  244. uint16_t DATA_LEN;
  245. uint8_t DATA [1];
  246. }
  247. * confirm = (struct vs_nw_info_confirm *)(message);
  248. struct __packed station
  249. {
  250. uint8_t MAC [ETHER_ADDR_LEN];
  251. uint8_t TEI;
  252. uint8_t Reserved [3];
  253. uint8_t BDA [ETHER_ADDR_LEN];
  254. uint16_t AVGTX;
  255. uint8_t COUPLING;
  256. uint8_t Reserved3;
  257. uint16_t AVGRX;
  258. uint16_t Reserved4;
  259. }
  260. * station;
  261. struct __packed network
  262. {
  263. uint8_t NID [7];
  264. uint8_t Reserved1 [2];
  265. uint8_t SNID;
  266. uint8_t TEI;
  267. uint8_t Reserved2 [4];
  268. uint8_t ROLE;
  269. uint8_t CCO_MAC [ETHER_ADDR_LEN];
  270. uint8_t CCO_TEI;
  271. uint8_t Reserved3 [3];
  272. uint8_t NUMSTAS;
  273. uint8_t Reserved4 [5];
  274. struct station stations [1];
  275. }
  276. * network;
  277. struct __packed networks
  278. {
  279. uint8_t Reserved;
  280. uint8_t NUMAVLNS;
  281. struct network networks [1];
  282. }
  283. * networks = (struct networks *) (confirm->DATA);
  284. #else
  285. #error "Unspecified Atheros chipset"
  286. #endif
  287. struct plcnetwork * plcnetwork = (struct plcnetwork *)(&plctopology->plcnetwork);
  288. struct plcstation * plcstation;
  289. byte bridges [255] [ETHER_ADDR_LEN];
  290. unsigned bridge = LocalDevices (channel, message, bridges, (size_t)(sizeof (bridges)));
  291. while (bridge--)
  292. {
  293. memset (message, 0, sizeof (* message));
  294. memcpy (channel->peer, bridges [bridge], sizeof (channel->peer));
  295. #if defined (INT6x00)
  296. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  297. QualcommHeader (&request->qualcomm, 0, (VS_NW_INFO | MMTYPE_REQ));
  298. #elif defined (AR7x00)
  299. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  300. QualcommHeader1 (&request->qualcomm, 1, (VS_NW_INFO | MMTYPE_REQ));
  301. #else
  302. #error "Unspecified Atheros chipset"
  303. #endif
  304. if (sendpacket (channel, message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) <= 0)
  305. {
  306. error (1, errno, CHANNEL_CANTSEND);
  307. }
  308. while ((packetsize = readpacket (channel, message, sizeof (* message))) > 0)
  309. {
  310. #if defined (INT6x00)
  311. if (UnwantedMessage (confirm, packetsize, 0, (VS_NW_INFO | MMTYPE_CNF)))
  312. #elif defined (AR7x00)
  313. if (UnwantedMessage (confirm, packetsize, 1, (VS_NW_INFO | MMTYPE_CNF)))
  314. #else
  315. #error "Unspecified Atheros chipset"
  316. #endif
  317. {
  318. continue;
  319. }
  320. network = (struct network *)(&networks->networks);
  321. plcstation = (struct plcstation *)(&plcnetwork->plcstation);
  322. memset (plcnetwork, 0, sizeof (* plcnetwork));
  323. memset (plcstation, 0, sizeof (* plcstation));
  324. plcstation->LOC = !memcmp (confirm->ethernet.OSA, bridges [bridge], sizeof (confirm->ethernet.OSA));
  325. plcstation->CCO = !memcmp (confirm->ethernet.OSA, network->CCO_MAC, sizeof (confirm->ethernet.OSA));
  326. plcstation->TEI = network->TEI;
  327. memcpy (plcstation->MAC, confirm->ethernet.OSA, sizeof (plcstation->MAC));
  328. memcpy (plcstation->BDA, confirm->ethernet.ODA, sizeof (plcstation->BDA));
  329. PLCPlatform (channel, plcstation);
  330. plcnetwork->plcstations++;
  331. plcstation++;
  332. while (networks->NUMAVLNS--)
  333. {
  334. station = (struct station *)(&network->stations);
  335. while (network->NUMSTAS--)
  336. {
  337. memset (plcstation, 0, sizeof (* plcstation));
  338. plcstation->LOC = !memcmp (station->BDA, channel->host, sizeof (station->BDA));
  339. plcstation->CCO = !memcmp (station->MAC, network->CCO_MAC, sizeof (station->MAC));
  340. plcstation->TEI = station->TEI;
  341. memcpy (plcstation->MAC, station->MAC, sizeof (plcstation->MAC));
  342. memcpy (plcstation->BDA, station->BDA, sizeof (plcstation->BDA));
  343. #if defined (INT6x00)
  344. plcstation->TX = station->AVGTX;
  345. plcstation->RX = station->AVGRX;
  346. #elif defined (AR7x00)
  347. plcstation->TX = LE16TOH (station->AVGTX);
  348. plcstation->RX = LE16TOH (station->AVGRX);
  349. #else
  350. #error "Unspecified Atheros chipset"
  351. #endif
  352. PLCPlatform (channel, plcstation);
  353. PLCIdentity (channel, plcstation);
  354. plcnetwork->plcstations++;
  355. plcstation++;
  356. station++;
  357. }
  358. plctopology->plcnetworks++;
  359. plcnetwork = (struct plcnetwork *)(plcstation);
  360. network = (struct network *)(station);
  361. }
  362. }
  363. }
  364. return (0);
  365. }
  366. /*====================================================================*
  367. *
  368. *--------------------------------------------------------------------*/
  369. #endif