예제 #1
0
파일: main.cpp 프로젝트: 1deus/tmk_keyboard
int main() {
    MBED_HOSTTEST_TIMEOUT(15);
    MBED_HOSTTEST_SELECT(default_auto);
    MBED_HOSTTEST_DESCRIPTION(HTTP client hello world);
    MBED_HOSTTEST_START("NET_7");

    char http_request_buffer[BUFFER_SIZE + 1] = {0};
    HTTPClient http;
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();

    //GET data
    {
        bool result = true;
        const char *url_hello_txt = "http://developer.mbed.org/media/uploads/donatien/hello.txt";
        printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt);
        HTTPResult ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE);
        if (ret == HTTP_OK) {
            printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer);
        } else {
            printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
            result = false;
        }

        if (result == false) {
            eth.disconnect();
            MBED_HOSTTEST_RESULT(false);
        }
    }

    //POST data
    {
        bool result = true;
        const char *url_httpbin_post = "http://httpbin.org/post";
        HTTPText text(http_request_buffer, BUFFER_SIZE);
        HTTPMap map;
        map.put("Hello", "World");
        map.put("test", "1234");
        printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post);
        HTTPResult ret = http.post(url_httpbin_post, map, &text);
        if (ret == HTTP_OK) {
            printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer));
            printf("HTTP_POST: %s\r\n", http_request_buffer);
        } else {
            printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
            result = false;
        }

        if (result == false) {
            eth.disconnect();
            MBED_HOSTTEST_RESULT(false);
        }
    }
    eth.disconnect();
    MBED_HOSTTEST_RESULT(true);
}
예제 #2
0
파일: main.cpp 프로젝트: 23chrischen/mbed
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;
}
예제 #3
0
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;
}
예제 #4
0
파일: main.cpp 프로젝트: dinau/mbed
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);
}
예제 #5
0
void terminate(bool status, UDPEchoClient* )
{
    if (client) {
        printf("MBED: Test finished!");
        delete client;
        client = NULL;
        eth.disconnect();
        TEST_ASSERT_TRUE_MESSAGE(status, "MBED: test failed!");
        Harness::validate_callback();
    }
}
예제 #6
0
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);
}
예제 #7
0
파일: main.cpp 프로젝트: haydar9/mbed
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;
}
예제 #8
0
파일: main.cpp 프로젝트: kjbracey-arm/mbed
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);
}
예제 #9
0
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);
}
예제 #10
0
파일: main.cpp 프로젝트: AsamQi/mbed
int main() {
    bool result = false;
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("UDP client IP Address is %s\n", eth.getIPAddress());

    UDPSocket sock;
    sock.init();

    Endpoint nist;
    nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT);

    char out_buffer[] = "plop"; // Does not matter
    sock.sendTo(nist, out_buffer, sizeof(out_buffer));

    union {
        char in_buffer_tab[4];
        unsigned int in_buffer_uint;
    };

    const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab));
    if (n > 0) {
        result = true;
        const unsigned int timeRes = ntohl(in_buffer_uint);
        const float years = timeRes / 60.0 / 60.0 / 24.0 / 365;
        printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port());
        printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]");
        printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]");

        if (years < YEARS_TO_PASS) {
            result = false;
        }
    }
    sock.close();
    eth.disconnect();
    notify_completion(result);
    return 0;
}
예제 #11
0
파일: main.cpp 프로젝트: NXPmicro/mbed
void test_udp_dtls_handshake() {
    EthernetInterface eth;
    int err = eth.connect();
    TEST_ASSERT_EQUAL(0, err);

    printf("MBED: UDPClient IP address is '%s'\n", eth.get_ip_address());
    printf("MBED: UDPClient 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: UDP Server IP address received: %s:%d \n", ipbuf, port);

    // align each size to 4-bits
    for (int i = 0; i < udp_dtls_handshake_count; i++) {
        udp_dtls_handshake_pattern[i] = (~0xf & udp_dtls_handshake_pattern[i]) + 0x10;
    }

    printf("MBED: DTLS pattern [");
    for (int i = 0; i < udp_dtls_handshake_count; i++) {
        printf("%d", udp_dtls_handshake_pattern[i]);
        if (i != udp_dtls_handshake_count-1) {
            printf(", ");
        }
    }
    printf("]\r\n");

    UDPSocket sock;
    SocketAddress udp_addr(ipbuf, port);
    sock.set_timeout(MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT);

    for (int attempt = 0; attempt < MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES; attempt++) {
        err = sock.open(&eth);
        TEST_ASSERT_EQUAL(0, err);

        for (int i = 0; i < udp_dtls_handshake_count; i++) {
            buffer[i] = udp_dtls_handshake_pattern[i] >> 4;
        }

        err = sock.sendto(udp_addr, buffer, udp_dtls_handshake_count);
        printf("UDP: tx -> %d\r\n", err);
        TEST_ASSERT_EQUAL(udp_dtls_handshake_count, err);

        int step = 0;
        while (step < udp_dtls_handshake_count) {
            err = sock.recvfrom(NULL, buffer, sizeof(buffer));
            printf("UDP: rx <- %d ", err);

            // check length
            if (err != udp_dtls_handshake_pattern[step]) {
                printf("x (expected %d)\r\n", udp_dtls_handshake_pattern[step]);
                break;
            }

            // check quick xor of packet
            uint8_t check = 0;
            for (int j = 0; j < udp_dtls_handshake_pattern[step]; j++) {
                check ^= buffer[j];
            }

            if (check != 0) {
                printf("x (checksum 0x%02x)\r\n", check);
                break;
            }

            // successfully got a packet
            printf("\r\n");
            step += 1;
        }

        err = sock.close();
        TEST_ASSERT_EQUAL(0, err);

        // got through all steps, test passed
        if (step == udp_dtls_handshake_count) {
            result = true;
            break;
        }
    }

    eth.disconnect();
    TEST_ASSERT(result);
}
예제 #12
0
void HTTPAPI_Deinit(void)
{
    (void)eth.disconnect();
}
예제 #13
0
파일: main.cpp 프로젝트: yogpan01/sockets
void terminate(bool status, TCPEchoClient* client)
{
    delete client;
    eth.disconnect();
    MBED_HOSTTEST_RESULT(status);
}
예제 #14
0
void app_start(int /*argc*/, char* /*argv*/[]) {
#else
int main() {
#endif

    // Instantiate the class which implements
    // mbed Client API
    MbedClient mbed_client;

#ifdef TARGET_LIKE_LINUX
    pthread_t unregister_thread;
    pthread_t observation_thread;

    m2mclient = &mbed_client;

    trace_init();
    set_trace_print_function( trace_printer );
    set_trace_config(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_DEBUG|TRACE_CARRIAGE_RETURN);

    signal(SIGINT, (signalhandler_t)ctrl_c_handle_function);

#else
    // This sets up the network interface configuration which will be used
    // by LWM2M Client API to communicate with mbed Device server.

#ifdef SIXLOWPAN_INTERFACE

    Mesh6LoWPAN_ND *mesh_api = Mesh6LoWPAN_ND::getInstance();
    int8_t status;

    status = mesh_api->init(rf_device_register(), mesh_network_callback);
    if (status != MESH_ERROR_NONE)
    {
        tr_error("Mesh network initialization failed %d!", status);
        return 1;
    }

    status = mesh_api->connect();
    if (status != MESH_ERROR_NONE)
    {
        tr_error("Can't connect to mesh network!");
        return 1;
    }

    do
    {
        mesh_api->processEvent();
    } while(mesh_network_state != MESH_CONNECTED);


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

    lwipv4_socket_init();
#endif

    // Set up Hardware interrupt button.
    InterruptIn obs_button(OBS_BUTTON);
    InterruptIn unreg_button(UNREG_BUTTON);

    // 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);


#endif

    // Create LWM2M Client API interface to manage bootstrap,
    // register and unregister

    //These 2 are defining the secure connection usage
    M2MSecurity::SecurityModeType mode = M2MSecurity::NoSecurity;
    _have_secure_conn = (mode == M2MSecurity::Certificate || mode == M2MSecurity::Psk);
    //    SecurityNotSet,
    //    Psk = 0,
    //    Certificate = 2,
    //    NoSecurity = 3

    mbed_client.create_interface();

#ifdef TARGET_LIKE_LINUX

#if defined (BOOTSTRAP_ENABLED)
    pthread_t bootstrap_thread;
    // Create LWM2M bootstrap object specifying bootstrap server
    // information.
    M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn);
    // Issue bootstrap command.
    mbed_client.test_bootstrap(security_object);

    pthread_create(&bootstrap_thread, NULL, &wait_for_bootstrap, (void*) &mbed_client);
    pthread_join(bootstrap_thread, NULL);

#else
    M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn);

    if( mode == M2MSecurity::Certificate ){
        register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
        register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len);
        register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len);
        register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len);
    }else if( mode == M2MSecurity::Psk ){
        register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk);
        register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len);
        register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len);
        register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len);
    }else{
        register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity);
    }

    mbed_client.set_register_object(register_object);
    M2MDevice* device_object = mbed_client.create_device_object();

    M2MObject* object = mbed_client.create_generic_object();

    M2MObjectList object_list;
    object_list.push_back(device_object);
    object_list.push_back(object);

    mbed_client.test_register(object_list);
