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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
29 #include "ocserverbasicops.h"
30 #include "ocpayload.h"
32 //string length of "/a/led/" + std::numeric_limits<int>::digits10 + '\0'"
34 const int URI_MAXSIZE = 17;
36 volatile sig_atomic_t gQuitFlag = 0;
38 static LEDResource LED;
39 // This variable determines instance number of the LED resource.
40 // Used by POST method to create a new instance of LED resource.
41 static int gCurrLedInstance = 0;
42 #define SAMPLE_MAX_NUM_POST_INSTANCE 2
43 static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
45 char *gResourceUri= (char *)"/a/led";
47 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
49 OCRepPayload* payload = OCRepPayloadCreate();
52 OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
56 OCRepPayloadSetUri(payload, uri);
57 OCRepPayloadSetPropBool(payload, "state", state);
58 OCRepPayloadSetPropInt(payload, "power", power);
63 //This function takes the request as an input and returns the response
64 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
66 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
68 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
72 OCRepPayload* input = reinterpret_cast<OCRepPayload*>(ehRequest->payload);
74 LEDResource *currLEDResource = &LED;
76 if (ehRequest->resource == gLedInstance[0].handle)
78 currLEDResource = &gLedInstance[0];
79 gResourceUri = (char *) "/a/led/0";
81 else if (ehRequest->resource == gLedInstance[1].handle)
83 currLEDResource = &gLedInstance[1];
84 gResourceUri = (char *) "/a/led/1";
87 if(OC_REST_PUT == ehRequest->method)
89 // Get pointer to query
91 if(OCRepPayloadGetPropInt(input, "power", &pow))
93 currLEDResource->power =pow;
97 if(OCRepPayloadGetPropBool(input, "state", &state))
99 currLEDResource->state = state;
103 return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
106 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
107 OCRepPayload **payload)
109 OCEntityHandlerResult ehResult;
110 OCRepPayload *getResp = constructResponse(ehRequest);
119 ehResult = OC_EH_ERROR;
125 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
126 OCRepPayload **payload)
128 OCEntityHandlerResult ehResult;
129 OCRepPayload *putResp = constructResponse(ehRequest);
138 ehResult = OC_EH_ERROR;
144 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
145 OCEntityHandlerResponse *response, OCRepPayload **payload)
147 OCRepPayload *respPLPost_led = nullptr;
148 OCEntityHandlerResult ehResult = OC_EH_OK;
151 * The entity handler determines how to process a POST request.
152 * Per the REST paradigm, POST can also be used to update representation of existing
153 * resource or create a new resource.
154 * In the sample below, if the POST is for /a/led then a new instance of the LED
155 * resource is created with default representation (if representation is included in
156 * POST payload it can be used as initial values) as long as the instance is
157 * lesser than max new instance count. Once max instance count is reached, POST on
158 * /a/led updated the representation of /a/led (just like PUT)
161 if (ehRequest->resource == LED.handle)
163 if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
165 // Create new LED instance
166 char newLedUri[URI_MAXSIZE ];
167 snprintf(newLedUri, URI_MAXSIZE, "/a/led/%d", gCurrLedInstance);
169 respPLPost_led = OCRepPayloadCreate();
170 OCRepPayloadSetUri(respPLPost_led, gResourceUri);
171 OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
173 if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
175 OC_LOG (INFO, TAG, "Created new LED instance");
176 gLedInstance[gCurrLedInstance].state = 0;
177 gLedInstance[gCurrLedInstance].power = 0;
179 strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
180 ehResult = OC_EH_RESOURCE_CREATED;
185 respPLPost_led = constructResponse(ehRequest);
190 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
192 if (ehRequest->resource == gLedInstance[i].handle)
196 respPLPost_led = constructResponse(ehRequest);
201 respPLPost_led = constructResponse(ehRequest);
207 if ((respPLPost_led != NULL))
209 *payload = respPLPost_led;
214 OC_LOG_V (INFO, TAG, "Payload was NULL");
215 ehResult = OC_EH_ERROR;
221 OCEntityHandlerResult
222 OCEntityHandlerCb (OCEntityHandlerFlag flag,
223 OCEntityHandlerRequest *entityHandlerRequest,void* callbackParam)
225 OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
227 OCEntityHandlerResult ehResult = OC_EH_ERROR;
228 OCEntityHandlerResponse response;
231 if (!entityHandlerRequest)
233 OC_LOG (ERROR, TAG, "Invalid request pointer");
237 OCRepPayload* payload = nullptr;
239 if (flag & OC_REQUEST_FLAG)
241 OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
242 if (entityHandlerRequest)
244 if (OC_REST_GET == entityHandlerRequest->method)
246 OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
247 ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
249 else if (OC_REST_PUT == entityHandlerRequest->method)
251 OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
252 ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
254 else if (OC_REST_POST == entityHandlerRequest->method)
256 OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
257 ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
261 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
262 entityHandlerRequest->method);
265 if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
267 // Format the response. Note this requires some info about the request
268 response.requestHandle = entityHandlerRequest->requestHandle;
269 response.resourceHandle = entityHandlerRequest->resource;
270 response.ehResult = ehResult;
271 response.payload = reinterpret_cast<OCPayload*>(payload);
272 response.numSendVendorSpecificHeaderOptions = 0;
273 memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
274 memset(response.resourceUri, 0, sizeof(response.resourceUri));
275 // Indicate that response is NOT in a persistent buffer
276 response.persistentBufferFlag = 0;
279 if (OCDoResponse(&response) != OC_STACK_OK)
281 OC_LOG(ERROR, TAG, "Error sending response");
282 ehResult = OC_EH_ERROR;
288 OCPayloadDestroy(response.payload);
292 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
293 void handleSigInt(int signum)
295 if (signum == SIGINT)
301 int main(int argc, char* argv[])
303 OC_LOG(DEBUG, TAG, "OCServer is starting...");
304 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
306 OC_LOG(ERROR, TAG, "OCStack init error");
311 * Declare and create the example resource: LED
313 createLEDResource(gResourceUri, &LED, false, 0);
315 // Break from loop with Ctrl-C
316 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
317 signal(SIGINT, handleSigInt);
320 if (OCProcess() != OC_STACK_OK)
322 OC_LOG(ERROR, TAG, "OCStack process error");
329 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
331 if (OCStop() != OC_STACK_OK)
333 OC_LOG(ERROR, TAG, "OCStack process error");
339 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
343 OC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
347 ledResource->state = resourceState;
348 ledResource->power= resourcePower;
349 OCStackResult res = OCCreateResource(&(ledResource->handle),
351 OC_RSRVD_INTERFACE_DEFAULT,
355 OC_DISCOVERABLE|OC_OBSERVABLE);
356 OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));