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