dc011aa8b43809de1f43e2c80252560a735e1965
[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 #include "oic_string.h"
26 #include "oic_malloc.h"
27
28 #define TAG "IoTivityZigbeeServer"
29 #define defaultComPort "/dev/ttyUSB0"
30 int main()
31 {
32     OIC_LOG(INFO, TAG, "Initializing IoTivity...");
33     OCStackResult result = OCInit(NULL, 0, OC_SERVER);
34     if (result != OC_STACK_OK)
35     {
36         OIC_LOG_V(ERROR, TAG, "OCInit Failed %d", result);
37         return -1;
38     }
39
40     result = SetPlatformInfo();
41     if (result != OC_STACK_OK)
42     {
43         OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed %d", result);
44         goto IotivityStop;
45     }
46
47     result  = SetDeviceInfo();
48     if (result != OC_STACK_OK)
49     {
50         OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed: %d", result);
51         goto IotivityStop;
52     }
53
54     result  = OCStartPresence(0);
55     if (result != OC_STACK_OK)
56     {
57         OIC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
58         goto IotivityStop;
59     }
60
61     // PIStartPlugin
62     PIPlugin* plugin = NULL;
63     OIC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
64     result = PIStartPlugin(defaultComPort, PLUGIN_ZIGBEE, &plugin);
65     if (result != OC_STACK_OK)
66     {
67         OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
68         goto IotivityStop;
69     }
70
71     if (signal(SIGINT, processCancel) == SIG_ERR)
72     {
73         OIC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
74     }
75     else
76     {
77         OIC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
78         // Loop until sigint
79         while (!processSignal(false) && result == OC_STACK_OK)
80         {
81             result = OCProcess();
82             if (result != OC_STACK_OK)
83             {
84                 OIC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
85                 break;
86             }
87
88             result = PIProcess(plugin);
89             if (result != OC_STACK_OK)
90             {
91                 OIC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
92             }
93         }
94     }
95
96     OIC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
97     result = PIStopPlugin(plugin);
98     if (result != OC_STACK_OK)
99     {
100         OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
101     }
102     OIC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
103     // OCStop
104 IotivityStop:
105     OIC_LOG(INFO, TAG, "Stopping IoTivity...");
106     result = OCStop();
107     if (result != OC_STACK_OK)
108     {
109         OIC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
110         return 0;
111     }
112
113     OIC_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 OCDeviceInfo deviceInfo =
140         {
141             .deviceName = "IoTivity/Zigbee Server Sample",
142         };
143     char *dup = OICStrdup("oic.wk.d");
144     deviceInfo.types = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
145     deviceInfo.types->value = dup;
146     OICFree(dup);
147     return OCSetDeviceInfo(deviceInfo);
148 }
149
150 bool processSignal(bool set)
151 {
152     static sig_atomic_t signal = 0;
153     if (set)
154     {
155         signal = 1;
156     }
157
158     return signal == 1;
159 }
160
161 void processCancel(int signal)
162 {
163     if(signal == SIGINT)
164     {
165         processSignal(true);
166     }
167 }