*
******************************************************************/
-#include <ctype.h>
-#include <fcntl.h>
-#include <errno.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
+#include <stdbool.h>
#include <pthread.h>
#include "cacommon.h"
#include "cainterface.h"
-static GMainLoop *mainloop;
-static GIOChannel *channel;
-static guint g_test_io_watch_id;
-static GError *g_err_Sample;
+#ifdef __WITH_DTLS__
+#include "ocsecurityconfig.h"
+#endif
+/**
+ * @def MAX_BUF_LEN
+ * @brief maximum buffer length
+ */
+#define MAX_BUF_LEN 1024
+
+/**
+ * @def MAX_OPT_LEN
+ * @brief maximum option length
+ */
+#define MAX_OPT_LEN 16
+/**
+ * @def PORT_LENGTH
+ * @brief maximum port length
+ */
+#define PORT_LENGTH 5
+
+/**
+ * @def SECURE_DEFAULT_PORT
+ * @brief default secured port
+ */
+#define SECURE_DEFAULT_PORT 5684
+
+#define RESOURCE_URI_LENGTH 14
+
+/**
+ * @def RS_IDENTITY
+ * @brief
+ */
+#define IDENTITY ("1111111111111111")
+/* @def RS_CLIENT_PSK
+ * @brief
+ */
+#define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
+
+static GMainLoop *g_mainloop = NULL;
pthread_t thread;
-#define MAX_BUF_LEN 1024
-#define MAX_OPT_LEN 16
+int g_received;
+uint16_t g_local_secure_port = SECURE_DEFAULT_PORT;
+CATransportType_t g_selected_nw_type = CA_IPV4;
+const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"};
char get_menu();
void process();
+CAResult_t get_network_type();
+CAResult_t get_input_data(char *buf, int32_t length);
-void initialize();
void start_listening_server();
void start_discovery_server();
void find_resource();
void send_request();
-void send_response();
-void advertise_resource();
+void send_request_all();
void send_notification();
void select_network();
void unselect_network();
void handle_request_response();
+void find_fixed_resource();
+void get_network_info();
+
+void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
+void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
+void error_handler(const CARemoteEndpoint_t *object, const CAErrorInfo_t* errorInfo);
+
+void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info);
+void get_resource_uri(char *URI, char *resourceURI, int length);
+int get_secure_information(CAPayload_t payLoad);
+
+static CAToken_t g_last_request_token = NULL;
+static const char SECURE_COAPS_PREFIX[] = "coaps://";
+static const char SECURE_INFO_DATA[] =
+ "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
+ "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
+static const char NORMAL_INFO_DATA[] =
+ "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
+ "\"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);
+ }
-void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
-void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
-void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
-void terminate();
+ memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
+ free(pskCredsBlob);
+ pskCredsBlob = NULL;
+ }
+ printf("clearDtlsCredentialInfo OUT\n");
+}
-void pthread_func()
+// Internal API. Invoked by CA stack to retrieve credentials from this module.
+void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
{
- g_main_loop_run(mainloop);
+ printf("CAGetDtlsPskCredentials IN\n");
+
+ if(NULL == credInfo)
+ {
+ printf("Invalid credential container");
+ return;
+ }
+
+ *credInfo = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
+ if (NULL == *credInfo)
+ {
+ printf("Failed to allocate credential blob.");
+ return;
+ }
+
+ int16_t credLen = sizeof(OCDtlsPskCreds) * (pskCredsBlob->num);
+ (*credInfo)->creds = (OCDtlsPskCreds *)malloc(credLen);
+ if (NULL == (*credInfo)->creds)
+ {
+ printf("Failed to allocate credentials.");
+ free(*credInfo);
+ *credInfo = NULL;
+ return;
+ }
+
+ memcpy((*credInfo)->identity, pskCredsBlob->identity, DTLS_PSK_ID_LEN);
+ (*credInfo)->num = pskCredsBlob->num;
+ memcpy((*credInfo)->creds, pskCredsBlob->creds, credLen);
+
+ printf("CAGetDtlsPskCredentials OUT\n");
+}
+
+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);
+
+ pskCredsBlob->num = 1;
+
+ 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(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
+ memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
+
+ printf("SetCredentials OUT\n");
+ return CA_STATUS_OK;
+}
+#endif
+
+void GMainLoopThread()
+{
+ g_main_loop_run(g_mainloop);
+}
+
+CAResult_t Initialize()
+{
+ g_mainloop = g_main_loop_new(NULL, FALSE);
+ if(!g_mainloop)
+ {
+ printf("g_main_loop_new failed\n");
+ return CA_STATUS_FAILED;
+ }
+
+ int result = pthread_create(&thread, NULL, (void *) &GMainLoopThread, NULL);
+ if (result < 0)
+ {
+ printf("pthread_create failed in initialize\n");
+ return CA_STATUS_FAILED;
+ }
+
+ CAResult_t res = CAInitialize();
+ if (res != CA_STATUS_OK)
+ {
+ printf("CAInitialize fail\n");
+ }
+ return res;
}
int main()
{
- system("clear");
+ int ret = system("clear");
+ // shell invoke error: 127, others: -1
+ if (127 == ret || -1 == ret)
+ {
+ printf("Terminal Clear Error: %d\n", ret);
+ }
printf("=============================================\n");
printf("\t\tsample main\n");
printf("=============================================\n");
+ CAResult_t res = Initialize();
+ if (CA_STATUS_OK != res)
+ {
+ printf("Initialization is failed\n");
+ return -1;
+ }
+
+ /*
+ * Read DTLS PSK credentials from persistent storage and
+ * set in the OC stack.
+ */
+#ifdef __WITH_DTLS__
+ res = SetCredentials();
+ if (CA_STATUS_OK != res)
+ {
+ printf("SetCredentials failed\n");
+ return -1;
+ }
+
+ res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Set credential handler fail\n");
+ return -1;
+ }
+#endif
+
+ // set handler.
+ CARegisterHandler(request_handler, response_handler, error_handler);
+
process();
+ CADestroyToken(g_last_request_token);
+
+ CATerminate();
+#ifdef __WITH_DTLS__
+ clearDtlsCredentialInfo();
+#endif
return 0;
}
{
while (1)
{
- char menu = toupper(get_menu());
+ char menu = get_menu();
switch (menu)
{
- case 'Q': // quits the sample program
+ case 'm': // menu
+ case 'M':
+ break;
+
+ case 'q': // quit
+ case 'Q':
printf("quit..!!\n");
- g_main_loop_quit(mainloop);
return;
- case 'I': // Initialize interface
- initialize();
-
- case 'S': // start server
+ case 's': // start server
+ case 'S':
start_listening_server();
break;
- case 'C': // start client
+ case 't': // send request
+ case 'T':
+ send_request_all();
+ break;
+
+ case 'c': // start client
+ case 'C':
start_discovery_server();
break;
- case 'F': // find resource
+ case 'f': // find resource
+ case 'F':
find_resource();
break;
- case 'R': // send request
+ case 'r': // send request
+ case 'R':
send_request();
break;
- case 'A': // advertise resource
- advertise_resource();
+ case 'b': // send notification
+ case 'B':
+ send_notification();
break;
- case 'N': // select network
+ case 'n': // select network
+ case 'N':
select_network();
break;
- case 'X': // unselect network
+ case 'x': // unselect network
+ case 'X':
unselect_network();
break;
- case 'H': // handle request response
+ case 'h': // handle request response
+ case 'H':
handle_request_response();
break;
- case 'T': // Terminate interface
- terminate();
+ 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();
+ while (g_received == 0)
+ {
+ sleep(1);
+ handle_request_response();
+ }
+ break;
+
+ case 'z':
+ case 'Z':
+ start_listening_server();
+ while (1)
+ {
+ sleep(1);
+ handle_request_response();
+ }
+ break;
+
+ case 'g': // get network information
+ case 'G':
+ get_network_info();
break;
default:
- printf("not supported menu!!\n");
+ printf("Not supported menu!!\n");
break;
}
}
}
-void initialize()
+void start_listening_server()
{
- mainloop = g_main_loop_new(NULL, FALSE);
- pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
-
- CAInitialize();
+ printf("Start listening server!!\n");
- // network enable
- // default
- printf("select default network(WIFI)\n");
- CASelectNetwork(CA_WIFI);
-
- // set handler.
- CARegisterHandler(request_handler, response_handler);
+ CAResult_t res = CAStartListeningServer();
+ if (CA_STATUS_OK != res)
+ {
+ printf("Start listening server fail, error code : %d\n", res);
+ }
+ else
+ {
+ printf("Start listening server success\n");
+ }
}
-void start_listening_server()
+void start_discovery_server()
{
- printf("start listening server!!\n");
+ printf("Start discovery client!!\n");
- CAStartListeningServer();
+ CAResult_t res = CAStartDiscoveryServer();
+ if (CA_STATUS_OK != res)
+ {
+ printf("Start discovery client fail, error code : %d\n", res);
+ }
+ else
+ {
+ printf("Start discovery client success\n");
+ }
}
-void start_discovery_server()
+void find_fixed_resource()
{
- printf("start discovery server at client!!\n");
+ // 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");
+
+ res = CAFindResource(buf, token, tokenLength);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Find resource error : %d\n", res);
+ }
+ else
+ {
+ printf("Find resource to %s URI\n", buf);
+ }
+
+ // delete token
+ CADestroyToken(token);
- CAStartDiscoveryServer();
+ printf("=============================================\n");
}
void find_resource()
{
- char buf[MAX_BUF_LEN];
-
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
-
printf("\n=============================================\n");
- printf("ex) a/light\n");
+ printf("ex) /a/light\n");
printf("reference uri : ");
- gets(buf);
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return;
+ }
- CAResult_t res = CAFindResource(buf);
+ // create token
+ CAToken_t token = NULL;
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
- if (res != CA_STATUS_OK)
+ CAResult_t res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("find resource error:%d !!\n", res);
+ 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 for %s URI\n", buf);
+ printf("Find resource to %s URI\n", buf);
+ CADestroyToken(g_last_request_token);
+ g_last_request_token = token;
}
printf("=============================================\n");
void send_request()
{
- char buf[MAX_BUF_LEN];
+ CAResult_t res = get_network_type();
+ if (CA_STATUS_OK != res)
+ {
+ return;
+ }
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
+ printf("Do you want to send secure request ?.... enter (0/1): ");
- printf("\n=============================================\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("uri : ");
+ char secureRequest[MAX_BUF_LEN] = {0};
+ if (CA_STATUS_OK != get_input_data(secureRequest, MAX_BUF_LEN))
+ {
+ return;
+ }
- gets(buf);
+ if (strcmp(secureRequest, "1") == 0)
+ {
+ printf("Enter the URI like below....\n");
+ printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
+ }
+ else if (strcmp(secureRequest, "0") == 0)
+ {
+ 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");
+ }
+ else
+ {
+ printf("Input data is wrong value\n");
+ return;
+ }
+
+ char uri[MAX_BUF_LEN] = {'\0'};
+ if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
+ {
+ return;
+ }
// create remote endpoint
CARemoteEndpoint_t *endpoint = NULL;
- CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
+ res = CACreateRemoteEndpoint(uri, g_selected_nw_type, &endpoint);
+ if (CA_STATUS_OK != res || !endpoint)
+ {
+ printf("Failed to create remote endpoint, error code : %d\n", res);
+ return;
+ }
- if (res != CA_STATUS_OK)
+ printf("\n=============================================\n");
+ printf("0:CON, 1:NON\n");
+ printf("select message type : ");
+
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
{
- printf("create remote endpoint error!!");
CADestroyRemoteEndpoint(endpoint);
return;
}
+ CAMessageType_t msgType = (buf[0] == '1') ? 1 : 0;
+
// create token
CAToken_t token = NULL;
- res = CAGenerateToken(&token);
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
- if (res != CA_STATUS_OK)
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("token generate error!!");
- token = NULL;
+ printf("Token generate error, error code : %d\n", res);
+ CADestroyRemoteEndpoint(endpoint);
+ return;
}
- printf("generated token %s\n", (token != NULL) ? token : "");
+ printf("Generated token %s\n", token);
+
+ // extract relative resourceuri from give uri
+ printf("URI : %s\n", uri);
- CAInfo_t requestData;
- memset(&requestData, 0, sizeof(CAInfo_t));
+ char resourceURI[15] = {0};
+ get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
+
+ // create request data
+ CAInfo_t requestData = { 0 };
requestData.token = token;
- requestData.payload = "Temp Json Payload";
+ requestData.tokenLength = tokenLength;
- CARequestInfo_t requestInfo;
- memset(&requestInfo, 0, sizeof(CARequestInfo_t));
+ if (strcmp(secureRequest, "1") == 0)
+ {
+ uint32_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);
+ CADestroyToken(token);
+ return;
+ }
+ snprintf(requestData.payload, length, SECURE_INFO_DATA, 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)
+ {
+ printf("Memory allocation fail\n");
+ CADestroyRemoteEndpoint(endpoint);
+ CADestroyToken(token);
+ return;
+ }
+ snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
+ }
+ requestData.type = msgType;
+
+ CARequestInfo_t requestInfo = { 0 };
requestInfo.method = CA_GET;
requestInfo.info = requestData;
// send request
- CASendRequest(endpoint, &requestInfo);
-
- if (token != NULL)
+ res = CASendRequest(endpoint, &requestInfo);
+ if (CA_STATUS_OK != res)
{
- CADestroyToken(token);
+ printf("Could not send request : %d\n", res);
}
+ //destroy token
+ CADestroyToken(token);
// destroy remote endpoint
- if (endpoint != NULL)
- {
- CADestroyRemoteEndpoint(endpoint);
- }
+ CADestroyRemoteEndpoint(endpoint);
+ free(requestData.payload);
+
printf("=============================================\n");
}
-void advertise_resource()
+void send_request_all()
{
- char buf[MAX_BUF_LEN];
-
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
+ CAResult_t res = get_network_type();
+ if (CA_STATUS_OK != res)
+ {
+ return;
+ }
printf("\n=============================================\n");
- printf("uri : ");
-
- scanf("%s", buf);
+ printf("ex) /a/light\n");
+ printf("resource uri : ");
- int optionNum = 0;
- char optionData[MAX_OPT_LEN];
-
- printf("Option Num : ");
- scanf("%d", &optionNum);
- CAHeaderOption_t *headerOpt;
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return;
+ }
- if (optionNum > 0)
+ // create remote endpoint
+ CARemoteEndpoint_t *endpoint = NULL;
+ res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
+ if (CA_STATUS_OK != res)
{
- headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
- if (NULL == headerOpt)
- {
- printf("memory allocation failed!\n");
- return;
- }
- memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
+ printf("Create remote endpoint error, error code: %d\n", res);
+ return;
}
- int i;
- for (i = 0; i < optionNum; i++)
+ CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
+ if (NULL == group)
{
- int optionID = 0;
- printf("[%d] Option ID : ", i + 1);
- scanf("%d", &optionID);
- headerOpt[i].optionID = optionID;
+ printf("Memory allocation failed!\n");
+ CADestroyRemoteEndpoint(endpoint);
+ return;
+ }
+ group->transportType = endpoint->transportType;
+ group->resourceUri = endpoint->resourceUri;
- memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
- printf("[%d] Option Data : ", i + 1);
- scanf("%s", optionData);
- memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
- printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
+ // create token
+ CAToken_t token = NULL;
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
- headerOpt[i].optionLength = (uint16_t) strlen(optionData);
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
+ {
+ printf("Token generate error!!\n");
+ CADestroyRemoteEndpoint(endpoint);
+ free(group);
+ return;
}
- printf("\n=============================================\n");
- CAAdvertiseResource(buf, headerOpt, (uint8_t) optionNum);
+ printf("generated token %s\n", token);
+
+ CAInfo_t requestData = {CA_MSG_RESET};
+ requestData.token = token;
+ requestData.tokenLength = tokenLength;
+ requestData.payload = "Temp Json Payload";
+ requestData.type = CA_MSG_NONCONFIRM;
+
+ CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
+ requestInfo.method = CA_GET;
+ requestInfo.info = requestData;
+
+ // 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;
+ }
- free(headerOpt);
+ // destroy remote endpoint
+ CADestroyRemoteEndpoint(endpoint);
+ free(group);
+ printf("=============================================\n");
}
void send_notification()
{
- char buf[MAX_BUF_LEN];
-
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
+ CAResult_t res = get_network_type();
+ if (CA_STATUS_OK != res)
+ {
+ return;
+ }
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("uri : ");
- gets(buf);
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return;
+ }
+
+ printf("\n=============================================\n");
+ printf("\tselect message type\n");
+ printf("CON : 0\n");
+ printf("NON : 1\n");
+ printf("select : ");
+
+ char messageTypeBuf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(messageTypeBuf, MAX_BUF_LEN))
+ {
+ return;
+ }
+
+ int messageType = messageTypeBuf[0] - '0';
+
+ switch(messageType)
+ {
+ case 0:
+ printf("CONFIRM messagetype is selected\n");
+ break;
+ case 1:
+ printf("NONCONFIRM messagetype is selected\n");
+ break;
+ default:
+ printf("Invalid Selection\n");
+ return;
+ }
// create remote endpoint
CARemoteEndpoint_t *endpoint = NULL;
- CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
+ res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Create remote endpoint error, error code: %d\n", res);
+ return;
+ }
- if (res != CA_STATUS_OK)
+ // create token
+ CAToken_t token = NULL;
+ uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+
+ res = CAGenerateToken(&token, tokenLength);
+ if ((CA_STATUS_OK != res) || (!token))
{
- printf("create remote endpoint error!!");
+ printf("Token generate error!!\n");
CADestroyRemoteEndpoint(endpoint);
return;
}
- CAInfo_t respondeData;
- memset(&respondeData, 0, sizeof(CAInfo_t));
- respondeData.token = "client token";
- respondeData.payload = "Temp Notification Data";
+ printf("Generated token %s\n", token);
- CAResponseInfo_t responseInfo;
- memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
- responseInfo.result = CA_CONTENT;
- responseInfo.info = respondeData;
+ CAInfo_t respondData = { 0 };
+ respondData.token = token;
+ respondData.tokenLength = tokenLength;
+ respondData.payload = "Temp Notification Data";
+ respondData.type = messageType;
- // send request
- CASendNotification(endpoint, &responseInfo);
+ CAResponseInfo_t responseInfo = { 0 };
+ responseInfo.result = CA_SUCCESS;
+ responseInfo.info = respondData;
- // destroy remote endpoint
- if (endpoint != NULL)
+ // send notification
+ res = CASendNotification(endpoint, &responseInfo);
+ if (CA_STATUS_OK != res)
{
- CADestroyRemoteEndpoint(endpoint);
+ printf("Send notification error, error code: %d\n", res);
+ }
+ else
+ {
+ printf("Send notification success\n");
}
+
+ // destroy token
+ CADestroyToken(token);
+ // destroy remote endpoint
+ CADestroyRemoteEndpoint(endpoint);
+
printf("\n=============================================\n");
}
void select_network()
{
- char buf[MAX_BUF_LEN];
-
printf("\n=============================================\n");
printf("\tselect network\n");
- printf("ETHERNET : 0\n");
- printf("WIFI : 1\n");
+ printf("IPv4 : 0\n");
printf("EDR : 2\n");
printf("LE : 3\n");
printf("select : ");
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
- gets(buf);
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return;
+ }
int number = buf[0] - '0';
- number = (number < 0 || number > 3) ? 1 : number;
+ if (number < 0 || number > 3)
+ {
+ printf("Invalid network type\n");
+ return;
+ }
- CASelectNetwork(1 << number);
+ CAResult_t res = CASelectNetwork(1 << number);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Select network error\n");
+ g_selected_nw_type = 1 << number;
+ }
+ else
+ {
+ printf("Select network success\n");
+ }
printf("=============================================\n");
}
void unselect_network()
{
- char buf[MAX_BUF_LEN];
-
printf("\n=============================================\n");
printf("\tunselect enabled network\n");
- printf("ETHERNET : 0\n");
- printf("WIFI : 1\n");
+ printf("IPv4 : 0\n");
printf("EDR : 2\n");
printf("LE : 3\n");
printf("select : ");
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
- gets(buf);
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return;
+ }
int number = buf[0] - '0';
- number = (number < 0 || number > 3) ? 1 : number;
+ if (number < 0 || number > 3)
+ {
+ printf("Invalid network type\n");
+ return;
+ }
+
+ CAResult_t res = CAUnSelectNetwork(1 << number);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Unselect network error\n");
+ }
+ else
+ {
+ printf("Unselect network success\n");
+ }
- CAUnSelectNetwork(1 << number);
- printf("Terminating...\n");
- CATerminate();
- //pthread_join(thread, NULL);
printf("=============================================\n");
}
char get_menu()
{
- char buf[MAX_BUF_LEN];
-
printf("\n=============================================\n");
printf("\t\tMenu\n");
- printf("\ti : Initialize\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("\tt : terminate\n");
+ printf("\ty : run static client\n");
+ printf("\tz : run static server\n");
+ printf("\tw : send secure request\n");
printf("\tq : quit\n");
printf("=============================================\n");
printf("select : ");
- memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
-
- gets(buf);
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ printf("Failed to get input data\n");
+ }
return buf[0];
}
void handle_request_response()
{
- printf("handle_request_response\n");
- CAHandleRequestResponse();
+ printf("Handle_request_response\n");
+
+ CAResult_t res = CAHandleRequestResponse();
+ if (CA_STATUS_OK != res)
+ {
+ printf("Handle request error, error code: %d\n", res);
+ }
+ else
+ {
+ printf("Handle request success\n");
+ }
+}
+
+void get_network_info()
+{
+ CALocalConnectivity_t *tempInfo = NULL;
+ uint32_t tempSize = 0;
+
+ CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
+ if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
+ {
+ printf("Network not connected\n");
+ free(tempInfo);
+ return;
+ }
+
+ printf("################## Network Information #######################\n");
+ printf("Network info total size is %d\n\n", tempSize);
+
+ int index;
+ for (index = 0; index < tempSize; index++)
+ {
+ printf("Type: %d\n", tempInfo[index].type);
+ if (CA_IPV4 == tempInfo[index].type)
+ {
+ printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
+ printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
+ }
+ else if (CA_EDR == tempInfo[index].type)
+ {
+ printf("Address: %s\n", tempInfo[index].addressInfo.BT.btMacAddress);
+ }
+ printf("Secured: %d\n\n", tempInfo[index].isSecured);
+
+ if (tempInfo[index].isSecured)
+ {
+ g_local_secure_port = tempInfo[index].addressInfo.IP.port;
+ printf("Secured: in global %d\n\n", g_local_secure_port);
+ }
+ }
+
+ free(tempInfo);
+ printf("##############################################################");
}
-void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
+void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
{
+ if (NULL == object || NULL == requestInfo)
+ {
+ printf("Input parameter is NULL\n");
+ return;
+ }
- printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
- (object != NULL) ? object->resourceUri : "",
- (requestInfo != NULL) ? requestInfo->info.payload : "");
+ if ((NULL != g_last_request_token) && (NULL != requestInfo->info.token)
+ && (strncmp(g_last_request_token, requestInfo->info.token,
+ requestInfo->info.tokenLength) == 0))
+ {
+ printf("Token is same. received request of it's own. skip.. \n");
+ return;
+ }
- printf("[CALLBACK] request_handler, address : %s\n",
- (object != NULL) ? object->addressInfo.IP.ipAddress : "");
+ printf("##########received request from remote device #############\n");
+ printf("Uri: %s\n", object->resourceUri);
+ if (CA_IPV4 == object->transportType)
+ {
+ printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
+ object->addressInfo.IP.port, object->isSecured);
+ }
+ else if (CA_EDR == object->transportType)
+ {
+ printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
+ }
+ printf("Data: %s\n", requestInfo->info.payload);
+ printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
if (requestInfo->info.options)
{
uint32_t i;
for (i = 0; i < len; i++)
{
- printf("[CALLBACK] request_handler, option ID : %d\n",
- requestInfo->info.options[i].optionID);
- printf("[CALLBACK] request_handler, options data length : %d\n",
- requestInfo->info.options[i].optionLength);
- printf("[CALLBACK] request_handler, options data : %s\n",
- requestInfo->info.options[i].optionData);
+ printf("Option %d\n", i + 1);
+ printf("ID : %d\n", requestInfo->info.options[i].optionID);
+ printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
+ requestInfo->info.options[i].optionData);
}
}
+ printf("############################################################\n");
- printf("send response with URI\n");
- send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
+ //Check if this has secure communication information
+ if (requestInfo->info.payload &&
+ (CA_IPV4 == object->transportType))
+ {
+ 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;
-void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
-{
+ // 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))
+ {
+ printf("Failed to create duplicate of remote endpoint!\n");
+ return;
+ }
+ endpoint->isSecured = true;
+ object = endpoint;
- printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
- (object != NULL) ? object->resourceUri : "",
- (responseInfo != NULL) ? responseInfo->info.payload : "");
+ free(uri);
+ }
+ }
- printf("[CALLBACK] response_handler, address : %s\n",
- (object != NULL) ? object->addressInfo.IP.ipAddress : "");
+ printf("Send response with URI\n");
+ send_response(object, &requestInfo->info);
+ g_received = 1;
+}
+
+void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
+{
+ printf("##########Received response from remote device #############\n");
+ printf("Uri: %s\n", object->resourceUri);
+ if (CA_IPV4 == object->transportType)
+ {
+ printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
+ object->addressInfo.IP.port, object->isSecured);
+ }
+ else if (CA_EDR == object->transportType)
+ {
+ printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
+ }
+ printf("response result : %d\n", responseInfo->result);
+ printf("Data: %s\n", responseInfo->info.payload);
+ printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
+ printf("Token: %s\n", responseInfo->info.token);
if (responseInfo->info.options)
{
uint32_t len = responseInfo->info.numOptions;
uint32_t i;
for (i = 0; i < len; i++)
{
- printf("[CALLBACK] response_handler, option ID : %d\n",
- responseInfo->info.options[i].optionID);
- printf("[CALLBACK] response_handler, options data length : %d\n",
- responseInfo->info.options[i].optionLength);
- printf("[CALLBACK] response_handler, options data : %s\n",
- responseInfo->info.options[i].optionData);
+ printf("Option %d\n", i + 1);
+ printf("ID : %d\n", responseInfo->info.options[i].optionID);
+ printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
+ responseInfo->info.options[i].optionData);
+ }
+ }
+ printf("############################################################\n");
+ g_received = 1;
+
+ //Check if this has secure communication information
+ if (responseInfo->info.payload)
+ {
+ int securePort = get_secure_information(responseInfo->info.payload);
+ if (0 < securePort) //Set the remote endpoint secure details and send response
+ {
+ printf("This is secure resource...\n");
}
}
+}
+
+void error_handler(const CARemoteEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
+{
+ printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++\n");
+ if(rep && rep->resourceUri )
+ {
+ printf("Error Handler, RemoteEndpoint Info resourceUri : %s\n", rep->resourceUri);
+ }
+ else
+ {
+ printf("Error Handler, RemoteEndpoint is NULL");
+ }
+
+ 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 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(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
+void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
{
+ printf("entering send_response\n");
printf("\n=============================================\n");
+ printf("\tselect message type\n");
+ printf("CON : 0\n");
+ printf("NON : 1\n");
+ printf("ACK : 2\n");
+ printf("RESET : 3\n");
+ printf("select : ");
- CAInfo_t responseData;
- //responseData = (CAInfo*) malloc(sizeof(CAInfo));
- memset(&responseData, 0, sizeof(CAInfo_t));
- responseData.token = request_token;
- responseData.payload = "response payload";
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return;
+ }
- CAResponseInfo_t responseInfo;
- //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
- memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
- responseInfo.result = 203;
+ int messageType = buf[0] - '0';
+ int responseCode = 0 ;
+ char responseCodeBuf[MAX_BUF_LEN] = { 0 };
+ if (CA_MSG_RESET != messageType)
+ {
+ 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("BAD_REQ : 400\n");
+ printf("BAD_OPT : 402\n");
+ printf("NOT_FOUND : 404\n");
+ printf("INTERNAL_SERVER_ERROR : 500\n");
+ printf("RETRANSMIT_TIMEOUT : 504\n");
+ printf("select : ");
+
+ if (CA_STATUS_OK != get_input_data(responseCodeBuf, MAX_BUF_LEN))
+ {
+ return;
+ }
+ responseCode = atoi(responseCodeBuf);
+ }
+ CAInfo_t responseData = { 0 };
+ responseData.type = messageType;
+
+ responseData.messageId = (info != NULL) ? info->messageId : 0;
+ if(CA_MSG_RESET != messageType)
+ {
+ responseData.token = (info != NULL) ? info->token : NULL;
+ responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
+
+ if (endpoint->isSecured)
+ {
+ printf("Sending response on secure communication\n");
+
+ uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(endpoint->resourceUri);
+ 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);
+ }
+ 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)
+ {
+ printf("Memory allocation fail\n");
+ return;
+ }
+ snprintf(responseData.payload, length, NORMAL_INFO_DATA, endpoint->resourceUri);
+ }
+ }
+
+ CAResponseInfo_t responseInfo = { 0 };
+ responseInfo.result = responseCode;
responseInfo.info = responseData;
- // send request (connectivityType from remoteEndpoint of request Info)
- CASendResponse(endpoint, &responseInfo);
+ // send response (transportType from remoteEndpoint of request Info)
+ CAResult_t res = CASendResponse(endpoint, &responseInfo);
+ if (CA_STATUS_OK != res)
+ {
+ printf("Send response error\n");
+ }
+ else
+ {
+ printf("Send response success\n");
+ }
printf("=============================================\n");
+}
+
+int get_secure_information(CAPayload_t payLoad)
+{
+ printf("Entering get_secure_information\n");
+
+ if (!payLoad)
+ {
+ printf("Payload is NULL\n");
+ return -1;
+ }
+ char *subString = NULL;
+ if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
+ {
+ printf("This is not secure resource\n");
+ return -1;
+ }
+
+ if (NULL == (subString = strstr(payLoad, "\"port\":")))
+ {
+ printf("This secure resource does not have port information\n");
+ return -1;
+ }
+
+ char *startPos = strstr(subString, ":");
+ if (!startPos)
+ {
+ printf("Parsing failed !\n");
+ return -1;
+ }
+
+ char *endPos = strstr(startPos, "}");
+ if (!endPos)
+ {
+ printf("Parsing failed !\n");
+ return -1;
+ }
+
+ if(((endPos - 1) - startPos) > 4)
+ {
+ printf("port length is not proper.Exceeding length 4\n");
+ return -1;
+ }
+
+ char portStr[4] = {0};
+ memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
+
+ printf("secured port is: %s\n", portStr);
+ return atoi(portStr);
}
-void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
+void get_resource_uri(char *URI, char *resourceURI, int length)
{
+ char *startPos = URI;
+ char *temp = NULL;
+ if (NULL != (temp = strstr(URI, "://")))
+ {
+ startPos = strchr(temp + 3, '/');
+ if (!startPos)
+ {
+ printf("Resource URI is missing\n");
+ return;
+ }
+ }
+
+ char *endPos = strchr(startPos, '?');
+ if (!endPos)
+ {
+ endPos = URI + strlen(URI);
+ }
+ endPos -= 1;
+
+ if (endPos - startPos <= length)
+ {
+ memcpy(resourceURI, startPos + 1, endPos - startPos);
+ }
+
+ printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
+}
+CAResult_t get_network_type()
+{
printf("\n=============================================\n");
+ printf("\tselect network type\n");
+ printf("IPv4 : 0\n");
+ printf("BT : 2\n");
+ printf("LE : 3\n");
+ printf("select : ");
- CAInfo_t requestData;
- memset(&requestData, 0, sizeof(CAInfo_t));
- requestData.token = token;
- requestData.payload = "Temp Json Payload";
+ char buf[MAX_BUF_LEN] = { 0 };
+ if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+ {
+ return CA_NOT_SUPPORTED ;
+ }
- CARequestInfo_t requestInfo;
- memset(&requestInfo, 0, sizeof(CARequestInfo_t));
- requestInfo.method = CA_GET;
- requestInfo.info = requestData;
+ int number = buf[0] - '0';
+ number = (number < 0 || number > 3) ? 0 : 1 << number;
- // send request
- endpoint->connectivityType = CA_WIFI;
- CASendRequest(endpoint, &requestInfo);
+ if (!(number & 0xf))
+ {
+ 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");
+ printf("\n=============================================\n");
+ return CA_STATUS_FAILED;
}
-void terminate()
+CAResult_t get_input_data(char *buf, int32_t length)
{
- unselect_network();
+ if (!fgets(buf, length, stdin))
+ {
+ printf("fgets error\n");
+ return CA_STATUS_FAILED;
+ }
+
+ char *p = NULL;
+ if ((p = strchr(buf, '\n')) != NULL)
+ {
+ *p = '\0';
+ }
+
+ return CA_STATUS_OK;
}