Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / resource / csdk / stack / test / linux / occlient.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <ocstack.h>
28 #include <logger.h>
29
30 #define TAG PCF("occlient")
31
32 int gQuitFlag = 0;
33
34 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
35 void handleSigInt(int signum) {
36     if (signum == SIGINT) {
37         gQuitFlag = 1;
38     }
39 }
40
41 // This is a function called back when a device is discovered
42 OCStackApplicationResult applicationDiscoverCB(
43         OCClientResponse * clientResponse) {
44     uint8_t remoteIpAddr[4];
45     uint16_t remotePortNu;
46     OC_LOG(INFO, TAG, "Entering applicationDiscoverCB (Application Layer CB)");
47     OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
48             remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
49     OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
50     OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d",clientResponse->resJSONPayload,remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
51     //return OC_STACK_DELETE_TRANSACTION;
52     return OC_STACK_KEEP_TRANSACTION;
53 }
54
55 int main() {
56     uint8_t addr[20];
57     uint16_t port = USE_RANDOM_PORT;
58     uint8_t ifname[] = "eth0";
59     OCDoHandle handle;
60
61     /*Get Ip address on defined interface and initialize coap on it with random port number
62      * this port number will be used as a source port in all coap communications*/
63     OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
64     OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
65
66     /* Initialize OCStack*/
67     if (OCInit((char *) addr, port, OC_CLIENT) != OC_STACK_OK) {
68         OC_LOG(ERROR, TAG, "OCStack init error");
69         return 0;
70     }
71
72     /* Start a discovery query*/
73     char szQueryUri[64] = { 0 };
74     strcpy(szQueryUri, OC_EXPLICIT_DEVICE_DISCOVERY_URI);
75     if (OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS,
76             0, 0, 0) != OC_STACK_OK) {
77         OC_LOG(ERROR, TAG, "OCStack resource error");
78         return 0;
79     }
80
81     // Break from loop with Ctrl+C
82     OC_LOG(INFO, TAG, "Entering occlient main loop...");
83     signal(SIGINT, handleSigInt);
84     while (!gQuitFlag) {
85
86         if (OCProcess() != OC_STACK_OK) {
87             OC_LOG(ERROR, TAG, "OCStack process error");
88             return 0;
89         }
90
91         sleep(1);
92     }OC_LOG(INFO, TAG, "Exiting occlient main loop...");
93
94     if (OCStop() != OC_STACK_OK) {
95         OC_LOG(ERROR, TAG, "OCStack stop error");
96     }
97
98     return 0;
99 }