C027 support

Fork of C027_Support by u-blox

Revision:
21:c4d64830bf02
Parent:
19:2b5d097ca15d
Child:
22:29322c22577e
diff -r 2b5d097ca15d -r c4d64830bf02 MDM.h
--- a/MDM.h	Mon Mar 24 07:38:05 2014 +0000
+++ b/MDM.h	Tue Apr 08 09:15:37 2014 +0000
@@ -1,6 +1,8 @@
 #pragma once 
 
 #include "mbed.h"
+#include <stdarg.h>
+
 #include "Pipe.h"
 #include "SerialPipe.h"
 
@@ -13,22 +15,97 @@
  #define _C027DEFAULT(name)
 #endif
 
+typedef uint32_t IP;
+    
 class MDMParser
 {
 public:
-    #define WAIT      -1
-    #define NOT_FOUND  0
+    // waitFinalResp Responses
+    #define NOT_FOUND    0
+    #define WAIT        -1 // TIMEOUT
+    #define OK          -2 
+    #define ERROR       -3
+    #define PROMPT      -4
+    // getLine Responses
+    #define LENGTH(x)  (x & 0x00FFFF)
+    #define TYPE(x)    (x & 0xFF0000)
+    #define TYPE_UNKNOWN    0x000000
+    #define TYPE_OK         0x110000
+    #define TYPE_ERROR      0x120000
+    #define TYPE_RING       0x210000
+    #define TYPE_CONNECT    0x220000
+    #define TYPE_NOCARRIER  0x230000
+    #define TYPE_NODIALTONE 0x240000
+    #define TYPE_BUSY       0x250000
+    #define TYPE_NOANSWER   0x260000
+    #define TYPE_PROMPT     0x300000
+    #define TYPE_PLUS       0x400000
+    // Socket Return Codes
+    #define SOCKET_ERROR -1
+    #define SOCKET_OK     0 
+    
+    MDMParser(void);
     
-    #define LENGTH(x)   (x & 0x00FFFF)
-//    #define PROTOCOL(x) (x & 0xFF0000)
+    // interaction with AT command interface
     virtual int getLine(char* buf, int len) = 0; 
-    virtual int getResp(char* buf, int len) = 0; 
     virtual int send(const char* buf, int len);
-    
+    int sendFormated(const char* format, ...);
+    typedef int (*_CB)(int type, const char* buf, int len, void* param);
+    int waitFinalResp(_CB cb = NULL, void* param = NULL, int timeout_ms = 5000);
+
+    // network
+    bool init(const char* pin = NULL);
+    bool checkNetStatus(void);
+    bool powerOff(void);
+    // internet connection
+    bool join(const char* apn, const char* user = NULL, const char* password = NULL);
+    bool disconnect(void);
+    bool gethostbyname(const char* host, IP* ip);
+    // sockets
+    typedef enum { IPPROTO_TCP, IPPROTO_UDP } IpProtocol;
+    int socketSocket(IpProtocol ipproto);
+    bool socketConnect(int socket, const char * host, int port);
+    int socketSend(int socket, const char * buf, int len);
+    int socketSendTo(int socket, IP ip, int port, const char * buf, int len);
+    int socketReadable(int socket);
+    int socketRecv(int socket, char* buf, int len);
+    int socketRecvFrom(int socket, char* buf, int len, IP* ip);
+    bool socketClose(int socket);
+    bool socketFree(int socket);
+    // sms
+    bool smsSend(const char* num, const char* buf);
+    bool smsDelete(int ix);
+    bool smsRead(int ix, char* num, char* buf, int len);
+    // ussd
+    int ussdCommand(const char* cmd, char* buf, int len);
 protected:
     static int _getLine(Pipe<char>* pipe, char* buffer, int length);
-    static int _getResp(Pipe<char>* pipe, char* buffer, int length);
+    static int _parseMatch(Pipe<char>* pipe, int len, const char* sta, const char* end);
+    static int _parseFormated(Pipe<char>* pipe, int len, const char* fmt);
     virtual int _send(const void* buf, int len) = 0;
+private:
+    typedef enum { MODEL_UNKNOWN, MODEL_SARA_G350, MODEL_LISA_U200, MODEL_LISA_C200 } Model; 
+    typedef enum { SIM_UNKNOWN, SIM_PIN, SIM_READY } Sim; 
+    typedef enum { NET_UNKNOWN, NET_DENIED, NET_NONE, NET_HOME, NET_ROAMING } Net; 
+    static int _cbATI(int type, const char* buf, int len, Model* model);
+    static int _cbUDNSRN(int type, const char* buf, int len, IP* ip);
+    static int _cbUSOCR(int type, const char* buf, int len, int* socket);
+    static int _cbUSORD(int type, const char* buf, int len, char* out);
+    typedef struct { char* buf; IP ip; int port; } USORFparam;
+    static int _cbUSORF(int type, const char* buf, int len, USORFparam* param);
+    typedef struct { char* buf; char* num; } CMGRparam;
+    static int _cbCUSD(int type, const char* buf, int len, char* buf);
+    static int _cbCMGR(int type, const char* buf, int len, CMGRparam* param);
+    static IP strToIp(const char* str);
+    IP _ip;
+    Model _model;
+    Sim _sim;
+    Net _net;
+    int _rssi;
+private:
+    typedef enum { SOCK_FREE, SOCK_CREATED, SOCK_CONNECTED } SockState;
+    typedef struct { SockState state; int pending; } SockCtrl;
+    SockCtrl _sockets[16];    
 };
 
 // -----------------------------------------------------------------------
@@ -44,9 +121,8 @@
               PinName cts   _C027DEFAULT(MDMCTS),
 #endif
               int rxSize    = 256 , 
-              int txSize    = 256 );
+              int txSize    = 128 );
     virtual int getLine(char* buffer, int length);
-    virtual int getResp(char* buffer, int length);
 protected:
     virtual int _send(const void* buf, int len);
 };
@@ -60,7 +136,6 @@
 public: 
     MDMUsb(void);
     virtual int getLine(char* buffer, int length);
-    virtual int getResp(char* buffer, int length);
 protected:
     virtual int _send(const void* buf, int len);
 };