Added timeouts and cleanup for zombie callbacks in NON cases.
[platform/upstream/iotivity.git] / resource / csdk / stack / include / internal / occlientcb.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
22 #ifndef OC_CLIENT_CB
23 #define OC_CLIENT_CB
24
25 #include <ocstack.h>
26
27 #include <ocresource.h>
28 #include "cacommon.h"
29
30 typedef struct OCPresence
31 {
32     // This is the TTL associated with presence
33     uint32_t TTL;
34     uint32_t * timeOut;
35     uint32_t TTLlevel;
36 } OCPresence;
37
38 typedef struct OCMulticastNode
39 {
40     char * uri;
41     uint32_t nonce;
42     struct OCMulticastNode * next;
43 } OCMulticastNode;
44
45 typedef struct ClientCB {
46     // callback method defined in application address space
47     OCClientResponseHandler callBack;
48     // callback context data
49     void * context;
50     // callback method to delete context data
51     OCClientContextDeleter deleteCallback;
52     //  when a response is recvd with this token, above callback will be invoked
53     CAToken_t token;
54     // a response is recvd with this token length
55     uint8_t tokenLength;
56     // Invocation handle tied to original call to OCDoResource()
57     OCDoHandle handle;
58     // This is used to determine if all responses should be consumed or not.
59     // (For now, only pertains to OC_REST_OBSERVE_ALL Vs. OC_REST_OBSERVE functionality)
60     OCMethod method;
61     // This is the sequence identifier the server applies to the invocation tied to 'handle'.
62     uint32_t sequenceNumber;
63     // This is the request uri associated with the call back
64     char * requestUri;
65     // Struct to hold TTL info for presence
66     #ifdef WITH_PRESENCE
67     OCPresence * presence;
68     OCResourceType * filterResourceType;
69     #endif
70     // The connectivity type on which the request was sent on.
71     OCConnectivityType conType;
72     // The TTL for this callback. Holds the time till when this callback can
73     // still be used. TTL is set to 0 when the callback is for presence and observe.
74     // Presence has ttl mechanism in the "presence" member of this struct and observes
75     // can be explicitly cancelled.
76     uint32_t TTL;
77     // next node in this list
78     struct ClientCB    *next;
79 } ClientCB;
80
81 extern struct ClientCB *cbList;
82
83 /** @ingroup ocstack
84  *
85  * This method is used to add a client callback method in cbList.
86  *
87  * @param[out] clientCB
88  *              The resulting node from making this call. Null if out of memory.
89  * @param[in] cbData
90  *              address to client callback function.
91  * @param[in] token
92  *              identifier for OTA CoAP comms.
93  * @param[in] handle
94  *              Masked in the public API as an 'invocation handle' - Used for callback management.
95  * @param[in] method
96  *              OCMethod via which this client callback is expected to operate
97  * @param[in] requestUri
98  *              the resource uri of the request.
99  * @param[in] resourceType
100  *              the resourceType associated with a presence request.
101  * @param[in] conType
102  *              the connectivity type on which the associated request for this clientCB was sent on.
103  * @param[in] ttl
104  *              time to live in coap_ticks for the callback.
105  * @brief If the handle you're looking for does not exist, the stack will reply with a RST message.
106  *
107  * @return OC_STACK_OK for Success, otherwise some error value
108  */
109 OCStackResult
110 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
111              CAToken_t token, uint8_t tokenLength,
112              OCDoHandle *handle, OCMethod method,
113              char * requestUri, char * resourceTypeName, OCConnectivityType conType, uint32_t ttl);
114
115 /** @ingroup ocstack
116  *
117  * This method is used to remove a callback node from cbList.
118  *
119  * @param[in] cbNode
120  *              address to client callback node.
121  */
122 void DeleteClientCB(ClientCB *cbNode);
123
124
125 /** @ingroup ocstack
126  *
127  * This method is used to search and retrieve a cb node in cbList.
128  *
129  * @param[in] token
130  *              token to search for.
131  * @param[in] handle
132  *              handle to search for.
133  * @param[in] requestUri
134  *              Uri to search for.
135  *
136  * @brief You can search by token OR by handle, but not both.
137  *
138  * @return address of the node if found, otherwise NULL
139  */
140 ClientCB* GetClientCB(const CAToken_t token, uint8_t tokenLength,
141         OCDoHandle handle, const char * requestUri);
142
143 /**
144  * Inserts a new resource type filter into this cb node.
145  *
146  * @param[in] cbNode
147  *              the node to add the new resourceType filter to
148  * @param[in] resourceTypeName
149  *              the value to create the new resourceType filter from
150  *
151  * @return
152  *      OC_STACK_OK on success
153  *      OC_STACK_ERROR with invalid parameters
154  *      OC_STACK_NO_MEMORY when out of memory
155  */
156 #ifdef WITH_PRESENCE
157 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName);
158 #endif // WITH_PRESENCE
159
160 /** @ingroup ocstack
161  *
162  * This method is used to clear the cbList.
163  *
164  */
165 void DeleteClientCBList();
166
167 /** @ingroup ocstack
168  *
169  * This method is used to verify the presence of a cb node in cbList
170  * and then delete it.
171  *
172  * @param[in] cbNode
173  *              address to client callback node.
174  */
175 void FindAndDeleteClientCB(ClientCB * cbNode);
176
177 /** @ingroup ocstack
178  *
179  * This method is used to search a multicast presence node from list.
180  *
181  * @param[in]  uri
182  *              the uri of the request.
183  *
184  * @return OCMulticastNode
185  *              The resulting node from making this call. Null if doesn't exist.
186  */
187 //------------------------------------------------------------------------
188 OCMulticastNode* GetMCPresenceNode(const char * uri);
189
190 /** @ingroup ocstack
191  *
192  * This method is used to add a multicast presence node to the list.
193  *
194  * @param[out] outnode
195  *              The resulting node from making this call. Null if out of memory.
196  * @param[in] uri
197  *              the uri of the server.
198  * @param[in] nonce
199  *              current nonce for the server
200  *
201  * @return OC_STACK_OK for Success, otherwise some error value
202  */
203 //------------------------------------------------------------------------
204 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce);
205
206 #endif //OC_CLIENT_CB
207