evse.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. * evse.c - QCA Electric Vehicle Supply Equipment Emulator;
  44. *
  45. * This program, in the current state, is not a finished product;
  46. * It has been released so that interested parties can begin to
  47. * see how the SLAC protocol might be implemented;
  48. *
  49. * Some key design features are:
  50. *
  51. * 1) the use of a channel variable to abstract ISO Layer 2 I/O;
  52. * the variable is used by functions openchannel, readmessage,
  53. * sendmessage and closechannel;
  54. *
  55. * 2) the use of a message variable to represent an IEEE 802.3
  56. * Ethernet frame; the variable allows one frame to be used
  57. * and re-used throughout the program but supports multiple
  58. * frame buffers if needed;
  59. *
  60. * 3) the use of a session variable to support multiple PEV-EVSE
  61. * interactions without using threads or subrocesses; this has
  62. * not demonstrated in this version of the program; some more
  63. * work is needed;
  64. *
  65. * 4) the absence of threads or subprocesses so that the program
  66. * can be ported to hosts without a multi-tasking operating
  67. * system;
  68. *
  69. * 5) lots of debugging messages; these can be suppressed or
  70. * deleted if not wanted;
  71. *
  72. *--------------------------------------------------------------------*/
  73. /*====================================================================*
  74. * system header files;
  75. *--------------------------------------------------------------------*/
  76. #include <unistd.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include <limits.h>
  80. #include <errno.h>
  81. #include <sys/time.h>
  82. /*====================================================================*
  83. * custom header files;
  84. *--------------------------------------------------------------------*/
  85. #include "../ether/channel.h"
  86. #include "../tools/getoptv.h"
  87. #include "../tools/putoptv.h"
  88. #include "../tools/memory.h"
  89. #include "../tools/number.h"
  90. #include "../tools/config.h"
  91. #include "../tools/types.h"
  92. #include "../tools/timer.h"
  93. #include "../tools/flags.h"
  94. #include "../tools/error.h"
  95. #include "../ether/channel.h"
  96. #include "../slac/slac.h"
  97. /*====================================================================*
  98. * custom source files;
  99. *--------------------------------------------------------------------*/
  100. #ifndef MAKEFILE
  101. #include "../tools/getoptv.c"
  102. #include "../tools/putoptv.c"
  103. #include "../tools/version.c"
  104. #include "../tools/uintspec.c"
  105. #include "../tools/hexdecode.c"
  106. #include "../tools/hexencode.c"
  107. #include "../tools/hexdump.c"
  108. #include "../tools/hexout.c"
  109. #include "../tools/hexstring.c"
  110. #include "../tools/decdecode.c"
  111. #include "../tools/decstring.c"
  112. #include "../tools/todigit.c"
  113. #include "../tools/strfbits.c"
  114. #include "../tools/error.c"
  115. #include "../tools/config.c"
  116. #endif
  117. #ifndef MAKEFILE
  118. #include "../ether/openchannel.c"
  119. #include "../ether/closechannel.c"
  120. #include "../ether/readpacket.c"
  121. #include "../ether/sendpacket.c"
  122. #include "../ether/channel.c"
  123. #endif
  124. #ifndef MAKEFILE
  125. #include "../plc/Devices.c"
  126. #endif
  127. #ifndef MAKEFILE
  128. #include "../mme/EthernetHeader.c"
  129. #include "../mme/QualcommHeader.c"
  130. #include "../mme/HomePlugHeader1.c"
  131. #include "../mme/UnwantedMessage.c"
  132. #include "../mme/readmessage.c"
  133. #include "../mme/sendmessage.c"
  134. #endif
  135. #ifndef MAKEFILE
  136. #include "../slac/slac_session.c"
  137. #include "../slac/slac_debug.c"
  138. #include "../slac/evse_cm_slac_param.c"
  139. #include "../slac/evse_cm_start_atten_char.c"
  140. #include "../slac/evse_cm_atten_char.c"
  141. #include "../slac/evse_cm_mnbc_sound.c"
  142. #include "../slac/evse_cm_slac_match.c"
  143. #include "../slac/evse_cm_set_key.c"
  144. #endif
  145. /*====================================================================*
  146. * program constants;
  147. *--------------------------------------------------------------------*/
  148. #define PLCDEVICE "PLC"
  149. #define PROFILE "evse.ini"
  150. #define SECTION "default"
  151. #define STATION ""
  152. #define EVSE_STATE_UNAVAILABLE 0
  153. #define EVSE_STATE_UNOCCUPIED 1
  154. #define EVSE_STATE_UNMATCHED 2
  155. #define EVSE_STATE_MATCHED 3
  156. #define EVSE_SID "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" // Station Identifier
  157. #define EVSE_NMK "B59319D7E8157BA001B018669CCEE30D" // HomePlugAV0123
  158. #define EVSE_NID "026BCBA5354E08" // HomePlugAV0123
  159. /*====================================================================*
  160. *
  161. * void configure ()
  162. *
  163. * print default EVSE-HLE configuration file on stdout so that the
  164. * profile, section and element names match;
  165. *
  166. *--------------------------------------------------------------------*/
  167. static void configure ()
  168. {
  169. printf ("# file: %s\n", PROFILE);
  170. printf ("# ====================================================================\n");
  171. printf ("# EVSE-HLE initiaization;\n");
  172. printf ("# --------------------------------------------------------------------\n");
  173. printf ("[%s]\n", SECTION);
  174. printf ("station identifier = %s\n", EVSE_SID);
  175. printf ("network membership key = %s\n", EVSE_NMK);
  176. printf ("network identifier = %s\n", EVSE_NID);
  177. printf ("number of sounds = %d\n", SLAC_MSOUNDS);
  178. printf ("time to sound = %d\n", SLAC_TIMETOSOUND);
  179. printf ("response type = %d\n", SLAC_RESPONSE_TYPE);
  180. printf ("settle time = %d\n", SLAC_SETTLETIME);
  181. printf ("charge time = %d\n", SLAC_CHARGETIME);
  182. return;
  183. }
  184. /*====================================================================*
  185. *
  186. * char void initialize (struct session * session, char const * profile, char const * section)
  187. *
  188. * read EVSE-HLE configuration profile; initialize session variable;
  189. *
  190. *--------------------------------------------------------------------*/
  191. static void initialize (struct session * session, char const * profile, char const * section)
  192. {
  193. session->next = session->prev = session;
  194. hexencode (session->EVSE_ID, sizeof (session->EVSE_ID), configstring (profile, section, "StationIdentifier", EVSE_SID));
  195. hexencode (session->NMK, sizeof (session->NMK), configstring (profile, section, "NetworkMembershipKey", EVSE_NMK));
  196. hexencode (session->NID, sizeof (session->NID), configstring (profile, section, "NetworkIdentifier", EVSE_NID));
  197. session->NUM_SOUNDS = confignumber_range (profile, section, "NumberOfSounds", SLAC_MSOUNDS, 0, UCHAR_MAX);
  198. session->TIME_OUT = confignumber_range (profile, section, "TimeToSound", SLAC_TIMETOSOUND, 0, UCHAR_MAX);
  199. session->RESP_TYPE = confignumber_range (profile, section, "ResponseType", SLAC_RESPONSE_TYPE, 0, UCHAR_MAX);
  200. session->chargetime = confignumber_range (profile, section, "ChargeTime", SLAC_CHARGETIME, 0, UINT_MAX);
  201. session->settletime = confignumber_range (profile, section, "SettleTime", SLAC_SETTLETIME, 0, UINT_MAX);
  202. memcpy (session->original_nmk, session->NMK, sizeof (session->original_nmk));
  203. memcpy (session->original_nid, session->NID, sizeof (session->original_nid));
  204. session->state = EVSE_STATE_UNOCCUPIED;
  205. slac_session (session);
  206. return;
  207. }
  208. /*====================================================================*
  209. *
  210. * signed identifier (struct session * session, struct channel * channel);
  211. *
  212. * copy channel host address to session EVSE MAC address; set session
  213. * EVSE identifier to zeros;
  214. *
  215. *--------------------------------------------------------------------*/
  216. static signed identifier (struct session * session, struct channel * channel)
  217. {
  218. memcpy (session->EVSE_MAC, channel->host, sizeof (session->EVSE_MAC));
  219. return (0);
  220. }
  221. /*====================================================================*
  222. *
  223. * void UnoccupiedState (struct session * session, struct channel * channel, struct message * message);
  224. *
  225. *--------------------------------------------------------------------*/
  226. static void UnoccupiedState (struct session * session, struct channel * channel, struct message * message)
  227. {
  228. slac_session (session);
  229. slac_debug (session, 0, __func__, "Listening ...");
  230. while (evse_cm_slac_param (session, channel, message));
  231. session->state = EVSE_STATE_UNMATCHED;
  232. return;
  233. }
  234. /*====================================================================*
  235. *
  236. * void MatchingState (struct session * session, struct channel * channel, struct message * message);
  237. *
  238. * the cm_start_atten_char message establishes msound count and
  239. * timeout;
  240. *
  241. *--------------------------------------------------------------------*/
  242. static void UnmatchedState (struct session * session, struct channel * channel, struct message * message)
  243. {
  244. slac_session (session);
  245. slac_debug (session, 0, __func__, "Sounding ...");
  246. if (evse_cm_start_atten_char (session, channel, message))
  247. {
  248. session->state = EVSE_STATE_UNOCCUPIED;
  249. return;
  250. }
  251. if (evse_cm_mnbc_sound (session, channel, message))
  252. {
  253. session->state = EVSE_STATE_UNOCCUPIED;
  254. return;
  255. }
  256. if (evse_cm_atten_char (session, channel, message))
  257. {
  258. session->state = EVSE_STATE_UNOCCUPIED;
  259. return;
  260. }
  261. if (_allset( session->flags, SLAC_SOUNDONLY))
  262. {
  263. session->state = EVSE_STATE_UNAVAILABLE;
  264. return;
  265. }
  266. slac_debug (session, 0, __func__, "Matching ...");
  267. if (evse_cm_slac_match (session, channel, message))
  268. {
  269. session->state = EVSE_STATE_UNOCCUPIED;
  270. return;
  271. }
  272. session->state = EVSE_STATE_MATCHED;
  273. return;
  274. }
  275. /*====================================================================*
  276. *
  277. * void MatchedState (struct session * session, struct channel * channel, struct message * message);
  278. *
  279. *--------------------------------------------------------------------*/
  280. static void MatchedState (struct session * session, struct channel * channel, struct message * message)
  281. {
  282. slac_debug (session, 0, __func__, "Connecting ...");
  283. #if SLAC_AVLN_EVSE
  284. if (evse_cm_set_key (session, channel, message))
  285. {
  286. session->state = EVSE_STATE_UNOCCUPIED;
  287. return;
  288. }
  289. sleep (session->settletime);
  290. #endif
  291. #if SLAC_AVLN_PEV
  292. slac_debug (session, 0, __func__, "waiting for pev to settle ...");
  293. sleep (session->settletime);
  294. #endif
  295. slac_debug (session, 0, __func__, "Charging (%d) ...\n\n", session->counter++);
  296. sleep (session->chargetime);
  297. slac_debug (session, 0, __func__, "Disconnecting ...");
  298. #if SLAC_AVLN_EVSE
  299. memcpy (session->NMK, session->original_nmk, sizeof (session->NMK));
  300. memcpy (session->NID, session->original_nid, sizeof (session->NID));
  301. if (evse_cm_set_key (session, channel, message))
  302. {
  303. session->state = EVSE_STATE_UNOCCUPIED;
  304. return;
  305. }
  306. sleep (session->settletime);
  307. #endif
  308. #if SLAC_AVLN_PEV
  309. slac_debug (session, 0, __func__, "waiting for pev to settle ...");
  310. sleep (session->settletime);
  311. #endif
  312. session->state = EVSE_STATE_UNOCCUPIED;
  313. return;
  314. }
  315. /*====================================================================*
  316. *
  317. * int main (int argc, char const * argv[]);
  318. *
  319. *--------------------------------------------------------------------*/
  320. int main (int argc, char const * argv [])
  321. {
  322. extern struct channel channel;
  323. static char const * optv [] =
  324. {
  325. "cCdi:Klp:qs:t:vx",
  326. "",
  327. "Qualcomm Atheros Electric Vehicle Supply Equipment Emulator",
  328. "c\tprint template configuration file on stdout",
  329. "C\tstop on count mismatch",
  330. "d\tdisplay debug information",
  331. #if defined (WINPCAP) || defined (LIBPCAP)
  332. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  333. #else
  334. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  335. #endif
  336. "K\tstop after sounding finished",
  337. "l\tdo not loop but exit after one run",
  338. "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
  339. "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
  340. "q\tquiet mode",
  341. "t n\tread timeout is (n) milliseconds [" LITERAL (SLAC_TIMEOUT) "]",
  342. "v\tverbose mode",
  343. "x\texit on error",
  344. (char const *) (0)
  345. };
  346. struct session session;
  347. struct message message;
  348. char const * profile = PROFILE;
  349. char const * section = SECTION;
  350. signed c;
  351. int dont_loop = 0;
  352. memset (& session, 0, sizeof (session));
  353. memset (& message, 0, sizeof (message));
  354. channel.timeout = SLAC_TIMEOUT;
  355. if (getenv (PLCDEVICE))
  356. {
  357. #if defined (WINPCAP) || defined (LIBPCAP)
  358. channel.ifindex = atoi (getenv (PLCDEVICE));
  359. #else
  360. channel.ifname = strdup (getenv (PLCDEVICE));
  361. #endif
  362. }
  363. optind = 1;
  364. while (~ (c = getoptv (argc, argv, optv)))
  365. {
  366. switch (c)
  367. {
  368. case 'c':
  369. configure ();
  370. return (0);
  371. case 'C':
  372. _setbits (session.flags, SLAC_COMPARE);
  373. break;
  374. case 'd':
  375. _setbits (session.flags, (SLAC_VERBOSE | SLAC_SESSION));
  376. break;
  377. case 'i':
  378. #if defined (WINPCAP) || defined (LIBPCAP)
  379. channel.ifindex = atoi (optarg);
  380. #else
  381. channel.ifname = optarg;
  382. #endif
  383. break;
  384. case 'K':
  385. _setbits (session.flags, SLAC_SOUNDONLY);
  386. break;
  387. case 'l':
  388. dont_loop = 1;
  389. break;
  390. case 'p':
  391. profile = optarg;
  392. break;
  393. case 's':
  394. section = optarg;
  395. break;
  396. case 'q':
  397. _setbits (channel.flags, CHANNEL_SILENCE);
  398. _setbits (session.flags, SLAC_SILENCE);
  399. break;
  400. case 't':
  401. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  402. break;
  403. case 'v':
  404. _setbits (channel.flags, CHANNEL_VERBOSE);
  405. break;
  406. case 'x':
  407. session.exit = session.exit? 0: 1;
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. argc -= optind;
  414. argv += optind;
  415. if (argc)
  416. {
  417. slac_debug (& session, 1, __func__, ERROR_TOOMANY);
  418. }
  419. openchannel (& channel);
  420. initialize (& session, profile, section);
  421. identifier (& session, & channel);
  422. if (_allclr (session.flags, SLAC_SOUNDONLY))
  423. {
  424. if (evse_cm_set_key (& session, & channel, & message))
  425. {
  426. slac_debug (& session, 1, __func__, "Can't set key.");
  427. }
  428. sleep (session.settletime);
  429. }
  430. while (session.state)
  431. {
  432. if (session.state == EVSE_STATE_UNOCCUPIED)
  433. {
  434. UnoccupiedState (& session, & channel, & message);
  435. continue;
  436. }
  437. if (session.state == EVSE_STATE_UNMATCHED)
  438. {
  439. UnmatchedState (& session, & channel, & message);
  440. if (dont_loop && session.state == EVSE_STATE_UNAVAILABLE)
  441. {
  442. break;
  443. }
  444. continue;
  445. }
  446. if (session.state == EVSE_STATE_MATCHED)
  447. {
  448. MatchedState (& session, & channel, & message);
  449. if (dont_loop)
  450. {
  451. break;
  452. }
  453. continue;
  454. }
  455. slac_debug (& session, 1, __func__, "Illegal state!");
  456. }
  457. closechannel (& channel);
  458. exit (0);
  459. }