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