SET(SRCS
src/tds-provider-main.c
src/tds-provider-service.c
+ src/tds-provider-advertiser.c
+ src/tds-provider-scanner.c
src/tds-provider-aware.c
+ src/csiphash/csiphash.c
+ src/tds-api.c
)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/csiphash)
SET(PKG_MODULES
dlog
gio-unix-2.0
capi-network-bluetooth
capi-network-wifi-aware
+ capi-system-info
)
INCLUDE(FindPkgConfig)
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TDS_API_H__
+#define __TDS_API_H__
+
+#include <stdio.h>
+#include <stdint.h>
+
+void _tds_bloom_filter_add_string(uint8_t *inFilterPtr, size_t inFilterLen, const char *inStr, size_t inLen);
+bool _tds_bloom_filter_contain_hash(const uint8_t *inFilterPtr, size_t inFilterLen, uint64_t inHash);
+bool _tds_bloom_filter_contain_string(const uint8_t *inFilterPtr, size_t inFilterLen, const char *inStr, size_t inLen);
+int _bt_tds_start_scan(bt_tds_provider_scan_result_cb cb, void *user_data);
+int _bt_tds_stop_scan();
+
+#endif /* __TDS_API_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TDS_PROVIDER_ADVERTISER_H__
+#define __TDS_PROVIDER_ADVERTISER_H__
+
+#include "tds-provider-service.h"
+
+int _tds_advertiser_enable(tds_transport_state_e state);
+int _tds_advertiser_disable();
+
+#endif /* __TDS_PROVIDER_ADVERTISER_H__ */
#ifndef __TDS_PROVIDER_AWARE_H__
#define __TDS_PROVIDER_AWARE_H__
-int _tds_aware_init();
-int _tds_aware_deinit();
+int _tds_aware_enable();
+int _tds_aware_disable();
#endif /* __TDS_PROVIDER_AWARE_H__ */
#include <glib.h>
#include <gio/gio.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#undef LOG_TAG
-#define LOG_TAG "TDS_PROVIDER"
-
-#ifdef FUNCTION_TRACE
-#define FN_START TDS_DBG("[ENTER FUNC]")
-#define FN_END TDS_DBG("[EXIT FUNC]")
-#else
-#define FN_START
-#define FN_END
-#endif
-
-#define LOG_COLOR_RESET "\033[0m"
-#define LOG_COLOR_RED "\033[31m"
-#define LOG_COLOR_YELLOW "\033[33m"
-#define LOG_COLOR_GREEN "\033[32m"
-#define LOG_COLOR_BLUE "\033[36m"
-#define LOG_COLOR_PURPLE "\033[35m"
-
-#define TDS_PERMANENT_LOG(fmt, args...) _tds_permanent_log(fmt, ##args)
-
-#define TDS_DBG(fmt, args...) \
- SLOGD(fmt, ##args)
-#define TDS_INFO(fmt, args...) \
- SLOGI(fmt, ##args)
-#define TDS_ERR(fmt, args...) \
- SLOGE(fmt, ##args)
-
-#define TDS_INFO_C(fmt, arg...) \
- SLOGI_IF(TRUE, LOG_COLOR_GREEN" "fmt" "LOG_COLOR_RESET, ##arg)
-#define TDS_ERR_C(fmt, arg...) \
- SLOGI_IF(TRUE, LOG_COLOR_RED" "fmt" "LOG_COLOR_RESET, ##arg)
-
-#define DBG_SECURE(fmt, args...) SECURE_SLOGD(fmt, ##args)
-#define ERR_SECURE(fmt, args...) SECURE_SLOGE(fmt, ##args)
-
#define ret_if(expr) \
do { \
if (expr) { \
} \
} while (0)
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
+#define TDS_BLOOM_FILTER_OPERATION_STR_PROVIDER "provider"
+#define TDS_BLOOM_FILTER_OPERATION_STR_SEEK "seek"
+#define TDS_BLOOM_FILTER_OPERATION_STR_BROWSE "browse"
+#define TDS_BLOOM_FILTER_OPERATION_STR_ACTIVATION "activation"
+
+typedef enum {
+ TDS_ERROR_NONE,
+ TDS_ERROR_INTERNAL,
+} tds_error_e;
+
+typedef enum {
+ TDS_BLOOM_FILTER_OPERATION_PROVIDER,
+ TDS_BLOOM_FILTER_OPERATION_SEEK,
+ TDS_BLOOM_FILTER_OPERATION_BROWSE,
+ TDS_BLOOM_FILTER_OPERATION_ACTIVATION,
+} tds_bloom_filter_operation_e;
+
+typedef enum {
+ TDS_TRANSPORT_STATE_OFF,
+ TDS_TRANSPORT_STATE_ON,
+} tds_transport_state_e;
+
#endif /* __TDS_PROVIDER_COMMON_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __TDS_PROVIDER_LOG_H__
+#define __TDS_PROVIDER_LOG_H__
+
+#include <sys/types.h>
+#include <dlog.h>
+#include <glib.h>
+#include <gio/gio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#undef LOG_TAG
+#define LOG_TAG "TDS_PROVIDER"
+
+#ifdef FUNCTION_TRACE
+#define FN_START TDS_DBG("[ENTER FUNC]")
+#define FN_END TDS_DBG("[EXIT FUNC]")
+#else
+#define FN_START
+#define FN_END
+#endif
+
+#define LOG_COLOR_RESET "\033[0m"
+#define LOG_COLOR_RED "\033[31m"
+#define LOG_COLOR_YELLOW "\033[33m"
+#define LOG_COLOR_GREEN "\033[32m"
+#define LOG_COLOR_BLUE "\033[36m"
+#define LOG_COLOR_PURPLE "\033[35m"
+
+#define TDS_PERMANENT_LOG(fmt, args...) _tds_permanent_log(fmt, ##args)
+
+#define TDS_DBG(fmt, args...) \
+ SLOGD(fmt, ##args)
+#define TDS_INFO(fmt, args...) \
+ SLOGI(fmt, ##args)
+#define TDS_ERR(fmt, args...) \
+ SLOGE(fmt, ##args)
+
+#define TDS_INFO_C(fmt, arg...) \
+ SLOGI_IF(TRUE, LOG_COLOR_GREEN" "fmt" "LOG_COLOR_RESET, ##arg)
+#define TDS_ERR_C(fmt, arg...) \
+ SLOGI_IF(TRUE, LOG_COLOR_RED" "fmt" "LOG_COLOR_RESET, ##arg)
+
+#define DBG_SECURE(fmt, args...) SECURE_SLOGD(fmt, ##args)
+#define ERR_SECURE(fmt, args...) SECURE_SLOGE(fmt, ##args)
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __TDS_PROVIDER_LOG_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TDS_PROVIDER_SCANNER_H__
+#define __TDS_PROVIDER_SCANNER_H__
+
+int _tds_scanner_enable();
+int _tds_scanner_disable();
+
+#endif /* __TDS_PROVIDER_SCANNER_H__ */
#ifndef __TDS_PROVIDER_SERVICE_H__
#define __TDS_PROVIDER_SERVICE_H__
-int _tds_service_init();
-int _tds_service_deinit();
+int _tds_service_enable();
+int _tds_service_disable();
#endif /* __TDS_PROVIDER_SERVICE_H__ */
BuildRequires: pkgconfig(gio-unix-2.0)
BuildRequires: pkgconfig(capi-network-bluetooth)
BuildRequires: pkgconfig(capi-network-wifi-aware)
+BuildRequires: pkgconfig(capi-system-info)
Requires(post): /sbin/ldconfig
Requires(postun): /sbin/ldconfig
--- /dev/null
+/* <MIT License>
+ Copyright (c) 2013 Marek Majkowski <marek@popcount.org>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ </MIT License>
+
+ Original location:
+ https://github.com/majek/csiphash/
+
+ Solution inspired by code from:
+ Samuel Neves (supercop/crypto_auth/siphash24/little)
+ djb (supercop/crypto_auth/siphash24/little2)
+ Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
+*/
+
+#include <stdint.h>
+
+#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
+ __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+# define _le64toh(x) ((uint64_t)(x))
+#elif defined(_WIN32)
+/* Windows is always little endian, unless you're on xbox360
+ http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */
+# define _le64toh(x) ((uint64_t)(x))
+#elif defined(__APPLE__)
+# include <libkern/OSByteOrder.h>
+# define _le64toh(x) OSSwapLittleToHostInt64(x)
+#else
+
+/* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
+# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
+# include <sys/endian.h>
+# else
+# include <endian.h>
+# endif
+# if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
+ __BYTE_ORDER == __LITTLE_ENDIAN
+# define _le64toh(x) ((uint64_t)(x))
+# else
+# define _le64toh(x) le64toh(x)
+# endif
+
+#endif
+
+
+#define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
+
+#define HALF_ROUND(a,b,c,d,s,t) \
+ a += b; c += d; \
+ b = ROTATE(b, s) ^ a; \
+ d = ROTATE(d, t) ^ c; \
+ a = ROTATE(a, 32);
+
+#define DOUBLE_ROUND(v0,v1,v2,v3) \
+ HALF_ROUND(v0,v1,v2,v3,13,16); \
+ HALF_ROUND(v2,v1,v0,v3,17,21); \
+ HALF_ROUND(v0,v1,v2,v3,13,16); \
+ HALF_ROUND(v2,v1,v0,v3,17,21);
+
+
+uint64_t siphash24(const void *src, unsigned long src_sz, const char key[16]) {
+ const uint64_t *_key = (uint64_t *)key;
+ uint64_t k0 = _le64toh(_key[0]);
+ uint64_t k1 = _le64toh(_key[1]);
+ uint64_t b = (uint64_t)src_sz << 56;
+ const uint64_t *in = (uint64_t*)src;
+
+ uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
+ uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
+ uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
+ uint64_t v3 = k1 ^ 0x7465646279746573ULL;
+
+ while (src_sz >= 8) {
+ uint64_t mi = _le64toh(*in);
+ in += 1; src_sz -= 8;
+ v3 ^= mi;
+ DOUBLE_ROUND(v0,v1,v2,v3);
+ v0 ^= mi;
+ }
+
+ uint64_t t = 0; uint8_t *pt = (uint8_t *)&t; uint8_t *m = (uint8_t *)in;
+ switch (src_sz) {
+ case 7: pt[6] = m[6];
+ case 6: pt[5] = m[5];
+ case 5: pt[4] = m[4];
+ case 4: *((uint32_t*)&pt[0]) = *((uint32_t*)&m[0]); break;
+ case 3: pt[2] = m[2];
+ case 2: pt[1] = m[1];
+ case 1: pt[0] = m[0];
+ }
+ b |= _le64toh(t);
+
+ v3 ^= b;
+ DOUBLE_ROUND(v0,v1,v2,v3);
+ v0 ^= b; v2 ^= 0xff;
+ DOUBLE_ROUND(v0,v1,v2,v3);
+ DOUBLE_ROUND(v0,v1,v2,v3);
+ return (v0 ^ v1) ^ (v2 ^ v3);
+}
--- /dev/null
+uint64_t siphash24(const void *src, unsigned long src_sz, const char key[16]);
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <dlog.h>
+#include <bluetooth.h>
+#include <bluetooth_internal.h>
+#include "csiphash/csiphash.h"
+
+uint64_t siphash24(const void *src, unsigned long src_sz, const char key[16]);
+
+#define kTDSSipHashKey ((const uint8_t *) \
+ "\x00\x00\x00\x00\x00\x00\x00\x00" \
+ "\x00\x00\x00\x00\x00\x00\x00\x01" \
+)
+#define kTDSSipHashCount 4
+
+void _tds_bloom_filter_add_string(uint8_t *inFilterPtr, size_t inFilterLen, const char *inStr, size_t inLen)
+{
+ const size_t bitCount = inFilterLen * 8;
+ uint64_t hash = siphash24(inStr, inLen, (char *)kTDSSipHashKey);
+ for (uint8_t i = 0; i < kTDSSipHashCount; i++) {
+ const size_t idx = hash % bitCount;
+ inFilterPtr[idx / 8] |= (1 << (7 - (idx & 7)));
+ hash /= bitCount;
+ }
+}
+
+bool _tds_bloom_filter_contain_hash(const uint8_t *inFilterPtr, size_t inFilterLen, uint64_t inHash)
+{
+ const size_t bitCount = inFilterLen * 8;
+ for (uint8_t i = 0; i < kTDSSipHashCount; ++i) {
+ const size_t idx = inHash % bitCount;
+ const uint8_t on = inFilterPtr[idx / 8] & (1 << (7 - (idx & 7)));
+ if (!on)
+ return false;
+ inHash /= bitCount;
+ }
+ return true;
+}
+
+bool _tds_bloom_filter_contain_string(const uint8_t *inFilterPtr, size_t inFilterLen, const char *inStr, size_t inLen)
+{
+ uint64_t hash = siphash24(inStr, inLen, (char *)kTDSSipHashKey); // param sequence check
+ return _tds_bloom_filter_contain_hash(inFilterPtr, inFilterLen, hash);
+}
+
+int _bt_tds_start_scan(bt_tds_provider_scan_result_cb cb, void *user_data)
+{
+ /* TODO: need to use le_start_scan instead of seeking_provider */
+ return bt_tds_start_seeking_providers(cb, user_data);
+}
+
+int _bt_tds_stop_scan()
+{
+ return bt_tds_stop_seeking_providers();
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <dlog.h>
+#include <bluetooth.h>
+#include <bluetooth_internal.h>
+#include "tds-provider-common.h"
+#include "tds-provider-log.h"
+#include "tds-provider-service.h"
+#include "tds-api.h"
+
+static bt_advertiser_h advertiser;
+static gboolean g_adv_started;
+static tds_transport_state_e g_transport_state;
+
+static void __tds_advertiser_adv_state_changed_cb(int result,
+ bt_advertiser_h advertiser, bt_adapter_le_advertising_state_e adv_state,
+ void *user_data)
+{
+ TDS_INFO("Result: %d, Advertising %s", result,
+ adv_state == BT_ADAPTER_LE_ADVERTISING_STARTED ? "started" : "stopped");
+ g_adv_started = TRUE;
+}
+
+int _tds_advertiser_enable(tds_transport_state_e transport_state)
+{
+ int ret;
+ unsigned char transport_data[20] = {0, };
+ char tds_flags = 0x02; /* Provider Only. Table 3.3: TDS Flags filed, TDS Specification */
+
+ if (g_adv_started && g_transport_state == transport_state) {
+ TDS_INFO("Advertising already in progress. transport_state: %d", g_transport_state);
+ return TDS_ERROR_NONE;
+ }
+
+ ret = bt_adapter_le_create_advertiser(&advertiser);
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_adapter_le_create_advertiser() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ TDS_INFO("Create bloom filter");
+ _tds_bloom_filter_add_string(transport_data, sizeof(transport_data),
+ TDS_BLOOM_FILTER_OPERATION_STR_PROVIDER, strlen(TDS_BLOOM_FILTER_OPERATION_STR_PROVIDER));
+ /* TODO: actually, below strings are not required. need to check that keep or not */
+ _tds_bloom_filter_add_string(transport_data, sizeof(transport_data),
+ "_ipp._tcp", strlen("_ipp._tcp"));
+ _tds_bloom_filter_add_string(transport_data, sizeof(transport_data),
+ "nan", strlen("nan"));
+ for (int i = 0; i < sizeof(transport_data); i++)
+ TDS_DBG("transport_data[%d]: 0x%02x", i, transport_data[i]);
+
+ if (transport_state == TDS_TRANSPORT_STATE_ON)
+ tds_flags |= 0x08; /* Transport State On. Table 3.3: TDS Flags filed, TDS Specification */
+
+ ret = bt_adapter_le_set_advertising_transport_discovery_data(advertiser,
+ BT_ADAPTER_LE_PACKET_ADVERTISING,
+ BT_TDS_TRANSPORT_WIFI_NAN,
+ tds_flags, transport_data, sizeof(transport_data));
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_adapter_le_set_advertising_transport_discovery_data() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = bt_adapter_le_start_advertising_new(advertiser, __tds_advertiser_adv_state_changed_cb, NULL);
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_adapter_le_start_advertising_new() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ g_transport_state = transport_state;
+ return TDS_ERROR_NONE;
+}
+
+int _tds_advertiser_disable()
+{
+ int ret;
+ TDS_DBG("Enter");
+
+ if (!g_adv_started)
+ return TDS_ERROR_NONE;
+
+ ret = bt_adapter_le_stop_advertising(advertiser);
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_adapter_le_stop_advertising() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = bt_adapter_le_destroy_advertiser(advertiser);
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_tds_provider_unregister() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ g_adv_started = FALSE;
+ return TDS_ERROR_NONE;
+}
#include <dlog.h>
#include <wifi-aware.h>
#include "tds-provider-common.h"
+#include "tds-provider-log.h"
-int _tds_aware_init()
+int _tds_aware_enable()
{
- TDS_DBG("enter");
- return 0;
+ TDS_DBG("Enter");
+ return TDS_ERROR_NONE;
}
-int _tds_aware_deinit()
+int _tds_aware_disable()
{
- return 0;
+ TDS_DBG("Enter");
+ return TDS_ERROR_NONE;
}
#include <glib.h>
#include <gio/gio.h>
#include <dlog.h>
+#include <system_info.h>
#include "tds-provider-common.h"
+#include "tds-provider-log.h"
#include "tds-provider-service.h"
-#include "tds-provider-aware.h"
static GMainLoop *main_loop = NULL;
int main()
{
- /* Check aware supported */
- _tds_aware_init();
+ int ret;
+ bool le_supported, aware_supported;
- /* Check aware status*/
+ ret = system_info_get_platform_bool("http://tizen.org/feature/network.bluetooth.le", &le_supported);
+ if (ret != SYSTEM_INFO_ERROR_NONE) {
+ TDS_ERR("system_info_get_platform_bool() failed. ret: %d", ret);
+ return EXIT_FAILURE;
+ }
+
+/* network.wifi.aware feature not added in model-config yet */
+#if 0
+ ret = system_info_get_platform_bool("http://tizen.org/feature/network.wifi.aware", &aware_supported);
+ if (ret != SYSTEM_INFO_ERROR_NONE) {
+ TDS_ERR("system_info_get_platform_bool() failed. ret: %d", ret);
+ return EXIT_FAILURE;
+ }
+#else
+ aware_supported = TRUE;
+#endif
+
+ if (!le_supported || !aware_supported) {
+ TDS_INFO("feature not supported. le: %d, aware: %d", le_supported, aware_supported);
+ return EXIT_SUCCESS;
+ }
- /* Start tds provider advertising */
- _tds_service_init();
+ ret = _tds_service_enable();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("_tds_service_enable() failed. ret: %d", ret);
+ return EXIT_FAILURE;
+ }
main_loop = g_main_loop_new(NULL, FALSE);
if (main_loop == NULL) {
- TDS_ERR("Creating main loop failed");
+ TDS_ERR("g_main_loop_new() failed.");
return EXIT_FAILURE;
}
g_main_loop_run(main_loop);
+ ret = _tds_service_disable();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("_tds_service_disable() failed. ret: %d", ret);
+ return EXIT_FAILURE;
+ }
+
if (main_loop != NULL)
g_main_loop_unref(main_loop);
- return 0;
+ return EXIT_SUCCESS;
}
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <dlog.h>
+#include <bluetooth.h>
+#include <bluetooth_internal.h>
+#include "tds-provider-common.h"
+#include "tds-provider-log.h"
+#include "tds-provider-service.h"
+
+int _tds_scanner_enable()
+{
+ TDS_DBG("Enter");
+ return TDS_ERROR_NONE;
+}
+
+int _tds_scanner_disable()
+{
+ TDS_DBG("Enter");
+ return TDS_ERROR_NONE;
+}
#include <bluetooth.h>
#include <bluetooth_internal.h>
#include "tds-provider-common.h"
+#include "tds-provider-log.h"
+#include "tds-provider-service.h"
+#include "tds-provider-scanner.h"
+#include "tds-provider-advertiser.h"
+#include "tds-provider-aware.h"
+#include "tds-api.h"
-int _tds_service_init()
+static int __tds_service_start()
{
- TDS_DBG("enter");
- return 0;
+ int ret;
+
+ ret = _tds_scanner_enable();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("_tds_scanner_enable() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = _tds_advertiser_enable(TDS_TRANSPORT_STATE_OFF);
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("_tds_advertiser_enable() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ return TDS_ERROR_NONE;
+}
+
+static int __tds_service_stop()
+{
+ int ret;
+
+ ret = _tds_scanner_disable();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("_tds_scanner_disable() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = _tds_advertiser_disable();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("_tds_advertiser_disable() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ return TDS_ERROR_NONE;
+}
+
+static void __adapter_state_changed_cb(int result,
+ bt_adapter_state_e adapter_state, void *user_data)
+{
+ int ret;
+
+ if (result != BT_ERROR_NONE) {
+ TDS_ERR("__adapter_state_changed_cb() failed. result: %d", result);
+ return;
+ }
+
+ if (adapter_state == BT_ADAPTER_ENABLED) {
+ TDS_INFO("Adapter enabled. Start service");
+ ret = __tds_service_start();
+ if (ret != TDS_ERROR_NONE)
+ TDS_ERR("__tds_service_start() failed. ret: %d", ret);
+ } else {
+ TDS_INFO("Adapter disabled. Stop service");
+ ret = __tds_service_stop();
+ if (ret != TDS_ERROR_NONE)
+ TDS_ERR("__tds_service_stop() failed. ret: %d", ret);
+ }
}
-int _tds_service_deinit()
+int _tds_service_enable()
{
- return 0;
+ int ret;
+ bt_adapter_state_e adapter_state;
+ TDS_DBG("Enter");
+
+ ret = bt_initialize();
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_initialize() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = bt_adapter_get_state(&adapter_state);
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_adapter_get_state() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = bt_adapter_set_state_changed_cb(__adapter_state_changed_cb, NULL);
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_adapter_set_state_changed_cb() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ if (adapter_state == BT_ADAPTER_ENABLED) {
+ TDS_INFO("Start service");
+ ret = __tds_service_start();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("__tds_service_start() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+ }
+
+ return TDS_ERROR_NONE;
+}
+
+int _tds_service_disable()
+{
+ int ret;
+ TDS_DBG("Enter");
+
+ ret = bt_adapter_unset_state_changed_cb();
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_deinitialize() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = __tds_service_stop();
+ if (ret != TDS_ERROR_NONE) {
+ TDS_ERR("__tds_service_stop() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ ret = bt_deinitialize();
+ if (ret != BT_ERROR_NONE) {
+ TDS_ERR("bt_deinitialize() failed. ret: %d", ret);
+ return TDS_ERROR_INTERNAL;
+ }
+
+ return TDS_ERROR_NONE;
}