Changed zigbee sample to use signal-atomic
[platform/upstream/iotivity.git] / plugins / samples / linux / IotivityandZigbeeServer.c
1 //******************************************************************
2 //
3 // Copyright 2015 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 #include "IotivityandZigbeeServer.h"
22 #include <signal.h>
23 #include <ocstack.h>
24 #include <logger.h>
25
26 #define TAG "IoTivityZigbeeServer"
27
28 int main(int argc, char* argv[])
29 {
30     OCStackResult result;
31     OC_LOG(INFO, TAG, "Initializing IoTivity...");
32
33     // Initialize Stack
34     result = OCInit(NULL, 0, OC_SERVER);
35     if (result != OC_STACK_OK)
36     {
37         OC_LOG_V(ERROR, TAG, "OCInit Failed %d", result);
38         return -1;
39     }
40
41     // Set Platform info
42     result = SetPlatformInfo();
43     if (result != OC_STACK_OK)
44     {
45         OC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed %d", result);
46         goto IotivityStop;
47     }
48
49     // Set Device Info
50     result  = SetDeviceInfo();
51     if (result != OC_STACK_OK)
52     {
53         OC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed: %d", result);
54         goto IotivityStop;
55     }
56     // Start Presence
57     result  = OCStartPresence(0);
58     if (result != OC_STACK_OK)
59     {
60         OC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
61         goto IotivityStop;
62     }
63
64     // PIStartPlugin
65     PIPluginBase* plugin = NULL;
66     OC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
67     result = PIStartPlugin(PLUGIN_ZIGBEE, &plugin);
68     if (result != OC_STACK_OK)
69     {
70         OC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
71         goto IotivityStop;
72     }
73
74     if (signal(SIGINT, processCancel) == SIG_ERR)
75     {
76         OC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
77     }
78     else
79     {
80         OC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
81         // Loop until sigint
82         while (!processSignal(false) && result == OC_STACK_OK)
83         {
84             result = OCProcess();
85             if (result != OC_STACK_OK)
86             {
87                 OC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
88                 break;
89             }
90
91             result = PIProcess(plugin);
92             if (result != OC_STACK_OK)
93             {
94                 OC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
95             }
96         }
97     }
98
99     OC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
100     // PIStopPlugin
101     OC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
102     result = PIStopPlugin(plugin);
103     if (result != OC_STACK_OK)
104     {
105         OC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
106     }
107
108     // OCStop
109 IotivityStop:
110     OC_LOG(INFO, TAG, "Stopping IoTivity...");
111     result = OCStop();
112     if (result != OC_STACK_OK)
113     {
114         OC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
115         return 0;
116     }
117
118     OC_LOG(INFO, TAG, "Application Completed Successfully");
119 }
120
121 OCStackResult SetPlatformInfo()
122 {
123     static const OCPlatformInfo platformInfo =
124         {
125             .platformID = "IoTivityZigbeeID",
126             .manufacturerName = "IoTivity",
127             .manufacturerUrl = "http://iotivity.org",
128             .modelNumber = "T1000",
129             .dateOfManufacture = "January 14th, 2015",
130             .platformVersion = "0.9.2",
131             .operatingSystemVersion = "7",
132             .hardwareVersion = "0.5",
133             .firmwareVersion = "0",
134             .supportUrl = "http://iotivity.org",
135             .systemTime = ""
136         };
137
138     return OCSetPlatformInfo(platformInfo);
139 }
140
141 OCStackResult SetDeviceInfo()
142 {
143     static const OCDeviceInfo deviceInfo =
144         {
145             .deviceName = "IoTivity/Zigbee Server Sample"
146         };
147
148     return OCSetDeviceInfo(deviceInfo);
149 }
150
151 bool processSignal(bool set)
152 {
153     static sig_atomic_t signal = 0;
154     if (set)
155     {
156         signal = 1;
157     }
158
159     return signal == 1;
160 }
161
162 void processCancel(int signal)
163 {
164     if(signal == SIGINT)
165     {
166         processSignal(true);
167     }
168 }
169