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"
31 #include "ocsecurityconfig.h"
34 #define MAX_BUF_LEN 1024
35 #define MAX_OPT_LEN 16
39 #define SECURE_DEFAULT_PORT 5684
41 #define RESOURCE_URI_LENGTH 14
43 #define SYSTEM_INVOKE_ERROR 127
44 #define SYSTEM_ERROR -1
50 #define IDENTITY ("1111111111111111")
54 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
57 uint16_t g_local_secure_port = SECURE_DEFAULT_PORT;
58 CATransportAdapter_t g_selected_nw_type = CA_ADAPTER_IP;
59 const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"};
63 char ipAddress[CA_IPADDR_SIZE];
69 CAResult_t get_network_type();
70 CAResult_t get_input_data(char *buf, int32_t length);
72 bool select_payload_type();
73 CAPayload_t get_binary_payload(size_t *payloadLength);
74 bool read_file(const char* name, CAPayload_t* bytes, size_t* length);
75 void create_file(CAPayload_t bytes, size_t length);
77 void start_listening_server();
78 void start_discovery_server();
80 void send_request_all();
81 void send_notification();
82 void select_network();
83 void unselect_network();
84 void handle_request_response();
85 void get_network_info();
86 void send_secure_request();
88 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
89 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
90 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
91 void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info);
92 void get_resource_uri(char *URI, char *resourceURI, int length);
93 int get_secure_information(CAPayload_t payLoad);
94 int get_address_set(const char *pAddress, addressSet_t* outAddress);
95 void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
96 CAHeaderOption_t* get_option_data(CAInfo_t* requestData);
98 static CAToken_t g_last_request_token = NULL;
100 static const char COAP_PREFIX[] = "coap://";
101 static const char COAPS_PREFIX[] = "coaps://";
102 static const uint16_t COAP_PREFIX_LEN = sizeof(COAP_PREFIX) - 1;
103 static const uint16_t COAPS_PREFIX_LEN = sizeof(COAPS_PREFIX) - 1;
105 static const char SECURE_INFO_DATA[] =
106 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
107 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":"
109 static const char NORMAL_INFO_DATA[] =
110 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
111 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
114 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
116 void clearDtlsCredentialInfo()
118 printf("clearDtlsCredentialInfo IN\n");
121 // Initialize sensitive data to zeroes before freeing.
122 if (pskCredsBlob->creds)
124 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
125 free(pskCredsBlob->creds);
128 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
132 printf("clearDtlsCredentialInfo OUT\n");
135 // Internal API. Invoked by CA stack to retrieve credentials from this module
136 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
138 printf("CAGetDtlsPskCredentials IN\n");
141 printf("Invalid credential container");
145 *credInfo = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
146 if (NULL == *credInfo)
148 printf("Failed to allocate credential blob.");
152 size_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
153 (*credInfo)->creds = (OCDtlsPskCreds *)malloc(credLen);
154 if (NULL == (*credInfo)->creds)
156 printf("Failed to allocate credentials.");
162 memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
163 (*credInfo)->num = pskCredsBlob->num;
164 memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
166 printf("CAGetDtlsPskCredentials OUT\n");
170 CAResult_t SetCredentials()
172 printf("SetCredentials IN\n");
173 pskCredsBlob = (CADtlsPskCredsBlob_t *)calloc(1, sizeof(CADtlsPskCredsBlob_t));
174 if (NULL == pskCredsBlob)
176 printf("Memory allocation failed!\n");
177 return CA_MEMORY_ALLOC_FAILED;
179 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
182 pskCredsBlob->num = 1;
184 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
185 if (NULL == pskCredsBlob->creds)
187 printf("Memory allocation failed!\n");
189 return CA_MEMORY_ALLOC_FAILED;
192 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
193 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
195 printf("SetCredentials OUT\n");
202 int ret = system("clear");
203 // shell invoke error: 127, others: -1
204 if (SYSTEM_INVOKE_ERROR == ret || SYSTEM_ERROR == ret)
206 printf("Terminal Clear Error: %d\n", ret);
210 printf("=============================================\n");
211 printf("\t\tsample main\n");
212 printf("=============================================\n");
214 CAResult_t res = CAInitialize();
215 if (CA_STATUS_OK != res)
217 printf("CAInitialize fail\n");
222 * Read DTLS PSK credentials from persistent storage and
223 * set in the OC stack.
226 res = SetCredentials();
227 if (CA_STATUS_OK != res)
229 printf("SetCredentials failed\n");
233 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
234 if (CA_STATUS_OK != res)
236 printf("Set credential handler fail\n");
242 CARegisterHandler(request_handler, response_handler, error_handler);
246 CADestroyToken(g_last_request_token);
248 g_last_request_token = NULL;
252 clearDtlsCredentialInfo();
261 char menu = get_menu();
271 printf("quit..!!\n");
274 case 's': // start server
276 start_listening_server();
279 case 't': // send request
284 case 'c': // start client
286 start_discovery_server();
289 case 'r': // send request
294 case 'b': // send notification
299 case 'n': // select network
304 case 'x': // unselect network
309 case 'h': // handle request response
311 handle_request_response();
317 start_discovery_server();
318 send_secure_request();
319 while (g_received == 0)
322 handle_request_response();
328 start_listening_server();
332 handle_request_response();
336 case 'g': // get network information
342 printf("not supported menu!!\n");
348 void start_listening_server()
350 printf("start listening server!!\n");
352 CAResult_t res = CAStartListeningServer();
353 if (CA_STATUS_OK != res)
355 printf("start listening server fail, error code : %d\n", res);
359 printf("start listening server success\n");
363 void start_discovery_server()
365 printf("start discovery client!!\n");
367 CAResult_t res = CAStartDiscoveryServer();
368 if (CA_STATUS_OK != res)
370 printf("start discovery client fail, error code : %d\n", res);
374 printf("start discovery client success\n");
378 bool select_payload_type()
380 char buf[MAX_BUF_LEN]={0};
381 printf("\n=============================================\n");
382 printf("Normal Payload : 0\nBig Payload : 1\n");
383 printf("select Payload type : ");
385 CAResult_t res = get_input_data(buf, sizeof(buf));
386 if (CA_STATUS_OK != res)
388 printf("Payload type selection error\n");
389 printf("Default: Using normal Payload\n");
393 return (buf[0] == '1') ? true : false;
396 CAPayload_t get_binary_payload(size_t *payloadLength)
398 CAPayload_t binaryPayload = NULL;
399 bool result = read_file("sample_input.txt", &binaryPayload, payloadLength);
405 return binaryPayload;
410 CAResult_t res = get_network_type();
411 if (CA_STATUS_OK != res)
416 printf("Do you want to send secure request ?.... enter (0/1): ");
418 char secureRequest[MAX_BUF_LEN] = {0};
419 if (CA_STATUS_OK != get_input_data(secureRequest, MAX_BUF_LEN))
424 if (strcmp(secureRequest, "1") == 0)
426 printf("Enter the URI like below....\n");
427 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
429 else if (strcmp(secureRequest, "0") == 0)
431 printf("Enter the URI like below....\n");
432 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
433 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
437 printf("Input data is wrong value\n");
441 char uri[MAX_BUF_LEN] = {'\0'};
442 if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
447 // create remote endpoint
448 CAEndpoint_t *endpoint = NULL;
449 CATransportFlags_t flags;
451 printf("URI : %s\n", uri);
452 addressSet_t address = {};
453 parsing_coap_uri(uri, &address, &flags);
455 res = CACreateEndpoint(flags, g_selected_nw_type,
456 (const char*)address.ipAddress, address.port, &endpoint);
457 if (CA_STATUS_OK != res || !endpoint)
459 printf("Failed to create remote endpoint, error code : %d\n", res);
463 printf("\n=============================================\n");
464 printf("0:CON, 1:NON\n");
465 printf("select message type : ");
467 char buf[MAX_BUF_LEN] = { 0 };
468 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
470 CADestroyEndpoint(endpoint);
474 CAMessageType_t msgType = (buf[0] == '1') ? 1 : 0;
477 CAToken_t token = NULL;
478 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
480 res = CAGenerateToken(&token, tokenLength);
481 if ((CA_STATUS_OK != res) || (!token))
483 printf("Token generate error, error code : %d\n", res);
484 CADestroyEndpoint(endpoint);
488 printf("Generated token %s\n", token);
490 // extract relative resourceuri from give uri
491 char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
492 get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
493 printf("resourceURI : %s\n", resourceURI);
495 // create request data
496 CAInfo_t requestData = { 0 };
497 requestData.token = token;
498 requestData.tokenLength = tokenLength;
499 requestData.resourceUri = (CAURI_t)resourceURI;
501 if (strcmp(secureRequest, "1") == 0)
503 size_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
504 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
505 if (NULL == requestData.payload)
507 printf("Memory allocation fail\n");
508 CADestroyEndpoint(endpoint);
509 CADestroyToken(token);
512 snprintf((char *) requestData.payload, length, SECURE_INFO_DATA,
513 (const char *) resourceURI, g_local_secure_port);
514 requestData.payloadSize = length;
518 bool useBigPayload = select_payload_type();
521 size_t payloadLength = 0;
522 CAPayload_t binaryPayload = get_binary_payload(&payloadLength);
526 CADestroyToken(token);
527 CADestroyEndpoint(endpoint);
531 requestData.payload = (CAPayload_t) malloc(payloadLength);
532 if (NULL == requestData.payload)
534 printf("Memory allocation failed!");
536 CADestroyToken(token);
537 CADestroyEndpoint(endpoint);
540 memcpy(requestData.payload, binaryPayload, payloadLength);
541 requestData.payloadSize = payloadLength;
548 size_t length = sizeof(NORMAL_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 CADestroyEndpoint(endpoint);
554 CADestroyToken(token);
557 snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA,
558 (const char *) resourceURI);
559 requestData.payloadSize = length;
562 requestData.type = msgType;
563 CAHeaderOption_t* headerOpt = get_option_data(&requestData);
565 CARequestInfo_t requestInfo = { 0 };
566 requestInfo.method = CA_GET;
567 requestInfo.info = requestData;
568 requestInfo.isMulticast = false;
571 res = CASendRequest(endpoint, &requestInfo);
572 if (CA_STATUS_OK != res)
574 printf("Could not send request : %d\n", res);
583 CADestroyToken(token);
584 // destroy remote endpoint
585 CADestroyEndpoint(endpoint);
586 free(requestData.payload);
588 printf("=============================================\n");
591 void send_secure_request()
593 char uri[MAX_BUF_LEN];
594 char ipv4addr[CA_IPADDR_SIZE];
596 printf("\n=============================================\n");
597 printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");
599 if (CA_STATUS_OK != get_input_data(ipv4addr, CA_IPADDR_SIZE))
603 snprintf(uri, MAX_BUF_LEN, "%s%s:5684/a/light", COAPS_PREFIX, ipv4addr);
605 // create remote endpoint
606 CAEndpoint_t *endpoint = NULL;
607 CAResult_t res = CACreateEndpoint(0, CA_ADAPTER_IP, ipv4addr, SECURE_DEFAULT_PORT, &endpoint);
608 if (CA_STATUS_OK != res)
610 printf("Failed to create remote endpoint, error code: %d\n", res);
615 CAToken_t token = NULL;
616 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
618 res = CAGenerateToken(&token, tokenLength);
619 if ((CA_STATUS_OK != res) || (!token))
621 printf("Token generate error, error code : %d\n", res);
625 printf("Generated token %s\n", token);
627 // create request data
628 CAMessageType_t msgType = CA_MSG_NONCONFIRM;
629 CAInfo_t requestData = { 0 };
630 requestData.token = token;
631 requestData.tokenLength = tokenLength;
632 requestData.type = msgType;
634 CARequestInfo_t requestInfo = { 0 };
635 requestInfo.method = CA_GET;
636 requestInfo.info = requestData;
637 requestInfo.isMulticast = false;
640 CASendRequest(endpoint, &requestInfo);
644 CADestroyToken(token);
645 CADestroyEndpoint(endpoint);
646 printf("=============================================\n");
650 void send_request_all()
652 CAResult_t res = get_network_type();
653 if (CA_STATUS_OK != res)
658 printf("\n=============================================\n");
659 printf("ex) /a/light\n");
660 printf("resource uri : ");
662 char resourceURI[MAX_BUF_LEN] = { 0 };
663 if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN))
668 // create remote endpoint
669 CAEndpoint_t *group = NULL;
670 res = CACreateEndpoint(CA_IPV4, g_selected_nw_type, NULL, 0, &group);
671 if (CA_STATUS_OK != res)
673 printf("Create remote endpoint error, error code: %d\n", res);
678 CAToken_t token = NULL;
679 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
681 res = CAGenerateToken(&token, tokenLength);
682 if ((CA_STATUS_OK != res) || (!token))
684 printf("Token generate error!!\n");
685 CADestroyEndpoint(group);
689 printf("generated token %s\n", token);
691 CAInfo_t requestData = { 0 };
692 requestData.token = token;
693 requestData.tokenLength = tokenLength;
694 requestData.payload = (CAPayload_t) "TempJsonPayload";
695 requestData.payloadSize = strlen((const char *) requestData.payload);
696 requestData.type = CA_MSG_NONCONFIRM;
697 requestData.resourceUri = (CAURI_t)resourceURI;
699 CAHeaderOption_t* headerOpt = get_option_data(&requestData);
701 CARequestInfo_t requestInfo = { 0 };
702 requestInfo.method = CA_GET;
703 requestInfo.info = requestData;
704 requestInfo.isMulticast = true;
707 res = CASendRequest(group, &requestInfo);
708 if (CA_STATUS_OK != res)
710 printf("Could not send request to all\n");
711 CADestroyToken(token);
715 CADestroyToken(g_last_request_token);
716 g_last_request_token = token;
724 // destroy remote endpoint
725 CADestroyEndpoint(group);
727 printf("=============================================\n");
730 void send_notification()
732 CAResult_t res = get_network_type();
733 if (CA_STATUS_OK != res)
738 printf("\n=============================================\n");
739 printf("Enter the URI like below....\n");
740 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
741 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
744 char uri[MAX_BUF_LEN] = { 0 };
745 if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
750 printf("\n=============================================\n");
751 printf("\tselect message type\n");
755 printf("RESET : 3\n");
758 char messageTypeBuf[MAX_BUF_LEN] = { 0 };
759 if (CA_STATUS_OK != get_input_data(messageTypeBuf, MAX_BUF_LEN))
764 int messageType = messageTypeBuf[0] - '0';
766 CATransportFlags_t flags;
767 addressSet_t address = {};
768 parsing_coap_uri(uri, &address, &flags);
770 // create remote endpoint
771 CAEndpoint_t *endpoint = NULL;
772 res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
773 if (CA_STATUS_OK != res)
775 printf("Create remote endpoint error, error code: %d\n", res);
780 CAToken_t token = NULL;
781 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
783 res = CAGenerateToken(&token, tokenLength);
784 if ((CA_STATUS_OK != res) || (!token))
786 printf("Token generate error!!\n");
787 CADestroyEndpoint(endpoint);
791 printf("Generated token %s\n", token);
793 CAInfo_t respondData = { 0 };
794 respondData.token = token;
795 respondData.tokenLength = tokenLength;
796 respondData.payload = (CAPayload_t) "TempNotificationData";
797 respondData.payloadSize = strlen((const char *) respondData.payload);
798 respondData.type = messageType;
799 respondData.resourceUri = (CAURI_t)uri;
801 CAResponseInfo_t responseInfo = { 0 };
802 responseInfo.result = CA_CONTENT;
803 responseInfo.info = respondData;
806 res = CASendNotification(endpoint, &responseInfo);
807 if (CA_STATUS_OK != res)
809 printf("Send notification error, error code: %d\n", res);
813 printf("Send notification success\n");
817 CADestroyToken(token);
818 // destroy remote endpoint
819 CADestroyEndpoint(endpoint);
821 printf("\n=============================================\n");
824 void select_network()
826 printf("\n=============================================\n");
827 printf("\tselect network\n");
829 printf("GATT : 1\n");
830 printf("RFCOMM : 2\n");
833 char buf[MAX_BUF_LEN] = { 0 };
834 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
839 int number = buf[0] - '0';
841 if (number < 0 || number > 3)
843 printf("Invalid network type\n");
847 CAResult_t res = CASelectNetwork(1 << number);
848 if (CA_STATUS_OK != res)
850 printf("Select network error\n");
854 printf("Select network success\n");
857 printf("=============================================\n");
860 void unselect_network()
862 printf("\n=============================================\n");
863 printf("\tunselect enabled network\n");
865 printf("GATT : 1\n");
866 printf("RFCOMM : 2\n");
869 char buf[MAX_BUF_LEN] = { 0 };
870 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
875 int number = buf[0] - '0';
877 if (number < 0 || number > 3)
879 printf("Invalid network type\n");
883 CAResult_t res = CAUnSelectNetwork(1 << number);
884 if (CA_STATUS_OK != res)
886 printf("Unselect network error\n");
890 printf("Unselect network success\n");
893 printf("=============================================\n");
898 printf("\n=============================================\n");
899 printf("\t\tMenu\n");
900 printf("\ts : start server\n");
901 printf("\tc : start client\n");
902 printf("\tr : send request\n");
903 printf("\tt : send request to all\n");
904 printf("\tb : send notification\n");
905 printf("\tn : select network\n");
906 printf("\tx : unselect network\n");
907 printf("\tg : get network information\n");
908 printf("\th : handle request response\n");
909 printf("\tz : run static server\n");
910 printf("\tw : send secure request\n");
911 printf("\tq : quit\n");
912 printf("=============================================\n");
915 char buf[MAX_BUF_LEN] = { 0 };
916 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
918 printf("Failed to get input data\n");
924 void handle_request_response()
926 printf("Handle_request_response\n");
928 CAResult_t res = CAHandleRequestResponse();
929 if (CA_STATUS_OK != res)
931 printf("Handle request error, error code: %d\n", res);
935 printf("Handle request success\n");
939 void get_network_info()
941 CAEndpoint_t *tempInfo = NULL;
942 uint32_t tempSize = 0;
944 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
945 if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
947 printf("Network not connected\n");
952 printf("################## Network Information #######################\n");
953 printf("Network info total size is %d\n\n", tempSize);
956 for (index = 0; index < tempSize; index++)
958 printf("Type: %d\n", tempInfo[index].adapter);
959 printf("Address: %s\n", tempInfo[index].addr);
960 if (CA_ADAPTER_IP == tempInfo[index].adapter)
962 printf("Port: %d\n", tempInfo[index].port);
964 printf("Secured: %s\n\n", (tempInfo[index].flags & CA_SECURE) ? "true" : "false");
966 if (tempInfo[index].flags & CA_SECURE)
968 g_local_secure_port = tempInfo[index].port;
969 printf("Secured: in global %d\n\n", g_local_secure_port);
974 printf("##############################################################");
977 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
979 if (NULL == object || NULL == requestInfo)
981 printf("Input parameter is NULL\n");
985 if ((NULL != g_last_request_token) && (NULL != requestInfo->info.token)
986 && (memcmp(g_last_request_token, requestInfo->info.token,
987 CA_MAX_TOKEN_LEN) == 0))
989 printf("token is same. received request of it's own. skip.. \n");
993 printf("##########received request from remote device #############\n");
994 if (CA_ADAPTER_IP == object->adapter)
996 printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
997 object->port, object->flags & CA_SECURE);
1001 printf("Remote Address: %s \n", object->addr);
1003 printf("Data: %s\n", requestInfo->info.payload);
1004 printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
1006 if (requestInfo->info.options)
1008 uint32_t len = requestInfo->info.numOptions;
1010 for (i = 0; i < len; i++)
1012 printf("Option %d\n", i + 1);
1013 printf("ID : %d\n", requestInfo->info.options[i].optionID);
1014 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
1015 requestInfo->info.options[i].optionData);
1018 printf("############################################################\n");
1020 //Check if this has secure communication information
1021 if (requestInfo->info.payload &&
1022 (CA_ADAPTER_IP == object->adapter))
1024 int securePort = get_secure_information(requestInfo->info.payload);
1025 if (0 < securePort) //Set the remote endpoint secure details and send response
1027 printf("This is secure resource...\n");
1029 CAEndpoint_t *endpoint = NULL;
1030 if (CA_STATUS_OK != CACreateEndpoint(0, object->adapter, object->addr,
1031 object->port, &endpoint))
1033 printf("Failed to create duplicate of remote endpoint!\n");
1036 endpoint->flags = CA_SECURE;
1041 // if received message is bulk data, create output file
1042 if ((requestInfo->info.payload) && (requestInfo->info.payloadSize > CA_DEFAULT_BLOCK_SIZE))
1044 create_file(requestInfo->info.payload, requestInfo->info.payloadSize);
1047 printf("Send response with URI\n");
1048 send_response(object, &requestInfo->info);
1053 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1055 printf("##########Received response from remote device #############\n");
1056 if (CA_ADAPTER_IP == object->adapter)
1058 printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
1059 object->port, object->flags & CA_SECURE);
1063 printf("Remote Address: %s \n", object->addr);
1065 printf("response result : %d\n", responseInfo->result);
1066 printf("Data: %s\n", responseInfo->info.payload);
1067 printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
1068 printf("Token: %s\n", responseInfo->info.token);
1069 if (responseInfo->info.options)
1071 uint32_t len = responseInfo->info.numOptions;
1073 for (i = 0; i < len; i++)
1075 printf("Option %d\n", i + 1);
1076 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1077 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1078 responseInfo->info.options[i].optionData);
1081 printf("############################################################\n");
1084 //Check if this has secure communication information
1085 if (responseInfo->info.payload)
1087 int securePort = get_secure_information(responseInfo->info.payload);
1088 if (0 < securePort) //Set the remote endpoint secure details and send response
1090 printf("This is secure resource...\n");
1094 // if received message is bulk data, create output file
1095 if ((responseInfo->info.payload) && (responseInfo->info.payloadSize > CA_DEFAULT_BLOCK_SIZE))
1097 create_file(responseInfo->info.payload, responseInfo->info.payloadSize);
1101 void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
1103 printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++\n");
1107 const CAInfo_t *info = &errorInfo->info;
1108 printf("Error Handler, ErrorInfo :\n");
1109 printf("Error Handler result : %d\n", errorInfo->result);
1110 printf("Error Handler token : %s\n", info->token);
1111 printf("Error Handler messageId : %d\n", (uint16_t) info->messageId);
1112 printf("Error Handler type : %d\n", info->type);
1113 printf("Error Handler resourceUri : %s\n", info->resourceUri);
1114 printf("Error Handler payload : %s\n", info->payload);
1116 if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
1118 printf("CA_ADAPTER_NOT_ENABLED, enable the adapter\n");
1120 else if(CA_SEND_FAILED == errorInfo->result)
1122 printf("CA_SEND_FAILED, unable to send the message, check parameters\n");
1124 else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
1126 printf("CA_MEMORY_ALLOC_FAILED, insufficient memory\n");
1128 else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
1130 printf("CA_SOCKET_OPERATION_FAILED, socket operation failed\n");
1132 else if(CA_STATUS_FAILED == errorInfo->result)
1134 printf("CA_STATUS_FAILED, message could not be delivered, internal error\n");
1137 printf("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++\n");
1142 void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info)
1144 printf("entering send_response\n");
1146 printf("\n=============================================\n");
1147 printf("\tselect message type\n");
1148 printf("CON : 0\n");
1149 printf("NON : 1\n");
1150 printf("ACK : 2\n");
1151 printf("RESET : 3\n");
1152 printf("select : ");
1154 char buf[MAX_BUF_LEN] = { 0 };
1155 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1160 int messageType = buf[0] - '0';
1161 if (0 > messageType || 3 < messageType)
1163 printf("Invalid message type\n");
1167 int responseCode = 0 ;
1168 char responseCodeBuf[MAX_BUF_LEN] = { 0 };
1169 if (CA_MSG_RESET != messageType)
1171 printf("\n=============================================\n");
1172 printf("\tselect response code\n");
1173 printf("EMPTY : 0\n");
1174 printf("SUCCESS : 200\n");
1175 printf("CREATED : 201\n");
1176 printf("DELETED : 202\n");
1177 printf("VALID : 203\n");
1178 printf("CHANGED : 204\n");
1179 printf("CONTENT : 205\n");
1180 printf("BAD_REQ : 400\n");
1181 printf("BAD_OPT : 402\n");
1182 printf("NOT_FOUND : 404\n");
1183 printf("INTERNAL_SERVER_ERROR : 500\n");
1184 printf("RETRANSMIT_TIMEOUT : 504\n");
1185 printf("select : ");
1187 if (CA_STATUS_OK != get_input_data(responseCodeBuf, MAX_BUF_LEN))
1191 responseCode = atoi(responseCodeBuf);
1193 CAInfo_t responseData = { 0 };
1194 responseData.type = messageType;
1195 responseData.messageId = (info != NULL) ? info->messageId : 0;
1196 responseData.resourceUri = (info != NULL) ? info->resourceUri : 0;
1198 if(CA_MSG_RESET != messageType)
1200 responseData.token = (info != NULL) ? info->token : NULL;
1201 responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
1203 if (endpoint->flags & CA_SECURE)
1205 printf("Sending response on secure communication\n");
1207 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri);
1208 responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
1209 if (NULL == responseData.payload)
1211 printf("Memory allocation fail\n");
1214 snprintf((char *) responseData.payload, length, SECURE_INFO_DATA,
1215 (const char *) responseData.resourceUri, g_local_secure_port);
1216 responseData.payloadSize = length;
1220 printf("Sending response on non-secure communication\n");
1222 bool useBigPayload = select_payload_type();
1225 size_t payloadLength = 0;
1226 CAPayload_t binaryPayload = get_binary_payload(&payloadLength);
1227 if (NULL == binaryPayload)
1229 free(binaryPayload);
1233 responseData.payload = (CAPayload_t) malloc(payloadLength);
1234 if (NULL == responseData.payload)
1236 printf("Memory allocation failed!");
1237 free(binaryPayload);
1240 memcpy(responseData.payload, binaryPayload, payloadLength);
1241 responseData.payloadSize = payloadLength;
1244 free(binaryPayload);
1248 uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri);
1249 responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
1250 if (NULL == responseData.payload)
1252 printf("Memory allocation fail\n");
1255 snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA,
1256 (const char *) responseData.resourceUri);
1257 responseData.payloadSize = length;
1262 CAResponseInfo_t responseInfo = { 0 };
1263 responseInfo.result = responseCode;
1264 responseInfo.info = responseData;
1266 // send response (transportType from remoteEndpoint of request Info)
1267 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1268 if (CA_STATUS_OK != res)
1270 printf("Send response error\n");
1274 printf("Send response success\n");
1277 if (responseData.payload)
1279 free(responseData.payload);
1282 printf("=============================================\n");
1285 int get_secure_information(CAPayload_t payLoad)
1287 printf("Entering get_secure_information\n");
1291 printf("Payload is NULL\n");
1295 char *subString = NULL;
1296 if (NULL == (subString = strstr((const char *) payLoad, "\"sec\":1")))
1298 printf("This is not secure resource\n");
1302 if (NULL == (subString = strstr((const char *) payLoad, "\"port\":")))
1304 printf("This secure resource does not have port information\n");
1308 char *startPos = strstr(subString, ":");
1311 printf("Parsing failed !\n");
1315 char *endPos = strstr(startPos, "}");
1318 printf("Parsing failed !\n");
1322 char portStr[6] = {0};
1323 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1325 printf("secured port is: %s\n", portStr);
1326 return atoi(portStr);
1329 void get_resource_uri(char *URI, char *resourceURI, int length)
1331 char *startPos = URI;
1333 if (NULL != (temp = strstr(URI, "://")))
1335 startPos = strchr(temp + 3, '/');
1338 printf("Resource URI is missing\n");
1343 char *endPos = strchr(startPos, '?');
1346 endPos = URI + strlen(URI);
1350 if (endPos - startPos <= length)
1352 memcpy(resourceURI, startPos + 1, endPos - startPos);
1355 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1358 CAResult_t get_network_type()
1360 char buf[MAX_BUF_LEN] = { 0 };
1362 printf("\n=============================================\n");
1363 printf("\tselect network type\n");
1365 printf("GATT : 1\n");
1366 printf("RFCOMM : 2\n");
1367 printf("select : ");
1369 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1371 return CA_NOT_SUPPORTED ;
1374 int number = buf[0] - '0';
1376 number = (number < 0 || number > 3) ? 0 : 1 << number;
1380 g_selected_nw_type = CA_ADAPTER_IP;
1381 return CA_STATUS_OK;
1385 g_selected_nw_type = CA_ADAPTER_GATT_BTLE;
1386 return CA_STATUS_OK;
1390 g_selected_nw_type = CA_ADAPTER_RFCOMM_BTEDR;
1391 return CA_STATUS_OK;
1394 return CA_NOT_SUPPORTED;
1397 CAResult_t get_input_data(char *buf, int32_t length)
1399 if (!fgets(buf, length, stdin))
1401 printf("fgets error\n");
1402 return CA_STATUS_FAILED;
1406 if ( (p = strchr(buf, '\n')) != NULL )
1411 return CA_STATUS_OK;
1414 CAHeaderOption_t* get_option_data(CAInfo_t* requestData)
1416 char optionNumBuf[MAX_BUF_LEN] = { 0 };
1417 char optionData[MAX_OPT_LEN] = { 0 } ;
1419 printf("Option Num : ");
1420 if (CA_STATUS_OK != get_input_data(optionNumBuf, MAX_BUF_LEN))
1424 int optionNum = atoi(optionNumBuf);
1426 CAHeaderOption_t * headerOpt = NULL;
1429 printf("there is no headerOption!\n");
1434 headerOpt = (CAHeaderOption_t *)calloc(1, optionNum * sizeof(CAHeaderOption_t));
1435 if (NULL == headerOpt)
1437 printf("Memory allocation failed!\n");
1442 for (i = 0; i < optionNum; i++)
1444 char getOptionID[MAX_BUF_LEN] = { 0 } ;
1446 printf("[%d] Option ID : ", i + 1);
1447 if (CA_STATUS_OK != get_input_data(getOptionID, MAX_BUF_LEN))
1452 int optionID = atoi(getOptionID);
1453 headerOpt[i].optionID = optionID;
1455 printf("[%d] Option Data : ", i + 1);
1456 if (CA_STATUS_OK != get_input_data(optionData, MAX_OPT_LEN))
1462 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
1464 headerOpt[i].optionLength = (uint16_t) strlen(optionData);
1466 requestData->numOptions = optionNum;
1467 requestData->options = headerOpt;
1472 void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
1476 printf("parameter is null\n");
1482 uint8_t startIndex = 0;
1483 if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
1485 printf("uri has '%s' prefix\n", COAPS_PREFIX);
1486 startIndex = COAPS_PREFIX_LEN;
1489 else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
1491 printf("uri has '%s' prefix\n", COAP_PREFIX);
1492 startIndex = COAP_PREFIX_LEN;
1496 // #2. copy uri for parse
1497 int32_t len = strlen(uri) - startIndex;
1501 printf("uri length is 0!\n");
1505 char *cloneUri = (char *) calloc(len + 1, sizeof(char));
1506 if (NULL == cloneUri)
1508 printf("Out of memory\n");
1512 memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
1513 cloneUri[len] = '\0';
1515 char *pAddress = cloneUri;
1516 printf("pAddress : %s\n", pAddress);
1518 int res = get_address_set(pAddress, address);
1521 printf("address parse error\n");
1530 int get_address_set(const char *pAddress, addressSet_t* outAddress)
1532 if (NULL == pAddress)
1534 printf("parameter is null !\n");
1538 int32_t len = strlen(pAddress);
1542 for (int i = 0; i < len; i++)
1544 if (pAddress[i] == '.')
1549 // found port number start index
1550 if (isIp && pAddress[i] == ':')
1559 if(ipLen && ipLen < sizeof(outAddress->ipAddress))
1561 strncpy(outAddress->ipAddress, pAddress, ipLen);
1562 outAddress->ipAddress[ipLen] = '\0';
1564 else if (!ipLen && len < sizeof(outAddress->ipAddress))
1566 strncpy(outAddress->ipAddress, pAddress, len);
1567 outAddress->ipAddress[len] = '\0';
1571 printf("IP Address too long: %d\n", ipLen==0 ? len : ipLen);
1577 outAddress->port = atoi(pAddress + ipLen + 1);
1584 void create_file(CAPayload_t bytes, size_t length)
1586 FILE *fp = fopen("sample_output.txt", "wb");
1587 fwrite(bytes, 1, length, fp);
1591 bool read_file(const char* name, CAPayload_t* bytes, size_t* length)
1595 printf("parameter is null\n");
1600 CAPayload_t buffer = NULL;
1601 unsigned long fileLen = 0;
1604 file = fopen(name, "rb");
1607 fprintf(stderr, "Unable to open file, %s\n", name);
1612 fseek(file, 0, SEEK_END);
1613 fileLen = ftell(file);
1614 fseek(file, 0, SEEK_SET);
1617 buffer = calloc(1, sizeof(uint8_t) * fileLen + 1);
1620 fprintf(stderr, "Memory error\n");
1625 // Read file contents into buffer
1626 size_t ret = fread(buffer, fileLen, 1, file);
1629 printf("Failed to read data from file, %s\n", name);