1# 轻量级公共基础库<a name="ZH-CN_TOPIC_0000001121139825"></a> 2 3- [简介](#section62661387114) 4- [目录](#section1464106163817) 5- [使用](#section25021123178) 6- [涉及仓](#section4571352889) 7 8## 简介<a name="section62661387114"></a> 9 10轻量级公共基础库存放OpenHarmony通用的基础组件。这些基础组件可被OpenHarmony各业务子系统及上层应用所使用。 11 12轻量级公共基础库在不同平台上提供的能力: 13 14- LiteOS-M内核\(Hi3861平台\):文件操作、定时器。 15- LiteOS-A内核\(Hi3516、Hi3518平台\):定时器、JS API\(设备查询,数据存储\)。 16 17**表 1** 轻量级公共基础库功能 18 19<a name="zh-cn_topic_0000001052623010_table206292206282"></a> 20<table><thead align="left"><tr id="zh-cn_topic_0000001052623010_row8629020112819"><th class="cellrowborder" valign="top" width="21.22%" id="mcps1.2.4.1.1"><p id="zh-cn_topic_0000001052623010_p66291220192816"><a name="zh-cn_topic_0000001052623010_p66291220192816"></a><a name="zh-cn_topic_0000001052623010_p66291220192816"></a>模块</p> 21</th> 22<th class="cellrowborder" valign="top" width="24.77%" id="mcps1.2.4.1.2"><p id="zh-cn_topic_0000001052623010_p3629122014289"><a name="zh-cn_topic_0000001052623010_p3629122014289"></a><a name="zh-cn_topic_0000001052623010_p3629122014289"></a>平台支持</p> 23</th> 24<th class="cellrowborder" valign="top" width="54.010000000000005%" id="mcps1.2.4.1.3"><p id="zh-cn_topic_0000001052623010_p206298206288"><a name="zh-cn_topic_0000001052623010_p206298206288"></a><a name="zh-cn_topic_0000001052623010_p206298206288"></a>说明</p> 25</th> 26</tr> 27</thead> 28<tbody> 29<tr id="zh-cn_topic_0000001052623010_row116301920152816"><td class="cellrowborder" valign="top" width="21.22%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0000001052623010_p96300207286"><a name="zh-cn_topic_0000001052623010_p96300207286"></a><a name="zh-cn_topic_0000001052623010_p96300207286"></a>文件操作</p> 30</td> 31<td class="cellrowborder" valign="top" width="24.77%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0000001052623010_p1463032012281"><a name="zh-cn_topic_0000001052623010_p1463032012281"></a><a name="zh-cn_topic_0000001052623010_p1463032012281"></a>LiteOS-M内核</p> 32</td> 33<td class="cellrowborder" valign="top" width="54.010000000000005%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0000001052623010_p163042052810"><a name="zh-cn_topic_0000001052623010_p163042052810"></a><a name="zh-cn_topic_0000001052623010_p163042052810"></a>提供统一的文件操作接口,屏蔽对底层不同芯片组件的差异。</p> 34</td> 35</tr> 36<tr id="zh-cn_topic_0000001052623010_row1163022022812"><td class="cellrowborder" valign="top" width="21.22%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0000001052623010_p66308202284"><a name="zh-cn_topic_0000001052623010_p66308202284"></a><a name="zh-cn_topic_0000001052623010_p66308202284"></a>定时器</p> 37</td> 38<td class="cellrowborder" valign="top" width="24.77%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0000001052623010_p26301620192820"><a name="zh-cn_topic_0000001052623010_p26301620192820"></a><a name="zh-cn_topic_0000001052623010_p26301620192820"></a>LiteOS-M内核、LiteOS-A内核</p> 39</td> 40<td class="cellrowborder" valign="top" width="54.010000000000005%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0000001052623010_p2630172062815"><a name="zh-cn_topic_0000001052623010_p2630172062815"></a><a name="zh-cn_topic_0000001052623010_p2630172062815"></a>提供统一的定时器操作接口,屏蔽对底层不同芯片组件的差异。</p> 41</td> 42</tr> 43<tr id="zh-cn_topic_0000001052623010_row363012202282"><td class="cellrowborder" valign="top" width="21.22%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0000001052623010_p4502105418285"><a name="zh-cn_topic_0000001052623010_p4502105418285"></a><a name="zh-cn_topic_0000001052623010_p4502105418285"></a>JS API</p> 44</td> 45<td class="cellrowborder" valign="top" width="24.77%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0000001052623010_p199870135293"><a name="zh-cn_topic_0000001052623010_p199870135293"></a><a name="zh-cn_topic_0000001052623010_p199870135293"></a>LiteOS-A内核</p> 46</td> 47<td class="cellrowborder" valign="top" width="54.010000000000005%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0000001052623010_p1550005422814"><a name="zh-cn_topic_0000001052623010_p1550005422814"></a><a name="zh-cn_topic_0000001052623010_p1550005422814"></a>提供获取设备信息,数据存储的JS API。</p> 48</td> 49</tr> 50</tbody> 51</table> 52 53## 目录<a name="section1464106163817"></a> 54 55``` 56commonlibrary/utils_lite/ # 轻量级公共基础库根目录 57├── file # 文件接口实现 58├── hals # HAL目录 59│ └── file # 文件操作硬件抽象层头文件 60├── include # 公共基础库对外接口文件 61├── js # JS API目录 62│ └── builtin 63│ ├── common # Builtin公共函数 64│ ├── deviceinfokit # 设备信息Kit 65│ ├── filekit # 文件Kit 66│ └── kvstorekit # KV存储Kit 67├── kal # KAL目录 68│ └── timer # Timer的KAL实现 69├── memory 70│ └── include # 内存池管理接口 71└── timer_task # Timer实现 72``` 73 74## 使用<a name="section25021123178"></a> 75 76- **文件操作** 77 78 ``` 79 // 打开或创建文件 80 const char fileName[] = "testfile"; 81 int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0); 82 83 // 向文件写入特定大小的数据 84 const char defValue[] = "test case of file system."; 85 int ret = UtilsFileWrite(fd, defValue, strlen(defValue)); 86 87 // 关闭文件 88 UtilsFileClose(fd); 89 90 // 获取文件大小 91 int fileLen = 0; 92 ret = UtilsFileStat(fileName, &fileLen); 93 printf("file size = %d\n", fileLen); 94 95 // 重新定位文件读/写偏移量 96 int fd1 = UtilsFileOpen(fileName, O_RDWR_FS, 0); 97 ret = UtilsFileSeek(fd1, 5, SEEK_SET_FS); 98 99 // 读取特定长度的文件数据 100 char buf[32] = {0}; 101 int readLen = UtilsFileRead(fd1, buf, 32); 102 ret = UtilsFileClose(fd1); 103 printf("read len = %d : buf = %s\n", readLen, buf); 104 105 // 删除指定文件 106 ret = UtilsFileDelete(fileName); 107 ``` 108 109 110 111## 涉及仓<a name="section4571352889"></a> 112 113[公共基础](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E5%85%AC%E5%85%B1%E5%9F%BA%E7%A1%80%E5%BA%93.md) 114 115[**commonlibrary\_utils\_lite**](https://gitee.com/openharmony/commonlibrary_utils_lite)