Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-directory / samples / rd_main.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 #include "rd_server.h"
21
22 #include <signal.h>
23 #include <unistd.h>
24
25 int g_quitFlag = 0;
26
27 void handleSigInt(int signum);
28
29 /*
30 * This method is an entry point of Resource Directory.
31 * This function should be run only on the device that it could be host device.
32 */
33
34 int main()
35 {
36     printf("OCResourceDirectory is starting...\n");
37     OCStackResult result = OCInit(NULL, 0, OC_CLIENT_SERVER);
38     if (result != OC_STACK_OK)
39     {
40         printf("Failed starting RD server ...\n");
41         return 0;
42     }
43     if (OCRDStart() != OC_STACK_OK)
44     {
45         printf("OCRDStart failed...\n");
46         return 0;
47     }
48
49     printf("OCRDStart successfully...\n");
50
51     signal(SIGINT, handleSigInt);
52     while (!g_quitFlag)
53     {
54         if (OCProcess() != OC_STACK_OK)
55         {
56             OCRDStop();
57             printf("OCStack process error\n");
58             return 0;
59         }
60     }
61
62     if (OCRDStop() != OC_STACK_OK)
63     {
64         printf("OCRDStop failed...\n");
65     }
66     else
67     {
68         printf("OCRDStop success...\n");
69     }
70
71     printf("Exiting OCResourceDirectory main loop...\n");
72     return 0;
73
74 }
75
76 /*
77 * This is a signal handling function for SIGINT(CTRL+C).
78 * A Resource Directory handle the SIGINT signal for safe exit.
79 *
80 * @param[in] signal
81 *                 signal number of caught signal.
82 */
83 void handleSigInt(int signum)
84 {
85     if (signum == SIGINT)
86     {
87         g_quitFlag = 1;
88     }
89 }