examples: Add notice about using experimental API
[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
25 #include "oic_string.h"
26 #include "oic_malloc.h"
27
28 #define TAG "IoTivityZigbeeServer"
29 #define defaultComPort "/dev/ttyUSB0"
30
31 /// This example is using experimental API, so there is no guarantee of support for future release,
32 /// nor any there any guarantee that breaking changes will not occur across releases.
33 #include "experimental/logger.h"
34
35
36 #define VERIFY_SUCCESS(op)                          \
37 {                                                   \
38     if (op !=  OC_STACK_OK)                         \
39     {                                               \
40         OIC_LOG_V(FATAL, TAG, "%s failed!!", #op);  \
41         goto exit;                                  \
42     }                                               \
43 }
44
45 int main()
46 {
47     OIC_LOG(INFO, TAG, "Initializing IoTivity...");
48     OCStackResult result = OCInit(NULL, 0, OC_SERVER);
49     if (result != OC_STACK_OK)
50     {
51         OIC_LOG_V(ERROR, TAG, "OCInit Failed %d", result);
52         return -1;
53     }
54
55     result = SetPlatformInfo();
56     if (result != OC_STACK_OK)
57     {
58         OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed %d", result);
59         goto IotivityStop;
60     }
61
62     result  = SetDeviceInfo();
63     if (result != OC_STACK_OK)
64     {
65         OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed: %d", result);
66         goto IotivityStop;
67     }
68
69     result  = OCStartPresence(0);
70     if (result != OC_STACK_OK)
71     {
72         OIC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
73         goto IotivityStop;
74     }
75
76     // PIStartPlugin
77     PIPluginPtr plugin = NULL;
78     OIC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
79     result = PIStartPlugin(defaultComPort, PLUGIN_ZIGBEE, &plugin);
80     if (result != OC_STACK_OK)
81     {
82         OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
83         goto IotivityStop;
84     }
85
86     if (signal(SIGINT, processCancel) == SIG_ERR)
87     {
88         OIC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
89     }
90     else
91     {
92         OIC_LOG(INFO, TAG, "Performing Zigbee discovery. This process takes 15 seconds.");
93
94         result = PISetup(plugin);
95         if (result != OC_STACK_OK)
96         {
97             OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Discovery Failed: %d", result);
98             goto IotivityStop;
99         }
100
101         OIC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
102         // Loop until sigint
103         while (!processSignal(false) && result == OC_STACK_OK)
104         {
105             result = OCProcess();
106             if (result != OC_STACK_OK)
107             {
108                 OIC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
109                 break;
110             }
111
112             result = PIProcess(plugin);
113             if (result != OC_STACK_OK)
114             {
115                 OIC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
116             }
117         }
118     }
119
120     OIC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
121     result = PIStopPlugin(plugin);
122     if (result != OC_STACK_OK)
123     {
124         OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
125     }
126     OIC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
127     // OCStop
128 IotivityStop:
129     OIC_LOG(INFO, TAG, "Stopping IoTivity...");
130     result = OCStop();
131     if (result != OC_STACK_OK)
132     {
133         OIC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
134         return 0;
135     }
136
137     OIC_LOG(INFO, TAG, "Application Completed Successfully");
138     return 0;
139 }
140
141 OCStackResult SetPlatformInfo()
142 {
143     static const OCPlatformInfo platformInfo =
144         {
145             .platformID = "IoTivityZigbeeID",
146             .manufacturerName = "IoTivity",
147             .manufacturerUrl = "http://iotivity.org",
148             .modelNumber = "T1000",
149             .dateOfManufacture = "January 14th, 2015",
150             .platformVersion = "0.9.2",
151             .operatingSystemVersion = "7",
152             .hardwareVersion = "0.5",
153             .firmwareVersion = "0",
154             .supportUrl = "http://iotivity.org",
155             .systemTime = ""
156         };
157
158     return OCSetPlatformInfo(platformInfo);
159 }
160
161 OCStackResult SetDeviceInfo()
162 {
163     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME,
164                                       "IoTivity/Zigbee Server Sample"));
165
166     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION,
167                                       "IoTivity/Zigbee Device Spec Version"));
168
169     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
170                                       "IoTivity/Zigbee Data Model Version"));
171
172     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
173                                       "4ea65ac9-59a3-4eb8-8d77-76c3ee72c250"));
174
175     OIC_LOG(INFO, TAG, "Device information initialized successfully.");
176     return OC_STACK_OK;
177
178 exit:
179     return OC_STACK_ERROR;
180
181 }
182
183 bool processSignal(bool set)
184 {
185     static sig_atomic_t signal = 0;
186     if (set)
187     {
188         signal = 1;
189     }
190
191     return signal == 1;
192 }
193
194 void processCancel(int signal)
195 {
196     if(signal == SIGINT)
197     {
198         processSignal(true);
199     }
200 }