Merge branch 'master' into windows-port
[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 //Squelch "decorated name length exceeded, name was truncated"
33 // This compensates for templates full of templates
34 #pragma warning(disable : 4503)
35 #endif
36
37 #include "octypes.h"
38 #include "OCHeaderOption.h"
39 #include <OCException.h>
40 #include "StringConstants.h"
41 #include "oc_logger.hpp"
42
43 #include <OCRepresentation.h>
44
45 namespace OC
46 {
47     class OCResource;
48     class OCResourceRequest;
49     class OCResourceResponse;
50     class OCDirectPairing;
51 } // namespace OC
52
53 namespace OC
54 {
55 #if defined(_MSC_VER)
56     extern std::ostream& oclog();
57 #else
58     typedef boost::iostreams::stream<OC::oc_log_stream>     log_target_t;
59
60     namespace detail
61     {
62         /* We'll want to provide some sort of explicit hook for custom logging at some
63         point; until then, this should do nicely (note that since these are lambdas,
64         later a special target could be captured, allowing much flexibility): */
65         auto oclog_target = []() -> log_target_t&
66         {
67             static OC::oc_log_stream    ols(oc_make_ostream_logger);
68             static log_target_t         os(ols);
69
70             return os;
71         };
72     } // namespace OC::detail
73
74     auto oclog = []() -> boost::iostreams::stream<OC::oc_log_stream>&
75     {
76         return detail::oclog_target();
77     };
78 #endif
79 } // namespace OC
80
81 namespace OC
82 {
83
84     enum class OCPlatformStatus
85     {
86         PlatformUp,
87         PlatformDown
88     };
89
90     enum class OCAdvertisementStatus
91     {
92         None
93     };
94
95     typedef std::string URI;
96
97     enum class ServiceType
98     {
99         InProc,
100         OutOfProc
101     };
102
103     /**
104      * Host Mode of Operation.
105      */
106     enum class ModeType
107     {
108         Server,
109         Client,
110         Both,
111         Gateway  /**< Client server mode along with routing capabilities.*/
112     };
113
114     /**
115      * Quality of Service attempts to abstract the guarantees provided by the underlying transport
116      * protocol. The precise definitions of each quality of service level depend on the
117      * implementation. In descriptions below are for the current implementation and may changed
118      * over time.
119      */
120     enum class QualityOfService : uint8_t
121     {
122         /** Packet delivery is best effort. */
123         LowQos      = OC_LOW_QOS,
124
125         /** Packet delivery is best effort. */
126         MidQos      = OC_MEDIUM_QOS,
127
128         /** Acknowledgments are used to confirm delivery. */
129         HighQos     = OC_HIGH_QOS,
130
131         /** No Quality is defined, let the stack decide. */
132         NaQos       = OC_NA_QOS
133     };
134
135     /**
136      *  Data structure to provide the configuration.
137      */
138     struct PlatformConfig
139     {
140         /** indicate InProc or OutOfProc. */
141         ServiceType                serviceType;
142
143         /** indicate whether we want to do server, client or both. */
144         ModeType                   mode;
145
146         /** default flags for server. */
147         OCConnectivityType         serverConnectivity;
148
149         /** default flags for client. */
150         OCConnectivityType         clientConnectivity;
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         public:
165             PlatformConfig()
166                 : serviceType(ServiceType::InProc),
167                 mode(ModeType::Both),
168                 serverConnectivity(CT_DEFAULT),
169                 clientConnectivity(CT_DEFAULT),
170                 ipAddress("0.0.0.0"),
171                 port(0),
172                 QoS(QualityOfService::NaQos),
173                 ps(nullptr)
174         {}
175             PlatformConfig(const ServiceType serviceType_,
176             const ModeType mode_,
177             OCConnectivityType serverConnectivity_,
178             OCConnectivityType clientConnectivity_,
179             const QualityOfService QoS_,
180             OCPersistentStorage *ps_ = nullptr)
181                 : serviceType(serviceType_),
182                 mode(mode_),
183                 serverConnectivity(serverConnectivity_),
184                 clientConnectivity(clientConnectivity_),
185                 ipAddress(""),
186                 port(0),
187                 QoS(QoS_),
188                 ps(ps_)
189         {}
190             // for backward compatibility
191             PlatformConfig(const ServiceType serviceType_,
192             const ModeType mode_,
193             const std::string& ipAddress_,
194             const uint16_t port_,
195             const QualityOfService QoS_,
196             OCPersistentStorage *ps_ = nullptr)
197                 : serviceType(serviceType_),
198                 mode(mode_),
199                 serverConnectivity(CT_DEFAULT),
200                 clientConnectivity(CT_DEFAULT),
201                 ipAddress(ipAddress_),
202                 port(port_),
203                 QoS(QoS_),
204                 ps(ps_)
205         {}
206     };
207
208     enum RequestHandlerFlag
209     {
210         RequestFlag = 1 << 1,
211         ObserverFlag = 1 << 2
212     };
213
214     enum class ObserveType
215     {
216         Observe,
217         ObserveAll
218     };
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 list of observation IDs
228     typedef std::vector<OCObservationId> ObservationIds;
229
230     enum class ObserveAction
231     {
232         ObserveRegister,
233         ObserveUnregister
234     };
235
236     typedef struct
237     {
238         // Action associated with observation request
239         ObserveAction action;
240         // Identifier for observation being registered/unregistered
241         OCObservationId obsId;
242
243         OCConnectivityType connectivityType;
244         std::string address;
245         uint16_t port;
246     } ObservationInfo;
247
248     // const strings for different interfaces
249
250     // Default interface
251     const std::string DEFAULT_INTERFACE = "oic.if.baseline";
252
253     // Used in discovering (GET) links to other resources of a collection.
254     const std::string LINK_INTERFACE = "oic.if.ll";
255
256     // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
257     const std::string BATCH_INTERFACE = "oic.if.b";
258
259     // Used in GET, PUT, POST methods on links to other remote resources of a group.
260     const std::string GROUP_INTERFACE = "oic.mi.grp";
261
262     //Typedef for list direct paired devices
263     typedef std::vector<std::shared_ptr<OCDirectPairing>> PairedDevices;
264
265     typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
266
267     typedef std::function<void(const std::string&, const int)> FindErrorCallback;
268
269     typedef std::function<void(const OCRepresentation&)> FindDeviceCallback;
270
271     typedef std::function<void(const OCRepresentation&)> FindPlatformCallback;
272
273     typedef std::function<OCEntityHandlerResult(
274                             const std::shared_ptr<OCResourceRequest>)> EntityHandler;
275
276     typedef std::function<void(OCStackResult, const unsigned int,
277                                 const std::string&)> SubscribeCallback;
278
279     typedef std::function<void(const HeaderOptions&,
280                                 const OCRepresentation&, const int)> GetCallback;
281
282     typedef std::function<void(const HeaderOptions&,
283                                 const OCRepresentation&, const int)> PostCallback;
284
285     typedef std::function<void(const HeaderOptions&,
286                                 const OCRepresentation&, const int)> PutCallback;
287
288     typedef std::function<void(const HeaderOptions&, const int)> DeleteCallback;
289
290     typedef std::function<void(const HeaderOptions&,
291                                 const OCRepresentation&, const int, const int)> ObserveCallback;
292
293     typedef std::function<void(std::shared_ptr<OCDirectPairing>, OCStackResult)> DirectPairingCallback;
294
295     typedef std::function<void(const PairedDevices&)> GetDirectPairedCallback;
296
297 } // namespace OC
298
299 #endif