#include "cainterface.h"
#include "cacommon.h"
+#include "caadapterutils.h"
#include "org_iotivity_ca_service_RMInterface.h"
uint16_t port;
} addressSet_t;
-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(CATransportAdapter_t transportType, const char *address);
-void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
+static void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo);
+static void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo);
+static void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
+static void get_resource_uri(const char *URI, char *resourceURI, int32_t length);
+static uint32_t get_secure_information(CAPayload_t payLoad);
+static CAResult_t get_network_type(uint32_t selectedNetwork);
+static void callback(char *subject, char *receivedData);
+static CAResult_t get_remote_address(const char *address);
+static void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
+static void delete_global_references(JNIEnv *env, jobject obj);
+static int get_address_set(const char *pAddress, addressSet_t* outAddress);
uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
CATransportAdapter_t g_selectedNwType = CA_ADAPTER_IP;
jobject listener)
{
LOGI("setNativeResponseListener");
- g_responseListenerObject = (*env)->NewGlobalRef(env, obj);
+ if (!env || !obj || !listener)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
+
+ g_responseListenerObject = (*env)->NewGlobalRef(env, listener);
}
#ifdef __WITH_DTLS__
Java_org_iotivity_ca_service_RMInterface_RMInitialize(JNIEnv *env, jobject obj, jobject context)
{
LOGI("RMInitialize");
+ if (!env || !obj || !context)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
//Currently set context for Android Platform
CANativeJNISetContext(env, context);
Java_org_iotivity_ca_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
{
LOGI("RMTerminate");
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
+
CADestroyToken(g_lastRequestToken);
CATerminate();
+ delete_global_references(env, obj);
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMStartListeningServer(JNIEnv *env, jobject obj)
{
LOGI("RMStartListeningServer");
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
if (CA_STATUS_OK != CAStartListeningServer())
{
Java_org_iotivity_ca_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env, jobject obj)
{
LOGI("RMStartDiscoveryServer");
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
if (CA_STATUS_OK != CAStartDiscoveryServer())
{
Java_org_iotivity_ca_service_RMInterface_RMRegisterHandler(JNIEnv *env, jobject obj)
{
LOGI("RMRegisterHandler");
+ if(!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
CARegisterHandler(request_handler, response_handler, error_handler);
}
jint isSecured, jint msgType)
{
LOGI("selectedNetwork - %d", selectedNetwork);
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
+
+ if (!payload)
+ {
+ LOGE("payload is NULL");
+ }
+
+ if (!uri)
+ {
+ LOGE("Invalid input parameter : uri");
+ return;
+ }
+
CAResult_t res = get_network_type(selectedNetwork);
if (CA_STATUS_OK != res)
{
LOGI("RMSendRequest - %s", strUri);
CATransportFlags_t flags;
- addressSet_t address = {};
+ addressSet_t address = {{0}, 0};
parsing_coap_uri(strUri, &address, &flags);
//create remote endpoint
CAEndpoint_t* endpoint = NULL;
- res = CACreateEndpoint(flags, g_selectedNwType, (const char*)address.ipAddress,
+ res = CACreateEndpoint(flags, g_selectedNwType, (const char*)(address.ipAddress),
address.port, &endpoint);
if (CA_STATUS_OK != res)
{
CADestroyEndpoint(endpoint);
return;
}
- snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
+ snprintf((char *) requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
requestData.payloadSize = length;
}
else
CADestroyEndpoint(endpoint);
return;
}
- snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
+ snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
requestData.payloadSize = length;
}
jint selectedNetwork)
{
LOGI("selectedNetwork - %d", selectedNetwork);
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
+
CAResult_t res = get_network_type(selectedNetwork);
if (CA_STATUS_OK != res)
{
jint responseValue)
{
LOGI("RMSendResponse");
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
LOGI("selectedNetwork - %d", selectedNetwork);
{
uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_resourceUri) + 1;
responseData.payload = (CAPayload_t) malloc(length);
- sprintf(responseData.payload, SECURE_INFO_DATA, g_resourceUri,
+ sprintf((char *) responseData.payload, SECURE_INFO_DATA, g_resourceUri,
g_localSecurePort);
responseData.payloadSize = length;
}
{
uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_resourceUri) + 1;
responseData.payload = (CAPayload_t) malloc(length);
- sprintf(responseData.payload, NORMAL_INFO_DATA, g_resourceUri);
+ sprintf((char *) responseData.payload, NORMAL_INFO_DATA, g_resourceUri);
responseData.payloadSize = length;
}
}
jint responseValue)
{
LOGI("selectedNetwork - %d", selectedNetwork);
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
+
+ if (!payload)
+ {
+ LOGE("payload is NULL");
+ }
+
+ if (!uri)
+ {
+ LOGE("Invalid input parameter : uri");
+ return;
+ }
CAResult_t res = get_network_type(selectedNetwork);
if (CA_STATUS_OK != res)
LOGI("RMSendNotification - %s", strUri);
CATransportFlags_t flags;
- addressSet_t address = {};
+ addressSet_t address = {{0}, 0};
parsing_coap_uri(strUri, &address, &flags);
//create remote endpoint
free(responseData.resourceUri);
return;
}
- snprintf(responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
+ snprintf((char *) responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
responseData.payloadSize = length;
}
else
free(responseData.resourceUri);
return;
}
- snprintf(responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
+ snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
responseData.payloadSize = length;
}
jint networkType)
{
LOGI("RMSelectNetwork Type : %d", networkType);
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
if (CA_STATUS_OK != CASelectNetwork(networkType))
{
jint networkType)
{
LOGI("RMUnSelectNetwork Type : %d", networkType);
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
if (CA_STATUS_OK != CAUnSelectNetwork(networkType))
{
Java_org_iotivity_ca_service_RMInterface_RMGetNetworkInfomation(JNIEnv *env, jobject obj)
{
LOGI("RMGetNetworkInfomation");
+ if (!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
CAEndpoint_t *tempInfo = NULL;
uint32_t tempSize = 0;
uint32_t index;
for (index = 0; index < tempSize; index++)
{
- res = get_remote_address(tempInfo[index].adapter, tempInfo[index].addr);
+ res = get_remote_address(tempInfo[index].addr);
if (CA_STATUS_OK != res)
{
free(tempInfo);
Java_org_iotivity_ca_service_RMInterface_RMHandleRequestResponse(JNIEnv *env, jobject obj)
{
LOGI("RMHandleRequestResponse");
+ if(!env || !obj)
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
if (CA_STATUS_OK != CAHandleRequestResponse())
{
return;
}
- CAResult_t res = get_remote_address(object->adapter, object->addr);
+ CAResult_t res = get_remote_address(object->addr);
if (CA_STATUS_OK != res)
{
return;
void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo)
{
+ if (!object || !responseInfo)
+ {
+ LOGE("Invalid input parameter");
+ return;
+ }
- CAResult_t res = get_remote_address(object->adapter, object->addr);
+ CAResult_t res = get_remote_address(object->addr);
if (CA_STATUS_OK != res)
{
return;
void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
{
- printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++");
+ LOGI("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++");
+
+ if (rep)
+ {
+ LOGI("Error Handler, Adapter Type : %d", rep->adapter);
+ LOGI("Error Handler, Adapter Type : %s", rep->addr);
+ }
- if(errorInfo)
+ if (errorInfo)
{
const CAInfo_t *info = &errorInfo->info;
LOGI("Error Handler, ErrorInfo :");
return;
}
-void get_resource_uri(const char *URI, char *resourceURI, uint32_t length)
+void get_resource_uri(const char *URI, char *resourceURI, int32_t length)
{
const char *startPos = URI;
const char *temp = strstr(URI, "://");
}
const char *subString = NULL;
- if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
+ if (NULL == (subString = strstr((const char *) payLoad, "\"sec\":1")))
{
LOGE("This is not secure resource");
return -1;
}
- if (NULL == (subString = strstr(payLoad, "\"port\":")))
+ if (NULL == (subString = strstr((const char *) payLoad, "\"port\":")))
{
LOGE("This secure resource does not have port information");
return -1;
void callback(char *subject, char *receivedData)
{
- JNIEnv* env = NULL;
- uint32_t status = (*g_jvm)->GetEnv(g_jvm, (void **) &env, JNI_VERSION_1_6);
- uint32_t res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
+ bool isAttached = false;
+ JNIEnv* env;
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
+ {
+ LOGI("Could not get JNIEnv pointer");
+ res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
+
+ if (JNI_OK != res)
+ {
+ LOGE("AttachCurrentThread has failed");
+ return;
+ }
+ isAttached = true;
+ }
jclass cls = (*env)->GetObjectClass(env, g_responseListenerObject);
jmethodID mid = (*env)->GetMethodID(env, cls, "OnResponseReceived",
"(Ljava/lang/String;Ljava/lang/String;)V");
+ if (!mid)
+ {
+ LOGE("could not get Method ID");
+ goto detach_thread;
+ }
jstring jsubject = (*env)->NewStringUTF(env, (char*) subject);
+ if (!jsubject)
+ {
+ LOGE("NewStringUTF failed");
+ goto detach_thread;
+ }
+
jstring jreceivedData = (*env)->NewStringUTF(env, (char*) receivedData);
+ if (!jreceivedData)
+ {
+ LOGE("NewStringUTF failed");
+ goto detach_thread;
+ }
+
(*env)->CallVoidMethod(env, g_responseListenerObject, mid, jsubject, jreceivedData);
+detach_thread :
+ if (isAttached)
+ {
+ (*g_jvm)->DetachCurrentThread(g_jvm);
+ LOGI("DetachCurrentThread");
+ }
}
-CAResult_t get_remote_address(CATransportAdapter_t transportType, const char *address)
+CAResult_t get_remote_address(const char *address)
{
uint32_t len = strlen(address);
cloneUri[len] = '\0';
char *pAddress = cloneUri;
- char *pResourceUri = NULL;
-
- int32_t i = 0;
- for (i = 0; i < len; i++)
- {
- if (cloneUri[i] == '/')
- {
- // separate
- cloneUri[i] = 0;
- pResourceUri = &cloneUri[i + 1];
- break;
- }
- }
LOGI("pAddress : %s", pAddress);
int res = get_address_set(pAddress, address);
free(cloneUri);
return;
}
+
return;
}
return -1;
}
- int32_t len = strlen(pAddress);
- int32_t isIp = 0;
- int32_t ipLen = 0;
+ size_t len = strlen(pAddress);
+ int isIp = 0;
+ size_t ipLen = 0;
- int32_t i = 0;
- for (i = 0; i < len; i++)
+ for (size_t i = 0; i < len; i++)
{
if (pAddress[i] == '.')
{
if (isIp)
{
- if(ipLen && ipLen < sizeof(outAddress->ipAddress))
+ if(ipLen && (ipLen < sizeof(outAddress->ipAddress)))
{
strncpy(outAddress->ipAddress, pAddress, ipLen);
outAddress->ipAddress[ipLen] = '\0';
}
- else if (!ipLen && len < sizeof(outAddress->ipAddress))
+ else if (!ipLen && (len < sizeof(outAddress->ipAddress)))
{
strncpy(outAddress->ipAddress, pAddress, len);
outAddress->ipAddress[len] = '\0';
return isIp;
}
+
+void delete_global_references(JNIEnv *env, jobject obj)
+{
+ LOGI("delete_global_references");
+ if (!env || !obj )
+ {
+ LOGI("Invalid input parameter");
+ return;
+ }
+
+ (*env)->DeleteGlobalRef(env, g_responseListenerObject);
+}
static void CAHandleNetlink()
{
#ifdef __linux__
-char buf[4096];
-struct nlmsghdr *nh;
-struct sockaddr_nl sa;
-struct iovec iov = { buf, sizeof(buf) };
-struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
-
-int len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
-for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
-{
- if (nh->nlmsg_type == RTM_NEWLINK)
- {
- struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
- if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
- {
- continue;
- }
-
- int newIndex = ifi->ifi_index;
+ char buf[4096];
+ struct nlmsghdr *nh;
+ struct sockaddr_nl sa;
+ struct iovec iov = { buf, sizeof(buf) };
+ struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
- u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
- if (!iflist)
- {
- OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
- return;
- }
+ size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
- uint32_t len = u_arraylist_length(iflist);
- for (uint32_t i = 0; i < len; i++)
+ for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
+ {
+ if (nh->nlmsg_type == RTM_NEWLINK)
{
- CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
- if (!ifitem)
+ struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
+ if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
{
continue;
}
- if ((int)ifitem->index != newIndex)
+ int newIndex = ifi->ifi_index;
+
+ u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
+ if (!iflist)
{
- continue;
+ OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
+ return;
}
- applyMulticastToInterface6(newIndex);
- struct in_addr inaddr;
- inaddr.s_addr = ifitem->ipv4addr;
- applyMulticastToInterface4(inaddr);
- break; // we found the one we were looking for
+ uint32_t listLength = u_arraylist_length(iflist);
+ for (uint32_t i = 0; i < listLength; i++)
+ {
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
+ {
+ continue;
+ }
+
+ if ((int)ifitem->index != newIndex)
+ {
+ continue;
+ }
+
+ applyMulticastToInterface6(newIndex);
+ struct in_addr inaddr;
+ inaddr.s_addr = ifitem->ipv4addr;
+ applyMulticastToInterface4(inaddr);
+ break; // we found the one we were looking for
+ }
+ u_arraylist_destroy(iflist);
}
- u_arraylist_destroy(iflist);
}
-}
#endif // __linux__
}