Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Mon Aug 10 10:39:53 2015 +0000
Revision:
26:9c36af176d91
Parent:
20:505d29d5bdfc
removed traffic accounting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * Parser.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Nov 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
xinlei 20:505d29d5bdfc 29 #include <assert.h>
xinlei 20:505d29d5bdfc 30 #include <stdio.h>
xinlei 20:505d29d5bdfc 31 #include <ctype.h>
Cumulocity 0:099f76422485 32 #include "Parser.h"
Cumulocity 0:099f76422485 33
Cumulocity 0:099f76422485 34 #define STATE_BLANK 0
Cumulocity 0:099f76422485 35 #define STATE_STRUCTURE 1
Cumulocity 0:099f76422485 36 #define STATE_VALUE 2
Cumulocity 0:099f76422485 37 #define STATE_INQUOTES 3
Cumulocity 0:099f76422485 38 #define STATE_AFTERQUOTE 4
Cumulocity 0:099f76422485 39 #define STATE_COMPLETE 5
Cumulocity 0:099f76422485 40 #define STATE_ERROR 6
Cumulocity 0:099f76422485 41
Cumulocity 11:e1bee9a77652 42
Cumulocity 11:e1bee9a77652 43 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 44 Parser::Parser()
Cumulocity 0:099f76422485 45 {
Cumulocity 11:e1bee9a77652 46 reset();
Cumulocity 0:099f76422485 47 }
Cumulocity 11:e1bee9a77652 48 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 49 uint8_t Parser::readFrom(AbstractDataSource& source, ParsedRecord& record)
Cumulocity 0:099f76422485 50 {
Cumulocity 11:e1bee9a77652 51 reset();
Cumulocity 11:e1bee9a77652 52 record.clear();
Cumulocity 0:099f76422485 53
xinlei 20:505d29d5bdfc 54 int status = -1;
xinlei 20:505d29d5bdfc 55 char read;
xinlei 20:505d29d5bdfc 56 while (_state < STATE_COMPLETE) {
xinlei 20:505d29d5bdfc 57 read = source.read();
xinlei 20:505d29d5bdfc 58 status = source.status();
xinlei 20:505d29d5bdfc 59 // if (isprint(read)) {
xinlei 20:505d29d5bdfc 60 // printf("%4c: %u\n", read, status);
xinlei 20:505d29d5bdfc 61 // } else {
xinlei 20:505d29d5bdfc 62 // printf("#%04x: %u\n", read, status);
xinlei 20:505d29d5bdfc 63 // }
xinlei 20:505d29d5bdfc 64 if (read > 0 || status == DS_STATUS_OK)
xinlei 20:505d29d5bdfc 65 parse(read);
xinlei 20:505d29d5bdfc 66 else
xinlei 20:505d29d5bdfc 67 break;
xinlei 20:505d29d5bdfc 68 }
xinlei 20:505d29d5bdfc 69 // printf("Parser: [E]while! %u, %d\r\n", _state, status);
xinlei 20:505d29d5bdfc 70 if (_state == STATE_COMPLETE) { // successfully read record
Cumulocity 11:e1bee9a77652 71 if (!record.set(_buffer, _count))
Cumulocity 11:e1bee9a77652 72 return PARSER_INTERNAL_ERROR;
xinlei 20:505d29d5bdfc 73 else
xinlei 20:505d29d5bdfc 74 return PARSER_SUCCESS;
xinlei 20:505d29d5bdfc 75 } else if (_state == STATE_BLANK) {
Cumulocity 11:e1bee9a77652 76 if (status == DS_STATUS_CLOSED)
Cumulocity 11:e1bee9a77652 77 return PARSER_END_OF_RESPONSE;
Cumulocity 11:e1bee9a77652 78 else if (status == DS_STATUS_TIMEOUT)
Cumulocity 11:e1bee9a77652 79 return PARSER_TIMEOUT_ERROR;
Cumulocity 11:e1bee9a77652 80 else
Cumulocity 11:e1bee9a77652 81 return PARSER_INTERNAL_ERROR;
xinlei 20:505d29d5bdfc 82 } else {
xinlei 20:505d29d5bdfc 83 if (status == DS_STATUS_TIMEOUT)
xinlei 20:505d29d5bdfc 84 return PARSER_TIMEOUT_ERROR;
xinlei 20:505d29d5bdfc 85 else if (status == DS_STATUS_ERROR)
xinlei 20:505d29d5bdfc 86 return PARSER_INTERNAL_ERROR;
xinlei 20:505d29d5bdfc 87 else
xinlei 20:505d29d5bdfc 88 return PARSER_PARSE_ERROR;
Cumulocity 11:e1bee9a77652 89 }
Cumulocity 0:099f76422485 90 }
Cumulocity 11:e1bee9a77652 91 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 92 void Parser::parse(char c)
Cumulocity 0:099f76422485 93 {
xinlei 20:505d29d5bdfc 94 if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE) {
Cumulocity 11:e1bee9a77652 95 _state = STATE_ERROR;
Cumulocity 11:e1bee9a77652 96 return;
Cumulocity 11:e1bee9a77652 97 }
Cumulocity 0:099f76422485 98
Cumulocity 11:e1bee9a77652 99 switch (_state)
Cumulocity 11:e1bee9a77652 100 {
xinlei 20:505d29d5bdfc 101 case STATE_BLANK: _state = STATE_STRUCTURE;
xinlei 20:505d29d5bdfc 102 case STATE_STRUCTURE:
xinlei 20:505d29d5bdfc 103 switch (c)
xinlei 20:505d29d5bdfc 104 {
xinlei 20:505d29d5bdfc 105 case ' ':
xinlei 20:505d29d5bdfc 106 case '\t':
xinlei 20:505d29d5bdfc 107 case '\r': break;
xinlei 20:505d29d5bdfc 108 case '"': _state = STATE_INQUOTES; break;
xinlei 20:505d29d5bdfc 109 case ',': close(); break;
xinlei 20:505d29d5bdfc 110 case '\0':
xinlei 20:505d29d5bdfc 111 case '\n':
xinlei 20:505d29d5bdfc 112 if (_count==0 && _length==0) {
xinlei 20:505d29d5bdfc 113 _state = STATE_BLANK;
xinlei 20:505d29d5bdfc 114 } else {
xinlei 20:505d29d5bdfc 115 close();
xinlei 20:505d29d5bdfc 116 _state = STATE_COMPLETE;
xinlei 20:505d29d5bdfc 117 }
xinlei 20:505d29d5bdfc 118 break;
xinlei 20:505d29d5bdfc 119 default:
xinlei 20:505d29d5bdfc 120 if (_length > 0) {
xinlei 20:505d29d5bdfc 121 _state = STATE_ERROR;
xinlei 20:505d29d5bdfc 122 } else {
xinlei 20:505d29d5bdfc 123 _state = STATE_VALUE;
xinlei 20:505d29d5bdfc 124 parse(c);
Cumulocity 11:e1bee9a77652 125 }
Cumulocity 11:e1bee9a77652 126 break;
xinlei 20:505d29d5bdfc 127 }
xinlei 20:505d29d5bdfc 128 break;
xinlei 20:505d29d5bdfc 129 case STATE_VALUE:
xinlei 20:505d29d5bdfc 130 switch (c)
xinlei 20:505d29d5bdfc 131 {
xinlei 20:505d29d5bdfc 132 case ',':
xinlei 20:505d29d5bdfc 133 case '\n': _state = STATE_STRUCTURE; parse(c); break;
xinlei 20:505d29d5bdfc 134 case '"': _state = STATE_ERROR; break;
xinlei 20:505d29d5bdfc 135 default:
xinlei 20:505d29d5bdfc 136 if ((c == ' ') || (c == '\t') || (c == '\r'))
xinlei 20:505d29d5bdfc 137 _trailing++;
xinlei 20:505d29d5bdfc 138 else
xinlei 20:505d29d5bdfc 139 _trailing = 0;
xinlei 20:505d29d5bdfc 140 append(c);
Cumulocity 11:e1bee9a77652 141 break;
xinlei 20:505d29d5bdfc 142 }
xinlei 20:505d29d5bdfc 143 break;
xinlei 20:505d29d5bdfc 144 case STATE_INQUOTES:
xinlei 20:505d29d5bdfc 145 switch (c)
xinlei 20:505d29d5bdfc 146 {
xinlei 20:505d29d5bdfc 147 case '"': _state = STATE_AFTERQUOTE; break;
xinlei 20:505d29d5bdfc 148 default: append(c); break;
xinlei 20:505d29d5bdfc 149 }
xinlei 20:505d29d5bdfc 150 break;
xinlei 20:505d29d5bdfc 151 case STATE_AFTERQUOTE:
xinlei 20:505d29d5bdfc 152 switch (c)
xinlei 20:505d29d5bdfc 153 {
xinlei 20:505d29d5bdfc 154 case '"': append(c); _state = STATE_INQUOTES; break;
xinlei 20:505d29d5bdfc 155 default: _state = STATE_STRUCTURE; parse(c); break;
xinlei 20:505d29d5bdfc 156 }
xinlei 20:505d29d5bdfc 157 break;
Cumulocity 11:e1bee9a77652 158 }
Cumulocity 0:099f76422485 159 }
Cumulocity 11:e1bee9a77652 160 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 161 void Parser::append(char c)
Cumulocity 0:099f76422485 162 {
Cumulocity 11:e1bee9a77652 163 *_ptr++ = c;
Cumulocity 11:e1bee9a77652 164 _length++;
Cumulocity 0:099f76422485 165 }
Cumulocity 11:e1bee9a77652 166 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 167 void Parser::close()
Cumulocity 0:099f76422485 168 {
Cumulocity 11:e1bee9a77652 169 _ptr -= _trailing;
Cumulocity 11:e1bee9a77652 170 *_ptr++ = 0;
Cumulocity 11:e1bee9a77652 171 _trailing = 0;
Cumulocity 11:e1bee9a77652 172 _length = 0;
Cumulocity 11:e1bee9a77652 173 _count++;
Cumulocity 0:099f76422485 174 }
Cumulocity 11:e1bee9a77652 175 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 176 void Parser::reset()
Cumulocity 0:099f76422485 177 {
Cumulocity 11:e1bee9a77652 178 _state = STATE_BLANK;
Cumulocity 11:e1bee9a77652 179 _ptr = _buffer;
Cumulocity 11:e1bee9a77652 180 _count = 0;
Cumulocity 11:e1bee9a77652 181 _trailing = 0;
Cumulocity 11:e1bee9a77652 182 _length = 0;
Cumulocity 0:099f76422485 183 }
Cumulocity 11:e1bee9a77652 184 /*-------------------------------------------------------------------------*/