1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "OCPlatform.h"
30 namespace PH = std::placeholders;
32 unsigned int startedThread;
33 unsigned int gObservation;
36 void* ObserveHandler(void *param);
38 class BookmarkResource
42 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
44 OCEntityHandlerResult ehResult = OC_EH_ERROR;
48 // Get the request type and request flag
49 std::string requestType = request->getRequestType();
50 int requestFlag = request->getRequestHandlerFlag();
52 if (requestFlag & RequestHandlerFlag::InitFlag)
54 cout << "\t\trequestFlag : Init\n";
56 // entity handler to perform resource initialization operations
58 else if (requestFlag & RequestHandlerFlag::RequestFlag)
60 auto pResponse = std::make_shared< OC::OCResourceResponse >();
61 pResponse->setRequestHandle(request->getRequestHandle());
62 pResponse->setResourceHandle(request->getResourceHandle());
64 // If the request type is GET
65 if (requestType == "GET")
68 else if (requestType == "PUT")
70 cout << "\t\t\trequestType : PUT\n";
72 else if (requestType == "POST")
74 // POST request operations
76 else if (requestType == "DELETE")
78 // DELETE request operations
81 pResponse->setErrorCode(200);
82 pResponse->setResponseResult(OC_EH_OK);
83 pResponse->setResourceRepresentation(getRepresentation());
84 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
90 if (requestFlag & RequestHandlerFlag::ObserverFlag)
92 cout << "\t\trequestFlag : Observer\n";
96 pthread_create(&threadId, NULL, ObserveHandler, (void *) NULL);
106 std::cout << "Request invalid" << std::endl;
118 m_BookmarkUri = "/core/bookmark"; // URI of the resource
119 m_BookmarkType = "core.bookmark"; // resource type name. In this case, it is light
121 m_BookmarkInterface = DEFAULT_INTERFACE; // resource interface.
122 m_BookmarkHandle = 0;
125 /// This function internally calls registerResource API.
126 void createResources()
128 EntityHandler cb = std::bind(&BookmarkResource::entityHandler, this, PH::_1);
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);
134 if (OC_STACK_OK != result)
136 cout << "Resource creation (bookmark) was unsuccessful\n";
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
148 void setRepresentation(OCRepresentation& rep)
150 // AttributeMap attributeMap = rep.getAttributeMap();
151 // if(rep.getValue("level", m_pressure) == true)
153 std::cout << m_pressure << endl;
157 OCRepresentation getRepresentation()
159 OCRepresentation rep;
161 rep.setValue("level", (int) m_pressure);
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;
175 // Create the instance of the resource class (in this case instance of class 'BookmarkResource').
176 BookmarkResource myBookmarkResource;
178 void* ObserveHandler(void *param)
180 while (startedThread)
184 cout << "input a integer(0:opened, 5:close) : ";
185 cin >> myBookmarkResource.m_pressure;
187 if (myBookmarkResource.m_pressure == 0 || // When your book opened.
188 myBookmarkResource.m_pressure == 5) // When your book closed.
190 cout << "notifyObservers call!" << endl;
192 OCStackResult result = OCPlatform::notifyAllObservers(
193 myBookmarkResource.m_BookmarkHandle);
195 if (OC_STACK_NO_OBSERVERS == result)
197 cout << "No More observers, stopping notifications" << endl;
209 // Create PlatformConfig object
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 };
217 // Create a OCPlatform instance.
218 // Note: Platform creation is synchronous call.
222 // Invoke createResource function of class bookmark.
223 myBookmarkResource.createResources();
231 catch (OCException e)
233 std::cout << "Exception in main: " << e.what();