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 steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
31 #include <condition_variable>
33 #include "OCPlatform.h"
39 #include "platform_features.h"
44 #define numPresenceResources (2)
46 // Forward declaring the entityHandler
47 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
49 /// This class represents a single resource named 'lightResource'. This resource has
50 /// two simple properties named 'state' and 'power'
55 /// Access this property from a TB client
58 std::string m_lightUri;
59 std::string m_lightUri2;
60 std::string m_lightUri3;
61 OCResourceHandle m_resourceHandle;
62 OCResourceHandle m_resourceHandle2;
63 OCResourceHandle m_resourceHandle3;
67 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
68 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
70 /* Note that this does not need to be a member function: for classes you do not have
71 access to, you can accomplish this with a free function: */
73 /// This function internally calls registerResource API.
76 std::string resourceURI = m_lightUri; // URI of the resource
77 std::string resourceTypeName = "core.light"; // resource type name.
78 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
80 // OCResourceProperty is defined ocstack.h
81 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
83 // This will internally create and register the resource.
84 OCStackResult result = OCPlatform::registerResource(
85 m_resourceHandle, resourceURI, resourceTypeName,
86 resourceInterface, &entityHandler, resourceProperty);
88 if (OC_STACK_OK != result)
90 cout << "Resource creation was unsuccessful\n";
94 /// This function internally calls registerResource API.
95 void createResource2()
97 std::string resourceURI = m_lightUri2; // URI of the resource
98 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
99 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
101 // OCResourceProperty is defined ocstack.h
102 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
104 // This will internally create and register the resource.
105 OCStackResult result = OCPlatform::registerResource(
106 m_resourceHandle2, resourceURI, resourceTypeName,
107 resourceInterface, &entityHandler, resourceProperty);
109 if (OC_STACK_OK != result)
111 cout << "Resource creation was unsuccessful\n";
115 void createResource3()
117 std::string resourceURI = m_lightUri3; // URI of the resource
118 std::string resourceTypeName = "core.light";
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_resourceHandle3, 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 void addType(const std::string& type) const
142 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
143 if (OC_STACK_OK != result)
145 cout << "Binding TypeName to Resource was unsuccessful\n";
149 void addInterface(const std::string& iface) const
151 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
152 if (OC_STACK_OK != result)
154 cout << "Binding TypeName to Resource was unsuccessful\n";
160 void createPresenceResources()
162 std::array<std::string, numPresenceResources> resourceURI { {
165 std::array<std::string, numPresenceResources> resourceTypeName { {
169 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
170 OCResourceHandle handle;
171 // OCResourceProperty is defined ocstack.h
172 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
174 // This will internally create and register the resource.
175 OCStackResult result = OC_STACK_OK;
176 for(int i=0; i<numPresenceResources; i++)
178 result = OCPlatform::registerResource(handle,
179 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
180 &entityHandler, resourceProperty);
181 if (result != OC_STACK_OK)
183 cout << "Resource creation was unsuccessful with resource URI "
184 << resourceURI.at(i);
189 // Create the instance of the resource class (in this case instance of class 'LightResource').
190 LightResource myLightResource;
192 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
194 cout << "\tIn Server CPP entity handler:\n";
200 // Create PlatformConfig object
202 OC::ServiceType::InProc,
203 OC::ModeType::Server,
204 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
205 0, // Uses randomly available port
206 OC::QualityOfService::LowQos
209 OCPlatform::Configure(cfg);
212 using namespace OC::OCPlatform;
213 // Time to Live is 30 seconds
216 // Invoke createResource function of class light.
217 myLightResource.createResource();
218 std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
220 std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
224 std :: cout << "\nCreating the second resource of type \"core.light\"" << std :: endl;
227 myLightResource.createResource2();
229 std :: cout << "Stopping presence\n" << std :: endl;
233 std :: cout << "Restarting presence\n" << std :: endl;
238 std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
241 myLightResource.createResource3();
243 std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
246 createPresenceResources();
248 // A condition variable will free the mutex it is given, then do a non-
249 // intensive block until 'notify' is called on it. In this case, since we
250 // don't ever call cv.notify, this should be a non-processor intensive version
253 std::condition_variable cv;
254 std::unique_lock<std::mutex> lock(blocker);
257 catch(OCException& e)
259 oclog() << "Exception in main: "<< e.what();
262 // No explicit call to stop the platform.
263 // When OCPlatform destructor is invoked, internally we do platform cleanup