1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "OCPlatform.h"
27 #include "OCResourceResponse.h"
34 int g_Observation = 0;
36 pthread_cond_t m_cond = PTHREAD_COND_INITIALIZER;
37 pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER;
39 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
40 std::shared_ptr< OCResourceResponse > response);
42 class TempHumidResource
50 OCResourceHandle m_resourceHandle;
52 OCRepresentation m_Rep;
56 m_temp(0), m_humid(0), m_uri("/a/TempHumSensor")
62 std::string resourceURI = "/a/NM/TempHumSensor";
63 std::string resourceTypeName = "NotificationManager.Hosting";
64 std::string resourceInterface = DEFAULT_INTERFACE;
68 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
70 OCStackResult result = OCPlatform::registerResource(m_resourceHandle , resourceURI ,
71 resourceTypeName , resourceInterface , &entityHandler , resourceProperty);
73 if(OC_STACK_OK != result)
75 cout << "Resource creation was unsuccessful\n";
79 OCStackResult createResource1()
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.
85 // OCResourceProperty is defined ocstack.h
86 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
88 OCResourceHandle resHandle;
90 // This will internally create and register the resource.
91 OCStackResult result = OCPlatform::registerResource(
92 resHandle, resourceURI, resourceTypeName,
93 resourceInterface, &entityHandler, resourceProperty);
95 if (OC_STACK_OK != result)
97 cout << "Resource creation was unsuccessful\n";
103 OCResourceHandle getHandle()
105 return m_resourceHandle;
108 OCRepresentation post(OCRepresentation& rep)
110 static int first = 1;
112 // for the first time it tries to create a resource
117 if(OC_STACK_OK == createResource1())
119 OCRepresentation rep1;
120 rep1.setValue("createduri", std::string("/a/light1"));
126 // from second time onwards it just puts
131 void put(OCRepresentation& rep)
134 if (rep.getValue("temperature", m_temp))
136 cout << "\t\t\t\t" << "temperature: " << m_temp << endl;
140 cout << "\t\t\t\t" << "temperature not found in the representation" << endl;
143 if (rep.getValue("humidity", m_humid))
145 cout << "\t\t\t\t" << "humidity: " << m_humid << endl;
149 cout << "\t\t\t\t" << "humidity not found in the representation" << endl;
154 cout << e.what() << endl;
159 OCRepresentation get()
161 cout << "resource get\n";
162 m_Rep.setValue("temperature", m_temp);
163 m_Rep.setValue("humidity", m_humid);
165 cout << "resource get : done\n";
172 TempHumidResource myResource;
174 void *ChangeLightRepresentation(void *param)
176 cout << "ChangeLigthRepresentation Enter\n";
178 cout << "pthread_cond_wait\n";
179 pthread_cond_wait(&m_cond, &m_mutex);
180 cout << "pthread_cond_start\n";
185 cout << "========================================================" << endl;
186 cout << "HUMTepm updated to : " << myResource.m_temp << endl;
187 cout << "Notifying observers with resource handle: " << myResource.getHandle() << 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;
195 OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle());
196 cout << "Notify Success\n";
198 if(OC_STACK_NO_OBSERVERS == result)
200 cout << "No More observers, stopping notifications" << endl;
204 cout << "ChangeLigthRepresentation Out\n";
210 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
211 std::shared_ptr< OCResourceResponse > response)
213 cout << "Sample Provider entityHandler\n";
216 cout << "flag : request\n";
217 std::string requestType = request->getRequestType();
218 int requestFlag = request->getRequestHandlerFlag();
220 if(requestFlag == RequestHandlerFlag::InitFlag)
222 cout << "\t\trequestFlag : Init\n";
225 if(requestFlag == RequestHandlerFlag::RequestFlag)
227 cout << "\t\trequestFlag : Request\n";
228 if(requestType == "GET")
230 cout << "\t\trequestType : GET\n";
235 OCRepresentation rep = myResource.get();
236 cout << rep.getJSONRepresentation() << endl;
237 response->setErrorCode(200);
238 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
242 cout << "response is null\n";
244 } catch(exception& e)
246 cout << e.what() << endl;
249 else if(requestType == "PUT")
251 cout << "\t\t\trequestType : PUT\n";
253 OCRepresentation rep = request->getResourceRepresentation();
258 response->setErrorCode(200);
259 response->setResourceRepresentation(myResource.get());
262 else if(requestType == "POST")
265 else if(requestType == "DELETE")
269 else if(requestFlag & RequestHandlerFlag::ObserverFlag)
273 cout << request->getResourceUri() << endl;
274 cout << request->getResourceRepresentation().getUri() << endl;
276 cout << "========================================================" << endl;
277 cout << "Receive ObserverFlag : Start Observe\n";
278 cout << "========================================================" << endl;
281 cout << "\t\trequestFlag : Observer\n";
282 static int startedThread = 0;
285 cout << "\t\tpthrerad_create\n";
286 pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL);
293 std::cout << "Request invalid" << std::endl;
303 OC::ServiceType::InProc,
304 OC::ModeType::Server,
307 OC::QualityOfService::LowQos
310 OCPlatform::Configure(cfg);
317 myResource.createResource();
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;
334 cout << "Temp is up!" << endl;
335 myResource.m_temp += 10;
336 pthread_cond_signal(&m_cond);
337 cout << "ChangeLightRepresentation Done!" << endl;
342 cout << "Temp is down!" << endl;
343 myResource.m_temp -= 10;
344 pthread_cond_signal(&m_cond);
345 cout << "ChangeLightRepresentation Done!" << endl;
350 cout << "Bye!" << endl;
356 cout << "You type wrong number. Try again!" << endl;
368 cout << "main exception : " << e.what() << endl;