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.
30 #include <condition_variable>
32 #include "OCPlatform.h"
38 // Forward declaring the entityHandler
39 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
41 /// This class represents a single resource named 'lightResource'. This resource has
42 /// two simple properties named 'state' and 'power'
47 /// Access this property from a TB client
50 std::string m_lightUri;
51 std::string m_lightUri2;
52 std::string m_lightUri3;
53 OCResourceHandle m_resourceHandle;
54 OCResourceHandle m_resourceHandle2;
55 OCResourceHandle m_resourceHandle3;
59 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
60 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
62 /* Note that this does not need to be a member function: for classes you do not have
63 access to, you can accomplish this with a free function: */
65 /// This function internally calls registerResource API.
68 std::string resourceURI = m_lightUri; // URI of the resource
69 std::string resourceTypeName = "core.light"; // resource type name.
70 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
72 // OCResourceProperty is defined ocstack.h
73 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
75 // This will internally create and register the resource.
76 OCStackResult result = OCPlatform::registerResource(
77 m_resourceHandle, resourceURI, resourceTypeName,
78 resourceInterface, &entityHandler, resourceProperty);
80 if (OC_STACK_OK != result)
82 cout << "Resource creation was unsuccessful\n";
86 /// This function internally calls registerResource API.
87 void createResource2()
89 std::string resourceURI = m_lightUri2; // URI of the resource
90 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
91 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
93 // OCResourceProperty is defined ocstack.h
94 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
96 // This will internally create and register the resource.
97 OCStackResult result = OCPlatform::registerResource(
98 m_resourceHandle2, resourceURI, resourceTypeName,
99 resourceInterface, &entityHandler, resourceProperty);
101 if (OC_STACK_OK != result)
103 cout << "Resource creation was unsuccessful\n";
107 void createResource3()
109 std::string resourceURI = m_lightUri3; // URI of the resource
110 std::string resourceTypeName = "core.light";
111 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
113 // OCResourceProperty is defined ocstack.h
114 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
116 // This will internally create and register the resource.
117 OCStackResult result = OCPlatform::registerResource(
118 m_resourceHandle3, resourceURI, resourceTypeName,
119 resourceInterface, &entityHandler, resourceProperty);
121 if (OC_STACK_OK != result)
123 cout << "Resource creation was unsuccessful\n";
127 OCResourceHandle getHandle()
129 return m_resourceHandle;
132 void addType(const std::string& type) const
134 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
135 if (OC_STACK_OK != result)
137 cout << "Binding TypeName to Resource was unsuccessful\n";
141 void addInterface(const std::string& interface) const
143 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
144 if (OC_STACK_OK != result)
146 cout << "Binding TypeName to Resource was unsuccessful\n";
152 // Create the instance of the resource class (in this case instance of class 'LightResource').
153 LightResource myLightResource;
155 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
157 cout << "\tIn Server CPP entity handler:\n";
163 // Create PlatformConfig object
165 OC::ServiceType::InProc,
166 OC::ModeType::Server,
167 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
168 0, // Uses randomly available port
169 OC::QualityOfService::LowQos
172 OCPlatform::Configure(cfg);
175 using namespace OC::OCPlatform;
176 // Time to Live is 30 seconds
179 // Invoke createResource function of class light.
180 myLightResource.createResource();
182 printf("\nEnter a key to create the second resource\n");
185 myLightResource.createResource2();
187 printf("\nEnter a key to stop the presence\n");
191 printf("\nEnter a key to restart the presence\n");
196 printf("\nEnter a key to create the third resource\n");
199 myLightResource.createResource3();
201 // A condition variable will free the mutex it is given, then do a non-
202 // intensive block until 'notify' is called on it. In this case, since we
203 // don't ever call cv.notify, this should be a non-processor intensive version
206 std::condition_variable cv;
207 std::unique_lock<std::mutex> lock(blocker);
215 // No explicit call to stop the platform.
216 // When OCPlatform destructor is invoked, internally we do platform cleanup