#endif

    pthread_create(&observation_thread, NULL, &send_observation, (void*) &mbed_client);
    pthread_create(&unregister_thread, NULL, &wait_for_unregister, (void*) &mbed_client);
    pthread_join(unregister_thread, NULL);

#else

#if defined (BOOTSTRAP_ENABLED)
    // Create LWM2M bootstrap object specifying bootstrap server
    // information.
    M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn);

    // Issue bootstrap command.
    mbed_client.test_bootstrap(security_object);

    // Wait till the bootstrap callback is called successfully.
    // Callback comes in bootstrap_done()
#ifdef SIXLOWPAN_INTERFACE
    /* wait network to be established */
    do {
        mesh_interface_run();
    } while(!mbed_client.bootstrap_successful());
#else
    while (!mbed_client.bootstrap_successful()) { __WFI(); }
#endif

#else

    M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn);

    if( mode == M2MSecurity::Certificate ){
        register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
        register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len);
        register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len);
        register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len);
    }else if( mode == M2MSecurity::Psk ){
        register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk);
        register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len);
        register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len);
        register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len);
    }else{
        register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity);
    }

    mbed_client.set_register_object(register_object);

#endif

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

    M2MObject* object = mbed_client.create_generic_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(object);

    // Issue register command.

    FunctionPointer1<void, M2MObjectList> fp(&mbed_client, &MbedClient::test_register);
    minar::Scheduler::postCallback(fp.bind(object_list));

    minar::Scheduler::start();

    // Wait till the register callback is called successfully.
    // Callback comes in object_registered()
