Wajahat Abbas / ublox-at-cellular-interface-ext

Dependencies:   ublox-at-cellular-interface

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             wait_ms(5000);
00128         }
00129     }
00130     TEST_ASSERT(pDriver->init(MBED_CONF_APP_DEFAULT_PIN));
00131     TEST_ASSERT(pDriver->disable_psm());
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     wait_ms(500);
00224 }
00225 
00226 // Test HTTP with TLS
00227 void test_http_tls() {
00228     int profile;
00229     SocketAddress address;
00230 
00231     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00232                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00233 
00234     profile = pDriver->httpAllocProfile();
00235     TEST_ASSERT(profile >= 0);
00236 
00237     pDriver->httpSetTimeout(profile, HTTP_TIMEOUT);
00238 
00239     // Set up the server to talk to and TLS, using the IP address this time just for variety
00240     TEST_ASSERT(pDriver->gethostbyname("www.amazon.com", &address) == 0);
00241     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address()));
00242     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SECURE, "1"));
00243     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_REQ_HEADER, "0:Host:www.amazon.com"));
00244 
00245     // Check HTTP get request
00246     memset(buf, 0, sizeof (buf));
00247     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00248                                      "/",
00249                                      NULL, NULL, 0, NULL,
00250                                      buf, sizeof (buf)) == NULL);
00251     tr_debug("Received: %s", buf);
00252     // This is what amazon.com returns if TLS is set
00253     TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL);
00254 
00255     // Reset the profile and check that this now fails
00256     TEST_ASSERT(pDriver->httpResetProfile(profile));
00257     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address()));
00258     memset(buf, 0, sizeof (buf));
00259     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00260                                      "/",
00261                                      NULL, NULL, 0, NULL,
00262                                      buf, sizeof (buf)) == NULL);
00263     tr_debug("Received: %s", buf);
00264     // This is what amazon.com returns if TLS is NOT set
00265     TEST_ASSERT(strstr(buf, "HTTP/1.1 403 Forbidden") != NULL);
00266 
00267     TEST_ASSERT(pDriver->httpFreeProfile(profile));
00268     TEST_ASSERT(pDriver->disconnect() == 0);
00269     // Wait for printfs to leave the building or the test result string gets messed up
00270     wait_ms(500);
00271 }
00272 
00273 // Allocate max profiles
00274 void test_alloc_profiles() {
00275     int profiles[MAX_PROFILES];
00276 
00277     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00278                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00279 
00280     // Allocate first profile and use it
00281     profiles[0] = pDriver->httpAllocProfile();
00282     TEST_ASSERT(profiles[0] >= 0);
00283     TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, "raw.githubusercontent.com"));
00284     TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SECURE, "1"));
00285 
00286     // Check HTTP get request
00287     memset(buf, 0, sizeof (buf));
00288     TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET,
00289                                      "/u-blox/mbed-os/master/features/cellular/mbed_lib.json",
00290                                      NULL, NULL, 0, NULL,
00291                                      buf, sizeof (buf)) == NULL);
00292     tr_debug("Received: %s", buf);
00293     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);
00294 
00295     // Check that we stop being able to get profiles at the max number
00296     for (int x = 1; x < sizeof (profiles) / sizeof (profiles[0]); x++) {
00297         profiles[x] = pDriver->httpAllocProfile();
00298         TEST_ASSERT(profiles[0] >= 0);
00299     }
00300     TEST_ASSERT(pDriver->httpAllocProfile() < 0);
00301 
00302     // Now use the last one and check that it doesn't affect the first one
00303     TEST_ASSERT(pDriver->httpSetPar(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1],
00304                                     UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER));
00305 
00306     // Check HTTP head request on last profile
00307     memset(buf, 0, sizeof (buf));
00308     TEST_ASSERT(pDriver->httpCommand(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1],
00309                                      UbloxATCellularInterfaceExt::HTTP_HEAD,
00310                                      "/headers",
00311                                      NULL, NULL, 0, NULL,
00312                                      buf, sizeof (buf)) == NULL);
00313     tr_debug("Received: %s", buf);
00314     TEST_ASSERT(strstr(buf, "Content-Length:") != NULL);
00315 
00316     // Check HTTP get request on first profile once more
00317     memset(buf, 0, sizeof (buf));
00318     TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET,
00319                                      "/u-blox/mbed-os/master/features/cellular/mbed_lib.json",
00320                                      NULL, NULL, 0, NULL,
00321                                      buf, sizeof (buf)) == NULL);
00322     tr_debug("Received: %s", buf);
00323     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);
00324 
00325     // Free the profiles again
00326     for (int x = 0; x < sizeof (profiles) / sizeof (profiles[0]); x++) {
00327         TEST_ASSERT(pDriver->httpFreeProfile(profiles[x]));
00328     }
00329 
00330     TEST_ASSERT(pDriver->disconnect() == 0);
00331     // Wait for printfs to leave the building or the test result string gets messed up
00332     wait_ms(500);
00333 }
00334 
00335 // ----------------------------------------------------------------
00336 // TEST ENVIRONMENT
00337 // ----------------------------------------------------------------
00338 
00339 // Setup the test environment
00340 utest::v1::status_t test_setup(const size_t number_of_cases) {
00341     // Setup Greentea with a timeout
00342     GREENTEA_SETUP(540, "default_auto");
00343     return verbose_test_setup_handler(number_of_cases);
00344 }
00345 
00346 // Test cases
00347 Case cases[] = {
00348     Case("HTTP commands", test_http_cmd),
00349 #ifndef TARGET_UBLOX_C027
00350     // C027 doesn't support TLS
00351     Case("HTTP with TLS", test_http_tls),
00352 #endif
00353     Case("Alloc max profiles", test_alloc_profiles)
00354 };
00355 
00356 Specification specification(test_setup, cases);
00357 
00358 // ----------------------------------------------------------------
00359 // MAIN
00360 // ----------------------------------------------------------------
00361 
00362 int main() {
00363 
00364 #ifdef FEATURE_COMMON_PAL
00365     mbed_trace_init();
00366 
00367     mbed_trace_mutex_wait_function_set(lock);
00368     mbed_trace_mutex_release_function_set(unlock);
00369 #endif
00370     
00371     // Run tests
00372     return !Harness::run(specification);
00373 }
00374 
00375 // End Of File
00376