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:
Mon Jun 26 13:49:08 2017 +0100
Revision:
1:8ea78dce6b36
Child:
4:2bf3875a13f1
UDP Sockets over AT for SARA-N2xx modules

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 interface->SendAT("AT+NSOCR=DGRAM, 17, 10000");
philware 1:8ea78dce6b36 513 interface->SendAT("AT+NSOST=0,10.10.10.10,7,200,1122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900");
philware 1:8ea78dce6b36 514
philware 1:8ea78dce6b36 515 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 516 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 517
philware 1:8ea78dce6b36 518 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 519 host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
philware 1:8ea78dce6b36 520
philware 1:8ea78dce6b36 521 tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 522 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 523
philware 1:8ea78dce6b36 524 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 525
philware 1:8ea78dce6b36 526 sock.set_timeout(10000);
philware 1:8ea78dce6b36 527
philware 1:8ea78dce6b36 528 int sizes[] = { 1, 2, 5, 150, 160, 170, 180, 190, 200, 250 };
philware 1:8ea78dce6b36 529
philware 1:8ea78dce6b36 530 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 531 //do_udp_echo(&sock, &host_address, 1);
philware 1:8ea78dce6b36 532 //do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 533 for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 534 do_udp_echo(&sock, &host_address, sizes[x]);
philware 1:8ea78dce6b36 535 }
philware 1:8ea78dce6b36 536
philware 1:8ea78dce6b36 537 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 538 // do_udp_echo(&sock, &host_address, 1);
philware 1:8ea78dce6b36 539 // do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 540 // for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 541 // size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1;
philware 1:8ea78dce6b36 542 // size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 543 // do_udp_echo(&sock, &host_address, size);
philware 1:8ea78dce6b36 544 // }
philware 1:8ea78dce6b36 545
philware 1:8ea78dce6b36 546 sock.close();
philware 1:8ea78dce6b36 547
philware 1:8ea78dce6b36 548 drop_connection(interface);
philware 1:8ea78dce6b36 549 tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.",
philware 1:8ea78dce6b36 550 x, MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 551 }
philware 1:8ea78dce6b36 552
philware 1:8ea78dce6b36 553 // Test many different sizes of UDP data arriving at once
philware 1:8ea78dce6b36 554 void test_udp_echo_recv_sizes() {
philware 1:8ea78dce6b36 555 UDPSocket sock;
philware 1:8ea78dce6b36 556 SocketAddress host_address;
philware 1:8ea78dce6b36 557 int x, y, z;
philware 1:8ea78dce6b36 558 int size;
philware 1:8ea78dce6b36 559 int tries = 0;
philware 1:8ea78dce6b36 560 unsigned int offset;
philware 1:8ea78dce6b36 561 char * recv_data;
philware 1:8ea78dce6b36 562 bool packetLoss;
philware 1:8ea78dce6b36 563 bool sendSuccess;
philware 1:8ea78dce6b36 564 Timer timer;
philware 1:8ea78dce6b36 565
philware 1:8ea78dce6b36 566 interface->deinit();
philware 1:8ea78dce6b36 567 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 568 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 569
philware 1:8ea78dce6b36 570 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 571 host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
philware 1:8ea78dce6b36 572
philware 1:8ea78dce6b36 573 tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 574 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 575
philware 1:8ea78dce6b36 576 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 577
philware 1:8ea78dce6b36 578 do {
philware 1:8ea78dce6b36 579 tr_debug("--- UDP packet size test, test try %d, flushing input buffers", tries + 1);
philware 1:8ea78dce6b36 580 // First of all, clear any junk from the socket
philware 1:8ea78dce6b36 581 sock.set_timeout(1000);
philware 1:8ea78dce6b36 582 recv_data = (char *) malloc (MBED_CONF_APP_UDP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 583 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 584 while (sock.recvfrom(&host_address, (void *) recv_data, MBED_CONF_APP_UDP_MAX_PACKET_SIZE) > 0) {
philware 1:8ea78dce6b36 585 // Throw it away
philware 1:8ea78dce6b36 586 }
philware 1:8ea78dce6b36 587 free (recv_data);
philware 1:8ea78dce6b36 588
philware 1:8ea78dce6b36 589 sock.set_timeout(10000);
philware 1:8ea78dce6b36 590
philware 1:8ea78dce6b36 591 // Throw random sized UDP packets up...
philware 1:8ea78dce6b36 592 x = 0;
philware 1:8ea78dce6b36 593 offset = 0;
philware 1:8ea78dce6b36 594 while (offset < sizeof (send_data)) {
philware 1:8ea78dce6b36 595 size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1;
philware 1:8ea78dce6b36 596 size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2);
philware 1:8ea78dce6b36 597 if (offset + size > sizeof (send_data)) {
philware 1:8ea78dce6b36 598 size = sizeof (send_data) - offset;
philware 1:8ea78dce6b36 599 }
philware 1:8ea78dce6b36 600 sendSuccess = false;
philware 1:8ea78dce6b36 601 for (y = 0; !sendSuccess && (y < NUM_UDP_RETRIES); y++) {
philware 1:8ea78dce6b36 602 tr_debug("Sending UDP packet number %d, size %d byte(s), send try %d.", x + 1, size, y + 1);
philware 1:8ea78dce6b36 603 if (sock.sendto(host_address, (void *) (send_data + offset), size) == size) {
philware 1:8ea78dce6b36 604 sendSuccess = true;
philware 1:8ea78dce6b36 605 offset += size;
philware 1:8ea78dce6b36 606 }
philware 1:8ea78dce6b36 607 }
philware 1:8ea78dce6b36 608 TEST_ASSERT(sendSuccess);
philware 1:8ea78dce6b36 609 x++;
philware 1:8ea78dce6b36 610 }
philware 1:8ea78dce6b36 611 tr_debug("--- All UDP packets sent");
philware 1:8ea78dce6b36 612
philware 1:8ea78dce6b36 613 // ...and capture them all again afterwards
philware 1:8ea78dce6b36 614 recv_data = (char *) malloc (sizeof (send_data));
philware 1:8ea78dce6b36 615 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 616 memset (recv_data, 0, sizeof (send_data));
philware 1:8ea78dce6b36 617 size = 0;
philware 1:8ea78dce6b36 618 y = 0;
philware 1:8ea78dce6b36 619 packetLoss = false;
philware 1:8ea78dce6b36 620 timer.start();
philware 1:8ea78dce6b36 621 while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 10000)) {
philware 1:8ea78dce6b36 622 y = sock.recvfrom(&host_address, (void *) (recv_data + size), sizeof (send_data) - size);
philware 1:8ea78dce6b36 623 if (y > 0) {
philware 1:8ea78dce6b36 624 size += y;
philware 1:8ea78dce6b36 625 }
philware 1:8ea78dce6b36 626 }
philware 1:8ea78dce6b36 627 timer.stop();
philware 1:8ea78dce6b36 628 timer.reset();
philware 1:8ea78dce6b36 629 tr_debug( "--- Either received everything back or timed out waiting");
philware 1:8ea78dce6b36 630
philware 1:8ea78dce6b36 631 // Check that we reassembled everything correctly
philware 1:8ea78dce6b36 632 if (size == sizeof (send_data)) {
philware 1:8ea78dce6b36 633 for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) {
philware 1:8ea78dce6b36 634 }
philware 1:8ea78dce6b36 635 if (x != sizeof (send_data)) {
philware 1:8ea78dce6b36 636 y = x - 5;
philware 1:8ea78dce6b36 637 if (y < 0) {
philware 1:8ea78dce6b36 638 y = 0;
philware 1:8ea78dce6b36 639 }
philware 1:8ea78dce6b36 640 z = 10;
philware 1:8ea78dce6b36 641 if (y + z > (int) sizeof (send_data)) {
philware 1:8ea78dce6b36 642 z = sizeof(send_data) - y;
philware 1:8ea78dce6b36 643 }
philware 1:8ea78dce6b36 644 tr_debug(" --- Difference at character %d (send \"%*.*s\", recv \"%*.*s\")",
philware 1:8ea78dce6b36 645 x + 1, z, z, send_data + y, z, z, recv_data + y);
philware 1:8ea78dce6b36 646 packetLoss = true;
philware 1:8ea78dce6b36 647 }
philware 1:8ea78dce6b36 648 } else {
philware 1:8ea78dce6b36 649 tr_debug(" --- %d bytes missing (%d bytes received when %d were expected))",
philware 1:8ea78dce6b36 650 sizeof (send_data) - size, size, sizeof (send_data));
philware 1:8ea78dce6b36 651 packetLoss = true;
philware 1:8ea78dce6b36 652 }
philware 1:8ea78dce6b36 653 free (recv_data);
philware 1:8ea78dce6b36 654 tries++;
philware 1:8ea78dce6b36 655 } while (packetLoss && (tries < NUM_UDP_RETRIES));
philware 1:8ea78dce6b36 656
philware 1:8ea78dce6b36 657 TEST_ASSERT(!packetLoss);
philware 1:8ea78dce6b36 658 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 659 sock.close();
philware 1:8ea78dce6b36 660 drop_connection(interface);
philware 1:8ea78dce6b36 661 }
philware 1:8ea78dce6b36 662
philware 1:8ea78dce6b36 663 // Test UDP data exchange via the asynchronous sigio() mechanism
philware 1:8ea78dce6b36 664 void test_udp_echo_async() {
philware 1:8ea78dce6b36 665 UDPSocket sock;
philware 1:8ea78dce6b36 666 SocketAddress host_address;
philware 1:8ea78dce6b36 667 SocketAddress local_address;
philware 1:8ea78dce6b36 668 bool callback_triggered = false;
philware 1:8ea78dce6b36 669 int x;
philware 1:8ea78dce6b36 670 int size;
philware 1:8ea78dce6b36 671
philware 1:8ea78dce6b36 672 interface->deinit();
philware 1:8ea78dce6b36 673 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 674 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 675
philware 1:8ea78dce6b36 676 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 677 host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT);
philware 1:8ea78dce6b36 678
philware 1:8ea78dce6b36 679 tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 680 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 681
philware 1:8ea78dce6b36 682 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 683
philware 1:8ea78dce6b36 684 // Set up the async callback and set the timeout to zero
philware 1:8ea78dce6b36 685 sock.sigio(callback(async_cb, &callback_triggered));
philware 1:8ea78dce6b36 686 sock.set_timeout(0);
philware 1:8ea78dce6b36 687
philware 1:8ea78dce6b36 688 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 689 // and this time allow the UDP packets to be fragmented
philware 1:8ea78dce6b36 690 do_udp_echo_async(&sock, &host_address, 1, &callback_triggered);
philware 1:8ea78dce6b36 691 do_udp_echo_async(&sock, &host_address, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE,
philware 1:8ea78dce6b36 692 &callback_triggered);
philware 1:8ea78dce6b36 693 for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 694 size = (rand() % MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE) + 1;
philware 1:8ea78dce6b36 695 size = fix(size, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE);
philware 1:8ea78dce6b36 696 do_udp_echo_async(&sock, &host_address, size, &callback_triggered);
philware 1:8ea78dce6b36 697 }
philware 1:8ea78dce6b36 698
philware 1:8ea78dce6b36 699 sock.close();
philware 1:8ea78dce6b36 700
philware 1:8ea78dce6b36 701 drop_connection(interface);
philware 1:8ea78dce6b36 702
philware 1:8ea78dce6b36 703 tr_debug("%d UDP packets of size up to %d byte(s) echoed asynchronously and successfully.",
philware 1:8ea78dce6b36 704 x, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE);
philware 1:8ea78dce6b36 705 }
philware 1:8ea78dce6b36 706
philware 1:8ea78dce6b36 707 // Test many different sizes of TCP data arriving at once
philware 1:8ea78dce6b36 708 void test_tcp_echo_recv_sizes() {
philware 1:8ea78dce6b36 709 TCPSocket sock;
philware 1:8ea78dce6b36 710 SocketAddress host_address;
philware 1:8ea78dce6b36 711 int x, y, z;
philware 1:8ea78dce6b36 712 int size;
philware 1:8ea78dce6b36 713 unsigned int offset;
philware 1:8ea78dce6b36 714 char * recv_data;
philware 1:8ea78dce6b36 715 Timer timer;
philware 1:8ea78dce6b36 716
philware 1:8ea78dce6b36 717 interface->deinit();
philware 1:8ea78dce6b36 718 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 719 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 720
philware 1:8ea78dce6b36 721 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 722 host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);
philware 1:8ea78dce6b36 723
philware 1:8ea78dce6b36 724 tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 725 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 726
philware 1:8ea78dce6b36 727 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 728
philware 1:8ea78dce6b36 729 TEST_ASSERT(sock.connect(host_address) == 0);
philware 1:8ea78dce6b36 730
philware 1:8ea78dce6b36 731 sock.set_timeout(10000);
philware 1:8ea78dce6b36 732
philware 1:8ea78dce6b36 733 // Throw random sized TCP packets up...
philware 1:8ea78dce6b36 734 x = 0;
philware 1:8ea78dce6b36 735 offset = 0;
philware 1:8ea78dce6b36 736 while (offset < sizeof (send_data)) {
philware 1:8ea78dce6b36 737 size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1;
philware 1:8ea78dce6b36 738 size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2);
philware 1:8ea78dce6b36 739 if (offset + size > sizeof (send_data)) {
philware 1:8ea78dce6b36 740 size = sizeof (send_data) - offset;
philware 1:8ea78dce6b36 741 }
philware 1:8ea78dce6b36 742 tr_debug("Sending TCP packet number %d, size %d byte(s).", x + 1, size);
philware 1:8ea78dce6b36 743 TEST_ASSERT(sendAll(&sock, (send_data + offset), size) == size);
philware 1:8ea78dce6b36 744 offset += size;
philware 1:8ea78dce6b36 745 x++;
philware 1:8ea78dce6b36 746 }
philware 1:8ea78dce6b36 747
philware 1:8ea78dce6b36 748 // ...and capture them all again afterwards
philware 1:8ea78dce6b36 749 recv_data = (char *) malloc (sizeof (send_data));
philware 1:8ea78dce6b36 750 TEST_ASSERT(recv_data != NULL);
philware 1:8ea78dce6b36 751 memset (recv_data, 0, sizeof (send_data));
philware 1:8ea78dce6b36 752 size = 0;
philware 1:8ea78dce6b36 753 x = 0;
philware 1:8ea78dce6b36 754 timer.start();
philware 1:8ea78dce6b36 755 while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 30000)) {
philware 1:8ea78dce6b36 756 y = sock.recv((void *) (recv_data + size), sizeof (send_data) - size);
philware 1:8ea78dce6b36 757 tr_debug("Received TCP packet number %d, size %d byte(s).", x, y);
philware 1:8ea78dce6b36 758 size += y;
philware 1:8ea78dce6b36 759 x++;
philware 1:8ea78dce6b36 760 }
philware 1:8ea78dce6b36 761 timer.stop();
philware 1:8ea78dce6b36 762 timer.reset();
philware 1:8ea78dce6b36 763
philware 1:8ea78dce6b36 764 // Check that we reassembled everything correctly
philware 1:8ea78dce6b36 765 for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) {
philware 1:8ea78dce6b36 766 }
philware 1:8ea78dce6b36 767 if (x != sizeof (send_data)) {
philware 1:8ea78dce6b36 768 y = x - 5;
philware 1:8ea78dce6b36 769 if (y < 0) {
philware 1:8ea78dce6b36 770 y = 0;
philware 1:8ea78dce6b36 771 }
philware 1:8ea78dce6b36 772 z = 10;
philware 1:8ea78dce6b36 773 if (y + z > (int) sizeof (send_data)) {
philware 1:8ea78dce6b36 774 z = sizeof(send_data) - y;
philware 1:8ea78dce6b36 775 }
philware 1:8ea78dce6b36 776 tr_debug("Difference at character %d (send \"%*.*s\", recv \"%*.*s\")",
philware 1:8ea78dce6b36 777 x + 1, z, z, send_data + y, z, z, recv_data + y);
philware 1:8ea78dce6b36 778 TEST_ASSERT(false);
philware 1:8ea78dce6b36 779 }
philware 1:8ea78dce6b36 780 free (recv_data);
philware 1:8ea78dce6b36 781
philware 1:8ea78dce6b36 782 TEST_ASSERT(!connection_has_gone_down);
philware 1:8ea78dce6b36 783 sock.close();
philware 1:8ea78dce6b36 784 drop_connection(interface);
philware 1:8ea78dce6b36 785 }
philware 1:8ea78dce6b36 786
philware 1:8ea78dce6b36 787 // Test TCP data exchange via the asynchronous sigio() mechanism
philware 1:8ea78dce6b36 788 void test_tcp_echo_async() {
philware 1:8ea78dce6b36 789 TCPSocket sock;
philware 1:8ea78dce6b36 790 SocketAddress host_address;
philware 1:8ea78dce6b36 791 bool callback_triggered = false;
philware 1:8ea78dce6b36 792 int x;
philware 1:8ea78dce6b36 793 int size;
philware 1:8ea78dce6b36 794
philware 1:8ea78dce6b36 795 interface->deinit();
philware 1:8ea78dce6b36 796 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 797 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 798
philware 1:8ea78dce6b36 799 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 800 host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);
philware 1:8ea78dce6b36 801
philware 1:8ea78dce6b36 802 tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER,
philware 1:8ea78dce6b36 803 host_address.get_ip_address(), host_address.get_port());
philware 1:8ea78dce6b36 804
philware 1:8ea78dce6b36 805 TEST_ASSERT(sock.open(interface) == 0)
philware 1:8ea78dce6b36 806
philware 1:8ea78dce6b36 807 // Set up the async callback and set the timeout to zero
philware 1:8ea78dce6b36 808 sock.sigio(callback(async_cb, &callback_triggered));
philware 1:8ea78dce6b36 809 sock.set_timeout(0);
philware 1:8ea78dce6b36 810
philware 1:8ea78dce6b36 811 TEST_ASSERT(sock.connect(host_address) == 0);
philware 1:8ea78dce6b36 812 // Test min, max, and some random sizes in-between
philware 1:8ea78dce6b36 813 do_tcp_echo_async(&sock, 1, &callback_triggered);
philware 1:8ea78dce6b36 814 do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, &callback_triggered);
philware 1:8ea78dce6b36 815 for (x = 0; x < 10; x++) {
philware 1:8ea78dce6b36 816 size = (rand() % MBED_CONF_APP_TCP_MAX_PACKET_SIZE) + 1;
philware 1:8ea78dce6b36 817 size = fix(size, MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 818 do_tcp_echo_async(&sock, size, &callback_triggered);
philware 1:8ea78dce6b36 819 }
philware 1:8ea78dce6b36 820
philware 1:8ea78dce6b36 821 sock.close();
philware 1:8ea78dce6b36 822
philware 1:8ea78dce6b36 823 drop_connection(interface);
philware 1:8ea78dce6b36 824
philware 1:8ea78dce6b36 825 tr_debug("%d TCP packets of size up to %d byte(s) echoed asynchronously and successfully.",
philware 1:8ea78dce6b36 826 x, MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
philware 1:8ea78dce6b36 827 }
philware 1:8ea78dce6b36 828 #endif
philware 1:8ea78dce6b36 829
philware 1:8ea78dce6b36 830 // Allocate max sockets
philware 1:8ea78dce6b36 831 void test_max_sockets() {
philware 1:8ea78dce6b36 832 UDPSocket sock[MAX_NUM_SOCKETS];
philware 1:8ea78dce6b36 833 UDPSocket sockNone;
philware 1:8ea78dce6b36 834 SocketAddress host_address;
philware 1:8ea78dce6b36 835
philware 1:8ea78dce6b36 836 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 837 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 838
philware 1:8ea78dce6b36 839 TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0);
philware 1:8ea78dce6b36 840 host_address.set_port(MBED_CONF_APP_NTP_PORT);
philware 1:8ea78dce6b36 841
philware 1:8ea78dce6b36 842 // Open the first socket and use it
philware 1:8ea78dce6b36 843 interface->set_LocalListenPort(10000);
philware 1:8ea78dce6b36 844 TEST_ASSERT(sock[0].open(interface) == 0)
philware 1:8ea78dce6b36 845 sock[0].set_timeout(10000);
philware 1:8ea78dce6b36 846 do_ntp_sock(&sock[0], host_address);
philware 1:8ea78dce6b36 847
philware 1:8ea78dce6b36 848 // Check that we stop being able to get sockets at the max number
philware 1:8ea78dce6b36 849 for (int x = 1; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) {
philware 1:8ea78dce6b36 850 interface->set_LocalListenPort(10000+x);
philware 1:8ea78dce6b36 851 TEST_ASSERT(sock[x].open(interface) == 0)
philware 1:8ea78dce6b36 852 }
philware 1:8ea78dce6b36 853 TEST_ASSERT(sockNone.open(interface) < 0);
philware 1:8ea78dce6b36 854
philware 1:8ea78dce6b36 855 // Now use the last one
philware 1:8ea78dce6b36 856 sock[sizeof (sock) / sizeof (sock[0]) - 1].set_timeout(10000);
philware 1:8ea78dce6b36 857 do_ntp_sock(&sock[sizeof (sock) / sizeof (sock[0]) - 1], host_address);
philware 1:8ea78dce6b36 858
philware 1:8ea78dce6b36 859 // Close all of the sockets
philware 1:8ea78dce6b36 860 for (int x = 0; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) {
philware 1:8ea78dce6b36 861 TEST_ASSERT(sock[x].close() == 0);
philware 1:8ea78dce6b36 862 }
philware 1:8ea78dce6b36 863
philware 1:8ea78dce6b36 864 drop_connection(interface);
philware 1:8ea78dce6b36 865 }
philware 1:8ea78dce6b36 866
philware 1:8ea78dce6b36 867 // Connect with credentials included in the connect request
philware 1:8ea78dce6b36 868 void test_connect_credentials() {
philware 1:8ea78dce6b36 869
philware 1:8ea78dce6b36 870 interface->deinit();
philware 1:8ea78dce6b36 871 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 872 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 873 use_connection(interface);
philware 1:8ea78dce6b36 874 drop_connection(interface);
philware 1:8ea78dce6b36 875 }
philware 1:8ea78dce6b36 876
philware 1:8ea78dce6b36 877 // Test with credentials preset
philware 1:8ea78dce6b36 878 void test_connect_preset_credentials() {
philware 1:8ea78dce6b36 879
philware 1:8ea78dce6b36 880 interface->deinit();
philware 1:8ea78dce6b36 881 TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN));
philware 1:8ea78dce6b36 882 interface->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 883 MBED_CONF_APP_PASSWORD);
philware 1:8ea78dce6b36 884 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN) == 0);
philware 1:8ea78dce6b36 885 use_connection(interface);
philware 1:8ea78dce6b36 886 drop_connection(interface);
philware 1:8ea78dce6b36 887 }
philware 1:8ea78dce6b36 888
philware 1:8ea78dce6b36 889 // Test adding and using a SIM pin, then removing it, using the pending
philware 1:8ea78dce6b36 890 // mechanism where the change doesn't occur until connect() is called
philware 1:8ea78dce6b36 891 void test_check_sim_pin_pending() {
philware 1:8ea78dce6b36 892
philware 1:8ea78dce6b36 893 interface->deinit();
philware 1:8ea78dce6b36 894
philware 1:8ea78dce6b36 895 // Enable PIN checking (which will use the current PIN)
philware 1:8ea78dce6b36 896 // and also flag that the PIN should be changed to MBED_CONF_APP_ALT_PIN,
philware 1:8ea78dce6b36 897 // then try connecting
philware 1:8ea78dce6b36 898 interface->set_sim_pin_check(true);
philware 1:8ea78dce6b36 899 interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN);
philware 1:8ea78dce6b36 900 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 901 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 902 use_connection(interface);
philware 1:8ea78dce6b36 903 drop_connection(interface);
philware 1:8ea78dce6b36 904 interface->deinit();
philware 1:8ea78dce6b36 905
philware 1:8ea78dce6b36 906 // Now change the PIN back to what it was before
philware 1:8ea78dce6b36 907 interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 908 TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 909 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 910 use_connection(interface);
philware 1:8ea78dce6b36 911 drop_connection(interface);
philware 1:8ea78dce6b36 912 interface->deinit();
philware 1:8ea78dce6b36 913
philware 1:8ea78dce6b36 914 // Check that it was changed back, and this time
philware 1:8ea78dce6b36 915 // use the other way of entering the PIN
philware 1:8ea78dce6b36 916 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 917 TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 918 MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 919 use_connection(interface);
philware 1:8ea78dce6b36 920 drop_connection(interface);
philware 1:8ea78dce6b36 921 interface->deinit();
philware 1:8ea78dce6b36 922
philware 1:8ea78dce6b36 923 // Remove PIN checking again and check that it no
philware 1:8ea78dce6b36 924 // longer matters what the PIN is
philware 1:8ea78dce6b36 925 interface->set_sim_pin_check(false);
philware 1:8ea78dce6b36 926 TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 927 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 928 use_connection(interface);
philware 1:8ea78dce6b36 929 drop_connection(interface);
philware 1:8ea78dce6b36 930 interface->deinit();
philware 1:8ea78dce6b36 931 TEST_ASSERT(interface->init(NULL));
philware 1:8ea78dce6b36 932 TEST_ASSERT(interface->connect(MBED_CONF_APP_INCORRECT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 933 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 934 use_connection(interface);
philware 1:8ea78dce6b36 935 drop_connection(interface);
philware 1:8ea78dce6b36 936
philware 1:8ea78dce6b36 937 // Put the SIM pin back to the correct value for any subsequent tests
philware 1:8ea78dce6b36 938 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 939 }
philware 1:8ea78dce6b36 940
philware 1:8ea78dce6b36 941 // Test adding and using a SIM pin, then removing it, using the immediate
philware 1:8ea78dce6b36 942 // mechanism
philware 1:8ea78dce6b36 943 void test_check_sim_pin_immediate() {
philware 1:8ea78dce6b36 944
philware 1:8ea78dce6b36 945 interface->deinit();
philware 1:8ea78dce6b36 946 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 947
philware 1:8ea78dce6b36 948 // Enable PIN checking (which will use the current PIN), change
philware 1:8ea78dce6b36 949 // the PIN to MBED_CONF_APP_ALT_PIN, then try connecting after powering on and
philware 1:8ea78dce6b36 950 // off the modem
philware 1:8ea78dce6b36 951 interface->set_sim_pin_check(true, true, MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 952 interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN, true);
philware 1:8ea78dce6b36 953 interface->deinit();
philware 1:8ea78dce6b36 954 TEST_ASSERT(interface->init(NULL));
philware 1:8ea78dce6b36 955 TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 956 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 957 use_connection(interface);
philware 1:8ea78dce6b36 958 drop_connection(interface);
philware 1:8ea78dce6b36 959
philware 1:8ea78dce6b36 960 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 961
philware 1:8ea78dce6b36 962 // Now change the PIN back to what it was before
philware 1:8ea78dce6b36 963 interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN, true);
philware 1:8ea78dce6b36 964 interface->deinit();
philware 1:8ea78dce6b36 965 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 966 TEST_ASSERT(interface->init(NULL));
philware 1:8ea78dce6b36 967 TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 968 MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 969 use_connection(interface);
philware 1:8ea78dce6b36 970 drop_connection(interface);
philware 1:8ea78dce6b36 971
philware 1:8ea78dce6b36 972 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 973
philware 1:8ea78dce6b36 974 // Remove PIN checking again and check that it no
philware 1:8ea78dce6b36 975 // longer matters what the PIN is
philware 1:8ea78dce6b36 976 interface->set_sim_pin_check(false, true);
philware 1:8ea78dce6b36 977 interface->deinit();
philware 1:8ea78dce6b36 978 TEST_ASSERT(interface->init(MBED_CONF_APP_INCORRECT_PIN));
philware 1:8ea78dce6b36 979 TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME,
philware 1:8ea78dce6b36 980 MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 981 use_connection(interface);
philware 1:8ea78dce6b36 982 drop_connection(interface);
philware 1:8ea78dce6b36 983
philware 1:8ea78dce6b36 984 // Put the SIM pin back to the correct value for any subsequent tests
philware 1:8ea78dce6b36 985 interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN);
philware 1:8ea78dce6b36 986 }
philware 1:8ea78dce6b36 987
philware 1:8ea78dce6b36 988 // Test being able to connect with a local instance of the interface
philware 1:8ea78dce6b36 989 // NOTE: since this local instance will fiddle with bits of HW that the
philware 1:8ea78dce6b36 990 // static instance thought it owned, the static instance will no longer
philware 1:8ea78dce6b36 991 // work afterwards, hence this must be run as the last test in the list
philware 1:8ea78dce6b36 992 void test_connect_local_instance_last_test() {
philware 1:8ea78dce6b36 993
philware 1:8ea78dce6b36 994 UbloxATCellularInterfaceN2xx *pLocalInterface = NULL;
philware 1:8ea78dce6b36 995
philware 1:8ea78dce6b36 996 pLocalInterface = new UbloxATCellularInterfaceN2xx(MDMTXD, MDMRXD,
philware 1:8ea78dce6b36 997 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
philware 1:8ea78dce6b36 998 MBED_CONF_APP_DEBUG_ON);
philware 1:8ea78dce6b36 999 pLocalInterface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 1000
philware 1:8ea78dce6b36 1001 TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 1002 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 1003 use_connection(pLocalInterface);
philware 1:8ea78dce6b36 1004 drop_connection(pLocalInterface);
philware 1:8ea78dce6b36 1005 delete pLocalInterface;
philware 1:8ea78dce6b36 1006
philware 1:8ea78dce6b36 1007 pLocalInterface = new UbloxATCellularInterfaceN2xx(MDMTXD, MDMRXD,
philware 1:8ea78dce6b36 1008 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
philware 1:8ea78dce6b36 1009 MBED_CONF_APP_DEBUG_ON);
philware 1:8ea78dce6b36 1010 pLocalInterface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 1011
philware 1:8ea78dce6b36 1012 TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
philware 1:8ea78dce6b36 1013 MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
philware 1:8ea78dce6b36 1014 use_connection(pLocalInterface);
philware 1:8ea78dce6b36 1015 drop_connection(pLocalInterface);
philware 1:8ea78dce6b36 1016 delete pLocalInterface;
philware 1:8ea78dce6b36 1017 }
philware 1:8ea78dce6b36 1018
philware 1:8ea78dce6b36 1019 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1020 // TEST ENVIRONMENT
philware 1:8ea78dce6b36 1021 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1022
philware 1:8ea78dce6b36 1023 // Setup the test environment
philware 1:8ea78dce6b36 1024 utest::v1::status_t test_setup(const size_t number_of_cases) {
philware 1:8ea78dce6b36 1025 // Setup Greentea with a timeout
philware 1:8ea78dce6b36 1026 GREENTEA_SETUP(960, "default_auto");
philware 1:8ea78dce6b36 1027 return verbose_test_setup_handler(number_of_cases);
philware 1:8ea78dce6b36 1028 }
philware 1:8ea78dce6b36 1029
philware 1:8ea78dce6b36 1030 // IMPORTANT!!! if you make a change to the tests here you should
philware 1:8ea78dce6b36 1031 // check whether the same change should be made to the tests under
philware 1:8ea78dce6b36 1032 // the PPP interface.
philware 1:8ea78dce6b36 1033
philware 1:8ea78dce6b36 1034 // Test cases
philware 1:8ea78dce6b36 1035 Case cases[] = {
philware 1:8ea78dce6b36 1036 Case("Set randomise", test_set_randomise),
philware 1:8ea78dce6b36 1037 #ifdef MBED_CONF_APP_ECHO_SERVER
philware 1:8ea78dce6b36 1038 Case("UDP echo test", test_udp_echo),
philware 1:8ea78dce6b36 1039 # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
philware 1:8ea78dce6b36 1040 Case("UDP recv sizes", test_udp_echo_recv_sizes),
philware 1:8ea78dce6b36 1041 # endif
philware 1:8ea78dce6b36 1042 Case("UDP async echo test", test_udp_echo_async),
philware 1:8ea78dce6b36 1043 # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
philware 1:8ea78dce6b36 1044 Case("TCP recv sizes", test_tcp_echo_recv_sizes),
philware 1:8ea78dce6b36 1045 # endif
philware 1:8ea78dce6b36 1046 Case("TCP async echo test", test_tcp_echo_async),
philware 1:8ea78dce6b36 1047 # endif
philware 1:8ea78dce6b36 1048 #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test
philware 1:8ea78dce6b36 1049 Case("Alloc max sockets", test_max_sockets),
philware 1:8ea78dce6b36 1050 #endif
philware 1:8ea78dce6b36 1051 Case("Connect with credentials", test_connect_credentials),
philware 1:8ea78dce6b36 1052 Case("Connect with preset credentials", test_connect_preset_credentials),
philware 1:8ea78dce6b36 1053 #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS
philware 1:8ea78dce6b36 1054 Case("Check SIM pin, pending", test_check_sim_pin_pending),
philware 1:8ea78dce6b36 1055 Case("Check SIM pin, immediate", test_check_sim_pin_immediate),
philware 1:8ea78dce6b36 1056 #endif
philware 1:8ea78dce6b36 1057 #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 for this
philware 1:8ea78dce6b36 1058 Case("Connect using local instance, must be last test", test_connect_local_instance_last_test)
philware 1:8ea78dce6b36 1059 #endif
philware 1:8ea78dce6b36 1060 };
philware 1:8ea78dce6b36 1061
philware 1:8ea78dce6b36 1062 Specification specification(test_setup, cases);
philware 1:8ea78dce6b36 1063
philware 1:8ea78dce6b36 1064 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1065 // MAIN
philware 1:8ea78dce6b36 1066 // ----------------------------------------------------------------
philware 1:8ea78dce6b36 1067
philware 1:8ea78dce6b36 1068 int main() {
philware 1:8ea78dce6b36 1069
philware 1:8ea78dce6b36 1070 #ifdef FEATURE_COMMON_PAL
philware 1:8ea78dce6b36 1071 mbed_trace_init();
philware 1:8ea78dce6b36 1072
philware 1:8ea78dce6b36 1073 mbed_trace_mutex_wait_function_set(lock);
philware 1:8ea78dce6b36 1074 mbed_trace_mutex_release_function_set(unlock);
philware 1:8ea78dce6b36 1075 #endif
philware 1:8ea78dce6b36 1076
philware 1:8ea78dce6b36 1077 interface = new UbloxATCellularInterfaceN2xx(MDMTXD, MDMRXD,
philware 1:8ea78dce6b36 1078 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
philware 1:8ea78dce6b36 1079 MBED_CONF_APP_DEBUG_ON);
philware 1:8ea78dce6b36 1080
philware 1:8ea78dce6b36 1081 interface->connection_status_cb(connection_down_cb);
philware 1:8ea78dce6b36 1082
philware 1:8ea78dce6b36 1083 tr_debug("Sending large AT command: 150bytes SendTo");
philware 1:8ea78dce6b36 1084 interface->SendAT("AT+NSOST=0,10.10.10.10,7,150,112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900");
philware 1:8ea78dce6b36 1085 tr_debug("Sending large AT command: 200bytes SendTo");
philware 1:8ea78dce6b36 1086 interface->SendAT("AT+NSOST=0,10.10.10.10,7,200,1122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900");
philware 1:8ea78dce6b36 1087
philware 1:8ea78dce6b36 1088 // Run tests
philware 1:8ea78dce6b36 1089 return !Harness::run(specification);
philware 1:8ea78dce6b36 1090 }
philware 1:8ea78dce6b36 1091
philware 1:8ea78dce6b36 1092 // End Of File
philware 1:8ea78dce6b36 1093