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 "OCPlatform.h"
36 // Forward declaring the entityHandler
37 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
39 /// This class represents a single resource named 'lightResource'. This resource has
40 /// two simple properties named 'state' and 'power'
45 /// Access this property from a TB client
48 std::string m_lightUri;
49 std::string m_lightUri2;
50 std::string m_lightUri3;
51 OCResourceHandle m_resourceHandle;
52 OCResourceHandle m_resourceHandle2;
53 OCResourceHandle m_resourceHandle3;
57 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
58 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
60 /* Note that this does not need to be a member function: for classes you do not have
61 access to, you can accomplish this with a free function: */
63 /// This function internally calls registerResource API.
66 std::string resourceURI = m_lightUri; // URI of the resource
67 std::string resourceTypeName = "core.light"; // resource type name.
68 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
70 // OCResourceProperty is defined ocstack.h
71 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
73 // This will internally create and register the resource.
74 OCStackResult result = OCPlatform::registerResource(
75 m_resourceHandle, resourceURI, resourceTypeName,
76 resourceInterface, &entityHandler, resourceProperty);
78 if (OC_STACK_OK != result)
80 cout << "Resource creation was unsuccessful\n";
84 /// This function internally calls registerResource API.
85 void createResource2()
87 std::string resourceURI = m_lightUri2; // URI of the resource
88 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
89 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
91 // OCResourceProperty is defined ocstack.h
92 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
94 // This will internally create and register the resource.
95 OCStackResult result = OCPlatform::registerResource(
96 m_resourceHandle2, resourceURI, resourceTypeName,
97 resourceInterface, &entityHandler, resourceProperty);
99 if (OC_STACK_OK != result)
101 cout << "Resource creation was unsuccessful\n";
105 void createResource3()
107 std::string resourceURI = m_lightUri3; // URI of the resource
108 std::string resourceTypeName = "core.light";
109 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
111 // OCResourceProperty is defined ocstack.h
112 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
114 // This will internally create and register the resource.
115 OCStackResult result = OCPlatform::registerResource(
116 m_resourceHandle3, resourceURI, resourceTypeName,
117 resourceInterface, &entityHandler, resourceProperty);
119 if (OC_STACK_OK != result)
121 cout << "Resource creation was unsuccessful\n";
125 OCResourceHandle getHandle()
127 return m_resourceHandle;
130 void addType(const std::string& type) const
132 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
133 if (OC_STACK_OK != result)
135 cout << "Binding TypeName to Resource was unsuccessful\n";
139 void addInterface(const std::string& interface) const
141 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
142 if (OC_STACK_OK != result)
144 cout << "Binding TypeName to Resource was unsuccessful\n";
150 // Create the instance of the resource class (in this case instance of class 'LightResource').
151 LightResource myLightResource;
153 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
155 cout << "\tIn Server CPP entity handler:\n";
161 // Create PlatformConfig object
163 OC::ServiceType::InProc,
164 OC::ModeType::Server,
165 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
166 0, // Uses randomly available port
167 OC::QualityOfService::LowQos
170 OCPlatform::Configure(cfg);
173 using namespace OC::OCPlatform;
174 // Time to Live is 30 seconds
177 // Invoke createResource function of class light.
178 myLightResource.createResource();
180 printf("\nEnter a key to create the second resource\n");
183 myLightResource.createResource2();
185 printf("\nEnter a key to stop the presence\n");
189 printf("\nEnter a key to restart the presence\n");
194 printf("\nEnter a key to create the third resource\n");
197 myLightResource.createResource3();
210 // No explicit call to stop the platform.
211 // When OCPlatform destructor is invoked, internally we do platform cleanup