Duy tran / Mbed OS iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Committer:
DuyLionTran
Date:
Thu Mar 08 13:13:53 2018 +0000
Revision:
43:dcde0e66874a
Parent:
11:3802c82a5ae9
* version 2.7.5 	03-08-2018: DS18B20 & IWDG is being developed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 11:3802c82a5ae9 1 /**
DuyLionTran 11:3802c82a5ae9 2 ******************************************************************************
DuyLionTran 11:3802c82a5ae9 3 * @file Message.cpp
DuyLionTran 11:3802c82a5ae9 4 * @author ST / Central Labs
DuyLionTran 11:3802c82a5ae9 5 * @version V2.0.0
DuyLionTran 11:3802c82a5ae9 6 * @date 28 Apr 2017
DuyLionTran 11:3802c82a5ae9 7 * @brief NDef Message class implementation
DuyLionTran 11:3802c82a5ae9 8 ******************************************************************************
DuyLionTran 11:3802c82a5ae9 9 * @attention
DuyLionTran 11:3802c82a5ae9 10 *
DuyLionTran 11:3802c82a5ae9 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
DuyLionTran 11:3802c82a5ae9 12 *
DuyLionTran 11:3802c82a5ae9 13 * Redistribution and use in source and binary forms, with or without modification,
DuyLionTran 11:3802c82a5ae9 14 * are permitted provided that the following conditions are met:
DuyLionTran 11:3802c82a5ae9 15 * 1. Redistributions of source code must retain the above copyright notice,
DuyLionTran 11:3802c82a5ae9 16 * this list of conditions and the following disclaimer.
DuyLionTran 11:3802c82a5ae9 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
DuyLionTran 11:3802c82a5ae9 18 * this list of conditions and the following disclaimer in the documentation
DuyLionTran 11:3802c82a5ae9 19 * and/or other materials provided with the distribution.
DuyLionTran 11:3802c82a5ae9 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
DuyLionTran 11:3802c82a5ae9 21 * may be used to endorse or promote products derived from this software
DuyLionTran 11:3802c82a5ae9 22 * without specific prior written permission.
DuyLionTran 11:3802c82a5ae9 23 *
DuyLionTran 11:3802c82a5ae9 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
DuyLionTran 11:3802c82a5ae9 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
DuyLionTran 11:3802c82a5ae9 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DuyLionTran 11:3802c82a5ae9 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
DuyLionTran 11:3802c82a5ae9 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DuyLionTran 11:3802c82a5ae9 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
DuyLionTran 11:3802c82a5ae9 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
DuyLionTran 11:3802c82a5ae9 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
DuyLionTran 11:3802c82a5ae9 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
DuyLionTran 11:3802c82a5ae9 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
DuyLionTran 11:3802c82a5ae9 34 *
DuyLionTran 11:3802c82a5ae9 35 ******************************************************************************
DuyLionTran 11:3802c82a5ae9 36 */
DuyLionTran 11:3802c82a5ae9 37
DuyLionTran 11:3802c82a5ae9 38 #include <cstdlib>
DuyLionTran 11:3802c82a5ae9 39 #include "NDefLib/Message.h"
DuyLionTran 11:3802c82a5ae9 40 #include "RecordType/EmptyRecord.h"
DuyLionTran 11:3802c82a5ae9 41 #include "RecordType/RecordText.h"
DuyLionTran 11:3802c82a5ae9 42 #include "RecordType/RecordAAR.h"
DuyLionTran 11:3802c82a5ae9 43 #include "RecordType/RecordMimeType.h"
DuyLionTran 11:3802c82a5ae9 44 #include "RecordType/RecordURI.h"
DuyLionTran 11:3802c82a5ae9 45
DuyLionTran 11:3802c82a5ae9 46 namespace NDefLib {
DuyLionTran 11:3802c82a5ae9 47
DuyLionTran 11:3802c82a5ae9 48 uint16_t Message::get_byte_length() const {
DuyLionTran 11:3802c82a5ae9 49 uint16_t lenght = 2; //length size
DuyLionTran 11:3802c82a5ae9 50
DuyLionTran 11:3802c82a5ae9 51 if (mRecords.size() == 0) {
DuyLionTran 11:3802c82a5ae9 52 return lenght + EmptyRecord().get_byte_length();
DuyLionTran 11:3802c82a5ae9 53 }
DuyLionTran 11:3802c82a5ae9 54
DuyLionTran 11:3802c82a5ae9 55 std::vector<Record*>::const_iterator it = mRecords.begin();
DuyLionTran 11:3802c82a5ae9 56 const std::vector<Record*>::const_iterator end = mRecords.end();
DuyLionTran 11:3802c82a5ae9 57
DuyLionTran 11:3802c82a5ae9 58 for (; it != end; ++it) {
DuyLionTran 11:3802c82a5ae9 59 lenght += (*it)->get_byte_length();
DuyLionTran 11:3802c82a5ae9 60 } //for
DuyLionTran 11:3802c82a5ae9 61
DuyLionTran 11:3802c82a5ae9 62 return lenght;
DuyLionTran 11:3802c82a5ae9 63 } //getByteLenght
DuyLionTran 11:3802c82a5ae9 64
DuyLionTran 11:3802c82a5ae9 65 uint16_t Message::write(uint8_t *buffer) const {
DuyLionTran 11:3802c82a5ae9 66
DuyLionTran 11:3802c82a5ae9 67 const uint16_t length = get_byte_length() - 2;
DuyLionTran 11:3802c82a5ae9 68 uint16_t offset = 0;
DuyLionTran 11:3802c82a5ae9 69 buffer[offset++] = (uint8_t) ((length & 0xFF00) >> 8);
DuyLionTran 11:3802c82a5ae9 70 buffer[offset++] = (uint8_t) ((length & 0x00FF));
DuyLionTran 11:3802c82a5ae9 71
DuyLionTran 11:3802c82a5ae9 72 const uint32_t nRecord = mRecords.size();
DuyLionTran 11:3802c82a5ae9 73
DuyLionTran 11:3802c82a5ae9 74 if (mRecords.size() == 0) {
DuyLionTran 11:3802c82a5ae9 75 offset += EmptyRecord().write(buffer + offset);
DuyLionTran 11:3802c82a5ae9 76 return offset;
DuyLionTran 11:3802c82a5ae9 77 } //else
DuyLionTran 11:3802c82a5ae9 78
DuyLionTran 11:3802c82a5ae9 79 for (uint32_t i = 0; i < nRecord; i++) {
DuyLionTran 11:3802c82a5ae9 80 Record *r = mRecords[i];
DuyLionTran 11:3802c82a5ae9 81
DuyLionTran 11:3802c82a5ae9 82 r->set_as_middle_record();
DuyLionTran 11:3802c82a5ae9 83 if (i == 0) {
DuyLionTran 11:3802c82a5ae9 84 r->set_as_first_record();
DuyLionTran 11:3802c82a5ae9 85 }
DuyLionTran 11:3802c82a5ae9 86 if (i == nRecord - 1) {
DuyLionTran 11:3802c82a5ae9 87 r->set_as_last_record();
DuyLionTran 11:3802c82a5ae9 88 }
DuyLionTran 11:3802c82a5ae9 89
DuyLionTran 11:3802c82a5ae9 90 offset += r->write(buffer + offset);
DuyLionTran 11:3802c82a5ae9 91 } //for
DuyLionTran 11:3802c82a5ae9 92
DuyLionTran 11:3802c82a5ae9 93 return offset;
DuyLionTran 11:3802c82a5ae9 94 } //write
DuyLionTran 11:3802c82a5ae9 95
DuyLionTran 11:3802c82a5ae9 96 void Message::parse_message(const uint8_t * const rawNdefFile, const uint16_t length, Message *msg) {
DuyLionTran 11:3802c82a5ae9 97 uint16_t offset = 0;
DuyLionTran 11:3802c82a5ae9 98 Record *r;
DuyLionTran 11:3802c82a5ae9 99
DuyLionTran 11:3802c82a5ae9 100 RecordHeader header;
DuyLionTran 11:3802c82a5ae9 101 do {
DuyLionTran 11:3802c82a5ae9 102 const uint8_t headerLenght = header.load_header(rawNdefFile + offset);
DuyLionTran 11:3802c82a5ae9 103 r = RecordText::parse(header, rawNdefFile + offset + headerLenght);
DuyLionTran 11:3802c82a5ae9 104 if (r == NULL) {
DuyLionTran 11:3802c82a5ae9 105 r = RecordAAR::parse(header, rawNdefFile + offset + headerLenght);
DuyLionTran 11:3802c82a5ae9 106 }
DuyLionTran 11:3802c82a5ae9 107 if (r == NULL) {
DuyLionTran 11:3802c82a5ae9 108 r = RecordMimeType::parse(header,
DuyLionTran 11:3802c82a5ae9 109 rawNdefFile + offset + headerLenght);
DuyLionTran 11:3802c82a5ae9 110 }
DuyLionTran 11:3802c82a5ae9 111 if (r == NULL) {
DuyLionTran 11:3802c82a5ae9 112 r = RecordURI::parse(header, rawNdefFile + offset + headerLenght);
DuyLionTran 11:3802c82a5ae9 113 }
DuyLionTran 11:3802c82a5ae9 114
DuyLionTran 11:3802c82a5ae9 115 offset += header.get_record_length();
DuyLionTran 11:3802c82a5ae9 116 msg->add_record(r);
DuyLionTran 11:3802c82a5ae9 117 } while (offset < length);
DuyLionTran 11:3802c82a5ae9 118 }
DuyLionTran 11:3802c82a5ae9 119
DuyLionTran 11:3802c82a5ae9 120 void Message::remove_and_delete_all_record(Message &msg){
DuyLionTran 11:3802c82a5ae9 121 const uint32_t nRecords =msg.get_N_records();
DuyLionTran 11:3802c82a5ae9 122 for (uint32_t i =0 ;i<nRecords ;i++) {
DuyLionTran 11:3802c82a5ae9 123 NDefLib::Record *r = msg[i];
DuyLionTran 11:3802c82a5ae9 124 delete r;
DuyLionTran 11:3802c82a5ae9 125 }//for
DuyLionTran 11:3802c82a5ae9 126 msg.mRecords.clear();
DuyLionTran 11:3802c82a5ae9 127 }//removeAndDeleteAllRecord
DuyLionTran 11:3802c82a5ae9 128
DuyLionTran 11:3802c82a5ae9 129 } /* namespace NDefLib */
DuyLionTran 11:3802c82a5ae9 130
DuyLionTran 11:3802c82a5ae9 131
DuyLionTran 11:3802c82a5ae9 132 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/