Beispiel #1
0
int OTAClass::beginArduinoMdnsService(char *device_name, uint16_t port) {

    int ret = -1;

    mdns_buf = (unsigned char *) malloc (DEFAULT_OTA_MDNS_BUF);
    txtRecord = malloc (sizeof(TXTRecordRef));

    do {
        if (mDNSResponderInit() != 0) {
            OTA_PRINTF("Fail to init mDNS service\r\n");
            break;
        }

        TXTRecordCreate((TXTRecordRef *)txtRecord, DEFAULT_OTA_MDNS_BUF, mdns_buf);

        TXTRecordSetValue((TXTRecordRef *)txtRecord, "board",       strlen("ameba_rtl8195a"), "ameba_rtl8195a");
        TXTRecordSetValue((TXTRecordRef *)txtRecord, "auth_upload", strlen("no"),             "no");
        TXTRecordSetValue((TXTRecordRef *)txtRecord, "tcp_check",   strlen("no"),             "no");
        TXTRecordSetValue((TXTRecordRef *)txtRecord, "ssh_upload",  strlen("no"),             "no");

        mdns_service_id = mDNSRegisterService(device_name, "_arduino._tcp", "local", port, (TXTRecordRef *)txtRecord);

        TXTRecordDeallocate((TXTRecordRef *)txtRecord);

        ret = 0;
    } while (0);

    if (ret < 0) {
        OTA_PRINTF("Fail to begin Arduino mDNS service\r\n");
    }

    return ret;
}
static void example_mdns_thread(void *param)
{
    DNSServiceRef dnsServiceRef = NULL;
    TXTRecordRef txtRecord;
    unsigned char txt_buf[100];	// use fixed buffer for text record to prevent malloc/free

    // Delay to wait for IP by DHCP
    vTaskDelay(10000);

    printf("\nmDNS Init\n");
    if(mDNSResponderInit() == 0) {
        printf("mDNS Register service\n");
        TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
        TXTRecordSetValue(&txtRecord, "text1", strlen("text1_content"), "text1_content");
        TXTRecordSetValue(&txtRecord, "text2", strlen("text2_content"), "text2_content");
        dnsServiceRef = mDNSRegisterService("ameba", "_service1._tcp", "local", 5000, &txtRecord);
        TXTRecordDeallocate(&txtRecord);
        printf("wait for 30s ... \n");
        vTaskDelay(30*1000);

        printf("mDNS Update service\n");
        TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
        TXTRecordSetValue(&txtRecord, "text1", strlen("text1_content_new"), "text1_content_new");
        mDNSUpdateService(dnsServiceRef, &txtRecord, 0);
        TXTRecordDeallocate(&txtRecord);
        printf("wait for 30s ... \n");
        vTaskDelay(30*1000);

        if(dnsServiceRef)
            mDNSDeregisterService(dnsServiceRef);

        // deregister service before mdns deinit is not necessary
        // mDNS deinit will also deregister all services
        printf("mDNS Deinit\n");
        mDNSResponderDeinit();
    }

    vTaskDelete(NULL);
}
Beispiel #3
0
void cmd_mdns(int argc, char **argv)
{
	static DNSServiceRef dnsServiceRef = NULL;
	static char hostname[32];

	if(argc == 1) goto error;

	if(strcmp(argv[1], "init") == 0) {
		WACPersistentConfig_t config;
		WACPlatformReadConfig(&config);
		if((config.name_len > 0) && (config.name_len < 32)) {
			strcpy(hostname, config.name);
		}
		else {
			uint8_t *mac = LwIP_GetMAC(&xnetif[0]);
			sprintf(hostname, "AMEBA%02x%02x%02x", mac[3], mac[4], mac[5]);
		}
		netif_set_hostname(&xnetif[0], hostname);

		if(mDNSResponderInit() == 0)
			printf("mDNS initialized\n");
		else
			printf("mDNS init failed\n");
	}
	else if(strcmp(argv[1], "deinit") == 0) {
		mDNSResponderDeinit();
		printf("mDNS de-initialized\n");
	}
	else if(strcmp(argv[1], "reg") == 0) {
		if(argc != 5) {
			printf("Usage: mdns reg NAME TYPE PORT\n");
			return;
		}

		if(atoi(argv[4]) == 0)
			printf("mDNS service incorrect port\n");

		if(dnsServiceRef) {
			mDNSDeregisterService(dnsServiceRef);
			dnsServiceRef = NULL;
		}

		if((dnsServiceRef = mDNSRegisterService(argv[2], argv[3], "local", atoi(argv[4]), NULL)) != NULL)
			printf("mDNS service %s.%s registered\n", argv[2], argv[3]);
		else
			printf("mDNS service %s.%s register failed\n", argv[2], argv[3]);
	}
	else if(strcmp(argv[1], "dereg") == 0) {
		if(dnsServiceRef) {
			mDNSDeregisterService(dnsServiceRef);
			dnsServiceRef = NULL;
			printf("mDNS service de-registered\n");
		}
		else {
			printf("mDNS no service\n");
		}
	}
	else if(strcmp(argv[1], "update") == 0) {
		if(argc != 5) {
			printf("Usage: mdns update TXT_KEY TXT_VALUE TTL\n");
			return;
		}

		if(dnsServiceRef) {
			TXTRecordRef txtRecord;
			unsigned char txt_buf[100];
			TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
			TXTRecordSetValue(&txtRecord, argv[2], strlen(argv[3]), argv[3]);
			mDNSUpdateService(dnsServiceRef, &txtRecord, atoi(argv[4]));
			TXTRecordDeallocate(&txtRecord);
			printf("mDNS service updated\n");
		}
		else {
			printf("mDNS no service\n");
		}
	}
	// For BCT mDNS manual name change test
	else if(strcmp(argv[1], "name_test") == 0) {
		char *new_name = "New - Bonjour Service Name";
		printf("Rename HAP service to \"%s\"._hap._tcp.local. %s\n", 
		       new_name, (RenameHAPBonjourService(new_name) == 0) ? "success":"fail");
	}
	else {
error:
		printf("Usage: mdns init|deinit|reg|dereg|name_test\n");
	}
}