Library that implements the CellularInterface using PPP and LWIP on the mbed MCU. May be used on the C027 and C030 (non-N2xx flavour) boards from mbed 5.5 onwards.
Dependents: example-ublox-cellular-interface HelloMQTT example-ublox-cellular-interface_r410M example-ublox-mbed-client
TESTS/unit_tests/default/main.cpp@0:44dd95724bc2, 2017-06-14 (annotated)
- Committer:
- RobMeades
- Date:
- Wed Jun 14 09:27:55 2017 +0000
- Revision:
- 0:44dd95724bc2
- Child:
- 1:80ec3fccad9e
Initial commit of u-blox PPP cellular interface, which implements a data connection using LWIP and PPP on the MCU.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:44dd95724bc2 | 1 | #include "UbloxPPPCellularInterface.h" |
RobMeades | 0:44dd95724bc2 | 2 | #include "mbed.h" |
RobMeades | 0:44dd95724bc2 | 3 | #include "Callback.h" |
RobMeades | 0:44dd95724bc2 | 4 | #include "greentea-client/test_env.h" |
RobMeades | 0:44dd95724bc2 | 5 | #include "unity.h" |
RobMeades | 0:44dd95724bc2 | 6 | #include "utest.h" |
RobMeades | 0:44dd95724bc2 | 7 | #include "UDPSocket.h" |
RobMeades | 0:44dd95724bc2 | 8 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 9 | #include "mbed_trace.h" |
RobMeades | 0:44dd95724bc2 | 10 | #define TRACE_GROUP "TEST" |
RobMeades | 0:44dd95724bc2 | 11 | #else |
RobMeades | 0:44dd95724bc2 | 12 | #define tr_debug(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__) |
RobMeades | 0:44dd95724bc2 | 13 | #define tr_info(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__) |
RobMeades | 0:44dd95724bc2 | 14 | #define tr_warn(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__) |
RobMeades | 0:44dd95724bc2 | 15 | #define tr_error(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__) |
RobMeades | 0:44dd95724bc2 | 16 | #endif |
RobMeades | 0:44dd95724bc2 | 17 | |
RobMeades | 0:44dd95724bc2 | 18 | using namespace utest::v1; |
RobMeades | 0:44dd95724bc2 | 19 | |
RobMeades | 0:44dd95724bc2 | 20 | #if NSAPI_PPP_AVAILABLE |
RobMeades | 0:44dd95724bc2 | 21 | |
RobMeades | 0:44dd95724bc2 | 22 | // IMPORTANT!!! if you make a change to the tests here you should |
RobMeades | 0:44dd95724bc2 | 23 | // check whether the same change should be made to the tests under |
RobMeades | 0:44dd95724bc2 | 24 | // the AT DATA driver. |
RobMeades | 0:44dd95724bc2 | 25 | |
RobMeades | 0:44dd95724bc2 | 26 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 27 | // COMPILE-TIME MACROS |
RobMeades | 0:44dd95724bc2 | 28 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 29 | |
RobMeades | 0:44dd95724bc2 | 30 | // These macros can be overridden with an mbed_app.json file and |
RobMeades | 0:44dd95724bc2 | 31 | // contents of the following form: |
RobMeades | 0:44dd95724bc2 | 32 | // |
RobMeades | 0:44dd95724bc2 | 33 | //{ |
RobMeades | 0:44dd95724bc2 | 34 | // "config": { |
RobMeades | 0:44dd95724bc2 | 35 | // "default-pin": { |
RobMeades | 0:44dd95724bc2 | 36 | // "value": "\"1234\"" |
RobMeades | 0:44dd95724bc2 | 37 | // } |
RobMeades | 0:44dd95724bc2 | 38 | //} |
RobMeades | 0:44dd95724bc2 | 39 | // |
RobMeades | 0:44dd95724bc2 | 40 | // See the template_mbed_app.txt in this directory for a fuller example. |
RobMeades | 0:44dd95724bc2 | 41 | |
RobMeades | 0:44dd95724bc2 | 42 | // Whether debug trace is on |
RobMeades | 0:44dd95724bc2 | 43 | #ifndef MBED_CONF_APP_DEBUG_ON |
RobMeades | 0:44dd95724bc2 | 44 | # define MBED_CONF_APP_DEBUG_ON false |
RobMeades | 0:44dd95724bc2 | 45 | #endif |
RobMeades | 0:44dd95724bc2 | 46 | |
RobMeades | 0:44dd95724bc2 | 47 | // Run the SIM change tests, which require the DEFAULT_PIN |
RobMeades | 0:44dd95724bc2 | 48 | // above to be correct for the board on which the test |
RobMeades | 0:44dd95724bc2 | 49 | // is being run (and the SIM PIN to be disabled before tests run). |
RobMeades | 0:44dd95724bc2 | 50 | #ifndef MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:44dd95724bc2 | 51 | # define MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS 0 |
RobMeades | 0:44dd95724bc2 | 52 | #endif |
RobMeades | 0:44dd95724bc2 | 53 | |
RobMeades | 0:44dd95724bc2 | 54 | #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:44dd95724bc2 | 55 | # ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:44dd95724bc2 | 56 | # error "MBED_CONF_APP_DEFAULT_PIN must be defined to run the SIM tests" |
RobMeades | 0:44dd95724bc2 | 57 | # endif |
RobMeades | 0:44dd95724bc2 | 58 | # ifndef MBED_CONF_APP_ALT_PIN |
RobMeades | 0:44dd95724bc2 | 59 | # error "MBED_CONF_APP_ALT_PIN must be defined to run the SIM tests" |
RobMeades | 0:44dd95724bc2 | 60 | # endif |
RobMeades | 0:44dd95724bc2 | 61 | # ifndef MBED_CONF_APP_INCORRECT_PIN |
RobMeades | 0:44dd95724bc2 | 62 | # error "MBED_CONF_APP_INCORRECT_PIN must be defined to run the SIM tests" |
RobMeades | 0:44dd95724bc2 | 63 | # endif |
RobMeades | 0:44dd95724bc2 | 64 | #endif |
RobMeades | 0:44dd95724bc2 | 65 | |
RobMeades | 0:44dd95724bc2 | 66 | // The credentials of the SIM in the board. |
RobMeades | 0:44dd95724bc2 | 67 | #ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:44dd95724bc2 | 68 | // Note: if PIN is enabled on your SIM, or you wish to run the SIM PIN change |
RobMeades | 0:44dd95724bc2 | 69 | // tests, you must define the PIN for your SIM (see note above on using |
RobMeades | 0:44dd95724bc2 | 70 | // mbed_app.json to do so). |
RobMeades | 0:44dd95724bc2 | 71 | # define MBED_CONF_APP_DEFAULT_PIN "0000" |
RobMeades | 0:44dd95724bc2 | 72 | #endif |
RobMeades | 0:44dd95724bc2 | 73 | #ifndef MBED_CONF_APP_APN |
RobMeades | 0:44dd95724bc2 | 74 | # define MBED_CONF_APP_APN NULL |
RobMeades | 0:44dd95724bc2 | 75 | #endif |
RobMeades | 0:44dd95724bc2 | 76 | #ifndef MBED_CONF_APP_USERNAME |
RobMeades | 0:44dd95724bc2 | 77 | # define MBED_CONF_APP_USERNAME NULL |
RobMeades | 0:44dd95724bc2 | 78 | #endif |
RobMeades | 0:44dd95724bc2 | 79 | #ifndef MBED_CONF_APP_PASSWORD |
RobMeades | 0:44dd95724bc2 | 80 | # define MBED_CONF_APP_PASSWORD NULL |
RobMeades | 0:44dd95724bc2 | 81 | #endif |
RobMeades | 0:44dd95724bc2 | 82 | |
RobMeades | 0:44dd95724bc2 | 83 | // Alternate PIN to use during pin change testing |
RobMeades | 0:44dd95724bc2 | 84 | #ifndef MBED_CONF_APP_ALT_PIN |
RobMeades | 0:44dd95724bc2 | 85 | # define MBED_CONF_APP_ALT_PIN "9876" |
RobMeades | 0:44dd95724bc2 | 86 | #endif |
RobMeades | 0:44dd95724bc2 | 87 | |
RobMeades | 0:44dd95724bc2 | 88 | // A PIN that is definitely incorrect |
RobMeades | 0:44dd95724bc2 | 89 | #ifndef MBED_CONF_APP_INCORRECT_PIN |
RobMeades | 0:44dd95724bc2 | 90 | # define MBED_CONF_APP_INCORRECT_PIN "1530" |
RobMeades | 0:44dd95724bc2 | 91 | #endif |
RobMeades | 0:44dd95724bc2 | 92 | |
RobMeades | 0:44dd95724bc2 | 93 | // Servers and ports |
RobMeades | 0:44dd95724bc2 | 94 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 95 | # ifndef MBED_CONF_APP_ECHO_UDP_PORT |
RobMeades | 0:44dd95724bc2 | 96 | # error "MBED_CONF_APP_ECHO_UDP_PORT (the port on which your echo server echoes UDP packets) must be defined" |
RobMeades | 0:44dd95724bc2 | 97 | # endif |
RobMeades | 0:44dd95724bc2 | 98 | # ifndef MBED_CONF_APP_ECHO_TCP_PORT |
RobMeades | 0:44dd95724bc2 | 99 | # error "MBED_CONF_APP_ECHO_TCP_PORT (the port on which your echo server echoes TCP packets) must be defined" |
RobMeades | 0:44dd95724bc2 | 100 | # endif |
RobMeades | 0:44dd95724bc2 | 101 | #endif |
RobMeades | 0:44dd95724bc2 | 102 | |
RobMeades | 0:44dd95724bc2 | 103 | #ifndef MBED_CONF_APP_NTP_SERVER |
RobMeades | 0:44dd95724bc2 | 104 | # define MBED_CONF_APP_NTP_SERVER "2.pool.ntp.org" |
RobMeades | 0:44dd95724bc2 | 105 | #else |
RobMeades | 0:44dd95724bc2 | 106 | # ifndef MBED_CONF_APP_NTP_PORT |
RobMeades | 0:44dd95724bc2 | 107 | # error "MBED_CONF_APP_NTP_PORT must be defined if MBED_CONF_APP_NTP_SERVER is defined" |
RobMeades | 0:44dd95724bc2 | 108 | # endif |
RobMeades | 0:44dd95724bc2 | 109 | #endif |
RobMeades | 0:44dd95724bc2 | 110 | #ifndef MBED_CONF_APP_NTP_PORT |
RobMeades | 0:44dd95724bc2 | 111 | # define MBED_CONF_APP_NTP_PORT 123 |
RobMeades | 0:44dd95724bc2 | 112 | #endif |
RobMeades | 0:44dd95724bc2 | 113 | |
RobMeades | 0:44dd95724bc2 | 114 | #ifndef MBED_CONF_APP_LOCAL_PORT |
RobMeades | 0:44dd95724bc2 | 115 | # define MBED_CONF_APP_LOCAL_PORT 15 |
RobMeades | 0:44dd95724bc2 | 116 | #endif |
RobMeades | 0:44dd95724bc2 | 117 | |
RobMeades | 0:44dd95724bc2 | 118 | // UDP packet size limit for testing |
RobMeades | 0:44dd95724bc2 | 119 | #ifndef MBED_CONF_APP_UDP_MAX_PACKET_SIZE |
RobMeades | 0:44dd95724bc2 | 120 | # define MBED_CONF_APP_UDP_MAX_PACKET_SIZE 508 |
RobMeades | 0:44dd95724bc2 | 121 | #endif |
RobMeades | 0:44dd95724bc2 | 122 | |
RobMeades | 0:44dd95724bc2 | 123 | // TCP packet size limit for testing |
RobMeades | 0:44dd95724bc2 | 124 | #ifndef MBED_CONF_APP_MBED_CONF_APP_TCP_MAX_PACKET_SIZE |
RobMeades | 0:44dd95724bc2 | 125 | # define MBED_CONF_APP_TCP_MAX_PACKET_SIZE 1500 |
RobMeades | 0:44dd95724bc2 | 126 | #endif |
RobMeades | 0:44dd95724bc2 | 127 | |
RobMeades | 0:44dd95724bc2 | 128 | // The number of retries for UDP exchanges |
RobMeades | 0:44dd95724bc2 | 129 | #define NUM_UDP_RETRIES 5 |
RobMeades | 0:44dd95724bc2 | 130 | |
RobMeades | 0:44dd95724bc2 | 131 | // How long to wait for stuff to travel in the async tests |
RobMeades | 0:44dd95724bc2 | 132 | #define ASYNC_TEST_WAIT_TIME 10000 |
RobMeades | 0:44dd95724bc2 | 133 | |
RobMeades | 0:44dd95724bc2 | 134 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 135 | // PRIVATE VARIABLES |
RobMeades | 0:44dd95724bc2 | 136 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 137 | |
RobMeades | 0:44dd95724bc2 | 138 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 139 | // Lock for debug prints |
RobMeades | 0:44dd95724bc2 | 140 | static Mutex mtx; |
RobMeades | 0:44dd95724bc2 | 141 | #endif |
RobMeades | 0:44dd95724bc2 | 142 | |
RobMeades | 0:44dd95724bc2 | 143 | // An instance of the cellular interface |
RobMeades | 0:44dd95724bc2 | 144 | static UbloxPPPCellularInterface *interface = new UbloxPPPCellularInterface(MDMTXD, |
RobMeades | 0:44dd95724bc2 | 145 | MDMRXD, |
RobMeades | 0:44dd95724bc2 | 146 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:44dd95724bc2 | 147 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:44dd95724bc2 | 148 | |
RobMeades | 0:44dd95724bc2 | 149 | // Connection flag |
RobMeades | 0:44dd95724bc2 | 150 | static bool connection_has_gone_down = false; |
RobMeades | 0:44dd95724bc2 | 151 | |
RobMeades | 0:44dd95724bc2 | 152 | static const char send_data[] = "_____0000:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 153 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 154 | "_____0100:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 155 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 156 | "_____0200:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 157 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 158 | "_____0300:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 159 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 160 | "_____0400:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 161 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 162 | "_____0500:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 163 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 164 | "_____0600:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 165 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 166 | "_____0700:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 167 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 168 | "_____0800:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 169 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 170 | "_____0900:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 171 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 172 | "_____1000:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 173 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 174 | "_____1100:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 175 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 176 | "_____1200:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 177 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 178 | "_____1300:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 179 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 180 | "_____1400:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 181 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 182 | "_____1500:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 183 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 184 | "_____1600:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 185 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 186 | "_____1700:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 187 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 188 | "_____1800:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 189 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 190 | "_____1900:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 191 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 192 | "_____2000:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 193 | "01234567890123456789012345678901234567890123456789"; |
RobMeades | 0:44dd95724bc2 | 194 | |
RobMeades | 0:44dd95724bc2 | 195 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 196 | // PRIVATE FUNCTIONS |
RobMeades | 0:44dd95724bc2 | 197 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 198 | |
RobMeades | 0:44dd95724bc2 | 199 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 200 | // Locks for debug prints |
RobMeades | 0:44dd95724bc2 | 201 | static void lock() |
RobMeades | 0:44dd95724bc2 | 202 | { |
RobMeades | 0:44dd95724bc2 | 203 | mtx.lock(); |
RobMeades | 0:44dd95724bc2 | 204 | } |
RobMeades | 0:44dd95724bc2 | 205 | |
RobMeades | 0:44dd95724bc2 | 206 | static void unlock() |
RobMeades | 0:44dd95724bc2 | 207 | { |
RobMeades | 0:44dd95724bc2 | 208 | mtx.unlock(); |
RobMeades | 0:44dd95724bc2 | 209 | } |
RobMeades | 0:44dd95724bc2 | 210 | #endif |
RobMeades | 0:44dd95724bc2 | 211 | |
RobMeades | 0:44dd95724bc2 | 212 | // Callback in case the connection goes down |
RobMeades | 0:44dd95724bc2 | 213 | void connection_down_cb(nsapi_error_t err) |
RobMeades | 0:44dd95724bc2 | 214 | { |
RobMeades | 0:44dd95724bc2 | 215 | connection_has_gone_down = true; |
RobMeades | 0:44dd95724bc2 | 216 | } |
RobMeades | 0:44dd95724bc2 | 217 | |
RobMeades | 0:44dd95724bc2 | 218 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 219 | // Make sure that size is greater than 0 and no more than limit, |
RobMeades | 0:44dd95724bc2 | 220 | // useful since, when moduloing a very large number number, |
RobMeades | 0:44dd95724bc2 | 221 | // compilers sometimes screw up and produce a small *negative* |
RobMeades | 0:44dd95724bc2 | 222 | // number. Who knew? For example, GCC decided that |
RobMeades | 0:44dd95724bc2 | 223 | // 492318453 (0x1d582ef5) modulo 508 was -47 (0xffffffd1). |
RobMeades | 0:44dd95724bc2 | 224 | static int fix (int size, int limit) |
RobMeades | 0:44dd95724bc2 | 225 | { |
RobMeades | 0:44dd95724bc2 | 226 | if (size <= 0) { |
RobMeades | 0:44dd95724bc2 | 227 | size = limit / 2; // better than 1 |
RobMeades | 0:44dd95724bc2 | 228 | } else if (size > limit) { |
RobMeades | 0:44dd95724bc2 | 229 | size = limit; |
RobMeades | 0:44dd95724bc2 | 230 | } |
RobMeades | 0:44dd95724bc2 | 231 | return size; |
RobMeades | 0:44dd95724bc2 | 232 | } |
RobMeades | 0:44dd95724bc2 | 233 | |
RobMeades | 0:44dd95724bc2 | 234 | // Do a UDP socket echo test to a given host of a given packet size |
RobMeades | 0:44dd95724bc2 | 235 | static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size) |
RobMeades | 0:44dd95724bc2 | 236 | { |
RobMeades | 0:44dd95724bc2 | 237 | bool success = false; |
RobMeades | 0:44dd95724bc2 | 238 | void * recv_data = malloc (size); |
RobMeades | 0:44dd95724bc2 | 239 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:44dd95724bc2 | 240 | |
RobMeades | 0:44dd95724bc2 | 241 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:44dd95724bc2 | 242 | for (int x = 0; !success && (x < NUM_UDP_RETRIES); x++) { |
RobMeades | 0:44dd95724bc2 | 243 | tr_debug("Echo testing UDP packet size %d byte(s), try %d.", size, x + 1); |
RobMeades | 0:44dd95724bc2 | 244 | if ((sock->sendto(*host_address, (void*) send_data, size) == size) && |
RobMeades | 0:44dd95724bc2 | 245 | (sock->recvfrom(host_address, recv_data, size) == size)) { |
RobMeades | 0:44dd95724bc2 | 246 | TEST_ASSERT (memcmp(send_data, recv_data, size) == 0); |
RobMeades | 0:44dd95724bc2 | 247 | success = true; |
RobMeades | 0:44dd95724bc2 | 248 | } |
RobMeades | 0:44dd95724bc2 | 249 | } |
RobMeades | 0:44dd95724bc2 | 250 | TEST_ASSERT (success); |
RobMeades | 0:44dd95724bc2 | 251 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 252 | |
RobMeades | 0:44dd95724bc2 | 253 | free (recv_data); |
RobMeades | 0:44dd95724bc2 | 254 | } |
RobMeades | 0:44dd95724bc2 | 255 | |
RobMeades | 0:44dd95724bc2 | 256 | // Send an entire TCP data buffer until done |
RobMeades | 0:44dd95724bc2 | 257 | static int sendAll(TCPSocket *sock, const char *data, int size) |
RobMeades | 0:44dd95724bc2 | 258 | { |
RobMeades | 0:44dd95724bc2 | 259 | int x; |
RobMeades | 0:44dd95724bc2 | 260 | int count = 0; |
RobMeades | 0:44dd95724bc2 | 261 | Timer timer; |
RobMeades | 0:44dd95724bc2 | 262 | |
RobMeades | 0:44dd95724bc2 | 263 | timer.start(); |
RobMeades | 0:44dd95724bc2 | 264 | while ((count < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { |
RobMeades | 0:44dd95724bc2 | 265 | x = sock->send(data + count, size - count); |
RobMeades | 0:44dd95724bc2 | 266 | if (x > 0) { |
RobMeades | 0:44dd95724bc2 | 267 | count += x; |
RobMeades | 0:44dd95724bc2 | 268 | tr_debug("%d byte(s) sent, %d left to send.", count, size - count); |
RobMeades | 0:44dd95724bc2 | 269 | } |
RobMeades | 0:44dd95724bc2 | 270 | wait_ms(10); |
RobMeades | 0:44dd95724bc2 | 271 | } |
RobMeades | 0:44dd95724bc2 | 272 | timer.stop(); |
RobMeades | 0:44dd95724bc2 | 273 | |
RobMeades | 0:44dd95724bc2 | 274 | return count; |
RobMeades | 0:44dd95724bc2 | 275 | } |
RobMeades | 0:44dd95724bc2 | 276 | |
RobMeades | 0:44dd95724bc2 | 277 | // The asynchronous callback |
RobMeades | 0:44dd95724bc2 | 278 | static void async_cb(bool *callback_triggered) |
RobMeades | 0:44dd95724bc2 | 279 | { |
RobMeades | 0:44dd95724bc2 | 280 | |
RobMeades | 0:44dd95724bc2 | 281 | TEST_ASSERT (callback_triggered != NULL); |
RobMeades | 0:44dd95724bc2 | 282 | *callback_triggered = true; |
RobMeades | 0:44dd95724bc2 | 283 | } |
RobMeades | 0:44dd95724bc2 | 284 | |
RobMeades | 0:44dd95724bc2 | 285 | // Do a TCP echo using the asynchronous interface |
RobMeades | 0:44dd95724bc2 | 286 | static void do_tcp_echo_async(TCPSocket *sock, int size, bool *callback_triggered) |
RobMeades | 0:44dd95724bc2 | 287 | { |
RobMeades | 0:44dd95724bc2 | 288 | void * recv_data = malloc (size); |
RobMeades | 0:44dd95724bc2 | 289 | int recv_size = 0; |
RobMeades | 0:44dd95724bc2 | 290 | int x, y; |
RobMeades | 0:44dd95724bc2 | 291 | Timer timer; |
RobMeades | 0:44dd95724bc2 | 292 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:44dd95724bc2 | 293 | |
RobMeades | 0:44dd95724bc2 | 294 | *callback_triggered = false; |
RobMeades | 0:44dd95724bc2 | 295 | tr_debug("Echo testing TCP packet size %d byte(s) async.", size); |
RobMeades | 0:44dd95724bc2 | 296 | TEST_ASSERT (sendAll(sock, send_data, size) == size); |
RobMeades | 0:44dd95724bc2 | 297 | // Wait for all the echoed data to arrive |
RobMeades | 0:44dd95724bc2 | 298 | timer.start(); |
RobMeades | 0:44dd95724bc2 | 299 | while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { |
RobMeades | 0:44dd95724bc2 | 300 | if (*callback_triggered) { |
RobMeades | 0:44dd95724bc2 | 301 | *callback_triggered = false; |
RobMeades | 0:44dd95724bc2 | 302 | x = sock->recv((char *) recv_data + recv_size, size); |
RobMeades | 0:44dd95724bc2 | 303 | // IMPORTANT: this is different to the version in the AT DATA tests |
RobMeades | 0:44dd95724bc2 | 304 | // In the AT DATA case we know that the only reason the callback |
RobMeades | 0:44dd95724bc2 | 305 | // will be triggered is if there is received data. In the case |
RobMeades | 0:44dd95724bc2 | 306 | // of calling the LWIP implementation other things can also trigger |
RobMeades | 0:44dd95724bc2 | 307 | // it, so don't rely on there being any bytes to receive. |
RobMeades | 0:44dd95724bc2 | 308 | if (x > 0) { |
RobMeades | 0:44dd95724bc2 | 309 | recv_size += x; |
RobMeades | 0:44dd95724bc2 | 310 | tr_debug("%d byte(s) echoed back so far.", recv_size); |
RobMeades | 0:44dd95724bc2 | 311 | } |
RobMeades | 0:44dd95724bc2 | 312 | } |
RobMeades | 0:44dd95724bc2 | 313 | wait_ms(10); |
RobMeades | 0:44dd95724bc2 | 314 | } |
RobMeades | 0:44dd95724bc2 | 315 | TEST_ASSERT(recv_size == size); |
RobMeades | 0:44dd95724bc2 | 316 | y = memcmp(send_data, recv_data, size); |
RobMeades | 0:44dd95724bc2 | 317 | if (y != 0) { |
RobMeades | 0:44dd95724bc2 | 318 | tr_debug("Sent %d, |%*.*s|", size, size, size, send_data); |
RobMeades | 0:44dd95724bc2 | 319 | tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data); |
RobMeades | 0:44dd95724bc2 | 320 | TEST_ASSERT(false); |
RobMeades | 0:44dd95724bc2 | 321 | } |
RobMeades | 0:44dd95724bc2 | 322 | timer.stop(); |
RobMeades | 0:44dd95724bc2 | 323 | |
RobMeades | 0:44dd95724bc2 | 324 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 325 | |
RobMeades | 0:44dd95724bc2 | 326 | free (recv_data); |
RobMeades | 0:44dd95724bc2 | 327 | } |
RobMeades | 0:44dd95724bc2 | 328 | #endif |
RobMeades | 0:44dd95724bc2 | 329 | |
RobMeades | 0:44dd95724bc2 | 330 | // Get NTP time |
RobMeades | 0:44dd95724bc2 | 331 | static void do_ntp(UbloxPPPCellularInterface *interface) |
RobMeades | 0:44dd95724bc2 | 332 | { |
RobMeades | 0:44dd95724bc2 | 333 | char ntp_values[48] = { 0 }; |
RobMeades | 0:44dd95724bc2 | 334 | time_t timestamp = 0; |
RobMeades | 0:44dd95724bc2 | 335 | struct tm *localTime; |
RobMeades | 0:44dd95724bc2 | 336 | char timeString[25]; |
RobMeades | 0:44dd95724bc2 | 337 | time_t TIME1970 = 2208988800U; |
RobMeades | 0:44dd95724bc2 | 338 | int len; |
RobMeades | 0:44dd95724bc2 | 339 | bool comms_done = false; |
RobMeades | 0:44dd95724bc2 | 340 | |
RobMeades | 0:44dd95724bc2 | 341 | ntp_values[0] = '\x1b'; |
RobMeades | 0:44dd95724bc2 | 342 | |
RobMeades | 0:44dd95724bc2 | 343 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:44dd95724bc2 | 344 | for (unsigned int x = 0; !comms_done && (x < NUM_UDP_RETRIES); x++) { |
RobMeades | 0:44dd95724bc2 | 345 | sock->sendto(ntp_address, (void*) ntp_values, sizeof(ntp_values)); |
RobMeades | 0:44dd95724bc2 | 346 | len = sock->recvfrom(&ntp_address, (void*) ntp_values, sizeof(ntp_values)); |
RobMeades | 0:44dd95724bc2 | 347 | if (len > 0) { |
RobMeades | 0:44dd95724bc2 | 348 | comms_done = true; |
RobMeades | 0:44dd95724bc2 | 349 | } |
RobMeades | 0:44dd95724bc2 | 350 | } |
RobMeades | 0:44dd95724bc2 | 351 | TEST_ASSERT (comms_done); |
RobMeades | 0:44dd95724bc2 | 352 | |
RobMeades | 0:44dd95724bc2 | 353 | tr_debug("UDP: %d byte(s) returned by NTP server.", len); |
RobMeades | 0:44dd95724bc2 | 354 | if (len >= 43) { |
RobMeades | 0:44dd95724bc2 | 355 | timestamp |= ((int) *(ntp_values + 40)) << 24; |
RobMeades | 0:44dd95724bc2 | 356 | timestamp |= ((int) *(ntp_values + 41)) << 16; |
RobMeades | 0:44dd95724bc2 | 357 | timestamp |= ((int) *(ntp_values + 42)) << 8; |
RobMeades | 0:44dd95724bc2 | 358 | timestamp |= ((int) *(ntp_values + 43)); |
RobMeades | 0:44dd95724bc2 | 359 | timestamp -= TIME1970; |
RobMeades | 0:44dd95724bc2 | 360 | srand (timestamp); |
RobMeades | 0:44dd95724bc2 | 361 | tr_debug("srand() called"); |
RobMeades | 0:44dd95724bc2 | 362 | localTime = localtime(×tamp); |
RobMeades | 0:44dd95724bc2 | 363 | if (localTime) { |
RobMeades | 0:44dd95724bc2 | 364 | if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { |
RobMeades | 0:44dd95724bc2 | 365 | printf("NTP timestamp is %s.\n", timeString); |
RobMeades | 0:44dd95724bc2 | 366 | } |
RobMeades | 0:44dd95724bc2 | 367 | } |
RobMeades | 0:44dd95724bc2 | 368 | } |
RobMeades | 0:44dd95724bc2 | 369 | } |
RobMeades | 0:44dd95724bc2 | 370 | |
RobMeades | 0:44dd95724bc2 | 371 | // Use a connection, checking that it is good |
RobMeades | 0:44dd95724bc2 | 372 | static void use_connection(UbloxPPPCellularInterface *interface) |
RobMeades | 0:44dd95724bc2 | 373 | { |
RobMeades | 0:44dd95724bc2 | 374 | const char * ip_address = interface->get_ip_address(); |
RobMeades | 0:44dd95724bc2 | 375 | const char * net_mask = interface->get_netmask(); |
RobMeades | 0:44dd95724bc2 | 376 | const char * gateway = interface->get_gateway(); |
RobMeades | 0:44dd95724bc2 | 377 | |
RobMeades | 0:44dd95724bc2 | 378 | TEST_ASSERT(interface->is_connected()); |
RobMeades | 0:44dd95724bc2 | 379 | |
RobMeades | 0:44dd95724bc2 | 380 | TEST_ASSERT(ip_address != NULL); |
RobMeades | 0:44dd95724bc2 | 381 | tr_debug ("IP address %s.", ip_address); |
RobMeades | 0:44dd95724bc2 | 382 | TEST_ASSERT(net_mask != NULL); |
RobMeades | 0:44dd95724bc2 | 383 | tr_debug ("Net mask %s.", net_mask); |
RobMeades | 0:44dd95724bc2 | 384 | TEST_ASSERT(gateway != NULL); |
RobMeades | 0:44dd95724bc2 | 385 | tr_debug ("Gateway %s.", gateway); |
RobMeades | 0:44dd95724bc2 | 386 | |
RobMeades | 0:44dd95724bc2 | 387 | do_ntp(interface); |
RobMeades | 0:44dd95724bc2 | 388 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 389 | } |
RobMeades | 0:44dd95724bc2 | 390 | |
RobMeades | 0:44dd95724bc2 | 391 | // Drop a connection and check that it has dropped |
RobMeades | 0:44dd95724bc2 | 392 | static void drop_connection(UbloxPPPCellularInterface *interface) |
RobMeades | 0:44dd95724bc2 | 393 | { |
RobMeades | 0:44dd95724bc2 | 394 | TEST_ASSERT(interface->disconnect() == 0); |
RobMeades | 0:44dd95724bc2 | 395 | TEST_ASSERT(connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 396 | connection_has_gone_down = false; |
RobMeades | 0:44dd95724bc2 | 397 | TEST_ASSERT(!interface->is_connected()); |
RobMeades | 0:44dd95724bc2 | 398 | } |
RobMeades | 0:44dd95724bc2 | 399 | |
RobMeades | 0:44dd95724bc2 | 400 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 401 | // TESTS |
RobMeades | 0:44dd95724bc2 | 402 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 403 | |
RobMeades | 0:44dd95724bc2 | 404 | // Call srand() using the NTP server |
RobMeades | 0:44dd95724bc2 | 405 | void test_set_randomise() { |
RobMeades | 0:44dd95724bc2 | 406 | UDPSocket sock; |
RobMeades | 0:44dd95724bc2 | 407 | SocketAddress host_address; |
RobMeades | 0:44dd95724bc2 | 408 | |
RobMeades | 0:44dd95724bc2 | 409 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 410 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 411 | do_ntp(interface); |
RobMeades | 0:44dd95724bc2 | 412 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 413 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 414 | } |
RobMeades | 0:44dd95724bc2 | 415 | |
RobMeades | 0:44dd95724bc2 | 416 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 417 | // Test UDP data exchange |
RobMeades | 0:44dd95724bc2 | 418 | void test_udp_echo() { |
RobMeades | 0:44dd95724bc2 | 419 | UDPSocket sock; |
RobMeades | 0:44dd95724bc2 | 420 | SocketAddress host_address; |
RobMeades | 0:44dd95724bc2 | 421 | int x; |
RobMeades | 0:44dd95724bc2 | 422 | int size; |
RobMeades | 0:44dd95724bc2 | 423 | |
RobMeades | 0:44dd95724bc2 | 424 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 425 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 426 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 427 | |
RobMeades | 0:44dd95724bc2 | 428 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:44dd95724bc2 | 429 | host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); |
RobMeades | 0:44dd95724bc2 | 430 | |
RobMeades | 0:44dd95724bc2 | 431 | tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:44dd95724bc2 | 432 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:44dd95724bc2 | 433 | |
RobMeades | 0:44dd95724bc2 | 434 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:44dd95724bc2 | 435 | |
RobMeades | 0:44dd95724bc2 | 436 | sock.set_timeout(10000); |
RobMeades | 0:44dd95724bc2 | 437 | |
RobMeades | 0:44dd95724bc2 | 438 | // Test min, max, and some random sizes in-between |
RobMeades | 0:44dd95724bc2 | 439 | do_udp_echo(&sock, &host_address, 1); |
RobMeades | 0:44dd95724bc2 | 440 | do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 441 | for (x = 0; x < 10; x++) { |
RobMeades | 0:44dd95724bc2 | 442 | size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1; |
RobMeades | 0:44dd95724bc2 | 443 | size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 444 | do_udp_echo(&sock, &host_address, size); |
RobMeades | 0:44dd95724bc2 | 445 | } |
RobMeades | 0:44dd95724bc2 | 446 | |
RobMeades | 0:44dd95724bc2 | 447 | sock.close(); |
RobMeades | 0:44dd95724bc2 | 448 | |
RobMeades | 0:44dd95724bc2 | 449 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 450 | |
RobMeades | 0:44dd95724bc2 | 451 | tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.", x, |
RobMeades | 0:44dd95724bc2 | 452 | MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 453 | } |
RobMeades | 0:44dd95724bc2 | 454 | |
RobMeades | 0:44dd95724bc2 | 455 | // Test TCP data exchange via the asynchronous sigio() mechanism |
RobMeades | 0:44dd95724bc2 | 456 | void test_tcp_echo_async() { |
RobMeades | 0:44dd95724bc2 | 457 | TCPSocket sock; |
RobMeades | 0:44dd95724bc2 | 458 | SocketAddress host_address; |
RobMeades | 0:44dd95724bc2 | 459 | bool callback_triggered = false; |
RobMeades | 0:44dd95724bc2 | 460 | int x; |
RobMeades | 0:44dd95724bc2 | 461 | int size; |
RobMeades | 0:44dd95724bc2 | 462 | |
RobMeades | 0:44dd95724bc2 | 463 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 464 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 465 | |
RobMeades | 0:44dd95724bc2 | 466 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:44dd95724bc2 | 467 | host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT); |
RobMeades | 0:44dd95724bc2 | 468 | |
RobMeades | 0:44dd95724bc2 | 469 | tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:44dd95724bc2 | 470 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:44dd95724bc2 | 471 | |
RobMeades | 0:44dd95724bc2 | 472 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:44dd95724bc2 | 473 | |
RobMeades | 0:44dd95724bc2 | 474 | // Set up the async callback and set the timeout to zero |
RobMeades | 0:44dd95724bc2 | 475 | sock.sigio(callback(async_cb, &callback_triggered)); |
RobMeades | 0:44dd95724bc2 | 476 | sock.set_timeout(0); |
RobMeades | 0:44dd95724bc2 | 477 | |
RobMeades | 0:44dd95724bc2 | 478 | TEST_ASSERT(sock.connect(host_address) == 0); |
RobMeades | 0:44dd95724bc2 | 479 | // Test min, max, and some random sizes in-between |
RobMeades | 0:44dd95724bc2 | 480 | do_tcp_echo_async(&sock, 1, &callback_triggered); |
RobMeades | 0:44dd95724bc2 | 481 | do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, &callback_triggered); |
RobMeades | 0:44dd95724bc2 | 482 | for (x = 0; x < 10; x++) { |
RobMeades | 0:44dd95724bc2 | 483 | size = (rand() % MBED_CONF_APP_TCP_MAX_PACKET_SIZE) + 1; |
RobMeades | 0:44dd95724bc2 | 484 | size = fix(size, MBED_CONF_APP_TCP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 485 | do_tcp_echo_async(&sock, size, &callback_triggered); |
RobMeades | 0:44dd95724bc2 | 486 | } |
RobMeades | 0:44dd95724bc2 | 487 | |
RobMeades | 0:44dd95724bc2 | 488 | sock.close(); |
RobMeades | 0:44dd95724bc2 | 489 | |
RobMeades | 0:44dd95724bc2 | 490 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 491 | |
RobMeades | 0:44dd95724bc2 | 492 | tr_debug("%d TCP packets of size up to %d byte(s) echoed asynchronously and successfully.", |
RobMeades | 0:44dd95724bc2 | 493 | x, MBED_CONF_APP_TCP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 494 | } |
RobMeades | 0:44dd95724bc2 | 495 | #endif |
RobMeades | 0:44dd95724bc2 | 496 | |
RobMeades | 0:44dd95724bc2 | 497 | // Connect with credentials included in the connect request |
RobMeades | 0:44dd95724bc2 | 498 | void test_connect_credentials() { |
RobMeades | 0:44dd95724bc2 | 499 | |
RobMeades | 0:44dd95724bc2 | 500 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 501 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 502 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 503 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 504 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 505 | } |
RobMeades | 0:44dd95724bc2 | 506 | |
RobMeades | 0:44dd95724bc2 | 507 | // Test with credentials preset |
RobMeades | 0:44dd95724bc2 | 508 | void test_connect_preset_credentials() { |
RobMeades | 0:44dd95724bc2 | 509 | |
RobMeades | 0:44dd95724bc2 | 510 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 511 | TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN)); |
RobMeades | 0:44dd95724bc2 | 512 | interface->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 513 | MBED_CONF_APP_PASSWORD); |
RobMeades | 0:44dd95724bc2 | 514 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN) == 0); |
RobMeades | 0:44dd95724bc2 | 515 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 516 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 517 | } |
RobMeades | 0:44dd95724bc2 | 518 | |
RobMeades | 0:44dd95724bc2 | 519 | // Test adding and using a SIM pin, then removing it, using the pending |
RobMeades | 0:44dd95724bc2 | 520 | // mechanism where the change doesn't occur until connect() is called |
RobMeades | 0:44dd95724bc2 | 521 | void test_check_sim_pin_pending() { |
RobMeades | 0:44dd95724bc2 | 522 | |
RobMeades | 0:44dd95724bc2 | 523 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 524 | |
RobMeades | 0:44dd95724bc2 | 525 | // Enable PIN checking (which will use the current PIN) |
RobMeades | 0:44dd95724bc2 | 526 | // and also flag that the PIN should be changed to MBED_CONF_APP_ALT_PIN, |
RobMeades | 0:44dd95724bc2 | 527 | // then try connecting |
RobMeades | 0:44dd95724bc2 | 528 | interface->sim_pin_check_enable(true); |
RobMeades | 0:44dd95724bc2 | 529 | interface->change_sim_pin(MBED_CONF_APP_ALT_PIN); |
RobMeades | 0:44dd95724bc2 | 530 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 531 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 532 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 533 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 534 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 535 | |
RobMeades | 0:44dd95724bc2 | 536 | // Now change the PIN back to what it was before |
RobMeades | 0:44dd95724bc2 | 537 | interface->change_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 538 | TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 539 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 540 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 541 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 542 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 543 | |
RobMeades | 0:44dd95724bc2 | 544 | // Check that it was changed back, and this time |
RobMeades | 0:44dd95724bc2 | 545 | // use the other way of entering the PIN |
RobMeades | 0:44dd95724bc2 | 546 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 547 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 548 | MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 549 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 550 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 551 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 552 | |
RobMeades | 0:44dd95724bc2 | 553 | // Remove PIN checking again and check that it no |
RobMeades | 0:44dd95724bc2 | 554 | // longer matters what the PIN is |
RobMeades | 0:44dd95724bc2 | 555 | interface->sim_pin_check_enable(false); |
RobMeades | 0:44dd95724bc2 | 556 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 557 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 558 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 559 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 560 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 561 | TEST_ASSERT(interface->init(NULL)); |
RobMeades | 0:44dd95724bc2 | 562 | TEST_ASSERT(interface->connect(MBED_CONF_APP_INCORRECT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 563 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 564 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 565 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 566 | |
RobMeades | 0:44dd95724bc2 | 567 | // Put the SIM pin back to the correct value for any subsequent tests |
RobMeades | 0:44dd95724bc2 | 568 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 569 | } |
RobMeades | 0:44dd95724bc2 | 570 | |
RobMeades | 0:44dd95724bc2 | 571 | // Test adding and using a SIM pin, then removing it, using the immediate |
RobMeades | 0:44dd95724bc2 | 572 | // mechanism |
RobMeades | 0:44dd95724bc2 | 573 | void test_check_sim_pin_immediate() { |
RobMeades | 0:44dd95724bc2 | 574 | |
RobMeades | 0:44dd95724bc2 | 575 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 576 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 577 | |
RobMeades | 0:44dd95724bc2 | 578 | // Enable PIN checking (which will use the current PIN), change |
RobMeades | 0:44dd95724bc2 | 579 | // the PIN to MBED_CONF_APP_ALT_PIN, then try connecting after powering on and |
RobMeades | 0:44dd95724bc2 | 580 | // off the modem |
RobMeades | 0:44dd95724bc2 | 581 | interface->set_sim_pin_check(true, true, MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 582 | interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN, true); |
RobMeades | 0:44dd95724bc2 | 583 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 584 | TEST_ASSERT(interface->init(NULL)); |
RobMeades | 0:44dd95724bc2 | 585 | TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 586 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 587 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 588 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 589 | |
RobMeades | 0:44dd95724bc2 | 590 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 591 | |
RobMeades | 0:44dd95724bc2 | 592 | // Now change the PIN back to what it was before |
RobMeades | 0:44dd95724bc2 | 593 | interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN, true); |
RobMeades | 0:44dd95724bc2 | 594 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 595 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 596 | TEST_ASSERT(interface->init(NULL)); |
RobMeades | 0:44dd95724bc2 | 597 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 598 | MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 599 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 600 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 601 | |
RobMeades | 0:44dd95724bc2 | 602 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 603 | |
RobMeades | 0:44dd95724bc2 | 604 | // Remove PIN checking again and check that it no |
RobMeades | 0:44dd95724bc2 | 605 | // longer matters what the PIN is |
RobMeades | 0:44dd95724bc2 | 606 | interface->set_sim_pin_check(false, true); |
RobMeades | 0:44dd95724bc2 | 607 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 608 | TEST_ASSERT(interface->init(MBED_CONF_APP_INCORRECT_PIN)); |
RobMeades | 0:44dd95724bc2 | 609 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 610 | MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 611 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 612 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 613 | |
RobMeades | 0:44dd95724bc2 | 614 | // Put the SIM pin back to the correct value for any subsequent tests |
RobMeades | 0:44dd95724bc2 | 615 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 616 | } |
RobMeades | 0:44dd95724bc2 | 617 | |
RobMeades | 0:44dd95724bc2 | 618 | // Test being able to connect with a local instance of the driver |
RobMeades | 0:44dd95724bc2 | 619 | // NOTE: since this local instance will fiddle with bits of HW that the |
RobMeades | 0:44dd95724bc2 | 620 | // static instance thought it owned, the static instance will no longer |
RobMeades | 0:44dd95724bc2 | 621 | // work afterwards, hence this must be run as the last test in the list |
RobMeades | 0:44dd95724bc2 | 622 | void test_connect_local_instance_last_test() { |
RobMeades | 0:44dd95724bc2 | 623 | |
RobMeades | 0:44dd95724bc2 | 624 | UbloxPPPCellularInterface *pLocalInterface = NULL; |
RobMeades | 0:44dd95724bc2 | 625 | |
RobMeades | 0:44dd95724bc2 | 626 | pLocalInterface = new UbloxPPPCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:44dd95724bc2 | 627 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:44dd95724bc2 | 628 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:44dd95724bc2 | 629 | pLocalInterface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 630 | |
RobMeades | 0:44dd95724bc2 | 631 | TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 632 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 633 | use_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 634 | drop_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 635 | delete pLocalInterface; |
RobMeades | 0:44dd95724bc2 | 636 | |
RobMeades | 0:44dd95724bc2 | 637 | pLocalInterface = new UbloxPPPCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:44dd95724bc2 | 638 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:44dd95724bc2 | 639 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:44dd95724bc2 | 640 | pLocalInterface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 641 | |
RobMeades | 0:44dd95724bc2 | 642 | TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 643 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 644 | use_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 645 | drop_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 646 | delete pLocalInterface; |
RobMeades | 0:44dd95724bc2 | 647 | } |
RobMeades | 0:44dd95724bc2 | 648 | |
RobMeades | 0:44dd95724bc2 | 649 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 650 | // TEST ENVIRONMENT |
RobMeades | 0:44dd95724bc2 | 651 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 652 | |
RobMeades | 0:44dd95724bc2 | 653 | // Setup the test environment |
RobMeades | 0:44dd95724bc2 | 654 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:44dd95724bc2 | 655 | // Setup Greentea with a timeout |
RobMeades | 0:44dd95724bc2 | 656 | GREENTEA_SETUP(600, "default_auto"); |
RobMeades | 0:44dd95724bc2 | 657 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:44dd95724bc2 | 658 | } |
RobMeades | 0:44dd95724bc2 | 659 | |
RobMeades | 0:44dd95724bc2 | 660 | // IMPORTANT!!! if you make a change to the tests here you should |
RobMeades | 0:44dd95724bc2 | 661 | // check whether the same change should be made to the tests under |
RobMeades | 0:44dd95724bc2 | 662 | // the AT DATA driver. |
RobMeades | 0:44dd95724bc2 | 663 | |
RobMeades | 0:44dd95724bc2 | 664 | // Test cases |
RobMeades | 0:44dd95724bc2 | 665 | Case cases[] = { |
RobMeades | 0:44dd95724bc2 | 666 | Case("Set randomise", test_set_randomise), |
RobMeades | 0:44dd95724bc2 | 667 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 668 | Case("UDP echo test", test_udp_echo), |
RobMeades | 0:44dd95724bc2 | 669 | # if MBED_CONF_LWIP_TCP_ENABLED |
RobMeades | 0:44dd95724bc2 | 670 | Case("TCP async echo test", test_tcp_echo_async), |
RobMeades | 0:44dd95724bc2 | 671 | # endif |
RobMeades | 0:44dd95724bc2 | 672 | #endif |
RobMeades | 0:44dd95724bc2 | 673 | Case("Connect with credentials", test_connect_credentials), |
RobMeades | 0:44dd95724bc2 | 674 | Case("Connect with preset credentials", test_connect_preset_credentials), |
RobMeades | 0:44dd95724bc2 | 675 | #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:44dd95724bc2 | 676 | Case("Check SIM pin, pending", test_check_sim_pin_pending), |
RobMeades | 0:44dd95724bc2 | 677 | Case("Check SIM pin, immediate", test_check_sim_pin_immediate), |
RobMeades | 0:44dd95724bc2 | 678 | #endif |
RobMeades | 0:44dd95724bc2 | 679 | #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 for this |
RobMeades | 0:44dd95724bc2 | 680 | Case("Connect using local instance, must be last test", test_connect_local_instance_last_test) |
RobMeades | 0:44dd95724bc2 | 681 | #endif |
RobMeades | 0:44dd95724bc2 | 682 | }; |
RobMeades | 0:44dd95724bc2 | 683 | |
RobMeades | 0:44dd95724bc2 | 684 | Specification specification(test_setup, cases); |
RobMeades | 0:44dd95724bc2 | 685 | |
RobMeades | 0:44dd95724bc2 | 686 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 687 | // MAIN |
RobMeades | 0:44dd95724bc2 | 688 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 689 | |
RobMeades | 0:44dd95724bc2 | 690 | int main() { |
RobMeades | 0:44dd95724bc2 | 691 | |
RobMeades | 0:44dd95724bc2 | 692 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 693 | mbed_trace_init(); |
RobMeades | 0:44dd95724bc2 | 694 | |
RobMeades | 0:44dd95724bc2 | 695 | mbed_trace_mutex_wait_function_set(lock); |
RobMeades | 0:44dd95724bc2 | 696 | mbed_trace_mutex_release_function_set(unlock); |
RobMeades | 0:44dd95724bc2 | 697 | |
RobMeades | 0:44dd95724bc2 | 698 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 699 | #endif |
RobMeades | 0:44dd95724bc2 | 700 | |
RobMeades | 0:44dd95724bc2 | 701 | // Run tests |
RobMeades | 0:44dd95724bc2 | 702 | return !Harness::run(specification); |
RobMeades | 0:44dd95724bc2 | 703 | } |
RobMeades | 0:44dd95724bc2 | 704 | |
RobMeades | 0:44dd95724bc2 | 705 | #else |
RobMeades | 0:44dd95724bc2 | 706 | |
RobMeades | 0:44dd95724bc2 | 707 | // This looks a bit peculiar. These tests should only be compiled if LWIP is included and PPP support is enabled. |
RobMeades | 0:44dd95724bc2 | 708 | // The way the "mbed test" command, which parses these files, does its filtering is by looking for the pattern: |
RobMeades | 0:44dd95724bc2 | 709 | // |
RobMeades | 0:44dd95724bc2 | 710 | // #ifndef THING |
RobMeades | 0:44dd95724bc2 | 711 | // #error [NOT_SUPPORTED] this test can only be used if THING is defined |
RobMeades | 0:44dd95724bc2 | 712 | // endif |
RobMeades | 0:44dd95724bc2 | 713 | // |
RobMeades | 0:44dd95724bc2 | 714 | // So if THING is not defined, this test will not even be compiled. However, the only compilation switch which |
RobMeades | 0:44dd95724bc2 | 715 | // is visible here for PPP being enabled or not is NSAPI_PPP_AVAILABLE, which is 0 if PPP is not enabled (the default) |
RobMeades | 0:44dd95724bc2 | 716 | // or 1 if PPP is enabled. There is NO visible compilation switch which is or isn't _defined_ when PPP is or |
RobMeades | 0:44dd95724bc2 | 717 | // isn't enabled. |
RobMeades | 0:44dd95724bc2 | 718 | // |
RobMeades | 0:44dd95724bc2 | 719 | // Instead, here is a dummy test body which clearly states that these tests are irrelevant 'cos there's no PPP. |
RobMeades | 0:44dd95724bc2 | 720 | |
RobMeades | 0:44dd95724bc2 | 721 | void dummy() { |
RobMeades | 0:44dd95724bc2 | 722 | } |
RobMeades | 0:44dd95724bc2 | 723 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:44dd95724bc2 | 724 | GREENTEA_SETUP(10, "default_auto"); |
RobMeades | 0:44dd95724bc2 | 725 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:44dd95724bc2 | 726 | } |
RobMeades | 0:44dd95724bc2 | 727 | Case cases[] = { |
RobMeades | 0:44dd95724bc2 | 728 | Case("No PPP, no tests to run", dummy) |
RobMeades | 0:44dd95724bc2 | 729 | }; |
RobMeades | 0:44dd95724bc2 | 730 | Specification specification(test_setup, cases); |
RobMeades | 0:44dd95724bc2 | 731 | |
RobMeades | 0:44dd95724bc2 | 732 | int main() { |
RobMeades | 0:44dd95724bc2 | 733 | return !Harness::run(specification);; |
RobMeades | 0:44dd95724bc2 | 734 | } |
RobMeades | 0:44dd95724bc2 | 735 | |
RobMeades | 0:44dd95724bc2 | 736 | #endif |
RobMeades | 0:44dd95724bc2 | 737 | |
RobMeades | 0:44dd95724bc2 | 738 | // End Of File |
RobMeades | 0:44dd95724bc2 | 739 |