想要实现的功能:
1.串口修改PAN ID;
2.串口修改CHANNEL;
3.广播发送给网内节点,重启建立新的网络。
实现例程:sampleapp例程修改
IAR预编译:
ZTOOL_P2
MT_TASK
MT_AF_FUNC
MT_AF_CB_FUNC
MT_APP_FUNC
MT_DEBUG_FUNC
MT_NWK_FUNC
MT_NWK_CB_FUNC
MT_SYS_FUNC
MT_SYS_OSAL_NV_READ_CERTIFICATE_DATA
MT_UTIL_FUNC
MT_ZDO_CB_FUNC
MT_ZDO_FUNC
MT_ZDO_MGMT
NV_RESTORE
一、串口修改PAN ID
在之前的实验中,我一直纠结于pan id的设置,有2种方法:
方法1.
pan_id=0x1122;//你想要的ID
zb_WriteConfiguration(ZCD_NV_PANID, sizeof(uint16), &pan_id) ;
zb_SystemReset();
方法2:
pan_id = 0x1122;
_NIB.nwkPanId = pan_id;
NLME_UpdateNV(0x01);
zb_SystemReset();
我自己的实验结果显示,方法2是可行的,于是,我对MT_UTIL.C函数中的MT_UtilSetPanID做了修改
(我用ztool中util修改PAN ID不成功,不知道为什么,有人遇到过吗?)
以下是修改后的代码
串口发送fe 02 27 02 00 05 22,PAN ID改为0x0500,自动重启,路由器不在网内
我用串口调试助手,IAR仿真结果显示PAN ID修改成功,剩下的我们就需要考虑如何广播出去了(这个简单)
二、修改CHANNEL
这里有个概念问题:
_NIB.channelList表示可以扫描的信道;
_NIB.nwkLogicalChannel表示正在使用的逻辑信道,从11~26,即0x0B~0x1A
所以我们要修改的目标是_NIB.nwkLogicalChannel
代码奉上:
记得改一下:-DDEFAULT_CHANLIST=0x00003800 //使能0B 0C 0D信道
串口发送数据:
_NIB.nwkLogicalChannel 帧结构
0x0b fe 05 27 03 00 08 00 00 0b 22
0x0c fe 05 27 03 00 10 00 00 0c 3d
三、广播发送
直接贴上代码,有不懂的同学想一想,实在想不通再问。
协调器广播部分:
其中KEY1 KEY2是例程自带的,KEY5是我自己定义的广播发送按键。
之前的帖子中说道协调器广播完后重启的问题,所以我在这里新建了一个任务,让协调器5秒后自动重启:
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SYS_RESET_SOFT, 5000);//新建一个任务,5S后重启- void SampleApp_HandleKeys( uint8 shift, uint8 keys )
- {
- (void)shift; // Intentionally unreferenced parameter
- if ( keys & HAL_KEY_SW_1 ) //FLASH读写实验
- {
- /* This key sends the Flash Command is sent to Group 1.
- * This device will not receive the Flash Command from this
- * device (even if it belongs to group 1).
- */
- SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );
- }
- if ( keys & HAL_KEY_SW_2 )
- {
- /* The Flashr Command is sent to Group 1.
- * This key toggles this device in and out of group 1.
- * If this device doesn't belong to group 1, this application
- * will not receive the Flash command sent to group 1.
- */
- aps_Group_t *grp;
- grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
- if ( grp )
- {
- // Remove from the group
- aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
- }
- else
- {
- // Add to the flash group
- aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );
- }
- }
- if ( keys & HAL_KEY_SW_5 )
- {
- //广播至所有节点
- if(UpdateAllNetwork(_NIB.nwkPanId, _NIB.nwkLogicalChannel) == afStatus_SUCCESS)
- {
- osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SYS_RESET_SOFT, 5000);//新建一个任务,5S后重启
- }
- }
- }
- static afStatus_t UpdateAllNetwork(uint16 panId, uint8 channelId)
- {
- afAddrType_t dstAddr;
- uint8 dataLength = 3; //PAN ID+CHANNEL
- uint8 *msg = osal_mem_alloc(dataLength);
- dstAddr.addrMode = (afAddrMode_t)afAddrBroadcast;
- dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR_DEVALL;
- dstAddr.endPoint = AF_BROADCAST_ENDPOINT;
- msg[0] = LO_UINT16(panId);
- msg[1] = HI_UINT16(panId);
- msg[2] = channelId;
- return AF_DataRequest(&dstAddr,
- &SampleApp_epDesc,
- YOURNAME_NETCONF_CLUSTERID,
- dataLength,
- msg,
- &SampleApp_TransID,
- 0,
- AF_DEFAULT_RADIUS);
- }
节点接收部分:- void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
- {
- uint16 flashTime;
- switch ( pkt->clusterId )
- {
- case SAMPLEAPP_PERIODIC_CLUSTERID:
- break;
- case SAMPLEAPP_FLASH_CLUSTERID:
- flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
- HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
- break;
- //配置网络信息
- case ELINKCS_NETCONF_CLUSTERID:
- _NIB.nwkPanId=BUILD_UINT16(pkt->cmd.Data[0], pkt->cmd.Data[1] );
- _NIB.nwkLogicalChannel=pkt->cmd.Data[2];
- NLME_UpdateNV(0x01);
- HalLedBlink( HAL_LED_4, 6, 50, (flashTime / 4) );
- osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SYS_RESET_SOFT, 5000);//创建一个任务,5S后重启
- break;
- }
- }
先写这么多,以后想到再完善!
|