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