-OCMulticastNode * mcPresenceNodes = NULL;
-
-OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
- OCCoAPToken * token, OCDoHandle handle, OCMethod method,
- unsigned char * requestUri, unsigned char * resourceType) {
- ClientCB *cbNode;
- cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
- if (cbNode) {
- cbNode->callBack = cbData->cb;
- cbNode->context = cbData->context;
- cbNode->deleteCallback = cbData->cd;
- memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
- cbNode->handle = handle;
- cbNode->method = method;
- cbNode->sequenceNumber = 0;
- #ifdef WITH_PRESENCE
- cbNode->presence = NULL;
- cbNode->filterResourceType = resourceType;
- #endif
- cbNode->requestUri = requestUri;
- LL_APPEND(cbList, cbNode);
+static OCMulticastNode * mcPresenceNodes = NULL;
+
+OCStackResult
+AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
+ CAToken_t * token, uint8_t tokenLength,
+ OCDoHandle *handle, OCMethod method,
+ char * requestUri, char * resourceTypeName)
+{
+ if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ ClientCB *cbNode = NULL;
+
+ #ifdef WITH_PRESENCE
+ if(method == OC_REST_PRESENCE)
+ { // Retrieve the presence callback structure for this specific requestUri.
+ cbNode = GetClientCB(NULL, 0, NULL, requestUri);
+ }
+ #endif // WITH_PRESENCE
+
+ if(!cbNode)// If it does not already exist, create new node.
+ {
+ cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
+ if(!cbNode)
+ {
+ *clientCB = NULL;
+ goto exit;
+ }
+ else
+ {
+ cbNode->callBack = cbData->cb;
+ cbNode->context = cbData->context;
+ cbNode->deleteCallback = cbData->cd;
+ //Note: token memory is allocated in the caller OCDoResource
+ //but freed in DeleteClientCB
+ cbNode->token = *token;
+ cbNode->tokenLength = tokenLength;
+ cbNode->handle = *handle;
+ cbNode->method = method;
+ cbNode->sequenceNumber = 0;
+ #ifdef WITH_PRESENCE
+ cbNode->presence = NULL;
+ cbNode->filterResourceType = NULL;
+ #endif // WITH_PRESENCE
+ cbNode->requestUri = requestUri;
+ LL_APPEND(cbList, cbNode);
+ *clientCB = cbNode;
+ }
+ }
+ else
+ {
+ // Ensure that the handle the SDK hands back up to the application layer for the
+ // OCDoResource call matches the found ClientCB Node.