#ifdef SIXLOWPAN_INTERFACE
    /* wait network to be established */
    do {
        mesh_api->processEvent();
    } while(!mbed_client.register_successful());
#endif

    // Wait for the unregister successful callback,
    // Callback comes in object_unregsitered(), this will be
    // waiting for user to press SW2 button on K64F board.
#ifdef SIXLOWPAN_INTERFACE
    /* wait network to be established */
    do {
        mesh_api->processEvent();
    } while(!mbed_client.unregister_successful());
#endif


#if defined(BOOTSTRAP_ENABLED)
    // This will turn on the LED on the board specifying that
    // the application has run successfully.
    notify_completion(mbed_client.unregister_successful() &&
                      mbed_client.register_successful() &&
                      mbed_client.bootstrap_successful());

    // Delete security object created for bootstrapping
    if(security_object) {
        delete security_object;
    }

#else

    // Disconnect the connect and teardown the network interface
#ifdef SIXLOWPAN_INTERFACE
    mesh_api->disconnect();
#else
    eth.disconnect();
#endif
#endif //BOOTSTRAP_ENABLED

    // Delete device object created for registering device
    // resources.
    if(device_object) {
        M2MDevice::delete_instance();
    }
    if(object) {
        delete object;
    }

#endif //TARGET_LIKE_LINUX

#ifndef TARGET_LIKE_MBED
return 0;
#endif
}
예제 #15
0
int main()
{
    setbuf(stdout, NULL); // no buffering for this filehandle
    char json[1024];

    int content_length;
    int response_code;
    char buf[1024];

    float amb_temp;

    setupEthernet();

    // set time
    lcd.printf("Reading Time... ");
    ntp.setTime("time-c.nist.gov");
    lcd.printf("Time set");


    while(1) {  // Loop
        char streamName[] = "amb-temp";  // Set value for stream you are using
     
        // GET Stream value
        response_code = getStream(streamName, json);
        content_length = readContentLength(json);
        pc.printf("GET Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

/////
        // PUT value to Strem
        amb_temp = tmp.read();

        sprintf(json, "{\"value\":\"%0.2f\"}", amb_temp);

        response_code = putStream(streamName, json);
        content_length = readContentLength(json);
        pc.printf("PUT Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);


//////
        // POST value(s) to Stream
        time_t seconds;

        seconds = time(NULL);   // get current time from mbed RTC
        strftime(buf,40, "%Y-%m-%dT%H:%M:%S%z", localtime(&seconds));

        amb_temp = tmp.read();

        sprintf(json, "{ \"values\": [ {\"at\": \"%s\", \"value\":\"%0.2f\"} ] }", buf, amb_temp);

        response_code = postStream(streamName, json);
        content_length = readContentLength(json);

        pc.printf("POST Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

///////
        // PUT Location to Feed
        sprintf(json, "{ \"name\": \"%s\", \"latitude\": \"%0.8f\", \"longitude\": \"%0.8f\", \"elevation\": \"%0.2f\" }", LOC_NAME, LOC_LAT, LOC_LONG, LOC_ELEV);

        response_code = putLocation(json);
        content_length = readContentLength(json);

        pc.printf("PUT Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

///////
        // GET Location of Feed
        response_code = getLocation(json);
        content_length = readContentLength(json);

        pc.printf("GET Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

    }

    eth.disconnect();

    while(1) {}
}