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;
37 #define MAX_BUF_LEN 1024
38 #define MAX_OPT_LEN 16
46 #define IDENTITY ("1111111111111111")
50 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
54 CABool_t gLocalUnicastPort;
55 CABool_t gLocalSecurePort;
56 CAConnectivityType_t gSelectedNwType = CA_ETHERNET;
57 const char* gMessageType[] = {"CON", "NON", "ACK", "RESET"};
61 CAResult_t get_network_type();
63 CAResult_t initialize();
64 void start_listening_server();
65 void start_discovery_server();
68 void send_request_all();
70 void advertise_resource();
71 void send_notification();
72 void select_network();
73 void unselect_network();
74 void handle_request_response();
75 void find_fixed_resource();
76 void get_network_info();
78 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
79 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
80 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
81 void get_resource_uri(char *URI, char *resourceURI, int length);
82 int get_secure_information(CAPayload_t payLoad);
84 static CAToken_t gLastRequestToken = NULL;
85 static const char *gSecureInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
86 "\"if\":[\"oc.mi.def\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
87 static const char *gNormalInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
88 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
91 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");
113 *credInfo = pskCredsBlob;
115 printf("CAGetDtlsPskCredentials OUT\n");
118 int32_t SetCredentials()
120 printf("SetCredentials IN\n");
121 pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
123 memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
124 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
126 pskCredsBlob->num = 1;
128 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
130 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
131 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
133 printf("SetCredentials OUT\n");
139 g_main_loop_run(mainloop);
146 printf("\n=============================================\n");
147 printf("\t\tsample main\n");
148 printf("=============================================\n");
152 * Read DTLS PSK credentials from persistent storage and
153 * set in the OC stack.
156 if (SetCredentials() == 0)
158 printf("SetCredentials failed\n");
162 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
163 if(res != CA_STATUS_OK)
165 printf("Set credential handler fail\n");
170 if (res != CA_STATUS_OK)
172 printf("CAInitialize fail\n");
179 printf("select default network(WIFI)\n");
180 res = CASelectNetwork(CA_WIFI);
181 if (res != CA_STATUS_OK)
183 printf("CASelectNetwork fail\n");
189 CARegisterHandler(request_handler, response_handler);
193 if (gLastRequestToken != NULL)
195 CADestroyToken(gLastRequestToken);
197 gLastRequestToken = NULL;
201 clearDtlsCredentialInfo();
210 char menu = toupper(get_menu());
217 case 'Q': // quits the sample program
218 printf("quit..!!\n");
219 g_main_loop_quit(mainloop);
222 case 's': // start server
223 case 'S': // start server
224 start_listening_server();
227 case 't': // send request
232 case 'c': // start client
234 start_discovery_server();
237 case 'F': // find resource
241 case 'R': // send request
245 case 'A': // advertise resource
246 advertise_resource();
249 case 'b': // send notification
253 case 'N': // select network
257 case 'X': // unselect network
261 case 'H': // handle request response
262 handle_request_response();
269 find_fixed_resource();
270 while (gReceived == 0)
273 handle_request_response();
280 start_listening_server();
284 handle_request_response();
288 case 'g': // get network information
294 printf("not supported menu!!\n");
301 CAResult_t initialize()
303 mainloop = g_main_loop_new(NULL, FALSE);
304 pthread_create (&thread, NULL, (void *) &pthread_func, NULL);
306 CAResult_t res = CAInitialize();
307 if (res != CA_STATUS_OK)
309 printf("CAInitialize fail\n");
310 return CA_STATUS_FAILED;
316 void start_listening_server()
318 printf("start listening server!!\n");
320 CAResult_t res = CAStartListeningServer();
321 if (res != CA_STATUS_OK)
323 printf("start listening server fail\n");
327 printf("start listening server success\n");
331 void start_discovery_server()
333 printf("start discovery client!!\n");
335 CAResult_t res = CAStartDiscoveryServer();
336 if (res != CA_STATUS_OK)
338 printf("start discovery client fail\n");
342 printf("start discovery client success\n");
346 void find_fixed_resource()
348 char buf[MAX_BUF_LEN] =
350 strcpy(buf, "/a/light");
353 CAToken_t token = NULL;
354 CAResult_t res = CAGenerateToken(&token);
355 if (res != CA_STATUS_OK)
357 printf("token generate error!!");
361 printf("generated token %s\n", (token != NULL) ? token : "");
363 res = CAFindResource(buf, token);
364 if (res != CA_STATUS_OK)
366 printf("find resource error!!\n");
370 printf("find resource to %s URI\n", buf);
377 CADestroyToken(token);
381 printf("=============================================\n");
385 char buf[MAX_BUF_LEN];
387 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
389 printf("\n=============================================\n");
390 printf("ex) /a/light\n");
391 printf("reference uri : ");
396 CAToken_t token = NULL;
397 CAResult_t res = CAGenerateToken(&token);
398 if (res != CA_STATUS_OK)
400 printf("token generate error!!\n");
404 printf("generated token %s\n", (token != NULL) ? token : "");
406 res = CAFindResource(buf, token);
407 if (res != CA_STATUS_OK)
409 printf("find resource error!!\n");
413 printf("find resource to %s URI\n", buf);
415 if (gLastRequestToken != NULL)
417 CADestroyToken(gLastRequestToken);
420 gLastRequestToken = token;
427 CADestroyToken(token);
431 printf("=============================================\n");
436 char secureRequest[2] = {0};
439 res = get_network_type();
440 if (res != CA_STATUS_OK)
445 printf("Do you want to send secure request ?.... enter (0/1): ");
447 if ('1' == secureRequest[0])
449 printf("Enter the URI like below....\n");
450 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
454 printf("Enter the URI like below....\n");
455 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
456 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
459 char uri[MAX_BUF_LEN] = {'\0'};
462 // create remote endpoint
463 CARemoteEndpoint_t *endpoint = NULL;
464 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, gSelectedNwType, &endpoint)
467 printf("Failed to create remote endpoint!\n");
468 CADestroyRemoteEndpoint(endpoint);
472 //endpoint->connectivityType = gSelectedNwType;
474 char buf[MAX_BUF_LEN];
475 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
477 printf("\n=============================================\n");
478 printf("0:CON, 1:NON\n");
479 printf("select message type : ");
483 CAMessageType_t msgType = (buf[0] == '0' || buf[0] == '1') ? buf[0] - '0' : 0;
486 CAToken_t token = NULL;
487 if (CA_STATUS_OK != CAGenerateToken(&token))
489 printf("token generate error!!\n");
493 printf("generated token %s\n", (token != NULL) ? token : "");
495 // extract relative resourceuri from give uri
496 char resourceURI[15] = {0};
498 printf("URI : %s\n", uri);
499 get_resource_uri(uri, resourceURI, 14);
501 // create request data
502 CAInfo_t requestData;
503 memset(&requestData, 0, sizeof(CAInfo_t));
504 requestData.token = token;
505 if ('1' == secureRequest[0])
507 int length = strlen(gSecureInfoData) + strlen(resourceURI) + 1;
508 requestData.payload = (CAPayload_t) malloc(length);
509 sprintf(requestData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
513 int length = strlen(gNormalInfoData) + strlen(resourceURI) + 1;
514 requestData.payload = (CAPayload_t) malloc(length);
515 sprintf(requestData.payload, gNormalInfoData, resourceURI);
517 requestData.type = msgType;
519 CARequestInfo_t requestInfo;
520 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
521 requestInfo.method = CA_GET;
522 requestInfo.info = requestData;
525 CASendRequest(endpoint, &requestInfo);
529 CADestroyToken(token);
532 if (requestData.payload != NULL)
534 free(requestData.payload);
537 // destroy remote endpoint
538 CADestroyRemoteEndpoint(endpoint);
539 printf("=============================================\n");
542 void send_request_all()
544 char buf[MAX_BUF_LEN];
545 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
549 res = get_network_type();
550 if (res != CA_STATUS_OK)
555 printf("\n=============================================\n");
556 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
557 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
562 // create remote endpoint
563 CARemoteEndpoint_t *endpoint = NULL;
564 res = CACreateRemoteEndpoint(buf, gSelectedNwType, &endpoint);
566 if (res != CA_STATUS_OK)
568 printf("create remote endpoint error!!\n");
569 CADestroyRemoteEndpoint(endpoint);
574 CAGroupEndpoint_t *group = NULL;
575 group = (CAGroupEndpoint_t *)malloc(sizeof(CAGroupEndpoint_t));
576 group->connectivityType = endpoint->connectivityType;
577 group->resourceUri = endpoint->resourceUri;
581 CAToken_t token = NULL;
582 res = CAGenerateToken(&token);
584 if (res != CA_STATUS_OK)
586 printf("token generate error!!\n");
590 printf("generated token %s\n", (token != NULL) ? token : "");
592 CAInfo_t requestData;
593 memset(&requestData, 0, sizeof(CAInfo_t));
594 requestData.token = token;
595 requestData.payload = "Temp Json Payload";
596 requestData.type = CA_MSG_NONCONFIRM;
597 CARequestInfo_t requestInfo;
598 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
599 requestInfo.method = CA_GET;
600 requestInfo.info = requestData;
603 // CASendRequest(endpoint, &requestInfo);
604 CASendRequestToAll(group, &requestInfo);
608 CADestroyToken(token);
611 // destroy remote endpoint
612 CADestroyRemoteEndpoint(endpoint);
616 printf("=============================================\n");
619 void advertise_resource()
621 char buf[MAX_BUF_LEN];
623 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
625 printf("\n=============================================\n");
631 char optionData[MAX_OPT_LEN];
633 printf("Option Num : ");
634 scanf("%d", &optionNum);
635 CAHeaderOption_t *headerOpt;
636 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
637 if (NULL == headerOpt)
639 printf("Memory allocation failed!\n");
642 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
645 for (i = 0 ; i < optionNum ; i++)
648 printf("[%d] Option ID : ", i + 1);
649 scanf("%d", &optionID);
650 headerOpt[i].optionID = optionID;
652 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
653 printf("[%d] Option Data : ", i + 1);
654 scanf("%s", optionData);
655 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
656 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
658 headerOpt[i].optionLength = (uint16_t)strlen(optionData);
660 printf("\n=============================================\n");
663 CAToken_t token = NULL;
664 CAResult_t res = CAGenerateToken(&token);
665 if (res != CA_STATUS_OK)
667 printf("token generate error!!\n");
671 printf("generated token %s\n", (token != NULL) ? token : "");
673 CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
678 CADestroyToken(token);
685 void send_notification()
687 char buf[MAX_BUF_LEN];
688 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
692 res = get_network_type();
693 if (res != CA_STATUS_OK)
698 printf("\n=============================================\n");
699 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
700 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
705 // create remote endpoint
706 CARemoteEndpoint_t *endpoint = NULL;
707 res = CACreateRemoteEndpoint(buf, gSelectedNwType, &endpoint);
709 if (res != CA_STATUS_OK)
711 printf("create remote endpoint error!!\n");
712 CADestroyRemoteEndpoint(endpoint);
716 CAInfo_t respondeData;
717 memset(&respondeData, 0, sizeof(CAInfo_t));
718 respondeData.token = "client token";
719 respondeData.payload = "Temp Notification Data";
721 CAResponseInfo_t responseInfo;
722 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
723 responseInfo.result = CA_SUCCESS;
724 responseInfo.info = respondeData;
727 res = CASendNotification(endpoint, &responseInfo);
728 if (res != CA_STATUS_OK)
730 printf("send notification error\n");
734 printf("send notification success\n");
737 // destroy remote endpoint
738 if (endpoint != NULL)
740 CADestroyRemoteEndpoint(endpoint);
743 printf("\n=============================================\n");
745 void select_network()
747 char buf[MAX_BUF_LEN];
749 printf("\n=============================================\n");
750 printf("\tselect network\n");
751 printf("ETHERNET : 0\n");
752 printf("WIFI : 1\n");
757 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
760 int number = buf[0] - '0';
762 number = (number < 0 || number > 3) ? 0 : number;
764 CAResult_t res = CASelectNetwork(1 << number);
765 if (res != CA_STATUS_OK)
767 printf("select network error\n");
768 gSelectedNwType = 1 << number;
772 printf("select network success\n");
775 printf("=============================================\n");
778 void unselect_network()
780 char buf[MAX_BUF_LEN];
782 printf("\n=============================================\n");
783 printf("\tunselect enabled network\n");
784 printf("ETHERNET : 0\n");
785 printf("WIFI : 1\n");
790 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
793 int number = buf[0] - '0';
795 number = (number < 0 || number > 3) ? 1 : number;
797 CAResult_t res = CAUnSelectNetwork(1 << number);
798 if (res != CA_STATUS_OK)
800 printf("unselect network error\n");
804 printf("unselect network success\n");
806 printf("=============================================\n");
811 char buf[MAX_BUF_LEN];
813 printf("\n=============================================\n");
814 printf("\t\tMenu\n");
815 printf("\ts : start server\n");
816 printf("\tD : start client\n");
817 printf("\tf : find resource\n");
818 printf("\tr : send request\n");
819 printf("\tt : send request to all\n");
820 printf("\ta : advertise resource\n");
821 printf("\tb : send notification\n");
822 printf("\tn : select network\n");
823 printf("\tx : unselect network\n");
824 printf("\tg : get network information\n");
825 printf("\th : handle request response\n");
826 printf("\ty : run static client\n");
827 printf("\tz : run static server\n");
828 printf("\tq : quit\n");
829 printf("=============================================\n");
832 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
839 void handle_request_response()
841 printf("handle_request_response\n");
843 CAResult_t res = CAHandleRequestResponse();
844 if (res != CA_STATUS_OK)
846 printf("handle request error\n");
850 printf("handle request success\n");
854 void get_network_info()
858 CALocalConnectivity_t *tempInfo = NULL;
859 uint32_t tempSize = 0;
861 CAGetNetworkInformation(&tempInfo, &tempSize);
862 if (tempInfo == NULL || tempSize <= 0)
864 printf("network not connected\n");
868 printf("################## Network Information #######################\n");
869 printf("network info total size is %d\n\n", tempSize);
871 for (index = 0; index < tempSize; index++)
873 printf("Type: %d\n", tempInfo[index].type);
874 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
875 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
876 printf("Secured: %d\n\n", tempInfo[index].isSecured);
878 if (CA_TRUE == tempInfo[index].isSecured)
880 gLocalSecurePort = tempInfo[index].addressInfo.IP.port;
884 gLocalUnicastPort = tempInfo[index].addressInfo.IP.port;
891 printf("##############################################################");
894 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
898 printf("Remote endpoint is NULL!");
904 printf("Request info is NULL!");
908 printf("##########received request from remote device #############\n");
909 printf("Uri: %s\n", object->resourceUri);
910 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
911 object->addressInfo.IP.port, object->isSecured);
913 printf("Data: %s\n", requestInfo->info.payload);
914 printf("Message type: %s\n", gMessageType[requestInfo->info.type]);
915 if (gLastRequestToken != NULL && requestInfo->info.token != NULL
916 && (strcmp((char *)gLastRequestToken, requestInfo->info.token) == 0))
918 printf("token is same. received request of it's own. skip.. \n");
922 if (requestInfo->info.options)
924 uint32_t len = requestInfo->info.numOptions;
926 for (i = 0; i < len; i++)
928 printf("Option %d\n", i + 1);
929 printf("ID : %d\n", requestInfo->info.options[i].optionID);
930 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
931 requestInfo->info.options[i].optionData);
934 printf("############################################################\n");
936 //Check if this has secure communication information
937 if (requestInfo->info.payload)
939 int securePort = get_secure_information(requestInfo->info.payload);
940 if (0 < securePort) //Set the remote endpoint secure details and send response
942 printf("This is secure resource...\n");
946 length = 8; //length of "coaps://"
947 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
948 length += strlen(object->resourceUri) + 1;
950 uri = calloc(1, sizeof(char) * length);
953 printf("Failed to create new uri\n");
956 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
957 securePort, object->resourceUri);
959 CARemoteEndpoint_t *endpoint = NULL;
960 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
962 printf("Failed to create duplicate of remote endpoint!\n");
965 //endpoint->connectivityType = object->connectivityType;
966 endpoint->isSecured = CA_TRUE;
973 printf("send response with URI\n");
974 send_response(object, &requestInfo->info);
979 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
981 printf("##########Received response 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);
985 printf("response result : %d\n", responseInfo->result);
986 printf("Data: %s\n", responseInfo->info.payload);
987 printf("Message type: %s\n", gMessageType[responseInfo->info.type]);
988 printf("Token: %s\n", responseInfo->info.token);
989 if (responseInfo->info.options)
991 uint32_t len = responseInfo->info.numOptions;
993 for (i = 0; i < len; i++)
995 printf("Option %d\n", i + 1);
996 printf("ID : %d\n", responseInfo->info.options[i].optionID);
997 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
998 responseInfo->info.options[i].optionData);
1001 printf("############################################################\n");
1004 //Check if this has secure communication information
1005 if (responseInfo->info.payload)
1007 int securePort = get_secure_information(responseInfo->info.payload);
1008 if (0 < securePort) //Set the remote endpoint secure details and send response
1010 printf("This is secure resource...\n");
1015 void send_response(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1017 char buf[MAX_BUF_LEN];
1019 printf("entering send_response\n");
1021 printf("\n=============================================\n");
1022 printf("\tselect message type\n");
1023 printf("CA_MSG_CONFIRM : 0\n");
1024 printf("CA_MSG_NONCONFIRM : 1\n");
1025 printf("CA_MSG_ACKNOWLEDGE : 2\n");
1026 printf("CA_MSG_RESET : 3\n");
1027 printf("select : ");
1029 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
1032 int messageType = buf[0] - '0';
1033 int responseCode = 231 ;
1034 if(messageType != 3)
1037 printf("\n=============================================\n");
1038 printf("\tselect response code\n");
1039 printf("CA_SUCCESS : 200\n");
1040 printf("CA_CREATED : 201\n");
1041 printf("CA_DELETED : 202\n");
1042 printf("CA_EMPTY : 231\n");
1043 printf("CA_BAD_REQ : 400\n");
1044 printf("CA_BAD_OPT : 402\n");
1045 printf("CA_NOT_FOUND : 404\n");
1046 printf("CA_RETRANSMIT_TIMEOUT : 531\n");
1047 printf("select : ");
1048 scanf("%d", &responseCode);
1050 CAInfo_t responseData;
1051 memset(&responseData, 0, sizeof(CAInfo_t));
1052 responseData.type = messageType;
1053 responseData.messageId = (info != NULL) ? info->messageId : 0;
1054 responseData.token = (info != NULL) ? info->token : "";
1055 responseData.payload = "response payload";
1057 CAResponseInfo_t responseInfo;
1058 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
1059 responseInfo.result = responseCode;
1060 responseInfo.info = responseData;
1062 if (CA_TRUE == endpoint->isSecured)
1063 printf("Sending response on secure communication\n");
1065 printf("Sending response on non-secure communication\n");
1067 // send request (connectivityType from remoteEndpoint of request Info)
1068 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1069 if (res != CA_STATUS_OK)
1071 printf("send response error\n");
1075 printf("send response success\n");
1078 printf("=============================================\n");
1081 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
1084 printf("\n=============================================\n");
1086 CAInfo_t requestData;
1087 memset(&requestData, 0, sizeof(CAInfo_t));
1088 requestData.token = token;
1089 requestData.payload = "Temp Json Payload";
1091 CARequestInfo_t requestInfo;
1092 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1093 requestInfo.method = CA_GET;
1094 requestInfo.info = requestData;
1097 endpoint->connectivityType = CA_WIFI;
1099 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1100 if (res != CA_STATUS_OK)
1102 printf("send request error\n");
1106 printf("send request success\n");
1109 printf("=============================================\n");
1113 int get_secure_information(CAPayload_t payLoad)
1115 printf("entering get_secure_information\n");
1119 printf("Payload is NULL\n");
1123 char *subString = NULL;
1124 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1126 printf("This is not secure resource\n");
1130 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1132 printf("This secure resource does not have port information\n");
1136 char *startPos = strstr(subString, ":");
1139 printf("Parsing failed !\n");
1143 char *endPos = strstr(startPos, "}");
1146 printf("Parsing failed !\n");
1150 char portStr[4] = {0};
1151 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1153 printf("secured port is: %s\n", portStr);
1154 return atoi(portStr);
1157 void get_resource_uri(char *URI, char *resourceURI, int length)
1159 char *startPos = URI;
1161 if (NULL != (temp = strstr(URI, "://")))
1163 startPos = strchr(temp + 3, '/');
1166 printf("Resource URI is missing\n");
1171 char *endPos = strchr(startPos, '?');
1174 endPos = URI + strlen(URI);
1178 if (endPos - startPos <= length)
1179 memcpy(resourceURI, startPos + 1, endPos - startPos);
1181 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1184 CAResult_t get_network_type()
1186 char buf[MAX_BUF_LEN];
1188 printf("\n=============================================\n");
1189 printf("\tselect network type\n");
1190 printf("ETHERNET : 0\n");
1191 printf("WIFI : 1\n");
1192 printf("EDR : 2\n");
1194 printf("select : ");
1196 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
1199 int number = buf[0] - '0';
1201 number = (number < 0 || number > 3) ? 0 : 1 << number;
1203 if (!(number & 0xf))
1205 return CA_NOT_SUPPORTED;
1207 if (number & CA_ETHERNET)
1209 gSelectedNwType = CA_ETHERNET;
1210 return CA_STATUS_OK;
1212 if (number & CA_WIFI)
1214 gSelectedNwType = CA_WIFI;
1215 return CA_STATUS_OK;
1217 if (number & CA_EDR)
1219 gSelectedNwType = CA_EDR;
1220 return CA_STATUS_OK;
1224 gSelectedNwType = CA_LE;
1225 return CA_STATUS_OK;
1228 printf("\n=============================================\n");
1230 return CA_STATUS_FAILED;