diff --git a/src/can2mqtt.go b/src/can2mqtt.go deleted file mode 100644 index c7709ee..0000000 --- a/src/can2mqtt.go +++ /dev/null @@ -1,48 +0,0 @@ -// minimal main package, only commandline argument parsing and a printHelp() function -package main - -import ( - "fmt" // print - C2M "github.com/c3re/can2mqtt/internal" - "os" // args -) - -// Parses commandline arguments -func main() { - conf := true - for i := 1; i < len(os.Args); i++ { - switch os.Args[i] { - case "-v": - C2M.SetDbg(true) - case "-c": - i++ - C2M.SetCi(os.Args[i]) - case "-m": - i++ - C2M.SetCs(os.Args[i]) - case "-f": - i++ - C2M.SetC2mf(os.Args[i]) - case "-d": - i++ - C2M.SetConfDirMode(os.Args[i]) - default: - i = len(os.Args) - conf = false - printHelp() - } - } - if conf { - C2M.Start() - } -} - -// help function (obvious...) -func printHelp() { - _, _ = fmt.Fprintf(os.Stderr, "welcome to the CAN2MQTT bridge!\n\n") - _, _ = fmt.Fprintf(os.Stderr, "Usage: can2mqtt [-f ] [-c ] [-m ] [-v] [-h] [-d ]\n") - _, _ = fmt.Fprintf(os.Stderr, ": a can2mqtt.csv file\n") - _, _ = fmt.Fprintf(os.Stderr, ": a CAN-Interface e.g. can0\n") - _, _ = fmt.Fprintf(os.Stderr, ": connectstring for MQTT. e.g.: tcp://[user:pass@]localhost:1883\n") - _, _ = fmt.Fprintf(os.Stderr, ": directional Mode 0 = bidirectional, 1 = can2mqtt only, 2 = mqtt2can only\n") -} diff --git a/src/internal/canbus.go b/src/canbus.go similarity index 93% rename from src/internal/canbus.go rename to src/canbus.go index 6bc9bb2..75c0573 100644 --- a/src/internal/canbus.go +++ b/src/canbus.go @@ -1,6 +1,4 @@ -// Package internal of c3re/can2mqtt contains some tools for bridging a CAN-Interface -// and a mqtt-network -package internal +package main import ( "github.com/brutella/can" diff --git a/src/internal/convertfunctions/bytecolor2colorcode.go b/src/convertfunctions/bytecolor2colorcode.go similarity index 100% rename from src/internal/convertfunctions/bytecolor2colorcode.go rename to src/convertfunctions/bytecolor2colorcode.go diff --git a/src/internal/convertfunctions/int2ascii.go b/src/convertfunctions/int2ascii.go similarity index 100% rename from src/internal/convertfunctions/int2ascii.go rename to src/convertfunctions/int2ascii.go diff --git a/src/internal/convertfunctions/none.go b/src/convertfunctions/none.go similarity index 100% rename from src/internal/convertfunctions/none.go rename to src/convertfunctions/none.go diff --git a/src/internal/convertfunctions/none_test.go b/src/convertfunctions/none_test.go similarity index 100% rename from src/internal/convertfunctions/none_test.go rename to src/convertfunctions/none_test.go diff --git a/src/internal/convertfunctions/pixelbin2ascii.go b/src/convertfunctions/pixelbin2ascii.go similarity index 100% rename from src/internal/convertfunctions/pixelbin2ascii.go rename to src/convertfunctions/pixelbin2ascii.go diff --git a/src/internal/convertfunctions/sixteenbool2ascii.go b/src/convertfunctions/sixteenbool2ascii.go similarity index 100% rename from src/internal/convertfunctions/sixteenbool2ascii.go rename to src/convertfunctions/sixteenbool2ascii.go diff --git a/src/internal/convertfunctions/uint2ascii.go b/src/convertfunctions/uint2ascii.go similarity index 100% rename from src/internal/convertfunctions/uint2ascii.go rename to src/convertfunctions/uint2ascii.go diff --git a/src/internal/main.go b/src/main.go similarity index 73% rename from src/internal/main.go rename to src/main.go index 7b5a6f3..f181185 100644 --- a/src/internal/main.go +++ b/src/main.go @@ -1,11 +1,10 @@ -package internal +package main import ( "bufio" // Reader "encoding/csv" // CSV Management - "fmt" // print :) - "github.com/brutella/can" - "github.com/c3re/can2mqtt/internal/convertfunctions" + "flag" + "github.com/c3re/can2mqtt/convertfunctions" "io" // EOF const "log" // error management "log/slog" @@ -14,86 +13,36 @@ import ( "sync" ) -type convertToCan func(input []byte) (can.Frame, error) -type convertToMqtt func(input can.Frame) ([]byte, error) - -type ConvertMode interface { - convertToCan - convertToMqtt -} - -// can2mqtt is a struct that represents the internal type of -// one line of the can2mqtt.csv file. It has -// the same three fields as the can2mqtt.csv file: CAN-ID, -// conversion method and MQTT-Topic. -type can2mqtt struct { - canId uint32 - convMethod string - toCan convertToCan - toMqtt convertToMqtt - mqttTopic string -} - var pairFromID map[uint32]*can2mqtt // c2m pair (lookup from ID) var pairFromTopic map[string]*can2mqtt // c2m pair (lookup from Topic) -var dbg = false // verbose on off [-v] -var ci = "can0" // the CAN-Interface [-c] -var cs = "tcp://localhost:1883" // mqtt-connect-string [-m] -var c2mf = "can2mqtt.csv" // path to the can2mqtt.csv [-f] -var dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d] +var debugLog bool +var canInterface, mqttConnection, configFile string +var dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d] var wg sync.WaitGroup -// SetDbg decides whether there is really verbose output or -// just standard information output. Default is false. -func SetDbg(v bool) { - dbg = v - slog.SetLogLoggerLevel(slog.LevelDebug) -} - -// SetCi sets the CAN-Interface to use for the CAN side -// of the bridge. Default is: can0. -func SetCi(c string) { - ci = c -} +func main() { + log.SetFlags(0) -// SetC2mf expects a string which is a path to a can2mqtt.csv file -// Default is: can2mqtt.csv -func SetC2mf(f string) { - c2mf = f -} + flag.BoolVar(&debugLog, "v", false, "show (very) verbose debug log") + flag.StringVar(&canInterface, "c", "can0", "which socket-can interface to use") + flag.StringVar(&mqttConnection, "m", "tcp://localhost:1883", "which mqtt-broker to use. Example: tcp://user:password@broker.hivemq.com:1883") + flag.StringVar(&configFile, "f", "can2mqtt.csv", "which config file to use") + flag.IntVar(&dirMode, "d", 0, "direction mode\n0: bidirectional (default)\n1: can2mqtt only\n2: mqtt2can only") + flag.Parse() -// SetCs sets the MQTT connect-string which contains: protocol, -// hostname and port. Default is: tcp://localhost:1883 -func SetCs(s string) { - cs = s -} + if dirMode < 0 || dirMode > 2 { + slog.Error("got invalid value for -d. Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", "d", dirMode) + } -// SetConfDirMode sets the dirMode -func SetConfDirMode(s string) { - if s == "0" { - dirMode = 0 - } else if s == "1" { - dirMode = 1 - } else if s == "2" { - dirMode = 2 - } else { - _ = fmt.Errorf("error: got invalid value for -d (%s). Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", s) + if debugLog { + slog.SetLogLoggerLevel(slog.LevelDebug) } -} -// Start is the function that should be called after debug-level -// connect-string, can interface and can2mqtt file have been set. -// Start takes care of everything that happens after that. -// It starts the CAN-Bus connection and the MQTT-Connection. It -// parses the can2mqtt.csv file and from there everything takes -// its course... -func Start() { - log.SetFlags(0) - slog.Info("Starting can2mqtt", "mqtt-config", cs, "can-interface", ci, "can2mqtt.csv", c2mf, "dir-mode", dirMode, "debug", dbg) + slog.Info("Starting can2mqtt", "mqtt-config", mqttConnection, "can-interface", canInterface, "can2mqtt.csv", configFile, "dir-mode", dirMode, "debug", debugLog) wg.Add(1) - go canStart(ci) // epic parallel shit ;-) - mqttStart(cs) - readC2MPFromFile(c2mf) + go canStart(canInterface) // epic parallel shit ;-) + mqttStart(mqttConnection) + readC2MPFromFile(configFile) wg.Wait() } diff --git a/src/internal/mqtt.go b/src/mqtt.go similarity index 99% rename from src/internal/mqtt.go rename to src/mqtt.go index 8cf9341..4a8a062 100644 --- a/src/internal/mqtt.go +++ b/src/mqtt.go @@ -1,4 +1,4 @@ -package internal +package main import ( MQTT "github.com/eclipse/paho.mqtt.golang" diff --git a/src/internal/receiving.go b/src/receiving.go similarity index 99% rename from src/internal/receiving.go rename to src/receiving.go index eee10e9..ae94b3e 100644 --- a/src/internal/receiving.go +++ b/src/receiving.go @@ -1,4 +1,4 @@ -package internal +package main import ( "github.com/brutella/can" diff --git a/src/types.go b/src/types.go new file mode 100644 index 0000000..cb87f5f --- /dev/null +++ b/src/types.go @@ -0,0 +1,18 @@ +package main + +import "github.com/brutella/can" + +type convertToCan func(input []byte) (can.Frame, error) +type convertToMqtt func(input can.Frame) ([]byte, error) + +// can2mqtt is a struct that represents the internal type of +// one line of the can2mqtt.csv file. It has +// the same three fields as the can2mqtt.csv file: CAN-ID, +// conversion method and MQTT-Topic. +type can2mqtt struct { + canId uint32 + convMethod string + toCan convertToCan + toMqtt convertToMqtt + mqttTopic string +}