Rod Landers / 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 //        "run-tcp-server-test": {
00032 //            "value": 1
00033 //        },
00034 //        "mga-token": {
00035 //            "value": "\"my_token\""
00036 //        }
00037 //}
00038 
00039 // Whether debug trace is on
00040 #ifndef MBED_CONF_APP_DEBUG_ON
00041 # define MBED_CONF_APP_DEBUG_ON false
00042 #endif
00043 
00044 // The credentials of the SIM in the board.
00045 #ifndef MBED_CONF_APP_DEFAULT_PIN
00046 // Note: if PIN is enabled on your SIM, you must define the PIN
00047 // for your SIM jere (e.g. using mbed_app.json to do so).
00048 # define MBED_CONF_APP_DEFAULT_PIN "0000"
00049 #endif
00050 
00051 // Network credentials.
00052 #ifndef MBED_CONF_APP_APN
00053 # define MBED_CONF_APP_APN         NULL
00054 #endif
00055 #ifndef MBED_CONF_APP_USERNAME
00056 # define MBED_CONF_APP_USERNAME    NULL
00057 #endif
00058 #ifndef MBED_CONF_APP_PASSWORD
00059 # define MBED_CONF_APP_PASSWORD    NULL
00060 #endif
00061 
00062 #ifndef MBED_CONF_APP_RUN_CELL_LOCATE_TCP_SERVER_TEST
00063 #  define MBED_CONF_APP_RUN_CELL_LOCATE_TCP_SERVER_TEST 0
00064 #endif
00065 
00066 // The authentication token for TCP access to the MGA server.
00067 #if MBED_CONF_APP_RUN_CELL_LOCATE_TCP_SERVER_TEST
00068 #  ifndef MBED_CONF_APP_CELL_LOCATE_MGA_TOKEN
00069 #    error "You must have a token for MGA server access to run Cell Locate with a TCP server"
00070 #  endif
00071 #endif
00072 
00073 // The type of response requested
00074 #ifndef MBED_CONF_APP_RESP_TYPE
00075 #  define MBED_CONF_APP_RESP_TYPE 1 // CELL_DETAILED
00076 #endif
00077 
00078 // The maximum number of hypotheses requested
00079 #ifndef MBED_CONF_APP_CELL_LOCATE_MAX_NUM_HYPOTHESIS
00080 #  if MBED_CONF_APP_RESP_TYPE == 2 // CELL_MULTIHYP
00081 #    define MBED_CONF_APP_CELL_LOCATE_MAX_NUM_HYPOTHESIS 16
00082 #  else
00083 #    define MBED_CONF_APP_CELL_LOCATE_MAX_NUM_HYPOTHESIS 1
00084 #  endif
00085 #endif
00086 
00087 #ifndef MBED_CONF_APP_CELL_LOCATE_MGA_TOKEN
00088 #  define MBED_CONF_APP_CELL_LOCATE_MGA_TOKEN "0"
00089 #endif
00090 
00091 // ----------------------------------------------------------------
00092 // PRIVATE VARIABLES
00093 // ----------------------------------------------------------------
00094 
00095 #ifdef FEATURE_COMMON_PAL
00096 // Lock for debug prints
00097 static Mutex mtx;
00098 #endif
00099 
00100 // Power up GNSS
00101 #ifdef TARGET_UBLOX_C030
00102 static DigitalInOut gnssEnable(GNSSEN, PIN_OUTPUT, PushPullNoPull, 1);
00103 #elif TARGET_UBLOX_C027
00104 static DigitalOut gnssEnable(GPSEN, 1);
00105 #elif UBLOX_M8N_GPS
00106 static DigitalOut gnssEnable(GPS_EN, 1);
00107 #endif
00108 
00109 // An instance of the cellular interface
00110 static UbloxATCellularInterfaceExt *pDriver =
00111        new UbloxATCellularInterfaceExt(MDMTXD, MDMRXD,
00112                                        MBED_CONF_UBLOX_CELL_BAUD_RATE,
00113                                        MBED_CONF_APP_DEBUG_ON);
00114 
00115 // ----------------------------------------------------------------
00116 // PRIVATE FUNCTIONS
00117 // ----------------------------------------------------------------
00118 
00119 #ifdef FEATURE_COMMON_PAL
00120 // Locks for debug prints
00121 static void lock()
00122 {
00123     mtx.lock();
00124 }
00125 
00126 static void unlock()
00127 {
00128     mtx.unlock();
00129 }
00130 #endif
00131 
00132 static void printCellLocateData(UbloxATCellularInterfaceExt::CellLocData *pData)
00133 {
00134     char timeString[25];
00135 
00136     tr_debug("Cell Locate data:");
00137     if (strftime(timeString, sizeof(timeString), "%F %T", (const tm *) &(pData->time)) > 0) {
00138         tr_debug("  time:               %s", timeString);
00139     }
00140     tr_debug("  longitude:          %.6f", pData->longitude);
00141     tr_debug("  latitude:           %.6f", pData->latitude);
00142     tr_debug("  altitude:           %d metre(s)", pData->altitude);
00143     switch (pData->sensor) {
00144         case UbloxATCellularInterfaceExt::CELL_LAST:
00145             tr_debug("  sensor type:        last");
00146             break;
00147         case UbloxATCellularInterfaceExt::CELL_GNSS:
00148             tr_debug("  sensor type:        GNSS");
00149             break;
00150         case UbloxATCellularInterfaceExt::CELL_LOCATE:
00151             tr_debug("  sensor type:        Cell Locate");
00152             break;
00153         case UbloxATCellularInterfaceExt::CELL_HYBRID:
00154             tr_debug("  sensor type:        hybrid");
00155             break;
00156         default:
00157             tr_debug("  sensor type:        unknown");
00158             break;
00159     }
00160     tr_debug("  uncertainty:        %d metre(s)", pData->uncertainty);
00161     tr_debug("  speed:              %d metre(s)/second", pData->speed);
00162     tr_debug("  direction:          %d degree(s)", pData->direction);
00163     tr_debug("  vertical accuracy:  %d metre(s)/second", pData->speed);
00164     tr_debug("  satellite(s) used:  %d", pData->svUsed);
00165     tr_debug("I am here:            "
00166             "https://maps.google.com/?q=%.5f,%.5f", pData->latitude, pData->longitude);       
00167 }
00168 
00169 // ----------------------------------------------------------------
00170 // TESTS
00171 // ----------------------------------------------------------------
00172 
00173 // Test Cell Locate talking to a UDP server
00174 void test_udp_server() {
00175     int numRes = 0;
00176     UbloxATCellularInterfaceExt::CellLocData data;
00177 
00178     memset(&data, 0, sizeof(data));
00179     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00180                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00181 
00182     TEST_ASSERT(pDriver->cellLocSrvUdp());
00183     TEST_ASSERT(pDriver->cellLocConfig(1));
00184     TEST_ASSERT(pDriver->cellLocRequest(UbloxATCellularInterfaceExt::CELL_HYBRID, 10, 100,
00185                                         (UbloxATCellularInterfaceExt::CellRespType) MBED_CONF_APP_RESP_TYPE,
00186                                         MBED_CONF_APP_CELL_LOCATE_MAX_NUM_HYPOTHESIS));
00187 
00188     for (int x = 0; (numRes == 0) && (x < 10); x++) {
00189         numRes = pDriver->cellLocGetRes();
00190     }
00191 
00192     TEST_ASSERT(numRes > 0);
00193     TEST_ASSERT(pDriver->cellLocGetData(&data));
00194 
00195     TEST_ASSERT(data.validData);
00196 
00197     printCellLocateData(&data);
00198 
00199     TEST_ASSERT(pDriver->disconnect() == 0);
00200     // Wait for printfs to leave the building or the test result string gets messed up
00201     wait_ms(500);
00202 }
00203 
00204 // Test Cell Locate talking to a TCP server
00205 void test_tcp_server() {
00206     int numRes = 0;
00207     UbloxATCellularInterfaceExt::CellLocData data;
00208 
00209     memset(&data, 0, sizeof(data));
00210     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00211                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00212 
00213     TEST_ASSERT(pDriver->cellLocSrvTcp(MBED_CONF_APP_CELL_LOCATE_MGA_TOKEN));
00214     TEST_ASSERT(pDriver->cellLocConfig(1));
00215     TEST_ASSERT(pDriver->cellLocRequest(UbloxATCellularInterfaceExt::CELL_HYBRID, 10, 100,
00216                                         (UbloxATCellularInterfaceExt::CellRespType) MBED_CONF_APP_RESP_TYPE,
00217                                         MBED_CONF_APP_CELL_LOCATE_MAX_NUM_HYPOTHESIS));
00218 
00219     for (int x = 0; (numRes == 0) && (x < 10); x++) {
00220         numRes = pDriver->cellLocGetRes();
00221     }
00222 
00223     TEST_ASSERT(numRes > 0);
00224     TEST_ASSERT(pDriver->cellLocGetData(&data));
00225 
00226     TEST_ASSERT(data.validData);
00227 
00228     printCellLocateData(&data);
00229 
00230     TEST_ASSERT(pDriver->disconnect() == 0);
00231     // Wait for printfs to leave the building or the test result string gets messed up
00232     wait_ms(500);
00233 }
00234 
00235 // Tidy up after testing so as not to screw with the test output strings
00236 void test_tidy_up() {
00237     pDriver->deinit();
00238 }
00239 
00240 // ----------------------------------------------------------------
00241 // TEST ENVIRONMENT
00242 // ----------------------------------------------------------------
00243 
00244 // Setup the test environment
00245 utest::v1::status_t test_setup(const size_t number_of_cases) {
00246     // Setup Greentea with a timeout
00247     GREENTEA_SETUP(540, "default_auto");
00248     return verbose_test_setup_handler(number_of_cases);
00249 }
00250 
00251 // Test cases
00252 Case cases[] = {
00253     Case("Cell Locate with UDP server", test_udp_server),
00254 #if MBED_CONF_APP_RUN_CELL_LOCATE_TCP_SERVER_TEST
00255     Case("Cell Locate with TCP server", test_tcp_server),
00256 #endif
00257     Case("Tidy up", test_tidy_up)
00258 };
00259 
00260 Specification specification(test_setup, cases);
00261 
00262 // ----------------------------------------------------------------
00263 // MAIN
00264 // ----------------------------------------------------------------
00265 
00266 int main() {
00267 
00268 #ifdef FEATURE_COMMON_PAL
00269     mbed_trace_init();
00270 
00271     mbed_trace_mutex_wait_function_set(lock);
00272     mbed_trace_mutex_release_function_set(unlock);
00273 #endif
00274     
00275     // Run tests
00276     return !Harness::run(specification);
00277 }
00278 
00279 // End Of File
00280