-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality for removing nodes and fmt.
- Loading branch information
Showing
7 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package zstack | ||
|
||
import ( | ||
"context" | ||
"github.com/shimmeringbee/zigbee" | ||
) | ||
|
||
func (z *ZStack) RemoveNode(ctx context.Context, nodeAddress zigbee.IEEEAddress) error { | ||
networkAddress, err := z.ResolveNodeNWKAddress(ctx, nodeAddress) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
request := ZdoMgmtLeaveReq{ | ||
NetworkAddress: networkAddress, | ||
IEEEAddress: nodeAddress, | ||
RemoveChildren: false, | ||
} | ||
|
||
_, err = z.nodeRequest(ctx, &request, &ZdoMgmtLeaveReqReply{}, &ZdoMgmtLeaveRsp{}, func(i interface{}) bool { | ||
msg := i.(*ZdoMgmtLeaveRsp) | ||
return msg.SourceAddress == networkAddress | ||
}) | ||
|
||
return err | ||
} | ||
|
||
type ZdoMgmtLeaveReq struct { | ||
NetworkAddress zigbee.NetworkAddress | ||
IEEEAddress zigbee.IEEEAddress | ||
RemoveChildren bool | ||
} | ||
|
||
const ZdoMgmtLeaveReqID uint8 = 0x34 | ||
|
||
type ZdoMgmtLeaveReqReply GenericZStackStatus | ||
|
||
func (r ZdoMgmtLeaveReqReply) WasSuccessful() bool { | ||
return r.Status == ZSuccess | ||
} | ||
|
||
const ZdoMgmtLeaveReqReplyID uint8 = 0x34 | ||
|
||
type ZdoMgmtLeaveRsp struct { | ||
SourceAddress zigbee.NetworkAddress | ||
Status ZStackStatus | ||
} | ||
|
||
func (r ZdoMgmtLeaveRsp) WasSuccessful() bool { | ||
return r.Status == ZSuccess | ||
} | ||
|
||
const ZdoMgmtLeaveRspID uint8 = 0xb4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package zstack | ||
|
||
import ( | ||
"context" | ||
"github.com/shimmeringbee/bytecodec" | ||
. "github.com/shimmeringbee/unpi" | ||
unpiTest "github.com/shimmeringbee/unpi/testing" | ||
"github.com/shimmeringbee/zigbee" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestZStack_RemoveNode(t *testing.T) { | ||
t.Run("returns an success on query, response for requested network address is received", func(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) | ||
defer cancel() | ||
|
||
unpiMock := unpiTest.NewMockAdapter() | ||
zstack := New(unpiMock, NewNodeTable()) | ||
defer unpiMock.Stop() | ||
|
||
call := unpiMock.On(SREQ, ZDO, ZdoMgmtLeaveReqReplyID).Return(Frame{ | ||
MessageType: SRSP, | ||
Subsystem: ZDO, | ||
CommandID: ZdoMgmtLeaveReqReplyID, | ||
Payload: []byte{0x00}, | ||
}) | ||
|
||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
unpiMock.InjectOutgoing(Frame{ | ||
MessageType: AREQ, | ||
Subsystem: ZDO, | ||
CommandID: ZdoMgmtLeaveRspID, | ||
Payload: []byte{0x00, 0x40, 0x00}, | ||
}) | ||
}() | ||
|
||
zstack.nodeTable.addOrUpdate(zigbee.IEEEAddress(1), zigbee.NetworkAddress(0x4000)) | ||
|
||
err := zstack.RemoveNode(ctx, zigbee.IEEEAddress(1)) | ||
assert.NoError(t, err) | ||
|
||
leaveReq := ZdoMgmtLeaveReq{} | ||
bytecodec.Unmarshal(call.CapturedCalls[0].Frame.Payload, &leaveReq) | ||
|
||
assert.Equal(t, zigbee.IEEEAddress(1), leaveReq.IEEEAddress) | ||
assert.Equal(t, zigbee.NetworkAddress(0x4000), leaveReq.NetworkAddress) | ||
assert.False(t, leaveReq.RemoveChildren) | ||
|
||
unpiMock.AssertCalls(t) | ||
}) | ||
} | ||
|
||
func Test_RemoveMessages(t *testing.T) { | ||
t.Run("verify ZdoMgmtLeaveReq marshals", func(t *testing.T) { | ||
req := ZdoMgmtLeaveReq{ | ||
NetworkAddress: 0x1234, | ||
IEEEAddress: zigbee.IEEEAddress(0x8899aabbccddeeff), | ||
RemoveChildren: true, | ||
} | ||
|
||
data, err := bytecodec.Marshal(req) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{0x34, 0x12, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x01}, data) | ||
}) | ||
|
||
t.Run("verify ZdoMgmtLeaveReqReply marshals", func(t *testing.T) { | ||
req := ZdoMgmtLeaveReqReply{ | ||
Status: 1, | ||
} | ||
|
||
data, err := bytecodec.Marshal(req) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{0x01}, data) | ||
}) | ||
|
||
t.Run("ZdoMgmtLeaveReqReply returns true if success", func(t *testing.T) { | ||
g := ZdoMgmtLeaveReqReply{Status: ZSuccess} | ||
assert.True(t, g.WasSuccessful()) | ||
}) | ||
|
||
t.Run("ZdoMgmtLeaveReqReply returns false if not success", func(t *testing.T) { | ||
g := ZdoMgmtLeaveReqReply{Status: ZFailure} | ||
assert.False(t, g.WasSuccessful()) | ||
}) | ||
|
||
t.Run("verify ZdoMgmtLeaveRsp marshals", func(t *testing.T) { | ||
req := ZdoMgmtLeaveRsp{ | ||
SourceAddress: zigbee.NetworkAddress(0x2000), | ||
Status: 1, | ||
} | ||
|
||
data, err := bytecodec.Marshal(req) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{0x00, 0x20, 0x01}, data) | ||
}) | ||
|
||
t.Run("ZdoMgmtLeaveRsp returns true if success", func(t *testing.T) { | ||
g := ZdoMgmtLeaveRsp{Status: ZSuccess} | ||
assert.True(t, g.WasSuccessful()) | ||
}) | ||
|
||
t.Run("ZdoMgmtLeaveRsp returns false if not success", func(t *testing.T) { | ||
g := ZdoMgmtLeaveRsp{Status: ZFailure} | ||
assert.False(t, g.WasSuccessful()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters