2022-12-20 01:47:29 +00:00
# Distributed DeviceProfile
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
## Introduction
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
DeviceProfile is used to manage device hardware capabilities and system software features. A typical device profile includes the device type, device name, OS type, and OS version. By allowing quick access to local and remote device profiles, DeviceProfile lays the foundation for initiating distributed services.
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
DeviceProfile provides the following functions:
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
- Inserting, deleting, and querying profiles of local devices.
- Querying remote device profile information.
- Subscribing to remote device profile changes.
- Synchronizing profile information across devices.
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
The following figure shows the architecture of the DeviceProfile module.
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
## Architecture
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
**Figure 1** DeviceProfile component architecture
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
![](figures/dp-architecture_en.png)
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
## Directory Structure
The main code directory structure of DeviceProfile is as follows:
2021-12-15 01:18:13 +00:00
2021-12-15 02:59:48 +00:00
```
2021-12-15 03:17:53 +00:00
├── interfaces
2022-12-20 01:47:29 +00:00
│ └── innerkits
│ └── distributeddeviceprofile // innerkits APIs
2021-12-15 03:17:53 +00:00
├── ohos.build
2022-12-20 01:47:29 +00:00
├── sa_profile // SAID profile
│ ├── 6001.xml
│ └── BUILD.gn
2022-10-09 02:54:46 +00:00
└── services
2022-12-20 01:47:29 +00:00
└── distributeddeviceprofile
├── BUILD.gn
├── include
│ ├── authority // Permission verification
│ ├── contentsensor // Header file for content sensor data collection
│ ├── dbstorage // Header file for database operations
│ ├── devicemanager // Header file for device management
│ └── subscribemanager // Header file for subscription management
├── src
│ ├── authority // Permission verification
│ ├── contentsensor // Implementation of content sensor data collection
│ ├── dbstorage // Implementation of database operations
│ ├── devicemanager // Implementation of device management
│ └── subscribemanager // Implementation of subscription management
└── test // Test cases
2021-12-15 02:59:48 +00:00
```
2021-12-15 01:18:13 +00:00
2022-12-20 01:47:29 +00:00
## Constraints
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
- The devices between which you want to set up a connection must be in the same LAN.
- Before setting up a connection between two devices, you must bind the devices. For details about the binding process, see the Security subsystem readme file.
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
## Usage
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
### Querying Profile Information
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
* Parameters of **GetDeviceProfile**
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
| Name | Type | Mandatory| Description |
2021-12-15 03:17:53 +00:00
| --------- | ---------------------------- | ---- | ----------------------------------- |
2022-12-20 01:47:29 +00:00
| deviceId | std::string | Yes | ID of the device whose profile is to be queried. A null value indicates the local device.|
| serviceId | std::string | Yes | Service ID. |
| profile | ServiceCharacteristicProfile | Yes | Device profile information returned. |
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Sample code
2021-12-15 03:17:53 +00:00
```c++
2022-12-20 01:47:29 +00:00
// Declare the return value.
2021-12-15 03:17:53 +00:00
ServiceCharacteristicProfile profile;
2022-12-20 01:47:29 +00:00
// Call GetDeviceProfile.
2021-12-15 03:17:53 +00:00
DistributedDeviceProfileClient::GetInstance().GetDeviceProfile(deviceId, serviceId, profile);
std::string jsonData = profile.GetCharacteristicProfileJson();
result.append("jsonData:" + jsonData + "\n");
2021-12-15 02:59:48 +00:00
```
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
### Inserting Profile Information
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Parameters of **PutDeviceProfile**
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
| Name | Type | Mandatory| Description |
2021-12-15 03:17:53 +00:00
| --------- | ---------------------------- | ---- | ----------------------------------- |
2022-12-20 01:47:29 +00:00
| profile | ServiceCharacteristicProfile | Yes | Profile information to insert. |
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Sample code
2021-12-15 03:17:53 +00:00
```c++
2022-12-20 01:47:29 +00:00
// Declare and fill in the data to insert.
2021-12-15 03:17:53 +00:00
ServiceCharacteristicProfile profile;
profile.SetServiceId(serviceId);
profile.SetServiceType(serviceType);
nlohmann::json j;
j["testVersion"] = "3.0.0";
j["testApiLevel"] = API_LEVEL;
profile.SetCharacteristicProfileJson(j.dump());
2022-12-20 01:47:29 +00:00
// Call PutDeviceProfile.
2021-12-15 03:17:53 +00:00
DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
2021-12-15 02:59:48 +00:00
```
2022-12-20 01:47:29 +00:00
### Deleting Profile Information
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Parameters of **DeleteDeviceProfile**
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
| Name | Type | Mandatory| Description |
2021-12-15 03:17:53 +00:00
| --------- | ---------------------------- | ---- | ----------------------------------- |
2022-12-20 01:47:29 +00:00
| serviceId | std::string | Yes | ID of the service record to delete. |
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
* Sample code
2021-12-15 03:17:53 +00:00
```c++
2022-12-20 01:47:29 +00:00
// Declare and fill in the data to delete.
2021-12-15 03:17:53 +00:00
std::string serviceId = "test";
// DeleteDeviceProfile
DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile(serviceId);
```
2022-12-20 01:47:29 +00:00
### Synchronizing Profile Information
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Parameters of **SyncDeviceProfile**
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
| Name | Type | Mandatory| Description |
2021-12-15 03:17:53 +00:00
| --------- | ---------------------------- | ---- | ----------------------------------- |
2022-12-20 01:47:29 +00:00
| syncOption| SyncOption | Yes | Synchronization range and mode. |
| syncCb | IProfileEventCallback | Yes | Callback used to return the synchronization result. |
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Sample code
2021-12-15 03:17:53 +00:00
```c++
2022-12-20 01:47:29 +00:00
// Define the synchronization mode and range.
2021-12-15 03:17:53 +00:00
SyncOptions syncOption;
2022-01-04 06:15:44 +00:00
syncOption.SetSyncMode((OHOS::DeviceProfile::SyncMode)atoi(mode.c_str()));
2021-12-15 03:17:53 +00:00
for (const auto& deviceId : deviceIds) {
syncOption.AddDevice(deviceId);
}
2022-12-20 01:47:29 +00:00
// Call SyncDeviceProfile.
2021-12-15 03:17:53 +00:00
DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOption,
std::make_shared< ProfileEventCallback > ());
2021-12-15 02:59:48 +00:00
```
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
### Subscribing to Profile Events (Synchronization and Change Events)
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Parameters of **SubscribeProfileEvents**
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
| Name | Type | Mandatory| Description |
2021-12-15 03:17:53 +00:00
| -------------- | ---------------------------- | ---- | ----------------------------------- |
2022-12-20 01:47:29 +00:00
| subscribeInfos | SubscribeInfo | Yes | Type of the event to subscribe to. |
| eventCb | IProfileEventCallback | Yes | Callback used to return the subscription event. |
| failedEvents | ProfileEvent | Yes | Failure event. |
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
* Sample code
2021-12-15 03:17:53 +00:00
```c++
auto callback = std::make_shared< ProfileEventCallback > ();
std::list< SubscribeInfo > subscribeInfos;
2021-12-20 03:17:21 +00:00
2022-12-20 01:47:29 +00:00
// Subscribe to the EVENT_PROFILE_CHANGED event.
2021-12-15 03:17:53 +00:00
ExtraInfo extraInfo;
extraInfo["deviceId"] = deviceId;
extraInfo["serviceIds"] = serviceIds;
2021-12-20 03:17:21 +00:00
SubscribeInfo changeEventInfo;
changeEventInfo.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
changeEventInfo.extraInfo = std::move(extraInfo);
subscribeInfos.emplace_back(changeEventInfo);
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
// Subscribe to the EVENT_SYNC_COMPLETED event.
2021-12-20 03:17:21 +00:00
SubscribeInfo syncEventInfo;
syncEventInfo.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
subscribeInfos.emplace_back(syncEventInfo);
2021-12-15 03:17:53 +00:00
2022-12-20 01:47:29 +00:00
// Call SubscribeProfileEvents.
2021-12-20 03:17:21 +00:00
std::list< ProfileEvent > failedEvents;
2021-12-15 03:17:53 +00:00
DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
callback, failedEvents);
2021-12-20 03:17:21 +00:00
2022-12-20 01:47:29 +00:00
// Cancel the subscription.
2021-12-15 03:17:53 +00:00
std::list< ProfileEvent > profileEvents;
profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents,
callback, failedEvents);
2021-12-15 02:59:48 +00:00
```
2022-12-20 01:47:29 +00:00
## Repositories Involved
2021-12-15 02:59:48 +00:00
2022-12-20 01:47:29 +00:00
[**deviceprofile_device_info_manager** ](https://gitee.com/openharmony/deviceprofile_device_info_manager )