From 867834c077dd83dbc567be3f89dfe9e8b721cb4e Mon Sep 17 00:00:00 2001 From: wanglei744 Date: Tue, 13 Sep 2022 15:56:01 +0800 Subject: [PATCH] fix softbus_bus_center API notes Signed-off-by: wanglei744 --- .../softbus_bus_center/devicejoinlnn_demo.c | 76 ++ .../softbus_bus_center/deviceleavelnn_demo.c | 54 ++ .../softbus_bus_center/devicepublish_demo.c | 103 +++ .../softbus_bus_center/metanode_demo.c | 62 ++ .../softbus_bus_center/shiftlnngear_demo.c | 46 ++ .../softbus_bus_center/timesync_demo.c | 58 ++ .../native_sdk/dsoftbus/softbus_bus_center.h | 759 ++++++++++++++++++ 7 files changed, 1158 insertions(+) create mode 100644 zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicejoinlnn_demo.c create mode 100644 zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/deviceleavelnn_demo.c create mode 100644 zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicepublish_demo.c create mode 100644 zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/metanode_demo.c create mode 100644 zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/shiftlnngear_demo.c create mode 100644 zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/timesync_demo.c create mode 100644 zh-cn/native_sdk/dsoftbus/softbus_bus_center.h diff --git a/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicejoinlnn_demo.c b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicejoinlnn_demo.c new file mode 100644 index 00000000..526dfc3f --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicejoinlnn_demo.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @file devicejoinlnn_demo.c + * + * @brief 设备组网的demo演示 + * + * @since 1.0 + * @version 1.0 + */ + +#include "softbus_bus_center.h" +#include "softbus_common.h" +#include + +// 设备A和设备B无可信关系 +// 设备A向软总线注册组网回调 +static void OnJoinLNNDone(const ConnectionAddr *addr, const char *networkId, int retCode) +{ + if (addr == NULL) { + printf("[demo]OnJoinLNNDone error\n"); + return; + } + if (retCode == 0) { + printf("[demo]OnJoinLNNDone enter networdid = %s, retCode = %d ip = %s port = %d\r\n", + networkId, retCode, addr->info.ip.ip, addr->info.ip.port); + } else { + printf("[demo]OnJoinLNNDone failed! networdid = %s, retCode = %d\r\n", networkId, retCode); + } +} + +// 设备A调用JoinLNN接口入网 +static int DeviceAMain(void) +{ + const char *pkgName = "pkgName.demo"; + ConnectionAddr addr = { + .type = CONNECTION_ADDR_WLAN, + .peerUid = "0123456789987654321001234567899876543210012345678998765432100123", + .info.ip.ip = "192.168.0.1", + .info.ip.port = 1000, + }; + const char *sessionName = "sessionName.demo"; + // 1.设备A会根据会话名称和连接信息在组网前打开认证通道进行身份协商 + int sessionId = OpenAuthSession(sessionName, &addr, 1, NULL); + + // 2.当成功打开身份认证会话时,通过NotifyAuthSuccess 通知设备认证成功 + NotifyAuthSuccess(sessionId); + + // 3.认证成功后,设备A调用JoinLNN入网,通过OnJoinLNNDone来返回调用接口的结果 + if (JoinLNN(pkgName, &addr, OnJoinLNNDone) != 0) { + printf("[demo]JoinLNN error!"); + return -1; + } + return 0; + // 4.会调用CompleteJoinLNN 完成上线通知 +} + +// 设备B +static int DeviceBMain(void) +{ + // 1.设备B 在认证通道打开之前,会打开AuthPort,来建立认证通道。 + // 2.设备B 同样会调用CompleteJoinLNN,完成组网上报通知。 +} \ No newline at end of file diff --git a/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/deviceleavelnn_demo.c b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/deviceleavelnn_demo.c new file mode 100644 index 00000000..84502c09 --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/deviceleavelnn_demo.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @file deviceleavelnn_demo.c + * + * @brief 设备退网的demo演示 + * + * @since 1.0 + * @version 1.0 + */ + +#include "softbus_bus_center.h" +#include "softbus_common.h" +#include + + +static void OnLeaveLNNDone(const char *networkId, int32_t retCode) +{ + (void)networkId; + (void)retCode; +} + +// 设备A +static int DeviceAMain(void) +{ + const char *pkgName = "pkgName.demo"; + char networkId[] = "0123456789987654321001234567899876543210012345678998765432100123"; + // 1.设备A调用LeaveLNN退网 + if (LeaveLNN(pkgName, networkId, OnLeaveLNNDone) != 0) { + printf("[demo]LeaveLNN error!"); + return -1; + } + // 2.设备A调用CompleteLeaveLNN 完成退网通知 + return 0; +} + +// 设备B +static int DeviceBMain(void) +{ + // 1.设备A调用LeaveLNN退网之后,通过状态机的判断,设备B会执行CompleteLeaveLNN +} \ No newline at end of file diff --git a/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicepublish_demo.c b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicepublish_demo.c new file mode 100644 index 00000000..257de154 --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/devicepublish_demo.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @file devicepublish_demo.c + * + * @brief 设备发布服务的demo演示 + * + * @since 1.0 + * @version 1.0 + */ + +#include "discovery_service.h" +#include "softbus_bus_center.h" +#include "softbus_common.h" +#include + + +static void TestPublishResult(int publishId, PublishResult reason) +{ + return; +} + +static IPublishCb g_publishCb = { + .OnPublishResult = TestPublishResult, +}; + +static PublishInfo g_pInfo = { + .publishId = 1, /* 发布ID */ + .mode = DISCOVER_MODE_ACTIVE, /* 发布选择主动发布 */ + .medium = COAP, /* 发布媒介:COAP发布 */ + .freq = MID, /* 发布频次:中等 */ + .capability = "dvKit", /* 发布能力 */ + .capabilityData = (unsigned char *)"capdata4", /* 发布能力数据 */ + .dataLen = sizeof("capdata4") /* 发布能力数据长长度 */ +}; + +static SubscribeInfo g_sInfo1 = { + .subscribeId = 1, + .medium = COAP, + .mode = DISCOVER_MODE_PASSIVE, + .freq = MID, + .capability = "ddmpCapability", + .capabilityData = (unsigned char *)"capdata3", + .dataLen = sizeof("capdata3"), + .isSameAccount = true, + .isWakeRemote = false +}; + +static void TestDeviceFound(const DeviceInfo *device) +{ + printf("[demo]TestDeviceFound\n"); +} + +static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason) +{ + printf("[demo]TestDiscoverFailed, subscribeId = %d, failReason = %d\n", subscribeId, failReason); +} + +static void TestDiscoverySuccess(int subscribeId) +{ + printf("[demo]TestDiscoverySuccess, subscribeId = %d\n", subscribeId); + +static IDiscoveryCallback g_subscribeCb = { + .OnDeviceFound = TestDeviceFound, + .OnDiscoverFailed = TestDiscoverFailed, + .OnDiscoverySuccess = TestDiscoverySuccess +}; + +/* 场景:设备A为发现方,设备B为被发现方, + * 设备B启动后调用PublishLNN服务发布。 + * 设备A通过调用StartDiscovery触发监听 + */ + +/* 设备A通过调用StartDiscovery触发广播监听,发现周围设备 */ +static void DeviceAStartDiscovery(void) +{ + const char *pkgNameA = "pkgName.demo1"; + (void)StartDiscovery(pkgNameA, &g_sInfo1, &g_subscribeCb); +} + +/* 设备B通过调用PublishLNN进行广播 */ +int main(void) +{ + const char *pkgName = "pkgName.demo"; + if (PublishLNN(pkgName, &g_pInfo, &g_publishCb) != 0) { + printf("[demo] PublishLNN fail\n"); + return -1; + } + return 0; +} diff --git a/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/metanode_demo.c b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/metanode_demo.c new file mode 100644 index 00000000..1b3cce5c --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/metanode_demo.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @file metanode_demo.c + * + * @brief 元节点的demo演示 + * + * @since 1.0 + * @version 1.0 + */ + +#include "softbus_bus_center.h" +#include "softbus_common.h" +#include + +int main(void) +{ + const char *pkgName = "pkgName.demo"; + char udid[] = "0123456789987654321001234567899876543210012345678998765432100123"; + char metaNodeId[NETWORK_ID_BUF_LEN] = {0}; + MetaNodeInfo infos[MAX_META_NODE_NUM]; + int32_t infoNum = MAX_META_NODE_NUM; + MetaNodeConfigInfo configInfo; + configInfo.addrNum = 1; + + if (strncpy_s(configInfo.udid, UDID_BUF_LEN, udid, UDID_BUF_LEN) != 0) { + printf("udid copy fail\n"); + return -1; + } + + // 元节点激活 + if (ActiveMetaNode(pkgName, &configInfo, metaNodeId) != 0) { + printf("[demo] ActiveMetaNode fail\n"); + return -1; + } + + // 获取所有元节点信息 + if (GetAllMetaNodeInfo(pkgName, infos, &infoNum) != 0) { + printf("[demo] GetAllMetaNodeInfo fail\n"); + return -1; + } + + // 释放元节点 + if (DeactiveMetaNode(pkgName, metaNodeId) != 0) { + printf("DeactiveMetaNode fail\n"); + return -1; + } + return 0; +} \ No newline at end of file diff --git a/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/shiftlnngear_demo.c b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/shiftlnngear_demo.c new file mode 100644 index 00000000..4d505afe --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/shiftlnngear_demo.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @file shiftlnngear_demo.c + * + * @brief 调整心跳的demo演示 + * + * @since 1.0 + * @version 1.0 + */ + +#include "softbus_bus_center.h" +#include "softbus_common.h" +#include + +int main(void) +{ + const char *pkgName = "pkgName.demo"; + const char *callerId = "1"; + const char *networkId = NULL; + GearMode mode = { + .cycle = MID_FREQ_CYCLE, + .duration = DEFAULT_DURATION, + .wakeupFlag = false, + }; + + // 调整心跳 + if (ShiftLNNGear(pkgName, callerId, networkId, &mode) != 0) { + printf("ShiftLNNGear fail\n"); + return -1; + } + return 0; +} \ No newline at end of file diff --git a/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/timesync_demo.c b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/timesync_demo.c new file mode 100644 index 00000000..8d4481c8 --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/example/softbus_bus_center/timesync_demo.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @file timesync_demo.c + * + * @brief 时间同步的demo演示 + * + * @since 1.0 + * @version 1.0 + */ + +#include "softbus_bus_center.h" +#include "softbus_common.h" +#include + +static void OnTimeSyncResult(const TimeSyncResultInfo *info, int32_t retCode) +{ + (void)info; + (void)retCode; +} + +static ITimeSyncCb g_timeSyncCb = { + .onTimeSyncResult = OnTimeSyncResult, +}; + +int main(void) +{ + const char *pkgName = "pkgName.demo"; + char networkId[] = "0123456789987654321001234567899876543210012345678998765432100123"; + TimeSyncAccuracy timeAccuracy = SUPER_HIGH_ACCURACY; + TimeSyncPeriod period = NORMAL_PERIOD; + + // 时间同步 + if (StartTimeSync(pkgName, networkId, timeAccuracy, period, &g_timeSyncCb) != 0) { + printf("[demo] StartTimeSync fail\n"); + return -1; + } + + // 停止时间同步 + if (StopTimeSync(pkgName, networkId) != 0) { + printf("[demo] StopTimeSync fail\n"); + return -1; + } + return 0; +} \ No newline at end of file diff --git a/zh-cn/native_sdk/dsoftbus/softbus_bus_center.h b/zh-cn/native_sdk/dsoftbus/softbus_bus_center.h new file mode 100644 index 00000000..d11516ca --- /dev/null +++ b/zh-cn/native_sdk/dsoftbus/softbus_bus_center.h @@ -0,0 +1,759 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @addtogroup Softbus + * @{ + * + * @brief Provides high-speed, secure communication between devices. + * + * This module implements unified distributed communication capability management between nearby devices, and provides + * link-independent device discovery and transmission interfaces to support service publishing and data transmission. + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file softbus_bus_center.h + * + * @brief Declares functions and constants for the bus center of the Intelligent Soft Bus. + * + * The functions are used to perform + * the following operations: \n + *
    + *
  • Adding a device to and removing a device from a LNN
  • + *
  • Listening for device online, offline, and information change events
  • + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef SOFTBUS_BUS_CENTER_H +#define SOFTBUS_BUS_CENTER_H + +#include +#include + +#include "softbus_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Indicates the length of a device name buffer, including the terminating null character \0. + * + * @since 1.0 + * @version 1.0 + */ +#define DEVICE_NAME_BUF_LEN 128 + +/** + * @brief Indicates the mask bit for a device online event. + * If you want to receive such events, set the mask bit in {@link INodeStateCb.events}. + * + * @since 1.0 + * @version 1.0 + */ +#define EVENT_NODE_STATE_ONLINE 0x1 + +/** + * @brief Indicates the mask bit for a device offline event. + * If you want to receive such events, set the mask bit in {@link INodeStateCb.events}. + * + * @since 1.0 + * @version 1.0 + */ +#define EVENT_NODE_STATE_OFFLINE 0x02 + +/** + * @brief Indicates the mask bit for a peer device information change event. + * If you want to receive such events, set the mask bit in {@link INodeStateCb.events}. + * + * @since 1.0 + * @version 1.0 + */ +#define EVENT_NODE_STATE_INFO_CHANGED 0x04 + +/** + * @brief Indicates mask bits for event state {@link INodeStateCb.events}. + * + * @since 1.0 + * @version 1.0 + */ +#define EVENT_NODE_STATE_MASK 0x07 + +/** + * @brief The maximum length of meta node bypass info {@link MetaNodeConfigInfo.bypassInfo}. + * + * @since 1.0 + * @version 1.0 + */ +#define META_NODE_BYPASS_INFO_LEN 64 + +/** + * @brief Indicates the maximum length of the callerId, including the terminating null character \0. + * + * @since 1.0 + * @version 1.0 + */ +#define CALLER_ID_MAX_LEN 128 + +/** + * @brief The maximum of meta node {@link MetaNodeConfigInfo.bypassInfo}. + * + * @since 1.0 + * @version 1.0 + */ +#define MAX_META_NODE_NUM 3 + +/** + * @brief Enumerates keys for an online device. + * The key can be obtained via {@link GetNodeKeyInfo}. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + NODE_KEY_UDID = 0, /**< UDID in string format*/ + NODE_KEY_UUID, /**< UUID in string format */ + NODE_KEY_MASTER_UDID, /**< UDID of master node in string format */ + NODE_KEY_BR_MAC, /**< BR MAC in string format */ + NODE_KEY_IP_ADDRESS, /**< IP address in string format */ + NODE_KEY_DEV_NAME, /**< Device name in string format */ + NODE_KEY_NETWORK_CAPABILITY, /**< Network capability in number format */ + NODE_KEY_NETWORK_TYPE, /**< Network type in number format */ +} NodeDeviceInfoKey; + +/** + * @brief Enumerates supported network capabilities. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + BIT_BLE = 0, /**< Support BLE */ + BIT_BR, /**< Support BR */ + BIT_WIFI, /**< Support WIFI */ + BIT_WIFI_P2P, /**< Support WIFI P2P */ + BIT_WIFI_24G, /**< Support WIFI 2.4G */ + BIT_WIFI_5G, /**< Support WIFI 5G */ + BIT_ETH, /**< Support ETH */ + BIT_COUNT, /**< Invalid type */ +} NetCapability; + +/** + * @brief Enumerates network types for an online device. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + BIT_NETWORK_TYPE_UNKNOWN = 0, /**< Unknown network type */ + BIT_NETWORK_TYPE_WIFI, /**< WIFI network type */ + BIT_NETWORK_TYPE_BLE, /**< BLE network type */ + BIT_NETWORK_TYPE_BR, /**< BR network type */ + BIT_NETWORK_TYPE_P2P, /**< P2P network type */ + BIT_NETWORK_TYPE_COUNT, /**< Invalid type */ +} NetworkType; + +/** + * @brief Enumerates accuracy for time synchronize among device. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + LOW_ACCURACY = 10, /**< 时间同步的精确度设置:低精确度 */ + NORMAL_ACCURACY, /**< 时间同步的精确度设置:一般精确度 */ + HIGH_ACCURACY, /**< 时间同步的精确度设置:高精确度 */ + SUPER_HIGH_ACCURACY, /**< 时间同步的精确度设置:超高精确度 */ + UNAVAIL_ACCURACY = 0xFFFF, /**< 时间同步的精确度设置:不可用的精确度 */ +} TimeSyncAccuracy; + +/** + * @brief Enumerates time synchronize period among device. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + SHORT_PERIOD = 0, /**< 时间同步的周期:短周期 */ + NORMAL_PERIOD, /**< 时间同步的周期:一般周期 */ + LONG_PERIOD, /**< 时间同步的周期:长周期 */ +} TimeSyncPeriod; + +/** + * @brief Enumerates time synchronize flag. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + NODE_SPECIFIC = 0, /**< 特定模式下同步 */ + ALL_LNN, /**< 整个LNN间同步 */ + WRITE_RTC, /**< 写入RTC同步 */ +} TimeSyncFlag; + +/** + * @brief Defines the basic information about a device. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + char networkId[NETWORK_ID_BUF_LEN]; /**< Device ID */ + char deviceName[DEVICE_NAME_BUF_LEN]; /**< Device name */ + uint16_t deviceTypeId; /**< Device type ID */ +} NodeBasicInfo; + +/** + * @brief Enumerates device information change types. For details, see {@link INodeStateCb.onNodeBasicInfoChanged}. + * + * @since 1.0 + * @version 1.0 + */ +typedef enum { + TYPE_NETWORK_ID = 0, /**< Network ID change */ + TYPE_DEVICE_NAME, /**< Device name change */ +} NodeBasicInfoType; + +/** + * @brief time synchronize result. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + int32_t millisecond; /**< 时间同步结果的毫秒表示 */ + int32_t microsecond; /**< 时间同步结果的微秒表示 */ + TimeSyncAccuracy accuracy; /**< 时间同步结果的精确度 */ +} TimeSyncResult; + +/** + * @brief time synchronize result info. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + TimeSyncResult result; /**< 时间同步结果 */ + TimeSyncFlag flag; /**< 时间同步标志 */ + union { + char targetNetworkId[NETWORK_ID_BUF_LEN]; /**< 目的节点ID */ + char masterNetworkId[NETWORK_ID_BUF_LEN]; /**< 主节点ID */ + } target; +} TimeSyncResultInfo; + +/** + * @brief Enumerates error codes for service publishing failures. + * + * The error codes are returned to the caller through IPublishCallback. + * + */ +typedef enum { + /* publish success */ + PUBLISH_LNN_SUCCESS = 0, + /* Unsupported medium */ + PUBLISH_LNN_NOT_SUPPORT_MEDIUM = 1, + /* internal error */ + PUBLISH_LNN_INTERNAL = 2, + /* Unknown reason */ + PUBLISH_LNN_UNKNOWN = 0xFF +} PublishResult; + +/** + * @brief Enumerates error codes for service subscription failures. + * + * The error codes are returned to the caller through IDiscoveryCallback. + * + */ +typedef enum { + /* refresh success */ + REFRESH_LNN_SUCCESS = 0, + /* Unsupported medium */ + REFRESH_LNN_NOT_SUPPORT_MEDIUM = 1, + /* internal error */ + REFRESH_LNN_INTERNAL = 2, + /* Unknown error */ + REFRESH_LNN_UNKNOWN = 0xFF +} RefreshResult; + +/** + * @brief Defines the callbacks for successful and failed service publishing. + * + */ +typedef struct { + /** Callback for publish result */ + void (*OnPublishResult)(int publishId, PublishResult reason); +} IPublishCb; + +/** + * @brief Defines a callback for service subscription. + * + * Three types of callbacks are available. + * + */ +typedef struct { + /** Callback that is invoked when a device is found */ + void (*OnDeviceFound)(const DeviceInfo *device); + /** Callback for a subscription result */ + void (*OnDiscoverResult)(int32_t refreshId, RefreshResult reason); +} IRefreshCallback; + +/** + * @brief Defines a callback that is invoked when the device state or information changes. + * For details, see {@link RegNodeDeviceStateCb}. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + /** + * Mask of an event that is listened for. + * The event can be received only if the corresponding mask bit is set and the callback is specified. + */ + uint32_t events; + /** + * @brief Called when a specified device, rather than a specified ability, gets online. + * + * @param info Indicates the pointer to the basic information about the device. + * For details, see {@link NodeBasicInfo}. + * + * @since 1.0 + * @version 1.0 + */ + void (*onNodeOnline)(NodeBasicInfo *info); + /** + * @brief Called when a specified device, rather than a specified ability, gets offline. + * + * @param info Indicates the pointer to the basic information about the device. + * For details, see {@link NodeBasicInfo}. + * + * @since 1.0 + * @version 1.0 + */ + void (*onNodeOffline)(NodeBasicInfo *info); + /** + * @brief Called when the basic information of a device changes. + * + * @param type Indicates the device type. For details, see {@link NodeBasicInfoType}. + * @param info Indicates the pointer to the new basic information of the device. + * For details, see {@link NodeBasicInfo}. + * + * @since 1.0 + * @version 1.0 + */ + void (*onNodeBasicInfoChanged)(NodeBasicInfoType type, NodeBasicInfo *info); +} INodeStateCb; + +/** + * @brief Defines a callback that is invoked when the time synchronize has result. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + /** + * @brief Called when the time synchronize has result. + * + * @param info Contains the time synchronize result info, see {@link TimeSyncResultInfo}. + * @param retCode Indicates the result code. Value 0 indicates that the time synchronize is successful and + * result is valid, and any other value indicates the opposite. + * + * @since 1.0 + * @version 1.0 + */ + void (*onTimeSyncResult)(const TimeSyncResultInfo *info, int32_t retCode); +} ITimeSyncCb; + +/** + * @brief Defines a meta node configuration, see {@link ActiveMetaNode}. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + /** 元节点的唯一标识,取值为元节点设备的udid */ + char udid[UDID_BUF_LEN]; + /** 元节点的设备名称 */ + char deviceName[DEVICE_NAME_BUF_LEN]; + /** 元节点的设备类型 */ + uint16_t deviceTypeId; + /** 元节点的连接地址的数量,可以为零 + * 如果为零,表示只加入可信列表,通过其他手段检测是否上线 + */ + uint8_t addrNum; + /** 元节点的连接地址 */ + ConnectionAddr addr[CONNECTION_ADDR_MAX]; + /** 元节点携带的额外数据,软总线仅负责透传,不解析 */ + char bypassInfo[META_NODE_BYPASS_INFO_LEN]; +} MetaNodeConfigInfo; + +/** + * @brief Defines a meta node infomation, see {@link GetAllMetaNodeInfo}. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + /** 元节点的唯一标识,内存由使用者分配,长度不小于NETWORK_ID_BUF_LEN */ + char metaNodeId[NETWORK_ID_BUF_LEN]; + /** 元节点是否在线标志 */ + bool isOnline; + /** 元节点的配置信息 */ + MetaNodeConfigInfo configInfo; +} MetaNodeInfo; + +/** + * @brief Defines heartbeat mode parameter, see {@link GearMode}. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + ModeCycle cycle; /**< Heartbeat mode cycle */ + ModeDuration duration; /**< Heartbeat mode duration */ + bool wakeupFlag; /**< Heartbeat wakeup peer device or not */ +} GearMode; + +/** + * @brief Called when a device is added to a LNN via {@link JoinLNN}. + * + * @param addr Indicates the pointer to the address of the peer device. + * @param networkId Indicates the pointer to the network ID of the device if it is successfully added to the LNN. + * This parameter makes no sense if the device fails to be added to the LNN. + * @param retCode Indicates the result code. Value 0 indicates that the device is successfully added to the LNN, + * and any other value indicates the opposite. + * + * @since 1.0 + * @version 1.0 + */ +typedef void (*OnJoinLNNResult)(ConnectionAddr *addr, const char *networkId, int32_t retCode); + +/** + * @brief Called when a device is removed from a LNN via {@link LeaveLNN}. + * + * @param networkId Indicates the pointer to the network ID of the device. + * @param retCode Indicates the result code. Value 0 indicates that the device is successfully + * removed from the LNN, and any other value indicates the opposite. + * + * @since 1.0 + * @version 1.0 + */ +typedef void (*OnLeaveLNNResult)(const char *networkId, int32_t retCode); + +/** + * @example devicejoinlnn_demo.c +*/ + +/** + * @brief Adds the current device to the LNN where a specified device resides. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param target Indicates the pointer to the address of the specified device. For details, see {@link ConnectionAddr}. + * @param cb Indicates the callback for the result. If you set this parameter to NULL, you will not receive the result. + * + * @return Returns 0 if the request to add the device is accepted, and the result can be obtained from the + * callback; returns any other value if the device fails to be added to the network, in which case you will not receive + * the result. + * + * @since 1.0 + * @version 1.0 + */ +int32_t JoinLNN(const char *pkgName, ConnectionAddr *target, OnJoinLNNResult cb); + +/** + * @example deviceleavelnn_demo.c +*/ + +/** + * @brief Removes the current device from the LNN. + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param networkId Indicates the pointer to the network ID that is returned + * after the device is added to the LNN via {@link JoinLNN}. + * @param cb Indicates the callback for the result. If you set this parameter to NULL, + * you will not receive the result. + * + * @return Returns 0 if the request to remove the device is accepted, and the result can be obtained from the + * callback; returns any other value if the device fails to be removed from the network, in which case you will not + * receive the result. + * + * @since 1.0 + * @version 1.0 + */ +int32_t LeaveLNN(const char *pkgName, const char *networkId, OnLeaveLNNResult cb); + +/** + * @brief Registers a callback for device state changes, rather than a specified ability, gets online. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param callback Indicates the pointer to the callback to register. For details, see {@link INodeStateCb}. + * + * @return Returns 0 if the registration is successful; returns any other value otherwise. + * + * @since 1.0 + * @version 1.0 + */ +int32_t RegNodeDeviceStateCb(const char *pkgName, INodeStateCb *callback); + +/** + * @brief Unregisters a callback for device state changes. + * + * @param callback Indicates the pointer to the callback to unregister. For details, see {@link INodeStateCb}. + * + * @return Returns 0 if the unregistration is successful; returns any other value otherwise. + * + * @since 1.0 + * @version 1.0 + */ +int32_t UnregNodeDeviceStateCb(INodeStateCb *callback); + +/** + * @brief Obtains basic information about all the online devices. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param info Indicates the double pointer to the memory that stores the obtained basic information. + * @param infoNum Indicates the pointer to the number of devices. + * + * @return Returns 0 if the basic information is obtained, in which case info and infoNum are + * valid; returns any other value otherwise. + * + * @since 1.0 + * @version 1.0 + */ +int32_t GetAllNodeDeviceInfo(const char *pkgName, NodeBasicInfo **info, int32_t *infoNum); + +/** + * @brief Releases the memory returned by {@link GetAllNodeDeviceInfo}. + * + * @param info Indicates the pointer to the memory returned by {@link GetAllNodeDeviceInfo}. + * + * @since 1.0 + * @version 1.0 + */ +void FreeNodeInfo(NodeBasicInfo *info); + +/** + * @brief Obtains basic information about the current device. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param info Indicates the double pointer to the memory that stores the obtained basic information. + * + * @return Returns 0 if the basic information is obtained, in which case info is valid; + * returns any other value otherwise. + * + * @since 1.0 + * @version 1.0 + */ +int32_t GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info); + +/** + * @brief Obtains a specified {@link NodeDeviceInfoKey} of an online device. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param networkId Indicates the pointer to the network ID of the device. + * @param key Indicates the key to be obtained. + * @param info Indicates the pointer to the buffer that stores the obtained key. + * @param infoLen Indicates the buffer length. + * + * @return Returns 0 if the {@link NodeDeviceInfoKey} is obtained, in which case info is valid; + * returns any other value otherwise. + * + * @since 1.0 + * @version 1.0 + */ +int32_t GetNodeKeyInfo(const char *pkgName, const char *networkId, + NodeDeviceInfoKey key, uint8_t *info, int32_t infoLen); + +/** + * @example timesync_demo.c +*/ + +/** + * @brief Start the time synchronize with specific target node. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param targetNetworkId Indicates the pointer to the address of the specified device. + * @param accuracy Time synchronize accuracy. + * @param period Time synchronize period + * @param cb Indicates the callback for the result. + * + * @return Returns 0 if the request is accepted, and the result can be obtained from the + * callback; returns any other value if the request fails, in which case you will not receive + * the result. + * + * @since 1.0 + * @version 1.0 + */ +int32_t StartTimeSync(const char *pkgName, const char *targetNetworkId, TimeSyncAccuracy accuracy, + TimeSyncPeriod period, ITimeSyncCb *cb); + +/** + * @brief Stop the time synchronize with specific target node. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param targetNetworkId Indicates the pointer to the address of the specified device. + * + * @return Returns 0 if the request is removed; returns any other value if the request fails. + * + * @since 1.0 + * @version 1.0 + */ +int32_t StopTimeSync(const char *pkgName, const char *targetNetworkId); + +/** + * @example devicepublish_demo.c +*/ + +/** + * @brief Publishes a specified service. + * + * Peer devices in the same LAN as the device that publishes this service can discover this service as needed. + * The service is identified by publicId and pkgName. + * + * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. + * @param info Indicates the pointer to the service publishing information. For details, see {@link PublishInfo}. + * @param cb Indicates the pointer to the service publishing callback {@link IPublishCallback}. + * @return Returns SOFTBUS_INVALID_PARAM if any parameter is null or invalid. + * @return Returns SOFTBUS_DISCOVER_NOT_INIT if the Intelligent Soft Bus client fails to be initialized. + * @return Returns SOFTBUS_LOCK_ERR if the mutex fails to be locked. + * @return Returns SOFTBUS_OK if the service is successfully published. + */ +int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb); + +/** + * @brief stoppublishes a specified service. + * + * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. + * @param publishId Indicates the service ID. + * @return Returns SOFTBUS_INVALID_PARAM if pkgName is invalid. + * @return Returns SOFTBUS_DISCOVER_NOT_INIT if the Intelligent Soft Bus client fails to be initialized. + * @return Returns SOFTBUS_OK if the service is successfully unpublished. + */ +int32_t StopPublishLNN(const char *pkgName, int32_t publishId); + +/** + * @brief Subscribes to a specified service. + * + * Information about the device that publishes the service will be reported to the device that subscribes to + * the service. + * The service is identified by subscribeId and pkgName. + * + * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. + * @param info Indicates the pointer to the service subscription information. For details, see {@link SubscribeInfo}. + * @param cb Indicates the service subscription callback {@link IRefreshCallback}. + * @return Returns SOFTBUS_INVALID_PARAM if any parameter is null or invalid. + * @return Returns SOFTBUS_DISCOVER_NOT_INIT if the Intelligent Soft Bus client fails to be initialized. + * @return Returns SOFTBUS_LOCK_ERR if the mutex fails to be locked. + * @return Returns SOFTBUS_OK if the service subscription is successful. + */ +int32_t RefreshLNN(const char *pkgName, const SubscribeInfo *info, const IRefreshCallback *cb); + +/** + * @brief Unsubscribes from a specified service. + * + * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. + * @param refreshId Indicates the service ID. + * @return Returns SOFTBUS_INVALID_PARAM if pkgName is invalid. + * @return Returns SOFTBUS_DISCOVER_NOT_INIT if the Intelligent Soft Bus client fails to be initialized. + * @return Returns SOFTBUS_OK if the service unsubscription is successful. + */ +int32_t StopRefreshLNN(const char *pkgName, int32_t refreshId); + +/** + * @example metanode_demo.c +*/ + +/** + * @brief Active meta node. The meta node online status will be notified by {@link INodeStateCb} + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param info Meta node configuration infomation, see {@link MetaNodeConfigInfo}. + * @param metaNodeId Save meta node ID when it is activated successfully, its buffer length must be not + * less then NETWORK_ID_BUF_LEN + * + * @return Returns 0 if the meta node is activated; returns any other value if the request fails. + * + * @since 1.0 + * @version 1.0 + */ +int32_t ActiveMetaNode(const char *pkgName, const MetaNodeConfigInfo *info, char *metaNodeId); + +/** + * @brief Deactive meta node. The meta node will be removed. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param metaNodeId The meta node ID which deactivated, it's obtained by {@link ActiveMetaNode}. + * + * @return Returns 0 if the meta node is deactivated; returns any other value if the request fails. + * + * @since 1.0 + * @version 1.0 + */ +int32_t DeactiveMetaNode(const char *pkgName, const char *metaNodeId); + +/** + * @brief Get all meta node by {@link ActiveMetaNode}. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param infos The buffer for save meta node info. + * @param infoNum The infos buffer num which maximum is {@link MAX_META_NODE_NUM}. + * + * @return Returns 0 if the call is success; returns any other value if it fails. + * + * @since 1.0 + * @version 1.0 + */ +int32_t GetAllMetaNodeInfo(const char *pkgName, MetaNodeInfo *infos, int32_t *infoNum); + +/** + * @example shiftlnngear_demo.c +*/ + +/** + * @brief Modify heartbeat parameters and trigger a temporary heartbeat. + * + * @param pkgName Indicates the pointer to the caller ID, for example, the package name. + * For the same caller, the value of this parameter must be the same for all functions. + * @param callerId The id of the caller, whitch cannot be NULL, and maxium length is {@link CALLER_ID_MAX_LEN}. + * @param targetNetworkId The networkId of the target device to refresh online state, and could be NULL. + * @param mode The duration or cycle parameter of heartbeat. For details, see {@link GearMode}. + * + * @return Returns 0 if the call is success; returns any other value if it fails. + * + * @since 1.0 + * @version 1.0 + */ +int32_t ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId, const GearMode *mode); + +#ifdef __cplusplus +} +#endif + +#endif +/** @} */ -- Gitee