#include "ocstack.h"
#include "logger.h"
#include "occlient.h"
-
-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 const char * TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core/d";
-static const char * TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY = "coap://224.0.1.187:5683/oc/core/d";
-static std::string putPayload = "{\"state\":\"on\",\"power\":5}";
-static std::string coapServerIP = "255.255.255.255";
-static std::string coapServerPort = "5683";
+#include "ocpayload.h"
+#include "payload_logging.h"
+
+#ifdef ROUTING_GATEWAY
+/**
+ * Maximum number of gateway requests to form the routing table.
+ */
+#define MAX_NUM_GATEWAY_REQUEST 20
+
+/**
+ * Sleep duration after every OCProcess().
+ */
+#define SLEEP_DURATION 100000
+#endif
+// Tracking user input
+static int UnicastDiscovery = 0;
+static int TestCase = 0;
+static int Connectivity = 0;
+
+static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
+static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
+static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
+
+//The following variable determines the interface protocol (IPv4, IPv6, etc)
+//to be used for sending unicast messages. Default set to IP dual stack.
+static OCConnectivityType ConnType = CT_ADAPTER_IP;
+static OCDevAddr serverAddr;
+static char discoveryAddr[100];
static std::string coapServerResource = "/a/light";
+void StripNewLineChar(char* str);
+
// The handle for the observe registration
OCDoHandle gObserveDoHandle;
#ifdef WITH_PRESENCE
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;
}
}
+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;
+}
+
static void PrintUsage()
{
- OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7>");
+ OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
+ OC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
+ OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
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 Nonconfirmable Put Requests");
- OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
- OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
- OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
- OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request for a resource which is unavailable");
-
- OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
- OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
- OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
- OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests");
-
- #ifdef WITH_PRESENCE
- OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
- OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
- "filter");
+ OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
+ " with query filter.");
+ OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
+ OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
+ OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
+ OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
+ OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
+ "for a resource which is unavailable");
+ OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
+ OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
+ OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
+ OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
+ " and cancel with Low QoS");
+
+#ifdef WITH_PRESENCE
+ OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
+ "filter");
+ OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
"2 filters");
- #endif
+ OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
+#endif
- OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
- "then cancel immediately");
- OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Get Request and "\
+ OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
+ "then cancel immediately with High QOS");
+ OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
"add vendor specific header options");
- OC_LOG(INFO, TAG, "-t 17 : Discover Devices");
+ OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
+ OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
}
OCStackResult InvokeOCDoResource(std::ostringstream &query,
- OCMethod method, OCQualityOfService qos,
- OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
+ OCDevAddr *remoteAddr,
+ OCMethod method,
+ OCQualityOfService qos,
+ OCClientResponseHandler cb,
+ OCHeaderOption * options,
+ uint8_t numOptions)
{
OCStackResult ret;
OCCallbackData cbData;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
-#ifdef CA_INT
- ret = OCDoResource(&handle, method, query.str().c_str(), 0,
- (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
- (OC_WIFI), qos, &cbData, options, numOptions);
-#else
- ret = OCDoResource(&handle, method, query.str().c_str(), 0,
- (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
- qos, &cbData, options, numOptions);
-#endif
+ ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
+ (method == OC_REST_PUT) ? putPayload() : NULL,
+ (ConnType), qos, &cbData, options, numOptions);
if (ret != OC_STACK_OK)
{
{
gObserveDoHandle = handle;
}
- #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
else if (method == OC_REST_PRESENCE)
{
gPresenceHandle = handle;
}
- #endif
+#endif
return ret;
}
-OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
+{
+ if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
}
- if(clientResponse)
+ if (clientResponse)
{
OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OC_LOG(INFO, TAG, ("=============> Put Response"));
+ }
+ else
+ {
+ OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
+OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
{
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+ if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
}
- if(clientResponse)
+ if (clientResponse)
{
OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response", clientResponse->resJSONPayload);
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OC_LOG(INFO, TAG, ("=============> Post Response"));
+ }
+ else
+ {
+ OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult deleteReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
+OCStackApplicationResult deleteReqCB(void *ctx,
+ OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
{
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+ if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
}
- if(clientResponse)
+ if (clientResponse)
{
OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response", clientResponse->resJSONPayload);
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OC_LOG(INFO, TAG, ("=============> Delete Response"));
+ }
+ else
+ {
+ OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
{
- if(clientResponse == NULL)
+ if (clientResponse == NULL)
{
- OC_LOG(INFO, TAG, "The clientResponse is NULL");
+ OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
return OC_STACK_DELETE_TRANSACTION;
}
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+
+ if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
}
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_PAYLOAD(INFO, clientResponse->payload);
+ OC_LOG(INFO, TAG, ("=============> Get Response"));
- if(clientResponse->rcvdVendorSpecificHeaderOptions &&
- clientResponse->numRcvdVendorSpecificHeaderOptions)
+ if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
{
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)
+ 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);
+ MAX_HEADER_OPTION_DATA_LENGTH);
}
}
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
+{
+ if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
}
- if(clientResponse)
+ if (clientResponse)
{
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, "Callback Context for OBSERVE notification recvd successfully %d", gNumObserveNotifies);
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response", clientResponse->resJSONPayload);
+ OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
+ gNumObserveNotifies);
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OC_LOG(INFO, TAG, ("=============> Obs Response"));
gNumObserveNotifies++;
- if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
+ if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
{
- if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON){
- if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
+ if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
+ {
+ if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "Observe cancel error");
}
return OC_STACK_DELETE_TRANSACTION;
- }else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM){
- if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK){
+ }
+ else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
+ {
+ if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "Observe cancel error");
}
}
}
- if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER){
+ if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
+ {
OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
- }else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER){
+ }
+ else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
+ {
OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
return OC_STACK_DELETE_TRANSACTION;
- }else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION){
+ }
+ else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
+ {
OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
return OC_STACK_DELETE_TRANSACTION;
}
}
+ else
+ {
+ OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
+ }
return OC_STACK_KEEP_TRANSACTION;
}
#ifdef WITH_PRESENCE
-OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
+{
+ if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
}
- if(clientResponse)
+ if (clientResponse)
{
- OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
- OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d", gNumPresenceNotifies);
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response", clientResponse->resJSONPayload);
+ OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+ OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
+ gNumPresenceNotifies);
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OC_LOG(INFO, TAG, ("=============> Presence Response"));
gNumPresenceNotifies++;
if (gNumPresenceNotifies == 20)
{
- if (OCCancel (gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
+ if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
+ {
OC_LOG(ERROR, TAG, "Presence cancel error");
}
return OC_STACK_DELETE_TRANSACTION;
}
}
+ else
+ {
+ OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
+ }
return OC_STACK_KEEP_TRANSACTION;
}
#endif
// 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;
-
+OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
+{
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);
-
-#ifdef CA_INT
std::string connectionType = getConnectivityType (clientResponse->connType);
OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
-#endif
OC_LOG_V(INFO, TAG,
- "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
- clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
- remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
+ "Device =============> Discovered @ %s:%d",
+ clientResponse->devAddr.addr,
+ clientResponse->devAddr.port);
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ ConnType = clientResponse->connType;
+ serverAddr = clientResponse->devAddr;
parseClientResponse(clientResponse);
- switch(TEST_CASE)
+ switch(TestCase)
{
case TEST_GET_REQ_NON:
- InitGetRequest(OC_LOW_QOS, 0);
+ InitGetRequest(OC_LOW_QOS, 0, 0);
+ break;
+ case TEST_GET_REQ_NON_WITH_FILTERS:
+ InitGetRequest(OC_LOW_QOS, 0, 1);
break;
case TEST_PUT_REQ_NON:
- InitPutRequest();
+ InitPutRequest(OC_LOW_QOS);
break;
case TEST_POST_REQ_NON:
InitPostRequest(OC_LOW_QOS);
InitObserveRequest(OC_LOW_QOS);
break;
case TEST_GET_UNAVAILABLE_RES_REQ_NON:
- InitGetRequestToUnavailableResource();
+ InitGetRequestToUnavailableResource(OC_LOW_QOS);
break;
case TEST_GET_REQ_CON:
- InitGetRequest(OC_HIGH_QOS, 0);
+ InitGetRequest(OC_HIGH_QOS, 0, 0);
break;
case TEST_POST_REQ_CON:
InitPostRequest(OC_HIGH_QOS);
case TEST_OBS_REQ_CON:
InitObserveRequest(OC_HIGH_QOS);
break;
- #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
case TEST_OBS_PRESENCE:
case TEST_OBS_PRESENCE_WITH_FILTER:
case TEST_OBS_PRESENCE_WITH_FILTERS:
+ case TEST_OBS_MULTICAST_PRESENCE:
InitPresence();
break;
- #endif
+#endif
case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
- InitGetRequest(OC_LOW_QOS, 1);
+ InitGetRequest(OC_LOW_QOS, 1, 0);
+ break;
+ case TEST_DISCOVER_PLATFORM_REQ:
+ InitPlatformDiscovery(OC_LOW_QOS);
break;
case TEST_DISCOVER_DEV_REQ:
- InitDeviceDiscovery();
+ InitDeviceDiscovery(OC_LOW_QOS);
break;
default:
PrintUsage();
break;
}
}
-#ifdef CA_INT
+ else
+ {
+ OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
+ }
return OC_STACK_KEEP_TRANSACTION;
-#else
- return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
-#endif
+}
+
+OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
+ OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
+{
+ if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
+ {
+ OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
+ }
+ if (clientResponse)
+ {
+ OC_LOG(INFO, TAG, ("Discovery Response:"));
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ }
+ else
+ {
+ OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
+ }
+
+ return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
}
-OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
- OCClientResponse * clientResponse)
+OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
{
if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
{
OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
}
- if(clientResponse)
+ if (clientResponse)
+ {
+ OC_LOG(INFO, TAG, ("Discovery Response:"));
+ OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ }
+ else
{
- //OC_LOG truncates the response as it is too long.
- fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
- fflush(stderr);
+ OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
}
- return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
+ return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
}
#ifdef WITH_PRESENCE
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
std::ostringstream querySuffix;
- query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
- if(TEST_CASE == TEST_OBS_PRESENCE)
+ query << OC_RSRVD_PRESENCE_URI;
+ if (TestCase == TEST_OBS_PRESENCE)
{
- result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
- presenceCB, NULL, 0);
+ result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
+ OC_LOW_QOS, presenceCB, NULL, 0);
}
- if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
+ if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
{
querySuffix.str("");
- querySuffix << query.str() << "?rt=core.light";
- result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
- presenceCB, NULL, 0);
+ querySuffix << query.str() << "?rt=core.led";
+ result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
+ OC_LOW_QOS, presenceCB, NULL, 0);
}
- if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
+ if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
{
- if(result == OC_STACK_OK)
+ if (result == OC_STACK_OK)
{
querySuffix.str("");
querySuffix << query.str() << "?rt=core.fan";
- result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
+ result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
+ presenceCB, NULL, 0);
+ }
+ }
+ if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
+ {
+ if (result == OC_STACK_OK)
+ {
+ std::ostringstream multicastPresenceQuery;
+ multicastPresenceQuery.str("");
+ multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
+ result = InvokeOCDoResource(multicastPresenceQuery, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
presenceCB, NULL, 0);
}
}
return result;
}
#endif
-int InitGetRequestToUnavailableResource()
+
+int InitGetRequestToUnavailableResource(OCQualityOfService qos)
{
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
- return (InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0));
+ query << "/SomeUnknownResource";
+ return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
+ getReqCB, NULL, 0));
}
int InitObserveRequest(OCQualityOfService qos)
{
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
- return (InvokeOCDoResource(query, OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
+ query << coapServerResource;
+ return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
+ (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
}
-int InitPutRequest()
+int InitPutRequest(OCQualityOfService qos)
{
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
- return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
+ query << coapServerResource;
+ return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
+ putReqCB, NULL, 0));
}
int InitPostRequest(OCQualityOfService qos)
OCStackResult result;
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ query << coapServerResource;
// First POST operation (to create an Light instance)
- result = InvokeOCDoResource(query, OC_REST_POST,
+ result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
postReqCB, NULL, 0);
if (OC_STACK_OK != result)
}
// Second POST operation (to create an Light instance)
- result = InvokeOCDoResource(query, OC_REST_POST,
+ result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
postReqCB, NULL, 0);
if (OC_STACK_OK != result)
}
// This POST operation will update the original resourced /a/light
- return (InvokeOCDoResource(query, OC_REST_POST,
+ return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
postReqCB, NULL, 0));
}
void* RequestDeleteDeathResourceTask(void* myqos)
{
- sleep (30); //long enough to give the server time to finish deleting the resource.
+ sleep (30);//long enough to give the server time to finish deleting the resource.
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ query << coapServerResource;
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
// Second DELETE operation to delete the resource that might have been removed already.
OCQualityOfService qos;
if (myqos == NULL)
+ {
qos = OC_LOW_QOS;
+ }
else
+ {
qos = OC_HIGH_QOS;
+ }
- OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
+ OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
qos,
deleteReqCB, NULL, 0);
{
OCStackResult result;
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ query << coapServerResource;
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
// First DELETE operation
- result = InvokeOCDoResource(query, OC_REST_DELETE,
+ result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
qos,
deleteReqCB, NULL, 0);
if (OC_STACK_OK != result)
return result;
}
-int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
+int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
{
+
OCHeaderOption options[MAX_HEADER_OPTIONS];
OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ query << coapServerResource;
- if(withVendorSpecificHeaderOptions)
+ // ocserver is written to only process "power<X" query.
+ if (getWithQuery)
{
- uint8_t option0[] = {1,2,3,4,5,6,7,8,9,10};
- uint8_t option1[] = {11,12,13,14,15,16,17,18,19,20};
+ OC_LOG(INFO, TAG, "Using query power<50");
+ query << "?power<50";
+ }
+
+ if (withVendorSpecificHeaderOptions)
+ {
+ uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+ uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
options[0].protocolID = OC_COAP_ID;
options[0].optionID = 2048;
memcpy(options[1].optionData, option1, sizeof(option1));
options[1].optionLength = 10;
}
- if(withVendorSpecificHeaderOptions)
+ if (withVendorSpecificHeaderOptions)
{
- return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, options, 2));
+ return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
+ (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
}
else
{
- return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
+ return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
+ (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
}
}
-int InitDeviceDiscovery()
+int InitPlatformDiscovery(OCQualityOfService qos)
{
+ OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
char szQueryUri[64] = { 0 };
- cbData.cb = DeviceDiscoveryReqCB;
+ snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
+
+ cbData.cb = PlatformDiscoveryReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
- if(UNICAST_DISCOVERY)
- {
- strncpy(szQueryUri, TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY,
- (strlen(TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY) + 1));
- }
- else
+ ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
+ (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+ &cbData, NULL, 0);
+ if (ret != OC_STACK_OK)
{
- strncpy(szQueryUri, TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY,
- (strlen(TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
+ OC_LOG(ERROR, TAG, "OCStack device error");
}
-#ifdef CA_INT
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ETHERNET | OC_WIFI),
- OC_LOW_QOS, &cbData, NULL, 0);
-#else
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
-#endif
+ return ret;
+}
+
+int InitDeviceDiscovery(OCQualityOfService qos)
+{
+ OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+
+ OCStackResult ret;
+ OCCallbackData cbData;
+ char szQueryUri[100] = { 0 };
+
+ snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
+ cbData.cb = DeviceDiscoveryReqCB;
+ cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
+ cbData.cd = NULL;
+
+ ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
+ (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+ &cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack device error");
return ret;
}
-int InitDiscovery()
+int InitDiscovery(OCQualityOfService qos)
{
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
- /* Start a discovery query*/
- char szQueryUri[64] = { 0 };
- if (UNICAST_DISCOVERY)
- {
- strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
- }
- else
- {
- #ifdef CA_INT
- // TODO-CA CA is using 5298 for MC. Why 5298?
- strcpy(szQueryUri, "coap://224.0.1.187:5298/oc/core");
- #else
- strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
- #endif
- }
+ char szQueryUri[100] = { 0 };
+
+ snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
+
cbData.cb = discoveryReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
-#ifdef CA_INT
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ETHERNET | OC_WIFI),
- OC_LOW_QOS, &cbData, NULL, 0);
-#else
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
-#endif
+
+ ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
+ (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+ &cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack resource error");
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, "u:t:")) != -1)
+ while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
{
switch(opt)
{
case 'u':
- UNICAST_DISCOVERY = atoi(optarg);
+ UnicastDiscovery = atoi(optarg);
break;
case 't':
- TEST_CASE = atoi(optarg);
+ TestCase = atoi(optarg);
+ break;
+ case 'c':
+ Connectivity = atoi(optarg);
break;
default:
PrintUsage();
}
}
- if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
- (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
+ if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
+ (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
+ (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
{
PrintUsage();
return -1;
}
+ if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
+ {
+ OC_LOG(ERROR, TAG, "OCStack init error");
+ return 0;
+ }
- /*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)) == ERR_SUCCESS)
+#ifdef ROUTING_GATEWAY
+ /*
+ * Before invoking Discover resource, we process the gateway requests
+ * and form the routing table.
+ */
+ for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
+ {
+ if (OC_STACK_OK != OCProcess())
+ {
+ OC_LOG(ERROR, TAG, "OCStack process error");
+ return 0;
+ }
+ usleep(SLEEP_DURATION);
+ }
+#endif
+ if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
{
- OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
- paddr = addr;
+ ConnType = CT_ADAPTER_IP;
+ }
+ else
+ {
+ OC_LOG(INFO, TAG, "Default Connectivity type selected...");
+ PrintUsage();
}
- /* Initialize OCStack*/
- if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
- OC_LOG(ERROR, TAG, "OCStack init error");
- return 0;
+ discoveryAddr[0] = '\0';
+
+ if (UnicastDiscovery)
+ {
+ OC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
+ OC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
+ OC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
+
+ if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
+ {
+ //Strip newline char from ipv4addr
+ StripNewLineChar(discoveryAddr);
+ }
+ else
+ {
+ OC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
+ return OC_STACK_INVALID_PARAM;
+ }
}
- InitDiscovery();
+ if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
+ {
+ InitDeviceDiscovery(OC_LOW_QOS);
+ }
+ else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
+ {
+ InitPlatformDiscovery(OC_LOW_QOS);
+ }
+ else
+ {
+ InitDiscovery(OC_LOW_QOS);
+ }
// 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);
+#ifndef ROUTING_GATEAWAY
+ sleep(1);
+#endif
}
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
- 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 getConnectivityType (OCConnectivityType connType)
{
- switch (connType)
+ switch (connType & CT_MASK_ADAPTER)
{
- case OC_ETHERNET:
- return "Ethernet";
+ case CT_ADAPTER_IP:
+ return "IP";
+
+ case CT_IP_USE_V4:
+ return "IPv4";
- case OC_WIFI:
- return "WiFi";
+ case CT_IP_USE_V6:
+ return "IPv6";
- case OC_LE:
- return "BLE";
+ case CT_ADAPTER_GATT_BTLE:
+ return "GATT";
- case OC_EDR:
- return "BT";
+ case CT_ADAPTER_RFCOMM_BTEDR:
+ return "RFCOMM";
default:
return "Incorrect connectivity";
}
}
-std::string getQueryStrForGetPut(OCClientResponse * clientResponse){
-
+std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
+{
return "/a/light";
}
-void parseClientResponse(OCClientResponse * clientResponse){
- coapServerIP = getIPAddrTBServer(clientResponse);
- coapServerPort = getPortTBServer(clientResponse);
+void parseClientResponse(OCClientResponse * clientResponse)
+{
coapServerResource = getQueryStrForGetPut(clientResponse);
}