[IOT-1413,IOT-1415] Fixed request handling bugs for /oic/ping resource
[platform/upstream/iotivity.git] / resource / csdk / stack / include / internal / ocresourcehandler.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 #ifndef OC_RESOURCEHANDLER_H
22 #define OC_RESOURCEHANDLER_H
23
24 #include "ocstack.h"
25 #include "ocstackinternal.h"
26 #include "ocserverrequest.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif // __cplusplus
31
32 /**
33  * Common JSON string components used by the stack to build JSON strings.
34  * These details are exposed in ocstackconfig.h file in the form of documentation.
35  * Remember to update the documentation there if these are changed.
36  */
37 #define OC_JSON_PREFIX                     "{\"oic\":["
38 #define OC_JSON_PREFIX_LEN                 (sizeof(OC_JSON_PREFIX) - 1)
39 #define OC_JSON_SUFFIX                     "]}"
40 #define OC_JSON_SUFFIX_LEN                 (sizeof(OC_JSON_SUFFIX) - 1)
41 #define OC_JSON_SEPARATOR                  ','
42 #define OC_JSON_SEPARATOR_STR              ","
43 #define OC_KEY_VALUE_DELIMITER             "="
44
45 /**
46  * Static values for various JSON attributes.
47  */
48 #define OC_RESOURCE_OBSERVABLE   1
49 #define OC_RESOURCE_SECURE       1
50
51 /**
52  *  OIC Virtual resources supported by every OIC device.
53  */
54 typedef enum
55 {
56     /** unknown URI.*/
57     OC_UNKNOWN_URI =0,
58
59     /** "/oic/res".*/
60     OC_WELL_KNOWN_URI,
61
62     /** "/oic/d" .*/
63     OC_DEVICE_URI,
64
65     /** "/oic/p" .*/
66     OC_PLATFORM_URI,
67
68     /** "/oic/res/d/type" .*/
69     OC_RESOURCE_TYPES_URI,
70 #ifdef ROUTING_GATEWAY
71     /** "/oic/gateway" .*/
72     OC_GATEWAY_URI,
73 #endif
74 #ifdef WITH_PRESENCE
75     /** "/oic/ad" .*/
76     OC_PRESENCE,
77 #endif
78
79 #ifdef MQ_BROKER
80     /** "/oic/ps" .*/
81     OC_MQ_BROKER_URI,
82 #endif
83
84 #ifdef TCP_ADAPTER
85     /** "/oic/ping" .*/
86     OC_KEEPALIVE_RESOURCE_URI,
87 #endif
88
89     /** Max items in the list */
90     OC_MAX_VIRTUAL_RESOURCES    //<s Max items in the list
91
92 } OCVirtualResources;
93
94 /**
95  * The type of query a request/response message is.
96  */
97 typedef enum
98 {
99     STACK_RES_DISCOVERY_NOFILTER = 0,
100     STACK_RES_DISCOVERY_IF_FILTER,
101     STACK_RES_DISCOVERY_RT_FILTER,
102     STACK_DEVICE_DISCOVERY_DI_FILTER,
103     STACK_DEVICE_DISCOVERY_DN_FILTER
104 } StackQueryTypes;
105
106 /**
107  * The type of handling required to handle a request.
108  */
109 typedef enum
110 {
111     OC_RESOURCE_VIRTUAL = 0,
112     OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER,
113     OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER,
114     OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER,
115     OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER,
116     OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER,
117     OC_RESOURCE_NOT_SPECIFIED
118 } ResourceHandling;
119
120 /**
121  * Default entity handler (ie. callback) to be used for resources with
122  * no entity handler.
123  */
124 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
125         OCEntityHandlerRequest * request, void* callbackParam);
126
127 /**
128  * Find and retrieve pointer to a resource associated with a specific resource
129  * URI.
130  * @return pointer to found resource
131  */
132 OCResource *FindResourceByUri(const char* resourceUri);
133
134 /**
135  * This function checks whether the specified resource URI aligns with a pre-existing
136  * virtual resource; returns false otherwise.
137  * @return true or false.
138  */
139 bool IsVirtualResource(const char* resourceUri);
140
141 /**
142  * Parameter @ref handling returns by-reference the type of resource handling
143  * required by the internal stack based on the specified @ref request.
144  * @return ::OC_STACK_OK for Success, otherwise some error value
145  */
146 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
147                                          ResourceHandling *handling,
148                                          OCResource **resource);
149
150 /**
151  * Processes the specified @ref request based on the type of resource handling
152  * @ref resHandling.
153  * @return ::OC_STACK_OK for Success, otherwise some error value.
154  */
155 OCStackResult ProcessRequest(ResourceHandling resHandling,
156                              OCResource *resource,
157                              OCServerRequest *request);
158
159 /**
160  * Internal API used to save all of the platform's information for use in platform
161  * discovery requests.
162  * @return ::OC_STACK_OK for Success, otherwise some error value.
163  */
164 OCStackResult SavePlatformInfo(OCPlatformInfo info);
165
166 /**
167  * Internal API used to save all of the device's information for use in platform
168  * discovery requests.
169  * @param info       Device name is received from the application.
170  *                   DeviceID, spec version and data model version are initialized by the stack.
171  * @return ::OC_STACK_OK for Success, otherwise some error value.
172  */
173 OCStackResult SaveDeviceInfo(OCDeviceInfo info);
174
175 /**
176  * Internal API used to clear the platform information.
177  */
178 void DeletePlatformInfo();
179
180 /**
181  * Internal API used to clear the device information.
182  */
183 void DeleteDeviceInfo();
184
185 /*
186  * Prepare payload for resource representation.
187  */
188 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
189                     OCRepPayload** payload);
190
191 /**
192  * A helper function that Maps an @ref OCEntityHandlerResult type to an
193  * @ref OCStackResult type.
194  */
195 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult);
196
197 #ifdef __cplusplus
198 }
199 #endif // __cplusplus
200 #endif //OC_RESOURCEHANDLER_H
201