1 /* ****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
24 #include "routingmanagerinterface.h"
25 #include "routingmessageparser.h"
26 #include "routingutility.h"
27 #include "ocobserve.h"
28 #include "include/logger.h"
32 * Logging tag for module name.
34 #define TAG "RM_INTERFACE"
37 * Name of resource type.
39 #define GW_RESOURCE_TYPE_NAME "core.gateway"
42 * Name of resource interface.
44 #define GW_RESOURCE_INTF_NAME "oc.mi.def"
47 * URI of the resource.
49 #define GW_RESOURCE_URI "/oic/gateway"
52 * Max Number of times to send data considering wifi packet drops.
54 #define MAX_SEND_DATA 3
57 * Pointer to handle of the newly created gateway resource.
59 static OCResourceHandle g_gateWayHandle = NULL;
62 * Discovery callback registered with RI for a Discover Request.
64 OCStackApplicationResult RMDiscoverGatewayCallback(void* ctx, OCDoHandle handle,
65 OCClientResponse * clientResponse);
68 * Observe callback registered with RI for a observe Request.
70 OCStackApplicationResult RMObserveRequestCallback(void* ctx, OCDoHandle handle,
71 OCClientResponse * clientResponse);
73 OCConnectivityType RMGetConnectivityType(OCTransportAdapter adapter)
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:
86 OC_LOG(DEBUG, TAG, "Default option will be selected");
91 OCStackResult RMInitGatewayResource()
93 OC_LOG(DEBUG, TAG, "RMInitGatewayResource IN");
95 // Create a Gateway resource
96 OCStackResult result = OCCreateResource(&g_gateWayHandle,
97 GW_RESOURCE_TYPE_NAME,
98 GW_RESOURCE_INTF_NAME,
104 if (OC_STACK_OK != result)
106 OC_LOG_V(ERROR, TAG, "Create resource for gateway failed[%d]", result);
109 OC_LOG(DEBUG, TAG, "RMInitGatewayResource OUT");
113 OCStackResult RMDiscoverGatewayResource()
115 OC_LOG(DEBUG, TAG, "RMDiscoverGatewayResource IN");
116 OCCallbackData discoverData = {.cb = RMDiscoverGatewayCallback};
117 OCStackResult result = OC_STACK_OK;
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);
123 // Temp fix for packet drops in WIFI.
124 for (uint8_t sendData = 0; sendData < MAX_SEND_DATA; sendData++)
126 result = OCDoResource(NULL, OC_REST_DISCOVER, GW_RESOURCE_URI, 0, 0,
127 CT_ADAPTER_IP, OC_LOW_QOS, &discoverData, NULL, 0);
130 OC_LOG(DEBUG, TAG, "RMDiscoverGatewayResource OUT");
134 OCStackApplicationResult RMDiscoverGatewayCallback(void* ctx, OCDoHandle handle,
135 OCClientResponse * clientResponse)
137 OC_LOG(DEBUG, TAG, "RMDiscoverGatewayCallback IN");
140 if (NULL == clientResponse)
142 OC_LOG(DEBUG, TAG, "clientResponse is NULL");
143 return OC_STACK_KEEP_TRANSACTION;
146 OCStackResult result = RMHandleResponsePayload(&(clientResponse->devAddr),
147 (OCRepPayload *)clientResponse->payload);
148 if (OC_STACK_OK != result)
150 OC_LOG_V(ERROR, TAG, "RMHandleResponsePayload Failed[%d]", result);
153 OCRepPayload *payload = NULL;
154 // Created payload is freed in the OCDoResource() api.
155 result= RMGetGatewayPayload(&payload);
156 if (OC_STACK_OK != result)
158 OC_LOG_V(ERROR, TAG, "RMGetGatewayPayload Failed[%d]", result);
161 RMSendObserveRequest(&(clientResponse->devAddr), payload);
163 OC_LOG(DEBUG, TAG, "RMDiscoverGatewayCallback OUT");
164 return OC_STACK_KEEP_TRANSACTION;
167 OCStackResult RMSendObserveRequest(const OCDevAddr *devAddr, OCRepPayload *payload)
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");
174 return OCDoResource(NULL, OC_REST_OBSERVE, GW_RESOURCE_URI, devAddr, (OCPayload *)payload,
175 RMGetConnectivityType(devAddr->adapter), OC_HIGH_QOS,
176 &observeData, NULL, 0);
179 OCStackResult RMSendDeleteRequest(const OCDevAddr *devAddr, OCRepPayload *payload)
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);
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,
192 OCStackResult RMSendResponse(const OCServerRequest *request, const OCResource *resource,
193 const OCRepPayload *payload)
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
202 OC_LOG(DEBUG, TAG, "RMSendResponse OUT");
204 return OCDoResponse(&response);
207 OCStackResult RMSendNotificationForListofObservers(OCObservationId *obsId, uint8_t obsLen,
208 const OCRepPayload *payload)
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");
220 OCStackApplicationResult RMObserveRequestCallback(void* ctx, OCDoHandle handle,
221 OCClientResponse *clientResponse)
223 OC_LOG(DEBUG, TAG, "RMObserveRequestCallback IN");
226 if (NULL == clientResponse)
228 OC_LOG(DEBUG, TAG, "clientResponse is NULL");
229 return OC_STACK_KEEP_TRANSACTION;
232 if (OC_STACK_COMM_ERROR == clientResponse->result)
234 OC_LOG(DEBUG, TAG, "Received TIMEOUT ERROR");
235 return OC_STACK_KEEP_TRANSACTION;
238 OCStackResult result = RMHandleResponsePayload(&(clientResponse->devAddr),
239 (OCRepPayload *)clientResponse->payload);
240 if (OC_STACK_OK != result)
242 OC_LOG_V(ERROR, TAG, "RMHandleResponsePayload Failed[%d]", result);
245 OC_LOG(DEBUG, TAG, "RMObserveRequestCallback OUT");
246 return OC_STACK_KEEP_TRANSACTION;
249 OCStackResult RMAddObserverToStack(const OCServerRequest *request, OCObservationId *obsID)
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");
255 OCStackResult result = OC_STACK_OK;
258 result = GenerateObserverId(obsID);
260 if (OC_STACK_OK != result)
262 OC_LOG_V(DEBUG, TAG, "GenerateObserverId failed[%d]", result);
266 OC_LOG_V(DEBUG, TAG, "Observer ID is %d", *obsID);
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,
273 OC_LOG(DEBUG, TAG, "RMAddObserverToStack OUT");