static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result) { OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN"); #ifndef SINGLE_THREAD CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); if (!cadata) { OIC_LOG(ERROR, TAG, "memory allocation failed"); return; } CAEndpoint_t* ep = CACloneEndpoint(endpoint); if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); OICFree(cadata); return; } CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t)); if (!errorInfo) { OICFree(cadata); CAFreeEndpoint(ep); return; } CAResult_t res = CACloneInfo(info, &errorInfo->info); if (CA_STATUS_OK != res) { OICFree(cadata); OICFree(errorInfo); CAFreeEndpoint(ep); return; } errorInfo->result = result; cadata->remoteEndpoint = ep; cadata->errorInfo = errorInfo; cadata->dataType = CA_ERROR_DATA; CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); #endif OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT"); }
void CAErrorHandler(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen, CAResult_t result) { OIC_LOG(DEBUG, TAG, "CAErrorHandler IN"); #ifndef SINGLE_THREAD VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint"); VERIFY_NON_NULL_VOID(data, TAG, "data"); uint32_t code = CA_NOT_FOUND; //Do not free remoteEndpoint and data. Currently they will be freed in data thread //Get PDU data coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "Parse PDU failed"); return; } CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA); if(!cadata) { OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!"); coap_delete_pdu(pdu); return; } cadata->errorInfo->result = result; CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); coap_delete_pdu(pdu); #endif OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT"); return; }
CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const uint8_t *data, uint32_t dataLength, uint32_t *sentLength) { OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterSendData"); if (false == g_adapterState) { OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!"); *sentLength = 0; return CA_ADAPTER_NOT_ENABLED; } // Input validation VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null"); VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null"); VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null"); // Create remote endpoint CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_RFCOMM_BTEDR, remoteAddress, 0); if (NULL == remoteEndpoint) { OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !"); return CA_MEMORY_ALLOC_FAILED; } // Add message to data queue CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength); CAQueueingThreadAddData(g_sendQueueHandle, edrData, sizeof (CAEDRData)); *sentLength = dataLength; // Free remote endpoint CAFreeEndpoint(remoteEndpoint); OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CAAdapterSendData"); return CA_STATUS_OK; }
static int32_t CAQueueIPData(bool isMulticast, const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_RET(endpoint, TAG, "remoteEndpoint", -1); VERIFY_NON_NULL_RET(data, TAG, "data", -1); if (0 == dataLength) { OIC_LOG(ERROR, TAG, "Invalid Data Length"); return -1; } #ifdef SINGLE_THREAD CAIPSendData(endpoint, data, dataLength, isMulticast); return dataLength; #else VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1); // Create IPData to add to queue CAIPData_t *ipData = CACreateIPData(endpoint, data, dataLength, isMulticast); if (!ipData) { OIC_LOG(ERROR, TAG, "Failed to create ipData!"); return -1; } // Add message to send queue CAQueueingThreadAddData(g_sendQueueHandle, ipData, sizeof(CAIPData_t)); #endif // SINGLE_THREAD OIC_LOG(DEBUG, TAG, "OUT"); return dataLength; }
void CAAdapterRecvData(const char *remoteAddress, const uint8_t *data, uint32_t dataLength, uint32_t *sentLength) { OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); if (false == g_adapterState) { OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!"); *sentLength = 0; return; } // Input validation VERIFY_NON_NULL_VOID(data, EDR_ADAPTER_TAG, "Data is null"); VERIFY_NON_NULL_VOID(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null"); // Create remote endpoint CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_RFCOMM_BTEDR, remoteAddress, 0); if (NULL == remoteEndpoint) { OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !"); return; } // Add message to data queue CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength); CAQueueingThreadAddData(g_recvQueueHandle, edrData, sizeof(CAEDRData)); *sentLength = dataLength; // Free remote endpoint CAFreeEndpoint(remoteEndpoint); OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); }
CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request) { VERIFY_NON_NULL(object, TAG, "object"); VERIFY_NON_NULL(request, TAG, "request"); if (false == CAIsSelectedNetworkAvailable()) { return CA_STATUS_FAILED; } #ifdef ARDUINO // If max retransmission queue is reached, then don't handle new request if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList)) { OIC_LOG(ERROR, TAG, "max RT queue size reached!"); return CA_SEND_FAILED; } #endif /* ARDUINO */ CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA); if(!data) { OIC_LOG(ERROR, TAG, "CAPrepareSendData failed"); return CA_MEMORY_ALLOC_FAILED; } #ifdef SINGLE_THREAD CAResult_t result = CAProcessSendData(data); if(CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAProcessSendData failed"); return result; } CADestroyData(data, sizeof(CAData_t)); #else #ifdef WITH_BWT if (CA_ADAPTER_GATT_BTLE != object->adapter #ifdef TCP_ADAPTER && CA_ADAPTER_TCP != object->adapter #endif ) { // send block data CAResult_t res = CASendBlockWiseData(data); if(CA_NOT_SUPPORTED == res) { OIC_LOG(DEBUG, TAG, "normal msg will be sent"); CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t)); return CA_STATUS_OK; } else { CADestroyData(data, sizeof(CAData_t)); } return res; } else #endif { CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t)); } #endif return CA_STATUS_OK; }
static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLen) { VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint"); VERIFY_NON_NULL_VOID(data, TAG, "data"); OIC_LOG(DEBUG, TAG, "received pdu data :"); OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen); uint32_t code = CA_NOT_FOUND; CAData_t *cadata = NULL; coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code, &(sep->endpoint)); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "Parse PDU failed"); return; } OIC_LOG_V(DEBUG, TAG, "code = %d", code); if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA); if (!cadata) { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!"); coap_delete_pdu(pdu); return; } } else { cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA); if (!cadata) { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!"); coap_delete_pdu(pdu); return; } #ifdef TCP_ADAPTER if (CA_ADAPTER_TCP == sep->endpoint.adapter) { OIC_LOG(INFO, TAG, "retransmission is not supported"); } else #endif { // for retransmission void *retransmissionPdu = NULL; CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr, pdu->length, &retransmissionPdu); // get token from saved data in retransmission list if (retransmissionPdu && CA_EMPTY == code) { if (cadata->responseInfo) { CAInfo_t *info = &cadata->responseInfo->info; CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu, info, &(sep->endpoint)); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list"); OICFree(info->token); info->tokenLength = 0; } } } OICFree(retransmissionPdu); } } cadata->type = SEND_TYPE_UNICAST; #ifdef SINGLE_THREAD CAProcessReceivedData(cadata); #else #ifdef WITH_BWT if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter #ifdef TCP_ADAPTER && CA_ADAPTER_TCP != sep->endpoint.adapter #endif ) { CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen); if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res) { OIC_LOG(ERROR, TAG, "this message does not have block option"); CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); } else { CADestroyData(cadata, sizeof(CAData_t)); } } else #endif { CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); } #endif coap_delete_pdu(pdu); }