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"
28 #include "ocsecurityconfig.h"
31 #define MAX_BUF_LEN 1024
32 #define MAX_OPT_LEN 16
40 #define IDENTITY ("1111111111111111")
44 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
48 CABool_t gLocalUnicastPort;
49 CABool_t gLocalSecurePort;
50 CAConnectivityType_t gSelectedNwType = CA_ETHERNET;
54 CAResult_t get_network_type();
56 void start_listening_server();
57 void start_discovery_server();
60 void send_request_all();
62 void advertise_resource();
63 void send_notification();
64 void select_network();
65 void unselect_network();
66 void handle_request_response();
67 void find_fixed_resource();
68 void get_network_info();
69 void send_secure_request();
71 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
72 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
73 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
74 void get_resource_uri(char *URI, char *resourceURI, int length);
75 int get_secure_information(CAPayload_t payLoad);
77 static CAToken_t gLastRequestToken = NULL;
78 static const char *gSecureInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
79 "\"if\":[\"oc.mi.def\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
80 static const char *gNormalInfoData = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
81 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
83 static OCDtlsPskCredsBlob *pskCredsBlob = NULL;
85 void clearDtlsCredentialInfo()
87 printf("clearDtlsCredentialInfo IN\n");
90 // Zero out sensitive data before freeing.
91 if (pskCredsBlob->num)
93 memset(pskCredsBlob->creds, 0,
94 sizeof(OCDtlsPskCredsBlob) + (sizeof(OCDtlsPskCreds)*(pskCredsBlob->num - 1)));
96 free(pskCredsBlob->creds);
99 printf("clearDtlsCredentialInfo OUT\n");
102 // Internal API. Invoked by OC stack to retrieve credentials from this module
103 void OCGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
105 printf("OCGetDtlsPskCredentials IN\n");
107 *credInfo = pskCredsBlob;
109 printf("OCGetDtlsPskCredentials OUT\n");
112 int32_t SetCredentials()
115 printf("SetCredentials IN\n");
116 pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
120 memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
121 pskCredsBlob->num = DtlsPskCredsBlobVer_CurrentVersion;
122 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
124 pskCredsBlob->num = 1;
126 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
127 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
132 printf("SetCredentials OUT\n");
140 printf("=============================================\n");
141 printf("\t\tsample main\n");
142 printf("=============================================\n");
145 * Read DTLS PSK credentials from persistent storage and
146 * set in the OC stack.
148 if (SetCredentials() == 0)
150 printf("SetCredentials failed\n");
154 CAResult_t res = CAInitialize();
155 if (res != CA_STATUS_OK)
157 printf("CAInitialize fail\n");
164 printf("select default network(WIFI)\n");
165 res = CASelectNetwork(CA_WIFI);
166 if (res != CA_STATUS_OK)
168 printf("CASelectNetwork fail\n");
174 res = CARegisterHandler(request_handler, response_handler);
175 if (res != CA_STATUS_OK)
177 printf("CARegisterHandler fail\n");
184 clearDtlsCredentialInfo();
193 char menu = get_menu();
203 printf("quit..!!\n");
206 case 's': // start server
208 start_listening_server();
211 case 't': // send request
216 case 'c': // start client
218 start_discovery_server();
221 case 'f': // find resource
226 case 'r': // send request
231 case 'a': // advertise resource
233 advertise_resource();
236 case 'b': // send notification
241 case 'n': // select network
246 case 'x': // unselect network
251 case 'h': // handle request response
253 handle_request_response();
260 find_fixed_resource();
261 while (gReceived == 0)
264 handle_request_response();
273 start_discovery_server();
274 send_secure_request();
275 while (gReceived == 0)
278 handle_request_response();
285 start_listening_server();
289 handle_request_response();
293 case 'g': // get network information
299 printf("not supported menu!!\n");
308 void start_listening_server()
310 printf("start listening server!!\n");
312 CAResult_t res = CAStartListeningServer();
313 if (res != CA_STATUS_OK)
315 printf("start listening server fail\n");
319 printf("start listening server success\n");
323 void start_discovery_server()
325 printf("start discovery client!!\n");
327 CAResult_t res = CAStartDiscoveryServer();
328 if (res != CA_STATUS_OK)
330 printf("start discovery client fail\n");
334 printf("start discovery client success\n");
338 void find_fixed_resource()
340 char buf[MAX_BUF_LEN] =
342 strcpy(buf, "/a/light");
345 CAToken_t token = NULL;
346 CAResult_t res = CAGenerateToken(&token);
347 if (res != CA_STATUS_OK)
349 printf("token generate error!!");
353 printf("generated token %s\n", (token != NULL) ? token : "");
355 res = CAFindResource(buf, token);
356 if (res != CA_STATUS_OK)
358 printf("find resource error!!\n");
362 printf("find resource to %s URI\n", buf);
369 CADestroyToken(token);
373 printf("=============================================\n");
378 char buf[MAX_BUF_LEN];
380 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
382 printf("\n=============================================\n");
383 printf("ex) /a/light\n");
384 printf("reference uri : ");
389 CAToken_t token = NULL;
390 CAResult_t res = CAGenerateToken(&token);
391 if (res != CA_STATUS_OK)
393 printf("token generate error!!\n");
397 printf("generated token %s\n", (token != NULL) ? token : "");
399 res = CAFindResource(buf, token);
400 if (res != CA_STATUS_OK)
402 printf("find resource error!!\n");
406 printf("find resource to %s URI\n", buf);
408 gLastRequestToken = token;
415 CADestroyToken(token);
419 printf("=============================================\n");
424 char secureRequest[2] = {0};
427 res = get_network_type();
428 if (res != CA_STATUS_OK)
433 printf("Do you want to send secure request ?.... enter (0/1): ");
435 if ('1' == secureRequest[0])
437 printf("Enter the URI like below....\n");
438 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
442 printf("Enter the URI like below....\n");
443 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
444 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
447 char uri[MAX_BUF_LEN] = {'\0'};
450 // create remote endpoint
451 CARemoteEndpoint_t *endpoint = NULL;
452 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, gSelectedNwType, &endpoint)
455 printf("Failed to create remote endpoint!\n");
456 CADestroyRemoteEndpoint(endpoint);
460 //endpoint->connectivityType = gSelectedNwType;
462 char buf[MAX_BUF_LEN];
463 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
465 printf("\n=============================================\n");
466 printf("0:CON, 1:NON\n");
467 printf("select message type : ");
471 CAMessageType_t msgType = (buf[0] == '0' || buf[0] == '1') ? buf[0] - '0' : 0;
474 CAToken_t token = NULL;
475 if (CA_STATUS_OK != CAGenerateToken(&token))
477 printf("token generate error!!\n");
481 printf("generated token %s\n", (token != NULL) ? token : "");
483 // extract relative resourceuri from give uri
484 char resourceURI[15] = {0};
486 printf("URI : %s\n", uri);
487 get_resource_uri(uri, resourceURI, 14);
489 // create request data
490 CAInfo_t requestData;
491 memset(&requestData, 0, sizeof(CAInfo_t));
492 requestData.token = token;
493 if ('1' == secureRequest[0])
495 int length = strlen(resourceURI) + 1;
496 requestData.payload = (CAPayload_t) malloc(length);
497 sprintf(requestData.payload, gSecureInfoData, resourceURI, gLocalSecurePort);
501 int length = strlen(gNormalInfoData) + strlen(resourceURI) + 1;
502 requestData.payload = (CAPayload_t) malloc(length);
503 sprintf(requestData.payload, gNormalInfoData, resourceURI);
505 requestData.type = msgType;
507 CARequestInfo_t requestInfo;
508 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
509 requestInfo.method = CA_GET;
510 requestInfo.info = requestData;
513 CASendRequest(endpoint, &requestInfo);
517 CADestroyToken(token);
520 // destroy remote endpoint
521 CADestroyRemoteEndpoint(endpoint);
522 printf("=============================================\n");
525 void send_secure_request()
527 char uri[MAX_BUF_LEN];
528 char ipv4addr[CA_IPADDR_SIZE];
530 printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");
532 fgets(ipv4addr, CA_IPADDR_SIZE, stdin);
533 snprintf(uri, MAX_BUF_LEN, "coaps://%s:5684/a/light", ipv4addr);
535 printf("\n=============================================\n");
536 // create remote endpoint
537 CARemoteEndpoint_t *endpoint = NULL;
538 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, CA_ETHERNET, &endpoint))
540 printf("Failed to create remote endpoint!\n");
545 CAToken_t token = NULL;
546 if (CA_STATUS_OK != CAGenerateToken(&token))
548 printf("Failed to generate token !\n");
552 // create request data
553 CAMessageType_t msgType = CA_MSG_NONCONFIRM;
554 CAInfo_t requestData;
555 memset(&requestData, 0, sizeof(CAInfo_t));
556 requestData.token = token;
557 requestData.type = msgType;
559 CARequestInfo_t requestInfo;
560 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
561 requestInfo.method = CA_GET;
562 requestInfo.info = requestData;
565 CASendRequest(endpoint, &requestInfo);
569 CADestroyToken(token);
570 CADestroyRemoteEndpoint(endpoint);
571 printf("=============================================\n");
575 void send_request_all()
577 char buf[MAX_BUF_LEN];
578 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
582 res = get_network_type();
583 if (res != CA_STATUS_OK)
588 printf("\n=============================================\n");
589 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
590 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
595 // create remote endpoint
596 CARemoteEndpoint_t *endpoint = NULL;
597 res = CACreateRemoteEndpoint(buf, gSelectedNwType, &endpoint);
599 if (res != CA_STATUS_OK)
601 printf("create remote endpoint error!!\n");
602 CADestroyRemoteEndpoint(endpoint);
607 CAGroupEndpoint_t *group = NULL;
608 group = (CAGroupEndpoint_t *)malloc(sizeof(CAGroupEndpoint_t));
609 group->connectivityType = endpoint->connectivityType;
610 group->resourceUri = endpoint->resourceUri;
614 CAToken_t token = NULL;
615 res = CAGenerateToken(&token);
617 if (res != CA_STATUS_OK)
619 printf("token generate error!!\n");
623 printf("generated token %s\n", (token != NULL) ? token : "");
625 CAInfo_t requestData;
626 memset(&requestData, 0, sizeof(CAInfo_t));
627 requestData.token = token;
628 requestData.payload = "Temp Json Payload";
630 CARequestInfo_t requestInfo;
631 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
632 requestInfo.method = CA_GET;
633 requestInfo.info = requestData;
636 // CASendRequest(endpoint, &requestInfo);
637 CASendRequestToAll(group, &requestInfo);
641 CADestroyToken(token);
644 // destroy remote endpoint
645 if (endpoint != NULL)
647 CADestroyRemoteEndpoint(endpoint);
652 printf("=============================================\n");
655 void advertise_resource()
657 char buf[MAX_BUF_LEN];
659 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
661 printf("\n=============================================\n");
667 char optionData[MAX_OPT_LEN];
669 printf("Option Num : ");
670 scanf("%d", &optionNum);
671 CAHeaderOption_t *headerOpt;
672 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
673 if (NULL == headerOpt)
675 printf("Memory allocation failed!\n");
678 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
681 for (i = 0; i < optionNum; i++)
684 printf("[%d] Option ID : ", i + 1);
685 scanf("%d", &optionID);
686 headerOpt[i].optionID = optionID;
688 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
689 printf("[%d] Option Data : ", i + 1);
690 scanf("%s", optionData);
691 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
692 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
694 headerOpt[i].optionLength = (uint16_t) strlen(optionData);
696 printf("\n=============================================\n");
699 CAToken_t token = NULL;
700 CAResult_t res = CAGenerateToken(&token);
701 if (res != CA_STATUS_OK)
703 printf("token generate error!!\n");
707 printf("generated token %s\n", (token != NULL) ? token : "");
709 CAAdvertiseResource(buf, token, headerOpt, (uint8_t) optionNum);
715 CADestroyToken(token);
723 void send_notification()
725 char buf[MAX_BUF_LEN];
726 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
730 res = get_network_type();
731 if (res != CA_STATUS_OK)
736 printf("\n=============================================\n");
737 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
738 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
743 // create remote endpoint
744 CARemoteEndpoint_t *endpoint = NULL;
745 res = CACreateRemoteEndpoint(buf, gSelectedNwType, &endpoint);
747 if (res != CA_STATUS_OK)
749 printf("create remote endpoint error!!\n");
750 CADestroyRemoteEndpoint(endpoint);
754 CAInfo_t respondeData;
755 memset(&respondeData, 0, sizeof(CAInfo_t));
756 respondeData.token = "client token";
757 respondeData.payload = "Temp Notification Data";
759 CAResponseInfo_t responseInfo;
760 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
761 responseInfo.result = CA_SUCCESS;
762 responseInfo.info = respondeData;
765 res = CASendNotification(endpoint, &responseInfo);
766 if (res != CA_STATUS_OK)
768 printf("send notification error\n");
772 printf("send notification success\n");
775 // destroy remote endpoint
776 if (endpoint != NULL)
778 CADestroyRemoteEndpoint(endpoint);
781 printf("\n=============================================\n");
784 void select_network()
786 char buf[MAX_BUF_LEN];
788 printf("\n=============================================\n");
789 printf("\tselect network\n");
790 printf("ETHERNET : 0\n");
791 printf("WIFI : 1\n");
796 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
799 int number = buf[0] - '0';
801 number = (number < 0 || number > 3) ? 0 : number;
803 CAResult_t res = CASelectNetwork(1 << number);
804 if (res != CA_STATUS_OK)
806 printf("select network error\n");
807 gSelectedNwType = 1 << number;
811 printf("select network success\n");
814 printf("=============================================\n");
817 void unselect_network()
819 char buf[MAX_BUF_LEN];
821 printf("\n=============================================\n");
822 printf("\tunselect enabled network\n");
823 printf("ETHERNET : 0\n");
824 printf("WIFI : 1\n");
829 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
832 int number = buf[0] - '0';
834 number = (number < 0 || number > 3) ? 1 : number;
836 CAResult_t res = CAUnSelectNetwork(1 << number);
837 if (res != CA_STATUS_OK)
839 printf("unselect network error\n");
843 printf("unselect network success\n");
846 printf("=============================================\n");
851 char buf[MAX_BUF_LEN];
853 printf("\n=============================================\n");
854 printf("\t\tMenu\n");
855 printf("\ts : start server\n");
856 printf("\tc : start client\n");
857 printf("\tf : find resource\n");
858 printf("\tr : send request\n");
859 printf("\tt : send request to all\n");
860 printf("\ta : advertise resource\n");
861 printf("\tb : send notification\n");
862 printf("\tn : select network\n");
863 printf("\tx : unselect network\n");
864 printf("\tg : get network information\n");
865 printf("\th : handle request response\n");
866 printf("\ty : run static client\n");
867 printf("\tz : run static server\n");
868 printf("\tw : send secure request\n");
869 printf("\tq : quit\n");
870 printf("=============================================\n");
873 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
880 void handle_request_response()
882 printf("handle_request_response\n");
884 CAResult_t res = CAHandleRequestResponse();
885 if (res != CA_STATUS_OK)
887 printf("handle request error\n");
891 printf("handle request success\n");
895 void get_network_info()
899 CALocalConnectivity_t *tempInfo = NULL;
900 uint32_t tempSize = 0;
902 if((CAGetNetworkInformation(&tempInfo, &tempSize) != CA_STATUS_OK) ||
903 !tempInfo || !tempSize)
905 printf("network not connected\n");
909 printf("################## Network Information #######################\n");
910 printf("network info total size is %d\n\n", tempSize);
911 for (index = 0; index < tempSize; index++)
913 printf("Type: %d\n", tempInfo[index].type);
914 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
915 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
916 printf("Secured: %d\n\n", tempInfo[index].isSecured);
918 if (CA_TRUE == tempInfo[index].isSecured)
920 gLocalSecurePort = tempInfo[index].addressInfo.IP.port;
924 gLocalUnicastPort = tempInfo[index].addressInfo.IP.port;
929 printf("##############################################################");
932 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
936 printf("Remote endpoint is NULL!");
942 printf("Request info is NULL!");
946 printf("##########received request from remote device #############\n");
947 printf("Uri: %s\n", object->resourceUri);
948 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
949 object->addressInfo.IP.port, object->isSecured);
951 printf("Data: %s\n", requestInfo->info.payload);
953 if (gLastRequestToken != NULL && requestInfo->info.token != NULL
954 && (strcmp((char *)gLastRequestToken, requestInfo->info.token) == 0))
956 printf("token is same. received request of it's own. skip.. \n");
960 if (requestInfo->info.options)
962 uint32_t len = requestInfo->info.numOptions;
964 for (i = 0; i < len; i++)
966 printf("Option %d\n", i + 1);
967 printf("ID : %d\n", requestInfo->info.options[i].optionID);
968 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
969 requestInfo->info.options[i].optionData);
972 printf("############################################################\n");
974 //Check if this has secure communication information
975 if (requestInfo->info.payload)
977 int securePort = get_secure_information(requestInfo->info.payload);
978 if (0 < securePort) //Set the remote endpoint secure details and send response
980 printf("This is secure resource...\n");
984 length = 8; //length of "coaps://"
985 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
986 length += strlen(object->resourceUri) + 1;
988 uri = calloc(1, sizeof(char) * length);
991 printf("Failed to create new uri\n");
994 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
995 securePort, object->resourceUri);
997 CARemoteEndpoint_t *endpoint = NULL;
998 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
1000 printf("Failed to create duplicate of remote endpoint!\n");
1003 //endpoint->connectivityType = object->connectivityType;
1004 endpoint->isSecured = CA_TRUE;
1009 printf("send response with URI\n");
1010 send_response(object, (requestInfo != NULL) ? &requestInfo->info : NULL);
1015 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1017 printf("##########Received response from remote device #############\n");
1018 printf("Uri: %s\n", object->resourceUri);
1019 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1020 object->addressInfo.IP.port, object->isSecured);
1021 printf("response result : %d\n", responseInfo->result);
1022 printf("Data: %s\n", responseInfo->info.payload);
1024 if (responseInfo->info.options)
1026 uint32_t len = responseInfo->info.numOptions;
1028 for (i = 0; i < len; i++)
1030 printf("Option %d\n", i + 1);
1031 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1032 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1033 responseInfo->info.options[i].optionData);
1036 printf("############################################################\n");
1039 //Check if this has secure communication information
1040 if (responseInfo->info.payload)
1042 int securePort = get_secure_information(responseInfo->info.payload);
1043 if (0 < securePort) //Set the remote endpoint secure details and send response
1045 printf("This is secure resource...\n");
1050 void send_response(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1052 printf("entering send_response\n");
1054 CAInfo_t responseData;
1055 memset(&responseData, 0, sizeof(CAInfo_t));
1058 ((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) :
1060 responseData.messageId = (info != NULL) ? info->messageId : 0;
1061 responseData.token = (info != NULL) ? info->token : "";
1062 responseData.payload = "response payload";
1064 CAResponseInfo_t responseInfo;
1065 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
1066 responseInfo.result = 203;
1067 responseInfo.info = responseData;
1069 if (CA_TRUE == endpoint->isSecured)
1070 printf("Sending response on secure communication\n");
1072 printf("Sending response on non-secure communication\n");
1074 // send request (connectivityType from remoteEndpoint of request Info)
1075 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1076 if (res != CA_STATUS_OK)
1078 printf("send response error\n");
1082 printf("send response success\n");
1085 printf("=============================================\n");
1088 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
1091 printf("\n=============================================\n");
1093 CAInfo_t requestData;
1094 memset(&requestData, 0, sizeof(CAInfo_t));
1095 requestData.token = token;
1096 requestData.payload = "Temp Json Payload";
1098 CARequestInfo_t requestInfo;
1099 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1100 requestInfo.method = CA_GET;
1101 requestInfo.info = requestData;
1104 endpoint->connectivityType = CA_WIFI;
1106 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1107 if (res != CA_STATUS_OK)
1109 printf("send request error\n");
1113 printf("send request success\n");
1116 printf("=============================================\n");
1120 int get_secure_information(CAPayload_t payLoad)
1122 printf("entering get_secure_information\n");
1126 printf("Payload is NULL\n");
1130 char *subString = NULL;
1131 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1133 printf("This is not secure resource\n");
1137 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1139 printf("This secure resource does not have port information\n");
1143 char *startPos = strstr(subString, ":");
1146 printf("Parsing failed !\n");
1150 char *endPos = strstr(startPos, "}");
1153 printf("Parsing failed !\n");
1157 char portStr[4] = {0};
1158 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1160 printf("secured port is: %s\n", portStr);
1161 return atoi(portStr);
1164 void get_resource_uri(char *URI, char *resourceURI, int length)
1166 char *startPos = URI;
1168 if (NULL != (temp = strstr(URI, "://")))
1170 startPos = strchr(temp + 3, '/');
1173 printf("Resource URI is missing\n");
1178 char *endPos = strchr(startPos, '?');
1181 endPos = URI + strlen(URI);
1185 if (endPos - startPos <= length)
1186 memcpy(resourceURI, startPos + 1, endPos - startPos);
1188 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1191 CAResult_t get_network_type()
1193 char buf[MAX_BUF_LEN];
1195 printf("\n=============================================\n");
1196 printf("\tselect network type\n");
1197 printf("ETHERNET : 0\n");
1198 printf("WIFI : 1\n");
1199 printf("EDR : 2\n");
1201 printf("select : ");
1203 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
1206 int number = buf[0] - '0';
1208 number = (number < 0 || number > 3) ? 0 : 1 << number;
1210 if (!(number & 0xf))
1212 return CA_NOT_SUPPORTED;
1214 if (number & CA_ETHERNET)
1216 gSelectedNwType = CA_ETHERNET;
1217 return CA_STATUS_OK;
1219 if (number & CA_WIFI)
1221 gSelectedNwType = CA_WIFI;
1222 return CA_STATUS_OK;
1224 if (number & CA_EDR)
1226 gSelectedNwType = CA_EDR;
1227 return CA_STATUS_OK;
1231 gSelectedNwType = CA_LE;
1232 return CA_STATUS_OK;
1235 printf("\n=============================================\n");