2 #include <android/log.h>
8 #include "cainterface.h"
11 #include "org_iotivity_ca_service_RMInterface.h"
13 #define LOG_TAG "JNI_INTERFACE_SAMPLE"
14 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
15 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
21 #define IDENTITY ("1111111111111111")
25 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
28 #define SECURE_DEFAULT_PORT 5684
29 #define RESOURCE_URI_LENGTH 14
30 #define OPTION_INFO_LENGTH 1024
31 #define NETWORK_INFO_LENGTH 1024
35 char ipAddress[CA_IPADDR_SIZE];
39 void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo);
40 void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo);
41 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
42 void get_resource_uri(const char *URI, char *resourceURI, uint32_t length);
43 uint32_t get_secure_information(CAPayload_t payLoad);
44 CAResult_t get_network_type(uint32_t selectedNetwork);
45 void callback(char *subject, char *receivedData);
46 CAResult_t get_remote_address(CATransportAdapter_t transportType, const char *address);
47 void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
49 uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
50 CATransportAdapter_t g_selectedNwType = CA_ADAPTER_IP;
51 static CAToken_t g_lastRequestToken = NULL;
52 static uint8_t g_lastRequestTokenLength = 0;
54 static const char COAP_PREFIX[] = "coap://";
55 static const char COAPS_PREFIX[] = "coaps://";
56 static const uint16_t COAP_PREFIX_LEN = sizeof(COAP_PREFIX) - 1;
57 static const uint16_t COAPS_PREFIX_LEN = sizeof(COAPS_PREFIX) - 1;
59 static const char SECURE_INFO_DATA[]
60 = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
61 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":"
63 static const char NORMAL_INFO_DATA[]
64 = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
65 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
67 static jobject g_responseListenerObject = NULL;
70 static CAEndpoint_t *g_clientEndpoint = NULL;
71 static char *g_resourceUri = NULL;
72 static CAToken_t g_clientToken = NULL;
73 static uint8_t g_clientTokenLength = 0;
75 static uint16_t g_clientMsgId = 0;
76 static char *g_remoteAddress = NULL;
79 JNIEXPORT void JNICALL
80 Java_org_iotivity_ca_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj,
83 LOGI("setNativeResponseListener");
84 g_responseListenerObject = (*env)->NewGlobalRef(env, obj);
88 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
90 void clearDtlsCredentialInfo()
92 LOGI("clearDtlsCredentialInfo IN");
95 // Initialize sensitive data to zeroes before freeing.
96 if (NULL != pskCredsBlob->creds)
98 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds)*(pskCredsBlob->num));
99 free(pskCredsBlob->creds);
102 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
106 LOGI("clearDtlsCredentialInfo OUT");
109 // Internal API. Invoked by OC stack to retrieve credentials from this module
110 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
112 LOGI("CAGetDtlsPskCredentials IN");
113 *credInfo = (CADtlsPskCredsBlob_t *) malloc(sizeof(CADtlsPskCredsBlob_t));
114 if (NULL == *credInfo)
116 LOGE("Failed to allocate credential blob.");
120 int16_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
121 (*credInfo)->creds = (OCDtlsPskCreds *) malloc(credLen);
122 if (NULL == (*credInfo)->creds)
124 LOGE("Failed to allocate crentials.");
130 memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
131 (*credInfo)->num = pskCredsBlob->num;
132 memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
134 LOGI("CAGetDtlsPskCredentials OUT");
137 CAResult_t SetCredentials()
139 LOGI("SetCredentials IN");
140 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
141 if (NULL == pskCredsBlob)
143 LOGE("Memory allocation failed!");
144 return CA_MEMORY_ALLOC_FAILED;
146 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
148 pskCredsBlob->num = 1;
150 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) *(pskCredsBlob->num));
151 if (NULL == pskCredsBlob->creds)
153 LOGE("Memory allocation failed!");
154 return CA_MEMORY_ALLOC_FAILED;
156 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
157 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
159 LOGI("SetCredentials OUT");
164 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
169 if (JNI_OK != (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6))
173 g_jvm = jvm; /* cache the JavaVM pointer */
175 CANativeJNISetJavaVM(g_jvm);
177 return JNI_VERSION_1_6;
180 void JNI_OnUnload(JavaVM *jvm, void *reserved)
182 LOGI("JNI_OnUnload");
185 if (JNI_OK != (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6))
193 JNIEXPORT void JNICALL
194 Java_org_iotivity_ca_service_RMInterface_RMInitialize(JNIEnv *env, jobject obj, jobject context)
196 LOGI("RMInitialize");
198 //Currently set context for Android Platform
199 CANativeJNISetContext(env, context);
201 CAResult_t res = CAInitialize();
203 if (CA_STATUS_OK != res)
205 LOGE("Could not Initialize");
209 if (CA_STATUS_OK != SetCredentials())
211 LOGE("SetCredentials failed");
215 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
216 if(CA_STATUS_OK != res)
218 LOGE("Set credential handler fail");
224 JNIEXPORT void JNICALL
225 Java_org_iotivity_ca_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
228 CADestroyToken(g_lastRequestToken);
232 JNIEXPORT void JNICALL
233 Java_org_iotivity_ca_service_RMInterface_RMStartListeningServer(JNIEnv *env, jobject obj)
235 LOGI("RMStartListeningServer");
237 if (CA_STATUS_OK != CAStartListeningServer())
239 LOGE("Could not start Listening server");
243 JNIEXPORT void JNICALL
244 Java_org_iotivity_ca_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env, jobject obj)
246 LOGI("RMStartDiscoveryServer");
248 if (CA_STATUS_OK != CAStartDiscoveryServer())
250 LOGE("Could not start discovery server");
254 JNIEXPORT void JNICALL
255 Java_org_iotivity_ca_service_RMInterface_RMRegisterHandler(JNIEnv *env, jobject obj)
257 LOGI("RMRegisterHandler");
259 CARegisterHandler(request_handler, response_handler, error_handler);
262 JNIEXPORT void JNICALL
263 Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
264 jstring payload, jint selectedNetwork,
265 jint isSecured, jint msgType)
267 LOGI("selectedNetwork - %d", selectedNetwork);
268 CAResult_t res = get_network_type(selectedNetwork);
269 if (CA_STATUS_OK != res)
274 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
275 LOGI("RMSendRequest - %s", strUri);
277 CATransportFlags_t flags;
278 addressSet_t address = {};
279 parsing_coap_uri(strUri, &address, &flags);
281 //create remote endpoint
282 CAEndpoint_t* endpoint = NULL;
283 res = CACreateEndpoint(flags, g_selectedNwType, (const char*)address.ipAddress,
284 address.port, &endpoint);
285 if (CA_STATUS_OK != res)
287 LOGE("Could not create remote end point");
288 (*env)->ReleaseStringUTFChars(env, uri, strUri);
292 CAMessageType_t messageType = msgType;
295 CAToken_t token = NULL;
296 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
298 res = CAGenerateToken(&token, tokenLength);
299 if ((CA_STATUS_OK != res) || (!token))
301 LOGE("token generate error!!");
302 // destroy remote endpoint
303 CADestroyEndpoint(endpoint);
304 (*env)->ReleaseStringUTFChars(env, uri, strUri);
308 char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
310 get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
311 (*env)->ReleaseStringUTFChars(env, uri, strUri);
313 CAInfo_t requestData = { 0 };
314 requestData.token = token;
315 requestData.tokenLength = tokenLength;
319 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
320 requestData.payload = (CAPayload_t) malloc(length);
321 if (NULL == requestData.payload)
323 LOGE("Memory allocation failed!");
325 CADestroyToken(token);
326 // destroy remote endpoint
327 CADestroyEndpoint(endpoint);
330 snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
331 requestData.payloadSize = length;
335 uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
336 requestData.payload = (CAPayload_t) malloc(length);
337 if (NULL == requestData.payload)
339 LOGE("Memory allocation failed!");
341 CADestroyToken(token);
342 // destroy remote endpoint
343 CADestroyEndpoint(endpoint);
346 snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
347 requestData.payloadSize = length;
350 requestData.type = messageType;
351 requestData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
352 if (NULL == requestData.resourceUri)
354 LOGE("Memory allocation failed!");
356 CADestroyToken(token);
357 // destroy remote endpoint
358 CADestroyEndpoint(endpoint);
359 free(requestData.payload);
362 memcpy(requestData.resourceUri, resourceURI, sizeof(resourceURI));
364 CARequestInfo_t requestInfo = { 0 };
365 requestInfo.method = CA_GET;
366 requestInfo.isMulticast = false;
367 requestInfo.info = requestData;
370 if (CA_STATUS_OK != CASendRequest(endpoint, &requestInfo))
372 LOGE("Could not send request");
376 CADestroyToken(token);
378 // destroy remote endpoint
379 CADestroyEndpoint(endpoint);
381 free(requestData.payload);
382 free(requestData.resourceUri);
385 JNIEXPORT void JNICALL
386 Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
387 jint selectedNetwork)
389 LOGI("selectedNetwork - %d", selectedNetwork);
390 CAResult_t res = get_network_type(selectedNetwork);
391 if (CA_STATUS_OK != res)
396 // create remote endpoint
397 CAEndpoint_t *endpoint = NULL;
398 res = CACreateEndpoint(CA_IPV4, g_selectedNwType, NULL, 0, &endpoint);
400 if (CA_STATUS_OK != res)
402 LOGE("create remote endpoint error, error code: %d", res);
407 CAToken_t token = NULL;
408 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
410 res = CAGenerateToken(&token, tokenLength);
411 if ((CA_STATUS_OK != res) || (!token))
413 LOGE("token generate error!!");
414 // destroy remote endpoint
415 CADestroyEndpoint(endpoint);
419 LOGI("generated token %s", token);
421 CAInfo_t requestData = { 0 };
422 requestData.token = token;
423 requestData.tokenLength = tokenLength;
424 requestData.payload = (CAPayload_t) "TempJsonPayload";
425 requestData.payloadSize = strlen((const char *) requestData.payload);
426 requestData.type = CA_MSG_NONCONFIRM;
428 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
429 LOGI("resourceUri - %s", strUri);
430 requestData.resourceUri = (CAURI_t)strUri;
432 uint8_t optionNum = 2;
433 CAHeaderOption_t *headerOpt = (CAHeaderOption_t*) calloc(1,
434 sizeof(CAHeaderOption_t) * optionNum);
435 if (NULL == headerOpt)
437 LOGE("Memory allocation failed");
438 // destroy remote endpoint
439 CADestroyEndpoint(endpoint);
443 char* FirstOptionData = "Hello";
444 headerOpt[0].optionID = 3000;
445 memcpy(headerOpt[0].optionData, FirstOptionData, strlen(FirstOptionData));
446 headerOpt[0].optionLength = (uint16_t) strlen(FirstOptionData);
448 char* SecondOptionData2 = "World";
449 headerOpt[1].optionID = 3001;
450 memcpy(headerOpt[1].optionData, SecondOptionData2, strlen(SecondOptionData2));
451 headerOpt[1].optionLength = (uint16_t) strlen(SecondOptionData2);
453 requestData.numOptions = optionNum;
454 requestData.options = headerOpt;
456 CARequestInfo_t requestInfo = { 0 };
457 requestInfo.method = CA_GET;
458 requestInfo.isMulticast = true;
459 requestInfo.info = requestData;
461 // send request to all
462 res = CASendRequest(endpoint, &requestInfo);
463 if (CA_STATUS_OK != res)
465 LOGE("Could not send request to all");
467 CADestroyToken(token);
471 CADestroyToken(g_lastRequestToken);
472 g_lastRequestToken = token;
473 g_lastRequestTokenLength = tokenLength;
476 //ReleaseStringUTFChars for strUri
477 (*env)->ReleaseStringUTFChars(env, uri, strUri);
481 // destroy remote endpoint
482 CADestroyEndpoint(endpoint);
485 JNIEXPORT void JNICALL
486 Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
487 jint selectedNetwork,
488 jint isSecured, jint msgType,
491 LOGI("RMSendResponse");
493 LOGI("selectedNetwork - %d", selectedNetwork);
495 CAResult_t res = get_network_type(selectedNetwork);
496 if (CA_STATUS_OK != res)
498 LOGE("Not supported network type");
502 if (NULL == g_clientEndpoint)
504 LOGE("No Request received");
508 CAMessageType_t messageType = msgType;
510 CAInfo_t responseData = { 0 };
511 responseData.type = messageType;
512 responseData.messageId = g_clientMsgId;
513 responseData.resourceUri = (CAURI_t)g_resourceUri;
515 CAResponseInfo_t responseInfo = { 0 };
517 if (msgType != CA_MSG_RESET)
519 responseData.token = g_clientToken;
520 responseData.tokenLength = g_clientTokenLength;
521 responseInfo.result = responseValue;
525 uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_resourceUri) + 1;
526 responseData.payload = (CAPayload_t) malloc(length);
527 sprintf(responseData.payload, SECURE_INFO_DATA, g_resourceUri,
529 responseData.payloadSize = length;
533 uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_resourceUri) + 1;
534 responseData.payload = (CAPayload_t) malloc(length);
535 sprintf(responseData.payload, NORMAL_INFO_DATA, g_resourceUri);
536 responseData.payloadSize = length;
542 responseInfo.result = CA_EMPTY;
545 responseInfo.info = responseData;
548 res = CASendResponse(g_clientEndpoint, &responseInfo);
549 if (CA_STATUS_OK != res)
551 LOGE("Could not send response");
555 CADestroyToken(g_clientToken);
556 g_clientToken = NULL;
557 g_clientTokenLength = 0;
559 // destroy remote endpoint
560 CADestroyEndpoint(g_clientEndpoint);
561 g_clientEndpoint = NULL;
564 JNIEXPORT void JNICALL
565 Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
566 jstring payload, jint selectedNetwork,
567 jint isSecured, jint msgType,
570 LOGI("selectedNetwork - %d", selectedNetwork);
572 CAResult_t res = get_network_type(selectedNetwork);
573 if (CA_STATUS_OK != res)
575 LOGE("Not supported network type");
579 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
580 LOGI("RMSendNotification - %s", strUri);
582 CATransportFlags_t flags;
583 addressSet_t address = {};
584 parsing_coap_uri(strUri, &address, &flags);
586 //create remote endpoint
587 CAEndpoint_t* endpoint = NULL;
588 if (CA_STATUS_OK != CACreateEndpoint(flags, g_selectedNwType,
589 (const char*)address.ipAddress,
590 address.port, &endpoint))
592 //ReleaseStringUTFChars for strUri
593 (*env)->ReleaseStringUTFChars(env, uri, strUri);
594 LOGE("Could not create remote end point");
598 char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
599 get_resource_uri(strUri, resourceURI, RESOURCE_URI_LENGTH);
601 //ReleaseStringUTFChars for strUri
602 (*env)->ReleaseStringUTFChars(env, uri, strUri);
604 CAMessageType_t messageType = msgType;
607 CAToken_t token = NULL;
608 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
610 res = CAGenerateToken(&token, tokenLength);
611 if ((CA_STATUS_OK != res) || (!token))
613 LOGE("token generate error!");
614 CADestroyEndpoint(endpoint);
618 CAInfo_t responseData = { 0 };
619 responseData.token = token;
620 responseData.tokenLength = tokenLength;
621 responseData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
622 if (NULL == responseData.resourceUri)
624 LOGE("Memory allocation failed!");
626 CADestroyToken(token);
627 // destroy remote endpoint
628 CADestroyEndpoint(endpoint);
631 memcpy(responseData.resourceUri, resourceURI, sizeof(resourceURI));
635 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
636 responseData.payload = (CAPayload_t) malloc(length);
637 if (NULL == responseData.payload)
639 LOGE("Memory allocation failed!");
641 CADestroyToken(token);
642 // destroy remote endpoint
643 CADestroyEndpoint(endpoint);
645 free(responseData.resourceUri);
648 snprintf(responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
652 uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
653 responseData.payload = (CAPayload_t) malloc(length);
654 if (NULL == responseData.payload)
656 LOGE("Memory allocation failed!");
658 CADestroyToken(token);
659 // destroy remote endpoint
660 CADestroyEndpoint(endpoint);
662 free(responseData.resourceUri);
665 snprintf(responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
668 responseData.type = messageType;
670 CAResponseInfo_t responseInfo = { 0 };
671 responseInfo.result = responseValue;
672 responseInfo.info = responseData;
675 if (CA_STATUS_OK != CASendNotification(endpoint, &responseInfo))
677 LOGE("Could not send notification");
680 LOGI("Send Notification");
683 CADestroyToken(token);
685 // destroy remote endpoint
686 CADestroyEndpoint(endpoint);
688 free(responseData.payload);
689 free(responseData.resourceUri);
692 JNIEXPORT void JNICALL
693 Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj,
696 LOGI("RMSelectNetwork Type : %d", networkType);
698 if (CA_STATUS_OK != CASelectNetwork(networkType))
700 LOGE("Could not select network");
704 JNIEXPORT void JNICALL
705 Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj,
708 LOGI("RMUnSelectNetwork Type : %d", networkType);
710 if (CA_STATUS_OK != CAUnSelectNetwork(networkType))
712 LOGE("Could not unselect network");
716 JNIEXPORT void JNICALL
717 Java_org_iotivity_ca_service_RMInterface_RMGetNetworkInfomation(JNIEnv *env, jobject obj)
719 LOGI("RMGetNetworkInfomation");
721 CAEndpoint_t *tempInfo = NULL;
722 uint32_t tempSize = 0;
724 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
725 if (CA_STATUS_OK != res)
727 LOGE("Could not start get network information");
732 LOGI("################## Network Information #######################");
733 callback("######## Network Information", "#######");
734 LOGI("Network info total size is %d", tempSize);
737 for (index = 0; index < tempSize; index++)
739 res = get_remote_address(tempInfo[index].adapter, tempInfo[index].addr);
740 if (CA_STATUS_OK != res)
745 if (NULL != g_responseListenerObject)
747 char networkInfo[NETWORK_INFO_LENGTH];
748 LOGI("Type: %d", tempInfo[index].adapter);
749 sprintf(networkInfo, "%d",tempInfo[index].adapter);
750 callback("Type :", networkInfo);
751 if (CA_ADAPTER_IP == tempInfo[index].adapter)
753 LOGI("Port: %d", tempInfo[index].port);
754 sprintf(networkInfo, "%d",tempInfo[index].port);
755 callback("Port: ", networkInfo);
757 LOGI("Secured: %d", (tempInfo[index].flags & CA_SECURE));
758 LOGI("Address: %s", g_remoteAddress);
759 callback("Address: ", g_remoteAddress);
760 free(g_remoteAddress);
762 if (tempInfo[index].flags & CA_SECURE)
764 g_localSecurePort = tempInfo[index].port;
771 LOGI("##############################################################");
774 JNIEXPORT void JNICALL
775 Java_org_iotivity_ca_service_RMInterface_RMHandleRequestResponse(JNIEnv *env, jobject obj)
777 LOGI("RMHandleRequestResponse");
779 if (CA_STATUS_OK != CAHandleRequestResponse())
781 LOGE("Could not handle request and response");
785 void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo)
790 LOGE("Remote endpoint is NULL!");
796 LOGE("Request info is NULL!");
800 if ((NULL != g_lastRequestToken) && (NULL != requestInfo->info.token) &&
801 (strncmp(g_lastRequestToken, requestInfo->info.token,
802 requestInfo->info.tokenLength) == 0))
804 LOGI("token is same. received request of it's own. skip.. ");
808 CAResult_t res = get_remote_address(object->adapter, object->addr);
809 if (CA_STATUS_OK != res)
814 LOGI("##########received request from remote device #############");
815 LOGI("Remote Address: %s", g_remoteAddress);
816 LOGI("Remote Port: %d", object->port);
817 LOGI("Uri: %s", requestInfo->info.resourceUri);
818 LOGI("Data: %s", requestInfo->info.payload);
819 LOGI("Token: %s", requestInfo->info.token);
820 LOGI("Code: %d", requestInfo->method);
821 LOGI("MessageType: %d", requestInfo->info.type);
823 if (NULL != g_responseListenerObject)
825 char *cloneUri = NULL;
828 if (NULL != requestInfo->info.resourceUri)
830 len = strlen(requestInfo->info.resourceUri);
831 cloneUri = (char *)malloc(sizeof(char) * (len + 1));
833 if (NULL == cloneUri)
835 LOGE("cloneUri Out of memory");
836 free(g_remoteAddress);
840 memcpy(cloneUri, requestInfo->info.resourceUri, len + 1);
841 callback("Uri: ", cloneUri);
844 len = strlen(g_remoteAddress);
845 char *cloneRemoteAddress = (char *) malloc(sizeof(char) * (len + 1));
847 if (NULL == cloneRemoteAddress)
849 LOGE("cloneRemoteAddress Out of memory");
850 free(g_remoteAddress);
855 memcpy(cloneRemoteAddress, g_remoteAddress, len + 1);
857 callback("Remote Address: ", cloneRemoteAddress);
858 free(cloneRemoteAddress);
859 free(g_remoteAddress);
861 char portInfo[PORT_LENGTH] = { 0, };
862 sprintf(portInfo, "%d", object->port);
863 callback("Remote Port: ", portInfo);
865 //clone g_clientEndpoint
866 g_clientEndpoint = (CAEndpoint_t *) malloc(sizeof(CAEndpoint_t));
867 if (NULL == g_clientEndpoint)
869 LOGE("g_clientEndpoint Out of memory");
873 memcpy(g_clientEndpoint, object, sizeof(CAEndpoint_t));
875 if (NULL != cloneUri)
877 len = strlen(cloneUri);
878 g_resourceUri = (char *) malloc(sizeof(char) * (len + 1));
879 if (NULL == g_resourceUri)
881 LOGE("g_clientEndpoint->resourceUri Out of memory");
882 free(g_clientEndpoint);
886 memcpy(g_resourceUri, cloneUri, len + 1);
889 //clone g_clientToken
890 len = requestInfo->info.tokenLength;
892 g_clientToken = (char *) malloc(sizeof(char) * len);
893 if (NULL == g_clientToken)
895 LOGE("g_clientToken Out of memory");
896 free(g_clientEndpoint);
900 if (NULL != requestInfo->info.token)
902 memcpy(g_clientToken, requestInfo->info.token, len);
903 g_clientTokenLength = len;
907 //clone g_clientMsgId
908 g_clientMsgId = requestInfo->info.messageId;
910 if (NULL != requestInfo->info.payload && requestInfo->info.payloadSize > 0)
912 len = requestInfo->info.payloadSize;
913 char *clonePayload = (char *) malloc(len + 1);
914 if (NULL == clonePayload)
916 LOGE("clonePayload Out of memory");
917 free(g_clientEndpoint);
921 memcpy(clonePayload, requestInfo->info.payload, len);
922 clonePayload[len] = '\0';
924 callback("Data: ", clonePayload);
929 if (requestInfo->info.options)
931 uint32_t len = requestInfo->info.numOptions;
934 LOGI("Option count: %d", requestInfo->info.numOptions);
936 for (i = 0; i < len; i++)
938 LOGI("Option %d", i + 1);
939 LOGI("ID : %d", requestInfo->info.options[i].optionID);
940 LOGI("Data[%d]: %s", requestInfo->info.options[i].optionLength,
941 requestInfo->info.options[i].optionData);
943 if (NULL != g_responseListenerObject)
945 char optionInfo[OPTION_INFO_LENGTH] = { 0, };
946 sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i + 1,
947 requestInfo->info.options[i].optionID,
948 requestInfo->info.options[i].optionLength);
950 callback("Option info: ", optionInfo);
952 size_t optionDataLen = strlen(requestInfo->info.options[i].optionData);
953 char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
954 if (NULL == cloneOptionData)
956 LOGE("cloneOptionData Out of memory");
957 free(g_clientEndpoint);
961 memcpy(cloneOptionData, requestInfo->info.options[i].optionData,
964 callback("Option Data: ", cloneOptionData);
965 free(cloneOptionData);
969 LOGI("############################################################");
971 //Check if this has secure communication information
972 if (requestInfo->info.payload && CA_ADAPTER_IP == object->adapter)
974 uint32_t securePort = get_secure_information(requestInfo->info.payload);
975 if (0 < securePort) //Set the remote endpoint secure details and send response
977 LOGI("This is secure resource...");
979 CAEndpoint_t *endpoint = NULL;
980 if (CA_STATUS_OK != CACreateEndpoint(CA_SECURE,
981 object->adapter, object->addr, securePort, &endpoint))
983 LOGE("Failed to create duplicate of remote endpoint!");
991 void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo)
994 CAResult_t res = get_remote_address(object->adapter, object->addr);
995 if (CA_STATUS_OK != res)
1000 LOGI("##########Received response from remote device #############");
1001 LOGI("Uri: %s", responseInfo->info.resourceUri);
1002 LOGI("Remote Address: %s", g_remoteAddress);
1003 LOGI("Remote Port: %d", object->port);
1004 LOGI("response result: %d", responseInfo->result);
1005 LOGI("Data: %s", responseInfo->info.payload);
1006 LOGI("Token: %s", responseInfo->info.token);
1007 LOGI("MessageType: %d", responseInfo->info.type);
1009 if (NULL != g_responseListenerObject)
1013 if (NULL != responseInfo->info.resourceUri)
1015 len = strlen(responseInfo->info.resourceUri);
1016 char *cloneUri = (char *) malloc(sizeof(char) * (len + 1));
1018 if (NULL == cloneUri)
1020 LOGE("cloneUri Out of memory");
1021 free(g_remoteAddress);
1025 memcpy(cloneUri, responseInfo->info.resourceUri, len + 1);
1027 callback("Uri: ", cloneUri);
1031 len = strlen(g_remoteAddress);
1032 char *cloneRemoteAddress = (char *) malloc(sizeof(char) * (len + 1));
1034 if (NULL == cloneRemoteAddress)
1036 LOGE("cloneRemoteAddress Out of memory");
1037 free(g_remoteAddress);
1041 memcpy(cloneRemoteAddress, g_remoteAddress, len + 1);
1043 callback("Remote Address: ", cloneRemoteAddress);
1044 free(cloneRemoteAddress);
1045 free(g_remoteAddress);
1047 char portInfo[PORT_LENGTH] = { 0, };
1048 sprintf(portInfo, "%d", object->port);
1049 callback("Remote Port: ", portInfo);
1051 if (NULL != responseInfo->info.payload && responseInfo->info.payloadSize)
1053 len = responseInfo->info.payloadSize;
1054 char *clonePayload = (char *) malloc(len + 1);
1055 if (NULL == clonePayload)
1057 LOGE("clonePayload Out of memory");
1061 memcpy(clonePayload, responseInfo->info.payload, len);
1062 clonePayload[len] = '\0';
1064 callback("Data: ", clonePayload);
1069 if (responseInfo->info.options)
1071 uint32_t len = responseInfo->info.numOptions;
1073 for (i = 0; i < len; i++)
1075 LOGI("Option %d", i + 1);
1076 LOGI("ID : %d", responseInfo->info.options[i].optionID);
1077 LOGI("Data[%d]: %s", responseInfo->info.options[i].optionLength,
1078 responseInfo->info.options[i].optionData);
1080 if (NULL != g_responseListenerObject)
1082 char optionInfo[OPTION_INFO_LENGTH] = { 0, };
1083 sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i + 1,
1084 responseInfo->info.options[i].optionID,
1085 responseInfo->info.options[i].optionLength);
1087 callback("Option info: ", optionInfo);
1089 size_t optionDataLen = strlen(responseInfo->info.options[i].optionData);
1090 char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
1091 if (NULL == cloneOptionData)
1093 LOGE("cloneOptionData Out of memory");
1096 memcpy(cloneOptionData, responseInfo->info.options[i].optionData,
1098 callback("Option Data: ", cloneOptionData);
1099 free(cloneOptionData);
1103 LOGI("############################################################");
1105 //Check if this has secure communication information
1106 if (responseInfo->info.payload && CA_ADAPTER_IP == object->adapter)
1108 uint32_t securePort = get_secure_information(responseInfo->info.payload);
1109 if (0 < securePort) //Set the remote endpoint secure details and send response
1111 LOGI("This is secure resource...");
1116 void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
1118 printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++");
1122 const CAInfo_t *info = &errorInfo->info;
1123 LOGI("Error Handler, ErrorInfo :");
1124 LOGI("Error Handler result : %d", errorInfo->result);
1125 LOGI("Error Handler token : %s", info->token);
1126 LOGI("Error Handler messageId : %d", (uint16_t) info->messageId);
1127 LOGI("Error Handler resourceUri : %s", info->resourceUri);
1128 LOGI("Error Handler type : %d", info->type);
1129 LOGI("Error Handler payload : %s", info->payload);
1131 if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
1133 LOGE("CA_ADAPTER_NOT_ENABLED, enable the adapter");
1135 else if(CA_SEND_FAILED == errorInfo->result)
1137 LOGE("CA_SEND_FAILED, unable to send the message, check parameters");
1139 else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
1141 LOGE("CA_MEMORY_ALLOC_FAILED, insufficient memory");
1143 else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
1145 LOGE("CA_SOCKET_OPERATION_FAILED, socket operation failed");
1147 else if(CA_STATUS_FAILED == errorInfo->result)
1149 LOGE("CA_STATUS_FAILED, message could not be delivered, internal error");
1152 LOGI("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++");
1157 void get_resource_uri(const char *URI, char *resourceURI, uint32_t length)
1159 const char *startPos = URI;
1160 const char *temp = strstr(URI, "://");
1163 startPos = strchr(temp + 3, '/');
1166 LOGE("Resource URI is missing");
1171 const char *endPos = strchr(startPos, '?');
1174 endPos = URI + strlen(URI);
1178 if (endPos - startPos <= length)
1180 memcpy(resourceURI, startPos + 1, endPos - startPos);
1183 LOGI("URI: %s, ResourceURI: %s", URI, resourceURI);
1186 uint32_t get_secure_information(CAPayload_t payLoad)
1188 LOGI("entering get_secure_information");
1192 LOGE("Payload is NULL");
1196 const char *subString = NULL;
1197 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1199 LOGE("This is not secure resource");
1203 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1205 LOGE("This secure resource does not have port information");
1209 const char *startPos = strstr(subString, ":");
1212 LOGE("Parsing failed !");
1216 const char *endPos = strstr(startPos, "}");
1219 LOGE("Parsing failed !");
1223 char portStr[6] = { 0 };
1224 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1226 LOGI("secured port is: %s", portStr);
1227 return atoi(portStr);
1230 CAResult_t get_network_type(uint32_t selectedNetwork)
1233 uint32_t number = selectedNetwork;
1235 if (!(number & 0xf))
1237 return CA_NOT_SUPPORTED;
1239 if (number & CA_ADAPTER_IP)
1241 g_selectedNwType = CA_ADAPTER_IP;
1242 return CA_STATUS_OK;
1244 if (number & CA_ADAPTER_RFCOMM_BTEDR)
1246 g_selectedNwType = CA_ADAPTER_RFCOMM_BTEDR;
1247 return CA_STATUS_OK;
1249 if (number & CA_ADAPTER_GATT_BTLE)
1251 g_selectedNwType = CA_ADAPTER_GATT_BTLE;
1252 return CA_STATUS_OK;
1255 return CA_NOT_SUPPORTED;
1258 void callback(char *subject, char *receivedData)
1261 uint32_t status = (*g_jvm)->GetEnv(g_jvm, (void **) &env, JNI_VERSION_1_6);
1262 uint32_t res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1264 jclass cls = (*env)->GetObjectClass(env, g_responseListenerObject);
1265 jmethodID mid = (*env)->GetMethodID(env, cls, "OnResponseReceived",
1266 "(Ljava/lang/String;Ljava/lang/String;)V");
1268 jstring jsubject = (*env)->NewStringUTF(env, (char*) subject);
1269 jstring jreceivedData = (*env)->NewStringUTF(env, (char*) receivedData);
1270 (*env)->CallVoidMethod(env, g_responseListenerObject, mid, jsubject, jreceivedData);
1274 CAResult_t get_remote_address(CATransportAdapter_t transportType, const char *address)
1276 uint32_t len = strlen(address);
1278 g_remoteAddress = (char *)malloc(sizeof (char) * (len + 1));
1279 if (NULL == g_remoteAddress)
1281 LOGE("g_remoteAddress Out of memory");
1282 return CA_MEMORY_ALLOC_FAILED;
1285 memcpy(g_remoteAddress, address, len + 1);
1287 return CA_STATUS_OK;
1291 void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
1293 if (NULL == uri || NULL == address)
1295 LOGE("parameter is null");
1301 uint8_t startIndex = 0;
1302 if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
1304 LOGI("uri has '%s' prefix", COAPS_PREFIX);
1305 startIndex = COAPS_PREFIX_LEN;
1308 else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
1310 LOGI("uri has '%s' prefix", COAP_PREFIX);
1311 startIndex = COAP_PREFIX_LEN;
1315 // #2. copy uri for parse
1316 int32_t len = strlen(uri) - startIndex;
1320 LOGE("uri length is 0!");
1324 char *cloneUri = (char *) calloc(len + 1, sizeof(char));
1325 if (NULL == cloneUri)
1327 LOGE("Out of memory");
1331 memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
1332 cloneUri[len] = '\0';
1334 char *pAddress = cloneUri;
1335 char *pResourceUri = NULL;
1338 for (i = 0; i < len; i++)
1340 if (cloneUri[i] == '/')
1344 pResourceUri = &cloneUri[i + 1];
1348 LOGI("pAddress : %s", pAddress);
1350 int res = get_address_set(pAddress, address);
1353 LOGE("address parse error");
1361 int get_address_set(const char *pAddress, addressSet_t* outAddress)
1363 if (NULL == pAddress || NULL == outAddress)
1365 LOGE("parameter is null");
1369 int32_t len = strlen(pAddress);
1374 for (i = 0; i < len; i++)
1376 if (pAddress[i] == '.')
1381 // found port number start index
1382 if (isIp && pAddress[i] == ':')
1391 if(ipLen && ipLen < sizeof(outAddress->ipAddress))
1393 strncpy(outAddress->ipAddress, pAddress, ipLen);
1394 outAddress->ipAddress[ipLen] = '\0';
1396 else if (!ipLen && len < sizeof(outAddress->ipAddress))
1398 strncpy(outAddress->ipAddress, pAddress, len);
1399 outAddress->ipAddress[len] = '\0';
1403 LOGE("IP Address too long: %d", ipLen==0 ? len : ipLen);
1409 outAddress->port = atoi(pAddress + ipLen + 1);
1414 strncpy(outAddress->ipAddress, pAddress, len);
1415 outAddress->ipAddress[len] = '\0';