csdk: fix minimal linux tests
[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 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_WINDOWS_H
30 #include <windows.h>
31 #endif
32 #include <ocstack.h>
33 #include <logger.h>
34
35 #define TAG ("occlient")
36
37 int gQuitFlag = 0;
38
39 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
40 void handleSigInt(int signum) {
41     if (signum == SIGINT) {
42         gQuitFlag = 1;
43     }
44 }
45
46 // This is a function called back when a device is discovered
47 OCStackApplicationResult applicationDiscoverCB(
48         OCClientResponse * clientResponse) {
49     OIC_LOG(INFO, TAG, "Entering applicationDiscoverCB (Application Layer CB)");
50     OIC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %s:%d",
51                                     clientResponse->resourceUri,
52                                     clientResponse->devAddr.addr,
53                                     clientResponse->devAddr.port);
54     //return OC_STACK_DELETE_TRANSACTION;
55     return OC_STACK_KEEP_TRANSACTION;
56 }
57
58 int main() {
59     OIC_LOG_V(INFO, TAG, "Starting occlient");
60
61     /* Initialize OCStack*/
62     if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
63         OIC_LOG(ERROR, TAG, "OCStack init error");
64         return 0;
65     }
66
67     /* Start a discovery query*/
68     char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
69     strcpy(szQueryUri, OC_MULTICAST_DISCOVERY_URI);
70     if (OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, 
71             CT_DEFAULT, OC_LOW_QOS, 0, 0, 0) != OC_STACK_OK) {
72         OIC_LOG(ERROR, TAG, "OCStack resource error");
73         return 0;
74     }
75
76     // Break from loop with Ctrl+C
77     OIC_LOG(INFO, TAG, "Entering occlient main loop...");
78     signal(SIGINT, handleSigInt);
79     while (!gQuitFlag) {
80
81         if (OCProcess() != OC_STACK_OK) {
82             OIC_LOG(ERROR, TAG, "OCStack process error");
83             return 0;
84         }
85
86         sleep(1);
87     }
88
89     OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
90
91     if (OCStop() != OC_STACK_OK) {
92         OIC_LOG(ERROR, TAG, "OCStack stop error");
93     }
94
95     return 0;
96 }
97