Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

MDM.h

Committer:
mazgch
Date:
2014-04-08
Revision:
28:4d9509e3b1cf
Parent:
26:07be5faf8925
Child:
29:53d346010624

File content as of revision 28:4d9509e3b1cf:

#pragma once 

#include "mbed.h"
#include <stdarg.h>

#include "Pipe.h"
#include "SerialPipe.h"

#ifdef TARGET_UBLOX_C027
 // if we detect the C027 platform we will assign the 
 // default pinname and baudrate in the constructor 
 // this helper macro will be used. 
 #define _C027DEFAULT(name) = name
#else
 #define _C027DEFAULT(name)
#endif

class MDMParser
{
public:
    // 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 
    typedef uint32_t IP;
    
    typedef enum { MODEL_UNKNOWN, MODEL_SARA_G350, MODEL_LISA_U200, MODEL_LISA_C200 } Model; 
    typedef enum { SIM_UNKNOWN, SIM_PIN, SIM_READY } Sim; 
    typedef struct { Model model; Sim sim; const char* imsi; const char* imei; const char* ccid; } DevStatus;
    typedef enum { NET_UNKNOWN, NET_DENIED, NET_NONE, NET_HOME, NET_ROAMING } Net; 
    typedef enum { ACT_UNKNOWN, ACT_GSM, ACT_EDGE, ACT_UTRAN, ACT_CDMA } AcT; 
    typedef struct { const char* num; const char* opr; int rssi; Net net; AcT act; } NetStatus;
    
    MDMParser(void);
    
    // interaction with AT command interface
    virtual int getLine(char* buf, int len) = 0; 
    virtual int send(const char* buf, int len);
    int sendFormated(const char* format, ...);
    
    typedef int (*_CALLBACKPTR)(int type, const char* buf, int len, void* param);
    int waitFinalResp(_CALLBACKPTR = NULL, 
                      void* param = NULL, 
                      int timeout_ms = 5000);
    template<class T>
    int waitFinalResp(int (*cb)(int type, const char* buf, int len, 
                      T* param), 
                      T* param, int timeout_ms = 5000) 
    {
        return waitFinalResp((_CALLBACKPTR)cb, (void*)param, timeout_ms);
    }
    // network
    bool init(const char* pin = NULL, DevStatus* status = NULL);
    bool checkNetStatus(NetStatus* status = NULL);
    bool powerOff(void);
    // internet connection
    bool join(const char* apn = NULL, const char* user = NULL, const char* password = NULL);
    bool disconnect(void);
    bool gethostbyname(const char* host, IP* ip);
    // socket interface
    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 _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:
    static int _cbATI(int type, const char* buf, int len, Model* model);
    static int _cbCIMI(int type, const char* buf, int len, char* imsi);
    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 int _cbCGSN(int type, const char* buf, int len, char* imei);
    static int _cbGSN(int type, const char* buf, int len, char* imei);
    static int _cbCCID(int type, const char* buf, int len, char* ccid);
    static IP strToIp(const char* str);
    IP _ip;
    Model _model;
    Sim _sim;
    Net _net;
    AcT _act;
    char _num[32];
    char _opr[32];
    int _rssi;
    char _imsi[32];
    char _imei[32];
    char _ccid[32];
private:
    typedef enum { SOCK_FREE, SOCK_CREATED, SOCK_CONNECTED } SockState;
    typedef struct { SockState state; int pending; } SockCtrl;
    SockCtrl _sockets[16];    
};

// -----------------------------------------------------------------------

class MDMSerial :  public SerialPipe, public MDMParser
{
public: 
    MDMSerial(PinName tx    _C027DEFAULT(MDMTXD), 
              PinName rx    _C027DEFAULT(MDMRXD), 
              int baudrate  _C027DEFAULT(MDMBAUD),
#if DEVICE_SERIAL_FC
              PinName rts   _C027DEFAULT(MDMRTS), 
              PinName cts   _C027DEFAULT(MDMCTS),
#endif
              int rxSize    = 256 , 
              int txSize    = 128 );
    virtual int getLine(char* buffer, int length);
protected:
    virtual int _send(const void* buf, int len);
};

// -----------------------------------------------------------------------

#define HAVE_MDMUSB
#ifdef HAVE_MDMUSB
class MDMUsb :  /*public UsbSerial,*/ public MDMParser
{
public: 
    MDMUsb(void);
    virtual int getLine(char* buffer, int length);
protected:
    virtual int _send(const void* buf, int len);
};
#endif