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
30 #include <boost/variant.hpp>
37 class OCResourceRequest;
38 class OCResourceResponse;
41 namespace OC { namespace OCReflect {
43 }} // namespace OC::OCReflect
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
81 ServiceType serviceType; // This will indicate whether it is InProc or OutOfProc
82 ModeType mode; // This will indicate whether we want to do server, client or both
83 std::string ipAddress; // This is the ipAddress of the server to connect to
84 uint16_t port; // Port of the server
89 PlatformConfig(const ServiceType serviceType_,
91 const std::string& ipAddress_,
93 const QualityOfService QoS_)
94 : serviceType(serviceType_),
96 ipAddress(ipAddress_),
102 enum class RequestHandlerFlag
109 enum class ObserveType
115 typedef std::map<std::string, std::string> AttributeMap;
117 struct AttributeNull {};
119 // Forward declaration
120 struct AttributeDataValue;
122 typedef std::map<std::string, AttributeDataValue> AttributeMapValue;
123 typedef std::vector<AttributeDataValue> AttributeValueVector;
125 typedef boost::variant<
131 AttributeValueVector> AttributeValue;
133 struct AttributeDataValue
138 class ComposeVisitor : public boost::static_visitor<std::string>
141 std::string operator() (const AttributeNull& nl) const
143 // TODO Not Implemented
144 return std::string();
147 // TODO different int sizes
148 std::string operator() (const int i) const
150 return std::to_string(i);
153 std::string operator() (const std::string& str) const
158 std::string operator() (const bool b) const
170 std::string operator() (const AttributeMapValue& objValue) const
172 // TODO Not Implemented
173 return std::string();
174 std::ostringstream json;
177 std::string operator() (const AttributeValueVector& values) const
179 // TODO Not Implemented
180 return std::string();
181 std::ostringstream json;
185 class ParseVisitor : public boost::static_visitor<void>
189 ParseVisitor(std::string str): m_str(str)
193 void operator() (AttributeNull& nl) const
195 // TODO Not Implemented
198 void operator() (int& i) const
200 i = std::stoi(m_str);
203 void operator() (std::string& str) const
208 void operator() (bool& b) const
210 b = m_str.compare("true") == 0;
213 void operator() (AttributeMapValue& objValue) const
215 // TODO Not Implemented
218 void operator() (AttributeValueVector& values) const
220 // TODO Not Implemented
227 typedef std::map<std::string, AttributeValue> AttributeMap1;
229 std::string getJSON(const AttributeValue& v);
230 void parseJSON(AttributeValue& v, std::string str);
232 // Typedef for query parameter map
233 typedef std::map<std::string, std::string> QueryParamsMap;
235 // const strings for different interfaces
238 const std::string DEFAULT_INTERFACE = "oc.mi.def";
240 // Used in discovering (GET) links to other resources of a collection.
241 const std::string LINK_INTERFACE = "oc.mi.ll";
243 // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
244 const std::string BATCH_INTERFACE = "oc.mi.b";
246 // Helper function to escape character in a string.
247 std::string escapeString(const std::string& value);
249 class OCRepresentation
254 AttributeMap m_attributeMap;
255 std::vector<std::string> m_resourceTypes;
256 std::vector<std::string> m_resourceInterfaces;
259 std::vector<OCRepresentation> m_children;
262 OCRepresentation() {}
264 std::string getUri(void) const
269 template <typename T>
270 void setValue(const std::string& str, const T& val)
272 m_attributeMap[str] = getJSON(val);
275 template <typename T>
276 bool getValue(const std::string& str, T& val) const
278 auto x = m_attributeMap.find(str);
280 if(m_attributeMap.end() != x)
282 AttributeValue v = val;
283 parseJSON(v, x->second);
284 val = boost::get<T>(v);
293 void setUri(std::string uri)
298 std::vector<OCRepresentation> getChildren(void) const
303 void setChildren(std::vector<OCRepresentation> children)
305 m_children = children;
308 std::weak_ptr<OCResource> getResource() const
310 // TODO Needs to be implemented
311 std::weak_ptr<OCResource> wp;
315 AttributeMap getAttributeMap() const
317 return m_attributeMap;
320 void setAttributeMap(const AttributeMap map)
322 m_attributeMap = map;
325 std::vector<std::string> getResourceTypes() const
327 return m_resourceTypes;
330 void setResourceTypes(std::vector<std::string> resourceTypes)
332 m_resourceTypes = resourceTypes;
335 std::vector<std::string> getResourceInterfaces(void) const
337 return m_resourceInterfaces;
340 void setResourceInterfaces(std::vector<std::string> resourceInterfaces)
342 m_resourceInterfaces = resourceInterfaces;
346 typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
347 typedef std::function<void(const std::shared_ptr<OCResourceRequest>, const std::shared_ptr<OCResourceResponse>)> RegisterCallback;
348 typedef std::function<void(OCStackResult, const unsigned int)> SubscribeCallback;
349 typedef std::function<void(const OCRepresentation&, const int)> GetCallback;
350 typedef std::function<void(const OCRepresentation&, const int)> PutCallback;
351 typedef std::function<void(const OCRepresentation&, const int, const int)> ObserveCallback;