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 ******************************************************************/
29 #include "cainterface.h"
30 #include "oic_malloc.h"
32 #include "ocsecurityconfig.h"
35 #define MAX_BUF_LEN 1024
36 #define MAX_OPT_LEN 16
40 #define SECURE_DEFAULT_PORT 5684
42 #define RESOURCE_URI_LENGTH 14
44 #define SYSTEM_INVOKE_ERROR 127
45 #define SYSTEM_ERROR -1
51 #define IDENTITY ("1111111111111111")
55 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
58 uint16_t g_local_secure_port = SECURE_DEFAULT_PORT;
59 CATransportType_t g_selected_nw_type = CA_IPV4;
60 const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"};
64 CAResult_t get_network_type();
65 CAResult_t get_input_data(char *buf, int32_t length);
67 void start_listening_server();
68 void start_discovery_server();
71 void send_request_all();
72 void advertise_resource();
73 void send_notification();
74 void select_network();
75 void unselect_network();
76 void handle_request_response();
77 void find_fixed_resource();
78 void get_network_info();
79 void send_secure_request();
81 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
82 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
83 void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info);
84 void get_resource_uri(char *URI, char *resourceURI, int length);
85 int get_secure_information(CAPayload_t payLoad);
87 static CAToken_t g_last_request_token = NULL;
88 static const char SECURE_COAPS_PREFIX[] = "coaps://";
89 static const char SECURE_INFO_DATA[] =
90 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
91 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
92 static const char NORMAL_INFO_DATA[] =
93 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
94 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
97 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
99 void clearDtlsCredentialInfo()
101 printf("clearDtlsCredentialInfo IN\n");
104 // Initialize sensitive data to zeroes before freeing.
105 if (pskCredsBlob->creds)
107 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
108 free(pskCredsBlob->creds);
111 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
115 printf("clearDtlsCredentialInfo OUT\n");
118 // Internal API. Invoked by CA stack to retrieve credentials from this module
119 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
121 printf("CAGetDtlsPskCredentials IN\n");
124 printf("Invalid credential container");
128 *credInfo = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
129 if (NULL == *credInfo)
131 printf("Failed to allocate credential blob.");
135 size_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
136 (*credInfo)->creds = (OCDtlsPskCreds *)malloc(credLen);
137 if (NULL == (*credInfo)->creds)
139 printf("Failed to allocate credentials.");
145 memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
146 (*credInfo)->num = pskCredsBlob->num;
147 memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
149 printf("CAGetDtlsPskCredentials OUT\n");
153 CAResult_t SetCredentials()
155 printf("SetCredentials IN\n");
156 pskCredsBlob = (CADtlsPskCredsBlob_t *)calloc(1, sizeof(CADtlsPskCredsBlob_t));
157 if (NULL == pskCredsBlob)
159 printf("Memory allocation failed!\n");
160 return CA_MEMORY_ALLOC_FAILED;
162 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
165 pskCredsBlob->num = 1;
167 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
168 if (NULL == pskCredsBlob->creds)
170 printf("Memory allocation failed!\n");
172 return CA_MEMORY_ALLOC_FAILED;
175 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
176 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
178 printf("SetCredentials OUT\n");
185 int ret = system("clear");
186 // shell invoke error: 127, others: -1
187 if (SYSTEM_INVOKE_ERROR == ret || SYSTEM_ERROR == ret)
189 printf("Terminal Clear Error: %d\n", ret);
193 printf("=============================================\n");
194 printf("\t\tsample main\n");
195 printf("=============================================\n");
197 CAResult_t res = CAInitialize();
198 if (CA_STATUS_OK != res)
200 printf("CAInitialize fail\n");
205 * Read DTLS PSK credentials from persistent storage and
206 * set in the OC stack.
209 res = SetCredentials();
210 if (CA_STATUS_OK != res)
212 printf("SetCredentials failed\n");
216 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
217 if (CA_STATUS_OK != res)
219 printf("Set credential handler fail\n");
225 CARegisterHandler(request_handler, response_handler);
229 CADestroyToken(g_last_request_token);
231 g_last_request_token = NULL;
235 clearDtlsCredentialInfo();
244 char menu = get_menu();
254 printf("quit..!!\n");
257 case 's': // start server
259 start_listening_server();
262 case 't': // send request
267 case 'c': // start client
269 start_discovery_server();
272 case 'f': // find resource
277 case 'r': // send request
282 case 'a': // advertise resource
284 advertise_resource();
287 case 'b': // send notification
292 case 'n': // select network
297 case 'x': // unselect network
302 case 'h': // handle request response
304 handle_request_response();
312 find_fixed_resource();
313 while (g_received == 0)
316 handle_request_response();
325 start_discovery_server();
326 send_secure_request();
327 while (g_received == 0)
330 handle_request_response();
336 start_listening_server();
340 handle_request_response();
344 case 'g': // get network information
350 printf("not supported menu!!\n");
356 void start_listening_server()
358 printf("start listening server!!\n");
360 CAResult_t res = CAStartListeningServer();
361 if (CA_STATUS_OK != res)
363 printf("start listening server fail, error code : %d\n", res);
367 printf("start listening server success\n");
371 void start_discovery_server()
373 printf("start discovery client!!\n");
375 CAResult_t res = CAStartDiscoveryServer();
376 if (CA_STATUS_OK != res)
378 printf("start discovery client fail, error code : %d\n", res);
382 printf("start discovery client success\n");
386 void find_fixed_resource()
389 CAToken_t token = NULL;
390 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
392 CAResult_t res = CAGenerateToken(&token, tokenLength);
393 if ((CA_STATUS_OK != res) || (!token))
395 printf("Token generate error!!");
398 printf("Generated token %s\n", token);
400 char buf[MAX_BUF_LEN] = { 0 };
401 strcpy(buf, "/a/light");
403 res = CAFindResource(buf, token, tokenLength);
404 if (CA_STATUS_OK != res)
406 printf("Find resource error : %d\n", res);
410 printf("Find resource to %s URI\n", buf);
414 CADestroyToken(token);
416 printf("=============================================\n");
421 printf("\n=============================================\n");
422 printf("ex) /a/light\n");
423 printf("reference uri : ");
425 char buf[MAX_BUF_LEN] = { 0 };
426 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
432 CAToken_t token = NULL;
433 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
435 CAResult_t res = CAGenerateToken(&token, tokenLength);
436 if ((CA_STATUS_OK != res) || (!token))
438 printf("Token generate error!!\n");
442 printf("Generated token %s\n", token);
444 res = CAFindResource(buf, token, tokenLength);
445 if (CA_STATUS_OK != res)
447 printf("Find resource error : %d\n", res);
448 CADestroyToken(token);
452 printf("Find resource to %s URI\n", buf);
453 CADestroyToken(g_last_request_token);
454 g_last_request_token = token;
457 printf("=============================================\n");
462 CAResult_t res = get_network_type();
463 if (CA_STATUS_OK != res)
468 printf("Do you want to send secure request ?.... enter (0/1): ");
470 char secureRequest[MAX_BUF_LEN] = {0};
471 if (CA_STATUS_OK != get_input_data(secureRequest, MAX_BUF_LEN))
476 if (strcmp(secureRequest, "1") == 0)
478 printf("Enter the URI like below....\n");
479 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
481 else if (strcmp(secureRequest, "0") == 0)
483 printf("Enter the URI like below....\n");
484 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
485 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
489 printf("Input data is wrong value\n");
493 char uri[MAX_BUF_LEN] = {'\0'};
494 if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
499 // create remote endpoint
500 CARemoteEndpoint_t *endpoint = NULL;
501 res = CACreateRemoteEndpoint(uri, g_selected_nw_type, &endpoint);
502 if (CA_STATUS_OK != res || !endpoint)
504 printf("Failed to create remote endpoint, error code : %d\n", res);
508 printf("\n=============================================\n");
509 printf("0:CON, 1:NON\n");
510 printf("select message type : ");
512 char buf[MAX_BUF_LEN] = { 0 };
513 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
515 CADestroyRemoteEndpoint(endpoint);
519 CAMessageType_t msgType = (buf[0] == '1') ? 1 : 0;
522 CAToken_t token = NULL;
523 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
525 res = CAGenerateToken(&token, tokenLength);
526 if ((CA_STATUS_OK != res) || (!token))
528 printf("Token generate error, error code : %d\n", res);
529 CADestroyRemoteEndpoint(endpoint);
533 printf("Generated token %s\n", token);
535 // extract relative resourceuri from give uri
536 printf("URI : %s\n", uri);
538 char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
539 get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
541 // create request data
542 CAInfo_t requestData = { 0 };
543 requestData.token = token;
544 requestData.tokenLength = tokenLength;
546 if (strcmp(secureRequest, "1") == 0)
548 size_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
549 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
550 if (NULL == requestData.payload)
552 printf("Memory allocation fail\n");
553 CADestroyRemoteEndpoint(endpoint);
554 CADestroyToken(token);
557 snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_local_secure_port);
561 size_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
562 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
563 if (NULL == requestData.payload)
565 printf("Memory allocation fail\n");
566 CADestroyRemoteEndpoint(endpoint);
567 CADestroyToken(token);
570 snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
572 requestData.type = msgType;
574 CARequestInfo_t requestInfo = { 0 };
575 requestInfo.method = CA_GET;
576 requestInfo.info = requestData;
579 res = CASendRequest(endpoint, &requestInfo);
580 if (CA_STATUS_OK != res)
582 printf("Could not send request : %d\n", res);
586 CADestroyToken(token);
587 // destroy remote endpoint
588 CADestroyRemoteEndpoint(endpoint);
589 free(requestData.payload);
592 printf("=============================================\n");
595 void send_secure_request()
597 char uri[MAX_BUF_LEN];
598 char ipv4addr[CA_IPADDR_SIZE];
600 printf("\n=============================================\n");
601 printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");
603 if (CA_STATUS_OK != get_input_data(ipv4addr, CA_IPADDR_SIZE))
607 snprintf(uri, MAX_BUF_LEN, "%s%s:5684/a/light", SECURE_COAPS_PREFIX, ipv4addr);
609 // create remote endpoint
610 CARemoteEndpoint_t *endpoint = NULL;
611 CAResult_t res = CACreateRemoteEndpoint(uri, CA_IPV4, &endpoint);
612 if (CA_STATUS_OK != res)
614 printf("Failed to create remote endpoint, error code: %d\n", res);
619 CAToken_t token = NULL;
620 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
622 res = CAGenerateToken(&token, tokenLength);
623 if ((CA_STATUS_OK != res) || (!token))
625 printf("Token generate error, error code : %d\n", res);
629 printf("Generated token %s\n", token);
631 // create request data
632 CAMessageType_t msgType = CA_MSG_NONCONFIRM;
633 CAInfo_t requestData = { 0 };
634 requestData.token = token;
635 requestData.tokenLength = tokenLength;
636 requestData.type = msgType;
638 CARequestInfo_t requestInfo = { 0 };
639 requestInfo.method = CA_GET;
640 requestInfo.info = requestData;
643 CASendRequest(endpoint, &requestInfo);
647 CADestroyToken(token);
648 CADestroyRemoteEndpoint(endpoint);
649 printf("=============================================\n");
653 void send_request_all()
655 CAResult_t res = get_network_type();
656 if (CA_STATUS_OK != res)
661 printf("\n=============================================\n");
662 printf("ex) /a/light\n");
663 printf("resource uri : ");
665 char buf[MAX_BUF_LEN] = { 0 };
666 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
671 // create remote endpoint
672 CARemoteEndpoint_t *endpoint = NULL;
673 res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
674 if (CA_STATUS_OK != res)
676 printf("Create remote endpoint error, error code: %d\n", res);
680 CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
683 printf("Memory allocation failed!\n");
684 CADestroyRemoteEndpoint(endpoint);
687 group->transportType = endpoint->transportType;
688 group->resourceUri = endpoint->resourceUri;
691 CAToken_t token = NULL;
692 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
694 res = CAGenerateToken(&token, tokenLength);
695 if ((CA_STATUS_OK != res) || (!token))
697 printf("Token generate error!!\n");
698 CADestroyRemoteEndpoint(endpoint);
703 printf("generated token %s\n", token);
705 CAInfo_t requestData = { 0 };
706 requestData.token = token;
707 requestData.tokenLength = tokenLength;
708 requestData.payload = "Temp Json Payload";
709 requestData.type = CA_MSG_NONCONFIRM;
711 CARequestInfo_t requestInfo = { 0 };
712 requestInfo.method = CA_GET;
713 requestInfo.info = requestData;
716 res = CASendRequestToAll(group, &requestInfo);
717 if (CA_STATUS_OK != res)
719 printf("Could not send request to all\n");
720 CADestroyToken(token);
724 CADestroyToken(g_last_request_token);
725 g_last_request_token = token;
728 // destroy remote endpoint
729 CADestroyRemoteEndpoint(endpoint);
732 printf("=============================================\n");
735 void advertise_resource()
737 printf("\n=============================================\n");
740 char buf[MAX_BUF_LEN] = { 0 };
741 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
746 char optionNumBuf[MAX_BUF_LEN] = { 0 };
747 char optionData[MAX_OPT_LEN] = { 0 } ;
749 printf("Option Num : ");
750 if (CA_STATUS_OK != get_input_data(optionNumBuf, MAX_BUF_LEN))
754 int optionNum = atoi(optionNumBuf);
756 CAHeaderOption_t * headerOpt = (CAHeaderOption_t *)
757 calloc(1, optionNum * sizeof(CAHeaderOption_t));
758 if (NULL == headerOpt)
760 printf("Memory allocation failed!\n");
765 for (i = 0; i < optionNum; i++)
767 char getOptionID[MAX_BUF_LEN] = { 0 } ;
769 printf("[%d] Option ID : ", i + 1);
770 if (CA_STATUS_OK != get_input_data(getOptionID, MAX_BUF_LEN))
775 int optionID = atoi(getOptionID);
777 headerOpt[i].optionID = optionID;
779 printf("[%d] Option Data : ", i + 1);
780 if (CA_STATUS_OK != get_input_data(optionData, MAX_OPT_LEN))
786 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
787 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
789 headerOpt[i].optionLength = (uint16_t) strlen(optionData);
791 printf("\n=============================================\n");
794 CAToken_t token = NULL;
795 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
797 CAResult_t res = CAGenerateToken(&token, tokenLength);
798 if ((CA_STATUS_OK != res) || (!token))
800 printf("Token generate error!!\n");
805 printf("Generated token %s\n", token);
807 res = CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t) optionNum);
808 if (CA_STATUS_OK != res)
810 printf("Could not start advertise resource\n");
811 CADestroyToken(token);
815 CADestroyToken(g_last_request_token);
816 g_last_request_token = token;
822 void send_notification()
824 CAResult_t res = get_network_type();
825 if (CA_STATUS_OK != res)
830 printf("\n=============================================\n");
831 printf("Enter the URI like below....\n");
832 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
833 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
836 char buf[MAX_BUF_LEN] = { 0 };
837 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
842 printf("\n=============================================\n");
843 printf("\tselect message type\n");
847 printf("RESET : 3\n");
850 char messageTypeBuf[MAX_BUF_LEN] = { 0 };
851 if (CA_STATUS_OK != get_input_data(messageTypeBuf, MAX_BUF_LEN))
856 int messageType = messageTypeBuf[0] - '0';
858 // create remote endpoint
859 CARemoteEndpoint_t *endpoint = NULL;
860 res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
861 if (CA_STATUS_OK != res)
863 printf("Create remote endpoint error, error code: %d\n", res);
868 CAToken_t token = NULL;
869 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
871 res = CAGenerateToken(&token, tokenLength);
872 if ((CA_STATUS_OK != res) || (!token))
874 printf("Token generate error!!\n");
875 CADestroyRemoteEndpoint(endpoint);
879 printf("Generated token %s\n", token);
881 CAInfo_t respondData = { 0 };
882 respondData.token = token;
883 respondData.payload = "Temp Notification Data";
884 respondData.type = messageType;
886 CAResponseInfo_t responseInfo = { 0 };
887 responseInfo.result = CA_SUCCESS;
888 responseInfo.info = respondData;
891 res = CASendNotification(endpoint, &responseInfo);
892 if (CA_STATUS_OK != res)
894 printf("Send notification error, error code: %d\n", res);
898 printf("Send notification success\n");
902 CADestroyToken(token);
903 // destroy remote endpoint
904 CADestroyRemoteEndpoint(endpoint);
906 printf("\n=============================================\n");
909 void select_network()
911 printf("\n=============================================\n");
912 printf("\tselect network\n");
913 printf("IPv4 : 0\n");
918 char buf[MAX_BUF_LEN] = { 0 };
919 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
924 int number = buf[0] - '0';
926 if (number < 0 || number > 3)
928 printf("Invalid network type\n");
932 CAResult_t res = CASelectNetwork(1 << number);
933 if (CA_STATUS_OK != res)
935 printf("Select network error\n");
939 printf("Select network success\n");
942 printf("=============================================\n");
945 void unselect_network()
947 printf("\n=============================================\n");
948 printf("\tunselect enabled network\n");
949 printf("IPv4 : 0\n");
954 char buf[MAX_BUF_LEN] = { 0 };
955 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
960 int number = buf[0] - '0';
962 if (number < 0 || number > 3)
964 printf("Invalid network type\n");
968 CAResult_t res = CAUnSelectNetwork(1 << number);
969 if (CA_STATUS_OK != res)
971 printf("Unselect network error\n");
975 printf("Unselect network success\n");
978 printf("=============================================\n");
983 printf("\n=============================================\n");
984 printf("\t\tMenu\n");
985 printf("\ts : start server\n");
986 printf("\tc : start client\n");
987 printf("\tf : find resource\n");
988 printf("\tr : send request\n");
989 printf("\tt : send request to all\n");
990 printf("\ta : advertise resource\n");
991 printf("\tb : send notification\n");
992 printf("\tn : select network\n");
993 printf("\tx : unselect network\n");
994 printf("\tg : get network information\n");
995 printf("\th : handle request response\n");
996 printf("\ty : run static client\n");
997 printf("\tz : run static server\n");
998 printf("\tw : send secure request\n");
999 printf("\tq : quit\n");
1000 printf("=============================================\n");
1001 printf("select : ");
1003 char buf[MAX_BUF_LEN] = { 0 };
1004 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1006 printf("Failed to get input data\n");
1012 void handle_request_response()
1014 printf("Handle_request_response\n");
1016 CAResult_t res = CAHandleRequestResponse();
1017 if (CA_STATUS_OK != res)
1019 printf("Handle request error, error code: %d\n", res);
1023 printf("Handle request success\n");
1027 void get_network_info()
1029 CALocalConnectivity_t *tempInfo = NULL;
1030 uint32_t tempSize = 0;
1032 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
1033 if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
1035 printf("Network not connected\n");
1040 printf("################## Network Information #######################\n");
1041 printf("Network info total size is %d\n\n", tempSize);
1044 for (index = 0; index < tempSize; index++)
1046 printf("Type: %d\n", tempInfo[index].type);
1047 if (CA_IPV4 == tempInfo[index].type)
1049 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
1050 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
1052 else if (CA_EDR == tempInfo[index].type)
1054 printf("Address: %s\n", tempInfo[index].addressInfo.BT.btMacAddress);
1056 else if (CA_LE == tempInfo[index].type)
1058 printf("Address: %s\n", tempInfo[index].addressInfo.LE.leMacAddress);
1060 printf("Secured: %s\n\n", tempInfo[index].isSecured ? "true" : "false");
1062 if (tempInfo[index].isSecured)
1064 g_local_secure_port = tempInfo[index].addressInfo.IP.port;
1065 printf("Secured: in global %d\n\n", g_local_secure_port);
1070 printf("##############################################################");
1073 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
1075 if (NULL == object || NULL == requestInfo)
1077 printf("Input parameter is NULL\n");
1081 if ((NULL != g_last_request_token) && (NULL != requestInfo->info.token)
1082 && (memcmp(g_last_request_token, requestInfo->info.token,
1083 CA_MAX_TOKEN_LEN) == 0))
1085 printf("token is same. received request of it's own. skip.. \n");
1089 printf("##########received request from remote device #############\n");
1090 printf("Uri: %s\n", object->resourceUri);
1091 if (CA_IPV4 == object->transportType)
1093 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1094 object->addressInfo.IP.port, object->isSecured);
1096 else if (CA_EDR == object->transportType)
1098 printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
1100 else if (CA_LE == object->transportType)
1102 printf("Remote Address: %s \n", object->addressInfo.LE.leMacAddress);
1104 printf("Data: %s\n", requestInfo->info.payload);
1105 printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
1107 if (requestInfo->info.options)
1109 uint32_t len = requestInfo->info.numOptions;
1111 for (i = 0; i < len; i++)
1113 printf("Option %d\n", i + 1);
1114 printf("ID : %d\n", requestInfo->info.options[i].optionID);
1115 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
1116 requestInfo->info.options[i].optionData);
1119 printf("############################################################\n");
1121 //Check if this has secure communication information
1122 if (requestInfo->info.payload &&
1123 (CA_IPV4 == object->transportType))
1125 int securePort = get_secure_information(requestInfo->info.payload);
1126 if (0 < securePort) //Set the remote endpoint secure details and send response
1128 printf("This is secure resource...\n");
1130 //length of "coaps://"
1131 size_t length = sizeof(SECURE_COAPS_PREFIX) - 1;
1133 // length of "ipaddress:port"
1134 length += strlen(object->addressInfo.IP.ipAddress) + PORT_LENGTH;
1135 length += strlen(object->resourceUri) + 1;
1137 char *uri = calloc(1, sizeof(char) * length);
1140 printf("Failed to create new uri\n");
1143 sprintf(uri, "%s%s:%d/%s", SECURE_COAPS_PREFIX, object->addressInfo.IP.ipAddress,
1144 object->addressInfo.IP.port, object->resourceUri);
1146 CARemoteEndpoint_t *endpoint = NULL;
1147 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->transportType, &endpoint))
1149 printf("Failed to create duplicate of remote endpoint!\n");
1152 endpoint->isSecured = true;
1159 printf("Send response with URI\n");
1160 send_response(object, &requestInfo->info);
1165 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1167 printf("##########Received response from remote device #############\n");
1168 printf("Uri: %s\n", object->resourceUri);
1169 if (CA_IPV4 == object->transportType)
1171 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1172 object->addressInfo.IP.port, object->isSecured);
1174 else if (CA_EDR == object->transportType)
1176 printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
1178 else if (CA_LE == object->transportType)
1180 printf("Remote Address: %s \n", object->addressInfo.LE.leMacAddress);
1182 printf("response result : %d\n", responseInfo->result);
1183 printf("Data: %s\n", responseInfo->info.payload);
1184 printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
1185 printf("Token: %s\n", responseInfo->info.token);
1186 if (responseInfo->info.options)
1188 uint32_t len = responseInfo->info.numOptions;
1190 for (i = 0; i < len; i++)
1192 printf("Option %d\n", i + 1);
1193 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1194 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1195 responseInfo->info.options[i].optionData);
1198 printf("############################################################\n");
1201 //Check if this has secure communication information
1202 if (responseInfo->info.payload)
1204 int securePort = get_secure_information(responseInfo->info.payload);
1205 if (0 < securePort) //Set the remote endpoint secure details and send response
1207 printf("This is secure resource...\n");
1212 void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
1214 printf("entering send_response\n");
1216 printf("\n=============================================\n");
1217 printf("\tselect message type\n");
1218 printf("CON : 0\n");
1219 printf("NON : 1\n");
1220 printf("ACK : 2\n");
1221 printf("RESET : 3\n");
1222 printf("select : ");
1224 char buf[MAX_BUF_LEN] = { 0 };
1225 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1230 int messageType = buf[0] - '0';
1231 if (0 > messageType || 3 < messageType)
1233 printf("Invalid message type\n");
1237 int responseCode = 0 ;
1238 char responseCodeBuf[MAX_BUF_LEN] = { 0 };
1239 if (CA_MSG_RESET != messageType)
1241 printf("\n=============================================\n");
1242 printf("\tselect response code\n");
1243 printf("EMPTY : 0\n");
1244 printf("SUCCESS : 200\n");
1245 printf("CREATED : 201\n");
1246 printf("DELETED : 202\n");
1247 printf("BAD_REQ : 400\n");
1248 printf("BAD_OPT : 402\n");
1249 printf("NOT_FOUND : 404\n");
1250 printf("INTERNAL_SERVER_ERROR : 500\n");
1251 printf("RETRANSMIT_TIMEOUT : 504\n");
1252 printf("select : ");
1254 if (CA_STATUS_OK != get_input_data(responseCodeBuf, MAX_BUF_LEN))
1258 responseCode = atoi(responseCodeBuf);
1260 CAInfo_t responseData = { 0 };
1261 responseData.type = messageType;
1262 responseData.messageId = (info != NULL) ? info->messageId : 0;
1264 if(CA_MSG_RESET != messageType)
1266 responseData.token = (info != NULL) ? info->token : NULL;
1267 responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
1269 if (endpoint->isSecured)
1271 printf("Sending response on secure communication\n");
1273 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(endpoint->resourceUri);
1274 responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
1275 if (NULL == responseData.payload)
1277 printf("Memory allocation fail\n");
1280 snprintf(responseData.payload, length, SECURE_INFO_DATA, endpoint->resourceUri,
1281 g_local_secure_port);
1285 printf("Sending response on non-secure communication\n");
1287 uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(endpoint->resourceUri);
1288 responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
1289 if (NULL == responseData.payload)
1291 printf("Memory allocation fail\n");
1294 snprintf(responseData.payload, length, NORMAL_INFO_DATA, endpoint->resourceUri);
1298 CAResponseInfo_t responseInfo = { 0 };
1299 responseInfo.result = responseCode;
1300 responseInfo.info = responseData;
1302 // send response (transportType from remoteEndpoint of request Info)
1303 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1304 if (CA_STATUS_OK != res)
1306 printf("Send response error\n");
1310 printf("Send response success\n");
1313 printf("=============================================\n");
1316 int get_secure_information(CAPayload_t payLoad)
1318 printf("Entering get_secure_information\n");
1322 printf("Payload is NULL\n");
1326 char *subString = NULL;
1327 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1329 printf("This is not secure resource\n");
1333 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1335 printf("This secure resource does not have port information\n");
1339 char *startPos = strstr(subString, ":");
1342 printf("Parsing failed !\n");
1346 char *endPos = strstr(startPos, "}");
1349 printf("Parsing failed !\n");
1353 char portStr[4] = {0};
1354 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1356 printf("secured port is: %s\n", portStr);
1357 return atoi(portStr);
1360 void get_resource_uri(char *URI, char *resourceURI, int length)
1362 char *startPos = URI;
1364 if (NULL != (temp = strstr(URI, "://")))
1366 startPos = strchr(temp + 3, '/');
1369 printf("Resource URI is missing\n");
1374 char *endPos = strchr(startPos, '?');
1377 endPos = URI + strlen(URI);
1381 if (endPos - startPos <= length)
1383 memcpy(resourceURI, startPos + 1, endPos - startPos);
1386 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1389 CAResult_t get_network_type()
1391 char buf[MAX_BUF_LEN] = { 0 };
1393 printf("\n=============================================\n");
1394 printf("\tselect network type\n");
1395 printf("IPv4 : 0\n");
1398 printf("select : ");
1400 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1402 return CA_NOT_SUPPORTED ;
1405 int number = buf[0] - '0';
1407 number = (number < 0 || number > 3) ? 0 : 1 << number;
1409 if (!(number & 0xf))
1411 return CA_NOT_SUPPORTED;
1413 if (number & CA_IPV4)
1415 g_selected_nw_type = CA_IPV4;
1416 return CA_STATUS_OK;
1418 if (number & CA_EDR)
1420 g_selected_nw_type = CA_EDR;
1421 return CA_STATUS_OK;
1425 g_selected_nw_type = CA_LE;
1426 return CA_STATUS_OK;
1429 printf("\n=============================================\n");
1431 return CA_STATUS_FAILED;
1434 CAResult_t get_input_data(char *buf, int32_t length)
1436 if (!fgets(buf, length, stdin))
1438 printf("fgets error\n");
1439 return CA_STATUS_FAILED;
1443 if ( (p = strchr(buf, '\n')) != NULL )
1448 return CA_STATUS_OK;