1 /* ****************************************************************
3 * Copyright 2016 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 ******************************************************************/
21 #include "ocendpoint.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
26 #include "cainterface.h"
28 #define VERIFY_NON_NULL(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
29 #define VERIFY_GT_ZERO(arg) { if (arg < 1) {OIC_LOG(FATAL, TAG, #arg " < 1"); goto exit;} }
30 #define VERIFY_GT(arg1, arg2) { if (arg1 <= arg2) {OIC_LOG(FATAL, TAG, #arg1 " <= " #arg2); goto exit;} }
31 #define TAG "OIC_RI_ENDPOINT"
33 OCStackResult OCGetSupportedEndpointFlags(const OCTpsSchemeFlags givenFlags, OCTpsSchemeFlags* out)
37 return OC_STACK_INVALID_PARAM;
40 CATransportAdapter_t SelectedNetwork = CAGetSelectedNetwork();
41 if ((givenFlags & (OC_COAP | OC_COAPS)) && (SelectedNetwork & CA_ADAPTER_IP))
43 *out = (OCTpsSchemeFlags)(*out | OC_COAP);
47 *out = (OCTpsSchemeFlags)(*out | OC_COAPS);
51 if ((givenFlags & (OC_COAP_TCP | OC_COAPS_TCP)) && (SelectedNetwork & CA_ADAPTER_TCP))
54 *out = (OCTpsSchemeFlags)(*out | OC_COAP_TCP);
58 *out = (OCTpsSchemeFlags)(*out | OC_COAPS_TCP);
63 if ((givenFlags & OC_COAP_RFCOMM) && (SelectedNetwork & CA_ADAPTER_RFCOMM_BTEDR))
65 *out = (OCTpsSchemeFlags)(*out | OC_COAP_RFCOMM);
69 if ((givenFlags & OC_COAP_GATT) && (SelectedNetwork & CA_ADAPTER_GATT_BTLE))
71 *out = (OCTpsSchemeFlags)(*out | OC_COAP_GATT);
75 if ((givenFlags & OC_COAP_NFC) && (SelectedNetwork & CA_ADAPTER_NFC))
77 *out = (OCTpsSchemeFlags)(*out | OC_COAP_NFC);
81 if ((givenFlags & OC_COAP_RA) && (SelectedNetwork & CA_ADAPTER_REMOTE_ACCESS))
83 *out = (OCTpsSchemeFlags)(*out | OC_COAP_RA);
90 OCStackResult OCGetMatchedTpsFlags(const CATransportAdapter_t adapter,
91 const CATransportFlags_t flags,
92 OCTpsSchemeFlags* out)
94 // return matched TPS Flags with given CA flags.
97 OIC_LOG(ERROR, TAG, "out param is NULL!!!");
98 return OC_STACK_INVALID_PARAM;
101 if ((adapter & OC_ADAPTER_IP) && (flags & (OC_IP_USE_V4 | OC_IP_USE_V6)))
103 if (flags & OC_FLAG_SECURE)
106 // typecasting to support C90(arduino)
107 *out = (OCTpsSchemeFlags)(*out | OC_COAPS);
111 // typecasting to support C90(arduino)
112 *out = (OCTpsSchemeFlags)(*out | OC_COAP);
117 if ((adapter & OC_ADAPTER_TCP) && (flags & (OC_IP_USE_V4 | OC_IP_USE_V6)))
119 if (flags & OC_FLAG_SECURE)
122 // typecasting to support C90(arduino)
123 *out = (OCTpsSchemeFlags)(*out | OC_COAPS_TCP);
128 // typecasting to support C90(arduino)
129 *out = (OCTpsSchemeFlags)(*out | OC_COAP_TCP);
135 // @todo iotivity not support http transport yet...
138 // @todo iotivity not support https transport yet...
142 if (adapter & OC_ADAPTER_RFCOMM_BTEDR)
144 // typecasting to support C90(arduino)
145 *out = (OCTpsSchemeFlags)(*out | OC_COAP_RFCOMM);
150 if (adapter & CA_ADAPTER_GATT_BTLE)
152 // typecasting to support C90(arduino)
153 *out = (OCTpsSchemeFlags)(*out | OC_COAP_GATT);
158 if (adapter & CA_ADAPTER_NFC)
160 // typecasting to support C90(arduino)
161 *out = (OCTpsSchemeFlags)(*out | OC_COAP_NFC);
166 if (adapter & CA_ADAPTER_REMOTE_ACCESS)
168 // typecasting to support C90(arduino)
169 *out = (OCTpsSchemeFlags)(*out | OC_COAP_RA);
176 OCStackResult OCConvertTpsToString(const OCTpsSchemeFlags tps, char** out)
178 // return given tps as string for payload
179 // OC_COAP_IPV4 -> OC_COAP
183 return OC_STACK_INVALID_PARAM;
189 *out = OICStrdup(COAP_STR);
193 *out = OICStrdup(COAPS_STR);
197 *out = OICStrdup(COAP_TCP_STR);
201 *out = OICStrdup(COAPS_TCP_STR);
206 *out = OICStrdup(HTTP_STR);
210 *out = OICStrdup(HTTPS_STR);
215 *out = OICStrdup(COAP_RFCOMM_STR);
219 return OC_STACK_INVALID_PARAM;
221 VERIFY_NON_NULL(*out);
225 return OC_STACK_NO_MEMORY;
228 char* OCCreateEndpointString(const OCEndpointPayload* endpoint)
235 char* buf = (char*)OICCalloc(MAX_ADDR_STR_SIZE, sizeof(char));
236 VERIFY_NON_NULL(buf);
238 if ((strcmp(endpoint->tps, COAP_STR) == 0) || (strcmp(endpoint->tps, COAPS_STR) == 0)
240 || (strcmp(endpoint->tps, COAP_TCP_STR) == 0) ||(strcmp(endpoint->tps, COAPS_TCP_STR) == 0)
243 || (strcmp(endpoint->tps, HTTP_STR) == 0) ||(strcmp(endpoint->tps, HTTPS_STR) == 0)
247 // checking addr is ipv4 or not
248 if (endpoint->family & OC_IP_USE_V4)
251 sprintf(buf, "%s://%s:%d", endpoint->tps, endpoint->addr, endpoint->port);
256 sprintf(buf, "%s://[%s]:%d", endpoint->tps, endpoint->addr, endpoint->port);
260 else if ((strcmp(endpoint->tps, COAP_RFCOMM_STR) == 0))
263 sprintf(buf, "%s://%s", endpoint->tps, endpoint->addr);
268 OIC_LOG_V(ERROR, TAG, "Payload has invalid TPS!!! %s", endpoint->tps);
277 OCStackResult OCParseEndpointString(const char* endpointStr, OCEndpointPayload* out)
279 if (!endpointStr || !out)
281 return OC_STACK_INVALID_PARAM;
290 size_t tpsCharsToWrite = 0;
291 size_t addrCharsToWrite = 0;
292 OCStackResult isEnabledAdapter = OC_STACK_ADAPTER_NOT_ENABLED;
293 OCTransportAdapter parsedAdapter = OC_DEFAULT_ADAPTER;
295 tps = (char*)OICCalloc(OC_MAX_TPS_STR_SIZE, sizeof(char));
296 VERIFY_NON_NULL(tps);
298 addr = (char*)OICCalloc(OC_MAX_ADDR_STR_SIZE, sizeof(char));
299 VERIFY_NON_NULL(addr);
301 origin = OICStrdup(endpointStr);
302 VERIFY_NON_NULL(origin);
305 tokPos = strstr(origin, OC_ENDPOINT_TPS_TOKEN);
306 VERIFY_NON_NULL(tokPos);
309 tpsCharsToWrite = tokPos - origin;
310 VERIFY_GT_ZERO(tpsCharsToWrite);
311 VERIFY_GT((size_t)OC_MAX_TPS_STR_SIZE, tpsCharsToWrite);
312 ret = memcpy(tps, origin, tpsCharsToWrite);
313 VERIFY_NON_NULL(ret);
314 OIC_LOG_V(INFO, TAG, "parsed tps is:%s", tps);
317 if (strcmp(tps, COAP_STR) == 0)
319 isEnabledAdapter = OC_STACK_OK;
320 parsedAdapter = OC_ADAPTER_IP;
322 else if (strcmp(tps, COAPS_STR) == 0)
324 isEnabledAdapter = OC_STACK_OK;
325 parsedAdapter = OC_ADAPTER_IP;
328 else if (strcmp(tps, COAP_TCP_STR) == 0)
330 isEnabledAdapter = OC_STACK_OK;
331 parsedAdapter = OC_ADAPTER_TCP;
333 else if (strcmp(tps, COAPS_TCP_STR) == 0)
335 isEnabledAdapter = OC_STACK_OK;
336 parsedAdapter = OC_ADAPTER_TCP;
340 // @todo iotivity not support http, https transport yet...
343 else if (strcmp(tps, COAP_RFCOMM_STR) == 0)
345 isEnabledAdapter = OC_STACK_OK;
346 parsedAdapter = OC_ADAPTER_RFCOMM_BTEDR;
349 // ignore unrecognized tps type
350 if (isEnabledAdapter == OC_STACK_ADAPTER_NOT_ENABLED
351 && parsedAdapter == OC_DEFAULT_ADAPTER)
356 return OC_STACK_ADAPTER_NOT_ENABLED;
360 if (parsedAdapter == OC_ADAPTER_RFCOMM_BTEDR)
364 ret = strcpy(addr, tokPos);
365 VERIFY_NON_NULL(ret);
368 out->family = OC_DEFAULT_FLAGS;
374 // first check epl has square bracket
375 tmp = strchr(origin, OC_ENDPOINT_BRACKET_START);
378 out->family = OC_IP_USE_V6;
380 tmp = strrchr(origin, OC_ENDPOINT_BRACKET_END);
384 out->family = OC_IP_USE_V4;
386 tmp = strrchr(origin, OC_ENDPOINT_ADDR_TOKEN);
388 VERIFY_NON_NULL(tmp);
391 addrCharsToWrite = tmp - tokPos;
392 VERIFY_GT_ZERO(addrCharsToWrite);
393 VERIFY_GT((size_t)OC_MAX_ADDR_STR_SIZE, addrCharsToWrite);
394 ret = memcpy(addr, tokPos, addrCharsToWrite);
395 VERIFY_NON_NULL(ret);
396 OIC_LOG_V(INFO, TAG, "parsed addr is:%s", addr);
398 tmp = strrchr(origin, OC_ENDPOINT_ADDR_TOKEN);
399 VERIFY_NON_NULL(tmp);
403 VERIFY_GT_ZERO(atoi(tokPos));
404 OIC_LOG_V(INFO, TAG, "parsed port is:%s", tokPos);
408 out->port = atoi(tokPos);
429 return OC_STACK_ERROR;
432 OCTpsSchemeFlags OCGetSupportedTpsFlags()
434 OCTpsSchemeFlags ret = OC_NO_TPS;
435 CATransportAdapter_t SelectedNetwork = CAGetSelectedNetwork();
437 if (SelectedNetwork & CA_ADAPTER_IP)
439 ret = (OCTpsSchemeFlags)(ret | OC_COAP);
443 ret = (OCTpsSchemeFlags)(ret | OC_COAPS);
447 if (SelectedNetwork & CA_ADAPTER_TCP)
449 ret = (OCTpsSchemeFlags)(ret | OC_COAP_TCP);
453 ret = (OCTpsSchemeFlags)(ret | OC_COAPS_TCP);
458 if (SelectedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
460 ret = (OCTpsSchemeFlags)(ret | OC_COAP_RFCOMM);
464 if (SelectedNetwork & CA_ADAPTER_GATT_BTLE)
466 ret = (OCTpsSchemeFlags)(ret | OC_COAP_GATT);
470 if (SelectedNetwork & CA_ADAPTER_NFC)
472 ret = (OCTpsSchemeFlags)(ret | OC_COAP_NFC);
476 if (SelectedNetwork & CA_ADAPTER_REMOTE_ACCESS)
478 ret = (OCTpsSchemeFlags)(ret | OC_COAP_RA);