-
Notifications
You must be signed in to change notification settings - Fork 6
/
lirc.go
214 lines (189 loc) · 4.28 KB
/
lirc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package lirc
import (
"bufio"
"encoding/hex"
"errors"
"log"
"net"
"strconv"
"strings"
"time"
)
// Router manages sending and receiving of commands / data
type Router struct {
handlers map[remoteButton]Handle
path string
connection net.Conn
writer *bufio.Writer
reply chan Reply
receive chan Event
}
// Event represents the IR Remote Key Press Event
type Event struct {
Code uint64
Repeat int
Button string
Remote string
}
// Reply received when a command is sent
type Reply struct {
Command string
Success int
DataLength int
Data []string
}
// Init initializes the connection to lirc daemon
func Init(path string) (*Router, error) {
l := new(Router)
c, err := net.Dial("unix", path)
if err != nil {
return nil, err
}
l.path = path
l.writer = bufio.NewWriter(c)
l.reply = make(chan Reply)
l.receive = make(chan Event)
scanner := bufio.NewScanner(c)
go reader(scanner, l.receive, l.reply)
return l, nil
}
func reader(scanner *bufio.Scanner, receive chan Event, reply chan Reply) {
const (
RECEIVE = iota
REPLY
MESSAGE
STATUS
DATA_START
DATA_LEN
DATA
END
)
var message Reply
state := RECEIVE
dataCnt := 0
for scanner.Scan() {
line := scanner.Text()
switch state {
case RECEIVE:
if line == "BEGIN" {
state = REPLY
} else {
r := strings.Split(line, " ")
c, err := hex.DecodeString(r[0])
if err != nil {
log.Println("Invalid lirc broadcats message received - code not parseable")
continue
}
if len(c) != 8 {
log.Println("Invalid lirc broadcats message received - code has wrong length")
continue
}
var code uint64
code = 0
for i := 0; i < 8; i++ {
code &= uint64(c[i]) << uint(8*i)
}
var event Event
event.Repeat, err = strconv.Atoi(r[1])
if err != nil {
log.Println("Invalid lirc broadcats message received - invalid repeat count")
}
event.Code = code
event.Button = r[2]
event.Remote = r[3]
receive <- event
}
case REPLY:
message.Command = line
message.Success = 0
message.DataLength = 0
message.Data = message.Data[:0]
state = STATUS
case STATUS:
if line == "SUCCESS" {
message.Success = 1
state = DATA_START
} else if line == "END" {
message.Success = 1
state = RECEIVE
reply <- message
} else if line == "ERROR" {
message.Success = 0
state = DATA_START
} else {
log.Println("Invalid lirc reply message received - invalid status")
state = RECEIVE
}
case DATA_START:
if line == "END" {
state = RECEIVE
reply <- message
} else if line == "DATA" {
state = DATA_LEN
} else {
log.Println("Invalid lirc reply message received - invalid data start")
state = RECEIVE
}
case DATA_LEN:
dataCnt = 0
var err error
message.DataLength, err = strconv.Atoi(line)
if err != nil {
log.Println("Invalid lirc reply message received - invalid data len")
state = RECEIVE
} else {
state = DATA
}
case DATA:
if dataCnt < message.DataLength {
message.Data = append(message.Data, line)
}
dataCnt++
if dataCnt == message.DataLength {
state = END
}
case END:
state = RECEIVE
if line == "END" {
reply <- message
} else {
log.Println("Invalid lirc reply message received - invalid end")
}
}
}
if err := scanner.Err(); err != nil {
log.Println("error reading from lircd socket")
}
}
// Command - Send any command to lircd
func (l *Router) Command(command string) Reply {
l.writer.WriteString(command + "\n")
l.writer.Flush()
reply := <-l.reply
return reply
}
// Send a SEND_ONCE command
func (l *Router) Send(command string) error {
reply := l.Command("SEND_ONCE " + command)
if reply.Success == 0 {
return errors.New(strings.Join(reply.Data, " "))
}
return nil
}
// SendLong sends a SEND_START command followed by a delay and SEND_STOP`
func (l *Router) SendLong(command string, delay time.Duration) error {
reply := l.Command("SEND_START " + command)
if reply.Success == 0 {
return errors.New(strings.Join(reply.Data, " "))
}
time.Sleep(delay)
reply = l.Command("SEND_STOP " + command)
if reply.Success == 0 {
return errors.New(strings.Join(reply.Data, " "))
}
return nil
}
// Close the connection to lirc daemon
func (l *Router) Close() {
l.connection.Close()
}