|
| 1 | +#define __STDC_FORMAT_MACROS 1 |
| 2 | +#include <inttypes.h> |
| 3 | +#include "mbed.h" |
| 4 | +#include "BufferedSerial.h" |
| 5 | + |
| 6 | +#define UBLOX 0 |
| 7 | +#define MTS_DRAGONFLY 1 |
| 8 | + |
| 9 | +#if MBED_CONF_APP_PLATFORM == UBLOX |
| 10 | +#include "UbloxCellularInterface.h" |
| 11 | +#include "ublox_low_level_api.h" |
| 12 | +UbloxCellularInterface *iface; |
| 13 | +#elif MBED_CONF_APP_PLATFORM == MTS_DRAGONFLY |
| 14 | +#include "DragonFlyCellularInterface.h" |
| 15 | +DragonFlyCellularInterface *iface; |
| 16 | +#endif |
| 17 | +#include "UDPSocket.h" |
| 18 | +#include "common_functions.h" |
| 19 | +#if defined(FEATURE_COMMON_PAL) |
| 20 | +#include "mbed_trace.h" |
| 21 | +#define TRACE_GROUP "MAIN" |
| 22 | +#else |
| 23 | +#define tr_debug(...) (void(0)) //dummies if feature common pal is not added |
| 24 | +#define tr_info(...) (void(0)) //dummies if feature common pal is not added |
| 25 | +#define tr_error(...) (void(0)) //dummies if feature common pal is not added |
| 26 | +#endif //defined(FEATURE_COMMON_PAL) |
| 27 | + |
| 28 | +UDPSocket *socket; |
| 29 | +static const char *host_name = "2.pool.ntp.org"; |
| 30 | +static const int port = 123; |
| 31 | +static Mutex mtx; |
| 32 | +static nsapi_error_t connection_down_reason = 0; |
| 33 | + |
| 34 | +void ppp_connection_down_cb(nsapi_error_t error) |
| 35 | +{ |
| 36 | + switch (error) { |
| 37 | + case NSAPI_ERROR_CONNECTION_LOST: |
| 38 | + case NSAPI_ERROR_NO_CONNECTION: |
| 39 | + tr_debug("Carrier/Connection lost"); |
| 40 | + break; |
| 41 | + case NSAPI_ERROR_CONNECTION_TIMEOUT: |
| 42 | + tr_debug("Connection timed out."); |
| 43 | + break; |
| 44 | + case NSAPI_ERROR_AUTH_FAILURE: |
| 45 | + tr_debug("Authentication failure"); |
| 46 | + break; |
| 47 | + } |
| 48 | + |
| 49 | + connection_down_reason = error; |
| 50 | +} |
| 51 | + |
| 52 | +static void lock() |
| 53 | +{ |
| 54 | + mtx.lock(); |
| 55 | +} |
| 56 | + |
| 57 | +static void unlock() |
| 58 | +{ |
| 59 | + mtx.unlock(); |
| 60 | +} |
| 61 | + |
| 62 | +// main() runs in its own thread in the OS |
| 63 | +// (note the calls to wait below for delays) |
| 64 | + |
| 65 | +int do_ntp() |
| 66 | +{ |
| 67 | + int ntp_values[12] = { 0 }; |
| 68 | + time_t TIME1970 = 2208988800U; |
| 69 | + |
| 70 | + UDPSocket sock; |
| 71 | + |
| 72 | + int ret = sock.open(iface); |
| 73 | + if (ret) { |
| 74 | + tr_error("UDPSocket.open() fails, code: %d", ret); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + SocketAddress nist; |
| 79 | + ret = iface->gethostbyname(host_name, &nist); |
| 80 | + if (ret) { |
| 81 | + tr_error("Couldn't resolve remote host: %s, code: %d", host_name, ret); |
| 82 | + return -1; |
| 83 | + } |
| 84 | + nist.set_port(port); |
| 85 | + |
| 86 | + tr_info("UDP: NIST server %s address: %s on port %d", host_name, |
| 87 | + nist.get_ip_address(), nist.get_port()); |
| 88 | + |
| 89 | + memset(ntp_values, 0x00, sizeof(ntp_values)); |
| 90 | + ntp_values[0] = '\x1b'; |
| 91 | + |
| 92 | + sock.set_timeout(5000); |
| 93 | + |
| 94 | + int ret_send = sock.sendto(nist, (void*) ntp_values, sizeof(ntp_values)); |
| 95 | + tr_debug("UDP: Sent %d Bytes to NTP server", ret_send); |
| 96 | + |
| 97 | + const int n = sock.recvfrom(&nist, (void*) ntp_values, sizeof(ntp_values)); |
| 98 | + sock.close(); |
| 99 | + |
| 100 | + if (n > 0) { |
| 101 | + tr_debug("UDP: Recved from NTP server %d Bytes", n); |
| 102 | + tr_debug("UDP: Values returned by NTP server:"); |
| 103 | + for (size_t i = 0; i < sizeof(ntp_values) / sizeof(ntp_values[0]); |
| 104 | + ++i) { |
| 105 | + tr_debug("\t[%02d] 0x%" PRIX32, i, |
| 106 | + common_read_32_bit((uint8_t*) &(ntp_values[i]))); |
| 107 | + if (i == 10) { |
| 108 | + const time_t timestamp = common_read_32_bit( |
| 109 | + (uint8_t*) &(ntp_values[i])) - TIME1970; |
| 110 | + struct tm *local_time = localtime(×tamp); |
| 111 | + if (local_time) { |
| 112 | + char time_string[25]; |
| 113 | + if (strftime(time_string, sizeof(time_string), |
| 114 | + "%a %b %d %H:%M:%S %Y", local_time) > 0) { |
| 115 | + tr_info("NTP timestamp is %s", time_string); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return 0; |
| 121 | + } |
| 122 | + |
| 123 | + if (n == NSAPI_ERROR_WOULD_BLOCK) { |
| 124 | + return -1; |
| 125 | + } |
| 126 | + |
| 127 | + return -1; |
| 128 | +} |
| 129 | + |
| 130 | +#if MBED_CONF_APP_PLATFORM == UBLOX |
| 131 | +UbloxCellularInterface my_iface(false, true); |
| 132 | +#elif MBED_CONF_APP_PLATFORM == MTS_DRAGONFLY |
| 133 | +DragonFlyCellularInterface my_iface(false); |
| 134 | +#endif |
| 135 | + |
| 136 | +nsapi_error_t connection() |
| 137 | +{ |
| 138 | + nsapi_error_t retcode; |
| 139 | + bool disconnected = false; |
| 140 | + |
| 141 | + while (!iface->isConnected()) { |
| 142 | + |
| 143 | + retcode = iface->connect(); |
| 144 | + if (retcode == NSAPI_ERROR_AUTH_FAILURE) { |
| 145 | + tr_error("Authentication Failure. Exiting application"); |
| 146 | + return retcode; |
| 147 | + } else if (retcode != NSAPI_ERROR_OK) { |
| 148 | + tr_error("Couldn't connect: %d", retcode); |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + tr_info("Connection Established."); |
| 156 | + |
| 157 | + return NSAPI_ERROR_OK; |
| 158 | +} |
| 159 | + |
| 160 | +int getTime() |
| 161 | +{ |
| 162 | + int retcode = -1; |
| 163 | + if (iface->isConnected()) { |
| 164 | + retcode = do_ntp(); |
| 165 | + } else { |
| 166 | + /* Determine why the network is down */ |
| 167 | + tr_warn("Connection down: %d", connection_down_reason); |
| 168 | + |
| 169 | + if (connection_down_reason == NSAPI_ERROR_AUTH_FAILURE) { |
| 170 | + tr_debug("Authentication Error"); |
| 171 | + } else if (connection_down_reason == NSAPI_ERROR_NO_CONNECTION |
| 172 | + || NSAPI_ERROR_CONNECTION_LOST) { |
| 173 | + tr_debug("Carrier lost"); |
| 174 | + } else if (connection_down_reason == NSAPI_ERROR_CONNECTION_TIMEOUT) { |
| 175 | + tr_debug("Connection timed out"); |
| 176 | + } |
| 177 | + |
| 178 | + return -1; |
| 179 | + } |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +int main() |
| 185 | +{ |
| 186 | + mbed_trace_init(); |
| 187 | + |
| 188 | + mbed_trace_mutex_wait_function_set(lock); |
| 189 | + mbed_trace_mutex_release_function_set(unlock); |
| 190 | + |
| 191 | + nsapi_error_t retcode = NSAPI_ERROR_OK; |
| 192 | + |
| 193 | + iface = &my_iface; |
| 194 | + iface->set_SIM_pin("1234"); |
| 195 | + |
| 196 | + iface->set_credentials("internet"); |
| 197 | + |
| 198 | + iface->connection_lost_notification_cb(ppp_connection_down_cb); |
| 199 | + |
| 200 | + retcode = connection(); |
| 201 | + if (retcode == NSAPI_ERROR_AUTH_FAILURE) { |
| 202 | + tr_error("Authentication Failure. Exiting application"); |
| 203 | + return -1; |
| 204 | + } |
| 205 | + |
| 206 | + if (getTime() == 0) { |
| 207 | + tr_info("Done."); |
| 208 | + } |
| 209 | + |
| 210 | + return 0; |
| 211 | +} |
| 212 | + |
0 commit comments