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<a name="table1171894475019"></a> 8<table><thead align="left"><tr id="row1471814441508"><th class="cellrowborder" valign="top" width="45.540000000000006%" id="mcps1.2.3.1.1"><p id="p11718174425016"><a name="p11718174425016"></a><a name="p11718174425016"></a>Function</p> 9</th> 10<th class="cellrowborder" valign="top" width="54.459999999999994%" id="mcps1.2.3.1.2"><p id="p6718124475013"><a name="p6718124475013"></a><a name="p6718124475013"></a>Description</p> 11</th> 12</tr> 13</thead> 14<tbody><tr id="row671818445506"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p871814441501"><a name="p871814441501"></a><a name="p871814441501"></a>int UtilsFileOpen(const char* path, int oflag, int mode)</p> 15</td> 16<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p127181444165016"><a name="p127181444165016"></a><a name="p127181444165016"></a>Opens or creates a file.</p> 17</td> 18</tr> 19<tr id="row1171874420507"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p1671884419509"><a name="p1671884419509"></a><a name="p1671884419509"></a>int UtilsFileClose(int fd)</p> 20</td> 21<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p1071884416504"><a name="p1071884416504"></a><a name="p1071884416504"></a>Closes a file with a specified file descriptor.</p> 22</td> 23</tr> 24<tr id="row6718744105017"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p137181644145015"><a name="p137181644145015"></a><a name="p137181644145015"></a>int UtilsFileRead(int fd, char *buf, unsigned int len)</p> 25</td> 26<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p1171854410509"><a name="p1171854410509"></a><a name="p1171854410509"></a>Reads a specified length of data from a file with the specified file descriptor and writes the data into the buffer.</p> 27</td> 28</tr> 29<tr id="row2071817440509"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p17718144465012"><a name="p17718144465012"></a><a name="p17718144465012"></a>int UtilsFileWrite(int fd, const char *buf, unsigned int len)</p> 30</td> 31<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p67191444145012"><a name="p67191444145012"></a><a name="p67191444145012"></a>Writes a specified length of data into a file with the specified file descriptor.</p> 32</td> 33</tr> 34<tr id="row471974425019"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p11719174475016"><a name="p11719174475016"></a><a name="p11719174475016"></a>int UtilsFileDelete(const char *path)</p> 35</td> 36<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p2071919446507"><a name="p2071919446507"></a><a name="p2071919446507"></a>Deletes a specified file.</p> 37</td> 38</tr> 39<tr id="row1071964425013"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p20719114405010"><a name="p20719114405010"></a><a name="p20719114405010"></a>int UtilsFileStat(const char *path, unsigned int *fileSize)</p> 40</td> 41<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p8719144435015"><a name="p8719144435015"></a><a name="p8719144435015"></a>Obtains the file size.</p> 42</td> 43</tr> 44<tr id="row2071924417504"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p57193447501"><a name="p57193447501"></a><a name="p57193447501"></a>int UtilsFileSeek(int fd, int offset, unsigned int whence)</p> 45</td> 46<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p1071934495014"><a name="p1071934495014"></a><a name="p1071934495014"></a>Adjusts the read and write position offset in a file.</p> 47</td> 48</tr> 49<tr id="row1135017417557"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p2350184112554"><a name="p2350184112554"></a><a name="p2350184112554"></a>int UtilsFileCopy(const char* src, const char* dest)</p> 50</td> 51<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p113501041155511"><a name="p113501041155511"></a><a name="p113501041155511"></a>Copies the source file to a target file.</p> 52</td> 53</tr> 54<tr id="row11216645145517"><td class="cellrowborder" valign="top" width="45.540000000000006%" headers="mcps1.2.3.1.1 "><p id="p192171345105519"><a name="p192171345105519"></a><a name="p192171345105519"></a>int UtilsFileMove(const char* src, const char* dest)</p> 55</td> 56<td class="cellrowborder" valign="top" width="54.459999999999994%" headers="mcps1.2.3.1.2 "><p id="p921744595517"><a name="p921744595517"></a><a name="p921744595517"></a>Moves the source file into a target file.</p> 57</td> 58</tr> 59</tbody> 60</table> 61 62Sample code for file operations: 63 64``` 65// Open a file and write data. 66char fileName[] = "testfile"; 67static const char def[] = "utils_file_operation implement."; 68int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0); 69printf("file handle = %d\n", fd); 70int ret = UtilsFileWrite(fd, def, strlen(def)); 71printf("write ret = %d\n", ret); 72 73// Adjust the position offset in the file. 74ret = UtilsFileSeek(fd, 5, SEEK_SET_FS); 75printf("lseek ret = %d\n", ret); 76 77// Read data and close the file. 78char buf[64] = {0}; 79int readLen = UtilsFileRead(fd, buf, 64); 80ret = UtilsFileClose(fd); 81printf("read len = %d : buf = %s\n", readLen, buf); 82 83// Obtain the file size. 84int fileLen = 0; 85ret = UtilsFileStat(fileName, &fileLen); 86printf("file size = %d\n", fileLen); 87 88// Delete the file. 89ret = UtilsFileDelete(fileName); 90printf("delete ret = %d\n", ret); 91``` 92 93**Table 2** APIs for KV store operations 94 95<a name="table1731550155318"></a> 96<table><thead align="left"><tr id="row4419501537"><th class="cellrowborder" valign="top" width="57.38999999999999%" id="mcps1.2.3.1.1"><p id="p54150165315"><a name="p54150165315"></a><a name="p54150165315"></a>Function</p> 97</th> 98<th class="cellrowborder" valign="top" width="42.61%" id="mcps1.2.3.1.2"><p id="p941150145313"><a name="p941150145313"></a><a name="p941150145313"></a>Description</p> 99</th> 100</tr> 101</thead> 102<tbody><tr id="row34145016535"><td class="cellrowborder" valign="top" width="57.38999999999999%" headers="mcps1.2.3.1.1 "><p id="p980953910190"><a name="p980953910190"></a><a name="p980953910190"></a>int UtilsGetValue(const char* key, char* value, unsigned int len)</p> 103</td> 104<td class="cellrowborder" valign="top" width="42.61%" headers="mcps1.2.3.1.2 "><p id="p13562171015712"><a name="p13562171015712"></a><a name="p13562171015712"></a>Obtains the value matching a specified key from the file system or cache.</p> 105</td> 106</tr> 107<tr id="row1746172917474"><td class="cellrowborder" valign="top" width="57.38999999999999%" headers="mcps1.2.3.1.1 "><p id="p242913617818"><a name="p242913617818"></a><a name="p242913617818"></a>int UtilsSetValue(const char* key, const char* value)</p> 108</td> 109<td class="cellrowborder" valign="top" width="42.61%" headers="mcps1.2.3.1.2 "><p id="p2431455765"><a name="p2431455765"></a><a name="p2431455765"></a>Adds or updates the value matching a specified key in the file system or cache.</p> 110</td> 111</tr> 112<tr id="row10992232154714"><td class="cellrowborder" valign="top" width="57.38999999999999%" headers="mcps1.2.3.1.1 "><p id="p118151439111910"><a name="p118151439111910"></a><a name="p118151439111910"></a>int UtilsDeleteValue(const char* key)</p> 113</td> 114<td class="cellrowborder" valign="top" width="42.61%" headers="mcps1.2.3.1.2 "><p id="p126575774517"><a name="p126575774517"></a><a name="p126575774517"></a>Deletes the value matching a specified key from the file system or cache.</p> 115</td> 116</tr> 117</tbody> 118</table> 119 120Sample code for the KV store: 121 122``` 123// Set the value matching the specified key. 124char key[] = "rw.sys.version_100"; 125char value[] = "Hello kv operation implement!"; 126int ret = UtilsSetValue(key, value); 127printf("UtilsSetValue set ret = %d\n", ret); 128 129// Obtain the value matching the specified key. 130char temp[128] = {0}; 131ret = UtilsGetValue(key, temp, 128); 132printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp); 133 134// Delete the value matching the specified key. 135ret = UtilsDeleteValue(key); 136printf("UtilsDeleteValue delete ret = %d\n", ret); 137``` 138 139## How to Develop<a name="section17450172710292"></a> 140 141### Developing a Native Application for the KV Store That Uses the LiteOS Cortex-A Kernel \(Hi3516 or Hi3518\)<a name="section258354119295"></a> 142 1431. Develop the native application for the KV store using **AbilityKit** APIs. 144 - Write the user program by calling the APIs provided by the KV store and compile the **libLauncher.so** file. 145 146 ``` 147 // Set the value matching the specified key. 148 char key[] = "rw.sys.version_100"; 149 char value[] = "Hello kv operation implement!"; 150 int ret = UtilsSetValue(key, value); 151 printf("UtilsSetValue set ret = %d\n", ret); 152 153 // Obtain the value matching the specified key. 154 char temp[128] = {0}; 155 ret = UtilsGetValue(key, temp, 128); 156 printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp); 157 158 // Delete the value matching the specified key. 159 ret = UtilsDeleteValue(key); 160 printf("UtilsDeleteValue delete ret = %d\n", ret); 161 ``` 162 163 - Edit the **config.json** file as follows: 164 165 ``` 166 { 167 "app": { 168 "bundleName": "com.example.launcher", 169 "vendor": "example, 170 "version": { 171 "code": 1, 172 "name": "1.0" 173 } 174 }, 175 "deviceConfig": { 176 "default": { 177 "reqSdk": { 178 "compatible": "zsdk 1.0.0", 179 "target": "zsdk 1.0.1" 180 }, 181 "keepAlive": false 182 }, 183 "smartCamera": { 184 "reqSdk": { 185 "compatible": "zsdk 1.0.0", 186 "target": "zsdk 1.0.1" 187 }, 188 "keepAlive": false 189 } 190 }, 191 "module": { 192 "package": "com.example.launcher", 193 "name": ".MyHarmonyAbilityPackage", 194 "deviceType": [ 195 "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartCamera" 196 ], 197 "distro": { 198 "deliveryWithInstall": true, 199 "moduleName": "Launcher", 200 "moduleType": "entry" 201 }, 202 "abilities": [{ 203 "name": "MainAbility", 204 "icon": "res/drawable/phone.png", 205 "label": "test app 1", 206 "launchType": "standard", 207 "type": "page" 208 }, 209 { 210 "name": "SecondAbility", 211 "icon": "res/drawable/phone.png", 212 "label": "test app 2", 213 "launchType": "standard", 214 "type": "page" 215 }, 216 { 217 "name": "ServiceAbility", 218 "icon": "res/drawable/phone.png", 219 "label": "test app 2", 220 "launchType": "standard", 221 "type": "service" 222 } 223 ] 224 } 225 } 226 ``` 227 228 - Generate a HAP file. 229 230 - Add resource files in the **res/drawable** directory based on the following directory structure. 231 232  233 234 - 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**. 235 2362. Connect the development board and send the command for installing the native KV store application to the board through the serial port. 237 238 ``` 239 ./nfs/dev_tools/bin/bm install -p /nfs/Launcher.hap 240 ``` 241 2423. Send the command for running the native KV store application to the board through the serial port. 243 244 ``` 245 ./nfs/dev_tools/bin/aa start -p com.example.launcher -n ServiceAbility 246 ``` 247 248 249### Dumping System Attributes on the Platform That Uses the LiteOS Cortex-M Kernel<a name="section9179161863014"></a> 250 2511. Connect the development board and send the **AT+SYSPARA** command to the board through the serial port. 252 253 ``` 254 AT+SYSPARA 255 ``` 256 257 **Figure 1** Output of the system attribute dumping command for the LiteOS Cortex-M kernel<a name="fig15179161863016"></a> 258  259 260 261### Dumping System Attributes on the Platform That Uses the LiteOS Cortex-A Kernel<a name="section3179121853017"></a> 262 2631. Connect the development board and run the **os\_dump --help** command in the **bin** directory to view the **os\_dump** help information. 264 265 ``` 266 ./bin/os_dump --help 267 ``` 268 2692. Run the **os\_dump -l** command in the **bin** directory to view system modules that support attribute dumping. 270 271 ``` 272 ./bin/os_dump -l 273 ``` 274 2753. Run the **os\_dump syspara** command in the **bin** directory to dump the current system attributes. 276 277 ``` 278 ./bin/os_dump syspara 279 ``` 280 281 **Figure 2** Output of the system attribute dumping command for the LiteOS Cortex-A kernel<a name="fig2179718143018"></a> 282  283 284 285