* add C027_Support fork

Fork of C027_Support by u-blox

MDM.cpp

Committer:
mazgch
Date:
2014-03-24
Revision:
19:2b5d097ca15d
Parent:
18:e5697801df29
Child:
21:c4d64830bf02

File content as of revision 19:2b5d097ca15d:

#include "mbed.h"
#include <ctype.h>
#include "MDM.h"


int MDMParser::send(const char* buf, int len)
{
    return _send(buf, len);
}

int MDMParser::_getLine(Pipe<char>* pipe, char* buffer, int length)
{
    int o = 0;
    int i = 0;
    int l = pipe->start();
    while ((i < l) && (o < length))
    {
        int t = pipe->next();
        i ++;
        if (t == '\r')     // terminate commands with carriage return
        {
            pipe->done();
            if (length > o)
                buffer[o] = '\0';
            return o;          // if enter send the zero char
        }
        else if (t == '\n')     // skip/filter new line 
             /* skip */;
        else if (t != '\b')     // normal char (no backspace)
            buffer[o++] = t;
        else if (o > 0)         // backspace
            o --;               // remove it
    }
    o = 0;
    if (length > 0)
        buffer[0] = '\0';
    return WAIT;
}

int MDMParser::_getResp(Pipe<char>* pipe, char* buffer, int length)
{
    int o = 0;
    int i = 0;
    int l = pipe->start();
    static const char erTxt[] = "ERROR\r\n";
    static const char okTxt[] = "OK\r\n";
    int er = 0;
    int ok = 0;
    while ((i < pipe->size()) && (o < length))
    {
        int t = pipe->next();
        i ++;
        buffer[o++] = t;
        ok = (t == okTxt[ok]) ? ok + 1 : 0;
        er = (t == erTxt[er]) ? er + 1 : 0;
        if ((okTxt[ok] == '\0') || (erTxt[er] == '\0'))
        {
            pipe->done();
            if (length > o)
                buffer[o] = '\0';
            return o;
        }
    }
    o = 0;
    if (length > 0)
        buffer[0] = '\0';
    return WAIT;
}

// ----------------------------------------------------------------
// Serial Implementation 
// ----------------------------------------------------------------

MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, 
            int baudrate /*= MDMBAUD*/,
#if DEVICE_SERIAL_FC
            PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/, 
#endif
            int rxSize /*= 256*/, int txSize /*= 128*/) : 
#if DEVICE_SERIAL_FC
            SerialPipe(tx, rx, rts, cts, rxSize, txSize)
#else
            SerialPipe(tx, rx, rxSize, txSize)
#endif
{
    baud(baudrate);
}

int MDMSerial::_send(const void* buf, int len)   
{ 
    return put((const char*)buf, len, true/*=blocking*/); 
}

int MDMSerial::getLine(char* buffer, int length)
{
    return _getLine(&_pipeRx, buffer, length);
}

int MDMSerial::getResp(char* buffer, int length)
{
    return _getResp(&_pipeRx, buffer, length);
}

// ----------------------------------------------------------------
// USB Implementation 
// ----------------------------------------------------------------

#ifdef HAVE_MDMUSB
// TODO properly implement with USB 
MDMUsb::MDMUsb(void)                             { }
int MDMUsb::_send(const void* buf, int len)      { return len; }
int MDMUsb::getLine(char* buffer, int length)    { return NOT_FOUND; }
int MDMUsb::getResp(char* buffer, int length)    { return NOT_FOUND; }
#endif