-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneral.go
179 lines (146 loc) · 5.27 KB
/
general.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
package nuki
import (
"context"
"fmt"
"github.com/go-ble/ble"
"github.com/kevinburke/nacl"
"github.com/tarent/go-nuki/communication"
"github.com/tarent/go-nuki/communication/command"
"time"
)
// ConnectionNotEstablishedError will be returned if the connection is not established before
var ConnectionNotEstablishedError = fmt.Errorf("the connection is not established")
// UnauthenticatedError will be returned if the client is not authenticated before
var UnauthenticatedError = fmt.Errorf("the client is not authenticated")
// InvalidPinError will be returned if the given pin is invalid
var InvalidPinError = fmt.Errorf("the given pin is invalid")
type Client struct {
client ble.Client
responseTimeout time.Duration
privateKey nacl.Key
publicKey nacl.Key
nukiPublicKey []byte
authId command.AuthorizationId
gdioCom communication.Communicator
udioCom communication.Communicator
}
func NewClient(bleDevice ble.Device) *Client {
ble.SetDefaultDevice(bleDevice)
return &Client{
responseTimeout: 10 * time.Second,
}
}
// WithTimeout sets the timeout which is used for each response waiting.
func (c *Client) WithTimeout(duration time.Duration) *Client {
c.responseTimeout = duration
return c
}
// EstablishConnection establish a connection to the given nuki device.
// Returns an error if there was a problem with connecting to the device.
func (c *Client) EstablishConnection(ctx context.Context, deviceAddress ble.Addr) error {
bleClient, err := ble.Dial(ctx, deviceAddress)
if err != nil {
return fmt.Errorf("error while establish connection: %w", err)
}
c.client = bleClient
c.gdioCom, err = communication.NewGeneralDataIOCommunicator(bleClient)
if err != nil {
return fmt.Errorf("error while establish communication: %w", err)
}
//in case of "re-establish" a connection (for example after reboot the device)
if c.nukiPublicKey != nil {
return c.Authenticate(c.privateKey, c.publicKey, c.nukiPublicKey, c.authId)
}
return nil
}
// GetDeviceType will return the discovered type of the connected device.
func (c *Client) GetDeviceType() communication.DeviceType {
if c.gdioCom == nil {
return communication.DeviceTypeUnknown
}
return c.gdioCom.GetDeviceType()
}
// GeneralDataIOCommunicator will return the communicator which is responsible for general data io.
// This is only available after the connection is established (EstablishConnection)
func (c *Client) GeneralDataIOCommunicator() communication.Communicator {
return c.gdioCom
}
// UserSpecificDataIOCommunicator will return the communicator which is responsible for user specific data io.
// This is only available after the connection is established (EstablishConnection) and the authentication is done (Pair or Authenticate).
func (c *Client) UserSpecificDataIOCommunicator() communication.Communicator {
return c.udioCom
}
// Close will close all underlying resources. This function should be called after
// the client will not be used anymore.
func (c *Client) Close() error {
errors := make([]error, 0, 3)
if c.gdioCom != nil {
if err := c.gdioCom.Close(); err != nil {
errors = append(errors, err)
}
c.gdioCom = nil
}
if c.udioCom != nil {
if err := c.udioCom.Close(); err != nil {
errors = append(errors, err)
}
c.udioCom = nil
}
if c.client != nil {
if err := c.client.Conn().Close(); err != nil {
errors = append(errors, err)
}
c.client = nil
}
if len(errors) > 0 {
return fmt.Errorf("error while closing resources: [%v]", errors)
}
return nil
}
// PerformAction will request the connected and paired nuki opener to perform the given command.
func (c *Client) PerformAction(ctx context.Context, actionBuilder func(nonce []byte) command.Command) error {
if c.client == nil {
return ConnectionNotEstablishedError
}
if c.udioCom == nil {
return UnauthenticatedError
}
err := c.udioCom.Send(command.NewRequest(command.IdChallenge))
if err != nil {
return fmt.Errorf("unable to send request for challenge: %w", err)
}
challenge, err := c.udioCom.WaitForSpecificResponse(ctx, command.IdChallenge, c.responseTimeout)
if err != nil {
return fmt.Errorf("error while waiting for challenge: %w", err)
}
toSend := actionBuilder(challenge.AsChallengeCommand().Nonce())
err = c.udioCom.Send(toSend)
if err != nil {
return fmt.Errorf("unable to send action: %w", err)
}
status, err := c.udioCom.WaitForSpecificResponse(ctx, command.IdStatus, c.responseTimeout)
if err != nil {
return fmt.Errorf("error while waiting for status: %w", err)
}
if status.AsStatusCommand().IsAccepted() {
// This will be returned to signal that a command has been accepted but the completion status will be signaled later.
// So here we just wait for the second status.
status, err = c.udioCom.WaitForSpecificResponse(ctx, command.IdStatus, c.responseTimeout)
if err != nil {
return fmt.Errorf("error while waiting for status: %w", err)
}
if !status.AsStatusCommand().IsComplete() {
return fmt.Errorf("unexpected status: expect 0x%02x got 0x%02x", command.CompletionStatusComplete, status.AsStatusCommand().Status())
}
}
return nil
}
func (c *Client) checkPreconditionAndParsePin(pin string) (command.Pin, error) {
if c.client == nil {
return 0, ConnectionNotEstablishedError
}
if c.udioCom == nil {
return 0, UnauthenticatedError
}
return command.NewPin(pin)
}