1
0
Fork 0
OpenLCP/protocol/acn/pdu-stream.cpp

391 lines
7.3 KiB
C++

/*
pdu-stream.cpp
Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "pdu-stream.h"
namespace ACN {
namespace PDU {
/**
* @brief pdu_stream::pdu_stream
* @param p the packet buffer
* @param l buffer length
*/
pdu_stream::pdu_stream(uint8_t * p, std::streamsize l)
: std::basic_streambuf<uint8_t>()
, std::basic_iostream<uint8_t>(this)
{
setg(p, p, p + l);
setp(p, p + l);
}
/**
* @brief pdu_stream::available
* @return
*/
uint32_t pdu_stream::available()
{
return in_avail();
}
/**
* @brief pdu_stream::data
* @return
*/
uint8_t * pdu_stream::data()
{
return gptr();
}
/**
* @brief pdu_stream::size
* @return
*/
uint32_t pdu_stream::size()
{
return pptr() - pbase();
}
/**
* @brief pdu_stream::base
* @return
*/
uint8_t * pdu_stream::base()
{
return pbase();
}
/**
* @brief pdu_stream::readString
* @param str std::string to which the read string will be appended.
* @param fixed_length this many bytes will be read from the stream. If 0, all
* available bytes on the stream will be used to construct the appended string.
*/
void pdu_stream::readString(std::string &str, const int fixed_length)
{
// if fixed_length == 0, use all available data on the stream
if (!fixed_length)
{
str += std::string(reinterpret_cast<char*>(data()), available());
return setstate(std::ios_base::eofbit);
}
// otherwise, construct a buffer and read into that.
uint8_t buffer[fixed_length];
read(buffer, fixed_length);
if (gcount() != fixed_length)
return setstate(std::ios_base::failbit);
str += std::string(reinterpret_cast<char*>(buffer));
}
/**
* @brief pdu_stream::writeString
* @param str
* @param fixed_length write this length to the stream, padding with null
* if str is shorter than fixed_length. If 0, will write the unrestricted
* contents of str.
* @param terminated If true, the last byte of fixed_length is guaranteed to
* be a 0 (null) byte.
*/
void pdu_stream::writeString(const std::string &str, const size_t fixed_length,
const bool terminated)
{
// fixed_length == 0 means dynamic length.
bool fixed = fixed_length > 0 ? true : false;
// fixed length strings restricted to fixed_lengh characters
size_t maxlen = fixed ? fixed_length : str.size();
// write no more of the string than is available
size_t wrtlen = str.size() < maxlen ? str.size() : maxlen;
// terminated fixed-length strings get a guaranteed padding byte
if (fixed && terminated && wrtlen == maxlen)
--wrtlen;
// output the correct ammount of data from the string
write(reinterpret_cast<const uint8_t*>(str.data()), wrtlen);
// output any required padding bytes
for (size_t i = wrtlen; i < maxlen; i++)
put(0);
}
/**
* @brief pdu_stream::operator >>
* @param obj
* @return
*/
pdu_stream& pdu_stream::operator>> (pdu_stream_object& obj)
{
obj.iStream(shared_from_this());
return *this;
}
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint8_t& val)
{
val = readType<uint8_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint16_t& val)
{
val = readType<uint16_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint32_t& val)
{
val = readType<uint32_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint64_t& val)
{
val = readType<uint64_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (int8_t& val)
{
val = readType<int8_t>();
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const int8_t& val)
{
writeType<int8_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (int16_t& val)
{
val = readType<int16_t>();
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const int16_t& val)
{
writeType<int16_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (int32_t& val)
{
val = readType<int32_t>();
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const int32_t& val)
{
writeType<int32_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (int64_t& val)
{
val = readType<int64_t>();
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const int64_t& val)
{
writeType<int64_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator >>
* @param uuid
* @return
*/
pdu_stream& pdu_stream::operator>> (UUID::uuid& uuid)
{
uint8_t buffer[UUID_LENGTH];
read(buffer, UUID_LENGTH);
if (gcount() != UUID_LENGTH)
{
setstate(std::ios_base::failbit);
return *this;
}
uuid.setBytes(buffer);
return *this;
};
/**
* @brief pdu_stream::operator <<
* @param obj
* @return
*/
pdu_stream& pdu_stream::operator<< (const pdu_stream_object& obj)
{
obj.oStream(shared_from_this());
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint8_t& val)
{
writeType<uint8_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint16_t& val)
{
writeType<uint16_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint32_t& val)
{
writeType<uint32_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint64_t& val)
{
writeType<uint64_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param uuid
* @return
*/
pdu_stream& pdu_stream::operator<< (const UUID::uuid& uuid)
{
write(uuid.bytes(), UUID_LENGTH);
return *this;
}
} // PDU
} // ACN