|
| 1 | +#include "GSM.h" |
| 2 | + |
| 3 | +#include "mbed.h" |
| 4 | +#include "CellularLog.h" |
| 5 | + |
| 6 | +#define MAXRETRY 3 |
| 7 | + |
| 8 | +mbed::CellularDevice *mbed::CellularDevice::get_default_instance() |
| 9 | +{ |
| 10 | + static BufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200); |
| 11 | +#if defined(MBED_CONF_GEMALTO_CINTERION_RTS) && defined(MBED_CONF_GEMALTO_CINTERION_CTS) |
| 12 | + serial.set_flow_control(mbed::SerialBase::RTSCTS, MBED_CONF_GEMALTO_CINTERION_RTS, MBED_CONF_GEMALTO_CINTERION_CTS); |
| 13 | +#endif |
| 14 | + static GEMALTO_CINTERION device(&serial); |
| 15 | + return &device; |
| 16 | +} |
| 17 | + |
| 18 | +int arduino::GSMClass::begin(const char* pin, const char* apn, const char* username, const char* password, RadioAccessTechnologyType rat) { |
| 19 | + if (gsm_if == nullptr) { |
| 20 | + printf("Invalid gsm_if\n"); |
| 21 | + return 0; |
| 22 | + } |
| 23 | + |
| 24 | + static mbed::DigitalOut on(PJ_7, 1); |
| 25 | + static mbed::DigitalOut rts(MBED_CONF_GEMALTO_CINTERION_RTS, 0); |
| 26 | + |
| 27 | + _context->set_sim_pin(pin); |
| 28 | + |
| 29 | + _device->init(); |
| 30 | + |
| 31 | + _context->set_authentication_type((mbed::CellularContext::AuthenticationType)1); |
| 32 | + |
| 33 | + _pin = pin; |
| 34 | + _apn = apn; |
| 35 | + _username = username; |
| 36 | + _password = password; |
| 37 | + _rat = rat; |
| 38 | + _context->set_credentials(apn, username, password); |
| 39 | + |
| 40 | + _context->set_access_technology(rat); |
| 41 | + |
| 42 | + uint8_t connect_status = NSAPI_ERROR_AUTH_FAILURE; |
| 43 | + uint8_t retryCount = 0; |
| 44 | + while(connect_status != NSAPI_ERROR_OK && retryCount < MAXRETRY) { |
| 45 | + |
| 46 | + connect_status = _context->connect(pin, apn, username, password); |
| 47 | + retryCount++; |
| 48 | + |
| 49 | + if (connect_status == NSAPI_ERROR_AUTH_FAILURE) { |
| 50 | + printf("Authentication Failure. Exiting application.\n"); |
| 51 | + } else if (connect_status == NSAPI_ERROR_OK || connect_status == NSAPI_ERROR_IS_CONNECTED) { |
| 52 | + connect_status = NSAPI_ERROR_OK; |
| 53 | + printf("Connection Established.\n"); |
| 54 | + } else if (retryCount > 2) { |
| 55 | + printf("Fatal connection failure: %d\n", connect_status); |
| 56 | + } else { |
| 57 | + printf("Couldn't connect, will retry...\n"); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + return connect_status; |
| 64 | +} |
| 65 | + |
| 66 | +void arduino::GSMClass::end() { |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +int arduino::GSMClass::disconnect() { |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +unsigned long arduino::GSMClass::getTime() |
| 75 | +{ |
| 76 | + return _device->get_time(); |
| 77 | +} |
| 78 | + |
| 79 | +unsigned long arduino::GSMClass::getLocalTime() |
| 80 | +{ |
| 81 | + return _device->get_local_time(); |
| 82 | +} |
| 83 | + |
| 84 | +bool arduino::GSMClass::setTime(unsigned long const epoch, int const timezone) |
| 85 | +{ |
| 86 | + return _device->set_time(epoch, timezone); |
| 87 | +} |
| 88 | + |
| 89 | +static PlatformMutex trace_mutex; |
| 90 | + |
| 91 | +static void trace_wait() |
| 92 | +{ |
| 93 | + trace_mutex.lock(); |
| 94 | +} |
| 95 | + |
| 96 | +static void trace_release() |
| 97 | +{ |
| 98 | + trace_mutex.unlock(); |
| 99 | +} |
| 100 | + |
| 101 | +static char* trace_time(size_t ss) |
| 102 | +{ |
| 103 | + static char time_st[50]; |
| 104 | + auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(rtos::Kernel::Clock::now()).time_since_epoch().count(); |
| 105 | + //snprintf(time_st, 49, "[%08llums]", ms); |
| 106 | + snprintf(time_st, 1, "\n"); |
| 107 | + return time_st; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +void arduino::GSMClass::debug() { |
| 112 | + |
| 113 | +#ifndef CELLULAR_DEMO_TRACING_H_ |
| 114 | +#define CELLULAR_DEMO_TRACING_H_ |
| 115 | + |
| 116 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 117 | + |
| 118 | + mbed_trace_init(); |
| 119 | + mbed_trace_prefix_function_set( &trace_time ); |
| 120 | + |
| 121 | + mbed_trace_mutex_wait_function_set(trace_wait); |
| 122 | + mbed_trace_mutex_release_function_set(trace_release); |
| 123 | + |
| 124 | + mbed_cellular_trace::mutex_wait_function_set(trace_wait); |
| 125 | + mbed_cellular_trace::mutex_release_function_set(trace_release); |
| 126 | +#endif |
| 127 | +#endif |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +NetworkInterface* arduino::GSMClass::getNetwork() { |
| 132 | + return gsm_if; |
| 133 | +} |
| 134 | + |
| 135 | +arduino::GSMClass GSM(mbed::CellularContext::get_default_instance()); |
0 commit comments