- Notifications
You must be signed in to change notification settings - Fork8
Implementation of a ZNP and support code designed to interface with Texas Instruments Z-Stack, written in Go.
License
shimmeringbee/zstack
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Implementation of a ZNP and support code designed to interface with Texas Instruments Z-Stack, written in Go.
Z-Stack is a Zigbee Stack made available by Texas Instruments for use on their CC 2.4Ghz SOC processors. Thislibrary implements a Zigbee Network Processor that is capable of controlling a Z-Stack implementation, specifically itsupports the CC series of Zigbee sniffers flashed with thezigbee2mqtt Z-Stack coordinator firmware.
More information about Z-Stack is available fromTexas Instruments directly or fromZ-Stack Developer's Guide.
Another implementation of a Z-Stack compatible ZNP exists for Golang, it didhold no license for a periodand the author could not be contacted. This has been rectified, so it may be of interest you. This is a completereimplementation of the library, however it is likely there will be strong coincidences due to Golang standards.
The following chips and sticks are known to work, though it's likely others in the series will too:
- CC253X
- Cheap Zigbee Sniffers from AliExpress - CC2531
- CC26X2R1
- Electrolama zig-a-sig-ah! - CC2652R
Huge thanks to @Koenkk for his work in providing Z-Stack firmware for these chips. You cangrab the firmware from GitHub.
Add an import and most IDEs willgo get
automatically, if it doesn'tgo build
will fetch.
import"github.com/shimmeringbee/zstack"
This libraries API is unstable and should not yet be relied upon.
/* Obtain a ReadWriter UART interface to CC253X */serialPort:=/* Construct node table, cache of network nodes. */t:=zstack.NewNodeTable()/* Create a new ZStack struct. */z:=zstack.New(serialPort,t)/* Generate random Zigbee network, on default channel (15) */netCfg,_:=zigbee.GenerateNetworkConfiguration()/* Obtain context for timeout of initialisation. */ctx,cancel:=context.WithTimeout(context.Background(),2*time.Minute)defercancel()/* Initialise ZStack and CC253X */)err=z.Initialise(ctx,nc)
It is critical that this is handled until you wish to stop the Z-Stack instance.
for {ctx:=context.Background()event,err:=z.ReadEvent(ctx)iferr!=nil {return }switche:=event.(type) {case zigbee.NodeJoinEvent:log.Printf("join: %v\n",e.Node)goexploreDevice(z,e.Node)case zigbee.NodeLeaveEvent:log.Printf("leave: %v\n",e.Node)case zigbee.NodeUpdateEvent:log.Printf("update: %v\n",e.Node)case zigbee.NodeIncomingMessageEvent:log.Printf("message: %v\n",e) }}
err:=z.PermitJoin(ctx,true)
err:=z.DenyJoin(ctx)
funcexploreDevice(z*zstack.ZStack,node zigbee.Node) {log.Printf("node %v: querying",node.IEEEAddress)ctx,cancel:=context.WithTimeout(context.Background(),1*time.Minute)defercancel()descriptor,err:=z.QueryNodeDescription(ctx,node.IEEEAddress)iferr!=nil {log.Printf("failed to get node descriptor: %v",err)return}log.Printf("node %v: descriptor: %+v",node.IEEEAddress,descriptor)endpoints,err:=z.QueryNodeEndpoints(ctx,node.IEEEAddress)iferr!=nil {log.Printf("failed to get node endpoints: %v",err)return}log.Printf("node %v: endpoints: %+v",node.IEEEAddress,endpoints)for_,endpoint:=rangeendpoints {endpointDes,err:=z.QueryNodeEndpointDescription(ctx,node.IEEEAddress,endpoint)iferr!=nil {log.Printf("failed to get node endpoint description: %v / %d",err,endpoint)}else {log.Printf("node %v: endpoint: %d desc: %+v",node.IEEEAddress,endpoint,endpointDes)}}}
zstack
requires aNodeTable
structure to cache a devices IEEE address to its Zibgee network address. A designdecision forzstack
was that all operations would reference the IEEE address. This cache must be persisted betweenprogram runs as the coordinator hardware does not retain this information between restarts.
// Create new tablenodeTable:=NewNodeTable()// Dump current contentnodes:=nodeTable.Nodes()// Load previous content - this should be done before starting ZStack.nodeTable.Load(nodes)
To handle ZCL messages you must handlezigbee.NodeIncomingMessageEvent
messages and process the ZCL payload with the ZCL library, responses can be sent withz.SendNodeMessage
.
Feel free to dive in!Open an issue or submit PRs.
All Shimmering Bee projects follow theContributor Covenant Code of Conduct.
Copyright 2019-2020 Shimmering Bee Contributors
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.