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 ******************************************************************/
27 #include "cainterface.h"
29 #include "ocsecurityconfig.h"
32 #define MAX_BUF_LEN 1024
33 #define MAX_OPT_LEN 16
41 #define IDENTITY ("1111111111111111")
45 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
49 CABool_t gLocalUnicastPort;
50 CABool_t gLocalSecurePort;
51 CAConnectivityType_t gSelectedNwType = CA_ETHERNET;
52 const char* gMessageType[] = {"CON", "NON", "ACK", "RESET"};
56 CAResult_t get_network_type();
58 void start_listening_server();
59 void start_discovery_server();
62 void send_request_all();
63 void advertise_resource();
64 void send_notification();
65 void select_network();
66 void unselect_network();
67 void handle_request_response();
68 void find_fixed_resource();
69 void get_network_info();
70 void send_secure_request();
72 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
73 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
74 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
75 void send_response(CARemoteEndpoint_t *endpoint, CAInfo_t *info);
76 void get_resource_uri(char *URI, char *resourceURI, int length);
77 int get_secure_information(CAPayload_t payLoad);
79 static CAToken_t gLastRequestToken = NULL;
80 static const char *gSecureInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
81 "\"if\":[\"oc.mi.def\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
82 static const char *gNormalInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
83 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
86 static OCDtlsPskCredsBlob *pskCredsBlob = NULL;
88 void clearDtlsCredentialInfo()
90 printf("clearDtlsCredentialInfo IN\n");
93 // Zero out sensitive data before freeing.
94 if (pskCredsBlob->num)
96 memset(pskCredsBlob, 0,
97 sizeof(OCDtlsPskCredsBlob) + (sizeof(OCDtlsPskCreds)*(pskCredsBlob->num - 1)));
102 printf("clearDtlsCredentialInfo OUT\n");
105 // Internal API. Invoked by OC stack to retrieve credentials from this module
106 void OCGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
108 printf("OCGetDtlsPskCredentials IN\n");
110 *credInfo = pskCredsBlob;
112 printf("OCGetDtlsPskCredentials OUT\n");
114 // Internal API. Invoked by OC stack to retrieve credentials from this module
115 void CAGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
117 printf("CAGetDtlsPskCredentials IN\n");
119 *credInfo = pskCredsBlob;
121 printf("CAGetDtlsPskCredentials OUT\n");
123 int32_t SetCredentials()
125 printf("SetCredentials IN\n");
126 pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
128 memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
129 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
131 pskCredsBlob->num = 1;
133 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
135 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
136 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
138 printf("SetCredentials OUT\n");
147 printf("=============================================\n");
148 printf("\t\tsample main\n");
149 printf("=============================================\n");
153 * Read DTLS PSK credentials from persistent storage and
154 * set in the OC stack.
157 if (SetCredentials() == 0)
159 printf("SetCredentials failed\n");
163 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
164 if(res != CA_STATUS_OK)
166 printf("Set credential handler fail\n");
170 res = CAInitialize();
171 if (res != CA_STATUS_OK)
173 printf("CAInitialize fail\n");
180 printf("select default network(WIFI)\n");
181 res = CASelectNetwork(CA_WIFI);
182 if (res != CA_STATUS_OK)
184 printf("CASelectNetwork fail\n");
190 CARegisterHandler(request_handler, response_handler);
194 if (gLastRequestToken != NULL)
196 CADestroyToken(gLastRequestToken);
198 gLastRequestToken = NULL;
202 clearDtlsCredentialInfo();
211 char menu = get_menu();
221 printf("quit..!!\n");
224 case 's': // start server
226 start_listening_server();
229 case 't': // send request
234 case 'c': // start client
236 start_discovery_server();
239 case 'f': // find resource
244 case 'r': // send request
249 case 'a': // advertise resource
251 advertise_resource();
254 case 'b': // send notification
259 case 'n': // select network
264 case 'x': // unselect network
269 case 'h': // handle request response
271 handle_request_response();
278 find_fixed_resource();
279 while (gReceived == 0)
282 handle_request_response();
291 start_discovery_server();
292 send_secure_request();
293 while (gReceived == 0)
296 handle_request_response();
303 start_listening_server();
307 handle_request_response();
311 case 'g': // get network information
317 printf("not supported menu!!\n");
326 void start_listening_server()
328 printf("start listening server!!\n");
330 CAResult_t res = CAStartListeningServer();
331 if (res != CA_STATUS_OK)
333 printf("start listening server fail\n");
337 printf("start listening server success\n");
341 void start_discovery_server()
343 printf("start discovery client!!\n");
345 CAResult_t res = CAStartDiscoveryServer();
346 if (res != CA_STATUS_OK)
348 printf("start discovery client fail\n");
352 printf("start discovery client success\n");
356 void find_fixed_resource()
358 char buf[MAX_BUF_LEN] =
360 strcpy(buf, "/a/light");
363 CAToken_t token = NULL;
364 CAResult_t res = CAGenerateToken(&token);
365 if (res != CA_STATUS_OK)
367 printf("token generate error!!");
371 printf("generated token %s\n", (token != NULL) ? token : "");
373 res = CAFindResource(buf, token);
374 if (res != CA_STATUS_OK)
376 printf("find resource error!!\n");
380 printf("find resource to %s URI\n", buf);
387 CADestroyToken(token);
391 printf("=============================================\n");
396 char buf[MAX_BUF_LEN];
398 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
400 printf("\n=============================================\n");
401 printf("ex) /a/light\n");
402 printf("reference uri : ");
404 if(gets(buf) == NULL)
408 CAToken_t token = NULL;
409 CAResult_t res = CAGenerateToken(&token);
410 if (res != CA_STATUS_OK)
412 printf("token generate error!!\n");
416 printf("generated token %s\n", (token != NULL) ? token : "");
418 res = CAFindResource(buf, token);
419 if (res != CA_STATUS_OK)
421 printf("find resource error!!\n");
425 printf("find resource to %s URI\n", buf);
427 if (gLastRequestToken != NULL)
429 CADestroyToken(gLastRequestToken);
432 gLastRequestToken = token;
439 CADestroyToken(token);
443 printf("=============================================\n");
448 char secureRequest[2] = {0};
451 res = get_network_type();
452 if (res != CA_STATUS_OK)
457 printf("Do you want to send secure request ?.... enter (0/1): ");
458 if( gets(secureRequest) == NULL)
460 if ('1' == secureRequest[0])
462 printf("Enter the URI like below....\n");
463 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
467 printf("Enter the URI like below....\n");
468 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
469 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
472 char uri[MAX_BUF_LEN] = {'\0'};
473 if( gets(uri) == NULL)
476 // create remote endpoint
477 CARemoteEndpoint_t *endpoint = NULL;
478 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, gSelectedNwType, &endpoint)
481 printf("Failed to create remote endpoint!\n");
482 CADestroyRemoteEndpoint(endpoint);
486 //endpoint->connectivityType = gSelectedNwType;
488 char buf[MAX_BUF_LEN];
489 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
491 printf("\n=============================================\n");
492 printf("0:CON, 1:NON\n");
493 printf("select message type : ");
495 if( gets(buf) == NULL)
498 CAMessageType_t msgType = (buf[0] == '0' || buf[0] == '1') ? buf[0] - '0' : 0;
501 CAToken_t token = NULL;
502 if (CA_STATUS_OK != CAGenerateToken(&token))
504 printf("token generate error!!\n");
508 printf("generated token %s\n", (token != NULL) ? token : "");
510 // extract relative resourceuri from give uri
511 char resourceURI[15] = {0};
513 printf("URI : %s\n", uri);
514 get_resource_uri(uri, resourceURI, 14);
516 // create request data
517 CAInfo_t requestData;
518 memset(&requestData, 0, sizeof(CAInfo_t));
519 requestData.token = token;
520 if ('1' == secureRequest[0])
522 int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
523 requestData.payload = (CAPayload_t) malloc(length);
524 sprintf(requestData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
528 int length = strlen(gNormalInfoData) + strlen(resourceURI) + 1;
529 requestData.payload = (CAPayload_t) malloc(length);
530 sprintf(requestData.payload, gNormalInfoData, resourceURI);
532 requestData.type = msgType;
534 CARequestInfo_t requestInfo;
535 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
536 requestInfo.method = CA_GET;
537 requestInfo.info = requestData;
540 CASendRequest(endpoint, &requestInfo);
544 CADestroyToken(token);
546 if (requestData.payload != NULL)
548 free(requestData.payload);
550 // destroy remote endpoint
551 CADestroyRemoteEndpoint(endpoint);
552 printf("=============================================\n");
555 void send_secure_request()
557 char uri[MAX_BUF_LEN];
558 char ipv4addr[CA_IPADDR_SIZE];
560 printf("\n=============================================\n");
561 printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");
563 fgets(ipv4addr, CA_IPADDR_SIZE, stdin);
564 snprintf(uri, MAX_BUF_LEN, "coaps://%s:5684/a/light", ipv4addr);
566 // create remote endpoint
567 CARemoteEndpoint_t *endpoint = NULL;
568 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, CA_ETHERNET, &endpoint))
570 printf("Failed to create remote endpoint!\n");
575 CAToken_t token = NULL;
576 if (CA_STATUS_OK != CAGenerateToken(&token))
578 printf("Failed to generate token !\n");
582 // create request data
583 CAMessageType_t msgType = CA_MSG_NONCONFIRM;
584 CAInfo_t requestData;
585 memset(&requestData, 0, sizeof(CAInfo_t));
586 requestData.token = token;
587 requestData.type = msgType;
589 CARequestInfo_t requestInfo;
590 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
591 requestInfo.method = CA_GET;
592 requestInfo.info = requestData;
595 CASendRequest(endpoint, &requestInfo);
599 CADestroyToken(token);
600 CADestroyRemoteEndpoint(endpoint);
601 printf("=============================================\n");
605 void send_request_all()
607 char buf[MAX_BUF_LEN];
608 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
612 res = get_network_type();
613 if (res != CA_STATUS_OK)
618 printf("\n=============================================\n");
619 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
620 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
623 if( gets(buf) == NULL)
626 // create remote endpoint
627 CARemoteEndpoint_t *endpoint = NULL;
628 res = CACreateRemoteEndpoint(buf, gSelectedNwType, &endpoint);
630 if (res != CA_STATUS_OK)
632 printf("create remote endpoint error!!\n");
633 CADestroyRemoteEndpoint(endpoint);
638 CAGroupEndpoint_t *group = NULL;
639 group = (CAGroupEndpoint_t *)malloc(sizeof(CAGroupEndpoint_t));
640 group->connectivityType = endpoint->connectivityType;
641 group->resourceUri = endpoint->resourceUri;
645 CAToken_t token = NULL;
646 res = CAGenerateToken(&token);
648 if (res != CA_STATUS_OK)
650 printf("token generate error!!\n");
654 printf("generated token %s\n", (token != NULL) ? token : "");
656 CAInfo_t requestData;
657 memset(&requestData, 0, sizeof(CAInfo_t));
658 requestData.token = token;
659 requestData.payload = "Temp Json Payload";
660 requestData.type = CA_MSG_NONCONFIRM;
662 CARequestInfo_t requestInfo;
663 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
664 requestInfo.method = CA_GET;
665 requestInfo.info = requestData;
668 // CASendRequest(endpoint, &requestInfo);
669 CASendRequestToAll(group, &requestInfo);
673 CADestroyToken(token);
676 // destroy remote endpoint
677 if (endpoint != NULL)
679 CADestroyRemoteEndpoint(endpoint);
684 printf("=============================================\n");
687 void advertise_resource()
689 char buf[MAX_BUF_LEN];
691 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
693 printf("\n=============================================\n");
699 char optionData[MAX_OPT_LEN];
701 printf("Option Num : ");
702 scanf("%d", &optionNum);
703 CAHeaderOption_t *headerOpt;
704 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
705 if (NULL == headerOpt)
707 printf("Memory allocation failed!\n");
710 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
713 for (i = 0; i < optionNum; i++)
716 printf("[%d] Option ID : ", i + 1);
717 scanf("%d", &optionID);
718 headerOpt[i].optionID = optionID;
720 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
721 printf("[%d] Option Data : ", i + 1);
722 scanf("%s", optionData);
723 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
724 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
726 headerOpt[i].optionLength = (uint16_t) strlen(optionData);
728 printf("\n=============================================\n");
731 CAToken_t token = NULL;
732 CAResult_t res = CAGenerateToken(&token);
733 if (res != CA_STATUS_OK)
735 printf("token generate error!!\n");
739 printf("generated token %s\n", (token != NULL) ? token : "");
741 CAAdvertiseResource(buf, token, headerOpt, (uint8_t) optionNum);
746 CADestroyToken(token);
753 void send_notification()
755 char buf[MAX_BUF_LEN];
756 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
760 res = get_network_type();
761 if (res != CA_STATUS_OK)
766 printf("\n=============================================\n");
767 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
768 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
771 if( gets(buf) == NULL)
774 // create remote endpoint
775 CARemoteEndpoint_t *endpoint = NULL;
776 res = CACreateRemoteEndpoint(buf, gSelectedNwType, &endpoint);
778 if (res != CA_STATUS_OK)
780 printf("create remote endpoint error!!\n");
781 CADestroyRemoteEndpoint(endpoint);
785 CAInfo_t respondeData;
786 memset(&respondeData, 0, sizeof(CAInfo_t));
787 respondeData.token = "client token";
788 respondeData.payload = "Temp Notification Data";
790 CAResponseInfo_t responseInfo;
791 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
792 responseInfo.result = CA_SUCCESS;
793 responseInfo.info = respondeData;
796 res = CASendNotification(endpoint, &responseInfo);
797 if (res != CA_STATUS_OK)
799 printf("send notification error\n");
803 printf("send notification success\n");
806 // destroy remote endpoint
807 if (endpoint != NULL)
809 CADestroyRemoteEndpoint(endpoint);
812 printf("\n=============================================\n");
815 void select_network()
817 char buf[MAX_BUF_LEN];
819 printf("\n=============================================\n");
820 printf("\tselect network\n");
821 printf("ETHERNET : 0\n");
822 printf("WIFI : 1\n");
827 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
828 if( gets(buf) == NULL)
831 int number = buf[0] - '0';
833 number = (number < 0 || number > 3) ? 0 : number;
835 CAResult_t res = CASelectNetwork(1 << number);
836 if (res != CA_STATUS_OK)
838 printf("select network error\n");
839 gSelectedNwType = 1 << number;
843 printf("select network success\n");
846 printf("=============================================\n");
849 void unselect_network()
851 char buf[MAX_BUF_LEN];
853 printf("\n=============================================\n");
854 printf("\tunselect enabled network\n");
855 printf("ETHERNET : 0\n");
856 printf("WIFI : 1\n");
861 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
862 if( gets(buf) == NULL)
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");
879 printf("=============================================\n");
884 char buf[MAX_BUF_LEN];
886 printf("\n=============================================\n");
887 printf("\t\tMenu\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("\tw : send secure request\n");
902 printf("\tq : quit\n");
903 printf("=============================================\n");
906 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
908 if( gets(buf) == NULL)
914 void handle_request_response()
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 get_network_info()
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 (CA_TRUE == tempInfo[index].isSecured)
955 gLocalSecurePort = tempInfo[index].addressInfo.IP.port;
959 gLocalUnicastPort = tempInfo[index].addressInfo.IP.port;
964 printf("##############################################################");
967 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
971 printf("Remote endpoint is NULL!");
977 printf("Request info is NULL!");
981 printf("##########received request from remote device #############\n");
982 printf("Uri: %s\n", object->resourceUri);
983 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
984 object->addressInfo.IP.port, object->isSecured);
986 printf("Data: %s\n", requestInfo->info.payload);
987 printf("Message type: %s\n", gMessageType[requestInfo->info.type]);
988 if (gLastRequestToken != NULL && requestInfo->info.token != NULL
989 && (strcmp((char *)gLastRequestToken, requestInfo->info.token) == 0))
991 printf("token is same. received request of it's own. skip.. \n");
995 if (requestInfo->info.options)
997 uint32_t len = requestInfo->info.numOptions;
999 for (i = 0; i < len; i++)
1001 printf("Option %d\n", i + 1);
1002 printf("ID : %d\n", requestInfo->info.options[i].optionID);
1003 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
1004 requestInfo->info.options[i].optionData);
1007 printf("############################################################\n");
1009 //Check if this has secure communication information
1010 if (requestInfo->info.payload)
1012 int securePort = get_secure_information(requestInfo->info.payload);
1013 if (0 < securePort) //Set the remote endpoint secure details and send response
1015 printf("This is secure resource...\n");
1019 length = 8; //length of "coaps://"
1020 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
1021 length += strlen(object->resourceUri) + 1;
1023 uri = calloc(1, sizeof(char) * length);
1026 printf("Failed to create new uri\n");
1029 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
1030 securePort, object->resourceUri);
1032 CARemoteEndpoint_t *endpoint = NULL;
1033 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
1035 printf("Failed to create duplicate of remote endpoint!\n");
1038 //endpoint->connectivityType = object->connectivityType;
1039 endpoint->isSecured = CA_TRUE;
1046 printf("send response with URI\n");
1047 send_response(object, &requestInfo->info);
1052 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1054 printf("##########Received response from remote device #############\n");
1055 printf("Uri: %s\n", object->resourceUri);
1056 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1057 object->addressInfo.IP.port, object->isSecured);
1058 printf("response result : %d\n", responseInfo->result);
1059 printf("Data: %s\n", responseInfo->info.payload);
1060 printf("Message type: %s\n", gMessageType[responseInfo->info.type]);
1061 printf("Token: %s\n", responseInfo->info.token);
1062 if (responseInfo->info.options)
1064 uint32_t len = responseInfo->info.numOptions;
1066 for (i = 0; i < len; i++)
1068 printf("Option %d\n", i + 1);
1069 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1070 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1071 responseInfo->info.options[i].optionData);
1074 printf("############################################################\n");
1077 //Check if this has secure communication information
1078 if (responseInfo->info.payload)
1080 int securePort = get_secure_information(responseInfo->info.payload);
1081 if (0 < securePort) //Set the remote endpoint secure details and send response
1083 printf("This is secure resource...\n");
1088 void send_response(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1090 char buf[MAX_BUF_LEN];
1092 printf("entering send_response\n");
1094 printf("\n=============================================\n");
1095 printf("\tselect message type\n");
1096 printf("CA_MSG_CONFIRM : 0\n");
1097 printf("CA_MSG_NONCONFIRM : 1\n");
1098 printf("CA_MSG_ACKNOWLEDGE : 2\n");
1099 printf("CA_MSG_RESET : 3\n");
1100 printf("select : ");
1102 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
1105 int messageType = buf[0] - '0';
1106 int responseCode = 231 ;
1107 if(messageType != 3)
1110 printf("\n=============================================\n");
1111 printf("\tselect response code\n");
1112 printf("CA_SUCCESS : 200\n");
1113 printf("CA_CREATED : 201\n");
1114 printf("CA_DELETED : 202\n");
1115 printf("CA_EMPTY : 231\n");
1116 printf("CA_BAD_REQ : 400\n");
1117 printf("CA_BAD_OPT : 402\n");
1118 printf("CA_NOT_FOUND : 404\n");
1119 printf("CA_RETRANSMIT_TIMEOUT : 531\n");
1120 printf("select : ");
1121 scanf("%d", &responseCode);
1123 CAInfo_t responseData;
1124 memset(&responseData, 0, sizeof(CAInfo_t));
1125 responseData.type = messageType;
1126 responseData.messageId = (info != NULL) ? info->messageId : 0;
1127 if(messageType != 3)
1129 responseData.token = (info != NULL) ? info->token : "";
1130 responseData.payload = "response payload";
1132 CAResponseInfo_t responseInfo;
1133 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
1134 responseInfo.result = responseCode;
1135 responseInfo.info = responseData;
1137 if (CA_TRUE == endpoint->isSecured)
1138 printf("Sending response on secure communication\n");
1140 printf("Sending response on non-secure communication\n");
1142 // send request (connectivityType from remoteEndpoint of request Info)
1143 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1144 if (res != CA_STATUS_OK)
1146 printf("send response error\n");
1150 printf("send response success\n");
1153 printf("=============================================\n");
1156 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
1159 printf("\n=============================================\n");
1161 CAInfo_t requestData;
1162 memset(&requestData, 0, sizeof(CAInfo_t));
1163 requestData.token = token;
1164 requestData.payload = "Temp Json Payload";
1166 CARequestInfo_t requestInfo;
1167 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1168 requestInfo.method = CA_GET;
1169 requestInfo.info = requestData;
1172 endpoint->connectivityType = CA_WIFI;
1174 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1175 if (res != CA_STATUS_OK)
1177 printf("send request error\n");
1181 printf("send request success\n");
1184 printf("=============================================\n");
1188 int get_secure_information(CAPayload_t payLoad)
1190 printf("entering get_secure_information\n");
1194 printf("Payload is NULL\n");
1198 char *subString = NULL;
1199 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1201 printf("This is not secure resource\n");
1205 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1207 printf("This secure resource does not have port information\n");
1211 char *startPos = strstr(subString, ":");
1214 printf("Parsing failed !\n");
1218 char *endPos = strstr(startPos, "}");
1221 printf("Parsing failed !\n");
1225 char portStr[4] = {0};
1226 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1228 printf("secured port is: %s\n", portStr);
1229 return atoi(portStr);
1232 void get_resource_uri(char *URI, char *resourceURI, int length)
1234 char *startPos = URI;
1236 if (NULL != (temp = strstr(URI, "://")))
1238 startPos = strchr(temp + 3, '/');
1241 printf("Resource URI is missing\n");
1246 char *endPos = strchr(startPos, '?');
1249 endPos = URI + strlen(URI);
1253 if (endPos - startPos <= length)
1254 memcpy(resourceURI, startPos + 1, endPos - startPos);
1256 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1259 CAResult_t get_network_type()
1261 char buf[MAX_BUF_LEN];
1263 printf("\n=============================================\n");
1264 printf("\tselect network type\n");
1265 printf("ETHERNET : 0\n");
1266 printf("WIFI : 1\n");
1267 printf("EDR : 2\n");
1269 printf("select : ");
1271 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
1272 if( gets(buf) == NULL)
1273 return CA_NOT_SUPPORTED ;
1275 int number = buf[0] - '0';
1277 number = (number < 0 || number > 3) ? 0 : 1 << number;
1279 if (!(number & 0xf))
1281 return CA_NOT_SUPPORTED;
1283 if (number & CA_ETHERNET)
1285 gSelectedNwType = CA_ETHERNET;
1286 return CA_STATUS_OK;
1288 if (number & CA_WIFI)
1290 gSelectedNwType = CA_WIFI;
1291 return CA_STATUS_OK;
1293 if (number & CA_EDR)
1295 gSelectedNwType = CA_EDR;
1296 return CA_STATUS_OK;
1300 gSelectedNwType = CA_LE;
1301 return CA_STATUS_OK;
1304 printf("\n=============================================\n");
1306 return CA_STATUS_FAILED;