• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Distributed DeviceProfile
2
3## Introduction
4
5DeviceProfile 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.
6
7DeviceProfile provides the following functions:
8
9-   Inserting, deleting, and querying profiles of local devices.
10-   Querying remote device profile information.
11-   Subscribing to remote device profile changes.
12-   Synchronizing profile information across devices.
13
14The following figure shows the architecture of the DeviceProfile module.
15
16## Architecture
17
18**Figure 1** DeviceProfile component architecture
19
20
21![](figures/dp-architecture_en.png)
22
23
24## Directory Structure
25
26The main code directory structure of DeviceProfile is as follows:
27
28```
29├── interfaces
30│   └── innerkits
31│       └── distributeddeviceprofile            // innerkits APIs
32├── ohos.build
33├── sa_profile                                  // SAID profile
34│   ├── 6001.xml
35│   └── BUILD.gn
36└── services
37    └── distributeddeviceprofile
38        ├── BUILD.gn
39        ├── include
40        │   ├── authority                       // Permission verification
41        │   ├── contentsensor                   // Header file for content sensor data collection
42        │   ├── dbstorage                       // Header file for database operations
43        │   ├── devicemanager                   // Header file for device management
44        │   └── subscribemanager                // Header file for subscription management
45        ├── src
46        │   ├── authority                       // Permission verification
47        │   ├── contentsensor                   // Implementation of content sensor data collection
48        │   ├── dbstorage                       // Implementation of database operations
49        │   ├── devicemanager                   // Implementation of device management
50        │   └── subscribemanager                // Implementation of subscription management
51        └── test                                // Test cases
52```
53
54## Constraints
55
56-   The devices between which you want to set up a connection must be in the same LAN.
57-   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.
58
59## Usage
60
61### Querying Profile Information
62
63* Parameters of **GetDeviceProfile**
64
65| Name     | Type                         | Mandatory| Description                               |
66| --------- | ---------------------------- | ---- | ----------------------------------- |
67| deviceId  | std::string                  | Yes  | ID of the device whose profile is to be queried. A null value indicates the local device.|
68| serviceId | std::string                  | Yes  | Service ID.                    |
69| profile   | ServiceCharacteristicProfile | Yes  | Device profile information returned.                              |
70
71* Sample code
72
73```c++
74// Declare the return value.
75ServiceCharacteristicProfile profile;
76// Call GetDeviceProfile.
77DistributedDeviceProfileClient::GetInstance().GetDeviceProfile(deviceId, serviceId, profile);
78std::string jsonData = profile.GetCharacteristicProfileJson();
79result.append("jsonData:" + jsonData + "\n");
80```
81
82### Inserting Profile Information
83
84* Parameters of **PutDeviceProfile**
85
86| Name     | Type                         | Mandatory| Description                               |
87| --------- | ---------------------------- | ---- | ----------------------------------- |
88| profile   | ServiceCharacteristicProfile | Yes  | Profile information to insert.               |
89
90* Sample code
91
92```c++
93// Declare and fill in the data to insert.
94ServiceCharacteristicProfile profile;
95profile.SetServiceId(serviceId);
96profile.SetServiceType(serviceType);
97nlohmann::json j;
98j["testVersion"] = "3.0.0";
99j["testApiLevel"] = API_LEVEL;
100profile.SetCharacteristicProfileJson(j.dump());
101// Call PutDeviceProfile.
102DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
103```
104
105### Deleting Profile Information
106
107* Parameters of **DeleteDeviceProfile**
108
109| Name     | Type                         | Mandatory| Description                               |
110| --------- | ---------------------------- | ---- | ----------------------------------- |
111| serviceId | std::string                  | Yes  | ID of the service record to delete.             |
112
113* Sample code
114
115```c++
116// Declare and fill in the data to delete.
117std::string serviceId = "test";
118// DeleteDeviceProfile
119DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile(serviceId);
120```
121
122### Synchronizing Profile Information
123
124* Parameters of **SyncDeviceProfile**
125
126| Name     | Type                         | Mandatory| Description                               |
127| --------- | ---------------------------- | ---- | ----------------------------------- |
128| syncOption| SyncOption                   | Yes  | Synchronization range and mode.                   |
129| syncCb    | IProfileEventCallback        | Yes  | Callback used to return the synchronization result.                        |
130
131* Sample code
132
133```c++
134// Define the synchronization mode and range.
135SyncOptions syncOption;
136syncOption.SetSyncMode((OHOS::DeviceProfile::SyncMode)atoi(mode.c_str()));
137for (const auto& deviceId : deviceIds) {
138    syncOption.AddDevice(deviceId);
139}
140// Call SyncDeviceProfile.
141DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOption,
142    std::make_shared<ProfileEventCallback>());
143```
144
145### Subscribing to Profile Events (Synchronization and Change Events)
146
147* Parameters of **SubscribeProfileEvents**
148
149| Name          | Type                         | Mandatory| Description                               |
150| -------------- | ---------------------------- | ---- | ----------------------------------- |
151| subscribeInfos | SubscribeInfo                | Yes  | Type of the event to subscribe to.                   |
152| eventCb        | IProfileEventCallback        | Yes  | Callback used to return the subscription event.                        |
153| failedEvents   | ProfileEvent                 | Yes  | Failure event.                            |
154
155* Sample code
156
157```c++
158auto callback = std::make_shared<ProfileEventCallback>();
159std::list<SubscribeInfo> subscribeInfos;
160
161// Subscribe to the EVENT_PROFILE_CHANGED event.
162ExtraInfo extraInfo;
163extraInfo["deviceId"] = deviceId;
164extraInfo["serviceIds"] = serviceIds;
165SubscribeInfo changeEventInfo;
166changeEventInfo.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
167changeEventInfo.extraInfo = std::move(extraInfo);
168subscribeInfos.emplace_back(changeEventInfo);
169
170// Subscribe to the EVENT_SYNC_COMPLETED event.
171SubscribeInfo syncEventInfo;
172syncEventInfo.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
173subscribeInfos.emplace_back(syncEventInfo);
174
175// Call SubscribeProfileEvents.
176std::list<ProfileEvent> failedEvents;
177DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
178    callback, failedEvents);
179
180// Cancel the subscription.
181std::list<ProfileEvent> profileEvents;
182profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
183DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents,
184    callback, failedEvents);
185```
186
187## Repositories Involved
188
189[**deviceprofile_device_info_manager**](https://gitee.com/openharmony/deviceprofile_device_info_manager)
190