1 // (c) Bernhard Tittelbach, 2013
11 //~ func StringArrayToByteArray(ss []string) [][]byte {
12 //~ bb := make([][]byte, len(ss))
13 //~ for index, s := range(ss) {
14 //~ bb[index] = []byte(s)
19 // ---------- Main Code -------------
30 flag.StringVar(&cmd_port_, "cmdport", "ipc:///run/tuer/door_cmd.ipc", "zmq command socket path")
31 flag.StringVar(&pub_port_, "pubport", "tcp://*:4242", "zmq public/listen socket path")
32 flag.StringVar(&door_tty_path_, "device", "/dev/door", "door tty device path")
33 flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog")
34 flag.BoolVar(&enable_debug_, "debug", false, "enable debug output")
39 if enable_syslog_ { LogEnableSyslog()}
40 if enable_debug_ { LogEnableDebuglog()}
41 Syslog_.Print("started")
42 defer Syslog_.Print("exiting")
44 zmqctx, cmd_chans, pub_chans := ZmqsInit(cmd_port_, pub_port_)
45 defer cmd_chans.Close()
46 defer pub_chans.Close()
49 serial_wr, serial_rd, err := OpenAndHandleSerial(door_tty_path_)
50 defer close(serial_wr)
55 var next_incoming_serial_is_client_reply bool
56 timeout_chan := make(chan bool)
57 defer close(timeout_chan)
60 case incoming_ser_line, is_notclosed := <- serial_rd:
62 //~ if Syslog_ != nil { Syslog_.Print(ByteArrayToString(incoming_ser_line)) }
63 if Syslog_ != nil { Syslog_.Printf("%s",incoming_ser_line) }
64 if next_incoming_serial_is_client_reply {
65 next_incoming_serial_is_client_reply = false
66 cmd_chans.Out() <- incoming_ser_line
68 pub_chans.Out() <- incoming_ser_line
70 Syslog_.Print("serial device disappeared, exiting")
73 case tv, timeout_notclosed := <- timeout_chan:
74 if timeout_notclosed && tv && next_incoming_serial_is_client_reply {
75 next_incoming_serial_is_client_reply = false
76 cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("No reply from firmware")}
78 case incoming_request, ic_notclosed := <- cmd_chans.In():
80 Syslog_.Print("zmq socket died, exiting")
83 if string(incoming_request[0]) == "log" {
84 if len(incoming_request) < 2 {
85 cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("argument missing")}
88 Syslog_.Printf("Log: %s",incoming_request[1:])
89 cmd_chans.Out() <- [][]byte{[]byte("Ok")}
92 Syslog_.Printf("%s",incoming_request)
93 if err := HandleCommand(incoming_request, serial_wr, serial_rd); err != nil {
94 out_msg := [][]byte{[]byte("ERROR"), []byte(err.Error())}
95 cmd_chans.Out() <- out_msg
97 pub_chans.Out() <- incoming_request
98 next_incoming_serial_is_client_reply = true
99 go func(){time.Sleep(3*time.Second); timeout_chan <- true;}()