Fixed memory issues related to AddClientCB
[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     // Invocation handle tied to original call to OCDoResource()
55     OCDoHandle handle;
56     // This is used to determine if all responses should be consumed or not.
57     // (For now, only pertains to OC_REST_OBSERVE_ALL Vs. OC_REST_OBSERVE functionality)
58     OCMethod method;
59     // This is the sequence identifier the server applies to the invocation tied to 'handle'.
60     uint32_t sequenceNumber;
61     // This is the request uri associated with the call back
62     char * requestUri;
63     // Struct to hold TTL info for presence
64     #ifdef WITH_PRESENCE
65     OCPresence * presence;
66     OCResourceType * filterResourceType;
67     #endif
68     // next node in this list
69     struct ClientCB    *next;
70 } ClientCB;
71
72 extern struct ClientCB *cbList;
73
74 /** @ingroup ocstack
75  *
76  * This method is used to add a client callback method in cbList.
77  *
78  * @param[out] clientCB
79  *              The resulting node from making this call. Null if out of memory.
80  * @param[in] cbData
81  *              address to client callback function.
82  * @param[in] token
83  *              identifier for OTA CoAP comms.
84  * @param[in] handle
85  *              Masked in the public API as an 'invocation handle' - Used for callback management.
86  * @param[in] method
87  *              OCMethod via which this client callback is expected to operate
88  * @param[in] requestUri
89  *              the resource uri of the request.
90  * @param[in] resourceType
91  *              the resourceType associated with a presence request.
92  *
93  * @brief If the handle you're looking for does not exist, the stack will reply with a RST message.
94  *
95  * @return OC_STACK_OK for Success, otherwise some error value
96  */
97 OCStackResult
98 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
99              CAToken_t * token, OCDoHandle *handle, OCMethod method,
100              char * requestUri, char * resourceTypeName);
101
102 /** @ingroup ocstack
103  *
104  * This method is used to remove a callback node from cbList.
105  *
106  * @param[in] cbNode
107  *              address to client callback node.
108  */
109 void DeleteClientCB(ClientCB *cbNode);
110
111
112 /** @ingroup ocstack
113  *
114  * This method is used to search and retrieve a cb node in cbList.
115  *
116  * @param[in] token
117  *              token to search for.
118  * @param[in] handle
119  *              handle to search for.
120  * @param[in] requestUri
121  *              Uri to search for.
122  *
123  * @brief You can search by token OR by handle, but not both.
124  *
125  * @return address of the node if found, otherwise NULL
126  */
127 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri);
128
129 /**
130  * Inserts a new resource type filter into this cb node.
131  *
132  * @param[in] cbNode
133  *              the node to add the new resourceType filter to
134  * @param[in] resourceTypeName
135  *              the value to create the new resourceType filter from
136  *
137  * @return
138  *      OC_STACK_OK on success
139  *      OC_STACK_ERROR with invalid parameters
140  *      OC_STACK_NO_MEMORY when out of memory
141  */
142 #ifdef WITH_PRESENCE
143 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName);
144 #endif // WITH_PRESENCE
145
146 /** @ingroup ocstack
147  *
148  * This method is used to clear the cbList.
149  *
150  */
151 void DeleteClientCBList();
152
153 /** @ingroup ocstack
154  *
155  * This method is used to verify the presence of a cb node in cbList
156  * and then delete it.
157  *
158  * @param[in] cbNode
159  *              address to client callback node.
160  */
161 void FindAndDeleteClientCB(ClientCB * cbNode);
162
163 /** @ingroup ocstack
164  *
165  * This method is used to search a multicast presence node from list.
166  *
167  * @param[in]  uri
168  *              the uri of the request.
169  *
170  * @return OCMulticastNode
171  *              The resulting node from making this call. Null if doesn't exist.
172  */
173 //------------------------------------------------------------------------
174 OCMulticastNode* GetMCPresenceNode(const char * uri);
175
176 /** @ingroup ocstack
177  *
178  * This method is used to add a multicast presence node to the list.
179  *
180  * @param[out] outnode
181  *              The resulting node from making this call. Null if out of memory.
182  * @param[in] uri
183  *              the uri of the server.
184  * @param[in] nonce
185  *              current nonce for the server
186  *
187  * @return OC_STACK_OK for Success, otherwise some error value
188  */
189 //------------------------------------------------------------------------
190 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce);
191
192 #endif //OC_CLIENT_CB
193