Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / routing / src / routingmanagerinterface.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
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "routingmanagerinterface.h"
25 #include "routingmessageparser.h"
26 #include "routingutility.h"
27 #include "ocobserve.h"
28 #include "include/logger.h"
29 #include "ocrandom.h"
30
31 /**
32  * Logging tag for module name.
33  */
34 #define TAG "RM_INTERFACE"
35
36 /**
37  * Name of resource type.
38  */
39 #define GW_RESOURCE_TYPE_NAME "core.gateway"
40
41 /**
42  * Name of resource interface.
43  */
44 #define GW_RESOURCE_INTF_NAME "oc.mi.def"
45
46 /**
47  * URI of the resource.
48  */
49 #define GW_RESOURCE_URI "/oic/gateway"
50
51 /**
52  * Max Number of times to send data considering wifi packet drops.
53  */
54 #define MAX_SEND_DATA 3
55
56 /**
57  * Pointer to handle of the newly created gateway resource.
58  */
59 static OCResourceHandle g_gateWayHandle = NULL;
60
61 /**
62  * Discovery callback registered with RI for a Discover Request.
63  */
64 OCStackApplicationResult RMDiscoverGatewayCallback(void* ctx, OCDoHandle handle,
65                                                    OCClientResponse * clientResponse);
66
67 /**
68  * Observe callback registered with RI for a observe Request.
69  */
70 OCStackApplicationResult RMObserveRequestCallback(void* ctx, OCDoHandle handle,
71                                                   OCClientResponse * clientResponse);
72
73 OCConnectivityType RMGetConnectivityType(OCTransportAdapter adapter)
74 {
75     switch(adapter)
76     {
77         case OC_ADAPTER_IP:
78             return CT_ADAPTER_IP;
79         case OC_ADAPTER_GATT_BTLE:
80             return CT_ADAPTER_GATT_BTLE;
81         case OC_ADAPTER_RFCOMM_BTEDR:
82             return CT_ADAPTER_RFCOMM_BTEDR;
83         case OC_DEFAULT_ADAPTER:
84             break;
85         default:
86             OC_LOG(DEBUG, TAG, "Default option will be selected");
87     }
88     return CT_DEFAULT;
89 }
90
91 OCStackResult RMInitGatewayResource()
92 {
93     OC_LOG(DEBUG, TAG, "RMInitGatewayResource IN");
94
95     // Create a Gateway resource
96     OCStackResult result = OCCreateResource(&g_gateWayHandle,
97                                             GW_RESOURCE_TYPE_NAME,
98                                             GW_RESOURCE_INTF_NAME,
99                                             GW_RESOURCE_URI,
100                                             NULL,
101                                             NULL,
102                                             OC_OBSERVABLE);
103
104     if (OC_STACK_OK != result)
105     {
106         OC_LOG_V(ERROR, TAG, "Create resource for gateway failed[%d]", result);
107     }
108
109     OC_LOG(DEBUG, TAG, "RMInitGatewayResource OUT");
110     return result;
111 }
112
113 OCStackResult RMDiscoverGatewayResource()
114 {
115     OC_LOG(DEBUG, TAG, "RMDiscoverGatewayResource IN");
116     OCCallbackData discoverData = {.cb = RMDiscoverGatewayCallback};
117     OCStackResult result = OC_STACK_OK;
118
119     result = OCDoResource(NULL, OC_REST_DISCOVER, GW_RESOURCE_URI, 0, 0,
120                           CT_ADAPTER_IP | CT_ADAPTER_RFCOMM_BTEDR,
121                           OC_LOW_QOS, &discoverData, NULL, 0);
122
123     // Temp fix for packet drops in WIFI.
124     for (uint8_t sendData = 0; sendData < MAX_SEND_DATA; sendData++)
125     {
126         result = OCDoResource(NULL, OC_REST_DISCOVER, GW_RESOURCE_URI, 0, 0,
127                               CT_ADAPTER_IP, OC_LOW_QOS, &discoverData, NULL, 0);
128         usleep(100000);
129     }
130     OC_LOG(DEBUG, TAG, "RMDiscoverGatewayResource OUT");
131     return result;
132 }
133
134 OCStackApplicationResult RMDiscoverGatewayCallback(void* ctx, OCDoHandle handle,
135                                                    OCClientResponse * clientResponse)
136 {
137     OC_LOG(DEBUG, TAG, "RMDiscoverGatewayCallback IN");
138     (void)ctx;
139     (void)handle;
140     if (NULL == clientResponse)
141     {
142         OC_LOG(DEBUG, TAG, "clientResponse is NULL");
143         return OC_STACK_KEEP_TRANSACTION;
144     }
145
146     OCStackResult result = RMHandleResponsePayload(&(clientResponse->devAddr),
147                                                    (OCRepPayload *)clientResponse->payload);
148     if (OC_STACK_OK != result)
149     {
150         OC_LOG_V(ERROR, TAG, "RMHandleResponsePayload Failed[%d]", result);
151     }
152
153     OCRepPayload *payload = NULL;
154     // Created payload is freed in the OCDoResource() api.
155     result= RMGetGatewayPayload(&payload);
156     if (OC_STACK_OK != result)
157     {
158         OC_LOG_V(ERROR, TAG, "RMGetGatewayPayload Failed[%d]", result);
159     }
160
161     RMSendObserveRequest(&(clientResponse->devAddr), payload);
162
163     OC_LOG(DEBUG, TAG, "RMDiscoverGatewayCallback OUT");
164     return OC_STACK_KEEP_TRANSACTION;
165 }
166
167 OCStackResult RMSendObserveRequest(const OCDevAddr *devAddr, OCRepPayload *payload)
168 {
169     OC_LOG(DEBUG, TAG, "RMSendObserveRequest IN");
170     OC_LOG_V(DEBUG, TAG, "Destination address is %s:%d", devAddr->addr, devAddr->port);
171     OCCallbackData observeData = {.cb = RMObserveRequestCallback};
172     OC_LOG(DEBUG, TAG, "RMSendObserveRequest OUT");
173
174     return OCDoResource(NULL, OC_REST_OBSERVE, GW_RESOURCE_URI, devAddr, (OCPayload *)payload,
175                         RMGetConnectivityType(devAddr->adapter), OC_HIGH_QOS,
176                         &observeData, NULL, 0);
177 }
178
179 OCStackResult RMSendDeleteRequest(const OCDevAddr *devAddr, OCRepPayload *payload)
180 {
181     OC_LOG(DEBUG, TAG, "RMSendDeleteRequest IN");
182     RM_NULL_CHECK_WITH_RET(payload, TAG, "payload");
183     OC_LOG_V(DEBUG, TAG, "Destination address is %s:%d", devAddr->addr, devAddr->port);
184
185     OCCallbackData deleteCb = {.cb = RMDiscoverGatewayCallback};
186     OC_LOG(DEBUG, TAG, "RMSendDeleteRequest OUT");
187     return OCDoResource(NULL, OC_REST_DELETE, GW_RESOURCE_URI, devAddr, (OCPayload *)payload,
188                     RMGetConnectivityType(devAddr->adapter), OC_LOW_QOS,
189                     &deleteCb, NULL, 0);
190 }
191
192 OCStackResult RMSendResponse(const OCServerRequest *request, const OCResource *resource,
193                              const OCRepPayload *payload)
194 {
195     OC_LOG(DEBUG, TAG, "RMSendResponse IN");
196     OCEntityHandlerResponse response = {.ehResult = OC_EH_OK,
197                                         .payload = (OCPayload *)payload,
198                                         .persistentBufferFlag = 0,
199                                         .requestHandle = (OCRequestHandle) request,
200                                         .resourceHandle = (OCResourceHandle) resource
201                                         };
202     OC_LOG(DEBUG, TAG, "RMSendResponse OUT");
203
204     return OCDoResponse(&response);
205 }
206
207 OCStackResult RMSendNotificationForListofObservers(OCObservationId *obsId, uint8_t obsLen,
208                                                    const OCRepPayload *payload)
209 {
210     OC_LOG(DEBUG, TAG, "RMSendNotificationForListofObservers IN");
211     RM_NULL_CHECK_WITH_RET(obsId, TAG, "obsId");
212     RM_NULL_CHECK_WITH_RET(payload, TAG, "payload");
213     OCStackResult result = OCNotifyListOfObservers(g_gateWayHandle, obsId, obsLen,
214                                                    payload, OC_LOW_QOS);
215     OC_LOG_V(DEBUG, TAG, "Result is %d", result);
216     OC_LOG(DEBUG, TAG, "RMSendNotificationForListofObservers OUT");
217     return result;
218 }
219
220 OCStackApplicationResult RMObserveRequestCallback(void* ctx, OCDoHandle handle,
221                                                   OCClientResponse *clientResponse)
222 {
223     OC_LOG(DEBUG, TAG, "RMObserveRequestCallback IN");
224     (void)ctx;
225     (void)handle;
226     if (NULL == clientResponse)
227     {
228         OC_LOG(DEBUG, TAG, "clientResponse is NULL");
229         return OC_STACK_KEEP_TRANSACTION;
230     }
231
232     if (OC_STACK_COMM_ERROR == clientResponse->result)
233     {
234         OC_LOG(DEBUG, TAG, "Received TIMEOUT ERROR");
235         return OC_STACK_KEEP_TRANSACTION;
236     }
237
238     OCStackResult result = RMHandleResponsePayload(&(clientResponse->devAddr),
239                                                    (OCRepPayload *)clientResponse->payload);
240     if (OC_STACK_OK != result)
241     {
242         OC_LOG_V(ERROR, TAG, "RMHandleResponsePayload Failed[%d]", result);
243     }
244
245     OC_LOG(DEBUG, TAG, "RMObserveRequestCallback OUT");
246     return OC_STACK_KEEP_TRANSACTION;
247 }
248
249 OCStackResult RMAddObserverToStack(const OCServerRequest *request, OCObservationId *obsID)
250 {
251     OC_LOG(DEBUG, TAG, "RMAddObserverToStack IN");
252     RM_NULL_CHECK_WITH_RET(request, TAG, "request");
253     RM_NULL_CHECK_WITH_RET(obsID, TAG, "obsID");
254
255     OCStackResult result = OC_STACK_OK;
256     while (0 == *obsID)
257     {
258         result = GenerateObserverId(obsID);
259     }
260     if (OC_STACK_OK != result)
261     {
262         OC_LOG_V(DEBUG, TAG, "GenerateObserverId failed[%d]", result);
263         return result;
264     }
265
266     OC_LOG_V(DEBUG, TAG, "Observer ID is %d", *obsID);
267     // Add the observer
268     result = AddObserver((const char*)(request->resourceUrl),
269                 (const char *)(request->query),
270                 *obsID, request->requestToken, request->tokenLength,
271                 (OCResource *)g_gateWayHandle, request->qos, OC_FORMAT_CBOR,
272                 &request->devAddr);
273     OC_LOG(DEBUG, TAG, "RMAddObserverToStack OUT");
274     return result;
275 }