Skip to content

Commit 5de5447

Browse files
authored
Fix crash when initialized without FS access (#380)
1 parent 4b89f46 commit 5de5447

File tree

4 files changed

+79
-27
lines changed

4 files changed

+79
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
- Build flag `MO_REPORT_NOERROR` to report error recovery ([#331](https://github.com/matth-x/MicroOcpp/pull/331))
2323
- Support for `parentIdTag` ([#344](https://github.com/matth-x/MicroOcpp/pull/344))
2424
- Input validation for unsigned int Configs ([#344](https://github.com/matth-x/MicroOcpp/pull/344))
25-
- Support for TransactionMessageAttempts/-RetryInterval ([#345](https://github.com/matth-x/MicroOcpp/pull/345))
25+
- Support for TransactionMessageAttempts/-RetryInterval ([#345](https://github.com/matth-x/MicroOcpp/pull/345), [#380](https://github.com/matth-x/MicroOcpp/pull/380))
2626
- Heap profiler and custom allocator support ([#350](https://github.com/matth-x/MicroOcpp/pull/350))
2727
- Migration of persistent storage ([#355](https://github.com/matth-x/MicroOcpp/pull/355))
2828
- Benchmarks pipeline ([#369](https://github.com/matth-x/MicroOcpp/pull/369), [#376](https://github.com/matth-x/MicroOcpp/pull/376))

src/MicroOcpp/Model/ConnectorBase/Connector.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,37 +81,39 @@ Connector::Connector(Context& context, std::shared_ptr<FilesystemAdapter> filesy
8181

8282
unsigned int txNrPivot = std::numeric_limits<unsigned int>::max();
8383

84-
filesystem->ftw_root([this, txFnamePrefix, txFnamePrefixLen, &txNrPivot] (const char *fname) {
85-
if (!strncmp(fname, txFnamePrefix, txFnamePrefixLen)) {
86-
unsigned int parsedTxNr = 0;
87-
for (size_t i = txFnamePrefixLen; fname[i] >= '0' && fname[i] <= '9'; i++) {
88-
parsedTxNr *= 10;
89-
parsedTxNr += fname[i] - '0';
90-
}
91-
92-
if (txNrPivot == std::numeric_limits<unsigned int>::max()) {
93-
txNrPivot = parsedTxNr;
94-
txNrBegin = parsedTxNr;
95-
txNrEnd = (parsedTxNr + 1) % MAX_TX_CNT;
96-
return 0;
97-
}
84+
if (filesystem) {
85+
filesystem->ftw_root([this, txFnamePrefix, txFnamePrefixLen, &txNrPivot] (const char *fname) {
86+
if (!strncmp(fname, txFnamePrefix, txFnamePrefixLen)) {
87+
unsigned int parsedTxNr = 0;
88+
for (size_t i = txFnamePrefixLen; fname[i] >= '0' && fname[i] <= '9'; i++) {
89+
parsedTxNr *= 10;
90+
parsedTxNr += fname[i] - '0';
91+
}
9892

99-
if ((parsedTxNr + MAX_TX_CNT - txNrPivot) % MAX_TX_CNT < MAX_TX_CNT / 2) {
100-
//parsedTxNr is after pivot point
101-
if ((parsedTxNr + 1 + MAX_TX_CNT - txNrPivot) % MAX_TX_CNT > (txNrEnd + MAX_TX_CNT - txNrPivot) % MAX_TX_CNT) {
93+
if (txNrPivot == std::numeric_limits<unsigned int>::max()) {
94+
txNrPivot = parsedTxNr;
95+
txNrBegin = parsedTxNr;
10296
txNrEnd = (parsedTxNr + 1) % MAX_TX_CNT;
97+
return 0;
10398
}
104-
} else if ((txNrPivot + MAX_TX_CNT - parsedTxNr) % MAX_TX_CNT < MAX_TX_CNT / 2) {
105-
//parsedTxNr is before pivot point
106-
if ((txNrPivot + MAX_TX_CNT - parsedTxNr) % MAX_TX_CNT > (txNrPivot + MAX_TX_CNT - txNrBegin) % MAX_TX_CNT) {
107-
txNrBegin = parsedTxNr;
99+
100+
if ((parsedTxNr + MAX_TX_CNT - txNrPivot) % MAX_TX_CNT < MAX_TX_CNT / 2) {
101+
//parsedTxNr is after pivot point
102+
if ((parsedTxNr + 1 + MAX_TX_CNT - txNrPivot) % MAX_TX_CNT > (txNrEnd + MAX_TX_CNT - txNrPivot) % MAX_TX_CNT) {
103+
txNrEnd = (parsedTxNr + 1) % MAX_TX_CNT;
104+
}
105+
} else if ((txNrPivot + MAX_TX_CNT - parsedTxNr) % MAX_TX_CNT < MAX_TX_CNT / 2) {
106+
//parsedTxNr is before pivot point
107+
if ((txNrPivot + MAX_TX_CNT - parsedTxNr) % MAX_TX_CNT > (txNrPivot + MAX_TX_CNT - txNrBegin) % MAX_TX_CNT) {
108+
txNrBegin = parsedTxNr;
109+
}
108110
}
109-
}
110111

111-
MO_DBG_DEBUG("found %s%u.jsn - Internal range from %u to %u (exclusive)", txFnamePrefix, parsedTxNr, txNrBegin, txNrEnd);
112-
}
113-
return 0;
114-
});
112+
MO_DBG_DEBUG("found %s%u.jsn - Internal range from %u to %u (exclusive)", txFnamePrefix, parsedTxNr, txNrBegin, txNrEnd);
113+
}
114+
return 0;
115+
});
116+
}
115117

116118
MO_DBG_DEBUG("found %u transactions for connector %u. Internal range from %u to %u (exclusive)", (txNrEnd + MAX_TX_CNT - txNrBegin) % MAX_TX_CNT, connectorId, txNrBegin, txNrEnd);
117119
txNrFront = txNrBegin;

tests/ChargingSessions.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,5 +1228,54 @@ TEST_CASE( "Charging sessions" ) {
12281228
REQUIRE( checkProcessed );
12291229
}
12301230

1231+
SECTION("No filesystem access behavior") {
1232+
1233+
//re-init without filesystem access
1234+
mocpp_deinitialize();
1235+
1236+
mocpp_initialize(loopback, ChargerCredentials(), MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Deactivate));
1237+
mocpp_set_timer(custom_timer_cb);
1238+
1239+
loop();
1240+
1241+
REQUIRE( getChargePointStatus() == ChargePointStatus_Available );
1242+
REQUIRE( !ocppPermitsCharge() );
1243+
1244+
for (size_t i = 0; i < 3; i++) {
1245+
1246+
beginTransaction("mIdTag");
1247+
loop();
1248+
1249+
REQUIRE( getChargePointStatus() == ChargePointStatus_Charging );
1250+
REQUIRE( ocppPermitsCharge() );
1251+
1252+
endTransaction();
1253+
loop();
1254+
1255+
REQUIRE( getChargePointStatus() == ChargePointStatus_Available );
1256+
REQUIRE( !ocppPermitsCharge() );
1257+
}
1258+
1259+
//Tx status will be lost over reboot
1260+
1261+
beginTransaction("mIdTag");
1262+
loop();
1263+
1264+
REQUIRE( getChargePointStatus() == ChargePointStatus_Charging );
1265+
REQUIRE( ocppPermitsCharge() );
1266+
1267+
mocpp_deinitialize();
1268+
1269+
mocpp_initialize(loopback, ChargerCredentials(), MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Deactivate));
1270+
mocpp_set_timer(custom_timer_cb);
1271+
1272+
loop();
1273+
1274+
REQUIRE( getChargePointStatus() == ChargePointStatus_Available );
1275+
REQUIRE( !ocppPermitsCharge() );
1276+
1277+
//Note: queueing offline transactions without FS is currently not implemented
1278+
}
1279+
12311280
mocpp_deinitialize();
12321281
}

tests/Transactions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ TEST_CASE( "Transactions" ) {
387387

388388
for (auto seqNo : tx->seqNos) {
389389
MO_DBG_DEBUG("stored seqNo %u", seqNo);
390+
(void)seqNo;
390391
}
391392

392393
for (size_t i = 1; i < tx->seqNos.size(); i++) {

0 commit comments

Comments
 (0)