1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
31 #include "cainterface.h"
33 static GMainLoop *mainloop = NULL;
37 #define MAX_BUF_LEN 1024
38 #define MAX_OPT_LEN 16
45 #define IDENTITY ("1111111111111111")
49 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
53 int g_localUnicastPort = 0;
54 int g_localSecurePort = 0;
55 CAConnectivityType_t g_selectedNwType = CA_ETHERNET;
56 const char* g_messageType[] = {"CON", "NON", "ACK", "RESET"};
60 CAResult_t GetNetworkType();
62 CAResult_t Initialize();
63 void StartListeningServer();
64 void StartDiscoveryServer();
67 void SendRequestAll();
69 void AdvertiseResource();
70 void SendNotification();
72 void UnselectNetwork();
73 void HandleRequestResponse();
74 void FindFixedResource();
75 void GetNetworkInfo();
77 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
78 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
79 void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token);
80 void GetResourceUri(char *URI, char *resourceURI, int length);
81 int GetSecureInformation(CAPayload_t payLoad);
83 static CAToken_t g_lastRequestToken = NULL;
84 static const char g_secureInfoData[] = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
85 "\"if\":[\"oc.mi.def\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
86 static const char g_normalInfoData[] = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
87 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
90 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
92 void ClearDtlsCredentialInfo()
94 printf("clearDtlsCredentialInfo IN\n");
97 // Initialize sensitive data to zeroes before freeing.
98 if (pskCredsBlob->creds)
100 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
101 free(pskCredsBlob->creds);
104 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
108 printf("clearDtlsCredentialInfo OUT\n");
111 // Internal API. Invoked by CA stack to retrieve credentials from this module
112 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
114 printf("CAGetDtlsPskCredentials IN\n");
117 printf("Invalid credential container");
121 *credInfo = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
122 if (NULL == *credInfo)
124 printf("Failed to allocate credential blob.");
128 int16_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
129 (*credInfo)->creds = (OCDtlsPskCreds *)malloc(credLen);
130 if (NULL == (*credInfo)->creds)
132 printf("Failed to allocate credentials.");
138 memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
139 (*credInfo)->num = pskCredsBlob->num;
140 memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
142 printf("CAGetDtlsPskCredentials OUT\n");
145 bool SetCredentials()
147 printf("SetCredentials IN\n");
148 pskCredsBlob = (CADtlsPskCredsBlob_t *)calloc(1, sizeof(CADtlsPskCredsBlob_t));
149 if (NULL == pskCredsBlob)
151 printf("Memory allocation failed!\n");
155 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
156 pskCredsBlob->num = 1;
158 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
159 if (NULL == pskCredsBlob->creds)
161 printf("Memory allocation failed!\n");
166 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
167 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
169 printf("SetCredentials OUT\n");
176 g_main_loop_run(mainloop);
183 printf("\n=============================================\n");
184 printf("\t\tsample main\n");
185 printf("=============================================\n");
187 CAResult_t res = CA_STATUS_FAILED;
189 * Read DTLS PSK credentials from persistent storage and
190 * set in the OC stack.
193 if (SetCredentials() == false)
195 printf("SetCredentials failed\n");
199 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
200 if(CA_STATUS_OK != res)
202 printf("Set credential handler fail\n");
207 if (CA_STATUS_OK != res)
209 printf("CAInitialize fail\n");
214 CARegisterHandler(RequestHandler, ResponseHandler);
218 if (g_lastRequestToken != NULL)
220 CADestroyToken(g_lastRequestToken);
222 g_lastRequestToken = NULL;
226 ClearDtlsCredentialInfo();
235 char menu = toupper(PrintOptions());
243 case 'Q': // quits the sample program
244 printf("quit..!!\n");
245 g_main_loop_quit(mainloop);
248 case 's': // start server
249 case 'S': // start server
250 StartListeningServer();
253 case 't': // send request
258 case 'c': // start client
260 StartDiscoveryServer();
263 case 'f': // find resource
268 case 'r': // send request
273 case 'a': // advertise resource
278 case 'b': // send notification
283 case 'n': // select network
288 case 'x': // unselect network
293 case 'h': // handle request response
295 HandleRequestResponse();
304 while (g_received == 0)
307 HandleRequestResponse();
314 StartListeningServer();
318 HandleRequestResponse();
322 case 'g': // get network information
328 printf("not supported menu!!\n");
335 CAResult_t Initialize()
337 CAResult_t res = CA_STATUS_FAILED;
338 mainloop = g_main_loop_new(NULL, FALSE);
341 printf("g_main_loop_new failed\n");
345 int result = pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
348 printf("pthread_create failed in initialize\n");
352 res = CAInitialize();
353 if (res != CA_STATUS_OK)
355 printf("CAInitialize fail\n");
360 void StartListeningServer()
362 printf("start listening server!!\n");
364 CAResult_t res = CAStartListeningServer();
365 if (res != CA_STATUS_OK)
367 printf("start listening server fail\n");
371 printf("start listening server success\n");
375 void StartDiscoveryServer()
377 printf("start discovery client!!\n");
379 CAResult_t res = CAStartDiscoveryServer();
380 if (res != CA_STATUS_OK)
382 printf("start discovery client fail\n");
386 printf("start discovery client success\n");
390 void FindFixedResource()
392 char buf[] ="/a/light";
395 CAToken_t token = NULL;
396 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
398 CAResult_t res = CAGenerateToken(&token, tokenLength);
399 if ((CA_STATUS_OK != res) || (!token))
401 printf("token generate error!!");
405 printf("Generated token %s\n", token);
407 res = CAFindResource(buf, token, tokenLength);
408 if (res != CA_STATUS_OK)
410 printf("find resource error : %d\n", res);
414 printf("find resource to %s URI\n", buf);
418 CADestroyToken(token);
420 printf("=============================================\n");
425 char buf[MAX_BUF_LEN] = {0};
427 printf("\n=============================================\n");
428 printf("ex) /a/light\n");
429 printf("reference uri : ");
431 if(!fgets(buf, MAX_BUF_LEN, stdin))
433 printf("fgets error\n");
438 CAToken_t token = NULL;
439 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
441 CAResult_t res = CAGenerateToken(&token, tokenLength);
442 if ((CA_STATUS_OK != res) || (!token))
444 printf("token generate error!!\n");
448 printf("Generated token %s\n", token);
450 res = CAFindResource(buf, token, tokenLength);
451 if (res != CA_STATUS_OK)
453 printf("find resource error : %d\n", res);
454 CADestroyToken(token);
458 printf("find resource to %s URI\n", buf);
460 CADestroyToken(g_lastRequestToken);
462 g_lastRequestToken = token;
465 printf("=============================================\n");
470 char secureRequest[2] = {0};
471 CAResult_t res = CA_STATUS_FAILED;
472 char uri[MAX_BUF_LEN] = {'\0'};
474 res = GetNetworkType();
475 if (res != CA_STATUS_OK)
480 printf("Do you want to send secure request ?.... enter (0/1): \n");
481 if(!fgets(secureRequest, 1, stdin))
483 printf("fgets error\n");
487 if ('1' == secureRequest[0])
489 printf("Enter the URI like below....\n");
490 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
495 printf("Enter the URI like below....\n");
496 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
497 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
501 if(!fgets(uri, MAX_BUF_LEN, stdin))
503 printf("fgets error\n");
507 // create remote endpoint
508 CARemoteEndpoint_t *endpoint = NULL;
509 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, g_selectedNwType, &endpoint)
512 printf("Failed to create remote endpoint!\n");
513 CADestroyRemoteEndpoint(endpoint);
517 char buf[MAX_BUF_LEN]={0};
519 printf("\n=============================================\n");
520 printf("0:CON, 1:NON\n");
521 printf("select message type : ");
523 if(!fgets(buf, MAX_BUF_LEN, stdin))
525 printf("fgets error\n");
526 CADestroyRemoteEndpoint(endpoint);
530 CAMessageType_t msgType = (buf[0] == '0' || buf[0] == '1') ? buf[0] - '0' : 0;
533 CAToken_t token = NULL;
534 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
536 res = CAGenerateToken(&token, tokenLength);
537 if ((CA_STATUS_OK != res) || (!token))
539 printf("token generate error, error code : %d\n", res);
543 printf("Generated token %s\n", token);
545 // extract relative resourceuri from give uri
546 char resourceURI[15] = {0};
548 printf("URI : %s\n", uri);
549 GetResourceUri(uri, resourceURI, 14);
551 // create request data
552 CAInfo_t requestData = {CA_MSG_RESET};
553 requestData.token = token;
554 if ('1' == secureRequest[0])
556 int length = strlen(g_secureInfoData) + strlen(resourceURI) + 1;
557 requestData.payload = (CAPayload_t) malloc(length);
558 if(!requestData.payload)
560 printf("Memory allocation is failed\n");
561 CADestroyToken(token);
562 CADestroyRemoteEndpoint(endpoint);
565 sprintf(requestData.payload, g_secureInfoData, resourceURI, g_localSecurePort);
569 int length = strlen(g_normalInfoData) + strlen(resourceURI) + 1;
570 requestData.payload = (CAPayload_t) malloc(length);
571 if(!requestData.payload)
573 printf("Memory allocation is failed\n");
574 CADestroyToken(token);
575 CADestroyRemoteEndpoint(endpoint);
578 sprintf(requestData.payload, g_normalInfoData, resourceURI);
580 requestData.type = msgType;
582 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
583 requestInfo.method = CA_GET;
584 requestInfo.info = requestData;
587 CASendRequest(endpoint, &requestInfo);
589 CADestroyToken(token);
591 free(requestData.payload);
593 // destroy remote endpoint
594 CADestroyRemoteEndpoint(endpoint);
595 printf("=============================================\n");
598 void SendRequestAll()
600 char buf[MAX_BUF_LEN] = {0};
602 CAResult_t res = CA_STATUS_FAILED;
604 res = GetNetworkType();
605 if (res != CA_STATUS_OK)
610 printf("\n=============================================\n");
611 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
612 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
615 if(!fgets(buf, MAX_BUF_LEN, stdin))
617 printf("fgets error\n");
621 // create remote endpoint
622 CARemoteEndpoint_t *endpoint = NULL;
623 res = CACreateRemoteEndpoint(buf, g_selectedNwType, &endpoint);
625 if (res != CA_STATUS_OK)
627 printf("create remote endpoint error!!\n");
628 CADestroyRemoteEndpoint(endpoint);
632 CAGroupEndpoint_t *group = NULL;
633 group = (CAGroupEndpoint_t *)malloc(sizeof(CAGroupEndpoint_t));
636 printf("Memory allocation failed!\n");
637 CADestroyRemoteEndpoint(endpoint);
640 group->connectivityType = endpoint->connectivityType;
641 group->resourceUri = endpoint->resourceUri;
644 CAToken_t token = NULL;
645 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
647 res = CAGenerateToken(&token, tokenLength);
648 if ((CA_STATUS_OK != res) || (!token))
650 printf("token generate error!!\n");
654 printf("generated token %s\n", token);
656 CAInfo_t requestData = {CA_MSG_RESET};
657 requestData.token = token;
658 requestData.tokenLength = tokenLength;
659 requestData.payload = "Temp Json Payload";
660 requestData.type = CA_MSG_NONCONFIRM;
662 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
663 requestInfo.method = CA_GET;
664 requestInfo.info = requestData;
667 CASendRequestToAll(group, &requestInfo);
669 CADestroyToken(token);
671 // destroy remote endpoint
672 CADestroyRemoteEndpoint(endpoint);
676 printf("=============================================\n");
679 void AdvertiseResource()
681 char buf[MAX_BUF_LEN] = {0};
683 printf("\n=============================================\n");
686 if(!fgets(buf, MAX_BUF_LEN, stdin))
688 printf("fgets error\n");
693 char optionData[MAX_OPT_LEN] = {0};
695 printf("Option Num : ");
696 scanf("%d", &optionNum);
697 CAHeaderOption_t *headerOpt = NULL;
698 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
699 if (NULL == headerOpt)
701 printf("Memory allocation failed!\n");
704 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
707 for (i = 0 ; i < optionNum ; i++)
710 printf("[%d] Option ID : ", i + 1);
711 scanf("%d", &optionID);
712 headerOpt[i].optionID = optionID;
714 printf("[%d] Option Data : ", i + 1);
715 scanf("%s", optionData);
716 int len = strlen(optionData);
717 memcpy(headerOpt[i].optionData, optionData, len + 1);
718 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
720 headerOpt[i].optionLength = (uint16_t)len;
722 printf("\n=============================================\n");
725 CAToken_t token = NULL;
726 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
728 CAResult_t res = CAGenerateToken(&token, tokenLength);
729 if ((CA_STATUS_OK != res) || (!token))
731 printf("token generate error!!\n");
735 printf("generated token %s\n", token);
737 CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t)optionNum);
740 CADestroyToken(token);
745 void SendNotification()
747 char buf[MAX_BUF_LEN] = {0};
749 CAResult_t res = CA_STATUS_FAILED;
751 res = GetNetworkType();
752 if (res != CA_STATUS_OK)
757 printf("\n=============================================\n");
758 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
759 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
762 if(!fgets(buf, MAX_BUF_LEN, stdin))
764 printf("fgets error\n");
768 // create remote endpoint
769 CARemoteEndpoint_t *endpoint = NULL;
770 res = CACreateRemoteEndpoint(buf, g_selectedNwType, &endpoint);
771 if (res != CA_STATUS_OK)
773 printf("create remote endpoint error!!\n");
774 CADestroyRemoteEndpoint(endpoint);
778 CAInfo_t respondData = {CA_MSG_RESET};
779 respondData.tokenLength = CA_MAX_TOKEN_LEN;
781 res = CAGenerateToken(&respondData.token, respondData.tokenLength);
782 if ((CA_STATUS_OK != res) || (!respondData.token))
784 printf("token generate error!!\n");
788 respondData.payload = "Temp Notification Data";
790 CAResponseInfo_t responseInfo = {0};
791 responseInfo.result = CA_SUCCESS;
792 responseInfo.info = respondData;
795 res = CASendNotification(endpoint, &responseInfo);
796 if (res != CA_STATUS_OK)
798 printf("send notification error, error code: %d\n", res);
802 printf("send notification success\n");
806 CADestroyToken(respondData.token);
807 // destroy remote endpoint
808 CADestroyRemoteEndpoint(endpoint);
810 printf("\n=============================================\n");
814 char buf[MAX_BUF_LEN] = {0};
816 printf("\n=============================================\n");
817 printf("\tselect network\n");
818 printf("ETHERNET : 0\n");
819 printf("WIFI : 1\n");
824 if(!fgets(buf, MAX_BUF_LEN, stdin))
826 printf("fgets error\n");
830 int number = buf[0] - '0';
832 number = (number < 0 || number > 3) ? 0 : number;
834 CAResult_t res = CASelectNetwork(1 << number);
835 if (res != CA_STATUS_OK)
837 printf("select network error\n");
838 g_selectedNwType = 1 << number;
842 printf("select network success\n");
845 printf("=============================================\n");
848 void UnselectNetwork()
850 char buf[MAX_BUF_LEN] = {0};
852 printf("\n=============================================\n");
853 printf("\tunselect enabled network\n");
854 printf("ETHERNET : 0\n");
855 printf("WIFI : 1\n");
860 if(!fgets(buf, MAX_BUF_LEN, stdin))
862 printf("fgets error\n");
866 int number = buf[0] - '0';
868 number = (number < 0 || number > 3) ? 1 : number;
870 CAResult_t res = CAUnSelectNetwork(1 << number);
871 if (res != CA_STATUS_OK)
873 printf("unselect network error\n");
877 printf("unselect network success\n");
879 printf("=============================================\n");
884 char buf[MAX_BUF_LEN] = {0};
886 printf("\n=============================================\n");
887 printf("\t\tMenu\n");
888 printf("\tm : show menu\n");
889 printf("\ts : start server\n");
890 printf("\tc : start client\n");
891 printf("\tf : find resource\n");
892 printf("\tr : send request\n");
893 printf("\tt : send request to all\n");
894 printf("\ta : advertise resource\n");
895 printf("\tb : send notification\n");
896 printf("\tn : select network\n");
897 printf("\tx : unselect network\n");
898 printf("\tg : get network information\n");
899 printf("\th : handle request response\n");
900 printf("\ty : run static client\n");
901 printf("\tz : run static server\n");
902 printf("\tq : quit\n");
903 printf("=============================================\n");
906 if(!fgets(buf, MAX_BUF_LEN, stdin))
908 printf("fgets error\n");
915 void HandleRequestResponse()
917 printf("handle_request_response\n");
919 CAResult_t res = CAHandleRequestResponse();
920 if (res != CA_STATUS_OK)
922 printf("handle request error\n");
926 printf("handle request success\n");
930 void GetNetworkInfo()
933 CALocalConnectivity_t *tempInfo = NULL;
934 uint32_t tempSize = 0;
936 CAGetNetworkInformation(&tempInfo, &tempSize);
937 if (tempInfo == NULL || tempSize <= 0)
939 printf("network not connected\n");
943 printf("################## Network Information #######################\n");
944 printf("network info total size is %d\n\n", tempSize);
946 for (index = 0; index < tempSize; index++)
948 printf("Type: %d\n", tempInfo[index].type);
949 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
950 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
951 printf("Secured: %d\n\n", tempInfo[index].isSecured);
953 if (true == tempInfo[index].isSecured)
955 g_localSecurePort = tempInfo[index].addressInfo.IP.port;
959 g_localUnicastPort = tempInfo[index].addressInfo.IP.port;
966 printf("##############################################################");
969 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
971 printf("##########received request from remote device #############\n");
974 printf("Remote endpoint is NULL!\n");
980 printf("Request info is NULL!\n");
984 printf("Uri: %s\n", object->resourceUri);
985 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
986 object->addressInfo.IP.port, object->isSecured);
988 printf("Data: %s\n", requestInfo->info.payload);
989 printf("Message type: %s\n", g_messageType[requestInfo->info.type]);
990 if (g_lastRequestToken != NULL && requestInfo->info.token != NULL
991 && (strcmp((char *)g_lastRequestToken, requestInfo->info.token) == 0))
993 printf("token is same. received request of it's own. skip.. \n");
997 if (requestInfo->info.options)
999 uint32_t len = requestInfo->info.numOptions;
1001 for (i = 0; i < len; i++)
1003 printf("Option %d\n", i + 1);
1004 printf("ID : %d\n", requestInfo->info.options[i].optionID);
1005 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
1006 requestInfo->info.options[i].optionData);
1009 printf("############################################################\n");
1011 //Check if this has secure communication information
1012 if (requestInfo->info.payload)
1014 int securePort = GetSecureInformation(requestInfo->info.payload);
1015 if (0 < securePort) //Set the remote endpoint secure details and send response
1017 printf("This is secure resource...\n");
1021 length = 8; //length of "coaps://"
1022 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
1023 length += strlen(object->resourceUri) + 1;
1025 uri = calloc(1, sizeof(char) * length);
1028 printf("Failed to create new uri\n");
1031 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
1032 securePort, object->resourceUri);
1034 CARemoteEndpoint_t *endpoint = NULL;
1035 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
1037 printf("Failed to create duplicate of remote endpoint!\n");
1040 //endpoint->connectivityType = object->connectivityType;
1041 endpoint->isSecured = true;
1048 printf("send response with URI\n");
1049 SendResponse(object, &requestInfo->info);
1054 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1056 printf("##########Received response from remote device #############\n");
1059 printf("response_handler object is null\n");
1065 printf("response_handler responseInfo is null\n");
1069 printf("Uri: %s\n", object->resourceUri);
1070 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1071 object->addressInfo.IP.port, object->isSecured);
1072 printf("response result : %d\n", responseInfo->result);
1073 printf("Data: %s\n", responseInfo->info.payload);
1074 printf("Message type: %s\n", g_messageType[responseInfo->info.type]);
1075 printf("Token: %s\n", responseInfo->info.token);
1076 if (responseInfo->info.options)
1078 uint32_t len = responseInfo->info.numOptions;
1080 for (i = 0; i < len; i++)
1082 printf("Option %d\n", i + 1);
1083 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1084 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1085 responseInfo->info.options[i].optionData);
1088 printf("############################################################\n");
1091 //Check if this has secure communication information
1092 if (responseInfo->info.payload)
1094 int securePort = GetSecureInformation(responseInfo->info.payload);
1095 if (0 < securePort) //Set the remote endpoint secure details and send response
1097 printf("This is secure resource...\n");
1102 void SendResponse(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1104 printf("entering send_response\n");
1108 printf("endpoint is null\n");
1112 CAInfo_t responseData = {CA_MSG_RESET};
1115 ((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) :
1117 responseData.messageId = 0;
1118 responseData.token = "";
1121 responseData.messageId = info->messageId;
1122 responseData.token = info->token;
1123 responseData.tokenLength = info->tokenLength;
1125 responseData.payload = "response payload";
1127 CAResponseInfo_t responseInfo = {0};
1128 responseInfo.result = 203;
1129 responseInfo.info = responseData;
1131 if (true == endpoint->isSecured)
1133 printf("Sending response on secure communication\n");
1137 printf("Sending response on non-secure communication\n");
1140 // send request (connectivityType from remoteEndpoint of request Info)
1141 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1142 if (res != CA_STATUS_OK)
1144 printf("send response failed\n");
1148 printf("send response success\n");
1151 printf("=============================================\n");
1154 void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token, uint8_t tokenLength)
1156 printf("\n=============================================\n");
1160 printf("endpoint is null\n");
1164 CAInfo_t requestData ={CA_MSG_RESET};
1165 requestData.token = token;
1166 requestData.tokenLength = tokenLength;
1167 requestData.payload = "Temp Json Payload";
1169 CARequestInfo_t requestInfo ={CA_GET, {CA_MSG_RESET}};
1170 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1171 requestInfo.method = CA_GET;
1172 requestInfo.info = requestData;
1175 endpoint->connectivityType = CA_WIFI;
1177 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1178 if (res != CA_STATUS_OK)
1180 printf("send request failed\n");
1184 printf("send request success\n");
1187 printf("=============================================\n");
1191 int GetSecureInformation(CAPayload_t payLoad)
1193 printf("entering get_secure_information\n");
1197 printf("Payload is NULL\n");
1201 char *subString = NULL;
1202 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1204 printf("This is not secure resource\n");
1208 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1210 printf("This secure resource does not have port information\n");
1214 char *startPos = strstr(subString, ":");
1217 printf("Parsing failed !\n");
1221 char *endPos = strstr(startPos, "}");
1224 printf("Parsing failed !\n");
1228 if(((endPos - 1) - startPos) > 4)
1230 printf("port length is not proper.Exceeding length 4\n");
1234 char portStr[4] = {0};
1235 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1237 printf("secured port is: %s\n", portStr);
1238 return atoi(portStr);
1241 void GetResourceUri(char *URI, char *resourceURI, int length)
1243 char *startPos = URI;
1245 if (NULL != (temp = strstr(URI, "://")))
1247 startPos = strchr(temp + 3, '/');
1250 printf("Resource URI is missing\n");
1255 char *endPos = strchr(startPos, '?');
1258 endPos = URI + strlen(URI);
1262 if (endPos - startPos <= length)
1263 memcpy(resourceURI, startPos + 1, endPos - startPos);
1265 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1268 CAResult_t GetNetworkType()
1270 char buf[MAX_BUF_LEN] = {0};
1272 printf("\n=============================================\n");
1273 printf("\tselect network type\n");
1274 printf("ETHERNET : 0\n");
1275 printf("WIFI : 1\n");
1276 printf("EDR : 2\n");
1278 printf("select : ");
1280 if(!fgets(buf, MAX_BUF_LEN, stdin))
1282 printf("fgets error\n");
1283 return CA_STATUS_FAILED;
1286 int number = buf[0] - '0';
1288 number = (number < 0 || number > 3) ? 0 : 1 << number;
1290 if (!(number & 0xf))
1292 return CA_NOT_SUPPORTED;
1295 if (number & CA_ETHERNET)
1297 g_selectedNwType = CA_ETHERNET;
1298 return CA_STATUS_OK;
1301 if (number & CA_WIFI)
1303 g_selectedNwType = CA_WIFI;
1304 return CA_STATUS_OK;
1307 if (number & CA_EDR)
1309 g_selectedNwType = CA_EDR;
1310 return CA_STATUS_OK;
1315 g_selectedNwType = CA_LE;
1316 return CA_STATUS_OK;
1319 printf("\n=============================================\n");
1321 return CA_STATUS_FAILED;