Merge branch 'master' into notification-service
[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         /** not used. */
150         std::string                ipAddress;
151
152         /** not used. */
153         uint16_t                   port;
154
155         /** indicate Quality of Service : LowQos, MidQos,HighQos and NaQos(No quality Defined). */
156         QualityOfService           QoS;
157
158         /** persistant storage Handler structure (open/read/write/close/unlink). */
159         OCPersistentStorage        *ps;
160
161         public:
162             PlatformConfig()
163                 : serviceType(ServiceType::InProc),
164                 mode(ModeType::Both),
165                 serverConnectivity(CT_DEFAULT),
166                 clientConnectivity(CT_DEFAULT),
167                 ipAddress("0.0.0.0"),
168                 port(0),
169                 QoS(QualityOfService::NaQos),
170                 ps(nullptr)
171         {}
172             PlatformConfig(const ServiceType serviceType_,
173             const ModeType mode_,
174             OCConnectivityType serverConnectivity_,
175             OCConnectivityType clientConnectivity_,
176             const QualityOfService QoS_,
177             OCPersistentStorage *ps_ = nullptr)
178                 : serviceType(serviceType_),
179                 mode(mode_),
180                 serverConnectivity(serverConnectivity_),
181                 clientConnectivity(clientConnectivity_),
182                 ipAddress(""),
183                 port(0),
184                 QoS(QoS_),
185                 ps(ps_)
186         {}
187             // for backward compatibility
188             PlatformConfig(const ServiceType serviceType_,
189             const ModeType mode_,
190             const std::string& ipAddress_,
191             const uint16_t port_,
192             const QualityOfService QoS_,
193             OCPersistentStorage *ps_ = nullptr)
194                 : serviceType(serviceType_),
195                 mode(mode_),
196                 serverConnectivity(CT_DEFAULT),
197                 clientConnectivity(CT_DEFAULT),
198                 ipAddress(ipAddress_),
199                 port(port_),
200                 QoS(QoS_),
201                 ps(ps_)
202         {}
203     };
204
205     enum RequestHandlerFlag
206     {
207         RequestFlag = 1 << 1,
208         ObserverFlag = 1 << 2
209     };
210
211     enum class ObserveType
212     {
213         Observe,
214         ObserveAll
215     };
216
217     // Typedef for list of resource handles.
218     typedef std::vector<OCResourceHandle> ResourceHandles;
219
220     // Typedef for header option vector.
221     // OCHeaderOption class is in HeaderOption namespace.
222     typedef std::vector<HeaderOption::OCHeaderOption> HeaderOptions;
223
224     // Typedef for query parameter map.
225     typedef std::map<std::string, std::string> QueryParamsMap;
226
227     // Typedef for query parameter map with Vector
228     typedef std::map< std::string, std::vector<std::string> > QueryParamsList;
229
230     // Typedef for list of observation IDs.
231     typedef std::vector<OCObservationId> ObservationIds;
232
233     enum class ObserveAction
234     {
235         ObserveRegister,
236         ObserveUnregister
237     };
238
239     typedef struct
240     {
241         // Action associated with observation request
242         ObserveAction action;
243         // Identifier for observation being registered/unregistered
244         OCObservationId obsId;
245
246         OCConnectivityType connectivityType;
247         std::string address;
248         uint16_t port;
249     } ObservationInfo;
250
251     // const strings for different interfaces
252
253     // Default interface
254     const std::string DEFAULT_INTERFACE = "oic.if.baseline";
255
256     // Used in discovering (GET) links to other resources of a collection.
257     const std::string LINK_INTERFACE = "oic.if.ll";
258
259     // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
260     const std::string BATCH_INTERFACE = "oic.if.b";
261
262     // Used in GET, PUT, POST methods on links to other remote resources of a group.
263     const std::string GROUP_INTERFACE = "oic.mi.grp";
264
265     //Typedef for list direct paired devices
266     typedef std::vector<std::shared_ptr<OCDirectPairing>> PairedDevices;
267
268     typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
269
270     typedef std::function<void(const std::string&, const int)> FindErrorCallback;
271
272     typedef std::function<void(const OCRepresentation&)> FindDeviceCallback;
273
274     typedef std::function<void(const OCRepresentation&)> FindPlatformCallback;
275
276     typedef std::function<OCEntityHandlerResult(
277                             const std::shared_ptr<OCResourceRequest>)> EntityHandler;
278
279     typedef std::function<void(OCStackResult, const unsigned int,
280                                 const std::string&)> SubscribeCallback;
281
282     typedef std::function<void(const HeaderOptions&,
283                                 const OCRepresentation&, const int)> GetCallback;
284
285     typedef std::function<void(const HeaderOptions&,
286                                 const OCRepresentation&, const int)> PostCallback;
287
288     typedef std::function<void(const HeaderOptions&,
289                                 const OCRepresentation&, const int)> PutCallback;
290
291     typedef std::function<void(const HeaderOptions&, const int)> DeleteCallback;
292
293     typedef std::function<void(const HeaderOptions&,
294                                 const OCRepresentation&, const int, const int)> ObserveCallback;
295
296     typedef std::function<void(std::shared_ptr<OCDirectPairing>, OCStackResult)> DirectPairingCallback;
297
298     typedef std::function<void(const PairedDevices&)> GetDirectPairedCallback;
299
300     typedef std::function<void(const HeaderOptions&,
301                                const OCRepresentation&, const int,
302                                std::shared_ptr<OCResource>)> MQCreateTopicCallback;
303 #ifdef RD_CLIENT
304     typedef std::function<void(const OCRepresentation&, const int)> PublishResourceCallback;
305
306     typedef std::function<void(const int)> DeleteResourceCallback;
307 #endif
308 } // namespace OC
309
310 #endif