ns3的sta接入ap很简单,实例程序很多。但之后如何接入其他ssid的wifi的方法却很难找到。其实很简单,只要通过节点获取到WifiMac对象指针再SetSsid即可。问题是这个指针的获取方法鲜有资料。后来看到一篇《ns3聚合对象》的文章,写到了列出各种聚合对象的方法:
Object::AggregateIterator iter = nodes.Get(0)->GetAggregateIterator();
while (iter.HasNext())
{
Ptr<const Object> obj = iter.Next();
NS_LOG_UNCOND(obj->GetInstanceTypeId().GetName());
}
iter = nodes.Get(0)->GetDevice(1)->GetAggregateIterator();
while (iter.HasNext())
{
Ptr<const Object> obj = iter.Next();
NS_LOG_UNCOND(obj->GetInstanceTypeId().GetName());
}
上述两段代码分别打印node节点的聚合对象、NetDevice设备的聚合对象。可以得到获得wifimac的方法如下:
Ptr<WifiNetDevice> pDev = nodes.Get(i)->GetDevice(1)->GetObject<WifiNetDevice> ();
if(pDev==NULL)
std::cout<<"WifiNetDevice:null ptr"<<std::endl;
Ptr<WifiMac> pMac = pDev->GetMac();
if(pMac==NULL)
std::cout<<"WifiMac : null ptr"<<std::endl;
pMac->SetAttribute("Ssid", SsidValue (Ssid ("wifi-0")));
为什么是GetDevice(1)不是GetDevice(0)?因为divece0是127.0.0.1的保留设备。
文章介绍了在ns3模拟环境中,如何通过获取WifiMac对象指针并设置SSID来让STA接入不同WiFi网络。通过Node的AggregateIterator遍历设备,找到WifiNetDevice并获取其Mac层对象,然后设置Ssid属性。注意,GetDevice(1)是因为设备0通常为回环接口。
4750

被折叠的 条评论
为什么被折叠?



