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