+OCStackResult OCToCAConnectivityType(OCConnectivityType ocConType, CAConnectivityType_t* caConType)
+{
+ OCStackResult ret = OC_STACK_OK;
+
+ switch(ocConType)
+ {
+ case OC_ETHERNET:
+ *caConType = CA_ETHERNET;
+ break;
+ case OC_WIFI:
+ *caConType = CA_WIFI;
+ break;
+ case OC_EDR:
+ *caConType = CA_EDR;
+ break;
+ case OC_LE:
+ *caConType = CA_LE;
+ break;
+ case OC_ALL:
+ // Currently OC_ALL represents WIFI and ETHERNET
+ // Add other connectivity types as they are enabled in future
+ *caConType = (CAConnectivityType_t) (CA_WIFI|CA_ETHERNET);
+ break;
+ default:
+ ret = OC_STACK_INVALID_PARAM;
+ break;
+ }
+ return ret;
+}
+
+OCStackResult CAToOCConnectivityType(CAConnectivityType_t caConType, OCConnectivityType *ocConType)
+{
+ OCStackResult ret = OC_STACK_OK;
+
+ switch(caConType)
+ {
+ case CA_ETHERNET:
+ *ocConType = OC_ETHERNET;
+ break;
+ case CA_WIFI:
+ *ocConType = OC_WIFI;
+ break;
+ case CA_EDR:
+ *ocConType = OC_EDR;
+ break;
+ case CA_LE:
+ *ocConType = OC_LE;
+ break;
+ default:
+ ret = OC_STACK_INVALID_PARAM;
+ break;
+ }
+ return ret;
+}
+
+// update response.addr appropriately from endPoint.addressInfo
+OCStackResult UpdateResponseAddr(OCClientResponse *response, const CARemoteEndpoint_t* endPoint)
+{
+ OCStackResult ret = OC_STACK_ERROR;
+ static OCDevAddr address = {0};
+ char * tok = NULL;
+ char * savePtr = NULL;
+ char * cpAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
+ if(!cpAddress)
+ {
+ ret = OC_STACK_NO_MEMORY;
+ goto exit;
+ }
+ memcpy(cpAddress, endPoint->addressInfo.IP.ipAddress,
+ strlen(endPoint->addressInfo.IP.ipAddress) + 1);
+
+ // Grabs the first three numbers from the IPv4 address and replaces dots
+ for(int i=0; i<4; i++)
+ {
+ tok = strtok_r(i==0 ? cpAddress : NULL, ".", &savePtr);
+
+ if(!tok)
+ {
+ ret = OC_STACK_ERROR;
+ goto exit;
+ }
+ address.addr[i] = atoi(tok);
+ }
+
+ memcpy(&address.addr[4], &endPoint->addressInfo.IP.port, sizeof(uint32_t));
+
+ if(response)
+ {
+ response->addr = &address;
+ ret = CAToOCConnectivityType(endPoint->connectivityType, &(response->connType));
+ }
+ else
+ {
+ OC_LOG(ERROR, TAG, PCF("OCClientResponse is NULL!"));
+ }
+exit:
+ OCFree(cpAddress);
+ return ret;
+}
+
+void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType)
+{
+ char * tok = NULL;
+ char * savePtr;
+ // The format of the payload is {"oc":[%u:%u:%s]}
+ // %u : sequence number,
+ // %u : max age
+ // %s : Resource Type (Optional)
+ tok = strtok_r(payload, "[:]}", &savePtr);
+ payload[strlen(payload)] = ':';
+ tok = strtok_r(NULL, "[:]}", &savePtr);
+ payload[strlen((char *)payload)] = ':';
+ *seqNum = (uint32_t) atoi(tok);
+ tok = strtok_r(NULL, "[:]}", &savePtr);
+ *maxAge = (uint32_t) atoi(tok);
+ tok = strtok_r(NULL, "[:]}",&savePtr);
+
+ if(tok)
+ {
+ *resType = (char *)OCMalloc(strlen(tok));
+ if(!*resType)
+ {
+ return;
+ }
+ payload[strlen((char *)payload)] = ':';
+ strcpy(*resType, tok);
+ OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s", *resType);
+ }
+ payload[strlen((char *)payload)] = ']';
+}
+
+OCStackResult HandlePresenceResponse(const CARemoteEndpoint_t* endPoint,
+ const CAResponseInfo_t* responseInfo)
+{
+ OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
+ ClientCB * cbNode = NULL;
+ char *resourceTypeName = NULL;
+ OCClientResponse response;
+ OCStackResult result = OC_STACK_ERROR;
+ uint32_t lowerBound = 0;
+ uint32_t higherBound = 0;
+ uint32_t maxAge = 0;
+
+ char *fullUri = NULL;
+ char *ipAddress = NULL;
+ int presenceSubscribe = 0;
+ int multicastPresenceSubscribe = 0;
+
+ if (responseInfo->result != CA_SUCCESS)
+ {
+ OC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
+ return OC_STACK_ERROR;
+ }
+
+ fullUri = (char *) OCMalloc(MAX_URI_LENGTH );
+
+ if(NULL == fullUri)
+ {
+ OC_LOG(ERROR, TAG, PCF("Memory could not be allocated for fullUri"));
+ result = OC_STACK_NO_MEMORY;
+ goto exit;
+ }
+
+ ipAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
+
+ if(NULL == ipAddress)
+ {
+ OC_LOG(ERROR, TAG, PCF("Memory could not be allocated for ipAddress"));
+ result = OC_STACK_NO_MEMORY;
+ goto exit;
+ }
+
+ strncpy(ipAddress, endPoint->addressInfo.IP.ipAddress,
+ strlen(endPoint->addressInfo.IP.ipAddress));
+ ipAddress[strlen(endPoint->addressInfo.IP.ipAddress)] = '\0';
+
+ snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port,
+ OC_PRESENCE_URI);
+
+ cbNode = GetClientCB(NULL, NULL, (unsigned char *) fullUri);
+
+ if(cbNode)
+ {
+ presenceSubscribe = 1;
+ }
+ else
+ {
+ snprintf(fullUri, MAX_URI_LENGTH, "%s%s", OC_MULTICAST_IP, endPoint->resourceUri);
+ cbNode = GetClientCB(NULL, NULL, (unsigned char *) fullUri);
+ if(cbNode)
+ {
+ multicastPresenceSubscribe = 1;
+ }
+ }
+
+ if(!presenceSubscribe && !multicastPresenceSubscribe)
+ {
+ OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
+ ------------ ignoring"));
+ goto exit;
+ }
+
+ // No payload to the application in case of presence
+ response.resJSONPayload = NULL;
+ response.result = OC_STACK_OK;
+
+ result = UpdateResponseAddr(&response, endPoint);
+ if(result != OC_STACK_OK)
+ {
+ goto exit;
+ }
+
+ if(responseInfo->info.payload)
+ {
+ parsePresencePayload(responseInfo->info.payload,
+ &(response.sequenceNumber),
+ &maxAge,
+ &resourceTypeName);
+ }
+
+ if(maxAge == 0)
+ {
+ OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
+ response.result = OC_STACK_PRESENCE_STOPPED;
+ if(cbNode->presence)
+ {
+ OCFree(cbNode->presence->timeOut);
+ OCFree(cbNode->presence);
+ cbNode->presence = NULL;
+ }
+ }
+ else if(presenceSubscribe)
+ {
+ if(!cbNode->presence)
+ {
+ cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
+ VERIFY_NON_NULL_V(cbNode->presence);
+ cbNode->presence->timeOut = NULL;
+ cbNode->presence->timeOut = (uint32_t *)
+ OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
+ if(!(cbNode->presence->timeOut)){
+ OCFree(cbNode->presence);
+ result = OC_STACK_NO_MEMORY;
+ }
+ }
+
+ OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %u", GetTime(0));
+ cbNode->presence->TTL = maxAge;
+ for(int index = 0; index < PresenceTimeOutSize; index++)
+ {
+ lowerBound = GetTime(((float)(PresenceTimeOut[index])
+ /(float)100)*(float)cbNode->presence->TTL);
+ higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
+ /(float)100)*(float)cbNode->presence->TTL);
+ cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
+ OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
+ OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
+ OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
+ cbNode->presence->timeOut[index]);
+ }
+ cbNode->presence->TTLlevel = 0;
+ OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
+ if(cbNode->sequenceNumber == response.sequenceNumber)
+ {
+ OC_LOG(INFO, TAG, PCF("===============No presence change"));
+ goto exit;
+ }
+ OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
+ cbNode->sequenceNumber = response.sequenceNumber;
+
+ // Ensure that a filter is actually applied.
+ if(resourceTypeName && cbNode->filterResourceType)
+ {
+ if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
+ {
+ goto exit;
+ }
+ }
+ }
+ else
+ {
+ // This is the multicast case
+
+ OCMulticastNode* mcNode = NULL;
+ mcNode = GetMCPresenceNode((const unsigned char *)fullUri);
+
+ if(mcNode != NULL)
+ {
+ if(mcNode->nonce == response.sequenceNumber)
+ {
+ OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
+ goto exit;
+ }
+ mcNode->nonce = response.sequenceNumber;
+ }
+ else
+ {
+ uint32_t uriLen = strlen((char*)fullUri);
+ unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
+ if(uri)
+ {
+ memcpy(uri, fullUri, (uriLen + 1));
+ }
+ else
+ {
+ OC_LOG(INFO, TAG,
+ PCF("===============No Memory for URI to store in the presence node"));
+ result = OC_STACK_NO_MEMORY;
+ goto exit;
+ }
+ result = AddMCPresenceNode(&mcNode, (unsigned char*) uri, response.sequenceNumber);
+ if(result == OC_STACK_NO_MEMORY)
+ {
+ OC_LOG(INFO, TAG,
+ PCF("===============No Memory for Multicast Presence Node"));
+ result = OC_STACK_NO_MEMORY;
+ goto exit;
+ }
+ }
+
+ // Ensure that a filter is actually applied.
+ if(resourceTypeName && cbNode->filterResourceType)
+ {
+ if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
+ {
+ goto exit;
+ }
+ }
+ }
+
+ cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
+
+ if (cbResult == OC_STACK_DELETE_TRANSACTION)
+ {
+ FindAndDeleteClientCB(cbNode);
+ }
+
+exit:
+OCFree(fullUri);
+OCFree(ipAddress);
+OCFree(resourceTypeName);
+return result;
+}
+
+