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 "OCPlatform.h"
35 // Forward declaring the entityHandler
36 void entityHandler(std::shared_ptr<OCResourceRequest> request,
37 std::shared_ptr<OCResourceResponse> response);
39 /// This class represents a single resource named 'lightResource'. This resource has
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;
58 GarageResource(): m_name("John's Garage"), m_state(false), m_garageUri("/a/garage") {
59 // Initialize representation
60 m_garageRep.setUri(m_garageUri);
62 m_garageRep.setValue("state", m_state);
63 m_garageRep.setValue("name", m_name);
65 // For demonstration purpose we are setting x to nullptr here.
66 // In reality it may happen else where.
69 // Check for nullptr and set null for that attribute
72 m_garageRep.setNULL("nullAttribute");
75 std::vector<bool> lightStates;
76 std::vector<int> lightPowers;
78 for(int i = 0; i <= 9; i++)
80 lightStates.push_back(i % 2 == 0);
81 lightPowers.push_back(i);
84 m_lightRep.setValue("states", lightStates);
85 m_lightRep.setValue("powers", lightPowers);
87 // Storing another representation within a representation
88 m_garageRep.setValue("light", m_lightRep);
90 OCRepresentation rep1;
92 rep1.setValue("key1", value1);
93 OCRepresentation rep2;
95 rep2.setValue("key2", value2);
97 m_reps.push_back(rep1);
98 m_reps.push_back(rep2);
100 // storing array of representations
101 m_garageRep.setValue("reps", m_reps);
104 /* Note that this does not need to be a member function: for classes you do not have
105 access to, you can accomplish this with a free function: */
107 /// This function internally calls registerResource API.
108 void createResource(OC::OCPlatform& platform)
110 std::string resourceURI = m_garageUri; // URI of the resource
111 std::string resourceTypeName = "core.garage"; // resource type name.
112 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
114 // OCResourceProperty is defined ocstack.h
115 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
117 // This will internally create and register the resource.
118 OCStackResult result = platform.registerResource(
119 m_resourceHandle, resourceURI, resourceTypeName,
120 resourceInterface, &entityHandler, resourceProperty);
122 if (OC_STACK_OK != result)
124 cout << "Resource creation was unsuccessful\n";
128 OCResourceHandle getHandle()
130 return m_resourceHandle;
133 // Puts representation.
134 // Gets values from the representation and
135 // updates the internal state
136 void put(OCRepresentation& rep)
139 if (rep.getValue("state", m_state))
141 cout << "\t\t\t\t" << "state: " << m_state << endl;
145 cout << "\t\t\t\t" << "state not found in the representation" << endl;
150 cout << e.what() << endl;
155 // gets the updated representation.
156 // Updates the representation with latest internal state before
158 OCRepresentation get()
160 m_garageRep.setValue("state", m_state);
167 // Create the instance of the resource class (in this case instance of class 'GarageResource').
168 GarageResource myGarage;
170 void entityHandler(std::shared_ptr<OCResourceRequest> request,
171 std::shared_ptr<OCResourceResponse> response)
173 cout << "\tIn Server CPP entity handler:\n";
177 // Get the request type and request flag
178 std::string requestType = request->getRequestType();
179 int requestFlag = request->getRequestHandlerFlag();
181 if(requestFlag & RequestHandlerFlag::InitFlag)
183 cout << "\t\trequestFlag : Init\n";
185 // entity handler to perform resource initialization operations
187 if(requestFlag & RequestHandlerFlag::RequestFlag)
189 cout << "\t\trequestFlag : Request\n";
191 // If the request type is GET
192 if(requestType == "GET")
194 cout << "\t\t\trequestType : GET\n";
199 response->setErrorCode(200);
201 response->setResourceRepresentation(myGarage.get());
204 else if(requestType == "PUT")
206 cout << "\t\t\trequestType : PUT\n";
208 OCRepresentation rep = request->getResourceRepresentation();
210 // Do related operations related to PUT request
212 // Update the lightResource
218 response->setErrorCode(200);
220 response->setResourceRepresentation(myGarage.get());
224 else if(requestType == "POST")
226 // POST request operations
228 else if(requestType == "DELETE")
230 // DELETE request operations
233 if(requestFlag & RequestHandlerFlag::ObserverFlag)
235 // OBSERVE operations
240 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::NonConfirmable
255 // Create a OCPlatform instance.
256 // Note: Platform creation is synchronous call.
259 OCPlatform platform(cfg);
261 // Invoke createResource function of class light.
262 myGarage.createResource(platform);
275 // No explicit call to stop the platform.
276 // When OCPlatform destructor is invoked, internally we do platform cleanup