设备如何使用Network rejoin方式加入网络?
本文主要讲解设备在拥有网络信息的情况下,如何通过NWK rejoin的方式入网。通常可通过外部的方式将网络信息传送给某设备节点,该设备节点获取所需要的网络信息后,直接通过NWK rejoin加入网络。此时该网络不需要处于允许设备入网状态(即PJoin状态未开启也可以入网)。下面通过一个例子来讲解该过程。本实验使用两块WSTK+4151radio板。
首先,将UART Bootloader以及NCP firmware烧录一个4151radio板。将板子连接电脑,使用Simplicity Command进行烧录,在CM中输入如下命令:
C:\Users\Lesun>commander flash C:\SiliconLabs\SimplicityStudio\v4\developer\sdks\gecko_sdk_suite\v1.1\protocol\zigbee_5.10\ncp-images\efr32mg1p232f256gm48\ncp-uart-rts-cts-use-with-serial-uart-btl-5.10.1.s37 C:\SiliconLabs\SimplicityStudio\v4\developer\sdks\gecko_sdk_suite\v1.1\protocol\zigbee_5.10\tool\bootloader-efr32mg1p232f256gm48\serial-uart-bootloader\serial-uart-bootloader.s37
其次,在Cgywin中编译Z3Gateway sample,并运行Host程序,使用如下命令建立网络。使用info CLI命令查看当前网络信息,在下一步设备加入网络时需要用到,包括网络PANID、信道、Network Key等,如下图所示。在Host端输入keys print可以查看该网络的Network Key。
然后,使用Z3Light sample作为基础示例,在其Z3LightSOC_callback.c上增加如下核心代码:
EmberStatus MyNodeSecurityInit() { EmberInitialSecurityState state; EmberStatus status; EmberKeyData NetworkKey = {0x58,0x12,0x17,0x34,0x64,0xC7,0x7A,0xDC,0x40,0xD9,0x39,0x01,0xFB,0xD0,0xF4,0xAE,}; MEMSET(&state, 0, sizeof(EmberInitialSecurityState)); state.bitmask |= EMBER_HAVE_NETWORK_KEY; MEMMOVE(emberKeyContents(&state.networkKey), emberKeyContents(&NetworkKey), EMBER_ENCRYPTION_KEY_SIZE); emberAfSecurityPrintln("set state to: 0x%2x", state.bitmask); status = emberSetInitialSecurityState(&state); if (status != EMBER_SUCCESS) { emberAfSecurityPrintln("security init node: 0x%x", status); return status; } return EMBER_SUCCESS; }
void findingAndBindingEventHandler() { #if 0 if (emberAfNetworkState() == EMBER_JOINED_NETWORK) { emberEventControlSetInactive(findingAndBindingEventControl); emberAfCorePrintln("Find and bind target start: 0x%X", emberAfPluginFindAndBindTargetStart(LIGHT_ENDPOINT)); } #else EmberStatus status = EMBER_INVALID_CALL; EmberNodeType nodeType = EMBER_ROUTER; EmberNetworkParameters networkParams; MEMSET(&networkParams, 0, sizeof(EmberNetworkParameters)); networkParams.radioChannel = 25; networkParams.radioTxPower = 8; networkParams.panId = 0x08B6; networkParams.joinMethod = EMBER_USE_NWK_REJOIN_HAVE_NWK_KEY; emberEventControlSetInactive(findingAndBindingEventControl); status = MyNodeSecurityInit(); emberAfCorePrintln("MyNodeSecurityInit status = 0x%X", status); status = emberJoinNetwork(nodeType, &networkParams); emberAfCorePrintln("%ping on ch %d, panId 0x%2X. status = 0x%X", "Join", networkParams.radioChannel, networkParams.panId, status); #endif }
最后,将Bootloader以及编译生成的应用程序烧录另一个4151radio板,按下WSTK地板上任何一个按键,可以看到该设备成功加入刚才建立的网络。输入info CLI命令可以查看该设备的网络信息,如下图所示。
总结:以上主要介绍一种out-of-band的实现方式。当设备获取网络参数之后,可以通过NWK rejoin加入网络,即通过调用emberJoinNetwork()以及在此之前调用emberSetInitialSecurityState()来实现。其中需要将安全状态的bitmask 设置为EMBER_HAVE_NETWORK_KEY,并设置正确的网络Key。
如何使用Network rejoin加入网络?