OCClose(ssfd);
}
+TEST(GetSocketInfo, Positive) {
+ OCDevAddr ipaddr;
+ int32_t sockfd;
+ uint8_t addr[20];
+ uint8_t ifname[] = "eth0";
+ uint16_t port;
+ uint8_t a,b,c,d;
+
+ OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
+ EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
+ EXPECT_EQ(ERR_SUCCESS, OCGetSocketInfo(sockfd, &port));
+ OC_LOG_V(DEBUG, MOD_NAME, "Port %d", port);
+ OCClose(sockfd);
+
+ OCBuildIPv4Address(0,0,0,0, 5678, &ipaddr);
+ EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
+ EXPECT_EQ(ERR_SUCCESS, OCGetSocketInfo(sockfd, &port));
+ OC_LOG_V(DEBUG, MOD_NAME, "Port %d", port);
+ EXPECT_TRUE(port == 5678);
+ OCClose(sockfd);
+
+ OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
+ sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
+ OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
+ EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
+ EXPECT_EQ(ERR_SUCCESS, OCGetSocketInfo(sockfd, &port));
+ OC_LOG_V(DEBUG, MOD_NAME, "Port %d", port);
+ EXPECT_TRUE(port == TEST_PORT_NUM);
+ OCClose(sockfd);
+}
#include "ocstack.h"
#include "logger.h"
#include "occlientbasicops.h"
+#include "cJSON.h"
#define TAG "occlientbasicops"
static int UNICAST_DISCOVERY = 0;
static int TEST_CASE = 0;
static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
-static std::string coapServerIP = "255.255.255.255";
-static std::string coapServerPort = "5683";
-static std::string coapServerResource = "/a/led";
+static std::string coapServerIP;
+static std::string coapServerPort;
+static std::string coapServerResource;
+static int coapSecureResource;
int gQuitFlag = 0;
OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
clientResponse->resJSONPayload);
}
- if(clientResponse->rcvdVendorSpecificHeaderOptions &&
- clientResponse->numRcvdVendorSpecificHeaderOptions)
- {
- OC_LOG (INFO, TAG, "Received vendor specific options");
- uint8_t i = 0;
- OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
- for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
- {
- if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
- {
- OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
- ((OCHeaderOption)rcvdOptions[i]).optionID );
- OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
- ((OCHeaderOption)rcvdOptions[i]).optionLength);
- }
- }
- }
return OC_STACK_DELETE_TRANSACTION;
}
clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
- parseClientResponse(clientResponse);
-
- switch(TEST_CASE)
+ if (parseClientResponse(clientResponse) != -1)
{
- case TEST_NON_CON_OP:
- InitGetRequest(OC_LOW_QOS);
- InitPutRequest();
- //InitPostRequest(OC_LOW_QOS);
- break;
- case TEST_CON_OP:
- InitGetRequest(OC_HIGH_QOS);
- InitPutRequest();
- //InitPostRequest(OC_HIGH_QOS);
- break;
+ switch(TEST_CASE)
+ {
+ case TEST_NON_CON_OP:
+ InitGetRequest(OC_LOW_QOS);
+ InitPutRequest();
+ //InitPostRequest(OC_LOW_QOS);
+ break;
+ case TEST_CON_OP:
+ InitGetRequest(OC_HIGH_QOS);
+ InitPutRequest();
+ //InitPostRequest(OC_HIGH_QOS);
+ break;
+ }
}
}
{
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coaps://" << coapServerIP << ":" << "5684" << coapServerResource;
+ query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
+ << ":" << coapServerPort << coapServerResource;
return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
}
OCStackResult result;
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coaps://" << coapServerIP << ":" << "5684" << coapServerResource;
+ query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
+ << ":" << coapServerPort << coapServerResource;
// First POST operation (to create an LED instance)
result = InvokeOCDoResource(query, OC_REST_POST,
{
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coaps://" << coapServerIP << ":" << "5684" << coapServerResource;
+ query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
+ << ":" << coapServerPort << coapServerResource;
return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
}
nanosleep(&timeout, NULL);
- //sleep(2);
}
OC_LOG(INFO, TAG, "Exiting occlient main loop...");
return ss.str();
}
-std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
+int parseClientResponse(OCClientResponse * clientResponse)
{
- return "/a/led";
-}
+ int port = -1;
+ cJSON * root = NULL;
+ cJSON * oc = NULL;
+
+ // Initialize all global variables
+ coapServerResource.clear();
+ coapServerPort.clear();
+ coapServerIP.clear();
+ coapSecureResource = 0;
+
+ root = cJSON_Parse((char *)(clientResponse->resJSONPayload));
+ if (!root)
+ {
+ return -1;
+ }
+
+ oc = cJSON_GetObjectItem(root,"oc");
+ if (!oc)
+ {
+ return -1;
+ }
+
+ if (oc->type == cJSON_Array)
+ {
+ if (cJSON_GetArraySize(oc) > 0)
+ {
+ cJSON * resource = cJSON_GetArrayItem(oc, 0);
+ if (cJSON_GetObjectItem(resource, "href"))
+ {
+ coapServerResource.assign(cJSON_GetObjectItem(resource, "href")->valuestring);
+ }
+ else
+ {
+ coapServerResource = "";
+ }
+ OC_LOG_V(INFO, TAG, "Uri -- %s", coapServerResource.c_str());
+
+ cJSON * prop = cJSON_GetObjectItem(resource,"prop");
+ if (prop)
+ {
+ // If this is a secure resource, the info about the port at which the
+ // resource is hosted on server is embedded inside discovery JSON response
+ if (cJSON_GetObjectItem(prop, "sec"))
+ {
+ if ((cJSON_GetObjectItem(prop, "sec")->valueint) == 1)
+ {
+ coapSecureResource = 1;
+ }
+ }
+ OC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO");
+ if (cJSON_GetObjectItem(prop, "port"))
+ {
+ port = cJSON_GetObjectItem(prop, "port")->valueint;
+ OC_LOG_V(INFO, TAG, "Hosting Server Port (embedded inside JSON) -- %u", port);
+
+ std::ostringstream ss;
+ ss << port;
+ coapServerPort = ss.str();
+ }
+ }
+ }
+ }
+ cJSON_Delete(root);
-void parseClientResponse(OCClientResponse * clientResponse)
-{
coapServerIP = getIPAddrTBServer(clientResponse);
- coapServerPort = getPortTBServer(clientResponse);
- coapServerResource = getQueryStrForGetPut(clientResponse);
+ if (port == -1)
+ {
+ coapServerPort = getPortTBServer(clientResponse);
+ OC_LOG_V(INFO, TAG, "Hosting Server Port -- %s", coapServerPort.c_str());
+ }
+ return 0;
}