-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
112 lines (99 loc) · 3.31 KB
/
connect.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
package main
import (
"fmt"
"encoding/hex"
"time"
"github.com/bettercap/gatt"
)
const HANDLE_AUTH_REQUEST = uint16(0x0055)
const HANDLE_CONN_CONTROL = uint16(0x0054)
const RESPONSE_CODE_PAIR = "100101"
const RESPONSE_CODE_RANDOM = "100201"
const RESPONSE_CODE_CONNECTED = "100301"
func (p *program) sendRequest(vhandle uint16, b []byte, per gatt.Peripheral) error {
c := &gatt.Characteristic{}
c.SetVHandle(vhandle)
return per.WriteCharacteristic(c, b, true)
}
func (p *program) sendAuthNotification(per gatt.Peripheral) error {
fmt.Println("Sending authorization request...")
if err := p.sendRequest(HANDLE_AUTH_REQUEST, []byte{1, 0}, per); err != nil {
fmt.Printf("Failed to send auth notification, err: %s\n", err)
return err
}
fmt.Println("Authorization request was sent")
return p.sendEncryptionKey(per)
}
func (p *program) sendEncryptionKey(per gatt.Peripheral) error {
message := "Sending encryption key..."
if err := p.reRunWhenUnlocked(per, p.state.isPaired, append([]byte{1, 0}, p.secret.key...), 3, 10, message); err != nil {
fmt.Printf("Failed to send encryption key, err: %s\n", err)
return err
}
fmt.Println("Encryption key was sent")
return p.requestRandomKey(per)
}
func (p *program) requestRandomKey(per gatt.Peripheral) error {
message := "Requesting random key..."
if err := p.reRunWhenUnlocked(per, p.state.isRandomNumber, []byte{2, 0}, 3, 10, message); err != nil {
fmt.Printf("Failed to send encryption key, err: %s\n", err)
return err
}
fmt.Println("Random key received")
return p.confirmPairing(per)
}
func (p *program) confirmPairing(per gatt.Peripheral) error {
encryptedNumbers, err := hex.DecodeString(p.state.RandomString())
if err != nil {
return err
}
p.secret.Encrypt(encryptedNumbers)
message := "Sending encrypted random key..."
if err := p.reRunWhenUnlocked(per, p.state.isConnected, append([]byte{3, 0}, encryptedNumbers...), 3, 10, message); err != nil {
fmt.Printf("Failed to send pairing confirmation, err: %s\n", err)
return err
}
fmt.Println("Encrypted random key was sent")
return nil
}
func (p *program) pairPeripheral(per gatt.Peripheral) {
// Subscribe to change 0x0054 characteristic.
c := &gatt.Characteristic{}
c.SetVHandle(HANDLE_CONN_CONTROL)
d := &gatt.Descriptor{}
d.SetHandle(HANDLE_CONN_CONTROL)
c.SetDescriptor(d)
per.SetNotifyValue(c, p.onChangeConnControl)
if err := p.sendAuthNotification(per); err != nil {
fmt.Printf("Failed to pair device, err: %s\n", err)
p.Stop()
}
}
func (p *program) reRunWhenUnlocked(per gatt.Peripheral, check func() bool, data []byte, attempts int, seconds time.Duration, message string) (err error) {
for i := 1; i < attempts && !check(); i++ {
fmt.Println(message)
err = p.sendRequest(HANDLE_CONN_CONTROL, data, per)
time.Sleep(seconds * time.Second)
}
return err
}
func (p *program) onChangeConnControl(c *gatt.Characteristic, data []byte, err error) {
code := hex.EncodeToString(data[:3])
switch code {
case RESPONSE_CODE_PAIR:
// Success pairing
p.state.Paired()
break
case RESPONSE_CODE_RANDOM:
// Received random key from device, 16 bytes without first 3.
randomString := hex.EncodeToString(data[3:19])
p.state.SetRandomString(randomString)
break
case RESPONSE_CODE_CONNECTED:
// Received random key from device.
p.state.Connected()
break
default:
fmt.Printf("Event: %s", code)
}
}