#include "ocstack.h"
#include "logger.h"
#include "occlientslow.h"
+#include "ocpayload.h"
+// Tracking user input
static int UNICAST_DISCOVERY = 0;
static int TEST_CASE = 0;
-static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:6298/oc/core";
-static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
+static int CONNECTIVITY = 0;
+
+static const char * UNICAST_DISCOVERY_QUERY = "coap://%s/oic/res";
static std::string coapServerIP = "255.255.255.255";
-static std::string coapServerPort = "5683";
+static uint16_t coapServerPort = 5683;
static std::string coapServerResource = "/a/led";
-//The following variable determines the interface protocol (IPv4, IPv6, etc)
-//to be used for sending unicast messages. Default set to IPv4.
-static OCConnectivityType OC_CONNTYPE = OC_IPV4;
-static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
-static int IPV4_ADDR_SIZE = 16;
+//The following variable determines the interface protocol (IP, etc)
+//to be used for sending unicast messages. Default set to IP.
+static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
+static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
+static int IPV4_ADDR_SIZE = 24;
void StripNewLineChar(char* str);
int gQuitFlag = 0;
static void PrintUsage()
{
- OC_LOG(INFO, TAG, "Usage : occlient -c <0|1> -u <0|1> -t <1|2|3>");
- OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
+ OC_LOG(INFO, TAG, "Usage : occlient -c <0|1|2> -u <0|1> -t <1|2|3>");
+ OC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
+ OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
+ OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate NonConfirmable Put Request");
+ OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Confirmable Put Request");
+}
+
+OCPayload* putPayload()
+{
+ OCRepPayload* payload = OCRepPayloadCreate();
+
+ if(!payload)
+ {
+ std::cout << "Failed to create put payload object"<<std::endl;
+ std::exit(1);
+ }
+
+ OCRepPayloadSetPropInt(payload, "power", 15);
+ OCRepPayloadSetPropBool(payload, "state", true);
+
+ return (OCPayload*) payload;
}
OCStackResult InvokeOCDoResource(std::ostringstream &query,
cbData.cd = NULL;
ret = OCDoResource(NULL, method, query.str().c_str(), 0,
- NULL, OC_CONNTYPE, qos, &cbData, options, numOptions);
+ (method == OC_REST_PUT) ? putPayload() : NULL,
+ OC_CONNTYPE, qos, &cbData, options, numOptions);
if (ret != OC_STACK_OK)
{
OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
- clientResponse->resJSONPayload);
+ OC_LOG(INFO, TAG, "Get Response =============> ");
+ OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
if(clientResponse->rcvdVendorSpecificHeaderOptions &&
clientResponse->numRcvdVendorSpecificHeaderOptions)
OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
OCClientResponse * clientResponse)
{
- uint8_t remoteIpAddr[4];
- uint16_t remotePortNu;
-
if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
{
OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
- remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
- OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
-
- OC_LOG_V(INFO, TAG,
- "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
- clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
- remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
+ OC_LOG_V(INFO, TAG, "Discovered @ %s:%u =============> ",
+ clientResponse->devAddr.addr, clientResponse->devAddr.port);
+ OC_LOG_PAYLOAD (INFO, TAG, clientResponse->payload);
parseClientResponse(clientResponse);
case TEST_CON_OP:
InitGetRequest(OC_HIGH_QOS);
break;
+ case TEST_NON_CON_PUT:
+ InitPutRequest(OC_LOW_QOS);
+ break;
+ case TEST_CON_PUT:
+ InitPutRequest(OC_HIGH_QOS);
+ break;
default:
PrintUsage();
break;
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
-
+ OC_LOG_V (INFO, TAG, "Performing GET with query : %s", query.str().c_str());
return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
}
+int InitPutRequest(OCQualityOfService qos)
+{
+ OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+ std::ostringstream query;
+ query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ OC_LOG_V (INFO, TAG, "Performing PUT with query : %s", query.str().c_str());
+ return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)?
+ OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
+}
+
int InitDiscovery()
{
OCStackResult ret;
if (UNICAST_DISCOVERY)
{
char ipv4addr[IPV4_ADDR_SIZE];
- printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
+ OC_LOG(INFO, TAG, "Enter IPv4:port of the Server hosting resource"\
+ "(Ex: 192.168.0.15:1234)");
if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
{
//Strip newline char from ipv4addr
}
else
{
- ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
+ ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
OC_LOW_QOS, &cbData, NULL, 0);
}
if (ret != OC_STACK_OK)
TEST_CASE = atoi(optarg);
break;
case 'c':
- // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
- // OC_CONNTYPE = OCConnectivityType(atoi(optarg));
- OC_CONNTYPE = OC_IPV4;
+ CONNECTIVITY = atoi(optarg);
break;
default:
PrintUsage();
}
if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
- (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
+ (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) ||
+ (CONNECTIVITY < CT_ADAPTER_DEFAULT || CONNECTIVITY >= MAX_CT))
{
PrintUsage();
return -1;
return 0;
}
+ if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IP)
+ {
+ OC_CONNTYPE = CT_ADAPTER_IP;
+ }
+ else
+ {
+ OC_LOG(INFO, TAG, "Default Connectivity type selected...");
+ OC_CONNTYPE = CT_ADAPTER_IP;
+ }
+
InitDiscovery();
// Break from loop with Ctrl+C
return 0;
}
-std::string getIPAddrTBServer(OCClientResponse * clientResponse)
-{
- if(!clientResponse) return "";
- if(!clientResponse->addr) return "";
- uint8_t a, b, c, d = 0;
- if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
-
- char ipaddr[16] = {'\0'};
- // ostringstream not working correctly here, hence snprintf
- snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
- return std::string (ipaddr);
-}
-
-std::string getPortTBServer(OCClientResponse * clientResponse)
-{
- if(!clientResponse) return "";
- if(!clientResponse->addr) return "";
- uint16_t p = 0;
- if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
- std::ostringstream ss;
- ss << p;
- return ss.str();
-}
-
std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
{
return "/a/led";
void parseClientResponse(OCClientResponse * clientResponse)
{
- coapServerIP = getIPAddrTBServer(clientResponse);
- coapServerPort = getPortTBServer(clientResponse);
+ coapServerIP = clientResponse->devAddr.addr;
+ coapServerPort = clientResponse->devAddr.port;
coapServerResource = getQueryStrForGetPut(clientResponse);
}