/**************************************************************************
输入参数:无
输出参数:无
*************************************************************************/
void joinCmd(void)
{
u8 packet[128];
u8 searchChannel;
u32 lastTime;
StStatus status = ST_SUCCESS;
printf("\r\n");
if(activeInNetwork) {
printf("Already in network\r\n");
return;
}
printf("Inactive node joining network and becoming a planet\r\n");
initNetworkState(); //初始化网络状态
TURN_RADIO_ON(); //打开无线
activeInNetwork = TRUE; //加入网络成功,如果加入网络失败,会重新设置为FALSE
//构造发送请求加入网络的数据包
packet[0] = (18+2);
packet[1] = FCF_DATA; //帧类型
packet[2] = FCF_SHORTDST + FCF_LONGSRC; //地址类型
packet[4] = (0xFFFF>>0)&0xFF; //目标PAN ID
packet[5] = (0xFFFF>>8)&0xFF;
packet[6] = (0xFFFF>>0)&0xFF; //目标Node ID
packet[7] = (0xFFFF>>8)&0xFF;
packet[8] = (0xFFFF>>0)&0xFF; //源PAN ID
packet[9] = (0xFFFF>>8)&0xFF;
memcpy((packet+10), ST_RadioGetEui64(), 8); //64位长地址
packet[18] = PT_SUN_SEARCH; //负载类型
printf("Trying channel");
//循环搜索所有信道
for(searchChannel=ST_MIN_802_15_4_CHANNEL_NUMBER;
searchChannel<=ST_MAX_802_15_4_CHANNEL_NUMBER; searchChannel++)
{
//信道搜索过程中会延迟200ms,所以每次都需要重置看门狗,,防止触发复位
halResetWatchdog();
printf(" %d", searchChannel);
status = ST_RadioSetChannel(searchChannel);
assert(status==ST_SUCCESS);
currSeqNum++; //数据包序列号
packet[3]=currSeqNum;
availableSunFound = FALSE;
enqueueTxPacket(TRUE, 0xFFFF, packet, 0); //广播搜索父节点
//延迟200ms等待回复
lastTime = halCommonGetInt32uMillisecondTick();
do {
processRxPacket(); //处理接收的数据包
txTick();
} while(elapsedTimeInt32u(lastTime, halCommonGetInt32uMillisecondTick())<200);
//判断是否加入网络成功
if(networkJoinedStopSearching)
{
printf("\r\n");
printf("Joined on channel %d with PAN ID 0x%04X. My ID is now 0x%04X.\r\n",
ST_RadioGetChannel(), ST_RadioGetPanId(), ST_RadioGetNodeId());
activeInNetwork = TRUE;
#ifdef PLANET_ROLE
autoSendRate = 60; //设置子节点向父节点数据包发送周期
halSetLed(LED_D4); //加入网络成功,点亮LED4
#endif
return;
}
}
printf("\r\n");
printf("Did not join. Returning to inactive state.\r\n");
activeInNetwork = FALSE; //加入网络失败
}
少吃点,吃好的。