Partial Implementation of US1574:
[platform/upstream/iotivity.git] / include / OCApi.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef __INTEL_OCAPI_H_2014_07_10
22 #define __INTEL_OCAPI_H_2014_07_10
23
24 #include <string>
25 #include <sstream>
26 #include <vector>
27 #include <map>
28 #include <memory>
29
30 #include <boost/variant.hpp>
31
32 #include "ocstack.h"
33
34 namespace OC
35 {
36     class OCResource;
37     class OCResourceRequest;
38     class OCResourceResponse;
39 } // namespace OC
40
41 namespace OC { namespace OCReflect {
42     struct entity;
43 }} // namespace OC::OCReflect
44
45 namespace OC
46 {
47     enum class OCPlatformStatus
48     {
49         PlatformUp,
50         PlatformDown
51     };
52
53     enum class OCAdvertisementStatus
54     {
55         None
56     };
57
58     typedef std::string URI;
59
60     enum class ServiceType
61     {
62         InProc,
63         OutOfProc
64     };
65
66     enum class ModeType
67     {
68         Server,
69         Client,
70         Both
71     };
72
73     enum class QualityOfService : uint8_t
74     {
75         Confirmable     = OC_CONFIRMABLE,
76         NonConfirmable  = OC_NON_CONFIRMABLE
77     };
78
79     struct PlatformConfig
80     {
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
85
86         QualityOfService           QoS;
87
88         public:
89             PlatformConfig(const ServiceType serviceType_,
90             const ModeType mode_,
91             const std::string& ipAddress_,
92             const uint16_t port_,
93             const QualityOfService QoS_)
94                 : serviceType(serviceType_),
95                 mode(mode_),
96                 ipAddress(ipAddress_),
97                 port(port_),
98                 QoS(QoS_)
99         {}
100     };
101
102     enum class RequestHandlerFlag
103     {
104         InitFlag,
105         RequestFlag,
106         ObserverFlag
107     };
108
109     enum class ObserveType
110     {
111         Observe,
112         ObserveAll
113     };
114
115     typedef std::map<std::string, std::string> AttributeMap;
116
117     struct AttributeNull {};
118
119     // Forward declaration
120     struct AttributeDataValue;
121
122     typedef std::map<std::string, AttributeDataValue> AttributeMapValue;
123     typedef std::vector<AttributeDataValue> AttributeValueVector;
124
125     typedef boost::variant<
126                 AttributeNull,
127                 int,
128                 bool,
129                 std::string,
130                 AttributeMapValue,
131                 AttributeValueVector> AttributeValue;
132
133     struct AttributeDataValue
134     {
135         AttributeValue data;
136     };
137
138     class ComposeVisitor : public boost::static_visitor<std::string>
139     {
140         public:
141             std::string operator() (const AttributeNull& nl) const
142             {
143                 // TODO Not Implemented
144                 return std::string();
145             }
146
147             // TODO different int sizes
148             std::string operator() (const int i) const
149             {
150                 return std::to_string(i);
151             }
152
153             std::string operator() (const std::string& str) const
154             {
155                 return str;
156             }
157
158             std::string operator() (const bool b) const
159             {
160                 if(b)
161                 {
162                     return "true";
163                 }
164                 else
165                 {
166                     return "false";
167                 }
168             }
169
170             std::string operator() (const AttributeMapValue& objValue) const
171             {
172                 // TODO Not Implemented
173                 return std::string();
174                 std::ostringstream json;
175             }
176
177             std::string operator() (const AttributeValueVector& values) const
178             {
179                 // TODO Not Implemented
180                 return std::string();
181                 std::ostringstream json;
182             }
183     };
184
185     class ParseVisitor : public boost::static_visitor<void>
186     {
187         public:
188
189             ParseVisitor(std::string str): m_str(str)
190             {
191             }
192
193             void operator() (AttributeNull& nl) const
194             {
195                 // TODO Not Implemented
196             }
197
198             void operator() (int& i) const
199             {
200                 i = std::stoi(m_str);
201             }
202
203             void operator() (std::string& str) const
204             {
205                 str = m_str;
206             }
207
208             void operator() (bool& b) const
209             {
210                 b = m_str.compare("true") == 0;
211             }
212
213             void operator() (AttributeMapValue& objValue) const
214             {
215                 // TODO Not Implemented
216             }
217
218             void operator() (AttributeValueVector& values) const
219             {
220                 // TODO Not Implemented
221             }
222
223         private:
224             std::string m_str;
225     };
226
227     typedef std::map<std::string, AttributeValue> AttributeMap1;
228
229     std::string getJSON(const AttributeValue& v);
230     void parseJSON(AttributeValue& v, std::string str);
231
232     // Typedef for query parameter map
233     typedef std::map<std::string, std::string> QueryParamsMap;
234
235     // const strings for different interfaces
236
237     // Default interface
238     const std::string DEFAULT_INTERFACE = "oc.mi.def";
239
240     // Used in discovering (GET) links to other resources of a collection.
241     const std::string LINK_INTERFACE = "oc.mi.ll";
242
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";
245
246     // Helper function to escape character in a string.
247     std::string escapeString(const std::string& value);
248
249     class OCRepresentation
250     {
251
252         private:
253         std::string m_uri;
254         AttributeMap m_attributeMap;
255         std::vector<std::string> m_resourceTypes;
256         std::vector<std::string> m_resourceInterfaces;
257         int errorCode;
258
259         std::vector<OCRepresentation> m_children;
260
261         public:
262         OCRepresentation() {}
263
264         std::string getUri(void) const
265         {
266             return m_uri;
267         }
268
269         template <typename T>
270         void setValue(const std::string& str, const T& val)
271         {
272             m_attributeMap[str] = getJSON(val);
273         }
274
275         template <typename T>
276         bool getValue(const std::string& str, T& val) const
277         {
278             auto x = m_attributeMap.find(str);
279
280             if(m_attributeMap.end() != x)
281             {
282                 AttributeValue v = val;
283                 parseJSON(v, x->second);
284                 val = boost::get<T>(v);
285                 return true;
286             }
287             else
288             {
289                 return false;
290             } 
291         }
292
293         void setUri(std::string uri)
294         {
295             m_uri = uri;
296         }
297
298         std::vector<OCRepresentation> getChildren(void) const
299         {
300             return m_children;
301         }
302
303         void setChildren(std::vector<OCRepresentation> children)
304         {
305         m_children = children;
306         }
307
308         std::weak_ptr<OCResource> getResource() const
309         {
310             // TODO Needs to be implemented
311             std::weak_ptr<OCResource> wp;
312             return wp;
313         }
314
315         AttributeMap getAttributeMap() const
316         {
317             return m_attributeMap;
318         }
319
320         void setAttributeMap(const AttributeMap map)
321         {
322             m_attributeMap = map;
323         }
324
325         std::vector<std::string> getResourceTypes() const
326         {
327             return m_resourceTypes;
328         }
329
330         void setResourceTypes(std::vector<std::string> resourceTypes)
331         {
332             m_resourceTypes = resourceTypes;
333         }
334
335         std::vector<std::string> getResourceInterfaces(void) const
336         {
337             return m_resourceInterfaces;
338         }
339
340         void setResourceInterfaces(std::vector<std::string> resourceInterfaces)
341         {
342             m_resourceInterfaces = resourceInterfaces;
343         }
344     };
345
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;
352 } // namespace OC
353
354 #endif