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 <OCException.h>
42 class OCResourceRequest;
43 class OCResourceResponse;
48 enum class OCPlatformStatus
54 enum class OCAdvertisementStatus
59 typedef std::string URI;
61 enum class ServiceType
74 enum class QualityOfService : uint8_t
76 Confirmable = OC_CONFIRMABLE,
77 NonConfirmable = OC_NON_CONFIRMABLE
81 * Data structure to provide the configuration.
82 * ServiceType: indicate InProc or OutOfProc
83 * ModeType : indicate whether we want to do server, client or both
84 * ipAddress : ip address of server.
85 * if you speecifiy 0.0.0.0 : it listens on any interface.
86 * port : port of server.
87 * : if you specifiy 0 : next available random port is used.
88 * : if you specify 5683 : client discovery can work even if they don't specify port.
89 * QoS : Quality of Service : CONFIRMABLE or NON CONFIRMABLE.
93 ServiceType serviceType;
95 std::string ipAddress;
101 PlatformConfig(const ServiceType serviceType_,
102 const ModeType mode_,
103 const std::string& ipAddress_,
104 const uint16_t port_,
105 const QualityOfService QoS_)
106 : serviceType(serviceType_),
108 ipAddress(ipAddress_),
114 enum RequestHandlerFlag
117 RequestFlag = 1 << 1,
118 ObserverFlag = 1 << 2
121 enum class ObserveType
127 // Helper function to escape character in a string.
128 std::string escapeString(const std::string& value);
130 typedef std::map<std::string, std::string> AttributeMap;
132 class OCRepresentation
136 AttributeMap m_attributeMap;
137 std::vector<std::string> m_resourceTypes;
138 std::vector<std::string> m_resourceInterfaces;
141 std::vector<OCRepresentation> m_children;
144 OCRepresentation() {}
146 bool erase(const std::string& str)
148 return m_attributeMap.erase(str) != 0;
151 std::string getUri(void) const
156 template <typename T>
157 void setValue(const std::string& str, const T& val);
159 template <typename T>
160 bool getValue(const std::string& str, T& val) const;
162 template <typename T>
163 T getValue(const std::string& str) const;
165 bool hasAttribute(const std::string& str) const
167 return m_attributeMap.find(str) != m_attributeMap.end();
170 void setNULL(const std::string& str)
172 m_attributeMap[str] = "null";
175 bool isNULL(const std::string& str) const
177 auto x = m_attributeMap.find(str);
179 if(m_attributeMap.end() != x)
181 return x->second.compare("null") == 0;
185 std::ostringstream message;
186 message << "attribute: " << str << " doesn't exist\n";
187 throw OCException(message.str());
193 int numberOfAttributes() const
195 return m_attributeMap.size();
198 void setUri(std::string uri)
203 std::vector<OCRepresentation> getChildren(void) const
208 void setChildren(const std::vector<OCRepresentation>& children)
210 m_children = children;
213 std::weak_ptr<OCResource> getResource() const
215 // TODO Needs to be implemented
216 std::weak_ptr<OCResource> wp;
220 AttributeMap getAttributeMap() const
222 return m_attributeMap;
225 void setAttributeMap(const AttributeMap& map)
227 m_attributeMap = map;
230 std::string getJSONRepresentation(void) const
232 std::ostringstream json;
236 for(auto itr = m_attributeMap.begin(); itr!= m_attributeMap.end(); ++ itr)
238 if(itr != m_attributeMap.begin())
242 json << "\""<<itr->first<<"\":"<< itr->second;
249 std::vector<std::string> getResourceTypes() const
251 return m_resourceTypes;
254 void setResourceTypes(const std::vector<std::string>& resourceTypes)
256 m_resourceTypes = resourceTypes;
259 std::vector<std::string> getResourceInterfaces(void) const
261 return m_resourceInterfaces;
264 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
266 m_resourceInterfaces = resourceInterfaces;
270 typedef boost::variant<
278 std::vector<std::string>,
279 OCRepresentation> AttributeValue;
281 template <typename T>
282 inline std::string getJSONFromVector(std::vector<T> v)
284 std::ostringstream json;
289 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(json, ","));
297 class ComposeVisitor : public boost::static_visitor<std::string>
301 // TODO different int sizes
302 std::string operator() (const int i) const
304 return std::to_string(i);
307 std::string operator() (const double d) const
309 return std::to_string(d);
312 std::string operator() (const std::string& str) const
314 std::ostringstream json;
322 std::string operator() (const bool b) const
334 std::string operator() (const std::vector<int> numbers) const
336 return getJSONFromVector(numbers);
339 std::string operator() (const std::vector<double> numbers) const
341 return getJSONFromVector(numbers);
344 std::string operator() (const std::vector<bool> bools) const
346 std::ostringstream json;
354 b ? json << "true" : json << "false";
359 b ? json << ",true" : json << ",false";
367 std::string operator() (const std::vector<std::string> strings) const
369 return getJSONFromVector(strings);
372 std::string operator() (const OCRepresentation& rep) const
374 std::ostringstream json;
378 json << escapeString(rep.getJSONRepresentation());
387 inline void split(std::string input, char delimiter, std::vector<std::string>& tokens)
389 std::stringstream ss(input);
392 while(std::getline(ss, item, delimiter))
394 tokens.push_back(item);
398 class ParseVisitor : public boost::static_visitor<void>
402 ParseVisitor(std::string str): m_str(str)
406 void operator() (int& i) const
408 i = std::stoi(m_str);
411 void operator() (double& d) const
413 d = std::stod(m_str);
416 void operator() (std::string& str) const
421 void operator() (bool& b) const
423 b = m_str.compare("true") == 0;
426 void operator() (std::vector<int>& numbers) const
430 if(m_str.length() >= 2)
432 std::string str = m_str.substr(1, m_str.length()-2);
434 std::vector<std::string> tokens;
435 split(str, ',', tokens);
439 numbers.push_back(std::stoi(s));
444 throw OCException("Array type should have at least []");
449 void operator() (std::vector<double>& numbers) const
453 if(m_str.length() >= 2)
455 std::string str = m_str.substr(1, m_str.length()-2);
456 std::vector<std::string> tokens;
457 split(str, ',', tokens);
461 numbers.push_back(std::stod(s));
466 throw OCException("Array type should have at least []");
470 void operator() (std::vector<bool>& bools) const
474 if(m_str.length() >= 2)
476 std::string str = m_str.substr(1, m_str.length()-2);
478 std::vector<std::string> tokens;
479 split(str, ',', tokens);
483 bools.push_back(s.compare("true") == 0);
488 throw OCException("Array type should have at least []");
493 void operator() (std::vector<std::string>& strings) const
497 if(m_str.length() >= 2)
499 std::string str = m_str.substr(1, m_str.length()-2);
501 std::vector<std::string> tokens;
502 split(str, ',', tokens);
506 strings.push_back(s);
511 throw OCException("Array type should have at least []");
515 void operator() (OCRepresentation& rep) const
517 // TODO this will refactored
518 AttributeMap attributeMap;
520 std::stringstream requestStream;
521 requestStream << m_str;
522 boost::property_tree::ptree payload;
525 boost::property_tree::read_json(requestStream, payload);
527 catch(boost::property_tree::json_parser::json_parser_error &e)
529 throw OCException("JSON parse error");
532 for(auto& item: payload)
534 std::string name = item.first.data();
535 std::string value = item.second.data();
537 attributeMap[name] = value;
540 rep.setAttributeMap(attributeMap);
548 inline std::string getJSON(const AttributeValue& v)
550 return boost::apply_visitor(ComposeVisitor(), v);
553 inline void parseJSON(AttributeValue& v, std::string str)
555 boost::apply_visitor(ParseVisitor(str), v);
558 template <typename T>
559 void OCRepresentation::setValue(const std::string& str, const T& val)
561 m_attributeMap[str] = getJSON(val);
564 template <typename T>
565 T OCRepresentation::getValue(const std::string& str) const
569 auto x = m_attributeMap.find(str);
571 if(m_attributeMap.end() != x)
573 AttributeValue v = val;
574 parseJSON(v, x->second);
575 val = boost::get<T>(v);
581 template <typename T>
582 bool OCRepresentation::getValue(const std::string& str, T& val) const
584 auto x = m_attributeMap.find(str);
586 if(m_attributeMap.end() != x)
588 AttributeValue v = val;
589 parseJSON(v, x->second);
590 val = boost::get<T>(v);
600 // Typedef for query parameter map
601 typedef std::map<std::string, std::string> QueryParamsMap;
603 // Typedef for list of observation IDs
604 typedef std::vector<OCObservationId> ObservationIds;
606 enum class ObserveAction
614 // Action associated with observation request
615 ObserveAction action;
616 // Identifier for observation being registered/unregistered
617 OCObservationId obsId;
620 // const strings for different interfaces
623 const std::string DEFAULT_INTERFACE = "oc.mi.def";
625 // Used in discovering (GET) links to other resources of a collection.
626 const std::string LINK_INTERFACE = "oc.mi.ll";
628 // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
629 const std::string BATCH_INTERFACE = "oc.mi.b";
631 typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
632 typedef std::function<void(const std::shared_ptr<OCResourceRequest>,
633 const std::shared_ptr<OCResourceResponse>)> EntityHandler;
634 typedef std::function<void(OCStackResult, const unsigned int)> SubscribeCallback;
635 typedef std::function<void(const OCRepresentation&, const int)> GetCallback;
636 typedef std::function<void(const OCRepresentation&, const int)> PutCallback;
637 typedef std::function<void(const OCRepresentation&, const int)> PostCallback;
638 typedef std::function<void(const OCRepresentation&, const int, const int)> ObserveCallback;