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 'GarageResource'.
44 /// Access this property from a TB client
47 std::string m_garageUri;
48 OCResourceHandle m_resourceHandle;
49 OCRepresentation m_garageRep;
50 ObservationIds m_interestedObservers;
52 // array of lights representation with in GarageResource
53 OCRepresentation m_lightRep;
54 std::vector<OCRepresentation> m_reps;
55 std::vector<std::vector<int>> m_hingeStates;
59 GarageResource(): m_name("John's Garage"), m_state(false), m_garageUri("/a/garage"),
60 m_hingeStates{{1,2,3},{4,5,6}}
62 // Initialize representation
63 m_garageRep.setUri(m_garageUri);
65 m_garageRep["state"] = m_state;
66 m_garageRep["name"] = m_name;
68 // For demonstration purpose we are setting x to nullptr here.
69 // In reality it may happen else where.
70 m_garageRep["nullAttribute"] = nullptr;
72 std::vector<bool> lightStates;
73 std::vector<int> lightPowers;
75 for(int i = 0; i <= 9; i++)
77 lightStates.push_back(i % 2 == 0);
78 lightPowers.push_back(i);
81 m_lightRep["states"] = lightStates;
82 m_lightRep["powers"] = lightPowers;
84 // Storing another representation within a representation
85 m_garageRep["light"] = m_lightRep;
87 OCRepresentation rep1;
89 rep1["key1"] = value1;
90 OCRepresentation rep2;
92 rep2["key2"] = value2;
94 m_reps.push_back(rep1);
95 m_reps.push_back(rep2);
97 // storing array of representations
98 m_garageRep["reps"] = m_reps;
101 // setting json string
102 std::string json = "{\"num\":10,\"rno\":23.5,\"aoa\":[[1,2],[3]],\"str\":\"john\",\
103 \"object\":{\"bl1\":false,\"ar\":[2,3]}, \"objects\":[{\"bl2\":true,\"nl\":null},{\"ar1\":[1,2]}]}";
104 m_garageRep["json"] = json;
106 m_garageRep["hinges"] = m_hingeStates;
109 /* Note that this does not need to be a member function: for classes you do not have
110 access to, you can accomplish this with a free function: */
112 /// This function internally calls registerResource API.
113 void createResource()
115 std::string resourceURI = m_garageUri; // URI of the resource
116 std::string resourceTypeName = "core.garage"; // resource type name.
117 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
119 // OCResourceProperty is defined ocstack.h
120 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
122 // This will internally create and register the resource.
123 OCStackResult result = OCPlatform::registerResource(
124 m_resourceHandle, resourceURI, resourceTypeName,
125 resourceInterface, &entityHandler, resourceProperty);
127 if (OC_STACK_OK != result)
129 cout << "Resource creation was unsuccessful\n";
133 OCResourceHandle getHandle()
135 return m_resourceHandle;
138 // Puts representation.
139 // Gets values from the representation and
140 // updates the internal state
141 void put(OCRepresentation& rep)
144 if (rep.getValue("state", m_state))
146 cout << "\t\t\t\t" << "state: " << m_state << endl;
150 cout << "\t\t\t\t" << "state not found in the representation" << endl;
155 cout << e.what() << endl;
160 // gets the updated representation.
161 // Updates the representation with latest internal state before
163 OCRepresentation get()
165 m_garageRep["state"] = m_state;
172 // Create the instance of the resource class (in this case instance of class 'GarageResource').
173 GarageResource myGarage;
175 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
177 auto pResponse = std::make_shared<OC::OCResourceResponse>();
178 pResponse->setRequestHandle(pRequest->getRequestHandle());
179 pResponse->setResourceHandle(pRequest->getResourceHandle());
180 pResponse->setResourceRepresentation(myGarage.get());
181 pResponse->setErrorCode(200);
182 pResponse->setResponseResult(OC_EH_OK);
184 return OCPlatform::sendResponse(pResponse);
187 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
189 cout << "\tIn Server CPP entity handler:\n";
190 OCEntityHandlerResult ehResult = OC_EH_ERROR;
194 // Get the request type and request flag
195 std::string requestType = request->getRequestType();
196 int requestFlag = request->getRequestHandlerFlag();
198 if(requestFlag & RequestHandlerFlag::RequestFlag)
200 cout << "\t\trequestFlag : Request\n";
202 // If the request type is GET
203 if(requestType == "GET")
205 cout << "\t\t\trequestType : GET\n";
206 if(OC_STACK_OK == sendResponse(request))
211 else if(requestType == "PUT")
213 cout << "\t\t\trequestType : PUT\n";
214 OCRepresentation rep = request->getResourceRepresentation();
215 // Do related operations related to PUT request
217 if(OC_STACK_OK == sendResponse(request))
222 else if(requestType == "POST")
224 // POST request operations
226 else if(requestType == "DELETE")
228 // DELETE request operations
231 if(requestFlag & RequestHandlerFlag::ObserverFlag)
233 // OBSERVE operations
238 std::cout << "Request invalid" << std::endl;
244 int main(int /*argc*/, char** /*argv[1]*/)
246 // Create PlatformConfig object
248 OC::ServiceType::InProc,
249 OC::ModeType::Server,
250 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
251 0, // Uses randomly available port
252 OC::QualityOfService::LowQos
255 OCPlatform::Configure(cfg);
258 // Invoke createResource function of class light.
259 myGarage.createResource();
261 // A condition variable will free the mutex it is given, then do a non-
262 // intensive block until 'notify' is called on it. In this case, since we
263 // don't ever call cv.notify, this should be a non-processor intensive version
266 std::condition_variable cv;
267 std::unique_lock<std::mutex> lock(blocker);
275 // No explicit call to stop the OCPlatform
276 // When OCPlatform destructor is invoked, internally we do Platform cleanup