当前位置: 首页>>代码示例>>C++>>正文


C++ EthernetInterface类代码示例

本文整理汇总了C++中EthernetInterface的典型用法代码示例。如果您正苦于以下问题:C++ EthernetInterface类的具体用法?C++ EthernetInterface怎么用?C++ EthernetInterface使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了EthernetInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main() {
    GREENTEA_SETUP(60, "default_auto");

    bool result = false;
    const time_t TIME1970 = 2208988800L;
    int ntp_send_values[12] = {0};
    int ntp_recv_values[12] = {0};

    EthernetInterface eth;
    eth.connect();
    printf("UDP client IP Address is %s\n", eth.get_ip_address());

    UDPSocket sock;
    sock.open(&eth);
    sock.set_timeout(15000);

    SocketAddress nist(&eth, HTTP_SERVER_NAME, HTTP_SERVER_PORT);

    printf("UDP: NIST server %s address: %s on port %d\r\n", HTTP_SERVER_NAME, nist.get_ip_address(), nist.get_port());

    memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
    ntp_send_values[0] = '\x1b';

    while(1) {
        memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));

        int ret_send = sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
        printf("UDP: Sent %d Bytes to NTP server \n", ret_send);

        SocketAddress source;
        const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));

        printf("UDP: Recved from NTP server %d Bytes \n", n);

        if (n > 0 && strcmp(source.get_ip_address(), nist.get_ip_address()) == 0) {
            result = true;

            printf("UDP: Values returned by NTP server: \n");
            for (size_t i=0; i < sizeof(ntp_recv_values) / sizeof(ntp_recv_values[0]); ++i) {
                printf("\t[%02d] 0x%X", i, ntohl(ntp_recv_values[i]));

                if (i == 10) {
                    time_t timestamp = ntohl(ntp_recv_values[i]) - TIME1970;
                    printf("\tNTP timestamp is %s", ctime(&timestamp));
                } else {
                    printf("\n");
                }
            }

            break;
        }

        printf("Failed to receive data, retrying in 5 seconds...\n");
        wait(5);
    }

    sock.close();
    eth.disconnect();
    GREENTEA_TESTSUITE_RESULT(result);
}
开发者ID:dinau,项目名称:mbed,代码行数:60,代码来源:main.cpp

示例2: main

int main() {
    printf("{{start}}\r\n");
    EthernetInterface eth;
    /* Initialise with DHCP, connect, and start up the stack */
    eth.init();
    eth.connect();
    lwipv4_socket_init();

    printf("UDP client IP Address is %s\r\n", eth.getIPAddress());

    /* Get the current time */
    UDPGetTime gt;

    {
        FunctionPointer1<void, const char*> fp(&gt, &UDPGetTime::startGetTime);
        minar::Scheduler::postCallback(fp.bind(HTTP_SERVER_NAME));
    }

    minar::Scheduler::start();

    printf("UDP: %lu seconds since 01/01/1900 00:00 GMT\r\n", gt.time());

    eth.disconnect();

    float years = (float) gt.time() / 60 / 60 / 24 / 365;

    printf("{{%s}}\r\n",(years < YEARS_TO_PASS ?"failure":"success"));
    printf("{{end}}\r\n");

    return 0;
}
开发者ID:mpg,项目名称:mbed-example-network,代码行数:31,代码来源:main.cpp

示例3: app_start

void app_start(int argc, char *argv[])
{
    (void) argc;
    (void) argv;
    MBED_HOSTTEST_TIMEOUT(5);
    MBED_HOSTTEST_SELECT(default);
    MBED_HOSTTEST_DESCRIPTION(Socket Abstraction Layer construction and utility test);
    MBED_HOSTTEST_START("SAL_INIT_UTIL");

    int tests_pass = 1;
    int rc;
    EthernetInterface eth;
    /* Initialise with DHCP, connect, and start up the stack */
    eth.init();
    eth.connect();

    do {
        socket_error_t err = lwipv4_socket_init();
        if (!TEST_EQ(err,SOCKET_ERROR_NONE)) {
            tests_pass = 0;
            break;
        }
        rc = socket_api_test_create_destroy(SOCKET_STACK_LWIP_IPV4, SOCKET_AF_INET6);
        tests_pass = tests_pass && rc;

        rc = socket_api_test_socket_str2addr(SOCKET_STACK_LWIP_IPV4, SOCKET_AF_INET6);
        tests_pass = tests_pass && rc;

    } while (0);
    MBED_HOSTTEST_RESULT(tests_pass);
}
开发者ID:stanly88,项目名称:mbed-example-network,代码行数:31,代码来源:init_api.cpp

