evse.c 14 KB

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