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