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_storage.h"
24 #include "rdpayload.h"
25 #include "payload_logging.h"
27 #define TAG PCF("RDServer")
29 // This is temporary hardcoded value for bias factor.
30 #define OC_RD_DISC_SEL 100
32 static OCStackResult sendResponse(const OCEntityHandlerRequest *ehRequest, OCRDPayload *rdPayload)
34 OCEntityHandlerResponse response = { 0 };
35 response.requestHandle = ehRequest->requestHandle;
36 response.resourceHandle = ehRequest->resource;
37 response.ehResult = OC_EH_OK;
38 response.payload = (OCPayload*)(rdPayload);
39 return OCDoResponse(&response);
43 * This internal method handles RD discovery request.
44 * Responds with the RD discovery payload message.
46 static OCEntityHandlerResult handleGetRequest(const OCEntityHandlerRequest *ehRequest)
50 OIC_LOG(DEBUG, TAG, "Invalid request pointer.");
54 OCEntityHandlerResult ehResult = OC_EH_OK;
55 OIC_LOG_V(DEBUG, TAG, "Received OC_REST_GET from client with query: %s.", ehRequest->query);
57 OCRDPayload *rdPayload = OCRDPayloadCreate();
60 return OC_STACK_NO_MEMORY;
63 rdPayload->rdDiscovery = OCRDDiscoveryPayloadCreate(NULL, OCGetServerInstanceIDString(), OC_RD_DISC_SEL);
64 if (!rdPayload->rdDiscovery)
66 OCRDPayloadDestroy(rdPayload);
67 return OC_STACK_NO_MEMORY;
70 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
72 if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK)
74 OIC_LOG(ERROR, TAG, "Sending response failed.");
75 ehResult = OC_EH_ERROR;
82 * This internal method handles RD publish request.
83 * Responds with the RD success message.
85 static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *ehRequest)
87 OCEntityHandlerResult ehResult = OC_EH_OK;
91 OIC_LOG(DEBUG, TAG, "Invalid request pointer");
95 OIC_LOG_V(DEBUG, TAG, "Received OC_REST_PUT from client with query: %s.", ehRequest->query);
97 OCRDPayload *payload = (OCRDPayload *)ehRequest->payload;
98 if (payload && payload->rdPublish)
100 OCRDStorePublishedResources(payload->rdPublish, &ehRequest->devAddr);
103 OCRDPayload *rdPayload = OCRDPayloadCreate();
106 OIC_LOG(ERROR, TAG, "Failed allocating memory.");
107 return OC_STACK_NO_MEMORY;
110 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
112 if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK)
114 OIC_LOG(ERROR, TAG, "Sending response failed.");
115 ehResult = OC_EH_ERROR;
122 * This internal method is the entity handler for RD resources and
123 * will handle REST request (GET/PUT/POST/DEL) for them.
125 static OCEntityHandlerResult rdEntityHandler(OCEntityHandlerFlag flag,
126 OCEntityHandlerRequest *ehRequest, __attribute__((unused)) void *callbackParameter)
128 OCEntityHandlerResult ehRet = OC_EH_ERROR;
135 if (flag & OC_REQUEST_FLAG)
137 OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG.");
138 switch (ehRequest->method)
141 case OC_REST_DISCOVER:
142 handleGetRequest(ehRequest);
145 handlePublishRequest(ehRequest);
149 case OC_REST_OBSERVE:
150 case OC_REST_OBSERVE_ALL:
151 case OC_REST_CANCEL_OBSERVE:
152 case OC_REST_PRESENCE:
153 case OC_REST_NOMETHOD:
162 * Registers RD resource
164 OCStackResult OCRDStart()
166 OCResourceHandle rdHandle = NULL;
168 OCStackResult result = OCCreateResource(&rdHandle,
169 OC_RSRVD_RESOURCE_TYPE_RD,
170 OC_RSRVD_INTERFACE_DEFAULT,
174 (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE));
176 if (result == OC_STACK_OK)
178 OIC_LOG(DEBUG, TAG, "Resource Directory Started.");
182 OIC_LOG(ERROR, TAG, "Failed starting Resource Directory.");
189 * Stops resource directory server
191 OCStackResult OCRDStop()
193 OCStackResult result = OCStop();
195 if (result == OC_STACK_OK)
197 OIC_LOG(DEBUG, TAG, "Resource Directory Stopped.");
201 OIC_LOG(ERROR, TAG, "Failed stopping Resource Directory.");