#include "cacommon.h"
#include "cainterface.h"
-#ifdef __WITH_DTLS__
-#include "ocsecurityconfig.h"
-#endif
/**
* @def MAX_BUF_LEN
* @brief maximum buffer length
#define RESOURCE_URI_LENGTH 14
+#define COAP_PREFIX "coap://"
+#define COAP_PREFIX_LEN 7
+#define COAPS_PREFIX "coaps://"
+#define COAPS_PREFIX_LEN 8
+
+#define COAP_TCP_PREFIX "coap+tcp://"
+#define COAP_TCP_PREFIX_LEN 11
+
+// Iotivity Device Identity.
+const unsigned char IDENTITY[] = ("1111111111111111");
+
+// PSK between this device and peer device.
+const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
+
/**
- * @def RS_IDENTITY
- * @brief
+ * Max size for big payload.
*/
-#define IDENTITY ("1111111111111111")
-/* @def RS_CLIENT_PSK
- * @brief
- */
-#define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
+#define BIG_PAYLOAD_SIZE 15000
static GMainLoop *g_mainloop = NULL;
pthread_t thread;
int g_received;
uint16_t g_local_secure_port = SECURE_DEFAULT_PORT;
-CATransportType_t g_selected_nw_type = CA_IPV4;
+CATransportAdapter_t g_selected_nw_type = CA_ADAPTER_IP;
const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"};
+typedef struct
+{
+ char ipAddress[CA_IPADDR_SIZE];
+ uint16_t port;
+} addressSet_t;
+
char get_menu();
void process();
CAResult_t get_network_type();
CAResult_t get_input_data(char *buf, int32_t length);
+bool select_payload();
+void populate_binary_payload(uint8_t *bigBuffer, size_t bigBufferLen);
+
void start_listening_server();
void start_discovery_server();
-void find_resource();
void send_request();
void send_request_all();
-void advertise_resource();
void send_notification();
void select_network();
void unselect_network();
void handle_request_response();
-void find_fixed_resource();
void get_network_info();
+void send_secure_request();
-void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
-void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
-void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info);
+void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
+void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
+void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
+void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info);
void get_resource_uri(char *URI, char *resourceURI, int length);
int get_secure_information(CAPayload_t payLoad);
+int get_address_set(const char *uri, addressSet_t* outAddress);
+void parse_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
static CAToken_t g_last_request_token = NULL;
static const char SECURE_COAPS_PREFIX[] = "coaps://";
"\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
#ifdef __WITH_DTLS__
-static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
-
-void clearDtlsCredentialInfo()
-{
- printf("clearDtlsCredentialInfo IN\n");
- if (pskCredsBlob)
- {
- // Initialize sensitive data to zeroes before freeing.
- if (pskCredsBlob->creds)
- {
- memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
- free(pskCredsBlob->creds);
- }
-
- memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
- free(pskCredsBlob);
- pskCredsBlob = NULL;
- }
- printf("clearDtlsCredentialInfo OUT\n");
-}
-
// Internal API. Invoked by CA stack to retrieve credentials from this module.
-void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
+int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
+ const unsigned char *desc, size_t desc_len,
+ unsigned char *result, size_t result_length)
{
printf("CAGetDtlsPskCredentials IN\n");
- if(NULL == credInfo)
- {
- printf("Invalid credential container");
- return;
- }
+ int32_t ret = -1;
- *credInfo = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
- if (NULL == *credInfo)
+ if (NULL == result)
{
- printf("Failed to allocate credential blob.");
- return;
+ return ret;
}
- int16_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
- (*credInfo)->creds = (OCDtlsPskCreds *)malloc(credLen);
- if (NULL == (*credInfo)->creds)
+ switch (type)
{
- printf("Failed to allocate credentials.");
- free(*credInfo);
- *credInfo = NULL;
- return;
- }
+ case CA_DTLS_PSK_HINT:
+ case CA_DTLS_PSK_IDENTITY:
- memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
- (*credInfo)->num = pskCredsBlob->num;
- memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
+ if (result_length < sizeof(IDENTITY))
+ {
+ printf("ERROR : Wrong value for result for storing IDENTITY");
+ return ret;
+ }
- printf("CAGetDtlsPskCredentials OUT\n");
-}
+ memcpy(result, IDENTITY, sizeof(IDENTITY));
+ ret = sizeof(IDENTITY);
+ break;
-CAResult_t SetCredentials()
-{
- printf("SetCredentials IN\n");
- pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
- if (NULL == pskCredsBlob)
- {
- printf("Memory allocation failed!\n");
- return CA_MEMORY_ALLOC_FAILED;
- }
- memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
+ case CA_DTLS_PSK_KEY:
- pskCredsBlob->num = 1;
+ if ((desc_len == sizeof(IDENTITY)) &&
+ memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
+ {
+ if (result_length < sizeof(RS_CLIENT_PSK))
+ {
+ printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
+ return ret;
+ }
- pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
- if (NULL == pskCredsBlob->creds)
- {
- printf("Memory allocation failed!\n");
- free(pskCredsBlob);
- return CA_MEMORY_ALLOC_FAILED;
- }
+ memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
+ ret = sizeof(RS_CLIENT_PSK);
+ }
+ break;
- memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
- memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
+ default:
- printf("SetCredentials OUT\n");
- return CA_STATUS_OK;
+ printf("Wrong value passed for PSK_CRED_TYPE.");
+ ret = -1;
+ }
+
+ printf("CAGetDtlsPskCredentials OUT\n");
+ return ret;
}
#endif
return -1;
}
- /*
- * Read DTLS PSK credentials from persistent storage and
- * set in the OC stack.
- */
+ // Set the PSK Credentials callback handler.
#ifdef __WITH_DTLS__
- res = SetCredentials();
- if (CA_STATUS_OK != res)
- {
- printf("SetCredentials failed\n");
- return -1;
- }
-
res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
if (CA_STATUS_OK != res)
{
#endif
// set handler.
- CARegisterHandler(request_handler, response_handler);
+ CARegisterHandler(request_handler, response_handler, error_handler);
process();
CADestroyToken(g_last_request_token);
CATerminate();
-#ifdef __WITH_DTLS__
- clearDtlsCredentialInfo();
-#endif
return 0;
}
start_discovery_server();
break;
- case 'f': // find resource
- case 'F':
- find_resource();
- break;
-
case 'r': // send request
case 'R':
send_request();
break;
- case 'a': // advertise resource
- case 'A':
- advertise_resource();
- break;
-
case 'b': // send notification
case 'B':
send_notification();
handle_request_response();
break;
- case 'y':
- case 'Y':
- while (1)
- {
- g_received = 0;
- find_fixed_resource();
- while (g_received == 0)
- {
- sleep(1);
- handle_request_response();
-
- }
- }
- break;
-
case 'w':
case 'W':
g_received = 0;
start_discovery_server();
- //send_secure_request();
+ send_secure_request();
while (g_received == 0)
{
sleep(1);
}
}
-void find_fixed_resource()
+bool select_payload()
{
- // create token
- CAToken_t token = NULL;
- uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
- CAResult_t res = CAGenerateToken(&token, tokenLength);
- if ((CA_STATUS_OK != res) || (!token))
- {
- printf("Token generate error!!");
- return;
- }
-
- printf("Generated token %s\n", token);
-
- char buf[MAX_BUF_LEN] = { 0 };
- strcpy(buf, "/a/light");
+ char buf[MAX_BUF_LEN]={0};
+ printf("\n=============================================\n");
+ printf("0:Normal Payload\n1:Big Payload(~15KB)\n");
+ printf("select Payload type : ");
- res = CAFindResource(buf, token, tokenLength);
+ CAResult_t res = get_input_data(buf, sizeof(buf));
if (CA_STATUS_OK != res)
{
- printf("Find resource error : %d\n", res);
+ printf("Payload type selection error\n");
+ printf("Default: Using normal Payload\n");
+ return false;
}
- else
- {
- printf("Find resource to %s URI\n", buf);
- }
-
- // delete token
- CADestroyToken(token);
- printf("=============================================\n");
+ return (buf[0] == '1') ? true : false;
}
-void find_resource()
+void populate_binary_payload(uint8_t *bigBuffer, size_t bigBufferLen)
{
- printf("\n=============================================\n");
- printf("ex) /a/light\n");
- printf("reference uri : ");
-
- char buf[MAX_BUF_LEN] = { 0 };
- if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
- {
- return;
- }
-
- // create token
- CAToken_t token = NULL;
- uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
- CAResult_t res = CAGenerateToken(&token, tokenLength);
- if ((CA_STATUS_OK != res) || (!token))
- {
- printf("Token generate error!!\n");
- return;
- }
-
- printf("Generated token %s\n", token);
-
- res = CAFindResource(buf, token, tokenLength);
- if (CA_STATUS_OK != res)
- {
- printf("Find resource error : %d\n", res);
- CADestroyToken(token);
- }
- else
- {
- printf("Find resource to %s URI\n", buf);
- CADestroyToken(g_last_request_token);
- g_last_request_token = token;
- }
-
- printf("=============================================\n");
+/**
+ * bigBuffer to be filled with binary data. For our sample application to verify, we may fill with
+ * any arbitrary value. Hence filling with '1' here.
+ */
+ memset(bigBuffer, '1', bigBufferLen-1);
+ //Last byte making NULL
+ bigBuffer[bigBufferLen-1] = '\0';
}
void send_request()
printf("Enter the URI like below....\n");
printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
+ printf("coap+tcp://10:11:12:13:45:45/resource_uri ( for TCP )\n");
}
else
{
}
// create remote endpoint
- CARemoteEndpoint_t *endpoint = NULL;
- res = CACreateRemoteEndpoint(uri, g_selected_nw_type, &endpoint);
+ CAEndpoint_t *endpoint = NULL;
+ CATransportFlags_t flags;
+
+ printf("URI : %s\n", uri);
+ addressSet_t address = {};
+ parse_coap_uri(uri, &address, &flags);
+
+ res = CACreateEndpoint(flags, g_selected_nw_type,
+ (const char*)address.ipAddress, address.port, &endpoint);
if (CA_STATUS_OK != res || !endpoint)
{
printf("Failed to create remote endpoint, error code : %d\n", res);
char buf[MAX_BUF_LEN] = { 0 };
if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
{
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
return;
}
if ((CA_STATUS_OK != res) || (!token))
{
printf("Token generate error, error code : %d\n", res);
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
return;
}
printf("Generated token %s\n", token);
// extract relative resourceuri from give uri
- printf("URI : %s\n", uri);
-
- char resourceURI[15] = {0};
+ char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
+ printf("resourceURI : %s\n", resourceURI);
// create request data
CAInfo_t requestData = { 0 };
requestData.token = token;
requestData.tokenLength = tokenLength;
+ requestData.resourceUri = (CAURI_t)resourceURI;
if (strcmp(secureRequest, "1") == 0)
{
- uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
+ size_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
if (NULL == requestData.payload)
{
printf("Memory allocation fail\n");
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
CADestroyToken(token);
return;
}
- snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_local_secure_port);
+ snprintf((char *) requestData.payload, length, SECURE_INFO_DATA,
+ (const char *) resourceURI, g_local_secure_port);
}
else
{
- uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
- requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
- if (NULL == requestData.payload)
+ bool useBigPayload = select_payload();
+ if (useBigPayload)
{
- printf("Memory allocation fail\n");
- CADestroyRemoteEndpoint(endpoint);
- CADestroyToken(token);
- return;
+ requestData.payload = (CAPayload_t) calloc(BIG_PAYLOAD_SIZE, sizeof(char));
+ if (NULL == requestData.payload)
+ {
+ printf("Memory allocation fail\n");
+ CADestroyEndpoint(endpoint);
+ CADestroyToken(token);
+ return;
+ }
+ populate_binary_payload(requestData.payload, BIG_PAYLOAD_SIZE);
+ }
+ else
+ {
+ size_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
+ requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
+ if (NULL == requestData.payload)
+ {
+ printf("Memory allocation fail\n");
+ CADestroyEndpoint(endpoint);
+ CADestroyToken(token);
+ return;
+ }
+ snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA,
+ (const char *) resourceURI);
}
- snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
}
+ requestData.payloadSize = strlen((char *)requestData.payload)+1;
requestData.type = msgType;
CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
requestInfo.info = requestData;
+ requestInfo.isMulticast = false;
// send request
res = CASendRequest(endpoint, &requestInfo);
//destroy token
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
free(requestData.payload);
printf("=============================================\n");
}
-void send_request_all()
+void send_secure_request()
{
- CAResult_t res = get_network_type();
- if (CA_STATUS_OK != res)
- {
- return;
- }
+ char ipv4addr[CA_IPADDR_SIZE];
printf("\n=============================================\n");
- printf("ex) /a/light\n");
- printf("resource uri : ");
+ printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");
- char buf[MAX_BUF_LEN] = { 0 };
- if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ if (CA_STATUS_OK != get_input_data(ipv4addr, CA_IPADDR_SIZE))
{
return;
}
+ printf("%s%s:5684/a/light", SECURE_COAPS_PREFIX, ipv4addr);
// create remote endpoint
- CARemoteEndpoint_t *endpoint = NULL;
- res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
+ CAEndpoint_t *endpoint = NULL;
+ CAResult_t res = CACreateEndpoint(0, CA_ADAPTER_IP, ipv4addr, SECURE_DEFAULT_PORT, &endpoint);
if (CA_STATUS_OK != res)
{
- printf("Create remote endpoint error, error code: %d\n", res);
- return;
- }
-
- CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
- if (NULL == group)
- {
- printf("Memory allocation failed!\n");
- CADestroyRemoteEndpoint(endpoint);
- return;
+ printf("Failed to create remote endpoint, error code: %d\n", res);
+ goto exit;
}
- group->transportType = endpoint->transportType;
- group->resourceUri = endpoint->resourceUri;
// create token
CAToken_t token = NULL;
res = CAGenerateToken(&token, tokenLength);
if ((CA_STATUS_OK != res) || (!token))
{
- printf("Token generate error!!\n");
- CADestroyRemoteEndpoint(endpoint);
- free(group);
- return;
+ printf("Token generate error, error code : %d\n", res);
+ goto exit;
}
- printf("generated token %s\n", token);
+ printf("Generated token %s\n", token);
- CAInfo_t requestData = {CA_MSG_RESET};
+ // create request data
+ CAMessageType_t msgType = CA_MSG_NONCONFIRM;
+ CAInfo_t requestData = { 0 };
requestData.token = token;
requestData.tokenLength = tokenLength;
+ requestData.type = msgType;
requestData.payload = "Temp Json Payload";
- requestData.type = CA_MSG_NONCONFIRM;
+ requestData.payloadSize = strlen(requestData.payload)+1;
- CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
+ CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
requestInfo.info = requestData;
+ requestInfo.isMulticast = false;
- // send request all
- res = CASendRequestToAll(group, &requestInfo);
- if (CA_STATUS_OK != res)
- {
- printf("Could not send request to all\n");
- CADestroyToken(token);
- }
- else
- {
- CADestroyToken(g_last_request_token);
- g_last_request_token = token;
- }
-
- // destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
- free(group);
+ // send request
+ CASendRequest(endpoint, &requestInfo);
+exit:
+ // cleanup
+ CADestroyToken(token);
+ CADestroyEndpoint(endpoint);
printf("=============================================\n");
}
-void advertise_resource()
-{
- printf("\n=============================================\n");
- printf("uri : ");
- char buf[MAX_BUF_LEN] = { 0 };
- if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+void send_request_all()
+{
+ CAResult_t res = get_network_type();
+ if (CA_STATUS_OK != res)
{
return;
}
- char optionNumBuf[MAX_BUF_LEN] = { 0 };
- char optionData[MAX_OPT_LEN] = { 0 } ;
+ printf("\n=============================================\n");
+ printf("ex) /a/light\n");
+ printf("resource uri : ");
- printf("Option Num : ");
- if (CA_STATUS_OK != get_input_data(optionNumBuf, MAX_BUF_LEN))
+ char resourceURI[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN))
{
return;
}
- int optionNum = atoi(optionNumBuf);
- CAHeaderOption_t * headerOpt = (CAHeaderOption_t *)
- calloc(1, optionNum * sizeof(CAHeaderOption_t));
- if (NULL == headerOpt)
+ // create remote endpoint
+ CAEndpoint_t *endpoint = NULL;
+ res = CACreateEndpoint(CA_IPV4, g_selected_nw_type, NULL, 0, &endpoint);
+ if (CA_STATUS_OK != res)
{
- printf("Memory allocation failed!\n");
+ printf("Create remote endpoint error, error code: %d\n", res);
return;
}
- int i;
- for (i = 0; i < optionNum; i++)
- {
- char getOptionID[MAX_BUF_LEN] = { 0 } ;
-
- printf("[%d] Option ID : ", i + 1);
- if (CA_STATUS_OK != get_input_data(getOptionID, MAX_BUF_LEN))
- {
- free(headerOpt);
- return;
- }
- int optionID = atoi(getOptionID);
-
- headerOpt[i].optionID = optionID;
-
- printf("[%d] Option Data : ", i + 1);
- if (CA_STATUS_OK != get_input_data(optionData, MAX_OPT_LEN))
- {
- free(headerOpt);
- return;
- }
-
- memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
- printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
-
- headerOpt[i].optionLength = (uint16_t) strlen(optionData);
- }
- printf("\n=============================================\n");
-
// create token
CAToken_t token = NULL;
uint8_t tokenLength = CA_MAX_TOKEN_LEN;
- CAResult_t res = CAGenerateToken(&token, tokenLength);
+ res = CAGenerateToken(&token, tokenLength);
if ((CA_STATUS_OK != res) || (!token))
{
printf("Token generate error!!\n");
- free(headerOpt);
+ CADestroyEndpoint(endpoint);
return;
}
- printf("Generated token %s\n", token);
+ printf("generated token %s\n", token);
+
+ CAInfo_t requestData = { 0 };
+ requestData.token = token;
+ requestData.tokenLength = tokenLength;
+ requestData.payload = (CAPayload_t) "TempJsonPayload";
+ requestData.payloadSize = strlen((const char *) requestData.payload);
+ requestData.type = CA_MSG_NONCONFIRM;
+ requestData.resourceUri = (CAURI_t)resourceURI;
+
+ CARequestInfo_t requestInfo = { 0 };
+ requestInfo.method = CA_GET;
+ requestInfo.info = requestData;
+ requestInfo.isMulticast = true;
- res = CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t) optionNum);
+ // send request
+ res = CASendRequest(endpoint, &requestInfo);
if (CA_STATUS_OK != res)
{
- printf("Could not start advertise resource\n");
- CADestroyToken(token);
+ printf("Could not send request to all\n");
}
else
{
g_last_request_token = token;
}
- free(headerOpt);
+ // destroy remote endpoint
+ CADestroyEndpoint(endpoint);
+
+ printf("=============================================\n");
}
void send_notification()
printf("\n=============================================\n");
printf("Enter the URI like below....\n");
- printf("10.11.12.13:4545/resource_uri ( for IP )\n");
- printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
+ printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
+ printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
printf("uri : ");
- char buf[MAX_BUF_LEN] = { 0 };
- if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ char uri[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
{
return;
}
printf("\tselect message type\n");
printf("CON : 0\n");
printf("NON : 1\n");
+ printf("ACK : 2\n");
+ printf("RESET : 3\n");
+
printf("select : ");
char messageTypeBuf[MAX_BUF_LEN] = { 0 };
return;
}
+ CATransportFlags_t flags;
+ addressSet_t address = {};
+ parse_coap_uri(uri, &address, &flags);
+
// create remote endpoint
- CARemoteEndpoint_t *endpoint = NULL;
- res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
+ CAEndpoint_t *endpoint = NULL;
+ res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
if (CA_STATUS_OK != res)
{
printf("Create remote endpoint error, error code: %d\n", res);
if ((CA_STATUS_OK != res) || (!token))
{
printf("Token generate error!!\n");
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
return;
}
printf("Generated token %s\n", token);
- CAInfo_t respondData = { 0 };
- respondData.token = token;
- respondData.tokenLength = tokenLength;
- respondData.payload = "Temp Notification Data";
- respondData.type = messageType;
+ CAInfo_t requestData = { 0 };
+ requestData.token = token;
+ requestData.tokenLength = tokenLength;
+ requestData.payload = (CAPayload_t) "TempNotificationData";
+ requestData.payloadSize = strlen((const char *) requestData.payload);
+ requestData.type = messageType;
+ requestData.resourceUri = (CAURI_t)uri;
- CAResponseInfo_t responseInfo = { 0 };
- responseInfo.result = CA_SUCCESS;
- responseInfo.info = respondData;
+ CARequestInfo_t requestInfo = { 0 };
+ requestInfo.method = CA_GET;
+ requestInfo.info = requestData;
// send notification
- res = CASendNotification(endpoint, &responseInfo);
+ res = CASendRequest(endpoint, &requestInfo);
if (CA_STATUS_OK != res)
{
printf("Send notification error, error code: %d\n", res);
// destroy token
CADestroyToken(token);
// destroy remote endpoint
- CADestroyRemoteEndpoint(endpoint);
+ CADestroyEndpoint(endpoint);
printf("\n=============================================\n");
}
{
printf("\n=============================================\n");
printf("\tselect network\n");
- printf("IPv4 : 0\n");
- printf("EDR : 2\n");
- printf("LE : 3\n");
+ printf("IP : 0\n");
+ printf("GATT : 1\n");
+ printf("RFCOMM : 2\n");
+ printf("TCP : 4\n");
printf("select : ");
char buf[MAX_BUF_LEN] = { 0 };
int number = buf[0] - '0';
- if (number < 0 || number > 3)
+ if (number < 0 || number > 4)
{
printf("Invalid network type\n");
return;
{
printf("Select network success\n");
}
-
printf("=============================================\n");
}
{
printf("\n=============================================\n");
printf("\tunselect enabled network\n");
- printf("IPv4 : 0\n");
- printf("EDR : 2\n");
- printf("LE : 3\n");
+ printf("IP : 0\n");
+ printf("GATT : 1\n");
+ printf("RFCOMM : 2\n");
+ printf("TCP : 4\n");
printf("select : ");
char buf[MAX_BUF_LEN] = { 0 };
int number = buf[0] - '0';
- if (number < 0 || number > 3)
+ if (number < 0 || number > 4)
{
printf("Invalid network type\n");
return;
printf("\t\tMenu\n");
printf("\ts : start server\n");
printf("\tc : start client\n");
- printf("\tf : find resource\n");
printf("\tr : send request\n");
printf("\tt : send request to all\n");
- printf("\ta : advertise resource\n");
printf("\tb : send notification\n");
printf("\tn : select network\n");
printf("\tx : unselect network\n");
printf("\tg : get network information\n");
printf("\th : handle request response\n");
- printf("\ty : run static client\n");
printf("\tz : run static server\n");
printf("\tw : send secure request\n");
printf("\tq : quit\n");
void get_network_info()
{
- CALocalConnectivity_t *tempInfo = NULL;
+ CAEndpoint_t *tempInfo = NULL;
uint32_t tempSize = 0;
CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
int index;
for (index = 0; index < tempSize; index++)
{
- printf("Type: %d\n", tempInfo[index].type);
- if (CA_IPV4 == tempInfo[index].type)
+ printf("Type: %d\n", tempInfo[index].adapter);
+ if (CA_ADAPTER_IP == tempInfo[index].adapter)
{
- printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
- printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
+ printf("Address: %s\n", tempInfo[index].addr);
+ printf("Port: %d\n", tempInfo[index].port);
}
- else if (CA_EDR == tempInfo[index].type)
+ else
{
- printf("Address: %s\n", tempInfo[index].addressInfo.BT.btMacAddress);
+ printf("Address: %s\n", tempInfo[index].addr);
}
- printf("Secured: %d\n\n", tempInfo[index].isSecured);
- if (tempInfo[index].isSecured)
+ printf("Secured: %s\n\n", (tempInfo[index].flags & CA_SECURE) ? "true" : "false");
+
+ if (tempInfo[index].flags & CA_SECURE)
{
- g_local_secure_port = tempInfo[index].addressInfo.IP.port;
+ g_local_secure_port = tempInfo[index].port;
printf("Secured: in global %d\n\n", g_local_secure_port);
}
}
printf("##############################################################");
}
-void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
+void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
{
if (NULL == object || NULL == requestInfo)
{
}
printf("##########received request from remote device #############\n");
- printf("Uri: %s\n", object->resourceUri);
- if (CA_IPV4 == object->transportType)
+ if (CA_ADAPTER_IP == object->adapter)
{
- printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
- object->addressInfo.IP.port, object->isSecured);
+ printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
+ object->port, object->flags & CA_SECURE);
}
- else if (CA_EDR == object->transportType)
+ else
{
- printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
+ printf("Remote Address: %s \n", object->addr);
}
printf("Data: %s\n", requestInfo->info.payload);
printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
//Check if this has secure communication information
if (requestInfo->info.payload &&
- (CA_IPV4 == object->transportType))
+ (CA_ADAPTER_IP == object->adapter))
{
int securePort = get_secure_information(requestInfo->info.payload);
if (0 < securePort) //Set the remote endpoint secure details and send response
{
printf("This is secure resource...\n");
- //length of "coaps://"
- int length = sizeof(SECURE_COAPS_PREFIX) - 1;
-
- // length of "ipaddress:port"
- length += strlen(object->addressInfo.IP.ipAddress) + PORT_LENGTH;
- length += strlen(object->resourceUri) + 1;
-
- char *uri = calloc(1, sizeof(char) * length);
- if (!uri)
- {
- printf("Failed to create new uri\n");
- return;
- }
- sprintf(uri, "%s%s:%d/%s", SECURE_COAPS_PREFIX, object->addressInfo.IP.ipAddress,
- object->addressInfo.IP.port, object->resourceUri);
-
- CARemoteEndpoint_t *endpoint = NULL;
- if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->transportType, &endpoint))
+ CAEndpoint_t *endpoint = NULL;
+ if (CA_STATUS_OK != CACreateEndpoint(0, object->adapter, object->addr,
+ object->port, &endpoint))
{
printf("Failed to create duplicate of remote endpoint!\n");
return;
}
- endpoint->isSecured = true;
+ endpoint->flags = CA_SECURE;
object = endpoint;
-
- free(uri);
}
}
g_received = 1;
}
-void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
+void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
{
printf("##########Received response from remote device #############\n");
- printf("Uri: %s\n", object->resourceUri);
- if (CA_IPV4 == object->transportType)
+ if (CA_ADAPTER_IP == object->adapter)
{
- printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
- object->addressInfo.IP.port, object->isSecured);
+ printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
+ object->port, object->flags & CA_SECURE);
}
- else if (CA_EDR == object->transportType)
+ else
{
- printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
+ printf("Remote Address: %s \n", object->addr);
}
+
+ printf("resource uri : %s\n", responseInfo->info.resourceUri);
printf("response result : %d\n", responseInfo->result);
printf("Data: %s\n", responseInfo->info.payload);
printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
}
}
-void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
+void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
+{
+ printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++\n");
+
+ if(errorInfo)
+ {
+ const CAInfo_t *info = &errorInfo->info;
+ printf("Error Handler, ErrorInfo :\n");
+ printf("Error Handler result : %d\n", errorInfo->result);
+ printf("Error Handler token : %s\n", info->token);
+ printf("Error Handler messageId : %d\n", (uint16_t) info->messageId);
+ printf("Error Handler type : %d\n", info->type);
+ printf("Error Handler resourceUri : %s\n", info->resourceUri);
+ printf("Error Handler payload : %s\n", info->payload);
+
+ if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
+ {
+ printf("CA_ADAPTER_NOT_ENABLED, enable the adapter\n");
+ }
+ else if(CA_SEND_FAILED == errorInfo->result)
+ {
+ printf("CA_SEND_FAILED, unable to send the message, check parameters\n");
+ }
+ else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
+ {
+ printf("CA_MEMORY_ALLOC_FAILED, insufficient memory\n");
+ }
+ else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
+ {
+ printf("CA_SOCKET_OPERATION_FAILED, socket operation failed\n");
+ }
+ else if(CA_STATUS_FAILED == errorInfo->result)
+ {
+ printf("CA_STATUS_FAILED, message could not be delivered, internal error\n");
+ }
+ }
+ printf("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++\n");
+
+ return;
+}
+
+void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info)
{
printf("entering send_response\n");
printf("\n=============================================\n");
printf("\tselect response code\n");
printf("EMPTY : 0\n");
- printf("SUCCESS : 200\n");
printf("CREATED : 201\n");
printf("DELETED : 202\n");
+ printf("VALID : 203\n");
+ printf("CHANGED : 204\n");
+ printf("CONTENT : 205\n");
printf("BAD_REQ : 400\n");
printf("BAD_OPT : 402\n");
printf("NOT_FOUND : 404\n");
}
CAInfo_t responseData = { 0 };
responseData.type = messageType;
-
responseData.messageId = (info != NULL) ? info->messageId : 0;
+ responseData.resourceUri = (info != NULL) ? info->resourceUri : 0;
+
if(CA_MSG_RESET != messageType)
{
responseData.token = (info != NULL) ? info->token : NULL;
responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
- if (endpoint->isSecured)
+ if (endpoint->flags & CA_SECURE)
{
printf("Sending response on secure communication\n");
- uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(endpoint->resourceUri);
+ uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri)
+ + sizeof(g_local_secure_port);
responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
if (NULL == responseData.payload)
{
printf("Memory allocation fail\n");
return;
}
- snprintf(responseData.payload, length, SECURE_INFO_DATA, endpoint->resourceUri,
- g_local_secure_port);
+ snprintf((char *) responseData.payload, length, SECURE_INFO_DATA,
+ (const char *) responseData.resourceUri, g_local_secure_port);
}
else
{
printf("Sending response on non-secure communication\n");
- uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(endpoint->resourceUri);
- responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
- if (NULL == responseData.payload)
+ bool useBigPayload = select_payload();
+ if (useBigPayload)
{
- printf("Memory allocation fail\n");
- return;
+ responseData.payload = (CAPayload_t) calloc(BIG_PAYLOAD_SIZE, sizeof(char));
+ if (NULL == responseData.payload)
+ {
+ printf("Memory allocation fail\n");
+ return;
+ }
+ populate_binary_payload(responseData.payload, BIG_PAYLOAD_SIZE);
+ }
+ else
+ {
+ size_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri);
+ responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
+ if (NULL == responseData.payload)
+ {
+ printf("Memory allocation fail\n");
+ return;
+ }
+ snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA,
+ (const char *) responseData.resourceUri);
}
- snprintf(responseData.payload, length, NORMAL_INFO_DATA, endpoint->resourceUri);
}
}
-
+ responseData.payloadSize = strlen((char *)responseData.payload)+1;
CAResponseInfo_t responseInfo = { 0 };
responseInfo.result = responseCode;
responseInfo.info = responseData;
}
char *subString = NULL;
- if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
+ if (NULL == (subString = strstr((const char *) payLoad, "\"sec\":1")))
{
printf("This is not secure resource\n");
return -1;
}
- if (NULL == (subString = strstr(payLoad, "\"port\":")))
+ if (NULL == (subString = strstr((const char *) payLoad, "\"port\":")))
{
printf("This secure resource does not have port information\n");
return -1;
return -1;
}
- if(((endPos - 1) - startPos) > 4)
- {
- printf("port length is not proper.Exceeding length 4\n");
- return -1;
- }
-
- char portStr[4] = {0};
+ char portStr[6] = {0};
memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
printf("secured port is: %s\n", portStr);
{
printf("\n=============================================\n");
printf("\tselect network type\n");
- printf("IPv4 : 0\n");
- printf("BT : 2\n");
- printf("LE : 3\n");
+ printf("IP : 0\n");
+ printf("GATT : 1\n");
+ printf("RFCOMM : 2\n");
+ printf("TCP : 4\n");
printf("select : ");
char buf[MAX_BUF_LEN] = { 0 };
}
int number = buf[0] - '0';
- number = (number < 0 || number > 3) ? 0 : 1 << number;
-
- if (!(number & 0xf))
+ if (0 > number || 4 < number)
{
+ printf("\nInvalid Network type");
return CA_NOT_SUPPORTED;
}
- if (number & CA_IPV4)
- {
- g_selected_nw_type = CA_IPV4;
- return CA_STATUS_OK;
- }
- if (number & CA_EDR)
- {
- g_selected_nw_type = CA_EDR;
- return CA_STATUS_OK;
- }
- if (number & CA_LE)
- {
- g_selected_nw_type = CA_LE;
- return CA_STATUS_OK;
- }
- printf("\n=============================================\n");
+ g_selected_nw_type = 1 << number;
- return CA_STATUS_FAILED;
+ return CA_STATUS_OK;
}
CAResult_t get_input_data(char *buf, int32_t length)
return CA_STATUS_OK;
}
+
+
+void parse_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
+{
+ if (NULL == uri)
+ {
+ printf("parameter is null\n");
+ return;
+ }
+
+ // parse uri
+ // #1. check prefix
+ uint8_t startIndex = 0;
+ if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
+ {
+ printf("uri has '%s' prefix\n", COAPS_PREFIX);
+ startIndex = COAPS_PREFIX_LEN;
+ *flags = CA_SECURE;
+ }
+ else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
+ {
+ printf("uri has '%s' prefix\n", COAP_PREFIX);
+ startIndex = COAP_PREFIX_LEN;
+ *flags = CA_IPV4;
+ }
+ else if (strncmp(COAP_TCP_PREFIX, uri, COAP_TCP_PREFIX_LEN) == 0)
+ {
+ printf("uri has '%s' prefix\n", COAP_TCP_PREFIX);
+ startIndex = COAP_TCP_PREFIX_LEN;
+ *flags = CA_IPV4;
+ }
+
+ // #2. copy uri for parse
+ int32_t len = strlen(uri) - startIndex;
+
+ if (len <= 0)
+ {
+ printf("uri length is 0!\n");
+ return;
+ }
+
+ int res = get_address_set(uri + startIndex, address);
+ if (res == -1)
+ {
+ printf("address parse error\n");
+ return;
+ }
+
+ return;
+}
+
+int get_address_set(const char *uri, addressSet_t* outAddress)
+{
+ if (NULL == uri || NULL == outAddress)
+ {
+ printf("parameter is null !\n");
+ return -1;
+ }
+
+ int32_t len = strlen(uri);
+ if (len <= 0)
+ {
+ printf("uri length is 0!\n");
+ return -1;
+ }
+
+ int32_t isIp = 0;
+ int32_t ipLen = 0;
+ for (int i = 0; i < len; i++)
+ {
+ if (uri[i] == '.')
+ {
+ isIp = 1;
+ }
+
+ // found port number start index
+ if (isIp && uri[i] == ':')
+ {
+ ipLen = i;
+ outAddress->port = atoi(uri + ipLen + 1);
+ break;
+ }
+
+ if (uri[i] == '/')
+ {
+ break;
+ }
+
+ outAddress->ipAddress[i] = uri[i];
+ }
+
+ return isIp;
+}