Skip to content

Commit

Permalink
Ocpp: fix race condition in retrieving chargepoint (#8623)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jun 25, 2023
1 parent 9cd614a commit f341ffa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion charger/ocpp/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func (cs *CS) NewChargePoint(chargePoint ocpp16.ChargePointConnection) {

if cp, err := cs.chargepointByID(chargePoint.ID()); err != nil {
// check for anonymous chargepoint
if cp, ok := cs.cps[""]; ok {
if cp, err := cs.chargepointByID(""); err == nil {
cs.log.INFO.Printf("chargepoint connected, registering: %s", chargePoint.ID())

// update id
cp.RegisterID(chargePoint.ID())

cs.cps[chargePoint.ID()] = cp
delete(cs.cps, "")

Expand Down
30 changes: 30 additions & 0 deletions charger/ocpp/cs_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (cs *CS) TriggerMeterValuesRequest(id string, connector int) {
// cp actions

func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -62,6 +65,9 @@ func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.Auth
}

func (cs *CS) OnBootNotification(id string, request *core.BootNotificationRequest) (*core.BootNotificationConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -71,6 +77,9 @@ func (cs *CS) OnBootNotification(id string, request *core.BootNotificationReques
}

func (cs *CS) OnDataTransfer(id string, request *core.DataTransferRequest) (*core.DataTransferConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -80,6 +89,9 @@ func (cs *CS) OnDataTransfer(id string, request *core.DataTransferRequest) (*cor
}

func (cs *CS) OnHeartbeat(id string, request *core.HeartbeatRequest) (*core.HeartbeatConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -89,6 +101,9 @@ func (cs *CS) OnHeartbeat(id string, request *core.HeartbeatRequest) (*core.Hear
}

func (cs *CS) OnMeterValues(id string, request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -98,6 +113,9 @@ func (cs *CS) OnMeterValues(id string, request *core.MeterValuesRequest) (*core.
}

func (cs *CS) OnStatusNotification(id string, request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -107,6 +125,9 @@ func (cs *CS) OnStatusNotification(id string, request *core.StatusNotificationRe
}

func (cs *CS) OnStartTransaction(id string, request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -116,6 +137,9 @@ func (cs *CS) OnStartTransaction(id string, request *core.StartTransactionReques
}

func (cs *CS) OnStopTransaction(id string, request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -125,6 +149,9 @@ func (cs *CS) OnStopTransaction(id string, request *core.StopTransactionRequest)
}

func (cs *CS) OnDiagnosticsStatusNotification(id string, request *firmware.DiagnosticsStatusNotificationRequest) (confirmation *firmware.DiagnosticsStatusNotificationConfirmation, err error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand All @@ -134,6 +161,9 @@ func (cs *CS) OnDiagnosticsStatusNotification(id string, request *firmware.Diagn
}

func (cs *CS) OnFirmwareStatusNotification(id string, request *firmware.FirmwareStatusNotificationRequest) (confirmation *firmware.FirmwareStatusNotificationConfirmation, err error) {
cs.mu.Lock()
defer cs.mu.Unlock()

cp, err := cs.chargepointByID(id)
if err != nil {
return nil, err
Expand Down

0 comments on commit f341ffa

Please sign in to comment.