-
Notifications
You must be signed in to change notification settings - Fork 1
/
communicator.go
68 lines (58 loc) · 1.42 KB
/
communicator.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
package bluetooth
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
)
type Params struct {
Address string
Log Log
}
var _ Communicator = Printer{}
// Printer is a mock implementation of Communicator
type Printer struct {
log Log
}
func (p Printer) Read(dataLen int) (int, []byte, error) {
return dataLen, nil, nil
}
func (p Printer) Write(d []byte) (int, error) {
p.log.Print(fmt.Sprintf("PRINTER %d >>> %v", len(d), d))
return len(d), nil
}
func (p Printer) Close() error {
return nil
}
// addressToByteArray converts MAC address string representation to little-endian byte array
func addressToByteArray(addr string) [6]byte {
a := strings.Split(addr, ":")
var b [6]byte
for i, tmp := range a {
u, _ := strconv.ParseUint(tmp, 16, 8)
b[len(b)-1-i] = byte(u)
}
return b
}
func byteArrayToAddress(addr [6]byte) string {
var resultSet []string
for i := range addr {
resultSet = append(resultSet, strings.ToUpper(hex.EncodeToString([]byte{addr[i]})))
}
return strings.Join(resultSet, ":")
}
// addressToUint64 converts MAC address string to uint64
func addressToUint64(address string) (uint64, error) {
addressParts := strings.Split(address, ":")
addressPartsLength := len(addressParts)
var result uint64
for i, tmp := range addressParts {
u, err := strconv.ParseUint(tmp, 16, 8)
if err != nil {
return 0, err
}
push := 8 * (addressPartsLength - 1 - i)
result += u << push
}
return result, nil
}