1 //******************************************************************
3 // Copyright 2014 Intel Corporation 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
30 #include "OCPlatform.h"
38 // Forward declaring the entityHandler
39 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
41 /// This class represents a single resource named 'lightResource'. This resource has
42 /// two simple properties named 'state' and 'power'
47 /// Access this property from a TB client
50 OCResourceHandle m_resourceHandle;
54 LightResource(): m_state(false), m_power(0){}
56 /* Note that this does not need to be a member function: for classes you do not have
57 access to, you can accomplish this with a free function: */
59 /// This function internally calls registerResource API.
60 void createResource(OC::OCPlatform& platform)
62 std::string resourceURI = "/a/light"; // URI of the resource
63 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
64 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
66 // OCResourceProperty is defined ocstack.h
67 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
69 // This will internally create and register the resource.
70 OCStackResult result = platform.registerResource(
71 m_resourceHandle, resourceURI, resourceTypeName,
72 resourceInterface, &entityHandler, resourceProperty);
74 if (OC_STACK_OK != result)
76 cout << "Resource creation was unsuccessful\n";
80 OCResourceHandle getHandle()
82 return m_resourceHandle;
85 void setRepresentation(AttributeMap& attributeMap)
87 cout << "\t\t\t" << "Received representation: " << endl;
88 cout << "\t\t\t\t" << "power: " << attributeMap["power"][0] << endl;
89 cout << "\t\t\t\t" << "state: " << attributeMap["state"][0] << endl;
91 m_state = attributeMap["state"][0].compare("true") == 0;
92 m_power = std::stoi(attributeMap["power"][0]);
95 void getRepresentation(AttributeMap& attributeMap)
97 AttributeValues stateVal;
100 stateVal.push_back("true");
104 stateVal.push_back("false");
107 AttributeValues powerVal;
108 powerVal.push_back(to_string(m_power));
110 attributeMap["state"] = stateVal;
111 attributeMap["power"] = powerVal;
114 void addType(const OC::OCPlatform& platform, const std::string& type) const
116 OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
117 if (OC_STACK_OK != result)
119 cout << "Binding TypeName to Resource was unsuccessful\n";
123 void addInterface(const OC::OCPlatform& platform, const std::string& interface) const
125 OCStackResult result = platform.bindInterfaceToResource(m_resourceHandle, interface);
126 if (OC_STACK_OK != result)
128 cout << "Binding TypeName to Resource was unsuccessful\n";
133 // Create the instance of the resource class (in this case instance of class 'LightResource').
134 LightResource myLightResource;
136 // ChangeLightRepresentaion is an observation function,
137 // which notifies any changes to the resource to stack
138 // via notifyObservers
139 void * ChangeLightRepresentation (void *param)
141 // This function continuously monitors for the changes
148 // If under observation if there are any changes to the light resource
149 // we call notifyObservors
151 // For demostration we are changing the power value and notifying.
152 myLightResource.m_power += 10;
154 cout << "\nPower updated to : " << myLightResource.m_power << endl;
155 cout << "Notifying observers with resource handle: " << myLightResource.getHandle() << endl;
157 OCStackResult result = OCPlatform::notifyObservers(myLightResource.getHandle());
159 if(OC_STACK_NO_OBSERVERS == result)
161 cout << "No More observers, stopping notifications" << endl;
171 // This is just a sample implementation of entity handler.
172 // Entity handler can be implemented in several ways by the manufacturer
173 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
175 cout << "\tIn Server CPP entity handler:\n";
179 // Get the request type and request flag
180 std::string requestType = request->getRequestType();
181 RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
183 if(requestFlag == RequestHandlerFlag::InitFlag)
185 cout << "\t\trequestFlag : Init\n";
187 // entity handler to perform resource initialization operations
189 else if(requestFlag == RequestHandlerFlag::RequestFlag)
191 cout << "\t\trequestFlag : Request\n";
193 // If the request type is GET
194 if(requestType == "GET")
196 cout << "\t\t\trequestType : GET\n";
198 // Check for query params (if any)
199 QueryParamsMap queryParamsMap = request->getQueryParameters();
201 cout << "\t\t\tquery params: \n";
202 for(QueryParamsMap::iterator it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
204 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
207 // Process query params and do required operations ..
209 // Get the representation of this resource at this point and send it as response
210 AttributeMap attributeMap;
212 myLightResource.getRepresentation(attributeMap);
217 response->setErrorCode(200);
218 response->setResourceRepresentation(attributeMap);
221 else if(requestType == "PUT")
223 cout << "\t\t\trequestType : PUT\n";
225 // Check for query params (if any)
226 QueryParamsMap queryParamsMap = request->getQueryParameters();
228 // Check queryParamsMap and do required operations ..
230 // Get the representation from the request
231 AttributeMap attributeMap = request->getAttributeRepresentation();
233 myLightResource.setRepresentation(attributeMap);
235 // Do related operations related to PUT request
237 myLightResource.getRepresentation(attributeMap);
241 response->setErrorCode(200);
242 response->setResourceRepresentation(attributeMap);
245 else if(requestType == "POST")
247 // POST request operations
249 else if(requestType == "DELETE")
251 // DELETE request operations
254 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
258 cout << "\t\trequestFlag : Observer\n";
261 static int startedThread = 0;
263 // Observation happens on a different thread in ChangeLightRepresentation function.
264 // If we have not created the thread already, we will create one here.
267 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
275 std::cout << "Request invalid" << std::endl;
281 // Create PlatformConfig object
284 cfg.ipAddress = "134.134.161.33";
286 cfg.mode = ModeType::Server;
287 cfg.serviceType = ServiceType::InProc;
289 // Create a OCPlatform instance.
290 // Note: Platform creation is synchronous call.
293 OCPlatform platform(cfg);
295 // Invoke createResource function of class light.
297 myLightResource.createResource(platform);
298 myLightResource.addType(platform, std::string("core.brightlight"));
299 myLightResource.addInterface(platform, std::string("oc.mi.ll"));
312 // No explicit call to stop the platform.
313 // When OCPlatform destructor is invoked, internally we do platform cleanup