Repo Merge: Moving resource API down a directory
[platform/upstream/iotivity.git] / resource / examples / presenceserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 ///
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
24 ///
25
26 #include <functional>
27
28 #include <pthread.h>
29
30 #include "OCPlatform.h"
31 #include "OCApi.h"
32
33 using namespace OC;
34 using namespace std;
35
36 // Forward declaring the entityHandler
37 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request,
38                                     std::shared_ptr<OCResourceResponse> response);
39
40 /// This class represents a single resource named 'lightResource'. This resource has
41 /// two simple properties named 'state' and 'power'
42
43 class LightResource
44 {
45 public:
46     /// Access this property from a TB client
47     bool m_state;
48     int m_power;
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;
55
56 public:
57     /// Constructor
58     LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
59                      m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
60
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: */
63
64     /// This function internally calls registerResource API.
65     void createResource()
66     {
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.
70
71         // OCResourceProperty is defined ocstack.h
72         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
73
74         // This will internally create and register the resource.
75         OCStackResult result = OCPlatform::registerResource(
76                                     m_resourceHandle, resourceURI, resourceTypeName,
77                                     resourceInterface, &entityHandler, resourceProperty);
78
79         if (OC_STACK_OK != result)
80         {
81             cout << "Resource creation was unsuccessful\n";
82         }
83     }
84
85     /// This function internally calls registerResource API.
86     void createResource2()
87     {
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.
91
92         // OCResourceProperty is defined ocstack.h
93         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
94
95         // This will internally create and register the resource.
96         OCStackResult result = OCPlatform::registerResource(
97                                     m_resourceHandle2, resourceURI, resourceTypeName,
98                                     resourceInterface, &entityHandler, resourceProperty);
99
100         if (OC_STACK_OK != result)
101         {
102             cout << "Resource creation was unsuccessful\n";
103         }
104     }
105
106     void createResource3()
107     {
108         std::string resourceURI = m_lightUri3; // URI of the resource
109         std::string resourceTypeName = "core.light";
110         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
111
112         // OCResourceProperty is defined ocstack.h
113         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
114
115         // This will internally create and register the resource.
116         OCStackResult result = OCPlatform::registerResource(
117                                     m_resourceHandle3, resourceURI, resourceTypeName,
118                                     resourceInterface, &entityHandler, resourceProperty);
119
120         if (OC_STACK_OK != result)
121         {
122             cout << "Resource creation was unsuccessful\n";
123         }
124     }
125
126     OCResourceHandle getHandle()
127     {
128         return m_resourceHandle;
129     }
130
131     void addType(const std::string& type) const
132     {
133         OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
134         if (OC_STACK_OK != result)
135         {
136             cout << "Binding TypeName to Resource was unsuccessful\n";
137         }
138     }
139
140     void addInterface(const std::string& interface) const
141     {
142         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
143         if (OC_STACK_OK != result)
144         {
145             cout << "Binding TypeName to Resource was unsuccessful\n";
146         }
147     }
148
149 };
150
151 // Create the instance of the resource class (in this case instance of class 'LightResource').
152 LightResource myLightResource;
153
154 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
155 {
156     cout << "\tIn Server CPP entity handler:\n";
157     return OC_EH_OK;
158 }
159
160 int main()
161 {
162     // Create PlatformConfig object
163     PlatformConfig cfg {
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
169     };
170
171     OCPlatform::Configure(cfg);
172     try
173     {
174         using namespace OC::OCPlatform;
175         // Time to Live is 30 seconds
176         startPresence(30);
177
178         // Invoke createResource function of class light.
179         myLightResource.createResource();
180
181         printf("\nEnter a key to create the second resource\n");
182         getchar();
183
184         myLightResource.createResource2();
185
186         printf("\nEnter a key to stop the presence\n");
187         getchar();
188         stopPresence();
189
190         printf("\nEnter a key to restart the presence\n");
191         getchar();
192
193         startPresence(30);
194
195         printf("\nEnter a key to create the third resource\n");
196         getchar();
197
198         myLightResource.createResource3();
199
200         // Perform app tasks
201         while(true)
202         {
203             // some tasks
204         }
205     }
206     catch(OCException e)
207     {
208         //log(e.what());
209     }
210
211     // No explicit call to stop the platform.
212     // When OCPlatform destructor is invoked, internally we do platform cleanup
213 }