pev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. * pev.c - QCA Plug-in Electric Vehicle 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. * 6) simplified state machine;
  73. *
  74. *--------------------------------------------------------------------*/
  75. /*====================================================================*
  76. * system header files;
  77. *--------------------------------------------------------------------*/
  78. #include <unistd.h>
  79. #include <stdlib.h>
  80. #include <limits.h>
  81. #include <string.h>
  82. #include <errno.h>
  83. #include <time.h>
  84. /*====================================================================*
  85. * custom header files;
  86. *--------------------------------------------------------------------*/
  87. #include "../tools/getoptv.h"
  88. #include "../tools/putoptv.h"
  89. #include "../tools/memory.h"
  90. #include "../tools/number.h"
  91. #include "../tools/types.h"
  92. #include "../tools/flags.h"
  93. #include "../tools/files.h"
  94. #include "../tools/error.h"
  95. #include "../tools/config.h"
  96. #include "../ether/channel.h"
  97. #include "../slac/slac.h"
  98. /*====================================================================*
  99. * custom source files;
  100. *--------------------------------------------------------------------*/
  101. #ifndef MAKEFILE
  102. #include "../tools/getoptv.c"
  103. #include "../tools/putoptv.c"
  104. #include "../tools/version.c"
  105. #include "../tools/hexdump.c"
  106. #include "../tools/hexdecode.c"
  107. #include "../tools/hexencode.c"
  108. #include "../tools/hexstring.c"
  109. #include "../tools/decdecode.c"
  110. #include "../tools/decstring.c"
  111. #include "../tools/uintspec.c"
  112. #include "../tools/todigit.c"
  113. #include "../tools/strfbits.c"
  114. #include "../tools/config.c"
  115. #include "../tools/memincr.c"
  116. #include "../tools/error.c"
  117. #endif
  118. #ifndef MAKEFILE
  119. #include "../plc/Devices.c"
  120. #endif
  121. #ifndef MAKEFILE
  122. #include "../mme/EthernetHeader.c"
  123. #include "../mme/QualcommHeader.c"
  124. #include "../mme/HomePlugHeader1.c"
  125. #include "../mme/UnwantedMessage.c"
  126. #include "../mme/readmessage.c"
  127. #include "../mme/sendmessage.c"
  128. #endif
  129. #ifndef MAKEFILE
  130. #include "../ether/channel.c"
  131. #include "../ether/openchannel.c"
  132. #include "../ether/closechannel.c"
  133. #include "../ether/sendpacket.c"
  134. #include "../ether/readpacket.c"
  135. #endif
  136. #ifndef MAKEFILE
  137. #include "../slac/slac_session.c"
  138. #include "../slac/slac_connect.c"
  139. #include "../slac/slac_debug.c"
  140. #include "../slac/pev_cm_slac_param.c"
  141. #include "../slac/pev_cm_start_atten_char.c"
  142. #include "../slac/pev_cm_atten_char.c"
  143. #include "../slac/pev_cm_mnbc_sound.c"
  144. #include "../slac/pev_cm_slac_match.c"
  145. #include "../slac/pev_cm_set_key.c"
  146. #endif
  147. /*====================================================================*
  148. * program constants;
  149. *--------------------------------------------------------------------*/
  150. #define PLCDEVICE "PLC"
  151. #define PROFILE "pev.ini"
  152. #define SECTION "default"
  153. #define PEV_STATE_NONE 0
  154. #define PEV_STATE_DISCONNECTED 1
  155. #define PEV_STATE_UNMATCHED 2
  156. #define PEV_STATE_MATCHED 3
  157. #define PEV_VID "0000000000000000000000000000000000" // VehicleIdentifier
  158. #define PEV_NMK "50D3E4933F855B7040784DF815AA8DB7" // HomePlugAV
  159. #define PEV_NID "B0F2E695666B03" // HomePlugAV
  160. /*====================================================================*
  161. * program variables;
  162. *--------------------------------------------------------------------*/
  163. unsigned state = 0;
  164. /*====================================================================*
  165. *
  166. * static void configure ();
  167. *
  168. * print template PEV-HLE configuration file on stdout so that
  169. * profile, section and element names match;
  170. *
  171. *--------------------------------------------------------------------*/
  172. static void configure ()
  173. {
  174. printf ("# file: %s\n", PROFILE);
  175. printf ("# ====================================================================\n");
  176. printf ("# PEV-HLE initialization;\n");
  177. printf ("# --------------------------------------------------------------------\n");
  178. printf ("[%s]\n", SECTION);
  179. printf ("vehicle identifier = %s\n", PEV_VID);
  180. printf ("network membership key = %s\n", PEV_NMK);
  181. printf ("network identifier = %s\n", PEV_NID);
  182. printf ("attenuation threshold = %d\n", SLAC_LIMIT);
  183. printf ("msound pause = %d\n", SLAC_PAUSE);
  184. printf ("charge time = %d\n", SLAC_CHARGETIME);
  185. printf ("settle time = %d\n", SLAC_SETTLETIME);
  186. return;
  187. }
  188. /*====================================================================*
  189. *
  190. * void initialize (struct session * session, char const * profile, char const * section);
  191. *
  192. * read PEV-HLE configuration profile; initialize session variable;
  193. *
  194. *--------------------------------------------------------------------*/
  195. static void initialize (struct session * session, char const * profile, char const * section)
  196. {
  197. session->next = session->prev = session;
  198. hexencode (session->PEV_ID, sizeof (session->PEV_ID), configstring (profile, section, "VehicleIdentifier", PEV_VID));
  199. hexencode (session->NMK, sizeof (session->NMK), configstring (profile, section, "NetworkMembershipKey", PEV_NMK));
  200. hexencode (session->NID, sizeof (session->NID), configstring (profile, section, "NetworkIdentifier", PEV_NID));
  201. session->limit = confignumber_range (profile, section, "AttenuationThreshold", SLAC_LIMIT, 0, UINT_MAX);
  202. session->pause = confignumber_range (profile, section, "MSoundPause", SLAC_PAUSE, 0, UINT_MAX);
  203. session->settletime = confignumber_range (profile, section, "SettleTime", SLAC_SETTLETIME, 0, UINT_MAX);
  204. session->chargetime = confignumber_range (profile, section, "ChargeTime", SLAC_CHARGETIME, 0, UINT_MAX);
  205. session->state = PEV_STATE_DISCONNECTED;
  206. memcpy (session->original_nmk, session->NMK, sizeof (session->original_nmk));
  207. memcpy (session->original_nid, session->NID, sizeof (session->original_nid));
  208. slac_session (session);
  209. return;
  210. }
  211. /*====================================================================*
  212. *
  213. * signed identifier (struct session * session, struct channel * channel);
  214. *
  215. * generate the run identifier and store in session variable;
  216. *
  217. * copy channel host address to session PEV MAC address; set session
  218. * PEV identifier to zeros;
  219. *
  220. *--------------------------------------------------------------------*/
  221. static signed identifier (struct session * session, struct channel * channel)
  222. {
  223. time_t now;
  224. time (& now);
  225. memset (session->RunID, 0, sizeof (session->RunID));
  226. memcpy (session->RunID, channel->host, ETHER_ADDR_LEN);
  227. memcpy (session->PEV_MAC, channel->host, sizeof (session->PEV_MAC));
  228. return (0);
  229. }
  230. /*====================================================================*
  231. *
  232. * void DisconnectedState (struct session * session, struct channel * channel, struct message * message);
  233. *
  234. *--------------------------------------------------------------------*/
  235. static void DisconnectedState (struct session * session, struct channel * channel, struct message * message)
  236. {
  237. slac_session (session);
  238. slac_debug (session, 0, __func__, "Probing ...");
  239. memincr (session->RunID, sizeof (session->RunID));
  240. while (pev_cm_slac_param (session, channel, message));
  241. session->state = PEV_STATE_UNMATCHED;
  242. return;
  243. }
  244. /*====================================================================*
  245. *
  246. * void MatchingState (struct session * session, struct channel * channel, struct message * message);
  247. *
  248. * The PEV-EVSE perform GreenPPEA protocol in this state;
  249. *
  250. * the cm_start_atten_char and cm_mnbc_sound messages are sent
  251. * broadcast; the application may receive multiple cm_atten_char
  252. * messages before sending the cm_slac_match message;
  253. *
  254. *--------------------------------------------------------------------*/
  255. static void UnmatchedState (struct session * session, struct channel * channel, struct message * message)
  256. {
  257. slac_session (session);
  258. slac_debug (session, 0, __func__, "Sounding ...");
  259. if (pev_cm_start_atten_char (session, channel, message))
  260. {
  261. session->state = PEV_STATE_DISCONNECTED;
  262. return;
  263. }
  264. if (pev_cm_mnbc_sound (session, channel, message))
  265. {
  266. session->state = PEV_STATE_DISCONNECTED;
  267. return;
  268. }
  269. if (pev_cm_atten_char (session, channel, message))
  270. {
  271. session->state = PEV_STATE_DISCONNECTED;
  272. return;
  273. }
  274. if (slac_connect (session))
  275. {
  276. session->state = PEV_STATE_DISCONNECTED;
  277. return;
  278. }
  279. if (_allset( session->flags, SLAC_SOUNDONLY))
  280. {
  281. session->state = PEV_STATE_NONE;
  282. return;
  283. }
  284. slac_debug (session, 0, __func__, "Matching ...");
  285. if (pev_cm_slac_match (session, channel, message))
  286. {
  287. session->state = PEV_STATE_DISCONNECTED;
  288. return;
  289. }
  290. session->state = PEV_STATE_MATCHED;
  291. return;
  292. }
  293. /*====================================================================*
  294. *
  295. * void MatchedState (struct session * session, struct channel * channel, struct message * message);
  296. *
  297. * charge vehicle; restore original NMK/NID and disconnect; loop
  298. * if SLAC_CONTINUE is set;
  299. *
  300. *--------------------------------------------------------------------*/
  301. static void MatchedState (struct session * session, struct channel * channel, struct message * message)
  302. {
  303. slac_session (session);
  304. slac_debug (session, 0, __func__, "Connecting ...");
  305. #if SLAC_AVLN_EVSE
  306. slac_debug (session, 0, __func__, "waiting for evse to settle ...");
  307. sleep (session->settletime);
  308. #endif
  309. #if SLAC_AVLN_PEV
  310. if (pev_cm_set_key (session, channel, message))
  311. {
  312. session->state = PEV_STATE_DISCONNECTED;
  313. return;
  314. }
  315. sleep (session->settletime);
  316. #endif
  317. slac_debug (session, 0, __func__, "Charging (%d) ...\n\n", session->counter++);
  318. sleep (session->chargetime);
  319. slac_debug (session, 0, __func__, "Disconnecting ...");
  320. #if SLAC_AVLN_EVSE
  321. slac_debug (session, 0, __func__, "waiting for evse to settle ...");
  322. sleep (session->settletime);
  323. #endif
  324. #if SLAC_AVLN_PEV
  325. memcpy (session->NMK, session->original_nmk, sizeof (session->NMK));
  326. memcpy (session->NID, session->original_nid, sizeof (session->NID));
  327. if (pev_cm_set_key (session, channel, message))
  328. {
  329. session->state = PEV_STATE_DISCONNECTED;
  330. return;
  331. }
  332. sleep (session->settletime);
  333. #endif
  334. session->state = state;
  335. return;
  336. }
  337. /*====================================================================*
  338. *
  339. * int main (int argc, char * argv[]);
  340. *
  341. *
  342. *--------------------------------------------------------------------*/
  343. int main (int argc, char const * argv [])
  344. {
  345. extern struct channel channel;
  346. static char const * optv [] =
  347. {
  348. "cCdi:Klp:qs:t:vx",
  349. "",
  350. "Qualcomm Atheros Plug-in Electric Vehicle Emulator",
  351. "c\tprint template configuration file on stdout",
  352. "C\tstop on count mismatch",
  353. "d\tdisplay debug information",
  354. #if defined (WINPCAP) || defined (LIBPCAP)
  355. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  356. #else
  357. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  358. #endif
  359. "K\tstop after sounding finished",
  360. "l\tloop indefinitely",
  361. "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
  362. "q\tsuppress normal output",
  363. "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
  364. "t n\tread timeout is (n) milliseconds [" LITERAL (SLAC_TIMEOUT) "]",
  365. "v\tverbose messages on stdout",
  366. "x\texit on error",
  367. (char const *) (0)
  368. };
  369. struct session session;
  370. struct message message;
  371. char const * profile = PROFILE;
  372. char const * section = SECTION;
  373. signed c;
  374. memset (& session, 0, sizeof (session));
  375. memset (& message, 0, sizeof (message));
  376. channel.timeout = SLAC_TIMEOUT;
  377. if (getenv (PLCDEVICE))
  378. {
  379. #if defined (WINPCAP) || defined (LIBPCAP)
  380. channel.ifindex = atoi (getenv (PLCDEVICE));
  381. #else
  382. channel.ifname = strdup (getenv (PLCDEVICE));
  383. #endif
  384. }
  385. optind = 1;
  386. while (~ (c = getoptv (argc, argv, optv)))
  387. {
  388. switch (c)
  389. {
  390. case 'c':
  391. configure ();
  392. return (0);
  393. case 'C':
  394. _setbits (session.flags, SLAC_COMPARE);
  395. break;
  396. case 'd':
  397. _setbits (session.flags, (SLAC_VERBOSE | SLAC_SESSION));
  398. break;
  399. case 'i':
  400. #if defined (WINPCAP) || defined (LIBPCAP)
  401. channel.ifindex = atoi (optarg);
  402. #else
  403. channel.ifname = optarg;
  404. #endif
  405. break;
  406. case 'K':
  407. _setbits (session.flags, SLAC_SOUNDONLY);
  408. break;
  409. case 'l':
  410. state = PEV_STATE_DISCONNECTED;
  411. break;
  412. case 'p':
  413. profile = optarg;
  414. break;
  415. case 's':
  416. section = optarg;
  417. break;
  418. case 'q':
  419. _setbits (channel.flags, CHANNEL_SILENCE);
  420. _setbits (session.flags, SLAC_SILENCE);
  421. break;
  422. case 't':
  423. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  424. break;
  425. case 'v':
  426. _setbits (channel.flags, CHANNEL_VERBOSE);
  427. break;
  428. case 'x':
  429. session.exit = session.exit? 0: 1;
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. argc -= optind;
  436. argv += optind;
  437. if (argc)
  438. {
  439. slac_debug (& session, 1, __func__, ERROR_TOOMANY);
  440. }
  441. openchannel (& channel);
  442. identifier (& session, & channel);
  443. initialize (& session, profile, section);
  444. if (_allclr (session.flags, SLAC_SOUNDONLY))
  445. {
  446. if (pev_cm_set_key (& session, & channel, & message))
  447. {
  448. slac_debug (& session, 1, __func__, "Can't set key");
  449. }
  450. sleep (session.settletime);
  451. }
  452. while (session.state)
  453. {
  454. if (session.state == PEV_STATE_DISCONNECTED)
  455. {
  456. DisconnectedState (& session, & channel, & message);
  457. continue;
  458. }
  459. if (session.state == PEV_STATE_UNMATCHED)
  460. {
  461. UnmatchedState (& session, & channel, & message);
  462. continue;
  463. }
  464. if (session.state == PEV_STATE_MATCHED)
  465. {
  466. MatchedState (& session, & channel, & message);
  467. continue;
  468. }
  469. slac_debug (& session, 1, __func__, "Illegal state!");
  470. }
  471. closechannel (& channel);
  472. return (0);
  473. }