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 <ThingResourceServer.h>
26 int g_Observation = 0;
28 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
31 * TempResourceFunctions
34 void TemphumidResource::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";
47 result = OCPlatform::bindTypeToResource(m_resourceHandle, m_resourceTypes[1]);
49 if (OC_STACK_OK != result)
51 cout << "Binding TypeName to Resource was unsuccessful\n";
55 OCResourceHandle TemphumidResource::getHandle()
57 return m_resourceHandle;
60 void TemphumidResource::setResourceRepresentation(OCRepresentation &rep)
65 rep.getValue("humidity", tempTemp);
66 rep.getValue("temperature", tempHumid);
71 cout << "\t\t\t" << "Received representation: " << endl;
72 cout << "\t\t\t\t" << "temp: " << m_humid << endl;
73 cout << "\t\t\t\t" << "humid: " << m_temp << endl;
76 OCRepresentation TemphumidResource::getResourceRepresentation()
79 // This representation is temporaily for soft-sensor-management - name, type, vale
80 m_resourceRep.setValue("temperature", std::to_string(m_temp));
81 m_resourceRep.setValue("humidity", std::to_string(m_humid));
86 // Create the instance of the TemphumidResource class
87 TemphumidResource g_myResource;
89 void *TestSensorVal(void *param)
93 g_myResource.m_temp = 27;
94 g_myResource.m_humid = 48;
96 // This function continuously monitors for the changes
103 g_myResource.m_temp += 1;
104 g_myResource.m_humid -= 1;
106 cout << "\ntemp updated to : " << g_myResource.m_temp << endl;
107 cout << "\nhumid updated to : " << g_myResource.m_humid << endl;
108 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
111 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
113 if (OC_STACK_NO_OBSERVERS == result)
115 cout << "No More observers, stopping notifications" << endl;
123 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
125 cout << "\tIn Server CPP entity handler:\n";
127 auto response = std::make_shared<OC::OCResourceResponse>();
131 // Get the request type and request flag
132 std::string requestType = request->getRequestType();
133 int requestFlag = request->getRequestHandlerFlag();
135 response->setRequestHandle(request->getRequestHandle());
136 response->setResourceHandle(request->getResourceHandle());
138 if (requestFlag & RequestHandlerFlag::RequestFlag)
140 cout << "\t\trequestFlag : Request\n";
142 // If the request type is GET
143 if (requestType == "GET")
145 cout << "\t\t\trequestType : GET\n";
147 // Check for query params (if any)
148 // Process query params and do required operations ..
150 // Get the representation of this resource at this point and send it as response
151 OCRepresentation rep = g_myResource.getResourceRepresentation();
156 response->setErrorCode(200);
157 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
160 else if (requestType == "PUT")
162 // TODO PUT request operations
164 else if (requestType == "POST")
166 // TODO POST request operations
168 else if (requestType == "DELETE")
170 // TODO DELETE request operations
174 if (requestFlag & RequestHandlerFlag::ObserverFlag)
178 cout << "\t\trequestFlag : Observer\n";
181 static int startedThread = 0;
185 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
192 std::cout << "Request invalid" << std::endl;
195 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
200 // Create PlatformConfig object
201 PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
205 OC::OCPlatform::Configure(cfg);
207 OC::OCPlatform::startPresence(60);
209 g_myResource.registerResource();
212 cout << "Type any key to terminate" << endl;
215 OC::OCPlatform::stopPresence();
217 catch (std::exception e)
219 cout << e.what() << endl;