diff --git a/can/can.go b/can/can.go index c812f61..379a37c 100644 --- a/can/can.go +++ b/can/can.go @@ -11,36 +11,11 @@ import ( "golang.org/x/sys/unix" ) -const CAN_ID_071 = 0x071 -const CAN_ID_073 = 0x073 -const CAN_ID_074 = 0x074 -const CAN_ID_075 = 0x075 -const CAN_ID_021 = 0x021 -const CAN_ID_022 = 0x022 -const CAN_ID_023 = 0x023 -const CAN_ID_011 = 0x011 -const CAN_ID_014 = 0x014 -const CAN_ID_015 = 0x015 -const CAN_ID_016 = 0x016 -const CAN_ID_01A = 0x01A -const CAN_ID_02A = 0x02A -const CAN_ID_041 = 0x041 -const CAN_ID_042 = 0x042 -const CAN_ID_046 = 0x046 -const CAN_ID_047 = 0x047 -const CAN_ID_048 = 0x048 -const CAN_ID_049 = 0x049 -const CAN_ID_067 = 0x067 -const CAN_ID_068 = 0x068 -const CAN_ID_06C = 0x06C -const CAN_ID_061 = 0x061 -const CAN_ID_062 = 0x062 -const CAN_ID_065 = 0x065 const bufferSize = 100 type CanFrame struct { CanId uint32 - Payload []uint8 + Payload *[]uint8 Date *time.Time } @@ -83,9 +58,9 @@ func (sck *Socket) Bind(addr string, filter []unix.CanFilter) error { return unix.Bind(sck.dev.fd, sck.addr) } -// Recv receives data from the CAN socket. +// Receive receives data from the CAN socket. // id is the CAN_frame id the data was originated from. -func (sck *Socket) Recv() (id uint32, data []byte, err error) { +func (sck *Socket) Receive() (id uint32, data *[]byte, err error) { var buf [frameSize]byte n, err := io.ReadFull(sck.dev, buf[:]) if err != nil { @@ -99,9 +74,9 @@ func (sck *Socket) Recv() (id uint32, data []byte, err error) { id = binary.LittleEndian.Uint32(buf[:4]) //TODO make correct switch betwin EFF and SFF id &= unix.CAN_EFF_MASK - data = make([]byte, buf[4]) - copy(data, buf[8:]) - return id, data, nil + payload := make([]byte, buf[4]) + copy(payload, buf[8:]) + return id, &payload, nil } type device struct { @@ -126,35 +101,7 @@ type canframe struct { Data [8]byte } -func StartCan(canbus string) (<-chan *CanFrame, error) { - - var filter []unix.CanFilter = []unix.CanFilter{ - {Id: CAN_ID_071, Mask: 0xFFF}, - {Id: CAN_ID_073, Mask: 0xFFF}, - {Id: CAN_ID_074, Mask: 0xFFF}, - {Id: CAN_ID_075, Mask: 0xFFF}, - {Id: CAN_ID_021, Mask: 0xFFF}, - {Id: CAN_ID_022, Mask: 0xFFF}, - {Id: CAN_ID_011, Mask: 0xFFF}, - {Id: CAN_ID_014, Mask: 0xFFF}, - {Id: CAN_ID_015, Mask: 0xFFF}, - {Id: CAN_ID_016, Mask: 0xFFF}, - {Id: CAN_ID_01A, Mask: 0xFFF}, - {Id: CAN_ID_023, Mask: 0xFFF}, - {Id: CAN_ID_02A, Mask: 0xFFF}, - {Id: CAN_ID_041, Mask: 0xFFF}, - {Id: CAN_ID_042, Mask: 0xFFF}, - {Id: CAN_ID_046, Mask: 0xFFF}, - {Id: CAN_ID_047, Mask: 0xFFF}, - {Id: CAN_ID_048, Mask: 0xFFF}, - {Id: CAN_ID_049, Mask: 0xFFF}, - {Id: CAN_ID_067, Mask: 0xFFF}, - {Id: CAN_ID_068, Mask: 0xFFF}, - {Id: CAN_ID_06C, Mask: 0xFFF}, - {Id: CAN_ID_061, Mask: 0xFFF}, - {Id: CAN_ID_062, Mask: 0xFFF}, - {Id: CAN_ID_065, Mask: 0xFFF}, - } +func StartCan(canbus string, filter []unix.CanFilter) (<-chan *CanFrame, error) { if socket, err := NewCan(); err == nil { @@ -167,7 +114,7 @@ func StartCan(canbus string) (<-chan *CanFrame, error) { defer socket.Close() for { //TODO implement stop - if id, data, serr := socket.Recv(); serr == nil { + if id, data, serr := socket.Receive(); serr == nil { var now = time.Now() canevents <- &CanFrame{ Date: &now, diff --git a/can/input.go b/can/input.go index 457b4a3..eeb9942 100644 --- a/can/input.go +++ b/can/input.go @@ -105,7 +105,7 @@ func fromString(text string) *CanFrame { return &CanFrame{ Date: &now, CanId: uint32(canId), - Payload: payload, + Payload: &payload, } } diff --git a/cli-mon.go b/cli-mon.go index a442827..964c48c 100644 --- a/cli-mon.go +++ b/cli-mon.go @@ -6,6 +6,7 @@ import ( "cli-mon/yabl" "flag" "fmt" + "golang.org/x/sys/unix" "os" "time" ) @@ -25,8 +26,36 @@ func main() { case len(*filename) > 0: frames = can.Readfile(filename) case len(*canbus) > 0: + filter := []unix.CanFilter{ + {Id: yabl.CanIdContIntState, Mask: 0xFFF}, + {Id: yabl.CanIdContIntErr, Mask: 0xFFF}, + {Id: yabl.CanIdContIntForce, Mask: 0xFFF}, + {Id: yabl.CanIdContIntDebug, Mask: 0xFFF}, + {Id: yabl.CanIdPuPresentEnergy, Mask: 0xFFF}, + {Id: yabl.CanIdPuPeriphery, Mask: 0xFFF}, + {Id: yabl.CanIdCpuPresentEnergy, Mask: 0xFFF}, + {Id: yabl.CanIdCpuPeriphery, Mask: 0xFFF}, + {Id: yabl.CanIdCpuEnergySettings, Mask: 0xFFF}, + {Id: yabl.CanIdCpuErrors, Mask: 0xFFF}, + {Id: yabl.CanIdCpuDebug, Mask: 0xFFF}, + {Id: yabl.CanIdPuErrors, Mask: 0xFFF}, + {Id: yabl.CanIdPuDebug, Mask: 0xFFF}, + {Id: yabl.CanIdSeccTargetEnergy, Mask: 0xFFF}, + {Id: yabl.CanIdSeccErrors, Mask: 0xFFF}, + {Id: yabl.CanIdLogicAuth, Mask: 0xFFF}, + {Id: yabl.CanIdLogicEnergyMode, Mask: 0xFFF}, + {Id: yabl.CanIdLogicErrors, Mask: 0xFFF}, + {Id: yabl.CanIdLogicWorkingMode, Mask: 0xFFF}, + {Id: yabl.CanIdPeripheryState, Mask: 0xFFF}, + {Id: yabl.CanIdPeripheryInfo, Mask: 0xFFF}, + {Id: yabl.CanIdPeripheryDebug, Mask: 0xFFF}, + {Id: yabl.CanIdConverterPresentEnergy, Mask: 0xFFF}, + {Id: yabl.CanIdConverterErrors, Mask: 0xFFF}, + {Id: yabl.CanIdConverterDebug, Mask: 0xFFF}, + } + var err error - frames, err = can.StartCan(*canbus) + frames, err = can.StartCan(*canbus, filter) if err != nil { fmt.Println(err) return diff --git a/yabl/const.go b/yabl/const.go index 2acd615..d08aa02 100644 --- a/yabl/const.go +++ b/yabl/const.go @@ -1,14 +1,41 @@ package yabl const ( - CONVERTERS_MAX = 18 + 1 - CONTACTOR_MAX = 18 + 1 - CONNECTOR_COUNT = 6 - CONNECTOR_MAX = CONNECTOR_COUNT + 1 - ALL_BITS = 0xFFFFFFFFFFFFFFFF - UNIT_ID_OFFSET = 12 - UNIT_ID_MASK = 0b111111111111 << UNIT_ID_OFFSET - ACTION_ID_MASK = 0b111111111111 + CONVERTERS_MAX = 18 + 1 + CONTACTOR_MAX = 18 + 1 + CONNECTOR_COUNT = 6 + CONNECTOR_MAX = CONNECTOR_COUNT + 1 + ALL_BITS = 0xFFFFFFFFFFFFFFFF + UNIT_ID_OFFSET = 12 + UNIT_ID_MASK = 0b111111111111 << UNIT_ID_OFFSET + ACTION_ID_MASK = 0b111111111111 + + CanIdContIntState = 0x071 + CanIdContIntErr = 0x073 + CanIdContIntForce = 0x074 + CanIdContIntDebug = 0x075 + CanIdPuPresentEnergy = 0x021 + CanIdPuPeriphery = 0x022 + CanIdPuErrors = 0x023 + CanIdCpuPresentEnergy = 0x011 + CanIdCpuPeriphery = 0x014 + CanIdCpuEnergySettings = 0x015 + CanIdCpuErrors = 0x016 + CanIdCpuDebug = 0x01A + CanIdPuDebug = 0x02A + CanIdSeccTargetEnergy = 0x041 + CanIdSeccErrors = 0x042 + CanIdLogicAuth = 0x046 + CanIdLogicEnergyMode = 0x047 + CanIdLogicErrors = 0x048 + CanIdLogicWorkingMode = 0x049 + CanIdPeripheryState = 0x067 + CanIdPeripheryInfo = 0x068 + CanIdPeripheryDebug = 0x06C + CanIdConverterPresentEnergy = 0x061 + CanIdConverterErrors = 0x062 + CanIdConverterDebug = 0x065 + BOARD_READY_OK BoardReadyType = 0 BOARD_READY_INFO BoardReadyType = 1 BOARD_READY_WARNING BoardReadyType = 2 diff --git a/yabl/init.go b/yabl/init.go index b910d9f..1c88a4d 100644 --- a/yabl/init.go +++ b/yabl/init.go @@ -1,12 +1,10 @@ package yabl -import "cli-mon/can" - func (c *converter) initialize() { c.protocolMap = make(map[key]action) for i := uint(1); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_011, i} + key := key{CanIdCpuPresentEnergy, i} cpe := &CpuPresentEnergy{unit: 1} c.cpuPresentEnergyArray[i] = cpe fields := []*field{ @@ -23,7 +21,7 @@ func (c *converter) initialize() { {length: 2, setter: c.cpuPeripheryInstance.setCircuitBreakerInput, name: FCircuitBreakerInput}, {length: 2, setter: c.cpuPeripheryInstance.setCircuitBreakerPowerCBInput, name: FCircuitBreakerPowerCBInput}, } - c.protocolMap[key{can.CAN_ID_014, 0}] = action{ + c.protocolMap[key{CanIdCpuPeriphery, 0}] = action{ fields: fields, interval: 1000, name: PCpuPeriphery, @@ -31,7 +29,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_015, i} + key := key{CanIdCpuEnergySettings, i} ces := &CpuEnergySettings{unit: unit(i)} c.cpuEnergySettingsArray[i] = ces fields := []*field{ @@ -65,7 +63,7 @@ func (c *converter) initialize() { {length: 2, setter: c.cpuErrorsInstance.setContactorInputError, name: FContactorInputError}, {length: 2, setter: c.cpuErrorsInstance.setNotReadyPeriphery, name: FNotReadyPeriphery}, } - c.protocolMap[key{can.CAN_ID_016, 0}] = action{ + c.protocolMap[key{CanIdCpuErrors, 0}] = action{ fields: fields, interval: 1000, name: PCpuErrors, @@ -79,7 +77,7 @@ func (c *converter) initialize() { {length: 3, setter: c.cpuDebugInstance.setDebugCircuitBreakerOn, name: FDebugCircuitBreakerOn}, {length: 3, setter: c.cpuDebugInstance.setDebugRedButtonSoftware, name: FDebugRedButtonSoftware}, } - c.protocolMap[key{can.CAN_ID_01A, 0}] = action{ + c.protocolMap[key{CanIdCpuDebug, 0}] = action{ fields: fields, interval: 1000, name: PCpuDebug, @@ -87,7 +85,7 @@ func (c *converter) initialize() { } for i := uint(1); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_021, i} + key := key{CanIdPuPresentEnergy, i} ppe := &PuPresentEnergy{unit: unit(i)} c.puPresentEnergyArray[i] = ppe fields := []*field{ @@ -100,7 +98,7 @@ func (c *converter) initialize() { } for i := uint(1); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_022, i} + key := key{CanIdPuPeriphery, i} pp := &PuPeriphery{unit: unit(i)} c.puPeripheryArray[i] = pp fields := []*field{ @@ -117,7 +115,7 @@ func (c *converter) initialize() { } for i := uint(1); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_023, i} + key := key{CanIdPuErrors, i} pe := &PuErrors{unit: 1} c.puErrorsArray[i] = pe fields := []*field{ @@ -142,7 +140,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_02A, i} + key := key{CanIdPuDebug, i} pd := &PuDebug{unit: unit(i)} c.puDebugArray[i] = pd fields = []*field{ @@ -153,7 +151,7 @@ func (c *converter) initialize() { } for i := uint(1); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_041, i} + key := key{CanIdSeccTargetEnergy, i} ste := &SeccTargetEnergy{unit: unit(i)} c.seccTargetEnergyArray[i] = ste fields = []*field{ @@ -166,7 +164,7 @@ func (c *converter) initialize() { } for i := uint(1); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_042, i} + key := key{CanIdSeccErrors, i} se := &SeccErrors{unit: unit(i)} c.seccErrorsArray[i] = se fields = []*field{ @@ -177,7 +175,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_046, i} + key := key{CanIdLogicAuth, i} la := &LogicAuth{unit: unit(i)} c.logicAuthArray[i] = la fields = []*field{ @@ -188,7 +186,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_047, i} + key := key{CanIdLogicEnergyMode, i} lem := &LogicEnergyMode{unit: unit(i)} c.logicEnergyMode[i] = lem fields = []*field{ @@ -205,7 +203,7 @@ func (c *converter) initialize() { {length: 3, setter: c.logicErrorsInstance.setBoardReady, name: FBoardReady}, {length: 2, setter: c.logicErrorsInstance.setNotReadySettings, name: FNotReadySettings}, } - c.protocolMap[key{can.CAN_ID_048, 0}] = action{ + c.protocolMap[key{CanIdLogicErrors, 0}] = action{ fields: fields, interval: 1000, name: PLogicErrors, @@ -213,7 +211,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_049, i} + key := key{CanIdLogicWorkingMode, i} lwm := &LogicWorkingMode{unit: unit(i)} c.logicWorkingMode[i] = lwm fields = []*field{ @@ -229,7 +227,7 @@ func (c *converter) initialize() { } for i := uint(0); i < CONTACTOR_MAX; i++ { - key := key{can.CAN_ID_071, i} + key := key{CanIdContIntState, i} cis := &ContactorInternalState{unit: unit(i)} c.contactorInternalStateArray[i] = cis fields = []*field{ @@ -253,7 +251,7 @@ func (c *converter) initialize() { {length: 2, setter: c.contactorInternalErrorsInstance.setDebug, name: FDebug}, {length: 5, setter: c.contactorInternalErrorsInstance.setPresentContactorMode, name: FPresentContactorMode}, } - c.protocolMap[key{can.CAN_ID_073, 0}] = action{ + c.protocolMap[key{CanIdContIntErr, 0}] = action{ fields: fields, interval: 1000, name: PContactorInternalErrors, @@ -265,7 +263,7 @@ func (c *converter) initialize() { {length: 2, setter: c.contactorsInternalForce.setForceModeEnabled, name: FForceModeEnabled}, {length: 4, setter: c.contactorsInternalForce.setForceModeValue, name: FForceModeValue}, } - c.protocolMap[key{can.CAN_ID_074, 0}] = action{ + c.protocolMap[key{CanIdContIntForce, 0}] = action{ fields: fields, interval: 1000, name: PContactorsInternalForce, @@ -273,7 +271,7 @@ func (c *converter) initialize() { } for i := uint(0); i < CONTACTOR_MAX; i++ { - key := key{can.CAN_ID_075, i} + key := key{CanIdContIntDebug, i} cid := &ContactorInternalDebug{unit: unit(i)} c.contactorInternalDebugArray[i] = cid fields = []*field{ @@ -284,7 +282,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_067, i} + key := key{CanIdPeripheryState, i} ps := &PeripheryState{unit: unit(i)} c.peripheryStateArray[i] = ps fields = []*field{ @@ -315,7 +313,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_068, i} + key := key{CanIdPeripheryInfo, i} pi := &PeripheryInfo{unit: unit(i)} c.peripheryInfoArray[i] = pi fields = []*field{ @@ -329,7 +327,7 @@ func (c *converter) initialize() { } for i := uint(0); i <= CONNECTOR_COUNT; i++ { - key := key{can.CAN_ID_06C, i} + key := key{CanIdPeripheryDebug, i} pd := &PeripheryDebug{unit: unit(i)} c.peripheryDebugArray[i] = pd fields = []*field{ @@ -342,7 +340,7 @@ func (c *converter) initialize() { } for i := uint(1); i < CONVERTERS_MAX; i++ { - key := key{can.CAN_ID_061, i} + key := key{CanIdConverterPresentEnergy, i} cpe := &ConverterPresentEnergy{unit: unit(i)} c.converterPresentEnergyArray[i] = cpe fields = []*field{ @@ -355,7 +353,7 @@ func (c *converter) initialize() { } for i := uint(1); i < CONVERTERS_MAX; i++ { - key := key{can.CAN_ID_062, i} + key := key{CanIdConverterErrors, i} ce := &ConverterErrors{unit: unit(i)} c.converterErrorsArray[i] = ce fields = []*field{ @@ -381,7 +379,7 @@ func (c *converter) initialize() { } for i := uint(0); i < CONVERTERS_MAX; i++ { - key := key{can.CAN_ID_065, i} + key := key{CanIdConverterDebug, i} cd := &ConverterDebug{unit: unit(i)} c.converterDebugArray[i] = cd fields = []*field{ diff --git a/yabl/protocol.go b/yabl/protocol.go index 9a0cbdc..d8ba314 100644 --- a/yabl/protocol.go +++ b/yabl/protocol.go @@ -104,8 +104,8 @@ func (c *converter) EventsFromFrame(frame *can.CanFrame) ([]*Event, bool) { return nil, false } -func get(from uint8, size uint8, buffer []byte) (uint64, bool) { - value := binary.LittleEndian.Uint64(buffer) +func get(from uint8, size uint8, buffer *[]byte) (uint64, bool) { + value := binary.LittleEndian.Uint64(*buffer) var mask uint64 = ^(ALL_BITS << size) mask = mask << from