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 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
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.
64 void createResource(OC::OCPlatform& platform)
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 = platform.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(OC::OCPlatform& platform)
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 = platform.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(OC::OCPlatform& platform)
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 = platform.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 OC::OCPlatform& platform, const std::string& type) const
132 OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
133 if (OC_STACK_OK != result)
135 cout << "Binding TypeName to Resource was unsuccessful\n";
139 void addInterface(const OC::OCPlatform& platform, const std::string& interface) const
141 OCStackResult result = platform.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 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
155 cout << "\tIn Server CPP entity handler:\n";
160 // Create PlatformConfig object
162 OC::ServiceType::InProc,
163 OC::ModeType::Server,
164 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
165 0, // Uses randomly available port
166 OC::QualityOfService::NonConfirmable
169 // Create a OCPlatform instance.
170 // Note: Platform creation is synchronous call.
173 OCPlatform platform(cfg);
175 // Time to Live is 30 seconds
176 platform.startPresence(30);
178 // Invoke createResource function of class light.
179 myLightResource.createResource(platform);
181 printf("\nEnter a key to create the second resource\n");
184 myLightResource.createResource2(platform);
186 printf("\nEnter a key to stop the presence\n");
188 platform.stopPresence();
190 printf("\nEnter a key to restart the presence\n");
193 platform.startPresence(30);
195 printf("\nEnter a key to create the third resource\n");
198 myLightResource.createResource3(platform);
211 // No explicit call to stop the platform.
212 // When OCPlatform destructor is invoked, internally we do platform cleanup