u-blox / ublox-at-cellular-interface-ext

Dependencies:   ublox-at-cellular-interface

Dependents:   example-ublox-at-cellular-interface-ext HelloMQTT ublox_new_driver_test example-ublox-at-cellular-interface-ext ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "greentea-client/test_env.h"
00003 #include "unity.h"
00004 #include "utest.h"
00005 #include "UbloxATCellularInterfaceExt.h"
00006 #include "UDPSocket.h"
00007 #ifdef FEATURE_COMMON_PAL
00008 #include "mbed_trace.h"
00009 #define TRACE_GROUP "TEST"
00010 #else
00011 #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__)
00012 #define tr_info(format, ...)  debug(format "\n", ## __VA_ARGS__)
00013 #define tr_warn(format, ...)  debug(format "\n", ## __VA_ARGS__)
00014 #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__)
00015 #endif
00016 
00017 using namespace utest::v1;
00018 
00019 // ----------------------------------------------------------------
00020 // COMPILE-TIME MACROS
00021 // ----------------------------------------------------------------
00022 
00023 // These macros can be overridden with an mbed_app.json file and
00024 // contents of the following form:
00025 //
00026 //{
00027 //    "config": {
00028 //        "apn": {
00029 //            "value": "\"my_apn\""
00030 //        }
00031 //}
00032 
00033 // Whether debug trace is on
00034 #ifndef MBED_CONF_APP_DEBUG_ON
00035 # define MBED_CONF_APP_DEBUG_ON false
00036 #endif
00037 
00038 // The credentials of the SIM in the board.
00039 #ifndef MBED_CONF_APP_DEFAULT_PIN
00040 // Note: if PIN is enabled on your SIM, you must define the PIN
00041 // for your SIM jere (e.g. using mbed_app.json to do so).
00042 # define MBED_CONF_APP_DEFAULT_PIN "0000"
00043 #endif
00044 
00045 // Network credentials.
00046 #ifndef MBED_CONF_APP_APN
00047 # define MBED_CONF_APP_APN         NULL
00048 #endif
00049 #ifndef MBED_CONF_APP_USERNAME
00050 # define MBED_CONF_APP_USERNAME    NULL
00051 #endif
00052 #ifndef MBED_CONF_APP_PASSWORD
00053 # define MBED_CONF_APP_PASSWORD    NULL
00054 #endif
00055 
00056 // The time to wait for a HTTP command to complete
00057 #ifndef MBED_CONF_APP_HTTP_TIMEOUT
00058 #define HTTP_TIMEOUT  10000
00059 #else
00060 #define HTTP_TIMEOUT  MBED_CONF_APP_HTTP_TIMEOUT
00061 #endif
00062 
00063 // The HTTP echo server, as described in the
00064 // first answer here:
00065 // http://stackoverflow.com/questions/5725430/http-test-server-that-accepts-get-post-calls
00066 // !!! IMPORTANT: this test relies on that server behaving in the same way forever !!!
00067 #define HTTP_ECHO_SERVER "httpbin.org"
00068 
00069 // The size of the test file
00070 #define TEST_FILE_SIZE 100
00071 
00072 // The maximum number of HTTP profiles
00073 #define MAX_PROFILES 4
00074 
00075 // ----------------------------------------------------------------
00076 // PRIVATE VARIABLES
00077 // ----------------------------------------------------------------
00078 
00079 #ifdef FEATURE_COMMON_PAL
00080 // Lock for debug prints
00081 static Mutex mtx;
00082 #endif
00083 
00084 // An instance of the cellular interface
00085 static UbloxATCellularInterfaceExt *pDriver =
00086        new UbloxATCellularInterfaceExt(MDMTXD, MDMRXD,
00087                                        MBED_CONF_UBLOX_CELL_BAUD_RATE,
00088                                        MBED_CONF_APP_DEBUG_ON);
00089 // A few buffers for general use
00090 static char buf[2048];
00091 static char buf1[sizeof(buf)];
00092 
00093 // ----------------------------------------------------------------
00094 // PRIVATE FUNCTIONS
00095 // ----------------------------------------------------------------
00096 
00097 #ifdef FEATURE_COMMON_PAL
00098 // Locks for debug prints
00099 static void lock()
00100 {
00101     mtx.lock();
00102 }
00103 
00104 static void unlock()
00105 {
00106     mtx.unlock();
00107 }
00108 #endif
00109 
00110 // ----------------------------------------------------------------
00111 // TESTS
00112 // ----------------------------------------------------------------
00113 
00114 // Test HTTP commands
00115 void test_http_cmd() {
00116     int profile;
00117     char * pData;
00118 #ifdef TARGET_UBLOX_C030_R41XM
00119     int mno_profile;
00120     if (pDriver->init(MBED_CONF_APP_DEFAULT_PIN) == false) //init can return false if profile set is SW_DEFAULT
00121     {
00122         TEST_ASSERT(pDriver->get_mno_profile(&mno_profile));
00123         if (mno_profile == UbloxATCellularInterface::SW_DEFAULT) {
00124             TEST_ASSERT(pDriver->set_mno_profile(UbloxATCellularInterface::STANDARD_EU));
00125             TEST_ASSERT(pDriver->reboot_modem());
00126             tr_debug("Reboot successful\n");
00127             ThisThread::sleep_for(5000);
00128         }
00129     }
00130     TEST_ASSERT(pDriver->init(MBED_CONF_APP_DEFAULT_PIN));
00131     TEST_ASSERT(pDriver->disable_power_saving_mode());
00132 #endif
00133     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00134                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00135 
00136     profile = pDriver->httpAllocProfile();
00137     TEST_ASSERT(profile >= 0);
00138 
00139     pDriver->httpSetTimeout(profile, HTTP_TIMEOUT);
00140 
00141     // Set up the server to talk to
00142     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER));
00143 
00144     // Check HTTP head request
00145     memset(buf, 0, sizeof (buf));
00146     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_HEAD,
00147                                      "/headers",
00148                                      NULL, NULL, 0, NULL,
00149                                      buf, sizeof (buf)) == NULL);
00150     tr_debug("Received: %s", buf);
00151     TEST_ASSERT(strstr(buf, "Content-Length:") != NULL);
00152 
00153     // Check HTTP get request
00154     memset(buf, 0, sizeof (buf));
00155     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00156                                      "/get",
00157                                      NULL, NULL, 0, NULL,
00158                                      buf, sizeof (buf)) == NULL);
00159     tr_debug("Received: %s", buf);
00160     TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL);
00161 
00162     // Check HTTP delete request
00163     memset(buf, 0, sizeof (buf));
00164     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_DELETE,
00165                                      "/delete",
00166                                      NULL, NULL, 0, NULL,
00167                                      buf, sizeof (buf)) == NULL);
00168     tr_debug("Received: %s", buf);
00169     TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL);
00170 
00171     // Check HTTP put request (this will fail as the echo server doesn't support it)
00172     memset(buf, 0, sizeof (buf));
00173     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_PUT,
00174                                      "/put",
00175                                      NULL, NULL, 0, NULL,
00176                                      buf, sizeof (buf)) != NULL);
00177 
00178     // Check HTTP post request with data
00179     memset(buf, 0, sizeof (buf));
00180     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_POST_DATA,
00181                                      "/post",
00182                                      NULL, "formData=0123456789", 0, NULL,
00183                                      buf, sizeof (buf)) == NULL);
00184     tr_debug("Received: %s", buf);
00185     TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL);
00186 
00187     // Check HTTP post request with a file, also checking that writing the response
00188     // to a named file works
00189     for (int x = 0; x < TEST_FILE_SIZE; x++) {
00190         buf[x] = (x % 10) + 0x30;
00191     }
00192     pDriver->delFile("post_test.txt");
00193     TEST_ASSERT(pDriver->writeFile("post_test.txt", buf, TEST_FILE_SIZE) == TEST_FILE_SIZE);
00194 
00195     // This may fail if rsp.txt doesn't happen to be sitting around from a previous run
00196     // so don't check the return value
00197     pDriver->delFile("rsp.txt");
00198 
00199     memset(buf, 0, sizeof (buf));
00200     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_POST_FILE,
00201                                      "/post",
00202                                      "rsp.txt", "post_test.txt",
00203                                      UbloxATCellularInterfaceExt::HTTP_CONTENT_TEXT, NULL,
00204                                      buf, sizeof (buf)) == NULL);
00205     tr_debug("Received: %s", buf);
00206     // Find the data in the response and check it
00207     pData = strstr(buf, "\"data\": \"");
00208     TEST_ASSERT(pData != NULL);
00209     pData += 9;
00210     for (int x = 0; x < TEST_FILE_SIZE; x++) {
00211         TEST_ASSERT(*(pData + x) == (x % 10) + 0x30);
00212     }
00213 
00214     // Also check that rsp.txt exists and is the same as buf
00215     pDriver->readFile("rsp.txt", buf1, sizeof (buf1));
00216     memcmp(buf1, buf, sizeof (buf1));
00217     TEST_ASSERT(pDriver->delFile("rsp.txt"));
00218     TEST_ASSERT(!pDriver->delFile("rsp.txt")); // Should fail
00219 
00220     TEST_ASSERT(pDriver->httpFreeProfile(profile));
00221     TEST_ASSERT(pDriver->disconnect() == 0);
00222     // Wait for printfs to leave the building or the test result string gets messed up
00223     ThisThread::sleep_for(500);
00224 
00225 #ifdef TARGET_UBLOX_C027
00226     pDriver->set_functionality_mode(UbloxATCellularInterfaceExt::FUNC_MIN);
00227 #else
00228     pDriver->set_functionality_mode(UbloxATCellularInterfaceExt::FUNC_AIRPLANE);
00229 #endif
00230 }
00231 
00232 // Test HTTP with TLS
00233 void test_http_tls() {
00234     int profile;
00235     SocketAddress address;
00236 
00237     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00238                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00239 
00240     profile = pDriver->httpAllocProfile();
00241     TEST_ASSERT(profile >= 0);
00242 
00243     pDriver->httpSetTimeout(profile, HTTP_TIMEOUT);
00244 
00245     // Set up the server to talk to and TLS, using the IP address this time just for variety
00246     TEST_ASSERT(pDriver->gethostbyname("www.amazon.com", &address) == 0);
00247     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address()));
00248     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SECURE, "1"));
00249     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_REQ_HEADER, "0:Host:www.amazon.com"));
00250 
00251     // Check HTTP get request
00252     memset(buf, 0, sizeof (buf));
00253     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00254                                      "/",
00255                                      NULL, NULL, 0, NULL,
00256                                      buf, sizeof (buf)) == NULL);
00257     tr_debug("Received: %s", buf);
00258     // This is what amazon.com returns if TLS is set
00259     TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL);
00260 
00261     // Reset the profile and check that this now fails
00262     TEST_ASSERT(pDriver->httpResetProfile(profile));
00263     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address()));
00264     memset(buf, 0, sizeof (buf));
00265     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00266                                      "/",
00267                                      NULL, NULL, 0, NULL,
00268                                      buf, sizeof (buf)) == NULL);
00269     tr_debug("Received: %s", buf);
00270     // This is what amazon.com returns if TLS is NOT set
00271     TEST_ASSERT(strstr(buf, "HTTP/1.1 403 Forbidden") != NULL);
00272 
00273     TEST_ASSERT(pDriver->httpFreeProfile(profile));
00274     TEST_ASSERT(pDriver->disconnect() == 0);
00275     // Wait for printfs to leave the building or the test result string gets messed up
00276     ThisThread::sleep_for(500);
00277 
00278 #ifdef TARGET_UBLOX_C027
00279     pDriver->set_functionality_mode(UbloxATCellularInterfaceExt::FUNC_MIN);
00280 #else
00281     pDriver->set_functionality_mode(UbloxATCellularInterfaceExt::FUNC_AIRPLANE);
00282 #endif
00283 }
00284 
00285 // Allocate max profiles
00286 void test_alloc_profiles() {
00287     int profiles[MAX_PROFILES];
00288 
00289     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00290                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00291 
00292     // Allocate first profile and use it
00293     profiles[0] = pDriver->httpAllocProfile();
00294     TEST_ASSERT(profiles[0] >= 0);
00295     TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, "raw.githubusercontent.com"));
00296     TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SECURE, "1"));
00297 
00298     // Check HTTP get request
00299     memset(buf, 0, sizeof (buf));
00300     TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET,
00301                                      "/u-blox/mbed-os/master/features/cellular/mbed_lib.json",
00302                                      NULL, NULL, 0, NULL,
00303                                      buf, sizeof (buf)) == NULL);
00304     tr_debug("Received: %s", buf);
00305     TEST_ASSERT(strstr(buf, "Radio access technology to use. Value in integer: GSM=0, GSM_COMPACT=1, UTRAN=2, EGPRS=3, HSDPA=4, HSUPA=5, HSDPA_HSUPA=6, E_UTRAN=7, CATM1=8 ,NB1=9") != NULL);
00306 
00307     // Check that we stop being able to get profiles at the max number
00308     for (int x = 1; x < sizeof (profiles) / sizeof (profiles[0]); x++) {
00309         profiles[x] = pDriver->httpAllocProfile();
00310         TEST_ASSERT(profiles[0] >= 0);
00311     }
00312     TEST_ASSERT(pDriver->httpAllocProfile() < 0);
00313 
00314     // Now use the last one and check that it doesn't affect the first one
00315     TEST_ASSERT(pDriver->httpSetPar(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1],
00316                                     UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER));
00317 
00318     // Check HTTP head request on last profile
00319     memset(buf, 0, sizeof (buf));
00320     TEST_ASSERT(pDriver->httpCommand(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1],
00321                                      UbloxATCellularInterfaceExt::HTTP_HEAD,
00322                                      "/headers",
00323                                      NULL, NULL, 0, NULL,
00324                                      buf, sizeof (buf)) == NULL);
00325     tr_debug("Received: %s", buf);
00326     TEST_ASSERT(strstr(buf, "Content-Length:") != NULL);
00327 
00328     // Check HTTP get request on first profile once more
00329     memset(buf, 0, sizeof (buf));
00330     TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET,
00331                                      "/u-blox/mbed-os/master/features/cellular/mbed_lib.json",
00332                                      NULL, NULL, 0, NULL,
00333                                      buf, sizeof (buf)) == NULL);
00334     tr_debug("Received: %s", buf);
00335     TEST_ASSERT(strstr(buf, "Radio access technology to use. Value in integer: GSM=0, GSM_COMPACT=1, UTRAN=2, EGPRS=3, HSDPA=4, HSUPA=5, HSDPA_HSUPA=6, E_UTRAN=7, CATM1=8 ,NB1=9") != NULL);
00336 
00337     // Free the profiles again
00338     for (int x = 0; x < sizeof (profiles) / sizeof (profiles[0]); x++) {
00339         TEST_ASSERT(pDriver->httpFreeProfile(profiles[x]));
00340     }
00341 
00342     TEST_ASSERT(pDriver->disconnect() == 0);
00343     // Wait for printfs to leave the building or the test result string gets messed up
00344     ThisThread::sleep_for(500);
00345 }
00346 
00347 // ----------------------------------------------------------------
00348 // TEST ENVIRONMENT
00349 // ----------------------------------------------------------------
00350 
00351 // Setup the test environment
00352 utest::v1::status_t test_setup(const size_t number_of_cases) {
00353     // Setup Greentea with a timeout
00354     GREENTEA_SETUP(540, "default_auto");
00355     return verbose_test_setup_handler(number_of_cases);
00356 }
00357 
00358 // Test cases
00359 Case cases[] = {
00360     Case("HTTP commands", test_http_cmd),
00361 #ifndef TARGET_UBLOX_C027
00362     // C027 doesn't support TLS
00363     Case("HTTP with TLS", test_http_tls),
00364 #endif
00365     Case("Alloc max profiles", test_alloc_profiles)
00366 };
00367 
00368 Specification specification(test_setup, cases);
00369 
00370 // ----------------------------------------------------------------
00371 // MAIN
00372 // ----------------------------------------------------------------
00373 
00374 int main() {
00375 
00376 #ifdef FEATURE_COMMON_PAL
00377     mbed_trace_init();
00378 
00379     mbed_trace_mutex_wait_function_set(lock);
00380     mbed_trace_mutex_release_function_set(unlock);
00381 #endif
00382     
00383     // Run tests
00384     return !Harness::run(specification);
00385 }
00386 
00387 // End Of File
00388