Skip to content

Commit

Permalink
fix: add device type to deviceInfo global
Browse files Browse the repository at this point in the history
  • Loading branch information
smlx committed Dec 15, 2023
1 parent e9c5567 commit 09beb9d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
32 changes: 23 additions & 9 deletions mitm/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,32 @@ func handleMetricsAckPacket(
data []byte,
log *slog.Logger,
) error {
var inboundMetricsAck InboundMetricsAckPacket
err := inboundMetricsAck.UnmarshalBinary(data)
var metricsAck InboundMetricsAckPacket
err := metricsAck.UnmarshalBinary(data)
if err != nil {
return fmt.Errorf("couldn't unmarshal metrics ack: %v", err)
}
devInfo, ok := deviceInfo[metricsAck.DeviceID]
if !ok {
return fmt.Errorf("unknown device ID: %v", metricsAck.DeviceID)
}
switch {
case slices.Equal(inboundMetricsAck.Data[:], metricsAckData):
log.Debug("metrics ack")
case slices.Equal(inboundMetricsAck.Data[:], metricsNackData):
log.Warn("metrics nack. bad metrics CRC?")
case slices.Equal(metricsAck.Data[:], metricsAckData):
log.Debug("metrics ack",
slog.String("device", devInfo[0]),
slog.String("model", devInfo[1]),
slog.String("serial", string(metricsAck.DeviceSerial[:])))
case slices.Equal(metricsAck.Data[:], metricsNackData):
log.Warn("metrics nack. bad metrics CRC?",
slog.String("device", devInfo[0]),
slog.String("model", devInfo[1]),
slog.String("serial", string(metricsAck.DeviceSerial[:])))
default:
log.Warn("unknown cleartext in metrics ack",
slog.Any("cleartext", inboundMetricsAck.Data[:]))
slog.Any("cleartext", metricsAck.Data[:]),
slog.String("device", devInfo[0]),
slog.String("model", devInfo[1]),
slog.String("serial", string(metricsAck.DeviceSerial[:])))
}
return nil
}
Expand Down Expand Up @@ -126,7 +139,7 @@ func (h *InboundPacketHandler) HandlePacket(
if err := header.UnmarshalBinary(headerData); err != nil {
return nil, nil, fmt.Errorf("couldn't unmarshal header: %v", err)
}
// validate data size: -2 for packet type field and +1 for length off-by-one = -1
// validate size: -2 for packet type field and +1 for length off-by-one = -1
expectedBodySize := header.Length - 1
if len(bodyData) != int(expectedBodySize) {
return nil, nil, fmt.Errorf("expected body size %d, got %d",
Expand All @@ -140,7 +153,8 @@ func (h *InboundPacketHandler) HandlePacket(
return nil, nil, nil
case meterTimeSyncResp:
if err := handleTimeSyncRespPacket(bodyData, log); err != nil {
return nil, nil, fmt.Errorf("couldn't handle time sync response packet: %v", err)
return nil, nil,
fmt.Errorf("couldn't handle time sync response packet: %v", err)
}
return nil, nil, nil
default:
Expand Down
9 changes: 9 additions & 0 deletions mitm/mitm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ var (
keepAlive = []byte{0x01, 0x02}
// prometheus metrics labels
labelNames = []string{"device", "model", "serial"}
// known Device IDs mapped to device type and model
deviceInfo = map[[8]byte][2]string{
{0x39, 0x31, 0x30, 0x30, 0x30, 0x48, 0x4b, 0x55}: [2]string{
"meter", "HomeKit 1000 Smart Meter",
},
{0x35, 0x33, 0x30, 0x30, 0x30, 0x44, 0x53, 0x43}: [2]string{
"inverter", "DNS G3 Inverter",
},
}
)

// PacketType indicates the type of the packet.
Expand Down
5 changes: 0 additions & 5 deletions mitm/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import (
)

var (
// known Device IDs
deviceIDToModel = map[[8]byte]string{
{0x39, 0x31, 0x30, 0x30, 0x30, 0x48, 0x4b, 0x55}: "HomeKit 1000 Smart Meter",
{0x35, 0x33, 0x30, 0x30, 0x30, 0x44, 0x53, 0x43}: "DNS G3 Inverter",
}
// byte order of the packet CRC field.
// yes, this differs between inbound and outbound.
outboundCRCByteOrder = binary.BigEndian
Expand Down
10 changes: 5 additions & 5 deletions mitm/outboundinverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ func handleInverterMetricsPacket(
if err != nil {
return fmt.Errorf("couldn't unmarshal metrics: %v", err)
}
model, ok := deviceIDToModel[metrics.DeviceID]
di, ok := deviceInfo[metrics.DeviceID]
if !ok {
return fmt.Errorf("unknown device ID: %v", metrics.DeviceID)
}
log.Debug("outbound metrics",
slog.String("device", "inverter"),
slog.String("model", model),
slog.String("device", di[0]),
slog.String("model", di[1]),
slog.String("serial", string(metrics.DeviceSerial[:])))
labels := prometheus.Labels{
"device": "inverter",
"model": model,
"device": di[0],
"model": di[1],
"serial": string(metrics.DeviceSerial[:]),
}
spew.Dump(labels)
Expand Down
30 changes: 15 additions & 15 deletions mitm/outboundmeter.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ func handleMeterTimeSyncPacket(
if err != nil {
return fmt.Errorf("couldn't unmarshal time sync: %v", err)
}
model, ok := deviceIDToModel[timeSync.DeviceID]
di, ok := deviceInfo[timeSync.DeviceID]
if !ok {
return fmt.Errorf("unknown device ID: %v", timeSync.DeviceID)
}
log.Debug("outbound time sync",
slog.String("device", "meter"),
slog.String("model", model),
slog.String("device", di[0]),
slog.String("model", di[1]),
slog.String("serial", string(timeSync.DeviceSerial[:])))
timeSyncPacketsTotal.With(prometheus.Labels{
"device": "meter",
"model": model,
"device": di[0],
"model": di[1],
"serial": string(timeSync.DeviceSerial[:]),
}).Inc()
return nil
Expand All @@ -133,17 +133,17 @@ func handleMeterMetricsPacket(
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal metrics: %v", err)
}
model, ok := deviceIDToModel[metrics.DeviceID]
di, ok := deviceInfo[metrics.DeviceID]
if !ok {
return nil, fmt.Errorf("unknown device ID: %v", metrics.DeviceID)
}
log.Debug("outbound metrics",
slog.String("device", "meter"),
slog.String("model", model),
slog.String("device", di[0]),
slog.String("model", di[1]),
slog.String("serial", string(metrics.DeviceSerial[:])))
labels := prometheus.Labels{
"device": "meter",
"model": model,
"device": di[0],
"model": di[1],
"serial": string(metrics.DeviceSerial[:]),
}
// record metrics
Expand Down Expand Up @@ -187,7 +187,7 @@ func handleMeterTimeSyncRespAckPacket(
if err != nil {
return fmt.Errorf("couldn't unmarshal time sync: %v", err)
}
model, ok := deviceIDToModel[timeSyncRespAck.DeviceID]
di, ok := deviceInfo[timeSyncRespAck.DeviceID]
if !ok {
return fmt.Errorf("unknown device ID: %v", timeSyncRespAck.DeviceID)
}
Expand All @@ -196,12 +196,12 @@ func handleMeterTimeSyncRespAckPacket(
slog.Any("cleartext", timeSyncRespAck.Data[:]))
}
log.Debug("outbound time sync response ack",
slog.String("device", "meter"),
slog.String("model", model),
slog.String("device", di[0]),
slog.String("model", di[1]),
slog.String("serial", string(timeSyncRespAck.DeviceSerial[:])))
timeSyncAckPacketsTotal.With(prometheus.Labels{
"device": "meter",
"model": model,
"device": di[0],
"model": di[1],
"serial": string(timeSyncRespAck.DeviceSerial[:]),
}).Inc()
return nil
Expand Down

0 comments on commit 09beb9d

Please sign in to comment.