+ OCRepPayload* root = OCRepPayloadCreate();
+ if (!root)
+ {
+ throw std::bad_alloc();
+ }
+
+ OCRepPayloadSetUri(root, getUri().c_str());
+
+ for(const std::string& type : getResourceTypes())
+ {
+ OCRepPayloadAddResourceType(root, type.c_str());
+ }
+
+ for(const std::string& iface : getResourceInterfaces())
+ {
+ OCRepPayloadAddInterface(root, iface.c_str());
+ }
+
+ for(auto& val : *this)
+ {
+ switch(val.type())
+ {
+ case AttributeType::Null:
+ OCRepPayloadSetNull(root, val.attrname().c_str());
+ break;
+ case AttributeType::Integer:
+ OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
+ break;
+ case AttributeType::Double:
+ OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
+ val.getValue<double>());
+ break;
+ case AttributeType::Boolean:
+ OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
+ break;
+ case AttributeType::String:
+ OCRepPayloadSetPropString(root, val.attrname().c_str(),
+ static_cast<std::string>(val).c_str());
+ break;
+ case AttributeType::OCByteString:
+ OCRepPayloadSetPropByteString(root, val.attrname().c_str(), val.getValue<OCByteString>());
+ break;
+ case AttributeType::OCRepresentation:
+ OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
+ static_cast<OCRepresentation>(val).getPayload());
+ break;
+ 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()));
+ break;
+ }
+ }
+
+ return root;
+ }
+
+ size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
+ {
+ if (dimensions[0] == 0)
+ {
+ throw std::logic_error("invalid calcArrayDepth");
+ }
+ else if (dimensions[1] == 0)
+ {
+ return 1;
+ }
+ else if (dimensions[2] == 0)
+ {
+ return 2;
+ }
+ else
+ {
+ return 3;
+ }
+ }
+
+ template<typename T>
+ T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
+ {
+ throw std::logic_error("payload_array_helper_copy: unsupported type");
+ }
+ template<>
+ int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
+ {
+ return pl->arr.iArray[index];
+ }
+ template<>
+ double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
+ {
+ return pl->arr.dArray[index];
+ }
+ template<>
+ bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
+ {
+ return pl->arr.bArray[index];
+ }
+ template<>
+ std::string OCRepresentation::payload_array_helper_copy<std::string>(
+ size_t index, const OCRepPayloadValue* pl)
+ {
+ if (pl->arr.strArray[index])
+ {
+ return std::string(pl->arr.strArray[index]);
+ }
+ else
+ {
+ return std::string{};
+ }
+ }
+
+ template<>
+ OCByteString OCRepresentation::payload_array_helper_copy<OCByteString>(
+ size_t index, const OCRepPayloadValue *pl)
+ {
+ OCByteString result {NULL, 0};
+ if (pl->arr.ocByteStrArray[index].len)
+ {
+ result = (pl->arr.ocByteStrArray[index]);
+ }
+ return result;