关于CC3000进行MDNS Boardcast的方法进行进行了下基本的研究,Ti官方给出了一个API去进行MDNS广播

Calling mdnsAdvertiser() with a specific device service name will cause the CC3000 to send one mDNS packet to a multicast address and port number.</p>

mDNS port - 5353
mDNS multicast address - 224.0.0.251

Other devices which listens to the same multicast IP and port can parse the packet and pull out all the CC3000 required information (device name, IP address...) .
CC3000 Host Driver supplies one API to support basic mDNS implementation:
/* mdnsEnabled - flag to enable/disable the mDNS feature */
</span> /* deviceServiceName - the service name as part of the published canonical domain name */
</span> /* deviceServiceNameLength - the length of the service name */
</span> /* return On success, zero is returned, return SOC_ERROR if socket was not opened successfully, or if an error occurred. */
</span> 
int</span> mdnsAdvertiser(</span>unsigned</span> short</span> mdnsEnabled,</span> char</span> *</span> deviceServiceName,</span> unsigned</span> short</span> deviceServiceNameLength)</span></pre>
由于我们在MEGA2560上一直使用adafruit所提供的CC3000库文件,而adafruit也提供了CC3000的MDNS的Arduino的库文件,为了方便,我们直接使用了adafriut的库文件。</p>

库文件下载地址:https://github.com/adafruit/CC3000_MDNS</pre>
关键使用方法很简单:</p>
  1. #include
    #include
    #include </li></p>
  2. Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
    SPI_CLOCK_DIV2);</li></p>
  3. mdns.begin("cc3kserver", cc3000)</li>
  4. mdns.update();</li>
    </ol>
    简单的这样的几步就可以发送mdns broadcast了。</p>

    其实发送mdns broadcast的原理非常简单,向224.0.0.251:5353使用socket发送一个response

    关键代码原理如下

    uint8_t respHeader[] = { 0x00, 0x00, // ID = 0
    0x84, 0x00, // Flags = response + authoritative answer
    0x00, 0x00, // Question count = 0
    0x00, 0x01, // Answer count = 1
    0x00, 0x00, // Name server records = 0
    0x00, 0x01 // Additional records = 1
    };
    uint8_taRecord[] = { 0x00, 0x01, // Type = 1, A record/IPV4 address
    0x80, 0x01, // Class = Internet, with cache flush bit
    0x00, 0x00, 0x00, 0x00, // TTL in seconds, to be filled in later
    0x00, 0x04, // Length of record
    0x00, 0x00, 0x00, 0x00 // IP address, to be filled in later
    };
    uint8_tnsecRecord[] = { 0xC0, 0x0C, // Name offset
    0x00, 0x2F, // Type = 47, NSEC (overloaded by MDNS)
    0x80, 0x01, // Class = Internet, with cache flush bit
    0x00, 0x00, 0x00, 0x00, // TTL in seconds, to be filled in later
    0x00, 0x08, // Length of record
    0xC0, 0x0C, // Next domain = offset to FQDN
    0x00, // Block number = 0
    0x04, // Length of bitmap = 4 bytes
    0x40, 0x00, 0x00, 0x00 // Bitmap value = Only first bit (A record/IPV4) is set
    };
    #####################################################
    #计算respons的长度
    #####################################################
    queryFQDNLen = _expectedLen - 4;
    _responseLen =HEADER_SIZE + queryFQDNLen + A_RECORD_SIZE + NSEC_RECORD_SIZE;
    #####################################################
    #拷贝之前的预格式化数据到_resopnse
    #####################################################
    memcpy(_response, respHeader, HEADER_SIZE);
    memcpy(_response + HEADER_SIZE, _expected, queryFQDNLen);
    uint8_t* records = _response + HEADER_SIZE + queryFQDNLen;
    memcpy(records, aRecord, A_RECORD_SIZE);
    memcpy(records + A_RECORD_SIZE, nsecRecord, NSEC_RECORD_SIZE);
    #####################################################
    # Add TTL to records.
    #####################################################
    uint8_t ttl[4] = { (uint8_t)(ttlSeconds >> 24), (uint8_t)(ttlSeconds >> 16), (uint8_t)(ttlSeconds >> 8), (uint8_t)ttlSeconds };
    memcpy(records + TTL_OFFSET, ttl, 4);
    memcpy(records + A_RECORD_SIZE + 2 + TTL_OFFSET, ttl, 4);
    ######################################################
    ipAddress = 获取CC3000 ip地址</p>

    records[IP_OFFSET] = (uint8_t)(ipAddress >> 24);
    records[IP_OFFSET + 1] = (uint8_t)(ipAddress >> 16);
    records[IP_OFFSET + 2] = (uint8_t)(ipAddress >> 8);
    records[IP_OFFSET + 3] = (uint8_t) ipAddress;

    #####################################################
    #最后通过socket向224.0.0.251:5353发送_response
    #####################################################
    #怎么创建socket这里就不阐述了
    send(_mdnsSocket, _response, _responseLen, 0);</pre>