replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / 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 OC_OCAPI_H_
22 #define OC_OCAPI_H_
23
24 #include <string>
25 #include <sstream>
26 #include <vector>
27 #include <map>
28 #include <memory>
29 #include <iterator>
30 #if defined(_MSC_VER)
31 #include <functional>
32 #endif
33
34 #include "octypes.h"
35 #include "OCHeaderOption.h"
36 #include <OCException.h>
37 #include "StringConstants.h"
38 #include "oc_logger.hpp"
39
40 #include <OCRepresentation.h>
41
42 namespace OC
43 {
44     class OCResource;
45     class OCResourceRequest;
46     class OCResourceResponse;
47     class OCDirectPairing;
48 } // namespace OC
49
50 namespace OC
51 {
52 #if defined(_MSC_VER)
53     extern std::ostream& oclog();
54 #else
55     typedef boost::iostreams::stream<OC::oc_log_stream>     log_target_t;
56
57     namespace detail
58     {
59         /* We'll want to provide some sort of explicit hook for custom logging at some
60         point; until then, this should do nicely (note that since these are lambdas,
61         later a special target could be captured, allowing much flexibility): */
62         auto oclog_target = []() -> log_target_t&
63         {
64             static OC::oc_log_stream    ols(oc_make_ostream_logger);
65             static log_target_t         os(ols);
66
67             return os;
68         };
69     } // namespace OC::detail
70
71     auto oclog = []() -> boost::iostreams::stream<OC::oc_log_stream>&
72     {
73         return detail::oclog_target();
74     };
75 #endif
76 } // namespace OC
77
78 namespace OC
79 {
80
81     enum class OCPlatformStatus
82     {
83         PlatformUp,
84         PlatformDown
85     };
86
87     enum class OCAdvertisementStatus
88     {
89         None
90     };
91
92     typedef std::string URI;
93
94     enum class ServiceType
95     {
96         InProc,
97         OutOfProc
98     };
99
100     /**
101      * Host Mode of Operation.
102      */
103     enum class ModeType
104     {
105         Server,
106         Client,
107         Both,
108         Gateway  /**< Client server mode along with routing capabilities.*/
109     };
110
111     /**
112      * Quality of Service attempts to abstract the guarantees provided by the underlying transport
113      * protocol. The precise definitions of each quality of service level depend on the
114      * implementation. In descriptions below are for the current implementation and may changed
115      * over time.
116      */
117     enum class QualityOfService : uint8_t
118     {
119         /** Packet delivery is best effort. */
120         LowQos      = OC_LOW_QOS,
121
122         /** Packet delivery is best effort. */
123         MidQos      = OC_MEDIUM_QOS,
124
125         /** Acknowledgments are used to confirm delivery. */
126         HighQos     = OC_HIGH_QOS,
127
128         /** No Quality is defined, let the stack decide. */
129         NaQos       = OC_NA_QOS
130     };
131
132     /**
133      *  Data structure to provide the configuration.
134      */
135     struct PlatformConfig
136     {
137         /** indicate InProc or OutOfProc. */
138         ServiceType                serviceType;
139
140         /** indicate whether we want to do server, client or both. */
141         ModeType                   mode;
142
143         /** default flags for server. */
144         OCConnectivityType         serverConnectivity;
145
146         /** default flags for client. */
147         OCConnectivityType         clientConnectivity;
148
149         /** transport type to initialize. */
150         OCTransportAdapter         transportType;
151
152         /** not used. */
153         std::string                ipAddress;
154
155         /** not used. */
156         uint16_t                   port;
157
158         /** indicate Quality of Service : LowQos, MidQos,HighQos and NaQos(No quality Defined). */
159         QualityOfService           QoS;
160
161         /** persistant storage Handler structure (open/read/write/close/unlink). */
162         OCPersistentStorage        *ps;
163
164         /** persistant storage Handler structure (open/read/write/close/unlink). */
165         OCPersistentStorage        *psEnc;
166
167         /** persistant storage Handler structure (open/read/write/close/unlink). */
168         OCPersistentStorage        *psRescue;
169
170         /** pointer to save key. */
171         unsigned char*              key;
172
173         public:
174             PlatformConfig()
175                 : serviceType(ServiceType::InProc),
176                 mode(ModeType::Both),
177                 serverConnectivity(CT_DEFAULT),
178                 clientConnectivity(CT_DEFAULT),
179                 transportType(OC_DEFAULT_ADAPTER),
180                 ipAddress("0.0.0.0"),
181                 port(0),
182                 QoS(QualityOfService::NaQos),                
183                 ps(nullptr),
184                 psEnc(nullptr),
185                 psRescue(nullptr),
186                 key(nullptr)
187         {}
188             PlatformConfig(const ServiceType serviceType_,
189             const ModeType mode_,
190             OCConnectivityType serverConnectivity_,
191             OCConnectivityType clientConnectivity_,
192             const QualityOfService QoS_,
193             OCPersistentStorage *ps_ = nullptr,
194             OCPersistentStorage *psEnc_ = nullptr,
195             OCPersistentStorage *psRescue_ = nullptr,
196             unsigned char *key_ = nullptr)
197                 : serviceType(serviceType_),
198                 mode(mode_),
199                 serverConnectivity(serverConnectivity_),
200                 clientConnectivity(clientConnectivity_),
201                 transportType(OC_DEFAULT_ADAPTER),
202                 ipAddress(""),
203                 port(0),
204                 QoS(QoS_),                
205                 ps(ps_),
206                 psEnc(psEnc_),
207                 psRescue(psRescue_),
208                 key(key_)
209
210         {}
211             // for backward compatibility
212             PlatformConfig(const ServiceType serviceType_,
213             const ModeType mode_,
214             const std::string& ipAddress_,
215             const uint16_t port_,
216             const QualityOfService QoS_,
217             OCPersistentStorage *ps_ = nullptr,
218             OCPersistentStorage *psEnc_ = nullptr,
219             OCPersistentStorage *psRescue_ = nullptr,
220             unsigned char *key_ = nullptr)
221                 : serviceType(serviceType_),
222                 mode(mode_),
223                 serverConnectivity(CT_DEFAULT),
224                 clientConnectivity(CT_DEFAULT),
225                 transportType(OC_DEFAULT_ADAPTER),
226                 ipAddress(ipAddress_),
227                 port(port_),
228                 QoS(QoS_),                
229                 ps(ps_),
230                 psEnc(psEnc_),
231                 psRescue(psRescue_),
232                 key(key_)
233  
234         {}
235
236             PlatformConfig(const ServiceType serviceType_,
237             const ModeType mode_,
238             const std::string& ipAddress_,
239             const uint16_t port_,
240             const OCTransportAdapter transportType_,
241             const QualityOfService QoS_,
242             unsigned char *key_,
243             OCPersistentStorage *ps_ = nullptr,
244             OCPersistentStorage *psEnc_ = nullptr,
245             OCPersistentStorage *psRescue_ = nullptr)
246                 : serviceType(serviceType_),
247                 mode(mode_),
248                 transportType(transportType_),
249                 ipAddress(ipAddress_),
250                 port(port_),
251                 QoS(QoS_),                
252                 ps(ps_),
253                 psEnc(psEnc_),
254                 psRescue(psRescue_),
255                 key(key_)
256         {}
257
258            PlatformConfig(const ServiceType serviceType_,
259             const ModeType mode_,
260             OCTransportAdapter transportType_,
261             const QualityOfService QoS_,
262             OCPersistentStorage *ps_ = nullptr,
263             OCPersistentStorage *psEnc_ = nullptr,
264             OCPersistentStorage *psRescue_ = nullptr,
265             unsigned char *key_ = nullptr)
266                 : serviceType(serviceType_),
267                 mode(mode_),
268                 serverConnectivity(CT_DEFAULT),
269                 clientConnectivity(CT_DEFAULT),
270                 transportType(transportType_),
271                 ipAddress(""),
272                 port(0),
273                 QoS(QoS_),
274                 ps(ps_),
275                 psEnc(psEnc_),
276                 psRescue(psRescue_),
277                 key(key_)
278         {}
279             PlatformConfig(const ServiceType serviceType_,
280             const ModeType mode_,
281             OCConnectivityType serverConnectivity_,
282             OCConnectivityType clientConnectivity_,
283             OCTransportAdapter transportType_,
284             const QualityOfService QoS_,
285             OCPersistentStorage *ps_ = nullptr,
286             OCPersistentStorage *psEnc_ = nullptr,
287             OCPersistentStorage *psRescue_ = nullptr,
288             unsigned char *key_ = nullptr)
289                 : serviceType(serviceType_),
290                 mode(mode_),
291                 serverConnectivity(serverConnectivity_),
292                 clientConnectivity(clientConnectivity_),
293                 transportType(transportType_),
294                 ipAddress(""),
295                 port(0),
296                 QoS(QoS_),
297                 ps(ps_),
298                 psEnc(psEnc_),
299                 psRescue(psRescue_),
300                 key(key_)
301         {}
302
303     };
304
305     enum RequestHandlerFlag
306     {
307         RequestFlag = 1 << 1,
308         ObserverFlag = 1 << 2
309     };
310
311     enum class ObserveType
312     {
313         Observe,
314         ObserveAll
315     };
316
317     // Typedef for list of resource handles.
318     typedef std::vector<OCResourceHandle> ResourceHandles;
319
320     // Typedef for header option vector.
321     // OCHeaderOption class is in HeaderOption namespace.
322     typedef std::vector<HeaderOption::OCHeaderOption> HeaderOptions;
323
324     // Typedef for query parameter map.
325     typedef std::map<std::string, std::string> QueryParamsMap;
326
327     // Typedef for query parameter map with Vector
328     typedef std::map< std::string, std::vector<std::string> > QueryParamsList;
329
330     // Typedef for list of observation IDs.
331     typedef std::vector<OCObservationId> ObservationIds;
332
333     enum class ObserveAction
334     {
335         ObserveRegister,
336         ObserveUnregister
337     };
338
339     typedef struct
340     {
341         // Action associated with observation request
342         ObserveAction action;
343         // Identifier for observation being registered/unregistered
344         OCObservationId obsId;
345
346         OCConnectivityType connectivityType;
347         std::string address;
348         uint16_t port;
349     } ObservationInfo;
350
351     // const strings for different interfaces
352
353     // Default interface
354     const std::string DEFAULT_INTERFACE = "oic.if.baseline";
355
356     // Used in discovering (GET) links to other resources of a collection.
357     const std::string LINK_INTERFACE = "oic.if.ll";
358
359     // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
360     const std::string BATCH_INTERFACE = "oic.if.b";
361
362     // Used in GET, PUT, POST methods on links to other remote resources of a group.
363     const std::string GROUP_INTERFACE = "oic.mi.grp";
364
365     //Typedef for list direct paired devices
366     typedef std::vector<std::shared_ptr<OCDirectPairing>> PairedDevices;
367
368     typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
369
370     typedef std::function<void(const std::string&, const int)> FindErrorCallback;
371
372     typedef std::function<void(std::vector<std::shared_ptr<OCResource>>)> FindResListCallback;
373
374     typedef std::function<void(const OCRepresentation&)> FindDeviceCallback;
375
376     typedef std::function<void(const OCRepresentation&)> FindPlatformCallback;
377
378     typedef std::function<OCEntityHandlerResult(
379                             const std::shared_ptr<OCResourceRequest>)> EntityHandler;
380
381     typedef std::function<void(OCStackResult, const unsigned int,
382                                 const std::string&)> SubscribeCallback;
383
384     typedef std::function<void(const HeaderOptions&,
385                                 const OCRepresentation&, const int)> GetCallback;
386
387     typedef std::function<void(const HeaderOptions&,
388                                 const OCRepresentation&, const int)> PostCallback;
389
390     typedef std::function<void(const HeaderOptions&,
391                                 const OCRepresentation&, const int)> PutCallback;
392
393     typedef std::function<void(const HeaderOptions&, const int)> DeleteCallback;
394
395     typedef std::function<void(const HeaderOptions&,
396                                 const OCRepresentation&, const int, const int)> ObserveCallback;
397
398     typedef std::function<void(std::shared_ptr<OCDirectPairing>, OCStackResult)> DirectPairingCallback;
399
400     typedef std::function<void(const PairedDevices&)> GetDirectPairedCallback;
401
402     typedef std::function<void(const int, const std::string&,
403                                std::shared_ptr<OCResource>)> MQTopicCallback;
404 #ifdef TCP_ADAPTER
405     typedef std::function<void(const int, const OCRepresentation&)> KeepAliveCallback;
406 #endif
407 } // namespace OC
408
409 #endif