namespace OC
{
+ static const char COAP[] = "coap://";
+ static const char COAPS[] = "coaps://";
+ static const char COAP_TCP[] = "coap+tcp://";
void MessageContainer::setPayload(const OCPayload* rep)
{
+ if (rep == nullptr)
+ {
+ return;
+ }
+
switch(rep->type)
{
case PAYLOAD_TYPE_REPRESENTATION:
void MessageContainer::setPayload(const OCDevicePayload* payload)
{
- OCRepresentation rep;
- rep.setUri(payload->uri);
- char uuidString[UUID_STRING_SIZE];
- if(payload->sid && RAND_UUID_OK == OCConvertUuidToString(payload->sid, uuidString))
- {
- rep[OC_RSRVD_DEVICE_ID] = std::string(uuidString);
- }
- else
+ if (payload == nullptr)
{
- rep[OC_RSRVD_DEVICE_ID] = std::string();
+ return;
}
+
+ OCRepresentation rep;
+ rep[OC_RSRVD_DEVICE_ID] = (payload->sid) ?
+ std::string(payload->sid) :
+ std::string();
rep[OC_RSRVD_DEVICE_NAME] = payload->deviceName ?
std::string(payload->deviceName) :
std::string();
rep[OC_RSRVD_DATA_MODEL_VERSION] = payload->dataModelVersion ?
std::string(payload->dataModelVersion) :
std::string();
+ for (OCStringLL *strll = payload->types; strll; strll = strll->next)
+ {
+ rep.addResourceType(strll->value);
+ }
m_reps.push_back(std::move(rep));
}
void MessageContainer::setPayload(const OCPlatformPayload* payload)
{
- OCRepresentation rep;
- rep.setUri(payload->uri);
+ if (payload == nullptr)
+ {
+ return;
+ }
+ OCRepresentation rep;
rep[OC_RSRVD_PLATFORM_ID] = payload->info.platformID ?
std::string(payload->info.platformID) :
std::string();
std::string(payload->info.systemTime) :
std::string();
+ for (OCStringLL *strll = payload->rt; strll; strll = strll->next)
+ {
+ rep.addResourceType(strll->value);
+ }
+ for (OCStringLL *strll = payload->interfaces; strll; strll = strll->next)
+ {
+ rep.addResourceInterface(strll->value);
+ }
+
m_reps.push_back(std::move(rep));
}
OCRepPayload* root = nullptr;
for(const auto& r : representations())
{
- if(!root)
+ if (!root)
{
root = r.getPayload();
}
OCRepPayload* OCRepresentation::getPayload() const
{
OCRepPayload* root = OCRepPayloadCreate();
- if(!root)
+ if (!root)
{
throw std::bad_alloc();
}
case AttributeType::Vector:
getPayloadArray(root, val);
break;
+ case AttributeType::Binary:
+ OCRepPayloadSetPropByteString(root, val.attrname().c_str(),
+ OCByteString{const_cast<uint8_t*>(val.getValue<std::vector<uint8_t>>().data()),
+ val.getValue<std::vector<uint8_t>>().size()});
+ break;
default:
throw std::logic_error(std::string("Getpayload: Not Implemented") +
std::to_string((int)val.type()));
size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
- if(dimensions[0] == 0)
+ if (dimensions[0] == 0)
{
throw std::logic_error("invalid calcArrayDepth");
}
- else if(dimensions[1] == 0)
+ else if (dimensions[1] == 0)
{
return 1;
}
template<typename T>
void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
{
- if(depth == 1)
+ if (depth == 1)
{
std::vector<T> val(pl->arr.dimensions[0]);
case OCREP_PROP_ARRAY:
setPayloadArray(val);
break;
+ case OCREP_PROP_BYTE_STRING:
+ setValue(val->name,
+ std::vector<uint8_t>
+ (val->ocByteStr.bytes, val->ocByteStr.bytes + val->ocByteStr.len)
+ );
+ break;
default:
throw std::logic_error(std::string("Not Implemented!") +
std::to_string((int)val->type));
{
m_children = children;
}
+
+ void OCRepresentation::setDevAddr(const OCDevAddr m_devAddr)
+ {
+ std::ostringstream ss;
+ if (m_devAddr.flags & OC_SECURE)
+ {
+ ss << COAPS;
+ }
+ else if (m_devAddr.adapter & OC_ADAPTER_TCP)
+ {
+ ss << COAP_TCP;
+ }
+ else
+ {
+ ss << COAP;
+ }
+ if (m_devAddr.flags & OC_IP_USE_V6)
+ {
+ ss << '[' << m_devAddr.addr << ']';
+ }
+ else
+ {
+ ss << m_devAddr.addr;
+ }
+ if (m_devAddr.port)
+ {
+ ss << ':' << m_devAddr.port;
+ }
+ m_host = ss.str();
+ }
+
+ const std::string OCRepresentation::getHost() const
+ {
+ return m_host;
+ }
+
void OCRepresentation::setUri(const char* uri)
{
m_uri = uri ? uri : "";
// child of a default or link item.
// Our values array is only printed in the if we are the child of a Batch resource,
// the parent in a 'default' situation, or not in a child/parent relationship.
- if(!m_uri.empty())
+ if (!m_uri.empty())
{
return false;
}
{
return false;
}
- else if((m_interfaceType == InterfaceType::None
+ else if ((m_interfaceType == InterfaceType::None
|| m_interfaceType == InterfaceType::BatchChild
|| m_interfaceType == InterfaceType::DefaultParent)
&& m_values.size()>0)
return false;
}
- if(m_children.size() > 0)
+ if (m_children.size() > 0)
{
return false;
}
{
auto x = m_values.find(str);
- if(m_values.end() != x)
+ if (m_values.end() != x)
{
return x->second.which() == AttributeValueNullIndex;
}
case AttributeType::Vector:
os << "Vector";
break;
+ case AttributeType::Binary:
+ os<< "Binary";
}
return os;
}
};
template<typename T>
- struct type_info<T, typename std::enable_if<is_vector<T>::value>::type>
+ struct type_info<
+ T,
+ typename std::enable_if<
+ is_vector<T>::value &&
+ !std::is_same<uint8_t, typename T::value_type>::value
+ >::type
+ >
{
typedef T type;
typedef typename type_info<typename T::value_type>::base_type base_type;
type_info<typename T::value_type>::depth;
};
+ // special case for binary data, which is a std::vector<uint8_t>
+ template<>
+ struct type_info<std::vector<uint8_t>, void>
+ {
+ typedef std::vector<uint8_t> type;
+ typedef std::vector<uint8_t> base_type;
+ constexpr static AttributeType enum_type = AttributeType::Binary;
+ constexpr static AttributeType enum_base_type = AttributeType::Binary;
+ constexpr static size_t depth = 0;
+ };
+
+
struct type_introspection_visitor : boost::static_visitor<>
{
AttributeType type;
OCRepresentation::iterator& OCRepresentation::iterator::operator++()
{
m_iterator++;
- if(m_iterator != m_item.m_values.end())
+ if (m_iterator != m_item.m_values.end())
{
m_item.m_attrName = m_iterator->first;
}
OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
{
m_iterator++;
- if(m_iterator != m_item.m_values.end())
+ if (m_iterator != m_item.m_values.end())
{
m_item.m_attrName = m_iterator->first;
}
std::string OCRepresentation::getValueToString(const std::string& key) const
{
auto x = m_values.find(key);
- if(x != m_values.end())
+ if (x != m_values.end())
{
to_string_visitor vis;
boost::apply_visitor(vis, x->second);
return os;
}
}
-