const char *getResult(OCStackResult result);
std::string getIPAddrTBServer(OCClientResponse * clientResponse);
std::string getPortTBServer(OCClientResponse * clientResponse);
-std::string getQueryStrForGetPut(unsigned const char * responsePayload);
+std::string getQueryStrForGetPut(const char * responsePayload);
#define TAG PCF("occlient")
#define DEFAULT_CONTEXT_VALUE 0x99
#define MAX_LENGTH_IPv4_ADDR 16
#endif
-typedef enum {
+typedef enum
+{
TEST_INVALID = 0,
TEST_GET_DEFAULT,
TEST_GET_BATCH,
typedef struct
{
- unsigned char text[30];
+ char text[30];
CLIENT_TEST test;
} testToTextMap;
testToTextMap queryInterface[] = {
{"invalid", TEST_INVALID},
- {"?if=oc.mi.def", TEST_GET_DEFAULT},
- {"?if=oc.mi.b", TEST_GET_BATCH},
- {"?if=oc.mi.ll", TEST_GET_LINK_LIST},
- {"?if=oc.mi.def", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
- {"?if=oc.mi.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
- {"?if=oc.mi.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
- {"?if=oc.mi.def", TEST_PUT_DEFAULT},
- {"?if=oc.mi.b", TEST_PUT_BATCH},
- {"?if=oc.mi.ll", TEST_PUT_LINK_LIST},
+ {"?if=oic.if.baseline", TEST_GET_DEFAULT},
+ {"?if=oic.if.b", TEST_GET_BATCH},
+ {"?if=oic.if.ll", TEST_GET_LINK_LIST},
+ {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
+ {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
+ {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
+ {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
+ {"?if=oic.if.b", TEST_PUT_BATCH},
+ {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
};
static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
+//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";
+
// The handle for the observe registration
OCDoHandle gObserveDoHandle;
// After this crosses a threshold client deregisters for further observations
int gQuitFlag = 0;
/* SIGINT handler: set gQuitFlag to 1 for graceful termination */
-void handleSigInt(int signum) {
- if (signum == SIGINT) {
+void handleSigInt(int signum)
+{
+ if (signum == SIGINT)
+ {
gQuitFlag = 1;
}
}
void PrintUsage()
{
- OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case>");
- OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an"\
+ OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
+ OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
+ OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
"available resource using default interface.");
- OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
"available resource using batch interface.");
- OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
"available resource using link list interface.");
- OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
"available resource using default interface.");
- OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
"available resource using batch interface.");
- OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
"available resource using link list interface.");
- OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
"unavailable resource using default interface.");
- OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
"unavailable resource using batch interface.");
- OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an"\
+ OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
"unavailable resource using link list interface.");
}
-OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
+OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+{
if(clientResponse == NULL)
{
OC_LOG(INFO, TAG, "The clientResponse is NULL");
return OC_STACK_DELETE_TRANSACTION;
}
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
+ if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+ {
OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
}
return OC_STACK_KEEP_TRANSACTION;
}
-OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
+OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+{
OC_LOG_V(INFO, TAG, "StackResult: %s",
getResult(clientResponse->result));
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
+ if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+ {
OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
- if(clientResponse->sequenceNumber == 0) {
+ if(clientResponse->sequenceNumber == 0)
+ {
OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
}
- else {
- OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d", gNumObserveNotifies);
+ else
+ {
+ OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
+ gNumObserveNotifies);
OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
gNumObserveNotifies++;
if (gNumObserveNotifies == 3)
{
- if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
+ if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "Observe cancel error");
}
}
// This is a function called back when a device is discovered
OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
- OCClientResponse * clientResponse) {
- uint8_t remoteIpAddr[4];
- uint16_t remotePortNu;
-
+ OCClientResponse * clientResponse)
+{
OC_LOG(INFO, TAG,
"Entering discoveryReqCB (Application Layer CB)");
OC_LOG_V(INFO, TAG, "StackResult: %s",
getResult(clientResponse->result));
- if (ctx == (void*) DEFAULT_CONTEXT_VALUE) {
+ if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
+ {
OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
}
- 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);
+ clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
{
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
std::ostringstream getQuery;
- getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << "/SomeUnknownResource";
+ getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
+ clientResponse->devAddr.port << "/SomeUnknownResource";
cbData.cb = getReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
- ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_LOW_QOS,
+ ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
&cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
OCCallbackData cbData;
OCDoHandle handle;
std::ostringstream obsReg;
- obsReg << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << getQueryStrForGetPut(clientResponse->resJSONPayload);
+ obsReg << "coap://" << clientResponse->devAddr.addr << ":" <<
+ clientResponse->devAddr.addr <<
+ getQueryStrForGetPut(clientResponse->resJSONPayload);
cbData.cb = getReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
OC_LOG_V(INFO, TAG, "OBSERVE payload from client = %s ", putPayload.c_str());
- ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_LOW_QOS,
- &cbData, NULL, 0);
+ ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_CONNTYPE,
+ OC_LOW_QOS, &cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack resource error");
{
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
//* Make a PUT query*/
std::ostringstream getQuery;
- getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) <<
- "/a/room" << queryInterface[TEST].text;
+ getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
+ clientResponse->devAddr.port <<
+ "/a/room" << queryInterface[TEST].text;
cbData.cb = putReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
OC_LOG_V(INFO, TAG, "PUT payload from client = %s ", putPayload.c_str());
- ret = OCDoResource(&handle, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
- OC_LOW_QOS, &cbData, NULL, 0);
+ ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
+ OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack resource error");
{
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
-
- uint8_t remoteIpAddr[4];
- uint16_t remotePortNu;
-
- OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
- remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
- OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
//* Make a GET query*/
std::ostringstream getQuery;
- getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) <<
- "/a/room" << queryInterface[TEST].text;
+ getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
+ clientResponse->devAddr.port <<
+ "/a/room" << queryInterface[TEST].text;
std::cout << "Get Query: " << getQuery.str() << std::endl;
cbData.cb = getReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
- ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_LOW_QOS,
+ ret = OCDoResource(NULL, OC_REST_GET,
+ getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
&cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
{
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
/* Start a discovery query*/
char szQueryUri[64] = { 0 };
- strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
+ strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
cbData.cb = discoveryReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS,
+ ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
+ OC_LOW_QOS,
&cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
return ret;
}
-int main(int argc, char* argv[]) {
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = USE_RANDOM_PORT;
- uint8_t ifname[] = "eth0";
+int main(int argc, char* argv[])
+{
int opt;
- while ((opt = getopt(argc, argv, "t:")) != -1)
+ while ((opt = getopt(argc, argv, "t:c:")) != -1)
{
- switch(opt)
+ switch (opt)
{
- case 't':
- TEST = atoi(optarg);
- break;
- default:
- PrintUsage();
- return -1;
+ case 't':
+ TEST = atoi(optarg);
+ break;
+ case 'c':
+ OC_CONNTYPE = CT_ADAPTER_IP;
+ break;
+ default:
+ PrintUsage();
+ return -1;
}
}
- if(TEST <= TEST_INVALID || TEST >= MAX_TESTS){
+ if (TEST <= TEST_INVALID || TEST >= MAX_TESTS)
+ {
PrintUsage();
return -1;
}
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == OC_ERR_SUCCESS)
- {
- OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
- paddr = addr;
- }
-
/* Initialize OCStack*/
- if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
+ if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
// Break from loop with Ctrl+C
OC_LOG(INFO, TAG, "Entering occlient main loop...");
signal(SIGINT, handleSigInt);
- while (!gQuitFlag) {
+ while (!gQuitFlag)
+ {
- if (OCProcess() != OC_STACK_OK) {
+ if (OCProcess() != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "OCStack process error");
return 0;
}
sleep(2);
- }
- OC_LOG(INFO, TAG, "Exiting occlient main loop...");
+ } OC_LOG(INFO, TAG, "Exiting occlient main loop...");
- if (OCStop() != OC_STACK_OK) {
+ if (OCStop() != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "OCStack stop error");
}
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'};
- snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
- //printf("IP address string of the TB server = %s\n", *out_ipaddr);
- 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(unsigned const char * responsePayload){
+std::string getQueryStrForGetPut(const char * responsePayload)
+{
- std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));
+ std::string jsonPayload(responsePayload);
return "/a/room";
}
+