Updated license header to reference Apache 2.0 License
[platform/upstream/iotivity.git] / include / OCPlatform.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation 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 /// @file OCPlatform.h 
22
23 /// @brief      This file contains the declaration of classes and its members related to 
24 ///                     OCPlatform.
25
26 #ifndef __OCPLATFORM_H
27 #define __OCPLATFORM_H
28
29 #include <map>
30
31 #include "OCApi.h"
32 #include "OCResource.h"
33 #include "OCPlatformHandler.h"
34 #include "WrapperFactory.h"
35 #include <OCReflect.h>
36
37 using namespace OC::OCReflect;
38
39 namespace OC
40 {
41         using OC::OCReflect::property_type;
42         using OC::OCReflect::named_property_binding;
43
44         /**
45         *       @brief  The core OC platform object initialized by the application or service via a static accessor.
46         *                       On successful initialization an instance of the OCPlatform is returned.  
47         *                       Handler registered that receives various platform/stack updates. The stack object 
48         *                       also provides methods for managing advertisement and discovery.  
49         */
50         class OCPlatform
51         {
52         public:
53                 /**
54                 * @fn    Constructor for OCPlatform. Constructs a new OCPlatform from a given PlatformConfig with 
55                 *                appropriate fields
56                 * @param config - PlatformConfig struct which has details such as modeType (server/client/both),
57                 *                                 in-proc/out-of-proc etc. 
58                 */
59                 OCPlatform(const PlatformConfig& config);
60
61                 /**
62                 * @fn    Virtual destructor
63                 */
64                 virtual ~OCPlatform(void);
65
66                 /**
67                 * @fn    API for Service and Resource Discovery
68                 * 
69                 * @param host - Host IP Address of a service to direct resource discovery query. If null or
70                 *        empty, performs service discovery OR resource discovery query to ALL group members.
71                 * @param filter<std::string FilterType, std::string Value> - Filter Type - Can be one of two of the link
72                 *        format equivalent attribute types declared in table 7 of the
73                 *        OC-Lite-Product-Specification-V1 Document. Either "n" (i.e. Resource Name) or "rt"
74                 *        (i.e. Resource Type). Value - Can be any valid std::string that's defined to work with CoRE
75                 *        Link Attributes as defined in the RFC6690.
76                 * @param handler - Handles callbacks, success states and failure states.
77                 * 
78                 *        Four modes of discovery defined as follows: 
79                 *        (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource discovery. 
80                 *        (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
81                 *                                  from ALL services. 
82                 *        (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service. 
83                 *        (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
84                 *                                  from a particular service.
85                 * 
86                 *        Thread-Safety: TODO Determine if it is going to be safe to call in a UI thread
87                 */
88                 void findResource(const std::string& host, const std::string& resourceName, 
89                         std::function<void(OCResource::Ptr)> resourceHandler);
90
91                 /**
92                 * @fn   Queries the network for available resources.
93                 * 
94                 * NOTE: This gets posted to a new thread so it doesn't hold the UI or network thread.
95                 * 
96                 * @param serviceURL - The URL to the service. NULL for a broadcast/multicast.
97                 * @param resourceType - The resource type filter. NULL for match any.
98                 * 
99                 * @throws This may throw if the service is not started, network is bad, etc.
100                 * 
101                 *         Thread-Safety: TODO Determine if it is going to be safe to call in a UI thread
102                 */
103                 void findResourceByType(const std::string& serviceURL, const std::string& resourceType,
104                         std::function<void(OCResource::Ptr)> resourceHandler);
105
106                 /**
107                 * @fn   Registers the resource with its name, type and list of properties
108                 * 
109                 * NOTE: Properties are prepared prior to this call using generic functions from OCReflect
110                 * 
111                 * @param resourceURI - The URI of the resoruce.
112                 * @param resourceTypeName - The resource type. 
113                 * @param properties - vector of properties for this resource
114                 * 
115                 */
116                 void registerResource(const std::string& resourceURI, const std::string& resourceTypeName, 
117                         named_property_binding_vector properties);
118
119         private:
120                 std::unique_ptr<WrapperFactory> m_WrapperInstance;
121                 IServerWrapper::Ptr m_server;
122                 IClientWrapper::Ptr m_client;
123
124                 /**
125                 *  @fn  Private function to initalize the platfrom 
126                 */
127                 void init(const PlatformConfig& config);
128
129                 /**
130                 *  @fn  Private function cleanup the platform
131                 */
132                 void cleanup();
133         };
134 }
135
136 #endif //__OCPLATFORM_H