csdk: fix minimal linux tests
[platform/upstream/iotivity.git] / resource / csdk / stack / test / linux / ocserver.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 <string.h>
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_WINDOWS_H
29 #include <windows.h>
30 #endif
31 #include <signal.h>
32 #include <stdbool.h>
33 #include <ocstack.h>
34 #include <logger.h>
35
36 #define TAG ("ocserver")
37
38 int gQuitFlag = 0;
39 OCStackResult createLightResource();
40
41 typedef struct LIGHTRESOURCE{
42     OCResourceHandle handle;
43     bool power;
44 } LightResource;
45
46 static LightResource Light;
47
48 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
49 void handleSigInt(int signum) {
50     if (signum == SIGINT) {
51         gQuitFlag = 1;
52     }
53 }
54
55 int main() {
56     OIC_LOG_V(INFO, TAG, "Starting ocserver");
57     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) {
58         OIC_LOG(ERROR, TAG, "OCStack init error");
59         return 0;
60     }
61
62     /*
63      * Declare and create the example resource: Light
64      */
65     if(createLightResource() != OC_STACK_OK)
66     {
67         OIC_LOG(ERROR, TAG, "OCStack cannot create resource...");
68     }
69
70     // Break from loop with Ctrl-C
71     OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
72     signal(SIGINT, handleSigInt);
73     while (!gQuitFlag) {
74
75         if (OCProcess() != OC_STACK_OK) {
76             OIC_LOG(ERROR, TAG, "OCStack process error");
77             return 0;
78         }
79
80         sleep(1);
81     }
82
83     OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
84
85     if (OCStop() != OC_STACK_OK) {
86         OIC_LOG(ERROR, TAG, "OCStack process error");
87     }
88
89     return 0;
90 }
91
92 OCStackResult createLightResource() {
93     Light.power = false;
94     OCStackResult res = OCCreateResource(&Light.handle,
95                     "core.light",
96                     "core.rw",
97                     "/a/light",
98                     0,
99                     NULL,
100                     OC_DISCOVERABLE|OC_OBSERVABLE);
101     return res;
102 }
103