diff --git a/hardware/firmware/src/main.cpp b/hardware/firmware/src/main.cpp index 54b4273..956aea9 100644 --- a/hardware/firmware/src/main.cpp +++ b/hardware/firmware/src/main.cpp @@ -46,9 +46,6 @@ static unsigned long s_cc2_last_change_ms = 0; static bool s_cc1_prev_plugged = false; static bool s_cc2_prev_plugged = false; -static bool s_key1_prev = false; -static bool s_key2_prev = false; - static bool s_auth_in_progress = false; static bool s_auth_ok = false; static unsigned long s_auth_ok_at_ms = 0; @@ -56,6 +53,18 @@ static String s_auth_id_tag; static bool s_remote_start_accepted = false; +static bool authWindowValid(); +static void clearAuthWait(const char *reason) +{ + if (s_auth_ok || s_auth_id_tag.length() > 0) + { + Serial.printf("[main] Clear pending authorization: %s\n", reason); + } + s_auth_ok = false; + s_auth_ok_at_ms = 0; + s_auth_id_tag = ""; +} + uint8_t mac[6]; char cpSerial[13]; @@ -118,7 +127,20 @@ static void updateConnectorPluggedState() } if ((now - s_cc1_last_change_ms) >= CC_DEBOUNCE_MS) { + bool cc1_plugged_old = s_cc1_plugged; s_cc1_plugged = s_cc1_raw_last; + + // If connector1 just connected and auth window is valid, try to start + if (!cc1_plugged_old && s_cc1_plugged && s_auth_ok && (millis() - s_auth_ok_at_ms) <= AUTH_WINDOW_MS && s_auth_id_tag.length() > 0 && isConnectorIdle(1)) + { + Serial.printf("[main] Connector 1 plugged in, auto-starting with idTag %s\n", s_auth_id_tag.c_str()); + auto tx = beginTransaction_authorized(s_auth_id_tag.c_str(), nullptr, 1); + if (tx != nullptr) + { + s_auth_ok = false; + s_auth_id_tag = ""; + } + } } const bool cc2_raw = (digitalRead(PIN_CC2) == HIGH); @@ -129,7 +151,20 @@ static void updateConnectorPluggedState() } if ((now - s_cc2_last_change_ms) >= CC_DEBOUNCE_MS) { + bool cc2_plugged_old = s_cc2_plugged; s_cc2_plugged = s_cc2_raw_last; + + // If connector2 just connected and auth window is valid, try to start + if (!cc2_plugged_old && s_cc2_plugged && s_auth_ok && (millis() - s_auth_ok_at_ms) <= AUTH_WINDOW_MS && s_auth_id_tag.length() > 0 && isConnectorIdle(2)) + { + Serial.printf("[main] Connector 2 plugged in, auto-starting with idTag %s\n", s_auth_id_tag.c_str()); + auto tx = beginTransaction_authorized(s_auth_id_tag.c_str(), nullptr, 2); + if (tx != nullptr) + { + s_auth_ok = false; + s_auth_id_tag = ""; + } + } } } @@ -182,9 +217,8 @@ static void requestAuthorizeByCard(const String &idTag) return; } + clearAuthWait("new card swiped"); s_auth_in_progress = true; - s_auth_ok = false; - s_auth_id_tag = ""; Serial.printf("[main] Authorize idTag: %s\n", idTag.c_str()); authorize( @@ -199,19 +233,40 @@ static void requestAuthorizeByCard(const String &idTag) s_auth_ok_at_ms = millis(); s_auth_id_tag = idTag; Serial.printf("[main] Authorize accepted for idTag %s\n", idTag.c_str()); + + // Check if there's already a connector plugged in; if so, start immediately + unsigned int targetConnector = 0; + if (s_cc1_plugged && isConnectorIdle(1) && isOperative(1)) + { + targetConnector = 1; + } + else if (s_cc2_plugged && isConnectorIdle(2) && isOperative(2)) + { + targetConnector = 2; + } + + if (targetConnector > 0) + { + // Immediately start the transaction on the plugged connector + Serial.printf("[main] Connector %u is plugged in, auto-starting transaction\n", targetConnector); + auto tx = beginTransaction_authorized(idTag.c_str(), nullptr, targetConnector); + if (tx != nullptr) + { + clearAuthWait("transaction started"); + } + } + // Otherwise, wait for a connector to be plugged in } else { - s_auth_ok = false; - s_auth_id_tag = ""; + clearAuthWait("authorize rejected"); Serial.printf("[main] Authorize rejected, status=%s\n", status); } }, []() { s_auth_in_progress = false; - s_auth_ok = false; - s_auth_id_tag = ""; + clearAuthWait("authorize aborted"); Serial.println("[main] Authorize aborted"); }); } @@ -240,49 +295,12 @@ static void pollRfidCard() requestAuthorizeByCard(idTag); } -static void tryStartByKey(unsigned int connectorId) +static void expireAuthWaitIfNeeded() { - if (!authWindowValid()) + if (s_auth_ok && s_auth_id_tag.length() > 0 && (millis() - s_auth_ok_at_ms) > AUTH_WINDOW_MS) { - Serial.println("[main] No valid authorization window. Swipe card first."); - return; + clearAuthWait("authorization timeout"); } - - if (!isConnectorStartReady(connectorId)) - { - Serial.printf("[main] Connector %u not ready for start (needs idle + operative + plugged).\n", connectorId); - return; - } - - auto tx = beginTransaction_authorized(s_auth_id_tag.c_str(), nullptr, connectorId); - if (tx) - { - Serial.printf("[main] Local start accepted on connector %u for idTag %s\n", connectorId, s_auth_id_tag.c_str()); - s_auth_ok = false; - s_auth_id_tag = ""; - } - else - { - Serial.printf("[main] Local start failed on connector %u\n", connectorId); - } -} - -static void pollStartKeys() -{ - const bool key1 = (digitalRead(PIN_KEY1) == HIGH); - const bool key2 = (digitalRead(PIN_KEY2) == HIGH); - - if (key1 && !s_key1_prev) - { - tryStartByKey(1); - } - if (key2 && !s_key2_prev) - { - tryStartByKey(2); - } - - s_key1_prev = key1; - s_key2_prev = key2; } /* LED Control Functions */ @@ -400,8 +418,6 @@ void setup() // Initialize CC switches (input) and panel LEDs (low-active output) pinMode(PIN_CC1, INPUT); pinMode(PIN_CC2, INPUT); - pinMode(PIN_KEY1, INPUT); - pinMode(PIN_KEY2, INPUT); pinMode(PIN_LED1, OUTPUT); pinMode(PIN_LED2, OUTPUT); pinMode(PIN_RELAY1, OUTPUT); @@ -756,7 +772,7 @@ void loop() updateConnectorPluggedState(); stopIfUnplugged(); pollRfidCard(); - pollStartKeys(); + expireAuthWaitIfNeeded(); mg_mgr_poll(&mgr, 10); mocpp_loop();