replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / resource-directory / src / rd_server.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "rd_server.h"
21
22 #include "rd_database.h"
23
24 #include "payload_logging.h"
25 #include "ocpayload.h"
26 #include "octypes.h"
27
28 #define TAG  PCF("RDServer")
29
30 #ifdef RD_SERVER
31
32 // This is temporary hardcoded value for bias factor.
33 static const int OC_RD_DISC_SEL = 100;
34
35 static OCResourceHandle rdHandle;
36
37 static OCStackResult sendResponse(const OCEntityHandlerRequest *ehRequest, OCRepPayload *rdPayload,
38     OCEntityHandlerResult ehResult)
39 {
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);
46 }
47
48 /**
49  * This internal method handles RD discovery request.
50  * Responds with the RD discovery payload message.
51  */
52 static OCEntityHandlerResult handleGetRequest(const OCEntityHandlerRequest *ehRequest)
53 {
54     if (!ehRequest)
55     {
56         OIC_LOG(DEBUG, TAG, "Invalid request pointer.");
57         return OC_EH_ERROR;
58     }
59
60     OCEntityHandlerResult ehResult = OC_EH_OK;
61     OIC_LOG_V(DEBUG, TAG, "Received OC_REST_GET from client with query: %s.", ehRequest->query);
62
63     OCRepPayload *rdPayload =  (OCRepPayload *)OCRepPayloadCreate();
64     if (!rdPayload)
65     {
66         return OC_STACK_NO_MEMORY;
67     }
68
69     const char *id = OCGetServerInstanceIDString();
70     if (id)
71     {
72         OCRepPayloadSetPropString(rdPayload, OC_RSRVD_DEVICE_ID, id);
73     }
74     OCRepPayloadSetPropInt(rdPayload, OC_RSRVD_RD_DISCOVERY_SEL, OC_RD_DISC_SEL);
75
76     OCRepPayloadAddResourceType(rdPayload, OC_RSRVD_RESOURCE_TYPE_RD);
77     OCRepPayloadAddResourceType(rdPayload, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
78
79     OCRepPayloadAddInterface(rdPayload, OC_RSRVD_INTERFACE_DEFAULT);
80
81     OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
82
83     if (sendResponse(ehRequest, rdPayload, OC_EH_OK) != OC_STACK_OK)
84     {
85         OIC_LOG(ERROR, TAG, "Sending response failed.");
86         ehResult = OC_EH_ERROR;
87     }
88
89     return ehResult;
90 }
91
92 /**
93  * This internal method handles RD publish request.
94  * Responds with the RD success message.
95  */
96 static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *ehRequest)
97 {
98     OCEntityHandlerResult ehResult = OC_EH_OK;
99
100     if (!ehRequest)
101     {
102         OIC_LOG(DEBUG, TAG, "Invalid request pointer");
103         return OC_EH_ERROR;
104     }
105
106     OIC_LOG_V(DEBUG, TAG, "Received OC_REST_POST from client with query: %s.", ehRequest->query);
107
108     OCRepPayload *payload = (OCRepPayload *)ehRequest->payload;
109     OCRepPayload *resPayload = NULL;
110     if (payload)
111     {
112         OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) payload);
113         if (OCRDDatabaseInit(NULL) == OC_STACK_OK)
114         {
115             if (OCRDDatabaseStoreResources(payload, &ehRequest->devAddr) == OC_STACK_OK)
116             {
117                 OIC_LOG_V(DEBUG, TAG, "Stored resources.");
118                 resPayload = payload;
119                 ehResult = OC_EH_OK;
120             }
121             else
122             {
123                 resPayload = (OCRepPayload *)OCRepPayloadCreate();
124                 ehResult = OC_EH_ERROR;
125             }
126         }
127
128         // Send Response
129         if (sendResponse(ehRequest, resPayload, ehResult) != OC_STACK_OK)
130         {
131             OIC_LOG(ERROR, TAG, "Sending response failed.");
132         }
133     }
134
135     return ehResult;
136 }
137
138 static OCEntityHandlerResult handleDeleteRequest(const OCEntityHandlerRequest *ehRequest)
139 {
140     OIC_LOG(DEBUG, TAG, "handleDeleteRequest - IN");
141
142     (void) ehRequest;  // eliminates release warning
143
144     OCEntityHandlerResult ehResult = OC_EH_OK;
145     return ehResult;
146 }
147
148 /*
149  * This internal method is the entity handler for RD resources and
150  * will handle REST request (GET/PUT/POST/DEL) for them.
151  */
152 static OCEntityHandlerResult rdEntityHandler(OCEntityHandlerFlag flag,
153         OCEntityHandlerRequest *ehRequest, OC_ANNOTATE_UNUSED void *callbackParameter)
154 {
155     OCEntityHandlerResult ehRet = OC_EH_ERROR;
156
157     if (!ehRequest)
158     {
159         return ehRet;
160     }
161
162     if (flag & OC_REQUEST_FLAG)
163     {
164         OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG.");
165         switch (ehRequest->method)
166         {
167             case OC_REST_GET:
168             case OC_REST_DISCOVER:
169                 ehRet = handleGetRequest(ehRequest);
170                 break;
171             case OC_REST_POST:
172                 ehRet = handlePublishRequest(ehRequest);
173                 break;
174             case OC_REST_DELETE:
175                 ehRet = handleDeleteRequest(ehRequest);
176                 break;
177             case OC_REST_PUT:
178             case OC_REST_OBSERVE:
179             case OC_REST_OBSERVE_ALL:
180 #ifdef WITH_PRESENCE
181             case OC_REST_PRESENCE:
182 #endif
183             case OC_REST_NOMETHOD:
184                 break;
185         }
186     }
187
188     return ehRet;
189 }
190
191 /**
192  * Registers RD resource
193  */
194 OCStackResult OCRDStart()
195 {
196     OCStackResult result = OCCreateResource(&rdHandle,
197                                 OC_RSRVD_RESOURCE_TYPE_RD,
198                                 OC_RSRVD_INTERFACE_DEFAULT,
199                                 OC_RSRVD_RD_URI,
200                                 rdEntityHandler,
201                                 NULL,
202                                 (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE));
203
204     if (result == OC_STACK_OK)
205     {
206         OIC_LOG(DEBUG, TAG, "Resource Directory resource created.");
207     }
208     else
209     {
210         OIC_LOG(ERROR, TAG, "Failed creating Resource Directory resource.");
211         return result;
212     }
213     result = OCBindResourceTypeToResource(rdHandle,
214                     OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
215     if (result == OC_STACK_OK)
216     {
217         OIC_LOG(DEBUG, TAG, "Resource Directory resource Publish created.");
218     }
219     else
220     {
221         OIC_LOG(ERROR, TAG, "Failed creating Resource Directory Publish resource.");
222     }
223
224     return result;
225 }
226
227 /**
228  * Stops resource directory server
229  */
230 OCStackResult OCRDStop()
231 {
232     if (!rdHandle)
233     {
234         OIC_LOG(ERROR, TAG, "Resource Directory resource handle is not initialized.");
235         return OC_STACK_NO_RESOURCE;
236     }
237
238     OCStackResult result = OCDeleteResource(rdHandle);
239
240     if (result == OC_STACK_OK)
241     {
242       OIC_LOG(DEBUG, TAG, "Resource Directory resource deleted.");
243     }
244     else
245     {
246       OIC_LOG(ERROR, TAG, "Resource Directory resource not deleted.");
247     }
248
249     return result;
250 }
251
252 #endif