sada.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * sada.c - Slave Membership Manager;
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*"
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <stdint.h>
  56. /*====================================================================*
  57. * custom header files;
  58. *--------------------------------------------------------------------*/
  59. #include "../tools/getoptv.h"
  60. #include "../tools/putoptv.h"
  61. #include "../tools/memory.h"
  62. #include "../tools/number.h"
  63. #include "../tools/symbol.h"
  64. #include "../tools/types.h"
  65. #include "../tools/flags.h"
  66. #include "../tools/files.h"
  67. #include "../tools/error.h"
  68. #include "../plc/plc.h"
  69. #include "../key/HPAVKey.h"
  70. #include "../ram/sdram.h"
  71. #include "../pib/pib.h"
  72. #include "../nvm/nvm.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../plc/Confirm.c"
  78. #include "../plc/Display.c"
  79. #include "../plc/Failure.c"
  80. #include "../plc/Request.c"
  81. #include "../plc/ReadMME.c"
  82. #include "../plc/SendMME.c"
  83. #include "../mme/UnwantedMessage.c"
  84. #include "../plc/SlaveMembership.c"
  85. #include "../plc/Devices.c"
  86. #endif
  87. #ifndef MAKEFILE
  88. #include "../tools/getoptv.c"
  89. #include "../tools/putoptv.c"
  90. #include "../tools/version.c"
  91. #include "../tools/uintspec.c"
  92. #include "../tools/hexdump.c"
  93. #include "../tools/hexencode.c"
  94. #include "../tools/hexdecode.c"
  95. #include "../tools/todigit.c"
  96. #include "../tools/synonym.c"
  97. #include "../tools/error.c"
  98. #endif
  99. #ifndef MAKEFILE
  100. #include "../ether/openchannel.c"
  101. #include "../ether/closechannel.c"
  102. #include "../ether/readpacket.c"
  103. #include "../ether/sendpacket.c"
  104. #include "../ether/channel.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../mme/MMECode.c"
  108. #include "../mme/EthernetHeader.c"
  109. #include "../mme/QualcommHeader.c"
  110. #endif
  111. /*====================================================================*
  112. * program constants;
  113. *--------------------------------------------------------------------*/
  114. #define SADA_AUTH_MODE 0
  115. #define SADA_RETRY_TIME 0
  116. /*====================================================================*
  117. *
  118. * void manager (struct plc * plc, signed count, signed pause);
  119. *
  120. * perform operations in logical order despite any order specfied
  121. * on the command line; for example read PIB before writing PIB;
  122. *
  123. * operation order is controlled by the order of "if" statements
  124. * shown here; the entire operation sequence can be repeated with
  125. * an optional pause between each iteration;
  126. *
  127. *
  128. *--------------------------------------------------------------------*/
  129. static void manager (struct plc * plc, signed count, signed pause)
  130. {
  131. while (count--)
  132. {
  133. SlaveMembership (plc);
  134. sleep (pause);
  135. }
  136. return;
  137. }
  138. /*====================================================================*
  139. *
  140. * int main (int argc, char const * argv[]);
  141. *
  142. * parse command line, populate plc structure and perform selected
  143. * operations; show help summary if asked; see getoptv and putoptv
  144. * to understand command line parsing and help summary display; see
  145. * plc.h for the definition of struct plc;
  146. *
  147. * the command line accepts multiple MAC addresses and the program
  148. * performs the specified operations on each address, in turn; the
  149. * address order is significant but the option order is not; the
  150. * default address is a local broadcast that causes all devices on
  151. * the local H1 interface to respond but not those at the remote
  152. * end of the powerline;
  153. *
  154. * the default address is 00:B0:52:00:00:01; omitting the address
  155. * will automatically address the local device; some options will
  156. * cancel themselves if this makes no sense;
  157. *
  158. * the default interface is eth1 because most people use eth0 as
  159. * their principle network connection; you can specify another
  160. * interface with -i or define environment string PLC to make
  161. * that the default interface and save typing;
  162. *
  163. *
  164. *--------------------------------------------------------------------*/
  165. int main (int argc, char const * argv [])
  166. {
  167. extern struct channel channel;
  168. static char const * optv [] =
  169. {
  170. "A:a:ei:qt:vx",
  171. "device [device] [...] [> stdout]",
  172. "Qualcomm Atheros Slave Membership Manager",
  173. "A x\tslave MAC address",
  174. "a n\tauthorization mode is n [" LITERAL (SADA_AUTH_MODE) "]\n\t0 = retain slave in AVLN\n\t1 = eject slave from AVNL\n\t2 = eject slave from AVLN and associates with next master",
  175. "e\tredirect stderr to stdout",
  176. #if defined (WINPCAP) || defined (LIBPCAP)
  177. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  178. #else
  179. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  180. #endif
  181. "q\tquiet mode",
  182. "t n\tassociation retry time [" LITERAL (SADA_RETRY_TIME) "]",
  183. "v\tverbose mode",
  184. "x\texit on error",
  185. (char const *) (0)
  186. };
  187. #include "../plc/plc.c"
  188. signed loop = 1;
  189. signed wait = 0;
  190. signed c;
  191. if (getenv (PLCDEVICE))
  192. {
  193. channel.ifname = strdup (getenv (PLCDEVICE));
  194. }
  195. optind = 1;
  196. plc.pushbutton = SADA_AUTH_MODE;
  197. plc.module = SADA_RETRY_TIME;
  198. while ((c = getoptv (argc, argv, optv)) != -1)
  199. {
  200. switch (c)
  201. {
  202. case 'A':
  203. if (!hexencode (plc.RDA, sizeof (plc.RDA), (char const *)(optarg)))
  204. {
  205. error (1, errno, PLC_BAD_MAC, optarg);
  206. }
  207. break;
  208. case 'a':
  209. plc.pushbutton = (uint32_t)(uintspec (optarg, 0, 2));
  210. break;
  211. case 'e':
  212. dup2 (STDOUT_FILENO, STDERR_FILENO);
  213. break;
  214. case 'i':
  215. #if defined (WINPCAP) || defined (LIBPCAP)
  216. channel.ifindex = atoi (optarg);
  217. #else
  218. channel.ifname = optarg;
  219. #endif
  220. break;
  221. case 'q':
  222. _setbits (channel.flags, CHANNEL_SILENCE);
  223. _setbits (plc.flags, PLC_SILENCE);
  224. break;
  225. case 't':
  226. plc.module = (uint32_t)(uintspec (optarg, 0, 0x0F));
  227. break;
  228. case 'v':
  229. _setbits (channel.flags, CHANNEL_VERBOSE);
  230. _setbits (plc.flags, PLC_VERBOSE);
  231. break;
  232. case 'x':
  233. _setbits (plc.flags, PLC_BAILOUT);
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. argc -= optind;
  240. argv += optind;
  241. openchannel (&channel);
  242. if (!(plc.message = malloc (sizeof (* plc.message))))
  243. {
  244. error (1, errno, PLC_NOMEMORY);
  245. }
  246. if (!argc)
  247. {
  248. manager (&plc, loop, wait);
  249. }
  250. while ((argc) && (* argv))
  251. {
  252. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  253. {
  254. error (1, errno, PLC_BAD_MAC, * argv);
  255. }
  256. manager (&plc, loop, wait);
  257. argv++;
  258. argc--;
  259. }
  260. free (plc.message);
  261. closechannel (&channel);
  262. return (0);
  263. }