mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
new: new can module for CAN-bus
This commit is contained in:
parent
9937e797ae
commit
5fe3ef3d52
12 changed files with 755 additions and 3 deletions
57
network/can_device.go
Normal file
57
network/can_device.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CANDevice struct {
|
||||
sync.Mutex
|
||||
LastSeen time.Time
|
||||
Name string
|
||||
Description string
|
||||
Read uint64
|
||||
}
|
||||
|
||||
type canDeviceJSON struct {
|
||||
LastSeen time.Time `json:"last_seen"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Read uint64 `json:"description"`
|
||||
}
|
||||
|
||||
func NewCANDevice(name string, description string, payload []byte) *CANDevice {
|
||||
dev := &CANDevice{
|
||||
LastSeen: time.Now(),
|
||||
Name: name,
|
||||
Description: description,
|
||||
Read: uint64(len(payload)),
|
||||
}
|
||||
|
||||
return dev
|
||||
}
|
||||
|
||||
func (dev *CANDevice) MarshalJSON() ([]byte, error) {
|
||||
dev.Lock()
|
||||
defer dev.Unlock()
|
||||
|
||||
doc := canDeviceJSON{
|
||||
LastSeen: dev.LastSeen,
|
||||
Name: dev.Name,
|
||||
Description: dev.Description,
|
||||
Read: dev.Read,
|
||||
}
|
||||
|
||||
return json.Marshal(doc)
|
||||
}
|
||||
|
||||
func (dev *CANDevice) AddPayload(payload []byte) {
|
||||
dev.Lock()
|
||||
defer dev.Unlock()
|
||||
|
||||
sz := len(payload)
|
||||
if payload != nil && sz > 0 {
|
||||
dev.Read += uint64(sz)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue