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 CATransportType_t g_selected_nw_type = CA_IPV4;
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->transportType = endpoint->transportType;
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 = get_network_type();
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("IPv4 : 0\n");
823 if(!fgets(buf, MAX_BUF_LEN, stdin))
825 printf("fgets error\n");
829 int number = buf[0] - '0';
831 number = (number < 0 || number > 3) ? 0 : number;
833 CAResult_t res = CASelectNetwork(1 << number);
834 if (res != CA_STATUS_OK)
836 printf("select network error\n");
837 g_selectedNwType = 1 << number;
841 printf("select network success\n");
844 printf("=============================================\n");
847 void UnselectNetwork()
849 char buf[MAX_BUF_LEN] = {0};
851 printf("\n=============================================\n");
852 printf("\tunselect enabled network\n");
853 printf("ETHERNET : 0\n");
854 printf("WIFI : 1\n");
859 if(!fgets(buf, MAX_BUF_LEN, stdin))
861 printf("fgets error\n");
865 int number = buf[0] - '0';
867 number = (number < 0 || number > 3) ? 1 : number;
869 CAResult_t res = CAUnSelectNetwork(1 << number);
870 if (res != CA_STATUS_OK)
872 printf("unselect network error\n");
876 printf("unselect network success\n");
878 printf("=============================================\n");
883 char buf[MAX_BUF_LEN] = {0};
885 printf("\n=============================================\n");
886 printf("\t\tMenu\n");
887 printf("\tm : show menu\n");
888 printf("\ts : start server\n");
889 printf("\tc : start client\n");
890 printf("\tf : find resource\n");
891 printf("\tr : send request\n");
892 printf("\tt : send request to all\n");
893 printf("\ta : advertise resource\n");
894 printf("\tb : send notification\n");
895 printf("\tn : select network\n");
896 printf("\tx : unselect network\n");
897 printf("\tg : get network information\n");
898 printf("\th : handle request response\n");
899 printf("\ty : run static client\n");
900 printf("\tz : run static server\n");
901 printf("\tq : quit\n");
902 printf("=============================================\n");
905 if(!fgets(buf, MAX_BUF_LEN, stdin))
907 printf("fgets error\n");
914 void HandleRequestResponse()
916 printf("handle_request_response\n");
918 CAResult_t res = CAHandleRequestResponse();
919 if (res != CA_STATUS_OK)
921 printf("handle request error\n");
925 printf("handle request success\n");
929 void GetNetworkInfo()
932 CALocalConnectivity_t *tempInfo = NULL;
933 uint32_t tempSize = 0;
935 CAGetNetworkInformation(&tempInfo, &tempSize);
936 if (tempInfo == NULL || tempSize <= 0)
938 printf("network not connected\n");
942 printf("################## Network Information #######################\n");
943 printf("network info total size is %d\n\n", tempSize);
945 for (index = 0; index < tempSize; index++)
947 printf("Type: %d\n", tempInfo[index].type);
948 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
949 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
950 printf("Secured: %d\n\n", tempInfo[index].isSecured);
952 if (true == tempInfo[index].isSecured)
954 g_localSecurePort = tempInfo[index].addressInfo.IP.port;
958 g_localUnicastPort = tempInfo[index].addressInfo.IP.port;
965 printf("##############################################################");
968 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
970 printf("##########received request from remote device #############\n");
973 printf("Remote endpoint is NULL!\n");
979 printf("Request info is NULL!\n");
983 printf("Uri: %s\n", object->resourceUri);
984 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
985 object->addressInfo.IP.port, object->isSecured);
987 printf("Data: %s\n", requestInfo->info.payload);
988 printf("Message type: %s\n", g_messageType[requestInfo->info.type]);
989 if (g_lastRequestToken != NULL && requestInfo->info.token != NULL
990 && (strcmp((char *)g_lastRequestToken, requestInfo->info.token) == 0))
992 printf("token is same. received request of it's own. skip.. \n");
996 if (requestInfo->info.options)
998 uint32_t len = requestInfo->info.numOptions;
1000 for (i = 0; i < len; i++)
1002 printf("Option %d\n", i + 1);
1003 printf("ID : %d\n", requestInfo->info.options[i].optionID);
1004 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
1005 requestInfo->info.options[i].optionData);
1008 printf("############################################################\n");
1010 //Check if this has secure communication information
1011 if (requestInfo->info.payload)
1013 int securePort = GetSecureInformation(requestInfo->info.payload);
1014 if (0 < securePort) //Set the remote endpoint secure details and send response
1016 printf("This is secure resource...\n");
1020 length = 8; //length of "coaps://"
1021 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
1022 length += strlen(object->resourceUri) + 1;
1024 uri = calloc(1, sizeof(char) * length);
1027 printf("Failed to create new uri\n");
1030 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
1031 securePort, object->resourceUri);
1033 CARemoteEndpoint_t *endpoint = NULL;
1034 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
1036 printf("Failed to create duplicate of remote endpoint!\n");
1039 //endpoint->connectivityType = object->connectivityType;
1040 endpoint->isSecured = true;
1047 printf("send response with URI\n");
1048 SendResponse(object, &requestInfo->info);
1053 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1055 printf("##########Received response from remote device #############\n");
1058 printf("response_handler object is null\n");
1064 printf("response_handler responseInfo is null\n");
1068 printf("Uri: %s\n", object->resourceUri);
1069 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1070 object->addressInfo.IP.port, object->isSecured);
1071 printf("response result : %d\n", responseInfo->result);
1072 printf("Data: %s\n", responseInfo->info.payload);
1073 printf("Message type: %s\n", g_messageType[responseInfo->info.type]);
1074 printf("Token: %s\n", responseInfo->info.token);
1075 if (responseInfo->info.options)
1077 uint32_t len = responseInfo->info.numOptions;
1079 for (i = 0; i < len; i++)
1081 printf("Option %d\n", i + 1);
1082 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1083 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1084 responseInfo->info.options[i].optionData);
1087 printf("############################################################\n");
1090 //Check if this has secure communication information
1091 if (responseInfo->info.payload)
1093 int securePort = GetSecureInformation(responseInfo->info.payload);
1094 if (0 < securePort) //Set the remote endpoint secure details and send response
1096 printf("This is secure resource...\n");
1101 void SendResponse(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1103 printf("entering send_response\n");
1107 printf("endpoint is null\n");
1111 CAInfo_t responseData = {CA_MSG_RESET};
1114 ((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) :
1116 responseData.messageId = 0;
1117 responseData.token = "";
1120 responseData.messageId = info->messageId;
1121 responseData.token = info->token;
1122 responseData.tokenLength = info->tokenLength;
1124 responseData.payload = "response payload";
1126 CAResponseInfo_t responseInfo = {0};
1127 responseInfo.result = 203;
1128 responseInfo.info = responseData;
1130 if (true == endpoint->isSecured)
1132 printf("Sending response on secure communication\n");
1136 printf("Sending response on non-secure communication\n");
1139 // send request (connectivityType from remoteEndpoint of request Info)
1140 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1141 if (res != CA_STATUS_OK)
1143 printf("send response failed\n");
1147 printf("send response success\n");
1150 printf("=============================================\n");
1153 void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token, uint8_t tokenLength)
1155 printf("\n=============================================\n");
1159 printf("endpoint is null\n");
1163 CAInfo_t requestData ={CA_MSG_RESET};
1164 requestData.token = token;
1165 requestData.tokenLength = tokenLength;
1166 requestData.payload = "Temp Json Payload";
1168 CARequestInfo_t requestInfo ={CA_GET, {CA_MSG_RESET}};
1169 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1170 requestInfo.method = CA_GET;
1171 requestInfo.info = requestData;
1174 endpoint->connectivityType = CA_WIFI;
1176 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1177 if (res != CA_STATUS_OK)
1179 printf("send request failed\n");
1183 printf("send request success\n");
1186 printf("=============================================\n");
1190 int GetSecureInformation(CAPayload_t payLoad)
1192 printf("entering get_secure_information\n");
1196 printf("Payload is NULL\n");
1200 char *subString = NULL;
1201 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1203 printf("This is not secure resource\n");
1207 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1209 printf("This secure resource does not have port information\n");
1213 char *startPos = strstr(subString, ":");
1216 printf("Parsing failed !\n");
1220 char *endPos = strstr(startPos, "}");
1223 printf("Parsing failed !\n");
1227 if(((endPos - 1) - startPos) > 4)
1229 printf("port length is not proper.Exceeding length 4\n");
1233 char portStr[4] = {0};
1234 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1236 printf("secured port is: %s\n", portStr);
1237 return atoi(portStr);
1240 void GetResourceUri(char *URI, char *resourceURI, int length)
1242 char *startPos = URI;
1244 if (NULL != (temp = strstr(URI, "://")))
1246 startPos = strchr(temp + 3, '/');
1249 printf("Resource URI is missing\n");
1254 char *endPos = strchr(startPos, '?');
1257 endPos = URI + strlen(URI);
1261 if (endPos - startPos <= length)
1262 memcpy(resourceURI, startPos + 1, endPos - startPos);
1264 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1267 CAResult_t get_network_type()
1269 printf("\n=============================================\n");
1270 printf("\tselect network type\n");
1271 printf("IPv4 : 0\n");
1274 printf("select : ");
1276 if(!fgets(buf, MAX_BUF_LEN, stdin))
1278 printf("fgets error\n");
1279 return CA_STATUS_FAILED;
1282 int number = buf[0] - '0';
1284 number = (number < 0 || number > 3) ? 0 : 1 << number;
1286 if (!(number & 0xf))
1288 return CA_NOT_SUPPORTED;
1290 if (number & CA_IPV4)
1292 g_selected_nw_type = CA_IPV4;
1293 return CA_STATUS_OK;
1296 if (number & CA_EDR)
1298 g_selected_nw_type = CA_EDR;
1299 return CA_STATUS_OK;
1303 g_selected_nw_type = CA_LE;
1304 return CA_STATUS_OK;
1307 printf("\n=============================================\n");
1309 return CA_STATUS_FAILED;