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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef __INTEL_OCAPI_H_2014_07_10
22 #define __INTEL_OCAPI_H_2014_07_10
31 #include <boost/property_tree/ptree.hpp>
32 #include <boost/property_tree/json_parser.hpp>
33 #include <boost/variant.hpp>
36 #include "OCHeaderOption.h"
37 #include <OCException.h>
39 #include "oc_logger.hpp"
45 class OCResourceRequest;
46 class OCResourceResponse;
51 typedef boost::iostreams::stream<OC::oc_log_stream> log_target_t;
55 /* We'll want to provide some sort of explicit hook for custom logging at some
56 point; until then, this should do nicely (note that since these are lambdas,
57 later a special target could be captured, allowing much flexibility): */
58 auto oclog_target = []() -> log_target_t&
60 static OC::oc_log_stream ols(oc_make_ostream_logger);
61 static log_target_t os(ols);
65 } // namespace OC::detail
67 auto oclog = []() -> boost::iostreams::stream<OC::oc_log_stream>&
69 return detail::oclog_target();
76 enum class OCPlatformStatus
82 enum class OCAdvertisementStatus
87 typedef std::string URI;
89 enum class ServiceType
102 enum class QualityOfService : uint8_t
105 MidQos = OC_MEDIUM_QOS,
106 HighQos = OC_HIGH_QOS,
107 NaQos = OC_NA_QOS // No Quality is defined, let the stack decide
111 * Data structure to provide the configuration.
112 * ServiceType: indicate InProc or OutOfProc
113 * ModeType : indicate whether we want to do server, client or both
114 * ipAddress : ip address of server.
115 * if you speecifiy 0.0.0.0 : it listens on any interface.
116 * port : port of server.
117 * : if you specifiy 0 : next available random port is used.
118 * : if you specify 5683 : client discovery can work even if they don't specify port.
119 * QoS : Quality of Service : CONFIRMABLE or NON CONFIRMABLE.
121 struct PlatformConfig
123 ServiceType serviceType;
125 std::string ipAddress;
128 QualityOfService QoS;
131 PlatformConfig(const ServiceType serviceType_,
132 const ModeType mode_,
133 const std::string& ipAddress_,
134 const uint16_t port_,
135 const QualityOfService QoS_)
136 : serviceType(serviceType_),
138 ipAddress(ipAddress_),
144 enum RequestHandlerFlag
147 RequestFlag = 1 << 1,
148 ObserverFlag = 1 << 2
151 enum class ObserveType
157 // Helper function to escape character in a string.
158 std::string escapeString(const std::string& value);
160 typedef std::map<std::string, std::string> AttributeMap;
162 class OCRepresentation
166 AttributeMap m_attributeMap;
167 std::vector<std::string> m_resourceTypes;
168 std::vector<std::string> m_resourceInterfaces;
171 std::vector<OCRepresentation> m_children;
174 OCRepresentation() {}
176 bool erase(const std::string& str)
178 return m_attributeMap.erase(str) != 0;
181 std::string getUri(void) const
186 template <typename T>
187 void setValue(const std::string& str, const T& val);
189 template <typename T>
190 bool getValue(const std::string& str, T& val) const;
192 template <typename T>
193 T getValue(const std::string& str) const;
195 bool hasAttribute(const std::string& str) const
197 return m_attributeMap.find(str) != m_attributeMap.end();
200 void setNULL(const std::string& str)
202 m_attributeMap[str] = "null";
205 bool isNULL(const std::string& str) const
207 auto x = m_attributeMap.find(str);
209 if(m_attributeMap.end() != x)
211 return x->second.compare("null") == 0;
215 std::ostringstream message;
216 message << "attribute: " << str << " doesn't exist\n";
217 throw OCException(message.str());
223 int numberOfAttributes() const
225 return m_attributeMap.size();
228 void setUri(std::string uri)
233 std::vector<OCRepresentation> getChildren(void) const
238 void setChildren(const std::vector<OCRepresentation>& children)
240 m_children = children;
243 std::weak_ptr<OCResource> getResource() const
245 // TODO Needs to be implemented
246 std::weak_ptr<OCResource> wp;
250 AttributeMap getAttributeMap() const
252 return m_attributeMap;
255 void setAttributeMap(const AttributeMap& map)
257 m_attributeMap = map;
260 std::string getJSONRepresentation(void) const
262 std::ostringstream json;
266 for(auto itr = m_attributeMap.begin(); itr!= m_attributeMap.end(); ++ itr)
268 if(itr != m_attributeMap.begin())
272 json << "\""<<itr->first<<"\":"<< itr->second;
279 std::vector<std::string> getResourceTypes() const
281 return m_resourceTypes;
284 void setResourceTypes(const std::vector<std::string>& resourceTypes)
286 m_resourceTypes = resourceTypes;
289 std::vector<std::string> getResourceInterfaces(void) const
291 return m_resourceInterfaces;
294 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
296 m_resourceInterfaces = resourceInterfaces;
300 OCRepresentation parseJSONToRepresentation(const std::string& str);
302 inline OCRepresentation parseJSONToRepresentation(const std::string& str)
304 OCRepresentation rep;
306 AttributeMap attributeMap;
308 std::stringstream requestStream;
309 requestStream << str;
310 boost::property_tree::ptree payload;
313 boost::property_tree::read_json(requestStream, payload);
315 catch(boost::property_tree::json_parser::json_parser_error &e)
317 throw OCException("JSON parse error");
320 for(auto& item: payload)
322 std::string name = item.first.data();
323 std::string value = item.second.data();
325 attributeMap[name] = value;
328 rep.setAttributeMap(attributeMap);
333 typedef boost::variant<
342 std::vector<std::string>,
343 std::vector<OCRepresentation>
346 template <typename T>
347 inline std::string getJSONFromVector(const std::vector<T>& v)
349 std::ostringstream json;
354 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(json, ","));
362 class ComposeVisitor : public boost::static_visitor<std::string>
366 // TODO different int sizes
367 std::string operator() (const int i) const
369 return std::to_string(i);
372 std::string operator() (const double d) const
374 return std::to_string(d);
377 std::string operator() (const std::string& str) const
379 std::ostringstream json;
387 std::string operator() (const bool b) const
399 std::string operator() (const std::vector<int>& numbers) const
401 return getJSONFromVector(numbers);
404 std::string operator() (const std::vector<double>& numbers) const
406 return getJSONFromVector(numbers);
409 std::string operator() (const std::vector<bool>& bools) const
411 std::ostringstream json;
419 b ? json << "true" : json << "false";
424 b ? json << ",true" : json << ",false";
432 std::string operator() (const std::vector<std::string>& strings) const
434 return getJSONFromVector(strings);
437 std::string operator() (const OCRepresentation& rep) const
439 std::ostringstream json;
443 json << escapeString(rep.getJSONRepresentation());
450 std::string operator() (const std::vector<OCRepresentation>& reps) const
452 std::ostringstream json;
461 json << escapeString(rep.getJSONRepresentation());
466 json << escapeString(rep.getJSONRepresentation());
477 inline void split(std::string input, char delimiter, std::vector<std::string>& tokens)
479 std::stringstream ss(input);
482 while(std::getline(ss, item, delimiter))
484 tokens.push_back(item);
488 class ParseVisitor : public boost::static_visitor<void>
492 ParseVisitor(std::string str): m_str(str)
496 void operator() (int& i) const
498 i = std::stoi(m_str);
501 void operator() (double& d) const
503 d = std::stod(m_str);
506 void operator() (std::string& str) const
511 void operator() (bool& b) const
513 b = m_str.compare("true") == 0;
516 void operator() (std::vector<int>& numbers) const
520 if(m_str.length() >= 2)
522 std::string str = m_str.substr(1, m_str.length()-2);
524 std::vector<std::string> tokens;
525 split(str, ',', tokens);
529 numbers.push_back(std::stoi(s));
534 throw OCException("Array type should have at least []");
539 void operator() (std::vector<double>& numbers) const
543 if(m_str.length() >= 2)
545 std::string str = m_str.substr(1, m_str.length()-2);
546 std::vector<std::string> tokens;
547 split(str, ',', tokens);
551 numbers.push_back(std::stod(s));
556 throw OCException("Array type should have at least []");
560 void operator() (std::vector<bool>& bools) const
564 if(m_str.length() >= 2)
566 std::string str = m_str.substr(2, m_str.length()-3);
568 std::vector<std::string> tokens;
569 split(str, ',', tokens);
573 bools.push_back(s.compare("true") == 0);
578 throw OCException("Array type should have at least []");
583 void operator() (std::vector<std::string>& strings) const
587 if(m_str.length() >= 2)
589 std::string str = m_str.substr(1, m_str.length()-2);
591 std::vector<std::string> tokens;
592 split(str, ',', tokens);
596 strings.push_back(s);
601 throw OCException("Array type should have at least []");
605 void operator() (std::vector<OCRepresentation>& reps) const
609 if(m_str.length() >= 2)
611 std::string str = m_str.substr(1, m_str.length()-2);
613 std::vector<std::string> tokens;
614 split(str, ',', tokens);
618 reps.push_back(parseJSONToRepresentation(s));
623 throw OCException("Array type should have at least []");
627 void operator() (OCRepresentation& rep) const
629 rep = parseJSONToRepresentation(m_str);
637 inline std::string getJSON(const AttributeValue& v)
639 return boost::apply_visitor(ComposeVisitor(), v);
642 inline void parseJSON(AttributeValue& v, std::string str)
644 boost::apply_visitor(ParseVisitor(str), v);
647 template <typename T>
648 void OCRepresentation::setValue(const std::string& str, const T& val)
650 m_attributeMap[str] = getJSON(val);
653 template <typename T>
654 T OCRepresentation::getValue(const std::string& str) const
658 auto x = m_attributeMap.find(str);
660 if(m_attributeMap.end() != x)
662 AttributeValue v = val;
663 parseJSON(v, x->second);
664 val = boost::get<T>(v);
670 template <typename T>
671 bool OCRepresentation::getValue(const std::string& str, T& val) const
673 auto x = m_attributeMap.find(str);
675 if(m_attributeMap.end() != x)
677 AttributeValue v = val;
678 parseJSON(v, x->second);
679 val = boost::get<T>(v);
688 // Typedef for header option vector
689 // OCHeaderOption class is in HeaderOption namespace
690 typedef std::vector<HeaderOption::OCHeaderOption> HeaderOptions;
692 // Typedef for query parameter map
693 typedef std::map<std::string, std::string> QueryParamsMap;
695 // Typedef for list of observation IDs
696 typedef std::vector<OCObservationId> ObservationIds;
698 enum class ObserveAction
706 // Action associated with observation request
707 ObserveAction action;
708 // Identifier for observation being registered/unregistered
709 OCObservationId obsId;
712 // const strings for different interfaces
715 const std::string DEFAULT_INTERFACE = "oc.mi.def";
717 // Used in discovering (GET) links to other resources of a collection.
718 const std::string LINK_INTERFACE = "oc.mi.ll";
720 // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
721 const std::string BATCH_INTERFACE = "oc.mi.b";
723 typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
725 typedef std::function<OCEntityHandlerResult (const std::shared_ptr<OCResourceRequest>,
726 const std::shared_ptr<OCResourceResponse>)> EntityHandler;
728 typedef std::function<void(OCStackResult, const unsigned int)> SubscribeCallback;
730 typedef std::function<void(const HeaderOptions&,
731 const OCRepresentation&, const int)> GetCallback;
733 typedef std::function<void(const HeaderOptions&,
734 const OCRepresentation&, const int)> PostCallback;
736 typedef std::function<void(const HeaderOptions&,
737 const OCRepresentation&, const int)> PutCallback;
739 typedef std::function<void(const HeaderOptions&, const int)> DeleteCallback;
741 typedef std::function<void(const HeaderOptions&,
742 const OCRepresentation&, const int, const int)> ObserveCallback;