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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
36 #include "ocserverbasicops.h"
37 #include "ocpayload.h"
39 //string length of "/a/led/" + std::numeric_limits<int>::digits10 + '\0'"
41 const int URI_MAXSIZE = 17;
43 volatile sig_atomic_t gQuitFlag = 0;
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 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
56 OCRepPayload* payload = OCRepPayloadCreate();
59 OIC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
63 OCRepPayloadSetUri(payload, uri);
64 OCRepPayloadSetPropBool(payload, "state", state);
65 OCRepPayloadSetPropInt(payload, "power", power);
70 //This function takes the request as an input and returns the response
71 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
73 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
75 OIC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
79 OCRepPayload* input = reinterpret_cast<OCRepPayload*>(ehRequest->payload);
81 LEDResource *currLEDResource = &LED;
83 if (ehRequest->resource == gLedInstance[0].handle)
85 currLEDResource = &gLedInstance[0];
86 gResourceUri = (char *) "/a/led/0";
88 else if (ehRequest->resource == gLedInstance[1].handle)
90 currLEDResource = &gLedInstance[1];
91 gResourceUri = (char *) "/a/led/1";
94 if(OC_REST_PUT == ehRequest->method)
96 // Get pointer to query
98 if(OCRepPayloadGetPropInt(input, "power", &pow))
100 currLEDResource->power =pow;
104 if(OCRepPayloadGetPropBool(input, "state", &state))
106 currLEDResource->state = state;
110 return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
113 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
114 OCRepPayload **payload)
116 OCEntityHandlerResult ehResult;
117 OCRepPayload *getResp = constructResponse(ehRequest);
126 ehResult = OC_EH_ERROR;
132 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
133 OCRepPayload **payload)
135 OCEntityHandlerResult ehResult;
136 OCRepPayload *putResp = constructResponse(ehRequest);
145 ehResult = OC_EH_ERROR;
151 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
152 OCEntityHandlerResponse *response, OCRepPayload **payload)
154 OCRepPayload *respPLPost_led = nullptr;
155 OCEntityHandlerResult ehResult = OC_EH_OK;
158 * The entity handler determines how to process a POST request.
159 * Per the REST paradigm, POST can also be used to update representation of existing
160 * resource or create a new resource.
161 * In the sample below, if the POST is for /a/led then a new instance of the LED
162 * resource is created with default representation (if representation is included in
163 * POST payload it can be used as initial values) as long as the instance is
164 * lesser than max new instance count. Once max instance count is reached, POST on
165 * /a/led updated the representation of /a/led (just like PUT)
168 if (ehRequest->resource == LED.handle)
170 if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
172 // Create new LED instance
173 char newLedUri[URI_MAXSIZE ];
174 snprintf(newLedUri, URI_MAXSIZE, "/a/led/%d", gCurrLedInstance);
176 respPLPost_led = OCRepPayloadCreate();
177 OCRepPayloadSetUri(respPLPost_led, gResourceUri);
178 OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
180 if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
182 OIC_LOG (INFO, TAG, "Created new LED instance");
183 gLedInstance[gCurrLedInstance].state = 0;
184 gLedInstance[gCurrLedInstance].power = 0;
186 strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
187 ehResult = OC_EH_RESOURCE_CREATED;
192 respPLPost_led = constructResponse(ehRequest);
197 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
199 if (ehRequest->resource == gLedInstance[i].handle)
203 respPLPost_led = constructResponse(ehRequest);
208 respPLPost_led = constructResponse(ehRequest);
214 if ((respPLPost_led != NULL))
216 *payload = respPLPost_led;
221 OIC_LOG_V (INFO, TAG, "Payload was NULL");
222 ehResult = OC_EH_ERROR;
228 OCEntityHandlerResult
229 OCEntityHandlerCb (OCEntityHandlerFlag flag,
230 OCEntityHandlerRequest *entityHandlerRequest,
231 void* /*callbackParam*/)
233 OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
235 OCEntityHandlerResult ehResult = OC_EH_ERROR;
236 OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false };
239 if (!entityHandlerRequest)
241 OIC_LOG (ERROR, TAG, "Invalid request pointer");
245 OCRepPayload* payload = nullptr;
247 if (flag & OC_REQUEST_FLAG)
249 OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
250 if (entityHandlerRequest)
252 if (OC_REST_GET == entityHandlerRequest->method)
254 OIC_LOG (INFO, TAG, "Received OC_REST_GET from client");
255 ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
257 else if (OC_REST_PUT == entityHandlerRequest->method)
259 OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
260 ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
262 else if (OC_REST_POST == entityHandlerRequest->method)
264 OIC_LOG (INFO, TAG, "Received OC_REST_POST from client");
265 ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
269 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
270 entityHandlerRequest->method);
273 if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
275 // Format the response. Note this requires some info about the request
276 response.requestHandle = entityHandlerRequest->requestHandle;
277 response.resourceHandle = entityHandlerRequest->resource;
278 response.ehResult = ehResult;
279 response.payload = reinterpret_cast<OCPayload*>(payload);
280 response.numSendVendorSpecificHeaderOptions = 0;
281 memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
282 memset(response.resourceUri, 0, sizeof(response.resourceUri));
283 // Indicate that response is NOT in a persistent buffer
284 response.persistentBufferFlag = 0;
287 if (OCDoResponse(&response) != OC_STACK_OK)
289 OIC_LOG(ERROR, TAG, "Error sending response");
290 ehResult = OC_EH_ERROR;
296 OCPayloadDestroy(response.payload);
300 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
301 void handleSigInt(int signum)
303 if (signum == SIGINT)
309 int main(int /*argc*/, char* /*argv*/[])
311 OIC_LOG(DEBUG, TAG, "OCServer is starting...");
312 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
314 OIC_LOG(ERROR, TAG, "OCStack init error");
319 * Declare and create the example resource: LED
321 createLEDResource(gResourceUri, &LED, false, 0);
323 // Break from loop with Ctrl-C
324 OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
325 signal(SIGINT, handleSigInt);
328 if (OCProcess() != OC_STACK_OK)
330 OIC_LOG(ERROR, TAG, "OCStack process error");
337 OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
339 if (OCStop() != OC_STACK_OK)
341 OIC_LOG(ERROR, TAG, "OCStack process error");
347 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
351 OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
355 ledResource->state = resourceState;
356 ledResource->power= resourcePower;
357 OCStackResult res = OCCreateResource(&(ledResource->handle),
359 OC_RSRVD_INTERFACE_DEFAULT,
363 OC_DISCOVERABLE|OC_OBSERVABLE);
364 OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));