Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / 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_storage.h"
23
24 #include "rdpayload.h"
25 #include "payload_logging.h"
26
27 #define TAG  PCF("RDServer")
28
29 // This is temporary hardcoded value for bias factor.
30 #define OC_RD_DISC_SEL 100
31
32 static OCStackResult sendResponse(const OCEntityHandlerRequest *ehRequest, OCRDPayload *rdPayload)
33 {
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);
40 }
41
42 /**
43  * This internal method handles RD discovery request.
44  * Responds with the RD discovery payload message.
45  */
46 static OCEntityHandlerResult handleGetRequest(const OCEntityHandlerRequest *ehRequest)
47 {
48     if (!ehRequest)
49     {
50         OIC_LOG(DEBUG, TAG, "Invalid request pointer.");
51         return OC_EH_ERROR;
52     }
53
54     OCEntityHandlerResult ehResult = OC_EH_OK;
55     OIC_LOG_V(DEBUG, TAG, "Received OC_REST_GET from client with query: %s.", ehRequest->query);
56
57     OCRDPayload *rdPayload = OCRDPayloadCreate();
58     if (!rdPayload)
59     {
60         return OC_STACK_NO_MEMORY;
61     }
62
63     rdPayload->rdDiscovery = OCRDDiscoveryPayloadCreate(NULL, OCGetServerInstanceIDString(), OC_RD_DISC_SEL);
64     if (!rdPayload->rdDiscovery)
65     {
66         OCRDPayloadDestroy(rdPayload);
67         return OC_STACK_NO_MEMORY;
68     }
69
70     OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
71
72     if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK)
73     {
74         OIC_LOG(ERROR, TAG, "Sending response failed.");
75         ehResult = OC_EH_ERROR;
76     }
77
78     return ehResult;
79 }
80
81 /**
82  * This internal method handles RD publish request.
83  * Responds with the RD success message.
84  */
85 static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *ehRequest)
86 {
87     OCEntityHandlerResult ehResult = OC_EH_OK;
88
89     if (!ehRequest)
90     {
91         OIC_LOG(DEBUG, TAG, "Invalid request pointer");
92         return OC_EH_ERROR;
93     }
94
95     OIC_LOG_V(DEBUG, TAG, "Received OC_REST_PUT from client with query: %s.", ehRequest->query);
96
97     OCRDPayload *payload = (OCRDPayload *)ehRequest->payload;
98     if (payload && payload->rdPublish)
99     {
100         OCRDStorePublishedResources(payload->rdPublish, &ehRequest->devAddr);
101     }
102
103     OCRDPayload *rdPayload = OCRDPayloadCreate();
104     if (!rdPayload)
105     {
106         OIC_LOG(ERROR, TAG, "Failed allocating memory.");
107         return OC_STACK_NO_MEMORY;
108     }
109
110     OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
111
112     if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK)
113     {
114         OIC_LOG(ERROR, TAG, "Sending response failed.");
115         ehResult = OC_EH_ERROR;
116     }
117
118     return ehResult;
119 }
120
121 /*
122  * This internal method is the entity handler for RD resources and
123  * will handle REST request (GET/PUT/POST/DEL) for them.
124  */
125 static OCEntityHandlerResult rdEntityHandler(OCEntityHandlerFlag flag,
126         OCEntityHandlerRequest *ehRequest, __attribute__((unused)) void *callbackParameter)
127 {
128     OCEntityHandlerResult ehRet = OC_EH_ERROR;
129
130     if (!ehRequest)
131     {
132         return ehRet;
133     }
134
135     if (flag & OC_REQUEST_FLAG)
136     {
137         OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG.");
138         switch (ehRequest->method)
139         {
140             case OC_REST_GET:
141             case OC_REST_DISCOVER:
142                 handleGetRequest(ehRequest);
143                 break;
144             case OC_REST_POST:
145                 handlePublishRequest(ehRequest);
146                 break;
147             case OC_REST_PUT:
148             case OC_REST_DELETE:
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:
154                 break;
155         }
156     }
157
158     return ehRet;
159 }
160
161 /**
162  * Registers RD resource
163  */
164 OCStackResult OCRDStart()
165 {
166     OCResourceHandle rdHandle = NULL;
167
168     OCStackResult result = OCCreateResource(&rdHandle,
169                                 OC_RSRVD_RESOURCE_TYPE_RD,
170                                 OC_RSRVD_INTERFACE_DEFAULT,
171                                 OC_RSRVD_RD_URI,
172                                 rdEntityHandler,
173                                 NULL,
174                                 (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE));
175
176     if (result == OC_STACK_OK)
177     {
178         OIC_LOG(DEBUG, TAG, "Resource Directory Started.");
179     }
180     else
181     {
182         OIC_LOG(ERROR, TAG, "Failed starting Resource Directory.");
183     }
184
185     return result;
186 }
187
188 /**
189  * Stops resource directory server
190  */
191 OCStackResult OCRDStop()
192 {
193     OCStackResult result = OCStop();
194
195     if (result == OC_STACK_OK)
196     {
197         OIC_LOG(DEBUG, TAG, "Resource Directory Stopped.");
198     }
199     else
200     {
201         OIC_LOG(ERROR, TAG, "Failed stopping Resource Directory.");
202     }
203     return result;
204 }