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