Sticking the ip and port into a char string with resource uri opens up
potential errors due to incorrect parsing. Use OCDevAddr struct instead
as it is meant to be used for cleaner and less fragile code.
Change-Id: I31eb7c4d39d55f7163cffbf85bc12ccb7cf77a0d
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2939
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
static uint32_t countDiscoveredResources = 0;
static bool promptUser = false;
static uint32_t countDiscoveredResources = 0;
static bool promptUser = false;
-static const char* coapServerIP = "255.255.255.255";
-static uint32_t coapServerPort = 5683;
+static OCDevAddr destinationAddress = {
+ .adapter = OC_ADAPTER_IP
+};
static void PrintResources ()
{
printf("\nResources: \n");
static void PrintResources ()
{
printf("\nResources: \n");
- for (int i = 0; i < countDiscoveredResources; ++i)
+ for (uint32_t i = 0; i < countDiscoveredResources; ++i)
{
printf("\t# : %u \t URI: %s \t Type:%s\n", i, g_discoveredResources[i].uri,
g_discoveredResources[i].resourceType);
{
printf("\t# : %u \t URI: %s \t Type:%s\n", i, g_discoveredResources[i].uri,
g_discoveredResources[i].resourceType);
-OCStackResult InvokeOCDoResource (char *query,
+OCStackResult InvokeOCDoResource (const char *query,
OCPayload *payload,
OCMethod method,
OCClientResponseHandler cb)
OCPayload *payload,
OCMethod method,
OCClientResponseHandler cb)
OCDoHandle handle = NULL;
OCDoHandle handle = NULL;
- OCStackResult ret = OCDoResource(&handle, method, query, 0, payload,
- CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
+ OCStackResult ret = OCDoResource(&handle, method, query, &destinationAddress,
+ payload, CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
if (ret != OC_STACK_OK)
{
OCClientResponse * clientResponse)
{
(void)handle;
OCClientResponse * clientResponse)
{
(void)handle;
if(clientResponse == NULL)
{
OC_LOG(INFO, TAG, "responseCallbacks received NULL clientResponse");
if(clientResponse == NULL)
{
OC_LOG(INFO, TAG, "responseCallbacks received NULL clientResponse");
int InitGetRequest (const char *resourceUri)
{
int InitGetRequest (const char *resourceUri)
{
- char query[MAX_QUERY_SIZE] = {0};
- snprintf (query, sizeof(query), "coap://%s:%u%s", coapServerIP,
- coapServerPort,resourceUri);
-
- OC_LOG_V(INFO, TAG, "Executing %s queryString is: %s", __func__, query);
-
- return (InvokeOCDoResource(query, NULL, OC_REST_GET, responseCallbacks));
+ OC_LOG_V(INFO, TAG, "Executing %s for resource: %s", __func__, resourceUri);
+ return (InvokeOCDoResource(resourceUri, NULL, OC_REST_GET, responseCallbacks));
}
int InitPutRequest (const char *resourceUri, OCPayload* payload)
{
}
int InitPutRequest (const char *resourceUri, OCPayload* payload)
{
- char query[MAX_QUERY_SIZE] = {0};
- snprintf (query, sizeof(query), "coap://%s:%u%s", coapServerIP, coapServerPort,
- resourceUri);
- OC_LOG_V(INFO, TAG, "Executing %s queryString is: %s", __func__, query);
-
- return (InvokeOCDoResource(query, payload, OC_REST_PUT, responseCallbacks));
+ OC_LOG_V(INFO, TAG, "Executing %s for resource: %s", __func__, resourceUri);
+ return (InvokeOCDoResource(resourceUri, payload, OC_REST_PUT, responseCallbacks));
}
OCPayload * getSwitchStatePayload (bool state)
}
OCPayload * getSwitchStatePayload (bool state)
OCClientResponse * clientResponse)
{
(void)handle;
OCClientResponse * clientResponse)
{
(void)handle;
if (!clientResponse)
{
OC_LOG(INFO, TAG, "Discovery response is NULL");
if (!clientResponse)
{
OC_LOG(INFO, TAG, "Discovery response is NULL");
OC_LOG_V(INFO, TAG, "Discovered @ %s:%d", clientResponse->devAddr.addr,
clientResponse->devAddr.port);
OC_LOG_V(INFO, TAG, "Discovered @ %s:%d", clientResponse->devAddr.addr,
clientResponse->devAddr.port);
- coapServerIP = OICStrdup (clientResponse->devAddr.addr);
- coapServerPort = clientResponse->devAddr.port;
+ destinationAddress = clientResponse->devAddr;
rememberDiscoveredResources (clientResponse);
rememberDiscoveredResources (clientResponse);
printf("\nUsage:<resource number> <test case> :");
char input[10] = {0};
printf("\nUsage:<resource number> <test case> :");
char input[10] = {0};
+ uint32_t resourceNo = 0;
int testCase = 0;
char * ret = fgets (input, sizeof(input), stdin);
int testCase = 0;
char * ret = fgets (input, sizeof(input), stdin);
int main(int argc, char* argv[])
{
int main(int argc, char* argv[])
{
+ (void) argc;
+ (void) argv;
OCStackResult result;
OC_LOG(INFO, TAG, "Initializing IoTivity...");
OCStackResult result;
OC_LOG(INFO, TAG, "Initializing IoTivity...");