Add a bunch of APNs
Fork of C027_Support by
MDM.cpp@18:e5697801df29, 2014-03-14 (annotated)
- Committer:
- mazgch
- Date:
- Fri Mar 14 13:07:48 2014 +0000
- Revision:
- 18:e5697801df29
- Child:
- 19:2b5d097ca15d
- Child:
- 20:535ef78655df
extend Api for GPS and GNSS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 18:e5697801df29 | 1 | #include "mbed.h" |
mazgch | 18:e5697801df29 | 2 | #include <ctype.h> |
mazgch | 18:e5697801df29 | 3 | #include "MDM.h" |
mazgch | 18:e5697801df29 | 4 | |
mazgch | 18:e5697801df29 | 5 | |
mazgch | 18:e5697801df29 | 6 | int MDMParser::send(const char* buf, int len) |
mazgch | 18:e5697801df29 | 7 | { |
mazgch | 18:e5697801df29 | 8 | return _send(buf, len); |
mazgch | 18:e5697801df29 | 9 | } |
mazgch | 18:e5697801df29 | 10 | |
mazgch | 18:e5697801df29 | 11 | int MDMParser::_getLine(Pipe<char>* pipe, char* buffer, int length) |
mazgch | 18:e5697801df29 | 12 | { |
mazgch | 18:e5697801df29 | 13 | int o = 0; |
mazgch | 18:e5697801df29 | 14 | int i = 0; |
mazgch | 18:e5697801df29 | 15 | int l = pipe->start(); |
mazgch | 18:e5697801df29 | 16 | while ((i < l) && (o < length)) |
mazgch | 18:e5697801df29 | 17 | { |
mazgch | 18:e5697801df29 | 18 | int t = pipe->next(); |
mazgch | 18:e5697801df29 | 19 | i ++; |
mazgch | 18:e5697801df29 | 20 | if (t == '\r') // terminate commands with carriage return |
mazgch | 18:e5697801df29 | 21 | { |
mazgch | 18:e5697801df29 | 22 | pipe->done(); |
mazgch | 18:e5697801df29 | 23 | if (length > o) |
mazgch | 18:e5697801df29 | 24 | buffer[o] = '\0'; |
mazgch | 18:e5697801df29 | 25 | return o; // if enter send the zero char |
mazgch | 18:e5697801df29 | 26 | } |
mazgch | 18:e5697801df29 | 27 | else if (t == '\n') // skip/filter new line |
mazgch | 18:e5697801df29 | 28 | /* skip */; |
mazgch | 18:e5697801df29 | 29 | else if (t != '\b') // normal char (no backspace) |
mazgch | 18:e5697801df29 | 30 | buffer[o++] = t; |
mazgch | 18:e5697801df29 | 31 | else if (o > 0) // backspace |
mazgch | 18:e5697801df29 | 32 | o --; // remove it |
mazgch | 18:e5697801df29 | 33 | } |
mazgch | 18:e5697801df29 | 34 | o = 0; |
mazgch | 18:e5697801df29 | 35 | if (length > 0) |
mazgch | 18:e5697801df29 | 36 | buffer[0] = '\0'; |
mazgch | 18:e5697801df29 | 37 | return WAIT; |
mazgch | 18:e5697801df29 | 38 | } |
mazgch | 18:e5697801df29 | 39 | |
mazgch | 18:e5697801df29 | 40 | int MDMParser::_getResp(Pipe<char>* pipe, char* buffer, int length) |
mazgch | 18:e5697801df29 | 41 | { |
mazgch | 18:e5697801df29 | 42 | int o = 0; |
mazgch | 18:e5697801df29 | 43 | int i = 0; |
mazgch | 18:e5697801df29 | 44 | int l = pipe->start(); |
mazgch | 18:e5697801df29 | 45 | static const char erTxt[] = "ERROR\r\n"; |
mazgch | 18:e5697801df29 | 46 | static const char okTxt[] = "OK\r\n"; |
mazgch | 18:e5697801df29 | 47 | int er = 0; |
mazgch | 18:e5697801df29 | 48 | int ok = 0; |
mazgch | 18:e5697801df29 | 49 | while ((i < pipe->size()) && (o < length)) |
mazgch | 18:e5697801df29 | 50 | { |
mazgch | 18:e5697801df29 | 51 | int t = pipe->next(); |
mazgch | 18:e5697801df29 | 52 | i ++; |
mazgch | 18:e5697801df29 | 53 | buffer[o++] = t; |
mazgch | 18:e5697801df29 | 54 | ok = (t == okTxt[ok]) ? ok + 1 : 0; |
mazgch | 18:e5697801df29 | 55 | er = (t == erTxt[er]) ? er + 1 : 0; |
mazgch | 18:e5697801df29 | 56 | if ((okTxt[ok] == '\0') || (erTxt[er] == '\0')) |
mazgch | 18:e5697801df29 | 57 | { |
mazgch | 18:e5697801df29 | 58 | pipe->done(); |
mazgch | 18:e5697801df29 | 59 | if (length > o) |
mazgch | 18:e5697801df29 | 60 | buffer[o] = '\0'; |
mazgch | 18:e5697801df29 | 61 | return o; |
mazgch | 18:e5697801df29 | 62 | } |
mazgch | 18:e5697801df29 | 63 | } |
mazgch | 18:e5697801df29 | 64 | o = 0; |
mazgch | 18:e5697801df29 | 65 | if (length > 0) |
mazgch | 18:e5697801df29 | 66 | buffer[0] = '\0'; |
mazgch | 18:e5697801df29 | 67 | return WAIT; |
mazgch | 18:e5697801df29 | 68 | } |
mazgch | 18:e5697801df29 | 69 | |
mazgch | 18:e5697801df29 | 70 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 71 | // Serial Implementation |
mazgch | 18:e5697801df29 | 72 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 73 | |
mazgch | 18:e5697801df29 | 74 | MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, int baudrate /*= MDMBAUD*/, |
mazgch | 18:e5697801df29 | 75 | int rxSize /*= 256*/, int txSize /*= 128*/) : |
mazgch | 18:e5697801df29 | 76 | SerialPipe(tx, rx, rxSize, txSize) |
mazgch | 18:e5697801df29 | 77 | { |
mazgch | 18:e5697801df29 | 78 | baud(baudrate); |
mazgch | 18:e5697801df29 | 79 | } |
mazgch | 18:e5697801df29 | 80 | |
mazgch | 18:e5697801df29 | 81 | MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, int baudrate /*= MDMBAUD*/, |
mazgch | 18:e5697801df29 | 82 | PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/, int rxSize /*= 256*/, int txSize /*= 128*/) : |
mazgch | 18:e5697801df29 | 83 | SerialPipe(tx, rx, rts, cts, rxSize, txSize) |
mazgch | 18:e5697801df29 | 84 | { |
mazgch | 18:e5697801df29 | 85 | baud(baudrate); |
mazgch | 18:e5697801df29 | 86 | } |
mazgch | 18:e5697801df29 | 87 | |
mazgch | 18:e5697801df29 | 88 | int MDMSerial::_send(const void* buf, int len) |
mazgch | 18:e5697801df29 | 89 | { |
mazgch | 18:e5697801df29 | 90 | return put((const char*)buf, len, true/*=blocking*/); |
mazgch | 18:e5697801df29 | 91 | } |
mazgch | 18:e5697801df29 | 92 | |
mazgch | 18:e5697801df29 | 93 | int MDMSerial::getLine(char* buffer, int length) |
mazgch | 18:e5697801df29 | 94 | { |
mazgch | 18:e5697801df29 | 95 | return _getLine(&_pipeRx, buffer, length); |
mazgch | 18:e5697801df29 | 96 | } |
mazgch | 18:e5697801df29 | 97 | |
mazgch | 18:e5697801df29 | 98 | int MDMSerial::getResp(char* buffer, int length) |
mazgch | 18:e5697801df29 | 99 | { |
mazgch | 18:e5697801df29 | 100 | return _getResp(&_pipeRx, buffer, length); |
mazgch | 18:e5697801df29 | 101 | } |
mazgch | 18:e5697801df29 | 102 | |
mazgch | 18:e5697801df29 | 103 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 104 | // USB Implementation |
mazgch | 18:e5697801df29 | 105 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 106 | |
mazgch | 18:e5697801df29 | 107 | #ifdef HAVE_MDMUSB |
mazgch | 18:e5697801df29 | 108 | // TODO properly implement with USB |
mazgch | 18:e5697801df29 | 109 | MDMUsb::MDMUsb(void) { } |
mazgch | 18:e5697801df29 | 110 | int MDMUsb::_send(const void* buf, int len) { return len; } |
mazgch | 18:e5697801df29 | 111 | int MDMUsb::getLine(char* buffer, int length) { return NOT_FOUND; } |
mazgch | 18:e5697801df29 | 112 | int MDMUsb::getResp(char* buffer, int length) { return NOT_FOUND; } |
mazgch | 18:e5697801df29 | 113 | #endif |