• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Utils Development Guidelines<a name="EN-US_TOPIC_0000001059307279"></a>
2
3-   [Available APIs](#section1633115419401)
4-   [How to Develop](#section17450172710292)
5    -   [Developing a Native Application for the KV Store That Uses the LiteOS Cortex-A Kernel \(Hi3516 or Hi3518\)](#section258354119295)
6    -   [Dumping System Attributes on the Platform That Uses the LiteOS Cortex-M Kernel](#section9179161863014)
7    -   [Dumping System Attributes on the Platform That Uses the LiteOS Cortex-A Kernel](#section3179121853017)
8
9
10## Available APIs<a name="section1633115419401"></a>
11
12**Table  1**  APIs for file operations
13
14<a name="table1171894475019"></a>
15<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>
16</th>
17<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>
18</th>
19</tr>
20</thead>
21<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>
22</td>
23<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>
24</td>
25</tr>
26<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>
27</td>
28<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>
29</td>
30</tr>
31<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>
32</td>
33<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>
34</td>
35</tr>
36<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>
37</td>
38<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>
39</td>
40</tr>
41<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>
42</td>
43<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>
44</td>
45</tr>
46<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>
47</td>
48<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>
49</td>
50</tr>
51<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>
52</td>
53<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>
54</td>
55</tr>
56<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>
57</td>
58<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>
59</td>
60</tr>
61<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>
62</td>
63<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>
64</td>
65</tr>
66</tbody>
67</table>
68
69Sample code for file operations:
70
71```
72// Open a file and write data.
73char fileName[] = "testfile";
74static const char def[] = "utils_file_operation implement.";
75int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
76printf("file handle = %d\n", fd);
77int ret = UtilsFileWrite(fd, def, strlen(def));
78printf("write ret = %d\n", ret);
79
80// Adjust the position offset in the file.
81ret = UtilsFileSeek(fd, 5, SEEK_SET_FS);
82printf("lseek ret = %d\n", ret);
83
84// Read data and close the file.
85char buf[64] = {0};
86int readLen = UtilsFileRead(fd, buf, 64);
87ret = UtilsFileClose(fd);
88printf("read len = %d : buf = %s\n", readLen, buf);
89
90// Obtain the file size.
91int fileLen = 0;
92ret = UtilsFileStat(fileName, &fileLen);
93printf("file size = %d\n", fileLen);
94
95// Delete the file.
96ret = UtilsFileDelete(fileName);
97printf("delete ret = %d\n", ret);
98```
99
100**Table  2**  APIs for KV store operations
101
102<a name="table1731550155318"></a>
103<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>
104</th>
105<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>
106</th>
107</tr>
108</thead>
109<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>
110</td>
111<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>
112</td>
113</tr>
114<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>
115</td>
116<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>
117</td>
118</tr>
119<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>
120</td>
121<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>
122</td>
123</tr>
124</tbody>
125</table>
126
127Sample code for the KV store:
128
129```
130// Set the value matching the specified key.
131char key[] = "rw.sys.version_100";
132char value[] = "Hello kv operation implement!";
133int ret = UtilsSetValue(key, value);
134printf("UtilsSetValue set ret = %d\n", ret);
135
136// Obtain the value matching the specified key.
137char temp[128] = {0};
138ret = UtilsGetValue(key, temp, 128);
139printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp);
140
141// Delete the value matching the specified key.
142ret = UtilsDeleteValue(key);
143printf("UtilsDeleteValue delete ret = %d\n", ret);
144```
145
146## How to Develop<a name="section17450172710292"></a>
147
148### Developing a Native Application for the KV Store That Uses the LiteOS Cortex-A Kernel \(Hi3516 or Hi3518\)<a name="section258354119295"></a>
149
1501.  Develop the native application for the KV store using  **AbilityKit**  APIs.
151    -   Write the user program by calling the APIs provided by the KV store and compile the  **libLauncher.so**  file.
152
153        ```
154        // Set the value matching the specified key.
155        char key[] = "rw.sys.version_100";
156        char value[] = "Hello kv operation implement!";
157        int ret = UtilsSetValue(key, value);
158        printf("UtilsSetValue set ret = %d\n", ret);
159
160        // Obtain the value matching the specified key.
161        char temp[128] = {0};
162        ret = UtilsGetValue(key, temp, 128);
163        printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp);
164
165        // Delete the value matching the specified key.
166        ret = UtilsDeleteValue(key);
167        printf("UtilsDeleteValue delete ret = %d\n", ret);
168        ```
169
170    -   Edit the  **config.json**  file as follows:
171
172        ```
173        {
174            "app": {
175                "bundleName": "com.huawei.launcher",
176                "vendor": "huawei",
177                "version": {
178                    "code": 1,
179                    "name": "1.0"
180                }
181            },
182            "deviceConfig": {
183                "default": {
184                    "reqSdk": {
185                        "compatible": "zsdk 1.0.0",
186                        "target": "zsdk 1.0.1"
187                    },
188                    "keepAlive": false
189                },
190                "smartCamera": {
191                    "reqSdk": {
192                        "compatible": "zsdk 1.0.0",
193                        "target": "zsdk 1.0.1"
194                    },
195                    "keepAlive": false
196                }
197            },
198            "module": {
199                "package": "com.huawei.launcher",
200                "name": ".MyHarmonyAbilityPackage",
201                "deviceType": [
202                    "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartCamera"
203                ],
204                "distro": {
205                    "deliveryWithInstall": true,
206                    "moduleName": "Launcher",
207                    "moduleType": "entry"
208                },
209                "abilities": [{
210                    "name": "MainAbility",
211                    "icon": "res/drawable/phone.png",
212                    "label": "test app 1",
213                    "launchType": "standard",
214                    "type": "page"
215                },
216                {
217                    "name": "SecondAbility",
218                    "icon": "res/drawable/phone.png",
219                    "label": "test app 2",
220                    "launchType": "standard",
221                    "type": "page"
222                },
223                {
224                    "name": "ServiceAbility",
225                    "icon": "res/drawable/phone.png",
226                    "label": "test app 2",
227                    "launchType": "standard",
228                    "type": "service"
229                }
230                ]
231            }
232        }
233        ```
234
235    -   Generate a HAP file.
236
237        -   Add resource files in the  **res/drawable**  directory based on the following directory structure.
238
239        ![](figure/unnaming.png)
240
241        -   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**.
242
2432.  Connect the development board and send the command for installing the native KV store application to the board through the serial port.
244
245    ```
246    ./nfs/dev_tools/bin/bm install -p /nfs/Launcher.hap
247    ```
248
2493.  Send the command for running the native KV store application to the board through the serial port.
250
251    ```
252    ./nfs/dev_tools/bin/aa start -p com.huawei.launcher -n ServiceAbility
253    ```
254
255
256### Dumping System Attributes on the Platform That Uses the LiteOS Cortex-M Kernel<a name="section9179161863014"></a>
257
2581.  Connect the development board and send the  **AT+SYSPARA**  command to the board through the serial port.
259
260    ```
261    AT+SYSPARA
262    ```
263
264    **Figure  1**  Output of the system attribute dumping command for the LiteOS Cortex-M kernel<a name="fig15179161863016"></a>
265    ![](figure/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")
266
267
268### Dumping System Attributes on the Platform That Uses the LiteOS Cortex-A Kernel<a name="section3179121853017"></a>
269
2701.  Connect the development board and run the  **os\_dump --help**  command in the  **bin**  directory to view the  **os\_dump**  help information.
271
272    ```
273    ./bin/os_dump --help
274    ```
275
2762.  Run the  **os\_dump -l**  command in the  **bin**  directory to view system modules that support attribute dumping.
277
278    ```
279    ./bin/os_dump -l
280    ```
281
2823.  Run the  **os\_dump syspara**  command in the  **bin**  directory to dump the current system attributes.
283
284    ```
285    ./bin/os_dump syspara
286    ```
287
288    **Figure  2**  Output of the system attribute dumping command for the LiteOS Cortex-A kernel<a name="fig2179718143018"></a>
289    ![](figure/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")
290
291
292