iot_water_monitor_v2

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Message.cpp Source File

Message.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    Message.cpp
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   NDef Message class implementation
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 #include <cstdlib>
00039 #include "NDefLib/Message.h"
00040 #include "RecordType/EmptyRecord.h"
00041 #include "RecordType/RecordText.h"
00042 #include "RecordType/RecordAAR.h"
00043 #include "RecordType/RecordMimeType.h"
00044 #include "RecordType/RecordURI.h"
00045 
00046 namespace NDefLib {
00047 
00048 uint16_t Message::get_byte_length() const {
00049     uint16_t lenght = 2; //length size
00050 
00051     if (mRecords.size() == 0) {
00052         return lenght + EmptyRecord().get_byte_length();
00053     }
00054 
00055     std::vector<Record*>::const_iterator it = mRecords.begin();
00056     const std::vector<Record*>::const_iterator end = mRecords.end();
00057 
00058     for (; it != end; ++it) {
00059         lenght += (*it)->get_byte_length();
00060     } //for
00061 
00062     return lenght;
00063 } //getByteLenght
00064 
00065 uint16_t Message::write(uint8_t *buffer) const {
00066 
00067     const uint16_t length = get_byte_length() - 2;
00068     uint16_t offset = 0;
00069     buffer[offset++] = (uint8_t) ((length & 0xFF00) >> 8);
00070     buffer[offset++] = (uint8_t) ((length & 0x00FF));
00071 
00072     const uint32_t nRecord = mRecords.size();
00073 
00074     if (mRecords.size() == 0) {
00075         offset += EmptyRecord().write(buffer + offset);
00076         return offset;
00077     } //else
00078 
00079     for (uint32_t i = 0; i < nRecord; i++) {
00080         Record *r = mRecords[i];
00081 
00082         r->set_as_middle_record();
00083         if (i == 0) {
00084             r->set_as_first_record();
00085         }
00086         if (i == nRecord - 1) {
00087             r->set_as_last_record();
00088         }
00089 
00090         offset += r->write(buffer + offset);
00091     } //for
00092 
00093     return offset;
00094 } //write
00095 
00096 void Message::parse_message(const uint8_t * const rawNdefFile, const uint16_t length, Message *msg) {
00097     uint16_t offset = 0;
00098     Record *r;
00099 
00100     RecordHeader header;
00101     do {
00102         const uint8_t headerLenght = header.load_header(rawNdefFile + offset);
00103         r = RecordText::parse(header, rawNdefFile + offset + headerLenght);
00104         if (r == NULL) {
00105             r = RecordAAR::parse(header, rawNdefFile + offset + headerLenght);
00106         }
00107         if (r == NULL) {
00108             r = RecordMimeType::parse(header,
00109             rawNdefFile + offset + headerLenght);
00110         }
00111         if (r == NULL) {
00112             r = RecordURI::parse(header, rawNdefFile + offset + headerLenght);
00113         }
00114 
00115         offset += header.get_record_length();
00116         msg->add_record(r);
00117     } while (offset < length);
00118 }
00119 
00120 void Message::remove_and_delete_all_record(Message &msg){
00121     const uint32_t nRecords =msg.get_N_records();
00122     for (uint32_t i =0 ;i<nRecords ;i++) {
00123         NDefLib::Record *r = msg[i];
00124         delete r;
00125     }//for
00126     msg.mRecords.clear();
00127 }//removeAndDeleteAllRecord
00128 
00129 } /* namespace NDefLib */
00130 
00131 
00132 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/