* add C027_Support fork

Fork of C027_Support by u-blox

Committer:
mazgch
Date:
Mon May 26 11:55:16 2014 +0000
Revision:
79:291df065e345
Parent:
76:f7c3dd568dae
Child:
80:34985b4d821e
added different Authentication protocols handle gprs registration

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