added error callback for findResource() in stack
[platform/upstream/iotivity.git] / resource / include / InProcClientWrapper.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_IN_PROC_CLIENT_WRAPPER_H_
22 #define OC_IN_PROC_CLIENT_WRAPPER_H_
23
24 #include <thread>
25 #include <mutex>
26 #include <sstream>
27 #include <iostream>
28
29 #include <OCApi.h>
30 #include <IClientWrapper.h>
31 #include <InitializeException.h>
32 #include <ResourceInitException.h>
33
34 namespace OC
35 {
36     namespace ClientCallbackContext
37     {
38         struct GetContext
39         {
40             GetCallback callback;
41             GetContext(GetCallback cb) : callback(cb){}
42         };
43
44         struct SetContext
45         {
46             PutCallback callback;
47             SetContext(PutCallback cb) : callback(cb){}
48         };
49
50         struct ListenContext
51         {
52             FindCallback callback;
53             std::weak_ptr<IClientWrapper> clientWrapper;
54
55             ListenContext(FindCallback cb, std::weak_ptr<IClientWrapper> cw)
56                 : callback(cb), clientWrapper(cw){}
57         };
58
59         struct ListenErrorContext
60         {
61             FindCallback callback;
62             FindErrorCallback errorCallback;
63             std::weak_ptr<IClientWrapper> clientWrapper;
64
65             ListenErrorContext(FindCallback cb1, FindErrorCallback cb2,
66                                std::weak_ptr<IClientWrapper> cw)
67                 : callback(cb1), errorCallback(cb2), clientWrapper(cw){}
68         };
69
70         struct DeviceListenContext
71         {
72             FindDeviceCallback callback;
73             IClientWrapper::Ptr clientWrapper;
74             DeviceListenContext(FindDeviceCallback cb, IClientWrapper::Ptr cw)
75                     : callback(cb), clientWrapper(cw){}
76         };
77
78         struct SubscribePresenceContext
79         {
80             SubscribeCallback callback;
81             SubscribePresenceContext(SubscribeCallback cb) : callback(cb){}
82         };
83
84         struct DeleteContext
85         {
86             DeleteCallback callback;
87             DeleteContext(DeleteCallback cb) : callback(cb){}
88         };
89
90         struct ObserveContext
91         {
92             ObserveCallback callback;
93             ObserveContext(ObserveCallback cb) : callback(cb){}
94         };
95     }
96
97     class InProcClientWrapper : public IClientWrapper
98     {
99
100     public:
101
102         InProcClientWrapper(std::weak_ptr<std::recursive_mutex> csdkLock,
103                             PlatformConfig cfg);
104         virtual ~InProcClientWrapper();
105
106         virtual OCStackResult ListenForResource(const std::string& serviceUrl,
107             const std::string& resourceType, OCConnectivityType transportFlags,
108             FindCallback& callback, QualityOfService QoS);
109
110         virtual OCStackResult ListenErrorForResource(const std::string& serviceUrl,
111             const std::string& resourceType, OCConnectivityType transportFlags,
112             FindCallback& callback, FindErrorCallback& errorCallback, QualityOfService QoS);
113
114         virtual OCStackResult ListenForDevice(const std::string& serviceUrl,
115             const std::string& deviceURI, OCConnectivityType transportFlags,
116             FindDeviceCallback& callback, QualityOfService QoS);
117
118         virtual OCStackResult GetResourceRepresentation(
119             const OCDevAddr& devAddr,
120             const std::string& uri,
121             const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
122             GetCallback& callback, QualityOfService QoS);
123
124         virtual OCStackResult PutResourceRepresentation(
125             const OCDevAddr& devAddr,
126             const std::string& uri,
127             const OCRepresentation& attributes, const QueryParamsMap& queryParams,
128             const HeaderOptions& headerOptions, PutCallback& callback, QualityOfService QoS);
129
130         virtual OCStackResult PostResourceRepresentation(
131             const OCDevAddr& devAddr,
132             const std::string& uri,
133             const OCRepresentation& attributes, const QueryParamsMap& queryParams,
134             const HeaderOptions& headerOptions, PostCallback& callback, QualityOfService QoS);
135
136         virtual OCStackResult DeleteResource(
137             const OCDevAddr& devAddr,
138             const std::string& uri,
139             const HeaderOptions& headerOptions,
140             DeleteCallback& callback, QualityOfService QoS);
141
142         virtual OCStackResult ObserveResource(
143             ObserveType observeType, OCDoHandle* handle,
144             const OCDevAddr& devAddr,
145             const std::string& uri,
146             const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
147             ObserveCallback& callback, QualityOfService QoS);
148
149         virtual OCStackResult CancelObserveResource(
150             OCDoHandle handle,
151             const std::string& host,
152             const std::string& uri,
153             const HeaderOptions& headerOptions, QualityOfService QoS);
154
155         virtual OCStackResult SubscribePresence(
156             OCDoHandle *handle,
157             const std::string& host,
158             const std::string& resourceType,
159             OCConnectivityType transportFlags,
160             SubscribeCallback& presenceHandler);
161
162         virtual OCStackResult UnsubscribePresence(OCDoHandle handle);
163         OCStackResult GetDefaultQos(QualityOfService& QoS);
164     private:
165         void listeningFunc();
166         std::string assembleSetResourceUri(std::string uri, const QueryParamsMap& queryParams);
167         OCPayload* assembleSetResourcePayload(const OCRepresentation& attributes);
168         OCHeaderOption* assembleHeaderOptions(OCHeaderOption options[],
169            const HeaderOptions& headerOptions);
170         std::thread m_listeningThread;
171         bool m_threadRun;
172         std::weak_ptr<std::recursive_mutex> m_csdkLock;
173
174     private:
175         PlatformConfig  m_cfg;
176     };
177 }
178
179 #endif
180