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"
33 * @brief maximum buffer length
35 #define MAX_BUF_LEN 1024
39 * @brief maximum option length
41 #define MAX_OPT_LEN 16
45 * @brief maximum port length
50 * @def SECURE_DEFAULT_PORT
51 * @brief default secured port
53 #define SECURE_DEFAULT_PORT 5684
55 #define RESOURCE_URI_LENGTH 14
57 #define COAP_PREFIX "coap://"
58 #define COAP_PREFIX_LEN 7
59 #define COAPS_PREFIX "coaps://"
60 #define COAPS_PREFIX_LEN 8
62 // Iotivity Device Identity.
63 const unsigned char IDENTITY[] = ("1111111111111111");
65 // PSK between this device and peer device.
66 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
69 * Max size for big payload.
71 #define BIG_PAYLOAD_SIZE 15000
73 static GMainLoop *g_mainloop = NULL;
77 uint16_t g_local_secure_port = SECURE_DEFAULT_PORT;
78 CATransportAdapter_t g_selected_nw_type = CA_ADAPTER_IP;
79 const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"};
83 char ipAddress[CA_IPADDR_SIZE];
89 CAResult_t get_network_type();
90 CAResult_t get_input_data(char *buf, int32_t length);
92 bool select_payload();
93 void populate_binary_payload(uint8_t *bigBuffer, size_t bigBufferLen);
95 void start_listening_server();
96 void start_discovery_server();
98 void send_request_all();
99 void send_notification();
100 void select_network();
101 void unselect_network();
102 void handle_request_response();
103 void get_network_info();
104 void send_secure_request();
106 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
107 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
108 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
109 void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info);
110 void get_resource_uri(char *URI, char *resourceURI, int length);
111 int get_secure_information(CAPayload_t payLoad);
112 int get_address_set(const char *uri, addressSet_t* outAddress);
113 void parse_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
115 static CAToken_t g_last_request_token = NULL;
116 static const char SECURE_COAPS_PREFIX[] = "coaps://";
117 static const char SECURE_INFO_DATA[] =
118 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
119 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
120 static const char NORMAL_INFO_DATA[] =
121 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
122 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
125 // Internal API. Invoked by CA stack to retrieve credentials from this module.
126 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
127 const unsigned char *desc, size_t desc_len,
128 unsigned char *result, size_t result_length)
130 printf("CAGetDtlsPskCredentials IN\n");
141 case CA_DTLS_PSK_HINT:
142 case CA_DTLS_PSK_IDENTITY:
144 if (result_length < sizeof(IDENTITY))
146 printf("ERROR : Wrong value for result for storing IDENTITY");
150 memcpy(result, IDENTITY, sizeof(IDENTITY));
151 ret = sizeof(IDENTITY);
154 case CA_DTLS_PSK_KEY:
156 if ((desc_len == sizeof(IDENTITY)) &&
157 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
159 if (result_length < sizeof(RS_CLIENT_PSK))
161 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
165 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
166 ret = sizeof(RS_CLIENT_PSK);
172 printf("Wrong value passed for PSK_CRED_TYPE.");
176 printf("CAGetDtlsPskCredentials OUT\n");
181 void GMainLoopThread()
183 g_main_loop_run(g_mainloop);
186 CAResult_t Initialize()
188 g_mainloop = g_main_loop_new(NULL, FALSE);
191 printf("g_main_loop_new failed\n");
192 return CA_STATUS_FAILED;
195 int result = pthread_create(&thread, NULL, (void *) &GMainLoopThread, NULL);
198 printf("pthread_create failed in initialize\n");
199 return CA_STATUS_FAILED;
202 CAResult_t res = CAInitialize();
203 if (res != CA_STATUS_OK)
205 printf("CAInitialize fail\n");
212 int ret = system("clear");
213 // shell invoke error: 127, others: -1
214 if (127 == ret || -1 == ret)
216 printf("Terminal Clear Error: %d\n", ret);
219 printf("=============================================\n");
220 printf("\t\tsample main\n");
221 printf("=============================================\n");
223 CAResult_t res = Initialize();
224 if (CA_STATUS_OK != res)
226 printf("Initialization is failed\n");
230 // Set the PSK Credentials callback handler.
232 res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
233 if (CA_STATUS_OK != res)
235 printf("Set credential handler fail\n");
241 CARegisterHandler(request_handler, response_handler, error_handler);
245 CADestroyToken(g_last_request_token);
255 char menu = get_menu();
265 printf("quit..!!\n");
268 case 's': // start server
270 start_listening_server();
273 case 't': // send request
278 case 'c': // start client
280 start_discovery_server();
283 case 'r': // send request
288 case 'b': // send notification
293 case 'n': // select network
298 case 'x': // unselect network
303 case 'h': // handle request response
305 handle_request_response();
311 start_discovery_server();
312 send_secure_request();
313 while (g_received == 0)
316 handle_request_response();
322 start_listening_server();
326 handle_request_response();
330 case 'g': // get network information
336 printf("Not supported menu!!\n");
343 void start_listening_server()
345 printf("Start listening server!!\n");
347 CAResult_t res = CAStartListeningServer();
348 if (CA_STATUS_OK != res)
350 printf("Start listening server fail, error code : %d\n", res);
354 printf("Start listening server success\n");
358 void start_discovery_server()
360 printf("Start discovery client!!\n");
362 CAResult_t res = CAStartDiscoveryServer();
363 if (CA_STATUS_OK != res)
365 printf("Start discovery client fail, error code : %d\n", res);
369 printf("Start discovery client success\n");
373 bool select_payload()
375 char buf[MAX_BUF_LEN]={0};
376 printf("\n=============================================\n");
377 printf("0:Normal Payload\n1:Big Payload(~15KB)\n");
378 printf("select Payload type : ");
380 CAResult_t res = get_input_data(buf, sizeof(buf));
381 if (CA_STATUS_OK != res)
383 printf("Payload type selection error\n");
384 printf("Default: Using normal Payload\n");
388 return (buf[0] == '1') ? true : false;
391 void populate_binary_payload(uint8_t *bigBuffer, size_t bigBufferLen)
394 * bigBuffer to be filled with binary data. For our sample application to verify, we may fill with
395 * any arbitrary value. Hence filling with '1' here.
397 memset(bigBuffer, '1', bigBufferLen-1);
398 //Last byte making NULL
399 bigBuffer[bigBufferLen-1] = '\0';
404 CAResult_t res = get_network_type();
405 if (CA_STATUS_OK != res)
410 printf("Do you want to send secure request ?.... enter (0/1): ");
412 char secureRequest[MAX_BUF_LEN] = {0};
413 if (CA_STATUS_OK != get_input_data(secureRequest, MAX_BUF_LEN))
418 if (strcmp(secureRequest, "1") == 0)
420 printf("Enter the URI like below....\n");
421 printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
423 else if (strcmp(secureRequest, "0") == 0)
425 printf("Enter the URI like below....\n");
426 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
427 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
431 printf("Input data is wrong value\n");
435 char uri[MAX_BUF_LEN] = {'\0'};
436 if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
441 // create remote endpoint
442 CAEndpoint_t *endpoint = NULL;
443 CATransportFlags_t flags;
445 printf("URI : %s\n", uri);
446 addressSet_t address = {};
447 parse_coap_uri(uri, &address, &flags);
449 res = CACreateEndpoint(flags, g_selected_nw_type,
450 (const char*)address.ipAddress, address.port, &endpoint);
451 if (CA_STATUS_OK != res || !endpoint)
453 printf("Failed to create remote endpoint, error code : %d\n", res);
457 printf("\n=============================================\n");
458 printf("0:CON, 1:NON\n");
459 printf("select message type : ");
461 char buf[MAX_BUF_LEN] = { 0 };
462 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
464 CADestroyEndpoint(endpoint);
468 CAMessageType_t msgType = (buf[0] == '1') ? 1 : 0;
471 CAToken_t token = NULL;
472 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
474 res = CAGenerateToken(&token, tokenLength);
475 if ((CA_STATUS_OK != res) || (!token))
477 printf("Token generate error, error code : %d\n", res);
478 CADestroyEndpoint(endpoint);
482 printf("Generated token %s\n", token);
484 // extract relative resourceuri from give uri
485 char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
486 get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
487 printf("resourceURI : %s\n", resourceURI);
489 // create request data
490 CAInfo_t requestData = { 0 };
491 requestData.token = token;
492 requestData.tokenLength = tokenLength;
493 requestData.resourceUri = (CAURI_t)resourceURI;
495 if (strcmp(secureRequest, "1") == 0)
497 size_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
498 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
499 if (NULL == requestData.payload)
501 printf("Memory allocation fail\n");
502 CADestroyEndpoint(endpoint);
503 CADestroyToken(token);
506 snprintf((char *) requestData.payload, length, SECURE_INFO_DATA,
507 (const char *) resourceURI, g_local_secure_port);
511 bool useBigPayload = select_payload();
514 requestData.payload = (CAPayload_t) calloc(BIG_PAYLOAD_SIZE, sizeof(char));
515 if (NULL == requestData.payload)
517 printf("Memory allocation fail\n");
518 CADestroyEndpoint(endpoint);
519 CADestroyToken(token);
522 populate_binary_payload(requestData.payload, BIG_PAYLOAD_SIZE);
526 size_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
527 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
528 if (NULL == requestData.payload)
530 printf("Memory allocation fail\n");
531 CADestroyEndpoint(endpoint);
532 CADestroyToken(token);
535 snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA,
536 (const char *) resourceURI);
539 requestData.payloadSize = strlen((char *)requestData.payload)+1;
540 requestData.type = msgType;
542 CARequestInfo_t requestInfo = { 0 };
543 requestInfo.method = CA_GET;
544 requestInfo.info = requestData;
545 requestInfo.isMulticast = false;
548 res = CASendRequest(endpoint, &requestInfo);
549 if (CA_STATUS_OK != res)
551 printf("Could not send request : %d\n", res);
555 CADestroyToken(token);
556 // destroy remote endpoint
557 CADestroyEndpoint(endpoint);
558 free(requestData.payload);
561 printf("=============================================\n");
564 void send_secure_request()
566 char ipv4addr[CA_IPADDR_SIZE];
568 printf("\n=============================================\n");
569 printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");
571 if (CA_STATUS_OK != get_input_data(ipv4addr, CA_IPADDR_SIZE))
575 printf("%s%s:5684/a/light", SECURE_COAPS_PREFIX, ipv4addr);
577 // create remote endpoint
578 CAEndpoint_t *endpoint = NULL;
579 CAResult_t res = CACreateEndpoint(0, CA_ADAPTER_IP, ipv4addr, SECURE_DEFAULT_PORT, &endpoint);
580 if (CA_STATUS_OK != res)
582 printf("Failed to create remote endpoint, error code: %d\n", res);
587 CAToken_t token = NULL;
588 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
590 res = CAGenerateToken(&token, tokenLength);
591 if ((CA_STATUS_OK != res) || (!token))
593 printf("Token generate error, error code : %d\n", res);
597 printf("Generated token %s\n", token);
599 // create request data
600 CAMessageType_t msgType = CA_MSG_NONCONFIRM;
601 CAInfo_t requestData = { 0 };
602 requestData.token = token;
603 requestData.tokenLength = tokenLength;
604 requestData.type = msgType;
605 requestData.payload = "Temp Json Payload";
606 requestData.payloadSize = strlen(requestData.payload)+1;
608 CARequestInfo_t requestInfo = { 0 };
609 requestInfo.method = CA_GET;
610 requestInfo.info = requestData;
611 requestInfo.isMulticast = false;
614 CASendRequest(endpoint, &requestInfo);
618 CADestroyToken(token);
619 CADestroyEndpoint(endpoint);
620 printf("=============================================\n");
624 void send_request_all()
626 CAResult_t res = get_network_type();
627 if (CA_STATUS_OK != res)
632 printf("\n=============================================\n");
633 printf("ex) /a/light\n");
634 printf("resource uri : ");
636 char resourceURI[MAX_BUF_LEN] = { 0 };
637 if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN))
642 // create remote endpoint
643 CAEndpoint_t *endpoint = NULL;
644 res = CACreateEndpoint(CA_IPV4, g_selected_nw_type, NULL, 0, &endpoint);
645 if (CA_STATUS_OK != res)
647 printf("Create remote endpoint error, error code: %d\n", res);
652 CAToken_t token = NULL;
653 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
655 res = CAGenerateToken(&token, tokenLength);
656 if ((CA_STATUS_OK != res) || (!token))
658 printf("Token generate error!!\n");
659 CADestroyEndpoint(endpoint);
663 printf("generated token %s\n", token);
665 CAInfo_t requestData = { 0 };
666 requestData.token = token;
667 requestData.tokenLength = tokenLength;
668 requestData.payload = (CAPayload_t) "TempJsonPayload";
669 requestData.payloadSize = strlen((const char *) requestData.payload);
670 requestData.type = CA_MSG_NONCONFIRM;
671 requestData.resourceUri = (CAURI_t)resourceURI;
673 CARequestInfo_t requestInfo = { 0 };
674 requestInfo.method = CA_GET;
675 requestInfo.info = requestData;
676 requestInfo.isMulticast = true;
679 res = CASendRequest(endpoint, &requestInfo);
680 if (CA_STATUS_OK != res)
682 printf("Could not send request to all\n");
686 CADestroyToken(g_last_request_token);
687 g_last_request_token = token;
690 // destroy remote endpoint
691 CADestroyEndpoint(endpoint);
693 printf("=============================================\n");
696 void send_notification()
698 CAResult_t res = get_network_type();
699 if (CA_STATUS_OK != res)
704 printf("\n=============================================\n");
705 printf("Enter the URI like below....\n");
706 printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
707 printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
710 char uri[MAX_BUF_LEN] = { 0 };
711 if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
716 printf("\n=============================================\n");
717 printf("\tselect message type\n");
721 printf("RESET : 3\n");
725 char messageTypeBuf[MAX_BUF_LEN] = { 0 };
726 if (CA_STATUS_OK != get_input_data(messageTypeBuf, MAX_BUF_LEN))
731 int messageType = messageTypeBuf[0] - '0';
736 printf("CONFIRM messagetype is selected\n");
739 printf("NONCONFIRM messagetype is selected\n");
742 printf("Invalid Selection\n");
746 CATransportFlags_t flags;
747 addressSet_t address = {};
748 parse_coap_uri(uri, &address, &flags);
750 // create remote endpoint
751 CAEndpoint_t *endpoint = NULL;
752 res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
753 if (CA_STATUS_OK != res)
755 printf("Create remote endpoint error, error code: %d\n", res);
760 CAToken_t token = NULL;
761 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
763 res = CAGenerateToken(&token, tokenLength);
764 if ((CA_STATUS_OK != res) || (!token))
766 printf("Token generate error!!\n");
767 CADestroyEndpoint(endpoint);
771 printf("Generated token %s\n", token);
773 CAInfo_t respondData = { 0 };
774 respondData.token = token;
775 respondData.tokenLength = tokenLength;
776 respondData.payload = (CAPayload_t) "TempNotificationData";
777 respondData.payloadSize = strlen((const char *) respondData.payload);
778 respondData.type = messageType;
779 respondData.resourceUri = (CAURI_t)uri;
781 CAResponseInfo_t responseInfo = { 0 };
782 responseInfo.result = CA_CONTENT;
783 responseInfo.info = respondData;
786 res = CASendNotification(endpoint, &responseInfo);
787 if (CA_STATUS_OK != res)
789 printf("Send notification error, error code: %d\n", res);
793 printf("Send notification success\n");
797 CADestroyToken(token);
798 // destroy remote endpoint
799 CADestroyEndpoint(endpoint);
801 printf("\n=============================================\n");
804 void select_network()
806 printf("\n=============================================\n");
807 printf("\tselect network\n");
809 printf("GATT : 1\n");
810 printf("RFCOMM : 2\n");
813 char buf[MAX_BUF_LEN] = { 0 };
814 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
819 int number = buf[0] - '0';
821 if (number < 0 || number > 3)
823 printf("Invalid network type\n");
827 CAResult_t res = CASelectNetwork(1 << number);
828 if (CA_STATUS_OK != res)
830 printf("Select network error\n");
831 g_selected_nw_type = 1 << number;
835 printf("Select network success\n");
837 printf("=============================================\n");
840 void unselect_network()
842 printf("\n=============================================\n");
843 printf("\tunselect enabled network\n");
845 printf("GATT : 1\n");
846 printf("RFCOMM : 2\n");
849 char buf[MAX_BUF_LEN] = { 0 };
850 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
855 int number = buf[0] - '0';
857 if (number < 0 || number > 3)
859 printf("Invalid network type\n");
863 CAResult_t res = CAUnSelectNetwork(1 << number);
864 if (CA_STATUS_OK != res)
866 printf("Unselect network error\n");
870 printf("Unselect network success\n");
873 printf("=============================================\n");
878 printf("\n=============================================\n");
879 printf("\t\tMenu\n");
880 printf("\ts : start server\n");
881 printf("\tc : start client\n");
882 printf("\tr : send request\n");
883 printf("\tt : send request to all\n");
884 printf("\tb : send notification\n");
885 printf("\tn : select network\n");
886 printf("\tx : unselect network\n");
887 printf("\tg : get network information\n");
888 printf("\th : handle request response\n");
889 printf("\tz : run static server\n");
890 printf("\tw : send secure request\n");
891 printf("\tq : quit\n");
892 printf("=============================================\n");
895 char buf[MAX_BUF_LEN] = { 0 };
896 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
898 printf("Failed to get input data\n");
904 void handle_request_response()
906 printf("Handle_request_response\n");
908 CAResult_t res = CAHandleRequestResponse();
909 if (CA_STATUS_OK != res)
911 printf("Handle request error, error code: %d\n", res);
915 printf("Handle request success\n");
919 void get_network_info()
921 CAEndpoint_t *tempInfo = NULL;
922 uint32_t tempSize = 0;
924 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
925 if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
927 printf("Network not connected\n");
932 printf("################## Network Information #######################\n");
933 printf("Network info total size is %d\n\n", tempSize);
936 for (index = 0; index < tempSize; index++)
938 printf("Type: %d\n", tempInfo[index].adapter);
939 if (CA_ADAPTER_IP == tempInfo[index].adapter)
941 printf("Address: %s\n", tempInfo[index].addr);
942 printf("Port: %d\n", tempInfo[index].port);
946 printf("Address: %s\n", tempInfo[index].addr);
949 printf("Secured: %s\n\n", (tempInfo[index].flags & CA_SECURE) ? "true" : "false");
951 if (tempInfo[index].flags & CA_SECURE)
953 g_local_secure_port = tempInfo[index].port;
954 printf("Secured: in global %d\n\n", g_local_secure_port);
959 printf("##############################################################");
962 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
964 if (NULL == object || NULL == requestInfo)
966 printf("Input parameter is NULL\n");
970 if ((NULL != g_last_request_token) && (NULL != requestInfo->info.token)
971 && (strncmp(g_last_request_token, requestInfo->info.token,
972 requestInfo->info.tokenLength) == 0))
974 printf("Token is same. received request of it's own. skip.. \n");
978 printf("##########received request from remote device #############\n");
979 if (CA_ADAPTER_IP == object->adapter)
981 printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
982 object->port, object->flags & CA_SECURE);
986 printf("Remote Address: %s \n", object->addr);
988 printf("Data: %s\n", requestInfo->info.payload);
989 printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
991 if (requestInfo->info.options)
993 uint32_t len = requestInfo->info.numOptions;
995 for (i = 0; i < len; i++)
997 printf("Option %d\n", i + 1);
998 printf("ID : %d\n", requestInfo->info.options[i].optionID);
999 printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
1000 requestInfo->info.options[i].optionData);
1003 printf("############################################################\n");
1005 //Check if this has secure communication information
1006 if (requestInfo->info.payload &&
1007 (CA_ADAPTER_IP == object->adapter))
1009 int securePort = get_secure_information(requestInfo->info.payload);
1010 if (0 < securePort) //Set the remote endpoint secure details and send response
1012 printf("This is secure resource...\n");
1014 CAEndpoint_t *endpoint = NULL;
1015 if (CA_STATUS_OK != CACreateEndpoint(0, object->adapter, object->addr,
1016 object->port, &endpoint))
1018 printf("Failed to create duplicate of remote endpoint!\n");
1021 endpoint->flags = CA_SECURE;
1026 printf("Send response with URI\n");
1027 send_response(object, &requestInfo->info);
1032 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
1034 printf("##########Received response from remote device #############\n");
1035 if (CA_ADAPTER_IP == object->adapter)
1037 printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
1038 object->port, object->flags & CA_SECURE);
1042 printf("Remote Address: %s \n", object->addr);
1045 printf("resource uri : %s\n", responseInfo->info.resourceUri);
1046 printf("response result : %d\n", responseInfo->result);
1047 printf("Data: %s\n", responseInfo->info.payload);
1048 printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
1049 printf("Token: %s\n", responseInfo->info.token);
1050 if (responseInfo->info.options)
1052 uint32_t len = responseInfo->info.numOptions;
1054 for (i = 0; i < len; i++)
1056 printf("Option %d\n", i + 1);
1057 printf("ID : %d\n", responseInfo->info.options[i].optionID);
1058 printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
1059 responseInfo->info.options[i].optionData);
1062 printf("############################################################\n");
1065 //Check if this has secure communication information
1066 if (responseInfo->info.payload)
1068 int securePort = get_secure_information(responseInfo->info.payload);
1069 if (0 < securePort) //Set the remote endpoint secure details and send response
1071 printf("This is secure resource...\n");
1076 void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
1078 printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++\n");
1082 const CAInfo_t *info = &errorInfo->info;
1083 printf("Error Handler, ErrorInfo :\n");
1084 printf("Error Handler result : %d\n", errorInfo->result);
1085 printf("Error Handler token : %s\n", info->token);
1086 printf("Error Handler messageId : %d\n", (uint16_t) info->messageId);
1087 printf("Error Handler type : %d\n", info->type);
1088 printf("Error Handler resourceUri : %s\n", info->resourceUri);
1089 printf("Error Handler payload : %s\n", info->payload);
1091 if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
1093 printf("CA_ADAPTER_NOT_ENABLED, enable the adapter\n");
1095 else if(CA_SEND_FAILED == errorInfo->result)
1097 printf("CA_SEND_FAILED, unable to send the message, check parameters\n");
1099 else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
1101 printf("CA_MEMORY_ALLOC_FAILED, insufficient memory\n");
1103 else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
1105 printf("CA_SOCKET_OPERATION_FAILED, socket operation failed\n");
1107 else if(CA_STATUS_FAILED == errorInfo->result)
1109 printf("CA_STATUS_FAILED, message could not be delivered, internal error\n");
1112 printf("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++\n");
1117 void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info)
1119 printf("entering send_response\n");
1121 printf("\n=============================================\n");
1122 printf("\tselect message type\n");
1123 printf("CON : 0\n");
1124 printf("NON : 1\n");
1125 printf("ACK : 2\n");
1126 printf("RESET : 3\n");
1127 printf("select : ");
1129 char buf[MAX_BUF_LEN] = { 0 };
1130 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1135 int messageType = buf[0] - '0';
1136 int responseCode = 0 ;
1137 char responseCodeBuf[MAX_BUF_LEN] = { 0 };
1138 if (CA_MSG_RESET != messageType)
1140 printf("\n=============================================\n");
1141 printf("\tselect response code\n");
1142 printf("EMPTY : 0\n");
1143 printf("CREATED : 201\n");
1144 printf("DELETED : 202\n");
1145 printf("VALID : 203\n");
1146 printf("CHANGED : 204\n");
1147 printf("CONTENT : 205\n");
1148 printf("BAD_REQ : 400\n");
1149 printf("BAD_OPT : 402\n");
1150 printf("NOT_FOUND : 404\n");
1151 printf("INTERNAL_SERVER_ERROR : 500\n");
1152 printf("RETRANSMIT_TIMEOUT : 504\n");
1153 printf("select : ");
1155 if (CA_STATUS_OK != get_input_data(responseCodeBuf, MAX_BUF_LEN))
1159 responseCode = atoi(responseCodeBuf);
1161 CAInfo_t responseData = { 0 };
1162 responseData.type = messageType;
1163 responseData.messageId = (info != NULL) ? info->messageId : 0;
1164 responseData.resourceUri = (info != NULL) ? info->resourceUri : 0;
1166 if(CA_MSG_RESET != messageType)
1168 responseData.token = (info != NULL) ? info->token : NULL;
1169 responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
1171 if (endpoint->flags & CA_SECURE)
1173 printf("Sending response on secure communication\n");
1175 uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri)
1176 + sizeof(g_local_secure_port);
1177 responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
1178 if (NULL == responseData.payload)
1180 printf("Memory allocation fail\n");
1183 snprintf((char *) responseData.payload, length, SECURE_INFO_DATA,
1184 (const char *) responseData.resourceUri, g_local_secure_port);
1188 printf("Sending response on non-secure communication\n");
1190 bool useBigPayload = select_payload();
1193 responseData.payload = (CAPayload_t) calloc(BIG_PAYLOAD_SIZE, sizeof(char));
1194 if (NULL == responseData.payload)
1196 printf("Memory allocation fail\n");
1199 populate_binary_payload(responseData.payload, BIG_PAYLOAD_SIZE);
1203 size_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri);
1204 responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
1205 if (NULL == responseData.payload)
1207 printf("Memory allocation fail\n");
1210 snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA,
1211 (const char *) responseData.resourceUri);
1215 responseData.payloadSize = strlen((char *)responseData.payload)+1;
1216 CAResponseInfo_t responseInfo = { 0 };
1217 responseInfo.result = responseCode;
1218 responseInfo.info = responseData;
1220 // send response (transportType from remoteEndpoint of request Info)
1221 CAResult_t res = CASendResponse(endpoint, &responseInfo);
1222 if (CA_STATUS_OK != res)
1224 printf("Send response error\n");
1228 printf("Send response success\n");
1231 printf("=============================================\n");
1234 int get_secure_information(CAPayload_t payLoad)
1236 printf("Entering get_secure_information\n");
1240 printf("Payload is NULL\n");
1244 char *subString = NULL;
1245 if (NULL == (subString = strstr((const char *) payLoad, "\"sec\":1")))
1247 printf("This is not secure resource\n");
1251 if (NULL == (subString = strstr((const char *) payLoad, "\"port\":")))
1253 printf("This secure resource does not have port information\n");
1257 char *startPos = strstr(subString, ":");
1260 printf("Parsing failed !\n");
1264 char *endPos = strstr(startPos, "}");
1267 printf("Parsing failed !\n");
1271 char portStr[6] = {0};
1272 memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
1274 printf("secured port is: %s\n", portStr);
1275 return atoi(portStr);
1278 void get_resource_uri(char *URI, char *resourceURI, int length)
1280 char *startPos = URI;
1282 if (NULL != (temp = strstr(URI, "://")))
1284 startPos = strchr(temp + 3, '/');
1287 printf("Resource URI is missing\n");
1292 char *endPos = strchr(startPos, '?');
1295 endPos = URI + strlen(URI);
1299 if (endPos - startPos <= length)
1301 memcpy(resourceURI, startPos + 1, endPos - startPos);
1304 printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
1307 CAResult_t get_network_type()
1309 printf("\n=============================================\n");
1310 printf("\tselect network type\n");
1312 printf("GATT : 1\n");
1313 printf("RFCOMM : 2\n");
1314 printf("select : ");
1316 char buf[MAX_BUF_LEN] = { 0 };
1317 if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
1319 return CA_NOT_SUPPORTED ;
1322 int number = buf[0] - '0';
1323 if (0 > number || 2 < number)
1325 printf("\nInvalid Network type");
1326 return CA_NOT_SUPPORTED;
1329 g_selected_nw_type = 1 << number;
1331 return CA_STATUS_OK;
1334 CAResult_t get_input_data(char *buf, int32_t length)
1336 if (!fgets(buf, length, stdin))
1338 printf("fgets error\n");
1339 return CA_STATUS_FAILED;
1343 if ((p = strchr(buf, '\n')) != NULL)
1348 return CA_STATUS_OK;
1352 void parse_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
1356 printf("parameter is null\n");
1362 uint8_t startIndex = 0;
1363 if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
1365 printf("uri has '%s' prefix\n", COAPS_PREFIX);
1366 startIndex = COAPS_PREFIX_LEN;
1369 else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
1371 printf("uri has '%s' prefix\n", COAP_PREFIX);
1372 startIndex = COAP_PREFIX_LEN;
1376 // #2. copy uri for parse
1377 int32_t len = strlen(uri) - startIndex;
1381 printf("uri length is 0!\n");
1385 int res = get_address_set(uri + startIndex, address);
1388 printf("address parse error\n");
1395 int get_address_set(const char *uri, addressSet_t* outAddress)
1397 if (NULL == uri || NULL == outAddress)
1399 printf("parameter is null !\n");
1403 int32_t len = strlen(uri);
1406 printf("uri length is 0!\n");
1412 for (int i = 0; i < len; i++)
1419 // found port number start index
1420 if (isIp && uri[i] == ':')
1423 outAddress->port = atoi(uri + ipLen + 1);
1432 outAddress->ipAddress[i] = uri[i];