1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
31 #include "cainterface.h"
33 static GMainLoop *mainloop = NULL;
37 #define MAX_BUF_LEN 1024
38 #define MAX_OPT_LEN 16
45 #define IDENTITY ("1111111111111111")
49 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
53 int g_localUnicastPort = 0;
54 int g_localSecurePort = 0;
55 CAConnectivityType_t g_selectedNwType = CA_ETHERNET;
56 const char* g_messageType[] = {"CON", "NON", "ACK", "RESET"};
60 CAResult_t GetNetworkType();
62 CAResult_t Initialize();
63 void StartListeningServer();
64 void StartDiscoveryServer();
67 void SendRequestAll();
69 void AdvertiseResource();
70 void SendNotification();
72 void UnselectNetwork();
73 void HandleRequestResponse();
74 void FindFixedResource();
75 void GetNetworkInfo();
77 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
78 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
79 void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token);
80 void GetResourceUri(char *URI, char *resourceURI, int length);
81 int GetSecureInformation(CAPayload_t payLoad);
83 static CAToken_t g_lastRequestToken = NULL;
84 static const char g_secureInfoData[] = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
85 "\"if\":[\"oc.mi.def\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
86 static const char g_normalInfoData[] = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
87 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
90 static OCDtlsPskCredsBlob *pskCredsBlob = NULL;
92 void ClearDtlsCredentialInfo()
94 printf("clearDtlsCredentialInfo IN\n");
97 // Initialize sensitive data to zeroes before freeing.
98 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
99 free(pskCredsBlob->creds);
101 memset(pskCredsBlob, 0, sizeof(OCDtlsPskCredsBlob));
105 printf("clearDtlsCredentialInfo OUT\n");
108 // Internal API. Invoked by OC stack to retrieve credentials from this module
109 void CAGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo)
111 printf("CAGetDtlsPskCredentials IN\n");
115 printf("Memory is not allocated for credInfo\n");
119 *credInfo = pskCredsBlob;
121 printf("CAGetDtlsPskCredentials OUT\n");
124 bool SetCredentials()
126 printf("SetCredentials IN\n");
127 pskCredsBlob = (OCDtlsPskCredsBlob *)malloc(sizeof(OCDtlsPskCredsBlob));
131 printf("Memory allocation is failed\n");
135 memset(pskCredsBlob, 0x0, sizeof(OCDtlsPskCredsBlob));
136 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
138 pskCredsBlob->num = 1;
140 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
142 if(!pskCredsBlob->creds)
144 printf("Memory allocation is failed\n");
149 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
150 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
152 printf("SetCredentials OUT\n");
159 g_main_loop_run(mainloop);
166 printf("\n=============================================\n");
167 printf("\t\tsample main\n");
168 printf("=============================================\n");
170 CAResult_t res = CA_STATUS_FAILED;
172 * Read DTLS PSK credentials from persistent storage and
173 * set in the OC stack.
176 if (SetCredentials() == 0)
178 printf("SetCredentials failed\n");
182 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
183 if(CA_STATUS_OK != res)
185 printf("Set credential handler fail\n");
190 if (CA_STATUS_OK != res)
192 printf("CAInitialize fail\n");
197 CARegisterHandler(RequestHandler, ResponseHandler);
201 if (g_lastRequestToken != NULL)
203 CADestroyToken(g_lastRequestToken);
205 g_lastRequestToken = NULL;
209 ClearDtlsCredentialInfo();
218 char menu = toupper(PrintOptions());
226 case 'Q': // quits the sample program
227 printf("quit..!!\n");
228 g_main_loop_quit(mainloop);
231 case 's': // start server
232 case 'S': // start server
233 StartListeningServer();
236 case 't': // send request
241 case 'c': // start client
243 StartDiscoveryServer();
246 case 'f': // find resource
251 case 'r': // send request
256 case 'a': // advertise resource
261 case 'b': // send notification
266 case 'n': // select network
271 case 'x': // unselect network
276 case 'h': // handle request response
278 HandleRequestResponse();
287 while (g_received == 0)
290 HandleRequestResponse();
297 StartListeningServer();
301 HandleRequestResponse();
305 case 'g': // get network information
311 printf("not supported menu!!\n");
318 CAResult_t Initialize()
320 CAResult_t res = CA_STATUS_FAILED;
321 mainloop = g_main_loop_new(NULL, FALSE);
324 printf("g_main_loop_new failed\n");
328 int result = pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
331 printf("pthread_create failed in initialize\n");
335 res = CAInitialize();
336 if (res != CA_STATUS_OK)
338 printf("CAInitialize fail\n");
343 void StartListeningServer()
345 printf("start listening server!!\n");
347 CAResult_t res = CAStartListeningServer();
348 if (res != CA_STATUS_OK)
350 printf("start listening server fail\n");
354 printf("start listening server success\n");
358 void StartDiscoveryServer()
360 printf("start discovery client!!\n");
362 CAResult_t res = CAStartDiscoveryServer();
363 if (res != CA_STATUS_OK)
365 printf("start discovery client fail\n");
369 printf("start discovery client success\n");
373 void FindFixedResource()
375 char buf[] ="/a/light";
378 CAToken_t token = NULL;
379 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
381 CAResult_t res = CAGenerateToken(&token, tokenLength);
382 if ((CA_STATUS_OK != res) || (!token))
384 printf("token generate error!!");
388 printf("Generated token %s\n", token);
390 res = CAFindResource(buf, token, tokenLength);
391 if (res != CA_STATUS_OK)
393 printf("find resource error : %d\n", res);
397 printf("find resource to %s URI\n", buf);
401 CADestroyToken(token);
403 printf("=============================================\n");
408 char buf[MAX_BUF_LEN] = {0};
410 printf("\n=============================================\n");
411 printf("ex) /a/light\n");
412 printf("reference uri : ");
414 if(!fgets(buf, MAX_BUF_LEN, stdin))
416 printf("fgets error\n");
421 CAToken_t token = NULL;
422 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
424 CAResult_t res = CAGenerateToken(&token, tokenLength);
425 if ((CA_STATUS_OK != res) || (!token))
427 printf("token generate error!!\n");
431 printf("Generated token %s\n", token);
433 res = CAFindResource(buf, token, tokenLength);
434 if (res != CA_STATUS_OK)
436 printf("find resource error : %d\n", res);
437 CADestroyToken(token);
441 printf("find resource to %s URI\n", buf);
443 CADestroyToken(g_lastRequestToken);
445 g_lastRequestToken = token;
448 printf("=============================================\n");
453 char secureRequest[2] = {0};
454 CAResult_t res = CA_STATUS_FAILED;
455 char uri[MAX_BUF_LEN] = {'\0'};
457 res = GetNetworkType();
458 if (res != CA_STATUS_OK)
463 printf("Do you want to send secure request ?.... enter (0/1): \n");
464 if(!fgets(secureRequest, 1, stdin))
466 printf("fgets error\n");
470 if ('1' == secureRequest[0])
472 printf("Enter the URI like below....\n");
473 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
478 printf("Enter the URI like below....\n");
479 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
480 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
484 if(!fgets(uri, MAX_BUF_LEN, stdin))
486 printf("fgets error\n");
490 // create remote endpoint
491 CARemoteEndpoint_t *endpoint = NULL;
492 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, g_selectedNwType, &endpoint)
495 printf("Failed to create remote endpoint!\n");
496 CADestroyRemoteEndpoint(endpoint);
500 char buf[MAX_BUF_LEN]={0};
502 printf("\n=============================================\n");
503 printf("0:CON, 1:NON\n");
504 printf("select message type : ");
506 if(!fgets(buf, MAX_BUF_LEN, stdin))
508 printf("fgets error\n");
509 CADestroyRemoteEndpoint(endpoint);
513 CAMessageType_t msgType = (buf[0] == '0' || buf[0] == '1') ? buf[0] - '0' : 0;
516 CAToken_t token = NULL;
517 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
519 res = CAGenerateToken(&token, tokenLength);
520 if ((CA_STATUS_OK != res) || (!token))
522 printf("token generate error, error code : %d\n", res);
526 printf("Generated token %s\n", token);
528 // extract relative resourceuri from give uri
529 char resourceURI[15] = {0};
531 printf("URI : %s\n", uri);
532 GetResourceUri(uri, resourceURI, 14);
534 // create request data
535 CAInfo_t requestData = {CA_MSG_RESET};
536 requestData.token = token;
537 if ('1' == secureRequest[0])
539 int length = strlen(g_secureInfoData) + strlen(resourceURI) + 1;
540 requestData.payload = (CAPayload_t) malloc(length);
541 if(!requestData.payload)
543 printf("Memory allocation is failed\n");
544 CADestroyToken(token);
545 CADestroyRemoteEndpoint(endpoint);
548 sprintf(requestData.payload, g_secureInfoData, resourceURI, g_localSecurePort);
552 int length = strlen(g_normalInfoData) + strlen(resourceURI) + 1;
553 requestData.payload = (CAPayload_t) malloc(length);
554 if(!requestData.payload)
556 printf("Memory allocation is failed\n");
557 CADestroyToken(token);
558 CADestroyRemoteEndpoint(endpoint);
561 sprintf(requestData.payload, g_normalInfoData, resourceURI);
563 requestData.type = msgType;
565 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
566 requestInfo.method = CA_GET;
567 requestInfo.info = requestData;
570 CASendRequest(endpoint, &requestInfo);
572 CADestroyToken(token);
574 free(requestData.payload);
576 // destroy remote endpoint
577 CADestroyRemoteEndpoint(endpoint);
578 printf("=============================================\n");
581 void SendRequestAll()
583 char buf[MAX_BUF_LEN] = {0};
585 CAResult_t res = CA_STATUS_FAILED;
587 res = GetNetworkType();
588 if (res != CA_STATUS_OK)
593 printf("\n=============================================\n");
594 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
595 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
598 if(!fgets(buf, MAX_BUF_LEN, stdin))
600 printf("fgets error\n");
604 // create remote endpoint
605 CARemoteEndpoint_t *endpoint = NULL;
606 res = CACreateRemoteEndpoint(buf, g_selectedNwType, &endpoint);
608 if (res != CA_STATUS_OK)
610 printf("create remote endpoint error!!\n");
611 CADestroyRemoteEndpoint(endpoint);
615 CAGroupEndpoint_t *group = NULL;
616 group = (CAGroupEndpoint_t *)malloc(sizeof(CAGroupEndpoint_t));
619 printf("Memory allocation failed!\n");
620 CADestroyRemoteEndpoint(endpoint);
623 group->connectivityType = endpoint->connectivityType;
624 group->resourceUri = endpoint->resourceUri;
627 CAToken_t token = NULL;
628 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
630 res = CAGenerateToken(&token, tokenLength);
631 if ((CA_STATUS_OK != res) || (!token))
633 printf("token generate error!!\n");
637 printf("generated token %s\n", token);
639 CAInfo_t requestData = {CA_MSG_RESET};
640 requestData.token = token;
641 requestData.tokenLength = tokenLength;
642 requestData.payload = "Temp Json Payload";
643 requestData.type = CA_MSG_NONCONFIRM;
645 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
646 requestInfo.method = CA_GET;
647 requestInfo.info = requestData;
650 CASendRequestToAll(group, &requestInfo);
652 CADestroyToken(token);
654 // destroy remote endpoint
655 CADestroyRemoteEndpoint(endpoint);
659 printf("=============================================\n");
662 void AdvertiseResource()
664 char buf[MAX_BUF_LEN] = {0};
666 printf("\n=============================================\n");
669 if(!fgets(buf, MAX_BUF_LEN, stdin))
671 printf("fgets error\n");
676 char optionData[MAX_OPT_LEN] = {0};
678 printf("Option Num : ");
679 scanf("%d", &optionNum);
680 CAHeaderOption_t *headerOpt = NULL;
681 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
682 if (NULL == headerOpt)
684 printf("Memory allocation failed!\n");
687 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
690 for (i = 0 ; i < optionNum ; i++)
693 printf("[%d] Option ID : ", i + 1);
694 scanf("%d", &optionID);
695 headerOpt[i].optionID = optionID;
697 printf("[%d] Option Data : ", i + 1);
698 scanf("%s", optionData);
699 int len = strlen(optionData);
700 memcpy(headerOpt[i].optionData, optionData, len + 1);
701 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
703 headerOpt[i].optionLength = (uint16_t)len;
705 printf("\n=============================================\n");
708 CAToken_t token = NULL;
709 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
711 CAResult_t res = CAGenerateToken(&token, tokenLength);
712 if ((CA_STATUS_OK != res) || (!token))
714 printf("token generate error!!\n");
718 printf("generated token %s\n", token);
720 CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t)optionNum);
723 CADestroyToken(token);
728 void SendNotification()
730 char buf[MAX_BUF_LEN] = {0};
732 CAResult_t res = CA_STATUS_FAILED;
734 res = GetNetworkType();
735 if (res != CA_STATUS_OK)
740 printf("\n=============================================\n");
741 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
742 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
745 if(!fgets(buf, MAX_BUF_LEN, stdin))
747 printf("fgets error\n");
751 // create remote endpoint
752 CARemoteEndpoint_t *endpoint = NULL;
753 res = CACreateRemoteEndpoint(buf, g_selectedNwType, &endpoint);
754 if (res != CA_STATUS_OK)
756 printf("create remote endpoint error!!\n");
757 CADestroyRemoteEndpoint(endpoint);
761 CAInfo_t respondData = {CA_MSG_RESET};
762 respondData.tokenLength = CA_MAX_TOKEN_LEN;
764 res = CAGenerateToken(&respondData.token, respondData.tokenLength);
765 if ((CA_STATUS_OK != res) || (!respondData.token))
767 printf("token generate error!!\n");
771 respondData.payload = "Temp Notification Data";
773 CAResponseInfo_t responseInfo = {0};
774 responseInfo.result = CA_SUCCESS;
775 responseInfo.info = respondData;
778 res = CASendNotification(endpoint, &responseInfo);
779 if (res != CA_STATUS_OK)
781 printf("send notification error, error code: %d\n", res);
785 printf("send notification success\n");
789 CADestroyToken(respondData.token);
790 // destroy remote endpoint
791 CADestroyRemoteEndpoint(endpoint);
793 printf("\n=============================================\n");
797 char buf[MAX_BUF_LEN] = {0};
799 printf("\n=============================================\n");
800 printf("\tselect network\n");
801 printf("ETHERNET : 0\n");
802 printf("WIFI : 1\n");
807 if(!fgets(buf, MAX_BUF_LEN, stdin))
809 printf("fgets error\n");
813 int number = buf[0] - '0';
815 number = (number < 0 || number > 3) ? 0 : number;
817 CAResult_t res = CASelectNetwork(1 << number);
818 if (res != CA_STATUS_OK)
820 printf("select network error\n");
821 g_selectedNwType = 1 << number;
825 printf("select network success\n");
828 printf("=============================================\n");
831 void UnselectNetwork()
833 char buf[MAX_BUF_LEN] = {0};
835 printf("\n=============================================\n");
836 printf("\tunselect enabled network\n");
837 printf("ETHERNET : 0\n");
838 printf("WIFI : 1\n");
843 if(!fgets(buf, MAX_BUF_LEN, stdin))
845 printf("fgets error\n");
849 int number = buf[0] - '0';
851 number = (number < 0 || number > 3) ? 1 : number;
853 CAResult_t res = CAUnSelectNetwork(1 << number);
854 if (res != CA_STATUS_OK)
856 printf("unselect network error\n");
860 printf("unselect network success\n");
862 printf("=============================================\n");
867 char buf[MAX_BUF_LEN] = {0};
869 printf("\n=============================================\n");
870 printf("\t\tMenu\n");
871 printf("\tm : show menu\n");
872 printf("\ts : start server\n");
873 printf("\tc : start client\n");
874 printf("\tf : find resource\n");
875 printf("\tr : send request\n");
876 printf("\tt : send request to all\n");
877 printf("\ta : advertise resource\n");
878 printf("\tb : send notification\n");
879 printf("\tn : select network\n");
880 printf("\tx : unselect network\n");
881 printf("\tg : get network information\n");
882 printf("\th : handle request response\n");
883 printf("\ty : run static client\n");
884 printf("\tz : run static server\n");
885 printf("\tq : quit\n");
886 printf("=============================================\n");
889 if(!fgets(buf, MAX_BUF_LEN, stdin))
891 printf("fgets error\n");
898 void HandleRequestResponse()
900 printf("handle_request_response\n");
902 CAResult_t res = CAHandleRequestResponse();
903 if (res != CA_STATUS_OK)
905 printf("handle request error\n");
909 printf("handle request success\n");
913 void GetNetworkInfo()
916 CALocalConnectivity_t *tempInfo = NULL;
917 uint32_t tempSize = 0;
919 CAGetNetworkInformation(&tempInfo, &tempSize);
920 if (tempInfo == NULL || tempSize <= 0)
922 printf("network not connected\n");
926 printf("################## Network Information #######################\n");
927 printf("network info total size is %d\n\n", tempSize);
929 for (index = 0; index < tempSize; index++)
931 printf("Type: %d\n", tempInfo[index].type);
932 printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
933 printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
934 printf("Secured: %d\n\n", tempInfo[index].isSecured);
936 if (true == tempInfo[index].isSecured)
938 g_localSecurePort = tempInfo[index].addressInfo.IP.port;
942 g_localUnicastPort = tempInfo[index].addressInfo.IP.port;
949 printf("##############################################################");
952 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
954 printf("##########received request from remote device #############\n");
957 printf("Remote endpoint is NULL!\n");
963 printf("Request info is NULL!\n");
967 printf("Uri: %s\n", object->resourceUri);
968 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
969 object->addressInfo.IP.port, object->isSecured);
971 printf("Data: %s\n", requestInfo->info.payload);
972 printf("Message type: %s\n", g_messageType[requestInfo->info.type]);
973 if (g_lastRequestToken != NULL && requestInfo->info.token != NULL
974 && (strcmp((char *)g_lastRequestToken, requestInfo->info.token) == 0))
976 printf("token is same. received request of it's own. skip.. \n");
980 if (requestInfo->info.options)
982 uint32_t len = requestInfo->info.numOptions;
984 for (i = 0; i < len; i++)
986 printf("Option %d\n", i + 1);
987 printf("ID : %d\n", requestInfo->info.options[i].optionID);
988 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
989 requestInfo->info.options[i].optionData);
992 printf("############################################################\n");
994 //Check if this has secure communication information
995 if (requestInfo->info.payload)
997 int securePort = GetSecureInformation(requestInfo->info.payload);
998 if (0 < securePort) //Set the remote endpoint secure details and send response
1000 printf("This is secure resource...\n");
1004 length = 8; //length of "coaps://"
1005 length += strlen(object->addressInfo.IP.ipAddress) + 5; // length of "ipaddress:port"
1006 length += strlen(object->resourceUri) + 1;
1008 uri = calloc(1, sizeof(char) * length);
1011 printf("Failed to create new uri\n");
1014 sprintf(uri, "coaps://%s:%d/%s", object->addressInfo.IP.ipAddress,
1015 securePort, object->resourceUri);
1017 CARemoteEndpoint_t *endpoint = NULL;
1018 if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->connectivityType, &endpoint))
1020 printf("Failed to create duplicate of remote endpoint!\n");
1023 //endpoint->connectivityType = object->connectivityType;
1024 endpoint->isSecured = true;
1031 printf("send response with URI\n");
1032 SendResponse(object, &requestInfo->info);
1037 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1039 printf("##########Received response from remote device #############\n");
1042 printf("response_handler object is null\n");
1048 printf("response_handler responseInfo is null\n");
1052 printf("Uri: %s\n", object->resourceUri);
1053 printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
1054 object->addressInfo.IP.port, object->isSecured);
1055 printf("response result : %d\n", responseInfo->result);
1056 printf("Data: %s\n", responseInfo->info.payload);
1057 printf("Message type: %s\n", g_messageType[responseInfo->info.type]);
1058 printf("Token: %s\n", responseInfo->info.token);
1059 if (responseInfo->info.options)
1061 uint32_t len = responseInfo->info.numOptions;
1063 for (i = 0; i < len; i++)
1065 printf("Option %d\n", i + 1);
1066 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1067 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1068 responseInfo->info.options[i].optionData);
1071 printf("############################################################\n");
1074 //Check if this has secure communication information
1075 if (responseInfo->info.payload)
1077 int securePort = GetSecureInformation(responseInfo->info.payload);
1078 if (0 < securePort) //Set the remote endpoint secure details and send response
1080 printf("This is secure resource...\n");
1085 void SendResponse(CARemoteEndpoint_t *endpoint, CAInfo_t *info)
1087 printf("entering send_response\n");
1091 printf("endpoint is null\n");
1095 CAInfo_t responseData = {CA_MSG_RESET};
1098 ((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) :
1100 responseData.messageId = 0;
1101 responseData.token = "";
1104 responseData.messageId = info->messageId;
1105 responseData.token = info->token;
1106 responseData.tokenLength = info->tokenLength;
1108 responseData.payload = "response payload";
1110 CAResponseInfo_t responseInfo = {0};
1111 responseInfo.result = 203;
1112 responseInfo.info = responseData;
1114 if (true == endpoint->isSecured)
1116 printf("Sending response on secure communication\n");
1120 printf("Sending response on non-secure communication\n");
1123 // send request (connectivityType from remoteEndpoint of request Info)
1124 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1125 if (res != CA_STATUS_OK)
1127 printf("send response failed\n");
1131 printf("send response success\n");
1134 printf("=============================================\n");
1137 void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token, uint8_t tokenLength)
1139 printf("\n=============================================\n");
1143 printf("endpoint is null\n");
1147 CAInfo_t requestData ={CA_MSG_RESET};
1148 requestData.token = token;
1149 requestData.tokenLength = tokenLength;
1150 requestData.payload = "Temp Json Payload";
1152 CARequestInfo_t requestInfo ={CA_GET, {CA_MSG_RESET}};
1153 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1154 requestInfo.method = CA_GET;
1155 requestInfo.info = requestData;
1158 endpoint->connectivityType = CA_WIFI;
1160 CAResult_t res = CASendRequest(endpoint, &requestInfo);
1161 if (res != CA_STATUS_OK)
1163 printf("send request failed\n");
1167 printf("send request success\n");
1170 printf("=============================================\n");
1174 int GetSecureInformation(CAPayload_t payLoad)
1176 printf("entering get_secure_information\n");
1180 printf("Payload is NULL\n");
1184 char *subString = NULL;
1185 if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
1187 printf("This is not secure resource\n");
1191 if (NULL == (subString = strstr(payLoad, "\"port\":")))
1193 printf("This secure resource does not have port information\n");
1197 char *startPos = strstr(subString, ":");
1200 printf("Parsing failed !\n");
1204 char *endPos = strstr(startPos, "}");
1207 printf("Parsing failed !\n");
1211 if(((endPos - 1) - startPos) > 4)
1213 printf("port length is not proper.Exceeding length 4\n");
1217 char portStr[4] = {0};
1218 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1220 printf("secured port is: %s\n", portStr);
1221 return atoi(portStr);
1224 void GetResourceUri(char *URI, char *resourceURI, int length)
1226 char *startPos = URI;
1228 if (NULL != (temp = strstr(URI, "://")))
1230 startPos = strchr(temp + 3, '/');
1233 printf("Resource URI is missing\n");
1238 char *endPos = strchr(startPos, '?');
1241 endPos = URI + strlen(URI);
1245 if (endPos - startPos <= length)
1246 memcpy(resourceURI, startPos + 1, endPos - startPos);
1248 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1251 CAResult_t GetNetworkType()
1253 char buf[MAX_BUF_LEN] = {0};
1255 printf("\n=============================================\n");
1256 printf("\tselect network type\n");
1257 printf("ETHERNET : 0\n");
1258 printf("WIFI : 1\n");
1259 printf("EDR : 2\n");
1261 printf("select : ");
1263 if(!fgets(buf, MAX_BUF_LEN, stdin))
1265 printf("fgets error\n");
1266 return CA_STATUS_FAILED;
1269 int number = buf[0] - '0';
1271 number = (number < 0 || number > 3) ? 0 : 1 << number;
1273 if (!(number & 0xf))
1275 return CA_NOT_SUPPORTED;
1278 if (number & CA_ETHERNET)
1280 g_selectedNwType = CA_ETHERNET;
1281 return CA_STATUS_OK;
1284 if (number & CA_WIFI)
1286 g_selectedNwType = CA_WIFI;
1287 return CA_STATUS_OK;
1290 if (number & CA_EDR)
1292 g_selectedNwType = CA_EDR;
1293 return CA_STATUS_OK;
1298 g_selectedNwType = CA_LE;
1299 return CA_STATUS_OK;
1302 printf("\n=============================================\n");
1304 return CA_STATUS_FAILED;