-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjoining.go
61 lines (47 loc) · 1.49 KB
/
joining.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
package zstack
import (
"context"
"fmt"
"github.com/shimmeringbee/zigbee"
)
func (z *ZStack) PermitJoin(ctx context.Context, allRouters bool) error {
if allRouters {
return z.sendJoin(ctx, zigbee.BroadcastRoutersCoordinators, JoiningOn, OnAllRouters)
} else {
return z.sendJoin(ctx, z.NetworkProperties.NetworkAddress, JoiningOn, OnCoordinator)
}
}
func (z *ZStack) DenyJoin(ctx context.Context) error {
return z.sendJoin(ctx, zigbee.BroadcastRoutersCoordinators, JoiningOff, Off)
}
func (z *ZStack) sendJoin(ctx context.Context, address zigbee.NetworkAddress, timeout uint8, newState JoinState) error {
if err := z.sem.Acquire(ctx, 1); err != nil {
return fmt.Errorf("failed to acquire semaphore: %w", err)
}
defer z.sem.Release(1)
response := ZDOMgmtPermitJoinRequestReply{}
if err := z.requestResponder.RequestResponse(ctx, ZDOMgmtPermitJoinRequest{
Destination: address,
Duration: timeout,
TCSignificance: 0x00,
}, &response); err != nil {
return err
}
if response.Status != ZSuccess {
return fmt.Errorf("adapter rejected permit join state change: state=%v", response.Status)
}
z.NetworkProperties.JoinState = newState
return nil
}
const (
JoiningOff uint8 = 0x00
JoiningOn uint8 = 0xff
)
type ZDOMgmtPermitJoinRequest struct {
Destination zigbee.NetworkAddress
Duration uint8
TCSignificance uint8
}
const ZDOMgmtPermitJoinRequestID = 0x36
type ZDOMgmtPermitJoinRequestReply GenericZStackStatus
const ZDOMgmtPermitJoinRequestReplyID uint8 = 0x36