9736b8d78a3e6d93285a9cd2f601d98f50af9c35
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / android / sample_service / jni / ResourceModel.c
1 #include <jni.h>
2 #include <android/log.h>
3 #include <stdio.h>
4 #include <dlfcn.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <../../../../api/cainterface.h>
9 #include <../../../../api/cacommon.h>
10 #include "com_iotivity_service_RMInterface.h"
11
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__)
15
16 /**
17  * @def RS_IDENTITY
18  * @brief
19  */
20 #define IDENTITY     ("1111111111111111")
21 /* @def RS_CLIENT_PSK
22  * @brief
23  */
24 #define RS_CLIENT_PSK   ("AAAAAAAAAAAAAAAA")
25
26 int gReceived;
27 CABool_t gLocalUnicastPort;
28 CABool_t gLocalSecurePort;
29
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 *receicedData);
37
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}}]}";
44
45 static jobject gResponseListenerObject = NULL;
46 extern JavaVM *g_jvm;
47
48 // init
49 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj, jobject listener){
50     LOGI("setNativeResponseListener");
51     gResponseListenerObject = (*env)->NewGlobalRef(env, obj);
52 }
53
54 #ifdef __WITH_DTLS__
55 static OCDtlsPskCredsBlob *pskCredsBlob = NULL;
56
57 void clearDtlsCredentialInfo()
58 {
59     printf("clearDtlsCredentialInfo IN\n");
60     if (pskCredsBlob)
61     {
62         // Initialize sensitive data to zeroes before freeing.
63         memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCredsBlob)*(pskCredsBlob->num));
64         free(pskCredsBlob->creds);
65
66         memset(pskCredsBlob, 0, sizeof(OCDtlsPskCredsBlob));
67         free(pskCredsBlob);
68         pskCredsBlob = NULL;
69     }
70     printf("clearDtlsCredentialInfo OUT\n");
71 }
72
73 // Internal API. Invoked by OC stack to retrieve credentials from this module
74 void CAGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
75 {
76     printf("CAGetDtlsPskCredentials IN\n");
77
78     *credInfo = pskCredsBlob;
79
80     printf("CAGetDtlsPskCredentials OUT\n");
81 }
82
83 int32_t SetCredentials()
84 {
85     printf("SetCredentials IN\n");
86     pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
87
88     memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
89     memcpy(pskCredsBlob->rsIdentity, IDENTITY, DTLS_PSK_ID_LEN);
90
91     pskCredsBlob->num = 1;
92
93     pskCredsBlob->creds = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob) *(pskCredsBlob->num));
94
95     memcpy(pskCredsBlob->creds[0].clientIdentity, IDENTITY, DTLS_PSK_ID_LEN);
96     memcpy(pskCredsBlob->creds[0].rsClientPsk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
97
98     printf("SetCredentials OUT\n");
99     return 1;
100 }
101 #endif
102
103 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMInitialize
104   (JNIEnv *env, jobject obj, jobject context)
105 {
106     LOGI("RMInitialize");
107     //Currently set context for WiFiCore
108     CAJniSetContext(context);
109     CALEServerJNISetContext(env, context);
110     CALEClientJNISetContext(env, context);
111     CALENetworkMonitorJNISetContext(env, context);
112
113     CAResult_t res;
114
115 #ifdef __WITH_DTLS__
116     if (SetCredentials() == 0)
117     {
118         printf("SetCredentials failed\n");
119     }
120
121     res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
122     if(res != CA_STATUS_OK)
123     {
124         printf("Set credential handler fail\n");
125     }
126 #endif
127
128     if(CA_STATUS_OK != CAInitialize())
129     {
130         LOGI("Could not Initialize");
131     }
132 }
133
134 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
135 {
136     LOGI("RMTerminate");
137
138     CATerminate();
139 }
140
141 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMStartListeningServer(JNIEnv *env,
142     jobject obj)
143 {
144     LOGI("RMStartListeningServer");
145
146     if(CA_STATUS_OK != CAStartListeningServer())
147     {
148         LOGI("Could not start Listening server");
149     }
150 }
151
152 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMStartDiscoveryServer(JNIEnv *env,
153     jobject obj)
154 {
155     LOGI("RMStartDiscoveryServer");
156
157     if(CA_STATUS_OK != CAStartDiscoveryServer())
158     {
159         LOGI("Could not start discovery server");
160     }
161 }
162
163 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMRegisterHandler(JNIEnv *env,
164     jobject obj)
165 {
166     LOGI("RMRegisterHandler");
167
168     CARegisterHandler(request_handler, response_handler);
169 }
170
171 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMFindResource(JNIEnv *env, jobject obj,
172     jstring uri)
173 {
174     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
175     LOGI("RMFindResource - %s", strUri);
176
177     // create token
178     CAToken_t token = NULL;
179     CAResult_t res = CAGenerateToken(&token);
180     if (res != CA_STATUS_OK)
181     {
182         printf("token generate error!!\n");
183         token = NULL;
184     }
185
186     LOGI("generated token %s\n", (token != NULL) ? token : "");
187
188     if(CA_STATUS_OK != CAFindResource((const CAURI_t)strUri, token))
189     {
190         LOGI("Could not find resource");
191     }
192     else
193     {
194         LOGI("find resource to %s URI", strUri);
195         gLastRequestToken = token;
196     }
197 }
198
199 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj,
200     jstring uri, jstring payload, jint selectedNetwork, jint isSecured, jint msgType)
201 {
202     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
203     LOGI("RMSendRequest - %s", strUri);
204
205     CAResult_t res;
206
207     LOGI("selectedNetwork - %d", selectedNetwork);
208     res = get_network_type(selectedNetwork);
209     if (res != CA_STATUS_OK)
210     {
211         return;
212     }
213
214     //create remote endpoint
215     CARemoteEndpoint_t* endpoint = NULL;
216
217     if(CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t)strUri, gSelectedNwType, &endpoint))
218     {
219         LOGI("Could not create remote end point");
220         CADestroyRemoteEndpoint(endpoint);
221         return;
222     }
223
224     CAMessageType_t messageType = msgType;
225
226     // create token
227     CAToken_t token = NULL;
228     res = CAGenerateToken(&token);
229     if (res != CA_STATUS_OK)
230     {
231         printf("token generate error!!\n");
232         token = NULL;
233     }
234
235     char resourceURI[15] = {0};
236
237     get_resource_uri((const CAURI_t)strUri, resourceURI, 14);
238
239     CAInfo_t requestData;
240     memset(&requestData, 0, sizeof(CAInfo_t));
241     requestData.token = token;
242
243     const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL);
244     if (isSecured == 1)
245     {
246         int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
247         requestData.payload = (CAPayload_t) malloc(length);
248         sprintf(requestData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
249     }
250     else
251     {
252         int length = strlen(strPayload) + strlen(resourceURI) + 1;
253         requestData.payload = (CAPayload_t) malloc(length);
254         sprintf(requestData.payload, strPayload, resourceURI);
255     }
256
257     requestData.type = messageType;
258
259     CARequestInfo_t requestInfo;
260     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
261     requestInfo.method = CA_GET;
262     requestInfo.info = requestData;
263
264     // send request
265     if(CA_STATUS_OK != CASendRequest(endpoint, &requestInfo))
266     {
267         LOGI("Could not send request");
268     }
269
270     // destroy token
271     if (token != NULL)
272     {
273         CADestroyToken(token);
274     }
275
276     // destroy remote endpoint
277     if (endpoint != NULL)
278     {
279         CADestroyRemoteEndpoint(endpoint);
280     }
281 }
282
283 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendResponse(JNIEnv *env,
284     jobject obj, jstring uri, jstring payload, jint selectedNetwork, jint isSecured)
285 {
286     LOGI("RMSendResponse");
287
288     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
289     LOGI("RMSendResponse - %s", strUri);
290
291     CAResult_t res;
292
293     LOGI("selectedNetwork - %d", selectedNetwork);
294
295     res = get_network_type(selectedNetwork);
296     if (res != CA_STATUS_OK)
297     {
298         LOGI("Not supported network type");
299         return;
300     }
301
302     //create remote endpoint
303     CARemoteEndpoint_t* endpoint = NULL;
304
305     if(CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t)strUri, gSelectedNwType, &endpoint))
306     {
307         LOGI("Could not create remote end point");
308         CADestroyRemoteEndpoint(endpoint);
309         return;
310     }
311
312     CAMessageType_t messageType = CA_MSG_ACKNOWLEDGE;
313
314     // create token
315     CAToken_t token = NULL;
316     res = CAGenerateToken(&token);
317     if (res != CA_STATUS_OK)
318     {
319         LOGI("token generate error!");
320         token = NULL;
321     }
322
323     char resourceURI[15] = {0};
324
325     get_resource_uri((const CAURI_t)strUri, resourceURI, 14);
326
327     CAInfo_t responseData;
328     memset(&responseData, 0, sizeof(CAInfo_t));
329     responseData.token = token;
330
331     const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL);
332     if (isSecured == 1)
333     {
334         int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
335         responseData.payload = (CAPayload_t) malloc(length);
336         sprintf(responseData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
337     }
338     else
339     {
340         int length = strlen(strPayload) + strlen(resourceURI) + 1;
341         responseData.payload = (CAPayload_t) malloc(length);
342         sprintf(responseData.payload, strPayload, resourceURI);
343     }
344
345     responseData.type = messageType;
346
347     CAResponseInfo_t responseInfo;
348     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
349     responseInfo.result = CA_SUCCESS;
350     responseInfo.info = responseData;
351
352     // send request
353     if(CA_STATUS_OK != CASendResponse(endpoint, &responseInfo))
354     {
355         LOGI("Could not send response");
356     }
357
358     LOGI("Send response");
359
360     // destroy token
361     if (token != NULL)
362     {
363         CADestroyToken(token);
364     }
365
366     // destroy remote endpoint
367     if (endpoint != NULL)
368     {
369         CADestroyRemoteEndpoint(endpoint);
370     }
371
372 }
373
374 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMAdvertiseResource(JNIEnv *env,
375     jobject obj, jstring uri, jint selectedNetwork)
376 {
377     LOGI("RMAdvertiseResource");
378
379     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
380
381     int optionNum = 2;
382
383     CAHeaderOption_t *headerOpt;
384     headerOpt = (CAHeaderOption_t*) malloc(sizeof(CAHeaderOption_t) * optionNum);
385     if (NULL == headerOpt)
386     {
387         printf("Memory allocation failed!\n");
388         return;
389     }
390     memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
391
392     char* tmpOptionData1 = "Hello";
393     headerOpt[0].optionID = 3000;
394     memcpy(headerOpt[0].optionData, tmpOptionData1, strlen(tmpOptionData1));
395     headerOpt[0].optionLength = (uint16_t) strlen(tmpOptionData1);
396
397     char* tmpOptionData2 = "World";
398     headerOpt[1].optionID = 3001;
399     memcpy(headerOpt[1].optionData, tmpOptionData2, strlen(tmpOptionData2));
400     headerOpt[1].optionLength = (uint16_t) strlen(tmpOptionData2);
401
402     // create token
403     CAToken_t token = NULL;
404     CAResult_t res = CAGenerateToken(&token);
405     if (res != CA_STATUS_OK)
406     {
407         LOGI("token generate error!");
408         token = NULL;
409     }
410
411     CAAdvertiseResource((const CAURI_t)strUri, token, headerOpt, (uint8_t) optionNum);
412
413     free(headerOpt);
414 }
415
416 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendNotification(JNIEnv *env,
417     jobject obj, jstring uri, jstring payload, jint selectedNetwork, jint isSecured)
418 {
419     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
420     LOGI("RMSendNotification - %s", strUri);
421
422     CAResult_t res;
423
424     LOGI("selectedNetwork - %d", selectedNetwork);
425
426     res = get_network_type(selectedNetwork);
427     if (res != CA_STATUS_OK)
428     {
429         LOGI("Not supported network type");
430         return;
431     }
432
433     //create remote endpoint
434     CARemoteEndpoint_t* endpoint = NULL;
435
436     if(CA_STATUS_OK != CACreateRemoteEndpoint((const CAURI_t)strUri, gSelectedNwType, &endpoint))
437     {
438         LOGI("Could not create remote end point");
439         CADestroyRemoteEndpoint(endpoint);
440         return;
441     }
442
443     CAMessageType_t messageType = CA_MSG_NONCONFIRM;
444
445     // create token
446     CAToken_t token = NULL;
447     res = CAGenerateToken(&token);
448     if (res != CA_STATUS_OK)
449     {
450         LOGI("token generate error!");
451         token = NULL;
452     }
453
454     char resourceURI[15] = {0};
455
456     get_resource_uri((const CAURI_t)strUri, resourceURI, 14);
457
458     CAInfo_t responseData;
459     memset(&responseData, 0, sizeof(CAInfo_t));
460     responseData.token = token;
461
462     const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL);
463     if (isSecured == 1)
464     {
465         int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
466         responseData.payload = (CAPayload_t) malloc(length);
467         sprintf(responseData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
468     }
469     else
470     {
471         int length = strlen(strPayload) + strlen(resourceURI) + 1;
472         responseData.payload = (CAPayload_t) malloc(length);
473         sprintf(responseData.payload, strPayload, resourceURI);
474     }
475
476     responseData.type = messageType;
477
478     CAResponseInfo_t responseInfo;
479     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
480     responseInfo.result = CA_SUCCESS;
481     responseInfo.info = responseData;
482
483     // send request
484     if(CA_STATUS_OK != CASendNotification(endpoint, &responseInfo))
485     {
486         LOGI("Could not send notification");
487     }
488
489     LOGI("Send Notification");
490
491     // destroy token
492     if (token != NULL)
493     {
494         CADestroyToken(token);
495     }
496
497     // destroy remote endpoint
498     if (endpoint != NULL)
499     {
500         CADestroyRemoteEndpoint(endpoint);
501     }
502 }
503
504 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj,
505     jint networkType)
506 {
507     LOGI("RMSelectNetwork Type : %d", networkType);
508
509     if(CA_STATUS_OK != CASelectNetwork(networkType))
510     {
511         LOGI("Could not select network");
512     }
513 }
514
515 JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMHandleRequestResponse(JNIEnv *env,
516     jobject obj)
517 {
518     LOGI("RMHandleRequestResponse");
519
520     if(CA_STATUS_OK != CAHandleRequestResponse())
521     {
522         LOGI("Could not handle request and response");
523     }
524 }
525
526 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
527 {
528     if (!object)
529     {
530         LOGI("Remote endpoint is NULL!");
531         return;
532     }
533
534     if (!requestInfo)
535     {
536         LOGI("Request info is NULL!");
537         return;
538     }
539
540     LOGI("##########received request from remote device #############\n");
541     LOGI("Uri: %s\n", object->resourceUri);
542     LOGI("Remote Address: %s\n", object->addressInfo.IP.ipAddress);
543
544     LOGI("Data: %s\n", requestInfo->info.payload);
545
546     if (NULL != gResponseListenerObject)
547     {
548         callback("received request from remote device", "#######");
549         callback("Uri: ", object->resourceUri);
550
551         callback("Remote Address: ", (char *) object->addressInfo.IP.ipAddress);
552
553         if(requestInfo->info.payload)
554         {
555             callback("Data: ", requestInfo->info.payload);
556         }
557     }
558
559     if (gLastRequestToken != NULL && requestInfo->info.token != NULL
560         && (strcmp((char *)gLastRequestToken, requestInfo->info.token) == 0))
561     {
562         LOGI("token is same. received request of it's own. skip.. \n");
563         return;
564     }
565
566     if (requestInfo->info.options)
567     {
568         uint32_t len = requestInfo->info.numOptions;
569         uint32_t i;
570         for (i = 0; i < len; i++)
571         {
572             LOGI("Option %d\n", i + 1);
573             LOGI("ID : %d\n", requestInfo->info.options[i].optionID);
574             LOGI("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
575                    requestInfo->info.options[i].optionData);
576
577             if (NULL != gResponseListenerObject)
578             {
579                 char tmpbuf[30];
580                 sprintf(tmpbuf, "%d", i + 1);
581                 callback("Option: ", tmpbuf);
582
583                 sprintf(tmpbuf, "%d", requestInfo->info.options[i].optionID);
584                 callback("ID: ", tmpbuf);
585
586                 sprintf(tmpbuf, "Data:[%d]",  requestInfo->info.options[i].optionLength);
587                 callback("tmpbuf: ", requestInfo->info.options[i].optionData);
588             }
589         }
590     }
591     printf("############################################################\n");
592
593     //Check if this has secure communication information
594     if (requestInfo->info.payload)
595     {
596         int securePort = get_secure_information(requestInfo->info.payload);
597         if (0 < securePort) //Set the remote endpoint secure details and send response
598         {
599             LOGI("This is secure resource...\n");
600             char *uri = NULL;
601             int length = 0;
602
603             length = 8; //length of "coaps://"
604             length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
605             length += strlen(object->resourceUri) + 1;
606
607             uri = calloc(1,sizeof(char)*length);
608             if (!uri)
609             {
610                 printf("Failed to create new uri\n");
611                 return;
612             }
613             sprintf(uri,"coaps://%s:%d/%s",object->addressInfo.IP.ipAddress,
614                       securePort, object->resourceUri);
615
616             CARemoteEndpoint_t *endpoint = NULL;
617             if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
618             {
619                 LOGI("Failed to create duplicate of remote endpoint!\n");
620                 return;
621             }
622             endpoint->isSecured = CA_TRUE;
623             object = endpoint;
624         }
625     }
626
627     gReceived = 1;
628
629     // response
630 //    send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
631
632 }
633
634 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
635 {
636
637     LOGI("##########Received response from remote device #############\n");
638     LOGI("Uri: %s\n", object->resourceUri);
639     LOGI("Remote Address: %s\n", object->addressInfo.IP.ipAddress);
640     LOGI("response result: %d\n", responseInfo->result);
641     LOGI("Data: %s\n", responseInfo->info.payload);
642
643     if (NULL != gResponseListenerObject)
644     {
645         callback("received response from remote device", "#######");
646         callback("Uri: ", object->resourceUri);
647
648         callback("Remote Address: ", (char *)object->addressInfo.IP.ipAddress);
649
650         if(responseInfo->info.payload)
651         {
652             callback("Data: ", responseInfo->info.payload);
653         }
654     }
655
656     if (responseInfo->info.options)
657     {
658         uint32_t len = responseInfo->info.numOptions;
659         uint32_t i;
660         for (i = 0; i < len; i++)
661         {
662             LOGI("Option %d\n", i + 1);
663             LOGI("ID : %d\n", responseInfo->info.options[i].optionID);
664             LOGI("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
665                       responseInfo->info.options[i].optionData);
666
667             if (NULL != gResponseListenerObject)
668             {
669                 char tmpbuf[30];
670                 sprintf(tmpbuf, "%d", i + 1);
671                 callback("Option: ", tmpbuf);
672
673                 sprintf(tmpbuf, "%d", responseInfo->info.options[i].optionID);
674                 callback("ID: ", tmpbuf);
675
676                 sprintf(tmpbuf, "Data:[%d]",  responseInfo->info.options[i].optionLength);
677                 callback("tmpbuf: ", responseInfo->info.options[i].optionData);
678             }
679         }
680     }
681     LOGI("############################################################\n");
682     gReceived = 1;
683
684     //Check if this has secure communication information
685     if (responseInfo->info.payload)
686     {
687         int securePort = get_secure_information(responseInfo->info.payload);
688         if (0 < securePort) //Set the remote endpoint secure details and send response
689         {
690             LOGI("This is secure resource...\n");
691         }
692     }
693 }
694
695 void send_response(const CARemoteEndpoint_t* endpoint, CAToken_t request_token)
696 {
697     LOGI("send_response");
698
699     CAInfo_t responseData;
700     memset(&responseData, 0, sizeof(CAInfo_t));
701     responseData.token = request_token;
702     responseData.payload = "response payload";
703
704     CAResponseInfo_t responseInfo;
705     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
706     responseInfo.result = 203;
707     responseInfo.info = responseData;
708
709     // send request
710     CASendResponse(endpoint, &responseInfo);
711 }
712
713 void get_resource_uri(char *URI, char *resourceURI, int length)
714 {
715     char *startPos = URI;
716     char *temp = NULL;
717     if (NULL != (temp = strstr(URI, "://")))
718     {
719         startPos = strchr(temp + 3, '/');
720         if (!startPos)
721         {
722             printf("Resource URI is missing\n");
723             return;
724         }
725     }
726
727     char *endPos = strchr(startPos, '?');
728     if (!endPos)
729     {
730         endPos = URI + strlen(URI);
731     }
732     endPos -= 1;
733
734     if (endPos - startPos <= length)
735         memcpy(resourceURI, startPos + 1, endPos - startPos);
736
737     printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
738 }
739
740 int get_secure_information(CAPayload_t payLoad)
741 {
742     printf("entering get_secure_information\n");
743
744     if (!payLoad)
745     {
746         printf("Payload is NULL\n");
747         return -1;
748     }
749
750     char *subString = NULL;
751     if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
752     {
753         printf("This is not secure resource\n");
754         return -1;
755     }
756
757     if (NULL == (subString = strstr(payLoad, "\"port\":")))
758     {
759         printf("This secure resource does not have port information\n");
760         return -1;
761     }
762
763     char *startPos = strstr(subString, ":");
764     if (!startPos)
765     {
766         printf("Parsing failed !\n");
767         return -1;
768     }
769
770     char *endPos = strstr(startPos, "}");
771     if (!endPos)
772     {
773         printf("Parsing failed !\n");
774         return -1;
775     }
776
777     char portStr[4] = {0};
778     memcpy(portStr, startPos + 1, (endPos-1) - startPos);
779
780     printf("secured port is: %s\n", portStr);
781     return atoi(portStr);
782 }
783
784 CAResult_t get_network_type(int selectedNetwork)
785 {
786
787     int number = selectedNetwork;
788
789     if (!(number & 0xf))
790     {
791         return CA_NOT_SUPPORTED;
792     }
793     if (number & CA_ETHERNET)
794     {
795         gSelectedNwType = CA_ETHERNET;
796         return CA_STATUS_OK;
797     }
798     if (number & CA_WIFI)
799     {
800         gSelectedNwType = CA_WIFI;
801         return CA_STATUS_OK;
802     }
803     if (number & CA_EDR)
804     {
805         gSelectedNwType = CA_EDR;
806         return CA_STATUS_OK;
807     }
808     if (number & CA_LE)
809     {
810         gSelectedNwType = CA_LE;
811         return CA_STATUS_OK;
812     }
813
814     return CA_NOT_SUPPORTED;
815 }
816
817 void callback(char *subject, char *receicedData)
818 {
819     JNIEnv* env = NULL;
820     int status = (*g_jvm)->GetEnv(g_jvm, (void **) &env, JNI_VERSION_1_6);
821     int res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
822
823     jclass cls = (*env)->GetObjectClass(env, gResponseListenerObject);
824     jmethodID mid = (*env)->GetMethodID(env, cls, "OnResponseReceived", "(Ljava/lang/String;Ljava/lang/String;)V");
825
826     jstring jsubject = (*env)->NewStringUTF(env, (char*)subject);
827     jstring jreceivedData = (*env)->NewStringUTF(env, (char*)receicedData);
828     (*env)->CallVoidMethod(env, gResponseListenerObject, mid, jsubject, jreceivedData);
829 }