我是使用Provisioning_smart_config这个例子,有些时候手机用smartconfig配置能成功,能看到红灯亮了,但是手机上一直都没有提示“new device found”,然后手机切入到devices中,不断更新也找不到,只有重新把手机的wifi starter 重新启动才能找到(只是偶尔能找到);请问怎样才能提高找到设备的成功率;
Yonghua Pan:
因为有些路由器对于mDNS的支持不好,所以CC3200在发出MDNS的包,有时候会被drop掉,建议在开发的时候可以使用UDP广播来做。
linda lee:
回复 Yonghua Pan:
您好,您说用provisioning_smartconfig的时候如若连接路由器成功,会开启mdns,但是我在程序中并没有找到register,只在初始化配置的时候看到了unregister,请问具体在哪里呢?
Terry Han:
回复 linda lee:
官方有一个mDNS的例子,我增加一些备注,可供参考
//*****************************************************************************////! Main function//!//! \param None//!//! \return None//!// In SDK example device is advertising it's IP using mDNS protocol. You need to implement mDNS listener at the other end(mobile or PC) . // You can easily implement mDNS listener as its very simple protocol. mDNS uses multicast UDP packets. //// 1. Create a udp socket on port 5353// 2. Join muticast group on "224.0.0.251"// 3. receive packets and parse the data retrieve IP//// For more details please go through mDNS RFC(rfc6762). //// sl_NetAppMDNSRegisterService()向 224.0.0.251 + 5353广播自己的IP地址和端口号及message// sl_NetAppDnsGetHostByService()监听224.0.0.251 + 5353地址接收到提供服务的目的IP地址和端口号及message//*****************************************************************************static long mDNS_Task(){ int iretvalmDNS; long lRetVal = -1; unsigned char policyVal;#ifndef MDNS_ADVERTISE unsigned int pAddr; unsigned long usPort; unsigned short ulTextLen = 200; char cText[200]; int i;#endif InitializeAppVariables(); // // Following function configure the device to default state by cleaning // the persistent settings stored in NVMEM (viz. connection profiles & // policies, power policy etc) // // Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton // // Note that all profiles and persistent settings that were done on the // device will be lost // lRetVal = ConfigureSimpleLinkToDefaultState(); if(lRetVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == lRetVal) { UART_PRINT("Failed to configure the device in its default state\n\r"); }
return -1; }
UART_PRINT("Device is configured in default state \n\r");
// // Assumption is that the device is configured in station mode already // and it is in its default state // lRetVal = sl_Start(0, 0, 0); if (lRetVal < 0 || ROLE_STA != lRetVal) { UART_PRINT("Failed to start the device \n\r"); return -1; }
UART_PRINT("Device started as STATION \n\r");
//powering on the CC3200 device
//reset all policies sl_WlanPolicySet( SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), &policyVal, 1 /*PolicyValLen*/);
// //Connecting to WLAN AP // lRetVal = WlanConnect(); ASSERT_ON_ERROR(lRetVal);
UART_PRINT("Connection established w/ AP and IP is aquired \n\r");
#ifdef MDNS_ADVERTISE //Registering for the mDNS service. iretvalmDNS = sl_NetAppMDNSUnRegisterService(SERVICE_NAME,(unsigned char)strlen(SERVICE_NAME));// Time To Live(TTL)生存时间(in seconds),简单的说它表示DNS记录在DNS服务器上缓存时间// Advertise(Mdns host) 向224.0.0.251 + 5353广播自己的IP地址和端口号及message//——————————————————————–mDNS服务理解——————————————————————————— // CC3200_A连接AP进入局域网获取自身IP地址,同时开启mDNS服务,并向mDNS服务注册一下信息(sl_NetAppMDNSRegisterService):我提供SERVICE_NAME服务,我IP:192.168.1.101端口200// 当CC3200_B连接AP进入局域网,并向CC3200_B的mDNS服务请求(sl_NetAppDnsGetHostByService),我要找局域网内SERVICE_NAME服务器,CC3200_B的mDNS就会去局域网内向其他的mDNS询问,// 并且最终告诉你,有一个IP地址为192.168.1.101,端口号是200的主机,也就是CC3200_A提供SERVICE_NAME服务,所以CC3200_B就知道了CC3200_A的IP地址和端口号了。//—————————————————————————————————————————————————————– iretvalmDNS = sl_NetAppMDNSRegisterService(SERVICE_NAME,(unsigned char)strlen(SERVICE_NAME),"Service registered for 3200",\ (unsigned char)strlen("Service registered for 3200"),200,2000,1); //端口号Port=200 + Time To Live(TTL)=2000 + service is unique
if(iretvalmDNS == 0) { Report("MDNS Registration successful\n\r"); } else { Report("MDNS Registered failed\n\r"); }#else for( i= 0;i<100;i++) { iretvalmDNS = 1; while(iretvalmDNS) { ulTextLen = 200;// listen(Mdns client) 监听224.0.0.251 + 5353地址接收到提供服务的目的IP地址和端口号及message iretvalmDNS = sl_NetAppDnsGetHostByService(SERVICE_NAME,(unsigned char)strlen(SERVICE_NAME),SL_AF_INET,(unsigned long *)&pAddr,&usPort,&ulTextLen,&cText[0]); } if(iretvalmDNS == 0) { cText[ulTextLen] = '\0'; Report("\n\rPort: %d\n\r",usPort ); Report("Addr: %d.%d.%d.%d\n\r",SL_IPV4_BYTE(pAddr,3), SL_IPV4_BYTE(pAddr, 2), SL_IPV4_BYTE(pAddr, 1), SL_IPV4_BYTE(pAddr, 0)); Report("TLen: %d\n\r",ulTextLen ); Report("Text: %s\n\r",cText ); }
}#endif return SUCCESS;}