Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_compile_definitions(
MO_FILENAME_PREFIX="./mo_store/"
MO_ENABLE_V201=1
MO_ENABLE_MBEDTLS=1
MO_ENABLE_TIMESTAMP_MILLISECONDS=1
)

add_executable(mo_simulator ${MO_SIM_SRC} ${MO_SIM_MG_SRC})
Expand Down
2 changes: 1 addition & 1 deletion lib/MicroOcpp
Submodule MicroOcpp updated 236 files
2 changes: 1 addition & 1 deletion lib/MicroOcppMongoose
63 changes: 14 additions & 49 deletions src/evse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ Evse::Evse(unsigned int connectorId) : connectorId{connectorId} {

}

MicroOcpp::Connector *getConnector(unsigned int connectorId) {
if (!getOcppContext()) {
MO_DBG_ERR("unitialized");
return nullptr;
}
return getOcppContext()->getModel().getConnector(connectorId);
}

void Evse::setup() {

#if MO_ENABLE_V201
Expand All @@ -43,12 +35,6 @@ void Evse::setup() {
}
#endif

auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return;
}

char key [30] = {'\0'};

snprintf(key, 30, "evPlugged_cId_%u", connectorId);
Expand Down Expand Up @@ -134,12 +120,11 @@ void Evse::setup() {
}

void Evse::loop() {
if (auto connector = getConnector(connectorId)) {
auto curStatus = connector->getStatus();

if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
status = MicroOcpp::cstrFromOcppEveState(curStatus);
}
auto curStatus = getChargePointStatus(connectorId);

if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
status = MicroOcpp::cstrFromOcppEveState(curStatus);
}

bool simulate_isCharging = ocppPermitsCharge(connectorId) && trackEvPluggedBool->getBool() && trackEvsePluggedBool->getBool() && trackEvReadyBool->getBool() && trackEvseReadyBool->getBool();
Expand Down Expand Up @@ -167,21 +152,16 @@ void Evse::presentNfcTag(const char *uid_cstr) {
return;
}
std::string uid = uid_cstr;
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return;
}

#if MO_ENABLE_V201
if (auto context = getOcppContext()) {
if (context->getVersion().major == 2) {
if (auto txService = context->getModel().getTransactionService()) {
if (auto evse = txService->getEvse(connectorId)) {
if (evse->getTransaction() && evse->getTransaction()->isAuthorized) {
evse->endAuthorization(uid_cstr);
if (evse->getTransaction() && evse->getTransaction()->isAuthorizationActive) {
evse->endAuthorization(MicroOcpp::IdToken(uid_cstr, MicroOcpp::IdToken::Type::KeyCode));
} else {
evse->beginAuthorization(uid_cstr);
evse->beginAuthorization(MicroOcpp::IdToken(uid_cstr, MicroOcpp::IdToken::Type::KeyCode));
}
return;
}
Expand All @@ -190,14 +170,14 @@ void Evse::presentNfcTag(const char *uid_cstr) {
}
#endif

if (connector->getTransaction() && connector->getTransaction()->isActive()) {
if (!uid.compare(connector->getTransaction()->getIdTag())) {
connector->endTransaction(uid.c_str());
if (isTransactionActive(connectorId)) {
if (!uid.compare(getTransactionIdTag(connectorId))) {
endTransaction(uid.c_str(), "Local", connectorId);
} else {
MO_DBG_INFO("RFID card denied");
}
} else {
connector->beginTransaction(uid.c_str());
beginTransaction(uid.c_str(), connectorId);
}
}

Expand Down Expand Up @@ -246,30 +226,15 @@ bool Evse::getEvseReady() {
}

const char *Evse::getSessionIdTag() {
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return nullptr;
}
return connector->getTransaction() ? connector->getTransaction()->getIdTag() : nullptr;
return getTransactionIdTag(connectorId) ? getTransactionIdTag(connectorId) : "";
}

int Evse::getTransactionId() {
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return -1;
}
return connector->getTransaction() ? connector->getTransaction()->getTransactionId() : -1;
return getTransaction(connectorId) ? getTransaction(connectorId)->getTransactionId() : -1;
}

bool Evse::chargingPermitted() {
auto connector = getConnector(connectorId);
if (!connector) {
MO_DBG_ERR("invalid state");
return false;
}
return connector->ocppPermitsCharge();
return ocppPermitsCharge(connectorId);
}

int Evse::getPower() {
Expand Down
69 changes: 68 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
// GPL-3.0 License

#include <iostream>
#include <signal.h>

#include <mbedtls/platform.h>

#include <MicroOcpp.h>
#include <MicroOcpp/Core/Context.h>
#include "evse.h"
#include "api.h"

#include <MicroOcpp/Core/Memory.h>

#if MO_NUMCONNECTORS == 3
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1,2}};
#else
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1}};
#endif

