1 //******************************************************************
3 // Copyright 2015 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26 #include "OCPlatform.h"
28 #include "OCResourceResponse.h"
34 using namespace OC::OCPlatform;
36 int g_Observation = 0;
39 pthread_cond_t m_cond = PTHREAD_COND_INITIALIZER;
40 pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER;
44 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
46 class TempHumidResource
53 OCResourceHandle m_resourceHandle;
54 ObservationIds m_interestedObservers;
55 OCRepresentation m_Rep;
59 m_temp(0), m_humid(0), m_uri("/a/TempHumSensor"), m_resourceHandle(NULL)
65 std::string resourceURI = "/a/TempHumSensor/hosting";
66 std::string resourceTypeName = "resource.hosting";
67 std::string resourceInterface = DEFAULT_INTERFACE;
71 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
73 OCStackResult result = OCPlatform::registerResource(m_resourceHandle , resourceURI ,
74 resourceTypeName , resourceInterface , &entityHandler , resourceProperty);
76 if (OC_STACK_OK != result)
78 cout << "Resource creation was unsuccessful\n";
82 OCStackResult createResource1()
84 std::string resourceURI = "/a/NM/TempHumSensor1"; // URI of the resource
85 std::string resourceTypeName =
86 "notificationmanager.hosting"; // resource type name. In this case, it is light
87 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
89 // OCResourceProperty is defined ocstack.h
90 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
92 OCResourceHandle resHandle;
94 // This will internally create and register the resource.
95 OCStackResult result = OCPlatform::registerResource(
96 resHandle, resourceURI, resourceTypeName,
97 resourceInterface, &entityHandler, resourceProperty);
99 if (OC_STACK_OK != result)
101 cout << "Resource creation was unsuccessful\n";
107 OCResourceHandle getHandle()
109 return m_resourceHandle;
112 OCRepresentation post(OCRepresentation &rep)
114 static int first = 1;
115 // for the first time it tries to create a resource
120 if (OC_STACK_OK == createResource1())
122 OCRepresentation rep1;
123 rep1.setValue("createduri", std::string("/a/light1"));
129 // from second time onwards it just puts
134 void put(OCRepresentation &rep)
138 if (rep.getValue("temperature", m_temp))
140 cout << "\t\t\t\t" << "temperature: " << m_temp << endl;
144 cout << "\t\t\t\t" << "temperature not found in the representation" << endl;
147 if (rep.getValue("humidity", m_humid))
149 cout << "\t\t\t\t" << "humidity: " << m_humid << endl;
153 cout << "\t\t\t\t" << "humidity not found in the representation" << endl;
158 cout << e.what() << endl;
163 OCRepresentation get()
165 m_Rep.setValue("temperature", m_temp);
166 m_Rep.setValue("humidity", m_humid);
169 OCStackResult deleteDeviceResource()
171 OCStackResult result = OCPlatform::unregisterResource(m_resourceHandle);
172 if (OC_STACK_OK != result)
174 throw std::runtime_error(
175 std::string("Device Resource failed to unregister/delete") + std::to_string(result));
181 TempHumidResource myResource;
183 void *ChangeLightRepresentation(void */*param*/)
185 cout << "ChangeLigthRepresentation Enter\n";
188 cout << "pthread_cond_wait\n";
189 pthread_cond_wait(&m_cond, &m_mutex);
190 cout << "pthread_cond_start\n";
195 cout << "========================================================" << endl;
196 cout << "HUMTepm updated to : " << myResource.m_temp << endl;
197 cout << "Notifying observers with resource handle: " << myResource.getHandle() << endl;
200 cout << "========================================================" << endl;
201 cout << "Send data : \n";
202 cout << "Attribute Name: Temp\tvalue: " << myResource.m_temp << endl;
203 cout << "Attribute Name: Humid\tvalue: " << myResource.m_humid << endl;
205 OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle());
206 cout << "Notify Success\n";
208 if (OC_STACK_NO_OBSERVERS == result)
210 cout << "No More observers, stopping notifications" << endl;
214 cout << "ChangeLigthRepresentation Out\n";
220 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
222 cout << "Sample Provider entityHandler\n";
224 OCEntityHandlerResult ehResult = OC_EH_ERROR;
227 cout << "flag : request\n";
228 std::string requestType = request->getRequestType();
229 int requestFlag = request->getRequestHandlerFlag();
231 if (requestFlag == RequestHandlerFlag::RequestFlag)
233 cout << "\t\trequestFlag : Request\n";
234 auto pResponse = std::make_shared<OC::OCResourceResponse>();
235 pResponse->setRequestHandle(request->getRequestHandle());
236 pResponse->setResourceHandle(request->getResourceHandle());
238 if (requestType == "GET")
240 cout << "\t\trequestType : GET\n";
242 pResponse->setErrorCode(200);
243 pResponse->setResponseResult(OC_EH_OK);
244 pResponse->setResourceRepresentation(myResource.get());
245 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
251 else if (requestType == "PUT")
253 cout << "\t\t\trequestType : PUT\n";
255 OCRepresentation rep = request->getResourceRepresentation();
260 pResponse->setErrorCode(200);
261 pResponse->setResourceRepresentation(myResource.get());
263 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
269 cout << "put request Error\n";
273 else if (requestType == "POST")
275 cout << "\t\t\trequestType : POST\n";
278 else if (requestType == "DELETE")
280 cout << "\t\trequestType : DELETE\n";
281 cout << "DeviceResource Delete Request" << std::endl;
283 if (myResource.deleteDeviceResource() == OC_STACK_OK)
285 cout << "\tSuccess DELETE\n";
286 pResponse->setErrorCode(200);
287 pResponse->setResponseResult(OC_EH_RESOURCE_DELETED);
292 pResponse->setResponseResult(OC_EH_ERROR);
293 ehResult = OC_EH_ERROR;
296 OCPlatform::sendResponse(pResponse);
299 if (requestFlag & RequestHandlerFlag::ObserverFlag)
303 ObservationInfo observationInfo = request->getObservationInfo();
304 if (ObserveAction::ObserveRegister == observationInfo.action)
306 myResource.m_interestedObservers.push_back(observationInfo.obsId);
308 else if (ObserveAction::ObserveUnregister == observationInfo.action)
310 myResource.m_interestedObservers.erase(std::remove(
311 myResource.m_interestedObservers.begin(),
312 myResource.m_interestedObservers.end(),
313 observationInfo.obsId),
314 myResource.m_interestedObservers.end());
317 cout << request->getResourceUri() << endl;
318 cout << request->getResourceRepresentation().getUri() << endl;
320 cout << "========================================================" << endl;
321 cout << "Receive ObserverFlag : Start Observe\n";
322 cout << "========================================================" << endl;
325 cout << "\t\trequestFlag : Observer\n";
326 static int startedThread = 0;
329 cout << "\t\tpthrerad_create\n";
330 pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL);
338 std::cout << "Request invalid" << std::endl;
346 unregisterResource(myResource.m_resourceHandle);
351 void handleSigInt(int signum)
353 if (signum == SIGINT)
355 std::cout << " handleSigInt in" << std::endl;
364 OC::ServiceType::InProc,
365 OC::ModeType::Server,
368 OC::QualityOfService::LowQos
371 OCPlatform::Configure(cfg);
379 myResource.createResource();
381 signal(SIGINT, handleSigInt);
386 cout << "========================================================" << endl;
387 cout << "1. Temp is up" << endl;
388 cout << "2. Temp is down" << endl;
389 cout << "3. This Program will be ended." << endl;
390 cout << "========================================================" << endl;
396 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
397 std::cout << "Invalid input type, please try again" << std::endl;
405 cout << "Temp is up!" << endl;
406 myResource.m_temp += 10;
407 pthread_cond_signal(&m_cond);
408 cout << "ChangeLightRepresentation Done!" << endl;
413 cout << "Temp is down!" << endl;
414 myResource.m_temp -= 10;
415 pthread_cond_signal(&m_cond);
416 cout << "ChangeLightRepresentation Done!" << endl;
421 cout << "Bye!" << endl;
429 cout << "Invalid input. Please try again." << endl;
441 cout << "main exception : " << e.what() << endl;