Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupaction / bookmark.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics 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 #include <functional>
22 #include <pthread.h>
23
24 #include "OCPlatform.h"
25 #include "OCApi.h"
26
27 using namespace OC;
28 using namespace std;
29
30 namespace PH = std::placeholders;
31
32 unsigned int startedThread;
33 unsigned int gObservation;
34 pthread_t threadId;
35
36 void* ObserveHandler(void *param);
37
38 class BookmarkResource
39 {
40
41 private:
42     OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
43     {
44         OCEntityHandlerResult ehResult = OC_EH_ERROR;
45
46         if (request)
47         {
48             // Get the request type and request flag
49             std::string requestType = request->getRequestType();
50             int requestFlag = request->getRequestHandlerFlag();
51
52             if (requestFlag & RequestHandlerFlag::RequestFlag)
53             {
54                 auto pResponse = std::make_shared< OC::OCResourceResponse >();
55                 pResponse->setRequestHandle(request->getRequestHandle());
56                 pResponse->setResourceHandle(request->getResourceHandle());
57
58                 // If the request type is GET
59                 if (requestType == "GET")
60                 {
61                 }
62                 else if (requestType == "PUT")
63                 {
64                     cout << "\t\t\trequestType : PUT\n";
65                 }
66                 else if (requestType == "POST")
67                 {
68                     // POST request operations
69                 }
70                 else if (requestType == "DELETE")
71                 {
72                     // DELETE request operations
73                 }
74
75                 pResponse->setErrorCode(200);
76                 pResponse->setResponseResult(OC_EH_OK);
77                 pResponse->setResourceRepresentation(getRepresentation());
78                 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
79                 {
80                     ehResult = OC_EH_OK;
81                 }
82             }
83
84             if (requestFlag & RequestHandlerFlag::ObserverFlag)
85             {
86                 cout << "\t\trequestFlag : Observer\n";
87
88                 if (!startedThread)
89                 {
90                     pthread_create(&threadId, NULL, ObserveHandler, (void *) NULL);
91                     startedThread = 1;
92                     gObservation = 1;
93                 }
94
95                 ehResult = OC_EH_OK;
96             }
97         }
98         else
99         {
100             std::cout << "Request invalid" << std::endl;
101         }
102
103         return ehResult;
104     }
105
106 public:
107     /// Constructor
108     BookmarkResource()
109     {
110         m_pressure = 0;
111
112         m_BookmarkUri = "/core/bookmark"; // URI of the resource
113         m_BookmarkType = "core.bookmark"; // resource type name. In this case, it is light
114
115         m_BookmarkInterface = DEFAULT_INTERFACE; // resource interface.
116         m_BookmarkHandle = 0;
117     }
118
119     /// This function internally calls registerResource API.
120     void createResources()
121     {
122         EntityHandler cb = std::bind(&BookmarkResource::entityHandler, this, PH::_1);
123
124         // This will internally create and register the resource.
125         OCStackResult result = OC::OCPlatform::registerResource(m_BookmarkHandle, m_BookmarkUri,
126                 m_BookmarkType, m_BookmarkInterface, cb, OC_DISCOVERABLE | OC_OBSERVABLE);
127
128         if (OC_STACK_OK != result)
129         {
130             cout << "Resource creation (bookmark) was unsuccessful\n";
131         }
132         else
133         {
134             cout << "Resource URI : " << m_BookmarkUri << endl;
135             cout << "\tResource Type Name : " << m_BookmarkType << endl;
136             cout << "\tResource Interface : " << DEFAULT_INTERFACE << endl;
137             cout << "\tResource creation is successful with resource handle : " << m_BookmarkHandle
138                     << endl;
139         }
140     }
141
142     void setRepresentation(OCRepresentation& rep)
143     {
144         // AttributeMap attributeMap = rep.getAttributeMap();
145         // if(rep.getValue("level", m_pressure) == true)
146         {
147             std::cout << m_pressure << endl;
148         }
149     }
150
151     OCRepresentation getRepresentation()
152     {
153         OCRepresentation rep;
154
155         rep.setValue("level", (int) m_pressure);
156
157         return rep;
158     }
159
160 public:
161     // Members of Bookmark
162     std::string m_BookmarkUri;
163     std::string m_BookmarkType;
164     std::string m_BookmarkInterface;
165     unsigned int m_pressure;
166     OCResourceHandle m_BookmarkHandle;
167 };
168
169 // Create the instance of the resource class (in this case instance of class 'BookmarkResource').
170 BookmarkResource myBookmarkResource;
171
172 void* ObserveHandler(void *param)
173 {
174     while (startedThread)
175     {
176         sleep(1);
177
178         cout << "input a integer(0:opened, 5:close) : ";
179         cin >> myBookmarkResource.m_pressure;
180
181         if (myBookmarkResource.m_pressure == 0 || // When your book opened.
182                 myBookmarkResource.m_pressure == 5) // When your book closed.
183         {
184             cout << "notifyObservers call!" << endl;
185
186             OCStackResult result = OCPlatform::notifyAllObservers(
187                     myBookmarkResource.m_BookmarkHandle);
188
189             if (OC_STACK_NO_OBSERVERS == result)
190             {
191                 cout << "No More observers, stopping notifications" << endl;
192                 gObservation = 0;
193                 startedThread = 0;
194             }
195         }
196     }
197
198     return NULL;
199 }
200
201 int main()
202 {
203     // Create PlatformConfig object
204
205     OC::PlatformConfig cfg
206     { OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0",
207     // By setting to "0.0.0.0", it binds to all available interfaces
208             0,// Uses randomly available port
209             OC::QualityOfService::LowQos };
210
211     // Create a OCPlatform instance.
212     // Note: Platform creation is synchronous call.
213     try
214     {
215
216         // Invoke createResource function of class bookmark.
217         myBookmarkResource.createResources();
218
219         // Perform app tasks
220         while (true)
221         {
222             // some tasks
223         }
224     }
225     catch (OCException e)
226     {
227         std::cout << "Exception: " << e.what() << std::endl;
228     }
229 }