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>
41 class OCResourceRequest;
42 class OCResourceResponse;
47 enum class OCPlatformStatus
53 enum class OCAdvertisementStatus
58 typedef std::string URI;
60 enum class ServiceType
73 enum class QualityOfService : uint8_t
75 Confirmable = OC_CONFIRMABLE,
76 NonConfirmable = OC_NON_CONFIRMABLE
80 * Data structure to provide the configuration.
81 * ServiceType: indicate InProc or OutOfProc
82 * ModeType : indicate whether we want to do server, client or both
83 * ipAddress : ip address of server.
84 * if you speecifiy 0.0.0.0 : it listens on any interface.
85 * port : port of server.
86 * : if you specifiy 0 : next available random port is used.
87 * : if you specify 5683 : client discovery can work even if they don't specify port.
88 * QoS : Quality of Service : CONFIRMABLE or NON CONFIRMABLE.
92 ServiceType serviceType;
94 std::string ipAddress;
100 PlatformConfig(const ServiceType serviceType_,
101 const ModeType mode_,
102 const std::string& ipAddress_,
103 const uint16_t port_,
104 const QualityOfService QoS_)
105 : serviceType(serviceType_),
107 ipAddress(ipAddress_),
113 enum RequestHandlerFlag
116 RequestFlag = 1 << 1,
117 ObserverFlag = 1 << 2
120 enum class ObserveType
126 // Helper function to escape character in a string.
127 std::string escapeString(const std::string& value);
129 typedef std::map<std::string, std::string> AttributeMap;
131 class OCRepresentation
135 AttributeMap m_attributeMap;
136 std::vector<std::string> m_resourceTypes;
137 std::vector<std::string> m_resourceInterfaces;
140 std::vector<OCRepresentation> m_children;
143 OCRepresentation() {}
145 bool erase(const std::string& str)
147 return m_attributeMap.erase(str) != 0;
150 std::string getUri(void) const
155 template <typename T>
156 void setValue(const std::string& str, const T& val);
158 template <typename T>
159 bool getValue(const std::string& str, T& val) const;
161 template <typename T>
162 T getValue(const std::string& str) const;
164 bool hasAttribute(const std::string& str) const
166 return m_attributeMap.find(str) != m_attributeMap.end();
169 int numberOfAttributes() const
171 return m_attributeMap.size();
174 void setUri(std::string uri)
179 std::vector<OCRepresentation> getChildren(void) const
184 void setChildren(const std::vector<OCRepresentation>& children)
186 m_children = children;
189 std::weak_ptr<OCResource> getResource() const
191 // TODO Needs to be implemented
192 std::weak_ptr<OCResource> wp;
196 AttributeMap getAttributeMap() const
198 return m_attributeMap;
201 void setAttributeMap(const AttributeMap& map)
203 m_attributeMap = map;
206 std::string getJSONRepresentation(void) const
208 std::ostringstream json;
212 for(auto itr = m_attributeMap.begin(); itr!= m_attributeMap.end(); ++ itr)
214 if(itr != m_attributeMap.begin())
218 json << "\""<<itr->first<<"\":"<< itr->second;
225 std::vector<std::string> getResourceTypes() const
227 return m_resourceTypes;
230 void setResourceTypes(const std::vector<std::string>& resourceTypes)
232 m_resourceTypes = resourceTypes;
235 std::vector<std::string> getResourceInterfaces(void) const
237 return m_resourceInterfaces;
240 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
242 m_resourceInterfaces = resourceInterfaces;
246 typedef boost::variant<
247 // TODO NULL value to be implmented.
255 std::vector<std::string>,
256 OCRepresentation> AttributeValue;
258 template <typename T>
259 inline std::string getJSONFromVector(std::vector<T> v)
261 std::ostringstream json;
266 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(json, ","));
274 class ComposeVisitor : public boost::static_visitor<std::string>
278 // TODO different int sizes
279 std::string operator() (const int i) const
281 return std::to_string(i);
284 std::string operator() (const double d) const
286 return std::to_string(d);
289 std::string operator() (const std::string& str) const
291 std::ostringstream json;
299 std::string operator() (const bool b) const
311 std::string operator() (const std::vector<int> numbers) const
313 return getJSONFromVector(numbers);
316 std::string operator() (const std::vector<double> numbers) const
318 return getJSONFromVector(numbers);
321 std::string operator() (const std::vector<bool> bools) const
323 std::ostringstream json;
331 b ? json << "true" : json << "false";
336 b ? json << ",true" : json << ",false";
344 std::string operator() (const std::vector<std::string> strings) const
346 return getJSONFromVector(strings);
349 std::string operator() (const OCRepresentation& rep) const
351 std::ostringstream json;
355 json << escapeString(rep.getJSONRepresentation());
364 inline void split(std::string input, char delimiter, std::vector<std::string>& tokens)
366 std::stringstream ss(input);
369 while(std::getline(ss, item, delimiter))
371 tokens.push_back(item);
375 class ParseVisitor : public boost::static_visitor<void>
379 ParseVisitor(std::string str): m_str(str)
383 void operator() (int& i) const
385 i = std::stoi(m_str);
388 void operator() (double& d) const
390 d = std::stod(m_str);
393 void operator() (std::string& str) const
398 void operator() (bool& b) const
400 b = m_str.compare("true") == 0;
403 void operator() (std::vector<int>& numbers) const
407 if(m_str.length() >= 2)
409 std::string str = m_str.substr(1, m_str.length()-2);
411 std::vector<std::string> tokens;
412 split(str, ',', tokens);
416 numbers.push_back(std::stoi(s));
422 std::cout << "Array type should have atleast []\n";
427 void operator() (std::vector<double>& numbers) const
431 if(m_str.length() >= 2)
433 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::stod(s));
445 std::cout << "Array type should have atleast []\n";
449 void operator() (std::vector<bool>& bools) const
453 if(m_str.length() >= 2)
455 std::string str = m_str.substr(1, m_str.length()-2);
457 std::vector<std::string> tokens;
458 split(str, ',', tokens);
462 bools.push_back(s.compare("true") == 0);
468 std::cout << "Array type should have atleast []\n";
473 void operator() (std::vector<std::string>& strings) const
477 if(m_str.length() >= 2)
479 std::string str = m_str.substr(1, m_str.length()-2);
481 std::vector<std::string> tokens;
482 split(str, ',', tokens);
486 strings.push_back(s);
492 std::cout << "Array type should have atleast []\n";
496 void operator() (OCRepresentation& rep) const
498 // TODO this will refactored
499 AttributeMap attributeMap;
501 std::stringstream requestStream;
502 requestStream << m_str;
503 boost::property_tree::ptree payload;
506 boost::property_tree::read_json(requestStream, payload);
508 catch(boost::property_tree::json_parser::json_parser_error &e)
511 std::cout << "Parse error\n";
515 for(auto& item: payload)
517 std::string name = item.first.data();
518 std::string value = item.second.data();
520 attributeMap[name] = value;
523 rep.setAttributeMap(attributeMap);
531 inline std::string getJSON(const AttributeValue& v)
533 return boost::apply_visitor(ComposeVisitor(), v);
536 inline void parseJSON(AttributeValue& v, std::string str)
538 boost::apply_visitor(ParseVisitor(str), v);
541 template <typename T>
542 void OCRepresentation::setValue(const std::string& str, const T& val)
544 m_attributeMap[str] = getJSON(val);
547 template <typename T>
548 T OCRepresentation::getValue(const std::string& str) const
552 auto x = m_attributeMap.find(str);
554 if(m_attributeMap.end() != x)
556 AttributeValue v = val;
557 parseJSON(v, x->second);
558 val = boost::get<T>(v);
564 template <typename T>
565 bool OCRepresentation::getValue(const std::string& str, T& val) const
567 auto x = m_attributeMap.find(str);
569 if(m_attributeMap.end() != x)
571 AttributeValue v = val;
572 parseJSON(v, x->second);
573 val = boost::get<T>(v);
583 // Typedef for query parameter map
584 typedef std::map<std::string, std::string> QueryParamsMap;
586 // Typedef for list of observation IDs
587 typedef std::vector<OCObservationId> ObservationIds;
589 enum class ObserveAction
597 // Action associated with observation request
598 ObserveAction action;
599 // Identifier for observation being registered/unregistered
600 OCObservationId obsId;
603 // const strings for different interfaces
606 const std::string DEFAULT_INTERFACE = "oc.mi.def";
608 // Used in discovering (GET) links to other resources of a collection.
609 const std::string LINK_INTERFACE = "oc.mi.ll";
611 // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
612 const std::string BATCH_INTERFACE = "oc.mi.b";
614 typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
615 typedef std::function<void(const std::shared_ptr<OCResourceRequest>, const std::shared_ptr<OCResourceResponse>)> RegisterCallback;
616 typedef std::function<void(OCStackResult, const unsigned int)> SubscribeCallback;
617 typedef std::function<void(const OCRepresentation&, const int)> GetCallback;
618 typedef std::function<void(const OCRepresentation&, const int)> PutCallback;
619 typedef std::function<void(const OCRepresentation&, const int, const int)> ObserveCallback;