Update OTM's unit test to use the real resource servers during OTM test
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / unittest / sampleserver1.cpp
1 /******************************************************************
2 *
3 * Copyright 2015 Samsung Electronics 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 //NOTE :  This sample server is generated based on ocserverbasicops.cpp
22 ///////////////////////////////////////////////////////////////////////
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include "ocstack.h"
30 #include "logger.h"
31 #include "ocpayload.h"
32 #include "oic_string.h"
33
34 #define TAG "UNITTEST_SERVER_1"
35
36 int gQuitFlag = 0;
37
38 /* Structure to represent a LED resource */
39 typedef struct LEDRESOURCE{
40     OCResourceHandle handle;
41     bool state;
42     int power;
43 } LEDResource;
44
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];
51
52 char *gResourceUri= (char *)"/a/led";
53
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_server1.dat";
58
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, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
98     0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31,
99     0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 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, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
102     0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31,
103     0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 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, 0x31, 0x31, 0x31, 0x31, 0x31,
106     0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31,
107     0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 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, 0x31,
111     0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31,
112     0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
113     0x31, 0x31, 0x31, 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, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31,
118     0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x2D, 0x31, 0x31, 0x31, 0x31, 0x31,
119     0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x63, 0x64, 0x70, 0x63, 0xF5, 0xFF
120 };
121
122 /* Function that creates a new LED resource by calling the
123  * OCCreateResource() method.
124  */
125 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower);
126
127 /* This method converts the payload to JSON format */
128 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest);
129
130 /* Following methods process the PUT, GET, POST
131  * requests
132  */
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);
140
141 /* Entity Handler callback functions */
142 OCEntityHandlerResult
143 OCEntityHandlerCb (OCEntityHandlerFlag flag,
144         OCEntityHandlerRequest *entityHandlerRequest,
145         void* callbackParam);
146
147 const char *getResult(OCStackResult result) {
148     switch (result) {
149     case OC_STACK_OK:
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";
183     #ifdef WITH_PRESENCE
184     case OC_STACK_PRESENCE_STOPPED:
185         return "OC_STACK_PRESENCE_STOPPED";
186     #endif
187     case OC_STACK_ERROR:
188         return "OC_STACK_ERROR";
189     default:
190         return "UNKNOWN";
191     }
192 }
193
194 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
195 {
196     OCRepPayload* payload = OCRepPayloadCreate();
197     if(!payload)
198     {
199         OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
200         return NULL;
201     }
202
203     OCRepPayloadSetUri(payload, uri);
204     OCRepPayloadSetPropBool(payload, "state", state);
205     OCRepPayloadSetPropInt(payload, "power", power);
206
207     return payload;
208 }
209
210 //This function takes the request as an input and returns the response
211 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
212 {
213     if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
214     {
215         OIC_LOG(ERROR, TAG, "Incoming payload not a representation");
216         return NULL;
217     }
218
219     OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
220
221     LEDResource *currLEDResource = &LED;
222
223     if (ehRequest->resource == gLedInstance[0].handle)
224     {
225         currLEDResource = &gLedInstance[0];
226         gResourceUri = (char *) "/a/led/0";
227     }
228     else if (ehRequest->resource == gLedInstance[1].handle)
229     {
230         currLEDResource = &gLedInstance[1];
231         gResourceUri = (char *) "/a/led/1";
232     }
233
234     if(OC_REST_PUT == ehRequest->method)
235     {
236         // Get pointer to query
237         int64_t pow;
238         if(OCRepPayloadGetPropInt(input, "power", &pow))
239         {
240             currLEDResource->power =pow;
241         }
242
243         bool state;
244         if(OCRepPayloadGetPropBool(input, "state", &state))
245         {
246             currLEDResource->state = state;
247         }
248     }
249
250     return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
251 }
252
253 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
254         OCRepPayload **payload)
255 {
256     OCEntityHandlerResult ehResult;
257
258     OCRepPayload *getResp = constructResponse(ehRequest);
259
260     if(getResp)
261     {
262         *payload = getResp;
263         ehResult = OC_EH_OK;
264     }
265     else
266     {
267         ehResult = OC_EH_ERROR;
268     }
269
270     return ehResult;
271 }
272
273 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
274         OCRepPayload **payload)
275 {
276     OCEntityHandlerResult ehResult;
277
278     OCRepPayload *putResp = constructResponse(ehRequest);
279
280     if(putResp)
281     {
282         *payload = putResp;
283         ehResult = OC_EH_OK;
284     }
285     else
286     {
287         ehResult = OC_EH_ERROR;
288     }
289
290     return ehResult;
291 }
292
293 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
294         OCEntityHandlerResponse *response, OCRepPayload **payload)
295 {
296     OCRepPayload *respPLPost_led = NULL;
297     OCEntityHandlerResult ehResult = OC_EH_OK;
298
299     /*
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)
308      */
309
310     if (ehRequest->resource == LED.handle)
311     {
312         if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
313         {
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);
318
319             respPLPost_led = OCRepPayloadCreate();
320             OCRepPayloadSetUri(respPLPost_led, gResourceUri);
321             OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
322
323             if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
324             {
325                 OIC_LOG (INFO, TAG, "Created new LED instance");
326                 gLedInstance[gCurrLedInstance].state = 0;
327                 gLedInstance[gCurrLedInstance].power = 0;
328                 gCurrLedInstance++;
329                 strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
330                 ehResult = OC_EH_RESOURCE_CREATED;
331             }
332         }
333         else
334         {
335             respPLPost_led = constructResponse(ehRequest);
336         }
337     }
338     else
339     {
340         for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
341         {
342             if (ehRequest->resource == gLedInstance[i].handle)
343             {
344                 if (i == 0)
345                 {
346                     respPLPost_led = constructResponse(ehRequest);
347                     break;
348                 }
349                 else if (i == 1)
350                 {
351                     respPLPost_led = constructResponse(ehRequest);
352                 }
353             }
354         }
355     }
356
357     if (respPLPost_led != NULL)
358     {
359         *payload = respPLPost_led;
360         ehResult = OC_EH_OK;
361     }
362     else
363     {
364         OIC_LOG_V (INFO, TAG, "Payload was NULL");
365         ehResult = OC_EH_ERROR;
366     }
367
368     return ehResult;
369 }
370
371 OCEntityHandlerResult
372 OCEntityHandlerCb (OCEntityHandlerFlag flag,
373         OCEntityHandlerRequest *entityHandlerRequest,
374         void* callbackParam)
375 {
376     OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
377     (void)callbackParam;
378     OCEntityHandlerResult ehResult = OC_EH_ERROR;
379
380     OCEntityHandlerResponse response;
381     memset(&response, 0, sizeof(response));
382
383     // Validate pointer
384     if (!entityHandlerRequest)
385     {
386         OIC_LOG (ERROR, TAG, "Invalid request pointer");
387         return OC_EH_ERROR;
388     }
389
390     OCRepPayload* payload = NULL;
391
392     if (flag & OC_REQUEST_FLAG)
393     {
394         OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
395         if (entityHandlerRequest)
396         {
397             if (OC_REST_GET == entityHandlerRequest->method)
398             {
399                 OIC_LOG (INFO, TAG, "Received OC_REST_GET from client");
400                 ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
401             }
402             else if (OC_REST_PUT == entityHandlerRequest->method)
403             {
404                 OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
405                 ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
406             }
407             else if (OC_REST_POST == entityHandlerRequest->method)
408             {
409                 OIC_LOG (INFO, TAG, "Received OC_REST_POST from client");
410                 ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
411             }
412             else
413             {
414                 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
415                         entityHandlerRequest->method);
416                 ehResult = OC_EH_ERROR;
417             }
418
419             if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
420             {
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;
432
433                 // Send the response
434                 if (OCDoResponse(&response) != OC_STACK_OK)
435                 {
436                     OIC_LOG(ERROR, TAG, "Error sending response");
437                     ehResult = OC_EH_ERROR;
438                 }
439             }
440         }
441     }
442
443     OCPayloadDestroy(response.payload);
444     return ehResult;
445 }
446
447 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
448 void handleSigInt(int signum)
449 {
450     if (signum == SIGINT)
451     {
452         gQuitFlag = 1;
453     }
454 }
455
456 static void GetCurrentWorkingDirectory(char* buf, size_t bufsize)
457 {
458     char cwd[1024] = {0};
459     const char* unittest_path = "resource/csdk/security/provisioning/unittest";
460     if(getcwd(cwd, sizeof(cwd)) != NULL)
461     {
462         if(strstr(cwd, unittest_path) == NULL)
463         {
464 #if defined __linux__
465 #if __x86_64__
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);
468 #else
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);
471 #endif //__x86_64__
472 #endif //defined __linux__
473         }
474         else
475         {
476             snprintf(buf, bufsize, "%s/", cwd);
477         }
478     }
479 }
480
481 FILE* server_fopen(const char *path, const char *mode)
482 {
483     (void)path;
484     char cwd[1024] = {0};
485     char cred_path[1024] = {0};
486     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
487     sprintf(cred_path, "%s%s", cwd, CRED_FILE);
488     return fopen(cred_path, mode);
489 }
490
491 int main()
492 {
493     struct timespec timeout;
494
495     //Delete previous SVR DB, if exist.
496     char cwd[1024] = {0};
497     char cred_path[1024] = {0};
498     char del_cmd[1024] = {0};
499     FILE* fp = NULL;
500     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
501     sprintf(del_cmd, "rm -rf %s%s", cwd, CRED_FILE);
502     system(del_cmd);
503
504     //Generate default SVR DB.
505     sprintf(cred_path, "%s%s", cwd, CRED_FILE);
506     fp = fopen(cred_path, "w");
507     if(NULL != fp)
508     {
509         size_t numberItems = fwrite(DEFAULT_SVR_DB, 1, sizeof(DEFAULT_SVR_DB), fp);
510         if(sizeof(DEFAULT_SVR_DB) != numberItems)
511         {
512             OIC_LOG_V(ERROR, TAG, "Failed to initialize SVR DB (%s)", cred_path);
513             fclose(fp);
514             return -1;
515         }
516         fclose(fp);
517     }
518
519     OIC_LOG(DEBUG, TAG, "OCServer is starting...");
520
521     // Initialize Persistent Storage for SVR database
522     OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
523
524     OCRegisterPersistentStorageHandler(&ps);
525
526     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
527     {
528         OIC_LOG(ERROR, TAG, "OCStack init error");
529         return 0;
530     }
531
532     /*
533      * Declare and create the example resource: LED
534      */
535     createLEDResource(gResourceUri, &LED, false, 0);
536
537     timeout.tv_sec  = 0;
538     timeout.tv_nsec = 100000000L;
539
540     // Break from loop with Ctrl-C
541     OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
542     signal(SIGINT, handleSigInt);
543     while (!gQuitFlag)
544     {
545         if (OCProcess() != OC_STACK_OK)
546         {
547             OIC_LOG(ERROR, TAG, "OCStack process error");
548             return 0;
549         }
550         nanosleep(&timeout, NULL);
551     }
552
553     OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
554
555     if (OCStop() != OC_STACK_OK)
556     {
557         OIC_LOG(ERROR, TAG, "OCStack process error");
558     }
559
560     return 0;
561 }
562
563 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
564 {
565     if (!uri)
566     {
567         OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
568         return -1;
569     }
570
571     ledResource->state = resourceState;
572     ledResource->power= resourcePower;
573     OCStackResult res = OCCreateResource(&(ledResource->handle),
574             "core.led",
575             OC_RSRVD_INTERFACE_DEFAULT,
576             uri,
577             OCEntityHandlerCb,
578             NULL,
579             OC_DISCOVERABLE|OC_OBSERVABLE | OC_SECURE);
580     OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
581
582     return 0;
583 }