support library for C027 helper functions for Buffer Pipes, Buffered Serial Port (rtos capable) and GPS parsing. It includes modem APIs for USSD, SMS and Sockets.

Dependents:   HTTPClient_Cellular_HelloWorld Cellular_HelloMQTT MbedSmartRestMain Car_Bon_car_module ... more

This library is intended to be used with u-blox products such as the C027 or a shield with u-blox cellular and GPS modules like the cellular and positioning shield from Embedded Artist.

For 2G/GSM and 3G/UMTS you need to:

  • have a SIM card and know its PIN number
  • need to know you network operators APN setting These setting should be passed to the connect or init and join functions. You can also extend the APN database in MDMAPN.h.

For CDMA products you need to make sure that you have provisioned and activated the modem with either Sprint or Verizon.

Committer:
mazgch
Date:
Mon May 12 13:00:27 2014 +0000
Revision:
54:7ba8e4c218e2
Parent:
52:8071747a7cb3
Child:
57:869bd35f44cc
added dump apis, more status fields, get modem instance only when connecting socket

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 17:296d94a006b4 1 #pragma once
mazgch 17:296d94a006b4 2
mazgch 17:296d94a006b4 3 #include "mbed.h"
mazgch 21:c4d64830bf02 4 #include <stdarg.h>
mazgch 21:c4d64830bf02 5
mazgch 18:e5697801df29 6 #include "Pipe.h"
mazgch 18:e5697801df29 7 #include "SerialPipe.h"
mazgch 17:296d94a006b4 8
mazgch 19:2b5d097ca15d 9 #ifdef TARGET_UBLOX_C027
mazgch 19:2b5d097ca15d 10 // if we detect the C027 platform we will assign the
mazgch 19:2b5d097ca15d 11 // default pinname and baudrate in the constructor
mazgch 19:2b5d097ca15d 12 // this helper macro will be used.
mazgch 19:2b5d097ca15d 13 #define _C027DEFAULT(name) = name
mazgch 19:2b5d097ca15d 14 #else
mazgch 19:2b5d097ca15d 15 #define _C027DEFAULT(name)
mazgch 19:2b5d097ca15d 16 #endif
mazgch 18:e5697801df29 17
mazgch 38:e6cab4632d84 18 /** basic modem parser class
mazgch 38:e6cab4632d84 19 */
mazgch 18:e5697801df29 20 class MDMParser
mazgch 18:e5697801df29 21 {
mazgch 18:e5697801df29 22 public:
mazgch 31:a0bed6c1e05d 23 //! Constructor
mazgch 35:9275215a3a5b 24 MDMParser();
mazgch 44:9d12223b78ff 25 //! get static instance
mazgch 44:9d12223b78ff 26 static MDMParser* getInstance() { return inst; };
mazgch 31:a0bed6c1e05d 27
mazgch 31:a0bed6c1e05d 28 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 29 // Types
mazgch 31:a0bed6c1e05d 30 // ----------------------------------------------------------------
mazgch 38:e6cab4632d84 31 //! MT Device Types
mazgch 38:e6cab4632d84 32 typedef enum { DEV_UNKNOWN, DEV_SARA_G350, DEV_LISA_U200, DEV_LISA_C200 } Dev;
mazgch 38:e6cab4632d84 33 //! SIM Status
mazgch 38:e6cab4632d84 34 typedef enum { SIM_UNKNOWN, SIM_PIN, SIM_READY } Sim;
mazgch 38:e6cab4632d84 35 //! SIM Status
mazgch 38:e6cab4632d84 36 typedef enum { LPM_DISABLED, LPM_ENABLED, LPM_ACTIVE, LPM_SLEEP } Lpm;
mazgch 38:e6cab4632d84 37 //! Device status
mazgch 33:fb8fb5021b09 38 typedef struct {
mazgch 34:3b3b7807c0c3 39 Dev dev; //!< Device Type
mazgch 35:9275215a3a5b 40 Lpm lpm; //!< Power Saving
mazgch 34:3b3b7807c0c3 41 Sim sim; //!< SIM Card Status
mazgch 34:3b3b7807c0c3 42 char ccid[20+1]; //!< Integrated Circuit Card ID
mazgch 34:3b3b7807c0c3 43 char imsi[15+1]; //!< International Mobile Station Identity
mazgch 34:3b3b7807c0c3 44 char imei[15+1]; //!< International Mobile Equipment Identity
mazgch 34:3b3b7807c0c3 45 char meid[18+1]; //!< Mobile Equipment IDentifier
mazgch 34:3b3b7807c0c3 46 char manu[16]; //!< Manufacturer (u-blox)
mazgch 34:3b3b7807c0c3 47 char model[16]; //!< Model Name (LISA-U200, LISA-C200 or SARA-G350)
mazgch 34:3b3b7807c0c3 48 char ver[16]; //!< Software Version
mazgch 38:e6cab4632d84 49 } DevStatus;
mazgch 38:e6cab4632d84 50 //! Registration Status
mazgch 38:e6cab4632d84 51 typedef enum { REG_UNKNOWN, REG_DENIED, REG_NONE, REG_HOME, REG_ROAMING } Reg;
mazgch 38:e6cab4632d84 52 //! Access Technology
mazgch 38:e6cab4632d84 53 typedef enum { ACT_UNKNOWN, ACT_GSM, ACT_EDGE, ACT_UTRAN, ACT_CDMA } AcT;
mazgch 38:e6cab4632d84 54 //! Network Status
mazgch 33:fb8fb5021b09 55 typedef struct {
mazgch 34:3b3b7807c0c3 56 Reg reg; //!< Registration Status
mazgch 34:3b3b7807c0c3 57 AcT act; //!< Access Technology
mazgch 34:3b3b7807c0c3 58 int rssi; //!< Received Signal Strength Indication (in dBm, range -113..-53)
mazgch 54:7ba8e4c218e2 59 int ber; //!< Bit Error Rate (BER), see 3GPP TS 45.008 [20] subclause 8.2.4
mazgch 34:3b3b7807c0c3 60 char opr[16+1]; //!< Operator Name
mazgch 34:3b3b7807c0c3 61 char num[32]; //!< Mobile Directory Number
mazgch 54:7ba8e4c218e2 62 unsigned short lac; //!< location area code in hexadecimal format (2 bytes in hex)
mazgch 54:7ba8e4c218e2 63 unsigned int ci; //!< Cell ID in hexadecimal format (2 to 4 bytes in hex)
mazgch 38:e6cab4632d84 64 } NetStatus;
mazgch 38:e6cab4632d84 65 //! An IP v4 address
mazgch 38:e6cab4632d84 66 typedef uint32_t IP;
mazgch 33:fb8fb5021b09 67 #define NOIP ((MDMParser::IP)0) //!< No IP address
mazgch 31:a0bed6c1e05d 68 // ip number formating and conversion
mazgch 31:a0bed6c1e05d 69 #define IPSTR "%d.%d.%d.%d"
mazgch 31:a0bed6c1e05d 70 #define IPNUM(ip) ((ip)>>24)&0xff, \
mazgch 31:a0bed6c1e05d 71 ((ip)>>16)&0xff, \
mazgch 31:a0bed6c1e05d 72 ((ip)>> 8)&0xff, \
mazgch 31:a0bed6c1e05d 73 ((ip)>> 0)&0xff
mazgch 31:a0bed6c1e05d 74 #define IPADR(a,b,c,d) ((((IP)(a))<<24) | \
mazgch 31:a0bed6c1e05d 75 (((IP)(b))<<16) | \
mazgch 31:a0bed6c1e05d 76 (((IP)(c))<< 8) | \
mazgch 31:a0bed6c1e05d 77 (((IP)(d))<< 0))
mazgch 31:a0bed6c1e05d 78
mazgch 31:a0bed6c1e05d 79
mazgch 31:a0bed6c1e05d 80 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 81 // Data Connection (GPRS)
mazgch 31:a0bed6c1e05d 82 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 83
mazgch 31:a0bed6c1e05d 84 /** register (Attach) the MT to the GPRS service.
mazgch 31:a0bed6c1e05d 85 \param pin a optional pin of the SIM card
mazgch 31:a0bed6c1e05d 86 \param status an optional struture to with device information
mazgch 31:a0bed6c1e05d 87 \return true if successful, false otherwise
mazgch 31:a0bed6c1e05d 88 */
mazgch 31:a0bed6c1e05d 89 bool init(const char* pin = NULL, DevStatus* status = NULL);
mazgch 31:a0bed6c1e05d 90
mazgch 31:a0bed6c1e05d 91 /** check if the network is available
mazgch 31:a0bed6c1e05d 92 \param status an optional structure to with network information
mazgch 31:a0bed6c1e05d 93 \return true if successful and connected to network, false otherwise
mazgch 31:a0bed6c1e05d 94 */
mazgch 31:a0bed6c1e05d 95 bool checkNetStatus(NetStatus* status = NULL);
mazgch 31:a0bed6c1e05d 96
mazgch 31:a0bed6c1e05d 97 /** Power off the MT, This function has to be called prior to
mazgch 31:a0bed6c1e05d 98 switching off the supply.
mazgch 31:a0bed6c1e05d 99 \return true if successfully, false otherwise
mazgch 31:a0bed6c1e05d 100 */
mazgch 31:a0bed6c1e05d 101 bool powerOff(void);
mazgch 31:a0bed6c1e05d 102
mazgch 31:a0bed6c1e05d 103 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 104 // Data Connection (GPRS)
mazgch 31:a0bed6c1e05d 105 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 106
mazgch 31:a0bed6c1e05d 107 /** register (Attach) the MT to the GPRS service.
mazgch 31:a0bed6c1e05d 108 \param apn the of the network provider e.g. "internet" or "apn.provider.com"
mazgch 31:a0bed6c1e05d 109 \param user is the user name text string for the authentication phase
mazgch 31:a0bed6c1e05d 110 \param password is the password text string for the authentication phase
mazgch 31:a0bed6c1e05d 111 \return the ip that is assigned
mazgch 31:a0bed6c1e05d 112 */
mazgch 31:a0bed6c1e05d 113 MDMParser::IP join(const char* apn = NULL, const char* user = NULL, const char* password = NULL);
mazgch 31:a0bed6c1e05d 114
mazgch 31:a0bed6c1e05d 115 /** deregister (detach) the MT from the GPRS service.
mazgch 31:a0bed6c1e05d 116 \return true if successful, false otherwise
mazgch 31:a0bed6c1e05d 117 */
mazgch 31:a0bed6c1e05d 118 bool disconnect(void);
mazgch 31:a0bed6c1e05d 119
mazgch 31:a0bed6c1e05d 120 /** Translates a domain name to an IP address
mazgch 31:a0bed6c1e05d 121 \param host the domain name to translate e.g. "u-blox.com"
mazgch 31:a0bed6c1e05d 122 \return the IP if successful, 0 otherwise
mazgch 31:a0bed6c1e05d 123 */
mazgch 31:a0bed6c1e05d 124 MDMParser::IP gethostbyname(const char* host);
mazgch 31:a0bed6c1e05d 125
mazgch 31:a0bed6c1e05d 126 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 127 // Sockets
mazgch 31:a0bed6c1e05d 128 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 129
mazgch 31:a0bed6c1e05d 130 //! Type of IP protocol
mazgch 31:a0bed6c1e05d 131 typedef enum { IPPROTO_TCP, IPPROTO_UDP } IpProtocol;
mazgch 31:a0bed6c1e05d 132
mazgch 31:a0bed6c1e05d 133 //! Socket error return codes
mazgch 31:a0bed6c1e05d 134 #define SOCKET_ERROR -1
mazgch 31:a0bed6c1e05d 135
mazgch 31:a0bed6c1e05d 136 /** Create a socket for a ip protocol
mazgch 31:a0bed6c1e05d 137 \param ipproto the protocol (UDP or TCP)
mazgch 31:a0bed6c1e05d 138 \return the socket handle if successful or SOCKET_ERROR on failure
mazgch 31:a0bed6c1e05d 139 */
mazgch 31:a0bed6c1e05d 140 int socketSocket(IpProtocol ipproto);
mazgch 31:a0bed6c1e05d 141
mazgch 31:a0bed6c1e05d 142 /** make a socket connection
mazgch 31:a0bed6c1e05d 143 \param socket the socket handle
mazgch 31:a0bed6c1e05d 144 \param host the domain name to connect e.g. "u-blox.com"
mazgch 31:a0bed6c1e05d 145 \param port the port to connect
mazgch 31:a0bed6c1e05d 146 \return true if successfully, false otherwise
mazgch 31:a0bed6c1e05d 147 */
mazgch 31:a0bed6c1e05d 148 bool socketConnect(int socket, const char* host, int port);
mazgch 31:a0bed6c1e05d 149
mazgch 44:9d12223b78ff 150 /** make a socket connection
mazgch 44:9d12223b78ff 151 \param socket the socket handle
mazgch 44:9d12223b78ff 152 \return true if connected, false otherwise
mazgch 44:9d12223b78ff 153 */
mazgch 44:9d12223b78ff 154 bool socketIsConnected(int socket);
mazgch 44:9d12223b78ff 155
mazgch 44:9d12223b78ff 156 /** Get the number of bytes pending for reading for this socket
mazgch 44:9d12223b78ff 157 \param socket the socket handle
mazgch 44:9d12223b78ff 158 \param timeout_ms -1 blocking, else non blocking timeout in ms
mazgch 44:9d12223b78ff 159 \return 0 if successful or SOCKET_ERROR on failure
mazgch 44:9d12223b78ff 160 */
mazgch 44:9d12223b78ff 161 bool socketSetBlocking(int socket, unsigned int timeout_ms);
mazgch 44:9d12223b78ff 162
mazgch 31:a0bed6c1e05d 163 /** Write socket data
mazgch 31:a0bed6c1e05d 164 \param socket the socket handle
mazgch 31:a0bed6c1e05d 165 \param buf the buffer to write
mazgch 31:a0bed6c1e05d 166 \param len the size of the buffer to write
mazgch 31:a0bed6c1e05d 167 \return the size written or SOCKET_ERROR on failure
mazgch 31:a0bed6c1e05d 168 */
mazgch 31:a0bed6c1e05d 169 int socketSend(int socket, const char * buf, int len);
mazgch 31:a0bed6c1e05d 170
mazgch 31:a0bed6c1e05d 171 /** Write socket data to a IP
mazgch 31:a0bed6c1e05d 172 \param socket the socket handle
mazgch 31:a0bed6c1e05d 173 \param ip the ip to send to
mazgch 31:a0bed6c1e05d 174 \param port the port to send to
mazgch 31:a0bed6c1e05d 175 \param buf the buffer to write
mazgch 31:a0bed6c1e05d 176 \param len the size of the buffer to write
mazgch 31:a0bed6c1e05d 177 \return the size written or SOCKET_ERROR on failure
mazgch 31:a0bed6c1e05d 178 */
mazgch 31:a0bed6c1e05d 179 int socketSendTo(int socket, IP ip, int port, const char * buf, int len);
mazgch 31:a0bed6c1e05d 180
mazgch 31:a0bed6c1e05d 181 /** Get the number of bytes pending for reading for this socket
mazgch 31:a0bed6c1e05d 182 \param socket the socket handle
mazgch 31:a0bed6c1e05d 183 \return the number of bytes pending or SOCKET_ERROR on failure
mazgch 31:a0bed6c1e05d 184 */
mazgch 31:a0bed6c1e05d 185 int socketReadable(int socket);
mazgch 31:a0bed6c1e05d 186
mazgch 31:a0bed6c1e05d 187 /** Read this socket
mazgch 31:a0bed6c1e05d 188 \param socket the socket handle
mazgch 31:a0bed6c1e05d 189 \param buf the buffer to read into
mazgch 31:a0bed6c1e05d 190 \param len the size of the buffer to read into
mazgch 31:a0bed6c1e05d 191 \return the number of bytes read or SOCKET_ERROR on failure
mazgch 31:a0bed6c1e05d 192 */
mazgch 31:a0bed6c1e05d 193 int socketRecv(int socket, char* buf, int len);
mazgch 31:a0bed6c1e05d 194
mazgch 31:a0bed6c1e05d 195 /** Read from this socket
mazgch 31:a0bed6c1e05d 196 \param socket the socket handle
mazgch 31:a0bed6c1e05d 197 \param buf the buffer to read into
mazgch 31:a0bed6c1e05d 198 \param len the size of the buffer to read into
mazgch 31:a0bed6c1e05d 199 \param ip the ip of host where the data originates from
mazgch 31:a0bed6c1e05d 200 \return the number of bytes read or SOCKET_ERROR on failure
mazgch 31:a0bed6c1e05d 201 */
mazgch 31:a0bed6c1e05d 202 int socketRecvFrom(int socket, char* buf, int len, IP* ip);
mazgch 31:a0bed6c1e05d 203
mazgch 31:a0bed6c1e05d 204 /** Close a connectied socket (that was connected with #socketConnect)
mazgch 31:a0bed6c1e05d 205 \param socket the socket handle
mazgch 31:a0bed6c1e05d 206 \return true if successfully, false otherwise
mazgch 31:a0bed6c1e05d 207 */
mazgch 31:a0bed6c1e05d 208 bool socketClose(int socket);
mazgch 31:a0bed6c1e05d 209
mazgch 31:a0bed6c1e05d 210 /** Free the socket (that was allocated before by #socketSocket)
mazgch 31:a0bed6c1e05d 211 \param socket the socket handle
mazgch 31:a0bed6c1e05d 212 \return true if successfully, false otherwise
mazgch 31:a0bed6c1e05d 213 */
mazgch 31:a0bed6c1e05d 214 bool socketFree(int socket);
mazgch 31:a0bed6c1e05d 215
mazgch 31:a0bed6c1e05d 216 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 217 // SMS Short Message Service
mazgch 31:a0bed6c1e05d 218 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 219
mazgch 31:a0bed6c1e05d 220 /** count the number of sms in the device and optionally return a
mazgch 31:a0bed6c1e05d 221 list with indexes from the storage locations in the device.
mazgch 31:a0bed6c1e05d 222 \param stat what type of messages you can use use
mazgch 31:a0bed6c1e05d 223 "REC UNREAD", "REC READ", "STO UNSENT", "STO SENT", "ALL"
mazgch 31:a0bed6c1e05d 224 \param ix list where to save the storage positions
mazgch 31:a0bed6c1e05d 225 \param num number of elements in the list
mazgch 31:a0bed6c1e05d 226 \return the number of messages, this can be bigger than num, -1 on failure
mazgch 31:a0bed6c1e05d 227 */
mazgch 31:a0bed6c1e05d 228 int smsList(const char* stat = "ALL", int* ix = NULL, int num = 0);
mazgch 31:a0bed6c1e05d 229
mazgch 31:a0bed6c1e05d 230 /** Read a Message from a storage position
mazgch 31:a0bed6c1e05d 231 \param ix the storage position to read
mazgch 31:a0bed6c1e05d 232 \param num the originator address (~16 chars)
mazgch 31:a0bed6c1e05d 233 \param buf a buffer where to save the sm
mazgch 31:a0bed6c1e05d 234 \param len the length of the sm
mazgch 31:a0bed6c1e05d 235 \return true if successful, false otherwise
mazgch 31:a0bed6c1e05d 236 */
mazgch 31:a0bed6c1e05d 237 bool smsRead(int ix, char* num, char* buf, int len);
mazgch 31:a0bed6c1e05d 238
mazgch 31:a0bed6c1e05d 239 /** Send a message to a recipient
mazgch 31:a0bed6c1e05d 240 \param ix the storage position to delete
mazgch 31:a0bed6c1e05d 241 \return true if successful, false otherwise
mazgch 31:a0bed6c1e05d 242 */
mazgch 31:a0bed6c1e05d 243 bool smsDelete(int ix);
mazgch 31:a0bed6c1e05d 244
mazgch 31:a0bed6c1e05d 245 /** Send a message to a recipient
mazgch 31:a0bed6c1e05d 246 \param num the phone number of the recipient
mazgch 31:a0bed6c1e05d 247 \param buf the content of the message to sent
mazgch 31:a0bed6c1e05d 248 \return true if successful, false otherwise
mazgch 31:a0bed6c1e05d 249 */
mazgch 31:a0bed6c1e05d 250 bool smsSend(const char* num, const char* buf);
mazgch 31:a0bed6c1e05d 251
mazgch 31:a0bed6c1e05d 252 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 253 // USSD Unstructured Supplementary Service Data
mazgch 31:a0bed6c1e05d 254 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 255
mazgch 31:a0bed6c1e05d 256 /** Read a Message from a storage position
mazgch 31:a0bed6c1e05d 257 \param cmd the ussd command to send e.g "*#06#"
mazgch 31:a0bed6c1e05d 258 \param buf a buffer where to save the reply
mazgch 31:a0bed6c1e05d 259 \return true if successful, false otherwise
mazgch 31:a0bed6c1e05d 260 */
mazgch 31:a0bed6c1e05d 261 bool ussdCommand(const char* cmd, char* buf);
mazgch 31:a0bed6c1e05d 262
mazgch 38:e6cab4632d84 263 // ----------------------------------------------------------------
mazgch 54:7ba8e4c218e2 264 // DUMP status to standard out (printf)
mazgch 54:7ba8e4c218e2 265 // ----------------------------------------------------------------
mazgch 54:7ba8e4c218e2 266
mazgch 54:7ba8e4c218e2 267 /** dump the device status to stdout using printf
mazgch 54:7ba8e4c218e2 268 \param status the status to convert to textual form,
mazgch 54:7ba8e4c218e2 269 unavailable fields are ommited (not printed)
mazgch 54:7ba8e4c218e2 270 */
mazgch 54:7ba8e4c218e2 271 static void dumpDevStatus(MDMParser::DevStatus *status);
mazgch 54:7ba8e4c218e2 272
mazgch 54:7ba8e4c218e2 273 /** dump the network status to stdout using printf
mazgch 54:7ba8e4c218e2 274 \param status the status to convert to textual form,
mazgch 54:7ba8e4c218e2 275 unavailable fields are ommited (not printed)
mazgch 54:7ba8e4c218e2 276 */
mazgch 54:7ba8e4c218e2 277 static void dumpNetStatus(MDMParser::NetStatus *status);
mazgch 54:7ba8e4c218e2 278
mazgch 54:7ba8e4c218e2 279 /** dump the ip address to stdout using printf
mazgch 54:7ba8e4c218e2 280 \param ip the ip to convert to textual form,
mazgch 54:7ba8e4c218e2 281 unavailable fields are ommited (not printed)
mazgch 54:7ba8e4c218e2 282 */
mazgch 54:7ba8e4c218e2 283 static void dumpIp(MDMParser::IP ip);
mazgch 54:7ba8e4c218e2 284
mazgch 54:7ba8e4c218e2 285 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 286 // Parseing
mazgch 31:a0bed6c1e05d 287 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 288
mazgch 51:e7b81c31baec 289 enum {
mazgch 51:e7b81c31baec 290 // waitFinalResp Responses
mazgch 52:8071747a7cb3 291 NOT_FOUND = 0,
mazgch 52:8071747a7cb3 292 WAIT = -1, // TIMEOUT
mazgch 52:8071747a7cb3 293 RESP_OK = -2,
mazgch 52:8071747a7cb3 294 RESP_ERROR = -3,
mazgch 52:8071747a7cb3 295 RESP_PROMPT = -4,
mazgch 31:a0bed6c1e05d 296
mazgch 51:e7b81c31baec 297 // getLine Responses
mazgch 51:e7b81c31baec 298 #define LENGTH(x) (x & 0x00FFFF) //!< extract/mask the length
mazgch 51:e7b81c31baec 299 #define TYPE(x) (x & 0xFF0000) //!< extract/mask the type
mazgch 51:e7b81c31baec 300
mazgch 51:e7b81c31baec 301 TYPE_UNKNOWN = 0x000000,
mazgch 51:e7b81c31baec 302 TYPE_OK = 0x110000,
mazgch 51:e7b81c31baec 303 TYPE_ERROR = 0x120000,
mazgch 51:e7b81c31baec 304 TYPE_RING = 0x210000,
mazgch 51:e7b81c31baec 305 TYPE_CONNECT = 0x220000,
mazgch 51:e7b81c31baec 306 TYPE_NOCARRIER = 0x230000,
mazgch 51:e7b81c31baec 307 TYPE_NODIALTONE = 0x240000,
mazgch 51:e7b81c31baec 308 TYPE_BUSY = 0x250000,
mazgch 51:e7b81c31baec 309 TYPE_NOANSWER = 0x260000,
mazgch 51:e7b81c31baec 310 TYPE_PROMPT = 0x300000,
mazgch 51:e7b81c31baec 311 TYPE_PLUS = 0x400000,
mazgch 51:e7b81c31baec 312 TYPE_TEXT = 0x500000
mazgch 51:e7b81c31baec 313 };
mazgch 31:a0bed6c1e05d 314
mazgch 31:a0bed6c1e05d 315 /** Get a line from the physical interface. This function need
mazgch 31:a0bed6c1e05d 316 to be implemented in a inherited class. Usually just calls
mazgch 31:a0bed6c1e05d 317 #_getLine on the rx buffer pipe.
mazgch 31:a0bed6c1e05d 318
mazgch 31:a0bed6c1e05d 319 \param buf the buffer to store it
mazgch 31:a0bed6c1e05d 320 \param buf size of the buffer
mazgch 31:a0bed6c1e05d 321 \return type and length if something was found,
mazgch 31:a0bed6c1e05d 322 WAIT if not enough data is available
mazgch 31:a0bed6c1e05d 323 NOT_FOUND if nothing was found
mazgch 31:a0bed6c1e05d 324 */
mazgch 31:a0bed6c1e05d 325 virtual int getLine(char* buf, int len) = 0;
mazgch 28:4d9509e3b1cf 326
mazgch 31:a0bed6c1e05d 327 /** Write data to the device
mazgch 31:a0bed6c1e05d 328 \param buf the buffer to write
mazgch 31:a0bed6c1e05d 329 \param buf size of the buffer to write
mazgch 31:a0bed6c1e05d 330 \return bytes written
mazgch 31:a0bed6c1e05d 331 */
mazgch 31:a0bed6c1e05d 332 virtual int send(const char* buf, int len);
mazgch 21:c4d64830bf02 333
mazgch 31:a0bed6c1e05d 334 /** Write formated date to the physical interface (printf style)
mazgch 31:a0bed6c1e05d 335 \param fmt the format string
mazgch 31:a0bed6c1e05d 336 \param .. variable arguments to be formated
mazgch 31:a0bed6c1e05d 337 \return bytes written
mazgch 31:a0bed6c1e05d 338 */
mazgch 21:c4d64830bf02 339 int sendFormated(const char* format, ...);
mazgch 26:07be5faf8925 340
mazgch 31:a0bed6c1e05d 341 /** callback function for #waitFinalResp with void* as argument
mazgch 31:a0bed6c1e05d 342 \param type the #getLine response
mazgch 31:a0bed6c1e05d 343 \param buf the parsed line
mazgch 31:a0bed6c1e05d 344 \param len the size of the parsed line
mazgch 31:a0bed6c1e05d 345 \param param the optional argument passed to #waitFinalResp
mazgch 31:a0bed6c1e05d 346 \return WAIT if processing should continue,
mazgch 31:a0bed6c1e05d 347 any other value aborts #waitFinalResp and this retunr value retuned
mazgch 31:a0bed6c1e05d 348 */
mazgch 26:07be5faf8925 349 typedef int (*_CALLBACKPTR)(int type, const char* buf, int len, void* param);
mazgch 31:a0bed6c1e05d 350
mazgch 31:a0bed6c1e05d 351 /** Wait for a final respons
mazgch 31:a0bed6c1e05d 352 \param cb the optional callback function
mazgch 31:a0bed6c1e05d 353 \param param the optional callback function parameter
mazgch 31:a0bed6c1e05d 354 \param timeout_ms the timeout to wait
mazgch 31:a0bed6c1e05d 355 */
mazgch 31:a0bed6c1e05d 356 int waitFinalResp(_CALLBACKPTR cb = NULL,
mazgch 26:07be5faf8925 357 void* param = NULL,
mazgch 26:07be5faf8925 358 int timeout_ms = 5000);
mazgch 31:a0bed6c1e05d 359
mazgch 31:a0bed6c1e05d 360 /** template version of #waitFinalResp when using callbacks,
mazgch 31:a0bed6c1e05d 361 This template will allow the compiler to do type cheking but
mazgch 31:a0bed6c1e05d 362 internally symply casts the arguments and call the (void*)
mazgch 31:a0bed6c1e05d 363 version of #waitFinalResp.
mazgch 31:a0bed6c1e05d 364 \sa waitFinalResp
mazgch 31:a0bed6c1e05d 365 */
mazgch 26:07be5faf8925 366 template<class T>
mazgch 26:07be5faf8925 367 int waitFinalResp(int (*cb)(int type, const char* buf, int len,
mazgch 26:07be5faf8925 368 T* param),
mazgch 26:07be5faf8925 369 T* param, int timeout_ms = 5000)
mazgch 26:07be5faf8925 370 {
mazgch 26:07be5faf8925 371 return waitFinalResp((_CALLBACKPTR)cb, (void*)param, timeout_ms);
mazgch 26:07be5faf8925 372 }
mazgch 31:a0bed6c1e05d 373
mazgch 18:e5697801df29 374 protected:
mazgch 31:a0bed6c1e05d 375 /** Write bytes to the physical interface. This function should be
mazgch 31:a0bed6c1e05d 376 implemented in a inherited class.
mazgch 31:a0bed6c1e05d 377 \param buf the buffer to write
mazgch 31:a0bed6c1e05d 378 \param buf size of the buffer to write
mazgch 31:a0bed6c1e05d 379 \return bytes written
mazgch 31:a0bed6c1e05d 380 */
mazgch 18:e5697801df29 381 virtual int _send(const void* buf, int len) = 0;
mazgch 31:a0bed6c1e05d 382
mazgch 31:a0bed6c1e05d 383 /** Helper: Parse a line from the receiving buffered pipe
mazgch 31:a0bed6c1e05d 384 \param pipe the receiving buffer pipe
mazgch 31:a0bed6c1e05d 385 \param buf the parsed line
mazgch 31:a0bed6c1e05d 386 \param len the size of the parsed line
mazgch 31:a0bed6c1e05d 387 \return type and length if something was found,
mazgch 31:a0bed6c1e05d 388 WAIT if not enough data is available
mazgch 31:a0bed6c1e05d 389 NOT_FOUND if nothing was found
mazgch 31:a0bed6c1e05d 390 */
mazgch 31:a0bed6c1e05d 391 static int _getLine(Pipe<char>* pipe, char* buffer, int length);
mazgch 31:a0bed6c1e05d 392
mazgch 31:a0bed6c1e05d 393 /** Helper: Parse a match from the pipe
mazgch 31:a0bed6c1e05d 394 \param pipe the buffered pipe
mazgch 31:a0bed6c1e05d 395 \param number of bytes to parse at maximum,
mazgch 31:a0bed6c1e05d 396 \param sta the starting string, NULL if none
mazgch 31:a0bed6c1e05d 397 \param end the terminating string, NULL if none
mazgch 31:a0bed6c1e05d 398 \return size of parsed match
mazgch 31:a0bed6c1e05d 399 */
mazgch 31:a0bed6c1e05d 400 static int _parseMatch(Pipe<char>* pipe, int len, const char* sta, const char* end);
mazgch 31:a0bed6c1e05d 401
mazgch 31:a0bed6c1e05d 402 /** Helper: Parse a match from the pipe
mazgch 31:a0bed6c1e05d 403 \param pipe the buffered pipe
mazgch 31:a0bed6c1e05d 404 \param number of bytes to parse at maximum,
mazgch 31:a0bed6c1e05d 405 \param fmt the formating string (%d any number, %c any char of last %d len)
mazgch 31:a0bed6c1e05d 406 \return size of parsed match
mazgch 31:a0bed6c1e05d 407 */
mazgch 31:a0bed6c1e05d 408 static int _parseFormated(Pipe<char>* pipe, int len, const char* fmt);
mazgch 31:a0bed6c1e05d 409
mazgch 35:9275215a3a5b 410 protected:
mazgch 31:a0bed6c1e05d 411 // parsing callbacks for different AT commands and their parameter arguments
mazgch 31:a0bed6c1e05d 412 static int _cbString(int type, const char* buf, int len, char* str);
mazgch 31:a0bed6c1e05d 413 static int _cbInt(int type, const char* buf, int len, int* val);
mazgch 31:a0bed6c1e05d 414 // device
mazgch 32:8f12ac182bbb 415 static int _cbATI(int type, const char* buf, int len, Dev* dev);
mazgch 31:a0bed6c1e05d 416 static int _cbCPIN(int type, const char* buf, int len, Sim* sim);
mazgch 31:a0bed6c1e05d 417 static int _cbCCID(int type, const char* buf, int len, char* ccid);
mazgch 31:a0bed6c1e05d 418 // network
mazgch 54:7ba8e4c218e2 419 static int _cbCSQ(int type, const char* buf, int len, NetStatus* status);
mazgch 31:a0bed6c1e05d 420 static int _cbCOPS(int type, const char* buf, int len, NetStatus* status);
mazgch 31:a0bed6c1e05d 421 static int _cbCNUM(int type, const char* buf, int len, char* num);
mazgch 31:a0bed6c1e05d 422 static int _cbCGATT(int type, const char* buf, int len, int* state);
mazgch 31:a0bed6c1e05d 423 // sockets
mazgch 32:8f12ac182bbb 424 static int _cbCMIP(int type, const char* buf, int len, IP* ip);
mazgch 31:a0bed6c1e05d 425 static int _cbUPSND(int type, const char* buf, int len, int* act);
mazgch 31:a0bed6c1e05d 426 static int _cbUPSND(int type, const char* buf, int len, IP* ip);
mazgch 21:c4d64830bf02 427 static int _cbUDNSRN(int type, const char* buf, int len, IP* ip);
mazgch 21:c4d64830bf02 428 static int _cbUSOCR(int type, const char* buf, int len, int* socket);
mazgch 21:c4d64830bf02 429 static int _cbUSORD(int type, const char* buf, int len, char* out);
mazgch 21:c4d64830bf02 430 typedef struct { char* buf; IP ip; int port; } USORFparam;
mazgch 21:c4d64830bf02 431 static int _cbUSORF(int type, const char* buf, int len, USORFparam* param);
mazgch 21:c4d64830bf02 432 typedef struct { char* buf; char* num; } CMGRparam;
mazgch 21:c4d64830bf02 433 static int _cbCUSD(int type, const char* buf, int len, char* buf);
mazgch 31:a0bed6c1e05d 434 // sms
mazgch 31:a0bed6c1e05d 435 typedef struct { int* ix; int num; } CMGLparam;
mazgch 31:a0bed6c1e05d 436 static int _cbCMGL(int type, const char* buf, int len, CMGLparam* param);
mazgch 21:c4d64830bf02 437 static int _cbCMGR(int type, const char* buf, int len, CMGRparam* param);
mazgch 38:e6cab4632d84 438 // variables
mazgch 31:a0bed6c1e05d 439 DevStatus _dev; //!< collected device information
mazgch 31:a0bed6c1e05d 440 NetStatus _net; //!< collected network information
mazgch 31:a0bed6c1e05d 441 IP _ip; //!< assigned ip address
mazgch 31:a0bed6c1e05d 442 // management struture for sockets
mazgch 21:c4d64830bf02 443 typedef enum { SOCK_FREE, SOCK_CREATED, SOCK_CONNECTED } SockState;
mazgch 44:9d12223b78ff 444 typedef struct { SockState state; int pending; unsigned int timeout_ms; } SockCtrl;
mazgch 35:9275215a3a5b 445 SockCtrl _sockets[16];
mazgch 44:9d12223b78ff 446 static MDMParser* inst;
mazgch 18:e5697801df29 447 };
mazgch 18:e5697801df29 448
mazgch 18:e5697801df29 449 // -----------------------------------------------------------------------
mazgch 18:e5697801df29 450
mazgch 38:e6cab4632d84 451 /** modem class which uses a serial port
mazgch 38:e6cab4632d84 452 as physical interface.
mazgch 38:e6cab4632d84 453 */
mazgch 18:e5697801df29 454 class MDMSerial : public SerialPipe, public MDMParser
mazgch 17:296d94a006b4 455 {
mazgch 17:296d94a006b4 456 public:
mazgch 38:e6cab4632d84 457 /** Constructor
mazgch 38:e6cab4632d84 458
mazgch 38:e6cab4632d84 459 \param tx is the serial ports transmit pin (modem to CPU)
mazgch 38:e6cab4632d84 460 \param rx is the serial ports receive pin (CPU to modem)
mazgch 38:e6cab4632d84 461 \param baudrate the baudrate of the modem use 115200
mazgch 38:e6cab4632d84 462 \param rts is the serial ports ready to send pin (CPU to modem)
mazgch 38:e6cab4632d84 463 this pin is optional
mazgch 38:e6cab4632d84 464 \param cts is the serial ports clear to send pin (modem to CPU)
mazgch 38:e6cab4632d84 465 this pin is optional, but required for power saving to be enabled
mazgch 38:e6cab4632d84 466 \param rxSize the size of the serial rx buffer
mazgch 38:e6cab4632d84 467 \param txSize the size of the serial tx buffer
mazgch 38:e6cab4632d84 468 */
mazgch 19:2b5d097ca15d 469 MDMSerial(PinName tx _C027DEFAULT(MDMTXD),
mazgch 19:2b5d097ca15d 470 PinName rx _C027DEFAULT(MDMRXD),
mazgch 19:2b5d097ca15d 471 int baudrate _C027DEFAULT(MDMBAUD),
mazgch 35:9275215a3a5b 472 #if DEVICE_SERIAL_FC
mazgch 19:2b5d097ca15d 473 PinName rts _C027DEFAULT(MDMRTS),
mazgch 19:2b5d097ca15d 474 PinName cts _C027DEFAULT(MDMCTS),
mazgch 35:9275215a3a5b 475 #endif
mazgch 19:2b5d097ca15d 476 int rxSize = 256 ,
mazgch 21:c4d64830bf02 477 int txSize = 128 );
mazgch 38:e6cab4632d84 478 /** Get a line from the physical interface.
mazgch 38:e6cab4632d84 479 \param buf the buffer to store it
mazgch 38:e6cab4632d84 480 \param buf size of the buffer
mazgch 38:e6cab4632d84 481 \return type and length if something was found,
mazgch 38:e6cab4632d84 482 WAIT if not enough data is available
mazgch 38:e6cab4632d84 483 NOT_FOUND if nothing was found
mazgch 38:e6cab4632d84 484 */
mazgch 18:e5697801df29 485 virtual int getLine(char* buffer, int length);
mazgch 18:e5697801df29 486 protected:
mazgch 38:e6cab4632d84 487 /** Write bytes to the physical interface.
mazgch 38:e6cab4632d84 488 \param buf the buffer to write
mazgch 38:e6cab4632d84 489 \param buf size of the buffer to write
mazgch 38:e6cab4632d84 490 \return bytes written
mazgch 38:e6cab4632d84 491 */
mazgch 18:e5697801df29 492 virtual int _send(const void* buf, int len);
mazgch 17:296d94a006b4 493 };
mazgch 18:e5697801df29 494
mazgch 18:e5697801df29 495 // -----------------------------------------------------------------------
mazgch 18:e5697801df29 496
mazgch 38:e6cab4632d84 497 //#define HAVE_MDMUSB
mazgch 18:e5697801df29 498 #ifdef HAVE_MDMUSB
mazgch 18:e5697801df29 499 class MDMUsb : /*public UsbSerial,*/ public MDMParser
mazgch 18:e5697801df29 500 {
mazgch 18:e5697801df29 501 public:
mazgch 18:e5697801df29 502 MDMUsb(void);
mazgch 18:e5697801df29 503 virtual int getLine(char* buffer, int length);
mazgch 18:e5697801df29 504 protected:
mazgch 18:e5697801df29 505 virtual int _send(const void* buf, int len);
mazgch 18:e5697801df29 506 };
mazgch 18:e5697801df29 507 #endif
mazgch 18:e5697801df29 508
mazgch 18:e5697801df29 509