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 #define numPresenceResources (2)
41 // Forward declaring the entityHandler
42 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
44 /// This class represents a single resource named 'lightResource'. This resource has
45 /// two simple properties named 'state' and 'power'
50 /// Access this property from a TB client
53 std::string m_lightUri;
54 std::string m_lightUri2;
55 std::string m_lightUri3;
56 OCResourceHandle m_resourceHandle;
57 OCResourceHandle m_resourceHandle2;
58 OCResourceHandle m_resourceHandle3;
62 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
63 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
65 /* Note that this does not need to be a member function: for classes you do not have
66 access to, you can accomplish this with a free function: */
68 /// This function internally calls registerResource API.
71 std::string resourceURI = m_lightUri; // URI of the resource
72 std::string resourceTypeName = "core.light"; // resource type name.
73 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
75 // OCResourceProperty is defined ocstack.h
76 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
78 // This will internally create and register the resource.
79 OCStackResult result = OCPlatform::registerResource(
80 m_resourceHandle, resourceURI, resourceTypeName,
81 resourceInterface, &entityHandler, resourceProperty);
83 if (OC_STACK_OK != result)
85 cout << "Resource creation was unsuccessful\n";
89 /// This function internally calls registerResource API.
90 void createResource2()
92 std::string resourceURI = m_lightUri2; // URI of the resource
93 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
94 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
96 // OCResourceProperty is defined ocstack.h
97 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
99 // This will internally create and register the resource.
100 OCStackResult result = OCPlatform::registerResource(
101 m_resourceHandle2, resourceURI, resourceTypeName,
102 resourceInterface, &entityHandler, resourceProperty);
104 if (OC_STACK_OK != result)
106 cout << "Resource creation was unsuccessful\n";
110 void createResource3()
112 std::string resourceURI = m_lightUri3; // URI of the resource
113 std::string resourceTypeName = "core.light";
114 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
116 // OCResourceProperty is defined ocstack.h
117 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
119 // This will internally create and register the resource.
120 OCStackResult result = OCPlatform::registerResource(
121 m_resourceHandle3, resourceURI, resourceTypeName,
122 resourceInterface, &entityHandler, resourceProperty);
124 if (OC_STACK_OK != result)
126 cout << "Resource creation was unsuccessful\n";
130 OCResourceHandle getHandle()
132 return m_resourceHandle;
135 void addType(const std::string& type) const
137 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
138 if (OC_STACK_OK != result)
140 cout << "Binding TypeName to Resource was unsuccessful\n";
144 void addInterface(const std::string& interface) const
146 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
147 if (OC_STACK_OK != result)
149 cout << "Binding TypeName to Resource was unsuccessful\n";
155 void createPresenceResources()
157 std::array<std::string, numPresenceResources> resourceURI { {
160 std::array<std::string, numPresenceResources> resourceTypeName { {
164 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
165 OCResourceHandle handle;
166 // OCResourceProperty is defined ocstack.h
167 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
169 // This will internally create and register the resource.
170 OCStackResult result = OC_STACK_OK;
171 for(int i=0; i<numPresenceResources; i++)
173 result = OCPlatform::registerResource(handle,
174 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
175 &entityHandler, resourceProperty);
176 if (result != OC_STACK_OK)
178 cout << "Resource creation was unsuccessful with resource URI "
179 << resourceURI.at(i);
184 // Create the instance of the resource class (in this case instance of class 'LightResource').
185 LightResource myLightResource;
187 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
189 cout << "\tIn Server CPP entity handler:\n";
195 // Create PlatformConfig object
197 OC::ServiceType::InProc,
198 OC::ModeType::Server,
199 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
200 0, // Uses randomly available port
201 OC::QualityOfService::LowQos
204 OCPlatform::Configure(cfg);
207 using namespace OC::OCPlatform;
208 // Time to Live is 30 seconds
211 // Invoke createResource function of class light.
212 myLightResource.createResource();
213 std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
215 std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
219 std :: cout << "\nCreating the second resource of type \"core.light\"" << std :: endl;
222 myLightResource.createResource2();
224 std :: cout << "Stopping presence\n" << std :: endl;
228 std :: cout << "Restarting presence\n" << std :: endl;
233 std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
236 myLightResource.createResource3();
238 std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
241 createPresenceResources();
243 // A condition variable will free the mutex it is given, then do a non-
244 // intensive block until 'notify' is called on it. In this case, since we
245 // don't ever call cv.notify, this should be a non-processor intensive version
248 std::condition_variable cv;
249 std::unique_lock<std::mutex> lock(blocker);
252 catch(OCException& e)
254 oclog() << "Exception in main: "<< e.what();
257 // No explicit call to stop the platform.
258 // When OCPlatform destructor is invoked, internally we do platform cleanup