1# Utils Development<a name="EN-US_TOPIC_0000001059307279"></a> 2 3## Available APIs<a name="section1633115419401"></a> 4 5**Table 1** APIs for file operations 6 7| Function | Description | 8| ---------- | ------------- | 9| int UtilsFileOpen(const char* path, int oflag, int mode) | Opens or creates a file. | 10| int UtilsFileClose(int fd) | Closes a file with a specified file descriptor. | 11| int UtilsFileRead(int fd, char *buf, unsigned int len) | Reads a specified length of data from a file with the specified file descriptor and writes the data into the buffer. | 12| int UtilsFileWrite(int fd, const char *buf, unsigned int len) | Writes a specified length of data into a file with the specified file descriptor. | 13| int UtilsFileDelete(const char *path) | Deletes a specified file. | 14| int UtilsFileStat(const char *path, unsigned int *fileSize) | Obtains the file size. | 15| int UtilsFileSeek(int fd, int offset, unsigned int whence) | Adjusts the read and write position offset in a file. | 16| int UtilsFileCopy(const char* src, const char* dest) | Copies the source file to a target file. | 17| int UtilsFileMove(const char* src, const char* dest) | Moves the source file into a target file. | 18 19 20Sample code for file operations: 21 22``` 23// Open a file and write data. 24char fileName[] = "testfile"; 25static const char def[] = "utils_file_operation implement."; 26int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0); 27printf("file handle = %d\n", fd); 28int ret = UtilsFileWrite(fd, def, strlen(def)); 29printf("write ret = %d\n", ret); 30 31// Adjust the position offset in the file. 32ret = UtilsFileSeek(fd, 5, SEEK_SET_FS); 33printf("lseek ret = %d\n", ret); 34 35// Read data and close the file. 36char buf[64] = {0}; 37int readLen = UtilsFileRead(fd, buf, 64); 38ret = UtilsFileClose(fd); 39printf("read len = %d : buf = %s\n", readLen, buf); 40 41// Obtain the file size. 42int fileLen = 0; 43ret = UtilsFileStat(fileName, &fileLen); 44printf("file size = %d\n", fileLen); 45 46// Delete the file. 47ret = UtilsFileDelete(fileName); 48printf("delete ret = %d\n", ret); 49``` 50 51**Table 2** APIs for KV store operations 52 53| Function | Description | 54| ----------- | ---------------- | 55| int UtilsGetValue(const char* key, char* value, unsigned int len) | Obtains the value matching a specified key from the file system or cache. | 56| int UtilsSetValue(const char* key, const char* value) | Adds or updates the value matching a specified key in the file system or cache. | 57| int UtilsDeleteValue(const char* key) | Deletes the value matching a specified key from the file system or cache. | 58 59 60Sample code for the KV store: 61 62``` 63// Set the value matching the specified key. 64char key[] = "rw.sys.version_100"; 65char value[] = "Hello kv operation implement!"; 66int ret = UtilsSetValue(key, value); 67printf("UtilsSetValue set ret = %d\n", ret); 68 69// Obtain the value matching the specified key. 70char temp[128] = {0}; 71ret = UtilsGetValue(key, temp, 128); 72printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp); 73 74// Delete the value matching the specified key. 75ret = UtilsDeleteValue(key); 76printf("UtilsDeleteValue delete ret = %d\n", ret); 77``` 78 79## How to Develop<a name="section17450172710292"></a> 80 81### Developing a Native Application for the KV Store That Uses the LiteOS Cortex-A Kernel \(Hi3516 or Hi3518\)<a name="section258354119295"></a> 82 831. Develop the native application for the KV store using **AbilityKit** APIs. 84 - Write the user program by calling the APIs provided by the KV store and compile the **libLauncher.so** file. 85 86 ``` 87 // Set the value matching the specified key. 88 char key[] = "rw.sys.version_100"; 89 char value[] = "Hello kv operation implement!"; 90 int ret = UtilsSetValue(key, value); 91 printf("UtilsSetValue set ret = %d\n", ret); 92 93 // Obtain the value matching the specified key. 94 char temp[128] = {0}; 95 ret = UtilsGetValue(key, temp, 128); 96 printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp); 97 98 // Delete the value matching the specified key. 99 ret = UtilsDeleteValue(key); 100 printf("UtilsDeleteValue delete ret = %d\n", ret); 101 ``` 102 103 - Edit the **config.json** file as follows: 104 105 ``` 106 { 107 "app": { 108 "bundleName": "com.example.launcher", 109 "vendor": "example, 110 "version": { 111 "code": 1, 112 "name": "1.0" 113 } 114 }, 115 "deviceConfig": { 116 "default": { 117 "reqSdk": { 118 "compatible": "zsdk 1.0.0", 119 "target": "zsdk 1.0.1" 120 }, 121 "keepAlive": false 122 }, 123 "smartCamera": { 124 "reqSdk": { 125 "compatible": "zsdk 1.0.0", 126 "target": "zsdk 1.0.1" 127 }, 128 "keepAlive": false 129 } 130 }, 131 "module": { 132 "package": "com.example.launcher", 133 "name": ".MyHarmonyAbilityPackage", 134 "deviceType": [ 135 "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartCamera" 136 ], 137 "distro": { 138 "deliveryWithInstall": true, 139 "moduleName": "Launcher", 140 "moduleType": "entry" 141 }, 142 "abilities": [{ 143 "name": "MainAbility", 144 "icon": "res/drawable/phone.png", 145 "label": "test app 1", 146 "launchType": "standard", 147 "type": "page" 148 }, 149 { 150 "name": "SecondAbility", 151 "icon": "res/drawable/phone.png", 152 "label": "test app 2", 153 "launchType": "standard", 154 "type": "page" 155 }, 156 { 157 "name": "ServiceAbility", 158 "icon": "res/drawable/phone.png", 159 "label": "test app 2", 160 "launchType": "standard", 161 "type": "service" 162 } 163 ] 164 } 165 } 166 ``` 167 168 - Generate a HAP file. 169 170 - Add resource files in the **res/drawable** directory based on the following directory structure. 171 172 ![](figures/unnaming.png) 173 174 - Compress the **libLauncher.so**, **config.json**, and resource files into a ZIP package and change the file name extension to **.hap**, for example, **Launcher.hap**. 175 1762. Connect the development board and send the command for installing the native KV store application to the board through the serial port. 177 178 ``` 179 ./nfs/dev_tools/bin/bm install -p /nfs/Launcher.hap 180 ``` 181 1823. Send the command for running the native KV store application to the board through the serial port. 183 184 ``` 185 ./nfs/dev_tools/bin/aa start -p com.example.launcher -n ServiceAbility 186 ``` 187 188 189### Dumping System Attributes on the Platform That Uses the LiteOS Cortex-M Kernel<a name="section9179161863014"></a> 190 1911. Connect the development board and send the **AT+SYSPARA** command to the board through the serial port. 192 193 ``` 194 AT+SYSPARA 195 ``` 196 197 **Figure 1** Output of the system attribute dumping command for the LiteOS Cortex-M kernel<a name="fig15179161863016"></a> 198 ![](figures/output-of-the-system-attribute-dumping-command-for-the-liteos-cortex-m-kernel.png "output-of-the-system-attribute-dumping-command-for-the-liteos-cortex-m-kernel") 199 200 201### Dumping System Attributes on the Platform That Uses the LiteOS Cortex-A Kernel<a name="section3179121853017"></a> 202 2031. Connect the development board and run the **os\_dump --help** command in the **bin** directory to view the **os\_dump** help information. 204 205 ``` 206 ./bin/os_dump --help 207 ``` 208 2092. Run the **os\_dump -l** command in the **bin** directory to view system modules that support attribute dumping. 210 211 ``` 212 ./bin/os_dump -l 213 ``` 214 2153. Run the **os\_dump syspara** command in the **bin** directory to dump the current system attributes. 216 217 ``` 218 ./bin/os_dump syspara 219 ``` 220 221 **Figure 2** Output of the system attribute dumping command for the LiteOS Cortex-A kernel<a name="fig2179718143018"></a> 222 ![](figures/output-of-the-system-attribute-dumping-command-for-the-liteos-cortex-a-kernel.png "output-of-the-system-attribute-dumping-command-for-the-liteos-cortex-a-kernel") 223