plcget.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * plcget.c -
  44. *
  45. *
  46. *--------------------------------------------------------------------*/
  47. /*====================================================================*"
  48. * system header files;
  49. *--------------------------------------------------------------------*/
  50. #include <unistd.h>
  51. #include <stdlib.h>
  52. #include <stdint.h>
  53. #include <limits.h>
  54. /*====================================================================*
  55. * custom header files;
  56. *--------------------------------------------------------------------*/
  57. #include "../tools/getoptv.h"
  58. #include "../tools/putoptv.h"
  59. #include "../tools/memory.h"
  60. #include "../tools/number.h"
  61. #include "../tools/symbol.h"
  62. #include "../tools/types.h"
  63. #include "../tools/flags.h"
  64. #include "../tools/files.h"
  65. #include "../tools/error.h"
  66. #include "../plc/plc.h"
  67. #include "../ether/channel.h"
  68. /*====================================================================*
  69. * custom source files;
  70. *--------------------------------------------------------------------*/
  71. #ifndef MAKEFILE
  72. #include "../plc/Devices.c"
  73. #include "../plc/Confirm.c"
  74. #include "../plc/Display.c"
  75. #include "../plc/Failure.c"
  76. #include "../plc/Request.c"
  77. #include "../plc/ReadMME.c"
  78. #include "../plc/SendMME.c"
  79. #include "../plc/GetProperty.c"
  80. #endif
  81. #ifndef MAKEFILE
  82. #include "../tools/synonym.c"
  83. #include "../tools/getoptv.c"
  84. #include "../tools/putoptv.c"
  85. #include "../tools/version.c"
  86. #include "../tools/uintspec.c"
  87. #include "../tools/basespec.c"
  88. #include "../tools/hexdump.c"
  89. #include "../tools/hexencode.c"
  90. #include "../tools/hexdecode.c"
  91. #include "../tools/todigit.c"
  92. #include "../tools/binout.c"
  93. #include "../tools/hexout.c"
  94. #include "../tools/decout.c"
  95. #include "../tools/chrout.c"
  96. #include "../tools/error.c"
  97. #endif
  98. #ifndef MAKEFILE
  99. #include "../ether/openchannel.c"
  100. #include "../ether/closechannel.c"
  101. #include "../ether/readpacket.c"
  102. #include "../ether/sendpacket.c"
  103. #include "../ether/channel.c"
  104. #endif
  105. #ifndef MAKEFILE
  106. #include "../mme/MMECode.c"
  107. #include "../mme/EthernetHeader.c"
  108. #include "../mme/QualcommHeader.c"
  109. #include "../mme/UnwantedMessage.c"
  110. #endif
  111. /*====================================================================*
  112. *
  113. * int main (int argc, char const * argv[]);
  114. *
  115. * parse command line, populate plc structure and perform selected
  116. * operations; show help summary if asked; see getoptv and putoptv
  117. * to understand command line parsing and help summary display; see
  118. * plc.h for the definition of struct plc;
  119. *
  120. * the command line accepts multiple MAC addresses and the program
  121. * performs the specified operations on each address, in turn; the
  122. * address order is significant but the option order is not; the
  123. * default address is a local broadcast that causes all devices on
  124. * the local H1 interface to respond but not those at the remote
  125. * end of the powerline;
  126. *
  127. * the default address is 00:B0:52:00:00:01; omitting the address
  128. * will automatically address the local device; some options will
  129. * cancel themselves if this makes no sense;
  130. *
  131. * the default interface is eth1 because most people use eth0 as
  132. * their principle network connection; you can specify another
  133. * interface with -i or define environment string PLC to make
  134. * that the default interface and save typing;
  135. *
  136. *
  137. *--------------------------------------------------------------------*/
  138. int main (int argc, char const * argv [])
  139. {
  140. extern struct channel channel;
  141. static char const * optv [] =
  142. {
  143. "abdehi:n:qs:t:v",
  144. "device [device] [...]",
  145. "Qualcomm Atheros PLC Get Property",
  146. "a\tdisplay property in ASCII",
  147. "b\tdisplay property in binary",
  148. "d\tdisplay property in decimal",
  149. "e\tredirect stderr to stdout",
  150. "h\tdisplay property in hexadecimal",
  151. #if defined (WINPCAP) || defined (LIBPCAP)
  152. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  153. #else
  154. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  155. #endif
  156. "n n\tproperty identifier or version is (n) [0]",
  157. "q\tquiet mode",
  158. "s x\tsession identifier is (x) [" LITERAL (PLCSESSION) "]",
  159. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  160. "v\tverbose mode",
  161. (char const *) (0)
  162. };
  163. #include "../plc/plc.c"
  164. struct plcproperty plcproperty;
  165. signed c;
  166. if (getenv (PLCDEVICE))
  167. {
  168. #if defined (WINPCAP) || defined (LIBPCAP)
  169. channel.ifindex = atoi (getenv (PLCDEVICE));
  170. #else
  171. channel.ifname = strdup (getenv (PLCDEVICE));
  172. #endif
  173. }
  174. optind = 1;
  175. memset (&plcproperty, 0, sizeof (plcproperty));
  176. while ((c = getoptv (argc, argv, optv)) != -1)
  177. {
  178. switch (c)
  179. {
  180. case 'a':
  181. plcproperty.DATA_FORMAT = PLC_FORMAT_ASC;
  182. break;
  183. case 'b':
  184. plcproperty.DATA_FORMAT = PLC_FORMAT_BIN;
  185. break;
  186. case 'h':
  187. plcproperty.DATA_FORMAT = PLC_FORMAT_HEX;
  188. break;
  189. case 'd':
  190. plcproperty.DATA_FORMAT = PLC_FORMAT_DEC;
  191. break;
  192. case 'e':
  193. dup2 (STDOUT_FILENO, STDERR_FILENO);
  194. break;
  195. case 'i':
  196. #if defined (WINPCAP) || defined (LIBPCAP)
  197. channel.ifindex = atoi (optarg);
  198. #else
  199. channel.ifname = optarg;
  200. #endif
  201. break;
  202. case 'n':
  203. if (!plcproperty.PROP_NUMBER)
  204. {
  205. plcproperty.PROP_FORMAT = 1;
  206. plcproperty.PROP_LENGTH = sizeof (uint32_t);
  207. plcproperty.PROP_NUMBER = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  208. }
  209. else
  210. {
  211. plcproperty.PROP_VERSION = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  212. }
  213. break;
  214. case 'q':
  215. _setbits (channel.flags, CHANNEL_SILENCE);
  216. _setbits (plc.flags, PLC_SILENCE);
  217. break;
  218. case 's':
  219. plc.cookie = (uint32_t)(basespec (optarg, 16, sizeof (plc.cookie)));
  220. break;
  221. case 't':
  222. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  223. break;
  224. case 'v':
  225. _setbits (channel.flags, CHANNEL_VERBOSE);
  226. _setbits (plc.flags, PLC_VERBOSE);
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. argc -= optind;
  233. argv += optind;
  234. openchannel (&channel);
  235. if (!(plc.message = malloc (sizeof (* plc.message))))
  236. {
  237. error (1, errno, PLC_NOMEMORY);
  238. }
  239. if (!argc)
  240. {
  241. GetProperty (&plc, &plcproperty);
  242. }
  243. while ((argc) && (* argv))
  244. {
  245. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  246. {
  247. error (1, errno, PLC_BAD_MAC, * argv);
  248. }
  249. GetProperty (&plc, &plcproperty);
  250. argc--;
  251. argv++;
  252. }
  253. free (plc.message);
  254. closechannel (&channel);
  255. exit (0);
  256. }