2 #include <android/log.h>
8 #include <../../../../api/cainterface.h>
9 #include <../../../../api/cacommon.h>
10 #include "com_iotivity_service_RMInterface.h"
12 #define LOG_TAG "JNI_INTERFACE_SAMPLE"
13 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
14 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
20 #define IDENTITY ("1111111111111111")
24 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
27 CABool_t gLocalUnicastPort;
28 CABool_t gLocalSecurePort;
30 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
31 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
32 void send_response(const CARemoteEndpoint_t* endpoint, CAToken_t request_token);
33 void get_resource_uri(char *URI, char *resourceURI, int length);
34 int get_secure_information(CAPayload_t payLoad);
35 CAResult_t get_network_type(int selectedNetwork);
36 void callback(char *subject, char *receivedData);
38 CAConnectivityType_t gSelectedNwType;
39 static CAToken_t gLastRequestToken = NULL;
40 static const char *gSecureInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
41 "\"if\":[\"oc.mi.def\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
42 static const char *gNormalInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
43 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
45 static jobject gResponseListenerObject = NULL;
48 static CARemoteEndpoint_t* clientEndpoint =NULL;
49 static CAToken_t clientToken;
52 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj, jobject listener){
53 LOGI("setNativeResponseListener");
54 gResponseListenerObject = (*env)->NewGlobalRef(env, obj);
58 static OCDtlsPskCredsBlob *pskCredsBlob = NULL;
60 void clearDtlsCredentialInfo()
62 printf("clearDtlsCredentialInfo IN\n");
65 // Initialize sensitive data to zeroes before freeing.
66 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCredsBlob)*(pskCredsBlob->num));
67 free(pskCredsBlob->creds);
69 memset(pskCredsBlob, 0, sizeof(OCDtlsPskCredsBlob));
73 printf("clearDtlsCredentialInfo OUT\n");
76 // Internal API. Invoked by OC stack to retrieve credentials from this module
77 void CAGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
79 printf("CAGetDtlsPskCredentials IN\n");
81 *credInfo = pskCredsBlob;
83 printf("CAGetDtlsPskCredentials OUT\n");
86 int32_t SetCredentials()
88 printf("SetCredentials IN\n");
89 pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
91 memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
92 memcpy(pskCredsBlob->rsIdentity, IDENTITY, DTLS_PSK_ID_LEN);
94 pskCredsBlob->num = 1;
96 pskCredsBlob->creds = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob) *(pskCredsBlob->num));
98 memcpy(pskCredsBlob->creds[0].clientIdentity, IDENTITY, DTLS_PSK_ID_LEN);
99 memcpy(pskCredsBlob->creds[0].rsClientPsk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
101 printf("SetCredentials OUT\n");
106 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
108 printf("JNI_OnLoad");
111 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
115 g_jvm = jvm; /* cache the JavaVM pointer */
117 CANativeJNISetJavaVM(g_jvm);
119 return JNI_VERSION_1_6;
122 void JNI_OnUnload(JavaVM *jvm, void *reserved)
124 printf("JNI_OnUnload");
127 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
135 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMInitialize
136 (JNIEnv *env, jobject obj, jobject context)
138 LOGI("RMInitialize");
140 //Currently set context for Android Platform
141 CANativeJNISetContext(env, context);
146 if (SetCredentials() == 0)
148 printf("SetCredentials failed\n");
151 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
152 if(res != CA_STATUS_OK)
154 printf("Set credential handler fail\n");
158 if(CA_STATUS_OK != CAInitialize())
160 LOGI("Could not Initialize");
164 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
171 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMStartListeningServer(JNIEnv *env,
174 LOGI("RMStartListeningServer");
176 if(CA_STATUS_OK != CAStartListeningServer())
178 LOGI("Could not start Listening server");
182 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env,
185 LOGI("RMStartDiscoveryServer");
187 if(CA_STATUS_OK != CAStartDiscoveryServer())
189 LOGI("Could not start discovery server");
193 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMRegisterHandler(JNIEnv *env,
196 LOGI("RMRegisterHandler");
198 CARegisterHandler(request_handler, response_handler);
201 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMFindResource(JNIEnv *env, jobject obj,
204 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
205 LOGI("RMFindResource - %s", strUri);
208 CAToken_t token = NULL;
209 CAResult_t res = CAGenerateToken(&token);
210 if (res != CA_STATUS_OK)
212 printf("token generate error!!\n");
216 LOGI("generated token %s\n", (token != NULL) ? token : "");
218 if(CA_STATUS_OK != CAFindResource((const CAURI_t)strUri, token))
220 LOGI("Could not find resource");
224 LOGI("find resource to %s URI", strUri);
225 gLastRequestToken = token;
229 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj,
230 jstring uri, jstring payload, jint selectedNetwork, jint isSecured, jint msgType)
232 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
233 LOGI("RMSendRequest - %s", strUri);
237 LOGI("selectedNetwork - %d", selectedNetwork);
238 res = get_network_type(selectedNetwork);
239 if (res != CA_STATUS_OK)
244 //create remote endpoint
245 CARemoteEndpoint_t* endpoint = NULL;
247 if(CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t)strUri, gSelectedNwType, &endpoint))
249 LOGI("Could not create remote end point");
250 CADestroyRemoteEndpoint(endpoint);
254 CAMessageType_t messageType = msgType;
257 CAToken_t token = NULL;
258 res = CAGenerateToken(&token);
259 if (res != CA_STATUS_OK)
261 printf("token generate error!!\n");
265 char resourceURI[15] = {0};
267 get_resource_uri((const CAURI_t)strUri, resourceURI, 14);
269 CAInfo_t requestData;
270 memset(&requestData, 0, sizeof(CAInfo_t));
271 requestData.token = token;
273 const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL);
276 int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
277 requestData.payload = (CAPayload_t) malloc(length);
278 sprintf(requestData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
282 int length = strlen(strPayload) + strlen(resourceURI) + 1;
283 requestData.payload = (CAPayload_t) malloc(length);
284 sprintf(requestData.payload, strPayload, resourceURI);
287 requestData.type = messageType;
289 CARequestInfo_t requestInfo;
290 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
291 requestInfo.method = CA_GET;
292 requestInfo.info = requestData;
295 if(CA_STATUS_OK != CASendRequest(endpoint, &requestInfo))
297 LOGI("Could not send request");
303 CADestroyToken(token);
306 // destroy remote endpoint
307 if (endpoint != NULL)
309 CADestroyRemoteEndpoint(endpoint);
313 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendResponse(JNIEnv *env,
314 jobject obj, jint selectedNetwork, jint isSecured, jint msgType, jint responseValue)
316 LOGI("RMSendResponse");
320 LOGI("selectedNetwork - %d", selectedNetwork);
322 res = get_network_type(selectedNetwork);
323 if (res != CA_STATUS_OK)
325 LOGI("Not supported network type");
329 if (clientEndpoint == NULL)
331 LOGI("No Request received");
335 CAMessageType_t messageType = msgType;
337 CAInfo_t responseData;
338 memset(&responseData, 0, sizeof(CAInfo_t));
339 responseData.token = clientToken;
343 int length = strlen(gSecureInfoData) + strlen(clientEndpoint->resourceUri) + 1;
344 responseData.payload = (CAPayload_t) malloc(length);
345 sprintf(responseData.payload, gSecureInfoData, clientEndpoint->resourceUri, gLocalSecurePort);
349 int length = strlen("sendResponse Payload") + strlen(clientEndpoint->resourceUri) + 1;
350 responseData.payload = (CAPayload_t) malloc(length);
351 sprintf(responseData.payload, gNormalInfoData, clientEndpoint->resourceUri);
354 responseData.type = messageType;
356 CAResponseInfo_t responseInfo;
357 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
358 responseInfo.result = responseValue;
359 responseInfo.info = responseData;
362 if(CA_STATUS_OK != CASendResponse(clientEndpoint, &responseInfo))
364 LOGI("Could not send response");
368 if (clientToken != NULL)
370 CADestroyToken(clientToken);
373 // destroy remote endpoint
374 if (clientEndpoint != NULL)
376 CADestroyRemoteEndpoint(clientEndpoint);
381 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMAdvertiseResource(JNIEnv *env,
382 jobject obj, jstring uri, jint selectedNetwork)
384 LOGI("RMAdvertiseResource");
386 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
390 CAHeaderOption_t *headerOpt;
391 headerOpt = (CAHeaderOption_t*) malloc(sizeof(CAHeaderOption_t) * optionNum);
392 if (NULL == headerOpt)
394 printf("Memory allocation failed!\n");
397 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
399 char* tmpOptionData1 = "Hello";
400 headerOpt[0].optionID = 3000;
401 memcpy(headerOpt[0].optionData, tmpOptionData1, strlen(tmpOptionData1));
402 headerOpt[0].optionLength = (uint16_t) strlen(tmpOptionData1);
404 char* tmpOptionData2 = "World";
405 headerOpt[1].optionID = 3001;
406 memcpy(headerOpt[1].optionData, tmpOptionData2, strlen(tmpOptionData2));
407 headerOpt[1].optionLength = (uint16_t) strlen(tmpOptionData2);
410 CAToken_t token = NULL;
411 CAResult_t res = CAGenerateToken(&token);
412 if (res != CA_STATUS_OK)
414 LOGI("token generate error!");
418 CAAdvertiseResource((const CAURI_t)strUri, token, headerOpt, (uint8_t) optionNum);
423 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendNotification(JNIEnv *env,
424 jobject obj, jstring uri, jstring payload, jint selectedNetwork, jint isSecured)
426 const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
427 LOGI("RMSendNotification - %s", strUri);
431 LOGI("selectedNetwork - %d", selectedNetwork);
433 res = get_network_type(selectedNetwork);
434 if (res != CA_STATUS_OK)
436 LOGI("Not supported network type");
440 //create remote endpoint
441 CARemoteEndpoint_t* endpoint = NULL;
443 if(CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t)strUri, gSelectedNwType, &endpoint))
445 LOGI("Could not create remote end point");
446 CADestroyRemoteEndpoint(endpoint);
450 CAMessageType_t messageType = CA_MSG_NONCONFIRM;
453 CAToken_t token = NULL;
454 res = CAGenerateToken(&token);
455 if (res != CA_STATUS_OK)
457 LOGI("token generate error!");
461 char resourceURI[15] = {0};
463 get_resource_uri((const CAURI_t)strUri, resourceURI, 14);
465 CAInfo_t responseData;
466 memset(&responseData, 0, sizeof(CAInfo_t));
467 responseData.token = token;
469 const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL);
472 int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
473 responseData.payload = (CAPayload_t) malloc(length);
474 sprintf(responseData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
478 int length = strlen(strPayload) + strlen(resourceURI) + 1;
479 responseData.payload = (CAPayload_t) malloc(length);
480 sprintf(responseData.payload, strPayload, resourceURI);
483 responseData.type = messageType;
485 CAResponseInfo_t responseInfo;
486 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
487 responseInfo.result = CA_SUCCESS;
488 responseInfo.info = responseData;
491 if(CA_STATUS_OK != CASendNotification(endpoint, &responseInfo))
493 LOGI("Could not send notification");
496 LOGI("Send Notification");
501 CADestroyToken(token);
504 // destroy remote endpoint
505 if (endpoint != NULL)
507 CADestroyRemoteEndpoint(endpoint);
511 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj,
514 LOGI("RMSelectNetwork Type : %d", networkType);
516 if(CA_STATUS_OK != CASelectNetwork(networkType))
518 LOGI("Could not select network");
522 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMHandleRequestResponse(JNIEnv *env,
525 LOGI("RMHandleRequestResponse");
527 if(CA_STATUS_OK != CAHandleRequestResponse())
529 LOGI("Could not handle request and response");
533 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
536 char *cloneUri = NULL;
537 char *cloneRemoteAddress = NULL;
538 char *clonePayload = NULL;
539 char *cloneOptionData = NULL;
544 LOGI("Remote endpoint is NULL!");
550 LOGI("Request info is NULL!");
554 LOGI("##########received request from remote device #############\n");
555 LOGI("Uri: %s\n", object->resourceUri);
556 LOGI("Remote Address: %s\n", object->addressInfo.IP.ipAddress);
557 LOGI("Data: %s\n", requestInfo->info.payload);
558 LOGI("Token: %s\n", requestInfo->info.token);
559 LOGI("Code: %d\n", requestInfo->method);
561 if (NULL != gResponseListenerObject)
563 callback("received request from remote device", "#######");
565 if (object->resourceUri != NULL)
567 len = strlen(object->resourceUri);
568 cloneUri = (char *) OICMalloc(sizeof(char) * (len + 1));
570 if (cloneUri == NULL)
572 LOGI("CACloneRemoteEndpoint Out of memory");
576 memset(cloneUri, 0, sizeof(char) * (len + 1));
577 strncpy(cloneUri, object->resourceUri, len);
579 callback("Uri: ", cloneUri);
583 len = strlen(object->addressInfo.IP.ipAddress);
584 cloneRemoteAddress = (char *) OICMalloc(sizeof(char) * (len + 1));
586 if (cloneRemoteAddress == NULL)
588 LOGI("CACloneRemoteEndpoint Out of memory");
592 memset(cloneRemoteAddress, 0, sizeof(char) * (len + 1));
593 strncpy(cloneRemoteAddress, object->addressInfo.IP.ipAddress, len);
595 callback("Remote Address: ", cloneRemoteAddress);
596 free(cloneRemoteAddress);
599 if(requestInfo->info.payload != NULL)
601 len = strlen(requestInfo->info.payload);
602 clonePayload = (char *) OICMalloc(sizeof(char) * (len + 1));
604 if (clonePayload == NULL)
606 LOGI("CACloneRemoteEndpoint Out of memory");
610 memset(clonePayload, 0, sizeof(char) * (len + 1));
611 strncpy(clonePayload, requestInfo->info.payload, len);
613 callback("Data: ", clonePayload);
618 if (gLastRequestToken != NULL && requestInfo->info.token != NULL
619 && (strcmp((char *)gLastRequestToken, requestInfo->info.token) == 0))
621 LOGI("token is same. received request of it's own. skip.. \n");
625 if (requestInfo->info.options)
627 uint32_t len = requestInfo->info.numOptions;
630 LOGI("Option count: %d\n", requestInfo->info.numOptions);
632 for (i = 0; i < len; i++)
634 LOGI("Option %d\n", i + 1);
635 LOGI("ID : %d\n", requestInfo->info.options[i].optionID);
636 LOGI("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
637 requestInfo->info.options[i].optionData);
639 if (NULL != gResponseListenerObject)
641 char optionInfo[1024] = {0,};
642 sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i+1,
643 requestInfo->info.options[i].optionID, requestInfo->info.options[i].optionLength);
645 callback("Option info: ", optionInfo);
647 if (requestInfo->info.options[i].optionData != NULL)
649 uint32_t optionDataLen = strlen(requestInfo->info.options[i].optionData);
650 cloneOptionData = (char *) OICMalloc(sizeof(char) * (optionDataLen + 1));
652 if (cloneOptionData == NULL)
654 LOGI("CACloneRemoteEndpoint Out of memory");
658 memset(cloneOptionData, 0, sizeof(char) * (optionDataLen + 1));
659 strncpy(cloneOptionData, requestInfo->info.options[i].optionData, optionDataLen);
661 callback("Option Data: ", cloneOptionData);
662 free(cloneOptionData);
668 printf("############################################################\n");
670 //Check if this has secure communication information
671 if (requestInfo->info.payload && object->resourceUri)
673 int securePort = get_secure_information(requestInfo->info.payload);
674 if (0 < securePort) //Set the remote endpoint secure details and send response
676 LOGI("This is secure resource...\n");
680 length = 8; //length of "coaps://"
681 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
682 length += strlen(object->resourceUri) + 1;
684 uri = calloc(1,sizeof(char)*length);
687 printf("Failed to create new uri\n");
690 sprintf(uri,"coaps://%s:%d/%s",object->addressInfo.IP.ipAddress,
691 securePort, object->resourceUri);
693 CARemoteEndpoint_t *endpoint = NULL;
694 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
696 LOGI("Failed to create duplicate of remote endpoint!\n");
699 endpoint->isSecured = CA_TRUE;
706 clientEndpoint = object;
707 clientToken = requestInfo->info.token;
710 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
713 char *cloneUri = NULL;
714 char *cloneRemoteAddress = NULL;
715 char *clonePayload = NULL;
716 char *cloneOptionData = NULL;
719 LOGI("##########Received response from remote device #############\n");
720 LOGI("Uri: %s\n", object->resourceUri);
721 LOGI("Remote Address: %s\n", object->addressInfo.IP.ipAddress);
722 LOGI("response result: %d\n", responseInfo->result);
723 LOGI("Data: %s\n", responseInfo->info.payload);
724 LOGI("Token: %s\n", responseInfo->info.token);
725 LOGI("Code: %d\n", responseInfo->result);
727 if (NULL != gResponseListenerObject)
729 if (object->resourceUri != NULL)
731 len = strlen(object->resourceUri);
732 cloneUri = (char *) OICMalloc(sizeof(char) * (len + 1));
734 if (cloneUri == NULL)
736 LOGI("CACloneRemoteEndpoint Out of memory");
740 memset(cloneUri, 0, sizeof(char) * (len + 1));
741 strncpy(cloneUri, object->resourceUri, len);
743 callback("Uri: ", cloneUri);
747 len = strlen(object->addressInfo.IP.ipAddress);
748 cloneRemoteAddress = (char *) OICMalloc(sizeof(char) * (len + 1));
750 if (cloneRemoteAddress == NULL)
752 LOGI("CACloneRemoteEndpoint Out of memory");
756 memset(cloneRemoteAddress, 0, sizeof(char) * (len + 1));
757 strncpy(cloneRemoteAddress, object->addressInfo.IP.ipAddress, len);
759 callback("Remote Address: ", cloneRemoteAddress);
760 free(cloneRemoteAddress);
763 if(responseInfo->info.payload != NULL)
765 len = strlen(responseInfo->info.payload);
766 clonePayload = (char *) OICMalloc(sizeof(char) * (len + 1));
768 if (clonePayload == NULL)
770 LOGI("CACloneRemoteEndpoint Out of memory");
774 memset(clonePayload, 0, sizeof(char) * (len + 1));
775 strncpy(clonePayload, responseInfo->info.payload, len);
777 callback("Data: ", clonePayload);
782 if (responseInfo->info.options)
784 uint32_t len = responseInfo->info.numOptions;
786 for (i = 0; i < len; i++)
788 LOGI("Option %d\n", i + 1);
789 LOGI("ID : %d\n", responseInfo->info.options[i].optionID);
790 LOGI("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
791 responseInfo->info.options[i].optionData);
793 if (NULL != gResponseListenerObject)
795 char optionInfo[1024] = {0,};
796 sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i+1,
797 responseInfo->info.options[i].optionID,
798 responseInfo->info.options[i].optionLength);
800 callback("Option info: ", optionInfo);
802 if (responseInfo->info.options[i].optionData != NULL)
804 uint32_t optionDataLen = strlen(responseInfo->info.options[i].optionData);
805 cloneOptionData = (char *) OICMalloc(sizeof(char) * (optionDataLen + 1));
807 if (cloneOptionData == NULL)
809 LOGI("CACloneRemoteEndpoint Out of memory");
812 memset(cloneOptionData, 0, sizeof(char) * (optionDataLen + 1));
813 strncpy(cloneOptionData, responseInfo->info.options[i].optionData, optionDataLen);
814 callback("Option Data: ", cloneOptionData);
815 free(cloneOptionData);
820 LOGI("############################################################\n");
823 //Check if this has secure communication information
824 if (responseInfo->info.payload)
826 int securePort = get_secure_information(responseInfo->info.payload);
827 if (0 < securePort) //Set the remote endpoint secure details and send response
829 LOGI("This is secure resource...\n");
834 void get_resource_uri(char *URI, char *resourceURI, int length)
836 char *startPos = URI;
838 if (NULL != (temp = strstr(URI, "://")))
840 startPos = strchr(temp + 3, '/');
843 printf("Resource URI is missing\n");
848 char *endPos = strchr(startPos, '?');
851 endPos = URI + strlen(URI);
855 if (endPos - startPos <= length)
856 memcpy(resourceURI, startPos + 1, endPos - startPos);
858 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
861 int get_secure_information(CAPayload_t payLoad)
863 printf("entering get_secure_information\n");
867 printf("Payload is NULL\n");
871 char *subString = NULL;
872 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
874 printf("This is not secure resource\n");
878 if (NULL == (subString = strstr(payLoad, "\"port\":")))
880 printf("This secure resource does not have port information\n");
884 char *startPos = strstr(subString, ":");
887 printf("Parsing failed !\n");
891 char *endPos = strstr(startPos, "}");
894 printf("Parsing failed !\n");
898 char portStr[4] = {0};
899 memcpy(portStr, startPos + 1, (endPos-1) - startPos);
901 printf("secured port is: %s\n", portStr);
902 return atoi(portStr);
905 CAResult_t get_network_type(int selectedNetwork)
908 int number = selectedNetwork;
912 return CA_NOT_SUPPORTED;
914 if (number & CA_ETHERNET)
916 gSelectedNwType = CA_ETHERNET;
919 if (number & CA_WIFI)
921 gSelectedNwType = CA_WIFI;
926 gSelectedNwType = CA_EDR;
931 gSelectedNwType = CA_LE;
935 return CA_NOT_SUPPORTED;
938 void callback(char *subject, char *receivedData)
941 int status = (*g_jvm)->GetEnv(g_jvm, (void **) &env, JNI_VERSION_1_6);
942 int res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
944 jclass cls = (*env)->GetObjectClass(env, gResponseListenerObject);
945 jmethodID mid = (*env)->GetMethodID(env, cls, "OnResponseReceived", "(Ljava/lang/String;Ljava/lang/String;)V");
947 jstring jsubject = (*env)->NewStringUTF(env, (char*)subject);
948 jstring jreceivedData = (*env)->NewStringUTF(env, (char*)receivedData);
949 (*env)->CallVoidMethod(env, gResponseListenerObject, mid, jsubject, jreceivedData);