1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the implementation of classes and its members related
25 * to OCRepresentation.
29 #include <OCRepresentation.h>
31 #include <boost/lexical_cast.hpp>
33 #include "ocpayload.h"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
41 void MessageContainer::setPayload(const OCPayload* rep)
45 case PAYLOAD_TYPE_REPRESENTATION:
46 setPayload(reinterpret_cast<const OCRepPayload*>(rep));
48 case PAYLOAD_TYPE_DEVICE:
49 setPayload(reinterpret_cast<const OCDevicePayload*>(rep));
51 case PAYLOAD_TYPE_PLATFORM:
52 setPayload(reinterpret_cast<const OCPlatformPayload*>(rep));
55 throw OC::OCException("Invalid Payload type in setPayload");
60 void MessageContainer::setPayload(const OCDevicePayload* payload)
63 rep.setUri(payload->uri);
64 char uuidString[UUID_STRING_SIZE];
65 if(payload->sid && RAND_UUID_OK == OCConvertUuidToString(payload->sid, uuidString))
67 rep[OC_RSRVD_DEVICE_ID] = std::string(uuidString);
71 rep[OC_RSRVD_DEVICE_ID] = std::string();
73 rep[OC_RSRVD_DEVICE_NAME] = payload->deviceName ?
74 std::string(payload->deviceName) :
76 rep[OC_RSRVD_SPEC_VERSION] = payload->specVersion ?
77 std::string(payload->specVersion) :
79 rep[OC_RSRVD_DATA_MODEL_VERSION] = payload->dataModelVersion ?
80 std::string(payload->dataModelVersion) :
82 m_reps.push_back(std::move(rep));
85 void MessageContainer::setPayload(const OCPlatformPayload* payload)
88 rep.setUri(payload->uri);
90 rep[OC_RSRVD_PLATFORM_ID] = payload->info.platformID ?
91 std::string(payload->info.platformID) :
93 rep[OC_RSRVD_MFG_NAME] = payload->info.manufacturerName ?
94 std::string(payload->info.manufacturerName) :
96 rep[OC_RSRVD_MFG_URL] = payload->info.manufacturerUrl ?
97 std::string(payload->info.manufacturerUrl) :
99 rep[OC_RSRVD_MODEL_NUM] = payload->info.modelNumber ?
100 std::string(payload->info.modelNumber) :
102 rep[OC_RSRVD_MFG_DATE] = payload->info.dateOfManufacture ?
103 std::string(payload->info.dateOfManufacture) :
105 rep[OC_RSRVD_PLATFORM_VERSION] = payload->info.platformVersion ?
106 std::string(payload->info.platformVersion) :
108 rep[OC_RSRVD_OS_VERSION] = payload->info.operatingSystemVersion ?
109 std::string(payload->info.operatingSystemVersion) :
111 rep[OC_RSRVD_HARDWARE_VERSION] = payload->info.hardwareVersion ?
112 std::string(payload->info.hardwareVersion) :
114 rep[OC_RSRVD_FIRMWARE_VERSION] = payload->info.firmwareVersion ?
115 std::string(payload->info.firmwareVersion) :
117 rep[OC_RSRVD_SUPPORT_URL] = payload->info.supportUrl ?
118 std::string(payload->info.supportUrl) :
120 rep[OC_RSRVD_SYSTEM_TIME] = payload->info.systemTime ?
121 std::string(payload->info.systemTime) :
124 m_reps.push_back(std::move(rep));
127 void MessageContainer::setPayload(const OCRepPayload* payload)
129 const OCRepPayload* pl = payload;
132 OCRepresentation cur;
136 this->addRepresentation(cur);
140 OCRepPayload* MessageContainer::getPayload() const
142 OCRepPayload* root = nullptr;
143 for(const auto& r : representations())
147 root = r.getPayload();
151 OCRepPayloadAppend(root, r.getPayload());
158 const std::vector<OCRepresentation>& MessageContainer::representations() const
163 void MessageContainer::addRepresentation(const OCRepresentation& rep)
165 m_reps.push_back(rep);
171 struct get_payload_array: boost::static_visitor<>
174 void operator()(T& /*arr*/)
176 throw std::logic_error("Invalid calc_dimensions_visitor type");
180 void operator()(std::vector<T>& arr)
183 dimensions[0] = arr.size();
184 dimTotal = calcDimTotal(dimensions);
186 array = (void*)OICMalloc(dimTotal * root_size);
188 for(size_t i = 0; i < dimensions[0]; ++i)
190 copy_to_array(arr[i], array, i);
195 void operator()(std::vector<std::vector<T>>& arr)
198 dimensions[0] = arr.size();
199 for(size_t i = 0; i < arr.size(); ++i)
201 dimensions[1] = std::max(dimensions[1], arr[i].size());
203 dimTotal = calcDimTotal(dimensions);
204 array = (void*)OICCalloc(1, dimTotal * root_size);
206 for(size_t i = 0; i < dimensions[0]; ++i)
208 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
210 copy_to_array(arr[i][j], array, i*dimensions[1] + j);
215 void operator()(std::vector<std::vector<std::vector<T>>>& arr)
218 dimensions[0] = arr.size();
219 for(size_t i = 0; i < arr.size(); ++i)
221 dimensions[1] = std::max(dimensions[1], arr[i].size());
223 for(size_t j = 0; j < arr[i].size(); ++j)
225 dimensions[2] = std::max(dimensions[2], arr[i][j].size());
229 dimTotal = calcDimTotal(dimensions);
230 array = (void*)OICCalloc(1, dimTotal * root_size);
232 for(size_t i = 0; i < dimensions[0]; ++i)
234 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
236 for(size_t k = 0; k < dimensions[2] && k < arr[i][j].size(); ++k)
238 copy_to_array(arr[i][j][k], array,
240 dimensions[2] * dimensions[1] * i +
248 void root_size_calc()
250 root_size = sizeof(T);
254 void copy_to_array(T item, void* array, size_t pos)
256 ((T*)array)[pos] = item;
259 size_t dimensions[MAX_REP_ARRAY_DEPTH];
266 void get_payload_array::root_size_calc<int>()
268 root_size = sizeof(int64_t);
272 void get_payload_array::root_size_calc<std::string>()
274 root_size = sizeof(char*);
278 void get_payload_array::root_size_calc<OC::OCRepresentation>()
280 root_size = sizeof(OCRepPayload*);
284 void get_payload_array::copy_to_array(int item, void* array, size_t pos)
286 ((int64_t*)array)[pos] = item;
290 void get_payload_array::copy_to_array(std::_Bit_reference br, void* array, size_t pos)
292 ((bool*)array)[pos] = static_cast<bool>(br);
296 void get_payload_array::copy_to_array(const std::string& item, void* array, size_t pos)
298 ((char**)array)[pos] = OICStrdup(item.c_str());
302 void get_payload_array::copy_to_array(OC::OCRepresentation item, void* array, size_t pos)
304 ((OCRepPayload**)array)[pos] = item.getPayload();
307 void OCRepresentation::getPayloadArray(OCRepPayload* payload,
308 const OCRepresentation::AttributeItem& item) const
310 get_payload_array vis{};
311 boost::apply_visitor(vis, m_values[item.attrname()]);
314 switch(item.base_type())
316 case AttributeType::Integer:
317 OCRepPayloadSetIntArrayAsOwner(payload, item.attrname().c_str(),
321 case AttributeType::Double:
322 OCRepPayloadSetDoubleArrayAsOwner(payload, item.attrname().c_str(),
326 case AttributeType::Boolean:
327 OCRepPayloadSetBoolArrayAsOwner(payload, item.attrname().c_str(),
331 case AttributeType::String:
332 OCRepPayloadSetStringArrayAsOwner(payload, item.attrname().c_str(),
336 case AttributeType::OCRepresentation:
337 OCRepPayloadSetPropObjectArrayAsOwner(payload, item.attrname().c_str(),
338 (OCRepPayload**)vis.array, vis.dimensions);
341 throw std::logic_error(std::string("GetPayloadArray: Not Implemented") +
342 std::to_string((int)item.base_type()));
346 OCRepPayload* OCRepresentation::getPayload() const
348 OCRepPayload* root = OCRepPayloadCreate();
351 throw std::bad_alloc();
354 OCRepPayloadSetUri(root, getUri().c_str());
356 for(const std::string& type : getResourceTypes())
358 OCRepPayloadAddResourceType(root, type.c_str());
361 for(const std::string& iface : getResourceInterfaces())
363 OCRepPayloadAddInterface(root, iface.c_str());
366 for(auto& val : *this)
370 case AttributeType::Null:
371 OCRepPayloadSetNull(root, val.attrname().c_str());
373 case AttributeType::Integer:
374 OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
376 case AttributeType::Double:
377 OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
378 val.getValue<double>());
380 case AttributeType::Boolean:
381 OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
383 case AttributeType::String:
384 OCRepPayloadSetPropString(root, val.attrname().c_str(),
385 static_cast<std::string>(val).c_str());
387 case AttributeType::OCRepresentation:
388 OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
389 static_cast<OCRepresentation>(val).getPayload());
391 case AttributeType::Vector:
392 getPayloadArray(root, val);
395 throw std::logic_error(std::string("Getpayload: Not Implemented") +
396 std::to_string((int)val.type()));
404 size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
406 if(dimensions[0] == 0)
408 throw std::logic_error("invalid calcArrayDepth");
410 else if(dimensions[1] == 0)
414 else if (dimensions[2] == 0)
425 T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
427 throw std::logic_error("payload_array_helper_copy: unsupported type");
430 int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
432 return pl->arr.iArray[index];
435 double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
437 return pl->arr.dArray[index];
440 bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
442 return pl->arr.bArray[index];
445 std::string OCRepresentation::payload_array_helper_copy<std::string>(
446 size_t index, const OCRepPayloadValue* pl)
448 return std::string(pl->arr.strArray[index]);
451 OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
452 size_t index, const OCRepPayloadValue* pl)
455 r.setPayload(pl->arr.objArray[index]);
460 void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
464 std::vector<T> val(pl->arr.dimensions[0]);
466 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
468 val[i] = payload_array_helper_copy<T>(i, pl);
470 this->setValue(std::string(pl->name), val);
474 std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
475 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
477 val[i].reserve(pl->arr.dimensions[1]);
478 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
480 val[i][j] = payload_array_helper_copy<T>(
481 i * pl->arr.dimensions[1] + j, pl);
484 this->setValue(std::string(pl->name), val);
488 std::vector<std::vector<std::vector<T>>> val;
489 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
491 val[i].reserve(pl->arr.dimensions[1]);
492 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
494 val[i][j].reserve(pl->arr.dimensions[2]);
495 for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
497 val[i][j][k] = payload_array_helper_copy<T>(
498 pl->arr.dimensions[2] * j +
499 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
505 this->setValue(std::string(pl->name), val);
509 throw std::logic_error("Invalid depth in payload_array_helper");
513 void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
519 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
521 case OCREP_PROP_DOUBLE:
522 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
524 case OCREP_PROP_BOOL:
525 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
527 case OCREP_PROP_STRING:
528 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
530 case OCREP_PROP_OBJECT:
531 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
534 throw std::logic_error("setPayload array invalid type");
539 void OCRepresentation::setPayload(const OCRepPayload* pl)
543 OCStringLL* ll = pl->types;
546 addResourceType(ll->value);
553 addResourceInterface(ll->value);
557 OCRepPayloadValue* val = pl->values;
563 case OCREP_PROP_NULL:
567 setValue<int>(val->name, val->i);
569 case OCREP_PROP_DOUBLE:
570 setValue<double>(val->name, val->d);
572 case OCREP_PROP_BOOL:
573 setValue<bool>(val->name, val->b);
575 case OCREP_PROP_STRING:
576 setValue<std::string>(val->name, val->str);
578 case OCREP_PROP_OBJECT:
580 OCRepresentation cur;
581 cur.setPayload(val->obj);
582 setValue<OCRepresentation>(val->name, cur);
585 case OCREP_PROP_ARRAY:
586 setPayloadArray(val);
589 throw std::logic_error(std::string("Not Implemented!") +
590 std::to_string((int)val->type));
597 void OCRepresentation::addChild(const OCRepresentation& rep)
599 m_children.push_back(rep);
602 void OCRepresentation::clearChildren()
607 const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
612 void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
614 m_children = children;
616 void OCRepresentation::setUri(const char* uri)
618 m_uri = uri ? uri : "";
621 void OCRepresentation::setUri(const std::string& uri)
626 std::string OCRepresentation::getUri() const
631 const std::vector<std::string>& OCRepresentation::getResourceTypes() const
633 return m_resourceTypes;
636 void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
638 m_resourceTypes = resourceTypes;
641 void OCRepresentation::addResourceType(const std::string& str)
643 m_resourceTypes.push_back(str);
646 const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
651 void OCRepresentation::addResourceInterface(const std::string& str)
653 m_interfaces.push_back(str);
656 void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
658 m_interfaces = resourceInterfaces;
661 bool OCRepresentation::hasAttribute(const std::string& str) const
663 return m_values.find(str) != m_values.end();
666 bool OCRepresentation::emptyData() const
668 // This logic is meant to determine whether based on the JSON serialization rules
669 // if this object will result in empty JSON. URI is only serialized if there is valid
670 // data, ResourceType and Interfaces are only serialized if we are a nothing, a
671 // child of a default or link item.
672 // Our values array is only printed in the if we are the child of a Batch resource,
673 // the parent in a 'default' situation, or not in a child/parent relationship.
678 else if ((m_interfaceType == InterfaceType::None
679 || m_interfaceType==InterfaceType::DefaultChild
680 || m_interfaceType==InterfaceType::LinkChild)
681 && (m_resourceTypes.size()>0 || m_interfaces.size()>0))
685 else if((m_interfaceType == InterfaceType::None
686 || m_interfaceType == InterfaceType::BatchChild
687 || m_interfaceType == InterfaceType::DefaultParent)
688 && m_values.size()>0)
693 if(m_children.size() > 0)
701 int OCRepresentation::numberOfAttributes() const
703 return m_values.size();
706 bool OCRepresentation::erase(const std::string& str)
708 return m_values.erase(str);
711 void OCRepresentation::setNULL(const std::string& str)
713 m_values[str] = OC::NullType();
716 bool OCRepresentation::isNULL(const std::string& str) const
718 auto x = m_values.find(str);
720 if(m_values.end() != x)
722 return x->second.which() == AttributeValueNullIndex;
726 throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
733 std::ostream& operator <<(std::ostream& os, const AttributeType at)
737 case AttributeType::Null:
740 case AttributeType::Integer:
743 case AttributeType::Double:
746 case AttributeType::Boolean:
749 case AttributeType::String:
752 case AttributeType::OCRepresentation:
753 os << "OCRepresentation";
755 case AttributeType::Vector:
763 // STL Container For OCRepresentation
766 OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
767 std::map<std::string, AttributeValue>& vals):
768 m_attrName(name), m_values(vals){}
770 OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
772 OCRepresentation::AttributeItem attr{key, m_values};
773 return std::move(attr);
776 const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
778 OCRepresentation::AttributeItem attr{key, m_values};
779 return std::move(attr);
782 const std::string& OCRepresentation::AttributeItem::attrname() const
787 template<typename T, typename = void>
790 // contains the actual type
792 // contains the inner most vector-type
794 // contains the AttributeType for this item
795 constexpr static AttributeType enum_type =
796 AttributeTypeConvert<T>::type;
797 // contains the AttributeType for this base-type
798 constexpr static AttributeType enum_base_type =
799 AttributeTypeConvert<T>::type;
800 // depth of the vector
801 constexpr static size_t depth = 0;
805 struct type_info<T, typename std::enable_if<is_vector<T>::value>::type>
808 typedef typename type_info<typename T::value_type>::base_type base_type;
809 constexpr static AttributeType enum_type = AttributeType::Vector;
810 constexpr static AttributeType enum_base_type =
811 type_info<typename T::value_type>::enum_base_type;
812 constexpr static size_t depth = 1 +
813 type_info<typename T::value_type>::depth;
816 struct type_introspection_visitor : boost::static_visitor<>
819 AttributeType base_type;
822 type_introspection_visitor() : boost::static_visitor<>(),
823 type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
825 template <typename T>
826 void operator()(T const& /*item*/)
828 type = type_info<T>::enum_type;
829 base_type = type_info<T>::enum_base_type;
830 depth = type_info<T>::depth;
834 AttributeType OCRepresentation::AttributeItem::type() const
836 type_introspection_visitor vis;
837 boost::apply_visitor(vis, m_values[m_attrName]);
841 AttributeType OCRepresentation::AttributeItem::base_type() const
843 type_introspection_visitor vis;
844 boost::apply_visitor(vis, m_values[m_attrName]);
845 return vis.base_type;
848 size_t OCRepresentation::AttributeItem::depth() const
850 type_introspection_visitor vis;
851 boost::apply_visitor(vis, m_values[m_attrName]);
855 OCRepresentation::iterator OCRepresentation::begin()
857 return OCRepresentation::iterator(m_values.begin(), m_values);
860 OCRepresentation::const_iterator OCRepresentation::begin() const
862 return OCRepresentation::const_iterator(m_values.begin(), m_values);
865 OCRepresentation::const_iterator OCRepresentation::cbegin() const
867 return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
870 OCRepresentation::iterator OCRepresentation::end()
872 return OCRepresentation::iterator(m_values.end(), m_values);
875 OCRepresentation::const_iterator OCRepresentation::end() const
877 return OCRepresentation::const_iterator(m_values.end(), m_values);
880 OCRepresentation::const_iterator OCRepresentation::cend() const
882 return OCRepresentation::const_iterator(m_values.cend(), m_values);
885 size_t OCRepresentation::size() const
887 return m_values.size();
890 bool OCRepresentation::empty() const
892 return m_values.empty();
895 bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
897 return m_iterator == rhs.m_iterator;
900 bool OCRepresentation::iterator::operator!=(const OCRepresentation::iterator& rhs) const
902 return m_iterator != rhs.m_iterator;
905 bool OCRepresentation::const_iterator::operator==(
906 const OCRepresentation::const_iterator& rhs) const
908 return m_iterator == rhs.m_iterator;
911 bool OCRepresentation::const_iterator::operator!=(
912 const OCRepresentation::const_iterator& rhs) const
914 return m_iterator != rhs.m_iterator;
917 OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
922 OCRepresentation::const_iterator::const_reference
923 OCRepresentation::const_iterator::operator*() const
928 OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
933 OCRepresentation::const_iterator::const_pointer
934 OCRepresentation::const_iterator::operator->() const
939 OCRepresentation::iterator& OCRepresentation::iterator::operator++()
942 if(m_iterator != m_item.m_values.end())
944 m_item.m_attrName = m_iterator->first;
948 m_item.m_attrName = "";
953 OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
956 if(m_iterator != m_item.m_values.end())
958 m_item.m_attrName = m_iterator->first;
962 m_item.m_attrName = "";
967 OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
969 OCRepresentation::iterator itr(*this);
974 OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
976 OCRepresentation::const_iterator itr(*this);
981 struct to_string_visitor : boost::static_visitor<>
984 template <typename T>
985 void operator()(T const& item)
987 str = boost::lexical_cast<std::string>(item);
990 template <typename T>
991 void operator()(std::vector<T> const& item)
993 to_string_visitor vis;
994 std::ostringstream stream;
997 for(const auto& i : item)
1000 stream << vis.str << " ";
1008 void to_string_visitor::operator()(bool const& item)
1010 str = item ? "true" : "false";
1014 void to_string_visitor::operator()(std::string const& item)
1020 void to_string_visitor::operator()(NullType const& /*item*/)
1026 void to_string_visitor::operator()(OCRepresentation const& /*item*/)
1028 str = "OC::OCRepresentation";
1031 std::string OCRepresentation::getValueToString(const std::string& key) const
1033 auto x = m_values.find(key);
1034 if(x != m_values.end())
1036 to_string_visitor vis;
1037 boost::apply_visitor(vis, x->second);
1038 return std::move(vis.str);
1044 std::string OCRepresentation::AttributeItem::getValueToString() const
1046 to_string_visitor vis;
1047 boost::apply_visitor(vis, m_values[m_attrName]);
1048 return std::move(vis.str);
1051 std::ostream& operator<<(std::ostream& os, const OCRepresentation::AttributeItem& ai)
1053 os << ai.getValueToString();