1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 using varous json types in the representation.
29 #include <condition_variable>
31 #include "OCPlatform.h"
37 // Forward declaring the entityHandler
38 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
40 /// This class represents a single resource named 'lightResource'. This resource has
45 /// Access this property from a TB client
48 std::string m_garageUri;
49 OCResourceHandle m_resourceHandle;
50 OCRepresentation m_garageRep;
51 ObservationIds m_interestedObservers;
53 // array of lights representation with in GarageResource
54 OCRepresentation m_lightRep;
55 std::vector<OCRepresentation> m_reps;
59 GarageResource(): m_name("John's Garage"), m_state(false), m_garageUri("/a/garage") {
60 // Initialize representation
61 m_garageRep.setUri(m_garageUri);
63 m_garageRep.setValue("state", m_state);
64 m_garageRep.setValue("name", m_name);
66 // For demonstration purpose we are setting x to nullptr here.
67 // In reality it may happen else where.
70 // Check for nullptr and set null for that attribute
73 m_garageRep.setNULL("nullAttribute");
76 std::vector<bool> lightStates;
77 std::vector<int> lightPowers;
79 for(int i = 0; i <= 9; i++)
81 lightStates.push_back(i % 2 == 0);
82 lightPowers.push_back(i);
85 m_lightRep.setValue("states", lightStates);
86 m_lightRep.setValue("powers", lightPowers);
88 // Storing another representation within a representation
89 m_garageRep.setValue("light", m_lightRep);
91 OCRepresentation rep1;
93 rep1.setValue("key1", value1);
94 OCRepresentation rep2;
96 rep2.setValue("key2", value2);
98 m_reps.push_back(rep1);
99 m_reps.push_back(rep2);
101 // storing array of representations
102 m_garageRep.setValue("reps", m_reps);
105 // setting json string
106 std::string json = "{\"num\":10,\"rno\":23.5,\"aoa\":[[1,2],[3]],\"str\":\"john\",\
107 \"object\":{\"bl1\":false,\"ar\":[2,3]}, \"objects\":[{\"bl2\":true,\"nl\":null},{\"ar1\":[1,2]}]}";
108 m_garageRep.setValue("json", json);
111 /* Note that this does not need to be a member function: for classes you do not have
112 access to, you can accomplish this with a free function: */
114 /// This function internally calls registerResource API.
115 void createResource()
117 std::string resourceURI = m_garageUri; // URI of the resource
118 std::string resourceTypeName = "core.garage"; // resource type name.
119 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
121 // OCResourceProperty is defined ocstack.h
122 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
124 // This will internally create and register the resource.
125 OCStackResult result = OCPlatform::registerResource(
126 m_resourceHandle, resourceURI, resourceTypeName,
127 resourceInterface, &entityHandler, resourceProperty);
129 if (OC_STACK_OK != result)
131 cout << "Resource creation was unsuccessful\n";
135 OCResourceHandle getHandle()
137 return m_resourceHandle;
140 // Puts representation.
141 // Gets values from the representation and
142 // updates the internal state
143 void put(OCRepresentation& rep)
146 if (rep.getValue("state", m_state))
148 cout << "\t\t\t\t" << "state: " << m_state << endl;
152 cout << "\t\t\t\t" << "state not found in the representation" << endl;
157 cout << e.what() << endl;
162 // gets the updated representation.
163 // Updates the representation with latest internal state before
165 OCRepresentation get()
167 m_garageRep.setValue("state", m_state);
174 // Create the instance of the resource class (in this case instance of class 'GarageResource').
175 GarageResource myGarage;
177 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
179 auto pResponse = std::make_shared<OC::OCResourceResponse>();
180 pResponse->setRequestHandle(pRequest->getRequestHandle());
181 pResponse->setResourceHandle(pRequest->getResourceHandle());
182 pResponse->setResourceRepresentation(myGarage.get());
183 pResponse->setErrorCode(200);
184 pResponse->setResponseResult(OC_EH_OK);
186 return OCPlatform::sendResponse(pResponse);
189 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
191 cout << "\tIn Server CPP entity handler:\n";
192 OCEntityHandlerResult ehResult = OC_EH_ERROR;
196 // Get the request type and request flag
197 std::string requestType = request->getRequestType();
198 int requestFlag = request->getRequestHandlerFlag();
200 if(requestFlag & RequestHandlerFlag::InitFlag)
202 cout << "\t\trequestFlag : Init\n";
204 // entity handler to perform resource initialization operations
206 if(requestFlag & RequestHandlerFlag::RequestFlag)
208 cout << "\t\trequestFlag : Request\n";
210 // If the request type is GET
211 if(requestType == "GET")
213 cout << "\t\t\trequestType : GET\n";
214 if(OC_STACK_OK == sendResponse(request))
219 else if(requestType == "PUT")
221 cout << "\t\t\trequestType : PUT\n";
222 OCRepresentation rep = request->getResourceRepresentation();
223 // Do related operations related to PUT request
225 if(OC_STACK_OK == sendResponse(request))
230 else if(requestType == "POST")
232 // POST request operations
234 else if(requestType == "DELETE")
236 // DELETE request operations
239 if(requestFlag & RequestHandlerFlag::ObserverFlag)
241 // OBSERVE operations
246 std::cout << "Request invalid" << std::endl;
252 int main(int argc, char* argv[1])
254 // Create PlatformConfig object
256 OC::ServiceType::InProc,
257 OC::ModeType::Server,
258 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
259 0, // Uses randomly available port
260 OC::QualityOfService::LowQos
263 OCPlatform::Configure(cfg);
266 // Invoke createResource function of class light.
267 myGarage.createResource();
269 // A condition variable will free the mutex it is given, then do a non-
270 // intensive block until 'notify' is called on it. In this case, since we
271 // don't ever call cv.notify, this should be a non-processor intensive version
274 std::condition_variable cv;
275 std::unique_lock<std::mutex> lock(blocker);
283 // No explicit call to stop the OCPlatform
284 // When OCPlatform destructor is invoked, internally we do Platform cleanup