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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 #include "oic_malloc.h"
33 #include "ocserverslow.h"
34 #include "ocpayload.h"
36 volatile sig_atomic_t gQuitFlag = 0;
38 static std::list<OCEntityHandlerRequest *> gRequestList;
39 static constexpr unsigned int SLOW_RESPONSE_DELAY_SEC = 5;
41 static LEDResource LED;
43 static constexpr unsigned int SAMPLE_MAX_NUM_POST_INSTANCE = 2;
44 static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
46 //char *gResourceUri= const_cast<char *>("/a/led");
47 char *gResourceUri= (char *)"/a/led";
49 //This function takes the request as an input and returns the response
51 OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
53 LEDResource *currLEDResource = &LED;
55 OC_LOG(INFO, TAG, "Entering constructResponse");
57 if (ehRequest->resource == gLedInstance[0].handle)
59 OC_LOG(INFO, TAG, "handle 0");
60 currLEDResource = &gLedInstance[0];
61 gResourceUri = const_cast<char *>("a/led/0");
63 else if (ehRequest->resource == gLedInstance[1].handle)
65 OC_LOG(INFO, TAG, "handle 1");
66 currLEDResource = &gLedInstance[1];
67 gResourceUri = const_cast<char *>("a/led/1");
70 if(OC_REST_PUT == ehRequest->method)
72 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
74 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
78 OCRepPayload *putPayload = reinterpret_cast<OCRepPayload*> (ehRequest->payload);
83 if (OCRepPayloadGetPropBool(putPayload, "state", &state))
85 currLEDResource->state = state;
87 if (OCRepPayloadGetPropInt (putPayload, "power", &power))
89 currLEDResource->power = power;
93 OCRepPayload *response = OCRepPayloadCreate();
97 OC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed.");
100 OCRepPayloadSetUri (response, gResourceUri);
101 OCRepPayloadSetPropBool(response, "state", currLEDResource->state);
102 OCRepPayloadSetPropInt(response, "power", currLEDResource->power);
107 void ProcessGetPutRequest (OCEntityHandlerRequest *ehRequest)
109 OC_LOG(INFO, TAG, "Entering ProcessGetPutRequest");
111 OCRepPayload *getResp = constructResponse(ehRequest);
115 OC_LOG(ERROR, TAG, "Failed to construct response");
119 OCEntityHandlerResponse response;
121 // Format the response. Note this requires some info about the request
122 response.requestHandle = ehRequest->requestHandle;
123 response.resourceHandle = ehRequest->resource;
124 response.ehResult = OC_EH_OK;
125 response.payload = reinterpret_cast<OCPayload*> (getResp);
126 response.numSendVendorSpecificHeaderOptions = 0;
127 memset(response.sendVendorSpecificHeaderOptions,
128 0, sizeof response.sendVendorSpecificHeaderOptions);
129 memset(response.resourceUri, 0, sizeof(response.resourceUri));
130 // Indicate that response is NOT in a persistent buffer
131 response.persistentBufferFlag = 0;
134 if (OCDoResponse(&response) != OC_STACK_OK)
136 OC_LOG(ERROR, TAG, "Error sending response");
142 OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandlerRequest)
144 OC_LOG(INFO, TAG, "Copying received request for slow response");
146 OCEntityHandlerRequest *copyOfRequest =
147 (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
152 memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
155 if (copyOfRequest->query)
157 // Do deep copy of query
158 copyOfRequest->query = (char *) OICMalloc(
159 strlen((const char *)entityHandlerRequest->query) + 1);
161 strcpy((char *)copyOfRequest->query, (const char *)entityHandlerRequest->query);
164 if (entityHandlerRequest->payload)
166 copyOfRequest->payload = reinterpret_cast<OCPayload*>
167 (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
170 // Ignore vendor specific header options for example
171 copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
172 copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
177 OC_LOG(INFO, TAG, "Copied client request");
181 OC_LOG(ERROR, TAG, "Error copying client request");
183 return copyOfRequest;
186 OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag,
187 OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam)
189 OCEntityHandlerResult result = OC_EH_ERROR;
190 OCEntityHandlerRequest *request = NULL;
192 OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
194 if (flag & OC_REQUEST_FLAG)
196 OC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
197 if (entityHandlerRequest)
199 OC_LOG_V (INFO, TAG, "request query %s from client",
200 entityHandlerRequest->query);
201 OC_LOG_PAYLOAD (INFO, TAG, entityHandlerRequest->payload);
203 // Make deep copy of received request and queue it for slow processing
204 request = CopyRequest(entityHandlerRequest);
209 OC_LOG(INFO, TAG, "Scheduling slow response for received request");
210 gRequestList.push_back(request);
211 // Indicate to the stack that this is a slow response
213 // Start the slow response alarm
214 alarm(SLOW_RESPONSE_DELAY_SEC);
218 OC_LOG(ERROR, TAG, "Error queuing request for slow response");
219 // Indicate to the stack that this is a slow response
220 result = OC_EH_ERROR;
225 OC_LOG(ERROR, TAG, "Invalid request");
226 result = OC_EH_ERROR;
232 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
233 void handleSigInt(int signum)
235 if (signum == SIGINT)
241 // SIGINT alarm handler: alarm set by entity handler. Does
242 // slow response when fired
243 void AlarmHandler(int sig)
247 OC_LOG (INFO, TAG, "Server starting slow response");
248 if (gRequestList.empty())
250 OC_LOG (INFO, TAG, "No requests to service");
254 // Get the request from the list
255 OCEntityHandlerRequest *entityHandlerRequest = gRequestList.front();
256 gRequestList.pop_front();
257 if (entityHandlerRequest->method == OC_REST_GET)
259 OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
260 ProcessGetPutRequest (entityHandlerRequest);
262 else if (entityHandlerRequest->method == OC_REST_PUT)
264 OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
265 ProcessGetPutRequest (entityHandlerRequest);
269 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
270 entityHandlerRequest->method);
273 OICFree(entityHandlerRequest->query);
274 OCPayloadDestroy(entityHandlerRequest->payload);
275 OICFree(entityHandlerRequest);
277 // If there are more requests in list, re-arm the alarm signal
278 if (gRequestList.empty())
280 alarm(SLOW_RESPONSE_DELAY_SEC);
285 int main(int argc, char* argv[])
287 OC_LOG(DEBUG, TAG, "OCServer is starting...");
289 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
291 OC_LOG(ERROR, TAG, "OCStack init error");
295 // Declare and create the example resource: LED
296 createLEDResource(gResourceUri, &LED, false, 42);
298 // Initialize slow response alarm
299 signal(SIGALRM, AlarmHandler);
301 // Break from loop with Ctrl-C
302 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
303 signal(SIGINT, handleSigInt);
307 if (OCProcess() != OC_STACK_OK)
309 OC_LOG(ERROR, TAG, "OCStack process error");
315 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
318 if (!gRequestList.empty())
320 for (auto iter = gRequestList.begin(); iter != gRequestList.end(); ++iter)
322 OICFree((*iter)->query);
323 OCPayloadDestroy((*iter)->payload);
326 gRequestList.clear();
329 if (OCStop() != OC_STACK_OK)
331 OC_LOG(ERROR, TAG, "OCStack process error");
337 int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, int resourcePower)
341 OC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
345 ledResource->state = resourceState;
346 ledResource->power= resourcePower;
347 OCStackResult res = OCCreateResource(&(ledResource->handle),
349 OC_RSRVD_INTERFACE_DEFAULT,
353 OC_DISCOVERABLE|OC_OBSERVABLE);
354 OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));