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::RequestFlag)
54 auto pResponse = std::make_shared< OC::OCResourceResponse >();
55 pResponse->setRequestHandle(request->getRequestHandle());
56 pResponse->setResourceHandle(request->getResourceHandle());
58 // If the request type is GET
59 if (requestType == "GET")
62 else if (requestType == "PUT")
64 cout << "\t\t\trequestType : PUT\n";
66 else if (requestType == "POST")
68 // POST request operations
70 else if (requestType == "DELETE")
72 // DELETE request operations
75 pResponse->setErrorCode(200);
76 pResponse->setResponseResult(OC_EH_OK);
77 pResponse->setResourceRepresentation(getRepresentation());
78 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
84 if (requestFlag & RequestHandlerFlag::ObserverFlag)
86 cout << "\t\trequestFlag : Observer\n";
90 pthread_create(&threadId, NULL, ObserveHandler, (void *) NULL);
100 std::cout << "Request invalid" << std::endl;
112 m_BookmarkUri = "/core/bookmark"; // URI of the resource
113 m_BookmarkType = "core.bookmark"; // resource type name. In this case, it is light
115 m_BookmarkInterface = DEFAULT_INTERFACE; // resource interface.
116 m_BookmarkHandle = 0;
119 /// This function internally calls registerResource API.
120 void createResources()
122 EntityHandler cb = std::bind(&BookmarkResource::entityHandler, this, PH::_1);
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);
128 if (OC_STACK_OK != result)
130 cout << "Resource creation (bookmark) was unsuccessful\n";
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
142 void setRepresentation(OCRepresentation& rep)
144 // AttributeMap attributeMap = rep.getAttributeMap();
145 // if(rep.getValue("level", m_pressure) == true)
147 std::cout << m_pressure << endl;
151 OCRepresentation getRepresentation()
153 OCRepresentation rep;
155 rep.setValue("level", (int) m_pressure);
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;
169 // Create the instance of the resource class (in this case instance of class 'BookmarkResource').
170 BookmarkResource myBookmarkResource;
172 void* ObserveHandler(void *param)
174 while (startedThread)
178 cout << "input a integer(0:opened, 5:close) : ";
179 cin >> myBookmarkResource.m_pressure;
181 if (myBookmarkResource.m_pressure == 0 || // When your book opened.
182 myBookmarkResource.m_pressure == 5) // When your book closed.
184 cout << "notifyObservers call!" << endl;
186 OCStackResult result = OCPlatform::notifyAllObservers(
187 myBookmarkResource.m_BookmarkHandle);
189 if (OC_STACK_NO_OBSERVERS == result)
191 cout << "No More observers, stopping notifications" << endl;
203 // Create PlatformConfig object
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 };
211 // Create a OCPlatform instance.
212 // Note: Platform creation is synchronous call.
216 // Invoke createResource function of class bookmark.
217 myBookmarkResource.createResources();
225 catch (OCException e)
227 std::cout << "Exception: " << e.what() << std::endl;