예제 #1
0
/**
 * Use a connection, checking that it is good
 * Checks via doing an NTP transaction
 */
static void use_connection(OnboardCellularInterface *driver)
{
    const char *ip_address = driver->get_ip_address();
    const char *net_mask = driver->get_netmask();
    const char *gateway = driver->get_gateway();

    TEST_ASSERT(driver->is_connected());

    TEST_ASSERT(ip_address != NULL);
    tr_debug("IP address %s.", ip_address);
    TEST_ASSERT(net_mask != NULL);
    tr_debug("Net mask %s.", net_mask);
    TEST_ASSERT(gateway != NULL);
    tr_debug("Gateway %s.", gateway);

    UDPSocket sock;
    SocketAddress host_address;

    TEST_ASSERT(driver->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
    host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);

    tr_debug("UDP: Server %s address: %s on port %d.",
             MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(),
             host_address.get_port());

    TEST_ASSERT(sock.open(driver) == 0)

    sock.set_timeout(10000);
    do_udp_echo(&sock, &host_address, 1);

    sock.close();
}
예제 #2
0
/**
 * Connect with credentials included in the connect request
 */
void test_connect_credentials()
{

    driver.disconnect();

    TEST_ASSERT(do_connect(&driver) == 0);
    use_connection(&driver);
    drop_connection(&driver);
}
예제 #3
0
/**
 * Test with credentials preset
 */
void test_connect_preset_credentials()
{
    driver.disconnect();
    driver.set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
    driver.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
                           MBED_CONF_APP_PASSWORD);
    int num_retries = 0;
    nsapi_error_t err = NSAPI_ERROR_OK;
    while (!driver.is_connected()) {
        err = driver.connect();
        if (err == NSAPI_ERROR_OK || num_retries > MBED_CONF_APP_MAX_RETRIES) {
            break;
        }
    }

    TEST_ASSERT(err == 0);
    use_connection(&driver);
    drop_connection(&driver);
}
예제 #4
0
/**
 * Test TCP data exchange via the asynchronous sigio() mechanism
 */
void test_tcp_echo_async()
{
    TCPSocket sock;
    SocketAddress host_address;
    bool callback_triggered = false;
    int x;
    int size;

    driver.disconnect();
    TEST_ASSERT(do_connect(&driver) == 0);

    TEST_ASSERT(
        driver.gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
    host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);

    tr_debug("TCP: Server %s address: %s on port %d.",
             MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(),
             host_address.get_port());

    TEST_ASSERT(sock.open(&driver) == 0)

    // Set up the async callback and set the timeout to zero
    sock.sigio(callback(async_cb, &callback_triggered));
    sock.set_timeout(0);

    TEST_ASSERT(sock.connect(host_address) == 0);
    // Test min, max, and some random sizes in-between
    do_tcp_echo_async(&sock, 1, &callback_triggered);
    do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE,
                      &callback_triggered);

    sock.close();

    drop_connection(&driver);

    tr_debug("TCP packets of size up to %d byte(s) echoed asynchronously and successfully.",
             MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
}
예제 #5
0
/**
 * Test UDP data exchange
 */
void test_udp_echo()
{
    UDPSocket sock;
    SocketAddress host_address;
    int x;
    int size;

    driver.disconnect();
    TEST_ASSERT(do_connect(&driver) == 0);

    TEST_ASSERT(driver.gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
    host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);

    tr_debug("UDP: Server %s address: %s on port %d.",
             MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(),
             host_address.get_port());

    TEST_ASSERT(sock.open(&driver) == 0)

    sock.set_timeout(10000);

    // Test min, max, and some random sizes in-between
    do_udp_echo(&sock, &host_address, 1);
    do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
    for (x = 0; x < 10; x++) {
        size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1;
        size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE + 1);
        do_udp_echo(&sock, &host_address, size);
    }

    sock.close();

    drop_connection(&driver);

    tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.", x,
             MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
}
예제 #6
0
/**
 * connect to the network
 */
static nsapi_error_t do_connect(OnboardCellularInterface *iface)
{
    int num_retries = 0;
    nsapi_error_t err = NSAPI_ERROR_OK;
    while (!iface->is_connected()) {
        err = driver.connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
                             MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
        if (err == NSAPI_ERROR_OK || num_retries > MBED_CONF_APP_MAX_RETRIES) {
            break;
        }
        num_retries++;
    }

    return err;
}
예제 #7
0
/**
 * Drop a connection and check that it has dropped
 */
static void drop_connection(OnboardCellularInterface *driver)
{
    TEST_ASSERT(driver->disconnect() == 0);
    TEST_ASSERT(!driver->is_connected());
}
예제 #8
0
/* \brief easy_connect     easy_connect() function to connect the pre-defined network bearer,
 *                         config done via mbed_app.json (see README.md for details).
 *
 * IN: bool  log_messages  print out diagnostics or not.
 */
