1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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>
23 int g_Observation = 0;
25 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
28 * TempResourceFunctions
31 void TemphumidResource::registerResource()
33 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
35 // This will internally create and register the resource.
36 OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
37 m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
39 if (OC_STACK_OK != result)
41 cout << "Resource creation was unsuccessful\n";
45 OCResourceHandle TemphumidResource::getHandle()
47 return m_resourceHandle;
50 void TemphumidResource::setResourceRepresentation(OCRepresentation &rep)
55 rep.getValue("2", tempTemp);
56 rep.getValue("5", tempHumid);
61 cout << "\t\t\t" << "Received representation: " << endl;
62 cout << "\t\t\t\t" << "temp: " << m_humid << endl;
63 cout << "\t\t\t\t" << "humid: " << m_temp << endl;
66 OCRepresentation TemphumidResource::getResourceRepresentation()
69 // This representation is temporaily for soft-sensor-management - name, type, vale
70 m_resourceRep.setValue("0", std::string("temperature"));
71 m_resourceRep.setValue("1", std::string("int"));
72 m_resourceRep.setValue("2", std::to_string(m_temp));
73 m_resourceRep.setValue("3", std::string("humidity"));
74 m_resourceRep.setValue("4", std::string("int"));
75 m_resourceRep.setValue("5", std::to_string(m_humid));
80 // Create the instance of the TemphumidResource class
81 TemphumidResource g_myResource;
83 void *TestSensorVal(void *param)
85 g_myResource.m_temp = 27;
86 g_myResource.m_humid = 48;
88 // This function continuously monitors for the changes
95 g_myResource.m_temp += 1;
96 g_myResource.m_humid -= 1;
98 cout << "\ntemp updated to : " << g_myResource.m_temp << endl;
99 cout << "\nhumid updated to : " << g_myResource.m_humid << endl;
100 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
103 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
105 if (OC_STACK_NO_OBSERVERS == result)
107 cout << "No More observers, stopping notifications" << endl;
115 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
117 cout << "\tIn Server CPP entity handler:\n";
119 auto response = std::make_shared<OC::OCResourceResponse>();
123 // Get the request type and request flag
124 std::string requestType = request->getRequestType();
125 int requestFlag = request->getRequestHandlerFlag();
127 response->setRequestHandle(request->getRequestHandle());
128 response->setResourceHandle(request->getResourceHandle());
130 if (requestFlag & RequestHandlerFlag::InitFlag)
132 cout << "\t\trequestFlag : Init\n";
134 // entity handler to perform resource initialization operations
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 // PUT request operations
163 else if (requestType == "POST")
165 // POST request operations
167 else if (requestType == "DELETE")
169 // 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;