Add a bunch of APNs
Fork of C027_Support by
SerialPipe.cpp@16:4a7ba1887e81, 2013-12-06 (annotated)
- Committer:
- mazgch
- Date:
- Fri Dec 06 09:28:43 2013 +0000
- Revision:
- 16:4a7ba1887e81
- Parent:
- 15:5eda64e5b9d1
- Child:
- 17:296d94a006b4
Added parsing helpers for AT interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 9:e7a5959ffae1 | 1 | #pragma once |
mazgch | 9:e7a5959ffae1 | 2 | |
mazgch | 9:e7a5959ffae1 | 3 | #include "SerialPipe.h" |
mazgch | 9:e7a5959ffae1 | 4 | |
mazgch | 15:5eda64e5b9d1 | 5 | SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize) |
mazgch | 15:5eda64e5b9d1 | 6 | : _SerialPipeBase(tx,rx), _pipeRx(rxSize), _pipeTx(txSize) |
mazgch | 9:e7a5959ffae1 | 7 | { |
mazgch | 9:e7a5959ffae1 | 8 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
mazgch | 9:e7a5959ffae1 | 9 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
mazgch | 9:e7a5959ffae1 | 10 | } |
mazgch | 9:e7a5959ffae1 | 11 | |
mazgch | 9:e7a5959ffae1 | 12 | SerialPipe::~SerialPipe(void) |
mazgch | 9:e7a5959ffae1 | 13 | { |
mazgch | 9:e7a5959ffae1 | 14 | attach(NULL, RxIrq); |
mazgch | 9:e7a5959ffae1 | 15 | attach(NULL, TxIrq); |
mazgch | 9:e7a5959ffae1 | 16 | } |
mazgch | 9:e7a5959ffae1 | 17 | |
mazgch | 9:e7a5959ffae1 | 18 | // tx channel |
mazgch | 13:e2446fcdc246 | 19 | int SerialPipe::writeable(void) |
mazgch | 13:e2446fcdc246 | 20 | { |
mazgch | 13:e2446fcdc246 | 21 | return _pipeTx.free(); |
mazgch | 13:e2446fcdc246 | 22 | } |
mazgch | 13:e2446fcdc246 | 23 | |
mazgch | 13:e2446fcdc246 | 24 | int SerialPipe::putc(int c) |
mazgch | 13:e2446fcdc246 | 25 | { |
mazgch | 15:5eda64e5b9d1 | 26 | c = _pipeTx.putc(c); |
mazgch | 15:5eda64e5b9d1 | 27 | txStart(); |
mazgch | 15:5eda64e5b9d1 | 28 | return c; |
mazgch | 13:e2446fcdc246 | 29 | } |
mazgch | 13:e2446fcdc246 | 30 | |
mazgch | 13:e2446fcdc246 | 31 | int SerialPipe::put(const void* buffer, int length, bool blocking) |
mazgch | 9:e7a5959ffae1 | 32 | { |
mazgch | 13:e2446fcdc246 | 33 | int count = length; |
mazgch | 13:e2446fcdc246 | 34 | const char* ptr = (const char*)buffer; |
mazgch | 15:5eda64e5b9d1 | 35 | if (count) |
mazgch | 9:e7a5959ffae1 | 36 | { |
mazgch | 15:5eda64e5b9d1 | 37 | do |
mazgch | 15:5eda64e5b9d1 | 38 | { |
mazgch | 15:5eda64e5b9d1 | 39 | int written = _pipeTx.put(ptr, count, false); |
mazgch | 15:5eda64e5b9d1 | 40 | ptr += written; |
mazgch | 15:5eda64e5b9d1 | 41 | count -= written; |
mazgch | 15:5eda64e5b9d1 | 42 | txStart(); |
mazgch | 15:5eda64e5b9d1 | 43 | } |
mazgch | 15:5eda64e5b9d1 | 44 | while (count && blocking); |
mazgch | 9:e7a5959ffae1 | 45 | } |
mazgch | 13:e2446fcdc246 | 46 | return (length - count); |
mazgch | 13:e2446fcdc246 | 47 | } |
mazgch | 13:e2446fcdc246 | 48 | |
mazgch | 9:e7a5959ffae1 | 49 | void SerialPipe::txIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 50 | { |
mazgch | 15:5eda64e5b9d1 | 51 | while (_SerialPipeBase::writeable() && _pipeTx.readable()) |
mazgch | 11:b084552b03fe | 52 | { |
mazgch | 13:e2446fcdc246 | 53 | char c = _pipeTx.getc(); |
mazgch | 15:5eda64e5b9d1 | 54 | _SerialPipeBase::_base_putc(c); |
mazgch | 11:b084552b03fe | 55 | } |
mazgch | 9:e7a5959ffae1 | 56 | } |
mazgch | 9:e7a5959ffae1 | 57 | |
mazgch | 13:e2446fcdc246 | 58 | void SerialPipe::txStart(void) |
mazgch | 13:e2446fcdc246 | 59 | { |
mazgch | 13:e2446fcdc246 | 60 | __disable_irq(); |
mazgch | 13:e2446fcdc246 | 61 | txIrqBuf(); |
mazgch | 13:e2446fcdc246 | 62 | __enable_irq(); |
mazgch | 13:e2446fcdc246 | 63 | } |
mazgch | 13:e2446fcdc246 | 64 | |
mazgch | 9:e7a5959ffae1 | 65 | // rx channel |
mazgch | 9:e7a5959ffae1 | 66 | int SerialPipe::readable(void) |
mazgch | 9:e7a5959ffae1 | 67 | { |
mazgch | 9:e7a5959ffae1 | 68 | return _pipeRx.readable(); |
mazgch | 9:e7a5959ffae1 | 69 | } |
mazgch | 9:e7a5959ffae1 | 70 | |
mazgch | 9:e7a5959ffae1 | 71 | int SerialPipe::getc(void) |
mazgch | 9:e7a5959ffae1 | 72 | { |
mazgch | 15:5eda64e5b9d1 | 73 | if (!_pipeRx.readable()) |
mazgch | 15:5eda64e5b9d1 | 74 | return EOF; |
mazgch | 15:5eda64e5b9d1 | 75 | return _pipeRx.getc(); |
mazgch | 13:e2446fcdc246 | 76 | } |
mazgch | 13:e2446fcdc246 | 77 | |
mazgch | 13:e2446fcdc246 | 78 | int SerialPipe::get(void* buffer, int length, bool blocking) |
mazgch | 13:e2446fcdc246 | 79 | { |
mazgch | 13:e2446fcdc246 | 80 | return _pipeRx.get((char*)buffer,length,blocking); |
mazgch | 13:e2446fcdc246 | 81 | } |
mazgch | 13:e2446fcdc246 | 82 | |
mazgch | 9:e7a5959ffae1 | 83 | void SerialPipe::rxIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 84 | { |
mazgch | 15:5eda64e5b9d1 | 85 | while (_SerialPipeBase::readable()) |
mazgch | 9:e7a5959ffae1 | 86 | { |
mazgch | 15:5eda64e5b9d1 | 87 | char c = _SerialPipeBase::_base_getc(); |
mazgch | 9:e7a5959ffae1 | 88 | if (_pipeRx.writeable()) |
mazgch | 13:e2446fcdc246 | 89 | _pipeRx.putc(c); |
mazgch | 9:e7a5959ffae1 | 90 | else |
mazgch | 9:e7a5959ffae1 | 91 | /* overflow */; |
mazgch | 9:e7a5959ffae1 | 92 | } |
mazgch | 9:e7a5959ffae1 | 93 | } |
mazgch | 9:e7a5959ffae1 | 94 | |
mazgch | 9:e7a5959ffae1 | 95 | // ----------------------------------------------------------------------- |
mazgch | 9:e7a5959ffae1 | 96 | |
mazgch | 15:5eda64e5b9d1 | 97 | SerialPipeEx::SerialPipeEx(PinName tx, PinName rx, int rxSize, int txSize) |
mazgch | 15:5eda64e5b9d1 | 98 | : SerialPipe(tx,rx,rxSize,txSize) |
mazgch | 9:e7a5959ffae1 | 99 | {} |
mazgch | 9:e7a5959ffae1 | 100 | |
mazgch | 13:e2446fcdc246 | 101 | int SerialPipeEx::getLine(char* buffer, int length) |
mazgch | 9:e7a5959ffae1 | 102 | { |
mazgch | 14:69c3e57ef0f5 | 103 | return getLine(buffer, length, &_pipeRx); |
mazgch | 14:69c3e57ef0f5 | 104 | } |
mazgch | 14:69c3e57ef0f5 | 105 | |
mazgch | 14:69c3e57ef0f5 | 106 | int SerialPipeEx::getLine(char* buffer, int length, Pipe<char>* pipe) |
mazgch | 14:69c3e57ef0f5 | 107 | { |
mazgch | 9:e7a5959ffae1 | 108 | int o = 0; |
mazgch | 9:e7a5959ffae1 | 109 | int i = 0; |
mazgch | 14:69c3e57ef0f5 | 110 | int l = pipe->start(); |
mazgch | 13:e2446fcdc246 | 111 | while ((i < l) && (o < length)) |
mazgch | 9:e7a5959ffae1 | 112 | { |
mazgch | 14:69c3e57ef0f5 | 113 | int t = pipe->next(); |
mazgch | 9:e7a5959ffae1 | 114 | i ++; |
mazgch | 9:e7a5959ffae1 | 115 | if (t == '\r') // terminate commands with carriage return |
mazgch | 9:e7a5959ffae1 | 116 | { |
mazgch | 14:69c3e57ef0f5 | 117 | pipe->done(); |
mazgch | 13:e2446fcdc246 | 118 | if (length > o) |
mazgch | 13:e2446fcdc246 | 119 | buffer[o] = '\0'; |
mazgch | 12:684b31d5482b | 120 | return o; // if enter send the zero char |
mazgch | 9:e7a5959ffae1 | 121 | } |
mazgch | 9:e7a5959ffae1 | 122 | else if (t == '\n') // skip/filter new line |
mazgch | 9:e7a5959ffae1 | 123 | /* skip */; |
mazgch | 9:e7a5959ffae1 | 124 | else if (t != '\b') // normal char (no backspace) |
mazgch | 13:e2446fcdc246 | 125 | buffer[o++] = t; |
mazgch | 9:e7a5959ffae1 | 126 | else if (o > 0) // backspace |
mazgch | 9:e7a5959ffae1 | 127 | o --; // remove it |
mazgch | 9:e7a5959ffae1 | 128 | } |
mazgch | 9:e7a5959ffae1 | 129 | o = 0; |
mazgch | 13:e2446fcdc246 | 130 | if (length > 0) |
mazgch | 13:e2446fcdc246 | 131 | buffer[0] = '\0'; |
mazgch | 9:e7a5959ffae1 | 132 | return WAIT; |
mazgch | 9:e7a5959ffae1 | 133 | } |
mazgch | 16:4a7ba1887e81 | 134 | |
mazgch | 16:4a7ba1887e81 | 135 | int SerialPipeEx::getResp(char* buffer, int length) |
mazgch | 16:4a7ba1887e81 | 136 | { |
mazgch | 16:4a7ba1887e81 | 137 | return getResp(buffer, length, &_pipeRx); |
mazgch | 16:4a7ba1887e81 | 138 | } |
mazgch | 16:4a7ba1887e81 | 139 | |
mazgch | 16:4a7ba1887e81 | 140 | int SerialPipeEx::getResp(char* buffer, int length, Pipe<char>* pipe) |
mazgch | 16:4a7ba1887e81 | 141 | { |
mazgch | 16:4a7ba1887e81 | 142 | int o = 0; |
mazgch | 16:4a7ba1887e81 | 143 | int i = 0; |
mazgch | 16:4a7ba1887e81 | 144 | int l = pipe->start(); |
mazgch | 16:4a7ba1887e81 | 145 | static const char erTxt[] = "ERROR\r\n"; |
mazgch | 16:4a7ba1887e81 | 146 | static const char okTxt[] = "OK\r\n"; |
mazgch | 16:4a7ba1887e81 | 147 | int er = 0; |
mazgch | 16:4a7ba1887e81 | 148 | int ok = 0; |
mazgch | 16:4a7ba1887e81 | 149 | while ((i < pipe->size()) && (o < length)) |
mazgch | 16:4a7ba1887e81 | 150 | { |
mazgch | 16:4a7ba1887e81 | 151 | int t = pipe->next(); |
mazgch | 16:4a7ba1887e81 | 152 | i ++; |
mazgch | 16:4a7ba1887e81 | 153 | buffer[o++] = t; |
mazgch | 16:4a7ba1887e81 | 154 | ok = (t == okTxt[ok]) ? ok + 1 : 0; |
mazgch | 16:4a7ba1887e81 | 155 | er = (t == erTxt[er]) ? er + 1 : 0; |
mazgch | 16:4a7ba1887e81 | 156 | if ((okTxt[ok] == '\0') || (erTxt[er] == '\0')) |
mazgch | 16:4a7ba1887e81 | 157 | { |
mazgch | 16:4a7ba1887e81 | 158 | pipe->done(); |
mazgch | 16:4a7ba1887e81 | 159 | if (length > o) |
mazgch | 16:4a7ba1887e81 | 160 | buffer[o] = '\0'; |
mazgch | 16:4a7ba1887e81 | 161 | return o; |
mazgch | 16:4a7ba1887e81 | 162 | } |
mazgch | 16:4a7ba1887e81 | 163 | } |
mazgch | 16:4a7ba1887e81 | 164 | o = 0; |
mazgch | 16:4a7ba1887e81 | 165 | if (length > 0) |
mazgch | 16:4a7ba1887e81 | 166 | buffer[0] = '\0'; |
mazgch | 16:4a7ba1887e81 | 167 | return WAIT; |
mazgch | 16:4a7ba1887e81 | 168 | } |