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 ///////////////////////////////////////////////////////////////////////
23 #include "iotivity_config.h"
35 #include "ocpayload.h"
36 #include "pinoxmcommon.h"
40 /** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */
44 #endif //HAVE_WINDOWS_H
45 #include "platform_features.h"
49 #define TAG "SAMPLE_PRECONF_PIN"
53 /* Structure to represent a LED resource */
54 typedef struct LEDRESOURCE{
55 OCResourceHandle handle;
60 static LEDResource LED;
61 // This variable determines instance number of the LED resource.
62 // Used by POST method to create a new instance of LED resource.
63 static int gCurrLedInstance = 0;
64 #define SAMPLE_MAX_NUM_POST_INSTANCE 2
65 static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
67 char *gResourceUri= (char *)"/a/led";
69 //Secure Virtual Resource database for Iotivity Server
70 //It contains Server's Identity and the PSK credentials
71 //of other devices which the server trusts
72 static char CRED_FILE[] = "oic_svr_db_server_preconfpin.dat";
74 /* Function that creates a new LED resource by calling the
75 * OCCreateResource() method.
77 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower);
79 /* This method converts the payload to JSON format */
80 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest);
82 /* Following methods process the PUT, GET, POST
85 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
86 OCRepPayload **payload);
87 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
88 OCRepPayload **payload);
89 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
90 OCEntityHandlerResponse *response,
91 OCRepPayload **payload);
93 /* Entity Handler callback functions */
95 OCEntityHandlerCb (OCEntityHandlerFlag flag,
96 OCEntityHandlerRequest *entityHandlerRequest,
99 const char *getResult(OCStackResult result) {
102 return "OC_STACK_OK";
103 case OC_STACK_RESOURCE_CREATED:
104 return "OC_STACK_RESOURCE_CREATED";
105 case OC_STACK_RESOURCE_DELETED:
106 return "OC_STACK_RESOURCE_DELETED";
107 case OC_STACK_INVALID_URI:
108 return "OC_STACK_INVALID_URI";
109 case OC_STACK_INVALID_QUERY:
110 return "OC_STACK_INVALID_QUERY";
111 case OC_STACK_INVALID_IP:
112 return "OC_STACK_INVALID_IP";
113 case OC_STACK_INVALID_PORT:
114 return "OC_STACK_INVALID_PORT";
115 case OC_STACK_INVALID_CALLBACK:
116 return "OC_STACK_INVALID_CALLBACK";
117 case OC_STACK_INVALID_METHOD:
118 return "OC_STACK_INVALID_METHOD";
119 case OC_STACK_NO_MEMORY:
120 return "OC_STACK_NO_MEMORY";
121 case OC_STACK_COMM_ERROR:
122 return "OC_STACK_COMM_ERROR";
123 case OC_STACK_INVALID_PARAM:
124 return "OC_STACK_INVALID_PARAM";
125 case OC_STACK_NOTIMPL:
126 return "OC_STACK_NOTIMPL";
127 case OC_STACK_NO_RESOURCE:
128 return "OC_STACK_NO_RESOURCE";
129 case OC_STACK_RESOURCE_ERROR:
130 return "OC_STACK_RESOURCE_ERROR";
131 case OC_STACK_SLOW_RESOURCE:
132 return "OC_STACK_SLOW_RESOURCE";
133 case OC_STACK_NO_OBSERVERS:
134 return "OC_STACK_NO_OBSERVERS";
136 case OC_STACK_PRESENCE_STOPPED:
137 return "OC_STACK_PRESENCE_STOPPED";
140 return "OC_STACK_ERROR";
146 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
148 OCRepPayload* payload = OCRepPayloadCreate();
151 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
155 OCRepPayloadSetUri(payload, uri);
156 OCRepPayloadSetPropBool(payload, "state", state);
157 OCRepPayloadSetPropInt(payload, "power", power);
162 //This function takes the request as an input and returns the response
163 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
165 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
167 OIC_LOG(ERROR, TAG, "Incoming payload not a representation");
171 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
173 LEDResource *currLEDResource = &LED;
175 if (ehRequest->resource == gLedInstance[0].handle)
177 currLEDResource = &gLedInstance[0];
178 gResourceUri = (char *) "/a/led/0";
180 else if (ehRequest->resource == gLedInstance[1].handle)
182 currLEDResource = &gLedInstance[1];
183 gResourceUri = (char *) "/a/led/1";
186 if(OC_REST_PUT == ehRequest->method)
188 // Get pointer to query
190 if(OCRepPayloadGetPropInt(input, "power", &pow))
192 currLEDResource->power =pow;
196 if(OCRepPayloadGetPropBool(input, "state", &state))
198 currLEDResource->state = state;
202 return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
205 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
206 OCRepPayload **payload)
208 OCEntityHandlerResult ehResult;
210 OCRepPayload *getResp = constructResponse(ehRequest);
219 ehResult = OC_EH_ERROR;
225 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
226 OCRepPayload **payload)
228 OCEntityHandlerResult ehResult;
230 OCRepPayload *putResp = constructResponse(ehRequest);
239 ehResult = OC_EH_ERROR;
245 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
246 OCEntityHandlerResponse *response, OCRepPayload **payload)
248 OCRepPayload *respPLPost_led = NULL;
249 OCEntityHandlerResult ehResult = OC_EH_OK;
252 * The entity handler determines how to process a POST request.
253 * Per the REST paradigm, POST can also be used to update representation of existing
254 * resource or create a new resource.
255 * In the sample below, if the POST is for /a/led then a new instance of the LED
256 * resource is created with default representation (if representation is included in
257 * POST payload it can be used as initial values) as long as the instance is
258 * lesser than max new instance count. Once max instance count is reached, POST on
259 * /a/led updated the representation of /a/led (just like PUT)
262 if (ehRequest->resource == LED.handle)
264 if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
266 // Create new LED instance
267 char newLedUri[15] = "/a/led/";
268 int newLedUriLength = strlen(newLedUri);
269 snprintf (newLedUri + newLedUriLength, sizeof(newLedUri)-newLedUriLength, "%d", gCurrLedInstance);
271 respPLPost_led = OCRepPayloadCreate();
272 OCRepPayloadSetUri(respPLPost_led, gResourceUri);
273 OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
275 if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
277 OIC_LOG (INFO, TAG, "Created new LED instance");
278 gLedInstance[gCurrLedInstance].state = 0;
279 gLedInstance[gCurrLedInstance].power = 0;
281 strncpy ((char *)response->resourceUri, newLedUri, sizeof(response->resourceUri));
282 ehResult = OC_EH_RESOURCE_CREATED;
287 respPLPost_led = constructResponse(ehRequest);
292 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
294 if (ehRequest->resource == gLedInstance[i].handle)
298 respPLPost_led = constructResponse(ehRequest);
303 respPLPost_led = constructResponse(ehRequest);
309 if (respPLPost_led != NULL)
311 *payload = respPLPost_led;
316 OIC_LOG_V (INFO, TAG, "Payload was NULL");
317 ehResult = OC_EH_ERROR;
323 OCEntityHandlerResult
324 OCEntityHandlerCb (OCEntityHandlerFlag flag,
325 OCEntityHandlerRequest *entityHandlerRequest,
328 OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
330 OCEntityHandlerResult ehResult = OC_EH_ERROR;
332 OCEntityHandlerResponse response;
333 memset(&response, 0, sizeof(response));
336 if (!entityHandlerRequest)
338 OIC_LOG (ERROR, TAG, "Invalid request pointer");
342 OCRepPayload* payload = NULL;
344 if (flag & OC_REQUEST_FLAG)
346 OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
347 if (entityHandlerRequest)
349 if (OC_REST_GET == entityHandlerRequest->method)
351 OIC_LOG (INFO, TAG, "Received OC_REST_GET from client");
352 ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
354 else if (OC_REST_PUT == entityHandlerRequest->method)
356 OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
357 ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
359 else if (OC_REST_POST == entityHandlerRequest->method)
361 OIC_LOG (INFO, TAG, "Received OC_REST_POST from client");
362 ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
366 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
367 entityHandlerRequest->method);
368 ehResult = OC_EH_ERROR;
371 if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
373 // Format the response. Note this requires some info about the request
374 response.requestHandle = entityHandlerRequest->requestHandle;
375 response.resourceHandle = entityHandlerRequest->resource;
376 response.ehResult = ehResult;
377 response.payload = (OCPayload*)(payload);
378 response.numSendVendorSpecificHeaderOptions = 0;
379 memset(response.sendVendorSpecificHeaderOptions, 0,
380 sizeof(response.sendVendorSpecificHeaderOptions));
381 memset(response.resourceUri, 0, sizeof(response.resourceUri));
382 // Indicate that response is NOT in a persistent buffer
383 response.persistentBufferFlag = 0;
386 if (OCDoResponse(&response) != OC_STACK_OK)
388 OIC_LOG(ERROR, TAG, "Error sending response");
389 ehResult = OC_EH_ERROR;
395 OCPayloadDestroy(response.payload);
399 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
400 void handleSigInt(int signum)
402 if (signum == SIGINT)
408 FILE* server_fopen(const char *path, const char *mode)
411 return fopen(CRED_FILE, mode);
416 struct timespec timeout;
418 OIC_LOG(DEBUG, TAG, "OCServer is starting...");
420 // Initialize Persistent Storage for SVR database
421 OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
423 OCRegisterPersistentStorageHandler(&ps);
425 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
427 OIC_LOG(ERROR, TAG, "OCStack init error");
432 * Declare and create the example resource: LED
434 createLEDResource(gResourceUri, &LED, false, 0);
437 timeout.tv_nsec = 100000000L;
439 // Break from loop with Ctrl-C
440 OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
441 signal(SIGINT, handleSigInt);
444 if (OCProcess() != OC_STACK_OK)
446 OIC_LOG(ERROR, TAG, "OCStack process error");
449 nanosleep(&timeout, NULL);
452 OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
454 if (OCStop() != OC_STACK_OK)
456 OIC_LOG(ERROR, TAG, "OCStack process error");
462 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
466 OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
470 ledResource->state = resourceState;
471 ledResource->power= resourcePower;
472 OCStackResult res = OCCreateResource(&(ledResource->handle),
474 OC_RSRVD_INTERFACE_DEFAULT,
478 OC_DISCOVERABLE|OC_OBSERVABLE | OC_SECURE);
479 OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));