示例4: main

int main()
{
    EthernetInterface eth;
    NTPClient ntp;
    eth.init(); //Use DHCP
    eth.connect();

    // NTP set time
    {
        bool result = true;
        const char *url_ntp_server = "0.pool.ntp.org";
        printf("NTP_SETTIME: Trying to update time... \r\n");
        const int ret = ntp.setTime(url_ntp_server);
        if (ret == 0) {
          time_t ctTime = time(NULL);
          printf("NTP_SETTIME: UTC Time read successfully ... [OK]\r\n");
          printf("NTP_SETTIME: %s\r\n", ctime(&ctTime));
        }
        else {
          printf("NTP_SETTIME: Error(%d) ... [FAIL]\r\n", ret);
          result = false;
        }

        if (result == false) {
            notify_completion(false);
            exit(ret);
        }
    }
    eth.disconnect();
    notify_completion(true);
    return 0;
}
开发者ID:23chrischen,项目名称:mbed,代码行数:32,代码来源:main.cpp

示例5: main

int main (void) {
    MBED_HOSTTEST_TIMEOUT(20);
    MBED_HOSTTEST_SELECT(udpecho_server_auto);
    MBED_HOSTTEST_DESCRIPTION(UDP echo server);
    MBED_HOSTTEST_START("NET_5");

    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT);

    UDPSocket server;
    server.bind(ECHO_SERVER_PORT);

    Endpoint client;
    char buffer[BUFFER_SIZE] = {0};
    printf("MBED: Waiting for packet...\r\n");
    while (true) {
        int n = server.receiveFrom(client, buffer, sizeof(buffer));
        if (n > 0) {
            //printf("Received packet from: %s\n", client.get_address());
            const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n;
            buffer[buffer_string_end_index] = '\0';
            //printf("Server received: %s\n", buffer);
            server.sendTo(client, buffer, n);
        }
    }
}
开发者ID:1deus,项目名称:tmk_keyboard,代码行数:28,代码来源:main.cpp

示例6: app_start

void app_start(int, char**) {

	// setup the EthernetInterface
	eth.init();	// DHCP
	eth.connect();
	
	// initialize the ipv4 tcp/ip stack
	lwipv4_socket_init();

	// setup the M2MInterface
	srand(time(NULL));
	uint16_t port = rand() % 65535 + 12345;
	srv = M2MInterfaceFactory::create_interface(observer,endpoint,type,
		lifetime,port,domain,M2MInterface::UDP,M2MInterface::LwIP_IPv4,context_address);

	// setup the Security object		
	sec = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
	sec->set_resource_value(M2MSecurity::M2MServerUri,address);
	sec->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity);

	// setup the Device object
	dev = M2MInterfaceFactory::create_device();
	dev->create_resource(M2MDevice::Manufacturer,"Freescale");
	dev->create_resource(M2MDevice::DeviceType,"frdm-k64f");
	dev->create_resource(M2MDevice::ModelNumber,"M64FN1MOVLL12");
	dev->create_resource(M2MDevice::SerialNumber,"EB1524XXXX");

	// setup the sensor objects
	obj = M2MInterfaceFactory::create_object("loc");
	M2MObjectInstance* ins = obj->create_object_instance();
	M2MResource* resx = ins->create_dynamic_resource("x","accel",M2MResourceInstance::INTEGER,true);
	resx->set_operation(M2MBase::GET_PUT_ALLOWED);
	resx->set_value((const uint8_t*)"0",1);
	M2MResource* resy = ins->create_dynamic_resource("y","accel",M2MResourceInstance::INTEGER,true);
	resy->set_operation(M2MBase::GET_PUT_ALLOWED);
	resy->set_value((const uint8_t*)"0",1);

	
	// Assemble the list of objects to register
	M2MObjectList list;
	list.push_back(dev);
	list.push_back(obj);

	// setup registration event
	Ticker timer;
	timer.attach(&reg,&Registrar::update,20);

	// enable accelerometer
	printf("Initializied accelerometer\r\n");
	accel.enable();
	Ticker sampler;
	sampler.attach(sample,5);

	// schedule 
	FunctionPointer1<void, M2MObjectList> fp(&reg, &Registrar::setup);
	minar::Scheduler::postCallback(fp.bind(list));
	minar::Scheduler::postCallback(sample).period(minar::milliseconds(10000));
	minar::Scheduler::start();
}
开发者ID:wotio,项目名称:mbedos-accel-frdm-k64f,代码行数:59,代码来源:app.cpp

