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