公共基础库开发指导

About 6 min

公共基础库开发指导

接口说明

表 1 文件操作接口说明

接口名

描述

int UtilsFileOpen(const char* path, int oflag, int mode)

打开或创建文件

int UtilsFileClose(int fd)

关闭文件

int UtilsFileRead(int fd, char *buf, unsigned int len)

读取特定长度的文件数据

int UtilsFileWrite(int fd, const char buf, unsigned int len)

向文件写入特定大小的数据

int UtilsFileDelete(const char path)

删除指定文件

int UtilsFileStat(const char path, unsigned int fileSize)

获取文件大小

int UtilsFileSeek(int fd, int offset, unsigned int whence)

重新定位文件读/写偏移量

int UtilsFileCopy(const char src, const char dest)

将源文件复制一份并存储到目标文件

int UtilsFileMove(const char src, const char dest)

将源文件移动到指定目标文件

文件操作使用示例:

// open && write
char fileName[] = "testfile";
static const char def[] = "utils_file_operation implement.";
int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
printf("file handle = %d\n", fd);
int ret = UtilsFileWrite(fd, def, strlen(def));
printf("write ret = %d\n", ret);

// seek
ret = UtilsFileSeek(fd, 5, SEEK_SET_FS);
printf("lseek ret = %d\n", ret);

// read && close
char buf[64] = {0};
int readLen = UtilsFileRead(fd, buf, 64);
ret = UtilsFileClose(fd);
printf("read len = %d : buf = %s\n", readLen, buf);

// stat
int fileLen = 0;
ret = UtilsFileStat(fileName, &fileLen);
printf("file size = %d\n", fileLen);

// delete
ret = UtilsFileDelete(fileName);
printf("delete ret = %d\n", ret);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

表 2 KV存储接口说明

接口名

描述

int UtilsGetValue(const char* key, char* value, unsigned int len)

提供给上层应用根据key获取对应数据项

int UtilsSetValue(const char* key, const char* value)

提供给上层应用用于存储/更新key对应数据项

int UtilsDeleteValue(const char* key)

提供给上层应用删除key对应数据项

KV存储使用示例:

// set
char key[] = "rw.sys.version_100";
char value[] = "Hello kv operation implement!";
int ret = UtilsSetValue(key, value);
printf("UtilsSetValue set ret = %d\n", ret);

// get
char temp[128] = {0};
ret = UtilsGetValue(key, temp, 128);
printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp);

// delete
ret = UtilsDeleteValue(key);
printf("UtilsDeleteValue delete ret = %d\n", ret);
1
2
3
4
5
6
7
8
9
10
11
12
13
14

开发步骤

LiteOS-A内核(Hi3516、Hi3518平台)KV存储的native应用开发步骤:

  1. 基于AbilityKit开发KV存储的native应用。

    • 基于KV存储提供的接口编写用户程序,并编译出so(libLauncher.so)文件。

      // set
      char key[] = "rw.sys.version_100";
      char value[] = "Hello kv operation implement!";
      int ret = UtilsSetValue(key, value);
      printf("UtilsSetValue set ret = %d\n", ret);
      
      // get
      char temp[128] = {0};
      ret = UtilsGetValue(key, temp, 128);
      printf("UtilsGetValue get ret = %d, temp = %s\n", ret, temp);
      
      // delete
      ret = UtilsDeleteValue(key);
      printf("UtilsDeleteValue delete ret = %d\n", ret);
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
    • 编写config.json文件,内容如下:

      {
          "app": {
              "bundleName": "com.huawei.launcher",
              "vendor": "huawei",
              "version": {
                  "code": 1,
                  "name": "1.0"
              }
          },
          "deviceConfig": {
              "default": {
                  "reqSdk": {
                      "compatible": "zsdk 1.0.0",
                      "target": "zsdk 1.0.1"
                  },
                  "keepAlive": false
              },
              "smartCamera": {
                  "reqSdk": {
                      "compatible": "zsdk 1.0.0",
                      "target": "zsdk 1.0.1"
                  },
                  "keepAlive": false
              }
          },
          "module": {
              "package": "com.huawei.launcher",
              "name": ".MyHarmonyAbilityPackage",
              "deviceType": [
                  "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartCamera"
              ], 
              "distro": {
                  "deliveryWithInstall": true, 
                  "moduleName": "Launcher", 
                  "moduleType": "entry"
              },
              "abilities": [{
                  "name": "MainAbility",
                  "icon": "res/drawable/phone.png",
                  "label": "test app 1", 
                  "launchType": "standard",
                  "type": "page"
              },
              {
                  "name": "SecondAbility",
                  "icon": "res/drawable/phone.png",
                  "label": "test app 2", 
                  "launchType": "standard",
                  "type": "page"
              },
              {
                  "name": "ServiceAbility",
                  "icon": "res/drawable/phone.png",
                  "label": "test app 2", 
                  "launchType": "standard",
                  "type": "service"
              }
              ]
          }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
    • 生成hap包。

      • 按照如下目录结构存放文件,res/drawable下面放置资源文件:

      • 将上述文件打包生成zip包,修改后缀为.hap,例如Launcher.hap
  2. 连接单板,通过串口向单板发送安装KV存储native应用的命令。

    ./nfs/dev_tools/bin/bm install -p /nfs/Launcher.hap
    
    1
  3. 通过串口向单板发送运行KV存储native应用的命令。

    ./nfs/dev_tools/bin/aa start -p com.huawei.launcher -n ServiceAbility
    
    1

Dump系统属性在LiteOS-M内核平台使用指南:

  1. 连接单板,通过串口向单板发送AT+SYSPARA命令。

    AT+SYSPARA
    
    1

    图 1 LiteOS-M平台dump系统属性输出

Dump系统属性在LiteOS-A内核平台使用指南:

  1. 连接单板,运行bin路径下的os_dump加参数--help,查看os_dump使用指导。

    ./bin/os_dump --help
    
    1
  2. os_dump加参数-l,查看当前系统有哪些模块支持获取属性。

    ./bin/os_dump -l
    
    1
  3. os_dump加参数syspara,查看当前系统属性

    ./bin/os_dump syspara
    
    1

    图 2 LiteOS-A平台dump系统属性输出