示例7: ethSetup

void ethSetup()
{
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());

    udp.init();
    udp.bind(5683);

    udp.set_blocking(false, 10000);
}
开发者ID:advcclin,项目名称:wakaama-mbed-tinydtls,代码行数:12,代码来源:main.cpp

示例8: main

int main() {
    GREENTEA_SETUP(20, "udp_echo");

    EthernetInterface eth;
    eth.connect();
    printf("UDP client IP Address is %s\n", eth.get_ip_address());

    greentea_send_kv("target_ip", eth.get_ip_address());

    char recv_key[] = "host_port";
    char ipbuf[60] = {0};
    char portbuf[16] = {0};
    unsigned int port = 0;

    UDPSocket sock;
    sock.open(&eth);
    sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);

    greentea_send_kv("host_ip", " ");
    greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));

    greentea_send_kv("host_port", " ");
    greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
    sscanf(portbuf, "%u", &port);

    printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
    SocketAddress udp_addr(ipbuf, port);

    int success = 0;

    for (int i=0; i < ECHO_LOOPS; ++i) {
        prep_buffer(tx_buffer, sizeof(tx_buffer));
        const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
        printf("[%02d] sent...%d Bytes \n", i, ret);

        SocketAddress temp_addr;
        const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
        printf("[%02d] recv...%d Bytes \n", i, n);

        if ((temp_addr == udp_addr &&
             n == sizeof(tx_buffer) &&
             memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
            success += 1;
        }
    }

    bool result = (success > 3*ECHO_LOOPS/4);

    sock.close();
    eth.disconnect();
    GREENTEA_TESTSUITE_RESULT(result);
}
开发者ID:SolarTeamEindhoven,项目名称:mbed,代码行数:52,代码来源:main.cpp

示例9: main

int main() {
    char buffer[BUFFER_SIZE] = {0};
    char out_buffer[BUFFER_SIZE] = {0};
    s_ip_address ip_addr = {0, 0, 0, 0};
    int port = 0;

    printf("MBED: TCPCllient waiting for server IP and port...\r\n");
    scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
    printf("MBED: Address received: %d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);

    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();

    printf("MBED: TCPClient IP Address is %s\r\n", eth.getIPAddress());
    sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);

    TCPSocketConnection socket;
    while (socket.connect(buffer, port) < 0) {
        printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer, port);
        wait(1);
    }

    // Test loop for multiple client connections
    bool result = true;
    int count_error = 0;
    for (int i = 0; i < MAX_ECHO_LOOPS; i++) {
        std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand);
        socket.send_all(out_buffer, BUFFER_SIZE);

        int n = socket.receive(buffer, BUFFER_SIZE);
        if (n > 0)
        {
            bool echoed = memcmp(out_buffer, buffer, BUFFER_SIZE) == 0;
            result = result && echoed;
            if (echoed == false) {
                count_error++;  // Count error messages
            }
        }
    }

    printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS);

    if (notify_completion_str(result, buffer)) {
        socket.send_all(buffer, strlen(buffer));
    }
    socket.close();
    eth.disconnect();
    notify_completion(result);
    return 0;
}
开发者ID:haydar9,项目名称:mbed,代码行数:51,代码来源:main.cpp

示例10: main

