#include "cainterface.h"
#include "cacommon.h"
-#include "org_iotivity_service_RMInterface.h"
+#include "org_iotivity_ca_service_RMInterface.h"
#define LOG_TAG "JNI_INTERFACE_SAMPLE"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define OPTION_INFO_LENGTH 1024
#define NETWORK_INFO_LENGTH 1024
-uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
+typedef struct
+{
+ char ipAddress[CA_IPADDR_SIZE];
+ uint16_t port;
+} addressSet_t;
-void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
-void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
+void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo);
+void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo);
+void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
void get_resource_uri(const char *URI, char *resourceURI, uint32_t length);
uint32_t get_secure_information(CAPayload_t payLoad);
CAResult_t get_network_type(uint32_t selectedNetwork);
void callback(char *subject, char *receivedData);
-CAResult_t get_remote_address(CATransportType_t transportType, CAAddress_t addressInfo);
+CAResult_t get_remote_address(CATransportAdapter_t transportType, const char *address);
+void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
-CATransportType_t g_selectedNwType = CA_IPV4;
+uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
+CATransportAdapter_t g_selectedNwType = CA_ADAPTER_IP;
static CAToken_t g_lastRequestToken = NULL;
-static uint8_t g_lastRequestTokenLength;
+static uint8_t g_lastRequestTokenLength = 0;
-static const char SECURE_COAPS_PREFIX[] = "coaps://";
+static const char COAP_PREFIX[] = "coap://";
+static const char COAPS_PREFIX[] = "coaps://";
+static const uint16_t COAP_PREFIX_LEN = sizeof(COAP_PREFIX) - 1;
+static const uint16_t COAPS_PREFIX_LEN = sizeof(COAPS_PREFIX) - 1;
static const char SECURE_INFO_DATA[]
= "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
- "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
+ "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":"
+ "%d}}]}";
static const char NORMAL_INFO_DATA[]
= "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
"\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
-
static jobject g_responseListenerObject = NULL;
static JavaVM *g_jvm;
-static CARemoteEndpoint_t *g_clientEndpoint = NULL;
-static CAToken_t g_clientToken;
-static uint8_t g_clientTokenLength = NULL;
+static CAEndpoint_t *g_clientEndpoint = NULL;
+static char *g_resourceUri = NULL;
+static CAToken_t g_clientToken = NULL;
+static uint8_t g_clientTokenLength = 0;
-static uint16_t g_clientMsgId;
+static uint16_t g_clientMsgId = 0;
static char *g_remoteAddress = NULL;
// init
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj,
- jobject listener)
+Java_org_iotivity_ca_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj,
+ jobject listener)
{
LOGI("setNativeResponseListener");
g_responseListenerObject = (*env)->NewGlobalRef(env, obj);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMInitialize(JNIEnv *env, jobject obj, jobject context)
+Java_org_iotivity_ca_service_RMInterface_RMInitialize(JNIEnv *env, jobject obj, jobject context)
{
LOGI("RMInitialize");
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
+Java_org_iotivity_ca_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
{
LOGI("RMTerminate");
CADestroyToken(g_lastRequestToken);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMStartListeningServer(JNIEnv *env, jobject obj)
+Java_org_iotivity_ca_service_RMInterface_RMStartListeningServer(JNIEnv *env, jobject obj)
{
LOGI("RMStartListeningServer");
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env, jobject obj)
+Java_org_iotivity_ca_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env, jobject obj)
{
LOGI("RMStartDiscoveryServer");
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMRegisterHandler(JNIEnv *env, jobject obj)
+Java_org_iotivity_ca_service_RMInterface_RMRegisterHandler(JNIEnv *env, jobject obj)
{
LOGI("RMRegisterHandler");
- CARegisterHandler(request_handler, response_handler);
-}
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMFindResource(JNIEnv *env, jobject obj, jstring uri)
-{
- // create token
- CAToken_t token = NULL;
- uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
- CAResult_t res = CAGenerateToken(&token, tokenLength);
- if ((CA_STATUS_OK != res) || (!token))
- {
- LOGE("token generate error!!");
- return;
- }
-
- printf("Generated token %s\n", token);
-
- const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
- LOGI("RMFindResource - %s", strUri);
-
- res = CAFindResource((const CAURI_t) strUri, token, tokenLength);
-
- //ReleseStringUTFCharss for strUri
- (*env)->ReleaseStringUTFChars(env, uri, strUri);
-
- if (CA_STATUS_OK != res)
- {
- 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;
- }
+ CARegisterHandler(request_handler, response_handler, error_handler);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
- jstring payload, jint selectedNetwork,
- jint isSecured, jint msgType)
+Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
+ jstring payload, jint selectedNetwork,
+ jint isSecured, jint msgType)
{
LOGI("selectedNetwork - %d", selectedNetwork);
CAResult_t res = get_network_type(selectedNetwork);
const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
LOGI("RMSendRequest - %s", strUri);
- //create remote endpoint
- CARemoteEndpoint_t* endpoint = NULL;
- res = CACreateRemoteEndpoint((const CAURI_t) strUri, g_selectedNwType, &endpoint);
-
- //ReleaseStringUTFChars for strUri
- (*env)->ReleaseStringUTFChars(env, uri, strUri);
+ CATransportFlags_t flags;
+ addressSet_t address = {};
+ parsing_coap_uri(strUri, &address, &flags);
+ //create remote endpoint
+ CAEndpoint_t* endpoint = NULL;
+ res = CACreateEndpoint(flags, g_selectedNwType, (const char*)address.ipAddress,
+ address.port, &endpoint);
if (CA_STATUS_OK != res)
{
LOGE("Could not create remote end point");
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
return;
}
{
LOGE("token generate error!!");
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
return;
}
char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
CAInfo_t requestData = { 0 };
requestData.token = token;
// destroy token
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
return;
}
snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
+ requestData.payloadSize = length;
}
else
{
// destroy token
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
return;
}
snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
+ requestData.payloadSize = length;
}
requestData.type = messageType;
+ requestData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
+ if (NULL == requestData.resourceUri)
+ {
+ LOGE("Memory allocation failed!");
+ // destroy token
+ CADestroyToken(token);
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
+ free(requestData.payload);
+ return;
+ }
+ memcpy(requestData.resourceUri, resourceURI, sizeof(resourceURI));
CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
+ requestInfo.isMulticast = false;
requestInfo.info = requestData;
// send request
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
free(requestData.payload);
+ free(requestData.resourceUri);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
- jint selectedNetwork)
+Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
+ jint selectedNetwork)
{
LOGI("selectedNetwork - %d", selectedNetwork);
CAResult_t res = get_network_type(selectedNetwork);
return;
}
- const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
- LOGI("RMSendReqestToAll - %s", strUri);
-
// create remote endpoint
- CARemoteEndpoint_t *endpoint = NULL;
- res = CACreateRemoteEndpoint((const CAURI_t) strUri, g_selectedNwType, &endpoint);
-
- //ReleaseStringUTFChars for strUri
- (*env)->ReleaseStringUTFChars(env, uri, strUri);
+ CAEndpoint_t *endpoint = NULL;
+ res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, NULL, 0, &endpoint);
if (CA_STATUS_OK != res)
{
return;
}
- CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
- if (NULL == group)
- {
- LOGE("Memory allocation failed!");
- CADestroyRemoteEndpoint(endpoint);
- return;
- }
- group->transportType = endpoint->transportType;
- group->resourceUri = endpoint->resourceUri;
-
// create token
CAToken_t token = NULL;
uint8_t tokenLength = CA_MAX_TOKEN_LEN;
{
LOGE("token generate error!!");
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
- free(group);
+ CADestroyEndpoint(endpoint);
return;
}
CAInfo_t requestData = { 0 };
requestData.token = token;
requestData.tokenLength = tokenLength;
- requestData.payload = "Temp Json Payload";
+ requestData.payload = (CAPayload_t) "TempJsonPayload";
+ requestData.payloadSize = strlen((const char *) requestData.payload);
requestData.type = CA_MSG_NONCONFIRM;
+ const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
+ LOGI("resourceUri - %s", strUri);
+ requestData.resourceUri = (CAURI_t)strUri;
+
+ uint8_t optionNum = 2;
+ CAHeaderOption_t *headerOpt = (CAHeaderOption_t*) calloc(1,
+ sizeof(CAHeaderOption_t) * optionNum);
+ if (NULL == headerOpt)
+ {
+ LOGE("Memory allocation failed");
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
+ return;
+ }
+
+ char* FirstOptionData = "Hello";
+ headerOpt[0].optionID = 3000;
+ memcpy(headerOpt[0].optionData, FirstOptionData, strlen(FirstOptionData));
+ headerOpt[0].optionLength = (uint16_t) strlen(FirstOptionData);
+
+ char* SecondOptionData2 = "World";
+ headerOpt[1].optionID = 3001;
+ memcpy(headerOpt[1].optionData, SecondOptionData2, strlen(SecondOptionData2));
+ headerOpt[1].optionLength = (uint16_t) strlen(SecondOptionData2);
+
+ requestData.numOptions = optionNum;
+ requestData.options = headerOpt;
+
CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
+ requestInfo.isMulticast = true;
requestInfo.info = requestData;
// send request to all
- res = CASendRequestToAll(group, &requestInfo);
+ res = CASendRequest(endpoint, &requestInfo);
if (CA_STATUS_OK != res)
{
LOGE("Could not send request to all");
g_lastRequestTokenLength = tokenLength;
}
- // destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
- free(group);
+ //ReleaseStringUTFChars for strUri
+ (*env)->ReleaseStringUTFChars(env, uri, strUri);
+
+ free(headerOpt);
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
- jint selectedNetwork,
- jint isSecured, jint msgType,
- jint responseValue)
+Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
+ jint selectedNetwork,
+ jint isSecured, jint msgType,
+ jint responseValue)
{
LOGI("RMSendResponse");
CAInfo_t responseData = { 0 };
responseData.type = messageType;
responseData.messageId = g_clientMsgId;
+ responseData.resourceUri = (CAURI_t)g_resourceUri;
CAResponseInfo_t responseInfo = { 0 };
if (1 == isSecured)
{
- uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_clientEndpoint->resourceUri) + 1;
+ uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_resourceUri) + 1;
responseData.payload = (CAPayload_t) malloc(length);
- sprintf(responseData.payload, SECURE_INFO_DATA, g_clientEndpoint->resourceUri,
+ sprintf(responseData.payload, SECURE_INFO_DATA, g_resourceUri,
g_localSecurePort);
+ responseData.payloadSize = length;
}
else
{
- uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_clientEndpoint->resourceUri) + 1;
+ uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_resourceUri) + 1;
responseData.payload = (CAPayload_t) malloc(length);
- sprintf(responseData.payload, NORMAL_INFO_DATA, g_clientEndpoint->resourceUri);
+ sprintf(responseData.payload, NORMAL_INFO_DATA, g_resourceUri);
+ responseData.payloadSize = length;
}
}
//msgType is RESET
g_clientTokenLength = 0;
// destroy remote endpoint
- CADestroyRemoteEndpoint(g_clientEndpoint);
+ CADestroyEndpoint(g_clientEndpoint);
g_clientEndpoint = NULL;
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMAdvertiseResource(JNIEnv *env, jobject obj, jstring uri)
-{
- LOGI("RMAdvertiseResource");
-
- uint32_t optionNum = 2;
-
- CAHeaderOption_t *headerOpt = (CAHeaderOption_t*) calloc(1,
- sizeof(CAHeaderOption_t) * optionNum);
- if (NULL == headerOpt)
- {
- LOGE("Memory allocation failed!");
- return;
- }
-
- char* tmpOptionData1 = "Hello";
- headerOpt[0].optionID = 3000;
- memcpy(headerOpt[0].optionData, tmpOptionData1, strlen(tmpOptionData1));
- headerOpt[0].optionLength = (uint16_t) strlen(tmpOptionData1);
-
- char* tmpOptionData2 = "World";
- headerOpt[1].optionID = 3001;
- memcpy(headerOpt[1].optionData, tmpOptionData2, strlen(tmpOptionData2));
- headerOpt[1].optionLength = (uint16_t) strlen(tmpOptionData2);
-
- // create token
- CAToken_t token = NULL;
- uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
- CAResult_t res = CAGenerateToken(&token, tokenLength);
- if ((CA_STATUS_OK != res) || (!token))
- {
- LOGE("token generate error!");
- free(headerOpt);
- return;
- }
-
- const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
-
- res = CAAdvertiseResource((const CAURI_t) strUri, token, tokenLength,
- headerOpt, (uint8_t) optionNum);
- if (CA_STATUS_OK != res)
- {
- LOGE("Could not start advertise resource");
- CADestroyToken(token);
- }
- else
- {
- CADestroyToken(g_lastRequestToken);
- g_lastRequestToken = token;
- g_lastRequestTokenLength = tokenLength;
- }
-
- free(headerOpt);
-
- //ReleaseStringUTFChars for strUri
- (*env)->ReleaseStringUTFChars(env, uri, strUri);
-}
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
- jstring payload, jint selectedNetwork,
- jint isSecured, jint msgType,
- jint responseValue)
+Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
+ jstring payload, jint selectedNetwork,
+ jint isSecured, jint msgType,
+ jint responseValue)
{
LOGI("selectedNetwork - %d", selectedNetwork);
const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
LOGI("RMSendNotification - %s", strUri);
+ CATransportFlags_t flags;
+ addressSet_t address = {};
+ parsing_coap_uri(strUri, &address, &flags);
+
//create remote endpoint
- CARemoteEndpoint_t* endpoint = NULL;
- if (CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t) strUri,
- g_selectedNwType, &endpoint))
+ CAEndpoint_t* endpoint = NULL;
+ if (CA_STATUS_OK != CACreateEndpoint(flags, g_selectedNwType,
+ (const char*)address.ipAddress,
+ address.port, &endpoint))
{
//ReleaseStringUTFChars for strUri
(*env)->ReleaseStringUTFChars(env, uri, strUri);
}
char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
- get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
+ get_resource_uri(strUri, resourceURI, RESOURCE_URI_LENGTH);
//ReleaseStringUTFChars for strUri
(*env)->ReleaseStringUTFChars(env, uri, strUri);
if ((CA_STATUS_OK != res) || (!token))
{
LOGE("token generate error!");
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
return;
}
CAInfo_t responseData = { 0 };
responseData.token = token;
responseData.tokenLength = tokenLength;
+ responseData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
+ if (NULL == responseData.resourceUri)
+ {
+ LOGE("Memory allocation failed!");
+ // destroy token
+ CADestroyToken(token);
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
+ return;
+ }
+ memcpy(responseData.resourceUri, resourceURI, sizeof(resourceURI));
if (1 == isSecured)
{
// destroy token
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
+
+ free(responseData.resourceUri);
return;
}
snprintf(responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
// destroy token
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
+
+ free(responseData.resourceUri);
return;
}
snprintf(responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
free(responseData.payload);
+ free(responseData.resourceUri);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
+Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj,
+ jint networkType)
{
LOGI("RMSelectNetwork Type : %d", networkType);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
+Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj,
+ jint networkType)
{
LOGI("RMUnSelectNetwork Type : %d", networkType);
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMGetNetworkInfomation(JNIEnv *env, jobject obj)
+Java_org_iotivity_ca_service_RMInterface_RMGetNetworkInfomation(JNIEnv *env, jobject obj)
{
LOGI("RMGetNetworkInfomation");
- CALocalConnectivity_t *tempInfo = NULL;
+ CAEndpoint_t *tempInfo = NULL;
uint32_t tempSize = 0;
CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
if (CA_STATUS_OK != res)
{
LOGE("Could not start get network information");
- OICFree(tempInfo);
+ free(tempInfo);
return;
}
uint32_t index;
for (index = 0; index < tempSize; index++)
{
- res = get_remote_address(tempInfo[index].type, tempInfo[index].addressInfo);
+ res = get_remote_address(tempInfo[index].adapter, tempInfo[index].addr);
if (CA_STATUS_OK != res)
{
- OICFree(tempInfo);
+ free(tempInfo);
return;
}
if (NULL != g_responseListenerObject)
{
char networkInfo[NETWORK_INFO_LENGTH];
- LOGI("Type: %d", tempInfo[index].type);
- sprintf(networkInfo, "%d",tempInfo[index].type);
+ LOGI("Type: %d", tempInfo[index].adapter);
+ sprintf(networkInfo, "%d",tempInfo[index].adapter);
callback("Type :", networkInfo);
- if (CA_IPV4 == tempInfo[index].type)
+ if (CA_ADAPTER_IP == tempInfo[index].adapter)
{
- LOGI("Port: %d", tempInfo[index].addressInfo.IP.port);
- sprintf(networkInfo, "%d",tempInfo[index].addressInfo.IP.port);
+ LOGI("Port: %d", tempInfo[index].port);
+ sprintf(networkInfo, "%d",tempInfo[index].port);
callback("Port: ", networkInfo);
}
- LOGI("Secured: %d", tempInfo[index].isSecured);
+ LOGI("Secured: %d", (tempInfo[index].flags & CA_SECURE));
LOGI("Address: %s", g_remoteAddress);
callback("Address: ", g_remoteAddress);
free(g_remoteAddress);
}
- if (true == tempInfo[index].isSecured)
+ if (tempInfo[index].flags & CA_SECURE)
{
- g_localSecurePort = tempInfo[index].addressInfo.IP.port;
+ g_localSecurePort = tempInfo[index].port;
}
}
// free
- OICFree(tempInfo);
+ free(tempInfo);
LOGI("##############################################################");
}
JNIEXPORT void JNICALL
-Java_org_iotivity_service_RMInterface_RMHandleRequestResponse(JNIEnv *env, jobject obj)
+Java_org_iotivity_ca_service_RMInterface_RMHandleRequestResponse(JNIEnv *env, jobject obj)
{
LOGI("RMHandleRequestResponse");
}
}
-void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
+void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo)
{
if (!object)
return;
}
- CAResult_t res = get_remote_address(object->transportType, object->addressInfo);
+ CAResult_t res = get_remote_address(object->adapter, object->addr);
if (CA_STATUS_OK != res)
{
return;
}
LOGI("##########received request from remote device #############");
- if (object->resourceUri)
- {
- LOGI("Uri: %s", object->resourceUri);
- }
LOGI("Remote Address: %s", g_remoteAddress);
+ LOGI("Uri: %s", requestInfo->info.resourceUri);
LOGI("Data: %s", requestInfo->info.payload);
LOGI("Token: %s", requestInfo->info.token);
LOGI("Code: %d", requestInfo->method);
char *cloneUri = NULL;
uint32_t len = 0;
- if (NULL != object->resourceUri)
+ if (NULL != requestInfo->info.resourceUri)
{
- len = strlen(object->resourceUri);
- cloneUri = (char *) malloc(sizeof(char) * (len + 1));
+ len = strlen(requestInfo->info.resourceUri);
+ cloneUri = (char *)malloc(sizeof(char) * (len + 1));
if (NULL == cloneUri)
{
return;
}
- memcpy(cloneUri, object->resourceUri, len + 1);
+ memcpy(cloneUri, requestInfo->info.resourceUri, len + 1);
callback("Uri: ", cloneUri);
}
free(g_remoteAddress);
//clone g_clientEndpoint
- g_clientEndpoint = (CARemoteEndpoint_t *) malloc(sizeof(CARemoteEndpoint_t));
+ g_clientEndpoint = (CAEndpoint_t *) malloc(sizeof(CAEndpoint_t));
if (NULL == g_clientEndpoint)
{
LOGE("g_clientEndpoint Out of memory");
free(cloneUri);
return;
}
- memcpy(g_clientEndpoint, object, sizeof(CARemoteEndpoint_t));
+ memcpy(g_clientEndpoint, object, sizeof(CAEndpoint_t));
if (NULL != cloneUri)
{
len = strlen(cloneUri);
- g_clientEndpoint->resourceUri = (char *) malloc(sizeof(char) * (len + 1));
- if (NULL == g_clientEndpoint)
+ g_resourceUri = (char *) malloc(sizeof(char) * (len + 1));
+ if (NULL == g_resourceUri)
{
LOGE("g_clientEndpoint->resourceUri Out of memory");
free(g_clientEndpoint);
free(cloneUri);
return;
}
- memcpy(g_clientEndpoint->resourceUri, cloneUri, len + 1);
+ memcpy(g_resourceUri, cloneUri, len + 1);
free(cloneUri);
}
//clone g_clientToken
if (NULL == g_clientToken)
{
LOGE("g_clientToken Out of memory");
- free(g_clientEndpoint->resourceUri);
free(g_clientEndpoint);
return;
}
if (NULL == clonePayload)
{
LOGE("clonePayload Out of memory");
- free(g_clientEndpoint->resourceUri);
free(g_clientEndpoint);
return;
}
callback("Option info: ", optionInfo);
- uint32_t optionDataLen = strlen(requestInfo->info.options[i].optionData);
+ size_t optionDataLen = strlen(requestInfo->info.options[i].optionData);
char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
if (NULL == cloneOptionData)
{
LOGE("cloneOptionData Out of memory");
- free(g_clientEndpoint->resourceUri);
free(g_clientEndpoint);
return;
}
LOGI("############################################################");
//Check if this has secure communication information
- if (requestInfo->info.payload && CA_IPV4 == object->transportType)
+ if (requestInfo->info.payload && CA_ADAPTER_IP == object->adapter)
{
uint32_t securePort = get_secure_information(requestInfo->info.payload);
if (0 < securePort) //Set the remote endpoint secure details and send response
{
LOGI("This is secure resource...");
- char *uri = NULL;
- uint32_t length = 0;
-
- length = sizeof(SECURE_COAPS_PREFIX) - 1; //length of "coaps://"
- // length of "ipaddress:port"
- length += strlen(object->addressInfo.IP.ipAddress) + PORT_LENGTH;
- length += strlen(object->resourceUri) + 1;
-
- uri = calloc(1, sizeof(char) * length);
- if (!uri)
- {
- LOGE("Failed to create new uri");
- free(uri);
- return;
- }
- sprintf(uri, "%s%s:%d/%s", SECURE_COAPS_PREFIX, object->addressInfo.IP.ipAddress,
- securePort, object->resourceUri);
- CARemoteEndpoint_t *endpoint = NULL;
- if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->transportType, &endpoint))
+ CAEndpoint_t *endpoint = NULL;
+ if (CA_STATUS_OK != CACreateEndpoint(CA_SECURE,
+ object->adapter, object->addr, securePort, &endpoint))
{
LOGE("Failed to create duplicate of remote endpoint!");
- free(uri);
return;
}
- endpoint->isSecured = true;
object = endpoint;
-
- free(uri);
}
}
}
-void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
+void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo)
{
- CAResult_t res = get_remote_address(object->transportType, object->addressInfo);
+ CAResult_t res = get_remote_address(object->adapter, object->addr);
if (CA_STATUS_OK != res)
{
return;
}
LOGI("##########Received response from remote device #############");
- LOGI("Uri: %s", object->resourceUri);
+ LOGI("Uri: %s", responseInfo->info.resourceUri);
LOGI("Remote Address: %s", g_remoteAddress);
LOGI("response result: %d", responseInfo->result);
LOGI("Data: %s", responseInfo->info.payload);
{
uint32_t len = 0;
- if (NULL != object->resourceUri)
+ if (NULL != responseInfo->info.resourceUri)
{
- len = strlen(object->resourceUri);
+ len = strlen(responseInfo->info.resourceUri);
char *cloneUri = (char *) malloc(sizeof(char) * (len + 1));
if (NULL == cloneUri)
return;
}
- memcpy(cloneUri, object->resourceUri, len + 1);
+ memcpy(cloneUri, responseInfo->info.resourceUri, len + 1);
callback("Uri: ", cloneUri);
free(cloneUri);
callback("Option info: ", optionInfo);
- uint32_t optionDataLen = strlen(responseInfo->info.options[i].optionData);
+ size_t optionDataLen = strlen(responseInfo->info.options[i].optionData);
char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
if (NULL == cloneOptionData)
{
LOGI("############################################################");
//Check if this has secure communication information
- if (responseInfo->info.payload && CA_IPV4 == object->transportType)
+ if (responseInfo->info.payload && CA_ADAPTER_IP == object->adapter)
{
uint32_t securePort = get_secure_information(responseInfo->info.payload);
if (0 < securePort) //Set the remote endpoint secure details and send response
}
}
+void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
+{
+ printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++");
+
+ if(errorInfo)
+ {
+ const CAInfo_t *info = &errorInfo->info;
+ LOGI("Error Handler, ErrorInfo :");
+ LOGI("Error Handler result : %d", errorInfo->result);
+ LOGI("Error Handler token : %s", info->token);
+ LOGI("Error Handler messageId : %d", (uint16_t) info->messageId);
+ LOGI("Error Handler resourceUri : %s", info->resourceUri);
+ LOGI("Error Handler type : %d", info->type);
+ LOGI("Error Handler payload : %s", info->payload);
+
+ if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
+ {
+ LOGE("CA_ADAPTER_NOT_ENABLED, enable the adapter");
+ }
+ else if(CA_SEND_FAILED == errorInfo->result)
+ {
+ LOGE("CA_SEND_FAILED, unable to send the message, check parameters");
+ }
+ else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
+ {
+ LOGE("CA_MEMORY_ALLOC_FAILED, insufficient memory");
+ }
+ else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
+ {
+ LOGE("CA_SOCKET_OPERATION_FAILED, socket operation failed");
+ }
+ else if(CA_STATUS_FAILED == errorInfo->result)
+ {
+ LOGE("CA_STATUS_FAILED, message could not be delivered, internal error");
+ }
+ }
+ LOGI("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++");
+
+ return;
+}
+
void get_resource_uri(const char *URI, char *resourceURI, uint32_t length)
{
const char *startPos = URI;
- const char *temp = NULL;
- if (NULL != (temp = strstr(URI, "://")))
+ const char *temp = strstr(URI, "://");
+ if (NULL != temp)
{
startPos = strchr(temp + 3, '/');
if (!startPos)
{
return CA_NOT_SUPPORTED;
}
- if (number & CA_IPV4)
+ if (number & CA_ADAPTER_IP)
{
- g_selectedNwType = CA_IPV4;
+ g_selectedNwType = CA_ADAPTER_IP;
return CA_STATUS_OK;
}
- if (number & CA_EDR)
+ if (number & CA_ADAPTER_RFCOMM_BTEDR)
{
- g_selectedNwType = CA_EDR;
+ g_selectedNwType = CA_ADAPTER_RFCOMM_BTEDR;
return CA_STATUS_OK;
}
- if (number & CA_LE)
+ if (number & CA_ADAPTER_GATT_BTLE)
{
- g_selectedNwType = CA_LE;
+ g_selectedNwType = CA_ADAPTER_GATT_BTLE;
return CA_STATUS_OK;
}
}
-CAResult_t get_remote_address(CATransportType_t transportType, CAAddress_t addressInfo)
+CAResult_t get_remote_address(CATransportAdapter_t transportType, const char *address)
{
+ uint32_t len = strlen(address);
- uint32_t len = 0;
- if (CA_IPV4 == transportType)
+ g_remoteAddress = (char *)malloc(sizeof (char) * (len + 1));
+ if (NULL == g_remoteAddress)
{
- len = strlen(addressInfo.IP.ipAddress);
- g_remoteAddress = (char *) malloc(sizeof(char) * (len + 1));
+ LOGE("g_remoteAddress Out of memory");
+ return CA_MEMORY_ALLOC_FAILED;
+ }
+
+ memcpy(g_remoteAddress, address, len + 1);
+
+ return CA_STATUS_OK;
+}
+
+
+void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
+{
+ if (NULL == uri || NULL == address)
+ {
+ LOGE("parameter is null");
+ return;
+ }
+
+ // parse uri
+ // #1. check prefix
+ uint8_t startIndex = 0;
+ if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
+ {
+ LOGI("uri has '%s' prefix", COAPS_PREFIX);
+ startIndex = COAPS_PREFIX_LEN;
+ *flags = CA_SECURE;
+ }
+ else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
+ {
+ LOGI("uri has '%s' prefix", COAP_PREFIX);
+ startIndex = COAP_PREFIX_LEN;
+ *flags = CA_DEFAULT_FLAGS;
+ }
+
+ // #2. copy uri for parse
+ int32_t len = strlen(uri) - startIndex;
+
+ if (len <= 0)
+ {
+ LOGE("uri length is 0!");
+ return;
+ }
+
+ char *cloneUri = (char *) calloc(len + 1, sizeof(char));
+ if (NULL == cloneUri)
+ {
+ LOGE("Out of memory");
+ return;
+ }
+
+ memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
+ cloneUri[len] = '\0';
- if (NULL == g_remoteAddress)
+ char *pAddress = cloneUri;
+ char *pResourceUri = NULL;
+
+ int32_t i = 0;
+ for (i = 0; i < len; i++)
+ {
+ if (cloneUri[i] == '/')
{
- LOGE("g_remoteAddress Out of memory");
- return CA_MEMORY_ALLOC_FAILED;
+ // separate
+ cloneUri[i] = 0;
+ pResourceUri = &cloneUri[i + 1];
+ break;
}
+ }
+ LOGI("pAddress : %s", pAddress);
- memcpy(g_remoteAddress, addressInfo.IP.ipAddress, len + 1);
+ int res = get_address_set(pAddress, address);
+ if (res == -1)
+ {
+ LOGE("address parse error");
+
+ free(cloneUri);
+ return;
}
+ return;
+}
- else if (CA_EDR == transportType)
+int get_address_set(const char *pAddress, addressSet_t* outAddress)
+{
+ if (NULL == pAddress || NULL == outAddress)
{
- len = strlen(addressInfo.BT.btMacAddress);
- g_remoteAddress = (char *) malloc(sizeof(char) * (len + 1));
+ LOGE("parameter is null");
+ return -1;
+ }
- if (NULL == g_remoteAddress)
+ int32_t len = strlen(pAddress);
+ int32_t isIp = 0;
+ int32_t ipLen = 0;
+
+ int32_t i = 0;
+ for (i = 0; i < len; i++)
+ {
+ if (pAddress[i] == '.')
{
- LOGE("g_remoteAddress Out of memory");
- return CA_MEMORY_ALLOC_FAILED;
+ isIp = 1;
}
- memcpy(g_remoteAddress, addressInfo.BT.btMacAddress, len + 1);
+ // found port number start index
+ if (isIp && pAddress[i] == ':')
+ {
+ ipLen = i;
+ break;
+ }
}
- else if (CA_LE == transportType)
+ if (isIp)
{
- len = strlen(addressInfo.LE.leMacAddress);
- g_remoteAddress = (char *) malloc(sizeof(char) * (len + 1));
-
- if (NULL == g_remoteAddress)
+ if(ipLen && ipLen < sizeof(outAddress->ipAddress))
+ {
+ strncpy(outAddress->ipAddress, pAddress, ipLen);
+ outAddress->ipAddress[ipLen] = '\0';
+ }
+ else if (!ipLen && len < sizeof(outAddress->ipAddress))
+ {
+ strncpy(outAddress->ipAddress, pAddress, len);
+ outAddress->ipAddress[len] = '\0';
+ }
+ else
{
- LOGE("g_remoteAddress Out of memory");
- return CA_MEMORY_ALLOC_FAILED;
+ LOGE("IP Address too long: %d", ipLen==0 ? len : ipLen);
+ return -1;
}
- memcpy(g_remoteAddress, addressInfo.LE.leMacAddress, len + 1);
+ if (ipLen > 0)
+ {
+ outAddress->port = atoi(pAddress + ipLen + 1);
+ }
+ }
+ else
+ {
+ strncpy(outAddress->ipAddress, pAddress, len);
+ outAddress->ipAddress[len] = '\0';
}
- return CA_STATUS_OK;
+ return isIp;
}