pev.c.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. pev.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='pcap_nametoindex.c.html' title=' pcap_nametoindex.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='pev_cm_atten_char.c.html' title=' pev_cm_atten_char.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * pev.c - QCA Plug-in Electric Vehicle Emulator;
  64. *
  65. * This program, in the current state, is not a finished product;
  66. * It has been released so that interested parties can begin to
  67. * see how the SLAC protocol might be implemented;
  68. *
  69. * Some key design features are:
  70. *
  71. * 1) the use of a channel variable to abstract ISO Layer 2 I/O;
  72. * the variable is used by functions openchannel, readmessage,
  73. * sendmessage and closechannel;
  74. *
  75. * 2) the use of a message variable to represent an IEEE 802.3
  76. * Ethernet frame; the variable allows one frame to be used
  77. * and re-used throughout the program but supports multiple
  78. * frame buffers if needed;
  79. *
  80. * 3) the use of a session variable to support multiple PEV-EVSE
  81. * interactions without using threads or subrocesses; this has
  82. * not demonstrated in this version of the program; some more
  83. * work is needed;
  84. *
  85. * 4) the absence of threads or subprocesses so that the program
  86. * can be ported to hosts without a multi-tasking operating
  87. * system;
  88. *
  89. * 5) lots of debugging messages; these can be suppressed or
  90. * deleted if not wanted;
  91. *
  92. * 6) simplified state machine;
  93. *
  94. *--------------------------------------------------------------------*/
  95. /*====================================================================*
  96. * system header files;
  97. *--------------------------------------------------------------------*/
  98. #include &lt;unistd.h&gt;
  99. #include &lt;stdlib.h&gt;
  100. #include &lt;limits.h&gt;
  101. #include &lt;string.h&gt;
  102. #include &lt;errno.h&gt;
  103. #include &lt;time.h&gt;
  104. /*====================================================================*
  105. * custom header files;
  106. *--------------------------------------------------------------------*/
  107. #include &quot;../tools/getoptv.h&quot;
  108. #include &quot;../tools/putoptv.h&quot;
  109. #include &quot;../tools/memory.h&quot;
  110. #include &quot;../tools/number.h&quot;
  111. #include &quot;../tools/types.h&quot;
  112. #include &quot;../tools/flags.h&quot;
  113. #include &quot;../tools/files.h&quot;
  114. #include &quot;../tools/error.h&quot;
  115. #include &quot;../tools/config.h&quot;
  116. #include &quot;../ether/channel.h&quot;
  117. #include &quot;../iso15118/slac.h&quot;
  118. /*====================================================================*
  119. * custom source files;
  120. *--------------------------------------------------------------------*/
  121. #ifndef MAKEFILE
  122. #include &quot;../tools/getoptv.c&quot;
  123. #include &quot;../tools/putoptv.c&quot;
  124. #include &quot;../tools/version.c&quot;
  125. #include &quot;../tools/hexdump.c&quot;
  126. #include &quot;../tools/hexdecode.c&quot;
  127. #include &quot;../tools/hexencode.c&quot;
  128. #include &quot;../tools/hexstring.c&quot;
  129. #include &quot;../tools/decdecode.c&quot;
  130. #include &quot;../tools/decstring.c&quot;
  131. #include &quot;../tools/uintspec.c&quot;
  132. #include &quot;../tools/todigit.c&quot;
  133. #include &quot;../tools/strfbits.c&quot;
  134. #include &quot;../tools/config.c&quot;
  135. #include &quot;../tools/memincr.c&quot;
  136. #include &quot;../tools/error.c&quot;
  137. #endif
  138. #ifndef MAKEFILE
  139. #include &quot;../plc/Devices.c&quot;
  140. #endif
  141. #ifndef MAKEFILE
  142. #include &quot;../mme/EthernetHeader.c&quot;
  143. #include &quot;../mme/QualcommHeader.c&quot;
  144. #include &quot;../mme/HomePlugHeader1.c&quot;
  145. #include &quot;../mme/UnwantedMessage.c&quot;
  146. #include &quot;../mme/readmessage.c&quot;
  147. #include &quot;../mme/sendmessage.c&quot;
  148. #endif
  149. #ifndef MAKEFILE
  150. #include &quot;../ether/channel.c&quot;
  151. #include &quot;../ether/openchannel.c&quot;
  152. #include &quot;../ether/closechannel.c&quot;
  153. #include &quot;../ether/sendpacket.c&quot;
  154. #include &quot;../ether/readpacket.c&quot;
  155. #endif
  156. #ifndef MAKEFILE
  157. #include &quot;../iso15118/slac_session.c&quot;
  158. #include &quot;../iso15118/slac_connect.c&quot;
  159. #include &quot;../iso15118/slac_debug.c&quot;
  160. #include &quot;../iso15118/pev_cm_slac_param.c&quot;
  161. #include &quot;../iso15118/pev_cm_start_atten_char.c&quot;
  162. #include &quot;../iso15118/pev_cm_atten_char.c&quot;
  163. #include &quot;../iso15118/pev_cm_mnbc_sound.c&quot;
  164. #include &quot;../iso15118/pev_cm_slac_match.c&quot;
  165. #include &quot;../iso15118/pev_cm_set_key.c&quot;
  166. #endif
  167. /*====================================================================*
  168. * program constants;
  169. *--------------------------------------------------------------------*/
  170. #define PLCDEVICE &quot;PLC&quot;
  171. #define PROFILE &quot;pev.ini&quot;
  172. #define SECTION &quot;default&quot;
  173. #define PEV_STATE_DISCONNECTED 1
  174. #define PEV_STATE_UNMATCHED 2
  175. #define PEV_STATE_MATCHED 3
  176. #define PEV_VID &quot;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&quot; // VehicleIdentifier
  177. #define PEV_NMK &quot;50D3E4933F855B7040784DF815AA8DB7&quot; // HomePlugAV
  178. #define PEV_NID &quot;B0F2E695666B03&quot; // HomePlugAV
  179. /*====================================================================*
  180. * program variables;
  181. *--------------------------------------------------------------------*/
  182. unsigned state = 0;
  183. /*====================================================================*
  184. *
  185. * static void configure ();
  186. *
  187. * print template PEV-HLE configuration file on stdout so that
  188. * profile, section and element names match;
  189. *
  190. *--------------------------------------------------------------------*/
  191. static void configure ()
  192. {
  193. printf (&quot;# file: %s\n&quot;, PROFILE);
  194. printf (&quot;# ====================================================================\n&quot;);
  195. printf (&quot;# PEV-HLE initialization;\n&quot;);
  196. printf (&quot;# --------------------------------------------------------------------\n&quot;);
  197. printf (&quot;[%s]\n&quot;, SECTION);
  198. printf (&quot;vehicle identifier = %s\n&quot;, PEV_VID);
  199. printf (&quot;network membership key = %s\n&quot;, PEV_NMK);
  200. printf (&quot;network identifier = %s\n&quot;, PEV_NID);
  201. printf (&quot;attenuation threshold = %d\n&quot;, SLAC_LIMIT);
  202. printf (&quot;msound pause = %d\n&quot;, SLAC_PAUSE);
  203. printf (&quot;charge time = %d\n&quot;, SLAC_CHARGETIME);
  204. printf (&quot;settle time = %d\n&quot;, SLAC_SETTLETIME);
  205. return;
  206. }
  207. /*====================================================================*
  208. *
  209. * void initialize (struct session * session, char const * profile, char const * section);
  210. *
  211. * read PEV-HLE configuration profile; initialize session variable;
  212. *
  213. *--------------------------------------------------------------------*/
  214. static void initialize (struct session * session, char const * profile, char const * section)
  215. {
  216. session-&gt;next = session-&gt;prev = session;
  217. hexencode (session-&gt;PEV_ID, sizeof (session-&gt;PEV_ID), configstring (profile, section, &quot;VehicleIdentifier&quot;, PEV_VID));
  218. hexencode (session-&gt;NMK, sizeof (session-&gt;NMK), configstring (profile, section, &quot;NetworkMembershipKey&quot;, PEV_NMK));
  219. hexencode (session-&gt;NID, sizeof (session-&gt;NID), configstring (profile, section, &quot;NetworkIdentifier&quot;, PEV_NID));
  220. session-&gt;limit = confignumber_range (profile, section, &quot;AttenuationThreshold&quot;, SLAC_LIMIT, 0, UINT_MAX);
  221. session-&gt;pause = confignumber_range (profile, section, &quot;MSoundPause&quot;, SLAC_PAUSE, 0, UINT_MAX);
  222. session-&gt;settletime = confignumber_range (profile, section, &quot;SettleTime&quot;, SLAC_SETTLETIME, 0, UINT_MAX);
  223. session-&gt;chargetime = confignumber_range (profile, section, &quot;ChargeTime&quot;, SLAC_CHARGETIME, 0, UINT_MAX);
  224. session-&gt;state = PEV_STATE_DISCONNECTED;
  225. memcpy (session-&gt;original_nmk, session-&gt;NMK, sizeof (session-&gt;original_nmk));
  226. memcpy (session-&gt;original_nid, session-&gt;NID, sizeof (session-&gt;original_nid));
  227. slac_session (session);
  228. return;
  229. }
  230. /*====================================================================*
  231. *
  232. * signed identifier (struct session * session, struct channel * channel);
  233. *
  234. * generate the run identifier and store in session variable;
  235. *
  236. * copy channel host address to session PEV MAC address; set session
  237. * PEV identifier to zeros;
  238. *
  239. *--------------------------------------------------------------------*/
  240. static signed identifier (struct session * session, struct channel * channel)
  241. {
  242. time_t now;
  243. time (&amp; now);
  244. memset (session-&gt;RunID, 0, sizeof (session-&gt;RunID));
  245. memcpy (session-&gt;RunID, channel-&gt;host, ETHER_ADDR_LEN);
  246. memcpy (session-&gt;PEV_MAC, channel-&gt;host, sizeof (session-&gt;PEV_MAC));
  247. return (0);
  248. }
  249. /*====================================================================*
  250. *
  251. * void DisconnectedState (struct session * session, struct channel * channel, struct message * message);
  252. *
  253. *--------------------------------------------------------------------*/
  254. static void DisconnectedState (struct session * session, struct channel * channel, struct message * message)
  255. {
  256. slac_session (session);
  257. slac_debug (session, 0, __func__, &quot;Probing ...&quot;);
  258. memincr (session-&gt;RunID, sizeof (session-&gt;RunID));
  259. while (pev_cm_slac_param (session, channel, message));
  260. session-&gt;state = PEV_STATE_UNMATCHED;
  261. return;
  262. }
  263. /*====================================================================*
  264. *
  265. * void MatchingState (struct session * session, struct channel * channel, struct message * message);
  266. *
  267. * The PEV-EVSE perform GreenPPEA protocol in this state;
  268. *
  269. * the cm_start_atten_char and cm_mnbc_sound messages are sent
  270. * broadcast; the application may receive multiple cm_atten_char
  271. * messages before sending the cm_slac_match message;
  272. *
  273. *--------------------------------------------------------------------*/
  274. static void UnmatchedState (struct session * session, struct channel * channel, struct message * message)
  275. {
  276. slac_session (session);
  277. slac_debug (session, 0, __func__, &quot;Sounding ...&quot;);
  278. if (pev_cm_start_atten_char (session, channel, message))
  279. {
  280. session-&gt;state = PEV_STATE_DISCONNECTED;
  281. return;
  282. }
  283. if (pev_cm_mnbc_sound (session, channel, message))
  284. {
  285. session-&gt;state = PEV_STATE_DISCONNECTED;
  286. return;
  287. }
  288. if (pev_cm_atten_char (session, channel, message))
  289. {
  290. session-&gt;state = PEV_STATE_DISCONNECTED;
  291. return;
  292. }
  293. if (slac_connect (session))
  294. {
  295. session-&gt;state = PEV_STATE_DISCONNECTED;
  296. return;
  297. }
  298. slac_debug (session, 0, __func__, &quot;Matching ...&quot;);
  299. if (pev_cm_slac_match (session, channel, message))
  300. {
  301. session-&gt;state = PEV_STATE_DISCONNECTED;
  302. return;
  303. }
  304. session-&gt;state = PEV_STATE_MATCHED;
  305. return;
  306. }
  307. /*====================================================================*
  308. *
  309. * void MatchedState (struct session * session, struct channel * channel, struct message * message);
  310. *
  311. * charge vehicle; restore original NMK/NID and disconnect; loop
  312. * if SLAC_CONTINUE is set;
  313. *
  314. *--------------------------------------------------------------------*/
  315. static void MatchedState (struct session * session, struct channel * channel, struct message * message)
  316. {
  317. slac_session (session);
  318. slac_debug (session, 0, __func__, &quot;Connecting ...&quot;);
  319. #if SLAC_AVLN_EVSE
  320. slac_debug (session, 0, __func__, &quot;waiting for evse to settle ...&quot;);
  321. sleep (session-&gt;settletime);
  322. #endif
  323. #if SLAC_AVLN_PEV
  324. if (pev_cm_set_key (session, channel, message))
  325. {
  326. session-&gt;state = PEV_STATE_DISCONNECTED;
  327. return;
  328. }
  329. sleep (session-&gt;settletime);
  330. #endif
  331. slac_debug (session, 0, __func__, &quot;Charging (%d) ...\n\n&quot;, session-&gt;counter++);
  332. sleep (session-&gt;chargetime);
  333. slac_debug (session, 0, __func__, &quot;Disconnecting ...&quot;);
  334. #if SLAC_AVLN_EVSE
  335. slac_debug (session, 0, __func__, &quot;waiting for evse to settle ...&quot;);
  336. sleep (session-&gt;settletime);
  337. #endif
  338. #if SLAC_AVLN_PEV
  339. memcpy (session-&gt;NMK, session-&gt;original_nmk, sizeof (session-&gt;NMK));
  340. memcpy (session-&gt;NID, session-&gt;original_nid, sizeof (session-&gt;NID));
  341. if (pev_cm_set_key (session, channel, message))
  342. {
  343. session-&gt;state = PEV_STATE_DISCONNECTED;
  344. return;
  345. }
  346. sleep (session-&gt;settletime);
  347. #endif
  348. session-&gt;state = state;
  349. return;
  350. }
  351. /*====================================================================*
  352. *
  353. * int main (int argc, char * argv[]);
  354. *
  355. *
  356. *--------------------------------------------------------------------*/
  357. int main (int argc, char const * argv [])
  358. {
  359. extern struct channel channel;
  360. static char const * optv [] =
  361. {
  362. &quot;cCdi:lp:qs:t:vx&quot;,
  363. &quot;&quot;,
  364. &quot;Plug-in Electric Vehicle Emulator&quot;,
  365. &quot;c\tprint template configuration file on stdout&quot;,
  366. &quot;C\tstop on count mismatch&quot;,
  367. &quot;d\tdisplay debug information&quot;,
  368. #if defined (WINPCAP) || defined (LIBPCAP)
  369. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  370. #else
  371. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  372. #endif
  373. &quot;l\tloop indefinitely&quot;,
  374. &quot;p s\tconfiguration profile is (s) [&quot; LITERAL (PROFILE) &quot;]&quot;,
  375. &quot;q\tsuppress normal output&quot;,
  376. &quot;s s\tconfiguration section is (s) [&quot; LITERAL (SECTION) &quot;]&quot;,
  377. &quot;t n\tread timeout is (n) milliseconds [&quot; LITERAL (SLAC_TIMEOUT) &quot;]&quot;,
  378. &quot;v\tverbose messages on stdout&quot;,
  379. &quot;x\texit on error&quot;,
  380. (char const *) (0)
  381. };
  382. struct session session;
  383. struct message message;
  384. char const * profile = PROFILE;
  385. char const * section = SECTION;
  386. signed c;
  387. memset (&amp; session, 0, sizeof (session));
  388. memset (&amp; message, 0, sizeof (message));
  389. channel.timeout = SLAC_TIMEOUT;
  390. if (getenv (PLCDEVICE))
  391. {
  392. #if defined (WINPCAP) || defined (LIBPCAP)
  393. channel.ifindex = atoi (getenv (PLCDEVICE));
  394. #else
  395. channel.ifname = strdup (getenv (PLCDEVICE));
  396. #endif
  397. }
  398. optind = 1;
  399. while (~ (c = getoptv (argc, argv, optv)))
  400. {
  401. switch (c)
  402. {
  403. case 'c':
  404. configure ();
  405. return (0);
  406. case 'C':
  407. _setbits (session.flags, SLAC_COMPARE);
  408. break;
  409. case 'd':
  410. _setbits (session.flags, (SLAC_VERBOSE | SLAC_SESSION));
  411. break;
  412. case 'i':
  413. #if defined (WINPCAP) || defined (LIBPCAP)
  414. channel.ifindex = atoi (optarg);
  415. #else
  416. channel.ifname = optarg;
  417. #endif
  418. break;
  419. case 'l':
  420. state = PEV_STATE_DISCONNECTED;
  421. break;
  422. case 'p':
  423. profile = optarg;
  424. break;
  425. case 's':
  426. section = optarg;
  427. break;
  428. case 'q':
  429. _setbits (channel.flags, CHANNEL_SILENCE);
  430. _setbits (session.flags, SLAC_SILENCE);
  431. break;
  432. case 't':
  433. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  434. break;
  435. case 'v':
  436. _setbits (channel.flags, CHANNEL_VERBOSE);
  437. break;
  438. case 'x':
  439. session.exit = session.exit? 0: 1;
  440. break;
  441. default:
  442. break;
  443. }
  444. }
  445. argc -= optind;
  446. argv += optind;
  447. if (argc)
  448. {
  449. slac_debug (&amp; session, 1, __func__, ERROR_TOOMANY);
  450. }
  451. openchannel (&amp; channel);
  452. identifier (&amp; session, &amp; channel);
  453. initialize (&amp; session, profile, section);
  454. if (pev_cm_set_key (&amp; session, &amp; channel, &amp; message))
  455. {
  456. slac_debug (&amp; session, 1, __func__, &quot;Can't set key&quot;);
  457. }
  458. sleep (session.settletime);
  459. while (session.state)
  460. {
  461. if (session.state == PEV_STATE_DISCONNECTED)
  462. {
  463. DisconnectedState (&amp; session, &amp; channel, &amp; message);
  464. continue;
  465. }
  466. if (session.state == PEV_STATE_UNMATCHED)
  467. {
  468. UnmatchedState (&amp; session, &amp; channel, &amp; message);
  469. continue;
  470. }
  471. if (session.state == PEV_STATE_MATCHED)
  472. {
  473. MatchedState (&amp; session, &amp; channel, &amp; message);
  474. continue;
  475. }
  476. slac_debug (&amp; session, 1, __func__, &quot;Illegal state!&quot;);
  477. }
  478. closechannel (&amp; channel);
  479. return (0);
  480. }
  481. </pre>
  482. <div class='footerlink'>
  483. [<a href='pcap_nametoindex.c.html' title=' pcap_nametoindex.c '>PREV</a>]
  484. [<a href='toolkit.html' title=' Index '>HOME</a>]
  485. [<a href='pev_cm_atten_char.c.html' title=' pev_cm_atten_char.c '>NEXT</a>]
  486. </div>
  487. </body>
  488. </html>