Merge "Implement functionality to set PSK credentials for DTLS in OC stack."
[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
39 /// This class represents a single resource named 'lightResource'. This resource has
40 /// two simple properties named 'state' and 'power'
41
42 class LightResource
43 {
44 public:
45     /// Access this property from a TB client
46     bool m_state;
47     int m_power;
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;
54
55 public:
56     /// Constructor
57     LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
58                      m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
59
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: */
62
63     /// This function internally calls registerResource API.
64     void createResource()
65     {
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.
69
70         // OCResourceProperty is defined ocstack.h
71         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
72
73         // This will internally create and register the resource.
74         OCStackResult result = OCPlatform::registerResource(
75                                     m_resourceHandle, resourceURI, resourceTypeName,
76                                     resourceInterface, &entityHandler, resourceProperty);
77
78         if (OC_STACK_OK != result)
79         {
80             cout << "Resource creation was unsuccessful\n";
81         }
82     }
83
84     /// This function internally calls registerResource API.
85     void createResource2()
86     {
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.
90
91         // OCResourceProperty is defined ocstack.h
92         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
93
94         // This will internally create and register the resource.
95         OCStackResult result = OCPlatform::registerResource(
96                                     m_resourceHandle2, resourceURI, resourceTypeName,
97                                     resourceInterface, &entityHandler, resourceProperty);
98
99         if (OC_STACK_OK != result)
100         {
101             cout << "Resource creation was unsuccessful\n";
102         }
103     }
104
105     void createResource3()
106     {
107         std::string resourceURI = m_lightUri3; // URI of the resource
108         std::string resourceTypeName = "core.light";
109         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
110
111         // OCResourceProperty is defined ocstack.h
112         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
113
114         // This will internally create and register the resource.
115         OCStackResult result = OCPlatform::registerResource(
116                                     m_resourceHandle3, resourceURI, resourceTypeName,
117                                     resourceInterface, &entityHandler, resourceProperty);
118
119         if (OC_STACK_OK != result)
120         {
121             cout << "Resource creation was unsuccessful\n";
122         }
123     }
124
125     OCResourceHandle getHandle()
126     {
127         return m_resourceHandle;
128     }
129
130     void addType(const std::string& type) const
131     {
132         OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
133         if (OC_STACK_OK != result)
134         {
135             cout << "Binding TypeName to Resource was unsuccessful\n";
136         }
137     }
138
139     void addInterface(const std::string& interface) const
140     {
141         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
142         if (OC_STACK_OK != result)
143         {
144             cout << "Binding TypeName to Resource was unsuccessful\n";
145         }
146     }
147
148 };
149
150 // Create the instance of the resource class (in this case instance of class 'LightResource').
151 LightResource myLightResource;
152
153 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
154 {
155     cout << "\tIn Server CPP entity handler:\n";
156     return OC_EH_OK;
157 }
158
159 int main()
160 {
161     // Create PlatformConfig object
162     PlatformConfig cfg {
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
168     };
169
170     OCPlatform::Configure(cfg);
171     try
172     {
173         using namespace OC::OCPlatform;
174         // Time to Live is 30 seconds
175         startPresence(30);
176
177         // Invoke createResource function of class light.
178         myLightResource.createResource();
179
180         printf("\nEnter a key to create the second resource\n");
181         getchar();
182
183         myLightResource.createResource2();
184
185         printf("\nEnter a key to stop the presence\n");
186         getchar();
187         stopPresence();
188
189         printf("\nEnter a key to restart the presence\n");
190         getchar();
191
192         startPresence(30);
193
194         printf("\nEnter a key to create the third resource\n");
195         getchar();
196
197         myLightResource.createResource3();
198
199         // Perform app tasks
200         while(true)
201         {
202             // some tasks
203         }
204     }
205     catch(OCException e)
206     {
207         //log(e.what());
208     }
209
210     // No explicit call to stop the platform.
211     // When OCPlatform destructor is invoked, internally we do platform cleanup
212 }