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
33 uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
35 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
36 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
37 void error_handler(const CARemoteEndpoint_t *object, const CAErrorInfo_t* errorInfo);
39 void get_resource_uri(const char *URI, char *resourceURI, uint32_t length);
40 uint32_t get_secure_information(CAPayload_t payLoad);
41 CAResult_t get_network_type(uint32_t selectedNetwork);
42 void callback(char *subject, char *receivedData);
43 CAResult_t get_remote_address(CATransportType_t transportType, CAAddress_t addressInfo);
45 CATransportType_t g_selectedNwType = CA_IPV4;
46 static CAToken_t g_lastRequestToken = NULL;
47 static uint8_t g_lastRequestTokenLength;
49 static const char SECURE_COAPS_PREFIX[] = "coaps://";
51 static const char SECURE_INFO_DATA[]
52 = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
53 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
54 static const char NORMAL_INFO_DATA[]
55 = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
56 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
59 static jobject g_responseListenerObject = NULL;
62 static CARemoteEndpoint_t *g_clientEndpoint = NULL;
63 static CAToken_t g_clientToken;
64 static uint8_t g_clientTokenLength = NULL;
66 static uint16_t g_clientMsgId;
67 static char *g_remoteAddress = NULL;
70 JNIEXPORT void JNICALL
71 Java_org_iotivity_ca_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj,
74 LOGI("setNativeResponseListener");
75 g_responseListenerObject = (*env)->NewGlobalRef(env, obj);
79 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
81 void clearDtlsCredentialInfo()
83 LOGI("clearDtlsCredentialInfo IN");
86 // Initialize sensitive data to zeroes before freeing.
87 if (NULL != pskCredsBlob->creds)
89 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds)*(pskCredsBlob->num));
90 free(pskCredsBlob->creds);
93 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
97 LOGI("clearDtlsCredentialInfo OUT");
100 // Internal API. Invoked by OC stack to retrieve credentials from this module
101 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
103 LOGI("CAGetDtlsPskCredentials IN");
104 *credInfo = (CADtlsPskCredsBlob_t *) malloc(sizeof(CADtlsPskCredsBlob_t));
105 if (NULL == *credInfo)
107 LOGE("Failed to allocate credential blob.");
111 int16_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
112 (*credInfo)->creds = (OCDtlsPskCreds *) malloc(credLen);
113 if (NULL == (*credInfo)->creds)
115 LOGE("Failed to allocate crentials.");
121 memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
122 (*credInfo)->num = pskCredsBlob->num;
123 memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
125 LOGI("CAGetDtlsPskCredentials OUT");
128 CAResult_t SetCredentials()
130 LOGI("SetCredentials IN");
131 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
132 if (NULL == pskCredsBlob)
134 LOGE("Memory allocation failed!");
135 return CA_MEMORY_ALLOC_FAILED;
137 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
139 pskCredsBlob->num = 1;
141 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) *(pskCredsBlob->num));
142 if (NULL == pskCredsBlob->creds)
144 LOGE("Memory allocation failed!");
145 return CA_MEMORY_ALLOC_FAILED;
147 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
148 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
150 LOGI("SetCredentials OUT");
155 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
160 if (JNI_OK != (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6))
164 g_jvm = jvm; /* cache the JavaVM pointer */
166 CANativeJNISetJavaVM(g_jvm);
168 return JNI_VERSION_1_6;
171 void JNI_OnUnload(JavaVM *jvm, void *reserved)
173 LOGI("JNI_OnUnload");
176 if (JNI_OK != (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6))
184 JNIEXPORT void JNICALL
185 Java_org_iotivity_ca_service_RMInterface_RMInitialize(JNIEnv *env, jobject obj, jobject context)
187 LOGI("RMInitialize");
189 //Currently set context for Android Platform
190 CANativeJNISetContext(env, context);
192 CAResult_t res = CAInitialize();
194 if (CA_STATUS_OK != res)
196 LOGE("Could not Initialize");
200 if (CA_STATUS_OK != SetCredentials())
202 LOGE("SetCredentials failed");
206 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
207 if(CA_STATUS_OK != res)
209 LOGE("Set credential handler fail");
215 JNIEXPORT void JNICALL
216 Java_org_iotivity_ca_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
219 CADestroyToken(g_lastRequestToken);
223 JNIEXPORT void JNICALL
224 Java_org_iotivity_ca_service_RMInterface_RMStartListeningServer(JNIEnv *env, jobject obj)
226 LOGI("RMStartListeningServer");
228 if (CA_STATUS_OK != CAStartListeningServer())
230 LOGE("Could not start Listening server");
234 JNIEXPORT void JNICALL
235 Java_org_iotivity_ca_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env, jobject obj)
237 LOGI("RMStartDiscoveryServer");
239 if (CA_STATUS_OK != CAStartDiscoveryServer())
241 LOGE("Could not start discovery server");
245 JNIEXPORT void JNICALL
246 Java_org_iotivity_ca_service_RMInterface_RMRegisterHandler(JNIEnv *env, jobject obj)
248 LOGI("RMRegisterHandler");
250 CARegisterHandler(request_handler, response_handler, error_handler);
253 JNIEXPORT void JNICALL
254 Java_org_iotivity_ca_service_RMInterface_RMFindResource(JNIEnv *env, jobject obj, jstring uri)
257 CAToken_t token = NULL;
258 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
260 CAResult_t res = CAGenerateToken(&token, tokenLength);
261 if ((CA_STATUS_OK != res) || (!token))
263 LOGE("token generate error!!");
267 printf("Generated token %s\n", token);
269 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
270 LOGI("RMFindResource - %s", strUri);
272 res = CAFindResource((const CAURI_t) strUri, token, tokenLength);
274 //ReleseStringUTFCharss for strUri
275 (*env)->ReleaseStringUTFChars(env, uri, strUri);
277 if (CA_STATUS_OK != res)
279 LOGE("Could not find resource");
281 CADestroyToken(token);
285 LOGI("find resource to %s URI", strUri);
286 CADestroyToken(g_lastRequestToken);
287 g_lastRequestToken = token;
288 g_lastRequestTokenLength = tokenLength;
292 JNIEXPORT void JNICALL
293 Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
294 jstring payload, jint selectedNetwork,
295 jint isSecured, jint msgType)
297 LOGI("selectedNetwork - %d", selectedNetwork);
298 CAResult_t res = get_network_type(selectedNetwork);
299 if (CA_STATUS_OK != res)
304 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
305 LOGI("RMSendRequest - %s", strUri);
307 //create remote endpoint
308 CARemoteEndpoint_t* endpoint = NULL;
309 res = CACreateRemoteEndpoint((const CAURI_t) strUri, g_selectedNwType, &endpoint);
311 //ReleaseStringUTFChars for strUri
312 (*env)->ReleaseStringUTFChars(env, uri, strUri);
314 if (CA_STATUS_OK != res)
316 LOGE("Could not create remote end point");
320 CAMessageType_t messageType = msgType;
323 CAToken_t token = NULL;
324 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
326 res = CAGenerateToken(&token, tokenLength);
327 if ((CA_STATUS_OK != res) || (!token))
329 LOGE("token generate error!!");
330 // destroy remote endpoint
331 CADestroyRemoteEndpoint(endpoint);
335 char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
337 get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
339 CAInfo_t requestData = { 0 };
340 requestData.token = token;
341 requestData.tokenLength = tokenLength;
345 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
346 requestData.payload = (CAPayload_t) malloc(length);
347 if (NULL == requestData.payload)
349 LOGE("Memory allocation failed!");
351 CADestroyToken(token);
352 // destroy remote endpoint
353 CADestroyRemoteEndpoint(endpoint);
356 snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
360 uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
361 requestData.payload = (CAPayload_t) malloc(length);
362 if (NULL == requestData.payload)
364 LOGE("Memory allocation failed!");
366 CADestroyToken(token);
367 // destroy remote endpoint
368 CADestroyRemoteEndpoint(endpoint);
371 snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
374 requestData.type = messageType;
376 CARequestInfo_t requestInfo = { 0 };
377 requestInfo.method = CA_GET;
378 requestInfo.info = requestData;
381 if (CA_STATUS_OK != CASendRequest(endpoint, &requestInfo))
383 LOGE("Could not send request");
387 CADestroyToken(token);
389 // destroy remote endpoint
390 CADestroyRemoteEndpoint(endpoint);
392 free(requestData.payload);
395 JNIEXPORT void JNICALL
396 Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
397 jint selectedNetwork)
399 LOGI("selectedNetwork - %d", selectedNetwork);
400 CAResult_t res = get_network_type(selectedNetwork);
401 if (CA_STATUS_OK != res)
406 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
407 LOGI("RMSendReqestToAll - %s", strUri);
409 // create remote endpoint
410 CARemoteEndpoint_t *endpoint = NULL;
411 res = CACreateRemoteEndpoint((const CAURI_t) strUri, g_selectedNwType, &endpoint);
413 //ReleaseStringUTFChars for strUri
414 (*env)->ReleaseStringUTFChars(env, uri, strUri);
416 if (CA_STATUS_OK != res)
418 LOGE("create remote endpoint error, error code: %d", res);
422 CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
425 LOGE("Memory allocation failed!");
426 CADestroyRemoteEndpoint(endpoint);
429 group->transportType = endpoint->transportType;
430 group->resourceUri = endpoint->resourceUri;
433 CAToken_t token = NULL;
434 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
436 res = CAGenerateToken(&token, tokenLength);
437 if ((CA_STATUS_OK != res) || (!token))
439 LOGE("token generate error!!");
440 // destroy remote endpoint
441 CADestroyRemoteEndpoint(endpoint);
446 LOGI("generated token %s", token);
448 CAInfo_t requestData = { 0 };
449 requestData.token = token;
450 requestData.tokenLength = tokenLength;
451 requestData.payload = "Temp Json Payload";
452 requestData.type = CA_MSG_NONCONFIRM;
454 CARequestInfo_t requestInfo = { 0 };
455 requestInfo.method = CA_GET;
456 requestInfo.info = requestData;
458 // send request to all
459 res = CASendRequestToAll(group, &requestInfo);
460 if (CA_STATUS_OK != res)
462 LOGE("Could not send request to all");
464 CADestroyToken(token);
468 CADestroyToken(g_lastRequestToken);
469 g_lastRequestToken = token;
470 g_lastRequestTokenLength = tokenLength;
473 // destroy remote endpoint
474 CADestroyRemoteEndpoint(endpoint);
479 JNIEXPORT void JNICALL
480 Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
481 jint selectedNetwork,
482 jint isSecured, jint msgType,
485 LOGI("RMSendResponse");
487 LOGI("selectedNetwork - %d", selectedNetwork);
489 CAResult_t res = get_network_type(selectedNetwork);
490 if (CA_STATUS_OK != res)
492 LOGE("Not supported network type");
496 if (NULL == g_clientEndpoint)
498 LOGE("No Request received");
502 CAMessageType_t messageType = msgType;
504 CAInfo_t responseData = { 0 };
505 responseData.type = messageType;
506 responseData.messageId = g_clientMsgId;
508 CAResponseInfo_t responseInfo = { 0 };
510 if (msgType != CA_MSG_RESET)
512 responseData.token = g_clientToken;
513 responseData.tokenLength = g_clientTokenLength;
514 responseInfo.result = responseValue;
518 uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_clientEndpoint->resourceUri) + 1;
519 responseData.payload = (CAPayload_t) malloc(length);
520 sprintf(responseData.payload, SECURE_INFO_DATA, g_clientEndpoint->resourceUri,
525 uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_clientEndpoint->resourceUri) + 1;
526 responseData.payload = (CAPayload_t) malloc(length);
527 sprintf(responseData.payload, NORMAL_INFO_DATA, g_clientEndpoint->resourceUri);
533 responseInfo.result = CA_EMPTY;
536 responseInfo.info = responseData;
539 res = CASendResponse(g_clientEndpoint, &responseInfo);
540 if (CA_STATUS_OK != res)
542 LOGE("Could not send response");
546 CADestroyToken(g_clientToken);
547 g_clientToken = NULL;
548 g_clientTokenLength = 0;
550 // destroy remote endpoint
551 CADestroyRemoteEndpoint(g_clientEndpoint);
552 g_clientEndpoint = NULL;
555 JNIEXPORT void JNICALL
556 Java_org_iotivity_ca_service_RMInterface_RMAdvertiseResource(JNIEnv *env, jobject obj, jstring uri)
558 LOGI("RMAdvertiseResource");
560 uint32_t optionNum = 2;
562 CAHeaderOption_t *headerOpt = (CAHeaderOption_t*) calloc(1,
563 sizeof(CAHeaderOption_t) * optionNum);
564 if (NULL == headerOpt)
566 LOGE("Memory allocation failed!");
570 char* tmpOptionData1 = "Hello";
571 headerOpt[0].optionID = 3000;
572 memcpy(headerOpt[0].optionData, tmpOptionData1, strlen(tmpOptionData1));
573 headerOpt[0].optionLength = (uint16_t) strlen(tmpOptionData1);
575 char* tmpOptionData2 = "World";
576 headerOpt[1].optionID = 3001;
577 memcpy(headerOpt[1].optionData, tmpOptionData2, strlen(tmpOptionData2));
578 headerOpt[1].optionLength = (uint16_t) strlen(tmpOptionData2);
581 CAToken_t token = NULL;
582 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
584 CAResult_t res = CAGenerateToken(&token, tokenLength);
585 if ((CA_STATUS_OK != res) || (!token))
587 LOGE("token generate error!");
592 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
594 res = CAAdvertiseResource((const CAURI_t) strUri, token, tokenLength,
595 headerOpt, (uint8_t) optionNum);
596 if (CA_STATUS_OK != res)
598 LOGE("Could not start advertise resource");
599 CADestroyToken(token);
603 CADestroyToken(g_lastRequestToken);
604 g_lastRequestToken = token;
605 g_lastRequestTokenLength = tokenLength;
610 //ReleaseStringUTFChars for strUri
611 (*env)->ReleaseStringUTFChars(env, uri, strUri);
614 JNIEXPORT void JNICALL
615 Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
616 jstring payload, jint selectedNetwork,
617 jint isSecured, jint msgType,
620 LOGI("selectedNetwork - %d", selectedNetwork);
622 CAResult_t res = get_network_type(selectedNetwork);
623 if (CA_STATUS_OK != res)
625 LOGE("Not supported network type");
629 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
630 LOGI("RMSendNotification - %s", strUri);
632 //create remote endpoint
633 CARemoteEndpoint_t* endpoint = NULL;
634 if (CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t) strUri,
635 g_selectedNwType, &endpoint))
637 //ReleaseStringUTFChars for strUri
638 (*env)->ReleaseStringUTFChars(env, uri, strUri);
639 LOGE("Could not create remote end point");
643 char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
644 get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
646 //ReleaseStringUTFChars for strUri
647 (*env)->ReleaseStringUTFChars(env, uri, strUri);
649 CAMessageType_t messageType = msgType;
652 CAToken_t token = NULL;
653 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
655 res = CAGenerateToken(&token, tokenLength);
656 if ((CA_STATUS_OK != res) || (!token))
658 LOGE("token generate error!");
659 CADestroyRemoteEndpoint(endpoint);
663 CAInfo_t responseData = { 0 };
664 responseData.token = token;
665 responseData.tokenLength = tokenLength;
669 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
670 responseData.payload = (CAPayload_t) malloc(length);
671 if (NULL == responseData.payload)
673 LOGE("Memory allocation failed!");
675 CADestroyToken(token);
676 // destroy remote endpoint
677 CADestroyRemoteEndpoint(endpoint);
680 snprintf(responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
684 uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
685 responseData.payload = (CAPayload_t) malloc(length);
686 if (NULL == responseData.payload)
688 LOGE("Memory allocation failed!");
690 CADestroyToken(token);
691 // destroy remote endpoint
692 CADestroyRemoteEndpoint(endpoint);
695 snprintf(responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
698 responseData.type = messageType;
700 CAResponseInfo_t responseInfo = { 0 };
701 responseInfo.result = responseValue;
702 responseInfo.info = responseData;
705 if (CA_STATUS_OK != CASendNotification(endpoint, &responseInfo))
707 LOGE("Could not send notification");
710 LOGI("Send Notification");
713 CADestroyToken(token);
715 // destroy remote endpoint
716 CADestroyRemoteEndpoint(endpoint);
718 free(responseData.payload);
721 JNIEXPORT void JNICALL
722 Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
724 LOGI("RMSelectNetwork Type : %d", networkType);
726 if (CA_STATUS_OK != CASelectNetwork(networkType))
728 LOGE("Could not select network");
732 JNIEXPORT void JNICALL
733 Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
735 LOGI("RMUnSelectNetwork Type : %d", networkType);
737 if (CA_STATUS_OK != CAUnSelectNetwork(networkType))
739 LOGE("Could not unselect network");
743 JNIEXPORT void JNICALL
744 Java_org_iotivity_ca_service_RMInterface_RMGetNetworkInfomation(JNIEnv *env, jobject obj)
746 LOGI("RMGetNetworkInfomation");
748 CALocalConnectivity_t *tempInfo = NULL;
749 uint32_t tempSize = 0;
751 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
752 if (CA_STATUS_OK != res)
754 LOGE("Could not start get network information");
759 LOGI("################## Network Information #######################");
760 callback("######## Network Information", "#######");
761 LOGI("Network info total size is %d", tempSize);
764 for (index = 0; index < tempSize; index++)
766 res = get_remote_address(tempInfo[index].type, tempInfo[index].addressInfo);
767 if (CA_STATUS_OK != res)
772 if (NULL != g_responseListenerObject)
774 char networkInfo[NETWORK_INFO_LENGTH];
775 LOGI("Type: %d", tempInfo[index].type);
776 sprintf(networkInfo, "%d",tempInfo[index].type);
777 callback("Type :", networkInfo);
778 if (CA_IPV4 == tempInfo[index].type)
780 LOGI("Port: %d", tempInfo[index].addressInfo.IP.port);
781 sprintf(networkInfo, "%d",tempInfo[index].addressInfo.IP.port);
782 callback("Port: ", networkInfo);
784 LOGI("Secured: %d", tempInfo[index].isSecured);
785 LOGI("Address: %s", g_remoteAddress);
786 callback("Address: ", g_remoteAddress);
787 free(g_remoteAddress);
789 if (true == tempInfo[index].isSecured)
791 g_localSecurePort = tempInfo[index].addressInfo.IP.port;
798 LOGI("##############################################################");
801 JNIEXPORT void JNICALL
802 Java_org_iotivity_ca_service_RMInterface_RMHandleRequestResponse(JNIEnv *env, jobject obj)
804 LOGI("RMHandleRequestResponse");
806 if (CA_STATUS_OK != CAHandleRequestResponse())
808 LOGE("Could not handle request and response");
812 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
817 LOGE("Remote endpoint is NULL!");
823 LOGE("Request info is NULL!");
827 if ((NULL != g_lastRequestToken) && (NULL != requestInfo->info.token) &&
828 (strncmp(g_lastRequestToken, requestInfo->info.token,
829 requestInfo->info.tokenLength) == 0))
831 LOGI("token is same. received request of it's own. skip.. ");
835 CAResult_t res = get_remote_address(object->transportType, object->addressInfo);
836 if (CA_STATUS_OK != res)
841 LOGI("##########received request from remote device #############");
842 if (object->resourceUri)
844 LOGI("Uri: %s", object->resourceUri);
846 LOGI("Remote Address: %s", g_remoteAddress);
847 LOGI("Data: %s", requestInfo->info.payload);
848 LOGI("Token: %s", requestInfo->info.token);
849 LOGI("Code: %d", requestInfo->method);
850 LOGI("MessageType: %d", requestInfo->info.type);
852 if (NULL != g_responseListenerObject)
854 callback("received request from remote device", "#######");
855 char *cloneUri = NULL;
858 if (NULL != object->resourceUri)
860 len = strlen(object->resourceUri);
861 cloneUri = (char *) malloc(sizeof(char) * (len + 1));
863 if (NULL == cloneUri)
865 LOGE("cloneUri Out of memory");
866 free(g_remoteAddress);
870 memcpy(cloneUri, object->resourceUri, len + 1);
871 callback("Uri: ", cloneUri);
874 len = strlen(g_remoteAddress);
875 char *cloneRemoteAddress = (char *) malloc(sizeof(char) * (len + 1));
877 if (NULL == cloneRemoteAddress)
879 LOGE("cloneRemoteAddress Out of memory");
880 free(g_remoteAddress);
885 memcpy(cloneRemoteAddress, g_remoteAddress, len + 1);
887 callback("Remote Address: ", cloneRemoteAddress);
888 free(cloneRemoteAddress);
889 free(g_remoteAddress);
891 //clone g_clientEndpoint
892 g_clientEndpoint = (CARemoteEndpoint_t *) malloc(sizeof(CARemoteEndpoint_t));
893 if (NULL == g_clientEndpoint)
895 LOGE("g_clientEndpoint Out of memory");
899 memcpy(g_clientEndpoint, object, sizeof(CARemoteEndpoint_t));
901 if (NULL != cloneUri)
903 len = strlen(cloneUri);
904 g_clientEndpoint->resourceUri = (char *) malloc(sizeof(char) * (len + 1));
905 if (NULL == g_clientEndpoint)
907 LOGE("g_clientEndpoint->resourceUri Out of memory");
908 free(g_clientEndpoint);
912 memcpy(g_clientEndpoint->resourceUri, cloneUri, len + 1);
915 //clone g_clientToken
916 len = requestInfo->info.tokenLength;
918 g_clientToken = (char *) malloc(sizeof(char) * len);
919 if (NULL == g_clientToken)
921 LOGE("g_clientToken Out of memory");
922 free(g_clientEndpoint->resourceUri);
923 free(g_clientEndpoint);
927 if (NULL != requestInfo->info.token)
929 memcpy(g_clientToken, requestInfo->info.token, len);
930 g_clientTokenLength = len;
934 //clone g_clientMsgId
935 g_clientMsgId = requestInfo->info.messageId;
937 if (NULL != requestInfo->info.payload)
939 len = strlen(requestInfo->info.payload);
940 char *clonePayload = (char *) malloc(sizeof(char) * (len + 1));
942 if (NULL == clonePayload)
944 LOGE("clonePayload Out of memory");
945 free(g_clientEndpoint->resourceUri);
946 free(g_clientEndpoint);
950 memcpy(clonePayload, requestInfo->info.payload, len + 1);
952 callback("Data: ", clonePayload);
957 if (requestInfo->info.options)
959 uint32_t len = requestInfo->info.numOptions;
962 LOGI("Option count: %d", requestInfo->info.numOptions);
964 for (i = 0; i < len; i++)
966 LOGI("Option %d", i + 1);
967 LOGI("ID : %d", requestInfo->info.options[i].optionID);
968 LOGI("Data[%d]: %s", requestInfo->info.options[i].optionLength,
969 requestInfo->info.options[i].optionData);
971 if (NULL != g_responseListenerObject)
973 char optionInfo[OPTION_INFO_LENGTH] = { 0, };
974 sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i + 1,
975 requestInfo->info.options[i].optionID,
976 requestInfo->info.options[i].optionLength);
978 callback("Option info: ", optionInfo);
980 uint32_t optionDataLen = strlen(requestInfo->info.options[i].optionData);
981 char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
982 if (NULL == cloneOptionData)
984 LOGE("cloneOptionData Out of memory");
985 free(g_clientEndpoint->resourceUri);
986 free(g_clientEndpoint);
990 memcpy(cloneOptionData, requestInfo->info.options[i].optionData,
993 callback("Option Data: ", cloneOptionData);
994 free(cloneOptionData);
998 LOGI("############################################################");
1000 //Check if this has secure communication information
1001 if (requestInfo->info.payload && CA_IPV4 == object->transportType)
1003 uint32_t securePort = get_secure_information(requestInfo->info.payload);
1004 if (0 < securePort) //Set the remote endpoint secure details and send response
1006 LOGI("This is secure resource...");
1008 uint32_t length = 0;
1010 length = sizeof(SECURE_COAPS_PREFIX) - 1; //length of "coaps://"
1011 // length of "ipaddress:port"
1012 length += strlen(object->addressInfo.IP.ipAddress) + PORT_LENGTH;
1013 length += strlen(object->resourceUri) + 1;
1015 uri = calloc(1, sizeof(char) * length);
1018 LOGE("Failed to create new uri");
1022 sprintf(uri, "%s%s:%d/%s", SECURE_COAPS_PREFIX, object->addressInfo.IP.ipAddress,
1023 securePort, object->resourceUri);
1025 CARemoteEndpoint_t *endpoint = NULL;
1026 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->transportType, &endpoint))
1028 LOGE("Failed to create duplicate of remote endpoint!");
1032 endpoint->isSecured = true;
1040 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
1043 CAResult_t res = get_remote_address(object->transportType, object->addressInfo);
1044 if (CA_STATUS_OK != res)
1049 LOGI("##########Received response from remote device #############");
1050 LOGI("Uri: %s", object->resourceUri);
1051 LOGI("Remote Address: %s", g_remoteAddress);
1052 LOGI("response result: %d", responseInfo->result);
1053 LOGI("Data: %s", responseInfo->info.payload);
1054 LOGI("Token: %s", responseInfo->info.token);
1055 LOGI("MessageType: %d", responseInfo->info.type);
1057 if (NULL != g_responseListenerObject)
1061 if (NULL != object->resourceUri)
1063 len = strlen(object->resourceUri);
1064 char *cloneUri = (char *) malloc(sizeof(char) * (len + 1));
1066 if (NULL == cloneUri)
1068 LOGE("cloneUri Out of memory");
1069 free(g_remoteAddress);
1073 memcpy(cloneUri, object->resourceUri, len + 1);
1075 callback("Uri: ", cloneUri);
1079 len = strlen(g_remoteAddress);
1080 char *cloneRemoteAddress = (char *) malloc(sizeof(char) * (len + 1));
1082 if (NULL == cloneRemoteAddress)
1084 LOGE("cloneRemoteAddress Out of memory");
1085 free(g_remoteAddress);
1089 memcpy(cloneRemoteAddress, g_remoteAddress, len + 1);
1091 callback("Remote Address: ", cloneRemoteAddress);
1092 free(cloneRemoteAddress);
1093 free(g_remoteAddress);
1095 if (NULL != responseInfo->info.payload)
1097 len = strlen(responseInfo->info.payload);
1098 char *clonePayload = (char *) malloc(sizeof(char) * (len + 1));
1100 if (NULL == clonePayload)
1102 LOGE("clonePayload Out of memory");
1106 memcpy(clonePayload, responseInfo->info.payload, len + 1);
1108 callback("Data: ", clonePayload);
1113 if (responseInfo->info.options)
1115 uint32_t len = responseInfo->info.numOptions;
1117 for (i = 0; i < len; i++)
1119 LOGI("Option %d", i + 1);
1120 LOGI("ID : %d", responseInfo->info.options[i].optionID);
1121 LOGI("Data[%d]: %s", responseInfo->info.options[i].optionLength,
1122 responseInfo->info.options[i].optionData);
1124 if (NULL != g_responseListenerObject)
1126 char optionInfo[OPTION_INFO_LENGTH] = { 0, };
1127 sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i + 1,
1128 responseInfo->info.options[i].optionID,
1129 responseInfo->info.options[i].optionLength);
1131 callback("Option info: ", optionInfo);
1133 uint32_t optionDataLen = strlen(responseInfo->info.options[i].optionData);
1134 char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
1135 if (NULL == cloneOptionData)
1137 LOGE("cloneOptionData Out of memory");
1140 memcpy(cloneOptionData, responseInfo->info.options[i].optionData,
1142 callback("Option Data: ", cloneOptionData);
1143 free(cloneOptionData);
1147 LOGI("############################################################");
1149 //Check if this has secure communication information
1150 if (responseInfo->info.payload && CA_IPV4 == object->transportType)
1152 uint32_t securePort = get_secure_information(responseInfo->info.payload);
1153 if (0 < securePort) //Set the remote endpoint secure details and send response
1155 LOGI("This is secure resource...");
1160 void error_handler(const CARemoteEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
1162 printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++");
1164 if(rep && rep->resourceUri )
1166 LOGI("Error Handler, RemoteEndpoint Info resourceUri : %s", rep->resourceUri);
1170 LOGI("Error Handler, RemoteEndpoint is NULL");
1175 const CAInfo_t *info = &errorInfo->info;
1176 LOGI("Error Handler, ErrorInfo :");
1177 LOGI("Error Handler result : %d", errorInfo->result);
1178 LOGI("Error Handler token : %s", info->token);
1179 LOGI("Error Handler messageId : %d", (uint16_t) info->messageId);
1180 LOGI("Error Handler type : %d", info->type);
1181 LOGI("Error Handler payload : %s", info->payload);
1183 if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
1185 LOGE("CA_ADAPTER_NOT_ENABLED, enable the adapter");
1187 else if(CA_SEND_FAILED == errorInfo->result)
1189 LOGE("CA_SEND_FAILED, unable to send the message, check parameters");
1191 else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
1193 LOGE("CA_MEMORY_ALLOC_FAILED, insufficient memory");
1195 else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
1197 LOGE("CA_SOCKET_OPERATION_FAILED, socket operation failed");
1199 else if(CA_STATUS_FAILED == errorInfo->result)
1201 LOGE("CA_STATUS_FAILED, message could not be delivered, internal error");
1204 LOGI("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++");
1209 void get_resource_uri(const char *URI, char *resourceURI, uint32_t length)
1211 const char *startPos = URI;
1212 const char *temp = NULL;
1213 if (NULL != (temp = strstr(URI, "://")))
1215 startPos = strchr(temp + 3, '/');
1218 LOGE("Resource URI is missing");
1223 const char *endPos = strchr(startPos, '?');
1226 endPos = URI + strlen(URI);
1230 if (endPos - startPos <= length)
1232 memcpy(resourceURI, startPos + 1, endPos - startPos);
1235 LOGI("URI: %s, ResourceURI: %s", URI, resourceURI);
1238 uint32_t get_secure_information(CAPayload_t payLoad)
1240 LOGI("entering get_secure_information");
1244 LOGE("Payload is NULL");
1248 const char *subString = NULL;
1249 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1251 LOGE("This is not secure resource");
1255 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1257 LOGE("This secure resource does not have port information");
1261 const char *startPos = strstr(subString, ":");
1264 LOGE("Parsing failed !");
1268 const char *endPos = strstr(startPos, "}");
1271 LOGE("Parsing failed !");
1275 char portStr[6] = { 0 };
1276 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1278 LOGI("secured port is: %s", portStr);
1279 return atoi(portStr);
1282 CAResult_t get_network_type(uint32_t selectedNetwork)
1285 uint32_t number = selectedNetwork;
1287 if (!(number & 0xf))
1289 return CA_NOT_SUPPORTED;
1291 if (number & CA_IPV4)
1293 g_selectedNwType = CA_IPV4;
1294 return CA_STATUS_OK;
1296 if (number & CA_EDR)
1298 g_selectedNwType = CA_EDR;
1299 return CA_STATUS_OK;
1303 g_selectedNwType = CA_LE;
1304 return CA_STATUS_OK;
1307 return CA_NOT_SUPPORTED;
1310 void callback(char *subject, char *receivedData)
1313 uint32_t status = (*g_jvm)->GetEnv(g_jvm, (void **) &env, JNI_VERSION_1_6);
1314 uint32_t res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1316 jclass cls = (*env)->GetObjectClass(env, g_responseListenerObject);
1317 jmethodID mid = (*env)->GetMethodID(env, cls, "OnResponseReceived",
1318 "(Ljava/lang/String;Ljava/lang/String;)V");
1320 jstring jsubject = (*env)->NewStringUTF(env, (char*) subject);
1321 jstring jreceivedData = (*env)->NewStringUTF(env, (char*) receivedData);
1322 (*env)->CallVoidMethod(env, g_responseListenerObject, mid, jsubject, jreceivedData);
1326 CAResult_t get_remote_address(CATransportType_t transportType, CAAddress_t addressInfo)
1330 if (CA_IPV4 == transportType)
1332 len = strlen(addressInfo.IP.ipAddress);
1333 g_remoteAddress = (char *) malloc(sizeof(char) * (len + 1));
1335 if (NULL == g_remoteAddress)
1337 LOGE("g_remoteAddress Out of memory");
1338 return CA_MEMORY_ALLOC_FAILED;
1341 memcpy(g_remoteAddress, addressInfo.IP.ipAddress, len + 1);
1344 else if (CA_EDR == transportType)
1346 len = strlen(addressInfo.BT.btMacAddress);
1347 g_remoteAddress = (char *) malloc(sizeof(char) * (len + 1));
1349 if (NULL == g_remoteAddress)
1351 LOGE("g_remoteAddress Out of memory");
1352 return CA_MEMORY_ALLOC_FAILED;
1355 memcpy(g_remoteAddress, addressInfo.BT.btMacAddress, len + 1);
1358 else if (CA_LE == transportType)
1360 len = strlen(addressInfo.LE.leMacAddress);
1361 g_remoteAddress = (char *) malloc(sizeof(char) * (len + 1));
1363 if (NULL == g_remoteAddress)
1365 LOGE("g_remoteAddress Out of memory");
1366 return CA_MEMORY_ALLOC_FAILED;
1369 memcpy(g_remoteAddress, addressInfo.LE.leMacAddress, len + 1);
1372 return CA_STATUS_OK;