From 4447e89c5ee71067241bee69dd240d515070d16b Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Sun, 31 Jan 2021 19:35:58 +0100 Subject: [PATCH] Add CGo typecasts everywhere --- uncanny/message.go | 82 ++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/uncanny/message.go b/uncanny/message.go index 23cf336..6fb3e3d 100644 --- a/uncanny/message.go +++ b/uncanny/message.go @@ -1,12 +1,14 @@ package uncanny /* -// include our message format definitions +// Include our message format definitions #cgo CPPFLAGS: -I${SRCDIR}/.. +#include #include -// since we can't use macros in Go directly, let's add some wrappers -// to avoid conflicts, we'll simply lowercase the symbol names +// Since we can't use C macros in Go directly, let's add some wrappers. +// WARNING: CGo type translation can be very wonky, so make sure to always +// cast to the expected type! const uint16_t can_msg_feedback_status = CAN_MSG_FEEDBACK_STATUS; const size_t can_msg_feedback_status_field_reset_sw = CAN_MSG_FEEDBACK_STATUS_FIELD_RESET_SW; @@ -128,54 +130,54 @@ func DecodeFeedbackMessage(frame can.Frame) (FeedbackMessage, error) { if frame.Length != 2 { return ret, InvalidMessageLength } - ret.EndD = frame.Data[C.can_msg_feedback_status_field_end_d] & C.can_msg_feedback_status_bit_end_d != 0 - ret.EndE = frame.Data[C.can_msg_feedback_status_field_end_e] & C.can_msg_feedback_status_bit_end_e != 0 - ret.EndF = frame.Data[C.can_msg_feedback_status_field_end_f] & C.can_msg_feedback_status_bit_end_f != 0 - ret.EndG = frame.Data[C.can_msg_feedback_status_field_end_g] & C.can_msg_feedback_status_bit_end_g != 0 - ret.EndH = frame.Data[C.can_msg_feedback_status_field_end_h] & C.can_msg_feedback_status_bit_end_h != 0 - ret.EmptyD = frame.Data[C.can_msg_feedback_status_field_empty_d] & C.can_msg_feedback_status_bit_empty_d != 0 - ret.EmptyE = frame.Data[C.can_msg_feedback_status_field_empty_e] & C.can_msg_feedback_status_bit_empty_e != 0 - ret.EmptyF = frame.Data[C.can_msg_feedback_status_field_empty_f] & C.can_msg_feedback_status_bit_empty_f != 0 - ret.EmptyG = frame.Data[C.can_msg_feedback_status_field_empty_g] & C.can_msg_feedback_status_bit_empty_g != 0 - ret.EmptyH = frame.Data[C.can_msg_feedback_status_field_empty_h] & C.can_msg_feedback_status_bit_empty_h != 0 - ret.ResetSw = frame.Data[C.can_msg_feedback_status_field_reset_sw] & C.can_msg_feedback_status_bit_reset_sw != 0 + ret.EndD = frame.Data[int(C.can_msg_feedback_status_field_end_d)] & uint8(C.can_msg_feedback_status_bit_end_d) != 0 + ret.EndE = frame.Data[int(C.can_msg_feedback_status_field_end_e)] & uint8(C.can_msg_feedback_status_bit_end_e) != 0 + ret.EndF = frame.Data[int(C.can_msg_feedback_status_field_end_f)] & uint8(C.can_msg_feedback_status_bit_end_f) != 0 + ret.EndG = frame.Data[int(C.can_msg_feedback_status_field_end_g)] & uint8(C.can_msg_feedback_status_bit_end_g) != 0 + ret.EndH = frame.Data[int(C.can_msg_feedback_status_field_end_h)] & uint8(C.can_msg_feedback_status_bit_end_h) != 0 + ret.EmptyD = frame.Data[int(C.can_msg_feedback_status_field_empty_d)] & uint8(C.can_msg_feedback_status_bit_empty_d) != 0 + ret.EmptyE = frame.Data[int(C.can_msg_feedback_status_field_empty_e)] & uint8(C.can_msg_feedback_status_bit_empty_e) != 0 + ret.EmptyF = frame.Data[int(C.can_msg_feedback_status_field_empty_f)] & uint8(C.can_msg_feedback_status_bit_empty_f) != 0 + ret.EmptyG = frame.Data[int(C.can_msg_feedback_status_field_empty_g)] & uint8(C.can_msg_feedback_status_bit_empty_g) != 0 + ret.EmptyH = frame.Data[int(C.can_msg_feedback_status_field_empty_h)] & uint8(C.can_msg_feedback_status_bit_empty_h) != 0 + ret.ResetSw = frame.Data[int(C.can_msg_feedback_status_field_reset_sw)] & uint8(C.can_msg_feedback_status_bit_reset_sw) != 0 return ret, nil } func (f FeedbackMessage) Encode() can.Frame { data := [8]uint8{} if f.EndD { - data[C.can_msg_feedback_status_field_end_d] |= C.can_msg_feedback_status_bit_end_d + data[int(C.can_msg_feedback_status_field_end_d)] |= uint8(C.can_msg_feedback_status_bit_end_d) } if f.EndE { - data[C.can_msg_feedback_status_field_end_e] |= C.can_msg_feedback_status_bit_end_e + data[int(C.can_msg_feedback_status_field_end_e)] |= uint8(C.can_msg_feedback_status_bit_end_e) } if f.EndF { - data[C.can_msg_feedback_status_field_end_f] |= C.can_msg_feedback_status_bit_end_f + data[int(C.can_msg_feedback_status_field_end_f)] |= uint8(C.can_msg_feedback_status_bit_end_f) } if f.EndG { - data[C.can_msg_feedback_status_field_end_g] |= C.can_msg_feedback_status_bit_end_g + data[int(C.can_msg_feedback_status_field_end_g)] |= uint8(C.can_msg_feedback_status_bit_end_g) } if f.EndH { - data[C.can_msg_feedback_status_field_end_h] |= C.can_msg_feedback_status_bit_end_h + data[int(C.can_msg_feedback_status_field_end_h)] |= uint8(C.can_msg_feedback_status_bit_end_h) } if f.EmptyD { - data[C.can_msg_feedback_status_field_empty_d] |= C.can_msg_feedback_status_bit_empty_d + data[int(C.can_msg_feedback_status_field_empty_d)] |= uint8(C.can_msg_feedback_status_bit_empty_d) } if f.EmptyE { - data[C.can_msg_feedback_status_field_empty_e] |= C.can_msg_feedback_status_bit_empty_e + data[int(C.can_msg_feedback_status_field_empty_e)] |= uint8(C.can_msg_feedback_status_bit_empty_e) } if f.EmptyF { - data[C.can_msg_feedback_status_field_empty_f] |= C.can_msg_feedback_status_bit_empty_f + data[int(C.can_msg_feedback_status_field_empty_f)] |= uint8(C.can_msg_feedback_status_bit_empty_f) } if f.EmptyG { - data[C.can_msg_feedback_status_field_empty_g] |= C.can_msg_feedback_status_bit_empty_g + data[int(C.can_msg_feedback_status_field_empty_g)] |= uint8(C.can_msg_feedback_status_bit_empty_g) } if f.EmptyH { - data[C.can_msg_feedback_status_field_empty_h] |= C.can_msg_feedback_status_bit_empty_h + data[int(C.can_msg_feedback_status_field_empty_h)] |= uint8(C.can_msg_feedback_status_bit_empty_h) } if f.ResetSw { - data[C.can_msg_feedback_status_field_reset_sw] |= C.can_msg_feedback_status_bit_reset_sw + data[int(C.can_msg_feedback_status_field_reset_sw)] |= uint8(C.can_msg_feedback_status_bit_reset_sw) } return can.Frame{ ID: uint32(C.can_msg_feedback_status), @@ -232,17 +234,17 @@ func DecodeDispenseMessage(frame can.Frame) (DispenseMessage, error) { return ret, InvalidMessageLength } switch frame.Data[0] { - case C.can_msg_power_dispense_off: + case uint8(C.can_msg_power_dispense_off): ret.Slot = DispenseSlotOff - case C.can_msg_power_dispense_slot1: + case uint8(C.can_msg_power_dispense_slot1): ret.Slot = 0 - case C.can_msg_power_dispense_slot2: + case uint8(C.can_msg_power_dispense_slot2): ret.Slot = 1 - case C.can_msg_power_dispense_slot3: + case uint8(C.can_msg_power_dispense_slot3): ret.Slot = 2 - case C.can_msg_power_dispense_slot4: + case uint8(C.can_msg_power_dispense_slot4): ret.Slot = 3 - case C.can_msg_power_dispense_slot5: + case uint8(C.can_msg_power_dispense_slot5): ret.Slot = 4 } return ret, nil @@ -252,17 +254,17 @@ func (f DispenseMessage) Encode() can.Frame { data := [8]uint8{} switch f.Slot { case DispenseSlotOff: - data[0] = C.can_msg_power_dispense_off + data[0] = uint8(C.can_msg_power_dispense_off) case 0: - data[0] = C.can_msg_power_dispense_slot1 + data[0] = uint8(C.can_msg_power_dispense_slot1) case 1: - data[0] = C.can_msg_power_dispense_slot2 + data[0] = uint8(C.can_msg_power_dispense_slot2) case 2: - data[0] = C.can_msg_power_dispense_slot3 + data[0] = uint8(C.can_msg_power_dispense_slot3) case 3: - data[0] = C.can_msg_power_dispense_slot4 + data[0] = uint8(C.can_msg_power_dispense_slot4) case 4: - data[0] = C.can_msg_power_dispense_slot5 + data[0] = uint8(C.can_msg_power_dispense_slot5) } return can.Frame{ ID: uint32(C.can_msg_power_dispense), @@ -284,7 +286,7 @@ func DecodeAutoMessage(frame can.Frame) (AutoMessage, error) { return ret, InvalidMessageLength } // we accept all non-null values as "enable" - if frame.Data[0] != C.can_msg_auto_status_disable { + if frame.Data[0] != uint8(C.can_msg_auto_status_disable) { ret.AutoUpdate = true } return ret, nil @@ -293,9 +295,9 @@ func DecodeAutoMessage(frame can.Frame) (AutoMessage, error) { func (f AutoMessage) Encode() can.Frame { data := [8]uint8{} if f.AutoUpdate { - data[0] = C.can_msg_auto_status_enable + data[0] = uint8(C.can_msg_auto_status_enable) } else { - data[0] = C.can_msg_auto_status_disable + data[0] = uint8(C.can_msg_auto_status_disable) } return can.Frame{ ID: uint32(C.can_msg_auto_status),