fork

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "UbloxATCellularInterface.h"
00003 #include "greentea-client/test_env.h"
00004 #include "unity.h"
00005 #include "utest.h"
00006 #include "UDPSocket.h"
00007 #ifdef FEATURE_COMMON_PAL
00008 #include "mbed_trace.h"
00009 #define TRACE_GROUP "TEST"
00010 #else
00011 #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__)
00012 #define tr_info(format, ...)  debug(format "\n", ## __VA_ARGS__)
00013 #define tr_warn(format, ...)  debug(format "\n", ## __VA_ARGS__)
00014 #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__)
00015 #endif
00016 
00017 using namespace utest::v1;
00018 
00019 // IMPORTANT!!! if you make a change to the tests here you should
00020 // check whether the same change should be made to the tests under
00021 // the PPP interface.
00022 
00023 // NOTE: these test are only as reliable as UDP across the internet
00024 // over a radio link.  The tests expect an NTP server to respond
00025 // to UDP packets and, if configured, an echo server to respond
00026 // to UDP packets.  This simply may not happen.  Please be patient.
00027 
00028 // ----------------------------------------------------------------
00029 // COMPILE-TIME MACROS
00030 // ----------------------------------------------------------------
00031 
00032 // These macros can be overridden with an mbed_app.json file and
00033 // contents of the following form:
00034 //
00035 //{
00036 //    "config": {
00037 //        "default-pin": {
00038 //            "value": "\"1234\""
00039 //        }
00040 //}
00041 //
00042 // See the template_mbed_app.txt in this directory for a fuller example.
00043 
00044 // Whether debug trace is on
00045 #ifndef MBED_CONF_APP_DEBUG_ON
00046 # define MBED_CONF_APP_DEBUG_ON false
00047 #endif
00048 
00049 // Run the SIM change tests, which require the DEFAULT_PIN
00050 // above to be correct for the board on which the test
00051 // is being run (and the SIM PIN to be disabled before tests run).
00052 #ifndef MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
00053 # define MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS 0
00054 #endif
00055 
00056 #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
00057 # ifndef MBED_CONF_APP_DEFAULT_PIN
00058 #   error "MBED_CONF_APP_DEFAULT_PIN must be defined to run the SIM tests"
00059 # endif
00060 # ifndef MBED_CONF_APP_ALT_PIN
00061 #   error "MBED_CONF_APP_ALT_PIN must be defined to run the SIM tests"
00062 # endif
00063 # ifndef MBED_CONF_APP_INCORRECT_PIN
00064 #   error "MBED_CONF_APP_INCORRECT_PIN must be defined to run the SIM tests"
00065 # endif
00066 #endif
00067 
00068 // The credentials of the SIM in the board.
00069 #ifndef MBED_CONF_APP_DEFAULT_PIN
00070 // Note: if PIN is enabled on your SIM, or you wish to run the SIM PIN change
00071 // tests, you must define the PIN for your SIM (see note above on using
00072 // mbed_app.json to do so).
00073 # define MBED_CONF_APP_DEFAULT_PIN "0000"
00074 #endif
00075 #ifndef MBED_CONF_APP_APN
00076 # define MBED_CONF_APP_APN         NULL
00077 #endif
00078 #ifndef MBED_CONF_APP_USERNAME
00079 # define MBED_CONF_APP_USERNAME    NULL
00080 #endif
00081 #ifndef MBED_CONF_APP_PASSWORD
00082 # define MBED_CONF_APP_PASSWORD    NULL
00083 #endif
00084 
00085 // Alternate PIN to use during pin change testing
00086 #ifndef MBED_CONF_APP_ALT_PIN
00087 # define MBED_CONF_APP_ALT_PIN    "9876"
00088 #endif
00089 
00090 // A PIN that is definitely incorrect
00091 #ifndef MBED_CONF_APP_INCORRECT_PIN
00092 # define MBED_CONF_APP_INCORRECT_PIN "1530"
00093 #endif
00094 
00095 // Servers and ports
00096 #ifdef MBED_CONF_APP_ECHO_SERVER
00097 # ifndef MBED_CONF_APP_ECHO_UDP_PORT
00098 #  error "MBED_CONF_APP_ECHO_UDP_PORT (the port on which your echo server echoes UDP packets) must be defined"
00099 # endif
00100 # ifndef MBED_CONF_APP_ECHO_TCP_PORT
00101 #  error "MBED_CONF_APP_ECHO_TCP_PORT (the port on which your echo server echoes TCP packets) must be defined"
00102 # endif
00103 #endif
00104 
00105 #ifndef MBED_CONF_APP_NTP_SERVER
00106 # define MBED_CONF_APP_NTP_SERVER "2.pool.ntp.org"
00107 #else
00108 # ifndef MBED_CONF_APP_NTP_PORT
00109 #  error "MBED_CONF_APP_NTP_PORT must be defined if MBED_CONF_APP_NTP_SERVER is defined"
00110 # endif
00111 #endif
00112 #ifndef MBED_CONF_APP_NTP_PORT
00113 # define MBED_CONF_APP_NTP_PORT 123
00114 #endif
00115 
00116 #ifndef MBED_CONF_APP_LOCAL_PORT
00117 # define MBED_CONF_APP_LOCAL_PORT 15
00118 #endif
00119 
00120 // UDP packet size limit for testing
00121 #ifndef MBED_CONF_APP_UDP_MAX_PACKET_SIZE
00122 #  define MBED_CONF_APP_UDP_MAX_PACKET_SIZE 1024
00123 #endif
00124 
00125 // The maximum size of UDP data fragmented across
00126 // multiple packets
00127 #ifndef MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE
00128 # define MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE 1500
00129 #endif
00130 
00131 // TCP packet size limit for testing
00132 #ifndef MBED_CONF_APP_MBED_CONF_APP_TCP_MAX_PACKET_SIZE
00133 # define MBED_CONF_APP_TCP_MAX_PACKET_SIZE 1500
00134 #endif
00135 
00136 // The number of retries for UDP exchanges
00137 #define NUM_UDP_RETRIES 5
00138 
00139 // How long to wait for stuff to travel in the async echo tests
00140 #define ASYNC_TEST_WAIT_TIME 10000
00141 
00142 // The maximum number of sockets that can be open at one time
00143 #define MAX_NUM_SOCKETS 7
00144 
00145 int previousSelectedRat = -1, previousPreferredRat = -1, previousSecondPreferredRat = -1;
00146 
00147 // ----------------------------------------------------------------
00148 // PRIVATE VARIABLES
00149 // ----------------------------------------------------------------
00150 
00151 #ifdef FEATURE_COMMON_PAL
00152 // Lock for debug prints
00153 static Mutex mtx;
00154 #endif
00155 
00156 // An instance of the cellular interface
00157 static UbloxATCellularInterface *interface =
00158        new UbloxATCellularInterface(MDMTXD, MDMRXD,
00159                                     MBED_CONF_UBLOX_CELL_BAUD_RATE,
00160                                     MBED_CONF_APP_DEBUG_ON);
00161 
00162 // Connection flag
00163 static bool connection_has_gone_down = false;
00164 
00165 // Data to exchange
00166 static const char send_data[] =  "_____0000:0123456789012345678901234567890123456789"
00167                                  "01234567890123456789012345678901234567890123456789"
00168                                  "_____0100:0123456789012345678901234567890123456789"
00169                                  "01234567890123456789012345678901234567890123456789"
00170                                  "_____0200:0123456789012345678901234567890123456789"
00171                                  "01234567890123456789012345678901234567890123456789"
00172                                  "_____0300:0123456789012345678901234567890123456789"
00173                                  "01234567890123456789012345678901234567890123456789"
00174                                  "_____0400:0123456789012345678901234567890123456789"
00175                                  "01234567890123456789012345678901234567890123456789"
00176                                  "_____0500:0123456789012345678901234567890123456789"
00177                                  "01234567890123456789012345678901234567890123456789"
00178                                  "_____0600:0123456789012345678901234567890123456789"
00179                                  "01234567890123456789012345678901234567890123456789"
00180                                  "_____0700:0123456789012345678901234567890123456789"
00181                                  "01234567890123456789012345678901234567890123456789"
00182                                  "_____0800:0123456789012345678901234567890123456789"
00183                                  "01234567890123456789012345678901234567890123456789"
00184                                  "_____0900:0123456789012345678901234567890123456789"
00185                                  "01234567890123456789012345678901234567890123456789"
00186                                  "_____1000:0123456789012345678901234567890123456789"
00187                                  "01234567890123456789012345678901234567890123456789"
00188                                  "_____1100:0123456789012345678901234567890123456789"
00189                                  "01234567890123456789012345678901234567890123456789"
00190                                  "_____1200:0123456789012345678901234567890123456789"
00191                                  "01234567890123456789012345678901234567890123456789"
00192                                  "_____1300:0123456789012345678901234567890123456789"
00193                                  "01234567890123456789012345678901234567890123456789"
00194                                  "_____1400:0123456789012345678901234567890123456789"
00195                                  "01234567890123456789012345678901234567890123456789"
00196                                  "_____1500:0123456789012345678901234567890123456789"
00197                                  "01234567890123456789012345678901234567890123456789"
00198                                  "_____1600:0123456789012345678901234567890123456789"
00199                                  "01234567890123456789012345678901234567890123456789"
00200                                  "_____1700:0123456789012345678901234567890123456789"
00201                                  "01234567890123456789012345678901234567890123456789"
00202                                  "_____1800:0123456789012345678901234567890123456789"
00203                                  "01234567890123456789012345678901234567890123456789"
00204                                  "_____1900:0123456789012345678901234567890123456789"
00205                                  "01234567890123456789012345678901234567890123456789"
00206                                  "_____2000:0123456789012345678901234567890123456789"
00207                                  "01234567890123456789012345678901234567890123456789";
00208 
00209 // ----------------------------------------------------------------
00210 // PRIVATE FUNCTIONS
00211 // ----------------------------------------------------------------
00212 
00213 #ifdef FEATURE_COMMON_PAL
00214 // Locks for debug prints
00215 static void lock()
00216 {
00217     mtx.lock();
00218 }
00219 
00220 static void unlock()
00221 {
00222     mtx.unlock();
00223 }
00224 #endif
00225 
00226 // Callback in case the connection goes down
00227 static void connection_down_cb(nsapi_error_t err)
00228 {
00229     connection_has_gone_down = true;
00230 }
00231 
00232 #ifdef MBED_CONF_APP_ECHO_SERVER
00233 // Make sure that size is greater than 0 and no more than limit,
00234 // useful since, when moduloing a very large number number,
00235 // compilers sometimes screw up and produce a small *negative*
00236 // number.  Who knew?  For example, GCC decided that
00237 // 492318453 (0x1d582ef5) modulo 508 was -47 (0xffffffd1).
00238 static int fix (int size, int limit)
00239 {
00240     if (size <= 0) {
00241         size = limit / 2; // better than 1
00242     } else if (size > limit) {
00243         size = limit;
00244     }
00245 
00246     return size;
00247 }
00248 
00249 // Do a UDP socket echo test to a given host of a given packet size
00250 static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size)
00251 {
00252     bool success = false;
00253     void * recv_data = malloc (size);
00254     SocketAddress sender_address;
00255     TEST_ASSERT(recv_data != NULL);
00256 
00257     // Retry this a few times, don't want to fail due to a flaky link
00258     for (int x = 0; !success && (x < NUM_UDP_RETRIES); x++) {
00259         tr_debug("Echo testing UDP packet size %d byte(s), try %d.", size, x + 1);
00260         if ((sock->sendto(*host_address, (void*) send_data, size) == size) &&
00261             (sock->recvfrom(&sender_address, recv_data, size) == size)) {
00262             TEST_ASSERT (memcmp(send_data, recv_data, size) == 0);
00263             TEST_ASSERT (strcmp(sender_address.get_ip_address(), host_address->get_ip_address()) == 0);
00264             TEST_ASSERT (sender_address.get_port() == host_address->get_port());
00265             success = true;
00266         }
00267     }
00268     TEST_ASSERT (success);
00269     TEST_ASSERT(!connection_has_gone_down);
00270 
00271     free (recv_data);
00272 }
00273 
00274 // The asynchronous callback
00275 static void async_cb(bool *callback_triggered)
00276 {
00277 
00278     TEST_ASSERT (callback_triggered != NULL);
00279     *callback_triggered = true;
00280 }
00281 
00282 // Do a UDP echo but using the asynchronous interface; we can exchange
00283 // packets longer in size than one UDP packet this way
00284 static void do_udp_echo_async(UDPSocket *sock, SocketAddress *host_address,
00285                               int size, bool *callback_triggered)
00286 {
00287     void * recv_data = malloc (size);
00288     int recv_size = 0;
00289     SocketAddress sender_address;
00290     Timer timer;
00291     int x, y, z;
00292     TEST_ASSERT(recv_data != NULL);
00293 
00294     *callback_triggered = false;
00295     for (y = 0; (recv_size < size) && (y < NUM_UDP_RETRIES); y++) {
00296         tr_debug("Echo testing UDP packet size %d byte(s) async, try %d.", size, y + 1);
00297         recv_size = 0;
00298         // Retry this a few times, don't want to fail due to a flaky link
00299         if (sock->sendto(*host_address, (void *) send_data, size) == size) {
00300             // Wait for all the echoed data to arrive
00301             timer.start();
00302             while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) {
00303                 if (*callback_triggered) {
00304                     *callback_triggered = false;
00305                     x = sock->recvfrom(&sender_address, (char *) recv_data + recv_size, size);
00306                     if (x > 0) {
00307                         recv_size += x;
00308                     }
00309                     tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, size - recv_size);
00310                     TEST_ASSERT(strcmp(sender_address.get_ip_address(), host_address->get_ip_address()) == 0);
00311                     TEST_ASSERT(sender_address.get_port() == host_address->get_port());
00312                 }
00313                 wait_ms(10);
00314             }
00315             timer.stop();
00316             timer.reset();
00317 
00318             // If everything arrived back, check it's the same as we sent
00319             if (recv_size == size) {
00320                 z = memcmp(send_data, recv_data, size);
00321                 if (z != 0) {
00322                     tr_debug("WARNING: mismatch, retrying");
00323                     tr_debug("Sent %d, |%*.*s|", size, size, size, send_data);
00324                     tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data);
00325                     // If things don't match, it could be due to data loss (this is UDP
00326                     // you know...), so set recv_size to 0 to cause another try
00327                     recv_size = 0;
00328                 }
00329             }
00330         }
00331     }
00332 
00333     TEST_ASSERT(recv_size == size);
00334     TEST_ASSERT(!connection_has_gone_down);
00335 
00336     free (recv_data);
00337 }
00338 
00339 // Send an entire TCP data buffer until done
00340 static int sendAll(TCPSocket *sock,  const char *data, int size)
00341 {
00342     int x;
00343     int count = 0;
00344     Timer timer;
00345 
00346     timer.start();
00347     while ((count < size) && (timer.read_ms() < 10000)) {
00348         x = sock->send(data + count, size - count);
00349         if (x > 0) {
00350             count += x;
00351             tr_debug("%d byte(s) sent, %d left to send.", count, size - count);
00352         }
00353         wait_ms(10);
00354     }
00355     timer.stop();
00356 
00357     return count;
00358 }
00359 
00360 // Do a TCP echo but using the asynchronous interface
00361 static void do_tcp_echo_async(TCPSocket *sock, int size, bool *callback_triggered)
00362 {
00363     void * recv_data = malloc (size);
00364     int recv_size = 0;
00365     int x, y;
00366     Timer timer;
00367     TEST_ASSERT(recv_data != NULL);
00368 
00369     *callback_triggered = false;
00370     tr_debug("Echo testing TCP packet size %d byte(s) async.", size);
00371     TEST_ASSERT (sendAll(sock, send_data, size) == size);
00372 
00373     // Wait for all the echoed data to arrive
00374     timer.start();
00375     while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) {
00376         if (*callback_triggered) {
00377             *callback_triggered = false;
00378             x = sock->recv((char *) recv_data + recv_size, size);
00379             TEST_ASSERT(x > 0);
00380             recv_size += x;
00381             tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, size - recv_size);
00382         }
00383         wait_ms(10);
00384     }
00385     TEST_ASSERT(recv_size == size);
00386     y = memcmp(send_data, recv_data, size);
00387     if (y != 0) {
00388         tr_debug("Sent %d, |%*.*s|", size, size, size, send_data);
00389         tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data);
00390         TEST_ASSERT(false);
00391     }
00392     timer.stop();
00393 
00394     TEST_ASSERT(!connection_has_gone_down);
00395 
00396     free (recv_data);
00397 }
00398 #endif
00399 
00400 // Get NTP time from a socket
00401 static void do_ntp_sock (UDPSocket *sock, SocketAddress ntp_address)
00402 {
00403     char ntp_values[48] = { 0 };
00404     time_t timestamp = 0;
00405     int len;
00406     bool comms_done = false;
00407 
00408     ntp_values[0] = '\x1b';
00409 
00410     // Retry this a few times, don't want to fail due to a flaky link
00411     for (unsigned int x = 0; !comms_done && (x < NUM_UDP_RETRIES); x++) {
00412         sock->sendto(ntp_address, (void*) ntp_values, sizeof(ntp_values));
00413         len = sock->recvfrom(&ntp_address, (void*) ntp_values, sizeof(ntp_values));
00414         if (len > 0) {
00415             comms_done = true;
00416         }
00417     }
00418     TEST_ASSERT (comms_done);
00419 
00420     tr_debug("UDP: %d byte(s) returned by NTP server.", len);
00421     if (len >= 43) {
00422         struct tm *localTime;
00423         time_t TIME1970 = 2208988800U;
00424         timestamp |= ((int) *(ntp_values + 40)) << 24;
00425         timestamp |= ((int) *(ntp_values + 41)) << 16;
00426         timestamp |= ((int) *(ntp_values + 42)) << 8;
00427         timestamp |= ((int) *(ntp_values + 43));
00428         timestamp -= TIME1970;
00429         srand (timestamp);
00430         tr_debug("srand() called");
00431         localTime = localtime(&timestamp);
00432         if (localTime) {
00433             char timeString[25];
00434             if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
00435                 printf("NTP timestamp is %s.\n", timeString);
00436             }
00437         }
00438     }
00439 }
00440 
00441 // Get NTP time
00442 static void do_ntp(UbloxATCellularInterface *interface)
00443 {
00444     UDPSocket sock;
00445     SocketAddress host_address;
00446 
00447     TEST_ASSERT(sock.open(interface) == 0)
00448 
00449     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0);
00450     host_address.set_port(MBED_CONF_APP_NTP_PORT);
00451 
00452     tr_debug("UDP: NIST server %s address: %s on port %d.", MBED_CONF_APP_NTP_SERVER,
00453              host_address.get_ip_address(), host_address.get_port());
00454 
00455     sock.set_timeout(10000);
00456 
00457     do_ntp_sock(&sock, host_address);
00458 
00459     sock.close();
00460 }
00461 
00462 // Use a connection, checking that it is good
00463 static void use_connection(UbloxATCellularInterface *interface)
00464 {
00465     const char * ip_address = interface->get_ip_address();
00466     const char * net_mask = interface->get_netmask();
00467     const char * gateway = interface->get_gateway();
00468 
00469     TEST_ASSERT(interface->is_connected());
00470 
00471     TEST_ASSERT(ip_address != NULL);
00472     tr_debug ("IP address %s.", ip_address);
00473     TEST_ASSERT(net_mask == NULL);
00474     tr_debug ("Net mask %s.", net_mask);
00475     TEST_ASSERT(gateway != NULL);
00476     tr_debug ("Gateway %s.", gateway);
00477 
00478     do_ntp(interface);
00479     TEST_ASSERT(!connection_has_gone_down);
00480 }
00481 
00482 // Drop a connection and check that it has dropped
00483 static void drop_connection(UbloxATCellularInterface *interface)
00484 {
00485     TEST_ASSERT(interface->disconnect() == 0);
00486     TEST_ASSERT(connection_has_gone_down);
00487     connection_has_gone_down = false;
00488     TEST_ASSERT(!interface->is_connected());
00489 }
00490 
00491 // ----------------------------------------------------------------
00492 // TESTS
00493 // ----------------------------------------------------------------
00494 
00495 // Tests of stuff in the base class
00496 void test_base_class() {
00497     const char *imei;
00498     const char *meid;
00499     const char *imsi;
00500     const char *iccid;
00501     int rssi;
00502 
00503     // Power-up the modem
00504     interface->init();
00505 
00506     // Check all of the IMEI, MEID, IMSI and ICCID calls
00507     imei = interface->imei();
00508     if (imei != NULL) {
00509         tr_debug("IMEI is %s.", imei);
00510     } else {
00511         TEST_ASSERT(false);
00512     }
00513 
00514     meid = interface->meid();
00515     if (meid != NULL) {
00516         tr_debug("MEID is %s.", meid);
00517     } else {
00518         TEST_ASSERT(false);
00519     }
00520 
00521     imsi = interface->imsi();
00522     if (imsi != NULL) {
00523         tr_debug("IMSI is %s.", imsi);
00524     } else {
00525         TEST_ASSERT(false);
00526     }
00527 
00528     iccid = interface->iccid();
00529     if (iccid != NULL) {
00530         tr_debug("ICCID is %s.", iccid);
00531     } else {
00532         TEST_ASSERT(false);
00533     }
00534 
00535     // Check the RSSI call at least doesn't assert
00536     rssi = interface->rssi();
00537     tr_debug("RSSI is %d dBm.", rssi);
00538 
00539     // Now connect and check that the answers for the
00540     // static fields are the same while connected
00541     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00542                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00543 
00544     TEST_ASSERT(strcmp(imei, interface->imei()) == 0);
00545     TEST_ASSERT(strcmp(meid, interface->meid()) == 0);
00546     TEST_ASSERT(strcmp(imsi, interface->imsi()) == 0);
00547     TEST_ASSERT(strcmp(iccid, interface->iccid()) == 0);
00548 
00549     // Check that the RSSI call still doesn't assert
00550     rssi = interface->rssi();
00551     tr_debug("RSSI is %d dBm.", rssi);
00552 }
00553 
00554 // Call srand() using the NTP server
00555 void test_set_randomise() {
00556     UDPSocket sock;
00557     SocketAddress host_address;
00558 
00559     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00560                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00561     do_ntp(interface);
00562     TEST_ASSERT(!connection_has_gone_down);
00563     drop_connection(interface);
00564 }
00565 
00566 #ifdef MBED_CONF_APP_ECHO_SERVER
00567 
00568 // Test UDP data exchange
00569 void test_udp_echo() {
00570     UDPSocket sock;
00571     SocketAddress host_address;
00572     SocketAddress local_address;
00573     int x;
00574     int size;
00575 
00576     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00577                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00578 
00579     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
00580     host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
00581 
00582     tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
00583              host_address.get_ip_address(), host_address.get_port());
00584 
00585     TEST_ASSERT(sock.open(interface) == 0)
00586 
00587     // Do a bind, just for the helluvit
00588     local_address.set_port(MBED_CONF_APP_LOCAL_PORT);
00589     TEST_ASSERT(sock.bind(local_address) == 0);
00590 
00591     sock.set_timeout(10000);
00592 
00593     // Test min, max, and some random sizes in-between
00594     do_udp_echo(&sock, &host_address, 1);
00595     do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
00596     for (x = 0; x < 10; x++) {
00597         size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1;
00598         size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
00599         do_udp_echo(&sock, &host_address, size);
00600     }
00601 
00602     sock.close();
00603     drop_connection(interface);
00604     tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.",
00605              x, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
00606 }
00607 
00608 // Test many different sizes of UDP data arriving at once
00609 void  test_udp_echo_recv_sizes() {
00610     UDPSocket sock;
00611     SocketAddress host_address;
00612     int x, y, z;
00613     int size;
00614     int tries = 0;
00615     unsigned int offset;
00616     char * recv_data;
00617     bool packetLoss;
00618     bool sendSuccess;
00619     Timer timer;
00620 
00621     interface->deinit();
00622     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00623                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00624 
00625     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
00626     host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
00627 
00628     tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
00629              host_address.get_ip_address(), host_address.get_port());
00630 
00631     TEST_ASSERT(sock.open(interface) == 0)
00632 
00633     do {
00634         tr_debug("--- UDP packet size test, test try %d, flushing input buffers", tries + 1);
00635         // First of all, clear any junk from the socket
00636         sock.set_timeout(1000);
00637         recv_data = (char *) malloc (MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
00638         TEST_ASSERT(recv_data != NULL);
00639         while (sock.recvfrom(&host_address, (void *) recv_data, MBED_CONF_APP_UDP_MAX_PACKET_SIZE) > 0) {
00640             // Throw it away
00641         }
00642         free (recv_data);
00643 
00644         sock.set_timeout(10000);
00645 
00646         // Throw random sized UDP packets up...
00647         x = 0;
00648         offset = 0;
00649         while (offset < sizeof (send_data)) {
00650             size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1;
00651             size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2);
00652             if (offset + size > sizeof (send_data)) {
00653                 size = sizeof (send_data) - offset;
00654             }
00655             sendSuccess = false;
00656             for (y = 0; !sendSuccess && (y < NUM_UDP_RETRIES); y++) {
00657                 tr_debug("Sending UDP packet number %d, size %d byte(s), send try %d.", x + 1, size, y + 1);
00658                 if (sock.sendto(host_address, (void *) (send_data + offset), size) == size) {
00659                     sendSuccess = true;
00660                     offset += size;
00661                 }
00662             }
00663             TEST_ASSERT(sendSuccess);
00664             x++;
00665         }
00666         tr_debug("--- All UDP packets sent");
00667 
00668         // ...and capture them all again afterwards
00669         recv_data = (char *) malloc (sizeof (send_data));
00670         TEST_ASSERT(recv_data != NULL);
00671         memset (recv_data, 0, sizeof (send_data));
00672         size = 0;
00673         y = 0;
00674         packetLoss = false;
00675         timer.start();
00676         while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 10000)) {
00677             y = sock.recvfrom(&host_address, (void *) (recv_data + size), sizeof (send_data) - size);
00678             if (y > 0) {
00679                 size += y;
00680             }
00681         }
00682         timer.stop();
00683         timer.reset();
00684         tr_debug(   "--- Either received everything back or timed out waiting");
00685 
00686         // Check that we reassembled everything correctly
00687         if (size == sizeof (send_data)) {
00688             for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) {
00689             }
00690             if (x != sizeof (send_data)) {
00691                 y = x - 5;
00692                 if (y < 0) {
00693                     y = 0;
00694                 }
00695                 z = 10;
00696                 if (y + z > (int) sizeof (send_data)) {
00697                     z = sizeof(send_data) - y;
00698                 }
00699                 tr_debug("   --- Difference at character %d (send \"%*.*s\", recv \"%*.*s\")",
00700                          x + 1, z, z, send_data + y, z, z, recv_data + y);
00701                 packetLoss = true;
00702             }
00703         } else {
00704             tr_debug("   --- %d bytes missing (%d bytes received when %d were expected))",
00705                       sizeof (send_data) - size, size, sizeof (send_data));
00706             packetLoss = true;
00707         }
00708         free (recv_data);
00709         tries++;
00710     } while (packetLoss && (tries < NUM_UDP_RETRIES));
00711 
00712     TEST_ASSERT(!packetLoss);
00713     TEST_ASSERT(!connection_has_gone_down);
00714     sock.close();
00715     drop_connection(interface);
00716 }
00717 
00718 // Test UDP data exchange via the asynchronous sigio() mechanism
00719 void test_udp_echo_async() {
00720     UDPSocket sock;
00721     SocketAddress host_address;
00722     SocketAddress local_address;
00723     bool callback_triggered = false;
00724     int x;
00725     int size;
00726 
00727     interface->deinit();
00728     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00729                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00730 
00731     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
00732     host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
00733 
00734     tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
00735              host_address.get_ip_address(), host_address.get_port());
00736 
00737     TEST_ASSERT(sock.open(interface) == 0)
00738 
00739     // Set up the async callback and set the timeout to zero
00740     sock.sigio(callback(async_cb, &callback_triggered));
00741     sock.set_timeout(0);
00742 
00743     // Test min, max, and some random sizes in-between
00744     // and this time allow the UDP packets to be fragmented
00745     do_udp_echo_async(&sock, &host_address, 1, &callback_triggered);
00746     do_udp_echo_async(&sock, &host_address, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE,
00747                       &callback_triggered);
00748     for (x = 0; x < 10; x++) {
00749         size = (rand() % MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE) + 1;
00750         size = fix(size, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE);
00751         do_udp_echo_async(&sock, &host_address, size, &callback_triggered);
00752     }
00753 
00754     sock.close();
00755 
00756     drop_connection(interface);
00757 
00758     tr_debug("%d UDP packets of size up to %d byte(s) echoed asynchronously and successfully.",
00759              x, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE);
00760 }
00761 
00762 // Test many different sizes of TCP data arriving at once
00763 void  test_tcp_echo_recv_sizes() {
00764     TCPSocket sock;
00765     SocketAddress host_address;
00766     int x, y, z;
00767     int size;
00768     unsigned int offset;
00769     char * recv_data;
00770     Timer timer;
00771 
00772     interface->deinit();
00773     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00774                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00775 
00776     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
00777     host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);
00778 
00779     tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
00780              host_address.get_ip_address(), host_address.get_port());
00781 
00782     TEST_ASSERT(sock.open(interface) == 0)
00783 
00784     TEST_ASSERT(sock.connect(host_address) == 0);
00785 
00786     sock.set_timeout(10000);
00787 
00788     // Throw random sized TCP packets up...
00789     x = 0;
00790     offset = 0;
00791     while (offset < sizeof (send_data)) {
00792         size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1;
00793         size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2);
00794         if (offset + size > sizeof (send_data)) {
00795             size = sizeof (send_data) - offset;
00796         }
00797         tr_debug("Sending TCP packet number %d, size %d byte(s).", x + 1, size);
00798         TEST_ASSERT(sendAll(&sock, (send_data + offset), size) == size);
00799         offset += size;
00800         x++;
00801     }
00802 
00803     // ...and capture them all again afterwards
00804     recv_data = (char *) malloc (sizeof (send_data));
00805     TEST_ASSERT(recv_data != NULL);
00806     memset (recv_data, 0, sizeof (send_data));
00807     size = 0;
00808     x = 0;
00809     timer.start();
00810     while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 30000)) {
00811         y = sock.recv((void *) (recv_data + size), sizeof (send_data) - size);
00812         tr_debug("Received TCP packet number %d, size %d byte(s).", x, y);
00813         size += y;
00814         x++;
00815     }
00816     timer.stop();
00817     timer.reset();
00818 
00819     // Check that we reassembled everything correctly
00820     for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) {
00821     }
00822     if (x != sizeof (send_data)) {
00823         y = x - 5;
00824         if (y < 0) {
00825             y = 0;
00826         }
00827         z = 10;
00828         if (y + z > (int) sizeof (send_data)) {
00829             z = sizeof(send_data) - y;
00830         }
00831         tr_debug("Difference at character %d (send \"%*.*s\", recv \"%*.*s\")",
00832                  x + 1, z, z, send_data + y, z, z, recv_data + y);
00833         TEST_ASSERT(false);
00834     }
00835     free (recv_data);
00836 
00837     TEST_ASSERT(!connection_has_gone_down);
00838     sock.close();
00839     drop_connection(interface);
00840 }
00841 
00842 // Test TCP data exchange via the asynchronous sigio() mechanism
00843 void test_tcp_echo_async() {
00844     TCPSocket sock;
00845     SocketAddress host_address;
00846     bool callback_triggered = false;
00847     int x;
00848     int size;
00849 
00850     interface->deinit();
00851     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00852                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00853 
00854     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
00855     host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);
00856 
00857     tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
00858              host_address.get_ip_address(), host_address.get_port());
00859 
00860     TEST_ASSERT(sock.open(interface) == 0)
00861 
00862     // Set up the async callback and set the timeout to zero
00863     sock.sigio(callback(async_cb, &callback_triggered));
00864     sock.set_timeout(0);
00865 
00866     TEST_ASSERT(sock.connect(host_address) == 0);
00867     // Test min, max, and some random sizes in-between
00868     do_tcp_echo_async(&sock, 1, &callback_triggered);
00869     do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, &callback_triggered);
00870     for (x = 0; x < 10; x++) {
00871         size = (rand() % MBED_CONF_APP_TCP_MAX_PACKET_SIZE) + 1;
00872         size = fix(size, MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
00873         do_tcp_echo_async(&sock, size, &callback_triggered);
00874     }
00875 
00876     sock.close();
00877 
00878     drop_connection(interface);
00879 
00880     tr_debug("%d TCP packets of size up to %d byte(s) echoed asynchronously and successfully.",
00881              x, MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
00882 }
00883 #endif
00884 
00885 // Allocate max sockets
00886 void test_max_sockets() {
00887     UDPSocket sock[MAX_NUM_SOCKETS];
00888     UDPSocket sockNone;
00889     SocketAddress host_address;
00890 
00891     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00892                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00893 
00894     TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0);
00895     host_address.set_port(MBED_CONF_APP_NTP_PORT);
00896 
00897     // Open the first socket and use it
00898     TEST_ASSERT(sock[0].open(interface) == 0)
00899     sock[0].set_timeout(10000);
00900     do_ntp_sock(&sock[0], host_address);
00901 
00902     // Check that we stop being able to get sockets at the max number
00903     for (int x = 1; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) {
00904         TEST_ASSERT(sock[x].open(interface) == 0)
00905     }
00906     TEST_ASSERT(sockNone.open(interface) < 0);
00907 
00908     // Now use the last one
00909     sock[sizeof (sock) / sizeof (sock[0]) - 1].set_timeout(10000);
00910     do_ntp_sock(&sock[sizeof (sock) / sizeof (sock[0]) - 1], host_address);
00911 
00912     // Close all of the sockets
00913     for (int x = 0; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) {
00914         TEST_ASSERT(sock[x].close() == 0);
00915     }
00916 
00917     drop_connection(interface);
00918 }
00919 
00920 // Connect with credentials included in the connect request
00921 void test_connect_credentials() {
00922 
00923     interface->deinit();
00924     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00925                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00926     use_connection(interface);
00927     drop_connection(interface);
00928 }
00929 
00930 // Test with credentials preset
00931 void test_connect_preset_credentials() {
00932 
00933     interface->deinit();
00934     TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN));
00935     interface->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
00936                                MBED_CONF_APP_PASSWORD);
00937     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN) == 0);
00938     use_connection(interface);
00939     drop_connection(interface);
00940 }
00941 
00942 // Test adding and using a SIM pin, then removing it, using the pending
00943 // mechanism where the change doesn't occur until connect() is called
00944 void test_check_sim_pin_pending() {
00945 
00946     interface->deinit();
00947 
00948     // Enable PIN checking (which will use the current PIN)
00949     // and also flag that the PIN should be changed to MBED_CONF_APP_ALT_PIN,
00950     // then try connecting
00951     interface->set_sim_pin_check(true);
00952     interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN);
00953     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00954                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00955     use_connection(interface);
00956     drop_connection(interface);
00957     interface->deinit();
00958 
00959     // Now change the PIN back to what it was before
00960     interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
00961     TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN,
00962                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00963     use_connection(interface);
00964     drop_connection(interface);
00965     interface->deinit();
00966 
00967     // Check that it was changed back, and this time
00968     // use the other way of entering the PIN
00969     interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
00970     TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
00971                                    MBED_CONF_APP_PASSWORD) == 0);
00972     use_connection(interface);
00973     drop_connection(interface);
00974     interface->deinit();
00975 
00976     // Remove PIN checking again and check that it no
00977     // longer matters what the PIN is
00978     interface->set_sim_pin_check(false);
00979     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00980                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00981     use_connection(interface);
00982     drop_connection(interface);
00983     interface->deinit();
00984     TEST_ASSERT(interface->init(NULL));
00985     TEST_ASSERT(interface->connect(MBED_CONF_APP_INCORRECT_PIN, MBED_CONF_APP_APN,
00986                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00987     use_connection(interface);
00988     drop_connection(interface);
00989 
00990     // Put the SIM pin back to the correct value for any subsequent tests
00991     interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
00992 }
00993 
00994 // Test adding and using a SIM pin, then removing it, using the immediate
00995 // mechanism
00996 void test_check_sim_pin_immediate() {
00997 
00998     interface->deinit();
00999     interface->connection_status_cb(connection_down_cb);
01000 
01001     // Enable PIN checking (which will use the current PIN), change
01002     // the PIN to MBED_CONF_APP_ALT_PIN, then try connecting after powering on and
01003     // off the modem
01004     interface->set_sim_pin_check(true, true, MBED_CONF_APP_DEFAULT_PIN);
01005     interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN, true);
01006     interface->deinit();
01007     TEST_ASSERT(interface->init(NULL));
01008     TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN,
01009                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
01010     use_connection(interface);
01011     drop_connection(interface);
01012 
01013     interface->connection_status_cb(connection_down_cb);
01014 
01015     // Now change the PIN back to what it was before
01016     interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN, true);
01017     interface->deinit();
01018     interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
01019     TEST_ASSERT(interface->init(NULL));
01020     TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
01021                                    MBED_CONF_APP_PASSWORD) == 0);
01022     use_connection(interface);
01023     drop_connection(interface);
01024 
01025     interface->connection_status_cb(connection_down_cb);
01026 
01027     // Remove PIN checking again and check that it no
01028     // longer matters what the PIN is
01029     interface->set_sim_pin_check(false, true);
01030     interface->deinit();
01031     TEST_ASSERT(interface->init(MBED_CONF_APP_INCORRECT_PIN));
01032     TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
01033                                    MBED_CONF_APP_PASSWORD) == 0);
01034     use_connection(interface);
01035     drop_connection(interface);
01036 
01037     // Put the SIM pin back to the correct value for any subsequent tests
01038     interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
01039 }
01040 
01041 // Test being able to connect with a local instance of the interface
01042 // NOTE: since this local instance will fiddle with bits of HW that the
01043 // static instance thought it owned, the static instance will no longer
01044 // work afterwards, hence this must be run as the last test in the list
01045 void test_connect_local_instance_last_test() {
01046 
01047     UbloxATCellularInterface *pLocalInterface = NULL;
01048 
01049     pLocalInterface = new UbloxATCellularInterface(MDMTXD, MDMRXD,
01050                                                    MBED_CONF_UBLOX_CELL_BAUD_RATE,
01051                                                    MBED_CONF_APP_DEBUG_ON);
01052     pLocalInterface->connection_status_cb(connection_down_cb);
01053 
01054     TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
01055                                          MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
01056     use_connection(pLocalInterface);
01057     drop_connection(pLocalInterface);
01058     delete pLocalInterface;
01059 
01060     pLocalInterface = new UbloxATCellularInterface(MDMTXD, MDMRXD,
01061                                                    MBED_CONF_UBLOX_CELL_BAUD_RATE,
01062                                                    MBED_CONF_APP_DEBUG_ON);
01063     pLocalInterface->connection_status_cb(connection_down_cb);
01064 
01065     TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
01066                                          MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
01067     use_connection(pLocalInterface);
01068     drop_connection(pLocalInterface);
01069     delete pLocalInterface;
01070 }
01071 
01072 void test_set_new_rat() {
01073 
01074     int currentSelectedRat = -1, currentPreferredRat = -1, currentSecondPreferredRat = -1;
01075 
01076     // Power-up the modem
01077     TEST_ASSERT(interface->init());
01078 
01079     // Check if modem is registered with network
01080     if (interface->is_registered_csd() || interface->is_registered_psd() || interface->is_registered_eps()) {
01081         tr_error("RAT should only be set in detached state");
01082         // Deregister from Network
01083         drop_connection(interface);
01084     }
01085 
01086     // Get and store initial RAT set on modem
01087     TEST_ASSERT(interface->get_modem_rat(&previousSelectedRat, &previousPreferredRat, &previousSecondPreferredRat));
01088     tr_debug("previous selected RAT: %d\nprevious preferred RAT: %d\nprevious second preferred RAT: %d\n", previousSelectedRat, previousPreferredRat, previousSecondPreferredRat);
01089 
01090 #ifdef TARGET_UBLOX_C030_U201
01091     // Set new RAT
01092     TEST_ASSERT(interface->set_modem_rat(UbloxATCellularInterface::GSM_UMTS, UbloxATCellularInterface::UMTS));
01093     tr_debug("RAT configured\n");
01094 
01095     // Get latest set RAT on modem
01096     TEST_ASSERT(interface->get_modem_rat(&currentSelectedRat, &currentPreferredRat, &currentSecondPreferredRat));
01097     tr_debug("new selected RAT: %d\nnew preferred RAT: %d\nnew second preferred RAT: %d\n", currentSelectedRat, currentPreferredRat, currentSecondPreferredRat);
01098 
01099     // Check RAT configured correctly
01100     TEST_ASSERT((currentSelectedRat == UbloxATCellularInterface::GSM_UMTS) && (currentPreferredRat == UbloxATCellularInterface::UMTS));
01101 #endif
01102 
01103 #ifdef TARGET_UBLOX_C030_R412M
01104     // Set new RAT
01105     TEST_ASSERT(interface->set_modem_rat(UbloxATCellularInterface::LTE_CATM1, UbloxATCellularInterface::LTE_CATNB1));
01106     tr_debug("RAT configured\n");
01107 
01108     // Get latest set RAT on modem
01109     TEST_ASSERT(interface->get_modem_rat(&currentSelectedRat, &currentPreferredRat, &currentSecondPreferredRat));
01110     tr_debug("new selected RAT: %d\nnew preferred RAT: %d\nnew second preferred RAT: %d\n", currentSelectedRat, currentPreferredRat, currentSecondPreferredRat);
01111 
01112     // Check RAT configured correctly
01113     TEST_ASSERT((currentSelectedRat == UbloxATCellularInterface::LTE_CATM1) && (currentPreferredRat == UbloxATCellularInterface::LTE_CATNB1));
01114 #endif
01115 }
01116 
01117 void test_reboot() {
01118 
01119     // Rebooting modem for settings to take effect
01120     TEST_ASSERT(interface->reboot_modem());
01121 }
01122 
01123 void test_registration() {
01124 
01125     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
01126                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
01127 
01128     drop_connection(interface);
01129 }
01130 
01131 
01132 void test_set_previous_rat() {
01133 
01134     int currentSelectedRat = -1, currentPreferredRat = -1, currentSecondPreferredRat = -1;
01135 
01136     // Restore RAT to previous settings
01137     TEST_ASSERT(interface->set_modem_rat((UbloxATCellularInterface::RAT)previousSelectedRat, (UbloxATCellularInterface::RAT)previousPreferredRat, (UbloxATCellularInterface::RAT)previousSecondPreferredRat));
01138     tr_debug("RAT configured\n");
01139 
01140     TEST_ASSERT(interface->get_modem_rat(&currentSelectedRat, &currentPreferredRat, &currentSecondPreferredRat));
01141     tr_debug("current selected RAT: %d\ncurrent preferred RAT: %d\ncurrent second preferred RAT: %d\n", currentSelectedRat, currentPreferredRat, currentSecondPreferredRat);
01142 
01143     // Check RAT configured correctly
01144     TEST_ASSERT((currentSelectedRat == previousSelectedRat) && (currentPreferredRat == previousPreferredRat));
01145 
01146     // Rebooting modem for settings to take effect
01147     TEST_ASSERT(interface->reboot_modem());
01148 }
01149 
01150 #ifdef TARGET_UBLOX_C030_R41XM
01151 void test_mno_profile() {
01152 
01153     int previous_profile, current_profile;
01154 
01155     // Power-up the modem
01156     TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN));
01157 
01158     // Check if modem is registered with network
01159     if (interface->is_registered_csd() || interface->is_registered_psd() || interface->is_registered_eps()) {
01160         tr_error("MNO profile should only be set in detached state");
01161         // Deregister from Network
01162         drop_connection(interface);
01163     }
01164 
01165     // Getting current mno profile
01166     TEST_ASSERT(interface->get_mno_profile(&previous_profile));
01167     tr_debug("Previous MNO profile is: %d\n\n", previous_profile);
01168 
01169     // Set MNO profile
01170     TEST_ASSERT(interface->set_mno_profile((UbloxATCellularInterface::MNOProfile)MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE));
01171     tr_debug("MNO configured\n");
01172 
01173     // Rebooting modem for settings to take effect
01174     TEST_ASSERT(interface->reboot_modem());
01175     tr_debug("Reboot successful\n");
01176     wait_ms(5000);
01177 
01178     // Check MNO profile configured correctly
01179     TEST_ASSERT(interface->get_mno_profile(&current_profile));
01180     tr_debug("New MNO profile is: %d\n\n", current_profile);
01181 
01182     TEST_ASSERT((UbloxATCellularInterface::MNOProfile)current_profile == (UbloxATCellularInterface::MNOProfile)MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE);
01183 
01184     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
01185                                    MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
01186 
01187     interface->get_receive_period();
01188 
01189     drop_connection(interface);
01190 }
01191 
01192 void test_edrx() {
01193     const int c_edrx_value = 2;
01194 
01195     // Power-up the modem
01196     TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN));
01197 
01198     // Check if modem is registered with network
01199     if (interface->is_registered_csd() || interface->is_registered_psd() || interface->is_registered_eps()) {
01200         tr_error("set edrx in detached state");
01201         // Deregister from Network
01202         drop_connection(interface);
01203     }
01204     // Set MNO profile
01205     TEST_ASSERT(interface->set_mno_profile((UbloxATCellularInterface::MNOProfile)MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE));
01206     tr_debug("MNO configured\n");
01207 
01208     interface->set_receive_period(2, UbloxCellularBase::EDRXEUTRAN_WB_S1_mode, c_edrx_value);
01209     interface->set_receive_period(2, UbloxCellularBase::EDRXEUTRAN_NB_S1_mode, c_edrx_value);
01210 
01211     // Rebooting modem for settings to take effect
01212     TEST_ASSERT(interface->reboot_modem());
01213     tr_debug("Reboot successful\n");
01214     wait_ms(5000);
01215 
01216     //TEST_ASSERT(interface->get_receive_period() == c_edrx_value);
01217 
01218     TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
01219                                        MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
01220 
01221     TEST_ASSERT(interface->get_receive_period() == c_edrx_value);
01222 
01223     drop_connection(interface);
01224 
01225     interface->set_receive_period(3, UbloxCellularBase::EDRXEUTRAN_WB_S1_mode);
01226     interface->set_receive_period(3, UbloxCellularBase::EDRXEUTRAN_NB_S1_mode);
01227 
01228     // Set MNO profile
01229     TEST_ASSERT(interface->set_mno_profile((UbloxATCellularInterface::MNOProfile)(MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE+1)));
01230     tr_debug("MNO configured\n");
01231 
01232     // Rebooting modem for settings to take effect
01233     TEST_ASSERT(interface->reboot_modem());
01234     tr_debug("Reboot successful\n");
01235     wait_ms(5000);
01236 
01237     // Set MNO profile
01238     TEST_ASSERT(interface->set_mno_profile((UbloxATCellularInterface::MNOProfile)MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE));
01239     tr_debug("MNO configured\n");
01240 
01241     // Rebooting modem for settings to take effect
01242     TEST_ASSERT(interface->reboot_modem());
01243     tr_debug("Reboot successful\n");
01244     wait_ms(5000);
01245 }
01246 #endif
01247 
01248 // ----------------------------------------------------------------
01249 // TEST ENVIRONMENT
01250 // ----------------------------------------------------------------
01251 
01252 // Setup the test environment
01253 utest::v1::status_t test_setup(const size_t number_of_cases) {
01254     // Setup Greentea with a timeout
01255     GREENTEA_SETUP(960, "default_auto");
01256     return verbose_test_setup_handler(number_of_cases);
01257 }
01258 
01259 // IMPORTANT!!! if you make a change to the tests here you should
01260 // check whether the same change should be made to the tests under
01261 // the PPP interface.
01262 
01263 // Test cases
01264 Case cases[] = {
01265 #ifdef TARGET_UBLOX_C030_R41XM
01266     Case("MNO profile test", test_mno_profile),
01267     Case("edrx test", test_edrx),
01268 #endif
01269     Case("Base class tests", test_base_class),
01270     Case("Set randomise", test_set_randomise),
01271 #ifdef MBED_CONF_APP_ECHO_SERVER
01272     Case("UDP echo test", test_udp_echo),
01273 # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
01274     Case("UDP recv sizes", test_udp_echo_recv_sizes),
01275 # endif
01276     Case("UDP async echo test", test_udp_echo_async),
01277 # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
01278     Case("TCP recv sizes", test_tcp_echo_recv_sizes),
01279 # endif
01280     Case("TCP async echo test", test_tcp_echo_async),
01281 #endif
01282 #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
01283     Case("Alloc max sockets", test_max_sockets),
01284 #endif
01285     Case("Connect with credentials", test_connect_credentials),
01286     Case("Connect with preset credentials", test_connect_preset_credentials),
01287 #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
01288     Case("Check SIM pin, pending", test_check_sim_pin_pending),
01289     Case("Check SIM pin, immediate", test_check_sim_pin_immediate),
01290 #endif
01291 #if defined (TARGET_UBLOX_C030_U201) || defined (TARGET_UBLOX_C030_R412M)
01292     Case("Set RAT test", test_set_new_rat),
01293     Case("Reboot test", test_reboot),
01294     Case("Register with network test", test_registration),
01295     Case("Set previous RAT test", test_set_previous_rat),
01296 #endif
01297 #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 for this
01298     Case("Connect using local instance, must be last test", test_connect_local_instance_last_test)
01299 #endif
01300 };
01301 
01302 Specification specification(test_setup, cases);
01303 
01304 // ----------------------------------------------------------------
01305 // MAIN
01306 // ----------------------------------------------------------------
01307 
01308 int main() {
01309 
01310 #ifdef FEATURE_COMMON_PAL
01311     mbed_trace_init();
01312 
01313     mbed_trace_mutex_wait_function_set(lock);
01314     mbed_trace_mutex_release_function_set(unlock);
01315 #endif
01316 
01317     interface->connection_status_cb(connection_down_cb);
01318 
01319     // Run tests
01320     return !Harness::run(specification);
01321 }
01322 
01323 // End Of File