There are Two modifications.
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / linux / sampleProvider / SampleProvider.cpp
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 #include <functional>
22
23 #include <pthread.h>
24
25 #include "OCPlatform.h"
26 #include "OCApi.h"
27 #include "OCResourceResponse.h"
28
29 #include <memory>
30
31 using namespace OC;
32 using namespace std;
33
34 int g_Observation = 0;
35
36 pthread_cond_t m_cond = PTHREAD_COND_INITIALIZER;
37 pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER;
38
39 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
40                                     std::shared_ptr< OCResourceResponse > response);
41
42 class TempHumidResource
43 {
44 public:
45
46     int m_temp;
47     int m_humid;
48
49     std::string m_uri;
50     OCResourceHandle m_resourceHandle;
51
52     OCRepresentation m_Rep;
53
54 public:
55     TempHumidResource() :
56             m_temp(0), m_humid(0), m_uri("/a/TempHumSensor")
57     {
58     }
59
60     void createResource()
61     {
62         std::string resourceURI = "/a/NM/TempHumSensor";
63         std::string resourceTypeName = "NotificationManager.Hosting";
64         std::string resourceInterface = DEFAULT_INTERFACE;
65
66         m_uri = resourceURI;
67
68         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
69
70         OCStackResult result = OCPlatform::registerResource(m_resourceHandle , resourceURI ,
71                 resourceTypeName , resourceInterface , &entityHandler , resourceProperty);
72
73         if(OC_STACK_OK != result)
74         {
75             cout << "Resource creation was unsuccessful\n";
76         }
77     }
78
79     OCStackResult createResource1()
80         {
81                 std::string resourceURI = "/a/NM/TempHumSensor1"; // URI of the resource
82                 std::string resourceTypeName = "NotificationManager.Hosting"; // resource type name. In this case, it is light
83                 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
84
85                 // OCResourceProperty is defined ocstack.h
86                 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
87
88                 OCResourceHandle resHandle;
89
90                 // This will internally create and register the resource.
91                 OCStackResult result = OCPlatform::registerResource(
92                                                                         resHandle, resourceURI, resourceTypeName,
93                                                                         resourceInterface, &entityHandler, resourceProperty);
94
95                 if (OC_STACK_OK != result)
96                 {
97                         cout << "Resource creation was unsuccessful\n";
98                 }
99
100                 return result;
101         }
102
103     OCResourceHandle getHandle()
104     {
105         return m_resourceHandle;
106     }
107
108     OCRepresentation post(OCRepresentation& rep)
109     {
110         static int first = 1;
111
112         // for the first time it tries to create a resource
113         if(first)
114         {
115             first = 0;
116
117             if(OC_STACK_OK == createResource1())
118             {
119                 OCRepresentation rep1;
120                 rep1.setValue("createduri", std::string("/a/light1"));
121
122                 return rep1;
123             }
124         }
125
126         // from second time onwards it just puts
127         put(rep);
128         return get();
129     }
130
131     void put(OCRepresentation& rep)
132         {
133                 try {
134                         if (rep.getValue("temperature", m_temp))
135                         {
136                                 cout << "\t\t\t\t" << "temperature: " << m_temp << endl;
137                         }
138                         else
139                         {
140                                 cout << "\t\t\t\t" << "temperature not found in the representation" << endl;
141                         }
142
143                         if (rep.getValue("humidity", m_humid))
144                         {
145                                 cout << "\t\t\t\t" << "humidity: " << m_humid << endl;
146                         }
147                         else
148                         {
149                                 cout << "\t\t\t\t" << "humidity not found in the representation" << endl;
150                         }
151                 }
152                 catch (exception& e)
153                 {
154                         cout << e.what() << endl;
155                 }
156
157         }
158
159     OCRepresentation get()
160         {
161         cout << "resource get\n";
162                 m_Rep.setValue("temperature", m_temp);
163                 m_Rep.setValue("humidity", m_humid);
164
165                 cout << "resource get : done\n";
166
167                 return m_Rep;
168         }
169
170 };
171
172 TempHumidResource myResource;
173
174 void *ChangeLightRepresentation(void *param)
175 {
176         cout << "ChangeLigthRepresentation Enter\n";
177         while(1){
178                 cout << "pthread_cond_wait\n";
179                 pthread_cond_wait(&m_cond, &m_mutex);
180                 cout << "pthread_cond_start\n";
181                 if(g_Observation)
182                 {
183
184                         cout << endl;
185                         cout << "========================================================" << endl;
186                         cout << "HUMTepm updated to : " << myResource.m_temp << endl;
187                         cout << "Notifying observers with resource handle: " << myResource.getHandle() << endl;
188
189                         cout << endl;
190                         cout << "========================================================" << endl;
191                         cout << "Send data : \n";
192                         cout << "Attribute Name: Temp\tvalue: " << myResource.m_temp << endl;
193                         cout << "Attribute Name: Humid\tvalue: " << myResource.m_humid << endl;
194
195                         OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle());
196                         cout << "Notify Success\n";
197
198                         if(OC_STACK_NO_OBSERVERS == result)
199                         {
200                                 cout << "No More observers, stopping notifications" << endl;
201                                 g_Observation = 0;
202                         }
203                 }
204                 cout << "ChangeLigthRepresentation Out\n";
205
206         }
207     return NULL;
208 }
209
210 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
211         std::shared_ptr< OCResourceResponse > response)
212 {
213         cout << "Sample Provider entityHandler\n";
214     if(request)
215     {
216         cout << "flag : request\n";
217         std::string requestType = request->getRequestType();
218         int requestFlag = request->getRequestHandlerFlag();
219
220         if(requestFlag == RequestHandlerFlag::InitFlag)
221         {
222                 cout << "\t\trequestFlag : Init\n";
223         }
224
225         if(requestFlag == RequestHandlerFlag::RequestFlag)
226         {
227                 cout << "\t\trequestFlag : Request\n";
228             if(requestType == "GET")
229             {
230                 cout << "\t\trequestType : GET\n";
231                 try
232                 {
233                                         if(response)
234                                         {
235                                                 OCRepresentation rep = myResource.get();
236                                                 cout << rep.getJSONRepresentation() << endl;
237                                                 response->setErrorCode(200);
238                                                 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
239                                         }
240                                         else
241                                         {
242                                                 cout << "response is null\n";
243                                         }
244                 } catch(exception& e)
245                 {
246                         cout << e.what() << endl;
247                 }
248             }
249             else if(requestType == "PUT")
250             {
251                 cout << "\t\t\trequestType : PUT\n";
252
253                 OCRepresentation rep = request->getResourceRepresentation();
254                 myResource.put(rep);
255
256                 if(response)
257                 {
258                     response->setErrorCode(200);
259                     response->setResourceRepresentation(myResource.get());
260                 }
261             }
262             else if(requestType == "POST")
263             {
264             }
265             else if(requestType == "DELETE")
266             {
267             }
268         }
269         else if(requestFlag & RequestHandlerFlag::ObserverFlag)
270         {
271                 pthread_t threadId;
272
273             cout << request->getResourceUri() << endl;
274             cout << request->getResourceRepresentation().getUri() << endl;
275
276             cout << "========================================================" << endl;
277             cout << "Receive ObserverFlag : Start Observe\n";
278             cout << "========================================================" << endl;
279             g_Observation = 1;
280
281             cout << "\t\trequestFlag : Observer\n";
282             static int startedThread = 0;
283             if(!startedThread)
284             {
285                 cout << "\t\tpthrerad_create\n";
286                 pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL);
287                 startedThread = 1;
288             }
289         }
290     }
291     else
292     {
293         std::cout << "Request invalid" << std::endl;
294     }
295
296     return OC_EH_OK;
297 }
298
299 int main()
300 {
301
302         PlatformConfig cfg {
303                 OC::ServiceType::InProc,
304                 OC::ModeType::Server,
305                 "0.0.0.0",
306                 0,
307                 OC::QualityOfService::LowQos
308             };
309
310         OCPlatform::Configure(cfg);
311
312     int number = 0;
313
314     try
315     {
316
317         myResource.createResource();
318
319         while(true)
320         {
321             bool end = false;
322             cout << endl;
323             cout << "========================================================" << endl;
324             cout << "1. Temp is up" << endl;
325             cout << "2. Temp is down" << endl;
326             cout << "3. This Program will be ended." << endl;
327             cout << "========================================================" << endl;
328             cin >> number;
329
330             switch(number)
331             {
332                 case 1:
333                 {
334                     cout << "Temp is up!" << endl;
335                     myResource.m_temp += 10;
336                     pthread_cond_signal(&m_cond);
337                     cout << "ChangeLightRepresentation Done!" << endl;
338                     break;
339                 }
340                 case 2:
341                 {
342                     cout << "Temp is down!" << endl;
343                     myResource.m_temp -= 10;
344                     pthread_cond_signal(&m_cond);
345                     cout << "ChangeLightRepresentation Done!" << endl;
346                     break;
347                 }
348                 case 3:
349                 {
350                     cout << "Bye!" << endl;
351                     end = true;
352                     break;
353                 }
354                 default:
355                 {
356                     cout << "You type wrong number. Try again!" << endl;
357                     break;
358                 }
359             }
360             if(end == true)
361             {
362                 break;
363             }
364         }
365     }
366     catch(exception& e)
367     {
368         cout << "main exception  : " << e.what() << endl;
369     }
370 }