OC_LOG(INFO, TAG, "-c <0|1> : Send unicast messages over Ethernet or WIFI");
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 "\
+ 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 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"\
+ 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 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 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");
- OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable multicast presence.");
+ OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
#endif
- OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
+ OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
"then cancel immediately with High QOS");
- OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Get Request and "\
+ OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
"add vendor specific header options");
- OC_LOG(INFO, TAG, "-t 18 : Discover Devices");
+ OC_LOG(INFO, TAG, "-t 19 : Discover Devices");
}
OCStackResult InvokeOCDoResource(std::ostringstream &query,
switch(TEST_CASE)
{
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(OC_LOW_QOS);
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);
break;
#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_DEV_REQ:
InitDeviceDiscovery(OC_LOW_QOS);
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;
+ // ocserver is written to only process "power<X" query.
+ if (getWithQuery)
+ {
+ OC_LOG(INFO, TAG, "Using query power<30");
+ query << "?power<30";
+ }
+
if (withVendorSpecificHeaderOptions)
{
uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
return jsonResponse;
}
+/*
+ * Very simple example of query parsing.
+ * The query may have multiple filters separated by '&'.
+ * It is upto the entity handler to parse the query for the individual filters,
+ * VALIDATE them and respond as it sees fit.
+
+ * This function only returns false if the query is exactly "power<X" and
+ * current power is greater than X. If X cannot be parsed for an int,
+ * true is returned.
+ */
+bool checkIfQueryForPowerPassed(char * query)
+{
+ if (query && strcmp(query, "power<") == 0)
+ {
+ char * pointerToOperator = strstr(query, "<");
+
+ if (pointerToOperator)
+ {
+ int powerRequested = atoi(pointerToOperator + 1);
+
+ if (Light.power > powerRequested)
+ {
+ OC_LOG_V(INFO, TAG, "Current power: %d. Requested: <%d", Light.power
+ , powerRequested);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
char *payload, uint16_t maxPayloadSize)
{
OCEntityHandlerResult ehResult;
- char *getResp = constructJsonResponse(ehRequest);
- if (maxPayloadSize > strlen ((char *)getResp))
+ bool queryPassed = checkIfQueryForPowerPassed(ehRequest->query);
+
+ // Empty payload if the query has no match.
+ if (queryPassed)
{
- strncpy(payload, getResp, strlen((char *)getResp));
- ehResult = OC_EH_OK;
+ char *getResp = constructJsonResponse(ehRequest);
+
+ if (maxPayloadSize > strlen ((char *)getResp))
+ {
+ strncpy(payload, getResp, strlen((char *)getResp));
+ ehResult = OC_EH_OK;
+ }
+ else
+ {
+ OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
+ maxPayloadSize);
+ ehResult = OC_EH_ERROR;
+ }
+
+ free(getResp);
}
else
{
- OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
- maxPayloadSize);
- ehResult = OC_EH_ERROR;
+ ehResult = OC_EH_OK;
}
- free(getResp);
-
return ehResult;
}