Merge "Fixed bug in array navigation in CA Adapter DTLS module" into connectivity...
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SDK / include / SSMClient.h
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics 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 /**
22  * @file    SSMClient.h
23  * @brief   This file contains the declaration of classes and its members related to SSMClient.
24  */
25
26 #ifndef RESOURCECLIENT_H_
27 #define RESOURCECLIENT_H_
28
29 #include <string>
30
31 #include "OCResource.h"
32 #include "OCPlatform.h"
33 #include "ISSMClientListener.h"
34
35 using namespace OC;
36
37 #if defined(WIN32)
38 class CSemaphore
39 {
40     private:
41         HANDLE hSemaphore;
42         int m_MaxTimeOut;
43
44     public:
45         /**
46          * This is CSemaphore constructor.
47          */
48         CSemaphore()
49         {
50             m_MaxTimeOut = 0;
51         }
52         /**
53          *This is CSemaphore destructor.
54          */
55         ~CSemaphore()
56         {
57         }
58
59         /**
60          * This is a function to creat a semaphore
61          * This is syncronus call.
62          * @param maximumRequestTimeOut
63          *          [IN] Request Time out.
64          *
65          * @return  None
66          *
67          */
68         void create(int maximumRequestTimeOut)
69         {
70             hSemaphore = CreateSemaphore(NULL, 1, 1, NULL);
71             m_MaxTimeOut = maximumRequestTimeOut;
72         }
73
74         /**
75          * This is a function to wait for semaphore
76          *
77          * @return  None
78          *
79          */
80         void wait()
81         {
82             WaitForSingleObject(hSemaphore, m_MaxTimeOut);
83         }
84
85         /**
86          * This is a function to release  semaphore
87          *
88          * @return  None
89          *
90          */
91         void release()
92         {
93             ReleaseSemaphore(hSemaphore, 1, NULL);
94         }
95
96 };
97
98 #elif defined(LINUX)
99 #include <semaphore.h>
100
101 #define SEM_SUCCESS     0
102
103 class CSemaphore
104 {
105     private:
106         sem_t hSemaphore;
107         timespec m_MaxTimeOut;
108
109     public:
110         /**
111          * This is CSemaphore constructor.
112          */
113         CSemaphore()
114         {
115             m_MaxTimeOut.tv_sec = 0;
116             m_MaxTimeOut.tv_nsec = 0;
117         }
118         /**
119          *This is CSemaphore destructor.
120          */
121         ~CSemaphore()
122         {
123         }
124         /**
125          * This is a function to creat a semaphore
126          * This is syncronus call.
127          * @param maximumRequestTimeOut
128          *          [IN] Request Time out.
129          *
130          * @return  None
131          *
132          */
133         void create(int maximumRequestTimeOut)
134         {
135             if ( sem_init(&hSemaphore, 1, 0) < SEM_SUCCESS )
136             {
137                 perror("Error : sem_init.");
138                 exit(0);
139             }
140             m_MaxTimeOut.tv_sec = maximumRequestTimeOut;
141         }
142
143         void wait()
144         {
145             sem_wait( &hSemaphore );
146         }
147
148         /**
149          * This is a function to release  semaphore
150          *
151          * @return  None
152          *
153          */
154         void release()
155         {
156             sem_post(&hSemaphore);
157         }
158
159 };
160 #endif
161
162 /**
163  *  @brief  SSMClient is a wrapper class to provide SoftSensorManager functionality to Application.
164  *          Basically, SoftSensorManager is developed in Resource model (i.e. messaging with basic functions of put, get, and post).
165  *          SSMClient abstracts the resource based operations from client applications and provides c++ style functions.
166  */
167 class SSMClient
168 {
169     private:
170         SSMReturn m_retResponse;
171         CSemaphore m_sem;
172         /**
173          * @brief SoftSensorManager Resource.
174          */
175         std::shared_ptr< OCResource > m_SSMResource;
176         /**
177          * @brief attribute map .
178          */
179         AttributeMap m_responseAttributeMap;
180         /**
181          * @brief query engine.
182          */
183         std::string m_queryEngineId;
184         /**
185          * @brief app listener
186          */
187         ISSMClientListener *m_appListener;
188
189         /**
190          * @brief internal find resource function
191          */
192         void _findResource(void);
193
194         /**
195          * @brief internal Query engine function
196          */
197         void _createQueryEngine(void);
198
199         /**
200          * @brief internal release query function
201          */
202         void _releaseQueryEngine(std::string queryEngineId);
203
204     public:
205         /**
206          * Constructor for SSMClient.
207          */
208         SSMClient();
209         ~SSMClient();
210
211         /**
212          * This API sends query strings of applications to SoftSensorManager on Iotivity Base messaging.
213          *
214          * @param [in] queryString - A conditions query statement where the caller application specifies sensors for required data and conditions when the caller wants to get the data.
215          * @param [in] listener - The address of listener class.  When an application which inherits the ISSMClientListener calls this operation, it sends its address for the listener so that
216          *                          SSMClient can callback message to the application.
217          * @param [out] cqid - A query id generated from SoftSensorManager service for the queryString request.
218          * @return  SSMReturn
219
220          */
221         SSMReturn registerQuery(std::string queryString, ISSMClientListener *listener,
222                                 std::string &cqid);
223         /**
224          * This API is to cancel the registered sends query strings of applications to SoftSensorManager on Iotivity Base messaging.
225          *
226          * @param [in] listener - The address of listener class.  When an application which inherits the ISSMClientListener calls this operation, it sends its address for the listener so that
227          *                          SSMClient can callback message to the application.
228          * @param [in] cqid - A query id generated from SoftSensorManager service for the queryString request.
229          * @return  SSMReturn
230          */
231         SSMReturn unregisterQuery(std::string cqid);
232
233         // friend option. for callback from SSMResource
234         void onFoundResource(std::shared_ptr< OCResource > resource);
235         void onCreateQueryEngine(const HeaderOptions &headerOptions, const OCRepresentation &rep,
236                                  const int eCode);
237         void onReleaseQueryEngine(const HeaderOptions &headerOptions, const OCRepresentation &rep,
238                                   const int eCode);
239         void onRegisterQuery(const HeaderOptions &headerOptions, const OCRepresentation &rep,
240                              const int eCode);
241         void onUnregisterQuery(const HeaderOptions &headerOptions, const OCRepresentation &rep,
242                                const int eCode);
243         void onObserve(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int &eCode,
244                        const int &sequenceNumber);
245 };
246
247 #endif /* RESOURCECLIENT_H_ */