Skip to content

Commit 2099648

Browse files
committed
[examples] Fix segfault on disconnect in central system examples
1 parent 0573edf commit 2099648

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

examples/common/DefaultCentralSystemEventsHandler.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ using namespace ocpp::x509;
4545
DefaultCentralSystemEventsHandler::DefaultCentralSystemEventsHandler(std::filesystem::path iso_v2g_root_ca,
4646
std::filesystem::path iso_mo_root_ca,
4747
bool set_pending_status)
48-
: m_iso_v2g_root_ca(iso_v2g_root_ca),
48+
: m_chargepoints_mutex(),
49+
m_iso_v2g_root_ca(iso_v2g_root_ca),
4950
m_iso_mo_root_ca(iso_mo_root_ca),
5051
m_set_pending_status(set_pending_status),
5152
m_chargepoints(),
@@ -83,6 +84,9 @@ bool DefaultCentralSystemEventsHandler::checkCredentials(const std::string& char
8384
void DefaultCentralSystemEventsHandler::chargePointConnected(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
8485
{
8586
cout << "Charge point [" << chargepoint->identifier() << "] connected" << endl;
87+
88+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
89+
8690
auto iter_chargepoint = m_chargepoints.find(chargepoint->identifier());
8791
if (iter_chargepoint == m_chargepoints.end())
8892
{
@@ -103,13 +107,38 @@ void DefaultCentralSystemEventsHandler::removeChargePoint(const std::string& ide
103107
[this, identifier = identifier]
104108
{
105109
std::this_thread::sleep_for(std::chrono::milliseconds(50));
110+
111+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
106112
m_chargepoints.erase(identifier);
107113
m_pending_chargepoints.erase(identifier);
108114
m_accepted_chargepoints.erase(identifier);
109115
});
110116
t.detach();
111117
}
112118

119+
/** @brief Indicate if a charge point must be accepted */
120+
bool DefaultCentralSystemEventsHandler::isAcceptedChargePoint(const std::string& identifier)
121+
{
122+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
123+
return (m_accepted_chargepoints.find(identifier) != m_accepted_chargepoints.end());
124+
}
125+
126+
/** @brief Add a charge point to the pending list */
127+
void DefaultCentralSystemEventsHandler::addPendingChargePoint(
128+
std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
129+
{
130+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
131+
m_pending_chargepoints[chargepoint->identifier()] = chargepoint;
132+
}
133+
134+
/** @brief Add a charge point to the accepted list */
135+
void DefaultCentralSystemEventsHandler::addAcceptedChargePoint(
136+
std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
137+
{
138+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
139+
m_accepted_chargepoints[chargepoint->identifier()] = chargepoint;
140+
}
141+
113142
/** @brief Constructor */
114143
DefaultCentralSystemEventsHandler::ChargePointRequestHandler::ChargePointRequestHandler(
115144
DefaultCentralSystemEventsHandler& event_handler, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>& chargepoint)
@@ -170,12 +199,9 @@ ocpp::types::RegistrationStatus DefaultCentralSystemEventsHandler::ChargePointRe
170199
ocpp::types::RegistrationStatus ret = RegistrationStatus::Accepted;
171200
if (m_event_handler.setPendingEnabled())
172201
{
173-
auto accepted_chargepoint = m_event_handler.acceptedChargePoints();
174-
auto iter_accepted = accepted_chargepoint.find(m_chargepoint->identifier());
175-
if (iter_accepted == accepted_chargepoint.end())
202+
if (!m_event_handler.isAcceptedChargePoint(m_chargepoint->identifier()))
176203
{
177-
m_event_handler.pendingChargePoints()[m_chargepoint->identifier()] = m_chargepoint;
178-
204+
m_event_handler.addPendingChargePoint(m_chargepoint);
179205
ret = RegistrationStatus::Pending;
180206
}
181207
}

examples/common/DefaultCentralSystemEventsHandler.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SOFTWARE.
3030

3131
#include <filesystem>
3232
#include <map>
33+
#include <mutex>
3334

3435
/** @brief Default central system event handlers implementation for the examples */
3536
class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSystemEventsHandler
@@ -237,19 +238,25 @@ class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSy
237238
std::string m_generated_certificate;
238239
};
239240

240-
/** @brief Get the list of the connected charge points */
241-
std::map<std::string, std::shared_ptr<ChargePointRequestHandler>>& chargePoints() { return m_chargepoints; }
241+
/** @brief Get the number connected charge points */
242+
size_t chargePointsCount()
243+
{
244+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
245+
return m_chargepoints.size();
246+
}
242247

243-
/** @brief Get the list of the pending charge points */
244-
std::map<std::string, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>>& pendingChargePoints()
248+
/** @brief Get the list of the connected charge points */
249+
std::map<std::string, std::shared_ptr<ChargePointRequestHandler>> chargePoints()
245250
{
246-
return m_pending_chargepoints;
251+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
252+
return m_chargepoints;
247253
}
248254

249-
/** @brief Get the list of the accepted charge points */
250-
std::map<std::string, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>>& acceptedChargePoints()
255+
/** @brief Get the list of the pending charge points */
256+
std::map<std::string, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>> pendingChargePoints()
251257
{
252-
return m_accepted_chargepoints;
258+
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
259+
return m_pending_chargepoints;
253260
}
254261

255262
/** @brief Path to the V2G root CA */
@@ -263,7 +270,18 @@ class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSy
263270
/** @brief Remove a charge point from the connected charge points */
264271
void removeChargePoint(const std::string& identifier);
265272

273+
/** @brief Indicate if a charge point must be accepted */
274+
bool isAcceptedChargePoint(const std::string& identifier);
275+
276+
/** @brief Add a charge point to the pending list */
277+
void addPendingChargePoint(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint);
278+
279+
/** @brief Add a charge point to the accepted list */
280+
void addAcceptedChargePoint(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint);
281+
266282
protected:
283+
/** @brief Mutex for charge point list */
284+
std::mutex m_chargepoints_mutex;
267285
/** @brief Path to the V2G root CA */
268286
std::filesystem::path m_iso_v2g_root_ca;
269287
/** @brief Path to the MO root CA */

examples/iso15118_centralsystem/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ int main(int argc, char* argv[])
148148
while (true)
149149
{
150150
// For each pending charge point
151-
for (auto& iter_chargepoint : event_handler.pendingChargePoints())
151+
auto pending_chargepoints = event_handler.pendingChargePoints();
152+
for (auto& iter_chargepoint : pending_chargepoints)
152153
{
153-
auto chargepoint = iter_chargepoint.second;
154-
auto iter_accepted = event_handler.acceptedChargePoints().find(chargepoint->identifier());
155-
if (iter_accepted == event_handler.acceptedChargePoints().end())
154+
auto chargepoint = iter_chargepoint.second;
155+
if (!event_handler.isAcceptedChargePoint(chargepoint->identifier()))
156156
{
157157
std::cout << "---------------------------------------------" << std::endl;
158158
std::cout << "Pending Charge point : " << chargepoint->identifier() << std::endl;
@@ -239,7 +239,7 @@ int main(int argc, char* argv[])
239239
}
240240

241241
// Accept charge point
242-
event_handler.acceptedChargePoints()[chargepoint->identifier()] = chargepoint;
242+
event_handler.addAcceptedChargePoint(chargepoint);
243243

244244
// Trigger a boot notification to force it to update its registration status
245245
chargepoint->triggerMessage(MessageTrigger::BootNotification, Optional<unsigned int>());

examples/quick_start_centralsystem/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ int main(int argc, char* argv[])
118118
std::this_thread::sleep_for(std::chrono::seconds(1));
119119

120120
// For each connected charge point
121-
for (auto& iter_chargepoint : event_handler.chargePoints())
121+
auto connected_chargepoints = event_handler.chargePoints();
122+
for (auto& iter_chargepoint : connected_chargepoints)
122123
{
123124
{
124125
auto chargepoint = iter_chargepoint.second->proxy();

0 commit comments

Comments
 (0)