1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 * Implementation of the OCPlatform functionality. It contains a singleton
25 * interface that is used only by the OCPlatform namespace and is the
26 * central entrance to the stack.
29 #ifndef __OCPLATFORM_IMPL_H
30 #define __OCPLATFORM_IMPL_H
35 #include "OCResource.h"
36 #include "WrapperFactory.h"
37 #include "OCResourceRequest.h"
38 #include "OCResourceResponse.h"
39 #include "OCRepresentation.h"
41 #include "oc_logger.hpp"
46 * @brief Both server and client must initialize the core platform by instantiating OCPlatform.
47 * On successful initialization, an instance of the OCPlatform is returned.
48 * APIs in OCPlatform provide mechanism to register a resource and host the resource
49 * on the server, find resources on the network etc.
54 static PlatformConfig& globalConfig();
57 * API for overwriting the default configuration of the OCPlatform object.
58 * Note: Any calls made to this AFTER the first call to OCPlatform::Instance
61 static void Configure(const PlatformConfig& config);
64 * API for retrieving the current OCPlatform object. This will use the
65 * default platform config, unless the default is over-written using the
66 * Configure method before the first call to instance.
68 static OCPlatform_impl& Instance();
71 // typedef for handle to cancel presence info with
72 typedef OCDoHandle OCPresenceHandle;
77 virtual ~OCPlatform_impl(void);
80 * API for notifying base that resource's attributes have changed.
82 * @param OCResourceHandle resource handle of the resource
83 * @param QualityOfService the quality of communication
85 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
86 * NOTE: This API is for server side only.
87 * NOTE: OCResourceHandle is defined in ocstack.h.
88 * NOTE: OCStackResult is defined in ocstack.h.
90 OCStackResult notifyAllObservers(OCResourceHandle resourceHandle);
91 OCStackResult notifyAllObservers(OCResourceHandle resourceHandle, QualityOfService QoS);
94 * API for notifying only specific clients that resource's attributes have changed.
96 * @param OCResourceHandle resource handle of the resource
97 * @param observationIds std vector of observationIds. These set of ids are ones which
98 * which will be notified upon resource change.
99 * @param responsePtr OCResourceResponse pointer used by app to fill the response for this
101 * @param QualityOfService the quality of communication
103 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
105 * NOTE: This API is for server side only.
106 * NOTE: OCResourceHandle is defined in ocstack.h.
107 * NOTE: OCStackResult is defined in ocstack.h.
109 OCStackResult notifyListOfObservers(
110 OCResourceHandle resourceHandle,
111 ObservationIds& observationIds,
112 const std::shared_ptr<OCResourceResponse> responsePtr);
113 OCStackResult notifyListOfObservers(
114 OCResourceHandle resourceHandle,
115 ObservationIds& observationIds,
116 const std::shared_ptr<OCResourceResponse> responsePtr,
117 QualityOfService QoS);
120 * API for Service and Resource Discovery.
121 * NOTE: This API applies to client side only.
123 * @param host - Host IP Address of a service to direct resource discovery query. If null or
124 * empty, performs multicast resource discovery query
125 * @param resourceURI - name of the resource. If null or empty, performs search for all
127 * @param handler - Handles callbacks, success states and failure states.
129 * Four modes of discovery defined as follows:
130 * (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource
132 * (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular
133 * resource(s) from ALL services.
134 * (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service.
135 * (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular
136 * resource(s) from a particular service.
137 * @param QualityOfService the quality of communication
139 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
140 * NOTE: First parameter 'host' currently represents an IP address. This will change in
141 * future and will refer to endpoint interface so that we can refer to other transports such
143 * NOTE: OCStackResult is defined in ocstack.h.
145 OCStackResult findResource(const std::string& host, const std::string& resourceURI,
146 OCConnectivityType connectivityType, FindCallback resourceHandler);
147 OCStackResult findResource(const std::string& host, const std::string& resourceURI,
148 OCConnectivityType connectivityType, FindCallback resourceHandler,
149 QualityOfService QoS);
151 * API for Device Discovery
153 * @param host - Host IP Address. If null or empty, Multicast is performed.
154 * @param resourceURI - Uri containing address to the virtual device in C Stack
157 * @param QualityOfService the quality of communication
160 OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
161 OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler);
162 OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
163 OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler,
164 QualityOfService QoS);
167 * This API registers a resource with the server
168 * NOTE: This API applies to server side only.
170 * @param resourceHandle - Upon successful registration, resourceHandle will be filled
171 * @param resourceURI - The URI of the resource. Example: "a/light". See NOTE below
172 * @param resourceTypeName - The resource type. Example: "light"
173 * @param resourceInterface - The resource interface (whether it is collection etc).
174 * @param entityHandler - entity handler callback.
175 * @param resourceProperty - indicates the property of the resource. Defined in ocstack.h.
176 * setting resourceProperty as OC_DISCOVERABLE will allow Discovery of this resource
177 * setting resourceProperty as OC_OBSERVABLE will allow observation
178 * settings resourceProperty as OC_DISCOVERABLE | OC_OBSERVABLE will allow both discovery
181 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
182 * NOTE: "a/light" is a relative URI.
183 * Above relative URI will be prepended (by core) with a host IP + namespace "oc"
184 * Therefore, fully qualified URI format would be //HostIP-Address/namespace/relativeURI"
185 * Example, a relative URI: 'a/light' will result in a fully qualified URI:
186 * //192.168.1.1/oc/a/light"
187 * First parameter can take a relative URI and core will take care of preparing the fully
189 * first parameter can take fully qualified URI and core will take that as is for further
191 * NOTE: OCStackResult is defined in ocstack.h.
193 OCStackResult registerResource(OCResourceHandle& resourceHandle,
194 std::string& resourceURI,
195 const std::string& resourceTypeName,
196 const std::string& resourceInterface,
197 EntityHandler entityHandler,
198 uint8_t resourceProperty);
201 * This API registers a resource with the server
202 * NOTE: This API applies to server & client side.
204 * @param resourceHandle - Upon successful registration, resourceHandle will be filled
205 * @param OCResource - The instance of OCResource that all data filled.
207 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
210 OCStackResult registerResource(OCResourceHandle& resourceHandle,
211 const std::shared_ptr<OCResource> resource);
214 * This API registers all the device specific information
216 * @param OCDeviceInfo - Structure containing all the device related information
218 * @return OCStackResult return value of the API. Returns OC_STACK_OK if success
220 * Note: OCDeviceInfo is defined in OCStack.h
222 OCStackResult registerDeviceInfo(const OCDeviceInfo deviceInfo);
225 * Set default device entity handler
227 * @param entityHandler - entity handler to handle requests for
228 * any undefined resources or default actions.
229 * if NULL is passed it removes the device default entity handler.
232 * OC_STACK_OK - no errors
233 * OC_STACK_ERROR - stack process error
235 OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler);
238 * This API unregisters a resource with the server
239 * NOTE: This API applies to server side only.
241 * @param resourceHandle - This is the resource handle which we which to unregister from the
244 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
245 * NOTE: OCStackResult is defined in ocstack.h.
247 OCStackResult unregisterResource(const OCResourceHandle& resourceHandle) const;
250 * Add a resource to a collection resource.
252 * @param collectionHandle - handle to the collection resource
253 * @param addedResourceHandle - handle to resource to be added to the collection resource
255 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.<br>
256 * NOTE: OCStackResult is defined in ocstack.h. <br>
257 * NOTE: bindResource must be used only after the both collection resource and
258 * resource to add under a collections are created and respective handles obtained<br>
259 * <b>Example:</b> <br>
260 * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
261 * entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
262 * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
263 * entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
264 * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);<br>
265 * At the end of Step 3, resource "a/home" will contain a reference to "a/kitchen".<br>
267 OCStackResult bindResource(const OCResourceHandle collectionHandle,
268 const OCResourceHandle resourceHandle);
271 * Add multiple resources to a collection resource.
273 * @param collectionHandle - handle to the collection resource
274 * @param addedResourceHandleList reference to list of resource handles to be added to
275 * the collection resource
277 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
278 * NOTE: OCStackResult is defined in ocstack.h. <br>
279 * NOTE: bindResources must be used only after the both collection resource and
280 * list of resources to add under a collection are created and respective handles
282 * <b> Example: </b> <br>
283 * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
284 * homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
285 * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
286 * kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
287 * Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface,
288 * roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
289 * Step 4: std::vector<OCResourceHandle> rList; rList.push_back(kitchenResourceHandle);
290 * rList.push_back(roomResourceHandle);<br>
291 * Step 5: bindResource(homeResourceHandle, rList);<br>
292 * At the end of Step 5, resource "a/home" will contain a references to "a/kitchen"
295 OCStackResult bindResources(const OCResourceHandle collectionHandle,
296 const std::vector<OCResourceHandle>& addedResourceHandleList);
299 * Unbind a resource from a collection resource.
301 * @param collectionHandle - handle to the collection resource
302 * @param resourceHandle resource handle to be unbound from the collection resource
304 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
305 * NOTE: OCStackResult is defined in ocstack.h.<br>
306 * NOTE: unbindResource must be used only after the both collection resource and
307 * resource to unbind from a collection are created and respective handles obtained<br>
308 * <b> Example </b> <br>
309 * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
310 * entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
311 * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
312 * entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
313 * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);<br>
314 * Step 4: unbindResource(homeResourceHandle, kitchenResourceHandle);<br>
315 * At the end of Step 4, resource "a/home" will no longer reference "a/kitchen". <br>
317 OCStackResult unbindResource(const OCResourceHandle collectionHandle,
318 const OCResourceHandle resourceHandle);
321 * Unbind resources from a collection resource.
323 * @param collectionHandle - handle to the collection resource
324 * @param resourceHandleList List of resource handles to be unbound from the collection
327 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
329 * NOTE: OCStackResult is defined in ocstack.h.<br>
330 * NOTE: unbindResources must be used only after the both collection resource and
331 * list of resources resource to unbind from a collection are created and respective handles
333 * <b>Example</b> <br>
334 * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
335 * homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
336 * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
337 * kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
338 * Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface,
339 * roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
340 * Step 4: std::vector<OCResourceHandle> rList; rList.push_back(kitchenResourceHandle);
341 * rList.push_back(roomResourceHandle);<br>
342 * Step 5: bindResource(homeResourceHandle, rList);<br>
343 * Step 6: unbindResources(homeResourceHandle, rList);<br>
344 * At the end of Step 6, resource "a/home" will no longer reference to "a/kitchen"
347 OCStackResult unbindResources(const OCResourceHandle collectionHandle,
348 const std::vector<OCResourceHandle>& resourceHandleList);
351 * Binds a type to a particular resource
352 * @param resourceHandle - handle to the resource
353 * @param resourceTypeName - new typename to bind to the resource
355 * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
357 OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
358 const std::string& resourceTypeName) const;
361 * Binds an interface to a particular resource
362 * @param resourceHandle - handle to the resource
363 * @param resourceTypeName - new interface to bind to the resource
365 * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
367 OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle,
368 const std::string& resourceInterfaceName) const;
371 * Start Presence announcements.
373 * @param ttl - time to live
374 * @return OCStackResult - Returns OCSTACK_OK if success <br>
376 * Server can call this function when it comes online for the
377 * first time, or when it comes back online from offline mode,
378 * or when it re enters network.
381 OCStackResult startPresence(const unsigned int ttl);
384 * Stop Presence announcements.
386 * @return OCStackResult - Returns OCSTACK_OK if success <br>
388 * Server can call this function when it is terminating,
389 * going offline, or when going away from network.
392 OCStackResult stopPresence();
395 * subscribes to a server's presence change events. By making this subscription,
396 * every time a server adds/removes/alters a resource, starts or is intentionally
397 * stopped (potentially more to be added later).
399 * @param presenceHandle - a handle object that can be used to identify this subscription
400 * request. It can be used to unsubscribe from these events in the future.
401 * It will be set upon successful return of this method.
402 * @param host - The IP address/addressable name of the server to subscribe to.
403 * @param resourceType - a resource type specified as a filter for subscription callbacks.
404 * @param presenceHandler - callback function that will receive notifications/subscription
407 * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
409 OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
410 OCConnectivityType connectivityType, SubscribeCallback presenceHandler);
411 OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
412 const std::string& resourceType, OCConnectivityType connectivityType,
413 SubscribeCallback presenceHandler);
415 * unsubscribes from a previously subscribed server's presence events. Note that
416 * you may for a short time still receive events from the server since it may take time
417 * for the unsubscribe to take effect.
419 * @param presenceHandle - the handle object provided by the subscribePresence call that
420 * identifies this subscription.
422 * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
424 OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
427 * Creates a resource proxy object so that get/put/observe functionality
428 * can be used without discovering the object in advance. Note that the
429 * consumer of this method needs to provide all of the details required to
430 * correctly contact and observe the object. If the consumer lacks any of
431 * this information, they should discover the resource object normally.
432 * Additionally, you can only create this object if OCPlatform was initialized
433 * to be a Client or Client/Server. Otherwise, this will return an empty
436 * @param host - a string containing a resolvable host address of the server
437 * holding the resource. Currently this should be in the format
438 * coap://address:port, though in the future, we expect this to
439 * change to //address:port
441 * @param uri - the rest of the resource's URI that will permit messages to be
442 * properly routed. Example: /a/light
444 * @param isObservable - a boolean containing whether the resource supports observation
446 * @param resourceTypes - a collection of resource types implemented by the resource
448 * @param interfaces - a collection of interfaces that the resource supports/implements
449 * @return OCResource::Ptr - a shared pointer to the new resource object
451 OCResource::Ptr constructResourceObject(const std::string& host, const std::string& uri,
452 OCConnectivityType connectivityType, bool isObservable,
453 const std::vector<std::string>& resourceTypes,
454 const std::vector<std::string>& interfaces);
456 * Allows application entity handler to send response to an incoming request.
458 * @param pResponse - OCResourceResponse pointer that will permit to set values related
459 * to resource response. <br>
460 * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
462 OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
465 PlatformConfig m_cfg;
468 std::unique_ptr<WrapperFactory> m_WrapperInstance;
469 IServerWrapper::Ptr m_server;
470 IClientWrapper::Ptr m_client;
471 std::shared_ptr<std::recursive_mutex> m_csdkLock;
475 * Constructor for OCPlatform. Constructs a new OCPlatform from a given PlatformConfig with
477 * @param config PlatformConfig struct which has details such as modeType
478 * (server/client/both), in-proc/out-of-proc etc.
480 OCPlatform_impl(const PlatformConfig& config);
483 * Private function to initialize the platform
485 void init(const PlatformConfig& config);
488 * Private constructor/operators to prevent copying
491 OCPlatform_impl(const OCPlatform_impl& other)= delete;
492 OCPlatform_impl& operator=(const OCPlatform_impl&) = delete;
493 OCPlatform_impl& operator=(const OCPlatform_impl&&) = delete;
497 #endif //__OCPLATFORM_IMPL_H