/* * This is a function called back when a device is discovered */ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "\n<====Callback Context for DISCOVERY query " "received successfully====>"); } else { OC_LOG(ERROR, TAG, "\n<====Callback Context for DISCOVERY fail====>"); } if (clientResponse) { OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); OC_LOG_V(INFO, TAG, "Device Discovered %s \n @ %d.%d.%d.%d:%d\n", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); collectUniqueResource(clientResponse); } else { OC_LOG(ERROR, TAG, "<====DISCOVERY Callback fail to receive clientResponse====>\n"); } return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; }
OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { printf ("Callback Context for DISCOVER query recvd successfully\n"); } if (clientResponse) { printf ("StackResult: %s\n", getResult(clientResponse->result)); OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); printf ("Device =============> Discovered %s @ %d.%d.%d.%d:%d\n", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); parseClientResponse(clientResponse); // Init GET request to the server. printf ("\n\nExecuting %s\n", __func__); std::ostringstream query; query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource; InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0); } return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION; }
OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "<====Callback Context for POST received successfully====>"); } else { OC_LOG(ERROR, TAG, "<====Callback Context for POST fail====>"); } if(clientResponse) { OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); OC_LOG_V(INFO, TAG,"POST Response: %s \nFrom %d.%d.%d.%d:%d\n", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); } else { OC_LOG(ERROR, TAG, "<====POST Callback fail to receive clientResponse====>\n"); } return OC_STACK_DELETE_TRANSACTION; }
std::string getIPAddrTBServer(OCClientResponse * clientResponse) { if(!clientResponse) return ""; if(!clientResponse->addr) return ""; uint8_t a, b, c, d = 0; if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return ""; char ipaddr[16] = {'\0'}; snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf return std::string (ipaddr); }
OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "<====Callback Context for GET received successfully====>"); } else { OC_LOG(ERROR, TAG, "<====Callback Context for GET fail====>"); } if (clientResponse) { OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); OC_LOG_V(INFO, TAG,"Get Response: %s \nFrom %d.%d.%d.%d:%d\n", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); if (clientResponse->rcvdVendorSpecificHeaderOptions && clientResponse->numRcvdVendorSpecificHeaderOptions) { OC_LOG (INFO, TAG, "Received vendor specific options"); uint8_t i = 0; OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions; for (i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++) { if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID) { OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with", ((OCHeaderOption)rcvdOptions[i]).optionID ); OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData, MAX_HEADER_OPTION_DATA_LENGTH); } } } } else { OC_LOG(ERROR, TAG, "<====GET Callback fail to receive clientResponse====>\n"); } return OC_STACK_DELETE_TRANSACTION; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully"); } if (clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); parseClientResponse(clientResponse); switch(TEST_CASE) { case TEST_NON_CON_OP: InitGetRequest(OC_LOW_QOS); InitPutRequest(); InitPostRequest(OC_LOW_QOS); break; case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); InitPutRequest(); InitPostRequest(OC_HIGH_QOS); break; default: PrintUsage(); break; } } return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; }
const char * getIPAddr(const OCClientResponse * clientResponse) { uint8_t a, b, c, d; if(!clientResponse || 0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d)) { return ""; } char * ipaddr = NULL; if((ipaddr = (char *) OICCalloc(1, MAX_IP_ADDR_ST_SZ))) { snprintf(ipaddr, MAX_IP_ADDR_ST_SZ, "%d.%d.%d.%d", a,b,c,d); } else { OC_LOG(ERROR, TAG, "Memory not allocated to ipaddr"); } return ipaddr; }
//discovery callback OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*) CTX_VAL) { OC_LOG_V(INFO, TAG, "Callback Context for DISCOVER query recvd successfully"); } if(clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); if(clientResponse->resJSONPayload !=NULL) { OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); //DM client char addr[MAX_URI_LEN]=""; snprintf(addr, sizeof(addr), "coap://%d.%d.%d.%d:%d", remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); dmc_resource( addr, clientResponse ); } else { OC_LOG_V(INFO, TAG, " response NULL"); } } return OC_STACK_KEEP_TRANSACTION ; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully"); } if (clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); std::string connectionType = getConnectivityType (clientResponse->connType); OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str()); OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); parseClientResponse(clientResponse); switch(TEST_CASE) { case TEST_GET_REQ_NON: InitGetRequest(OC_LOW_QOS, 0, 0); break; case TEST_GET_REQ_NON_WITH_FILTERS: InitGetRequest(OC_LOW_QOS, 0, 1); break; case TEST_PUT_REQ_NON: InitPutRequest(OC_LOW_QOS); break; case TEST_POST_REQ_NON: InitPostRequest(OC_LOW_QOS); break; case TEST_DELETE_REQ_NON: InitDeleteRequest(OC_LOW_QOS); break; case TEST_OBS_REQ_NON: case TEST_OBS_REQ_NON_CANCEL_IMM: InitObserveRequest(OC_LOW_QOS); break; case TEST_GET_UNAVAILABLE_RES_REQ_NON: InitGetRequestToUnavailableResource(OC_LOW_QOS); break; case TEST_GET_REQ_CON: InitGetRequest(OC_HIGH_QOS, 0, 0); break; case TEST_POST_REQ_CON: InitPostRequest(OC_HIGH_QOS); break; case TEST_DELETE_REQ_CON: InitDeleteRequest(OC_HIGH_QOS); break; case TEST_OBS_REQ_CON: InitObserveRequest(OC_HIGH_QOS); break; #ifdef WITH_PRESENCE case TEST_OBS_PRESENCE: case TEST_OBS_PRESENCE_WITH_FILTER: case TEST_OBS_PRESENCE_WITH_FILTERS: case TEST_OBS_MULTICAST_PRESENCE: InitPresence(); break; #endif case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS: InitGetRequest(OC_LOW_QOS, 1, 0); break; case TEST_DISCOVER_DEV_REQ: InitDeviceDiscovery(OC_LOW_QOS); break; default: PrintUsage(); break; } } else { OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse"); } return OC_STACK_KEEP_TRANSACTION; }
//This function is called back by libcoap when a response is received static void HandleCoAPResponses(struct coap_context_t *ctx, const coap_queue_t * rcvdResponse) { OCResponse * response = NULL; OCCoAPToken rcvdToken; OCClientResponse clientResponse = {0}; ClientCB * cbNode = NULL; unsigned char bufRes[MAX_RESPONSE_LENGTH] = {0}; uint32_t sequenceNumber = OC_OBSERVE_NO_OPTION; uint32_t maxAge = 0; OCStackResult result = OC_STACK_ERROR; coap_pdu_t * sendPdu = NULL; coap_pdu_t * recvPdu = NULL; unsigned char rcvdUri[MAX_URI_LENGTH] = { 0 }; uint8_t isObserveNotification = 0; #ifdef WITH_PRESENCE char * resourceTypeName = NULL; uint8_t remoteIpAddr[4]; uint16_t remotePortNu; unsigned char fullUri[MAX_URI_LENGTH] = { 0 }; uint8_t isPresenceNotification = 0; uint8_t isMulticastPresence = 0; uint32_t lowerBound; uint32_t higherBound; char * tok = NULL; #endif coap_block_t rcvdBlock1 = {COAP_BLOCK_FILL_VALUE}; coap_block_t rcvdBlock2 = {COAP_BLOCK_FILL_VALUE}; uint16_t rcvdSize2 = 0; VERIFY_NON_NULL(ctx); VERIFY_NON_NULL(rcvdResponse); recvPdu = rcvdResponse->pdu; result = ParseCoAPPdu(recvPdu, rcvdUri, NULL, &sequenceNumber, &maxAge, &clientResponse.numRcvdVendorSpecificHeaderOptions, clientResponse.rcvdVendorSpecificHeaderOptions, &rcvdBlock1, &rcvdBlock2, NULL, &rcvdSize2, bufRes); VERIFY_SUCCESS(result, OC_STACK_OK); OC_LOG_V(DEBUG, TAG, "The sequenceNumber/NONCE of this response %u", sequenceNumber); OC_LOG_V(DEBUG, TAG, "The maxAge/TTL of this response %u", maxAge); OC_LOG_V(DEBUG, TAG, "The response received is %s", bufRes); if(sequenceNumber >= OC_OFFSET_SEQUENCE_NUMBER) { isObserveNotification = 1; OC_LOG(INFO, TAG, PCF("Received an observe notification")); } #ifdef WITH_PRESENCE if(!strcmp((char *)rcvdUri, (char *)OC_PRESENCE_URI)){ char* tokSavePtr; isPresenceNotification = 1; OC_LOG(INFO, TAG, PCF("Received a presence notification")); tok = strtok_r((char *)bufRes, "[:]}", &tokSavePtr); bufRes[strlen((char *)bufRes)] = ':'; tok = strtok_r(NULL, "[:]}", &tokSavePtr); bufRes[strlen((char *)bufRes)] = ':'; VERIFY_NON_NULL(tok); sequenceNumber = (uint32_t )atol(tok); OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", sequenceNumber); tok = strtok_r(NULL, "[:]}", &tokSavePtr); VERIFY_NON_NULL(tok); maxAge = (uint32_t )atol(tok); OC_LOG_V(DEBUG, TAG, "The received TTL is %u", maxAge); tok = strtok_r(NULL, "[:]}", &tokSavePtr); if(tok) { bufRes[strlen((char *)bufRes)] = ':'; resourceTypeName = (char *)OCMalloc(strlen(tok)); if(!resourceTypeName) { goto exit; } strcpy(resourceTypeName, tok); OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s", resourceTypeName); } bufRes[strlen((char *)bufRes)] = ']'; } #endif // fill OCCoAPToken structure RetrieveOCCoAPToken(recvPdu, &rcvdToken); OC_LOG_V(INFO, TAG,"Received a pdu with Token", rcvdToken.tokenLength); OC_LOG_BUFFER(INFO, TAG, rcvdToken.token, rcvdToken.tokenLength); // fill OCClientResponse structure result = FormOCClientResponse(&clientResponse, CoAPToOCResponseCode(recvPdu->hdr->code), (OCDevAddr *) &(rcvdResponse->remote), sequenceNumber, bufRes); VERIFY_SUCCESS(result, OC_STACK_OK); cbNode = GetClientCB(&rcvdToken, NULL, NULL); #ifdef WITH_PRESENCE // Check if the application subscribed for presence if(!cbNode) { // get the address of the remote OCDevAddrToIPv4Addr((OCDevAddr *) &(rcvdResponse->remote), remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) &(rcvdResponse->remote), &remotePortNu); snprintf((char *)fullUri, sizeof(fullUri), "coap://%d.%d.%d.%d:%d%s", remoteIpAddr[0],remoteIpAddr[1],remoteIpAddr[2],remoteIpAddr[3], remotePortNu,rcvdUri); cbNode = GetClientCB(NULL, NULL, fullUri); } // Check if application subscribed for multicast presence if(!cbNode) { snprintf((char *)fullUri, sizeof(fullUri), "%s%s", OC_MULTICAST_IP, rcvdUri); cbNode = GetClientCB(NULL, NULL, fullUri); isMulticastPresence = 1; isPresenceNotification = 0; } #endif // fill OCResponse structure result = FormOCResponse(&response, cbNode, maxAge, &clientResponse); VERIFY_SUCCESS(result, OC_STACK_OK); if(cbNode) { if(!isObserveNotification) { #ifdef WITH_PRESENCE if(!isPresenceNotification) { #endif OC_LOG(INFO, TAG, PCF("Received a regular response")); if(recvPdu->hdr->type == COAP_MESSAGE_CON) { sendPdu = GenerateCoAPPdu(COAP_MESSAGE_ACK, 0, recvPdu->hdr->id, NULL, NULL, NULL); VERIFY_NON_NULL(sendPdu); result = SendCoAPPdu(gCoAPCtx, (coap_address_t*) &rcvdResponse->remote, sendPdu, (coap_send_flags_t)(rcvdResponse->secure ? SEND_SECURE_PORT : 0)); } #ifdef WITH_PRESENCE } #endif } if(isObserveNotification) { OC_LOG(INFO, TAG, PCF("Received an observe notification")); if(recvPdu->hdr->type == COAP_MESSAGE_CON) { sendPdu = GenerateCoAPPdu(COAP_MESSAGE_ACK, 0, recvPdu->hdr->id, NULL, NULL, NULL); VERIFY_NON_NULL(sendPdu); result = SendCoAPPdu(gCoAPCtx, (coap_address_t*) &rcvdResponse->remote, sendPdu, (coap_send_flags_t)(rcvdResponse->secure ? SEND_SECURE_PORT : 0)); } //TODO: check the standard for methods to detect wrap around condition if(cbNode->method == OC_REST_OBSERVE && (clientResponse.sequenceNumber <= cbNode->sequenceNumber || (clientResponse.sequenceNumber > cbNode->sequenceNumber && clientResponse.sequenceNumber == (MAX_SEQUENCE_NUMBER)))) { OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \ Ignoring Incoming:%d Against Current:%d.", clientResponse.sequenceNumber, cbNode->sequenceNumber); goto exit; } if(clientResponse.sequenceNumber > cbNode->sequenceNumber){ cbNode->sequenceNumber = clientResponse.sequenceNumber; } } else { #ifdef WITH_PRESENCE if(isPresenceNotification)
// This is a function called back when a homegateway is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { char resource_addr[64] = {0}; char register_addr[64] = {0}; char stringAddress[DEV_ADDR_SIZE_MAX]; if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { Serial.println("gw discovered correctly"); } if (clientResponse) { if (clientResponse->result == OC_STACK_OK) { Serial.println("clientResponse ok"); OCStackResult ret; OCDoHandle handle; cJSON *root = cJSON_CreateObject(); cJSON *rep = cJSON_CreateArray(); cJSON *format_rep, *format; char *jsonPayload; IPAddress ip = Ethernet.localIP(); snprintf(resource_addr, sizeof(resource_addr), "coap://%d.%d.%d.%d:5683/oc/core?rt=com.intel", ip[0], ip[1], ip[2], ip[3]); cJSON_AddItemToObject(root, "oc", rep); cJSON_AddItemToArray(rep, format_rep=cJSON_CreateObject()); cJSON_AddItemToObject(format_rep, "rep", format=cJSON_CreateObject()); cJSON_AddStringToObject(format, "name", (char *)"fan"); cJSON_AddStringToObject(format, "address", (char *)resource_addr); jsonPayload = cJSON_Print(root); cJSON_Delete(root); Serial.println(jsonPayload); OCCallbackData cbData; cbData.cb = registerReqCB; cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; uint8_t a,b,c,d; OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d); snprintf(register_addr, sizeof(register_addr), "coap://%d.%d.%d.%d:5683/gw/sensor", a, b, c, d); ret = OCDoResource(&handle, OC_REST_PUT, register_addr, 0, jsonPayload, OC_IPV4, OC_LOW_QOS, &cbData, NULL, 0); if (ret != OC_STACK_OK) { Serial.println("register error"); Serial.println(ret); } free(jsonPayload); } } return OC_STACK_DELETE_TRANSACTION; }