//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <stdio.h>
+#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
static int g_testCase = 0;
static int g_connectivity = 0;
+static GMainLoop *g_mainloop = NULL;
+pthread_t g_thread;
+
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";
+static const char *DEFAULT_DB_FILE_PATH = "/opt/usr/etc/oic_svr_db_client.dat";
+
//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 g_connType = CT_ADAPTER_IP;
static void PrintUsage()
{
cout << "Hello";
- cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2>";
+ cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2|3>";
cout << "\n-u <0|1> : Perform multicast/unicast discovery of resources";
cout << "\n-c 0 : Default IP selection";
cout << "\n-c 1 : IP Connectivity Type";
cout << "\n-c 2 : EDR Connectivity Type (IPv6 not currently supported)";
+ cout << "\n-c 3 : TCP Connectivity Type";
cout << "\n-t 1 : Discover Resources";
cout << "\n-t 2 : Discover Resources and Initiate Nonconfirmable Get Request";
cout << "\n-t 3 : Discover Resources and Initiate Nonconfirmable Get Request with query filter";
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
- ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
- (method == OC_REST_PUT) ? putPayload() : NULL,
- (g_connType), qos, &cbData, options, numOptions);
+ OCPayload* payload = (method == OC_REST_PUT) ? putPayload() : NULL;
+
+ ret = OCDoRequest(&handle, method, query.str().c_str(), remoteAddr,
+ payload, (g_connType), qos, &cbData, options, numOptions);
+
+ OCPayloadDestroy(payload);
if (ret != OC_STACK_OK)
{
if(clientResponse)
{
cout << "\nStackResult: " << getResult(clientResponse->result);
- //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
}
else
{
cout << "\nStackResult: " << getResult(clientResponse->result);
cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
- //OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
+ //OIC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
{
if (clientResponse)
{
- cout << "\nStackResult: " << getResult(clientResponse->result);
- cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
- cout << "\nCallback Context for OBSERVE notification recvd successfully ";
- //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
- gNumObserveNotifies++;
- if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
+ if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
{
- if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
+ if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
{
- if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
- {
- cout << "\nObserve cancel error";
- }
- return OC_STACK_DELETE_TRANSACTION;
+ cout << "This also serves as a registration confirmation" << endl;
}
- else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
+
+ cout << "\nStackResult: " << getResult(clientResponse->result);
+ cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
+ cout << "\nCallback Context for OBSERVE notification recvd successfully ";
+ //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ gNumObserveNotifies++;
+
+ if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
{
- if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
+ if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
+ {
+ if (OCCancel(gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
+ {
+ cout << "Observe cancel error" << endl;
+ }
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+ else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
{
- cout << "\nObserve cancel error";
+ if (OCCancel(gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
+ {
+ cout << "\nObserve cancel error";
+ }
}
}
}
- if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
- {
- cout << "\nThis also serves as a registration confirmation";
- }
- else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
- {
- cout << "\nThis also serves as a deregistration confirmation";
- return OC_STACK_DELETE_TRANSACTION;
- }
- else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
+ else
{
- cout << "\nThis also tells you that registration/deregistration failed";
+ OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
+ OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
return OC_STACK_DELETE_TRANSACTION;
}
}
cout << "\nStackResult: " << getResult(clientResponse->result);
cout << "\nNONCE NUMBER: " << clientResponse->sequenceNumber;
cout << "\nCallback Context for Presence notification recvd successfully ";
- //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
gNumPresenceNotifies++;
if (gNumPresenceNotifies == 20)
{
cout << "\nDiscovered on " << connectionType.c_str();
cout << "\nDevice ======> Discovered ";
cout << clientResponse->devAddr.addr;
- if (CT_ADAPTER_IP == clientResponse->connType)
+ if (CT_ADAPTER_IP == (clientResponse->connType & CT_MASK_ADAPTER))
{
cout << ":" << clientResponse->devAddr.port;
}
- //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
cout << "\nConnectivity type: " << clientResponse->connType;
- g_connType = clientResponse->connType;
g_serverAddr = clientResponse->devAddr;
parseClientResponse(clientResponse);
+ OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
+ if (!payload)
+ {
+ cout << "\nDiscovery payload is empty!";
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
+ int targetResourceFound = 0;
+ while(resource)
+ {
+ cout << "\nFound resource: " << resource->uri;
+ if (resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
+ {
+ cout << "\nLight resource found.";
+ targetResourceFound = 1;
+
+ if (g_testCase >= TEST_OBS_PRESENCE) break;
+
+ if (resource->secure && resource->port)
+ {
+ cout << "\nLight resource with secure flag";
+ g_serverAddr.flags = (OCTransportFlags) (clientResponse->devAddr.flags | OC_SECURE);
+ g_serverAddr.port = resource->port;
+ }
+
+#ifdef WITH_TCP
+ if(g_connType == CT_ADAPTER_TCP && resource->tcpPort)
+ {
+ cout << "\nRequest will be send with TCP";
+ g_serverAddr.port = resource->tcpPort;
+ g_serverAddr.adapter = OC_ADAPTER_TCP;
+ }
+#endif
+
+ break;
+ }
+ resource = resource->next;
+ }
+
+ if (!targetResourceFound)
+ {
+ OIC_LOG_V(INFO, TAG, "No %s in payload", coapServerResource.c_str());
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
switch(g_testCase)
{
case TEST_GET_REQ_NON:
if (clientResponse)
{
- //OC_LOG truncates the response as it is too long.
- //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ //OIC_LOG truncates the response as it is too long.
+ //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
}
else
{
if (clientResponse)
{
- //OC_LOG truncates the response as it is too long.
+ //OIC_LOG truncates the response as it is too long.
cout << "\nDiscovery response: ";
cout << clientResponse->payload;
}
OCStackResult ret;
OCCallbackData cbData;
- char szQueryUri[64] = { 0 };
+ char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, g_discoveryAddr);
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);
+ ret = OCDoRequest(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)
{
cout << "\nOCStack device error";
OCStackResult ret;
OCCallbackData cbData;
- char szQueryUri[100] = { 0 };
+ char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, g_discoveryAddr);
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);
+ ret = OCDoRequest(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)
{
cout << "\nOCStack device error";
{
OCStackResult ret;
OCCallbackData cbData;
- char szQueryUri[100] = { 0 };
+ char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, g_discoveryAddr);
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);
+ ret = OCDoRequest(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)
{
cout << "\nOCStack resource error";
return ret;
}
+void *GMainLoopThread(void *param)
+{
+
+ if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
+ {
+ InitDeviceDiscovery(OC_LOW_QOS);
+ }
+ else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
+ {
+ InitPlatformDiscovery(OC_LOW_QOS);
+ }
+ else
+ {
+ InitDiscovery(OC_LOW_QOS);
+ }
+
+ while (!gQuitFlag)
+ {
+ if (OCProcess() != OC_STACK_OK)
+ {
+ cout << "\nOCStack process error";
+ return NULL;
+ }
+#ifndef ROUTING_GATEWAY
+ sleep(1);
+#endif
+ }
+
+ if (g_mainloop)
+ {
+ g_main_loop_quit(g_mainloop);
+ }
+ return NULL;
+}
+
+static FILE *client_fopen(const char */*path*/, const char *mode)
+{
+ return fopen(DEFAULT_DB_FILE_PATH, mode);
+}
+
int main(int argc, char* argv[])
{
int opt;
+ g_mainloop = g_main_loop_new(NULL, FALSE);
+ if(!g_mainloop)
+ {
+ printf("g_main_loop_new failed\n");
+ return 0;
+ }
+
while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
{
switch(opt)
cout << "\nEntering occlient main loop...\n";
- if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
+ OCPersistentStorage ps{ client_fopen, fread, fwrite, fclose, unlink };
+ OCRegisterPersistentStorageHandler(&ps);
+
+ if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
{
cout << "\nOCStack init error";
return 0;
{
if (OCProcess() != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack process error");
+ OIC_LOG(ERROR, TAG, "OCStack process error");
return 0;
}
usleep(SLEEP_DURATION);
cout << "\nSelected EDR Adapter\n";
g_connType = CT_ADAPTER_RFCOMM_BTEDR;
}
+ else if(g_connectivity == CT_TCP)
+ {
+ cout << "\nSelected TCP Adapter\n";
+ g_connType = CT_ADAPTER_TCP;
+ }
else
{
cout << "\nDefault Connectivity type selected...";
}
}
- if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
- {
- InitDeviceDiscovery(OC_LOW_QOS);
- }
- else if (g_unicastDiscovery == 0 && g_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...");
+ OIC_LOG(INFO, TAG, "Entering occlient main loop...");
signal(SIGINT, handleSigInt);
- while (!gQuitFlag)
- {
- if (OCProcess() != OC_STACK_OK)
- {
- cout << "\nOCStack process error\n";
- return 0;
- }
-#ifndef ROUTING_GATEWAY
- sleep(1);
-#endif
+ int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
+ if (result < 0)
+ {
+ printf("pthread_create failed in initialize\n");
+ return 0;
}
+ g_main_loop_run(g_mainloop);
+
cout << "\nExiting occlient main loop...\n";
if (OCStop() != OC_STACK_OK)