/*************************************************************************** * Copyright (C) 2009 by gempa GmbH * * * * All Rights Reserved. * * * * NOTICE: All information contained herein is, and remains * * the property of gempa GmbH and its suppliers, if any. The intellectual * * and technical concepts contained herein are proprietary to gempa GmbH * * and its suppliers. * * Dissemination of this information or reproduction of this material * * is strictly forbidden unless prior written permission is obtained * * from gempa GmbH. * ***************************************************************************/ #ifndef GEMPA_CAPS_RAWPACKET_H #define GEMPA_CAPS_RAWPACKET_H #include #include namespace Gempa { namespace CAPS { struct SC_GEMPA_CAPS_API RawResponseHeader { int64_t timeSeconds; int32_t timeMicroSeconds; bool get(std::streambuf &buf) { Endianess::Reader get(buf); get(timeSeconds); get(timeMicroSeconds); return get.good; } int dataSize() const { return sizeof(timeSeconds) + sizeof(timeMicroSeconds); } }; class RawDataRecord : public DataRecord { public: RawDataRecord(); const char *formatName() const; /** * @brief Reads metadata from data record header * @param The streambuf object * @param The size of the data record in bytes * @param The data record header object * @param The startTime * @param The endTime */ bool readMetaData(std::streambuf &buf, int size, Header &header, Time &startTime, Time &endTime); void setHeader(const Header &header); /** * @brief Returns the meta information of the data if supported * @return The data record header */ const Header *header() const; /** * @brief Returns the start time of the record * @return The start time */ Time startTime() const; /** * @brief Returns the end time of the record * @return The end time */ Time endTime() const; /** * @brief canTrim checks if data trimming is possible * without modifying preceding data * @return True, if data trimming is possible */ bool canTrim() const; /** * @brief canMerge checks if data merging is possible * without modifying preceding data * @return True, if data merging is possible */ bool canMerge() const; /** * @brief Trims a record to start and end time. Trimming * should not modify any data but give access to trimmed * start and end times and the resulting data size. The trimming * also influences writing the record to a stream. * Trimming requires the resulting start time being greater * or equal than the requested start time. * (Un)Trimming to the original record is semantically * equal to passing an invalid start and end time. * @param start The requested start time * @param end The requested end time * @return It returns true if trimming has been done or false * if an error occured or trimming is not supported. */ bool trim(const Time &start, const Time &end) const; /** * @brief Returns the data size in bytes if the current state would * be written to a stream. Trimming has also to be taken into * account while calculating the size. * @param withHeader Take header into account * @return Returns the data size in bytes */ size_t dataSize(bool withHeader) const; /** * @brief Reads the packet data including header from a streambuf * and trims the data if possible to start and end. * If maxBytes is greater than 0 then the record should * not use more than this size of memory (in bytes). * * @param The streambuf object * @param The buffer size * @param The requested start time * @param The requested end time * @param The Max bytes to use * @return If not the complete record has been read, RS_Partial * must be returned, RS_Complete otherwise. */ ReadStatus get(std::streambuf &buf, int size, const Time &start = Time(), const Time &end = Time(), int maxBytes = -1); /** * @brief Reads the packet data without header from a streambuf * and trims the data if possible to start and end. * If maxBytes is greater than 0 then the record should * not use more than this size of memory (in bytes). * * @param The streambuf object * @param The buffer size * @param The requested start time * @param The requested end time * @param The Max bytes to use * @return If not the complete record has been read, RS_Partial * must be returned, RS_Complete otherwise. */ ReadStatus getData(std::streambuf &buf, int size, const Time &start = Time(), const Time &end = Time(), int maxBytes = -1); //! writes the packet to a streambuf and trims the data //! if possible to start and end /** * @brief Writes the packet to a streambuf and trims the data * if possible to start and end * @param The streambuf object * @param Take header into account * @return True, if the data has been written */ bool put(std::streambuf &buf, bool withHeader) const; /** * @brief Returns the packet type * @return The packet type */ PacketType packetType() const { return RawDataPacket; } /** * @brief Sets the start time of the record * @param The start time */ void setStartTime(const Time &time); /** * @brief Sets the sampling frequency of the record * @param numerator The numerator * @param denominator The denomintor */ void setSamplingFrequency(uint16_t numerator, uint16_t denominator); /** * @brief Sets the data type of the record * @param The datatype to use */ void setDataType(DataType dt); /** * @brief Initializes the internal data vector from the given buffer * @param The buffer to read the data from * @param The buffer size */ void setBuffer(const void *data, size_t size); protected: Header _header; mutable Header _currentHeader; mutable size_t _dataOfs; mutable size_t _dataSize; mutable Time _startTime; mutable Time _endTime; mutable bool _dirty; }; class FixedRawDataRecord : public RawDataRecord { public: virtual bool canTrim() const { return false; } virtual bool canMerge() const { return false; } virtual bool trim(const Time &start, const Time &end) const { return false; } virtual const char *formatName() const; virtual ReadStatus get(std::streambuf &buf, int size, const Time &/*start*/, const Time &/*end*/, int maxBytes) { return RawDataRecord::get(buf, size, Time(), Time(), maxBytes); } PacketType packetType() const { return FixedRawDataPacket; } }; } } #endif