NetworkInterface* easy_connect(bool log_messages) {
    NetworkInterface* network_interface = NULL;
    int connect_success = -1;

#if defined (EASY_CONNECT_WIFI)
    // We check if the _ssid and _password have already been set (via the easy_connect()
    // that takes thoses parameters or not.
    // If they have not been set, use the ones we can gain from mbed_app.json.
    if (_ssid == NULL) { 
        if(strlen(MBED_CONF_APP_WIFI_SSID) > WIFI_SSID_MAX_LEN) {
            printf("ERROR - MBED_CONF_APP_WIFI_SSID is too long %d vs. %d\n", 
                    strlen(MBED_CONF_APP_WIFI_SSID),
                    WIFI_SSID_MAX_LEN);
            return NULL;
        }
    }

    if (_password == NULL) {
        if(strlen(MBED_CONF_APP_WIFI_PASSWORD) > WIFI_PASSWORD_MAX_LEN) {
            printf("ERROR - MBED_CONF_APP_WIFI_PASSWORD is too long %d vs. %d\n", 
                    strlen(MBED_CONF_APP_WIFI_PASSWORD),
                    WIFI_PASSWORD_MAX_LEN);
            return NULL;
        }
    }
#endif // EASY_CONNECT_WIFI

    /// This should be removed once mbedOS supports proper dual-stack
    if (log_messages) {
#if defined (EASY_CONNECT_MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
        printf("[EasyConnect] IPv6 mode\n");
#else
        printf("[EasyConnect] IPv4 mode\n");
#endif
    }

#if defined (EASY_CONNECT_WIFI)
    if (log_messages) {
        printf("[EasyConnect] Using WiFi (%s) \n", EASY_CONNECT_WIFI_TYPE);
        printf("[EasyConnect] Connecting to WiFi %s\n", 
                ((_ssid == NULL) ? MBED_CONF_APP_WIFI_SSID : _ssid) );
    }
    network_interface = &wifi;
    if (_ssid == NULL) {
        connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD,
                          (strlen(MBED_CONF_APP_WIFI_PASSWORD) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE);
    }
    else {
        connect_success = wifi.connect(_ssid, _password, (strlen(_password) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE);
    }
#elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_ONBOARD || MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR
#  ifdef MBED_CONF_APP_CELLULAR_SIM_PIN
    cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
#  endif
#  ifdef MBED_CONF_APP_CELLULAR_APN
#    ifndef MBED_CONF_APP_CELLULAR_USERNAME
#      define MBED_CONF_APP_CELLULAR_USERNAME 0
#    endif
#    ifndef MBED_CONF_APP_CELLULAR_PASSWORD
#      define MBED_CONF_APP_CELLULAR_PASSWORD 0
#    endif
    cellular.set_credentials(MBED_CONF_APP_CELLULAR_APN, MBED_CONF_APP_CELLULAR_USERNAME, MBED_CONF_APP_CELLULAR_PASSWORD);
    if (log_messages) {
        printf("[EasyConnect] Connecting using Cellular interface and APN %s\n", MBED_CONF_APP_CELLULAR_APN);
    }
#  else
    if (log_messages) {
        printf("[EasyConnect] Connecting using Cellular interface and default APN\n");
    }
#  endif
    connect_success = cellular.connect();
    network_interface = &cellular;

#elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A
    if (log_messages) {
        printf("[EasyConnect] Using WNC14A2A\n");
    }
#   if MBED_CONF_APP_WNC_DEBUG == true
    printf("[EasyConnect] With WNC14A2A debug output set to 0x%02X\n",MBED_CONF_APP_WNC_DEBUG_SETTING);
    wnc.doDebug(MBED_CONF_APP_WNC_DEBUG_SETTING);
#   endif
    network_interface = &wnc;
    connect_success = wnc.connect();

#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
    if (log_messages) {
        printf("[EasyConnect] Using Ethernet\n");
    }
    network_interface = &eth;
#if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION
    eth.set_blocking(false);
#endif
    connect_success = eth.connect();
#endif

#ifdef EASY_CONNECT_MESH
    if (log_messages) {
        printf("[EasyConnect] Using Mesh (%s)\n", EASY_CONNECT_MESH_TYPE);
        printf("[EasyConnect] Connecting to Mesh...\n");
    }
    network_interface = &mesh;
#if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION
    mesh.set_blocking(false);
#endif
    mesh.initialize(&rf_phy);
    connect_success = mesh.connect();
#endif
    if(connect_success == 0
#if (MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION && (MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET || defined(EASY_CONNECT_MESH)))
            || connect_success == NSAPI_ERROR_IS_CONNECTED || connect_success == NSAPI_ERROR_ALREADY
#endif
            ) {
#if (MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION && (MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET || defined(EASY_CONNECT_MESH)))
        nsapi_connection_status_t connection_status;

        for (;;) {

            // Check current connection status.
            connection_status = network_interface->get_connection_status();

            if (connection_status == NSAPI_STATUS_GLOBAL_UP) {

                // Connection ready.
                break;

            } else if (connection_status == NSAPI_STATUS_ERROR_UNSUPPORTED) {

                if (log_messages) {
                    print_MAC(network_interface, log_messages);
                    printf("[EasyConnect] Connection to Network Failed %d!\n", connection_status);
                }
                return NULL;

            }

            // Not ready yet, give some runtime to the network stack.
            mbed::mbed_event_queue()->dispatch(100);

        }
#endif

        if (log_messages) {
            printf("[EasyConnect] Connected to Network successfully\n");
            print_MAC(network_interface, log_messages);
        }
    } else {
        if (log_messages) {
            print_MAC(network_interface, log_messages);
            printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
        }
        return NULL;
    }
    const char *ip_addr  = network_interface->get_ip_address();
    if (ip_addr == NULL) {
        if (log_messages) {
            printf("[EasyConnect] ERROR - No IP address\n");
        }
        return NULL;
    }

    if (log_messages) {
        printf("[EasyConnect] IP address %s\n", ip_addr);
    }
    return network_interface;
}