mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Wed Jul 09 15:37:19 2014 +0200
Revision:
4:059b8b90e0d9
Parent:
0:099f76422485
Child:
6:cd7ba1ddb664
Updated from revision b9c81426d0e6

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
Cumulocity 0:099f76422485 29 #include "Parser.h"
Cumulocity 0:099f76422485 30
Cumulocity 0:099f76422485 31 #define STATE_BLANK 0
Cumulocity 0:099f76422485 32 #define STATE_STRUCTURE 1
Cumulocity 0:099f76422485 33 #define STATE_VALUE 2
Cumulocity 0:099f76422485 34 #define STATE_INQUOTES 3
Cumulocity 0:099f76422485 35 #define STATE_AFTERQUOTE 4
Cumulocity 0:099f76422485 36 #define STATE_COMPLETE 5
Cumulocity 0:099f76422485 37 #define STATE_ERROR 6
Cumulocity 0:099f76422485 38
Cumulocity 0:099f76422485 39 Parser::Parser()
Cumulocity 0:099f76422485 40 {
Cumulocity 0:099f76422485 41 reset();
Cumulocity 0:099f76422485 42 }
Cumulocity 0:099f76422485 43
Cumulocity 0:099f76422485 44 uint8_t Parser::readFrom(AbstractDataSource& source, ParsedRecord& record)
Cumulocity 0:099f76422485 45 {
Cumulocity 0:099f76422485 46 uint8_t status; char read;
Cumulocity 0:099f76422485 47
Cumulocity 0:099f76422485 48 reset();
Cumulocity 4:059b8b90e0d9 49 record.clear();
Cumulocity 0:099f76422485 50
Cumulocity 0:099f76422485 51 while ((_state < STATE_COMPLETE) &&
Cumulocity 0:099f76422485 52 (((read = source.read()) > 0 ) ||
Cumulocity 0:099f76422485 53 ((status = source.status()) == DS_STATUS_OK)))
Cumulocity 0:099f76422485 54 parse(read);
Cumulocity 0:099f76422485 55
Cumulocity 4:059b8b90e0d9 56 if (_state == STATE_COMPLETE) {
Cumulocity 4:059b8b90e0d9 57 if (!record.set(_buffer, _count))
Cumulocity 4:059b8b90e0d9 58 return PARSER_INTERNAL_ERROR;
Cumulocity 4:059b8b90e0d9 59 }
Cumulocity 0:099f76422485 60
Cumulocity 0:099f76422485 61 if (_state == STATE_COMPLETE)
Cumulocity 0:099f76422485 62 return PARSER_SUCCESS;
Cumulocity 0:099f76422485 63 else if (_state == STATE_BLANK)
Cumulocity 0:099f76422485 64 if (status == DS_STATUS_CLOSED)
Cumulocity 0:099f76422485 65 return PARSER_END_OF_RESPONSE;
Cumulocity 0:099f76422485 66 else
Cumulocity 0:099f76422485 67 return PARSER_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 68 else
Cumulocity 0:099f76422485 69 if (status == DS_STATUS_TIMEOUT)
Cumulocity 0:099f76422485 70 return PARSER_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 71 else
Cumulocity 0:099f76422485 72 return PARSER_PARSE_ERROR;
Cumulocity 0:099f76422485 73 }
Cumulocity 0:099f76422485 74
Cumulocity 0:099f76422485 75 void Parser::parse(char c)
Cumulocity 0:099f76422485 76 {
Cumulocity 0:099f76422485 77 if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE) {
Cumulocity 0:099f76422485 78 _state = STATE_ERROR;
Cumulocity 0:099f76422485 79 return;
Cumulocity 0:099f76422485 80 }
Cumulocity 0:099f76422485 81
Cumulocity 0:099f76422485 82 switch (_state) {
Cumulocity 0:099f76422485 83 case STATE_BLANK:
Cumulocity 0:099f76422485 84 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 85 case STATE_STRUCTURE:
Cumulocity 0:099f76422485 86 switch (c) {
Cumulocity 0:099f76422485 87 case ' ':
Cumulocity 0:099f76422485 88 case '\t':
Cumulocity 0:099f76422485 89 case '\r':
Cumulocity 0:099f76422485 90 break;
Cumulocity 0:099f76422485 91 case '"':
Cumulocity 0:099f76422485 92 _state = STATE_INQUOTES;
Cumulocity 0:099f76422485 93 break;
Cumulocity 0:099f76422485 94 case ',':
Cumulocity 0:099f76422485 95 close();
Cumulocity 0:099f76422485 96 break;
Cumulocity 0:099f76422485 97 case '\0':
Cumulocity 0:099f76422485 98 case '\n':
Cumulocity 0:099f76422485 99 if ((_count == 0) && (_length == 0)) {
Cumulocity 0:099f76422485 100 _state = STATE_BLANK;
Cumulocity 0:099f76422485 101 } else {
Cumulocity 0:099f76422485 102 close();
Cumulocity 0:099f76422485 103 _state = STATE_COMPLETE;
Cumulocity 0:099f76422485 104 }
Cumulocity 0:099f76422485 105 break;
Cumulocity 0:099f76422485 106 default:
Cumulocity 0:099f76422485 107 if (_length > 0) {
Cumulocity 0:099f76422485 108 _state = STATE_ERROR;
Cumulocity 0:099f76422485 109 } else {
Cumulocity 0:099f76422485 110 _state = STATE_VALUE;
Cumulocity 0:099f76422485 111 parse(c);
Cumulocity 0:099f76422485 112 }
Cumulocity 0:099f76422485 113 break;
Cumulocity 0:099f76422485 114 }
Cumulocity 0:099f76422485 115 break;
Cumulocity 0:099f76422485 116 case STATE_VALUE:
Cumulocity 0:099f76422485 117 switch (c) {
Cumulocity 0:099f76422485 118 case ',':
Cumulocity 0:099f76422485 119 case '\n':
Cumulocity 0:099f76422485 120 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 121 parse(c);
Cumulocity 0:099f76422485 122 break;
Cumulocity 0:099f76422485 123 case '"':
Cumulocity 0:099f76422485 124 _state = STATE_ERROR;
Cumulocity 0:099f76422485 125 break;
Cumulocity 0:099f76422485 126 default:
Cumulocity 0:099f76422485 127 if ((c == ' ') || (c == '\t') || (c == '\r'))
Cumulocity 0:099f76422485 128 _trailing++;
Cumulocity 0:099f76422485 129 else
Cumulocity 0:099f76422485 130 _trailing = 0;
Cumulocity 0:099f76422485 131 append(c);
Cumulocity 0:099f76422485 132 break;
Cumulocity 0:099f76422485 133 }
Cumulocity 0:099f76422485 134 break;
Cumulocity 0:099f76422485 135 case STATE_INQUOTES:
Cumulocity 0:099f76422485 136 switch (c) {
Cumulocity 0:099f76422485 137 case '"':
Cumulocity 0:099f76422485 138 _state = STATE_AFTERQUOTE;
Cumulocity 0:099f76422485 139 break;
Cumulocity 0:099f76422485 140 default:
Cumulocity 0:099f76422485 141 append(c);
Cumulocity 0:099f76422485 142 break;
Cumulocity 0:099f76422485 143 }
Cumulocity 0:099f76422485 144 break;
Cumulocity 0:099f76422485 145 case STATE_AFTERQUOTE:
Cumulocity 0:099f76422485 146 switch (c) {
Cumulocity 0:099f76422485 147 case '"':
Cumulocity 0:099f76422485 148 append(c);
Cumulocity 0:099f76422485 149 _state = STATE_INQUOTES;
Cumulocity 0:099f76422485 150 break;
Cumulocity 0:099f76422485 151 default:
Cumulocity 0:099f76422485 152 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 153 parse(c);
Cumulocity 0:099f76422485 154 break;
Cumulocity 0:099f76422485 155 }
Cumulocity 0:099f76422485 156 break;
Cumulocity 0:099f76422485 157 }
Cumulocity 0:099f76422485 158 }
Cumulocity 0:099f76422485 159
Cumulocity 0:099f76422485 160 void Parser::append(char c)
Cumulocity 0:099f76422485 161 {
Cumulocity 0:099f76422485 162 *_ptr++ = c;
Cumulocity 0:099f76422485 163 _length++;
Cumulocity 0:099f76422485 164 }
Cumulocity 0:099f76422485 165
Cumulocity 0:099f76422485 166 void Parser::close()
Cumulocity 0:099f76422485 167 {
Cumulocity 0:099f76422485 168 _ptr -= _trailing;
Cumulocity 0:099f76422485 169 *_ptr++ = 0;
Cumulocity 0:099f76422485 170 _trailing = 0;
Cumulocity 0:099f76422485 171 _length = 0;
Cumulocity 0:099f76422485 172 _count++;
Cumulocity 0:099f76422485 173 }
Cumulocity 0:099f76422485 174
Cumulocity 0:099f76422485 175 void Parser::reset()
Cumulocity 0:099f76422485 176 {
Cumulocity 0:099f76422485 177 _state = STATE_BLANK;
Cumulocity 0:099f76422485 178 _ptr = _buffer;
Cumulocity 0:099f76422485 179 _count = 0;
Cumulocity 0:099f76422485 180 _trailing = 0;
Cumulocity 0:099f76422485 181 _length = 0;
Cumulocity 0:099f76422485 182 }