-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cpp
330 lines (274 loc) · 8.99 KB
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//-----------------------------------------------------------------------------
// Title: [IoTivity][Blood Pressure Monitor] Server
// Description: Defines "oic.d.bloodpressuremonitor" and its information
//-----------------------------------------------------------------------------
#include "iotivity_config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#include <signal.h>
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#include "ocstack.h"
#include "logger.h"
#include "server.h"
#define TAG "SERVER"
int gQuitFlag = 0;
static char CRED_FILE[] = "server.dat";
/* SIGINT handler: set gQuitFlag to 1 for graceful termination */
void handleSigInt(int signum)
{
if (signum == SIGINT)
{
gQuitFlag = 1;
}
}
FILE* server_fopen(const char *path, const char *mode)
{
if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
{
return fopen(CRED_FILE, mode);
}
else
{
return fopen(path, mode);
}
}
void *iotivityThread(void *data) {
struct timespec timeout;
timeout.tv_sec = 0;
timeout.tv_nsec = 100000000L;
// Break from loop with Ctrl-C
OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
signal(SIGINT, handleSigInt);
while (!gQuitFlag)
{
if (OCProcess() != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack process error");
return 0;
}
nanosleep(&timeout, NULL);
}
OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
if (OCStop() != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack process error");
}
}
// Platform Info
/**
* This structure describes the platform properties. All non-Null properties will be
* included in a platform discovery request.
* @deprecated: Use OCSetPropertyValue to set platform value.
*/
const char *gPlatformID = "12341234-1234-1234-1234-123412341234";
const char *gManufacturerName = "Electronics and Telecommunications Research Institute";
const char *gManufacturerLink = "https://www.etri.re.kr";
const char *gModelNumber = "myModelNumber";
const char *gDateOfManufacture = "2017-11-09";
const char *gPlatformVersion = "1.0";
const char *gOperatingSystemVersion = "Ubuntu 16.04 LTS";
const char *gHardwareVersion = "MacBook Pro";
const char *gFirmwareVersion = "1.0";
const char *gSupportLink = "https://www.etri.re.kr";
const char *gSystemTime = "2011-08-30T13:22:53.108Z";
// Device Info
/**
* This structure is expected as input for device properties.
* device name is mandatory and expected from the application.
* device id of type UUID will be generated by the stack.
* @deprecated: Use OCSetPropertyValue to set device value.
*/
// Blood Pressure Monitor
const char *gDeviceTypeBloodPressure = "oic.d.bloodpressuremonitor";
const char *gDeviceNameBloodPressure = "Blood Pressure Monitor";
const char *gSpecVersion = "ocf.1.3.0";
const char *gDataModelVersions = "ocf.res.1.3.0, ocf.sh.1.3.0";
const char *gProtocolIndependentID = "12341234-1234-1234-1234-123412341234";
OCPlatformInfo platformInfo;
void DeletePlatformInfo()
{
free(platformInfo.platformID);
free(platformInfo.manufacturerName);
free(platformInfo.manufacturerUrl);
free(platformInfo.modelNumber);
free(platformInfo.dateOfManufacture);
free(platformInfo.platformVersion);
free(platformInfo.operatingSystemVersion);
free(platformInfo.hardwareVersion);
free(platformInfo.firmwareVersion);
free(platformInfo.supportUrl);
free(platformInfo.systemTime);
}
bool DuplicateString(char** targetString, const char* sourceString)
{
if(!sourceString)
{
return false;
}
else
{
*targetString = (char *) malloc(strlen(sourceString) + 1);
if(*targetString)
{
strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
return true;
}
}
return false;
}
OCStackResult SetPlatformInfo(const char* platformID, const char *manufacturerName,
const char *manufacturerUrl, const char *modelNumber, const char *dateOfManufacture,
const char *platformVersion, const char* operatingSystemVersion, const char* hardwareVersion,
const char *firmwareVersion, const char* supportUrl, const char* systemTime)
{
bool success = true;
if(manufacturerName != NULL && (strlen(manufacturerName) > MAX_PLATFORM_NAME_LENGTH))
{
return OC_STACK_INVALID_PARAM;
}
if(manufacturerUrl != NULL && (strlen(manufacturerUrl) > MAX_PLATFORM_URL_LENGTH))
{
return OC_STACK_INVALID_PARAM;
}
if(!DuplicateString(&platformInfo.platformID, platformID))
{
success = false;
}
if(!DuplicateString(&platformInfo.manufacturerName, manufacturerName))
{
success = false;
}
if(!DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl))
{
success = false;
}
if(!DuplicateString(&platformInfo.modelNumber, modelNumber))
{
success = false;
}
if(!DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture))
{
success = false;
}
if(!DuplicateString(&platformInfo.platformVersion, platformVersion))
{
success = false;
}
if(!DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion))
{
success = false;
}
if(!DuplicateString(&platformInfo.hardwareVersion, hardwareVersion))
{
success = false;
}
if(!DuplicateString(&platformInfo.firmwareVersion, firmwareVersion))
{
success = false;
}
if(!DuplicateString(&platformInfo.supportUrl, supportUrl))
{
success = false;
}
if(!DuplicateString(&platformInfo.systemTime, systemTime))
{
success = false;
}
if(success)
{
return OC_STACK_OK;
}
DeletePlatformInfo();
return OC_STACK_ERROR;
}
#define VERIFY_SUCCESS(op) \
{ \
if (op != OC_STACK_OK) \
{ \
OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); \
goto exit; \
} \
}
OCStackResult SetDeviceInfo(char c)
{
OCResourceHandle resourceHandle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
if (resourceHandle == NULL)
{
OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
goto exit;
}
VERIFY_SUCCESS(OCBindResourceTypeToResource(resourceHandle, gDeviceTypeBloodPressure));
VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, gDeviceNameBloodPressure));
VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, gSpecVersion));
VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
gDataModelVersions));
VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
gProtocolIndependentID));
OIC_LOG(INFO, TAG, "Device information(Blood Pressure Monitor) initialized successfully.");
return OC_STACK_OK;
exit:
return OC_STACK_ERROR;
}
int main()
{
OIC_LOG(DEBUG, TAG, "OCServer is starting...");
char command = 'P';
#if IS_SECURE_MODE
// Initialize Persistent Storage for SVR database
OCPersistentStorage ps = { server_fopen, fread, fwrite, fclose, unlink };
OCRegisterPersistentStorageHandler(&ps);
#endif
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
OCStackResult registrationResult =
SetPlatformInfo(gPlatformID, gManufacturerName, gManufacturerLink, gModelNumber,
gDateOfManufacture, gPlatformVersion, gOperatingSystemVersion,
gHardwareVersion, gFirmwareVersion, gSupportLink, gSystemTime);
if (registrationResult != OC_STACK_OK)
{
OIC_LOG(INFO, TAG, "Platform info setting failed locally!");
exit (EXIT_FAILURE);
}
registrationResult = OCSetPlatformInfo(platformInfo);
if (registrationResult != OC_STACK_OK)
{
OIC_LOG(INFO, TAG, "Platform Registration failed!");
exit (EXIT_FAILURE);
} else {
OIC_LOG(INFO, TAG, "Platform information initialized successfully.");
}
registrationResult = SetDeviceInfo(command);
if (registrationResult != OC_STACK_OK)
{
OIC_LOG(INFO, TAG, "Device Registration failed!");
exit (EXIT_FAILURE);
}
//Declare and create the example resource: BP
createBP0Resource();
createBP1Resource();
createBP2Resource();
int status;
pthread_t p_thread[3];
int thread_id;
char p2[] = "iotivity_thread";
thread_id = pthread_create(&p_thread[1], NULL, iotivityThread, (void *)p2);
if (thread_id < 0)
{
perror("thread create error : ");
exit(0);
}
pthread_join(p_thread[1], (void **)&status);
return 0;
}