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