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