int main() {
    GREENTEA_SETUP(20, "tcp_echo_client");

    EthernetInterface eth;
    eth.connect();

    printf("MBED: TCPClient IP address is '%s'\n", eth.get_ip_address());
    printf("MBED: TCPClient waiting for server IP and port...\n");

    greentea_send_kv("target_ip", eth.get_ip_address());

    bool result = false;

    char recv_key[] = "host_port";
    char ipbuf[60] = {0};
    char portbuf[16] = {0};
    unsigned int port = 0;

    greentea_send_kv("host_ip", " ");
    greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));

    greentea_send_kv("host_port", " ");
    greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
    sscanf(portbuf, "%u", &port);

    printf("MBED: Server IP address received: %s:%d \n", ipbuf, port);

    TCPSocket sock(&eth);
    SocketAddress tcp_addr(ipbuf, port);
    if (sock.connect(tcp_addr) == 0) {
        printf("HTTP: Connected to %s:%d\r\n", ipbuf, port);
        printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer));
        printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer));

        prep_buffer(tx_buffer, sizeof(tx_buffer));
        sock.send(tx_buffer, sizeof(tx_buffer));

        // Server will respond with HTTP GET's success code
        const int ret = sock.recv(rx_buffer, sizeof(rx_buffer));
        
        result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer));
        
        TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer));
        TEST_ASSERT_EQUAL(true, result);
    }

    sock.close();
    eth.disconnect();
    GREENTEA_TESTSUITE_RESULT(result);
}
开发者ID:kjbracey-arm,项目名称:mbed,代码行数:50,代码来源:main.cpp

示例11: main

int main() {
    GREENTEA_SETUP(20, "udp_echo_client");

    EthernetInterface eth;
    eth.connect();
    printf("UDP client IP Address is %s\n", eth.get_ip_address());

    greentea_send_kv("target_ip", eth.get_ip_address());

    bool result = true;

    char recv_key[] = "host_port";
    char ipbuf[60] = {0};
    char portbuf[16] = {0};
    unsigned int port = 0;

    UDPSocket sock;
    sock.open(&eth);

    greentea_send_kv("host_ip", " ");
    greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));

    greentea_send_kv("host_port", " ");
    greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
    sscanf(portbuf, "%u", &port);

    printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);

    SocketAddress addr(ipbuf, port);

    for (int i=0; i < ECHO_LOOPS; ++i) {
        prep_buffer(tx_buffer, sizeof(tx_buffer));
        const int ret = sock.sendto(addr, tx_buffer, sizeof(tx_buffer));
        printf("[%02d] sent...%d Bytes \n", i, ret);

        const int n = sock.recvfrom(&addr, rx_buffer, sizeof(rx_buffer));
        printf("[%02d] recv...%d Bytes \n", i, n);

        if (memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer))) {
            result = false;
            break;
        }
    }

    sock.close();
    eth.disconnect();
    GREENTEA_TESTSUITE_RESULT(result);
}
开发者ID:Neuromancer2701,项目名称:mbedROS2_STF7,代码行数:48,代码来源:main.cpp

示例12: main

int main(void)
{
    EthernetInterface eth;

    eth.init(); //Use DHCP
    eth.connect();
    printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT);

    Thread UdpServerTask(udp_server_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25);
    Thread UdpClientTask(udp_client_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25);

    // Control TCP server to get mbed statistics
    {
        const int TELNET_SERVER_PORT = 23;
        const int BUFFER_SIZE = 256;
        TCPSocketServer server;
        server.bind(TELNET_SERVER_PORT);
        server.listen();

        while (true) {
            printf("Wait for new connection...\n");
            TCPSocketConnection client;
            server.accept(client);
            client.set_blocking(false, 1500); // Timeout after (1.5)s
            printf("Connection from: %s\n", client.get_address());

            char buffer[BUFFER_SIZE] = { 0 };
            while (true) {
                int n = client.receive(buffer, sizeof(buffer));
                //if (n <= 0) break;
                if (n > 0) {
                    printf("Recv %d chars\r\n", n);
                    const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n;
                    buffer[buffer_string_end_index] = '\0';
                    // client.send_all(buffer, strlen(buffer));
                    if (strncmp(buffer, "stat", 4) == 0) {
                        sprintf(buffer, "received_packets %d\nforwarded_packets %d\nmax_queue_len %d",
                                received_packets, forwarded_packets, max_queue_len);
                        client.send_all(buffer, strlen(buffer));
                        // printf("%s", buffer);
                    }
                }
                //if (n <= 0) break;
            }
            client.close();
        }
    }
}
开发者ID:23chrischen,项目名称:mbed,代码行数:48,代码来源:main.cpp

