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