[CONPRO-1337] Disabled Presence Feature
[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 #ifdef WITH_PRESENCE
55     result  = OCStartPresence(0);
56     if (result != OC_STACK_OK)
57     {
58         OIC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
59         goto IotivityStop;
60     }
61 #endif
62     // PIStartPlugin
63     PIPlugin* plugin = NULL;
64     OIC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
65     result = PIStartPlugin(defaultComPort, PLUGIN_ZIGBEE, &plugin);
66     if (result != OC_STACK_OK)
67     {
68         OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
69         goto IotivityStop;
70     }
71
72     if (signal(SIGINT, processCancel) == SIG_ERR)
73     {
74         OIC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
75     }
76     else
77     {
78         OIC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
79         // Loop until sigint
80         while (!processSignal(false) && result == OC_STACK_OK)
81         {
82             result = OCProcess();
83             if (result != OC_STACK_OK)
84             {
85                 OIC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
86                 break;
87             }
88
89             result = PIProcess(plugin);
90             if (result != OC_STACK_OK)
91             {
92                 OIC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
93             }
94         }
95     }
96
97     OIC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
98     result = PIStopPlugin(plugin);
99     if (result != OC_STACK_OK)
100     {
101         OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
102     }
103     OIC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
104     // OCStop
105 IotivityStop:
106     OIC_LOG(INFO, TAG, "Stopping IoTivity...");
107     result = OCStop();
108     if (result != OC_STACK_OK)
109     {
110         OIC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
111         return 0;
112     }
113
114     OIC_LOG(INFO, TAG, "Application Completed Successfully");
115     return 0;
116 }
117
118 OCStackResult SetPlatformInfo()
119 {
120     static const OCPlatformInfo platformInfo =
121         {
122             .platformID = "IoTivityZigbeeID",
123             .manufacturerName = "IoTivity",
124             .manufacturerUrl = "http://iotivity.org",
125             .modelNumber = "T1000",
126             .dateOfManufacture = "January 14th, 2015",
127             .platformVersion = "0.9.2",
128             .operatingSystemVersion = "7",
129             .hardwareVersion = "0.5",
130             .firmwareVersion = "0",
131             .supportUrl = "http://iotivity.org",
132             .systemTime = ""
133         };
134
135     return OCSetPlatformInfo(platformInfo);
136 }
137
138 OCStackResult SetDeviceInfo()
139 {
140     static OCDeviceInfo deviceInfo =
141         {
142             .deviceName = "IoTivity/Zigbee Server Sample",
143             .specVersion = "IoTivity/Zigbee Device Spec Version",
144         };
145     char *dmv = OICStrdup("IoTivity/Zigbee Data Model Version");
146     deviceInfo.dataModelVersions = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
147     deviceInfo.dataModelVersions->value = dmv;
148     char *dup = OICStrdup("oic.wk.d");
149     deviceInfo.types = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
150     deviceInfo.types->value = dup;
151     return OCSetDeviceInfo(deviceInfo);
152 }
153
154 bool processSignal(bool set)
155 {
156     static sig_atomic_t signal = 0;
157     if (set)
158     {
159         signal = 1;
160     }
161
162     return signal == 1;
163 }
164
165 void processCancel(int signal)
166 {
167     if(signal == SIGINT)
168     {
169         processSignal(true);
170     }
171 }