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 OCDtlsPskCredsBlob *pskCredsBlob = NULL;
92 void ClearDtlsCredentialInfo()
94 printf("clearDtlsCredentialInfo IN\n");
97 // Initialize sensitive data to zeroes before freeing.
98 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
99 free(pskCredsBlob->creds);
101 memset(pskCredsBlob, 0, sizeof(OCDtlsPskCredsBlob));
105 printf("clearDtlsCredentialInfo OUT\n");
108 // Internal API. Invoked by OC stack to retrieve credentials from this module
109 void CAGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
111 printf("CAGetDtlsPskCredentials IN\n");
115 printf("Memory is not allocated for credInfo\n");
119 *credInfo = pskCredsBlob;
121 printf("CAGetDtlsPskCredentials OUT\n");
124 bool SetCredentials()
126 printf("SetCredentials IN\n");
127 pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
131 printf("Memory allocation is failed\n");
135 memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
136 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
138 pskCredsBlob->num = 1;
140 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
142 if(!pskCredsBlob->creds)
144 printf("Memory allocation is failed\n");
149 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
150 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
152 printf("SetCredentials OUT\n");
159 g_main_loop_run(mainloop);
166 printf("\n=============================================\n");
167 printf("\t\tsample main\n");
168 printf("=============================================\n");
170 CAResult_t res = CA_STATUS_FAILED;
172 * Read DTLS PSK credentials from persistent storage and
173 * set in the OC stack.
176 if (SetCredentials() == 0)
178 printf("SetCredentials failed\n");
182 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
183 if(CA_STATUS_OK != res)
185 printf("Set credential handler fail\n");
190 if (CA_STATUS_OK != res)
192 printf("CAInitialize fail\n");
197 CARegisterHandler(RequestHandler, ResponseHandler);
201 if (g_lastRequestToken != NULL)
203 CADestroyToken(g_lastRequestToken);
205 g_lastRequestToken = NULL;
209 ClearDtlsCredentialInfo();
218 char menu = toupper(PrintOptions());
226 case 'Q': // quits the sample program
227 printf("quit..!!\n");
228 g_main_loop_quit(mainloop);
231 case 's': // start server
232 case 'S': // start server
233 StartListeningServer();
236 case 't': // send request
241 case 'c': // start client
243 StartDiscoveryServer();
246 case 'f': // find resource
251 case 'r': // send request
256 case 'a': // advertise resource
261 case 'b': // send notification
266 case 'n': // select network
271 case 'x': // unselect network
276 case 'h': // handle request response
278 HandleRequestResponse();
287 while (g_received == 0)
290 HandleRequestResponse();
297 StartListeningServer();
301 HandleRequestResponse();
305 case 'g': // get network information
311 printf("not supported menu!!\n");
318 CAResult_t Initialize()
320 CAResult_t res = CA_STATUS_FAILED;
321 mainloop = g_main_loop_new(NULL, FALSE);
324 printf("g_main_loop_new failed\n");
328 int result = pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
331 printf("pthread_create failed in initialize\n");
335 res = CAInitialize();
336 if (res != CA_STATUS_OK)
338 printf("CAInitialize fail\n");
343 void StartListeningServer()
345 printf("start listening server!!\n");
347 CAResult_t res = CAStartListeningServer();
348 if (res != CA_STATUS_OK)
350 printf("start listening server fail\n");
354 printf("start listening server success\n");
358 void StartDiscoveryServer()
360 printf("start discovery client!!\n");
362 CAResult_t res = CAStartDiscoveryServer();
363 if (res != CA_STATUS_OK)
365 printf("start discovery client fail\n");
369 printf("start discovery client success\n");
373 void FindFixedResource()
375 char buf[] ="/a/light";
378 CAToken_t token = NULL;
379 CAResult_t res = CAGenerateToken(&token);
380 if (res != CA_STATUS_OK)
382 printf("token generate error!!");
386 printf("generated token %s\n", (token != NULL) ? token : "");
388 res = CAFindResource(buf, token);
389 if (res != CA_STATUS_OK)
391 printf("find resource error!!\n");
395 printf("find resource to %s URI\n", buf);
401 CADestroyToken(token);
404 printf("=============================================\n");
409 char buf[MAX_BUF_LEN] = {0};
411 printf("\n=============================================\n");
412 printf("ex) /a/light\n");
413 printf("reference uri : ");
415 if(!fgets(buf, MAX_BUF_LEN, stdin))
417 printf("fgets error\n");
422 CAToken_t token = NULL;
423 CAResult_t res = CAGenerateToken(&token);
424 if (res != CA_STATUS_OK)
426 printf("token generate error!!\n");
430 printf("generated token %s\n", (token != NULL) ? token : "");
432 res = CAFindResource(buf, token);
433 if (res != CA_STATUS_OK)
435 printf("find resource error!!\n");
439 printf("find resource to %s URI\n", buf);
441 if (g_lastRequestToken)
443 CADestroyToken(g_lastRequestToken);
446 g_lastRequestToken = token;
449 printf("=============================================\n");
454 char secureRequest[2] = {0};
455 CAResult_t res = CA_STATUS_FAILED;
456 char uri[MAX_BUF_LEN] = {'\0'};
458 res = GetNetworkType();
459 if (res != CA_STATUS_OK)
464 printf("Do you want to send secure request ?.... enter (0/1): \n");
465 if(!fgets(secureRequest, 1, stdin))
467 printf("fgets error\n");
471 if ('1' == secureRequest[0])
473 printf("Enter the URI like below....\n");
474 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
479 printf("Enter the URI like below....\n");
480 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
481 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
485 if(!fgets(uri, MAX_BUF_LEN, stdin))
487 printf("fgets error\n");
491 // create remote endpoint
492 CARemoteEndpoint_t *endpoint = NULL;
493 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, g_selectedNwType, &endpoint)
496 printf("Failed to create remote endpoint!\n");
497 CADestroyRemoteEndpoint(endpoint);
501 char buf[MAX_BUF_LEN]={0};
503 printf("\n=============================================\n");
504 printf("0:CON, 1:NON\n");
505 printf("select message type : ");
507 if(!fgets(buf, MAX_BUF_LEN, stdin))
509 printf("fgets error\n");
510 CADestroyRemoteEndpoint(endpoint);
514 CAMessageType_t msgType = (buf[0] == '0' || buf[0] == '1') ? buf[0] - '0' : 0;
517 CAToken_t token = NULL;
518 if (CA_STATUS_OK != CAGenerateToken(&token))
520 printf("token generate error!!\n");
524 printf("generated token %s\n", (token != NULL) ? token : "");
526 // extract relative resourceuri from give uri
527 char resourceURI[15] = {0};
529 printf("URI : %s\n", uri);
530 GetResourceUri(uri, resourceURI, 14);
532 // create request data
533 CAInfo_t requestData = {CA_MSG_RESET};
534 requestData.token = token;
535 if ('1' == secureRequest[0])
537 int length = strlen(g_secureInfoData) + strlen(resourceURI) + 1;
538 requestData.payload = (CAPayload_t) malloc(length);
539 if(!requestData.payload)
541 printf("Memory allocation is failed\n");
542 CADestroyToken(token);
543 CADestroyRemoteEndpoint(endpoint);
546 sprintf(requestData.payload, g_secureInfoData, resourceURI, g_localSecurePort);
550 int length = strlen(g_normalInfoData) + strlen(resourceURI) + 1;
551 requestData.payload = (CAPayload_t) malloc(length);
552 if(!requestData.payload)
554 printf("Memory allocation is failed\n");
555 CADestroyToken(token);
556 CADestroyRemoteEndpoint(endpoint);
559 sprintf(requestData.payload, g_normalInfoData, resourceURI);
561 requestData.type = msgType;
563 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
564 requestInfo.method = CA_GET;
565 requestInfo.info = requestData;
568 CASendRequest(endpoint, &requestInfo);
572 CADestroyToken(token);
575 if (requestData.payload)
577 free(requestData.payload);
580 // destroy remote endpoint
581 CADestroyRemoteEndpoint(endpoint);
582 printf("=============================================\n");
585 void SendRequestAll()
587 char buf[MAX_BUF_LEN] = {0};
589 CAResult_t res = CA_STATUS_FAILED;
591 res = GetNetworkType();
592 if (res != CA_STATUS_OK)
597 printf("\n=============================================\n");
598 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
599 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
602 if(!fgets(buf, MAX_BUF_LEN, stdin))
604 printf("fgets error\n");
608 // create remote endpoint
609 CARemoteEndpoint_t *endpoint = NULL;
610 res = CACreateRemoteEndpoint(buf, g_selectedNwType, &endpoint);
612 if (res != CA_STATUS_OK)
614 printf("create remote endpoint error!!\n");
615 CADestroyRemoteEndpoint(endpoint);
619 CAGroupEndpoint_t *group = NULL;
620 group = (CAGroupEndpoint_t *)malloc(sizeof(CAGroupEndpoint_t));
623 printf("Memory allocation failed!\n");
624 CADestroyRemoteEndpoint(endpoint);
627 group->connectivityType = endpoint->connectivityType;
628 group->resourceUri = endpoint->resourceUri;
631 CAToken_t token = NULL;
632 res = CAGenerateToken(&token);
634 if (res != CA_STATUS_OK)
636 printf("token generate error!!\n");
640 printf("generated token %s\n", (token != NULL) ? token : "");
642 CAInfo_t requestData = {CA_MSG_RESET};
643 requestData.token = token;
644 requestData.payload = "Temp Json Payload";
645 requestData.type = CA_MSG_NONCONFIRM;
647 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
648 requestInfo.method = CA_GET;
649 requestInfo.info = requestData;
652 // CASendRequest(endpoint, &requestInfo);
653 CASendRequestToAll(group, &requestInfo);
657 CADestroyToken(token);
660 // destroy remote endpoint
661 CADestroyRemoteEndpoint(endpoint);
665 printf("=============================================\n");
668 void AdvertiseResource()
670 char buf[MAX_BUF_LEN] = {0};
672 printf("\n=============================================\n");
675 if(!fgets(buf, MAX_BUF_LEN, stdin))
677 printf("fgets error\n");
682 char optionData[MAX_OPT_LEN] = {0};
684 printf("Option Num : ");
685 scanf("%d", &optionNum);
686 CAHeaderOption_t *headerOpt = NULL;
687 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
688 if (NULL == headerOpt)
690 printf("Memory allocation failed!\n");
693 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
696 for (i = 0 ; i < optionNum ; i++)
699 printf("[%d] Option ID : ", i + 1);
700 scanf("%d", &optionID);
701 headerOpt[i].optionID = optionID;
703 printf("[%d] Option Data : ", i + 1);
704 scanf("%s", optionData);
705 int len = strlen(optionData);
706 memcpy(headerOpt[i].optionData, optionData, len + 1);
707 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
709 headerOpt[i].optionLength = (uint16_t)len;
711 printf("\n=============================================\n");
714 CAToken_t token = NULL;
715 CAResult_t res = CAGenerateToken(&token);
716 if (res != CA_STATUS_OK)
718 printf("token generate error!!\n");
722 printf("generated token %s\n", (token != NULL) ? token : "");
724 CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
729 CADestroyToken(token);
736 void SendNotification()
738 char buf[MAX_BUF_LEN] = {0};
740 CAResult_t res = CA_STATUS_FAILED;
742 res = GetNetworkType();
743 if (res != CA_STATUS_OK)
748 printf("\n=============================================\n");
749 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
750 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
753 if(!fgets(buf, MAX_BUF_LEN, stdin))
755 printf("fgets error\n");
759 // create remote endpoint
760 CARemoteEndpoint_t *endpoint = NULL;
761 res = CACreateRemoteEndpoint(buf, g_selectedNwType, &endpoint);
762 if (res != CA_STATUS_OK)
764 printf("create remote endpoint error!!\n");
765 CADestroyRemoteEndpoint(endpoint);
769 CAInfo_t respondeData = {CA_MSG_RESET};
770 respondeData.token = "client token";
771 respondeData.payload = "Temp Notification Data";
773 CAResponseInfo_t responseInfo = {0};
774 responseInfo.result = CA_SUCCESS;
775 responseInfo.info = respondeData;
778 res = CASendNotification(endpoint, &responseInfo);
779 if (res != CA_STATUS_OK)
781 printf("send notification error\n");
785 printf("send notification success\n");
788 // destroy remote endpoint
789 if (endpoint != NULL)
791 CADestroyRemoteEndpoint(endpoint);
794 printf("\n=============================================\n");
798 char buf[MAX_BUF_LEN] = {0};
800 printf("\n=============================================\n");
801 printf("\tselect network\n");
802 printf("ETHERNET : 0\n");
803 printf("WIFI : 1\n");
808 if(!fgets(buf, MAX_BUF_LEN, stdin))
810 printf("fgets error\n");
814 int number = buf[0] - '0';
816 number = (number < 0 || number > 3) ? 0 : number;
818 CAResult_t res = CASelectNetwork(1 << number);
819 if (res != CA_STATUS_OK)
821 printf("select network error\n");
822 g_selectedNwType = 1 << number;
826 printf("select network success\n");
829 printf("=============================================\n");
832 void UnselectNetwork()
834 char buf[MAX_BUF_LEN] = {0};
836 printf("\n=============================================\n");
837 printf("\tunselect enabled network\n");
838 printf("ETHERNET : 0\n");
839 printf("WIFI : 1\n");
844 if(!fgets(buf, MAX_BUF_LEN, stdin))
846 printf("fgets error\n");
850 int number = buf[0] - '0';
852 number = (number < 0 || number > 3) ? 1 : number;
854 CAResult_t res = CAUnSelectNetwork(1 << number);
855 if (res != CA_STATUS_OK)
857 printf("unselect network error\n");
861 printf("unselect network success\n");
863 printf("=============================================\n");
868 char buf[MAX_BUF_LEN] = {0};
870 printf("\n=============================================\n");
871 printf("\t\tMenu\n");
872 printf("\tm : show menu\n");
873 printf("\ts : start server\n");
874 printf("\tc : start client\n");
875 printf("\tf : find resource\n");
876 printf("\tr : send request\n");
877 printf("\tt : send request to all\n");
878 printf("\ta : advertise resource\n");
879 printf("\tb : send notification\n");
880 printf("\tn : select network\n");
881 printf("\tx : unselect network\n");
882 printf("\tg : get network information\n");
883 printf("\th : handle request response\n");
884 printf("\ty : run static client\n");
885 printf("\tz : run static server\n");
886 printf("\tq : quit\n");
887 printf("=============================================\n");
890 if(!fgets(buf, MAX_BUF_LEN, stdin))
892 printf("fgets error\n");
899 void HandleRequestResponse()
901 printf("handle_request_response\n");
903 CAResult_t res = CAHandleRequestResponse();
904 if (res != CA_STATUS_OK)
906 printf("handle request error\n");
910 printf("handle request success\n");
914 void GetNetworkInfo()
917 CALocalConnectivity_t *tempInfo = NULL;
918 uint32_t tempSize = 0;
920 CAGetNetworkInformation(&tempInfo, &tempSize);
921 if (tempInfo == NULL || tempSize <= 0)
923 printf("network not connected\n");
927 printf("################## Network Information #######################\n");
928 printf("network info total size is %d\n\n", tempSize);
930 for (index = 0; index < tempSize; index++)
932 printf("Type: %d\n", tempInfo[index].type);
933 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
934 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
935 printf("Secured: %d\n\n", tempInfo[index].isSecured);
937 if (true == tempInfo[index].isSecured)
939 g_localSecurePort = tempInfo[index].addressInfo.IP.port;
943 g_localUnicastPort = tempInfo[index].addressInfo.IP.port;
950 printf("##############################################################");
953 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
955 printf("##########received request from remote device #############\n");
958 printf("Remote endpoint is NULL!\n");
964 printf("Request info is NULL!\n");
968 printf("Uri: %s\n", object->resourceUri);
969 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
970 object->addressInfo.IP.port, object->isSecured);
972 printf("Data: %s\n", requestInfo->info.payload);
973 printf("Message type: %s\n", g_messageType[requestInfo->info.type]);
974 if (g_lastRequestToken != NULL && requestInfo->info.token != NULL
975 && (strcmp((char *)g_lastRequestToken, requestInfo->info.token) == 0))
977 printf("token is same. received request of it's own. skip.. \n");
981 if (requestInfo->info.options)
983 uint32_t len = requestInfo->info.numOptions;
985 for (i = 0; i < len; i++)
987 printf("Option %d\n", i + 1);
988 printf("ID : %d\n", requestInfo->info.options[i].optionID);
989 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
990 requestInfo->info.options[i].optionData);
993 printf("############################################################\n");
995 //Check if this has secure communication information
996 if (requestInfo->info.payload)
998 int securePort = GetSecureInformation(requestInfo->info.payload);
999 if (0 < securePort) //Set the remote endpoint secure details and send response
1001 printf("This is secure resource...\n");
1005 length = 8; //length of "coaps://"
1006 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
1007 length += strlen(object->resourceUri) + 1;
1009 uri = calloc(1, sizeof(char) * length);
1012 printf("Failed to create new uri\n");
1015 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
1016 securePort, object->resourceUri);
1018 CARemoteEndpoint_t *endpoint = NULL;
1019 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
1021 printf("Failed to create duplicate of remote endpoint!\n");
1024 //endpoint->connectivityType = object->connectivityType;
1025 endpoint->isSecured = true;
1032 printf("send response with URI\n");
1033 SendResponse(object, &requestInfo->info);
1038 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1040 printf("##########Received response from remote device #############\n");
1043 printf("response_handler object is null\n");
1049 printf("response_handler responseInfo is null\n");
1053 printf("Uri: %s\n", object->resourceUri);
1054 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1055 object->addressInfo.IP.port, object->isSecured);
1056 printf("response result : %d\n", responseInfo->result);
1057 printf("Data: %s\n", responseInfo->info.payload);
1058 printf("Message type: %s\n", g_messageType[responseInfo->info.type]);
1059 if (responseInfo->info.options)
1061 uint32_t len = responseInfo->info.numOptions;
1063 for (i = 0; i < len; i++)
1065 printf("Option %d\n", i + 1);
1066 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1067 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1068 responseInfo->info.options[i].optionData);
1071 printf("############################################################\n");
1074 //Check if this has secure communication information
1075 if (responseInfo->info.payload)
1077 int securePort = GetSecureInformation(responseInfo->info.payload);
1078 if (0 < securePort) //Set the remote endpoint secure details and send response
1080 printf("This is secure resource...\n");
1085 void SendResponse(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1087 printf("entering send_response\n");
1091 printf("endpoint is null\n");
1095 CAInfo_t responseData = {CA_MSG_RESET};
1098 ((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) :
1100 responseData.messageId = 0;
1101 responseData.token = "";
1104 responseData.messageId = info->messageId;
1105 responseData.token = info->token;
1107 responseData.payload = "response payload";
1109 CAResponseInfo_t responseInfo = {0};
1110 responseInfo.result = 203;
1111 responseInfo.info = responseData;
1113 if (true == endpoint->isSecured)
1115 printf("Sending response on secure communication\n");
1119 printf("Sending response on non-secure communication\n");
1122 // send request (connectivityType from remoteEndpoint of request Info)
1123 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1124 if (res != CA_STATUS_OK)
1126 printf("send response failed\n");
1130 printf("send response success\n");
1133 printf("=============================================\n");
1136 void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token)
1138 printf("\n=============================================\n");
1142 printf("endpoint is null\n");
1146 CAInfo_t requestData ={CA_MSG_RESET};
1147 requestData.token = token;
1148 requestData.payload = "Temp Json Payload";
1150 CARequestInfo_t requestInfo ={CA_GET, {CA_MSG_RESET}};
1151 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1152 requestInfo.method = CA_GET;
1153 requestInfo.info = requestData;
1156 endpoint->connectivityType = CA_WIFI;
1158 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1159 if (res != CA_STATUS_OK)
1161 printf("send request failed\n");
1165 printf("send request success\n");
1168 printf("=============================================\n");
1172 int GetSecureInformation(CAPayload_t payLoad)
1174 printf("entering get_secure_information\n");
1178 printf("Payload is NULL\n");
1182 char *subString = NULL;
1183 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1185 printf("This is not secure resource\n");
1189 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1191 printf("This secure resource does not have port information\n");
1195 char *startPos = strstr(subString, ":");
1198 printf("Parsing failed !\n");
1202 char *endPos = strstr(startPos, "}");
1205 printf("Parsing failed !\n");
1209 if(((endPos - 1) - startPos) > 4)
1211 printf("port length is not proper.Exceeding length 4\n");
1215 char portStr[4] = {0};
1216 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1218 printf("secured port is: %s\n", portStr);
1219 return atoi(portStr);
1222 void GetResourceUri(char *URI, char *resourceURI, int length)
1224 char *startPos = URI;
1226 if (NULL != (temp = strstr(URI, "://")))
1228 startPos = strchr(temp + 3, '/');
1231 printf("Resource URI is missing\n");
1236 char *endPos = strchr(startPos, '?');
1239 endPos = URI + strlen(URI);
1243 if (endPos - startPos <= length)
1244 memcpy(resourceURI, startPos + 1, endPos - startPos);
1246 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1249 CAResult_t GetNetworkType()
1251 char buf[MAX_BUF_LEN] = {0};
1253 printf("\n=============================================\n");
1254 printf("\tselect network type\n");
1255 printf("ETHERNET : 0\n");
1256 printf("WIFI : 1\n");
1257 printf("EDR : 2\n");
1259 printf("select : ");
1261 if(!fgets(buf, MAX_BUF_LEN, stdin))
1263 printf("fgets error\n");
1264 return CA_STATUS_FAILED;
1267 int number = buf[0] - '0';
1269 number = (number < 0 || number > 3) ? 0 : 1 << number;
1271 if (!(number & 0xf))
1273 return CA_NOT_SUPPORTED;
1276 if (number & CA_ETHERNET)
1278 g_selectedNwType = CA_ETHERNET;
1279 return CA_STATUS_OK;
1282 if (number & CA_WIFI)
1284 g_selectedNwType = CA_WIFI;
1285 return CA_STATUS_OK;
1288 if (number & CA_EDR)
1290 g_selectedNwType = CA_EDR;
1291 return CA_STATUS_OK;
1296 g_selectedNwType = CA_LE;
1297 return CA_STATUS_OK;
1300 printf("\n=============================================\n");
1302 return CA_STATUS_FAILED;