bool g_isOcpp201 = false;
bool g_runSimulator = true;

bool g_isUpAndRunning = false; //if the initial BootNotification and StatusNotifications got through + 1s delay
unsigned int g_bootNotificationTime = 0;

#define MO_NETLIB_MONGOOSE 1
#define MO_NETLIB_WASM 2
Expand Down Expand Up @@ -42,6 +52,31 @@ MicroOcpp::Connection *conn = nullptr;
#error Please ensure that build flag MO_NETLIB is set as MO_NETLIB_MONGOOSE or MO_NETLIB_WASM
#endif

#if MBEDTLS_PLATFORM_MEMORY //configure MbedTLS with allocation hook functions

void *mo_mem_mbedtls_calloc( size_t n, size_t count ) {
size_t size = n * count;
auto ptr = MO_MALLOC("MbedTLS", size);
if (ptr) {
memset(ptr, 0, size);
}
return ptr;
}
void mo_mem_mbedtls_free( void *ptr ) {
MO_FREE(ptr);
}

#endif //MBEDTLS_PLATFORM_MEMORY

void mo_sim_sig_handler(int s){

if (!g_runSimulator) { //already tried to shut down, now force stop
exit(EXIT_FAILURE);
}

g_runSimulator = false; //shut down simulator gracefully
}

/*
* Setup MicroOcpp and API
*/
Expand Down Expand Up @@ -94,6 +129,17 @@ void app_loop() {
#if MO_NETLIB == MO_NETLIB_MONGOOSE

int main() {

#if MBEDTLS_PLATFORM_MEMORY
mbedtls_platform_set_calloc_free(mo_mem_mbedtls_calloc, mo_mem_mbedtls_free);
#endif //MBEDTLS_PLATFORM_MEMORY

struct sigaction sigIntHandler;
sigIntHandler.sa_handler = mo_sim_sig_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);

mg_log_set(MG_LL_INFO);
mg_mgr_init(&mgr);

Expand All @@ -117,11 +163,32 @@ int main() {
server_initialize(osock);
app_setup(*osock, filesystem);

for (;;) { // Block forever
setOnResetExecute([] (bool isHard) {
g_runSimulator = false;
});

while (g_runSimulator) { //Run Simulator until OCPP Reset is executed or user presses Ctrl+C
mg_mgr_poll(&mgr, 100);
app_loop();

if (!g_bootNotificationTime && getOcppContext()->getModel().getClock().now() >= MicroOcpp::MIN_TIME) {
//time has been set, BootNotification succeeded
g_bootNotificationTime = mocpp_tick_ms();
}

if (!g_isUpAndRunning && g_bootNotificationTime && mocpp_tick_ms() - g_bootNotificationTime >= 1000) {
printf("[Sim] Resetting maximum heap usage after boot success\n");
g_isUpAndRunning = true;
MO_MEM_RESET();
}
}

printf("[Sim] Shutting down Simulator\n");

MO_MEM_PRINT_STATS();

mocpp_deinitialize();

delete osock;
mg_mgr_free(&mgr);
return 0;
Expand Down
Loading