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 <ThingResourceServer1.h>
27 int g_Observation = 0;
29 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
32 * TempResourceFunctions
35 void TemphumidResource::registerResource()
37 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
39 // This will internally create and register the resource.
40 OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
41 m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
43 if (OC_STACK_OK != result)
45 cout << "Resource creation was unsuccessful\n";
48 result = OCPlatform::bindTypeToResource(m_resourceHandle, m_resourceTypes[1]);
50 if (OC_STACK_OK != result)
52 cout << "Binding TypeName to Resource was unsuccessful\n";
56 OCResourceHandle TemphumidResource::getHandle()
58 return m_resourceHandle;
61 void TemphumidResource::setResourceRepresentation(OCRepresentation &rep)
66 rep.getValue("humidity", tempTemp);
67 rep.getValue("temperature", tempHumid);
72 cout << "\t\t\t" << "Received representation: " << endl;
73 cout << "\t\t\t\t" << "temp: " << m_humid << endl;
74 cout << "\t\t\t\t" << "humid: " << m_temp << endl;
77 OCRepresentation TemphumidResource::getResourceRepresentation()
79 m_resourceRep.setValue("temperature", std::to_string(m_temp));
80 m_resourceRep.setValue("humidity", std::to_string(m_humid));
85 // Create the instance of the TemphumidResource class
86 TemphumidResource g_myResource;
88 void *TestSensorVal(void *param)
92 g_myResource.m_temp = 27;
93 g_myResource.m_humid = 48;
95 // This function continuously monitors for the changes
102 g_myResource.m_temp += 1;
103 g_myResource.m_humid -= 1;
105 cout << "\ntemp updated to : " << g_myResource.m_temp << endl;
106 cout << "\nhumid updated to : " << g_myResource.m_humid << endl;
107 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
110 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
112 if (OC_STACK_NO_OBSERVERS == result)
114 cout << "No More observers, stopping notifications" << endl;
122 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
124 cout << "\tIn Server CPP entity handler:\n";
126 auto response = std::make_shared<OC::OCResourceResponse>();
130 // Get the request type and request flag
131 std::string requestType = request->getRequestType();
132 int requestFlag = request->getRequestHandlerFlag();
134 response->setRequestHandle(request->getRequestHandle());
135 response->setResourceHandle(request->getResourceHandle());
137 if (requestFlag & RequestHandlerFlag::RequestFlag)
139 cout << "\t\trequestFlag : Request\n";
141 // If the request type is GET
142 if (requestType == "GET")
144 cout << "\t\t\trequestType : GET\n";
146 // Check for query params (if any)
147 // Process query params and do required operations ..
149 // Get the representation of this resource at this point and send it as response
150 OCRepresentation rep = g_myResource.getResourceRepresentation();
155 response->setErrorCode(200);
156 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
159 else if (requestType == "PUT")
161 // TODO PUT request operations
163 else if (requestType == "POST")
165 // TODO POST request operations
167 else if (requestType == "DELETE")
169 // TODO DELETE request operations
173 if (requestFlag & RequestHandlerFlag::ObserverFlag)
177 cout << "\t\trequestFlag : Observer\n";
180 static int startedThread = 0;
184 pthread_create(&threadId, NULL, TestSensorVal, (void *)NULL);
191 std::cout << "Request invalid" << std::endl;
194 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
199 // Create PlatformConfig object
200 PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
204 OC::OCPlatform::Configure(cfg);
206 OC::OCPlatform::startPresence(60);
208 g_myResource.registerResource();
211 cout << "Type any key to terminate" << endl;
214 OC::OCPlatform::stopPresence();
216 catch (std::exception e)
218 cout << e.what() << endl;