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