evse.c.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. evse.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='EthernetHeader.c.html' title=' EthernetHeader.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='evse_cm_atten_char.c.html' title=' evse_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. * evse.c - QCA Electric Vehicle Supply Equipment 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. *--------------------------------------------------------------------*/
  93. /*====================================================================*
  94. * system header files;
  95. *--------------------------------------------------------------------*/
  96. #include &lt;unistd.h&gt;
  97. #include &lt;stdlib.h&gt;
  98. #include &lt;string.h&gt;
  99. #include &lt;limits.h&gt;
  100. #include &lt;errno.h&gt;
  101. #include &lt;sys/time.h&gt;
  102. /*====================================================================*
  103. * custom header files;
  104. *--------------------------------------------------------------------*/
  105. #include &quot;../ether/channel.h&quot;
  106. #include &quot;../tools/getoptv.h&quot;
  107. #include &quot;../tools/putoptv.h&quot;
  108. #include &quot;../tools/memory.h&quot;
  109. #include &quot;../tools/number.h&quot;
  110. #include &quot;../tools/config.h&quot;
  111. #include &quot;../tools/types.h&quot;
  112. #include &quot;../tools/timer.h&quot;
  113. #include &quot;../tools/flags.h&quot;
  114. #include &quot;../tools/error.h&quot;
  115. #include &quot;../ether/channel.h&quot;
  116. #include &quot;../iso15118/slac.h&quot;
  117. /*====================================================================*
  118. * custom source files;
  119. *--------------------------------------------------------------------*/
  120. #ifndef MAKEFILE
  121. #include &quot;../tools/getoptv.c&quot;
  122. #include &quot;../tools/putoptv.c&quot;
  123. #include &quot;../tools/version.c&quot;
  124. #include &quot;../tools/uintspec.c&quot;
  125. #include &quot;../tools/hexdecode.c&quot;
  126. #include &quot;../tools/hexencode.c&quot;
  127. #include &quot;../tools/hexdump.c&quot;
  128. #include &quot;../tools/hexout.c&quot;
  129. #include &quot;../tools/hexstring.c&quot;
  130. #include &quot;../tools/decdecode.c&quot;
  131. #include &quot;../tools/decstring.c&quot;
  132. #include &quot;../tools/todigit.c&quot;
  133. #include &quot;../tools/strfbits.c&quot;
  134. #include &quot;../tools/error.c&quot;
  135. #include &quot;../tools/config.c&quot;
  136. #endif
  137. #ifndef MAKEFILE
  138. #include &quot;../ether/openchannel.c&quot;
  139. #include &quot;../ether/closechannel.c&quot;
  140. #include &quot;../ether/readpacket.c&quot;
  141. #include &quot;../ether/sendpacket.c&quot;
  142. #include &quot;../ether/channel.c&quot;
  143. #endif
  144. #ifndef MAKEFILE
  145. #include &quot;../plc/Devices.c&quot;
  146. #endif
  147. #ifndef MAKEFILE
  148. #include &quot;../mme/EthernetHeader.c&quot;
  149. #include &quot;../mme/QualcommHeader.c&quot;
  150. #include &quot;../mme/HomePlugHeader1.c&quot;
  151. #include &quot;../mme/UnwantedMessage.c&quot;
  152. #include &quot;../mme/readmessage.c&quot;
  153. #include &quot;../mme/sendmessage.c&quot;
  154. #endif
  155. #ifndef MAKEFILE
  156. #include &quot;../iso15118/slac_session.c&quot;
  157. #include &quot;../iso15118/slac_debug.c&quot;
  158. #include &quot;../iso15118/evse_cm_slac_param.c&quot;
  159. #include &quot;../iso15118/evse_cm_start_atten_char.c&quot;
  160. #include &quot;../iso15118/evse_cm_atten_char.c&quot;
  161. #include &quot;../iso15118/evse_cm_mnbc_sound.c&quot;
  162. #include &quot;../iso15118/evse_cm_slac_match.c&quot;
  163. #include &quot;../iso15118/evse_cm_set_key.c&quot;
  164. #endif
  165. /*====================================================================*
  166. * program constants;
  167. *--------------------------------------------------------------------*/
  168. #define PLCDEVICE &quot;PLC&quot;
  169. #define PROFILE &quot;evse.ini&quot;
  170. #define SECTION &quot;default&quot;
  171. #define STATION &quot;&quot;
  172. #define EVSE_STATE_UNAVAILABLE 0
  173. #define EVSE_STATE_UNOCCUPIED 1
  174. #define EVSE_STATE_UNMATCHED 2
  175. #define EVSE_STATE_MATCHED 3
  176. #define EVSE_SID &quot;BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB&quot; // Station Identifier
  177. #define EVSE_NMK &quot;B59319D7E8157BA001B018669CCEE30D&quot; // HomePlugAV0123
  178. #define EVSE_NID &quot;026BCBA5354E08&quot; // HomePlugAV0123
  179. /*====================================================================*
  180. *
  181. * void configure ()
  182. *
  183. * print default EVSE-HLE configuration file on stdout so that the
  184. * profile, section and element names match;
  185. *
  186. *--------------------------------------------------------------------*/
  187. static void configure ()
  188. {
  189. printf (&quot;# file: %s\n&quot;, PROFILE);
  190. printf (&quot;# ====================================================================\n&quot;);
  191. printf (&quot;# EVSE-HLE initiaization;\n&quot;);
  192. printf (&quot;# --------------------------------------------------------------------\n&quot;);
  193. printf (&quot;[%s]\n&quot;, SECTION);
  194. printf (&quot;station identifier = %s\n&quot;, EVSE_SID);
  195. printf (&quot;network membership key = %s\n&quot;, EVSE_NMK);
  196. printf (&quot;network identifier = %s\n&quot;, EVSE_NID);
  197. printf (&quot;number of sounds = %d\n&quot;, SLAC_MSOUNDS);
  198. printf (&quot;time to sound = %d\n&quot;, SLAC_TIMETOSOUND);
  199. printf (&quot;response type = %d\n&quot;, SLAC_RESPONSE_TYPE);
  200. printf (&quot;settle time = %d\n&quot;, SLAC_SETTLETIME);
  201. printf (&quot;charge time = %d\n&quot;, SLAC_CHARGETIME);
  202. return;
  203. }
  204. /*====================================================================*
  205. *
  206. * char void initialize (struct session * session, char const * profile, char const * section)
  207. *
  208. * read EVSE-HLE configuration profile; initialize session variable;
  209. *
  210. *--------------------------------------------------------------------*/
  211. static void initialize (struct session * session, char const * profile, char const * section)
  212. {
  213. session-&gt;next = session-&gt;prev = session;
  214. hexencode (session-&gt;EVSE_ID, sizeof (session-&gt;EVSE_ID), configstring (profile, section, &quot;StationIdentifier&quot;, EVSE_SID));
  215. hexencode (session-&gt;NMK, sizeof (session-&gt;NMK), configstring (profile, section, &quot;NetworkMembershipKey&quot;, EVSE_NMK));
  216. hexencode (session-&gt;NID, sizeof (session-&gt;NID), configstring (profile, section, &quot;NetworkIdentifier&quot;, EVSE_NID));
  217. session-&gt;NUM_SOUNDS = confignumber_range (profile, section, &quot;NumberOfSounds&quot;, SLAC_MSOUNDS, 0, UCHAR_MAX);
  218. session-&gt;TIME_OUT = confignumber_range (profile, section, &quot;TimeToSound&quot;, SLAC_TIMETOSOUND, 0, UCHAR_MAX);
  219. session-&gt;RESP_TYPE = confignumber_range (profile, section, &quot;ResponseType&quot;, SLAC_RESPONSE_TYPE, 0, UCHAR_MAX);
  220. session-&gt;chargetime = confignumber_range (profile, section, &quot;ChargeTime&quot;, SLAC_CHARGETIME, 0, UINT_MAX);
  221. session-&gt;settletime = confignumber_range (profile, section, &quot;SettleTime&quot;, SLAC_SETTLETIME, 0, UINT_MAX);
  222. memcpy (session-&gt;original_nmk, session-&gt;NMK, sizeof (session-&gt;original_nmk));
  223. memcpy (session-&gt;original_nid, session-&gt;NID, sizeof (session-&gt;original_nid));
  224. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  225. slac_session (session);
  226. return;
  227. }
  228. /*====================================================================*
  229. *
  230. * signed identifier (struct session * session, struct channel * channel);
  231. *
  232. * copy channel host address to session EVSE MAC address; set session
  233. * EVSE identifier to zeros;
  234. *
  235. *--------------------------------------------------------------------*/
  236. static signed identifier (struct session * session, struct channel * channel)
  237. {
  238. memcpy (session-&gt;EVSE_MAC, channel-&gt;host, sizeof (session-&gt;EVSE_MAC));
  239. return (0);
  240. }
  241. /*====================================================================*
  242. *
  243. * void UnoccupiedState (struct session * session, struct channel * channel, struct message * message);
  244. *
  245. *--------------------------------------------------------------------*/
  246. static void UnoccupiedState (struct session * session, struct channel * channel, struct message * message)
  247. {
  248. slac_session (session);
  249. slac_debug (session, 0, __func__, &quot;Listening ...&quot;);
  250. while (evse_cm_slac_param (session, channel, message));
  251. session-&gt;state = EVSE_STATE_UNMATCHED;
  252. return;
  253. }
  254. /*====================================================================*
  255. *
  256. * void MatchingState (struct session * session, struct channel * channel, struct message * message);
  257. *
  258. * the cm_start_atten_char message establishes msound count and
  259. * timeout;
  260. *
  261. *--------------------------------------------------------------------*/
  262. static void UnmatchedState (struct session * session, struct channel * channel, struct message * message)
  263. {
  264. slac_session (session);
  265. slac_debug (session, 0, __func__, &quot;Sounding ...&quot;);
  266. if (evse_cm_start_atten_char (session, channel, message))
  267. {
  268. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  269. return;
  270. }
  271. if (evse_cm_mnbc_sound (session, channel, message))
  272. {
  273. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  274. return;
  275. }
  276. if (evse_cm_atten_char (session, channel, message))
  277. {
  278. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  279. return;
  280. }
  281. slac_debug (session, 0, __func__, &quot;Matching ...&quot;);
  282. if (evse_cm_slac_match (session, channel, message))
  283. {
  284. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  285. return;
  286. }
  287. session-&gt;state = EVSE_STATE_MATCHED;
  288. return;
  289. }
  290. /*====================================================================*
  291. *
  292. * void MatchedState (struct session * session, struct channel * channel, struct message * message);
  293. *
  294. *--------------------------------------------------------------------*/
  295. static void MatchedState (struct session * session, struct channel * channel, struct message * message)
  296. {
  297. slac_debug (session, 0, __func__, &quot;Connecting ...&quot;);
  298. #if SLAC_AVLN_EVSE
  299. if (evse_cm_set_key (session, channel, message))
  300. {
  301. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  302. return;
  303. }
  304. sleep (session-&gt;settletime);
  305. #endif
  306. #if SLAC_AVLN_PEV
  307. slac_debug (session, 0, __func__, &quot;waiting for pev to settle ...&quot;);
  308. sleep (session-&gt;settletime);
  309. #endif
  310. slac_debug (session, 0, __func__, &quot;Charging (%d) ...\n\n&quot;, session-&gt;counter++);
  311. sleep (session-&gt;chargetime);
  312. slac_debug (session, 0, __func__, &quot;Disconnecting ...&quot;);
  313. #if SLAC_AVLN_EVSE
  314. memcpy (session-&gt;NMK, session-&gt;original_nmk, sizeof (session-&gt;NMK));
  315. memcpy (session-&gt;NID, session-&gt;original_nid, sizeof (session-&gt;NID));
  316. if (evse_cm_set_key (session, channel, message))
  317. {
  318. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  319. return;
  320. }
  321. sleep (session-&gt;settletime);
  322. #endif
  323. #if SLAC_AVLN_PEV
  324. slac_debug (session, 0, __func__, &quot;waiting for pev to settle ...&quot;);
  325. sleep (session-&gt;settletime);
  326. #endif
  327. session-&gt;state = EVSE_STATE_UNOCCUPIED;
  328. return;
  329. }
  330. /*====================================================================*
  331. *
  332. * int main (int argc, char const * argv[]);
  333. *
  334. *--------------------------------------------------------------------*/
  335. int main (int argc, char const * argv [])
  336. {
  337. extern struct channel channel;
  338. static char const * optv [] =
  339. {
  340. &quot;cCdi:p:qs:t:vx&quot;,
  341. &quot;&quot;,
  342. &quot;Electric Vehicle Supply Equipment Emulator&quot;,
  343. &quot;c\tprint template configuration file on stdout&quot;,
  344. &quot;C\tstop on count mismatch&quot;,
  345. &quot;d\tdisplay debug information&quot;,
  346. #if defined (WINPCAP) || defined (LIBPCAP)
  347. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  348. #else
  349. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  350. #endif
  351. &quot;p s\tconfiguration profile is (s) [&quot; LITERAL (PROFILE) &quot;]&quot;,
  352. &quot;s s\tconfiguration section is (s) [&quot; LITERAL (SECTION) &quot;]&quot;,
  353. &quot;q\tquiet mode&quot;,
  354. &quot;t n\tread timeout is (n) milliseconds [&quot; LITERAL (SLAC_TIMEOUT) &quot;]&quot;,
  355. &quot;v\tverbose mode&quot;,
  356. &quot;x\texit on error&quot;,
  357. (char const *) (0)
  358. };
  359. struct session session;
  360. struct message message;
  361. char const * profile = PROFILE;
  362. char const * section = SECTION;
  363. signed c;
  364. memset (&amp; session, 0, sizeof (session));
  365. memset (&amp; message, 0, sizeof (message));
  366. channel.timeout = SLAC_TIMEOUT;
  367. if (getenv (PLCDEVICE))
  368. {
  369. #if defined (WINPCAP) || defined (LIBPCAP)
  370. channel.ifindex = atoi (getenv (PLCDEVICE));
  371. #else
  372. channel.ifname = strdup (getenv (PLCDEVICE));
  373. #endif
  374. }
  375. optind = 1;
  376. while (~ (c = getoptv (argc, argv, optv)))
  377. {
  378. switch (c)
  379. {
  380. case 'c':
  381. configure ();
  382. return (0);
  383. case 'C':
  384. _setbits (session.flags, SLAC_COMPARE);
  385. break;
  386. case 'd':
  387. _setbits (session.flags, (SLAC_VERBOSE | SLAC_SESSION));
  388. break;
  389. case 'i':
  390. #if defined (WINPCAP) || defined (LIBPCAP)
  391. channel.ifindex = atoi (optarg);
  392. #else
  393. channel.ifname = optarg;
  394. #endif
  395. break;
  396. case 'p':
  397. profile = optarg;
  398. break;
  399. case 's':
  400. section = optarg;
  401. break;
  402. case 'q':
  403. _setbits (channel.flags, CHANNEL_SILENCE);
  404. _setbits (session.flags, SLAC_SILENCE);
  405. break;
  406. case 't':
  407. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  408. break;
  409. case 'v':
  410. _setbits (channel.flags, CHANNEL_VERBOSE);
  411. break;
  412. case 'x':
  413. session.exit = session.exit? 0: 1;
  414. break;
  415. default:
  416. break;
  417. }
  418. }
  419. argc -= optind;
  420. argv += optind;
  421. if (argc)
  422. {
  423. slac_debug (&amp; session, 1, __func__, ERROR_TOOMANY);
  424. }
  425. openchannel (&amp; channel);
  426. initialize (&amp; session, profile, section);
  427. identifier (&amp; session, &amp; channel);
  428. if (evse_cm_set_key (&amp; session, &amp; channel, &amp; message))
  429. {
  430. slac_debug (&amp; session, 1, __func__, &quot;Can't set key.&quot;);
  431. }
  432. sleep (session.settletime);
  433. while (session.state)
  434. {
  435. if (session.state == EVSE_STATE_UNOCCUPIED)
  436. {
  437. UnoccupiedState (&amp; session, &amp; channel, &amp; message);
  438. continue;
  439. }
  440. if (session.state == EVSE_STATE_UNMATCHED)
  441. {
  442. UnmatchedState (&amp; session, &amp; channel, &amp; message);
  443. continue;
  444. }
  445. if (session.state == EVSE_STATE_MATCHED)
  446. {
  447. MatchedState (&amp; session, &amp; channel, &amp; message);
  448. continue;
  449. }
  450. slac_debug (&amp; session, 1, __func__, &quot;Illegal state!&quot;);
  451. }
  452. closechannel (&amp; channel);
  453. exit (0);
  454. }
  455. </pre>
  456. <div class='footerlink'>
  457. [<a href='EthernetHeader.c.html' title=' EthernetHeader.c '>PREV</a>]
  458. [<a href='toolkit.html' title=' Index '>HOME</a>]
  459. [<a href='evse_cm_atten_char.c.html' title=' evse_cm_atten_char.c '>NEXT</a>]
  460. </div>
  461. </body>
  462. </html>