Implementation of the CellularInterface for u-blox C030 boards with N2xx modems. Note: requires the N211 module firmware to be at least 06.57 A01.02.

Dependents:   example-ublox-cellular-interface HelloMQTT example-ublox-cellular-interface_r410M example-ublox-mbed-client ... more

Committer:
philware
Date:
Tue Aug 22 16:37:30 2017 +0100
Revision:
4:2bf3875a13f1
Parent:
1:8ea78dce6b36
Child:
9:f6d022f5c4f5
Adding a SendATChopped() function to chop up very long AT command strings for the SendTo() function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
philware 1:8ea78dce6b36 1 #include "UbloxATCellularInterfaceN2xx.h"
philware 1:8ea78dce6b36 2 #include "greentea-client/test_env.h"
philware 1:8ea78dce6b36 3 #include "unity.h"
philware 1:8ea78dce6b36 4 #include "utest.h"
philware 1:8ea78dce6b36 5 #include "UDPSocket.h"
philware 1:8ea78dce6b36 6 #ifdef FEATURE_COMMON_PAL
philware 1:8ea78dce6b36 7 #include "mbed_trace.h"
philware 1:8ea78dce6b36 8 #define TRACE_GROUP "TEST"
philware 1:8ea78dce6b36 9 #else
philware 1:8ea78dce6b36 10 #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__)
philware 1:8ea78dce6b36 11 #define tr_info(format, ...) debug(format "\n", ## __VA_ARGS__)
philware 1:8ea78dce6b36 12 #define tr_warn(format, ...) debug(format "\n", ## __VA_ARGS__)
philware 1:8ea78dce6b36 13 #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__)
philware 1:8ea78dce6b36 14 #endif
philware 1:8ea78dce6b36 15
philware 1:8ea78dce6b36 16 using namespace utest::v1;
philware 1:8ea78dce6b36 17
philware 1:8ea78dce6b36 18 // IMPORTANT!!! if you make a change to the tests here you should
philware 1:8ea78dce6b36 19 // check whether the same change should be made to the tests under
philware 1:8ea78dce6b36 20 // the PPP interface.
philware 1:8ea78dce6b36 21
philware 1:8ea78dce6b36 22 // NOTE: these test are only as reliable as UDP across the internet
philware 1:8ea78dce6b36 23 // over a radio link. The tests expect an NTP server to respond
philware 1:8ea78dce6b36 24 // to UDP packets and, if configured, an echo server to respond
philware 1:8ea78dce6b36 25 // to UDP packets. This simply may not happen. Please be patient.
philware 1:8ea78dce6b36 26
philware 1:8ea78dce6b36 27 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 28 // COMPILE-TIME MACROS
philware 1:8ea78dce6b36 29 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 30
philware 1:8ea78dce6b36 31 // These macros can be overridden with an mbed_app.json file and
philware 1:8ea78dce6b36 32 // contents of the following form:
philware 1:8ea78dce6b36 33 //
philware 1:8ea78dce6b36 34 //{
philware 1:8ea78dce6b36 35 // "config": {
philware 1:8ea78dce6b36 36 // "default-pin": {
philware 1:8ea78dce6b36 37 // "value": "\"1234\""
philware 1:8ea78dce6b36 38 // }
philware 1:8ea78dce6b36 39 //}
philware 1:8ea78dce6b36 40 //
philware 1:8ea78dce6b36 41 // See the template_mbed_app.txt in this directory for a fuller example.
philware 1:8ea78dce6b36 42
philware 1:8ea78dce6b36 43 // Whether debug trace is on
philware 1:8ea78dce6b36 44 #ifndef MBED_CONF_APP_DEBUG_ON
philware 1:8ea78dce6b36 45 # define MBED_CONF_APP_DEBUG_ON false
philware 1:8ea78dce6b36 46 #endif
philware 1:8ea78dce6b36 47
philware 1:8ea78dce6b36 48 // Run the SIM change tests, which require the DEFAULT_PIN
philware 1:8ea78dce6b36 49 // above to be correct for the board on which the test
philware 1:8ea78dce6b36 50 // is being run (and the SIM PIN to be disabled before tests run).
philware 1:8ea78dce6b36 51 #ifndef MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
philware 1:8ea78dce6b36 52 # define MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS 0
philware 1:8ea78dce6b36 53 #endif
philware 1:8ea78dce6b36 54
philware 1:8ea78dce6b36 55 #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
philware 1:8ea78dce6b36 56 # ifndef MBED_CONF_APP_DEFAULT_PIN
philware 1:8ea78dce6b36 57 # error "MBED_CONF_APP_DEFAULT_PIN must be defined to run the SIM tests"
philware 1:8ea78dce6b36 58 # endif
philware 1:8ea78dce6b36 59 # ifndef MBED_CONF_APP_ALT_PIN
philware 1:8ea78dce6b36 60 # error "MBED_CONF_APP_ALT_PIN must be defined to run the SIM tests"
philware 1:8ea78dce6b36 61 # endif
philware 1:8ea78dce6b36 62 # ifndef MBED_CONF_APP_INCORRECT_PIN
philware 1:8ea78dce6b36 63 # error "MBED_CONF_APP_INCORRECT_PIN must be defined to run the SIM tests"
philware 1:8ea78dce6b36 64 # endif
philware 1:8ea78dce6b36 65 #endif
philware 1:8ea78dce6b36 66
philware 1:8ea78dce6b36 67 // The credentials of the SIM in the board.
philware 1:8ea78dce6b36 68 #ifndef MBED_CONF_APP_DEFAULT_PIN
philware 1:8ea78dce6b36 69 // Note: if PIN is enabled on your SIM, or you wish to run the SIM PIN change
philware 1:8ea78dce6b36 70 // tests, you must define the PIN for your SIM (see note above on using
philware 1:8ea78dce6b36 71 // mbed_app.json to do so).
philware 1:8ea78dce6b36 72 # define MBED_CONF_APP_DEFAULT_PIN "0000"
philware 1:8ea78dce6b36 73 #endif
philware 1:8ea78dce6b36 74 #ifndef MBED_CONF_APP_APN
philware 1:8ea78dce6b36 75 # define MBED_CONF_APP_APN NULL
philware 1:8ea78dce6b36 76 #endif
philware 1:8ea78dce6b36 77 #ifndef MBED_CONF_APP_USERNAME
philware 1:8ea78dce6b36 78 # define MBED_CONF_APP_USERNAME NULL
philware 1:8ea78dce6b36 79 #endif
philware 1:8ea78dce6b36 80 #ifndef MBED_CONF_APP_PASSWORD
philware 1:8ea78dce6b36 81 # define MBED_CONF_APP_PASSWORD NULL
philware 1:8ea78dce6b36 82 #endif
philware 1:8ea78dce6b36 83
philware 1:8ea78dce6b36 84 // Alternate PIN to use during pin change testing
philware 1:8ea78dce6b36 85 #ifndef MBED_CONF_APP_ALT_PIN
philware 1:8ea78dce6b36 86 # define MBED_CONF_APP_ALT_PIN "9876"
philware 1:8ea78dce6b36 87 #endif
philware 1:8ea78dce6b36 88
philware 1:8ea78dce6b36 89 // A PIN that is definitely incorrect
philware 1:8ea78dce6b36 90 #ifndef MBED_CONF_APP_INCORRECT_PIN
philware 1:8ea78dce6b36 91 # define MBED_CONF_APP_INCORRECT_PIN "1530"
philware 1:8ea78dce6b36 92 #endif
philware 1:8ea78dce6b36 93
philware 1:8ea78dce6b36 94 // Servers and ports
philware 1:8ea78dce6b36 95 #ifdef MBED_CONF_APP_ECHO_SERVER
philware 1:8ea78dce6b36 96 # ifndef MBED_CONF_APP_ECHO_UDP_PORT
philware 1:8ea78dce6b36 97 # error "MBED_CONF_APP_ECHO_UDP_PORT (the port on which your echo server echoes UDP packets) must be defined"
philware 1:8ea78dce6b36 98 # endif
philware 1:8ea78dce6b36 99 # ifndef MBED_CONF_APP_ECHO_TCP_PORT
philware 1:8ea78dce6b36 100 # error "MBED_CONF_APP_ECHO_TCP_PORT (the port on which your echo server echoes TCP packets) must be defined"
philware 1:8ea78dce6b36 101 # endif
philware 1:8ea78dce6b36 102 #endif
philware 1:8ea78dce6b36 103
philware 1:8ea78dce6b36 104 #ifndef MBED_CONF_APP_NTP_SERVER
philware 1:8ea78dce6b36 105 # define MBED_CONF_APP_NTP_SERVER "80.86.38.193"
philware 1:8ea78dce6b36 106 #else
philware 1:8ea78dce6b36 107 # ifndef MBED_CONF_APP_NTP_PORT
philware 1:8ea78dce6b36 108 # error "MBED_CONF_APP_NTP_PORT must be defined if MBED_CONF_APP_NTP_SERVER is defined"
philware 1:8ea78dce6b36 109 # endif
philware 1:8ea78dce6b36 110 #endif
philware 1:8ea78dce6b36 111 #ifndef MBED_CONF_APP_NTP_PORT
philware 1:8ea78dce6b36 112 # define MBED_CONF_APP_NTP_PORT 123
philware 1:8ea78dce6b36 113 #endif
philware 1:8ea78dce6b36 114
philware 1:8ea78dce6b36 115 #ifndef MBED_CONF_APP_LOCAL_PORT
philware 1:8ea78dce6b36 116 # define MBED_CONF_APP_LOCAL_PORT 15
philware 1:8ea78dce6b36 117 #endif
philware 1:8ea78dce6b36 118
philware 1:8ea78dce6b36 119 // UDP packet size limit for testing
philware 1:8ea78dce6b36 120 #ifndef MBED_CONF_APP_UDP_MAX_PACKET_SIZE
philware 1:8ea78dce6b36 121 # define MBED_CONF_APP_UDP_MAX_PACKET_SIZE 1024
philware 1:8ea78dce6b36 122 #endif
philware 1:8ea78dce6b36 123
philware 1:8ea78dce6b36 124 // The maximum size of UDP data fragmented across
philware 1:8ea78dce6b36 125 // multiple packets
philware 1:8ea78dce6b36 126 #ifndef MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE
philware 1:8ea78dce6b36 127 # define MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE 1500
philware 1:8ea78dce6b36 128 #endif
philware 1:8ea78dce6b36 129
philware 1:8ea78dce6b36 130 // TCP packet size limit for testing
philware 1:8ea78dce6b36 131 #ifndef MBED_CONF_APP_MBED_CONF_APP_TCP_MAX_PACKET_SIZE
philware 1:8ea78dce6b36 132 # define MBED_CONF_APP_TCP_MAX_PACKET_SIZE 1500
philware 1:8ea78dce6b36 133 #endif
philware 1:8ea78dce6b36 134
philware 1:8ea78dce6b36 135 // The number of retries for UDP exchanges
philware 1:8ea78dce6b36 136 #define NUM_UDP_RETRIES 5
philware 1:8ea78dce6b36 137
philware 1:8ea78dce6b36 138 // How long to wait for stuff to travel in the async echo tests
philware 1:8ea78dce6b36 139 #define ASYNC_TEST_WAIT_TIME 10000
philware 1:8ea78dce6b36 140
philware 1:8ea78dce6b36 141 // The maximum number of sockets that can be open at one time
philware 1:8ea78dce6b36 142 #define MAX_NUM_SOCKETS 7
philware 1:8ea78dce6b36 143
philware 1:8ea78dce6b36 144 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 145 // PRIVATE VARIABLES
philware 1:8ea78dce6b36 146 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 147
philware 1:8ea78dce6b36 148 #ifdef FEATURE_COMMON_PAL
philware 1:8ea78dce6b36 149 // Lock for debug prints
philware 1:8ea78dce6b36 150 static Mutex mtx;
philware 1:8ea78dce6b36 151 #endif
philware 1:8ea78dce6b36 152
philware 1:8ea78dce6b36 153 // An instance of the cellular interface
philware 1:8ea78dce6b36 154 static UbloxATCellularInterfaceN2xx *interface;
philware 1:8ea78dce6b36 155
philware 1:8ea78dce6b36 156 // Connection flag
philware 1:8ea78dce6b36 157 static bool connection_has_gone_down = false;
philware 1:8ea78dce6b36 158
philware 1:8ea78dce6b36 159 // Data to exchange
philware 1:8ea78dce6b36 160 static const char send_data[] = "_____0000:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 161 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 162 "_____0100:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 163 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 164 "_____0200:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 165 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 166 "_____0300:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 167 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 168 "_____0400:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 169 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 170 "_____0500:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 171 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 172 "_____0600:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 173 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 174 "_____0700:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 175 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 176 "_____0800:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 177 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 178 "_____0900:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 179 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 180 "_____1000:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 181 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 182 "_____1100:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 183 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 184 "_____1200:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 185 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 186 "_____1300:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 187 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 188 "_____1400:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 189 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 190 "_____1500:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 191 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 192 "_____1600:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 193 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 194 "_____1700:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 195 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 196 "_____1800:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 197 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 198 "_____1900:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 199 "01234567890123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 200 "_____2000:0123456789012345678901234567890123456789"
philware 1:8ea78dce6b36 201 "01234567890123456789012345678901234567890123456789";
philware 1:8ea78dce6b36 202
philware 1:8ea78dce6b36 203 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 204 // PRIVATE FUNCTIONS
philware 1:8ea78dce6b36 205 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 206
philware 1:8ea78dce6b36 207 #ifdef FEATURE_COMMON_PAL
philware 1:8ea78dce6b36 208 // Locks for debug prints
philware 1:8ea78dce6b36 209 static void lock()
philware 1:8ea78dce6b36 210 {
philware 1:8ea78dce6b36 211 mtx.lock();
philware 1:8ea78dce6b36 212 }
philware 1:8ea78dce6b36 213
philware 1:8ea78dce6b36 214 static void unlock()
philware 1:8ea78dce6b36 215 {
philware 1:8ea78dce6b36 216 mtx.unlock();
philware 1:8ea78dce6b36 217 }
philware 1:8ea78dce6b36 218 #endif
philware 1:8ea78dce6b36 219
philware 1:8ea78dce6b36 220 // Callback in case the connection goes down
philware 1:8ea78dce6b36 221 static void connection_down_cb(nsapi_error_t err)
philware 1:8ea78dce6b36 222 {
philware 1:8ea78dce6b36 223 connection_has_gone_down = true;
philware 1:8ea78dce6b36 224 }
philware 1:8ea78dce6b36 225
philware 1:8ea78dce6b36 226 #ifdef MBED_CONF_APP_ECHO_SERVER
philware 1:8ea78dce6b36 227 // Make sure that size is greater than 0 and no more than limit,
philware 1:8ea78dce6b36 228 // useful since, when moduloing a very large number number,
philware 1:8ea78dce6b36 229 // compilers sometimes screw up and produce a small *negative*
philware 1:8ea78dce6b36 230 // number. Who knew? For example, GCC decided that
philware 1:8ea78dce6b36 231 // 492318453 (0x1d582ef5) modulo 508 was -47 (0xffffffd1).
philware 1:8ea78dce6b36 232 static int fix (int size, int limit)
philware 1:8ea78dce6b36 233 {
philware 1:8ea78dce6b36 234 if (size <= 0) {
philware 1:8ea78dce6b36 235 size = limit / 2; // better than 1
philware 1:8ea78dce6b36 236 } else if (size > limit) {
philware 1:8ea78dce6b36 237 size = limit;
philware 1:8ea78dce6b36 238 }
philware 1:8ea78dce6b36 239
philware 1:8ea78dce6b36 240 return size;
philware 1:8ea78dce6b36 241 }
philware 1:8ea78dce6b36 242
philware 1:8ea78dce6b36 243 // Do a UDP socket echo test to a given host of a given packet size
philware 1:8ea78dce6b36 244 static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size)
philware 1:8ea78dce6b36 245 {
philware 1:8ea78dce6b36 246 bool success = false;
philware 1:8ea78dce6b36 247 void * recv_data = malloc (size);
philware 1:8ea78dce6b36 248 SocketAddress sender_address;
philware 1:8ea78dce6b36 249 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 250
philware 1:8ea78dce6b36 251 // Retry this a few times, don't want to fail due to a flaky link
philware 1:8ea78dce6b36 252 for (int x = 0; !success && (x < NUM_UDP_RETRIES); x++) {
philware 1:8ea78dce6b36 253 tr_debug("Echo testing UDP packet size %d byte(s), try %d.", size, x + 1);
philware 1:8ea78dce6b36 254 if ((sock->sendto(*host_address, (void*) send_data, size) == size) &&
philware 1:8ea78dce6b36 255 (sock->recvfrom(&sender_address, recv_data, size) == size)) {
philware 1:8ea78dce6b36 256 TEST_ASSERT (memcmp(send_data, recv_data, size) == 0);
philware 1:8ea78dce6b36 257 TEST_ASSERT (strcmp(sender_address.get_ip_address(), host_address->get_ip_address()) == 0);
philware 1:8ea78dce6b36 258 TEST_ASSERT (sender_address.get_port() == host_address->get_port());
philware 1:8ea78dce6b36 259 success = true;
philware 1:8ea78dce6b36 260 }
philware 1:8ea78dce6b36 261 }
philware 1:8ea78dce6b36 262 TEST_ASSERT (success);
philware 1:8ea78dce6b36 263 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 264
philware 1:8ea78dce6b36 265 free (recv_data);
philware 1:8ea78dce6b36 266 }
philware 1:8ea78dce6b36 267
philware 1:8ea78dce6b36 268 // The asynchronous callback
philware 1:8ea78dce6b36 269 static void async_cb(bool *callback_triggered)
philware 1:8ea78dce6b36 270 {
philware 1:8ea78dce6b36 271
philware 1:8ea78dce6b36 272 TEST_ASSERT (callback_triggered != NULL);
philware 1:8ea78dce6b36 273 *callback_triggered = true;
philware 1:8ea78dce6b36 274 }
philware 1:8ea78dce6b36 275
philware 1:8ea78dce6b36 276 // Do a UDP echo but using the asynchronous interface; we can exchange
philware 1:8ea78dce6b36 277 // packets longer in size than one UDP packet this way
philware 1:8ea78dce6b36 278 static void do_udp_echo_async(UDPSocket *sock, SocketAddress *host_address,
philware 1:8ea78dce6b36 279 int size, bool *callback_triggered)
philware 1:8ea78dce6b36 280 {
philware 1:8ea78dce6b36 281 void * recv_data = malloc (size);
philware 1:8ea78dce6b36 282 int recv_size = 0;
philware 1:8ea78dce6b36 283 SocketAddress sender_address;
philware 1:8ea78dce6b36 284 Timer timer;
philware 1:8ea78dce6b36 285 int x, y, z;
philware 1:8ea78dce6b36 286 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 287
philware 1:8ea78dce6b36 288 *callback_triggered = false;
philware 1:8ea78dce6b36 289 for (y = 0; (recv_size < size) && (y < NUM_UDP_RETRIES); y++) {
philware 1:8ea78dce6b36 290 tr_debug("Echo testing UDP packet size %d byte(s) async, try %d.", size, y + 1);
philware 1:8ea78dce6b36 291 recv_size = 0;
philware 1:8ea78dce6b36 292 // Retry this a few times, don't want to fail due to a flaky link
philware 1:8ea78dce6b36 293 if (sock->sendto(*host_address, (void *) send_data, size) == size) {
philware 1:8ea78dce6b36 294 // Wait for all the echoed data to arrive
philware 1:8ea78dce6b36 295 timer.start();
philware 1:8ea78dce6b36 296 while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) {
philware 1:8ea78dce6b36 297 if (*callback_triggered) {
philware 1:8ea78dce6b36 298 *callback_triggered = false;
philware 1:8ea78dce6b36 299 x = sock->recvfrom(&sender_address, (char *) recv_data + recv_size, size);
philware 1:8ea78dce6b36 300 if (x > 0) {
philware 1:8ea78dce6b36 301 recv_size += x;
philware 1:8ea78dce6b36 302 }
philware 1:8ea78dce6b36 303 tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, size - recv_size);
philware 1:8ea78dce6b36 304 TEST_ASSERT(strcmp(sender_address.get_ip_address(), host_address->get_ip_address()) == 0);
philware 1:8ea78dce6b36 305 TEST_ASSERT(sender_address.get_port() == host_address->get_port());
philware 1:8ea78dce6b36 306 }
philware 1:8ea78dce6b36 307 wait_ms(10);
philware 1:8ea78dce6b36 308 }
philware 1:8ea78dce6b36 309 timer.stop();
philware 1:8ea78dce6b36 310 timer.reset();
philware 1:8ea78dce6b36 311
philware 1:8ea78dce6b36 312 // If everything arrived back, check it's the same as we sent
philware 1:8ea78dce6b36 313 if (recv_size == size) {
philware 1:8ea78dce6b36 314 z = memcmp(send_data, recv_data, size);
philware 1:8ea78dce6b36 315 if (z != 0) {
philware 1:8ea78dce6b36 316 tr_debug("WARNING: mismatch, retrying");
philware 1:8ea78dce6b36 317 tr_debug("Sent %d, |%*.*s|", size, size, size, send_data);
philware 1:8ea78dce6b36 318 tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data);
philware 1:8ea78dce6b36 319 // If things don't match, it could be due to data loss (this is UDP
philware 1:8ea78dce6b36 320 // you know...), so set recv_size to 0 to cause another try
philware 1:8ea78dce6b36 321 recv_size = 0;
philware 1:8ea78dce6b36 322 }
philware 1:8ea78dce6b36 323 }
philware 1:8ea78dce6b36 324 }
philware 1:8ea78dce6b36 325 }
philware 1:8ea78dce6b36 326
philware 1:8ea78dce6b36 327 TEST_ASSERT(recv_size == size);
philware 1:8ea78dce6b36 328 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 329
philware 1:8ea78dce6b36 330 free (recv_data);
philware 1:8ea78dce6b36 331 }
philware 1:8ea78dce6b36 332
philware 1:8ea78dce6b36 333 // Send an entire TCP data buffer until done
philware 1:8ea78dce6b36 334 static int sendAll(TCPSocket *sock, const char *data, int size)
philware 1:8ea78dce6b36 335 {
philware 1:8ea78dce6b36 336 int x;
philware 1:8ea78dce6b36 337 int count = 0;
philware 1:8ea78dce6b36 338 Timer timer;
philware 1:8ea78dce6b36 339
philware 1:8ea78dce6b36 340 timer.start();
philware 1:8ea78dce6b36 341 while ((count < size) && (timer.read_ms() < 10000)) {
philware 1:8ea78dce6b36 342 x = sock->send(data + count, size - count);
philware 1:8ea78dce6b36 343 if (x > 0) {
philware 1:8ea78dce6b36 344 count += x;
philware 1:8ea78dce6b36 345 tr_debug("%d byte(s) sent, %d left to send.", count, size - count);
philware 1:8ea78dce6b36 346 }
philware 1:8ea78dce6b36 347 wait_ms(10);
philware 1:8ea78dce6b36 348 }
philware 1:8ea78dce6b36 349 timer.stop();
philware 1:8ea78dce6b36 350
philware 1:8ea78dce6b36 351 return count;
philware 1:8ea78dce6b36 352 }
philware 1:8ea78dce6b36 353
philware 1:8ea78dce6b36 354 // Do a TCP echo but using the asynchronous interface
philware 1:8ea78dce6b36 355 static void do_tcp_echo_async(TCPSocket *sock, int size, bool *callback_triggered)
philware 1:8ea78dce6b36 356 {
philware 1:8ea78dce6b36 357 void * recv_data = malloc (size);
philware 1:8ea78dce6b36 358 int recv_size = 0;
philware 1:8ea78dce6b36 359 int x, y;
philware 1:8ea78dce6b36 360 Timer timer;
philware 1:8ea78dce6b36 361 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 362
philware 1:8ea78dce6b36 363 *callback_triggered = false;
philware 1:8ea78dce6b36 364 tr_debug("Echo testing TCP packet size %d byte(s) async.", size);
philware 1:8ea78dce6b36 365 TEST_ASSERT (sendAll(sock, send_data, size) == size);
philware 1:8ea78dce6b36 366
philware 1:8ea78dce6b36 367 // Wait for all the echoed data to arrive
philware 1:8ea78dce6b36 368 timer.start();
philware 1:8ea78dce6b36 369 while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) {
philware 1:8ea78dce6b36 370 if (*callback_triggered) {
philware 1:8ea78dce6b36 371 *callback_triggered = false;
philware 1:8ea78dce6b36 372 x = sock->recv((char *) recv_data + recv_size, size);
philware 1:8ea78dce6b36 373 TEST_ASSERT(x > 0);
philware 1:8ea78dce6b36 374 recv_size += x;
philware 1:8ea78dce6b36 375 tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, size - recv_size);
philware 1:8ea78dce6b36 376 }
philware 1:8ea78dce6b36 377 wait_ms(10);
philware 1:8ea78dce6b36 378 }
philware 1:8ea78dce6b36 379 TEST_ASSERT(recv_size == size);
philware 1:8ea78dce6b36 380 y = memcmp(send_data, recv_data, size);
philware 1:8ea78dce6b36 381 if (y != 0) {
philware 1:8ea78dce6b36 382 tr_debug("Sent %d, |%*.*s|", size, size, size, send_data);
philware 1:8ea78dce6b36 383 tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data);
philware 1:8ea78dce6b36 384 TEST_ASSERT(false);
philware 1:8ea78dce6b36 385 }
philware 1:8ea78dce6b36 386 timer.stop();
philware 1:8ea78dce6b36 387
philware 1:8ea78dce6b36 388 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 389
philware 1:8ea78dce6b36 390 free (recv_data);
philware 1:8ea78dce6b36 391 }
philware 1:8ea78dce6b36 392 #endif
philware 1:8ea78dce6b36 393
philware 1:8ea78dce6b36 394 // Get NTP time from a socket
philware 1:8ea78dce6b36 395 static void do_ntp_sock (UDPSocket *sock, SocketAddress ntp_address)
philware 1:8ea78dce6b36 396 {
philware 1:8ea78dce6b36 397 char ntp_values[48] = { 0 };
philware 1:8ea78dce6b36 398 time_t timestamp = 0;
philware 1:8ea78dce6b36 399 struct tm *localTime;
philware 1:8ea78dce6b36 400 char timeString[25];
philware 1:8ea78dce6b36 401 time_t TIME1970 = 2208988800U;
philware 1:8ea78dce6b36 402 int len;
philware 1:8ea78dce6b36 403 bool comms_done = false;
philware 1:8ea78dce6b36 404
philware 1:8ea78dce6b36 405 ntp_values[0] = '\x1b';
philware 1:8ea78dce6b36 406
philware 1:8ea78dce6b36 407 // Retry this a few times, don't want to fail due to a flaky link
philware 1:8ea78dce6b36 408 for (unsigned int x = 0; !comms_done && (x < NUM_UDP_RETRIES); x++) {
philware 1:8ea78dce6b36 409 sock->sendto(ntp_address, (void*) ntp_values, sizeof(ntp_values));
philware 1:8ea78dce6b36 410 len = sock->recvfrom(&ntp_address, (void*) ntp_values, sizeof(ntp_values));
philware 1:8ea78dce6b36 411 if (len > 0) {
philware 1:8ea78dce6b36 412 comms_done = true;
philware 1:8ea78dce6b36 413 }
philware 1:8ea78dce6b36 414 }
philware 1:8ea78dce6b36 415 TEST_ASSERT (comms_done);
philware 1:8ea78dce6b36 416
philware 1:8ea78dce6b36 417 tr_debug("UDP: %d byte(s) returned by NTP server.", len);
philware 1:8ea78dce6b36 418 if (len >= 43) {
philware 1:8ea78dce6b36 419 timestamp |= ((int) *(ntp_values + 40)) << 24;
philware 1:8ea78dce6b36 420 timestamp |= ((int) *(ntp_values + 41)) << 16;
philware 1:8ea78dce6b36 421 timestamp |= ((int) *(ntp_values + 42)) << 8;
philware 1:8ea78dce6b36 422 timestamp |= ((int) *(ntp_values + 43));
philware 1:8ea78dce6b36 423 timestamp -= TIME1970;
philware 1:8ea78dce6b36 424 srand (timestamp);
philware 1:8ea78dce6b36 425 tr_debug("srand() called");
philware 1:8ea78dce6b36 426 localTime = localtime(&timestamp);
philware 1:8ea78dce6b36 427 if (localTime) {
philware 1:8ea78dce6b36 428 if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
philware 1:8ea78dce6b36 429 printf("NTP timestamp is %s.\n", timeString);
philware 1:8ea78dce6b36 430 }
philware 1:8ea78dce6b36 431 }
philware 1:8ea78dce6b36 432 }
philware 1:8ea78dce6b36 433 }
philware 1:8ea78dce6b36 434
philware 1:8ea78dce6b36 435 // Get NTP time
philware 1:8ea78dce6b36 436 static void do_ntp(UbloxATCellularInterfaceN2xx *interface)
philware 1:8ea78dce6b36 437 {
philware 1:8ea78dce6b36 438 UDPSocket sock;
philware 1:8ea78dce6b36 439 SocketAddress host_address;
philware 1:8ea78dce6b36 440
philware 1:8ea78dce6b36 441 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 442
philware 1:8ea78dce6b36 443 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 444 host_address.set_port(MBED_CONF_APP_NTP_PORT);
philware 1:8ea78dce6b36 445
philware 1:8ea78dce6b36 446 tr_debug("UDP: NIST server %s address: %s on port %d.", MBED_CONF_APP_NTP_SERVER,
philware 1:8ea78dce6b36 447 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 448
philware 1:8ea78dce6b36 449 sock.set_timeout(10000);
philware 1:8ea78dce6b36 450
philware 1:8ea78dce6b36 451 do_ntp_sock(&sock, host_address);
philware 1:8ea78dce6b36 452
philware 1:8ea78dce6b36 453 sock.close();
philware 1:8ea78dce6b36 454 }
philware 1:8ea78dce6b36 455
philware 1:8ea78dce6b36 456 // Use a connection, checking that it is good
philware 1:8ea78dce6b36 457 static void use_connection(UbloxATCellularInterfaceN2xx *interface)
philware 1:8ea78dce6b36 458 {
philware 1:8ea78dce6b36 459 const char * ip_address = interface->get_ip_address();
philware 1:8ea78dce6b36 460 const char * net_mask = interface->get_netmask();
philware 1:8ea78dce6b36 461 const char * gateway = interface->get_gateway();
philware 1:8ea78dce6b36 462
philware 1:8ea78dce6b36 463 TEST_ASSERT(interface->is_connected());
philware 1:8ea78dce6b36 464
philware 1:8ea78dce6b36 465 TEST_ASSERT(ip_address != NULL);
philware 1:8ea78dce6b36 466 tr_debug ("IP address %s.", ip_address);
philware 1:8ea78dce6b36 467 TEST_ASSERT(net_mask == NULL);
philware 1:8ea78dce6b36 468 tr_debug ("Net mask %s.", net_mask);
philware 1:8ea78dce6b36 469 TEST_ASSERT(gateway != NULL);
philware 1:8ea78dce6b36 470 tr_debug ("Gateway %s.", gateway);
philware 1:8ea78dce6b36 471
philware 1:8ea78dce6b36 472 do_ntp(interface);
philware 1:8ea78dce6b36 473 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 474 }
philware 1:8ea78dce6b36 475
philware 1:8ea78dce6b36 476 // Drop a connection and check that it has dropped
philware 1:8ea78dce6b36 477 static void drop_connection(UbloxATCellularInterfaceN2xx *interface)
philware 1:8ea78dce6b36 478 {
philware 1:8ea78dce6b36 479 TEST_ASSERT(interface->disconnect() == 0);
philware 1:8ea78dce6b36 480 TEST_ASSERT(connection_has_gone_down);
philware 1:8ea78dce6b36 481 connection_has_gone_down = false;
philware 1:8ea78dce6b36 482 TEST_ASSERT(!interface->is_connected());
philware 1:8ea78dce6b36 483 }
philware 1:8ea78dce6b36 484
philware 1:8ea78dce6b36 485 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 486 // TESTS
philware 1:8ea78dce6b36 487 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 488
philware 1:8ea78dce6b36 489 // Call srand() using the NTP server
philware 1:8ea78dce6b36 490 void test_set_randomise() {
philware 1:8ea78dce6b36 491 UDPSocket sock;
philware 1:8ea78dce6b36 492 SocketAddress host_address;
philware 1:8ea78dce6b36 493
philware 1:8ea78dce6b36 494 tr_debug("Starting connect()...");
philware 1:8ea78dce6b36 495 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 496 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 497 do_ntp(interface);
philware 1:8ea78dce6b36 498 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 499 drop_connection(interface);
philware 1:8ea78dce6b36 500 }
philware 1:8ea78dce6b36 501
philware 1:8ea78dce6b36 502 #ifdef MBED_CONF_APP_ECHO_SERVER
philware 1:8ea78dce6b36 503
philware 1:8ea78dce6b36 504 // Test UDP data exchange
philware 1:8ea78dce6b36 505 void test_udp_echo() {
philware 1:8ea78dce6b36 506 UDPSocket sock;
philware 1:8ea78dce6b36 507 SocketAddress host_address;
philware 1:8ea78dce6b36 508 SocketAddress local_address;
philware 1:8ea78dce6b36 509 int x;
philware 1:8ea78dce6b36 510 int size;
philware 1:8ea78dce6b36 511
philware 1:8ea78dce6b36 512 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 513 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 514
philware 1:8ea78dce6b36 515 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 516 host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
philware 1:8ea78dce6b36 517
philware 1:8ea78dce6b36 518 tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 519 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 520
philware 1:8ea78dce6b36 521 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 522
philware 1:8ea78dce6b36 523 sock.set_timeout(10000);
philware 1:8ea78dce6b36 524
philware 1:8ea78dce6b36 525 int sizes[] = { 1, 2, 5, 150, 160, 170, 180, 190, 200, 250 };
philware 1:8ea78dce6b36 526
philware 1:8ea78dce6b36 527 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 528 //do_udp_echo(&sock, &host_address, 1);
philware 1:8ea78dce6b36 529 //do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 530 for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 531 do_udp_echo(&sock, &host_address, sizes[x]);
philware 1:8ea78dce6b36 532 }
philware 1:8ea78dce6b36 533
philware 1:8ea78dce6b36 534 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 535 // do_udp_echo(&sock, &host_address, 1);
philware 1:8ea78dce6b36 536 // do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 537 // for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 538 // size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1;
philware 1:8ea78dce6b36 539 // size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 540 // do_udp_echo(&sock, &host_address, size);
philware 1:8ea78dce6b36 541 // }
philware 1:8ea78dce6b36 542
philware 1:8ea78dce6b36 543 sock.close();
philware 1:8ea78dce6b36 544
philware 1:8ea78dce6b36 545 drop_connection(interface);
philware 1:8ea78dce6b36 546 tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.",
philware 1:8ea78dce6b36 547 x, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 548 }
philware 1:8ea78dce6b36 549
philware 1:8ea78dce6b36 550 // Test many different sizes of UDP data arriving at once
philware 1:8ea78dce6b36 551 void test_udp_echo_recv_sizes() {
philware 1:8ea78dce6b36 552 UDPSocket sock;
philware 1:8ea78dce6b36 553 SocketAddress host_address;
philware 1:8ea78dce6b36 554 int x, y, z;
philware 1:8ea78dce6b36 555 int size;
philware 1:8ea78dce6b36 556 int tries = 0;
philware 1:8ea78dce6b36 557 unsigned int offset;
philware 1:8ea78dce6b36 558 char * recv_data;
philware 1:8ea78dce6b36 559 bool packetLoss;
philware 1:8ea78dce6b36 560 bool sendSuccess;
philware 1:8ea78dce6b36 561 Timer timer;
philware 1:8ea78dce6b36 562
philware 1:8ea78dce6b36 563 interface->deinit();
philware 1:8ea78dce6b36 564 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 565 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 566
philware 1:8ea78dce6b36 567 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 568 host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
philware 1:8ea78dce6b36 569
philware 1:8ea78dce6b36 570 tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 571 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 572
philware 1:8ea78dce6b36 573 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 574
philware 1:8ea78dce6b36 575 do {
philware 1:8ea78dce6b36 576 tr_debug("--- UDP packet size test, test try %d, flushing input buffers", tries + 1);
philware 1:8ea78dce6b36 577 // First of all, clear any junk from the socket
philware 1:8ea78dce6b36 578 sock.set_timeout(1000);
philware 1:8ea78dce6b36 579 recv_data = (char *) malloc (MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 580 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 581 while (sock.recvfrom(&host_address, (void *) recv_data, MBED_CONF_APP_UDP_MAX_PACKET_SIZE) > 0) {
philware 1:8ea78dce6b36 582 // Throw it away
philware 1:8ea78dce6b36 583 }
philware 1:8ea78dce6b36 584 free (recv_data);
philware 1:8ea78dce6b36 585
philware 1:8ea78dce6b36 586 sock.set_timeout(10000);
philware 1:8ea78dce6b36 587
philware 1:8ea78dce6b36 588 // Throw random sized UDP packets up...
philware 1:8ea78dce6b36 589 x = 0;
philware 1:8ea78dce6b36 590 offset = 0;
philware 1:8ea78dce6b36 591 while (offset < sizeof (send_data)) {
philware 1:8ea78dce6b36 592 size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1;
philware 1:8ea78dce6b36 593 size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2);
philware 1:8ea78dce6b36 594 if (offset + size > sizeof (send_data)) {
philware 1:8ea78dce6b36 595 size = sizeof (send_data) - offset;
philware 1:8ea78dce6b36 596 }
philware 1:8ea78dce6b36 597 sendSuccess = false;
philware 1:8ea78dce6b36 598 for (y = 0; !sendSuccess && (y < NUM_UDP_RETRIES); y++) {
philware 1:8ea78dce6b36 599 tr_debug("Sending UDP packet number %d, size %d byte(s), send try %d.", x + 1, size, y + 1);
philware 1:8ea78dce6b36 600 if (sock.sendto(host_address, (void *) (send_data + offset), size) == size) {
philware 1:8ea78dce6b36 601 sendSuccess = true;
philware 1:8ea78dce6b36 602 offset += size;
philware 1:8ea78dce6b36 603 }
philware 1:8ea78dce6b36 604 }
philware 1:8ea78dce6b36 605 TEST_ASSERT(sendSuccess);
philware 1:8ea78dce6b36 606 x++;
philware 1:8ea78dce6b36 607 }
philware 1:8ea78dce6b36 608 tr_debug("--- All UDP packets sent");
philware 1:8ea78dce6b36 609
philware 1:8ea78dce6b36 610 // ...and capture them all again afterwards
philware 1:8ea78dce6b36 611 recv_data = (char *) malloc (sizeof (send_data));
philware 1:8ea78dce6b36 612 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 613 memset (recv_data, 0, sizeof (send_data));
philware 1:8ea78dce6b36 614 size = 0;
philware 1:8ea78dce6b36 615 y = 0;
philware 1:8ea78dce6b36 616 packetLoss = false;
philware 1:8ea78dce6b36 617 timer.start();
philware 1:8ea78dce6b36 618 while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 10000)) {
philware 1:8ea78dce6b36 619 y = sock.recvfrom(&host_address, (void *) (recv_data + size), sizeof (send_data) - size);
philware 1:8ea78dce6b36 620 if (y > 0) {
philware 1:8ea78dce6b36 621 size += y;
philware 1:8ea78dce6b36 622 }
philware 1:8ea78dce6b36 623 }
philware 1:8ea78dce6b36 624 timer.stop();
philware 1:8ea78dce6b36 625 timer.reset();
philware 1:8ea78dce6b36 626 tr_debug( "--- Either received everything back or timed out waiting");
philware 1:8ea78dce6b36 627
philware 1:8ea78dce6b36 628 // Check that we reassembled everything correctly
philware 1:8ea78dce6b36 629 if (size == sizeof (send_data)) {
philware 1:8ea78dce6b36 630 for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) {
philware 1:8ea78dce6b36 631 }
philware 1:8ea78dce6b36 632 if (x != sizeof (send_data)) {
philware 1:8ea78dce6b36 633 y = x - 5;
philware 1:8ea78dce6b36 634 if (y < 0) {
philware 1:8ea78dce6b36 635 y = 0;
philware 1:8ea78dce6b36 636 }
philware 1:8ea78dce6b36 637 z = 10;
philware 1:8ea78dce6b36 638 if (y + z > (int) sizeof (send_data)) {
philware 1:8ea78dce6b36 639 z = sizeof(send_data) - y;
philware 1:8ea78dce6b36 640 }
philware 1:8ea78dce6b36 641 tr_debug(" --- Difference at character %d (send \"%*.*s\", recv \"%*.*s\")",
philware 1:8ea78dce6b36 642 x + 1, z, z, send_data + y, z, z, recv_data + y);
philware 1:8ea78dce6b36 643 packetLoss = true;
philware 1:8ea78dce6b36 644 }
philware 1:8ea78dce6b36 645 } else {
philware 1:8ea78dce6b36 646 tr_debug(" --- %d bytes missing (%d bytes received when %d were expected))",
philware 1:8ea78dce6b36 647 sizeof (send_data) - size, size, sizeof (send_data));
philware 1:8ea78dce6b36 648 packetLoss = true;
philware 1:8ea78dce6b36 649 }
philware 1:8ea78dce6b36 650 free (recv_data);
philware 1:8ea78dce6b36 651 tries++;
philware 1:8ea78dce6b36 652 } while (packetLoss && (tries < NUM_UDP_RETRIES));
philware 1:8ea78dce6b36 653
philware 1:8ea78dce6b36 654 TEST_ASSERT(!packetLoss);
philware 1:8ea78dce6b36 655 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 656 sock.close();
philware 1:8ea78dce6b36 657 drop_connection(interface);
philware 1:8ea78dce6b36 658 }
philware 1:8ea78dce6b36 659
philware 1:8ea78dce6b36 660 // Test UDP data exchange via the asynchronous sigio() mechanism
philware 1:8ea78dce6b36 661 void test_udp_echo_async() {
philware 1:8ea78dce6b36 662 UDPSocket sock;
philware 1:8ea78dce6b36 663 SocketAddress host_address;
philware 1:8ea78dce6b36 664 SocketAddress local_address;
philware 1:8ea78dce6b36 665 bool callback_triggered = false;
philware 1:8ea78dce6b36 666 int x;
philware 1:8ea78dce6b36 667 int size;
philware 1:8ea78dce6b36 668
philware 1:8ea78dce6b36 669 interface->deinit();
philware 1:8ea78dce6b36 670 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 671 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 672
philware 1:8ea78dce6b36 673 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 674 host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
philware 1:8ea78dce6b36 675
philware 1:8ea78dce6b36 676 tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 677 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 678
philware 1:8ea78dce6b36 679 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 680
philware 1:8ea78dce6b36 681 // Set up the async callback and set the timeout to zero
philware 1:8ea78dce6b36 682 sock.sigio(callback(async_cb, &callback_triggered));
philware 1:8ea78dce6b36 683 sock.set_timeout(0);
philware 1:8ea78dce6b36 684
philware 1:8ea78dce6b36 685 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 686 // and this time allow the UDP packets to be fragmented
philware 1:8ea78dce6b36 687 do_udp_echo_async(&sock, &host_address, 1, &callback_triggered);
philware 1:8ea78dce6b36 688 do_udp_echo_async(&sock, &host_address, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE,
philware 1:8ea78dce6b36 689 &callback_triggered);
philware 1:8ea78dce6b36 690 for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 691 size = (rand() % MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE) + 1;
philware 1:8ea78dce6b36 692 size = fix(size, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE);
philware 1:8ea78dce6b36 693 do_udp_echo_async(&sock, &host_address, size, &callback_triggered);
philware 1:8ea78dce6b36 694 }
philware 1:8ea78dce6b36 695
philware 1:8ea78dce6b36 696 sock.close();
philware 1:8ea78dce6b36 697
philware 1:8ea78dce6b36 698 drop_connection(interface);
philware 1:8ea78dce6b36 699
philware 1:8ea78dce6b36 700 tr_debug("%d UDP packets of size up to %d byte(s) echoed asynchronously and successfully.",
philware 1:8ea78dce6b36 701 x, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE);
philware 1:8ea78dce6b36 702 }
philware 1:8ea78dce6b36 703
philware 1:8ea78dce6b36 704 // Test many different sizes of TCP data arriving at once
philware 1:8ea78dce6b36 705 void test_tcp_echo_recv_sizes() {
philware 1:8ea78dce6b36 706 TCPSocket sock;
philware 1:8ea78dce6b36 707 SocketAddress host_address;
philware 1:8ea78dce6b36 708 int x, y, z;
philware 1:8ea78dce6b36 709 int size;
philware 1:8ea78dce6b36 710 unsigned int offset;
philware 1:8ea78dce6b36 711 char * recv_data;
philware 1:8ea78dce6b36 712 Timer timer;
philware 1:8ea78dce6b36 713
philware 1:8ea78dce6b36 714 interface->deinit();
philware 1:8ea78dce6b36 715 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 716 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 717
philware 1:8ea78dce6b36 718 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 719 host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);
philware 1:8ea78dce6b36 720
philware 1:8ea78dce6b36 721 tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 722 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 723
philware 1:8ea78dce6b36 724 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 725
philware 1:8ea78dce6b36 726 TEST_ASSERT(sock.connect(host_address) == 0);
philware 1:8ea78dce6b36 727
philware 1:8ea78dce6b36 728 sock.set_timeout(10000);
philware 1:8ea78dce6b36 729
philware 1:8ea78dce6b36 730 // Throw random sized TCP packets up...
philware 1:8ea78dce6b36 731 x = 0;
philware 1:8ea78dce6b36 732 offset = 0;
philware 1:8ea78dce6b36 733 while (offset < sizeof (send_data)) {
philware 1:8ea78dce6b36 734 size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1;
philware 1:8ea78dce6b36 735 size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2);
philware 1:8ea78dce6b36 736 if (offset + size > sizeof (send_data)) {
philware 1:8ea78dce6b36 737 size = sizeof (send_data) - offset;
philware 1:8ea78dce6b36 738 }
philware 1:8ea78dce6b36 739 tr_debug("Sending TCP packet number %d, size %d byte(s).", x + 1, size);
philware 1:8ea78dce6b36 740 TEST_ASSERT(sendAll(&sock, (send_data + offset), size) == size);
philware 1:8ea78dce6b36 741 offset += size;
philware 1:8ea78dce6b36 742 x++;
philware 1:8ea78dce6b36 743 }
philware 1:8ea78dce6b36 744
philware 1:8ea78dce6b36 745 // ...and capture them all again afterwards
philware 1:8ea78dce6b36 746 recv_data = (char *) malloc (sizeof (send_data));
philware 1:8ea78dce6b36 747 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 748 memset (recv_data, 0, sizeof (send_data));
philware 1:8ea78dce6b36 749 size = 0;
philware 1:8ea78dce6b36 750 x = 0;
philware 1:8ea78dce6b36 751 timer.start();
philware 1:8ea78dce6b36 752 while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 30000)) {
philware 1:8ea78dce6b36 753 y = sock.recv((void *) (recv_data + size), sizeof (send_data) - size);
philware 1:8ea78dce6b36 754 tr_debug("Received TCP packet number %d, size %d byte(s).", x, y);
philware 1:8ea78dce6b36 755 size += y;
philware 1:8ea78dce6b36 756 x++;
philware 1:8ea78dce6b36 757 }
philware 1:8ea78dce6b36 758 timer.stop();
philware 1:8ea78dce6b36 759 timer.reset();
philware 1:8ea78dce6b36 760
philware 1:8ea78dce6b36 761 // Check that we reassembled everything correctly
philware 1:8ea78dce6b36 762 for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) {
philware 1:8ea78dce6b36 763 }
philware 1:8ea78dce6b36 764 if (x != sizeof (send_data)) {
philware 1:8ea78dce6b36 765 y = x - 5;
philware 1:8ea78dce6b36 766 if (y < 0) {
philware 1:8ea78dce6b36 767 y = 0;
philware 1:8ea78dce6b36 768 }
philware 1:8ea78dce6b36 769 z = 10;
philware 1:8ea78dce6b36 770 if (y + z > (int) sizeof (send_data)) {
philware 1:8ea78dce6b36 771 z = sizeof(send_data) - y;
philware 1:8ea78dce6b36 772 }
philware 1:8ea78dce6b36 773 tr_debug("Difference at character %d (send \"%*.*s\", recv \"%*.*s\")",
philware 1:8ea78dce6b36 774 x + 1, z, z, send_data + y, z, z, recv_data + y);
philware 1:8ea78dce6b36 775 TEST_ASSERT(false);
philware 1:8ea78dce6b36 776 }
philware 1:8ea78dce6b36 777 free (recv_data);
philware 1:8ea78dce6b36 778
philware 1:8ea78dce6b36 779 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 780 sock.close();
philware 1:8ea78dce6b36 781 drop_connection(interface);
philware 1:8ea78dce6b36 782 }
philware 1:8ea78dce6b36 783
philware 1:8ea78dce6b36 784 // Test TCP data exchange via the asynchronous sigio() mechanism
philware 1:8ea78dce6b36 785 void test_tcp_echo_async() {
philware 1:8ea78dce6b36 786 TCPSocket sock;
philware 1:8ea78dce6b36 787 SocketAddress host_address;
philware 1:8ea78dce6b36 788 bool callback_triggered = false;
philware 1:8ea78dce6b36 789 int x;
philware 1:8ea78dce6b36 790 int size;
philware 1:8ea78dce6b36 791
philware 1:8ea78dce6b36 792 interface->deinit();
philware 1:8ea78dce6b36 793 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 794 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 795
philware 1:8ea78dce6b36 796 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 797 host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);
philware 1:8ea78dce6b36 798
philware 1:8ea78dce6b36 799 tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 800 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 801
philware 1:8ea78dce6b36 802 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 803
philware 1:8ea78dce6b36 804 // Set up the async callback and set the timeout to zero
philware 1:8ea78dce6b36 805 sock.sigio(callback(async_cb, &callback_triggered));
philware 1:8ea78dce6b36 806 sock.set_timeout(0);
philware 1:8ea78dce6b36 807
philware 1:8ea78dce6b36 808 TEST_ASSERT(sock.connect(host_address) == 0);
philware 1:8ea78dce6b36 809 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 810 do_tcp_echo_async(&sock, 1, &callback_triggered);
philware 1:8ea78dce6b36 811 do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, &callback_triggered);
philware 1:8ea78dce6b36 812 for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 813 size = (rand() % MBED_CONF_APP_TCP_MAX_PACKET_SIZE) + 1;
philware 1:8ea78dce6b36 814 size = fix(size, MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 815 do_tcp_echo_async(&sock, size, &callback_triggered);
philware 1:8ea78dce6b36 816 }
philware 1:8ea78dce6b36 817
philware 1:8ea78dce6b36 818 sock.close();
philware 1:8ea78dce6b36 819
philware 1:8ea78dce6b36 820 drop_connection(interface);
philware 1:8ea78dce6b36 821
philware 1:8ea78dce6b36 822 tr_debug("%d TCP packets of size up to %d byte(s) echoed asynchronously and successfully.",
philware 1:8ea78dce6b36 823 x, MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 824 }
philware 1:8ea78dce6b36 825 #endif
philware 1:8ea78dce6b36 826
philware 1:8ea78dce6b36 827 // Allocate max sockets
philware 1:8ea78dce6b36 828 void test_max_sockets() {
philware 1:8ea78dce6b36 829 UDPSocket sock[MAX_NUM_SOCKETS];
philware 1:8ea78dce6b36 830 UDPSocket sockNone;
philware 1:8ea78dce6b36 831 SocketAddress host_address;
philware 1:8ea78dce6b36 832
philware 1:8ea78dce6b36 833 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 834 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 835
philware 1:8ea78dce6b36 836 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 837 host_address.set_port(MBED_CONF_APP_NTP_PORT);
philware 1:8ea78dce6b36 838
philware 1:8ea78dce6b36 839 // Open the first socket and use it
philware 1:8ea78dce6b36 840 interface->set_LocalListenPort(10000);
philware 1:8ea78dce6b36 841 TEST_ASSERT(sock[0].open(interface) == 0)
philware 1:8ea78dce6b36 842 sock[0].set_timeout(10000);
philware 1:8ea78dce6b36 843 do_ntp_sock(&sock[0], host_address);
philware 1:8ea78dce6b36 844
philware 1:8ea78dce6b36 845 // Check that we stop being able to get sockets at the max number
philware 1:8ea78dce6b36 846 for (int x = 1; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) {
philware 1:8ea78dce6b36 847 interface->set_LocalListenPort(10000+x);
philware 1:8ea78dce6b36 848 TEST_ASSERT(sock[x].open(interface) == 0)
philware 1:8ea78dce6b36 849 }
philware 1:8ea78dce6b36 850 TEST_ASSERT(sockNone.open(interface) < 0);
philware 1:8ea78dce6b36 851
philware 1:8ea78dce6b36 852 // Now use the last one
philware 1:8ea78dce6b36 853 sock[sizeof (sock) / sizeof (sock[0]) - 1].set_timeout(10000);
philware 1:8ea78dce6b36 854 do_ntp_sock(&sock[sizeof (sock) / sizeof (sock[0]) - 1], host_address);
philware 1:8ea78dce6b36 855
philware 1:8ea78dce6b36 856 // Close all of the sockets
philware 1:8ea78dce6b36 857 for (int x = 0; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) {
philware 1:8ea78dce6b36 858 TEST_ASSERT(sock[x].close() == 0);
philware 1:8ea78dce6b36 859 }
philware 1:8ea78dce6b36 860
philware 1:8ea78dce6b36 861 drop_connection(interface);
philware 1:8ea78dce6b36 862 }
philware 1:8ea78dce6b36 863
philware 1:8ea78dce6b36 864 // Connect with credentials included in the connect request
philware 1:8ea78dce6b36 865 void test_connect_credentials() {
philware 1:8ea78dce6b36 866
philware 1:8ea78dce6b36 867 interface->deinit();
philware 1:8ea78dce6b36 868 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 869 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 870 use_connection(interface);
philware 1:8ea78dce6b36 871 drop_connection(interface);
philware 1:8ea78dce6b36 872 }
philware 1:8ea78dce6b36 873
philware 1:8ea78dce6b36 874 // Test with credentials preset
philware 1:8ea78dce6b36 875 void test_connect_preset_credentials() {
philware 1:8ea78dce6b36 876
philware 1:8ea78dce6b36 877 interface->deinit();
philware 1:8ea78dce6b36 878 TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN));
philware 1:8ea78dce6b36 879 interface->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 880 MBED_CONF_APP_PASSWORD);
philware 1:8ea78dce6b36 881 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN) == 0);
philware 1:8ea78dce6b36 882 use_connection(interface);
philware 1:8ea78dce6b36 883 drop_connection(interface);
philware 1:8ea78dce6b36 884 }
philware 1:8ea78dce6b36 885
philware 1:8ea78dce6b36 886 // Test adding and using a SIM pin, then removing it, using the pending
philware 1:8ea78dce6b36 887 // mechanism where the change doesn't occur until connect() is called
philware 1:8ea78dce6b36 888 void test_check_sim_pin_pending() {
philware 1:8ea78dce6b36 889
philware 1:8ea78dce6b36 890 interface->deinit();
philware 1:8ea78dce6b36 891
philware 1:8ea78dce6b36 892 // Enable PIN checking (which will use the current PIN)
philware 1:8ea78dce6b36 893 // and also flag that the PIN should be changed to MBED_CONF_APP_ALT_PIN,
philware 1:8ea78dce6b36 894 // then try connecting
philware 1:8ea78dce6b36 895 interface->set_sim_pin_check(true);
philware 1:8ea78dce6b36 896 interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN);
philware 1:8ea78dce6b36 897 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 898 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 899 use_connection(interface);
philware 1:8ea78dce6b36 900 drop_connection(interface);
philware 1:8ea78dce6b36 901 interface->deinit();
philware 1:8ea78dce6b36 902
philware 1:8ea78dce6b36 903 // Now change the PIN back to what it was before
philware 1:8ea78dce6b36 904 interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 905 TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 906 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 907 use_connection(interface);
philware 1:8ea78dce6b36 908 drop_connection(interface);
philware 1:8ea78dce6b36 909 interface->deinit();
philware 1:8ea78dce6b36 910
philware 1:8ea78dce6b36 911 // Check that it was changed back, and this time
philware 1:8ea78dce6b36 912 // use the other way of entering the PIN
philware 1:8ea78dce6b36 913 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 914 TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 915 MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 916 use_connection(interface);
philware 1:8ea78dce6b36 917 drop_connection(interface);
philware 1:8ea78dce6b36 918 interface->deinit();
philware 1:8ea78dce6b36 919
philware 1:8ea78dce6b36 920 // Remove PIN checking again and check that it no
philware 1:8ea78dce6b36 921 // longer matters what the PIN is
philware 1:8ea78dce6b36 922 interface->set_sim_pin_check(false);
philware 1:8ea78dce6b36 923 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 924 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 925 use_connection(interface);
philware 1:8ea78dce6b36 926 drop_connection(interface);
philware 1:8ea78dce6b36 927 interface->deinit();
philware 1:8ea78dce6b36 928 TEST_ASSERT(interface->init(NULL));
philware 1:8ea78dce6b36 929 TEST_ASSERT(interface->connect(MBED_CONF_APP_INCORRECT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 930 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 931 use_connection(interface);
philware 1:8ea78dce6b36 932 drop_connection(interface);
philware 1:8ea78dce6b36 933
philware 1:8ea78dce6b36 934 // Put the SIM pin back to the correct value for any subsequent tests
philware 1:8ea78dce6b36 935 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 936 }
philware 1:8ea78dce6b36 937
philware 1:8ea78dce6b36 938 // Test adding and using a SIM pin, then removing it, using the immediate
philware 1:8ea78dce6b36 939 // mechanism
philware 1:8ea78dce6b36 940 void test_check_sim_pin_immediate() {
philware 1:8ea78dce6b36 941
philware 1:8ea78dce6b36 942 interface->deinit();
philware 1:8ea78dce6b36 943 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 944
philware 1:8ea78dce6b36 945 // Enable PIN checking (which will use the current PIN), change
philware 1:8ea78dce6b36 946 // the PIN to MBED_CONF_APP_ALT_PIN, then try connecting after powering on and
philware 1:8ea78dce6b36 947 // off the modem
philware 1:8ea78dce6b36 948 interface->set_sim_pin_check(true, true, MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 949 interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN, true);
philware 1:8ea78dce6b36 950 interface->deinit();
philware 1:8ea78dce6b36 951 TEST_ASSERT(interface->init(NULL));
philware 1:8ea78dce6b36 952 TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 953 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 954 use_connection(interface);
philware 1:8ea78dce6b36 955 drop_connection(interface);
philware 1:8ea78dce6b36 956
philware 1:8ea78dce6b36 957 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 958
philware 1:8ea78dce6b36 959 // Now change the PIN back to what it was before
philware 1:8ea78dce6b36 960 interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN, true);
philware 1:8ea78dce6b36 961 interface->deinit();
philware 1:8ea78dce6b36 962 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 963 TEST_ASSERT(interface->init(NULL));
philware 1:8ea78dce6b36 964 TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 965 MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 966 use_connection(interface);
philware 1:8ea78dce6b36 967 drop_connection(interface);
philware 1:8ea78dce6b36 968
philware 1:8ea78dce6b36 969 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 970
philware 1:8ea78dce6b36 971 // Remove PIN checking again and check that it no
philware 1:8ea78dce6b36 972 // longer matters what the PIN is
philware 1:8ea78dce6b36 973 interface->set_sim_pin_check(false, true);
philware 1:8ea78dce6b36 974 interface->deinit();
philware 1:8ea78dce6b36 975 TEST_ASSERT(interface->init(MBED_CONF_APP_INCORRECT_PIN));
philware 1:8ea78dce6b36 976 TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 977 MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 978 use_connection(interface);
philware 1:8ea78dce6b36 979 drop_connection(interface);
philware 1:8ea78dce6b36 980
philware 1:8ea78dce6b36 981 // Put the SIM pin back to the correct value for any subsequent tests
philware 1:8ea78dce6b36 982 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 983 }
philware 1:8ea78dce6b36 984
philware 1:8ea78dce6b36 985 // Test being able to connect with a local instance of the interface
philware 1:8ea78dce6b36 986 // NOTE: since this local instance will fiddle with bits of HW that the
philware 1:8ea78dce6b36 987 // static instance thought it owned, the static instance will no longer
philware 1:8ea78dce6b36 988 // work afterwards, hence this must be run as the last test in the list
philware 1:8ea78dce6b36 989 void test_connect_local_instance_last_test() {
philware 1:8ea78dce6b36 990
philware 1:8ea78dce6b36 991 UbloxATCellularInterfaceN2xx *pLocalInterface = NULL;
philware 1:8ea78dce6b36 992
philware 1:8ea78dce6b36 993 pLocalInterface = new UbloxATCellularInterfaceN2xx(MDMTXD, MDMRXD,
philware 1:8ea78dce6b36 994 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
philware 1:8ea78dce6b36 995 MBED_CONF_APP_DEBUG_ON);
philware 1:8ea78dce6b36 996 pLocalInterface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 997
philware 1:8ea78dce6b36 998 TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 999 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 1000 use_connection(pLocalInterface);
philware 1:8ea78dce6b36 1001 drop_connection(pLocalInterface);
philware 1:8ea78dce6b36 1002 delete pLocalInterface;
philware 1:8ea78dce6b36 1003
philware 1:8ea78dce6b36 1004 pLocalInterface = new UbloxATCellularInterfaceN2xx(MDMTXD, MDMRXD,
philware 1:8ea78dce6b36 1005 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
philware 1:8ea78dce6b36 1006 MBED_CONF_APP_DEBUG_ON);
philware 1:8ea78dce6b36 1007 pLocalInterface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 1008
philware 1:8ea78dce6b36 1009 TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 1010 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 1011 use_connection(pLocalInterface);
philware 1:8ea78dce6b36 1012 drop_connection(pLocalInterface);
philware 1:8ea78dce6b36 1013 delete pLocalInterface;
philware 1:8ea78dce6b36 1014 }
philware 1:8ea78dce6b36 1015
philware 1:8ea78dce6b36 1016 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1017 // TEST ENVIRONMENT
philware 1:8ea78dce6b36 1018 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1019
philware 1:8ea78dce6b36 1020 // Setup the test environment
philware 1:8ea78dce6b36 1021 utest::v1::status_t test_setup(const size_t number_of_cases) {
philware 1:8ea78dce6b36 1022 // Setup Greentea with a timeout
philware 1:8ea78dce6b36 1023 GREENTEA_SETUP(960, "default_auto");
philware 1:8ea78dce6b36 1024 return verbose_test_setup_handler(number_of_cases);
philware 1:8ea78dce6b36 1025 }
philware 1:8ea78dce6b36 1026
philware 1:8ea78dce6b36 1027 // IMPORTANT!!! if you make a change to the tests here you should
philware 1:8ea78dce6b36 1028 // check whether the same change should be made to the tests under
philware 1:8ea78dce6b36 1029 // the PPP interface.
philware 1:8ea78dce6b36 1030
philware 1:8ea78dce6b36 1031 // Test cases
philware 1:8ea78dce6b36 1032 Case cases[] = {
philware 1:8ea78dce6b36 1033 Case("Set randomise", test_set_randomise),
philware 1:8ea78dce6b36 1034 #ifdef MBED_CONF_APP_ECHO_SERVER
philware 1:8ea78dce6b36 1035 Case("UDP echo test", test_udp_echo),
philware 1:8ea78dce6b36 1036 # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
philware 1:8ea78dce6b36 1037 Case("UDP recv sizes", test_udp_echo_recv_sizes),
philware 1:8ea78dce6b36 1038 # endif
philware 1:8ea78dce6b36 1039 Case("UDP async echo test", test_udp_echo_async),
philware 1:8ea78dce6b36 1040 # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
philware 1:8ea78dce6b36 1041 Case("TCP recv sizes", test_tcp_echo_recv_sizes),
philware 1:8ea78dce6b36 1042 # endif
philware 1:8ea78dce6b36 1043 Case("TCP async echo test", test_tcp_echo_async),
philware 1:8ea78dce6b36 1044 # endif
philware 1:8ea78dce6b36 1045 #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
philware 1:8ea78dce6b36 1046 Case("Alloc max sockets", test_max_sockets),
philware 1:8ea78dce6b36 1047 #endif
philware 1:8ea78dce6b36 1048 Case("Connect with credentials", test_connect_credentials),
philware 1:8ea78dce6b36 1049 Case("Connect with preset credentials", test_connect_preset_credentials),
philware 1:8ea78dce6b36 1050 #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
philware 1:8ea78dce6b36 1051 Case("Check SIM pin, pending", test_check_sim_pin_pending),
philware 1:8ea78dce6b36 1052 Case("Check SIM pin, immediate", test_check_sim_pin_immediate),
philware 1:8ea78dce6b36 1053 #endif
philware 1:8ea78dce6b36 1054 #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 for this
philware 1:8ea78dce6b36 1055 Case("Connect using local instance, must be last test", test_connect_local_instance_last_test)
philware 1:8ea78dce6b36 1056 #endif
philware 1:8ea78dce6b36 1057 };
philware 1:8ea78dce6b36 1058
philware 1:8ea78dce6b36 1059 Specification specification(test_setup, cases);
philware 1:8ea78dce6b36 1060
philware 1:8ea78dce6b36 1061 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1062 // MAIN
philware 1:8ea78dce6b36 1063 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1064
philware 1:8ea78dce6b36 1065 int main() {
philware 1:8ea78dce6b36 1066
philware 1:8ea78dce6b36 1067 #ifdef FEATURE_COMMON_PAL
philware 1:8ea78dce6b36 1068 mbed_trace_init();
philware 1:8ea78dce6b36 1069
philware 1:8ea78dce6b36 1070 mbed_trace_mutex_wait_function_set(lock);
philware 1:8ea78dce6b36 1071 mbed_trace_mutex_release_function_set(unlock);
philware 1:8ea78dce6b36 1072 #endif
philware 1:8ea78dce6b36 1073
philware 1:8ea78dce6b36 1074 interface = new UbloxATCellularInterfaceN2xx(MDMTXD, MDMRXD,
philware 1:8ea78dce6b36 1075 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
philware 1:8ea78dce6b36 1076 MBED_CONF_APP_DEBUG_ON);
philware 1:8ea78dce6b36 1077
philware 1:8ea78dce6b36 1078 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 1079
philware 1:8ea78dce6b36 1080 // Run tests
philware 1:8ea78dce6b36 1081 return !Harness::run(specification);
philware 1:8ea78dce6b36 1082 }
philware 1:8ea78dce6b36 1083
philware 1:8ea78dce6b36 1084 // End Of File
philware 1:8ea78dce6b36 1085