* if message id is zero, it will generated by CA inside.
* otherwise, you can use it */
CAToken_t token; /**< Token for CA */
+ uint8_t tokenLength; /**< token length*/
CAHeaderOption_t *options; /** Header Options for the request */
uint8_t numOptions; /**< Number of Header options */
CAPayload_t payload; /**< payload of the request */
/**
* @brief Generating the token for matching the request and response.
- * @param token [OUT] Token for the request
+ * @param token [OUT] Token for the request
+ * @param tokenLength [IN] length of the token
* @return #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
+ * or #CA_STATUS_NOT_INITIALIZED
* @remark Token memory is destroyed by the caller using CADestroyToken().
* @see CADestroyToken
*/
-CAResult_t CAGenerateToken(CAToken_t *token);
+CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength);
/**
* @brief Destroy the token generated by CAGenerateToken
- * @param token [IN] Token for the request
+ * @param token [IN] token to be freed
* @return NONE
*/
void CADestroyToken(CAToken_t token);
* @param resourceUri [IN] Uri to send multicast search request. Must contain only relative
* path of Uri to be search.
* @param token [IN] Token for the request
- * @return #CA_STATUS_OK or #CA_STATUS_FAILED
+ * @param tokenLength [IN] length of the token
+ * @return #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_STATUS_NOT_INITIALIZED
*/
-CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token);
+CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength);
/**
* @brief Send control Request on a resource
/**
* @brief To advertise the resource
- * @param resourceUri [IN] URI to be advertised
- * @param token [IN] Token for the request
- * @param options [IN] Header options information
- * @param numOptions [IN] Number of options
- * @return #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
+ * @param resourceUri [IN] URI to be advertised
+ * @param token [IN] Token for the request
+ * @param tokenLength [IN] length of the token
+ * @param options [IN] Header options information
+ * @param numOptions [IN] Number of options
+ * @return #CA_STATUS_OK or #CA_STATUS_FAILED or
+ * #CA_MEMORY_ALLOC_FAILED or #CA_STATUS_NOT_INITIALIZED
*/
CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token,
- const CAHeaderOption_t *options,const uint8_t numOptions);
+ uint8_t tokenLength, const CAHeaderOption_t *options,
+ const uint8_t numOptions);
/**
* @brief Select network to use
* @brief Detaches control from the caller for sending request
* @param resourceUri [IN] resource uri that needs to be sent in the request
* @param token [IN] token information of the request
+ * @param tokenLength [IN] length of the token
* @param options [IN] header options that need to be append in the request
* @param numOptions [IN] number of options be appended
- * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
+ * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
*/
CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
- const CAHeaderOption_t *options, const uint8_t numOptions);
+ uint8_t tokenLength, const CAHeaderOption_t *options,
+ uint8_t numOptions);
/**
* @brief Setting the request and response callbacks for network packets
/**
* @brief Detaches control from the caller for sending request
- * @param resourceUri [IN] resource uri that needs to be sent in the request
- * @param token [IN] token information of the request
- * @param options [IN] header options that need to be append in the request
- * @param numOptions [IN] number of options be appended
- * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
+ * @param resourceUri [IN] resource uri that needs to be sent in the request
+ * @param token [IN] token information of the request
+ * @param tokenLength [IN] length of the token
+ * @param options [IN] header options that need to be append in the request
+ * @param numOptions [IN] number of options be appended
+ * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
*/
CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
- const CAHeaderOption_t *options, uint8_t numOptions);
+ uint8_t tokenLength, const CAHeaderOption_t *options,
+ uint8_t numOptions);
/**
* @brief Setting the request and response callbacks for network packets
coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode);
/**
- * @brief generates the token
- * @param token [OUT] generated token
+ * @brief generates the token
+ * @param token [OUT] generated token
+ * @param tokenLength [IN] length of the token
* @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
*/
-
-CAResult_t CAGenerateTokenInternal(CAToken_t *token);
+CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
/**
* @brief destroys the token
coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode);
/**
- * @brief generates the token
- * @param token [OUT] generated token
+ * @brief generates the token
+ * @param token [OUT] generated token
+ * @param tokenLength [IN] length of the token
* @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
*/
-
-CAResult_t CAGenerateTokenInternal(CAToken_t *token);
+CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
/**
* @brief destroys the token
CAConnectivityType_t g_selectedNwType = CA_WIFI;
static CAToken_t g_lastRequestToken = NULL;
+static uint8_t g_lastRequestTokenLength;
static const char SECURE_COAPS_PREFIX[] = "coaps://";
static const char SECURE_INFO_DATA[]
= "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
static jobject g_responseListenerObject = NULL;
extern JavaVM *g_jvm;
+static CAToken_t g_clientToken;
+static uint8_t g_clientTokenLength = NULL;
// init
JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_setNativeResponseListener
(JNIEnv *env, jobject obj, jobject listener){
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- LOGI("token generate error!!\n");
- token = NULL;
+ LOGE("token generate error!!");
return;
}
- LOGI("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
+
+ res = CAFindResource((const CAURI_t) strUri, token, tokenLength);
- if(CA_STATUS_OK != CAFindResource((const CAURI_t)strUri, token))
+ if (res != CA_STATUS_OK)
{
- LOGI("Could not find resource");
+ LOGE("Could not find resource");
+ //destroy token
+ CADestroyToken(token);
}
else
{
LOGI("find resource to %s URI", strUri);
+ CADestroyToken(g_lastRequestToken);
g_lastRequestToken = token;
+ g_lastRequestTokenLength = tokenLength;
}
}
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- LOGI("token generate error!!\n");
- token = NULL;
- // destroy token
- CADestroyToken(token);
+ LOGE("token generate error!!");
// destroy remote endpoint
CADestroyRemoteEndpoint(endpoint);
return;
CAInfo_t requestData = {0};
requestData.token = token;
+ requestData.tokenLength = tokenLength;
const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL);
if (isSecured == 1)
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- LOGI("token generate error!");
- token = NULL;
+ LOGE("token generate error!");
+ CADestroyRemoteEndpoint(endpoint);
}
char resourceURI[15] = {0};
}
}
- if (g_lastRequestToken != NULL && requestInfo->info.token != NULL
- && (strcmp((char *)g_lastRequestToken, requestInfo->info.token) == 0))
+ if ((!g_lastRequestToken) && (!requestInfo->info.token) &&
+ (strncmp(g_lastRequestToken, requestInfo->info.token,
+ requestInfo->info.tokenLength) == 0))
{
LOGI("token is same. received request of it's own. skip.. \n");
return;
}
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK || token == NULL)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if (res != CA_STATUS_OK || (!token))
{
Serial.println("token error");
return;
Serial.print("token:");
Serial.println(token);
- res = CAFindResource(buf, token);
+ res = CAFindResource(buf, token, tokenLength);
if (res != CA_STATUS_OK)
{
Serial.print("find error: ");
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if (res != CA_STATUS_OK || (!token))
{
Serial.println("token error");
- token = NULL;
return;
}
- Serial.println((token != NULL) ? token : "");
+ Serial.println(token);
CAInfo_t requestData = {CA_MSG_RESET};
requestData.token = token;
+ requestData.tokenLength = tokenLength;
requestData.payload = (CAPayload_t)"Json Payload";
requestData.type = msgType;
// send request
CASendRequest(endpoint, &requestInfo);
- if (token != NULL)
+ if (NULL != token)
{
CADestroyToken(token);
}
CADestroyRemoteEndpoint(endpoint);
}
+ CADestroyToken(token);
Serial.println("============");
}
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if (res != CA_STATUS_OK || (!token))
{
Serial.println("token error");
- token = NULL;
return;
}
- Serial.println((token != NULL) ? token : "");
+ Serial.println(token);
CAInfo_t requestData = {CA_MSG_RESET};
requestData.token = token;
+ requestData.tokenLength = tokenLength;
requestData.payload = "Temp Json Payload";
requestData.type = CA_MSG_NONCONFIRM;
// CASendRequest(endpoint, &requestInfo);
CASendRequestToAll(group, &requestInfo);
- if (token != NULL)
+ if (NULL != token)
{
CADestroyToken(token);
}
Serial.println("============");
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if (res != CA_STATUS_OK || (!token))
{
Serial.println("token error");
- token = NULL;
return;
}
Serial.println("token");
- Serial.println((token != NULL) ? token : "");
+ Serial.println(token);
- CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
+ CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t)optionNum);
OICFree(headerOpt);
CADestroyToken(token);
}
return;
}
- CAInfo_t respondeData = {CA_MSG_RESET};
- respondeData.token = (CAToken_t)"token";
- respondeData.payload = (CAPayload_t)"Notification Data";
+ // create token
+ CAToken_t token = NULL;
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if (res != CA_STATUS_OK || (!token))
+ {
+ Serial.println("token error");
+ return;
+ }
+
+ CAInfo_t respondData = {CA_MSG_NONCONFIRM};
+ respondData.token = token;
+ respondData.tokenLength = tokenLength;
+ respondData.payload = (CAPayload_t)"Notification Data";
CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}};
responseInfo.result = CA_SUCCESS;
- responseInfo.info = respondeData;
+ responseInfo.info = respondData;
// send request
CASendNotification(endpoint, &responseInfo);
{
CADestroyRemoteEndpoint(endpoint);
}
+
+ CADestroyToken(token);
Serial.println("============");
}
void SendResponse(CARemoteEndpoint_t *endpoint, const CAInfo_t* info)
{
+ char buf[MAX_BUF_LEN] = {0};
+
Serial.println("============");
CAInfo_t responseData = {CA_MSG_RESET};
if(info && info->type == CA_MSG_CONFIRM)
}
responseData.messageId = (info != NULL) ? info->messageId : 0;
- responseData.token = (info != NULL) ? (CAToken_t)info->token : (CAToken_t)"";
+ responseData.token = (info != NULL) ? (CAToken_t)info->token : NULL;
+ responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
responseData.payload = (CAPayload_t)"response payload";
CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}};
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error!!");
+ printf("Token generate error!!");
return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
- res = CAFindResource(buf, token);
- if (res != CA_STATUS_OK)
+ res = CAFindResource(buf, token, tokenLength);
+ if (CA_STATUS_OK != res)
{
- printf("find resource error : %d\n", res);
+ printf("Find resource error : %d\n", res);
}
else
{
- printf("find resource to %s URI\n", buf);
+ printf("Find resource to %s URI\n", buf);
}
// delete token
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error!!\n");
+ printf("Token generate error!!\n");
return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
- res = CAFindResource(buf, token);
- if (res != CA_STATUS_OK)
+ res = CAFindResource(buf, token, tokenLength);
+ if (CA_STATUS_OK != res)
{
- printf("find resource error : %d\n", res);
+ printf("Find resource error : %d\n", res);
+ CADestroyToken(token);
}
else
{
- printf("find resource to %s URI\n", buf);
-
- if (g_last_request_token != NULL)
- {
- CADestroyToken(g_last_request_token);
- }
-
+ printf("Find resource to %s URI\n", buf);
+ CADestroyToken(g_last_request_token);
g_last_request_token = token;
}
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
- if (CA_STATUS_OK != res)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error, error code : %d\n", res);
+ printf("Token generate error, error code : %d\n", res);
+ CADestroyRemoteEndpoint(endpoint);
return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
// extract relative resourceuri from give uri
char resourceURI[15] = {0};
// create request data
CAInfo_t requestData = { 0 };
requestData.token = token;
+ requestData.tokenLength = tokenLength;
+
if (strcmp(secureRequest, "1") == 0)
{
int length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
if (requestData.payload == NULL)
{
- printf("memory alloc fail\n");
+ printf("Memory allocation fail\n");
+ CADestroyRemoteEndpoint(endpoint);
+ CADestroyToken(token);
return;
}
snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_local_secure_port);
requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
if (requestData.payload == NULL)
{
- printf("memory alloc fail\n");
+ printf("Memory allocation fail\n");
+ CADestroyRemoteEndpoint(endpoint);
+ CADestroyToken(token);
return;
}
snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
// create token
CAToken_t token = NULL;
- if (CA_STATUS_OK != CAGenerateToken(&token))
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("Failed to generate token !\n");
+ printf("Token generate error, error code : %d\n", res);
goto exit;
}
+ printf("Generated token %s\n", token);
+
// create request data
CAMessageType_t msgType = CA_MSG_NONCONFIRM;
CAInfo_t requestData = { 0 };
requestData.token = token;
+ requestData.tokenLength = tokenLength;
requestData.type = msgType;
CARequestInfo_t requestInfo = { 0 };
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
- if (res != CA_STATUS_OK)
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error!!\n");
+ printf("Token generate error!!\n");
+ CADestroyRemoteEndpoint(endpoint);
+ free(group);
return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("generated token %s\n", token);
CAInfo_t requestData = { 0 };
requestData.token = token;
+ requestData.tokenLength = tokenLength;
requestData.payload = "Temp Json Payload";
requestData.type = CA_MSG_NONCONFIRM;
requestInfo.info = requestData;
// send request
- CASendRequestToAll(group, &requestInfo);
-
- CADestroyToken(token);
+ res = CASendRequestToAll(group, &requestInfo);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Could not send request to all\n");
+ CADestroyToken(token);
+ }
+ else
+ {
+ CADestroyToken(g_last_request_token);
+ g_last_request_token = token;
+ }
// destroy remote endpoint
CADestroyRemoteEndpoint(endpoint);
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error!!\n");
- token = NULL;
+ printf("Token generate error!!\n");
+ free(headerOpt);
return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
-
- CAAdvertiseResource(buf, token, headerOpt, (uint8_t) optionNum);
+ printf("Generated token %s\n", token);
- // delete token
- CADestroyToken(token);
+ res = CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t) optionNum);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Could not start advertise resource\n");
+ CADestroyToken(token);
+ }
+ else
+ {
+ CADestroyToken(g_last_request_token);
+ g_last_request_token = token;
+ }
free(headerOpt);
}
return;
}
+ // create token
+ CAToken_t token = NULL;
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
+ {
+ printf("Token generate error!!\n");
+ CADestroyRemoteEndpoint(endpoint);
+ return;
+ }
+
+ printf("Generated token %s\n", token);
CAInfo_t respondData = { 0 };
respondData.token = "client token";
respondData.payload = "Temp Notification Data";
printf("send notification success\n");
}
+ // destroy token
+ CADestroyToken(token);
// destroy remote endpoint
CADestroyRemoteEndpoint(endpoint);
printf("Data: %s\n", requestInfo->info.payload);
printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
- if (g_last_request_token != NULL && requestInfo->info.token != NULL
- && (strcmp((char *)g_last_request_token, requestInfo->info.token) == 0))
+ if ((!g_last_request_token) && (!requestInfo->info.token)
+ && (strncmp(g_last_request_token, requestInfo->info.token,
+ requestInfo->info.tokenLength) == 0))
{
printf("token is same. received request of it's own. skip.. \n");
return;
((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) :
CA_MSG_NONCONFIRM;
responseData.messageId = (info != NULL) ? info->messageId : 0;
- responseData.token = (info != NULL) ? info->token : "";
- responseData.payload = "response payload";
+ if(3 != messageType)
+ {
+ responseData.token = (info != NULL) ? info->token : NULL;
+ responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
+ responseData.payload = "response payload";
+ }
CAResponseInfo_t responseInfo = { 0 };
responseInfo.result = 203;
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
printf("token generate error!!");
- token = NULL;
+ return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
- res = CAFindResource(buf, token);
+ res = CAFindResource(buf, token, tokenLength);
if (res != CA_STATUS_OK)
{
- printf("find resource error!!\n");
+ printf("find resource error : %d\n", res);
}
else
{
printf("find resource to %s URI\n", buf);
}
- //delete token
- if (token)
- {
- CADestroyToken(token);
- }
+ // delete token
+ CADestroyToken(token);
printf("=============================================\n");
}
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
printf("token generate error!!\n");
- token = NULL;
+ return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
- res = CAFindResource(buf, token);
+ res = CAFindResource(buf, token, tokenLength);
if (res != CA_STATUS_OK)
{
- printf("find resource error!!\n");
+ printf("find resource error : %d\n", res);
+ CADestroyToken(token);
}
else
{
printf("find resource to %s URI\n", buf);
- if (g_lastRequestToken)
- {
- CADestroyToken(g_lastRequestToken);
- }
+ CADestroyToken(g_lastRequestToken);
g_lastRequestToken = token;
}
// create token
CAToken_t token = NULL;
- if (CA_STATUS_OK != CAGenerateToken(&token))
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error!!\n");
- token = NULL;
+ printf("token generate error, error code : %d\n", res);
+ return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
// extract relative resourceuri from give uri
char resourceURI[15] = {0};
// send request
CASendRequest(endpoint, &requestInfo);
- if (token)
- {
- CADestroyToken(token);
- }
+ CADestroyToken(token);
- if (requestData.payload)
- {
- free(requestData.payload);
- }
+ free(requestData.payload);
// destroy remote endpoint
CADestroyRemoteEndpoint(endpoint);
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
- if (res != CA_STATUS_OK)
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
printf("token generate error!!\n");
- token = NULL;
+ return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("generated token %s\n", token);
CAInfo_t requestData = {CA_MSG_RESET};
requestData.token = token;
+ requestData.tokenLength = tokenLength;
requestData.payload = "Temp Json Payload";
requestData.type = CA_MSG_NONCONFIRM;
requestInfo.info = requestData;
// send request
- // CASendRequest(endpoint, &requestInfo);
CASendRequestToAll(group, &requestInfo);
- if (token != NULL)
- {
- CADestroyToken(token);
- }
+ CADestroyToken(token);
// destroy remote endpoint
CADestroyRemoteEndpoint(endpoint);
// create token
CAToken_t token = NULL;
- CAResult_t res = CAGenerateToken(&token);
- if (res != CA_STATUS_OK)
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
printf("token generate error!!\n");
- token = NULL;
+ return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("generated token %s\n", token);
- CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
+ CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t)optionNum);
// delete token
- if (token)
- {
- CADestroyToken(token);
- }
+ CADestroyToken(token);
free(headerOpt);
-
}
void SendNotification()
return;
}
- CAInfo_t respondeData = {CA_MSG_RESET};
- respondeData.token = "client token";
- respondeData.payload = "Temp Notification Data";
+ CAInfo_t respondData = {CA_MSG_RESET};
+ respondData.tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&respondData.token, respondData.tokenLength);
+ if ((CA_STATUS_OK != res) || (!respondData.token))
+ {
+ printf("token generate error!!\n");
+ return;
+ }
+
+ respondData.payload = "Temp Notification Data";
CAResponseInfo_t responseInfo = {0};
responseInfo.result = CA_SUCCESS;
- responseInfo.info = respondeData;
+ responseInfo.info = respondData;
// send request
res = CASendNotification(endpoint, &responseInfo);
if (res != CA_STATUS_OK)
{
- printf("send notification error\n");
+ printf("send notification error, error code: %d\n", res);
}
else
{
printf("send notification success\n");
}
+ // delete token
+ CADestroyToken(respondData.token);
// destroy remote endpoint
- if (endpoint != NULL)
- {
- CADestroyRemoteEndpoint(endpoint);
- }
+ CADestroyRemoteEndpoint(endpoint);
printf("\n=============================================\n");
}
printf("response result : %d\n", responseInfo->result);
printf("Data: %s\n", responseInfo->info.payload);
printf("Message type: %s\n", g_messageType[responseInfo->info.type]);
+ printf("Token: %s\n", responseInfo->info.token);
if (responseInfo->info.options)
{
uint32_t len = responseInfo->info.numOptions;
{
responseData.messageId = info->messageId;
responseData.token = info->token;
+ responseData.tokenLength = info->tokenLength;
}
responseData.payload = "response payload";
printf("=============================================\n");
}
-void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token)
+void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token, uint8_t tokenLength)
{
printf("\n=============================================\n");
CAInfo_t requestData ={CA_MSG_RESET};
requestData.token = token;
+ requestData.tokenLength = tokenLength;
requestData.payload = "Temp Json Payload";
CARequestInfo_t requestInfo ={CA_GET, {CA_MSG_RESET}};
CADestroyRemoteEndpointInternal(rep);
}
-CAResult_t CAGenerateToken(CAToken_t *token)
+CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
{
OIC_LOG_V(DEBUG, TAG, "CAGenerateToken");
- return CAGenerateTokenInternal(token);
+ return CAGenerateTokenInternal(token, tokenLength);
}
void CADestroyToken(CAToken_t token)
return CAGetNetworkInformationInternal(info, size);
}
-CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token)
+CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength)
{
OIC_LOG_V(DEBUG, TAG, "CAFindResource");
- return CADetachMessageResourceUri(resourceUri, token, NULL, 0);
+ return CADetachMessageResourceUri(resourceUri, token, tokenLength, NULL, 0);
}
}
CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token,
- const CAHeaderOption_t *options,const uint8_t numOptions)
+ uint8_t tokenLength, const CAHeaderOption_t *options,
+ const uint8_t numOptions)
{
OIC_LOG_V(DEBUG, TAG, "CAAdvertiseResource");
- return CADetachMessageResourceUri(resourceUri, token, options, numOptions);
+ return CADetachMessageResourceUri(resourceUri, token, tokenLength, options, numOptions);
}
CADestroyRemoteEndpointInternal(rep);
}
-CAResult_t CAGenerateToken(CAToken_t *token)
+CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
{
- return CAGenerateTokenInternal(token);
+ return CAGenerateTokenInternal(token, tokenLength);
}
void CADestroyToken(CAToken_t token)
return CAGetNetworkInformationInternal(info, size);
}
-CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token)
+CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength)
{
OIC_LOG(DEBUG, TAG, "IN");
- return CADetachMessageResourceUri(resourceUri, token, NULL, 0);
-
+ return CADetachMessageResourceUri(resourceUri, token, tokenLength, NULL, 0);
}
CAResult_t CASendRequest(const CARemoteEndpoint_t *object,const CARequestInfo_t *requestInfo)
}
CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token,
- const CAHeaderOption_t *options,
+ uint8_t tokenLength, const CAHeaderOption_t *options,
const uint8_t numOptions)
{
- return CADetachMessageResourceUri(resourceUri, token, options, numOptions);
+ return CADetachMessageResourceUri(resourceUri, token, tokenLength, options, numOptions);
}
CAResult_t CASelectNetwork(const uint32_t interestedNetwork)
info.options = data->options;
info.numOptions = data->numOptions;
info.token = data->requestInfo->info.token;
+ info.tokenLength = data->requestInfo->info.tokenLength;
info.type = data->requestInfo->info.type;
pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info);
if (NULL != ReqInfo->info.token)
{
OIC_LOG(DEBUG, TAG, "Request- token:");
- OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
+ OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
+ ReqInfo->info.tokenLength);
}
if (NULL != endpoint)
}
CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
- const CAHeaderOption_t *options,const uint8_t numOptions)
+ uint8_t tokenLength, const CAHeaderOption_t *options,
+ uint8_t numOptions)
{
- if (resourceUri == NULL)
- {
- return CA_STATUS_FAILED;
- }
- CARequestInfo_t *ReqInfo = NULL;
- CAToken_t tempToken = NULL;
+ OIC_LOG(DEBUG, TAG, "IN");
+ VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
+ VERIFY_NON_NULL(token, TAG, "Token is NULL");
+
CARemoteEndpoint_t *remoteEndpoint = NULL;
+ CARequestInfo_t *reqInfo = NULL;
+ char *tempToken = NULL;
+ // allocate & initialize
CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
CA_MEMORY_ALLOC_CHECK(data);
CAAddress_t addr = {};
remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
- CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
+ CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
// create request info
- ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
- CA_MEMORY_ALLOC_CHECK(ReqInfo);
+ reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
+ CA_MEMORY_ALLOC_CHECK(reqInfo);
- // copy token value
- if (token != NULL)
+ if (tokenLength)
{
- int32_t len = strlen(token);
- tempToken = (char *) OICCalloc((len + 1), sizeof(char));
+ // copy token value
+ tempToken = (char *) OICMalloc(tokenLength);
CA_MEMORY_ALLOC_CHECK(tempToken);
- strncpy(tempToken, token, len);
+ memcpy(tempToken, token, tokenLength);
}
// save request info data
- ReqInfo->method = CA_GET;
- ReqInfo->info.token = tempToken;
- ReqInfo->info.type = CA_MSG_NONCONFIRM;
+ reqInfo->method = CA_GET;
+ reqInfo->info.type = CA_MSG_NONCONFIRM;
+
+ reqInfo->info.token = tempToken;
+ reqInfo->info.tokenLength = tokenLength;
+
// save data
data->type = SEND_TYPE_MULTICAST;
data->remoteEndpoint = remoteEndpoint;
- data->requestInfo = ReqInfo;
+ data->requestInfo = reqInfo;
data->responseInfo = NULL;
data->options = NULL;
CADestroyRemoteEndpointInternal(remoteEndpoint);
- if (tempToken != NULL)
- {
- OICFree(tempToken);
- }
-
- if (ReqInfo != NULL)
- {
- OICFree(ReqInfo);
- }
-
- if (data != NULL)
- {
- OICFree(data);
- }
-
+ OICFree(tempToken);
+ OICFree(reqInfo);
+ OICFree(data);
+ OIC_LOG(DEBUG, TAG, "OUT");
return CA_MEMORY_ALLOC_FAILED;
}
info.options = data->options;
info.numOptions = data->numOptions;
info.token = data->requestInfo->info.token;
+ info.tokenLength = data->requestInfo->info.tokenLength;
info.type = data->requestInfo->info.type;
pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info);
}
CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
- const CAHeaderOption_t *options,
+ uint8_t tokenLength, const CAHeaderOption_t *options,
uint8_t numOptions)
{
OIC_LOG(DEBUG, TAG, "IN");
- if (resourceUri == NULL)
- {
- return CA_STATUS_FAILED;
- }
-
- CARemoteEndpoint_t *remoteEndpoint = NULL;
+ VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
+ VERIFY_NON_NULL(token, TAG, "Token is NULL");
// allocate & initialize
CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
CA_MEMORY_ALLOC_CHECK(data);
CAAddress_t addr = {0};
- remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
- CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
+ CARemoteEndpoint_t *remoteEndpoint =
+ CACreateRemoteEndpointInternal(resourceUri, addr,
+ CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
+
+ // create request info
+ CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
+ CA_MEMORY_ALLOC_CHECK(reqInfo);
+
+ // save request info data
+ reqInfo->method = CA_GET;
+ reqInfo->info.type = CA_MSG_NONCONFIRM;
+
+ reqInfo->info.token = token;
+ reqInfo->info.tokenLength = tokenLength;
// save data
data->type = SEND_TYPE_MULTICAST;
data->remoteEndpoint = remoteEndpoint;
- CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
- CA_MEMORY_ALLOC_CHECK(ReqInfo);
- ReqInfo->method = CA_GET;
- ReqInfo->info.token = token;
- ReqInfo->info.type = CA_MSG_NONCONFIRM;
- data->requestInfo = ReqInfo;
+ data->requestInfo = reqInfo;
data->responseInfo = NULL;
data->options = NULL;
data->numOptions = 0;
- CAHeaderOption_t *temp = NULL;
- if (options != NULL && numOptions > 0)
+ CAHeaderOption_t *headerOption = NULL;
+ if (NULL != options && numOptions > 0)
{
// copy data
- temp = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions);
- CA_MEMORY_ALLOC_CHECK(temp);
- memcpy(temp, options, sizeof(CAHeaderOption_t) * numOptions);
+ headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions);
+ CA_MEMORY_ALLOC_CHECK(headerOption);
+ memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
- data->options = temp;
+ data->options = headerOption;
data->numOptions = numOptions;
}
CAProcessData(data);
+
CADestroyRemoteEndpoint(remoteEndpoint);
- OICFree(temp);
+ OICFree(headerOption);
OICFree(data);
- OICFree(ReqInfo);
+ OICFree(reqInfo);
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
if (info.token)
{
- uint32_t tokenLength = strlen(info.token);
- OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, tokenLength);
+ uint32_t tokenLength = info.tokenLength;
+ OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
+ OIC_LOG_BUFFER(DEBUG, TAG, info.token, tokenLength);
- int32_t ret = coap_add_token(pdu, tokenLength, (uint8_t *) info.token);
+ int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *) info.token);
if (0 == ret)
{
OIC_LOG(DEBUG, TAG, "cannot add token to request");
// set token data
if (pdu->hdr->token_length > 0)
{
- OIC_LOG(DEBUG, TAG, "inside pdu->hdr->token_length");
- outInfo->token = (char *) OICMalloc(pdu->hdr->token_length + 1);
- if (outInfo->token == NULL)
+ OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length);
+ outInfo->token = (char *) OICMalloc(pdu->hdr->token_length);
+ if (NULL == outInfo->token)
{
- OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU, Memory allocation failed !");
+ OIC_LOG(ERROR, TAG, "memory allocation failed");
OICFree(outInfo->options);
return;
}
memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length);
- outInfo->token[pdu->hdr->token_length] = '\0';
}
+ outInfo->tokenLength = pdu->hdr->token_length;
+
// set payload data
if (NULL != pdu->data)
{
return;
}
-CAResult_t CAGenerateTokenInternal(CAToken_t *token)
+CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
{
OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal IN");
- if (token == NULL)
+
+ if(!token)
{
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, TAG, "invalid token pointer");
+ return CA_STATUS_INVALID_PARAM;
}
- // memory allocation
- char *temp = (char *) OICCalloc(1, (CA_MAX_TOKEN_LEN + 1) * sizeof(char));
- if (temp == NULL)
+ if((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
{
- OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal, Memory allocation failed !");
- return CA_MEMORY_ALLOC_FAILED;
+ OIC_LOG(ERROR, TAG, "invalid token length");
+ return CA_STATUS_INVALID_PARAM;
}
if (SEED == 0)
{
OIC_LOG(DEBUG, TAG, "Failed to Create Seed!");
SEED = 0;
- OICFree(temp);
return CA_STATUS_FAILED;
}
srandom(SEED);
}
+ // memory allocation
+ char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
+ if (NULL == temp)
+ {
+ OIC_LOG(ERROR, TAG, "CAGenerateTokenInternal, Memory allocation failed !");
+ return CA_MEMORY_ALLOC_FAILED;
+ }
+
// set random byte
- uint32_t index;
- for (index = 0; index < CA_MAX_TOKEN_LEN; index++)
+ uint8_t index;
+ for (index = 0; index < tokenLength; index++)
{
// use valid characters
- temp[index] = (random() % 94 + 33) & 0xFF;
+ temp[index] = random() & 0x00FF;
}
- temp[index] = '\0';
// save token
*token = temp;
- OIC_LOG(DEBUG, TAG, "generate the token!!");
- OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) *token, CA_MAX_TOKEN_LEN);
+ OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
+ OIC_LOG_BUFFER(DEBUG, TAG, *token, tokenLength);
OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal OUT");
return CA_STATUS_OK;
void CADestroyTokenInternal(CAToken_t token)
{
OIC_LOG(DEBUG, TAG, "CADestroyTokenInternal IN");
- if (token != NULL)
- {
- OIC_LOG(DEBUG, TAG, "destroy the token!!");
- OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) token, CA_MAX_TOKEN_LEN);
- OICFree(token);
- token = NULL;
- }
-
+ OICFree(token);
OIC_LOG(DEBUG, TAG, "CADestroyTokenInternal OUT");
}
if (info.token)
{
- uint32_t tokenLength = strlen(info.token);
- OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, tokenLength);
+ uint32_t tokenLength = info.tokenLength;
+ OIC_LOG_V(DEBUG, TAG, "tokenLength : %d, token : ", tokenLength);
+ OIC_LOG_BUFFER(DEBUG, TAG, info.token, tokenLength);
+
int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *) info.token);
if (0 == ret)
{
// set token data
if (pdu->hdr->token_length > 0)
{
- OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length>0");
- outInfo->token = (char *) OICMalloc(pdu->hdr->token_length + 1);
- if (outInfo->token == NULL)
+ OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length > 0");
+ outInfo->token = (char *) OICMalloc(pdu->hdr->token_length);
+ if (NULL == outInfo->token)
{
- OIC_LOG(ERROR, TAG, "malloc failed");
+ OIC_LOG(ERROR, TAG, "memory allocation failed");
OICFree(outInfo->options);
return;
}
memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length);
- outInfo->token[pdu->hdr->token_length] = '\0';
}
+ outInfo->tokenLength = pdu->hdr->token_length;
+
// set payload data
if (NULL != pdu->data)
{
return;
}
-CAResult_t CAGenerateTokenInternal(CAToken_t *token)
+CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
{
OIC_LOG(DEBUG, TAG, "IN");
- if (token == NULL)
+
+ if(!token)
{
- OIC_LOG(ERROR, TAG, "tok null");
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, TAG, "invalid token pointer");
+ return CA_STATUS_INVALID_PARAM;
}
- // memory allocation
- char *temp = (char *) OICCalloc(CA_MAX_TOKEN_LEN + 1, sizeof(char));
- if (temp == NULL)
+ if((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
{
- OIC_LOG(ERROR, TAG, "malloc failed");
- return CA_MEMORY_ALLOC_FAILED;
+ OIC_LOG(ERROR, TAG, "invalid token length");
+ return CA_STATUS_INVALID_PARAM;
}
if (SEED == 0)
{
OIC_LOG(DEBUG, TAG, "Failed to Create Seed!");
SEED = 0;
- OICFree(temp);
return CA_STATUS_FAILED;
}
srandom(SEED);
}
- // set random byte
- uint32_t index;
+ // memory allocation
+ char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
+ if (NULL == temp)
+ {
+ OIC_LOG(ERROR, TAG, "Calloc failed");
+ return CA_MEMORY_ALLOC_FAILED;
+ }
- for (index = 0; index < CA_MAX_TOKEN_LEN; index++)
+ // set random byte
+ for (uint8_t index = 0; index < tokenLength; index++)
{
- // To create random value 33~126(ASCII code) for valid character
- temp[index] = (rand() % 94 + 33) & 0xFF;
+ temp[index] = rand() & 0x00FF;
}
- temp[index] = '\0';
// save token
*token = temp;
- OIC_LOG(DEBUG, TAG, "gen token:");
- OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) *token, CA_MAX_TOKEN_LEN);
+ OIC_LOG_V(DEBUG, TAG, "gen token tokenLength : %d, token : ", tokenLength);
+ OIC_LOG_BUFFER(DEBUG, TAG, *token, tokenLength);
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
void CADestroyTokenInternal(CAToken_t token)
{
OIC_LOG(DEBUG, TAG, "IN");
- if (token != NULL)
- {
- OIC_LOG_V(DEBUG, TAG, "destroy token(%s)!!", token);
- OICFree(token);
- }
-
+ OICFree(token);
OIC_LOG(DEBUG, TAG, "OUT");
}
CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
{
- char *temp = NULL;
- int len = 0;
-
if (rep == NULL)
return NULL;
// allocate the request info structure.
CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
- if (clone == NULL)
+ if (!clone)
{
OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
return NULL;
memcpy(clone, rep, sizeof(CARequestInfo_t));
- if (rep->info.token != NULL)
+ if (rep->info.token)
{
+ char *temp = NULL;
+
// allocate token field
- len = strlen(rep->info.token);
+ int len = rep->info.tokenLength;
- temp = (char *) OICCalloc(len + 1, sizeof(char));
- if (temp == NULL)
+ if (len)
{
- OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
+ temp = (char *) OICCalloc(len, sizeof(char));
+ if (!temp)
+ {
+ OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
- CADestroyRequestInfoInternal(clone);
+ CADestroyRequestInfoInternal(clone);
- return NULL;
+ return NULL;
+ }
+ memcpy(temp, rep->info.token, len);
}
- strncpy(temp, rep->info.token, len);
// save the token
clone->info.token = temp;
+ clone->info.tokenLength = len;
}
if (rep->info.options != NULL && rep->info.numOptions > 0)
if (rep->info.payload != NULL)
{
+ char *temp = NULL;
+
// allocate payload field
- len = strlen(rep->info.payload);
+ int len = strlen(rep->info.payload);
temp = (char *) OICMalloc(sizeof(char) * (len + 1));
if (temp == NULL)
CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
{
- char *temp = NULL;
- int len = 0;
-
if (rep == NULL)
return NULL;
}
memcpy(clone, rep, sizeof(CAResponseInfo_t));
- if (rep->info.token != NULL)
+ if (rep->info.token)
{
+ char *temp = NULL;
+
// allocate token field
- len = strlen(rep->info.token);
+ int len = rep->info.tokenLength;
- temp = (char *) OICCalloc(len + 1, sizeof(char));
- if (temp == NULL)
+ if (len)
{
- OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
+ temp = (char *) OICCalloc(len, sizeof(char));
+ if (!temp)
+ {
+ OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
- CADestroyResponseInfoInternal(clone);
+ CADestroyResponseInfoInternal(clone);
- return NULL;
+ return NULL;
+ }
+ memcpy(temp, rep->info.token, len);
}
- strncpy(temp, rep->info.token, len);
-
// save the token
clone->info.token = temp;
+ clone->info.tokenLength = len;
}
if (rep->info.options != NULL && rep->info.numOptions)
if (rep->info.payload != NULL)
{
+ char *temp = NULL;
// allocate payload field
- len = strlen(rep->info.payload);
+ int len = strlen(rep->info.payload);
temp = (char *) OICCalloc(len + 1, sizeof(char));
if (temp == NULL)
return;
// free token field
- OICFree((char *) rep->info.token);
+ OICFree(rep->info.token);
// free options field
OICFree((CAHeaderOption_t *) rep->info.options);
return;
// free token field
- OICFree((char *) rep->info.token);
+ OICFree(rep->info.token);
// free options field
if (rep->info.options != NULL && rep->info.numOptions)
OCClientContextDeleter deleteCallback;
// when a response is recvd with this token, above callback will be invoked
CAToken_t token;
+ // a response is recvd with this token length
+ uint8_t tokenLength;
// Invocation handle tied to original call to OCDoResource()
OCDoHandle handle;
// This is used to determine if all responses should be consumed or not.
*/
OCStackResult
AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
- CAToken_t * token, OCDoHandle *handle, OCMethod method,
+ CAToken_t * token, uint8_t tokenLength,
+ OCDoHandle *handle, OCMethod method,
char * requestUri, char * resourceTypeName);
/** @ingroup ocstack
*
* @return address of the node if found, otherwise NULL
*/
-ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri);
+ClientCB* GetClientCB(const CAToken_t * token, uint8_t tokenLength,
+ OCDoHandle handle, const char * requestUri);
/**
* Inserts a new resource type filter into this cb node.
char *query;
//token for the observe request
CAToken_t token;
+ //token length for the observe request
+ uint8_t tokenLength;
// Resource handle
OCResource *resource;
//TODO bundle it in Endpoint structure(address, uri, type, secured)
const char *query,
OCObservationId obsId,
CAToken_t *token,
+ uint8_t tokenLength,
OCResource *resHandle,
OCQualityOfService qos,
const CAAddress_t *addressInfo,
CAConnectivityType_t connectivityType);
-OCStackResult DeleteObserverUsingToken (CAToken_t * token);
+OCStackResult DeleteObserverUsingToken (CAToken_t * token, uint8_t tokenLength);
-ResourceObserver* GetObserverUsingToken (const CAToken_t * token);
+ResourceObserver* GetObserverUsingToken (const CAToken_t * token, uint8_t tokenLength);
ResourceObserver* GetObserverUsingId (const OCObservationId observeId);
CAAddress_t addressInfo;
/** Connectivity of the endpoint**/
CAConnectivityType_t connectivityType;
- // token for the observe request
+ // token for the request
CAToken_t requestToken;
+ // token length the request
+ uint8_t tokenLength;
// The ID of CoAP pdu //Kept in
uint16_t coapID; //CoAP
uint8_t delayedResNeeded;
OCRequestHandle requestHandle;
} OCServerResponse;
-OCServerRequest * GetServerRequestUsingToken (const CAToken_t token);
+OCServerRequest * GetServerRequestUsingToken (const CAToken_t token, uint8_t tokenLength);
OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle);
OCQualityOfService qos, char * query,
OCHeaderOption * rcvdVendorSpecificHeaderOptions,
char * reqJSONPayload, CAToken_t * requestToken,
+ uint8_t tokenLength,
char * resourceUrl, size_t reqTotalSize,
CAAddress_t *addressInfo, CAConnectivityType_t connectivityType);
//token for the observe request
CAToken_t requestToken;
-
+ uint8_t tokenLength; //token length
// The ID of CoAP pdu
uint16_t coapID;
uint8_t delayedResNeeded;
OCStackResult
AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
- CAToken_t * token, OCDoHandle *handle, OCMethod method,
+ CAToken_t * token, uint8_t tokenLength,
+ OCDoHandle *handle, OCMethod method,
char * requestUri, char * resourceTypeName)
{
- if(!clientCB || !cbData || !token || !handle || !requestUri)
+ if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
{
return OC_STACK_INVALID_PARAM;
}
#ifdef WITH_PRESENCE
if(method == OC_REST_PRESENCE)
{ // Retrieve the presence callback structure for this specific requestUri.
- cbNode = GetClientCB(NULL, NULL, requestUri);
+ cbNode = GetClientCB(NULL, 0, NULL, requestUri);
}
#endif // WITH_PRESENCE
//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;
if(cbNode) {
LL_DELETE(cbList, cbNode);
OC_LOG(INFO, TAG, PCF("deleting tokens"));
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
CADestroyToken (cbNode->token);
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
OCFree(cbNode->handle);
OCFree(cbNode->requestUri);
if(cbNode->deleteCallback)
}
}
-ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri)
+ClientCB* GetClientCB(const CAToken_t * token, uint8_t tokenLength,
+ OCDoHandle handle, const char * requestUri)
{
+
ClientCB* out = NULL;
- if(token) {
- LL_FOREACH(cbList, out) {
+
+ if(token && *token &&
+ tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
+ {
+ LL_FOREACH(cbList, out)
+ {
OC_LOG(INFO, TAG, PCF("comparing tokens"));
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
- if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, tokenLength);
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
+ if(memcmp(out->token, *token, tokenLength) == 0)
{
return out;
}
}
}
- else if(handle) {
- LL_FOREACH(cbList, out) {
- if(out->handle == handle) {
+ else if(handle)
+ {
+ LL_FOREACH(cbList, out)
+ {
+ if(out->handle == handle)
+ {
return out;
}
}
}
- else if(requestUri) {
- LL_FOREACH(cbList, out) {
+ else if(requestUri)
+ {
+ LL_FOREACH(cbList, out)
+ {
if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
{
return out;
result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
0, resPtr->sequenceNum, qos, resourceObserver->query,
NULL, NULL,
- &resourceObserver->token, resourceObserver->resUri, 0,
+ &resourceObserver->token, resourceObserver->tokenLength,
+ resourceObserver->resUri, 0,
&(resourceObserver->addressInfo), resourceObserver->connectivityType);
if(request)
result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
0, resPtr->sequenceNum, qos, resourceObserver->query,
NULL, NULL,
- &resourceObserver->token, resourceObserver->resUri, 0,
+ &resourceObserver->token, resourceObserver->tokenLength,
+ resourceObserver->resUri, 0,
&(resourceObserver->addressInfo), resourceObserver->connectivityType);
if(result == OC_STACK_OK)
result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
0, resource->sequenceNum, qos, observation->query,
- NULL, NULL, &observation->token,
+ NULL, NULL, &observation->token, observation->tokenLength,
observation->resUri, 0,
&(observation->addressInfo), observation->connectivityType);
const char *query,
OCObservationId obsId,
CAToken_t *token,
+ uint8_t tokenLength,
OCResource *resHandle,
OCQualityOfService qos,
const CAAddress_t *addressInfo,
VERIFY_NON_NULL (obsNode->query);
memcpy (obsNode->query, query, strlen(query)+1);
}
-
- obsNode->token = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
- VERIFY_NON_NULL (obsNode->token);
- memcpy(obsNode->token, *token, CA_MAX_TOKEN_LEN);
- obsNode->token[CA_MAX_TOKEN_LEN]='\0';
-
+ // If tokenLength is zero, the return value depends on the
+ // particular library implementation (it may or may not be a null pointer).
+ if(tokenLength)
+ {
+ obsNode->token = (CAToken_t)OCMalloc(tokenLength);
+ VERIFY_NON_NULL (obsNode->token);
+ memcpy(obsNode->token, *token, tokenLength);
+ }
+ obsNode->tokenLength = tokenLength;
obsNode->addressInfo = *addressInfo;
obsNode->connectivityType = connectivityType;
obsNode->resource = resHandle;
return NULL;
}
-ResourceObserver* GetObserverUsingToken (const CAToken_t * token)
+ResourceObserver* GetObserverUsingToken (const CAToken_t * token, uint8_t tokenLength)
{
ResourceObserver *out = NULL;
LL_FOREACH (serverObsList, out)
{
OC_LOG(INFO, TAG,PCF("comparing tokens"));
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN);
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
- if((memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0))
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
+ if((memcmp(out->token, *token, tokenLength) == 0))
{
return out;
}
return NULL;
}
-OCStackResult DeleteObserverUsingToken (CAToken_t * token)
+OCStackResult DeleteObserverUsingToken (CAToken_t * token, uint8_t tokenLength)
{
if(!token || !*token)
{
ResourceObserver *obsNode = NULL;
- obsNode = GetObserverUsingToken (token);
+ obsNode = GetObserverUsingToken (token, tokenLength);
if (obsNode)
{
OC_LOG_V(INFO, TAG, PCF("deleting tokens"));
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, CA_MAX_TOKEN_LEN);
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, tokenLength);
LL_DELETE (serverObsList, obsNode);
OCFree(obsNode->resUri);
OCFree(obsNode->query);
{
if(out)
{
- DeleteObserverUsingToken (&(out->token));
+ DeleteObserverUsingToken (&(out->token), out->tokenLength);
}
}
serverObsList = NULL;
result = AddObserver ((const char*)(request->resourceUrl),
(const char *)(request->query),
- ehRequest.obsInfo.obsId, &request->requestToken,
+ ehRequest.obsInfo.obsId, &request->requestToken, request->tokenLength,
resource, request->qos,
&request->addressInfo, request->connectivityType);
{
OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
- resObs = GetObserverUsingToken (&request->requestToken);
+ resObs = GetObserverUsingToken (&request->requestToken, request->tokenLength);
if (NULL == resObs)
{
ehRequest.obsInfo.obsId = resObs->observeId;
ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
- result = DeleteObserverUsingToken (&request->requestToken);
+ result = DeleteObserverUsingToken (&request->requestToken, request->tokenLength);
if(result == OC_STACK_OK)
{
static struct OCServerRequest * serverRequestList = NULL;
static struct OCServerResponse * serverResponseList = NULL;
-OCServerRequest * GetServerRequestUsingToken (const CAToken_t token)
+OCServerRequest * GetServerRequestUsingToken (const CAToken_t token, uint8_t tokenLength)
{
if(!token)
{
LL_FOREACH (serverRequestList, out)
{
OC_LOG(INFO, TAG,PCF("comparing tokens"));
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN);
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->requestToken, CA_MAX_TOKEN_LEN);
- if(memcmp(out->requestToken, token, CA_MAX_TOKEN_LEN) == 0)
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->requestToken, tokenLength);
+ if(memcmp(out->requestToken, token, tokenLength) == 0)
{
return out;
}
OCQualityOfService qos, char * query,
OCHeaderOption * rcvdVendorSpecificHeaderOptions,
char * reqJSONPayload, CAToken_t * requestToken,
+ uint8_t tokenLength,
char * resourceUrl, size_t reqTotalSize,
CAAddress_t *addressInfo, CAConnectivityType_t connectivityType)
{
serverRequest->requestComplete = 0;
if(requestToken)
{
- serverRequest->requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
- VERIFY_NON_NULL (serverRequest->requestToken);
- memcpy(serverRequest->requestToken, *requestToken, CA_MAX_TOKEN_LEN);
- serverRequest->requestToken[CA_MAX_TOKEN_LEN]='\0';
+ // If tokenLength is zero, the return value depends on the
+ // particular library implementation (it may or may not be a null pointer).
+ if (tokenLength)
+ {
+ serverRequest->requestToken = (CAToken_t) OCMalloc(tokenLength);
+ VERIFY_NON_NULL(serverRequest->requestToken);
+ memcpy(serverRequest->requestToken, *requestToken, tokenLength);
+ }
+
}
+ serverRequest->tokenLength = tokenLength;
if(resourceUrl)
{
break;
}
responseInfo.info.type = qualityOfServiceToMessageType(serverRequest->qos);
- char token[CA_MAX_TOKEN_LEN + 1] = {};
+ char token[CA_MAX_TOKEN_LEN] = {};
responseInfo.info.token = token;
- memcpy(responseInfo.info.token, serverRequest->requestToken, CA_MAX_TOKEN_LEN);
+ memcpy(responseInfo.info.token, serverRequest->requestToken, serverRequest->tokenLength);
+ responseInfo.info.tokenLength = serverRequest->tokenLength;
if(serverRequest->observeResult == OC_STACK_OK)
{
snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port,
OC_PRESENCE_URI);
- cbNode = GetClientCB(NULL, NULL, fullUri);
+ cbNode = GetClientCB(NULL, 0, NULL, fullUri);
if(cbNode)
{
else
{
snprintf(fullUri, MAX_URI_LENGTH, "%s%s", OC_MULTICAST_IP, endPoint->resourceUri);
- cbNode = GetClientCB(NULL, NULL, fullUri);
+ cbNode = GetClientCB(NULL, 0, NULL, fullUri);
if(cbNode)
{
multicastPresenceSubscribe = 1;
return;
}
- ClientCB *cbNode = GetClientCB(&(responseInfo->info.token), NULL, NULL);
+ ClientCB *cbNode = GetClientCB(&(responseInfo->info.token),
+ responseInfo->info.tokenLength, NULL, NULL);
if (cbNode)
{
}
}
- OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", CA_MAX_TOKEN_LEN);
- OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token, CA_MAX_TOKEN_LEN);
+ OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d",
+ requestInfo->info.tokenLength);
+ OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token,
+ requestInfo->info.tokenLength);
- serverRequest.requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
+ serverRequest.requestToken = (CAToken_t)OCMalloc(requestInfo->info.tokenLength);
+ serverRequest.tokenLength = requestInfo->info.tokenLength;
// Module Name
if (!serverRequest.requestToken)
{
OC_LOG(FATAL, TAG, "Server Request Token is NULL");
return;
}
- memcpy(serverRequest.requestToken, requestInfo->info.token, CA_MAX_TOKEN_LEN);
+ memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength);
if (requestInfo->info.type == CA_MSG_CONFIRM)
{
return OC_STACK_INVALID_PARAM;
}
- OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
+ OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken,
+ protocolRequest->tokenLength);
if(!request)
{
OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
protocolRequest->observationOption, protocolRequest->qos,
protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
+ protocolRequest->tokenLength,
protocolRequest->resourceUrl,protocolRequest->reqTotalSize,
&protocolRequest->addressInfo, protocolRequest->connectivityType);
if (OC_STACK_OK != result)
CARemoteEndpoint_t* endpoint = NULL;
CAResult_t caResult;
CAToken_t token = NULL;
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
OCDoHandle resHandle = NULL;
CAInfo_t requestData ={};
CARequestInfo_t requestInfo ={};
}
// create token
- caResult = CAGenerateToken(&token);
+ caResult = CAGenerateToken(&token, tokenLength);
if (caResult != CA_STATUS_OK)
{
OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
requestData.type = qualityOfServiceToMessageType(qos);
requestData.token = token;
+ requestData.tokenLength = tokenLength;
if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
{
result = CreateObserveHeaderOption (&(requestData.options), options,
goto exit;
}
- if((result = AddClientCB(&clientCB, cbData, &token, &resHandle, method,
+ if((result = AddClientCB(&clientCB, cbData, &token, tokenLength, &resHandle, method,
requestUri, resourceType)) != OC_STACK_OK)
{
result = OC_STACK_NO_MEMORY;
OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
- ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
+ ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL);
if(clientCB)
{
memset(&requestData, 0, sizeof(CAInfo_t));
requestData.type = qualityOfServiceToMessageType(qos);
requestData.token = clientCB->token;
+ requestData.tokenLength = clientCB->tokenLength;
if (CreateObserveHeaderOption (&(requestData.options),
options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
{
{
ret = OC_STACK_OK;
}
+ break;
#ifdef WITH_PRESENCE
case OC_REST_PRESENCE:
FindAndDeleteClientCB(clientCB);
*/
OCStackResult OCStartPresence(const uint32_t ttl)
{
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
OCChangeResourceProperty(
&(((OCResource *)presenceResource.handle)->resourceProperties),
OC_ACTIVE, 1);
addressInfo.IP.port = OC_MULTICAST_PORT;
CAToken_t caToken = NULL;
- CAResult_t caResult = CAGenerateToken(&caToken);
+ CAResult_t caResult = CAGenerateToken(&caToken, tokenLength);
if (caResult != CA_STATUS_OK)
{
OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
CAConnectivityType_t connType;
OCToCAConnectivityType(OC_ALL, &connType );
- AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken,
+ AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken, tokenLength,
(OCResource *)presenceResource.handle, OC_LOW_QOS,
&addressInfo, connType);
CADestroyToken(caToken);