SWRU368C May 2018 – January 2021 CC3100 , CC3100MOD , CC3200 , CC3200MOD
In a continuous mDNS query, having received one response is not necessarily an indication that there will be no more relevant responses, and the querying operation continues until no further responses are required. Since one-shot query is the default setting, extra settings needs to be done before continuous query can be used. However, since sl_NetAppDnsGetHostByService() only returns one discovered host at a time, use sl_NetAppGetServiceList() to get a complete list of services. Here’s an example with return list in the format of
SlNetAppGetFullServiceWithTextIpv4List_t:
#define LISTEN_SERVICE_NAME "_workstation._tcp.local"
#define SERVICE_GET_COUNT_MAX 3 // Specify the max number of services to discover
void mDNS_Query_Continuous()
{
_u32 pAddr;
_u32 usPort;
_u16 ulTextLen;
_i8 cText[200];
_i32 iretvalmDNS = -1;
_i32 i;
// The structure to be used for retrieving the list
SlNetAppGetFullServiceWithTextIpv4List_t serviceList[SERVICE_GET_COUNT_MAX];
// Set to perform Continuous mDNS query
iretvalmDNS = sl_NetAppSet(SL_NET_APP_MDNS_ID, NETAPP_SET_GET_MDNS_CONT_QUERY_OPT, 0, 0);
Report("sl_NetAppSet() return: %d\n\r", iretvalmDNS);
// Set an infinite loop until getting a response
iretvalmDNS = 1;
while(iretvalmDNS)
{
ulTextLen = 200;
iretvalmDNS = sl_NetAppDnsGetHostByService(LISTEN_SERVICE_NAME,
(unsigned char) strlen(LISTEN_SERVICE_NAME), SL_AF_INET,
(_u32 *)&pAddr, &usPort,&ulTextLen,&cText[0]);
}
if(iretvalmDNS == 0)
{
// Prints the first response, but we want more later on
cText[ulTextLen] = '\0';
Report("First Response: Addr: %d.%d.%d.%d, Port: %d, Text: %s, TextLength: %d\n\r",
SL_IPV4_BYTE(pAddr, 3), SL_IPV4_BYTE(pAddr, 2),
SL_IPV4_BYTE(pAddr, 1), SL_IPV4_BYTE(pAddr, 0),
usPort, cText, ulTextLen);
}
// Get a list of discovered services
iretvalmDNS = sl_NetAppGetServiceList(0,
SERVICE_GET_COUNT_MAX,
SL_NET_APP_FULL_SERVICE_WITH_TEXT_IPV4_TYPE,
(_i8*) &serviceList[0],
1479);
// 1479 is the maximum number of characters to return
// The function returns the total number of discovered services
// This number will never exceed the SERVICE_GET_COUNT_MAX parameter we passed into the
// function
Report("sl_NetAppSet() return: %d\n\r", iretvalmDNS);
if(iretvalmDNS > 0)
{
// Now just prints all the services
Report("Complete List:\n\r");
for(i=0; i<iretvalmDNS; i++)
{
Report("Host %d: %s, IP: %d.%d.%d.%d, Name: %s Port: %d, Text: %s\n\r",
i,
serviceList[i].service_host,
SL_IPV4_BYTE(serviceList[i].service_ipv4, 3),
SL_IPV4_BYTE(serviceList[i].service_ipv4, 2),
SL_IPV4_BYTE(serviceList[i].service_ipv4, 1),
SL_IPV4_BYTE(serviceList[i].service_ipv4, 0),
serviceList[i].service_name,
serviceList[i].service_port,
serviceList[i].service_text);
}
}
}