Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / stack / test / linux / occlient.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <signal.h>
11 #include <unistd.h>
12 #include <ocstack.h>
13
14 #define TAG PCF("occlient")
15
16 int gQuitFlag = 0;
17
18 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
19 void handleSigInt(int signum) {
20     if (signum == SIGINT) {
21         gQuitFlag = 1;
22     }
23 }
24
25 // This is a function called back when a device is discovered
26 OCStackApplicationResult applicationDiscoverCB(
27         OCClientResponse * clientResponse) {
28     uint8_t remoteIpAddr[4];
29     uint16_t remotePortNu;
30     OC_LOG(INFO, TAG, "Entering applicationDiscoverCB (Application Layer CB)");
31     OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
32             remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
33     OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
34     OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d",clientResponse->resJSONPayload,remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
35     //return OC_STACK_DELETE_TRANSACTION;
36     return OC_STACK_KEEP_TRANSACTION;
37 }
38
39 int main() {
40     uint8_t addr[20];
41     uint16_t port = USE_RANDOM_PORT;
42     uint8_t ifname[] = "eth0";
43
44     /*Get Ip address on defined interface and initialize coap on it with random port number
45      * this port number will be used as a source port in all coap communications*/
46     OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
47     OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
48
49     /* Initialize OCStack*/
50     if (OCInit((char *) addr, port, OC_CLIENT) != OC_STACK_OK) {
51         OC_LOG(ERROR, TAG, "OCStack init error");
52         return 0;
53     }
54
55     /* Start a discovery query*/
56     char szQueryUri[64] = { 0 };
57     strcpy(szQueryUri, OC_EXPLICIT_DEVICE_DISCOVERY_URI);
58     if (OCDoResource(OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE,
59             applicationDiscoverCB) != OC_STACK_OK) {
60         OC_LOG(ERROR, TAG, "OCStack resource error");
61         return 0;
62     }
63
64     // Break from loop with Ctrl+C
65     OC_LOG(INFO, TAG, "Entering occlient main loop...");
66     signal(SIGINT, handleSigInt);
67     while (!gQuitFlag) {
68
69         if (OCProcess() != OC_STACK_OK) {
70             OC_LOG(ERROR, TAG, "OCStack process error");
71             return 0;
72         }
73
74         sleep(1);
75     }OC_LOG(INFO, TAG, "Exiting occlient main loop...");
76
77     if (OCStop() != OC_STACK_OK) {
78         OC_LOG(ERROR, TAG, "OCStack stop error");
79     }
80
81     return 0;
82 }