1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "iotivity_config.h"
37 #include "ocserverbasicops.h"
38 #include "ocpayload.h"
40 //string length of "/a/led/" + std::numeric_limits<int>::digits10 + '\0'"
42 const int URI_MAXSIZE = 17;
44 volatile sig_atomic_t gQuitFlag = 0;
46 static LEDResource LED;
47 // This variable determines instance number of the LED resource.
48 // Used by POST method to create a new instance of LED resource.
49 static int gCurrLedInstance = 0;
50 #define SAMPLE_MAX_NUM_POST_INSTANCE 2
51 static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
53 char *gResourceUri= (char *)"/a/led";
55 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
57 OCRepPayload* payload = OCRepPayloadCreate();
60 OIC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
64 OCRepPayloadSetUri(payload, uri);
65 OCRepPayloadSetPropBool(payload, "state", state);
66 OCRepPayloadSetPropInt(payload, "power", power);
71 //This function takes the request as an input and returns the response
72 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
74 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
76 OIC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
80 OCRepPayload* input = reinterpret_cast<OCRepPayload*>(ehRequest->payload);
82 LEDResource *currLEDResource = &LED;
84 if (ehRequest->resource == gLedInstance[0].handle)
86 currLEDResource = &gLedInstance[0];
87 gResourceUri = (char *) "/a/led/0";
89 else if (ehRequest->resource == gLedInstance[1].handle)
91 currLEDResource = &gLedInstance[1];
92 gResourceUri = (char *) "/a/led/1";
95 if(OC_REST_PUT == ehRequest->method)
97 // Get pointer to query
99 if(OCRepPayloadGetPropInt(input, "power", &pow))
101 currLEDResource->power =pow;
105 if(OCRepPayloadGetPropBool(input, "state", &state))
107 currLEDResource->state = state;
111 return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
114 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
115 OCRepPayload **payload)
117 OCEntityHandlerResult ehResult;
118 OCRepPayload *getResp = constructResponse(ehRequest);
127 ehResult = OC_EH_ERROR;
133 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
134 OCRepPayload **payload)
136 OCEntityHandlerResult ehResult;
137 OCRepPayload *putResp = constructResponse(ehRequest);
146 ehResult = OC_EH_ERROR;
152 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
153 OCEntityHandlerResponse *response, OCRepPayload **payload)
155 OCRepPayload *respPLPost_led = nullptr;
156 OCEntityHandlerResult ehResult = OC_EH_OK;
159 * The entity handler determines how to process a POST request.
160 * Per the REST paradigm, POST can also be used to update representation of existing
161 * resource or create a new resource.
162 * In the sample below, if the POST is for /a/led then a new instance of the LED
163 * resource is created with default representation (if representation is included in
164 * POST payload it can be used as initial values) as long as the instance is
165 * lesser than max new instance count. Once max instance count is reached, POST on
166 * /a/led updated the representation of /a/led (just like PUT)
169 if (ehRequest->resource == LED.handle)
171 if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
173 // Create new LED instance
174 char newLedUri[URI_MAXSIZE ];
175 snprintf(newLedUri, URI_MAXSIZE, "/a/led/%d", gCurrLedInstance);
177 respPLPost_led = OCRepPayloadCreate();
178 OCRepPayloadSetUri(respPLPost_led, gResourceUri);
179 OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
181 if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
183 OIC_LOG (INFO, TAG, "Created new LED instance");
184 gLedInstance[gCurrLedInstance].state = 0;
185 gLedInstance[gCurrLedInstance].power = 0;
187 strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
188 ehResult = OC_EH_RESOURCE_CREATED;
193 respPLPost_led = constructResponse(ehRequest);
198 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
200 if (ehRequest->resource == gLedInstance[i].handle)
204 respPLPost_led = constructResponse(ehRequest);
209 respPLPost_led = constructResponse(ehRequest);
215 if ((respPLPost_led != NULL))
217 *payload = respPLPost_led;
222 OIC_LOG_V (INFO, TAG, "Payload was NULL");
223 ehResult = OC_EH_ERROR;
229 OCEntityHandlerResult
230 OCEntityHandlerCb (OCEntityHandlerFlag flag,
231 OCEntityHandlerRequest *entityHandlerRequest,
232 void* /*callbackParam*/)
234 OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
236 OCEntityHandlerResult ehResult = OC_EH_ERROR;
237 OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false };
240 if (!entityHandlerRequest)
242 OIC_LOG (ERROR, TAG, "Invalid request pointer");
246 OCRepPayload* payload = nullptr;
248 if (flag & OC_REQUEST_FLAG)
250 OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
251 if (entityHandlerRequest)
253 if (OC_REST_GET == entityHandlerRequest->method)
255 OIC_LOG (INFO, TAG, "Received OC_REST_GET from client");
256 ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
258 else if (OC_REST_PUT == entityHandlerRequest->method)
260 OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
261 ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
263 else if (OC_REST_POST == entityHandlerRequest->method)
265 OIC_LOG (INFO, TAG, "Received OC_REST_POST from client");
266 ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
270 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
271 entityHandlerRequest->method);
274 if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
276 // Format the response. Note this requires some info about the request
277 response.requestHandle = entityHandlerRequest->requestHandle;
278 response.resourceHandle = entityHandlerRequest->resource;
279 response.ehResult = ehResult;
280 response.payload = reinterpret_cast<OCPayload*>(payload);
281 response.numSendVendorSpecificHeaderOptions = 0;
282 memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
283 memset(response.resourceUri, 0, sizeof(response.resourceUri));
284 // Indicate that response is NOT in a persistent buffer
285 response.persistentBufferFlag = 0;
288 if (OCDoResponse(&response) != OC_STACK_OK)
290 OIC_LOG(ERROR, TAG, "Error sending response");
291 ehResult = OC_EH_ERROR;
297 OCPayloadDestroy(response.payload);
301 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
302 void handleSigInt(int signum)
304 if (signum == SIGINT)
310 int main(int /*argc*/, char* /*argv*/[])
312 OIC_LOG(DEBUG, TAG, "OCServer is starting...");
313 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
315 OIC_LOG(ERROR, TAG, "OCStack init error");
320 * Declare and create the example resource: LED
322 createLEDResource(gResourceUri, &LED, false, 0);
324 // Break from loop with Ctrl-C
325 OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
326 signal(SIGINT, handleSigInt);
329 if (OCProcess() != OC_STACK_OK)
331 OIC_LOG(ERROR, TAG, "OCStack process error");
338 OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
340 if (OCStop() != OC_STACK_OK)
342 OIC_LOG(ERROR, TAG, "OCStack process error");
348 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
352 OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
356 ledResource->state = resourceState;
357 ledResource->power= resourcePower;
358 OCStackResult res = OCCreateResource(&(ledResource->handle),
360 OC_RSRVD_INTERFACE_DEFAULT,
364 OC_DISCOVERABLE|OC_OBSERVABLE);
365 OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));