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,
38 std::shared_ptr<OCResourceResponse> response);
40 /// This class represents a single resource named 'lightResource'. This resource has
41 /// two simple properties named 'state' and 'power'
46 /// Access this property from a TB client
49 std::string m_lightUri;
50 std::string m_lightUri2;
51 std::string m_lightUri3;
52 OCResourceHandle m_resourceHandle;
53 OCResourceHandle m_resourceHandle2;
54 OCResourceHandle m_resourceHandle3;
58 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
59 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
61 /* Note that this does not need to be a member function: for classes you do not have
62 access to, you can accomplish this with a free function: */
64 /// This function internally calls registerResource API.
67 std::string resourceURI = m_lightUri; // URI of the resource
68 std::string resourceTypeName = "core.light"; // resource type name.
69 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
71 // OCResourceProperty is defined ocstack.h
72 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
74 // This will internally create and register the resource.
75 OCStackResult result = OCPlatform::registerResource(
76 m_resourceHandle, resourceURI, resourceTypeName,
77 resourceInterface, &entityHandler, resourceProperty);
79 if (OC_STACK_OK != result)
81 cout << "Resource creation was unsuccessful\n";
85 /// This function internally calls registerResource API.
86 void createResource2()
88 std::string resourceURI = m_lightUri2; // URI of the resource
89 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
90 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
92 // OCResourceProperty is defined ocstack.h
93 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
95 // This will internally create and register the resource.
96 OCStackResult result = OCPlatform::registerResource(
97 m_resourceHandle2, resourceURI, resourceTypeName,
98 resourceInterface, &entityHandler, resourceProperty);
100 if (OC_STACK_OK != result)
102 cout << "Resource creation was unsuccessful\n";
106 void createResource3()
108 std::string resourceURI = m_lightUri3; // URI of the resource
109 std::string resourceTypeName = "core.light";
110 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
112 // OCResourceProperty is defined ocstack.h
113 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
115 // This will internally create and register the resource.
116 OCStackResult result = OCPlatform::registerResource(
117 m_resourceHandle3, resourceURI, resourceTypeName,
118 resourceInterface, &entityHandler, resourceProperty);
120 if (OC_STACK_OK != result)
122 cout << "Resource creation was unsuccessful\n";
126 OCResourceHandle getHandle()
128 return m_resourceHandle;
131 void addType(const std::string& type) const
133 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
134 if (OC_STACK_OK != result)
136 cout << "Binding TypeName to Resource was unsuccessful\n";
140 void addInterface(const std::string& interface) const
142 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
143 if (OC_STACK_OK != result)
145 cout << "Binding TypeName to Resource was unsuccessful\n";
151 // Create the instance of the resource class (in this case instance of class 'LightResource').
152 LightResource myLightResource;
154 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
156 cout << "\tIn Server CPP entity handler:\n";
162 // Create PlatformConfig object
164 OC::ServiceType::InProc,
165 OC::ModeType::Server,
166 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
167 0, // Uses randomly available port
168 OC::QualityOfService::LowQos
171 OCPlatform::Configure(cfg);
174 using namespace OC::OCPlatform;
175 // Time to Live is 30 seconds
178 // Invoke createResource function of class light.
179 myLightResource.createResource();
181 printf("\nEnter a key to create the second resource\n");
184 myLightResource.createResource2();
186 printf("\nEnter a key to stop the presence\n");
190 printf("\nEnter a key to restart the presence\n");
195 printf("\nEnter a key to create the third resource\n");
198 myLightResource.createResource3();
211 // No explicit call to stop the platform.
212 // When OCPlatform destructor is invoked, internally we do platform cleanup