New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
[platform/upstream/iotivity.git] / examples / simpleserver.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 int gObservation = 0;
37
38 // Specifies where to notify all observers or list of observers
39 // 0 - notifies all observers
40 // 1 - notifies list of observers
41 int isListOfObservers = 0;
42
43 // Forward declaring the entityHandler
44 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
45
46 /// This class represents a single resource named 'lightResource'. This resource has
47 /// two simple properties named 'state' and 'power'
48
49 class LightResource
50 {
51 public:
52     /// Access this property from a TB client
53     std::string m_name;
54     bool m_state;
55     int m_power;
56     std::string m_lightUri;
57     OCResourceHandle m_resourceHandle;
58     OCRepresentation m_lightRep;
59     ObservationIds m_interestedObservers;
60
61 public:
62     /// Constructor
63     LightResource(): m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
64         // Initialize representation
65         m_lightRep.setUri(m_lightUri);
66
67         m_lightRep.setValue("state", m_state);
68         m_lightRep.setValue("power", m_power);
69         m_lightRep.setValue("name", m_name);
70     }
71
72     /* Note that this does not need to be a member function: for classes you do not have
73     access to, you can accomplish this with a free function: */
74
75     /// This function internally calls registerResource API.
76     void createResource(OC::OCPlatform& platform)
77     {
78         std::string resourceURI = m_lightUri; // URI of the resource
79         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
80         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
81
82         // OCResourceProperty is defined ocstack.h
83         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
84
85         // This will internally create and register the resource.
86         OCStackResult result = platform.registerResource(
87                                     m_resourceHandle, resourceURI, resourceTypeName,
88                                     resourceInterface, &entityHandler, resourceProperty);
89
90         if (OC_STACK_OK != result)
91         {
92             cout << "Resource creation was unsuccessful\n";
93         }
94     }
95
96     OCResourceHandle getHandle()
97     {
98         return m_resourceHandle;
99     }
100
101     // Puts representation.
102     // Gets values from the representation and
103     // updates the internal state
104     void put(OCRepresentation& rep)
105     {
106         try {
107             if (rep.getValue("state", m_state))
108             {
109                 cout << "\t\t\t\t" << "state: " << m_state << endl;
110             }
111             else
112             {
113                 cout << "\t\t\t\t" << "state not found in the representation" << endl;
114             }
115
116             if (rep.getValue("power", m_power))
117             {
118                 cout << "\t\t\t\t" << "power: " << m_power << endl;
119             }
120             else
121             {
122                 cout << "\t\t\t\t" << "power not found in the representation" << endl;
123             }
124         }
125         catch (exception& e)
126         {
127             cout << e.what() << endl;
128         }
129
130     }
131
132     // gets the updated representation.
133     // Updates the representation with latest internal state before
134     // sending out.
135     OCRepresentation get()
136     {
137         m_lightRep.setValue("state", m_state);
138         m_lightRep.setValue("power", m_power);
139
140         return m_lightRep;
141     }
142
143     void addType(const OC::OCPlatform& platform, const std::string& type) const
144     {
145         OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
146         if (OC_STACK_OK != result)
147         {
148             cout << "Binding TypeName to Resource was unsuccessful\n";
149         }
150     }
151
152     void addInterface(const OC::OCPlatform& platform, const std::string& interface) const
153     {
154         OCStackResult result = platform.bindInterfaceToResource(m_resourceHandle, interface);
155         if (OC_STACK_OK != result)
156         {
157             cout << "Binding TypeName to Resource was unsuccessful\n";
158         }
159     }
160 };
161
162 // Create the instance of the resource class (in this case instance of class 'LightResource').
163 LightResource myLight;
164
165 // ChangeLightRepresentaion is an observation function,
166 // which notifies any changes to the resource to stack
167 // via notifyObservers
168 void * ChangeLightRepresentation (void *param)
169 {
170     // This function continuously monitors for the changes
171     while (1)
172     {
173         sleep (5);
174
175         if (gObservation)
176         {
177             // If under observation if there are any changes to the light resource
178             // we call notifyObservors
179             //
180             // For demostration we are changing the power value and notifying.
181             myLight.m_power += 10;
182
183             cout << "\nPower updated to : " << myLight.m_power << endl;
184             cout << "Notifying observers with resource handle: " << myLight.getHandle() << endl;
185
186             OCStackResult result = OC_STACK_OK;
187
188             if(isListOfObservers)
189             {
190                 std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
191
192                 resourceResponse->setErrorCode(200);
193                 resourceResponse->setResourceRepresentation(myLight.get(), DEFAULT_INTERFACE);
194
195                 result = OCPlatform::notifyListOfObservers(  myLight.getHandle(),
196                                                              myLight.m_interestedObservers,
197                                                              resourceResponse);
198             }
199             else
200             {
201                 result = OCPlatform::notifyAllObservers(myLight.getHandle());
202             }
203
204             if(OC_STACK_NO_OBSERVERS == result)
205             {
206                 cout << "No More observers, stopping notifications" << endl;
207                 gObservation = 0;
208             }
209         }
210     }
211
212     return NULL;
213 }
214
215 // This is just a sample implementation of entity handler.
216 // Entity handler can be implemented in several ways by the manufacturer
217 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
218 {
219     cout << "\tIn Server CPP entity handler:\n";
220
221     if(request)
222     {
223         // Get the request type and request flag
224         std::string requestType = request->getRequestType();
225         int requestFlag = request->getRequestHandlerFlag();
226
227         if(requestFlag & RequestHandlerFlag::InitFlag)
228         {
229             cout << "\t\trequestFlag : Init\n";
230
231             // entity handler to perform resource initialization operations
232         }
233         if(requestFlag & RequestHandlerFlag::RequestFlag)
234         {
235             cout << "\t\trequestFlag : Request\n";
236
237             // If the request type is GET
238             if(requestType == "GET")
239             {
240                 cout << "\t\t\trequestType : GET\n";
241
242                 if(response)
243                 {
244                     // TODO Error Code
245                     response->setErrorCode(200);
246
247                     response->setResourceRepresentation(myLight.get());
248                 }
249             }
250             else if(requestType == "PUT")
251             {
252                 cout << "\t\t\trequestType : PUT\n";
253
254                 OCRepresentation rep = request->getResourceRepresentation();
255
256                 // Do related operations related to PUT request
257
258                 // Update the lightResource
259                 myLight.put(rep);
260
261                 if(response)
262                 {
263                     // TODO Error Code
264                     response->setErrorCode(200);
265
266                     response->setResourceRepresentation(myLight.get());
267                 }
268
269             }
270             else if(requestType == "POST")
271             {
272                 // POST request operations
273             }
274             else if(requestType == "DELETE")
275             {
276                 // DELETE request operations
277             }
278         }
279         if(requestFlag & RequestHandlerFlag::ObserverFlag)
280         {
281             ObservationInfo observationInfo = request->getObservationInfo();
282             if(ObserveAction::ObserveRegister == observationInfo.action)
283             {
284                 myLight.m_interestedObservers.push_back(observationInfo.obsId);
285             }
286             else if(ObserveAction::ObserveUnregister == observationInfo.action)
287             {
288                 myLight.m_interestedObservers.erase(std::remove(
289                                                             myLight.m_interestedObservers.begin(),
290                                                             myLight.m_interestedObservers.end(),
291                                                             observationInfo.obsId),
292                                                             myLight.m_interestedObservers.end());
293             }
294
295             pthread_t threadId;
296
297             cout << "\t\trequestFlag : Observer\n";
298             gObservation = 1;
299
300             static int startedThread = 0;
301
302             // Observation happens on a different thread in ChangeLightRepresentation function.
303             // If we have not created the thread already, we will create one here.
304             if(!startedThread)
305             {
306                 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
307                 startedThread = 1;
308             }
309         }
310     }
311     else
312     {
313         std::cout << "Request invalid" << std::endl;
314     }
315 }
316
317 void PrintUsage()
318 {
319     std::cout << std::endl;
320     std::cout << "Usage : simplserver <isListOfObservers>\n";
321     std::cout << "   ObserveType : 0 - Observe All\n";
322     std::cout << "   ObserveType : 1 - Observe List of observers\n\n";
323 }
324
325
326 int main(int argc, char* argv[1])
327 {
328     PrintUsage();
329
330     if (argc == 1)
331     {
332         isListOfObservers = 0;
333     }
334     else if (argc == 2)
335     {
336         int value = atoi(argv[1]);
337         if (value == 1)
338             isListOfObservers = 1;
339         else
340             isListOfObservers = 0;
341     }
342     else
343     {
344         return -1;
345     }
346
347     // Create PlatformConfig object
348     PlatformConfig cfg {
349         OC::ServiceType::InProc,
350         OC::ModeType::Server,
351         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
352         0,         // Uses randomly available port
353         OC::QualityOfService::NonConfirmable
354     };
355
356     // Create a OCPlatform instance.
357     // Note: Platform creation is synchronous call.
358     try
359     {
360         OCPlatform platform(cfg);
361
362         // Invoke createResource function of class light.
363         myLight.createResource(platform);
364
365         myLight.addType(platform, std::string("core.brightlight"));
366         myLight.addInterface(platform, std::string("oc.mi.ll"));
367         // Perform app tasks
368         while(true)
369         {
370             // some tasks
371         }
372     }
373     catch(OCException e)
374     {
375         //log(e.what());
376     }
377
378     // No explicit call to stop the platform.
379     // When OCPlatform destructor is invoked, internally we do platform cleanup
380 }