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