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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <HeightSensorApp.h>
26 int g_Observation = 0;
28 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
31 * TempResourceFunctions
34 void HeightResource::registerResource()
36 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
38 // This will internally create and register the resource.
39 OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
40 m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
42 if (OC_STACK_OK != result)
44 cout << "Resource creation was unsuccessful\n";
48 OCResourceHandle HeightResource::getHandle()
50 return m_resourceHandle;
53 void HeightResource::setResourceRepresentation(OCRepresentation &rep)
55 double tempHeight = 0.0;
57 rep.getValue("height", tempHeight);
59 m_height = tempHeight;
61 cout << "\t\t\t" << "Received representation: " << endl;
63 cout << "\t\t\t\t" << "height" << m_height << endl;
67 OCRepresentation HeightResource::getResourceRepresentation()
69 m_resourceRep.setValue("height", std::to_string(m_height));
74 // Create the instance of the TemphumidResource class
75 HeightResource g_myResource;
77 void *TestSensorVal(void *param)
82 int nSleep_time = INTERVAL_FOR_CHECK;
85 std::cout << "[HeightSensorAPP] ::" << __func__ << " is called."
90 // This function continuously monitors for the changes
99 nSleep_time = INTERVAL_FOR_MEASUREMENT;
101 g_myResource.m_height = nHeight;
105 nSleep_time = INTERVAL_FOR_CHECK;
108 bFlag = bFlag ^ true;
110 cout << "\n height updated to : " << g_myResource.m_height << endl;
111 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
114 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
117 if (OC_STACK_NO_OBSERVERS == result)
119 cout << "No More observers, stopping notifications" << endl;
125 nSleep_time = INTERVAL_FOR_CHECK ;
131 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
133 cout << "\tIn Server CPP entity handler:\n";
135 auto response = std::make_shared<OC::OCResourceResponse>();
139 // Get the request type and request flag
140 std::string requestType = request->getRequestType();
141 int requestFlag = request->getRequestHandlerFlag();
143 response->setRequestHandle(request->getRequestHandle());
144 response->setResourceHandle(request->getResourceHandle());
146 if (requestFlag & RequestHandlerFlag::RequestFlag)
148 cout << "\t\trequestFlag : Request\n";
150 // If the request type is GET
151 if (requestType == "GET")
153 cout << "\t\t\trequestType : GET\n";
155 // Check for query params (if any)
156 // Process query params and do required operations ..
158 // Get the representation of this resource at this point and send it as response
159 OCRepresentation rep = g_myResource.getResourceRepresentation();
164 response->setErrorCode(200);
165 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
168 else if (requestType == "PUT")
170 // TODO: PUT request operations
172 else if (requestType == "POST")
174 // TODO: POST request operations
176 else if (requestType == "DELETE")
178 // TODO: DELETE request operations
182 if (requestFlag & RequestHandlerFlag::ObserverFlag)
184 pthread_t threadId = {};
186 cout << "\t\trequestFlag : Observer\n";
189 static int startedThread = 0;
193 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
200 std::cout << "Request invalid" << std::endl;
203 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
208 // Create PlatformConfig object
209 PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
213 OC::OCPlatform::Configure(cfg);
215 OC::OCPlatform::startPresence(60);
217 g_myResource.registerResource();
220 cout << "Type any key to terminate" << endl;
223 OC::OCPlatform::stopPresence();
225 catch (std::exception & e)
227 cout << e.what() << endl;