Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

MDM.h

Committer:
mazgch
Date:
2014-04-08
Revision:
23:05a1aeeb5fd9
Parent:
22:29322c22577e
Child:
24:0e287a85ac9e

File content as of revision 23:05a1aeeb5fd9:

#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;
    
    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 (*_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);
    // 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:
    typedef enum { MODEL_UNKNOWN, MODEL_SARA_G350, MODEL_LISA_U200, MODEL_LISA_C200 } Model; 
    typedef enum { NET_UNKNOWN, NET_DENIED, NET_NONE, NET_HOME, NET_ROAMING } Net; 
    typedef enum { SIM_UNKNOWN, SIM_PIN, SIM_READY } Sim; 
    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;
    char _num[32];
    int _rssi;
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