int6ktone.c 7.4 KB

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