1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
20 ///////////////////////////////////////////////////////////////////////
21 //NOTE : This sample server is generated based on ocserverbasicops.cpp
22 ///////////////////////////////////////////////////////////////////////
31 #include "ocpayload.h"
32 #include "oic_string.h"
34 #define TAG "UNITTEST_SERVER_2"
38 /* Structure to represent a LED resource */
39 typedef struct LEDRESOURCE{
40 OCResourceHandle handle;
45 static LEDResource LED;
46 // This variable determines instance number of the LED resource.
47 // Used by POST method to create a new instance of LED resource.
48 static int gCurrLedInstance = 0;
49 #define SAMPLE_MAX_NUM_POST_INSTANCE 2
50 static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
52 char *gResourceUri= (char *)"/a/led";
54 //Secure Virtual Resource database for Iotivity Server
55 //It contains Server's Identity and the PSK credentials
56 //of other devices which the server trusts
57 static char CRED_FILE[] = "oic_svr_db_server2.dat";
59 static uint8_t DEFAULT_SVR_DB[] = {
60 0xBF, 0x63, 0x61, 0x63, 0x6C, 0x59, 0x02, 0x76, 0xA2, 0x66, 0x61, 0x63, 0x6C, 0x69, 0x73, 0x74,
61 0xA1, 0x64, 0x61, 0x63, 0x65, 0x73, 0x84, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74,
62 0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73,
63 0x86, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x68, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x72, 0x65, 0x73,
64 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68,
65 0x72, 0x65, 0x66, 0x66, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62,
66 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x66, 0x2F, 0x6F,
67 0x69, 0x63, 0x2F, 0x70, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66,
68 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6A, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x72, 0x65, 0x73,
69 0x2F, 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4,
70 0x64, 0x68, 0x72, 0x65, 0x66, 0x70, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x72, 0x65, 0x73, 0x2F, 0x74,
71 0x79, 0x70, 0x65, 0x73, 0x2F, 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62,
72 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6D, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x70,
73 0x72, 0x65, 0x73, 0x65, 0x6E, 0x63, 0x65, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60,
74 0x62, 0x69, 0x66, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02,
75 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69,
76 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73, 0x84, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66,
77 0x6D, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x64, 0x6F, 0x78, 0x6D, 0x63, 0x72,
78 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65,
79 0x66, 0x6E, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x70, 0x73, 0x74, 0x61, 0x74,
80 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68,
81 0x72, 0x65, 0x66, 0x6C, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x61, 0x63, 0x6C,
82 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68,
83 0x72, 0x65, 0x66, 0x6D, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x63, 0x72, 0x65,
84 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0x6A, 0x70,
85 0x65, 0x72, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x06, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A,
86 0x65, 0x63, 0x74, 0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72,
87 0x63, 0x65, 0x73, 0x82, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6E, 0x2F, 0x6F, 0x69, 0x63, 0x2F,
88 0x73, 0x65, 0x63, 0x2F, 0x70, 0x63, 0x6F, 0x6E, 0x66, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72,
89 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x71, 0x2F, 0x6F, 0x69,
90 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x64, 0x70, 0x61, 0x69, 0x72, 0x69, 0x6E, 0x67, 0x63, 0x72,
91 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D,
92 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x06, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74,
93 0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73,
94 0x81, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6C, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63,
95 0x2F, 0x76, 0x65, 0x72, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66,
96 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0x6A, 0x72, 0x6F,
97 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
98 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32,
99 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x65, 0x70,
100 0x73, 0x74, 0x61, 0x74, 0x58, 0x79, 0xA7, 0x64, 0x69, 0x73, 0x6F, 0x70, 0xF4, 0x6A, 0x64, 0x65,
101 0x76, 0x69, 0x63, 0x65, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
102 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32,
103 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x62, 0x63,
104 0x6D, 0x02, 0x62, 0x74, 0x6D, 0x00, 0x62, 0x6F, 0x6D, 0x03, 0x62, 0x73, 0x6D, 0x03, 0x6A, 0x72,
105 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x32, 0x32, 0x32, 0x32, 0x32,
106 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32,
107 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x64,
108 0x64, 0x6F, 0x78, 0x6D, 0x58, 0xB6, 0xA8, 0x64, 0x6F, 0x78, 0x6D, 0x73, 0x81, 0x00, 0x66, 0x6F,
109 0x78, 0x6D, 0x73, 0x65, 0x6C, 0x00, 0x63, 0x73, 0x63, 0x74, 0x01, 0x65, 0x6F, 0x77, 0x6E, 0x65,
110 0x64, 0xF4, 0x6A, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x32,
111 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32,
112 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
113 0x32, 0x32, 0x32, 0x6C, 0x64, 0x65, 0x76, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64,
114 0x78, 0x24, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2D, 0x30, 0x30, 0x30, 0x30, 0x2D,
115 0x30, 0x30, 0x30, 0x30, 0x2D, 0x30, 0x30, 0x30, 0x30, 0x2D, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
116 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x6A, 0x72, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69,
117 0x64, 0x78, 0x24, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32,
118 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x2D, 0x32, 0x32, 0x32, 0x32, 0x32,
119 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x63, 0x64, 0x70, 0x63, 0xF5, 0xFF
122 /* Function that creates a new LED resource by calling the
123 * OCCreateResource() method.
125 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower);
127 /* This method converts the payload to JSON format */
128 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest);
130 /* Following methods process the PUT, GET, POST
133 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
134 OCRepPayload **payload);
135 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
136 OCRepPayload **payload);
137 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
138 OCEntityHandlerResponse *response,
139 OCRepPayload **payload);
141 /* Entity Handler callback functions */
142 OCEntityHandlerResult
143 OCEntityHandlerCb (OCEntityHandlerFlag flag,
144 OCEntityHandlerRequest *entityHandlerRequest,
145 void* callbackParam);
147 const char *getResult(OCStackResult result) {
150 return "OC_STACK_OK";
151 case OC_STACK_RESOURCE_CREATED:
152 return "OC_STACK_RESOURCE_CREATED";
153 case OC_STACK_RESOURCE_DELETED:
154 return "OC_STACK_RESOURCE_DELETED";
155 case OC_STACK_INVALID_URI:
156 return "OC_STACK_INVALID_URI";
157 case OC_STACK_INVALID_QUERY:
158 return "OC_STACK_INVALID_QUERY";
159 case OC_STACK_INVALID_IP:
160 return "OC_STACK_INVALID_IP";
161 case OC_STACK_INVALID_PORT:
162 return "OC_STACK_INVALID_PORT";
163 case OC_STACK_INVALID_CALLBACK:
164 return "OC_STACK_INVALID_CALLBACK";
165 case OC_STACK_INVALID_METHOD:
166 return "OC_STACK_INVALID_METHOD";
167 case OC_STACK_NO_MEMORY:
168 return "OC_STACK_NO_MEMORY";
169 case OC_STACK_COMM_ERROR:
170 return "OC_STACK_COMM_ERROR";
171 case OC_STACK_INVALID_PARAM:
172 return "OC_STACK_INVALID_PARAM";
173 case OC_STACK_NOTIMPL:
174 return "OC_STACK_NOTIMPL";
175 case OC_STACK_NO_RESOURCE:
176 return "OC_STACK_NO_RESOURCE";
177 case OC_STACK_RESOURCE_ERROR:
178 return "OC_STACK_RESOURCE_ERROR";
179 case OC_STACK_SLOW_RESOURCE:
180 return "OC_STACK_SLOW_RESOURCE";
181 case OC_STACK_NO_OBSERVERS:
182 return "OC_STACK_NO_OBSERVERS";
184 case OC_STACK_PRESENCE_STOPPED:
185 return "OC_STACK_PRESENCE_STOPPED";
188 return "OC_STACK_ERROR";
194 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
196 OCRepPayload* payload = OCRepPayloadCreate();
199 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
203 OCRepPayloadSetUri(payload, uri);
204 OCRepPayloadSetPropBool(payload, "state", state);
205 OCRepPayloadSetPropInt(payload, "power", power);
210 //This function takes the request as an input and returns the response
211 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
213 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
215 OIC_LOG(ERROR, TAG, "Incoming payload not a representation");
219 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
221 LEDResource *currLEDResource = &LED;
223 if (ehRequest->resource == gLedInstance[0].handle)
225 currLEDResource = &gLedInstance[0];
226 gResourceUri = (char *) "/a/led/0";
228 else if (ehRequest->resource == gLedInstance[1].handle)
230 currLEDResource = &gLedInstance[1];
231 gResourceUri = (char *) "/a/led/1";
234 if(OC_REST_PUT == ehRequest->method)
236 // Get pointer to query
238 if(OCRepPayloadGetPropInt(input, "power", &pow))
240 currLEDResource->power =pow;
244 if(OCRepPayloadGetPropBool(input, "state", &state))
246 currLEDResource->state = state;
250 return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
253 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
254 OCRepPayload **payload)
256 OCEntityHandlerResult ehResult;
258 OCRepPayload *getResp = constructResponse(ehRequest);
267 ehResult = OC_EH_ERROR;
273 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
274 OCRepPayload **payload)
276 OCEntityHandlerResult ehResult;
278 OCRepPayload *putResp = constructResponse(ehRequest);
287 ehResult = OC_EH_ERROR;
293 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
294 OCEntityHandlerResponse *response, OCRepPayload **payload)
296 OCRepPayload *respPLPost_led = NULL;
297 OCEntityHandlerResult ehResult = OC_EH_OK;
300 * The entity handler determines how to process a POST request.
301 * Per the REST paradigm, POST can also be used to update representation of existing
302 * resource or create a new resource.
303 * In the sample below, if the POST is for /a/led then a new instance of the LED
304 * resource is created with default representation (if representation is included in
305 * POST payload it can be used as initial values) as long as the instance is
306 * lesser than max new instance count. Once max instance count is reached, POST on
307 * /a/led updated the representation of /a/led (just like PUT)
310 if (ehRequest->resource == LED.handle)
312 if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
314 // Create new LED instance
315 char newLedUri[15] = "/a/led/";
316 int newLedUriLength = strlen(newLedUri);
317 snprintf (newLedUri + newLedUriLength, sizeof(newLedUri)-newLedUriLength, "%d", gCurrLedInstance);
319 respPLPost_led = OCRepPayloadCreate();
320 OCRepPayloadSetUri(respPLPost_led, gResourceUri);
321 OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
323 if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
325 OIC_LOG (INFO, TAG, "Created new LED instance");
326 gLedInstance[gCurrLedInstance].state = 0;
327 gLedInstance[gCurrLedInstance].power = 0;
329 strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
330 ehResult = OC_EH_RESOURCE_CREATED;
335 respPLPost_led = constructResponse(ehRequest);
340 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
342 if (ehRequest->resource == gLedInstance[i].handle)
346 respPLPost_led = constructResponse(ehRequest);
351 respPLPost_led = constructResponse(ehRequest);
357 if (respPLPost_led != NULL)
359 *payload = respPLPost_led;
364 OIC_LOG_V (INFO, TAG, "Payload was NULL");
365 ehResult = OC_EH_ERROR;
371 OCEntityHandlerResult
372 OCEntityHandlerCb (OCEntityHandlerFlag flag,
373 OCEntityHandlerRequest *entityHandlerRequest,
376 OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
378 OCEntityHandlerResult ehResult = OC_EH_ERROR;
380 OCEntityHandlerResponse response;
381 memset(&response, 0, sizeof(response));
384 if (!entityHandlerRequest)
386 OIC_LOG (ERROR, TAG, "Invalid request pointer");
390 OCRepPayload* payload = NULL;
392 if (flag & OC_REQUEST_FLAG)
394 OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
395 if (entityHandlerRequest)
397 if (OC_REST_GET == entityHandlerRequest->method)
399 OIC_LOG (INFO, TAG, "Received OC_REST_GET from client");
400 ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
402 else if (OC_REST_PUT == entityHandlerRequest->method)
404 OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
405 ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
407 else if (OC_REST_POST == entityHandlerRequest->method)
409 OIC_LOG (INFO, TAG, "Received OC_REST_POST from client");
410 ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
414 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
415 entityHandlerRequest->method);
416 ehResult = OC_EH_ERROR;
419 if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
421 // Format the response. Note this requires some info about the request
422 response.requestHandle = entityHandlerRequest->requestHandle;
423 response.resourceHandle = entityHandlerRequest->resource;
424 response.ehResult = ehResult;
425 response.payload = (OCPayload*)(payload);
426 response.numSendVendorSpecificHeaderOptions = 0;
427 memset(response.sendVendorSpecificHeaderOptions, 0,
428 sizeof(response.sendVendorSpecificHeaderOptions));
429 memset(response.resourceUri, 0, sizeof(response.resourceUri));
430 // Indicate that response is NOT in a persistent buffer
431 response.persistentBufferFlag = 0;
434 if (OCDoResponse(&response) != OC_STACK_OK)
436 OIC_LOG(ERROR, TAG, "Error sending response");
437 ehResult = OC_EH_ERROR;
443 OCPayloadDestroy(response.payload);
447 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
448 void handleSigInt(int signum)
450 if (signum == SIGINT)
456 static void GetCurrentWorkingDirectory(char* buf, size_t bufsize)
458 char cwd[1024] = {0};
459 const char* unittest_path = "resource/csdk/security/provisioning/unittest";
460 if(getcwd(cwd, sizeof(cwd)) != NULL)
462 if(strstr(cwd, unittest_path) == NULL)
464 #if defined __linux__
466 snprintf(buf, bufsize, "%s/out/linux/x86_64/release/%s/", cwd, unittest_path);
467 snprintf(buf, bufsize, "%s/out/linux/x86_64/release/%s/", cwd, unittest_path);
469 snprintf(buf, bufsize, "%s/out/linux/x86/release/%s/", cwd, unittest_path);
470 snprintf(buf, bufsize, "%s/out/linux/x86/release/%s/", cwd, unittest_path);
472 #endif //defined __linux__
476 snprintf(buf, bufsize, "%s/", cwd);
481 FILE* server_fopen(const char *path, const char *mode)
484 char cwd[1024] = {0};
485 char cred_path[1024] = {0};
486 GetCurrentWorkingDirectory(cwd, sizeof(cwd));
487 snprintf(cred_path, sizeof(cred_path), "%s%s", cwd, CRED_FILE);
488 return fopen(cred_path, mode);
493 struct timespec timeout;
495 //Delete previous SVR DB, if exist.
496 char cwd[1024] = {0};
497 char cred_path[1024] = {0};
498 char del_cmd[1024] = {0};
500 GetCurrentWorkingDirectory(cwd, sizeof(cwd));
501 snprintf(del_cmd, sizeof(del_cmd), "rm -rf %s%s", cwd, CRED_FILE);
504 //Generate default SVR DB.
505 snprintf(cred_path, sizeof(cred_path), "%s%s", cwd, CRED_FILE);
506 fp = fopen(cred_path, "w");
509 size_t numberItems = fwrite(DEFAULT_SVR_DB, 1, sizeof(DEFAULT_SVR_DB), fp);
510 if(sizeof(DEFAULT_SVR_DB) != numberItems)
512 OIC_LOG_V(ERROR, TAG, "Failed to initialize SVR DB (%s)", cred_path);
519 OIC_LOG(DEBUG, TAG, "OCServer is starting...");
521 // Initialize Persistent Storage for SVR database
522 OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
524 OCRegisterPersistentStorageHandler(&ps);
526 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
528 OIC_LOG(ERROR, TAG, "OCStack init error");
533 * Declare and create the example resource: LED
535 createLEDResource(gResourceUri, &LED, false, 0);
538 timeout.tv_nsec = 100000000L;
540 // Break from loop with Ctrl-C
541 OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
542 signal(SIGINT, handleSigInt);
545 if (OCProcess() != OC_STACK_OK)
547 OIC_LOG(ERROR, TAG, "OCStack process error");
550 nanosleep(&timeout, NULL);
553 OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
555 if (OCStop() != OC_STACK_OK)
557 OIC_LOG(ERROR, TAG, "OCStack process error");
563 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
567 OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
571 ledResource->state = resourceState;
572 ledResource->power= resourcePower;
573 OCStackResult res = OCCreateResource(&(ledResource->handle),
575 OC_RSRVD_INTERFACE_DEFAULT,
579 OC_DISCOVERABLE|OC_OBSERVABLE | OC_SECURE);
580 OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));