Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

SerialPipe.cpp

Committer:
mazgch
Date:
2014-01-31
Revision:
17:296d94a006b4
Parent:
16:4a7ba1887e81
Child:
18:e5697801df29

File content as of revision 17:296d94a006b4:

#pragma once 

#include "SerialPipe.h"

SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize) 
    : _SerialPipeBase(tx,rx), _pipeRx(rxSize), _pipeTx(txSize)
{
    attach(this, &SerialPipe::rxIrqBuf, RxIrq);
    attach(this, &SerialPipe::txIrqBuf, TxIrq);
}

SerialPipe::SerialPipe(PinName tx, PinName rx, PinName rts, PinName cts, 
    int rxSize, int txSize) 
    : _SerialPipeBase(tx,rx), _pipeRx(rxSize), _pipeTx(txSize)
{
    attach(this, &SerialPipe::rxIrqBuf, RxIrq);
    attach(this, &SerialPipe::txIrqBuf, TxIrq);
    
    set_flow_control(RTSCTS, rts, cts);
}

SerialPipe::~SerialPipe(void)
{
    attach(NULL, RxIrq);
    attach(NULL, TxIrq);
}

// tx channel
int SerialPipe::writeable(void)    
{
    return _pipeTx.free();
}

int SerialPipe::putc(int c)    
{
    c = _pipeTx.putc(c);
    txStart();
    return c;
}

int SerialPipe::put(const void* buffer, int length, bool blocking)    
{ 
    int count = length;
    const char* ptr = (const char*)buffer;
    if (count)
    {
        do
        {
            int written = _pipeTx.put(ptr, count, false);
            ptr += written;
            count -= written;
            txStart();
        }
        while (count && blocking);
    }
    return (length - count);
}

void SerialPipe::txIrqBuf(void)
{
    while (_SerialPipeBase::writeable() && _pipeTx.readable())
    {
        char c = _pipeTx.getc();
        _SerialPipeBase::_base_putc(c);
    }
}

void SerialPipe::txStart(void)
{
    __disable_irq();
    txIrqBuf();
    __enable_irq();
}

// rx channel
int SerialPipe::readable(void)                      
{ 
    return _pipeRx.readable(); 
} 

int SerialPipe::getc(void)                          
{ 
    if (!_pipeRx.readable())
        return EOF;
    return _pipeRx.getc(); 
} 

int SerialPipe::get(void* buffer, int length, bool blocking) 
{ 
    return _pipeRx.get((char*)buffer,length,blocking); 
}

void SerialPipe::rxIrqBuf(void)
{
    while (_SerialPipeBase::readable())
    {
        char c = _SerialPipeBase::_base_getc();
        if (_pipeRx.writeable())
            _pipeRx.putc(c);
        else 
            /* overflow */;
    }
}

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

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

int SerialPipeEx::getLine(char* buffer, int length, Pipe<char>* pipe)
{
    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 SerialPipeEx::getResp(char* buffer, int length)
{
    return getResp(buffer, length, &_pipeRx);
}

int SerialPipeEx::getResp(char* buffer, int length, Pipe<char>* pipe)
{
    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;
}