This class provides SMS, USSD and modem file system support for u-blox modules on the C027 and C030 boards (excepting the C030 N2xx flavour) from mbed 5.5 onwards.

Dependents:   example-ublox-at-cellular-interface-ext example-ublox-cellular-driver-gen HelloMQTT ublox_new_driver_test ... 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 "UbloxCellularDriverGen.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 //        "default-pin": {
00029 //            "value": "\"my_pin\""
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: this is the PIN for the SIM with ICCID
00041 // 8944501104169548380.
00042 # define MBED_CONF_APP_DEFAULT_PIN "5134"
00043 #endif
00044 
00045 // A number to send SMS messages to which will respond
00046 // to the instruction given in the messages.
00047 // IMPORTANT: spaces in the string are NOT allowed
00048 #ifndef MBED_CONF_APP_SMS_DESTINATION
00049 # error "Must define a destination number to use for SMS testing (and someone must be there to reply); the number must contain no spaces and should be in international format"
00050 #endif
00051 
00052 // The message to send.
00053 #ifndef MBED_CONF_APP_SMS_SEND_CONTENTS
00054 # define MBED_CONF_APP_SMS_SEND_CONTENTS "Please reply to this message within 60 seconds with the single word ACK (in upper case)."
00055 #endif
00056 
00057 // The number of milliseconds to wait for a reply to the sent SMS.
00058 #ifndef MBED_CONF_APP_SMS_RECEIVE_TIMEOUT
00059 # define MBED_CONF_APP_SMS_RECEIVE_TIMEOUT 60000
00060 #endif
00061 
00062 // The string that the reply must contain.
00063 #ifndef MBED_CONF_APP_SMS_RECEIVE_CONTENTS
00064 # define MBED_CONF_APP_SMS_RECEIVE_CONTENTS "ACK"
00065 #endif
00066 
00067 // ----------------------------------------------------------------
00068 // PRIVATE VARIABLES
00069 // ----------------------------------------------------------------
00070 
00071 #ifdef FEATURE_COMMON_PAL
00072 // Lock for debug prints
00073 static Mutex mtx;
00074 #endif
00075 
00076 // An instance of the generic cellular class
00077 static UbloxCellularDriverGen *pDriver =
00078        new UbloxCellularDriverGen(MDMTXD, MDMRXD,
00079                                   MBED_CONF_UBLOX_CELL_BAUD_RATE,
00080                                   MBED_CONF_APP_DEBUG_ON);
00081 
00082 // ----------------------------------------------------------------
00083 // PRIVATE FUNCTIONS
00084 // ----------------------------------------------------------------
00085 
00086 #ifdef FEATURE_COMMON_PAL
00087 // Locks for debug prints
00088 static void lock()
00089 {
00090     mtx.lock();
00091 }
00092 
00093 static void unlock()
00094 {
00095     mtx.unlock();
00096 }
00097 #endif
00098 
00099 // ----------------------------------------------------------------
00100 // TESTS
00101 // ----------------------------------------------------------------
00102 
00103 // Register with the network
00104 void test_start() {
00105     TEST_ASSERT(pDriver->init(MBED_CONF_APP_DEFAULT_PIN));
00106     TEST_ASSERT(pDriver->nwk_registration());
00107 }
00108 
00109 // Send an SMS message
00110 void test_send() {
00111     TEST_ASSERT(pDriver->smsSend(MBED_CONF_APP_SMS_DESTINATION,
00112                                  MBED_CONF_APP_SMS_SEND_CONTENTS));
00113 }
00114 
00115 // Receive an SMS message, check it and delete it
00116 void test_receive() {
00117     int numSms = 0;
00118     int index;
00119     char num[17];
00120     char buf[SMS_BUFFER_SIZE];
00121     Timer timer;
00122 
00123     tr_warn("!!! YOU HAVE %d SECONDS to reply to the text message !!!",
00124             MBED_CONF_APP_SMS_RECEIVE_TIMEOUT / 1000);
00125     timer.start();
00126     while ((numSms == 0) &&
00127            (timer.read_ms() < MBED_CONF_APP_SMS_RECEIVE_TIMEOUT)) {
00128         numSms = pDriver->smsList("REC UNREAD", &index, 1);
00129         if (numSms == 0) {
00130             wait_ms(1000);
00131         }
00132     }
00133     timer.stop();
00134 
00135     TEST_ASSERT (numSms > 0);
00136 
00137     TEST_ASSERT(pDriver->smsRead(index, num, buf, sizeof (buf)));
00138     tr_debug("Received: \"%.*s\"", sizeof (buf), buf);
00139     TEST_ASSERT (strstr(buf, MBED_CONF_APP_SMS_RECEIVE_CONTENTS) != NULL);
00140 
00141     // Delete the message and check that it's gone
00142     numSms = pDriver->smsList();
00143     TEST_ASSERT(numSms > 0);
00144     TEST_ASSERT(pDriver->smsDelete(index));
00145     TEST_ASSERT(pDriver->smsList() == numSms - 1);
00146 }
00147 
00148 // De-register from the network
00149 void test_end() {
00150     TEST_ASSERT(pDriver->nwk_deregistration());
00151 }
00152 
00153 // ----------------------------------------------------------------
00154 // TEST ENVIRONMENT
00155 // ----------------------------------------------------------------
00156 
00157 // Setup the test environment
00158 utest::v1::status_t test_setup(const size_t number_of_cases) {
00159     // Setup Greentea with a timeout
00160     GREENTEA_SETUP(180, "default_auto");
00161     return verbose_test_setup_handler(number_of_cases);
00162 }
00163 
00164 // Test cases
00165 Case cases[] = {
00166     Case("Register", test_start),
00167     Case("SMS send", test_send),
00168     Case("SMS receive and delete", test_receive),
00169     Case("Deregister", test_end)
00170 };
00171 
00172 Specification specification(test_setup, cases);
00173 
00174 // ----------------------------------------------------------------
00175 // MAIN
00176 // ----------------------------------------------------------------
00177 
00178 int main() {
00179 
00180 #ifdef FEATURE_COMMON_PAL
00181     mbed_trace_init();
00182 
00183     mbed_trace_mutex_wait_function_set(lock);
00184     mbed_trace_mutex_release_function_set(unlock);
00185 #endif
00186     
00187     // Run tests
00188     return !Harness::run(specification);
00189 }
00190 
00191 // End Of File
00192