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"
33 int g_Observation = 0;
35 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
36 std::shared_ptr< OCResourceResponse > response);
38 class TempHumidResource
46 OCResourceHandle m_resourceHandle;
48 OCRepresentation m_Rep;
52 m_temp(0), m_humid(0), m_uri("/a/TempHumSensor")
58 std::string resourceURI = "/a/NM/TempHumSensor";
59 std::string resourceTypeName = "NotificationManager.Hosting";
60 std::string resourceInterface = DEFAULT_INTERFACE;
64 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
66 OCStackResult result = OCPlatform::registerResource(m_resourceHandle , resourceURI ,
67 resourceTypeName , resourceInterface , &entityHandler , resourceProperty);
69 if(OC_STACK_OK != result)
71 cout << "Resource creation was unsuccessful\n";
75 OCStackResult createResource1()
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.
81 // OCResourceProperty is defined ocstack.h
82 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
84 OCResourceHandle resHandle;
86 // This will internally create and register the resource.
87 OCStackResult result = OCPlatform::registerResource(
88 resHandle, resourceURI, resourceTypeName,
89 resourceInterface, &entityHandler, resourceProperty);
91 if (OC_STACK_OK != result)
93 cout << "Resource creation was unsuccessful\n";
99 OCResourceHandle getHandle()
101 return m_resourceHandle;
104 OCRepresentation post(OCRepresentation& rep)
106 static int first = 1;
108 // for the first time it tries to create a resource
113 if(OC_STACK_OK == createResource1())
115 OCRepresentation rep1;
116 rep1.setValue("createduri", std::string("/a/light1"));
122 // from second time onwards it just puts
127 void put(OCRepresentation& rep)
130 if (rep.getValue("temperature", m_temp))
132 cout << "\t\t\t\t" << "temperature: " << m_temp << endl;
136 cout << "\t\t\t\t" << "temperature not found in the representation" << endl;
139 if (rep.getValue("humidity", m_humid))
141 cout << "\t\t\t\t" << "humidity: " << m_humid << endl;
145 cout << "\t\t\t\t" << "m_power not found in the representation" << endl;
150 cout << e.what() << endl;
155 OCRepresentation get()
157 m_Rep.setValue("temperature", m_temp);
158 m_Rep.setValue("humidity", m_humid);
165 TempHumidResource myResource;
167 void *ChangeLightRepresentation(void *param)
174 cout << "========================================================" << endl;
175 cout << "HUMTepm updated to : " << myResource.m_temp << endl;
176 cout << "Notifying observers with resource handle: " << myResource.getHandle() << 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;
184 OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle());
186 if(OC_STACK_NO_OBSERVERS == result)
188 cout << "No More observers, stopping notifications" << endl;
196 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
197 std::shared_ptr< OCResourceResponse > response)
202 std::string requestType = request->getRequestType();
203 int requestFlag = request->getRequestHandlerFlag();
205 if(requestFlag == RequestHandlerFlag::InitFlag)
208 if(requestFlag == RequestHandlerFlag::RequestFlag)
210 if(requestType == "GET")
214 response->setErrorCode(200);
215 response->setResourceRepresentation(myResource.get());
218 else if(requestType == "PUT")
220 cout << "\t\t\trequestType : PUT\n";
222 OCRepresentation rep = request->getResourceRepresentation();
227 response->setErrorCode(200);
228 response->setResourceRepresentation(myResource.get());
231 else if(requestType == "POST")
234 else if(requestType == "DELETE")
238 else if(requestFlag & RequestHandlerFlag::ObserverFlag)
242 cout << "========================================================" << endl;
243 cout << "Receive ObserverFlag : Start Observe\n";
244 cout << "========================================================" << endl;
247 static int startedThread = 0;
250 pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL);
257 std::cout << "Request invalid" << std::endl;
267 OC::ServiceType::InProc,
268 OC::ModeType::Server,
271 OC::QualityOfService::LowQos
274 OCPlatform::Configure(cfg);
281 myResource.createResource();
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;
298 cout << "Temp is up!" << endl;
299 myResource.m_temp += 10;
300 ChangeLightRepresentation((void *) NULL);
306 cout << "Temp is down!" << endl;
307 myResource.m_temp -= 10;
308 ChangeLightRepresentation((void *) NULL);
314 cout << "Bye!" << endl;
320 cout << "You type wrong number. Try again!" << endl;