示例13: app_start

void app_start(int argc, char *argv[]) {
    (void)argc;
    (void)argv;
    MBED_HOSTTEST_TIMEOUT(20);
    MBED_HOSTTEST_SELECT(tcpecho_client_auto);
    MBED_HOSTTEST_DESCRIPTION(TCP echo client);
    MBED_HOSTTEST_START("NET_4");
    socket_error_t err = lwipv4_socket_init();
    TEST_EQ(err, SOCKET_ERROR_NONE);

    memset(buffer, 0, sizeof(buffer));
    port = 0;
    s_ip_address ip_addr = {0, 0, 0, 0};
    printf("TCPClient waiting for server IP and port..." NL);
    scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
    printf("Address received:%d.%d.%d.%d:%d" NL, ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);

    eth.init(); //Use DHCP
    eth.connect();

    printf("TCPClient IP Address is %s" NL, eth.getIPAddress());
    sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);
    client = new TCPEchoClient(SOCKET_STACK_LWIP_IPV4);

    {
        FunctionPointer2<void, char *, uint16_t> fp(client, &TCPEchoClient::start_test);
        minar::Scheduler::postCallback(fp.bind(buffer, port));
    }
}
开发者ID:yogpan01,项目名称:sockets,代码行数:29,代码来源:main.cpp

示例14: app_start

void app_start(int /*argc*/, char* /*argv*/[]) {

    //Sets the console baud-rate
    output.baud(115200);
    
    // MAC address handling
    mbed_mac_address(mmac);

    // This sets up the network interface configuration which will be used
    // by LWM2M Client API to communicate with mbed Device server.
    //eth.init("192.168.1.248","255.255.255.0","192.168.1.1"); 
    eth.init(); //Use DHCP
    eth.connect();

    lwipv4_socket_init();
    output.printf("IP address %s\r\n", eth.getIPAddress());

    // On press of SW3 button on K64F board, example application
    // will call unregister API towards mbed Device Server
    unreg_button.fall(&mbed_client,&MbedClient::test_unregister);

    // On press of SW2 button on K64F board, example application
    // will send observation towards mbed Device Server
    obs_button.fall(&mbed_client,&MbedClient::update_resource);

    // Create LWM2M Client API interface to manage register and unregister
    mbed_client.create_interface();

    // Create LWM2M server object specifying mbed device server
    // information.
    M2MSecurity* register_object = mbed_client.create_register_object();

    // Create LWM2M device object specifying device resources
    // as per OMA LWM2M specification.
    M2MDevice* device_object = mbed_client.create_device_object();

    // Create Generic object specifying custom resources
    M2MObject* generic_object = mbed_client.create_generic_object();
    
    // Create LED Object
    M2MObject* led_object = mbed_client.create_led_object();

    // Add all the objects that you would like to register
    // into the list and pass the list for register API.
    M2MObjectList object_list;
    object_list.push_back(device_object);
    object_list.push_back(generic_object);
    object_list.push_back(led_object);

    mbed_client.set_register_object(register_object);

    // Issue register command.
    FunctionPointer2<void, M2MSecurity*, M2MObjectList> fp(&mbed_client, &MbedClient::test_register);
    minar::Scheduler::postCallback(fp.bind(register_object,object_list));
    minar::Scheduler::postCallback(&mbed_client,&MbedClient::test_update_register).period(minar::milliseconds(25000));
}
开发者ID:ARMmbed,项目名称:mbed-client-examples-ibm,代码行数:56,代码来源:main.cpp

示例15: setupEthernet

void setupEthernet()
{

    lcd.printf("Setup Ethernet...\r\n");   // Starting
    eth.init();   // Initialize
    while (eth.connect()) {   // timeout and loop until connected
        lcd.printf("Connect timeout\r\n");
    };
    lcd.printf("IP Address is %s\r\n", eth.getIPAddress());   // successful connect
}
开发者ID:Jbossalini,项目名称:m2x-mbed,代码行数:10,代码来源:main.cpp


注:本文中的EthernetInterface类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。