1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "rd_server.h"
22 #include "rd_database.h"
24 #include "payload_logging.h"
25 #include "ocpayload.h"
28 #define TAG PCF("RDServer")
32 // This is temporary hardcoded value for bias factor.
33 static const int OC_RD_DISC_SEL = 100;
35 static OCResourceHandle rdHandle;
37 static OCStackResult sendResponse(const OCEntityHandlerRequest *ehRequest, OCRepPayload *rdPayload,
38 OCEntityHandlerResult ehResult)
40 OCEntityHandlerResponse response = { 0 };
41 response.requestHandle = ehRequest->requestHandle;
42 response.resourceHandle = ehRequest->resource;
43 response.ehResult = ehResult;
44 response.payload = (OCPayload*)(rdPayload);
45 return OCDoResponse(&response);
49 * This internal method handles RD discovery request.
50 * Responds with the RD discovery payload message.
52 static OCEntityHandlerResult handleGetRequest(const OCEntityHandlerRequest *ehRequest)
56 OIC_LOG(DEBUG, TAG, "Invalid request pointer.");
60 OCEntityHandlerResult ehResult = OC_EH_OK;
61 OIC_LOG_V(DEBUG, TAG, "Received OC_REST_GET from client with query: %s.", ehRequest->query);
63 OCRepPayload *rdPayload = (OCRepPayload *)OCRepPayloadCreate();
66 return OC_STACK_NO_MEMORY;
69 const char *id = OCGetServerInstanceIDString();
72 OCRepPayloadSetPropString(rdPayload, OC_RSRVD_DEVICE_ID, id);
74 OCRepPayloadSetPropInt(rdPayload, OC_RSRVD_RD_DISCOVERY_SEL, OC_RD_DISC_SEL);
76 OCRepPayloadAddResourceType(rdPayload, OC_RSRVD_RESOURCE_TYPE_RD);
77 OCRepPayloadAddResourceType(rdPayload, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
79 OCRepPayloadAddInterface(rdPayload, OC_RSRVD_INTERFACE_DEFAULT);
81 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
83 if (sendResponse(ehRequest, rdPayload, OC_EH_OK) != OC_STACK_OK)
85 OIC_LOG(ERROR, TAG, "Sending response failed.");
86 ehResult = OC_EH_ERROR;
93 * This internal method handles RD publish request.
94 * Responds with the RD success message.
96 static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *ehRequest)
98 OCEntityHandlerResult ehResult = OC_EH_OK;
102 OIC_LOG(DEBUG, TAG, "Invalid request pointer");
106 OIC_LOG_V(DEBUG, TAG, "Received OC_REST_POST from client with query: %s.", ehRequest->query);
108 OCRepPayload *payload = (OCRepPayload *)ehRequest->payload;
109 OCRepPayload *resPayload = NULL;
112 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) payload);
113 if (OCRDDatabaseInit(NULL) == OC_STACK_OK)
115 if (OCRDDatabaseStoreResources(payload, &ehRequest->devAddr) == OC_STACK_OK)
117 OIC_LOG_V(DEBUG, TAG, "Stored resources.");
118 resPayload = payload;
123 resPayload = (OCRepPayload *)OCRepPayloadCreate();
124 ehResult = OC_EH_ERROR;
129 if (sendResponse(ehRequest, resPayload, ehResult) != OC_STACK_OK)
131 OIC_LOG(ERROR, TAG, "Sending response failed.");
138 static OCEntityHandlerResult handleDeleteRequest(const OCEntityHandlerRequest *ehRequest)
140 OIC_LOG(DEBUG, TAG, "handleDeleteRequest - IN");
142 (void) ehRequest; // eliminates release warning
144 OCEntityHandlerResult ehResult = OC_EH_OK;
149 * This internal method is the entity handler for RD resources and
150 * will handle REST request (GET/PUT/POST/DEL) for them.
152 static OCEntityHandlerResult rdEntityHandler(OCEntityHandlerFlag flag,
153 OCEntityHandlerRequest *ehRequest, OC_ANNOTATE_UNUSED void *callbackParameter)
155 OCEntityHandlerResult ehRet = OC_EH_ERROR;
162 if (flag & OC_REQUEST_FLAG)
164 OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG.");
165 switch (ehRequest->method)
168 case OC_REST_DISCOVER:
169 ehRet = handleGetRequest(ehRequest);
172 ehRet = handlePublishRequest(ehRequest);
175 ehRet = handleDeleteRequest(ehRequest);
178 case OC_REST_OBSERVE:
179 case OC_REST_OBSERVE_ALL:
181 case OC_REST_PRESENCE:
183 case OC_REST_NOMETHOD:
192 * Registers RD resource
194 OCStackResult OCRDStart()
196 OCStackResult result = OCCreateResource(&rdHandle,
197 OC_RSRVD_RESOURCE_TYPE_RD,
198 OC_RSRVD_INTERFACE_DEFAULT,
202 (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE));
204 if (result == OC_STACK_OK)
206 OIC_LOG(DEBUG, TAG, "Resource Directory resource created.");
210 OIC_LOG(ERROR, TAG, "Failed creating Resource Directory resource.");
213 result = OCBindResourceTypeToResource(rdHandle,
214 OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
215 if (result == OC_STACK_OK)
217 OIC_LOG(DEBUG, TAG, "Resource Directory resource Publish created.");
221 OIC_LOG(ERROR, TAG, "Failed creating Resource Directory Publish resource.");
228 * Stops resource directory server
230 OCStackResult OCRDStop()
234 OIC_LOG(ERROR, TAG, "Resource Directory resource handle is not initialized.");
235 return OC_STACK_NO_RESOURCE;
238 OCStackResult result = OCDeleteResource(rdHandle);
240 if (result == OC_STACK_OK)
242 OIC_LOG(DEBUG, TAG, "Resource Directory resource deleted.");
246 OIC_LOG(ERROR, TAG, "Resource Directory resource not deleted.");