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