1# 公共基础库开发指导 2 3 4## 接口说明 5 6 **表1** 文件操作接口说明 7 8| 接口名 | 描述 | 9| -------- | -------- | 10| int UtilsFileOpen(const char\* path, int oflag, int mode) | 打开或创建文件 | 11| int UtilsFileClose(int fd) | 关闭文件 | 12| int UtilsFileRead(int fd, char \*buf, unsigned int len) | 读取特定长度的文件数据 | 13| int UtilsFileWrite(int fd, const char \*buf, unsigned int len) | 向文件写入特定大小的数据 | 14| int UtilsFileDelete(const char \*path) | 删除指定文件 | 15| int UtilsFileStat(const char \*path, unsigned int \*fileSize) | 获取文件大小 | 16| int UtilsFileSeek(int fd, int offset, unsigned int whence) | 重新定位文件读/写偏移量 | 17| int UtilsFileCopy(const char\* src, const char\* dest) | 将源文件复制一份并存储到目标文件 | 18| int UtilsFileMove(const char\* src, const char\* dest) | 将源文件移动到指定目标文件 | 19 20文件操作使用示例: 21 22 23``` 24// open && write 25char fileName[] = "testfile"; 26static const char def[] = "utils_file_operation implement."; 27int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0); 28printf("file handle = %d\n", fd); 29int ret = UtilsFileWrite(fd, def, strlen(def)); 30printf("write ret = %d\n", ret); 31 32// seek 33ret = UtilsFileSeek(fd, 5, SEEK_SET_FS); 34printf("lseek ret = %d\n", ret); 35 36// read && close 37char buf[64] = {0}; 38int readLen = UtilsFileRead(fd, buf, 64); 39ret = UtilsFileClose(fd); 40printf("read len = %d : buf = %s\n", readLen, buf); 41 42// stat 43int fileLen = 0; 44ret = UtilsFileStat(fileName, &fileLen); 45printf("file size = %d\n", fileLen); 46 47// delete 48ret = UtilsFileDelete(fileName); 49printf("delete ret = %d\n", ret); 50``` 51 52 **表2** KV存储接口说明 53 54| 接口名 | 描述 | 55| -------- | -------- | 56| int UtilsGetValue(const char\* key, char\* value, unsigned int len) | 提供给上层应用根据key获取对应数据项 | 57| int UtilsSetValue(const char\* key, const char\* value) | 提供给上层应用用于存储/更新key对应数据项 | 58| int UtilsDeleteValue(const char\* key) | 提供给上层应用删除key对应数据项 | 59 60KV存储使用示例: 61 62 63``` 64// set 65char key[] = "rw.sys.version_100"; 66char value[] = "Hello kv operation implement!"; 67int ret = UtilsSetValue(key, value); 68printf("UtilsSetValue set ret = %d\n", ret); 69 70// get 71char temp[128] = {0}; 72ret = UtilsGetValue(key, temp, 128); 73printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp); 74 75// delete 76ret = UtilsDeleteValue(key); 77printf("UtilsDeleteValue delete ret = %d\n", ret); 78``` 79 80 81 82## 开发步骤 83 84 85### LiteOS-A内核(Hi3516、Hi3518平台)KV存储的native应用开发: 86 87 881. 基于AbilityKit开发KV存储的native应用。 89 - 基于KV存储提供的接口编写用户程序,并编译出so(libLauncher.so)文件。 90 91 ``` 92 // set 93 char key[] = "rw.sys.version_100"; 94 char value[] = "Hello kv operation implement!"; 95 int ret = UtilsSetValue(key, value); 96 printf("UtilsSetValue set ret = %d\n", ret); 97 98 // get 99 char temp[128] = {0}; 100 ret = UtilsGetValue(key, temp, 128); 101 printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp); 102 103 // delete 104 ret = UtilsDeleteValue(key); 105 printf("UtilsDeleteValue delete ret = %d\n", ret); 106 ``` 107 - 编写config.json文件,内容如下: 108 109 ``` 110 { 111 "app": { 112 "bundleName": "com.example.launcher", 113 "vendor": "example", 114 "version": { 115 "code": 1, 116 "name": "1.0" 117 } 118 }, 119 "deviceConfig": { 120 "default": { 121 "reqSdk": { 122 "compatible": "zsdk 1.0.0", 123 "target": "zsdk 1.0.1" 124 }, 125 "keepAlive": false 126 }, 127 "smartCamera": { 128 "reqSdk": { 129 "compatible": "zsdk 1.0.0", 130 "target": "zsdk 1.0.1" 131 }, 132 "keepAlive": false 133 } 134 }, 135 "module": { 136 "package": "com.example.launcher", 137 "name": ".MyOpenHarmonyAbilityPackage", 138 "deviceType": [ 139 "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartCamera" 140 ], 141 "distro": { 142 "deliveryWithInstall": true, 143 "moduleName": "Launcher", 144 "moduleType": "entry" 145 }, 146 "abilities": [{ 147 "name": "MainAbility", 148 "icon": "res/drawable/phone.png", 149 "label": "test app 1", 150 "launchType": "standard", 151 "type": "page" 152 }, 153 { 154 "name": "SecondAbility", 155 "icon": "res/drawable/phone.png", 156 "label": "test app 2", 157 "launchType": "standard", 158 "type": "page" 159 }, 160 { 161 "name": "ServiceAbility", 162 "icon": "res/drawable/phone.png", 163 "label": "test app 2", 164 "launchType": "standard", 165 "type": "service" 166 } 167 ] 168 } 169 } 170 ``` 171 172 - 生成hap包。 173 - 按照如下目录结构存放文件,res/drawable下面放置资源文件: 174 175 **图1** 资源文件路径图 176 177 ![zh-cn_image_0000001154153558](figures/zh-cn_image_0000001154153558.png) 178 179 - 将上述文件打包生成zip包,修改后缀为.hap,例如Launcher.hap 180 1812. 连接单板,通过串口向单板发送安装KV存储native应用的命令。 182 183 ``` 184 ./nfs/dev_tools/bin/bm install -p /nfs/Launcher.hap 185 ``` 186 1873. 通过串口向单板发送运行KV存储native应用的命令。 188 189 ``` 190 ./nfs/dev_tools/bin/aa start -p com.example.launcher -n ServiceAbility 191 ``` 192 193 194### Dump系统属性在LiteOS-M内核平台使用: 195 1961. 连接单板,通过串口向单板发送AT+SYSPARA命令。 197 198 ``` 199 AT+SYSPARA 200 ``` 201 202 **图2** LiteOS-M平台dump系统属性输出 203 204 ![zh-cn_image_0000001115123966](figures/zh-cn_image_0000001115123966.png) 205 206 207### Dump系统属性在LiteOS-A内核平台使用: 208 2091. 连接单板,运行bin路径下的os_dump加参数--help,查看os_dump使用指导。 210 211 ``` 212 ./bin/os_dump --help 213 ``` 214 2152. os_dump加参数-l,查看当前系统有哪些模块支持获取属性。 216 217 ``` 218 ./bin/os_dump -l 219 ``` 220 2213. os_dump加参数syspara,查看当前系统属性。 222 223 ``` 224 ./bin/os_dump syspara 225 ``` 226 227 **图3** LiteOS-A平台dump系统属性输出 228 229 ![zh-cn_image_0000001197369959](figures/zh-cn_image_0000001197369959.png) 230