Skip to content

Commit 4bd63d5

Browse files
committed
SHM: generate stable device ids (BC)
1 parent 04c2f8c commit 4bd63d5

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

hems/shm/shm.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shm
22

33
import (
4+
"crypto/sha256"
45
"encoding/binary"
56
"encoding/hex"
67
"encoding/xml"
@@ -40,6 +41,7 @@ type SEMP struct {
4041
log *util.Logger
4142
vid string
4243
did []byte
44+
legacy bool
4345
uid string
4446
hostURI string
4547
port int
@@ -50,6 +52,7 @@ type Config struct {
5052
AllowControl_ bool `json:"allowControl,omitempty"` // deprecated
5153
VendorId string `json:"vendorId"`
5254
DeviceId string `json:"deviceId"`
55+
LegacyId bool `json:"legacyId"`
5356
}
5457

5558
// NewFromConfig creates a new SEMP instance from configuration and starts it
@@ -82,11 +85,12 @@ func NewFromConfig(cfg Config, site site.API, addr string, router *mux.Router) e
8285
}
8386

8487
s := &SEMP{
85-
log: util.NewLogger("semp"),
86-
site: site,
87-
uid: uid.String(),
88-
vid: vendorId,
89-
did: did,
88+
log: util.NewLogger("semp"),
89+
site: site,
90+
uid: uid.String(),
91+
vid: vendorId,
92+
did: did,
93+
legacy: cfg.LegacyId,
9094
}
9195

9296
// find external port
@@ -321,9 +325,21 @@ func UniqueDeviceID() ([]byte, error) {
321325

322326
// deviceID combines base device id with device number
323327
func (s *SEMP) deviceID(id int) string {
324-
// numerically add device number
325-
did := append([]byte{0, 0}, s.did...)
326-
return fmt.Sprintf(sempDeviceId, s.vid, ^uint64(0xffff<<48)&(binary.BigEndian.Uint64(did)+uint64(id)))
328+
// build device id from unique id plus loadpoint id (unstable!)
329+
if s.legacy {
330+
did := append([]byte{0, 0}, s.did...)
331+
return fmt.Sprintf(sempDeviceId, s.vid, ^uint64(0xffff<<48)&(binary.BigEndian.Uint64(did)+uint64(id)))
332+
}
333+
334+
// build stable device id of unique id plus charger ref
335+
lp := s.site.Loadpoints()[id]
336+
337+
did := s.did
338+
for i, b := range sha256.Sum256([]byte(lp.GetChargerRef())) {
339+
did[i%len(did)] ^= b
340+
}
341+
342+
return fmt.Sprintf(sempDeviceId, s.vid, did)
327343
}
328344

329345
func (s *SEMP) deviceInfo(id int, lp loadpoint.API) DeviceInfo {

0 commit comments

Comments
 (0)