plctone.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * plctone.c -
  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. #include <limits.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/putoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/symbol.h"
  64. #include "../tools/number.h"
  65. #include "../tools/types.h"
  66. #include "../tools/flags.h"
  67. #include "../tools/files.h"
  68. #include "../tools/error.h"
  69. #include "../plc/plc.h"
  70. /*====================================================================*
  71. * custom source files;
  72. *--------------------------------------------------------------------*/
  73. #ifndef MAKEFILE
  74. #include "../plc/Devices.c"
  75. #include "../plc/Failure.c"
  76. #include "../plc/ReadMME.c"
  77. #include "../plc/SendMME.c"
  78. #include "../plc/ToneMaps2.c"
  79. #include "../plc/SignalToNoise2.c"
  80. #endif
  81. #ifndef MAKEFILE
  82. #include "../tools/error.c"
  83. #include "../tools/getoptv.c"
  84. #include "../tools/putoptv.c"
  85. #include "../tools/version.c"
  86. #include "../tools/uintspec.c"
  87. #include "../tools/hexdump.c"
  88. #include "../tools/hexencode.c"
  89. #include "../tools/hexdecode.c"
  90. #include "../tools/hexstring.c"
  91. #include "../tools/todigit.c"
  92. #include "../tools/synonym.c"
  93. #endif
  94. #ifndef MAKEFILE
  95. #include "../ether/openchannel.c"
  96. #include "../ether/closechannel.c"
  97. #include "../ether/readpacket.c"
  98. #include "../ether/sendpacket.c"
  99. #include "../ether/channel.c"
  100. #endif
  101. #ifndef MAKEFILE
  102. #include "../mme/EthernetHeader.c"
  103. #include "../mme/QualcommHeader1.c"
  104. #include "../mme/UnwantedMessage.c"
  105. #include "../mme/MMECode.c"
  106. #endif
  107. /*====================================================================*
  108. * program variables;
  109. *--------------------------------------------------------------------*/
  110. static const struct _term_ couple [] =
  111. {
  112. {
  113. "alt",
  114. "1"
  115. },
  116. {
  117. "pri",
  118. "0"
  119. }
  120. };
  121. /*====================================================================*
  122. *
  123. * int main (int argc, char const * argv[]);
  124. *
  125. * parse command line, populate plc structure and perform selected
  126. * operations; show help summary if asked; see getoptv and putoptv
  127. * to understand command line parsing and help summary display; see
  128. * plc.h for the definition of struct plc;
  129. *
  130. * the command line accepts multiple MAC addresses and the program
  131. * performs the specified operations on each address, in turn; the
  132. * address order is significant but the option order is not; the
  133. * default address is a local broadcast that causes all devices on
  134. * the local H1 interface to respond but not those at the remote
  135. * end of the powerline;
  136. *
  137. * the default address is 00:B0:52:00:00:01; omitting the address
  138. * will automatically address the local device; some options will
  139. * cancel themselves if this makes no sense;
  140. *
  141. * the default interface is eth1 because most people use eth0 as
  142. * their principle network connection; you can specify another
  143. * interface with -i or define environment string PLC to make
  144. * that the default interface and save typing;
  145. *
  146. *
  147. *--------------------------------------------------------------------*/
  148. int main (int argc, char const * argv [])
  149. {
  150. extern struct channel channel;
  151. static char const * optv [] =
  152. {
  153. "ehi:p:qsvx",
  154. "node peer [> stdout]",
  155. "Qualcomm Atheros Panther/Lynx Tone Map Dump",
  156. "e\tredirect stderr to stdout",
  157. "h\tprint mean-square histogram",
  158. #if defined (WINPCAP) || defined (LIBPCAP)
  159. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  160. #else
  161. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  162. #endif
  163. "p n\tcoupling [" LITERAL (PLCOUPLING) "]",
  164. "q\tquiet mode",
  165. "s\tcompute signal-to-noise and bits-per-carrier ratios",
  166. "v\tverbose mode",
  167. "x\texit on error",
  168. (char const *) (0)
  169. };
  170. #include "../plc/plc.c"
  171. signed (* function) (struct plc *) = ToneMaps2;
  172. signed c;
  173. plc.action = PLCOUPLING;
  174. if (getenv (PLCDEVICE))
  175. {
  176. #if defined (WINPCAP) || defined (LIBPCAP)
  177. channel.ifindex = atoi (getenv (PLCDEVICE));
  178. #else
  179. channel.ifname = strdup (getenv (PLCDEVICE));
  180. #endif
  181. }
  182. optind = 1;
  183. while ((c = getoptv (argc, argv, optv)) != -1)
  184. {
  185. switch (c)
  186. {
  187. case 'p':
  188. plc.coupling = (unsigned)(uintspec (synonym (optarg, couple, SIZEOF (couple)), 0, UCHAR_MAX));
  189. break;
  190. case 'e':
  191. dup2 (STDOUT_FILENO, STDERR_FILENO);
  192. break;
  193. case 'h':
  194. _setbits (plc.flags, PLC_GRAPH);
  195. break;
  196. case 'i':
  197. #if defined (WINPCAP) || defined (LIBPCAP)
  198. channel.ifindex = atoi (optarg);
  199. #else
  200. channel.ifname = optarg;
  201. #endif
  202. break;
  203. case 'q':
  204. _setbits (channel.flags, CHANNEL_SILENCE);
  205. _setbits (plc.flags, PLC_SILENCE);
  206. break;
  207. case 's':
  208. _setbits (plc.flags, PLC_ANALYSE);
  209. function = SignalToNoise2;
  210. break;
  211. case 'v':
  212. _setbits (channel.flags, CHANNEL_VERBOSE);
  213. _setbits (plc.flags, PLC_VERBOSE);
  214. break;
  215. case 'x':
  216. _setbits (plc.flags, PLC_BAILOUT);
  217. break;
  218. default:
  219. break;
  220. }
  221. }
  222. argc -= optind;
  223. argv += optind;
  224. if (!argc || !argv)
  225. {
  226. error (1, ECANCELED, "No node address given");
  227. }
  228. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  229. {
  230. error (1, errno, PLC_BAD_MAC, * argv);
  231. }
  232. argc--;
  233. argv++;
  234. if (!argc || !argv)
  235. {
  236. error (1, ECANCELED, "No peer address given");
  237. }
  238. if (!hexencode (plc.RDA, sizeof (plc.RDA), synonym (* argv, devices, SIZEOF (devices))))
  239. {
  240. error (1, errno, PLC_BAD_MAC, * argv);
  241. }
  242. argc--;
  243. argv++;
  244. openchannel (&channel);
  245. if (!(plc.message = malloc (sizeof (* plc.message))))
  246. {
  247. error (1, errno, PLC_NOMEMORY);
  248. }
  249. function (&plc);
  250. free (plc.message);
  251. closechannel (&channel);
  252. return (0);
  253. }