diff --git a/OAT.xml b/OAT.xml index f602634..20ec23c 100755 --- a/OAT.xml +++ b/OAT.xml @@ -65,7 +65,7 @@ - + @@ -83,12 +83,18 @@ + + + + + + @@ -97,6 +103,8 @@ + + diff --git a/README.md b/README.md deleted file mode 100755 index 4f0562e..0000000 --- a/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# device_board_bearpi - -### 介绍 - -本仓用于放置BearPi开发板板相关内容,详情请参见各开发板readme。 - -[轻量系统BearPi-HM Nano开发板介绍](bearpi_hm_nano/docs/quick-start/BearPi-HM_Nano开发板介绍.md) - - -### 开发环境搭建 - -开发环境搭建教程请参见各开发板readme。 - -[轻量系统BearPi-HM Nano开发环境搭建](bearpi_hm_nano/docs/quick-start/BearPi-HM_Nano开发搭建环境.md) - - -### 编译调试 - -请参见各开发板readme。 - -[轻量系统BearPi-HM Nano开发板编译调试](bearpi_hm_nano/docs/quick-start/BearPi-HM_Nano开发板编译调试.md) - - diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000..7363d26 --- /dev/null +++ b/README_zh.md @@ -0,0 +1,54 @@ +# device_board_bearpi +- [简介](#section469617221261) +- [开发环境搭建](#section469617221262) +- [编译调试](#section469617221263) +- [目录](#section161941989596) +- [相关仓](#section641143415335) +## 简介 + +本仓用于放置小熊派BearPi开发板相关内容,目前,小熊派已有两款开发板适配OpenHarmony,分别是基于海思Hi3861的轻量系统BearPi-HM Nano开发板和基于意法半导体STM32MP157的小型系统BearPi-HM Micro开发板,详情请参见各开发板readme。 + +[轻量系统BearPi-HM Nano开发板介绍](bearpi_hm_nano/docs/quick-start/BearPi-HM_Nano开发板介绍.md) + + +[小型系统BearPi-HM Micro开发板介绍](bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板介绍.md) + +## 开发环境搭建 + +开发环境搭建教程请参见各开发板readme。 + +[轻量系统BearPi-HM Nano开发环境搭建](bearpi_hm_nano/docs/quick-start/BearPi-HM_Nano开发搭建环境.md) + +[小型系统BearPi-HM Micro开发环境搭建](bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发环境搭建.md) + +## 编译调试 + +请参见各开发板readme。 + +[轻量系统BearPi-HM Nano开发板编译调试](bearpi_hm_nano/docs/quick-start/BearPi-HM_Nano开发板编译调试.md) + +[小型系统BearPi-HM Micro开发板编译调试](bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板编译调试.md) +## 目录 +``` +/device/board/bearpi +├── bearpi_hm_micro # 小熊派小型系统STM32MP157开发板 + └── app # 应用案例 + └── docs # 文档 + └── hal # 外设HDI适配 + └── liteos_a # liteos_a 内核适配 + └── tools # tf-a、uboot镜像 +├── bearpi_hm_nano # 小熊派轻量系统Hi3861开发板 + └── app # 应用案例 + └── config # 驱动差异化使能配置 + └── docs # 文档 + └── iot_hardware_hals # 外设适配 + └── liteos_m # liteos_m 内核适配 + └── third_party # 三方库 +``` +## 相关仓 + +**[device_board_bearpi](https://gitee.com/openharmony/device_board_bearpi)** + +[device_soc_st](https://gitee.com/openharmony/device_soc_st) + +[vendor_bearpi](https://gitee.com/openharmony/vendor_bearpi) diff --git a/bearpi_hm_micro/BUILD.gn b/bearpi_hm_micro/BUILD.gn new file mode 100644 index 0000000..3919244 --- /dev/null +++ b/bearpi_hm_micro/BUILD.gn @@ -0,0 +1,15 @@ +# Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("bearpi_hm_micro") { +} diff --git a/bearpi_hm_micro/README.md b/bearpi_hm_micro/README.md new file mode 100644 index 0000000..4edb37b --- /dev/null +++ b/bearpi_hm_micro/README.md @@ -0,0 +1,42 @@ + +--- +# 小熊派-鸿蒙·叔(BearPi-HM Micro) + +## 一、BearPi-HM Micro 开发板介绍 + +BearPi-HM Micro开发板是一块高度集成并可运行OpenHarmony系统的开发板,板载高性能的工业级处理器STM32MP157芯片,搭配4.3寸LCD电容式触摸屏,并板载wifi电路及标准的E53接口,标准的E53接口可扩展智能加湿器、智能台灯、智能安防、智能烟感等案例。可折叠式屏幕设计大大提高用户开发体验,便于携带和存放,更好地满足不同用户的需求,拓展无限可能。 + +## 二、设备开发手把手教程系列 + +1、视频系列(B站) 对应PPT地址 +* [P1. 初识BearPi-HM Micro开发板](https://www.bilibili.com/video/BV12Y411H7ry?p=1) +* [P2. 开发环境搭建(复杂)——编译环境搭建](https://www.bilibili.com/video/BV12Y411H7ry?p=3) +* [P3. 开发环境搭建(复杂)——源码获取及编译](https://www.bilibili.com/video/BV12Y411H7ry?p=4) +* [P4. BearPi-HM Micro开发板源码烧录](https://www.bilibili.com/video/BV12Y411H7ry?p=5) +* [P5. OpenHarmony 快速入门:HelloWorld](https://www.bilibili.com/video/BV12Y411H7ry?p=6) +* [P6. OpenHarmony 快速入门:点亮LED](https://www.bilibili.com/video/BV12Y411H7ry?p=7) +* [P7. OpenHarmony 快速入门:安装HAP](https://www.bilibili.com/video/BV12Y411H7ry?p=8) +* [P8. OpenHarmony 快速入门:JS点亮LED](https://www.bilibili.com/video/BV12Y411H7ry?p=9) + +2、文档系列(Gitee) +- [1. 初识BearPi-HM Micro开发板](docs/device-dev/BearPi-HM_Micro开发板介绍.md) +- [2. 开发环境搭建](docs/device-dev/BearPi-HM_Micro开发环境搭建.md) +- [3. 源码获取、编译烧录](docs/device-dev/BearPi-HM_Micro开发板编译调试.md) +- [4. 编写一个hello_world程序](docs/device-dev/编写一个hello_world程序.md) +- [5. 如何运行一个可执行程序](docs/device-dev/如何运行一个可执行程序.md) +- [6. 编写一个点亮LED灯程序](docs/device-dev/编写一个点亮LED灯程序.md) +- [7. 通过JS应用控制LED灯](docs/device-dev/通过JS应用控制LED灯.md) +- [8. 如何在开发板上安装HAP应用](docs/device-dev/如何在开发板上安装HAP应用.md) + + +## 三、开发板图片预览[(淘宝链接)](https://item.taobao.com/item.htm?id=662078665554) + +[![](docs/figures/BearPi-HM_Micro_Info.png)](https://item.taobao.com/item.htm?id=633296694816) + + +## 四、联系我们 + + + +![](docs/figures/bearpi_club_wechat.jpg) + diff --git a/bearpi_hm_micro/app/LICENSE b/bearpi_hm_micro/app/LICENSE new file mode 100644 index 0000000..4a45986 --- /dev/null +++ b/bearpi_hm_micro/app/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/bearpi_hm_micro/app/OAT.xml b/bearpi_hm_micro/app/OAT.xml new file mode 100644 index 0000000..3a49a27 --- /dev/null +++ b/bearpi_hm_micro/app/OAT.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bearpi_hm_micro/app/bundle.json b/bearpi_hm_micro/app/bundle.json new file mode 100644 index 0000000..4b95613 --- /dev/null +++ b/bearpi_hm_micro/app/bundle.json @@ -0,0 +1,72 @@ +{ + "name": "@ohos/bearpi_hm_micro_sample", + "version": "3.1", + "description": "bearpi_hm_micro samples.", + "homePage": "https://gitee.com/openharmony", + "license": "Apache License 2.0", + "repository": "", + "domain": "os", + "language": "", + "publishAs": "", + "private": false, + "scripts": {}, + "envs": [], + "dirs": [ + "device/board/bearpi/bearpi_hm_micro/app/launcher", + "device/board/bearpi/bearpi_hm_micro/app/setting", + "device/board/bearpi/bearpi_hm_micro/app/screensaver", + "device/board/bearpi/bearpi_hm_micro/app/communication" + ], + "component": { + "name": "bearpi_hm_micro_sample", + "subsystem": "bearpi_hm_micro_sample", + "syscap": [ + "" + ], + "features": [], + "adapted_system_type": [ + "liteos_a" + ], + "rom": "22KB", + "ram": "~10KB", + "output": [ + "launcher.so", + "setting.so", + "screensaver.so" + ], + "deps": { + "components": [ + "aafwk_lite", + "appexecfwk_lite", + "surface", + "ui", + "graphic_utils", + "kv_store", + "syspara_lite", + "permission", + "ipc_lite", + "samgr_lite", + "utils_base" + ], + "third_party": [ + "bounds_checking_function", + "wpa_supplicant", + "cjson", + "giflib", + "libjpeg", + "libpng" + ] + }, + "build": { + "sub_component": [ + "//device/board/bearpi/bearpi_hm_micro/app/launcher:launcher_hap", + "//device/board/bearpi/bearpi_hm_micro/app/setting:setting_hap", + "//device/board/bearpi/bearpi_hm_micro/app/screensaver:screensaver_hap", + "//device/board/bearpi/bearpi_hm_micro/app/communication:sample" + ], + "inner_kits": [], + "test": [] + } + } + } + \ No newline at end of file diff --git a/bearpi_hm_micro/app/communication/BUILD.gn b/bearpi_hm_micro/app/communication/BUILD.gn new file mode 100644 index 0000000..266dcb0 --- /dev/null +++ b/bearpi_hm_micro/app/communication/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +lite_component("sample") { + features = [ + "hostapd:hostapd_sample", + "wpa_cli:wpa_cli_sample", + "wpa_supplicant:wpa_sample", + ] +} + +generate_notice_file("communication_sample") { + module_name = "communication_sample" + module_source_dir_list = [ + "//third_party/wpa_supplicant/wpa_supplicant-2.9/hostapd", + "//third_party/wpa_supplicant/wpa_supplicant-2.9/wpa_supplicant", + ] +} diff --git a/bearpi_hm_micro/app/communication/LICENSE b/bearpi_hm_micro/app/communication/LICENSE new file mode 100644 index 0000000..4a45986 --- /dev/null +++ b/bearpi_hm_micro/app/communication/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/bearpi_hm_micro/app/communication/README.md b/bearpi_hm_micro/app/communication/README.md new file mode 100644 index 0000000..519f9bc --- /dev/null +++ b/bearpi_hm_micro/app/communication/README.md @@ -0,0 +1,11 @@ +## Introduction +This directory is the sample code of the communication subsystem. For now, it contains the usage example of wpa_supplicant. +## Directions + +``` +. +├── hostapd +├── wpa_cli +└── wpa_supplicant + +``` diff --git a/bearpi_hm_micro/app/communication/hostapd/BUILD.gn b/bearpi_hm_micro/app/communication/hostapd/BUILD.gn new file mode 100644 index 0000000..66ac1c9 --- /dev/null +++ b/bearpi_hm_micro/app/communication/hostapd/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +sample_sources = [ "src/hostapd_sample.c" ] + +config_file = [ "config/hostapd.conf" ] + +executable("hostapd_sample_exe") { + output_name = "hostapd" + sources = sample_sources +} + +copy("config") { + sources = config_file + outputs = [ "$root_out_dir/etc/hostapd.conf" ] +} + +group("hostapd_sample") { + deps = [ + ":config", + ":hostapd_sample_exe", + ] +} diff --git a/bearpi_hm_micro/app/communication/hostapd/config/hostapd.conf b/bearpi_hm_micro/app/communication/hostapd/config/hostapd.conf new file mode 100644 index 0000000..bc6057c --- /dev/null +++ b/bearpi_hm_micro/app/communication/hostapd/config/hostapd.conf @@ -0,0 +1,7 @@ +interface=wlan0 +driver=hdf wifi +ctrl_interface=udp +ssid=testap +hw_mode=g +channel=1 +ignore_broadcast_ssid=0 diff --git a/bearpi_hm_micro/app/communication/hostapd/src/hostapd_sample.c b/bearpi_hm_micro/app/communication/hostapd/src/hostapd_sample.c new file mode 100644 index 0000000..fa1975b --- /dev/null +++ b/bearpi_hm_micro/app/communication/hostapd/src/hostapd_sample.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +pthread_t g_apThread; + +char* g_apArg[20] = {0}; +int g_apArgc = 0; + +static void* ThreadMain() +{ + printf("[HostapdSample]init hostapd.\n"); + + void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL); + if (handleLibWpa == NULL) { + printf("[HostapdSample]dlopen libwpa failed.\n"); + return NULL; + } + int (*func)(int, char **) = NULL; + func = dlsym(handleLibWpa, "ap_main"); + if (func == NULL) { + dlclose(handleLibWpa); + printf("[HostapdSample]dlsym ap_main failed.\n"); + return NULL; + } + int ret = func(g_apArgc, g_apArg); + + printf("[HostapdSample]run ap_main failed, ret:%d.\n", ret); + for (int i = 0; i < g_apArgc; i++) { + printf("[HostapdSample]arg %d:%s.\n", i, g_apArg[i]); + } + + if (dlclose(handleLibWpa) != 0) { + printf("[HostapdSample]dlclose libwpa failed.\n"); + return NULL; + } + return NULL; +} + +int main(int argc, char *argv[]) +{ + g_apArgc = argc; + for (int i = 0; i < g_apArgc; i++) { + g_apArg[i] = argv[i]; + } + + int ret = pthread_create(&g_apThread, NULL, ThreadMain, NULL); + if (ret != 0) { + printf("[HostapdSample]create thread failed error:%s.\n", strerror(ret)); + return 1; + } + pthread_join(g_apThread, NULL); + return 0; +} diff --git a/bearpi_hm_micro/app/communication/wpa_cli/BUILD.gn b/bearpi_hm_micro/app/communication/wpa_cli/BUILD.gn new file mode 100644 index 0000000..0861061 --- /dev/null +++ b/bearpi_hm_micro/app/communication/wpa_cli/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +sample_sources = [ "src/wpa_cli_sample.c" ] + +sample_include_dirs = [ + "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/", + "//third_party/bounds_checking_function:libsec_shared/include/", +] + +executable("wpa_cli_exe") { + output_name = "wpa_cli" + sources = sample_sources + include_dirs = sample_include_dirs + out_dir = rebase_path(root_build_dir) + deps = [ + "//third_party/bounds_checking_function:libsec_shared", + "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", + ] + ldflags = [ + "-L${out_dir}", + "-lwpa_client", + ] +} + +group("wpa_cli_sample") { + deps = [ ":wpa_cli_exe" ] +} diff --git a/bearpi_hm_micro/app/communication/wpa_cli/src/wpa_cli_sample.c b/bearpi_hm_micro/app/communication/wpa_cli/src/wpa_cli_sample.c new file mode 100644 index 0000000..0895f79 --- /dev/null +++ b/bearpi_hm_micro/app/communication/wpa_cli/src/wpa_cli_sample.c @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/includes.h" +#include "pthread.h" +#include "common/wpa_ctrl.h" +#include "securec.h" + + +#define WPA_IFACE_NAME "wlan0" +#define WIFI_AUTH_FAILED_REASON_STR "WRONG_KEY" +#define WIFI_AUTH_FAILED_REASON_CODE "reason=2" +#define WPA_CTRL_REQUEST_OK "OK" +#define WPA_CTRL_REQUEST_FAIL "FAIL" + +#define SAMPLE_INFO(format, args...) \ + do { \ + fprintf(stderr, "\033[1;32m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + +#define SAMPLE_ERROR(format, args...) \ + do { \ + fprintf(stderr, "\033[1;31m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + + +static struct wpa_ctrl *g_monitorConn; +static struct wpa_ctrl *g_ctrlConn; +static pthread_t g_wpaThreadId; +static int g_scanAvailable = 0; + +static void DumpString(const char *buf, int len, const char *tag) +{ + SAMPLE_INFO("%s dump start.", tag); + for (int i = 0; i < len; i++) { + printf("%c", buf[i]); + } + printf("\n"); + SAMPLE_INFO("%s dump end.", tag); +} + +static int StrMatch(const char *a, const char *b) +{ + return strncmp(a, b, strlen(b)) == 0; +} + +static void WifiEventHandler(char *rawEvent, int len) +{ + char *pos = rawEvent; + if (*pos == '<') { + pos = strchr(pos, '>'); + if (pos) { + pos++; + } else { + pos = rawEvent; + } + } + if (StrMatch(pos, WPA_EVENT_CONNECTED)) { + SAMPLE_INFO("WIFI_EVENT_CONNECTED"); + return; + } + if (StrMatch(pos, WPA_EVENT_SCAN_RESULTS)) { + SAMPLE_INFO("WIFI_EVENT_SCAN_DONE"); + g_scanAvailable = 1; + return; + } + if (StrMatch(pos, WPA_EVENT_TEMP_DISABLED) && strstr(pos, WIFI_AUTH_FAILED_REASON_STR)) { + SAMPLE_INFO("WIFI_EVENT_WRONG_KEY"); + return; + } + if (StrMatch(pos, WPA_EVENT_DISCONNECTED) && !strstr(pos, WIFI_AUTH_FAILED_REASON_CODE)) { + SAMPLE_INFO("WIFI_EVENT_DISCONNECTED"); + return; + } +} + +static void CliRecvPending(void) +{ + while (wpa_ctrl_pending(g_monitorConn)) { + char buf[4096]; + size_t len = sizeof(buf) - 1; + if (wpa_ctrl_recv(g_monitorConn, buf, &len) == 0) { + buf[len] = '\0'; + SAMPLE_INFO("event received %s", buf); + WifiEventHandler(buf, len); + } else { + SAMPLE_INFO("could not read pending message."); + break; + } + } +} + +static void* MonitorTask(void *args) +{ + (void)args; + int fd, ret; + fd_set rfd; + while (1) { + fd = wpa_ctrl_get_fd(g_monitorConn); + FD_ZERO(&rfd); + FD_SET(fd, &rfd); + ret = select(fd + 1, &rfd, NULL, NULL, NULL); + if (ret <= 0) { + SAMPLE_INFO("select failed ret = %d\n", ret); + break; + } + CliRecvPending(); + sleep(1); + } + return NULL; +} + +static int SendCtrlCommand(const char *cmd, char *reply, size_t *replyLen) +{ + size_t len = *replyLen - 1; + wpa_ctrl_request(g_ctrlConn, cmd, strlen(cmd), reply, &len, 0); + DumpString(reply, len, "SendCtrlCommand raw return"); + if (len != 0 && !StrMatch(reply, WPA_CTRL_REQUEST_FAIL)) { + *replyLen = len; + return 0; + } + SAMPLE_ERROR("send ctrl request [%s] failed.", cmd); + return -1; +} + +static void TestNetworkConfig(void) +{ + char networkId[20] = {0}; + size_t networkIdLen = sizeof(networkId); + int ret = SendCtrlCommand("DISCONNECT", networkId, &networkIdLen); + ret += SendCtrlCommand("ADD_NETWORK", networkId, &networkIdLen); + if (ret != 0) { + SAMPLE_ERROR("add network failed."); + return; + } + SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, networkId); + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + char cmd[200] = {0}; + sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"example\"", networkIdLen, networkId); + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"012345678\"", networkIdLen, networkId); + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, networkId); + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + ret += SendCtrlCommand("RECONNECT", reply, &replyLen); + replyLen = sizeof(reply); + if (ret == 0) { + SAMPLE_INFO("network config success."); + return; + } + sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, networkId); + SendCtrlCommand(cmd, reply, &replyLen); + SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, networkId); +} + +static void TestCliConnection(void) +{ + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + int ret = SendCtrlCommand("PING", reply, &replyLen); + if (ret == 0 && StrMatch(reply, "PONG")) { + SAMPLE_INFO("connect to wpa success."); + return; + } + SAMPLE_INFO("connect to wpa failed, err = %s.", reply); +} + +static void TestScan() +{ + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + g_scanAvailable = 0; + SendCtrlCommand("SCAN", reply, &replyLen); + while (1) { + sleep(1); + if (g_scanAvailable == 1) { + SAMPLE_INFO("scan result received."); + break; + } + SAMPLE_INFO("waiting scan result."); + } + char scanResult[4096] = {0}; + size_t scanLen = sizeof(scanResult); + int ret = SendCtrlCommand("SCAN_RESULTS", scanResult, &scanLen); + if (ret != 0) { + SAMPLE_ERROR("request scan results failed."); + return; + } + DumpString(scanResult, scanLen, "scan results"); +} + +static void StartTest() +{ + TestCliConnection(); // test if wpa control interface connected successfully + TestScan(); // test scan and get scan results + TestNetworkConfig(); // test config network and connect +} + +int InitControlInterface() +{ + g_ctrlConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for send cmd + g_monitorConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for event monitor + if (!g_ctrlConn || !g_monitorConn) { + SAMPLE_ERROR("open wpa control interface failed."); + return -1; + } + if (wpa_ctrl_attach(g_monitorConn) == 0) { // start monitor + pthread_create(&g_wpaThreadId, NULL, MonitorTask, NULL); // create thread for read event + return 0; + } + return -1; +} + +int main() +{ + if (InitControlInterface() != 0) { + SAMPLE_ERROR("control interface init failed, exit client."); + return -1; + } + SAMPLE_INFO("control interface init success."); + StartTest(); + pthread_join(g_wpaThreadId, NULL); + SAMPLE_INFO("test finished, exit client."); +} diff --git a/bearpi_hm_micro/app/communication/wpa_supplicant/BUILD.gn b/bearpi_hm_micro/app/communication/wpa_supplicant/BUILD.gn new file mode 100644 index 0000000..f5450bf --- /dev/null +++ b/bearpi_hm_micro/app/communication/wpa_supplicant/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +sample_sources = [ "src/wpa_sample.c" ] + +config_file = [ "config/wpa_supplicant.conf" ] + +executable("wpa_sample_exe") { + output_name = "wpa_supplicant" + sources = sample_sources +} + +copy("config") { + sources = config_file + outputs = [ "$root_out_dir/etc/wpa_supplicant.conf" ] +} + +group("wpa_sample") { + deps = [ + ":config", + ":wpa_sample_exe", + ] +} diff --git a/bearpi_hm_micro/app/communication/wpa_supplicant/config/wpa_supplicant.conf b/bearpi_hm_micro/app/communication/wpa_supplicant/config/wpa_supplicant.conf new file mode 100644 index 0000000..7169d61 --- /dev/null +++ b/bearpi_hm_micro/app/communication/wpa_supplicant/config/wpa_supplicant.conf @@ -0,0 +1,6 @@ +country=GB +ctrl_interface=udp +network={ + ssid="bearpi" + psk="0987654321" +} diff --git a/bearpi_hm_micro/app/communication/wpa_supplicant/src/wpa_sample.c b/bearpi_hm_micro/app/communication/wpa_supplicant/src/wpa_sample.c new file mode 100644 index 0000000..a30d8f6 --- /dev/null +++ b/bearpi_hm_micro/app/communication/wpa_supplicant/src/wpa_sample.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +pthread_t g_wpaThread; + +char* g_wpaArg[20] = {0}; +int g_wpaArgc = 0; + +static void* ThreadMain() +{ + printf("[WpaSample]init wpa_supplicant.\n"); + + void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL); + if (handleLibWpa == NULL) { + printf("[WpaSample]dlopen libwpa failed.\n"); + return NULL; + } + int (*func)(int, char **) = NULL; + func = dlsym(handleLibWpa, "wpa_main"); + if (func == NULL) { + dlclose(handleLibWpa); + printf("[WpaSample]dlsym wpa_main failed.\n"); + return NULL; + } + int ret = func(g_wpaArgc, g_wpaArg); + + printf("[WpaSample]run wpa_main failed, ret:%d.\n", ret); + for (int i = 0; i < g_wpaArgc; i++) { + printf("[WpaSample]arg %d:%s.\n", i, g_wpaArg[i]); + } + + if (dlclose(handleLibWpa) != 0) { + printf("[WpaSample]dlclose libwpa failed.\n"); + return NULL; + } + return NULL; +} + +int main(int argc, char *argv[]) +{ + g_wpaArgc = argc; + for (int i = 0; i < g_wpaArgc; i++) { + g_wpaArg[i] = argv[i]; + } + + int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL); + if (ret != 0) { + printf("[WpaSample]create thread failed error:%s.\n", strerror(ret)); + return 1; + } + pthread_join(g_wpaThread, NULL); + return 0; +} diff --git a/bearpi_hm_micro/app/launcher/BUILD.gn b/bearpi_hm_micro/app/launcher/BUILD.gn new file mode 100644 index 0000000..d68095a --- /dev/null +++ b/bearpi_hm_micro/app/launcher/BUILD.gn @@ -0,0 +1,64 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/hap_pack.gni") + +shared_library("launcher") { + sources = [ + "launcher/src/main/cpp/app_info.cpp", + "launcher/src/main/cpp/app_manage.cpp", + "launcher/src/main/cpp/long_press_view.cpp", + "launcher/src/main/cpp/main_ability.cpp", + "launcher/src/main/cpp/main_ability_slice.cpp", + "launcher/src/main/cpp/swipe_view.cpp", + "launcher/src/main/cpp/time_weather_view.cpp", + "launcher/src/main/cpp/view_group_page.cpp", + ] + + deps = [ + "${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite", + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//foundation/graphic/surface", + "//foundation/graphic/ui:lite_ui", + "//foundation/graphic/utils:lite_graphic_utils", + "//utils/native/lite/kv_store:kv_store", + ] + + include_dirs = [ + "launcher/src/main/cpp", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", + "${aafwk_lite_path}/interfaces/kits/want_lite", + "//base/startup/syspara_lite/interfaces/kits", + ] + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + ] +} + +hap_pack("launcher_hap") { + deps = [ ":launcher" ] + mode = "hap" + json_path = "launcher/src/main/config.json" + ability_so_path = "$root_out_dir/liblauncher.so" + force = "true" + cert_profile = "cert/com.huawei.launcher_HarmonyAppProvision_release.p7b" + resources_path = "launcher/src/main/resources" + hap_name = "launcher" + privatekey = "HOS Application Provision Release" +} diff --git a/bearpi_hm_micro/app/launcher/cert/com.huawei.launcher_HarmonyAppProvision_release.p7b b/bearpi_hm_micro/app/launcher/cert/com.huawei.launcher_HarmonyAppProvision_release.p7b new file mode 100644 index 0000000..fb05882 Binary files /dev/null and b/bearpi_hm_micro/app/launcher/cert/com.huawei.launcher_HarmonyAppProvision_release.p7b differ diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/config.json b/bearpi_hm_micro/app/launcher/launcher/src/main/config.json new file mode 100644 index 0000000..d319aed --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/config.json @@ -0,0 +1,43 @@ +{ + "app": { + "bundleName": "com.huawei.launcher", + "vendor": "huawei", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + "keepAlive": true + }, + "smartCamera": { + "keepAlive": true + } + }, + "module": { + "package": "com.huawei.launcher", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", "tv","tablet","car","smartWatch","sportsWatch","smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "launcher", + "moduleType": "entry" + }, + "abilities": [{ + "name": "MainAbility", + "icon": "assets/launcher/resources/base/media/background.png", + "label": "launcher", + "launchType": "standard", + "type": "page", + "visible":true + } + ] + } +} diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_info.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_info.cpp new file mode 100644 index 0000000..f2d2036 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_info.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "app_info.h" +#include "ui_config.h" + +namespace OHOS { +AppInfo::AppInfo() +{ + ReSet(); +} +void AppInfo::Release() +{ + if (button_) { + delete button_; + button_ = nullptr; + } + if (lable_) { + delete lable_; + lable_ = nullptr; + } + if (appClickListener_) { + delete appClickListener_; + appClickListener_ = nullptr; + } + if (appLpListener_) { + delete appLpListener_; + appLpListener_ = nullptr; + } +} + +AppInfo::~AppInfo() +{ + Release(); +} + +void AppInfo::ReSet() +{ + button_ = nullptr; + lable_ = nullptr; + appClickListener_ = nullptr; + appLpListener_ = nullptr; +} + +void AppInfo::SetButton(UILabelButton* button) +{ + button->SetPosition(buttonXY_.x, buttonXY_.y, buttonHV_.x, buttonHV_.y); + button->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + button->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED); + button->SetStyleForState(STYLE_BACKGROUND_OPA, TOTAL_OPACITY, UIButton::PRESSED); + button->SetStyleForState(STYLE_BORDER_OPA, TOTAL_OPACITY, UIButton::PRESSED); + button->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS); + button->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + button->SetStyle(STYLE_BORDER_OPA, TOTAL_OPACITY); + button->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + button->SetImageSrc(appIconDir_, appIconDir_); + button_ = button; +} + +void AppInfo::SetLable(UILabel* lable) +{ + lable->SetPosition(lableXY_.x, lableXY_.y, lableHV_.x, lableHV_.y); + char* p = strrchr(appName_, static_cast('.')); + if (p != nullptr) { + lable->SetText(p + 1); + } else { + lable->SetText(appName_); + } + lable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_TOP); + lable->SetFont(FOND_PATH, APP_FOND_ID); + lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + lable_ = lable; +} + +void AppInfo::SetListener(AppInfo* app) +{ + AppClickListener* clickListener = new AppClickListener(app->funcclick_, this); + button_->SetOnClickListener(clickListener); + appClickListener_ = static_cast(clickListener); + AppLongPressListener* lPListener = new AppLongPressListener(app->funclPress_, this); + button_->SetOnLongPressListener(lPListener); + appLpListener_ = static_cast(lPListener); + button_->SetDraggable(true); +} + +void AppInfo::SetLocation(int16_t r, int16_t c) +{ + row_col_.x = r; + row_col_.y = c; +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_info.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_info.h new file mode 100644 index 0000000..fab38e3 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_info.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_APP_INFO_H +#define OHOS_APP_INFO_H + +#include +#include +#include + +#include "native_base.h" +#include "ui_config.h" + +namespace OHOS { + +class AppInfo; +using funcLongPress = bool (*)(AppInfo *app); +using funcClick = bool (*)(AppInfo *app); +using UninstallApp = bool (*)(AppInfo *app); +using AddApp = bool (*)(AppInfo *app); + +struct MyPoint { + int16_t x; // the x coordinate of the point + int16_t y; // the y coordinate of the point +}; + +class AppInfo { +public: + AppInfo(); + virtual ~AppInfo(); + void Release(); + void ReSet(); + void SetButton(UILabelButton *button); + void SetLable(UILabel *lable); + void SetListener(AppInfo *app); + void SetLocation(int16_t r, int16_t c); + + UILabelButton* button_ { nullptr }; + UILabel* lable_ { nullptr }; + + UIView::OnLongPressListener* appLpListener_ { nullptr }; + UIView::OnClickListener* appClickListener_ { nullptr }; + + funcClick funcclick_ { nullptr }; + funcLongPress funclPress_ { nullptr }; + MyPoint lableXY_ { 0 }; + MyPoint lableHV_ { 0 }; + MyPoint buttonXY_ { 0 }; + MyPoint buttonHV_ { 0 }; + MyPoint row_col_ { 0 }; + char appName_[TMP_BUF_SIZE] = { 0 }; + char abilityName_[TMP_BUF_SIZE] = { 0 }; + char appIconDir_[TMP_BUF_SIZE] = { 0 }; +}; + +class AppClickListener : public UIView::OnClickListener { +public: + AppClickListener(funcClick func, AppInfo* app) : funcClick_(func), appInfo_(app) {} + virtual ~AppClickListener() {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + funcClick_(appInfo_); + return true; + } + +private: + funcClick funcClick_ { nullptr }; + AppInfo *appInfo_ { nullptr }; +}; + +class AppLongPressListener : public UIView::OnLongPressListener { +public: + AppLongPressListener(funcClick func, AppInfo* app) : appInfo_(app), funcLongPress_(func) {} + virtual ~AppLongPressListener() {} + bool OnLongPress(UIView& view, const LongPressEvent& event) override + { + funcLongPress_(appInfo_); + return true; + } + +private: + AppInfo* appInfo_ { nullptr }; + funcLongPress funcLongPress_ { nullptr }; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_manage.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_manage.cpp new file mode 100644 index 0000000..08e24fc --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_manage.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "app_manage.h" + +namespace OHOS { +ViewGroupPage* AppManage::viewPage_[MAX_VIEWGROUP] = { nullptr }; +funcClick AppManage::installFuncClick_ = { nullptr }; +funcLongPress AppManage::installFunclPress_ = { nullptr }; +int AppManage::size_ = 0; + +AppManage::~AppManage() +{ + UnregisterCallback(); +} + +bool AppManage::GetAbilityInfosByBundleName(const char* bundleName, AppInfo* pApp) +{ + if (bundleName == nullptr || pApp == nullptr) { + return false; + } + uint8_t ret = -1; + BundleInfo* pBundleInfos = nullptr; + int count = 0; + ret = GetBundleInfos(1, &pBundleInfos, &count); + if (ret == 0) { + BundleInfo* pBundleInfo = pBundleInfos; + for (int i = 0; i < count; i++, pBundleInfo++) { + if (memcmp(bundleName, pBundleInfo->bundleName, strlen(pBundleInfo->bundleName)) == 0) { + memcpy_s( + pApp->appName_, sizeof(pApp->appName_), pBundleInfo->bundleName, strlen(pBundleInfo->bundleName)); + pApp->appName_[strlen(pBundleInfo->bundleName)] = 0; + if (pBundleInfo->abilityInfos[0].name) { + memcpy_s(pApp->abilityName_, sizeof(pApp->abilityName_), pBundleInfo->abilityInfos[0].name, + strlen(pBundleInfo->abilityInfos[0].name)); + pApp->abilityName_[strlen(pBundleInfo->abilityInfos[0].name)] = 0; + } + if (pBundleInfo->bigIconPath) { + memcpy_s(pApp->appIconDir_, sizeof(pApp->appIconDir_), pBundleInfo->bigIconPath, + strlen(pBundleInfo->bigIconPath)); + pApp->appIconDir_[strlen(pBundleInfo->bigIconPath)] = 0; + } + return true; + } + } + } + return false; +} + +bool AppManage::GetAppInstallInfo(const char* bundleName) +{ + if (bundleName == nullptr) { + return false; + } + AppInfo* pApp = new AppInfo(); + pApp->funcclick_ = installFuncClick_; + pApp->funclPress_ = installFunclPress_; + if (GetAbilityInfosByBundleName(bundleName, pApp) == true) { + int i = 0; + for (; i < size_; i++) { + if (viewPage_[i]->FindApp(pApp)) { + break; + } + } + if (i == size_) { + for (i = 0; i < size_; i++) { + if (viewPage_[i]->AddApp(pApp)) { + return true; + } + } + } + } + delete pApp; + pApp = nullptr; + return false; +} + +void AppManage::MyBundleStateCallback( + const uint8_t installType, const uint8_t resultCode, const void* resultMessage, const char* bundleName, void* data) +{ + if (installType == 0) { // install update + if (resultCode == 0 && bundleName != nullptr) { + char tmpName[TMP_BUF_SIZE] = {0}; + if (memcpy_s(tmpName, sizeof(tmpName), bundleName, strlen(bundleName)) == LAUNCHER_SUCCESS) { + tmpName[strlen(bundleName)] = 0; + GetAppInstallInfo(tmpName); + } + } + } +} + +void AppManage::MyBundleOwnCallback(const uint8_t resultCode, const void* resultMessage) +{ + // todo uninstall callback +} + +bool AppManage::LauncherApp(BundleInfo** info, int& count) +{ + callBackParam_.bundleName = nullptr; + callBackParam_.data = nullptr; + callBackParam_.callBack = MyBundleStateCallback; + RegisterCallback(&callBackParam_); + + BundleInfo* pBundleInfos = nullptr; + uint8_t ret = GetBundleInfos(1, &pBundleInfos, &count); + if (ret == 0) { + *info = pBundleInfos; + return true; + } else { + *info = nullptr; + return false; + } +} + +bool AppManage::InstallApp(AppInfo* app) +{ + return true; +} + +bool AppManage::UnInstallApp(AppInfo* app) +{ + return Uninstall(app->appName_, nullptr, MyBundleOwnCallback); +} + +bool AppManage::StartApp(AppInfo* app) +{ + Want want1 = { nullptr }; + ElementName element = { nullptr }; + SetElementBundleName(&element, app->appName_); + SetElementAbilityName(&element, app->abilityName_); + SetWantElement(&want1, element); + SetWantData(&want1, "WantData", strlen("WantData") + 1); + StartAbility(&want1); + ClearElement(&element); + ClearWant(&want1); + return true; +} + +void AppManage::SetViewGroup(funcClick click, funcLongPress press, ViewGroupPage* viewPage[MAX_VIEWGROUP], int size) +{ + if (click == nullptr || press == nullptr || viewPage == nullptr) { + return; + } + for (int i = 0; i < size; i++) { + viewPage_[i] = viewPage[i]; + } + size_ = size; + installFuncClick_ = click; + installFunclPress_ = press; +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_manage.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_manage.h new file mode 100644 index 0000000..21e3c20 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/app_manage.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_APP_MANAGE_H +#define OHOS_APP_MANAGE_H + +#include +#include +#include +#include +#include +#include + +#include "app_info.h" +#include "ui_config.h" +#include "view_group_page.h" + +namespace OHOS { +class AppManage { +public: + AppManage() = default; + ~AppManage(); + // start all app ,get all app info + + bool LauncherApp(BundleInfo** info, int& count); + bool InstallApp(AppInfo* app); + bool UnInstallApp(AppInfo* app); + bool StartApp(AppInfo* app); + static void SetViewGroup(funcClick click, funcLongPress press, ViewGroupPage* arrPage[MAX_VIEWGROUP], int size); + +private: + static void MyBundleStateCallback(const uint8_t installType, const uint8_t resultCode, const void* resultMessage, + const char* bundleName, void* data); + static void MyBundleOwnCallback(const uint8_t resultCode, const void* resultMessage); + static bool GetAbilityInfosByBundleName(const char* bundleName, AppInfo* pApp); + static bool GetAppInstallInfo(const char* bundleName); + +private: + BundleStatusCallback callBackParam_ { nullptr }; + static ViewGroupPage* viewPage_[MAX_VIEWGROUP]; + static int size_; + static funcClick installFuncClick_; + static funcLongPress installFunclPress_; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/event_listener.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/event_listener.h new file mode 100644 index 0000000..144610a --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/event_listener.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include +#include + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : public UIView::OnClickListener, public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView& view, const ClickEvent& event) override + { + if (!onClick_) { + return false; + } + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + return onClick_(*currentView, event); + } + + bool OnLongPress(UIView& view, const LongPressEvent& event) override + { + if (!onLongPress_) { + return false; + } + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + return onLongPress_(*currentView, event); + } + +private: + OnEventFunc onClick_ { nullptr }; + OnEventFunc onLongPress_ { nullptr }; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/long_press_view.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/long_press_view.cpp new file mode 100644 index 0000000..bc38327 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/long_press_view.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "long_press_view.h" +#include "ui_config.h" + +namespace OHOS { +LongPressView::LongPressView(UninstallApp uninstall) +{ + bStatus_ = false; + uninstall_ = uninstall; + viewGroup_ = new UIViewGroup(); + viewGroup_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Black())); + viewGroup_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS); + viewGroup_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + + buttUninstall_ = new UILabelButton(); + buttUninstall_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray())); + buttUninstall_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS); + buttUninstall_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + buttUninstall_->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED); + buttUninstall_->SetStyleForState(STYLE_BACKGROUND_OPA, HALF_OPACITY, UIButton::PRESSED); + buttUninstall_->SetText("卸载"); + buttUninstall_->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + buttUninstall_->SetOnClickListener(this); + + buttCancel_ = new UILabelButton(); + buttCancel_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray())); + buttCancel_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS); + buttCancel_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + buttCancel_->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED); + buttCancel_->SetStyleForState(STYLE_BACKGROUND_OPA, HALF_OPACITY, UIButton::PRESSED); + buttCancel_->SetText("取消"); + buttCancel_->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + buttCancel_->SetOnClickListener(this); + + viewGroup_->Add(buttUninstall_); + viewGroup_->Add(buttCancel_); + viewGroup_->SetVisible(false); +} + +LongPressView::~LongPressView() +{ + DeleteChildren(viewGroup_); +} + +void LongPressView::RemoveLview() +{ + if (bStatus_ == false) { + return; + } + viewParent_->Remove(viewGroup_); + viewGroup_->SetVisible(false); + viewParent_->Invalidate(); + bStatus_ = false; +} + +void LongPressView::Show(UIViewGroup* viewParent, AppInfo* pApp) +{ + const int16_t HEIGHT_DISCOUNT = 3; + const int16_t WIDTH_DISCOUNT = 2; + bStatus_ = true; + viewParent_ = viewParent; + app_ = pApp; + viewGroup_->SetPosition(pApp->buttonXY_.x / WIDTH_DISCOUNT + pApp->button_->GetWidth(), + pApp->buttonXY_.y / WIDTH_DISCOUNT + pApp->button_->GetHeight(), pApp->button_->GetWidth(), + (pApp->button_->GetHeight() * WIDTH_DISCOUNT) / HEIGHT_DISCOUNT + pApp->button_->GetHeight() / WIDTH_DISCOUNT); + buttUninstall_->SetPosition(0, 0, + pApp->button_->GetWidth(), pApp->button_->GetHeight() / WIDTH_DISCOUNT); + buttCancel_->SetPosition(0, (pApp->button_->GetHeight() * WIDTH_DISCOUNT) / HEIGHT_DISCOUNT, + pApp->button_->GetWidth(), pApp->button_->GetHeight() / WIDTH_DISCOUNT); + viewGroup_->SetVisible(true); + viewParent_->Add(viewGroup_); + viewParent_->Invalidate(); +} + +bool LongPressView::OnClick(UIView& view, const ClickEvent& event) +{ + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + UILabelButton* lbutt = nullptr; + lbutt = static_cast(currentView); + RemoveLview(); + if (currentView == buttUninstall_) { + uninstall_(app_); + } + return true; +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/long_press_view.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/long_press_view.h new file mode 100644 index 0000000..fe4ca57 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/long_press_view.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_LISTENTER_H +#define OHOS_LISTENTER_H + +#include "app_info.h" +#include "view_group_page.h" +#include "app_manage.h" + +namespace OHOS { +class LongPressView : public UIView::OnClickListener, public NativeBase { +public: + explicit LongPressView(UninstallApp uninstall); + virtual ~LongPressView(); + bool OnClick(UIView& view, const ClickEvent& event) override; + void RemoveLview(); + void Show(UIViewGroup* viewParent, AppInfo* pApp); + void SetStatus(bool status) + { + bStatus_ = status; + } + + bool GetStatus() const + { + return bStatus_; + } + +private: + UIViewGroup* viewParent_ { nullptr }; + UIViewGroup* viewGroup_ { nullptr }; + UILabelButton* buttUninstall_ { nullptr }; + UILabelButton* buttCancel_ { nullptr }; + UninstallApp uninstall_ { nullptr }; + AppInfo* app_ { nullptr }; + bool bStatus_ { false }; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability.cpp new file mode 100644 index 0000000..0609cb0 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "main_ability.h" + +namespace OHOS { +REGISTER_AA(MainAbility) + +void MainAbility::OnStart(const Want& want) +{ + SetMainRoute("MainAbilitySlice"); + Ability::OnStart(want); +} + +void MainAbility::OnInactive() +{ + Ability::OnInactive(); +} + +void MainAbility::OnActive(const Want& want) +{ + Ability::OnActive(want); +} + +void MainAbility::OnBackground() +{ + Ability::OnBackground(); +} + +void MainAbility::OnStop() +{ + Ability::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability.h new file mode 100644 index 0000000..bab86e3 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MAIN_ABILITY_H +#define OHOS_MAIN_ABILITY_H + +#include + +namespace OHOS { +class MainAbility : public Ability { +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability_slice.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability_slice.cpp new file mode 100644 index 0000000..746fbc8 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability_slice.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "main_ability_slice.h" +#include "ability_manager.h" + +namespace OHOS { +REGISTER_AS(MainAbilitySlice) + +MainAbilitySlice::~MainAbilitySlice() +{ + ReleaseView(); +} + +void MainAbilitySlice::ReleaseView() +{ + if (swipeView_) { + delete swipeView_; + swipeView_ = nullptr; + } + if (uiImageView_) { + delete uiImageView_; + uiImageView_ = nullptr; + } + if (lableHead_) { + delete lableHead_; + lableHead_ = nullptr; + } + if (lableTail_) { + delete lableTail_; + lableTail_ = nullptr; + } +} + +void MainAbilitySlice::SetHead() +{ + char tmp[TMP_BUF_SIZE] = { 0 }; + time_t t = time(nullptr); + struct tm* st = nullptr; + st = localtime(&t); + sprintf_s(tmp, sizeof(tmp), "%02d : %02d", st->tm_hour, st->tm_min); + UILabel* label = new UILabel(); + rootview_->Add(label); + label->SetPosition(0, 0, Screen::GetInstance().GetWidth(), LABLE_TITLE_HEIGHT); + label->SetText(tmp); + label->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_TOP); + label->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + label->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + label->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + lableHead_ = label; +} + +void MainAbilitySlice::SetTail() +{ + UILabel* label = new UILabel(); + rootview_->Add(label); + label->SetPosition(0, Screen::GetInstance().GetHeight() - LABLE_TAIL_HEIGHT, + Screen::GetInstance().GetWidth(), LABLE_TAIL_HEIGHT); + char buf[TMP_BUF_SIZE] = { 0 }; + sprintf_s(buf, sizeof(buf), ".%d.", 1); + label->SetText(buf); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + label->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + label->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + lableTail_ = label; +} + +void MainAbilitySlice::SetImageView() +{ + uiImageView_ = new UIImageView(); + // modify image view height + + uiImageView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + uiImageView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::White())); + uiImageView_->SetSrc(TABLE_BACKGROUND); + uiImageView_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + rootview_->Add(uiImageView_); +} + +void MainAbilitySlice::SetSwipe() +{ + swipeView_ = new SwipeView(lableHead_, lableTail_); + swipeView_->OnSetUpView(); + rootview_->Add(swipeView_->GetSwipeView()); +} + +void MainAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + rootview_ = RootView::GetWindowRootView(); + rootview_->SetPosition(0, 0); + rootview_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + rootview_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + rootview_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x30, 0x30, 0x30))); + SetImageView(); + SetHead(); + SetTail(); + SetSwipe(); + SetUIContent(rootview_); + rootview_->Invalidate(); +} + +void MainAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void MainAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void MainAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void MainAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability_slice.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability_slice.h new file mode 100644 index 0000000..e7adda8 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/main_ability_slice.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MAIN_ABILITY_SLICE_H +#define OHOS_MAIN_ABILITY_SLICE_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "swipe_view.h" +#include "ui_config.h" + +namespace OHOS { +class MainAbilitySlice : public AbilitySlice { +public: + MainAbilitySlice() = default; + ~MainAbilitySlice() override; + +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + void SetHead(); + void SetTail(); + void SetSwipe(); + void ReleaseView(); + void SetImageView(); + +private: + SwipeView* swipeView_ { nullptr }; + UIImageView* uiImageView_ { nullptr }; + UILabel* lableHead_ { nullptr }; + UILabel* lableTail_ { nullptr }; + RootView *rootview_ { nullptr }; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/native_base.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/native_base.h new file mode 100644 index 0000000..10508cc --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/native_base.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_NATIVE_BASE_H +#define OHOS_NATIVE_BASE_H + +#include + +namespace OHOS { +class NativeBase : public HeapBase { +public: + NativeBase() {} + virtual ~NativeBase() {} + void DeleteChildren(UIView *view) + { + if (view == nullptr) { + return; + } + while (view) { + UIView *tempView = view; + view = view->GetNextSibling(); + if (tempView->IsViewGroup()) { + DeleteChildren(static_cast(tempView)->GetChildrenHead()); + } + if (tempView->GetParent()) { + static_cast(tempView->GetParent())->Remove(tempView); + } + delete tempView; + } + } +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/swipe_view.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/swipe_view.cpp new file mode 100644 index 0000000..dd8f455 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/swipe_view.cpp @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "swipe_view.h" + +namespace OHOS { +AppEvent* AppEvent::appEvent_ = { nullptr }; +SwipeView* AppEvent::nativeView_ = { nullptr }; +static constexpr int32_t PERIOD_TIME = 60 * 1000; // 60 seconds + +SwipeView::SwipeView(UILabel* titlellable, UILabel* taillable) +{ + lableTitle_ = titlellable; + lableTail_ = taillable; + groupCount_ = 0; + for (int i = 0; i < MAX_VIEWGROUP; i++) { + arrPage_[i] = nullptr; + } + arrViewListener_ = nullptr; + swipeListener_ = nullptr; + appManage_ = new AppManage(); + lpView_ = new LongPressView(AppEvent::UninstallApp); + Task::Init(); + Task::SetPeriod(PERIOD_TIME); +} + +SwipeView::~SwipeView() +{ + OnStop(); +} + +void SwipeView::SetUpSwipe() +{ + swipe_ = new UISwipeView(); + swipe_->SetPosition(0, LABLE_TITLE_HEIGHT, Screen::GetInstance().GetWidth(), + Screen::GetInstance().GetHeight() - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT); + swipe_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + swipe_->SetLoopState(true); + swipe_->SetAnimatorTime(20); // set swipe view animator time 20s +} + +UIViewGroup* SwipeView::AddViewGroup() +{ + if (groupCount_ >= MAX_VIEWGROUP) { + return nullptr; + } + UIViewGroup* viewGroup = new UIViewGroup(); + viewGroup->SetPosition(0, LABLE_TITLE_HEIGHT, Screen::GetInstance().GetWidth(), + Screen::GetInstance().GetHeight() - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT); + viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + groupCount_++; + ViewGroupPage* page = new ViewGroupPage(viewGroup); + arrPage_[groupCount_ - 1] = page; + swipe_->Add(viewGroup); + return viewGroup; +} + +UIViewGroup* SwipeView::AddFirstViewGroup() +{ + UIViewGroup* firstView = new UIViewGroup(); + firstView->SetPosition(0, LABLE_TITLE_HEIGHT, Screen::GetInstance().GetWidth(), + Screen::GetInstance().GetHeight() - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT); + firstView->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + UIViewGroup* viewTimeWeather = new UIViewGroup(); + // 2: set first view to 2 piece + viewTimeWeather->SetPosition(0, 0, firstView->GetWidth() / 2, firstView->GetHeight()); + viewTimeWeather->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + timeWeatherView_ = new TimeWeatherView(viewTimeWeather); + timeWeatherView_->SetUpView(); + firstView->Add(viewTimeWeather); + + UIViewGroup* viewGroup = new UIViewGroup(); + // 2 : get left && right view width and height + viewGroup->SetPosition(firstView->GetWidth() / 2, 0, firstView->GetWidth() / 2, firstView->GetHeight()); + viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + firstView->Add(viewGroup); + groupCount_++; + ViewGroupPage *page = new ViewGroupPage(viewGroup); + arrPage_[groupCount_ - 1] = page; + + swipe_->Add(firstView); + firstView->Invalidate(); + Task::TaskExecute(); + return firstView; +} + +void SwipeView::OnSetUpView() +{ + SetUpSwipe(); + swipeListener_ = new SwipeListener(lpView_, swipe_, lableTail_); + swipe_->SetOnSwipeListener(swipeListener_); + arrViewListener_ = new ViewPageListener(lpView_); + swipe_->SetOnClickListener(arrViewListener_); + + AddFirstViewGroup(); + AddViewGroup(); + AddViewGroup(); + // Reserved. Touch and hold to add a page. + arrPage_[0]->SetMatrix(APP_ROW_COUNT, APP_COL_COUNT); + arrPage_[0]->SetScale(0.6); // 0.6 blank/icon width + for (int16_t i = 1; i < groupCount_; i++) { + arrPage_[i]->SetMatrix(APP_ROW_COUNT, 2 * APP_COL_COUNT); // 2 scale of first view's col count + arrPage_[i]->SetScale(0.69); // 0.69 blank/icon width + } + AppEvent::GetInstance(this); + AppManage::SetViewGroup(AppEvent::ClickEvent, AppEvent::LongPressEvent, arrPage_, groupCount_); + BundleInfo* pBundleInfos = nullptr; + int count = 0; + if (appManage_->LauncherApp(&pBundleInfos, count)) { + for (int j = 0; j < count; j++) { + for (int i = 0; i < groupCount_; i++) { + if (memcmp(LAUNCHER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) { + break; + } + if (memcmp(SCREENSAVER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) { + break; + } + + AppInfo* app = new AppInfo(); + app->funcclick_ = AppEvent::ClickEvent; + app->funclPress_ = AppEvent::LongPressEvent; + if (pBundleInfos[j].bundleName) { + memcpy_s(app->appName_, sizeof(app->appName_), pBundleInfos[j].bundleName, + strlen(pBundleInfos[j].bundleName)); + app->appName_[strlen(pBundleInfos[j].bundleName)] = 0; + } + if (pBundleInfos[j].abilityInfos[0].name) { + memcpy_s(app->abilityName_, sizeof(app->abilityName_), pBundleInfos[j].abilityInfos[0].name, + strlen(pBundleInfos[j].abilityInfos[0].name)); + app->abilityName_[strlen(pBundleInfos[j].abilityInfos[0].name)] = 0; + } + if (pBundleInfos[j].bigIconPath) { + memcpy_s(app->appIconDir_, sizeof(app->appIconDir_), pBundleInfos[j].bigIconPath, + strlen(pBundleInfos[j].bigIconPath)); + app->appIconDir_[strlen(pBundleInfos[j].bigIconPath)] = 0; + } + if (arrPage_[i]->AddApp(app)) { + break; + } + } + } + } + swipe_->SetCurrentPage(0); +} + +void SwipeView::StartApp(AppInfo* app) +{ + if (lpView_->GetStatus() == true) { + lpView_->RemoveLview(); + return; + } + appManage_->StartApp(app); +} + +void SwipeView::ShowLongPressView(AppInfo* app) +{ + lpView_->Show(static_cast(app->button_->GetParent()), app); +} + +void SwipeView::UninstallApp(AppInfo* app) +{ + if (appManage_->UnInstallApp(app)) { + for (int16_t i = 0; i < groupCount_; i++) { + if (arrPage_[i]) { + if (arrPage_[i]->RemoveApp(app->appName_)) { + swipe_->Invalidate(); + return; + } + } + } + } +} + +void SwipeView::InstallApp(AppInfo* app) +{ + appManage_->InstallApp(app); + AppInfo* pApp = new AppInfo(); + if (pApp == nullptr) { + return; + } + app->funcclick_ = AppEvent::ClickEvent; + app->funclPress_ = AppEvent::LongPressEvent; + int16_t i; + for (i = 0; i < groupCount_; i++) { + if (arrPage_[i]->AddApp(pApp)) { + break; + } + } + if (i == groupCount_) { + delete pApp; + pApp = nullptr; + } +} + +void SwipeView::OnStop() +{ + if (lpView_) { + delete lpView_; + lpView_ = nullptr; + } + if (appManage_) { + delete appManage_; + appManage_ = nullptr; + } + if (arrViewListener_) { + delete arrViewListener_; + arrViewListener_ = nullptr; + } + if (swipeListener_) { + delete swipeListener_; + swipeListener_ = nullptr; + } + if (timeWeatherView_) { + delete timeWeatherView_; + timeWeatherView_ = nullptr; + } + for (int i = 0; i < MAX_VIEWGROUP; i++) { + if (arrPage_[i]) { + delete arrPage_[i]; + arrPage_[i] = nullptr; + } + } + DeleteChildren(swipe_); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/swipe_view.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/swipe_view.h new file mode 100644 index 0000000..3971a6c --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/swipe_view.h @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SWIPE_VIEW_H +#define OHOS_SWIPE_VIEW_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ui_config.h" +#include "app_info.h" +#include "view_group_page.h" +#include "native_base.h" +#include "long_press_view.h" +#include "app_manage.h" +#include "time_weather_view.h" + +namespace OHOS { +class ViewPageListener : public UIView::OnClickListener { +public: + explicit ViewPageListener(LongPressView* view) : view_(view) {} + virtual ~ViewPageListener() {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + view_->RemoveLview(); + return true; + } + +private: + LongPressView *view_ { nullptr }; +}; + +class SwipeListener : public UISwipeView::OnSwipeListener { +public: + SwipeListener(LongPressView* view, UISwipeView* swipe, UILabel* lable) + : view_(view), swipe_(swipe), lable_(lable) {}; + ~SwipeListener() {}; + virtual void OnSwipe(UISwipeView& view) override + { + char buf[TMP_BUF_SIZE] = { 0 }; + sprintf_s(buf, sizeof(buf), ".%d.", swipe_->GetCurrentPage() + 1); + lable_->SetText(buf); + view_->RemoveLview(); + } + +private: + LongPressView* view_ { nullptr }; + UISwipeView* swipe_ { nullptr }; + UILabel* lable_ { nullptr }; +}; + +class SwipeView : public Task, public NativeBase { +public: + SwipeView() = delete; + SwipeView(UILabel* titlellable, UILabel* taillable); + virtual ~SwipeView(); + void OnSetUpView(); + void StartApp(AppInfo* app); + void ShowLongPressView(AppInfo* app); + void UninstallApp(AppInfo* app); + void InstallApp(AppInfo* app); + void Callback() override + { + char tmp[TMP_BUF_SIZE] = { 0 }; + time_t t = time(nullptr); + struct tm* st = localtime(&t); + if (st != nullptr) { + int ret = sprintf_s(tmp, sizeof(tmp), "%02d : %02d", st->tm_hour, st->tm_min); + if (ret != LAUNCHER_PARAMERROR) { + lableTitle_->SetText(tmp); + timeWeatherView_->SetUpTimeView(); + } + } + } + + UISwipeView* GetSwipeView() const + { + return swipe_; + } + +private: + void OnStop(); + void SetUpSwipe(); + UIViewGroup* AddViewGroup(); + UIViewGroup* AddFirstViewGroup(); + ViewGroupPage* arrPage_[MAX_VIEWGROUP] { nullptr }; + UISwipeView* swipe_ { nullptr }; + UILabel* lableTitle_ { nullptr }; // view title time label + UILabel* lableTail_ { nullptr }; + int groupCount_ { 0 }; + ViewPageListener* arrViewListener_ { nullptr }; + SwipeListener* swipeListener_ { nullptr }; + AppManage* appManage_ { nullptr }; + LongPressView* lpView_ { nullptr }; + TimeWeatherView* timeWeatherView_ {nullptr}; +}; + +class AppEvent { +public: + static AppEvent* GetInstance(SwipeView* nativeView) + { + if (appEvent_ == nullptr) { + appEvent_ = new AppEvent(); + nativeView_ = nativeView; + } + return appEvent_; + } + // app click + + static bool ClickEvent(AppInfo* app) + { + nativeView_->StartApp(app); + return true; + } + // app long press show window + + static bool LongPressEvent(AppInfo* app) + { + nativeView_->ShowLongPressView(app); + return true; + } + // app uninstall click + + static bool UninstallApp(AppInfo* app) + { + nativeView_->UninstallApp(app); + return true; + } + // none install app used appmanage::InstallApp, this function is invailate + + static bool InstallApp(AppInfo* app) + { + nativeView_->InstallApp(app); + return true; + } + +private: + AppEvent() {} + ~AppEvent() + { + if (appEvent_) { + delete appEvent_; + appEvent_ = nullptr; + } + } + +private: + static AppEvent* appEvent_; + static SwipeView* nativeView_; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/time_weather_view.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/time_weather_view.cpp new file mode 100644 index 0000000..d5b7b5e --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/time_weather_view.cpp @@ -0,0 +1,196 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "time_weather_view.h" + +namespace OHOS { +static constexpr int16_t DISPLATE_PICESE = 2; +static constexpr int16_t BLANK_H = 5; +static constexpr int16_t BLANK_TW = 15; +static constexpr int16_t BLANK_W = 100; +static constexpr int16_t BIGLABEL_H = 100; +static constexpr int16_t SMALLLABEL_H = 35; +static constexpr int16_t IMAGE_H = 40; +static constexpr int16_t IMAGE_W = 40; +const char* g_weekDate[WEEK_DAY_MAX] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; + +TimeWeatherView::TimeWeatherView(UIViewGroup* viewGroup) +{ + viewGroup_ = viewGroup; +} +TimeWeatherView::~TimeWeatherView() +{ + // todo other release +} + +void TimeWeatherView::SetStyle(Style sty) +{ + viewGroup_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + viewGroup_->Invalidate(); +} + +void TimeWeatherView::SetPosion(int16_t x, int16_t y, int16_t height, int16_t width) +{ + viewGroup_->SetPosition(x, y, width, height); +} + +void TimeWeatherView::SetUpView() +{ + SetUpTimeView(); + SetUpWeatherView(); + viewGroup_->Invalidate(); +} + +void TimeWeatherView::SetUpTimeView() +{ + char hour_min[TMP_BUF_SIZE] = { 0 }; + char mont_day[TMP_BUF_SIZE] = { 0 }; + char week_day[TMP_BUF_SIZE] = { 0 }; + char date[TMP_BUF_SIZE] = { 0 }; + const int16_t january = 1; + const int16_t commonYear = 1970; + time_t t = time(nullptr); + struct tm* st = localtime(&t); + if (st == nullptr) { + return; + } + int ret = sprintf_s(hour_min, sizeof(hour_min), "%02d : %02d", st->tm_hour, st->tm_min); + if (ret == LAUNCHER_PARAMERROR) { + return; + } + ret = sprintf_s(mont_day, sizeof(mont_day), "%02d月%02d日", st->tm_mon + january, st->tm_mday); + if (ret == LAUNCHER_PARAMERROR) { + return; + } + GetWeekdayByYearday(st->tm_year + commonYear, st->tm_mon + january, st->tm_mday, week_day, sizeof(week_day)); + ret = sprintf_s(date, sizeof(date), "%s %s", mont_day, week_day); + if (ret == LAUNCHER_PARAMERROR) { + return; + } + if (viewTime_ == nullptr) { + viewTime_ = new UIViewGroup(); + viewTime_->SetPosition(BLANK_TW, BLANK_H, viewGroup_->GetWidth() - BLANK_W, + viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H); + viewTime_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + UILabel* lable = new UILabel(); + lable->SetPosition(BLANK_TW, BLANK_H, viewTime_->GetWidth(), BIGLABEL_H); + lable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_BOTTOM); + lable->SetText(hour_min); + lable->SetFont(FOND_PATH, BIGLAUNCHER_FOND_ID); + lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + lable->SetViewId("labletime"); + + UILabel* lable2 = new UILabel(); + lable2->SetPosition(BLANK_TW, BLANK_H + BIGLABEL_H + BLANK_H, viewTime_->GetWidth(), SMALLLABEL_H); + lable2->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_TOP); + lable2->SetText(date); + lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + + lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + lable2->SetViewId("labledate"); + + viewTime_->Add(lable); + viewTime_->Add(lable2); + viewGroup_->Add(viewTime_); + } else { + UILabel* label = nullptr; + label = static_cast(viewTime_->GetChildById("labletime")); + if (label) { + label->SetText(hour_min); + } + label = static_cast(viewTime_->GetChildById("labledate")); + if (label) { + label->SetText(date); + } + viewTime_->Invalidate(); + } +} + +void TimeWeatherView::GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size) +{ + if (date == nullptr) { + return; + } + const int16_t months = 12; + const int16_t january = 1; + const int16_t february = 2; + const int16_t oneHundred = 100; + const int16_t fourHundred = 400; + int iWeekDay = -1; + if (january == iM || february == iM) { + iM += months; + iY--; + } + // 1 : MONDAY_LAUNCHER, 2 : TUESDAY_LAUNCHER, 3 : WEDNESDAY_LAUNCHER, 4 : , 5 : ect + iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / oneHundred + iY / fourHundred) % WEEKEND_LAUNCHER; + for (int i = 0; i < WEEK_DAY_MAX; i++) { + if (iWeekDay == i) { + if (memcpy_s(date, size, g_weekDate[i], strlen(g_weekDate[i])) == LAUNCHER_SUCCESS) { + date[strlen(g_weekDate[i])] = 0; + break; + } + } + } + return; +} + +void TimeWeatherView::SetUpWeatherView() +{ + const int16_t countTimes = 6; + viewweather_ = new UIViewGroup(); + viewweather_->SetPosition(BLANK_W, viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H, + viewGroup_->GetWidth() / DISPLATE_PICESE + BLANK_TW, DISPLATE_PICESE * (BLANK_H + SMALLLABEL_H) + BLANK_H); + viewweather_->SetStyle(STYLE_BACKGROUND_OPA, HALF_OPACITY); + viewweather_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS); + viewweather_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray())); + + UIImageView* uiImageView = new UIImageView(); + uiImageView->SetPosition(BLANK_TW, BLANK_H * countTimes, IMAGE_W, IMAGE_H); + uiImageView->SetSrc(RES_WEATHER); + uiImageView->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + + UILabel* lable = new UILabel(); + lable->SetPosition(BLANK_TW + IMAGE_W, BLANK_H, + viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H); + lable->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER); + lable->SetText("室内温度 26℃"); + lable->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + UILabel* lable2 = new UILabel(); + lable2->SetPosition(BLANK_TW + IMAGE_W, SMALLLABEL_H + BLANK_H + BLANK_H, + viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H); + lable2->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER); + lable2->SetText("空气污染指数 136"); + lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + viewweather_->Add(uiImageView); + viewweather_->Add(lable); + viewweather_->Add(lable2); + viewGroup_->Add(viewweather_); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/time_weather_view.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/time_weather_view.h new file mode 100644 index 0000000..dd5747a --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/time_weather_view.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_TIME_WEATHER_H +#define OHOS_TIME_WEATHER_H + +#include +#include +#include +#include + +#include "app_info.h" +#include "native_base.h" +#include "ui_config.h" + +namespace OHOS { +enum DATE_LAUNCHER { + SUNDAY_LAUNCHER = 0, + MONDAY_LAUNCHER, + TUESDAY_LAUNCHER, + WEDNESDAY_LAUNCHER, + THURSDAY_LAUNCHER, + FRIDAY_LAUNCHER, + SATURDAY_LAUNCHER, + WEEKEND_LAUNCHER +}; + +class TimeWeatherView : public NativeBase { +public: + TimeWeatherView() = delete; + explicit TimeWeatherView(UIViewGroup* viewGroup); + virtual ~TimeWeatherView(); + void SetStyle(Style sty); + void SetPosion(int16_t width, int16_t height, int16_t x, int16_t y); + void SetUpView(); + void SetUpTimeView(); + +protected: + void SetUpWeatherView(); + void GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size); + +private: + UIViewGroup* viewTime_ { nullptr }; + UIViewGroup* viewweather_ { nullptr }; + UIViewGroup* viewGroup_ { nullptr }; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/ui_config.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/ui_config.h new file mode 100644 index 0000000..61cbaec --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/ui_config.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_UI_CONFIG_H +#define OHOS_UI_CONFIG_H + +#include +#include + +namespace OHOS { +static constexpr int16_t LABLE_TITLE_HEIGHT = 30; // tail lable height +static constexpr int16_t LABLE_TAIL_HEIGHT = 30; +static constexpr int16_t APP_WIDTH_COUNT = 7; // blank + app + blank + app + blank +static constexpr int16_t APP_HEIGHT_COUNT = 4; // app + lable + app + lable +static constexpr int16_t MAX_VIEWGROUP = 3; // swipe window count +static constexpr int16_t APP_ROW_COUNT = 2; // a swipe view app count in row +static constexpr int16_t APP_COL_COUNT = 3; // a swipe view app count in col +static constexpr int16_t LAUNCHER_FOND_ID = 16; // other view fond id +static constexpr int16_t APP_FOND_ID = 16; // app name fond id +static constexpr int16_t BIGLAUNCHER_FOND_ID = 48; // time big fond id +static constexpr int16_t TOTAL_OPACITY = 0; // transparent +static constexpr int16_t HALF_OPACITY = 50; // diaphanous +static constexpr int16_t UN_OPACITY = 255; // opaque +static constexpr int16_t BUTTON_RADIUS = 20; // app icon radius +static constexpr int16_t LABLE_RADIUS = 0; // lable icon radius +static constexpr int16_t TITLE_LABLE_OPACITY = 255; // translucent +static constexpr int16_t GROUP_VIEW_RADIUS = 20; // view radius + +#ifndef TMP_BUF_SIZE +#define TMP_BUF_SIZE 128 +#endif + +#define LAUNCHER_BUNDLE_NAME "com.huawei.launcher" +#define SCREENSAVER_BUNDLE_NAME "com.huawei.screensaver" +#define TABLE_BACKGROUND \ + "/storage/app/run/com.huawei.launcher/launcher/assets/launcher/resources/base/media/background.png" +#define RES_WEATHER "/storage/app/run/com.huawei.launcher/launcher/assets/launcher/resources/base/media/weather.png" +#define FOND_PATH "SourceHanSansSC-Regular.otf" + +#ifndef LAUNCHER_SUCCESS +#define LAUNCHER_SUCCESS 0 +#endif + +#ifndef LAUNCHER_PARAMERROR +#define LAUNCHER_PARAMERROR (-1) +#endif + +#ifndef WEEK_DAY_MAX +#define WEEK_DAY_MAX 7 +#endif +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/view_group_page.cpp b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/view_group_page.cpp new file mode 100644 index 0000000..0b64953 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/view_group_page.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "view_group_page.h" +#include "ui_config.h" + +namespace OHOS { +ViewGroupPage::ViewGroupPage(UIViewGroup* viewGroup) +{ + viewGroup_ = viewGroup; +} + +ViewGroupPage::~ViewGroupPage() +{ + ListNode* app = appInfo_.Begin(); + while (app != appInfo_.End()) { + delete app->data_; + app = app->next_; + } + appInfo_.Clear(); + if (row_col_) { + delete[] row_col_; + } +} + +bool ViewGroupPage::IsFull(int16_t& row, int16_t& col) +{ + for (int16_t i = 0; i < row_; i++) { + for (int16_t j = 0; j < col_; j++) { + if (row_col_[i][j] == false) { + row = i; + col = j; + return false; + } + } + } + return true; +} + +void ViewGroupPage::SetStyle(Style sty) +{ + viewGroup_->SetStyle(sty); + viewGroup_->Invalidate(); +} + +void ViewGroupPage::SetPosion(int16_t width, int16_t height, int16_t x, int16_t y) +{ + viewGroup_->SetPosition(x, y, width, height); +} + +void ViewGroupPage::SetScale(double scale) +{ + scale_ = scale; +} + +void ViewGroupPage::SetMatrix(int16_t rows, int16_t cols) +{ + row_col_ = new bool* [rows]; + for (int i = 0; i < rows; i++) { + row_col_[i] = new bool[cols](); + } + row_ = rows; + col_ = cols; +} + +void ViewGroupPage::CalculateAppPosition(AppInfo* pAppInfo, int16_t row, int16_t col) +{ + int16_t w = viewGroup_->GetWidth(); + + const double scale = scale_; + const int16_t blank1 = 10; + const int16_t blank2 = 30; + const int16_t labelH = 2; + int16_t width = static_cast(static_cast(w) / static_cast(scale * col_ + col_ + scale)); + int16_t heightB = width; + int16_t heightL = heightB / labelH; + int16_t xB = scale * width + (scale + 1) * width * col; + int16_t yB = blank1 + (blank2 + heightL + heightB) * row; + int16_t xL = xB; + int16_t yL = yB + heightB + blank1; + + pAppInfo->buttonXY_.x = xB; + pAppInfo->buttonXY_.y = yB; + pAppInfo->buttonHV_.x = width; + pAppInfo->buttonHV_.y = heightB; + + pAppInfo->lableXY_.x = xL; + pAppInfo->lableXY_.y = yL; + pAppInfo->lableHV_.x = width; + pAppInfo->lableHV_.y = heightL; +} + +void ViewGroupPage::SetUpApp(AppInfo *pAppInfo) +{ + UILabelButton *button = new UILabelButton(); + UILabel *lable = new UILabel(); + lable->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Red())); + lable->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + pAppInfo->SetButton(button); + pAppInfo->SetLable(lable); + + pAppInfo->SetListener(pAppInfo); + viewGroup_->Add(button); + viewGroup_->Add(lable); + viewGroup_->Invalidate(); +} + +bool ViewGroupPage::AddApp(AppInfo* pAppInfo) +{ + int16_t row = 0; + int16_t col = 0; + + if (FindApp(pAppInfo)) { + return true; + } + + if (IsFull(row, col)) { + return false; + } + + pAppInfo->SetLocation(row, col); + CalculateAppPosition(pAppInfo, row, col); + + SetUpApp(pAppInfo); + appInfo_.PushBack(pAppInfo); + row_col_[row][col] = true; + return true; +} + +bool ViewGroupPage::FindApp(AppInfo* pApp) +{ + ListNode* app = appInfo_.Begin(); + while (app != appInfo_.End()) { + if (memcmp(app->data_->appName_, pApp->appName_, strlen(pApp->appName_)) == 0) { + return true; + } + app = app->next_; + } + return false; +} + +bool ViewGroupPage::RemoveApp(const char* pAppName) +{ + ListNode* app = appInfo_.Begin(); + while (app != appInfo_.End()) { + if (memcmp(app->data_->appName_, pAppName, strlen(pAppName)) == 0) { + row_col_[app->data_->row_col_.x][app->data_->row_col_.y] = false; + viewGroup_->Remove(app->data_->button_); + viewGroup_->Remove(app->data_->lable_); + viewGroup_->Invalidate(); + appInfo_.Remove(app); + return true; + } + app = app->next_; + } + return false; +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/view_group_page.h b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/view_group_page.h new file mode 100644 index 0000000..b4d29d5 --- /dev/null +++ b/bearpi_hm_micro/app/launcher/launcher/src/main/cpp/view_group_page.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_VIEWGROUP_PAGE_H +#define OHOS_VIEWGROUP_PAGE_H + +#include +#include +#include +#include "gfx_utils/list.h" + +#include "ui_config.h" +#include "app_info.h" +#include "native_base.h" + +namespace OHOS { +class ViewGroupPage { +public: + ViewGroupPage() = delete; + explicit ViewGroupPage(UIViewGroup* viewGroup); + virtual ~ViewGroupPage(); + void SetMatrix(int16_t rows, int16_t cols); + bool AddApp(AppInfo* pAppInfo); + bool RemoveApp(const char* appName); + bool FindApp(AppInfo* pAppInfo); + void SetScale(double scale); + +protected: + void SetPosion(int16_t width, int16_t height, int16_t x = 0, int16_t y = 0); + void SetStyle(Style sty); + bool IsFull(int16_t& row, int16_t& col); + void SetUpApp(AppInfo* pAppInfo); + void CalculateAppPosition(AppInfo* pAppInfo, int16_t row, int16_t col); + +private: + List appInfo_; + UIViewGroup* viewGroup_ { nullptr }; + bool** row_col_ { nullptr }; + int16_t row_ { 0 }; + int16_t col_ { 0 }; + double scale_ { 0.0 }; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/resources/base/media/background.png b/bearpi_hm_micro/app/launcher/launcher/src/main/resources/base/media/background.png new file mode 100644 index 0000000..7766727 Binary files /dev/null and b/bearpi_hm_micro/app/launcher/launcher/src/main/resources/base/media/background.png differ diff --git a/bearpi_hm_micro/app/launcher/launcher/src/main/resources/base/media/weather.png b/bearpi_hm_micro/app/launcher/launcher/src/main/resources/base/media/weather.png new file mode 100644 index 0000000..4449637 Binary files /dev/null and b/bearpi_hm_micro/app/launcher/launcher/src/main/resources/base/media/weather.png differ diff --git a/bearpi_hm_micro/app/screensaver/BUILD.gn b/bearpi_hm_micro/app/screensaver/BUILD.gn new file mode 100644 index 0000000..02a310b --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/hap_pack.gni") + +shared_library("screensaver") { + sources = [ + "screensaver/src/main/cpp/screensaver_ability.cpp", + "screensaver/src/main/cpp/screensaver_ability_slice.cpp", + ] + + deps = [ + "${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite", + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//foundation/graphic/surface", + "//foundation/graphic/ui:lite_ui", + "//foundation/graphic/utils:lite_graphic_utils", + "//utils/native/lite/kv_store:kv_store", + ] + + include_dirs = [ + "screensaver/src/main/cpp", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", + "${aafwk_lite_path}/interfaces/kits/want_lite", + "//base/startup/syspara_lite/interfaces/kits", + ] + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + ] +} + +hap_pack("screensaver_hap") { + deps = [ ":screensaver" ] + mode = "hap" + json_path = "screensaver/src/main/config.json" + ability_so_path = "$root_out_dir/libscreensaver.so" + force = "true" + cert_profile = "cert/com.huawei.screensaver_HarmonyAppProvision_release.p7b" + resources_path = "screensaver/src/main/resources" + hap_name = "screensaver" + privatekey = "HOS Application Provision Release" +} diff --git a/bearpi_hm_micro/app/screensaver/LICENSE b/bearpi_hm_micro/app/screensaver/LICENSE new file mode 100644 index 0000000..4947287 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/bearpi_hm_micro/app/screensaver/README.md b/bearpi_hm_micro/app/screensaver/README.md new file mode 100644 index 0000000..a88590c --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/README.md @@ -0,0 +1,34 @@ +# CAMERA\_SCREENSAVER\_APP + +- [Introduction](#section132119717356) +- [Directory Structure](#section176641621345) +- [Constraints](#section722512541395) +- [Repositories Involved](#section16511040154318) + +## Introduction + +This repository provides the screen saver capability. You can use it to: + +- Play the preset images. + +- Tap the screen to exit the screen saver. + +**Figure 1** Position of this repository in the graphics subsystem + + +![](figures/screensaver_en.png) + +## Directory Structure + +``` +applications/sample/camera/screensaver +├── cert Certificate +├── screensaver/src/main + ├──resources Resources + ├──cpp Source code +``` + +## Constraints + +C++ 11 or later + diff --git a/bearpi_hm_micro/app/screensaver/README_zh.md b/bearpi_hm_micro/app/screensaver/README_zh.md new file mode 100644 index 0000000..e751714 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/README_zh.md @@ -0,0 +1,36 @@ +# 图形子系统CAMERA\_SCREENSAVER\_APP组件 + +- [简介](#section132119717356) +- [目录](#section176641621345) +- [约束](#section722512541395) +- [相关仓](#section16511040154318) + +## 简介 + +CAMERA\_SCREENSAVER\_APP组件, 提供用户屏幕保护能力。它主要实现了以下功能: + +1、支持播放预设图片能力。 + +2、支持点击屏幕退出能力。 + +**图 1** 组件架构图(红色框是屏保应用) + + +![](figures/screensaver.png) + +## 目录 + +``` +applications/sample/camera/screensaver +├── cert 签名目录 +├── screensaver/src/main + ├──resources 资源目录 + ├──cpp 源码目录 +``` + +## 约束 + +C++11版本或以上 + + + diff --git a/bearpi_hm_micro/app/screensaver/cert/com.huawei.screensaver_HarmonyAppProvision_release.p7b b/bearpi_hm_micro/app/screensaver/cert/com.huawei.screensaver_HarmonyAppProvision_release.p7b new file mode 100644 index 0000000..18c687c Binary files /dev/null and b/bearpi_hm_micro/app/screensaver/cert/com.huawei.screensaver_HarmonyAppProvision_release.p7b differ diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/config.json b/bearpi_hm_micro/app/screensaver/screensaver/src/main/config.json new file mode 100644 index 0000000..f119078 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/config.json @@ -0,0 +1,40 @@ +{ + "app": { + "bundleName": "com.huawei.screensaver", + "vendor": "huawei", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + + } + }, + "module": { + "package": "com.huawei.screensaver", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", "tv","tablet","car","smartWatch","sportsWatch","smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "screensaver", + "moduleType": "entry" + }, + "abilities": [ + { + "name": "ScreensaverAbility", + "label": "screensaver", + "launchType": "standard", + "type": "page", + "visible":true + } + ] + } +} diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/event_listener.h b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/event_listener.h new file mode 100644 index 0000000..a132bd8 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/event_listener.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include + +#include "components/ui_view.h" +#include "events/click_event.h" +#include "events/event.h" +#include "events/long_press_event.h" + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : public UIView::OnClickListener, public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView& view, const ClickEvent &event) override + { + if (!onClick_) { + return false; + } + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + return onClick_(*currentView, event); + } + + bool OnLongPress(UIView& view, const LongPressEvent &event) override + { + if (!onLongPress_) { + return false; + } + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + return onLongPress_(*currentView, event); + } + +private: + OnEventFunc onClick_ {}; + OnEventFunc onLongPress_ {}; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability.cpp b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability.cpp new file mode 100644 index 0000000..3ac4bbc --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screensaver_ability.h" + +namespace OHOS { +REGISTER_AA(ScreensaverAbility) + +void ScreensaverAbility::OnStart(const Want& want) +{ + SetMainRoute("ScreensaverAbilitySlice"); + Ability::OnStart(want); +} + +void ScreensaverAbility::OnInactive() +{ + Ability::OnInactive(); +} + +void ScreensaverAbility::OnActive(const Want& want) +{ + Ability::OnActive(want); +} + +void ScreensaverAbility::OnBackground() +{ + Ability::OnBackground(); +} + +void ScreensaverAbility::OnStop() +{ + Ability::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability.h b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability.h new file mode 100644 index 0000000..e0fa768 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREENSAVER_ABILITY_H +#define OHOS_SCREENSAVER_ABILITY_H + +#include +#include "want.h" + +namespace OHOS { +class ScreensaverAbility : public Ability { +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability_slice.cpp b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability_slice.cpp new file mode 100644 index 0000000..5d79507 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability_slice.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screensaver_ability_slice.h" +#include "common/screen.h" + +namespace OHOS { +REGISTER_AS(ScreensaverAbilitySlice) +namespace { + int16_t screenWidth = static_cast(Screen::GetInstance().GetWidth()); + int16_t screenHeight = static_cast(Screen::GetInstance().GetHeight()); +} +static ImageAnimatorInfo g_imageAnimatorInfo[IMAGE_TOTAL_NUM] = { + {{IMG_DEFAULT_001_PATH}, {0, 0}, screenWidth, screenHeight, IMG_SRC_FILE_PATH}, + {{IMG_DEFAULT_002_PATH}, {0, 0}, screenWidth, screenHeight, IMG_SRC_FILE_PATH}, + {{IMG_DEFAULT_003_PATH}, {0, 0}, screenWidth, screenHeight, IMG_SRC_FILE_PATH}, + {{IMG_DEFAULT_004_PATH}, {0, 0}, screenWidth, screenHeight, IMG_SRC_FILE_PATH}, + {{IMG_DEFAULT_005_PATH}, {0, 0}, screenWidth, screenHeight, IMG_SRC_FILE_PATH}, +}; + +ScreensaverAbilitySlice::~ScreensaverAbilitySlice() +{ + if (imageAnimator_ != nullptr) { + delete imageAnimator_; + imageAnimator_ = nullptr; + } + + if (exitListener_ != nullptr) { + delete exitListener_; + exitListener_ = nullptr; + } +} + +void ScreensaverAbilitySlice::SetCyclePlayView() +{ + imageAnimator_ = new UIImageAnimatorView(); + imageAnimator_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, IMAGE_TOTAL_NUM, IMAGE_ANIMATOR_TIME_S); + rootView_->Add(imageAnimator_); + + auto onClick = [this] (UIView& view, const Event& event) -> bool { + TerminateAbility(); + return true; + }; + exitListener_ = new EventListener(onClick, nullptr); + imageAnimator_->SetOnClickListener(exitListener_); + imageAnimator_->SetTouchable(true); + + imageAnimator_->Start(); +} + +void ScreensaverAbilitySlice::OnStart(const Want &want) +{ + rootView_ = RootView::GetWindowRootView(); + rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full); + + SetCyclePlayView(); + SetUIContent(rootView_); + rootView_->Invalidate(); + AbilitySlice::OnStart(want); +} + +void ScreensaverAbilitySlice::OnInactive() +{ + printf("ScreensaverAbilitySlice::OnInactive\n"); + AbilitySlice::OnInactive(); +} + +void ScreensaverAbilitySlice::OnActive(const Want &want) +{ + printf("ScreensaverAbilitySlice::OnActive\n"); + AbilitySlice::OnActive(want); +} + +void ScreensaverAbilitySlice::OnBackground() +{ + printf("ScreensaverAbilitySlice::OnBackground\n"); + AbilitySlice::OnBackground(); +} + +void ScreensaverAbilitySlice::OnStop() +{ + printf("ScreensaverAbilitySlice::OnStop\n"); + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability_slice.h b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability_slice.h new file mode 100644 index 0000000..0120e72 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/screensaver_ability_slice.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREENSAVER_ABILITY_SLICE_H +#define OHOS_SCREENSAVER_ABILITY_SLICE_H + +#include +#include +#include +#include "components/root_view.h" +#include "components/ui_image_animator.h" +#include "event_listener.h" +#include "ui_config.h" +#include "want.h" + +namespace OHOS { +class ScreensaverAbilitySlice : public AbilitySlice { +public: + ScreensaverAbilitySlice() : rootView_(nullptr), imageAnimator_(nullptr), exitListener_(nullptr) {} + virtual ~ScreensaverAbilitySlice(); + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetCyclePlayView(); + + RootView* rootView_; + UIImageAnimatorView* imageAnimator_; + EventListener* exitListener_; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/ui_config.h b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/ui_config.h new file mode 100644 index 0000000..9ac5a48 --- /dev/null +++ b/bearpi_hm_micro/app/screensaver/screensaver/src/main/cpp/ui_config.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_UI_CONFIG_H +#define OHOS_UI_CONFIG_H + +#include + +namespace OHOS { +static constexpr uint16_t IMAGE_ANIMATOR_TIME_S = 2 * 1000; // Cyclic image playback interval +static constexpr uint8_t IMAGE_TOTAL_NUM = 5; // Number of images + +static const char* const IMG_DEFAULT_001_PATH = + "/storage/app/run/com.huawei.screensaver/screensaver/assets/screensaver/resources/base/media/img_default_1.png"; +static const char* const IMG_DEFAULT_002_PATH = + "/storage/app/run/com.huawei.screensaver/screensaver/assets/screensaver/resources/base/media/img_default_2.png"; +static const char* const IMG_DEFAULT_003_PATH = + "/storage/app/run/com.huawei.screensaver/screensaver/assets/screensaver/resources/base/media/img_default_3.png"; +static const char* const IMG_DEFAULT_004_PATH = + "/storage/app/run/com.huawei.screensaver/screensaver/assets/screensaver/resources/base/media/img_default_4.png"; +static const char* const IMG_DEFAULT_005_PATH = + "/storage/app/run/com.huawei.screensaver/screensaver/assets/screensaver/resources/base/media/img_default_5.png"; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_1.png b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_1.png new file mode 100644 index 0000000..d9af72d Binary files /dev/null and b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_1.png differ diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_2.png b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_2.png new file mode 100644 index 0000000..baa5598 Binary files /dev/null and b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_2.png differ diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_3.png b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_3.png new file mode 100644 index 0000000..4060c7c Binary files /dev/null and b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_3.png differ diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_4.png b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_4.png new file mode 100644 index 0000000..2e2abb3 Binary files /dev/null and b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_4.png differ diff --git a/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_5.png b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_5.png new file mode 100644 index 0000000..acdde94 Binary files /dev/null and b/bearpi_hm_micro/app/screensaver/screensaver/src/main/resources/base/media/img_default_5.png differ diff --git a/bearpi_hm_micro/app/setting/BUILD.gn b/bearpi_hm_micro/app/setting/BUILD.gn new file mode 100644 index 0000000..5cecd37 --- /dev/null +++ b/bearpi_hm_micro/app/setting/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/hap_pack.gni") + +shared_library("setting") { + sources = [ + "setting/src/main/cpp/app_ability_slice.cpp", + "setting/src/main/cpp/app_info_ability_slice.cpp", + "setting/src/main/cpp/main_ability_slice.cpp", + "setting/src/main/cpp/setting_about_ability_slice.cpp", + "setting/src/main/cpp/setting_display_ability_slice.cpp", + "setting/src/main/cpp/setting_main_ability.cpp", + "setting/src/main/cpp/setting_utils.cpp", + "setting/src/main/cpp/setting_wifi_ability_slice.cpp", + "setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp", + "setting/src/main/cpp/wpa_work.c", + ] + + deps = [ + "${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite", + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "//base/powermgr/powermgr_lite/frameworks:powermgr", + "//base/startup/syspara_lite/frameworks/parameter:parameter", + "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//foundation/graphic/surface", + "//foundation/graphic/ui:lite_ui", + "//foundation/graphic/utils:lite_graphic_utils", + "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", + "//utils/native/lite/kv_store:kv_store", + ] + + include_dirs = [ + "setting/src/main/cpp", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", + "${aafwk_lite_path}/interfaces/kits/want_lite", + "//base/startup/syspara_lite/interfaces/kits", + "//base/security/permission/interfaces/kits/permission_lite", + "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/common", + ] + + ldflags = [ + "-lwpa", + "-lwpa_client", + "-lsysparam", + "-lpms_client", + ] + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + ] +} + +hap_pack("setting_hap") { + deps = [ ":setting" ] + mode = "hap" + json_path = "setting/src/main/config.json" + ability_so_path = "$root_out_dir/libsetting.so" + force = "true" + cert_profile = "cert/com.huawei.setting_HarmonyAppProvision_release.p7b" + resources_path = "setting/src/main/resources" + hap_name = "setting" + privatekey = "HOS Application Provision Release" +} diff --git a/bearpi_hm_micro/app/setting/cert/com.huawei.setting_HarmonyAppProvision_release.p7b b/bearpi_hm_micro/app/setting/cert/com.huawei.setting_HarmonyAppProvision_release.p7b new file mode 100644 index 0000000..7892e1e Binary files /dev/null and b/bearpi_hm_micro/app/setting/cert/com.huawei.setting_HarmonyAppProvision_release.p7b differ diff --git a/bearpi_hm_micro/app/setting/setting/src/main/config.json b/bearpi_hm_micro/app/setting/setting/src/main/config.json new file mode 100644 index 0000000..52560f4 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/config.json @@ -0,0 +1,40 @@ +{ + "app": { + "bundleName": "com.huawei.setting", + "vendor": "huawei", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + + } + }, + "module": { + "package": "com.huawei.setting", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", "tv","tablet","car","smartWatch","sportsWatch","smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "setting", + "moduleType": "entry" + }, + "abilities": [{ + "name": "SettingMainAbility", + "icon": "assets/setting/resources/base/media/setting.png", + "label": "test testapp2 1", + "launchType": "standard", + "type": "page", + "visible":true + } + ] + } +} diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_ability_slice.cpp new file mode 100644 index 0000000..e5873ed --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_ability_slice.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "app_ability_slice.h" +#include "gfx_utils/style.h" + +namespace OHOS { +REGISTER_AS(AppAbilitySlice) + +AppAbilitySlice::~AppAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + if (buttonAppInfoListener_) { + delete buttonAppInfoListener_; + buttonAppInfoListener_ = nullptr; + } + if (pBundleInfos_) { + ClearBundleInfo(pBundleInfos_); + } +} + +void AppAbilitySlice::SetButtonListener(void) +{ + auto onClick1 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("MainAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonBackListener_ = new EventListener(onClick1, nullptr); +} + +void AppAbilitySlice::SetAppButtonListener(const char* appName) +{ + auto onClick2 = [this, appName](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + bool ret = SetWantData(&want1, appName, strlen(appName) + 1); + if (ret != true) { + return false; + } + StartAbility(want1); + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("AppInfoAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + ClearWant(&want1); + return true; + }; + buttonAppInfoListener_ = new EventListener(onClick2, nullptr); +} + +void AppAbilitySlice::SetHead() +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("应用"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void AppAbilitySlice::SetAnAppInfo(const int count, BundleInfo& pBundleInfo) +{ + UIViewGroup* itemView = new UIViewGroup(); + char buff[64] = {0}; + int useX = 0; + int useY = count * DE_ITEM_INTERVAL; + itemView->SetPosition(useX, useY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + itemView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + itemView->SetTouchable(true); + + int err = strcpy_s(buff, sizeof(buff), pBundleInfo.bundleName); + if (err != EOK) { + printf("[ERROR]strcpy_s pBundleInfo.bundleName failed, err = %d\n", err); + return; + } + for (size_t i = 0; i < strlen(pBundleInfo.bundleName); i++) { + buff[i] = pBundleInfo.bundleName[i]; + } + SetAppButtonListener(pBundleInfo.bundleName); + itemView->SetOnClickListener(buttonAppInfoListener_); + scrollView_->Add(itemView); + + UIImageView* imageIdView = new UIImageView(); + imageIdView->SetPosition(APP_IMAGE_X, APP_IMAGE_Y, APP_IMAGE_WIDTH, APP_IMAGE_HEIGHT); + imageIdView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + imageIdView->SetSrc(DE_IMAGE_APP); + itemView->Add(imageIdView); + + int bundleNameOffset = 11; + UILabel* name = new UILabel(); + name->SetPosition(APP_NAME_X, APP_NAME_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + name->SetText(pBundleInfo.bundleName + bundleNameOffset); + name->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + name->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + itemView->Add(name); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + itemView->Add(imageView); +} + + +void AppAbilitySlice::SetScrollView() +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + + uint8_t ret = -1; + int num = 0; + ret = GetBundleInfos(1, &pBundleInfos_, &num); + if (ret == 0) { + BundleInfo* pBundleInfo = pBundleInfos_; + for (int count = 0; count < num; count++, pBundleInfo++) { + printf("[LOG]pBundleInfo.bundleName->%s versionName->%s \n", + pBundleInfo->bundleName, pBundleInfo->versionName); + if (pBundleInfo->isSystemApp == false) { + SetAnAppInfo(count, *pBundleInfo); + } + } + } +} + +void AppAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetScrollView(); + SetUIContent(rootView_); +} + +void AppAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void AppAbilitySlice::OnActive(const Want &want) +{ + AbilitySlice::OnActive(want); +} + +void AppAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void AppAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_ability_slice.h new file mode 100644 index 0000000..075ba45 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_ability_slice.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_APP_ABILITY_SLICE_H +#define OHOS_APP_ABILITY_SLICE_H + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_manager.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "module_info.h" +#include "setting_utils.h" +#include "want.h" +#include "securec.h" + +namespace OHOS { +class AppAbilitySlice : public AbilitySlice { +public: + AppAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), pBundleInfos_(nullptr), + buttonBackListener_(nullptr), buttonAppInfoListener_(nullptr) {} + virtual ~AppAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetButtonListener(); + void SetAppButtonListener(const char* appName); + void SetHead(); + void SetScrollView(); + void SetAnAppInfo(int count, BundleInfo& pBundleInfo); + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + BundleInfo* pBundleInfos_; + EventListener* buttonBackListener_; + EventListener* buttonAppInfoListener_; + + constexpr static int APP_IMAGE_X = 12; + constexpr static int APP_IMAGE_Y = 12; + constexpr static int APP_IMAGE_WIDTH = 64; + constexpr static int APP_IMAGE_HEIGHT = 64; + constexpr static int APP_NAME_X = 94; + constexpr static int APP_NAME_Y = 28; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_info_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_info_ability_slice.cpp new file mode 100644 index 0000000..16c34ad --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_info_ability_slice.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "app_info_ability_slice.h" +#include "gfx_utils/style.h" + +namespace OHOS { +REGISTER_AS(AppInfoAbilitySlice) + +AppInfoAbilitySlice::~AppInfoAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + + if (!buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + + if (permissions_) { + free(permissions_); + } + ListNode* node = listListener_.Begin(); + while (node != listListener_.End()) { + delete node->data_; + node = node->next_; + } + listListener_.Clear(); +} + +void AppInfoAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Terminate(); + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void AppInfoAbilitySlice::SetHead() +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + printf("[LOG] bundleName_-> %s +11->%s \n", bundleName_, bundleName_ + 11); // 11 + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText(bundleName_ + 11); // use 11 + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void AppInfoAbilitySlice::SetAppPermissionInfo(int index, PermissionSaved& permissions) +{ + UIViewGroup* itemView = new UIViewGroup(); + int useX = 0; + int useY = index * DE_ITEM_INTERVAL; + itemView->SetPosition(useX, useY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + itemView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + scrollView_->Add(itemView); + + UILabel* nameLabel = new UILabel(); + nameLabel->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + nameLabel->SetText(permissions.name + 16); // 16 is get offset name + nameLabel->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + nameLabel->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER); + nameLabel->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + itemView->Add(nameLabel); + UIToggleButton* togglebutton = new UIToggleButton(); + togglebutton->SetPosition(DE_TOGGLE_BUTTON_X, DE_TOGGLE_BUTTON_Y); + if (permissions.granted == 0) { + togglebutton->SetState(false); + } else { + togglebutton->SetState(true); + } + ToggBtnOnListener* listener = new ToggBtnOnListener(togglebutton); + listener->SetPermissionName(permissions.name, strlen(permissions.name)); + listener->SetBundleName(bundleName_, strlen(bundleName_)); + togglebutton->SetOnClickListener(listener); + listListener_.PushBack(listener); + itemView->Add(togglebutton); +} + +void AppInfoAbilitySlice::PermissionInfoList() +{ + int permNum = 0; + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + int ret = QueryPermission(bundleName_, &permissions_, &permNum); + if (ret == 0) { + printf("[LOG]PermissionInfoList bundleName_ -> %s ,permNum->%d\n", bundleName_, permNum); + for (int i = 0; i < permNum; i++) { + if (permissions_ != nullptr) { + printf("[LOG]PermissionInfoList xxx -> name %s \n", permissions_->name); + SetAppPermissionInfo(i, permissions_[i]); + } + } + } +} + +void AppInfoAbilitySlice::OnStart(const Want& want) +{ + int ret; + printf("[LOG]receive the data -> %s\n", static_cast(want.data)); + AbilitySlice::OnStart(want); + + ret = memcpy_s(bundleName_, sizeof(bundleName_), want.data, want.dataLength); + if (ret != EOK) { + return; + } + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + PermissionInfoList(); + SetUIContent(rootView_); +} + +void AppInfoAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void AppInfoAbilitySlice::OnActive(const Want &want) +{ + AbilitySlice::OnActive(want); +} + +void AppInfoAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void AppInfoAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_info_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_info_ability_slice.h new file mode 100644 index 0000000..586fa4e --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/app_info_ability_slice.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_APP_INFO_ABILITY_SLICE_H +#define OHOS_APP_INFO_ABILITY_SLICE_H + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_manager.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "module_info.h" +#include "setting_utils.h" +#include "want.h" +#include "pms_interface.h" + +#include +#include + +namespace OHOS { + +class ToggBtnOnListener : public UIView::OnClickListener { +public: + + explicit ToggBtnOnListener(UIToggleButton* togglebutton) + { + status_ = false; + togglebutton_ = togglebutton; + } + + virtual ~ToggBtnOnListener(){} + + void SetPermissionName(const char* permissionsName, int nameLength) + { + int ret; + ret = memcpy_s(name_, sizeof(name_), permissionsName, nameLength); + if (ret != EOK) { + printf("[ERR] memcpy_s func[SetToggleButton]\n"); + return; + } + name_[nameLength] = 0; + } + + void SetBundleName(const char* bundleName, int nameLength) + { + int ret; + ret = memcpy_s(bundleName_, sizeof(bundleName_), bundleName, nameLength); + if (ret != EOK) { + printf("[ERR] memcpy_s func[SetBundleName]\n"); + return; + } + bundleName_[nameLength] = 0; + } + + bool OnClick(UIView& view, const ClickEvent& event) override + { + int ret; + if (status_) { + ret = RevokePermission(bundleName_, name_); + if (ret == 0) { + status_ = false; + togglebutton_->SetState(false); + } + } else { + ret = GrantPermission(bundleName_, name_); + if (ret == 0) { + status_ = true; + togglebutton_->SetState(true); + } + } + return true; + } +private: + char name_[128] = {0}; + char bundleName_[128] = {0}; + bool status_ = false; + UIToggleButton* togglebutton_ = nullptr; +}; + +class AppInfoAbilitySlice : public AbilitySlice { +public: + AppInfoAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), permissions_(nullptr), + buttonBackListener_(nullptr) {} + virtual ~AppInfoAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +private: + void SetAppButtonListener(const char* appName); + void SetButtonListener(void); + void SetHead(void); + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + PermissionSaved* permissions_; + EventListener* buttonBackListener_; + List listListener_; + + void PermissionInfoList(); + void SetAppPermissionInfo(int index, PermissionSaved& permissions); + char bundleName_[128]; + +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/event_listener.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/event_listener.h new file mode 100644 index 0000000..980df14 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/event_listener.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include + +#include "components/ui_view.h" + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : public UIView::OnClickListener, public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView& view, const ClickEvent &event) override + { + if (!onClick_) { + return false; + } + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + return onClick_(*currentView, event); + } + + bool OnLongPress(UIView& view, const LongPressEvent &event) override + { + if (!onLongPress_) { + return false; + } + UIView *currentView = &view; + if (currentView == nullptr) { + return false; + } + return onLongPress_(*currentView, event); + } + +private: + OnEventFunc onClick_ {}; + OnEventFunc onLongPress_ {}; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/main_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/main_ability_slice.cpp new file mode 100644 index 0000000..04455c6 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/main_ability_slice.cpp @@ -0,0 +1,360 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "main_ability_slice.h" +#include "ability_loader.h" +#include "ability_slice.h" +#include "ability_info.h" +#include "ability_manager.h" +#include "event_listener.h" +#include "bundle_manager.h" +#include "module_info.h" +#include "element_name.h" +#include "wpa_work.h" +#include "gfx_utils/style.h" +#include +#include + +namespace OHOS { +REGISTER_AS(MainAbilitySlice) + +MainAbilitySlice::~MainAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + + if (buttonWifiListener_) { + delete buttonWifiListener_; + buttonWifiListener_ = nullptr; + } + + if (buttonDisplayListener_) { + delete buttonDisplayListener_; + buttonDisplayListener_ = nullptr; + } + + if (buttonAppListener_) { + delete buttonAppListener_; + buttonAppListener_ = nullptr; + } + + if (buttonAboutListener_) { + delete buttonAboutListener_; + buttonAboutListener_ = nullptr; + } +} + +void MainAbilitySlice::SetButtonListenerWifi(void) +{ + auto onClick1 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("SettingWifiAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonWifiListener_ = new EventListener(onClick1, nullptr); +} + +void MainAbilitySlice::SetButtonListenerApp(void) +{ + auto onClick2 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("AppAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined AppInfoAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonAppListener_ = new EventListener(onClick2, nullptr); +} + +void MainAbilitySlice::SetButtonListenerDisplay(void) +{ + auto onClick3 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("SettingDisplayAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingDisplayAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonDisplayListener_ = new EventListener(onClick3, nullptr); +} + + +void MainAbilitySlice::SetButtonListenerAbout(void) +{ + auto onClick4 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("SettingAboutAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingAboutAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonAboutListener_ = new EventListener(onClick4, nullptr); +} + +void MainAbilitySlice::SetHead(void) +{ + auto toLauncher = [this] (UIView &view, const Event &event) -> bool { + TerminateAbility(); + return true; + }; + buttonBackListener_ = new EventListener(toLauncher, nullptr); + + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("设置"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void MainAbilitySlice::SetWifiButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(WIFI_BUTTON_X, WIFI_BUTTON_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonWifiListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFontWifi = new UILabel(); + lablelFontWifi->SetPosition(DE_TITLE_TEXT_X, WIFI_BUTTON_TEXT_WIFI_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFontWifi->SetText("WiFi"); + lablelFontWifi->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFontWifi->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFontWifi); + + char buff[64] = {0}; + int myX = WIFI_BUTTON_TEXT_SSID_X; + int myY = WIFI_BUTTON_TEXT_SSID_Y; + int ret = GetCurrentConnInfo(buff, sizeof(buff)); + lablelFontSsid_ = new UILabel(); + lablelFontSsid_->SetPosition(myX, myY, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); + if (ret == 0) { + lablelFontSsid_->SetText(buff); + } else { + lablelFontSsid_->SetText("未连接"); + } + + lablelFontSsid_->SetFont(DE_FONT_OTF, DE_SUBTITLE_TEXT_SIZE); + lablelFontSsid_->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + buttonView->Add(lablelFontSsid_); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +void MainAbilitySlice::SetAppButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(APP_BUTTON_X, APP_BUTTON_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonAppListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("应用"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFont); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +void MainAbilitySlice::SetDisplayButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(DISPALY_BUTTON_X, DISPALY_BUTTON_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonDisplayListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("显示"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFont); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +static void setAboutTest(UIViewGroup *buttonView, int positionX, int positionY, const char *setText) +{ + UILabel* lablelFontSystem = new UILabel(); + lablelFontSystem->SetPosition(positionX, positionY, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); + lablelFontSystem->SetText(setText); + lablelFontSystem->SetFont(DE_FONT_OTF, DE_SUBTITLE_TEXT_SIZE); + lablelFontSystem->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + buttonView->Add(lablelFontSystem); +} + +void MainAbilitySlice::SetAboutButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(ABOUT_BUTTON_X, ABOUT_BUTTON_Y, DE_BUTTON_WIDTH, ABOUT_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonAboutListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFontAbout = new UILabel(); + lablelFontAbout->SetPosition(DE_TITLE_TEXT_X, ABOUT_BUTTON_TEXT_ABOUT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFontAbout->SetText("关于"); + lablelFontAbout->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFontAbout->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFontAbout); + + char buff[62]; + const char* gDV = GetDisplayVersion(); + int err = sprintf_s(buff, sizeof(buff), "系统版本: %s", gDV); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + gDV = nullptr; + return; + } + setAboutTest(buttonView, ABOUT_BUTTON_TEXT_SYSTEM_X, ABOUT_BUTTON_TEXT_SYSTEM_Y, buff); + + err = memset_s(buff, sizeof(buff), 0, sizeof(buff)); + if (err < EOK) { + printf("[ERROR]memset_s failed, err = %d\n", err); + return; + } + const char* gPT = GetDeviceType(); + err = sprintf_s(buff, sizeof(buff), "设备名称: %s", gPT); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + gPT = nullptr; + return; + } + setAboutTest(buttonView, ABOUT_BUTTON_TEXT_DEVICE_X, ABOUT_BUTTON_TEXT_DEVICE_Y, buff); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, ABOUT_BUTTON_IMAGE_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +void MainAbilitySlice::SetScrollView() +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(false); + rootView_->Add(scrollView_); + SetWifiButtonView(); + SetAppButtonView(); + SetDisplayButtonView(); + SetAboutButtonView(); +} + +void MainAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + SetButtonListenerWifi(); + SetButtonListenerApp(); + SetButtonListenerDisplay(); + SetButtonListenerAbout(); + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + + SetHead(); + SetScrollView(); + SetUIContent(rootView_); +} + +void MainAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void MainAbilitySlice::OnActive(const Want& want) +{ + char buff[64] = {0}; + int ret = GetCurrentConnInfo(buff, sizeof(buff)); + if (ret == 0) { + printf("##### SetText -> %s \n", buff); + lablelFontSsid_->SetText(buff); + } else { + lablelFontSsid_->SetText("未连接"); + } + AbilitySlice::OnActive(want); +} + +void MainAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void MainAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/main_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/main_ability_slice.h new file mode 100644 index 0000000..9538da4 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/main_ability_slice.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MAIN_ABILITY_SLICE_H +#define OHOS_MAIN_ABILITY_SLICE_H + +#include + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_manager.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "module_info.h" +#include "parameter.h" +#include "pthread.h" +#include "setting_utils.h" +#include "want.h" +#include "wpa_work.h" + +namespace OHOS { +class MainAbilitySlice : public AbilitySlice { +public: + MainAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), lablelFontSsid_(nullptr), + buttonWifiListener_(nullptr), buttonAppListener_(nullptr), buttonDisplayListener_(nullptr), + buttonAboutListener_(nullptr), buttonBackListener_(nullptr) {} + virtual ~MainAbilitySlice(); + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetButtonListenerWifi(); + void SetButtonListenerApp(); + void SetButtonListenerDisplay(); + void SetButtonListenerAbout(); + void SetAboutButtonView(); + void SetAppButtonView(); + void SetDisplayButtonView(); + void SetWifiButtonView(); + void SetScrollView(); + void SetHead(); + + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + UILabel* lablelFontSsid_; + EventListener* buttonWifiListener_; + EventListener* buttonAppListener_; + EventListener* buttonDisplayListener_; + EventListener* buttonAboutListener_; + EventListener* buttonBackListener_; + + constexpr static int WIFI_BUTTON_X = 0; + constexpr static int WIFI_BUTTON_Y = 0; + constexpr static int WIFI_BUTTON_TEXT_WIFI_Y = 13; + constexpr static int WIFI_BUTTON_TEXT_SSID_X = 18; + constexpr static int WIFI_BUTTON_TEXT_SSID_Y = 45; + + constexpr static int APP_BUTTON_X = 0; + constexpr static int APP_BUTTON_Y = 95; + + constexpr static int DISPALY_BUTTON_X = 0; + constexpr static int DISPALY_BUTTON_Y = 190; + + constexpr static int ABOUT_BUTTON_X = 0; + constexpr static int ABOUT_BUTTON_Y = 190 + DE_BUTTON_HEIGHT + 6; + constexpr static int ABOUT_BUTTON_HEIGHT = 113; + constexpr static int ABOUT_BUTTON_TEXT_ABOUT_Y = 5; + constexpr static int ABOUT_BUTTON_TEXT_SYSTEM_X = 18; + constexpr static int ABOUT_BUTTON_TEXT_SYSTEM_Y = 39; + constexpr static int ABOUT_BUTTON_TEXT_DEVICE_X = 18; + constexpr static int ABOUT_BUTTON_TEXT_DEVICE_Y = 72; + constexpr static int ABOUT_BUTTON_IMAGE_Y = 34; +}; +} + +#endif // OHOS_MAIN_ABILITY_SLICE_H diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_about_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_about_ability_slice.cpp new file mode 100644 index 0000000..089a9bd --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_about_ability_slice.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "setting_about_ability_slice.h" +#include "gfx_utils/style.h" + +namespace OHOS { +REGISTER_AS(SettingAboutAbilitySlice) + +SettingAboutAbilitySlice::~SettingAboutAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + for (int count = 0; count < SCROLL_ITEM_NUM; count++) { + if (!itemInfo_[count][1]) { + itemInfo_[count][1] = nullptr; + } + } +} + +void SettingAboutAbilitySlice::SetButtonListener() +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Terminate(); + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingAboutAbilitySlice::SetItemInfo() +{ + itemInfo_[0][0] = (char*) "设备名称"; // 0 + itemInfo_[0][1] = GetDeviceType(); // 0 + itemInfo_[1][0] = (char*) "厂家信息"; // 1 + itemInfo_[1][1] = GetManufacture(); // 1 + itemInfo_[2][0] = (char*) "品牌信息"; // 2 + itemInfo_[2][1] = GetBrand(); // 2 + itemInfo_[3][0] = (char*) "硬件版本号"; // 3 + itemInfo_[3][1] = GetHardwareModel(); // 3 + itemInfo_[4][0] = (char*) "设备序列号"; // 4 + itemInfo_[4][1] = GetSerial(); // 4 + itemInfo_[5][0] = (char*) "操作系统名"; // 5 + itemInfo_[5][1] = GetOSFullName(); // 5 + itemInfo_[6][0] = (char*) "软件版本号"; // 6 + itemInfo_[6][1] = GetDisplayVersion(); // 6 + itemInfo_[7][0] = (char*) "BootLoader版本号"; // 7 + itemInfo_[7][1] = GetBootloaderVersion(); // 7 + itemInfo_[8][0] = (char*) "构建时间"; // 8 + itemInfo_[8][1] = GetBuildTime(); // 8 +} + +void SettingAboutAbilitySlice::SetHead() +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("关于"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingAboutAbilitySlice::SetScrollItem(int count) +{ + int myPositonY = count * DE_ITEM_INTERVAL; + + UIViewGroup* itemView = new UIViewGroup(); + itemView->SetPosition(ITEM_X, myPositonY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + scrollView_->Add(itemView); + + UILabel* lablelFontName = new UILabel(); + lablelFontName->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFontName->SetText(itemInfo_[count][0]); + lablelFontName->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFontName->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + itemView->Add(lablelFontName); + + UILabel* lablelFontInfo = new UILabel(); + lablelFontInfo->SetPosition(ITEM_INFO_X, ITEM_INFO_Y, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); + lablelFontInfo->SetText(itemInfo_[count][1]); + lablelFontInfo->SetFont(DE_FONT_OTF, DE_SUBTITLE_TEXT_SIZE); + lablelFontInfo->SetAlign(TEXT_ALIGNMENT_RIGHT); + lablelFontInfo->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + itemView->Add(lablelFontInfo); +} + +void SettingAboutAbilitySlice::SetScroll() +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + for (int count = 0; count < SCROLL_ITEM_NUM; count++) { + SetScrollItem(count); + } +} + +void SettingAboutAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + SetButtonListener(); + SetItemInfo(); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetHead(); + SetScroll(); + + SetUIContent(rootView_); +} + +void SettingAboutAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingAboutAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void SettingAboutAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingAboutAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_about_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_about_ability_slice.h new file mode 100644 index 0000000..c795739 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_about_ability_slice.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SETTING_ABOUT_ABILITY_SLICE_H +#define OHOS_SETTING_ABOUT_ABILITY_SLICE_H + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "module_info.h" +#include "parameter.h" +#include "setting_utils.h" +#include "want.h" + +namespace OHOS { +class SettingAboutAbilitySlice : public AbilitySlice { +public: + SettingAboutAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), buttonBackListener_(nullptr) {} + virtual ~SettingAboutAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +private: + void SetItemInfo(); + void SetButtonListener(); + void SetScrollItem(int count); + void SetScroll(); + void SetHead(); + + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + EventListener *buttonBackListener_; + constexpr static int SCROLL_ITEM_NUM = 9; + const char *itemInfo_[SCROLL_ITEM_NUM][2]; + constexpr static int ITEM_X = 0; + constexpr static int ITEM_INFO_X = 385; + constexpr static int ITEM_INFO_Y = 36; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_display_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_display_ability_slice.cpp new file mode 100644 index 0000000..9ad7b5a --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_display_ability_slice.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "setting_display_ability_slice.h" +#include +#include +#include +#include "gfx_utils/style.h" + +namespace OHOS { +REGISTER_AS(SettingDisplayAbilitySlice) + +#define COM_SET_ON 0 +#define COM_SET_OFF 1 +#define COM_GET_STATUS 2 + +SettingDisplayAbilitySlice::~SettingDisplayAbilitySlice() +{ + if (toggleButtonView_) { + DeleteChildren(toggleButtonView_); + toggleButtonView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + + if (changeListener_) { + delete changeListener_; + changeListener_ = nullptr; + } + + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } +} + +void SettingDisplayAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("MainAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined MainAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingDisplayAbilitySlice::SetHead(void) +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("显示"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingDisplayAbilitySlice::SetToggleButton(void) +{ + toggleButtonView_ = new UIViewGroup(); + toggleButtonView_->SetPosition(TOGGLE_X, TOGGLE_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + toggleButtonView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + toggleButtonView_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + toggleButtonView_->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + rootView_->Add(toggleButtonView_); + + auto lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("屏保"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + toggleButtonView_->Add(lablelFont); + + SetScreenSaverState(TRUE); + UIToggleButton* togglebutton = new UIToggleButton(); + togglebutton->SetState(true); + changeListener_ = new DisBtnOnStateChangeListener(togglebutton); + togglebutton->SetOnClickListener(changeListener_); + togglebutton->SetPosition(DE_TOGGLE_BUTTON_X, DE_TOGGLE_BUTTON_Y); + toggleButtonView_->Add(togglebutton); +} + +void SettingDisplayAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetToggleButton(); + SetUIContent(rootView_); +} + +void SettingDisplayAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingDisplayAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void SettingDisplayAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingDisplayAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_display_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_display_ability_slice.h new file mode 100644 index 0000000..5d913ad --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_display_ability_slice.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SETTING_DISPLAY_ABILITY_SLICE_H +#define OHOS_SETTING_DISPLAY_ABILITY_SLICE_H + +#include +#include +#include +#include +#include + +#include "ability_loader.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "parameter.h" +#include "samgr_lite.h" +#include "setting_utils.h" + +namespace OHOS { +class DisBtnOnStateChangeListener : public OHOS::UICheckBox::OnChangeListener, public OHOS::UIView::OnClickListener { +public: + ~DisBtnOnStateChangeListener() {} + explicit DisBtnOnStateChangeListener(UIToggleButton* togglebutton) : myTogglebutton(togglebutton) {} + + bool OnChange(UICheckBox::UICheckBoxState state) override + { + return true; + } + bool OnClick(UIView& view, const ClickEvent& event) override + { + bool status = myTogglebutton->GetState(); + SetScreenSaverState(status ? TRUE : FALSE); + + return true; + } +private: + UIToggleButton* myTogglebutton; +}; + +class SettingDisplayAbilitySlice : public AbilitySlice { +public: + SettingDisplayAbilitySlice() + : headView_(nullptr), toggleButtonView_(nullptr), rootView_(nullptr), + buttonBackListener_(nullptr), changeListener_(nullptr) {} + + virtual ~SettingDisplayAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetButtonListener(); + void SetHead(); + void SetToggleButton(); + + UIViewGroup* headView_; + UIViewGroup* toggleButtonView_; + RootView* rootView_; + EventListener* buttonBackListener_; + DisBtnOnStateChangeListener* changeListener_; + constexpr static int TOGGLE_X = 36; + constexpr static int TOGGLE_Y = 72; + + constexpr static int USE_WIFI_FONT_X = 54; + constexpr static int USE_WIFI_FONT_Y = 187; + + constexpr static int ADD_WIFI_X = 0; + constexpr static int SCROLL_WIFI_X = 36; + constexpr static int SCROLL_WIFI_Y = 242; + constexpr static int SCROLL_WIFI_WIDTH = 800; + constexpr static int SCROLL_WIFI_HEIGHT = 238; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_main_ability.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_main_ability.cpp new file mode 100644 index 0000000..080a87f --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_main_ability.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "setting_main_ability.h" + +namespace OHOS { +REGISTER_AA(SettingMainAbility) + +void SettingMainAbility::OnStart(const Want& want) +{ + SetMainRoute("MainAbilitySlice"); + Ability::OnStart(want); +} + +void SettingMainAbility::OnInactive() +{ + Ability::OnInactive(); +} + +void SettingMainAbility::OnActive(const Want& want) +{ + Ability::OnActive(want); +} + +void SettingMainAbility::OnBackground() +{ + Ability::OnBackground(); +} + +void SettingMainAbility::OnStop() +{ + + Ability::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_main_ability.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_main_ability.h new file mode 100644 index 0000000..30038f9 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_main_ability.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MAIN_ABILITY_H +#define OHOS_MAIN_ABILITY_H + +#include "ability_loader.h" + +namespace OHOS { +class SettingMainAbility : public Ability { +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_utils.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_utils.cpp new file mode 100644 index 0000000..8d24987 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_utils.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "setting_utils.h" + +namespace OHOS { +void DeleteChildren(UIView *view) +{ + if (view == nullptr) { + return; + } + while (view != nullptr) { + UIView *tempView = view; + view = view->GetNextSibling(); + if (tempView->IsViewGroup()) { + DeleteChildren(static_cast(tempView)->GetChildrenHead()); + } + if (tempView->GetParent()) { + static_cast(tempView->GetParent())->Remove(tempView); + } + delete tempView; + tempView = nullptr; + } +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_utils.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_utils.h new file mode 100644 index 0000000..986ea58 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_utils.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SETTING_UTILS_H +#define OHOS_SETTING_UTILS_H + +#include "components/ui_view_group.h" + +namespace OHOS { +#define DE_IMAGE_BACK "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/back.png" +#define DE_IMAGE_FORWORD \ + "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/forward.png" +#define DE_IMAGE_ENTER "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/enter.png" +#define DE_IMAGE_APP "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/app.png" +#define DE_FONT_OTF "SourceHanSansSC-Regular.otf" + +#define DE_ROOT_X 0 +#define DE_ROOT_Y 0 +#define DE_ROOT_WIDTH 800 +#define DE_ROOT_HEIGHT 480 +#define DE_ROOT_BACKGROUND_COLOR Color::ColorTo32(Color::Black()) + +#define DE_SCROLL_X 36 +#define DE_SCROLL_Y 72 +#define DE_SCROLL_WIDTH 737 +#define DE_SCROLL_HEIGHT 408 +#define DE_SCROLL_COLOR Color::ColorTo32(Color::Black()) + +#define DE_HEAD_X 0 +#define DE_HEAD_Y 0 +#define DE_HEAD_WIDTH 249 +#define DE_HEAD_HEIGHT 72 + +#define DE_HEAD_IMAGE_X 39 +#define DE_HEAD_IMAGE_Y 16 +#define DE_HEAD_IMAGE_WIDTH 30 +#define DE_HEAD_IMAGE_HEIGHT 30 + +#define DE_HEAD_TEXT_X 100 +#define DE_HEAD_TEXT_Y 15 +#define DE_HEAD_TEXT_WIDTH 180 +#define DE_HEAD_TEXT_HEIGHT 60 +#define DE_HEAD_TEXT_SIZE 32 +#define DE_HEAD_TEXT_COLOR Color::ColorTo32(Color::White()) + +#define DE_BUTTON_WIDTH 737 +#define DE_BUTTON_HEIGHT 89 +#define DE_BUTTON_BACKGROUND_COLOR Color::ColorTo32(Color::GetColorFromRGB(0x33, 0x33, 0x33)) +#define DE_BUTTON_RADIUS 16 + +#define DE_TITLE_TEXT_X 18 +#define DE_TITLE_TEXT_Y 28 +#define DE_TITLE_TEXT_WIDTH 332 +#define DE_TITLE_TEXT_HEIGHT 50 +#define DE_TITLE_TEXT_SIZE 26 +#define DE_TITLE_TEXT_COLOR Color::ColorTo32(Color::White()) + +#define DE_SUBTITLE_TEXT_WIDTH 332 +#define DE_SUBTITLE_TEXT_HEIGHT 40 +#define DE_SUBTITLE_TEXT_COLOR Color::ColorTo32(Color::GetColorFromRGB(0x9F, 0x9F, 0x9F)) +#define DE_SUBTITLE_TEXT_SIZE 24 + +#define DE_FORWARD_IMG_X 709 +#define DE_FORWARD_IMG_Y 24 +#define DE_FORWARD_IMG_WIDTH 12 +#define DE_FORWARD_IMG_HEIGHT 12 + +#define DE_TOGGLE_BUTTON_X 677 +#define DE_TOGGLE_BUTTON_Y 14 + +#define DE_ITEM_INTERVAL 95 +#define DE_CONTENT_FONT_SIZE 28 +#define DE_OPACITY_ALL 255 + +void DeleteChildren(UIView *view); +} +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp new file mode 100644 index 0000000..4f331dd --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "setting_wifi_ability_slice.h" +#include +#include +#include "gfx_utils/style.h" + +namespace OHOS { +REGISTER_AS(SettingWifiAbilitySlice) +static int g_wifiStatus = 0; + +SettingWifiAbilitySlice::SettingWifiAbilitySlice() + : headView_(nullptr), toggleButtonView_(nullptr), scrollView_(nullptr), rootView_(nullptr), + changeListener_(nullptr), buttonBackListener_(nullptr), buttonInputListener_(nullptr), wpaCount_(0) +{ + int taskPeriod = 5000; + Task::Init(); + SetPeriod(taskPeriod); +} + +SettingWifiAbilitySlice::~SettingWifiAbilitySlice() +{ + if (wpaCount_ == 1) { + printf("%s:%d call ExitWpa, this %p\n", __FUNCTION__, __LINE__, this); + ExitWpa(); + ExitWpaScan(); + wpaCount_ = 0; + } + + if (toggleButtonView_) { + DeleteChildren(toggleButtonView_); + toggleButtonView_ = nullptr; + } + + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + + if (changeListener_) { + delete changeListener_; + changeListener_ = nullptr; + } + + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + + if (buttonInputListener_) { + delete buttonInputListener_; + buttonInputListener_ = nullptr; + } +} + +void SettingWifiAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("MainAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingWifiAbilitySlice::SetWifiButtonListener(char* ssid) +{ + auto onClick2 = [this, ssid](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + bool ret = SetWantData(&want1, ssid, strlen(ssid) + 1); + if (ret != true) { + return false; + } + StartAbility(want1); + AbilitySlice* nextSlice = + AbilityLoader::GetInstance().GetAbilitySliceByName("SettingWifiInputPasswordAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonInputListener_ = new EventListener(onClick2, nullptr); +} + +void SettingWifiAbilitySlice::SetHead(void) +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("WiFi"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingWifiAbilitySlice::SetToggleButton(void) +{ + toggleButtonView_ = new UIViewGroup(); + toggleButtonView_->SetPosition(TOGGLE_X, TOGGLE_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + toggleButtonView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + toggleButtonView_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + toggleButtonView_->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + rootView_->Add(toggleButtonView_); + + auto lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("WiFi"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + toggleButtonView_->Add(lablelFont); + + UIToggleButton* togglebutton = new UIToggleButton(); + changeListener_ = new TestBtnOnStateChangeListener(reinterpret_cast(scrollView_)); + togglebutton->SetOnClickListener(changeListener_); + togglebutton->SetPosition(DE_TOGGLE_BUTTON_X, DE_TOGGLE_BUTTON_Y); + togglebutton->SetState(true); + scrollView_->SetVisible(true); + + toggleButtonView_->Add(togglebutton); +} + +void SettingWifiAbilitySlice::SetUseWifi(void) +{ + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(USE_WIFI_FONT_X, USE_WIFI_FONT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("可用WiFi列表"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + rootView_->Add(lablelFont); +} + +void SettingWifiAbilitySlice::AddWifi(void) +{ + int ssidIndex, ssidCount; + + ssidCount = GetIdNum(); + if (ssidCount == 0) { + printf("[LOG]SettingWifiAbilitySlice::AddWifi ssidCount == 0 \n"); + return; + } + if (scrollView_ == nullptr) { + printf("[LOG]SettingWifiAbilitySlice::AddWifi scrollView_ == nullptr \n"); + return; + } + if (g_wifiStatus == 1) { + printf("[LOG]SettingWifiAbilitySlice::AddWifi wifiStatus == 1 \n"); + return; + } + for (ssidIndex = 0; ssidIndex < ssidCount; ssidIndex++) { + UIViewGroup *useWifiView = new UIViewGroup(); + useWifiView->SetPosition(ADD_WIFI_X, DE_ITEM_INTERVAL * ssidIndex, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + useWifiView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + useWifiView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + useWifiView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + useWifiView->SetTouchable(true); + char* buff = GetSsid(ssidIndex); // GetSsid need return point + SetWifiButtonListener(buff); + useWifiView->SetOnClickListener(buttonInputListener_); + scrollView_->Add(useWifiView); + + auto lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText(buff); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + useWifiView->Add(lablelFont); + g_wifiStatus = 1; + } + + scrollView_->Invalidate(); +} + +void SettingWifiAbilitySlice::SetScrollWifi(void) +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, SCROLL_WIFI_Y, DE_SCROLL_WIDTH, SCROLL_WIFI_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + g_wifiStatus = 0; + AddWifi(); +} + +void SettingWifiAbilitySlice::Callback() +{ + if (GetAndResetScanStat() == 1) { + LockWifiData(); + AddWifi(); + UnLockWifiData(); + } +} + +void SettingWifiAbilitySlice::OnStart(const Want& want) +{ + if (wpaCount_ == 0) { + printf("%s:%d call WpaClientStart, this %p\n", __FUNCTION__, __LINE__, this); + WpaClientStart(); + WpaScanReconnect(nullptr, nullptr, HIDDEN_CLOSE); + wpaCount_ = 1; + } + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetUseWifi(); + SetScrollWifi(); + SetToggleButton(); + + TaskExecute(); + SetUIContent(rootView_); +} + +void SettingWifiAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingWifiAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void SettingWifiAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingWifiAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); + if (wpaCount_ == 1) { + printf("%s:%d call ExitWpa, this %p\n", __FUNCTION__, __LINE__, this); + ExitWpa(); + ExitWpaScan(); + wpaCount_ = 0; + } +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_ability_slice.h new file mode 100644 index 0000000..e6f5cb3 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_ability_slice.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SETTING_WIFI_ABILITY_SLICE_H +#define OHOS_SETTING_WIFI_ABILITY_SLICE_H + +#include +#include +#include + +#include "ability_loader.h" +#include "common/task.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "event_listener.h" +#include "gfx_utils/list.h" +#include "parameter.h" +#include "pthread.h" +#include "setting_utils.h" +#include "wpa_work.h" + +namespace OHOS { +class TestBtnOnStateChangeListener : public OHOS::UICheckBox::OnChangeListener, public OHOS::UIView::OnClickListener { +public: + ~TestBtnOnStateChangeListener() {} + explicit TestBtnOnStateChangeListener(UIView* uiView) : myUiView(uiView) {} + + bool OnChange(UICheckBox::UICheckBoxState state) override + { + return true; + } + bool OnClick(UIView& view, const ClickEvent& event) override + { + bool ret = myUiView->IsVisible(); + if (ret == false) { + myUiView->SetVisible(true); + } else { + myUiView->SetVisible(false); + } + myUiView->Invalidate(); + return true; + } + +private: + UIView* myUiView; +}; + +class SettingWifiAbilitySlice : public AbilitySlice, Task { +public: + SettingWifiAbilitySlice(); + virtual ~SettingWifiAbilitySlice(); + void AddWifi(void); + void Callback() override; + +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void MyThread(); + void SetWifiButtonListener(char* ssid); + void SetButtonListener(); + void SetHead(); + void SetToggleButton(); + void SetUseWifi(); + void SetScrollWifi(); + + UIViewGroup* headView_; + UIViewGroup* toggleButtonView_; + UIScrollView* scrollView_; + RootView* rootView_; + TestBtnOnStateChangeListener* changeListener_; + EventListener* buttonBackListener_; + EventListener* buttonInputListener_; + int wpaCount_; + + constexpr static int TOGGLE_X = 36; + constexpr static int TOGGLE_Y = 72; + + constexpr static int USE_WIFI_FONT_X = 54; + constexpr static int USE_WIFI_FONT_Y = 187; + + constexpr static int ADD_WIFI_X = 0; + constexpr static int SCROLL_WIFI_X = 36; + constexpr static int SCROLL_WIFI_Y = 242; + constexpr static int SCROLL_WIFI_WIDTH = 800; + constexpr static int SCROLL_WIFI_HEIGHT = 238; +}; +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp new file mode 100644 index 0000000..2fbbee7 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "gfx_utils/style.h" + +namespace OHOS { +REGISTER_AS(SettingWifiInputPasswordAbilitySlice) + +static UIView::OnClickListener* clickLeftListener_ = nullptr; + +static const int g_maxPassword = 10; // Maximum length of a password. +static char* g_inputSsid = nullptr; +static char g_inputPassword[g_maxPassword + 1] = { 0 }; +static int g_inputCount = 0; +static int g_cursorPositionX = 20; // Initial position of cursor X + +SettingWifiInputPasswordAbilitySlice::~SettingWifiInputPasswordAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + if (inputView_) { + DeleteChildren(inputView_); + inputView_ = nullptr; + } + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + }; + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + if (clickLeftListener_) { + delete clickLeftListener_; + clickLeftListener_ = nullptr; + } +} + +class TestBtnOnClickInputPasswordChangeListener : public OHOS::UIView::OnClickListener { +public: + ~TestBtnOnClickInputPasswordChangeListener() {} + TestBtnOnClickInputPasswordChangeListener(UILabel* uiLabel, UILabel* uiCursor, const int ii, + const int cursorOffset) : myUiLabel(uiLabel), myUiCursor(uiCursor), mIi(ii), myCursorOffset(cursorOffset) {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + if (g_inputCount >= g_maxPassword) { + return true; + } + g_inputPassword[g_inputCount] = '0' + mIi; + myUiLabel->SetText(g_inputPassword); + g_inputCount++; + g_inputPassword[g_inputCount] = '\0'; + g_cursorPositionX += myCursorOffset; + myUiCursor->SetX(g_cursorPositionX); + return true; + } + +private: + UILabel* myUiLabel; + UILabel* myUiCursor; + int mIi; + int myCursorOffset; +}; + +class TestBtnOnClickEnterChangeListener : public OHOS::UIView::OnClickListener { +public: + explicit TestBtnOnClickEnterChangeListener(SettingWifiInputPasswordAbilitySlice* slice) : mySlice(slice) {} + ~TestBtnOnClickEnterChangeListener() {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + WpaScanReconnect(g_inputSsid, g_inputPassword, HIDDEN_OPEN); + g_inputSsid = nullptr; + int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword)); + if (err != EOK) { + printf("[ERROR]memset_s failed, err = %d\n", err); + return false; + } + mySlice->Terminate(); + return true; + } + SettingWifiInputPasswordAbilitySlice* mySlice; +}; + +void SettingWifiInputPasswordAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Terminate(); + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingWifiInputPasswordAbilitySlice::SetHead(void) +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText(g_inputSsid); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingWifiInputPasswordAbilitySlice::SetInput(void) +{ + inputView_ = new UIViewGroup(); + inputView_->SetPosition(INPUT_X, INPUT_Y, INPUT_WIDTH, INPUT_HEIGHT); + + inputView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + inputView_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + inputView_->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + rootView_->Add(inputView_); + + lablelInputText_ = new UILabel(); + lablelInputText_->SetPosition(INPUT_TEXT_X, INPUT_TEXT_Y, INPUT_TEXT_WIDTH, INPUT_TEXT_HEIGHT); + lablelInputText_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + lablelInputText_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + lablelInputText_->SetText("输入密码"); + lablelInputText_->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + inputView_->Add(lablelInputText_); + + lablelCursorText_ = new UILabel(); + lablelCursorText_->SetPosition(g_cursorPositionX, INPUT_CURSOR_Y, INPUT_CURSOR_WIDTH, INPUT_CURSOR_HEIGHT); + lablelCursorText_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x0D, 0x9F, 0xF8))); + lablelCursorText_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + inputView_->Add(lablelCursorText_); + + UIViewGroup* enterView = new UIViewGroup(); + enterView->SetPosition(INPUT_ENTER_X, INPUT_ENTER_Y, INPUT_ENTER_WIDTH, INPUT_ENTER_HEIGHT); + enterView->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x0D, 0x9F, 0xF8))); + enterView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + enterView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + inputView_->Add(enterView); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(INPUT_IMAGE_X, INPUT_IMAGE_Y, INPUT_IMAGE_WIDTH, INPUT_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_ENTER); + enterView->Add(imageView); + imageView->SetTouchable(true); + clickLeftListener_ = new TestBtnOnClickEnterChangeListener(this); + imageView->SetOnClickListener(clickLeftListener_); +} + +void SettingWifiInputPasswordAbilitySlice::AddInputKeyBoardZero(void) +{ + char buf[8] = {0}; + int myUseX = BUTTON_INTERVAL_X; + int myUseY = 198; + int inputNum = 0; + UILabelButton* inputButton = new UILabelButton(); + + inputButton->SetPosition(myUseX, myUseY); + sprintf_s(buf, sizeof(buf), "%d", inputNum); + inputButton->SetWidth(BUTTON_WIDTH); + inputButton->SetHeight(BUTTON_HEIGHT); + inputButton->SetText(buf); + inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS); + inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + inputButton->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + + clickLeftListener_ = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_, (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET); + inputButton->SetOnClickListener(clickLeftListener_); + scrollView_->Add(inputButton); +} + +void SettingWifiInputPasswordAbilitySlice::SetScrollView(void) +{ + char buf[8] = {0}; + int inputNum; + scrollView_ = new UIScrollView(); + scrollView_->SetPosition(SCROLL_WIFI_INPUT_X, SCROLL_WIFI_INPUT_Y, SCROLL_WIFI_INPUT_WIDTH, + SCROLL_WIFI_INPUT_WIDTH); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(false); + rootView_->Add(scrollView_); + for (int countFirst = 0; countFirst < BUTTON_NUM; countFirst++) { + for (int countSecond = 0; countSecond < BUTTON_NUM; countSecond++) { + int myUseX = countSecond * BUTTON_INTERVAL_X; + int myUseY = countFirst * BUTTON_INTERVAL_Y; + inputNum = ((countFirst * BUTTON_NUM) + countSecond + 1); + UILabelButton* inputButton = new UILabelButton(); + inputButton->SetPosition(myUseX, myUseY, BUTTON_WIDTH, BUTTON_HEIGHT); + int err = sprintf_s(buf, sizeof(buf), "%d", inputNum); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + inputButton->SetText(buf); // SetText is system functions + inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS); + inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + inputButton->SetFont(DE_FONT_OTF, DE_CONTENT_FONT_SIZE); + UIView::OnClickListener* clickLeftListener = nullptr; + clickLeftListener = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_, + (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET); + inputButton->SetOnClickListener(clickLeftListener); + scrollView_->Add(inputButton); + } + } + AddInputKeyBoardZero(); +} + +void SettingWifiInputPasswordAbilitySlice::OnStart(const Want& want) +{ + printf("[LOG]receive the data -> %s\n", reinterpret_cast(want.data)); + AbilitySlice::OnStart(want); + + g_inputSsid = reinterpret_cast(want.data); + rootView_ = RootView::GetWindowRootView(); + + int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, g_maxPassword); + if (err != EOK) { + printf("[ERROR]memcpy_s failed, err = %d\n", err); + return; + } + g_inputCount = 0; + g_cursorPositionX = 20; // 20 + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetInput(); + SetScrollView(); + SetUIContent(rootView_); +} + +void SettingWifiInputPasswordAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingWifiInputPasswordAbilitySlice::OnActive(const Want& want) +{ + int err; + lablelInputText_->SetText("输入密码"); + g_cursorPositionX = 20; // 20 + lablelCursorText_->SetX(g_cursorPositionX); + g_inputCount = 0; + err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword)); + if (err != EOK) { + return; + } + AbilitySlice::OnActive(want); +} + +void SettingWifiInputPasswordAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingWifiInputPasswordAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h new file mode 100644 index 0000000..ade1c19 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SETTING_WIFI_INPUT_PASSWORD_ABILITY_SLICE_H +#define OHOS_SETTING_WIFI_INPUT_PASSWORD_ABILITY_SLICE_H + +#include "ability_loader.h" +#include "common/task.h" +#include "components/ui_checkbox.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_view_group.h" +#include "event_listener.h" +#include "parameter.h" +#include "setting_utils.h" +#include "setting_wifi_input_password_ability_slice.h" +#include "wpa_work.h" + +namespace OHOS { +class SettingWifiInputPasswordAbilitySlice : public AbilitySlice { +public: + SettingWifiInputPasswordAbilitySlice() : headView_(nullptr), inputView_(nullptr), lablelCursorText_(nullptr), + scrollView_(nullptr), rootView_(nullptr), lablelInputText_(nullptr), + buttonBackListener_(nullptr) {} + virtual ~SettingWifiInputPasswordAbilitySlice(); + +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetHead(); + void SetInput(); + void AddInputKeyBoardZero(); + void SetScrollView(); + void SetButtonListener(); + UIViewGroup* headView_; + UIViewGroup* inputView_; + UILabel* lablelCursorText_; + UIScrollView* scrollView_; + RootView* rootView_; + UILabel* lablelInputText_; + EventListener* buttonBackListener_; + + constexpr static int CURSOR_POSITION_OFFSET = 16; + constexpr static int RECT_RADIUS = 6; + constexpr static int INPUT_X = 36; + constexpr static int INPUT_Y = 108; + constexpr static int INPUT_WIDTH = 737; + constexpr static int INPUT_HEIGHT = 54; + constexpr static int INPUT_RADIUS = 32; + constexpr static int INPUT_CURSOR_X = 20; + constexpr static int INPUT_CURSOR_Y = 10; + constexpr static int INPUT_CURSOR_WIDTH = 2; + constexpr static int INPUT_CURSOR_HEIGHT = 36; + constexpr static int INPUT_TEXT_X = 24; + constexpr static int INPUT_TEXT_Y = 11; + constexpr static int INPUT_TEXT_WIDTH = 398; + constexpr static int INPUT_TEXT_HEIGHT = 38; + constexpr static int INPUT_FONT_SIZE = 28; + constexpr static int INPUT_ENTER_X = 650; + constexpr static int INPUT_ENTER_Y = 6; + constexpr static int INPUT_ENTER_WIDTH = 70; + constexpr static int INPUT_ENTER_HEIGHT = 42; + constexpr static int INPUT_ENTER_RADIUS = 32; + + constexpr static int INPUT_IMAGE_X = 10; + constexpr static int INPUT_IMAGE_Y = 2; + constexpr static int INPUT_IMAGE_WIDTH = 38; + constexpr static int INPUT_IMAGE_HEIGHT = 38; + + constexpr static int BUTTON_NUM = 3; + + constexpr static int BUTTON_INTERVAL_X = 140; + constexpr static int BUTTON_INTERVAL_Y = 66; + constexpr static int BUTTON_WIDTH = 132; + constexpr static int BUTTON_HEIGHT = 54; + + constexpr static int SCROLL_WIFI_INPUT_X = 228; + constexpr static int SCROLL_WIFI_INPUT_Y = 198; + constexpr static int SCROLL_WIFI_INPUT_WIDTH = 439; + constexpr static int SCROLL_WIFI_INPUT_HEIGHT = 252; +}; + +} // namespace OHOS +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/wpa_work.c b/bearpi_hm_micro/app/setting/setting/src/main/cpp/wpa_work.c new file mode 100644 index 0000000..b504a05 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/wpa_work.c @@ -0,0 +1,542 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wpa_work.h" +#include + +#define WPA_IFACE_NAME "wlan0" +#define WIFI_AUTH_FAILED_REASON_STR "WRONG_KEY" +#define WIFI_AUTH_FAILED_REASON_CODE "reason=2" +#define WPA_CTRL_REQUEST_OK "OK" +#define WPA_CTRL_REQUEST_FAIL "FAIL" + +#define SAMPLE_INFO(format, args...) \ + do { \ + fprintf(stderr, "\033[1;32m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + +#define SAMPLE_ERROR(format, args...) \ + do { \ + fprintf(stderr, "\033[1;31m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + + +static struct wpa_ctrl *g_monitorConn = NULL; +static pthread_t g_threadId = 0; +pthread_mutex_t g_mutex; +pthread_mutex_t g_monitorConnMutex; +static char g_mySsidD[40][40] = {0}; +static int g_ssidCount = 0; + +static char g_useSsidD[40][40] = {0}; +static int g_useSsidCount = 0; + +static pthread_t g_wpaThreadId = 0; +static pthread_t g_scanThreadId = 0; +static int g_scanAvailable = 0; +static int g_Connect = 0; + +static void DumpString(const char *buf, int len, const char *tag) +{ + SAMPLE_INFO("%s dump start.", tag); + for (int i = 0; i < len; i++) { + printf("%c", buf[i]); + } + printf("\n"); + + printf("\n"); + SAMPLE_INFO("%s dump end.", tag); +} + +static int StrMatch(const char *a, const char *b) +{ + return strncmp(a, b, strlen(b)) == 0; +} + +static int SendCtrlCommand(const char *cmd, char *reply, size_t *replyLen) +{ + static struct wpa_ctrl *ctrlConn = NULL; + int i = 0; + int reTime = 5; + while (i++ < reTime) { // check wpa init success + ctrlConn = wpa_ctrl_open(WPA_IFACE_NAME); + if (ctrlConn != NULL) { + break; + } + sleep(1); + } + if (ctrlConn == NULL) { + printf("%s:%d [ERROR] control connect handle is null\n", __FUNCTION__, __LINE__); + return -1; + } + size_t len = *replyLen - 1; + wpa_ctrl_request(ctrlConn, cmd, strlen(cmd), reply, &len, 0); + DumpString(reply, len, "SendCtrlCommand raw return"); + if (ctrlConn != NULL) { + wpa_ctrl_close(ctrlConn); + ctrlConn = NULL; + } + + if (len != 0 && !StrMatch(reply, WPA_CTRL_REQUEST_FAIL)) { + *replyLen = len; + return 0; + } + SAMPLE_ERROR("send ctrl request [%s] failed.", cmd); + return -1; +} + +static void ProcessScanResult(const char *buf, int len) +{ + int myUse1 = 1; + int myUse2 = 2; + int myUse5 = 5; + + int err = memset_s(g_mySsidD, sizeof(g_mySsidD), 0, sizeof(g_mySsidD)); + g_ssidCount = 0; + if (err != EOK) { + return; + } + for (int i = 0; i < len; i++) { + if (buf[i] == 'E' && buf[i + myUse1] == 'S' && buf[i + myUse2] == 'S') { + int j; + printf("[ERROR]get the ESS i->%d\n", i); + i = i + myUse5; + for (j = 0; i < len && buf[i] != '\n'; i++) { + g_mySsidD[g_ssidCount][j] = buf[i]; + j++; + } + + g_ssidCount++; + } + } +} + +void ResetSSIDBuff(void) +{ + g_useSsidCount = 0; +} + +int GetIdNum(void) +{ + return g_useSsidCount; +} + +int GetCurrentConnInfo(char *ssid, int len) +{ + int offset = 5; + char connInfo[2048] = {0}; + size_t infoLen = sizeof(connInfo); + if (g_Connect == 0) { + printf("[LOG]GetCurrentConnInfo g_Connect -> 0 \n"); + return -1; + } + + int ret = SendCtrlCommand("STATUS", connInfo, &infoLen); + if (ret == -1) { + printf("[ERROR]GetCurrentConnInfo Command(STATUS) Error \n"); + return -1; + } + DumpString(connInfo, infoLen, "connInfo"); + printf("[LOG]end the DumpStrint\n"); + char *pos = strstr(connInfo, "ssid="); + if (pos == NULL) { + printf("[ERROR]strstr(ssid) is null"); + return -1; + } + if (offset >= strlen(pos)) { + return -1; + } + pos += offset; + pos = strstr(pos, "ssid="); + if (pos == NULL) { + printf("[ERROR]second strstr(ssid) is null"); + return -1; + } + char *end = strchr(pos, '\n'); + if (end == NULL) { + printf("[ERROR]second strstr(end) is null"); + return -1; + } + int ssidLen = end - pos - offset; + if (len < ssidLen) { + return -1; + } + int i = 0; + int myOffset = 5; + for (pos += myOffset; pos < end; pos++, i++) { + ssid[i] = *pos; + } + return 0; +} + +void ExitWpaScan(void) +{ + int ret; + if (g_scanThreadId != 0) { + ret = pthread_cancel(g_scanThreadId); + printf("[INFO]pthread_cancel(g_scanThreadId) ret -> %d \n", ret); + g_scanThreadId = 0; + } + if (g_wpaThreadId != 0) { + ret = pthread_cancel(g_wpaThreadId); + printf("[INFO]pthread_cancel(g_wpaThreadId) ret -> %d \n", ret); + g_wpaThreadId = 0; + } +} + +void ExitWpa(void) +{ + int ret; + pthread_mutex_lock(&g_monitorConnMutex); + if (g_monitorConn != NULL) { + wpa_ctrl_close(g_monitorConn); + printf("[INFO]wpa_ctrl_close(g_monitorConn).\n"); + g_monitorConn = NULL; + } + pthread_mutex_unlock(&g_monitorConnMutex); + char result[100] = {0}; + size_t len = sizeof(result); + printf("[INFO]ExitWpa TERMINATE begin.\n"); + SendCtrlCommand("TERMINATE", result, &len); + printf("[INFO]ExitWpa TERMINATE end.\n"); + sleep(1); + DeinitWifiService(); + if (g_threadId != 0) { + ret = pthread_cancel(g_threadId); + if (ret != 0) { + printf("[ERROR]pthread_cancel(g_threadId) ret -> %d \n", ret); + } + } + + ret = pthread_mutex_destroy(&g_mutex); + if (ret != 0) { + printf("[ERROR]pthread_mutex_destroy ret -> %d \n", ret); + } + ret = pthread_mutex_destroy(&g_monitorConnMutex); + if (ret != 0) { + printf("[ERROR]pthread_mutex_destroy ret -> %d \n", ret); + } +} + +int GetAndResetScanStat(void) +{ + int ret = g_scanAvailable; + g_scanAvailable = 0; + return ret; +} + +char *GetSsid(const int ssidNum) +{ + return g_useSsidD[ssidNum]; +} + +void LockWifiData() +{ + pthread_mutex_lock(&g_mutex); +} + +void UnLockWifiData() +{ + pthread_mutex_unlock(&g_mutex); +} + +static void CheckSsid(void) +{ + int i, ret, err; + int checkSsid = 0x5C; + + err = memset_s(g_useSsidD, sizeof(g_useSsidD), 0, sizeof(g_useSsidD)); + if (err != EOK) { + printf("[ERROR]memset_s g_useSsidD failed, err = %d\n", err); + return; + } + g_useSsidCount = 0; + for (i = 0; i < g_ssidCount; i++) { + int j = i + 1; + for (; j < g_ssidCount; j++) { + ret = strcmp(g_mySsidD[i], g_useSsidD[j]); + if (ret == 0) { + break; + } + } + if ((g_mySsidD[i][0] == checkSsid) || (g_mySsidD[i][0] == 0)) { + continue; + } + if (j == g_ssidCount) { + err = strcpy_s(g_useSsidD[g_useSsidCount], sizeof(g_useSsidD[g_useSsidCount]), g_mySsidD[i]); + if (err != EOK) { + continue; + } + g_useSsidCount++; + } + } +} + +static void WifiEventHandler(char *rawEvent, int len) +{ + char *pos = rawEvent; + if (*pos == '<') { + pos = strchr(pos, '>'); + if (pos) { + pos++; + } else { + pos = rawEvent; + } + } + if (StrMatch(pos, WPA_EVENT_CONNECTED)) { + SAMPLE_INFO("WIFI_EVENT_CONNECTED"); + g_Connect = 1; + return; + } + if (StrMatch(pos, WPA_EVENT_SCAN_RESULTS)) { + pthread_mutex_lock(&g_mutex); + SAMPLE_INFO("WIFI_EVENT_SCAN_DONE"); + char scanResult[4096] = {0}; + size_t scanLen = sizeof(scanResult); + SendCtrlCommand("SCAN_RESULTS", scanResult, &scanLen); + + ProcessScanResult(scanResult, scanLen); + CheckSsid(); + g_scanAvailable = 1; + pthread_mutex_unlock(&g_mutex); + return; + } + if (StrMatch(pos, WPA_EVENT_TEMP_DISABLED) && strstr(pos, WIFI_AUTH_FAILED_REASON_STR)) { + SAMPLE_INFO("WIFI_EVENT_WRONG_KEY"); + return; + } + if (StrMatch(pos, WPA_EVENT_DISCONNECTED) && !strstr(pos, WIFI_AUTH_FAILED_REASON_CODE)) { + SAMPLE_INFO("WIFI_EVENT_DISCONNECTED"); + return; + } +} + +static void CliRecvPending(void) +{ + int pendingResult = -1; + pthread_mutex_lock(&g_monitorConnMutex); + if (g_monitorConn!= NULL) { + pendingResult = wpa_ctrl_pending(g_monitorConn); + } + while (pendingResult > 0) { + char buf[4096]; + size_t len = sizeof(buf) - 1; + if (g_monitorConn != NULL) { + if (wpa_ctrl_recv(g_monitorConn, buf, &len) == 0) { + buf[len] = '\0'; + SAMPLE_INFO("event received %s", buf); + WifiEventHandler(buf, len); + } else { + SAMPLE_INFO("could not read pending message."); + break; + } + pendingResult = wpa_ctrl_pending(g_monitorConn); + } + } + pthread_mutex_unlock(&g_monitorConnMutex); +} + +static void* MonitorTask(void *args) +{ + (void)args; + int fd, ret; + fd_set rfd; + while (1) { + fd = -1; + pthread_mutex_lock(&g_monitorConnMutex); + if (g_monitorConn != NULL) { + fd = wpa_ctrl_get_fd(g_monitorConn); + } else { + pthread_mutex_unlock(&g_monitorConnMutex); + break; + } + pthread_mutex_unlock(&g_monitorConnMutex); + FD_ZERO(&rfd); + FD_SET(fd, &rfd); + ret = select(fd + 1, &rfd, NULL, NULL, NULL); + if (ret <= 0) { + SAMPLE_INFO("select failed ret = %d\n", ret); + break; + } + CliRecvPending(); + sleep(1); + } + return NULL; +} + +static void TestNetworkConfig(const char *gSsid, const char *gPassWord) +{ + char networkId[20] = {0}; + size_t networkIdLen = sizeof(networkId); + int ret = SendCtrlCommand("DISCONNECT", networkId, &networkIdLen); + ret += SendCtrlCommand("ADD_NETWORK", networkId, &networkIdLen); + if (ret != 0) { + SAMPLE_ERROR("add network failed."); + return; + } + SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, networkId); + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + char cmd[200] = {0}; + int err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"%s\"", networkIdLen, networkId, gSsid); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"%s\"", networkIdLen, networkId, gPassWord); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + err = sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, networkId); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + ret += SendCtrlCommand("RECONNECT", reply, &replyLen); + replyLen = sizeof(reply); + if (ret == 0) { + SAMPLE_INFO("network config success."); + return; + } + err = sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, networkId); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + SendCtrlCommand(cmd, reply, &replyLen); + SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, networkId); +} + +int InitControlInterface() +{ + int i = 0; + int ret; + int reTime = 5; + while (i++ < reTime) { // create control interface for event monitor + g_monitorConn = wpa_ctrl_open(WPA_IFACE_NAME); + if (g_monitorConn != NULL) { + break; + } + sleep(1); + } + if (!g_monitorConn) { + SAMPLE_ERROR("open wpa control interface failed."); + return -1; + } + + ret = -1; + pthread_mutex_lock(&g_monitorConnMutex); + if (g_monitorConn!= NULL) { + ret = wpa_ctrl_attach(g_monitorConn); + } + pthread_mutex_unlock(&g_monitorConnMutex); + printf("[INFO]wpa_ctrl_attach return %d.\n", ret); + if (ret == 0) { // start monitor + ret = pthread_create(&g_wpaThreadId, NULL, MonitorTask, NULL); // create thread for read event + if (ret != 0) { + printf("[ERROR]thread error %s\n", strerror(ret)); + return -1; + } + return 0; + } + return -1; +} + +void* WpaScanThread(void *args) +{ + int mySleep = 2; + int ret = 0; + sleep(mySleep); + if (g_monitorConn == NULL) { + ret = InitControlInterface(); + printf("%s:%d [INFO] InitControlInterface return %d.\n", __FUNCTION__, __LINE__, ret); + if (ret == -1) { + return NULL; + } + } + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + g_scanAvailable = 0; + SendCtrlCommand("SCAN", reply, &replyLen); + return NULL; +} + +void WpaScanReconnect(const char *gSsid, const char *gPassWord, const int hiddenSwitch) +{ + if (HIDDEN_OPEN == hiddenSwitch) { + TestNetworkConfig(gSsid, gPassWord); + } else { + int ret = pthread_create(&g_scanThreadId, NULL, WpaScanThread, NULL); // create thread for read event + if (ret != 0) { + printf("[ERROR]thread error %s\n", strerror(ret)); + return; + } + } +} + +static void *ThreadMain() +{ + prctl(PR_SET_NAME, "WPA_THREAD"); + int i = 0; + int myfor = 5; + char *arg[20] = {0}; + arg[i] = (char *)"wpa_supplicant"; + arg[++i] = (char *)"-i"; + arg[++i] = (char *)"wlan0"; + arg[++i] = (char *)"-c"; + arg[++i] = (char *)"/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/element/wpa_supplicant.conf"; + + for (i = 0; i < myfor; i++) { + printf("[LOG]arg[%d]->%s \n", i, arg[i]); + } + wpa_main(myfor, arg); + return NULL; +} + +void WpaClientStart(void) +{ + static int runStatus = 0; + if (runStatus == 0) { + int ret = pthread_create(&g_threadId, NULL, ThreadMain, NULL); + if (ret != 0) { + printf("[ERROR]thread error %s\n", strerror(ret)); + return; + } + ret = pthread_mutex_init(&g_mutex, NULL); + if (ret != 0) { + printf("[ERROR]pthread_mutex_init error %s\n", strerror(ret)); + return; + } + + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + ret = pthread_mutex_init(&g_monitorConnMutex, &attr); + if (ret != 0) { + printf("[ERROR]pthread_mutex_init error %s\n", strerror(ret)); + return; + } + runStatus = 1; + } +} diff --git a/bearpi_hm_micro/app/setting/setting/src/main/cpp/wpa_work.h b/bearpi_hm_micro/app/setting/setting/src/main/cpp/wpa_work.h new file mode 100644 index 0000000..7ef41a0 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/cpp/wpa_work.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_WPA_WORK_H +#define OHOS_WPA_WORK_H + +#include +#include +#include + +#include "parameter.h" +#include "pthread.h" +#include "securec.h" +#include "wpa_ctrl.h" +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + #define HIDDEN_CLOSE 0 + #define HIDDEN_OPEN 1 + void ExitWpaScan(void); + void ExitWpa(void); + int wpa_main(int argc, char *argv[]); + int GetCurrentConnInfo(char *ssid, int len); + void DeinitWifiService(); + void ResetSSIDBuff(void); + int GetIdNum(void); + int GetAndResetScanStat(void); + char *GetSsid(const int ssidNum); + void WpaScanReconnect(const char *gSsid, const char *gPassWord, const int hiddenSwitch); + void WpaClientStart(void); + void LockWifiData(void); + void UnLockWifiData(void); +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ +#endif diff --git a/bearpi_hm_micro/app/setting/setting/src/main/resources/base/element/wpa_supplicant.conf b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/element/wpa_supplicant.conf new file mode 100644 index 0000000..029e475 --- /dev/null +++ b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/element/wpa_supplicant.conf @@ -0,0 +1,6 @@ +country=GB +ctrl_interface=udp +network={ +} + + diff --git a/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/app.png b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/app.png new file mode 100644 index 0000000..8a5d6c0 Binary files /dev/null and b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/app.png differ diff --git a/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/back.png b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/back.png new file mode 100644 index 0000000..9dc41df Binary files /dev/null and b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/back.png differ diff --git a/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/enter.png b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/enter.png new file mode 100644 index 0000000..ffbfa96 Binary files /dev/null and b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/enter.png differ diff --git a/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/forward.png b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/forward.png new file mode 100644 index 0000000..9d63c11 Binary files /dev/null and b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/forward.png differ diff --git a/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/setting.png b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/setting.png new file mode 100644 index 0000000..f36e3e4 Binary files /dev/null and b/bearpi_hm_micro/app/setting/setting/src/main/resources/base/media/setting.png differ diff --git a/bearpi_hm_micro/docs/README.md b/bearpi_hm_micro/docs/README.md new file mode 100644 index 0000000..e0c4b31 --- /dev/null +++ b/bearpi_hm_micro/docs/README.md @@ -0,0 +1,44 @@ + +--- +# 小熊派-鸿蒙·叔(BearPi-HM Micro) + +## 一、BearPi-HM Micro 开发板介绍 + +BearPi-HM Micro开发板是一块高度集成并可运行OpenHarmony系统的开发板,板载高性能的工业级处理器STM32MP157芯片,搭配4.3寸LCD电容式触摸屏,并板载wifi电路及标准的E53接口,标准的E53接口可扩展智能加湿器、智能台灯、智能安防、智能烟感等案例。可折叠式屏幕设计大大提高用户开发体验,便于携带和存放,更好地满足不同用户的需求,拓展无限可能。 + + + +## 二、设备开发手把手教程系列 + +1、视频系列(B站) 对应PPT地址 +* [P1. 初识BearPi-HM Micro开发板](https://www.bilibili.com/video/BV12Y411H7ry?p=1) +* [P2. 开发环境搭建(复杂)——编译环境搭建](https://www.bilibili.com/video/BV12Y411H7ry?p=3) +* [P3. 开发环境搭建(复杂)——源码获取及编译](https://www.bilibili.com/video/BV12Y411H7ry?p=4) +* [P4. BearPi-HM Micro开发板源码烧录](https://www.bilibili.com/video/BV12Y411H7ry?p=5) +* [P5. OpenHarmony 快速入门:HelloWorld](https://www.bilibili.com/video/BV12Y411H7ry?p=6) +* [P6. OpenHarmony 快速入门:点亮LED](https://www.bilibili.com/video/BV12Y411H7ry?p=7) +* [P7. OpenHarmony 快速入门:安装HAP](https://www.bilibili.com/video/BV12Y411H7ry?p=8) +* [P8. OpenHarmony 快速入门:JS点亮LED](https://www.bilibili.com/video/BV12Y411H7ry?p=9) + +2、文档系列(Gitee) +- [1. 初识BearPi-HM Micro开发板](device-dev/BearPi-HM_Micro开发板介绍.md) +- [2. 开发环境搭建](device-dev/BearPi-HM_Micro开发环境搭建.md) +- [3. 源码获取、编译烧录](device-dev/BearPi-HM_Micro开发板编译调试.md) +- [4. 编写一个hello_world程序](device-dev/编写一个hello_world程序.md) +- [5. 如何运行一个可执行程序](device-dev/如何运行一个可执行程序.md) +- [6. 编写一个点亮LED灯程序](device-dev/编写一个点亮LED灯程序.md) +- [7. 通过JS应用控制LED灯](device-dev/通过JS应用控制LED灯.md) +- [8. 如何在开发板上安装HAP应用](device-dev/如何在开发板上安装HAP应用.md) + + +## 三、开发板图片预览[(淘宝链接)](https://item.taobao.com/item.htm?id=662078665554) + +[![](figures/BearPi-HM_Micro_Info.png)](https://item.taobao.com/item.htm?id=633296694816) + + +## 四、联系我们 + + + +![](figures/bearpi_club_wechat.jpg) + diff --git a/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板介绍.md b/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板介绍.md new file mode 100644 index 0000000..235bc3a --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板介绍.md @@ -0,0 +1,74 @@ +# BearPi-HM Micro开发板介绍 + +本文档介绍BearPi-HM Micro开发板外观、功能、资源及参数规格,通过学习,开发者会对BearPi-HM Micro开发板有初步认识,并可上手业务开发。 + +## 开发板简介 + +BearPi-HM Micro开发板是一块高度集成并可运行OpenHarmony系统的开发板,板载高性能的工业级处理器STM32MP157芯片,搭配4.3寸LCD电容式触摸屏,并板载wifi电路及标准的E53接口,标准的E53接口可扩展智能加湿器、智能台灯、智能安防、智能烟感等案例。可折叠式屏幕设计大大提高用户开发体验,便于携带和存放,更好地满足不同用户的需求,拓展无限可能。 + +### 购买地址 [点击购买](https://item.taobao.com/item.htm?id=662078665554) + +### 视频教程地址 [点击观看](https://space.bilibili.com/470797387?spm_id_from=333.788.b_765f7570696e666f.2) + +![](figures/板子功能图.png) + + + +## 开发板硬件资源 + +**表 1** BearPi-HM Micro开发板硬件规格清单 + + +| No | 功能 | 参数 | +| ---- | ---- | ------------------------------------------------------------ | +| 1 | Power | 5V TypeC供电| +| 2 | eMMC | 1x4GB| +| 3 | DDR | 1x4Gb| +| 4 | LCD | 4.3寸电容式触摸屏| +| 5 |WIFI | Hi3881 支持WIFI 2.4G| +| 6 | KEY| Reset key x1, User Key x1 | +| 7 | TypeC | 集供电、烧录程序、调试一体| +| 8 | LED | LED x1| +| 9 | USB HOST | 一路 USB2.0 HOST| +| 10 |AUDIO |4段式耳机 x1,MIC咪头 x1,扬声器 x1| +| 11 |Expansion | 支持 标准E53接口 x1, S24接口 x1| + +## 开发板软件资源 + +* 搭载OpenHarmony 3.0 操作系统 +* 轻量级ACE UI框架,支持JS应用开发部署 +* 支持分布式软总线,用户应用程序可分可合、可跨设备流转(TODO) + + +**表 2** BearPi-HM Micro开发板平台驱动支持清单 + +| No | 类别   | 是否支持                                            | +| ---- | ---- | ------------- | +| 1 | GPIO | 支持| +| 2 | I2C | 支持| +| 3 |RTC | TODO| +| 4 | SDIO | 支持| +| 5 | SPI | 支持| +| 6 | UART | 支持| +| 7 | WATCHDOG | 支持| +| 9 |MMC | 支持| +| 10 |PWM | 支持| +| 11 |ADC | 支持| +| 12 |I2S | TODO| + +**表 3** BearPi-HM Micro开发板外设驱动支持清单 + +| No | 类别             | 是否支持                                            | +| ---- | ---- | ------ | +| 1 | LCD | 支持| +| 2 | TOUCH | 支持| +| 3 | AUDIO | TODO| +| 4 | NETWORK | 支持| +| 5 | SENSOR | TODO| +| 6 | USB | TODO| + + + + + + diff --git a/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板编译调试.md b/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板编译调试.md new file mode 100644 index 0000000..751eeb2 --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发板编译调试.md @@ -0,0 +1,120 @@ +# BearPi-HM_Micro开发板编译调试 + +## 获取源码 +``` +repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify +repo sync -c +repo forall -c 'git lfs pull' +./build/prebuilts_download.sh +``` +## 源码编译 + +1. 在源码目录下输入以下指令。 + ``` + hb set + + bearpi + >bearpi_hm_micro + + 选择bearpi_hm_micro + + hb build --tee -f + ``` + +2. 查看编译出的固件位置 + + 当编译完后,在Windows中可以直接查看到最终编译的固件,具体路径在: + `\out\bearpi-hm_micro\bearpi-hm_micro` + 其中有以下文件是后面烧录系统需要使用的。 + + * OHOS_Image.stm32:系统镜像文件 + * rootfs_vfat.img:根文件系统 + * userfs_vfat.img:用户文件系统 + + + ![](figures/查看编译输出文件.png) + +3. 在MobaXterm中执行以下三条指令将以上三个文件复制到`device/board/bearpi/bearpi_hm_micro/tools/download_img/kernel/`下,以便后续烧录系统使用 + + ``` + cp out/bearpi_hm_micro/bearpi_hm_micro/OHOS_Image.stm32 device/board/bearpi/bearpi_hm_micro/tools/download_img/kernel/ + cp out/bearpi_hm_micro/bearpi_hm_micro/rootfs_vfat.img device/board/bearpi/bearpi_hm_micro/tools/download_img/kernel/ + cp out/bearpi_hm_micro/bearpi_hm_micro/userfs_vfat.img device/board/bearpi/bearpi_hm_micro/tools/download_img/kernel/ + ``` + +## 烧录镜像并启动开发板 +### 一、安装软件 + +- 下载并安装JRE + - 下载地址(百度云):https://pan.baidu.com/s/1yuMn4YulfP6j50mQA8faoA 提取码:1234 + +- 下载并安装STM32CubeProgrammer(需要2.4.0+版本) + - 下载地址(百度云):https://pan.baidu.com/s/1XfFFFs7UdE9TPdN0FdA6ag 提取码:1234 + +### 二、连接开发板 + +1. 通过TypeC数据线,把电脑与BearPi-HM Micro连接。 + +2. 安装CH340驱动。 + + 下载地址:http://www.wch.cn/downloads/CH341SER_EXE.html + +3. 关闭虚拟机捕获USB功能。(有很多开发者都是因为虚拟机捕获了USB设备,导致本机Windows电脑看不到串口) + + ![](figures/关闭虚拟机捕获USB.png) + + 如果上面操作不行,直接关闭VMware Workstation,选择挂起,然后再重新插拔USB。 + +4. 查看开发板的串口 + + ![](figures/获取到开发板串口号.png) + +### 三、烧录镜像 + +1. 将开发板的拨码开关上拨到“000”烧录模式,并按一下开发板的RESET按键 + + ![](figures/进入烧录模式.png) + +2. 在Windows打开STM32CubeProgrammer工具,选择“USB”烧录方式,再点击刷新按钮,然后点击"Connect"。 + + ![](figures/连接USB.png) + +3. 点击STM32CubeProgrammer工具的“+”按钮,然后选择烧录配置的tvs文件。 + + ![](figures/选择烧录配置文件.png) + +4. 点击Browse按钮,然后选择工程源码下的烧录镜像路径 + + ![](figures/选择镜像文件路径.png) + +5. 点击Download按钮启动镜像烧录,并等待烧录完毕。 + + ![](figures/烧录镜像.png) + +### 四、启动开发板进入终端 + +1. 打开MobaXterm, + + 1. 点击:`Session`、`Serial`按钮 + + 2. 设置Seral port为 第一-4 查看到的COM号 + + 3. 设置Speed为 `115200` + + 4. 点击`OK` + + + ![](figures/Mobax_Serial_选择.png) + + +3. 将开发板的拨码开关拨至“010”emmc启动模式,并按一下开发板的“RESET”按钮复位开发板。 + + ![](figures/启动开发板.png) + +4. MobaXterm上打印出运行日志,等待启动日志运行结束,按键盘“回车”按键进入开发板shell终端,输入例如"ls"命令,可与开发板交互。 + + ![](figures/启动日志.png) + +5. 开发板屏幕出现桌面及预安装的"setting"应用,点击"setting"应用可查看系统信息。 + + ![](figures/启动桌面.png) \ No newline at end of file diff --git a/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发环境搭建.md b/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发环境搭建.md new file mode 100644 index 0000000..1a08895 --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/BearPi-HM_Micro开发环境搭建.md @@ -0,0 +1,342 @@ +# BearPi-HM_Micro开发环境搭建 + +## 环境要求 + +### 硬件要求 + +- Linux服务器 +- Windows工作台(主机电脑) +- BearPi-HM_Micro开发板 +- USB Type-C线(Windows工作台通过USB与BearPi-HM_Micr开发板连接) + + +### 软件系统要求 + +**表 1** BearPi-HM_Micro开发板开发平台要求 + + + + + + + + + + + + + + + + + + +

硬件

+

软件

+

描述

+

备注

+

Linux编译服务器

+

操作系统

+

Ubuntu16.04及以上64位系统版本,Shell使用bash

+

开发人员可以在Windows工作台中进行程序开发,或者远程登录到Linux服务器进行程序开发。

+

Windows工作台

+

操作系统

+

Windows XP/Windows7/Windows10

+
+ +### Linux构建工具要求 + +Linux服务器通用环境配置需要的工具及其获取途径如下表所示。 + +**表 2** Linux服务器开发工具及获取途径 + + + + + + + + + + + + + + + + + + + +

开发工具

+

用途

+

获取途径

+

Python3.7+

+

编译构建工具

+

https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz

+

bash

+

命令处理器

+

通过互联网获取

+
+ +### Windows开发工具要求 + +**表 3** Windows工作台开发工具及获取途径 + + + + + + + + + + + + + + + + + + + + + + + + + +

开发工具

+

用途

+

获取途径

+

Visual Studio Code

+

代码编辑工具。

+

https://code.visualstudio.com/

+

MobaXterm、PuTTY或其他超级终端(选其一)

+

远程连接Linux编译服务器,连接模组串口工具。

+

通过互联网获取(如:https://mobaxterm.mobatek.net/

+

CH341SER.EXE

+

USB转串口驱动。

+

http://www.wch.cn/search?q=ch340g&t=downloads

+

STM32CubeProgrammer

+

代码烧录工具。

+

https://pan.baidu.com/s/1XfFFFs7UdE9TPdN0FdA6ag密码:1234

+
+ +## 安装编译基础环境 + +### 一、连接Linux服务器 + +使用Windows远程登录Linux,以MobaXterm工具为例: + +1. 打开[MobaXterm工具](https://mobaxterm.mobatek.net/), 输入Linux服务器IP地址,后点击“OK”,打开连接。 + + **图 2** MobaXterm界面示例图 + + ![](figures/MobaXterm登录服务器.png "MobaXterm登录服务器") + +2. 弹框告警选择“是\(Y\)”。 +3. 弹出界面中输入账号并按回车键,继续输入密码并回车键。 + + **图 3** 登录界面图 + ![](figures/Linux服务器登录.png "Linux服务器登录") + +4. 登录成功。 + + **图 4** 登录成功界面 + ![](figures/Linux服务器登录成功.png "Linux服务器登录成功") + + +### 二、将Linux shell改为bash + +查看shell是否为bash,在终端运行如下命令 + +``` +ls -l /bin/sh +``` + +如果为显示为/bin/sh -\> bash则为正常,否则请按以下方式修改: + +**方法一**:在终端运行如下命令,然后选择 no。 + +``` +sudo dpkg-reconfigure dash +``` + +**方法二**:先删除sh,再创建软链接。 + +``` +rm -rf /bin/sh +sudo ln -s /bin/bash /bin/sh +``` + +### 三、修改镜像源(可选) + +1. 打开/etc/apt路径下的sources.list文件 + + ![](figures/打开源文件.png) + +2. 将原本的镜像源改为中科大镜像源,然后关闭文件并保存 + + ![](figures/设置为中科大源.png) + + ``` + deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse + deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse + deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse + deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse + deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse + deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse + deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse + deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse + deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse + deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse + ``` + +3. 更新镜像源 + + ``` + sudo apt-get update + ``` +### 四、安装必要的库和工具 +- 使用如下apt-get命令安装编译所需的必要的库和工具: + + ``` + sudo apt-get install build-essential gcc g++ make zlib* libffi-dev e2fsprogs pkg-config flex bison perl bc openssl libssl-dev libelf-dev libc6-dev-amd64 binutils binutils-dev libdwarf-dev u-boot-tools mtd-utils gcc-arm-linux-gnueabi cpio device-tree-compiler net-tools openssh-server git vim openjdk-11-jre-headless + ``` +### 五、安装Python3 + +1. 打开Linux编译服务器终端。 +2. 输入如下命令,查看python版本号,若为python3.7+可跳过安装Python3步骤: + + ``` + python3 --version + ``` + + 如果低于python3.7版本,不建议直接升级,请按照如下步骤重新安装。以python3.8为例,按照以下步骤安装python。 + + 1. 运行如下命令,查看Ubuntu版本: + + ``` + cat /etc/issue + ``` + + 2. 根据Ubuntu不同版本,安装python。 + - 如果Ubuntu 版本为18+,运行如下命令。 + + ``` + sudo apt-get install python3.8 + ``` + + - 如果Ubuntu版本为16。 + + a. 安装依赖包 + + ``` + sudo apt update && sudo apt install software-properties-common + ``` + + b. 添加deadsnakes PPA 源,然后按回车键确认安装。 + + ``` + sudo add-apt-repository ppa:deadsnakes/ppa + ``` + + c. 安装python3.8 + + ``` + sudo apt upgrade && sudo apt install python3.8 + ``` + + +3. 设置python和python3软链接为python3.8。 + + ``` + sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 + sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 + ``` + +4. 安装并升级Python包管理工具(pip3),任选如下一种方式。 + - **命令行方式:** + + ``` + sudo apt-get install python3-setuptools python3-pip -y + sudo pip3 install --upgrade pip + ``` + + - **安装包方式:** + + ``` + curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py + python get-pip.py + ``` + +### 六、安装hb + +1. 运行如下命令安装hb + + ``` + python3 -m pip install --user ohos-build + ``` + +2. 设置环境变量 + + ``` + vim ~/.bashrc + ``` + + 将以下命令拷贝到.bashrc文件的最后一行,保存并退出。 + + ``` + export PATH=~/.local/bin:$PATH + ``` + + 执行如下命令更新环境变量。 + + ``` + source ~/.bashrc + ``` +3. 执行"hb -h",有打印以下信息即表示安装成功: + + ``` + usage: hb + + OHOS build system + + positional arguments: + {build,set,env,clean} + build Build source code + set OHOS build settings + env Show OHOS build env + clean Clean output + + optional arguments: + -h, --help show this help message and exit + ``` + +### 七、把ubuntu文件远程映射到Windows上 + +1. 安装RaiDrive软件 +- 下载地址:https://forspeed.rbread05.cn/down/newdown/5/28/RaiDrive.rar + + 默认安装即可。 + +2. 打开软件切换为中文语言 + + ![](figures/RaiDrive_chinese.png) + +3. 添加链接信息 + + * 取消勾选只读 + * SFTP://______ (这个输入 三.9 获取到的地址) + * 账户:账号和密码皆为 bearpi + * 其他默认 + * 点击`确定` + + ![](figures/RaiDrive_mesg.png) + +4. 查看本地映射的ubuntu文件路径 + + ![](figures/RaiDrive本地映射.png) + diff --git a/bearpi_hm_micro/docs/device-dev/figures/Linux服务器登录.png b/bearpi_hm_micro/docs/device-dev/figures/Linux服务器登录.png new file mode 100644 index 0000000..430b7f8 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Linux服务器登录.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Linux服务器登录成功.png b/bearpi_hm_micro/docs/device-dev/figures/Linux服务器登录成功.png new file mode 100644 index 0000000..f2b8eb7 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Linux服务器登录成功.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/MobaXterm登录服务器 copy.png b/bearpi_hm_micro/docs/device-dev/figures/MobaXterm登录服务器 copy.png new file mode 100644 index 0000000..8e29264 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/MobaXterm登录服务器 copy.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/MobaXterm登录服务器.png b/bearpi_hm_micro/docs/device-dev/figures/MobaXterm登录服务器.png new file mode 100644 index 0000000..8e29264 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/MobaXterm登录服务器.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/MobaXterm首页.png b/bearpi_hm_micro/docs/device-dev/figures/MobaXterm首页.png new file mode 100644 index 0000000..38c0309 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/MobaXterm首页.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/MobaX地址输入.png b/bearpi_hm_micro/docs/device-dev/figures/MobaX地址输入.png new file mode 100644 index 0000000..f05cd62 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/MobaX地址输入.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Mobax_Serial_选择.png b/bearpi_hm_micro/docs/device-dev/figures/Mobax_Serial_选择.png new file mode 100644 index 0000000..4bf3aa7 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Mobax_Serial_选择.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Mobax_保存密码.png b/bearpi_hm_micro/docs/device-dev/figures/Mobax_保存密码.png new file mode 100644 index 0000000..de083ce Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Mobax_保存密码.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Mobax_密码输入.png b/bearpi_hm_micro/docs/device-dev/figures/Mobax_密码输入.png new file mode 100644 index 0000000..7c23d78 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Mobax_密码输入.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Mobax_打开服务工具.png b/bearpi_hm_micro/docs/device-dev/figures/Mobax_打开服务工具.png new file mode 100644 index 0000000..fe192c0 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Mobax_打开服务工具.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Mobax_账号输入.png b/bearpi_hm_micro/docs/device-dev/figures/Mobax_账号输入.png new file mode 100644 index 0000000..e175d36 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Mobax_账号输入.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/OpenHarmony系统烧录文件.png b/bearpi_hm_micro/docs/device-dev/figures/OpenHarmony系统烧录文件.png new file mode 100644 index 0000000..6ba6ca8 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/OpenHarmony系统烧录文件.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/RaiDrive_chinese.png b/bearpi_hm_micro/docs/device-dev/figures/RaiDrive_chinese.png new file mode 100644 index 0000000..a5cafe6 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/RaiDrive_chinese.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/RaiDrive_mesg.png b/bearpi_hm_micro/docs/device-dev/figures/RaiDrive_mesg.png new file mode 100644 index 0000000..cb71a3b Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/RaiDrive_mesg.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/RaiDrive本地映射.png b/bearpi_hm_micro/docs/device-dev/figures/RaiDrive本地映射.png new file mode 100644 index 0000000..0db1d7e Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/RaiDrive本地映射.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Stm32CubeProgrammer目录.gif b/bearpi_hm_micro/docs/device-dev/figures/Stm32CubeProgrammer目录.gif new file mode 100644 index 0000000..dc40115 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Stm32CubeProgrammer目录.gif differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/Ubuntu18.4镜像目录.png b/bearpi_hm_micro/docs/device-dev/figures/Ubuntu18.4镜像目录.png new file mode 100644 index 0000000..aaadb46 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/Ubuntu18.4镜像目录.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/VMWare设置.png b/bearpi_hm_micro/docs/device-dev/figures/VMWare设置.png new file mode 100644 index 0000000..9e904f4 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/VMWare设置.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/VMware_net_config.png b/bearpi_hm_micro/docs/device-dev/figures/VMware_net_config.png new file mode 100644 index 0000000..2ddfc55 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/VMware_net_config.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/mkimage路径.png b/bearpi_hm_micro/docs/device-dev/figures/mkimage路径.png new file mode 100644 index 0000000..54e81d6 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/mkimage路径.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/reset.png b/bearpi_hm_micro/docs/device-dev/figures/reset.png new file mode 100644 index 0000000..d83428c Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/reset.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/ubuntu打开终端.png b/bearpi_hm_micro/docs/device-dev/figures/ubuntu打开终端.png new file mode 100644 index 0000000..61189cc Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/ubuntu打开终端.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/ubuntu登录.png b/bearpi_hm_micro/docs/device-dev/figures/ubuntu登录.png new file mode 100644 index 0000000..061b95c Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/ubuntu登录.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/vmware_workstation.png b/bearpi_hm_micro/docs/device-dev/figures/vmware_workstation.png new file mode 100644 index 0000000..29b62ac Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/vmware_workstation.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/zh-cn_image_0000001054564784.png b/bearpi_hm_micro/docs/device-dev/figures/zh-cn_image_0000001054564784.png new file mode 100644 index 0000000..f2a5855 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/zh-cn_image_0000001054564784.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/关闭虚拟机捕获USB.png b/bearpi_hm_micro/docs/device-dev/figures/关闭虚拟机捕获USB.png new file mode 100644 index 0000000..d589a1a Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/关闭虚拟机捕获USB.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/发送消息到HDF.png b/bearpi_hm_micro/docs/device-dev/figures/发送消息到HDF.png new file mode 100644 index 0000000..c835b12 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/发送消息到HDF.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/启动开发板.png b/bearpi_hm_micro/docs/device-dev/figures/启动开发板.png new file mode 100644 index 0000000..0276bfb Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/启动开发板.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/启动日志.png b/bearpi_hm_micro/docs/device-dev/figures/启动日志.png new file mode 100644 index 0000000..5589355 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/启动日志.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/启动桌面.png b/bearpi_hm_micro/docs/device-dev/figures/启动桌面.png new file mode 100644 index 0000000..149d833 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/启动桌面.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/导入镜像.png b/bearpi_hm_micro/docs/device-dev/figures/导入镜像.png new file mode 100644 index 0000000..38dafdb Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/导入镜像.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/开启ubuntu虚拟机.png b/bearpi_hm_micro/docs/device-dev/figures/开启ubuntu虚拟机.png new file mode 100644 index 0000000..7ebeae6 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/开启ubuntu虚拟机.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/打开源文件.png b/bearpi_hm_micro/docs/device-dev/figures/打开源文件.png new file mode 100644 index 0000000..d2dd49f Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/打开源文件.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/打开镜像.png b/bearpi_hm_micro/docs/device-dev/figures/打开镜像.png new file mode 100644 index 0000000..0d77b8a Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/打开镜像.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/板子功能图.png b/bearpi_hm_micro/docs/device-dev/figures/板子功能图.png new file mode 100644 index 0000000..1ce988e Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/板子功能图.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/查看编译输出文件.png b/bearpi_hm_micro/docs/device-dev/figures/查看编译输出文件.png new file mode 100644 index 0000000..2f63cc2 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/查看编译输出文件.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/点亮LED导图.png b/bearpi_hm_micro/docs/device-dev/figures/点亮LED导图.png new file mode 100644 index 0000000..8c08258 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/点亮LED导图.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/烧写固件.gif b/bearpi_hm_micro/docs/device-dev/figures/烧写固件.gif new file mode 100644 index 0000000..fc3204d Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/烧写固件.gif differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/烧录镜像.png b/bearpi_hm_micro/docs/device-dev/figures/烧录镜像.png new file mode 100644 index 0000000..da5300a Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/烧录镜像.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/编译总结.png b/bearpi_hm_micro/docs/device-dev/figures/编译总结.png new file mode 100644 index 0000000..23d4409 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/编译总结.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/获取到开发板串口号.png b/bearpi_hm_micro/docs/device-dev/figures/获取到开发板串口号.png new file mode 100644 index 0000000..a44123e Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/获取到开发板串口号.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/获取虚拟机ip地址.png b/bearpi_hm_micro/docs/device-dev/figures/获取虚拟机ip地址.png new file mode 100644 index 0000000..43ada34 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/获取虚拟机ip地址.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/虚拟机USB选择.png b/bearpi_hm_micro/docs/device-dev/figures/虚拟机USB选择.png new file mode 100644 index 0000000..dfdedf3 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/虚拟机USB选择.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/虚拟机登录页面.png b/bearpi_hm_micro/docs/device-dev/figures/虚拟机登录页面.png new file mode 100644 index 0000000..b525321 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/虚拟机登录页面.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/设置为中科大源.png b/bearpi_hm_micro/docs/device-dev/figures/设置为中科大源.png new file mode 100644 index 0000000..4530db1 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/设置为中科大源.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/运行hello_world.png b/bearpi_hm_micro/docs/device-dev/figures/运行hello_world.png new file mode 100644 index 0000000..6171d96 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/运行hello_world.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/进入烧录模式.png b/bearpi_hm_micro/docs/device-dev/figures/进入烧录模式.png new file mode 100644 index 0000000..a834149 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/进入烧录模式.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/连接USB.png b/bearpi_hm_micro/docs/device-dev/figures/连接USB.png new file mode 100644 index 0000000..1187b3a Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/连接USB.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/选择板子.png b/bearpi_hm_micro/docs/device-dev/figures/选择板子.png new file mode 100644 index 0000000..97a6d88 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/选择板子.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/选择烧录配置文件.png b/bearpi_hm_micro/docs/device-dev/figures/选择烧录配置文件.png new file mode 100644 index 0000000..c56af3a Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/选择烧录配置文件.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/选择镜像文件路径.png b/bearpi_hm_micro/docs/device-dev/figures/选择镜像文件路径.png new file mode 100644 index 0000000..bbf50f9 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/选择镜像文件路径.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/防火墙和网络保护界面图.png b/bearpi_hm_micro/docs/device-dev/figures/防火墙和网络保护界面图.png new file mode 100644 index 0000000..775ce6f Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/防火墙和网络保护界面图.png differ diff --git a/bearpi_hm_micro/docs/device-dev/figures/驱动配置.png b/bearpi_hm_micro/docs/device-dev/figures/驱动配置.png new file mode 100644 index 0000000..f073236 Binary files /dev/null and b/bearpi_hm_micro/docs/device-dev/figures/驱动配置.png differ diff --git a/bearpi_hm_micro/docs/device-dev/如何在开发板上安装HAP应用.md b/bearpi_hm_micro/docs/device-dev/如何在开发板上安装HAP应用.md new file mode 100644 index 0000000..4257f45 --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/如何在开发板上安装HAP应用.md @@ -0,0 +1,34 @@ +# 如何在开发板上安装HAP应用 + +以下以安装一个控制灯开关的应用为例讲解如何在开发板上安装HAP应用. + +## 一、准备工作 + +- 准备一张SD卡(需要格式化为FAT32),以及一个读卡器 + +## 二、安装HAP应用 + +1. 将device/board/bearpi/bearpi_hm_micro/tools/hap_tools/hap_example路径下的bm、LED_1.0.0.hap拷贝到SD卡中 + +2. 将SD卡插入到开发板中,并按开发板的RESET按键重启开发板 + +3. 输入以下命令,挂载SD卡 + ``` + mount /dev/mmcblk0p0 /sdcard vfat + ``` +4. 输入以下命令,进入SD卡目录 + ``` + cd /sdcard + ``` +5. 输入以下命令,打开调试模式 + ``` + ./bm set -s disable + ./bm set -d enable + ``` + +6. 安装应用 + ``` + ./bm install -p LED_1.0.0.hap + ``` + +注: LED_1.0.0.hap为安装包名称,安装其他应用需要修改为对应的安装包名称。 diff --git a/bearpi_hm_micro/docs/device-dev/如何运行一个可执行程序.md b/bearpi_hm_micro/docs/device-dev/如何运行一个可执行程序.md new file mode 100644 index 0000000..9e88d53 --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/如何运行一个可执行程序.md @@ -0,0 +1,6 @@ +# 如何运行一个可执行程序 + +以在[《编写一个hello_world应用》](./编写一个hello_world程序.md)一文中编译出来的hello_world可执行程序为例,在命令行输入指令“./bin/hello_world”执行写入的demo程序,执行成功后会显示如下结果: + + +![](figures/运行hello_world.png) \ No newline at end of file diff --git a/bearpi_hm_micro/docs/device-dev/编写一个hello_world程序.md b/bearpi_hm_micro/docs/device-dev/编写一个hello_world程序.md new file mode 100644 index 0000000..cfa63d5 --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/编写一个hello_world程序.md @@ -0,0 +1,145 @@ +# 编写一个hello_world应用 + +本示例将演示如何在开发板上运行第一个应用程序,输出“Hello World”。 + +## 编写源码 + +1. 确定目录结构。 + + 开发者编写业务时,务必先在./applications/sample路径下新建一个目录(或一套目录结构),用于存放业务源码文件。 + + 例如:在app下新增业务my\_first\_app,其中hello\_world.c为业务代码,BUILD.gn为编译脚本,具体规划目录结构如下: + + ``` + . + └── applications + └── sample + │── my_first_app + │── hello_world.c + └── BUILD.gn + + ``` + +2. 编写业务代码。 + + 在hello\_world.c中编写业务代码。 + + ``` + #include + + int main(int argc, char **argv) + { + printf("\n************************************************\n"); + printf("\n\t\tHello BearPi!\n"); + printf("\n************************************************\n\n"); + + return 0; + } + ``` + +3. 编写将构建业务代码的BUILD.gn文件。 + + 如步骤1所述,BUILD.gn文件由三部分内容(目标、源文件、头文件路径)构成,需由开发者完成填写。以my\_first\_app为例,需要创建./applications/sample/my\_first\_app/BUILD.gn,并完如下配置。 + + ``` + import("//build/lite/config/component/lite_component.gni") + + executable("hello_world_lib") { + output_name = "hello_world" + sources = [ "hello_world.c" ] + include_dirs = [] + defines = [] + cflags_c = [] + ldflags = [] + } + + lite_component("my_app") { + features = [ + ":hello_world_lib", + ] + } + ``` + + - 首先导入 gni 组件,将源码hello_world.c编译成hello_world_lib库文件 + - 然后将hello_world_lib打包成 lite_component,命名为my_app组件。 + + - 输出的可执行文件名称由output_name定义为hello_world + +4. 添加新组件 + + 修改文件build/lite/components/applications.json,添加组件my_sample的配置,如下所示为applications.json文件片段,"##start##"和"##end##"之间为新增配置("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行): + + ``` + { + "components": [ + ##start## + { + "component": "my_sample", + "description": "my samples", + "optional": "true", + "dirs": [ + "applications/sample/my_first_app" + ], + "targets": [ + "//applications/sample/my_first_app:my_app" + ], + "rom": "", + "ram": "", + "output": [], + "adapted_kernel": [ "liteos_a" ], + "features": [], + "deps": { + "components": [], + "third_party": [ ] + } + }, + ##end## + { + "component": "camera_sample_communication", + "description": "Communication related samples.", + "optional": "true", + "dirs": [ + "applications/sample/camera/communication" + ], + "targets": [ + "//applications/sample/camera/communication:sample" + ], + "rom": "", + "ram": "", + "output": [], + "adapted_kernel": [ "liteos_a" ], + "features": [], + "deps": { + "components": [], + "third_party": [ ] + } + }, + ``` +5. 修改单板配置文件 + + 修改文件vendor/bearpi/bearpi_hm_micro/config.json,新增my_sample组件的条目,如下所示代码片段为applications子系统配置,"##start##"和"##end##"之间为新增条目("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行): + + ``` + { + "subsystem": "applications", + "components": [ + ##start## + { "component": "my_sample", "features":[] }, + ##end## + { "component": "bearpi_hm_micro_sample", "features":[] } + ] + }, + ``` + + +## 运行结果 + +示例代码[编译、烧录](BearPi-HM_Micro开发板编译调试.md)后,在命令行输入指令“./bin/hello_world”执行写入的demo程序,会显示如下结果: + + +![](figures/运行hello_world.png) + + + + +在此希望开发者能仔细琢磨并掌握整个流程,有利于学习OpenHarmony的编译框架,以及为后续自主开发应用打下基础 diff --git a/bearpi_hm_micro/docs/device-dev/编写一个点亮LED灯程序.md b/bearpi_hm_micro/docs/device-dev/编写一个点亮LED灯程序.md new file mode 100644 index 0000000..504d678 --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/编写一个点亮LED灯程序.md @@ -0,0 +1,647 @@ +# 编写点亮LED灯程序 + +在本示例将演示如何在开发板上运行一个控制LED灯的程序,达到能关闭灯、开启灯以及翻转灯的状态。希望通过本教程的学习,开发者能掌握如何开发一个设备驱动,以及如何在应用层调用驱动。 + +**注:** 在学习本教程之前,请确保已经熟练掌握[如何编写一个hello_world应用](./编写一个hello_world程序.md),以下教程篇幅较长,请耐心仔细阅读。 + +## 任务介绍 +点亮LED主要包含以下任务: + +1. LED驱动代码开发 + + 驱动开发主要包含以下工作 + + - 编写驱动代码 + - 编写驱动编译文件 + - 编写驱动配置文件 + +2. 点亮LED业务代码开发 + + 业务代码开发主要包含以下工作 + - 编写业务代码 + - 编写业务代码编译文件 + + ![](figures/点亮LED导图.png) + + + +## 一、LED灯驱动开发 + +1. HDF驱动简介 + + HDF框架以组件化的驱动模型作为核心设计思路,为开发者提供更精细化的驱动管理,让驱动开发和部署更加规范。HDF框架将一类设备驱动放在同一个host里面,开发者也可以将驱动功能分层独立开发和部署,支持一个驱动多个node,HDF驱动模型如下图所示: + + ![](figures/zh-cn_image_0000001054564784.png) + +2. 确定目录结构。 + + 在device\soc\st\common\platform路径下新建led文件,并创建驱动文件led.c和编译构建文件BUILD.gn、Makefile + 。 + ``` + . + └── device + └── soc + └── st + └── common + └── platform + └── led + │── led.c + └── BUILD.gn + └── Makefile + + ``` + + 在device\soc\st\stm32mp1xx\sdk_liteos\hdf_config路径下新建led文件夹,并创建驱动配置文件led_config.hcs。 + ``` + . + └── device + └── soc + └── st + └── stm32mp1xx + └── sdk_liteos + └── hdf_config + └── led + │── led_config.hcs + ``` + +3. LED驱动实现 + + 驱动实现包含驱动业务代码和驱动入口注册,在led.c文件中添加以下代码: + + ``` + #include "hdf_device_desc.h" + #include "hdf_log.h" + #include "device_resource_if.h" + #include "osal_io.h" + #include "osal.h" + #include "osal_mem.h" + #include "gpio_if.h" + + #define HDF_LOG_TAG led_driver // 打印日志所包含的标签,如果不定义则用默认定义的HDF_TAG标签 + #define LED_WRITE_READ 1 // 读写操作码1 + + enum LedOps { + LED_OFF, + LED_ON, + LED_TOGGLE, + }; + + struct Stm32Mp1ILed { + uint32_t gpioNum; + }; + static struct Stm32Mp1ILed g_Stm32Mp1ILed; + uint8_t status = 0; + // Dispatch是用来处理用户态发下来的消息 + int32_t LedDriverDispatch(struct HdfDeviceIoClient *client, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply) + { + uint8_t contrl; + HDF_LOGE("Led driver dispatch"); + if (client == NULL || client->device == NULL) + { + HDF_LOGE("Led driver device is NULL"); + return HDF_ERR_INVALID_OBJECT; + } + + switch (cmdCode) + { + /* 接收到用户态发来的LED_WRITE_READ命令 */ + case LED_WRITE_READ: + /* 读取data里的数据,赋值给contrl */ + HdfSbufReadUint8(data,&contrl); + switch (contrl) + { + /* 开灯 */ + case LED_ON: + GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_LOW); + status = 1; + break; + /* 关灯 */ + case LED_OFF: + GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_HIGH); + status = 0; + break; + /* 状态翻转 */ + case LED_TOGGLE: + if(status == 0) + { + GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_LOW); + status = 1; + } + else + { + GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_HIGH); + status = 0; + } + break; + default: + break; + } + /* 把LED的状态值写入reply, 可被带至用户程序 */ + if (!HdfSbufWriteInt32(reply, status)) + { + HDF_LOGE("replay is fail"); + return HDF_FAILURE; + } + break; + default: + break; + } + return HDF_SUCCESS; + } + + // 读取驱动私有配置 + static int32_t Stm32LedReadDrs(struct Stm32Mp1ILed *led, const struct DeviceResourceNode *node) + { + int32_t ret; + struct DeviceResourceIface *drsOps = NULL; + + drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (drsOps == NULL || drsOps->GetUint32 == NULL) { + HDF_LOGE("%s: invalid drs ops!", __func__); + return HDF_FAILURE; + } + /* 读取led.hcs里面led_gpio_num的值 */ + ret = drsOps->GetUint32(node, "led_gpio_num", &led->gpioNum, 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read led gpio num fail!", __func__); + return ret; + } + return HDF_SUCCESS; + } + + //驱动对外提供的服务能力,将相关的服务接口绑定到HDF框架 + int32_t HdfLedDriverBind(struct HdfDeviceObject *deviceObject) + { + if (deviceObject == NULL) + { + HDF_LOGE("Led driver bind failed!"); + return HDF_ERR_INVALID_OBJECT; + } + static struct IDeviceIoService ledDriver = { + .Dispatch = LedDriverDispatch, + }; + deviceObject->service = (struct IDeviceIoService *)(&ledDriver); + HDF_LOGD("Led driver bind success"); + return HDF_SUCCESS; + } + + // 驱动自身业务初始的接口 + int32_t HdfLedDriverInit(struct HdfDeviceObject *device) + { + struct Stm32Mp1ILed *led = &g_Stm32Mp1ILed; + int32_t ret; + + if (device == NULL || device->property == NULL) { + HDF_LOGE("%s: device or property NULL!", __func__); + return HDF_ERR_INVALID_OBJECT; + } + /* 读取hcs私有属性值 */ + ret = Stm32LedReadDrs(led, device->property); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: get led device resource fail:%d", __func__, ret); + return ret; + } + /* 将GPIO管脚配置为输出 */ + ret = GpioSetDir(led->gpioNum, GPIO_DIR_OUT); + if (ret != 0) + { + HDF_LOGE("GpioSerDir: failed, ret %d\n", ret); + return ret; + } + HDF_LOGD("Led driver Init success"); + return HDF_SUCCESS; + } + + // 驱动资源释放的接口 + void HdfLedDriverRelease(struct HdfDeviceObject *deviceObject) + { + if (deviceObject == NULL) + { + HDF_LOGE("Led driver release failed!"); + return; + } + HDF_LOGD("Led driver release success"); + return; + } + + // 定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量 + struct HdfDriverEntry g_ledDriverEntry = { + .moduleVersion = 1, + .moduleName = "HDF_LED", + .Bind = HdfLedDriverBind, + .Init = HdfLedDriverInit, + .Release = HdfLedDriverRelease, + }; + + // 调用HDF_INIT将驱动入口注册到HDF框架中 + HDF_INIT(g_ledDriverEntry); + + + ``` +4. 驱动编译 + + 在led/BUILD.gn文件中添加以下代码,将led.c编译成hdf_led。 + ``` + import("//drivers/adapter/khdf/liteos/hdf.gni") + + hdf_driver("hdf_led") { + sources = [ + "led.c", + ] + } + + ``` + + 在led/Makefile文件中添加以下代码,将led.c编译成hdf_led。 + ``` + include $(LITEOSTOPDIR)/config.mk + include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk + + MODULE_NAME := hdf_led + + LOCAL_CFLAGS += $(HDF_INCLUDE) + + LOCAL_SRCS += led.c + + LOCAL_CFLAGS += -fstack-protector-strong -Wextra -Wall -Werror -fsigned-char -fno-strict-aliasing -fno-common + + include $(HDF_DRIVER) + ``` + + 在/device/soc/st/common/platform/BUILD.gn文件中添加以下代码,将hdf_led编译进内核。"##start##"和"##end##"之间为新增配置("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行) + ``` + import("//drivers/adapter/khdf/liteos/hdf.gni") + + group("drivers") { + deps = [ + "uart", + "iwdg", + "i2c", + "gpio", + ##start## + "led", + ##end## + "stm32mp1xx_hal", + ] + } + ``` + + 在/device/soc/st/common/platform/lite.mk文件中添加以下代码,将hdf_led编译进内核。 + ``` + LITEOS_BASELIB += -lhdf_led + LIB_SUBDIRS += $(ST_DRIVERS_ROOT)/led + ``` +5. 驱动配置 + + HDF使用HCS作为配置描述源码,HCS详细介绍参考配置管理介绍。 + + 驱动配置包含两部分,HDF框架定义的驱动设备描述和驱动的私有配置信息,具体写法如下: + + - 驱动设备描述 + + HDF框架加载驱动所需要的信息来源于HDF框架定义的驱动设备描述,因此基于HDF框架开发的驱动必须要在HDF框架定义的device_info.hcs配置文件中添加对应的设备描述,所以我们需要在vendor\bearpi\bearpi_hm_micro\hdf_config\device_info\device_info.hcs中添加LED设备描述。 "##start##"和"##end##"之间为新增配置("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行) + ``` + platform :: host { + hostName = "platform_host"; + priority = 50; + ##start## + device_led :: device { // led设备节点 + device0 :: deviceNode { // led驱动的DeviceNode节点 + policy = 2; // policy字段是驱动服务发布的策略,在驱动服务管理章节有详细介绍 + priority = 10; // 驱动启动优先级(0-200),值越大优先级越低,建议默认配100,优先级相同则不保证device的加载顺序 + preload = 1; // 驱动按需加载字段 + permission = 0777; // 驱动创建设备节点权限 + moduleName = "HDF_LED"; // 驱动名称,该字段的值必须和驱动入口结构的moduleName值一致 + serviceName = "hdf_led"; // 驱动对外发布服务的名称,必须唯一 + deviceMatchAttr = "st_stm32mp157_led"; // 驱动私有数据匹配的关键字,必须和驱动私有数据配置表中的match_attr值相等 + } + } + ##end## + device_gpio :: device { + device0 :: deviceNode { + policy = 0; + priority = 10; + permission = 0644; + moduleName = "HDF_PLATFORM_GPIO"; + serviceName = "HDF_PLATFORM_GPIO"; + deviceMatchAttr = "st_stm32mp157_gpio"; + } + } + ``` + - 驱动私有配置信息 + + 如果驱动有私有配置,则可以添加一个驱动的配置文件,用来填写一些驱动的默认配置信息,HDF框架在加载驱动的时候,会将对应的配置信息获取并保存在HdfDeviceObject 中的property里面,通过Bind和Init传递给驱动,所以我们需要在device\soc\st\stm32mp1xx\sdk_liteos\hdf_config\led\led_config.hcs中添加LED私有配置描述。 + + ``` + root { + LedDriverConfig { + led_gpio_num = 13; + match_attr = "st_stm32mp157_led"; //该字段的值必须和device_info.hcs中的deviceMatchAttr值一致 + } + } + ``` + + + + 配置信息定义之后,需要将该配置文件添加到板级配置入口文件vendor\bearpi\bearpi_hm_micro\hdf_config\hdf.hcs,示例如下: + + ``` + #include "../../../../device/soc/st/stm32mp1xx/sdk_liteos/hdf_config/led/led_config.hcs" + ``` + - 小结 + 1. device_info.hcs文件中的moduleName必须要和驱动文件中的moduleName字段匹配,这样驱动才会加载起来 + 2. device_info.hcs文件中的deviceMatchAttr的字段必须和私有配置文件中led_config.hcs的match_attr的字段匹配,这样私有配置才能生效 + + ![](figures/驱动配置.png) + +## 二、点亮LED灯业务代码 + + +1. 确定目录结构。 + + 开发者编写业务时,务必先在./applications/sample路径下新建一个目录(或一套目录结构),用于存放业务源码文件。 + + 例如:在app下新增业务my_led_app,其中my_led_app.c为业务代码,BUILD.gn为编译脚本,具体规划目录结构如下: + + ``` + . + └── applications + └── sample + │── my_led_app + │── my_led_app.c + └── BUILD.gn + + ``` + +2. 编写业务代码。 + + 在my_led_app.c中添加以下业务代码: + + ``` + #include + #include + #include + #include + #include + #include "hdf_sbuf.h" + #include "hdf_io_service_if.h" + + #define LED_WRITE_READ 1 + #define LED_SERVICE "hdf_led" + + static int SendEvent(struct HdfIoService *serv, uint8_t eventData) + { + int ret = 0; + struct HdfSBuf *data = HdfSBufObtainDefaultSize(); + if (data == NULL) + { + printf("fail to obtain sbuf data!\r\n"); + return 1; + } + + struct HdfSBuf *reply = HdfSBufObtainDefaultSize(); + if (reply == NULL) + { + printf("fail to obtain sbuf reply!\r\n"); + ret = HDF_DEV_ERR_NO_MEMORY; + goto out; + } + /* 写入数据 */ + if (!HdfSbufWriteUint8(data, eventData)) + { + printf("fail to write sbuf!\r\n"); + ret = HDF_FAILURE; + goto out; + } + /* 通过Dispatch发送到驱动 */ + ret = serv->dispatcher->Dispatch(&serv->object, LED_WRITE_READ, data, reply); + if (ret != HDF_SUCCESS) + { + printf("fail to send service call!\r\n"); + goto out; + } + + int replyData = 0; + /* 读取驱动的回复数据 */ + if (!HdfSbufReadInt32(reply, &replyData)) + { + printf("fail to get service call reply!\r\n"); + ret = HDF_ERR_INVALID_OBJECT; + goto out; + } + printf("\r\nGet reply is: %d\r\n", replyData); + out: + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return ret; + } + + int main(int argc, char **argv) + { + int i; + + /* 获取服务 */ + struct HdfIoService *serv = HdfIoServiceBind(LED_SERVICE); + if (serv == NULL) + { + printf("fail to get service %s!\r\n", LED_SERVICE); + return HDF_FAILURE; + } + + for (i=0; i < argc; i++) + { + printf("\r\nArgument %d is %s.\r\n", i, argv[i]); + } + + SendEvent(serv, atoi(argv[1])); + + HdfIoServiceRecycle(serv); + printf("exit"); + + return HDF_SUCCESS; + } + ``` + +3. 编写将构建业务代码的BUILD.gn文件。 + + BUILD.gn文件由三部分内容(目标、源文件、头文件路径)构成,需由开发者完成填写。以my_led_app为例,需要创建./applications/sample/my_led_app/BUILD.gn,并完如下配置。 + + ``` + import("//build/lite/config/component/lite_component.gni") + + HDF_FRAMEWORKS = "//drivers/framework" + + executable("led_lib") { + output_name = "my_led" + sources = [ + "my_led_app.c", + ] + + include_dirs = [ + "$HDF_FRAMEWORKS/ability/sbuf/include", + "$HDF_FRAMEWORKS/core/shared/include", + "$HDF_FRAMEWORKS/core/host/include", + "$HDF_FRAMEWORKS/core/master/include", + "$HDF_FRAMEWORKS/include/core", + "$HDF_FRAMEWORKS/include/utils", + "$HDF_FRAMEWORKS/utils/include", + "$HDF_FRAMEWORKS/include/osal", + "//drivers/adapter/uhdf/posix/include", + "//third_party/bounds_checking_function/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + ] + + deps = [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//drivers/adapter/uhdf/manager:hdf_core", + "//drivers/adapter/uhdf/posix:hdf_posix_osal", + ] + } + + lite_component("my_led_app") { + features = [ + ":led_lib", + ] + } + ``` + + - 首先导入 gni 组件,将源码my_led_app.c编译成led_lib库文件 + - 输出的可执行文件名称由 output_name 定义为my_led + - include_dirs 里面加入my_led_app.c里面需要用到的.h的头文件路径 + - deps 里面加入所依赖的库。 + - 然后将led_lib打包成 lite_component,命名为my_led_app组件。 + + + +4. 添加新组件 + + 修改文件build/lite/components/applications.json,添加组件my_sample的配置,如下所示为applications.json文件片段,"##start##"和"##end##"之间为新增配置("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行),可基于在[hello_world](编写一个hello_world程序.md)基础上在dirs里添加my_led_app路径。在targets里面添加my_led_app目标项。 + + ``` + { + "components": [ + ##start## + { + "component": "my_sample", + "description": "my samples", + "optional": "true", + "dirs": [ + "applications/sample/my_first_app", + "applications/sample/my_led_app" + ], + "targets": [ + "//applications/sample/my_first_app:my_app", + "//applications/sample/my_led_app:my_led_app" + ], + "rom": "", + "ram": "", + "output": [], + "adapted_kernel": [ "liteos_a" ], + "features": [], + "deps": { + "components": [], + "third_party": [ ] + } + }, + ##end## + { + "component": "camera_sample_communication", + "description": "Communication related samples.", + "optional": "true", + "dirs": [ + "applications/sample/camera/communication" + ], + "targets": [ + "//applications/sample/camera/communication:sample" + ], + "rom": "", + "ram": "", + "output": [], + "adapted_kernel": [ "liteos_a" ], + "features": [], + "deps": { + "components": [], + "third_party": [ ] + } + }, + ``` +5. 修改单板配置文件 + + 修改文件vendor/bearpi/bearpi_hm_micro/config.json,新增my_sample组件的条目,如下所示代码片段为applications子系统配置,"##start##"和"##end##"之间为新增条目("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行): + + ``` + { + "subsystem": "applications", + "components": [ + ##start## + { "component": "my_sample", "features":[] }, + ##end## + { "component": "bearpi_hm_micro_sample", "features":[] } + ] + }, + ``` + + +## 三、运行结果 + +示例代码[编译、烧录](BearPi-HM_Micro开发板编译调试.md)后,在命令行输入以下指令可控制开发板的LED灯。 + +关闭LED: +``` +./bin/my_led 0 +``` +开启LED: +``` +./bin/my_led 1 +``` +翻转LED: +``` +./bin/my_led 2 +``` + +从以下日志的Get reply中可以收到驱动上报的当前灯的状态,"0"表示当前灯为关闭状态,"1"表示当前灯为打开状态, + +``` +OHOS # +OHOS # ./bin/my_led 0 +OHOS # +Argument 0 is bin/my_led. + +Argument 1 is 0. + +Get reply is: 0 +exit01-01 00:01:06.784 19 43 E 02500/led_driver: Led driver dispatch + +OHOS # +OHOS # ./bin/my_led 1 +OHOS # +Argument 0 is bin/my_led. + +Argument 1 is 1. + +Get reply is: 1 +exit01-01 00:01:08.833 20 43 E 02500/led_driver: Led driver dispatch + +OHOS # +OHOS # ./bin/my_led 2 +OHOS # +Argument 0 is bin/my_led. + +Argument 1 is 2. + +Get reply is: 0 +exit01-01 00:01:11.391 21 43 E 02500/led_driver: Led driver dispatch + + +``` +## 总结 + +用户程序是无法直接访问驱动的,当只有驱动程序向用户态暴露server后,用户程序才能通过Dispatch的方式发送指令到驱动程序,并可以将用户态的数据携带到驱动程序,也可以从驱动程序读出数据,如下图所示为用户态程序与驱动自己数据交互的过程。 + +![](figures/发送消息到HDF.png) + +1. 发送LED_WRITE_READ命令到驱动,此处开发者可以自定义创建更多的命令。 +2. 携带数据到驱动,解析出开关灯的动作。 +3. 读取IO口电平状态并通过reply携带到用户程序。 + +在此希望开发者能仔细琢磨并掌握整个流程,掌握用户态应用程序与内核态驱动程序之间的数据交互流程,以及驱动的编写方式。 diff --git a/bearpi_hm_micro/docs/device-dev/通过JS应用控制LED灯.md b/bearpi_hm_micro/docs/device-dev/通过JS应用控制LED灯.md new file mode 100644 index 0000000..a9c34dc --- /dev/null +++ b/bearpi_hm_micro/docs/device-dev/通过JS应用控制LED灯.md @@ -0,0 +1,241 @@ +# 编写通过JS应用控制LED灯 +在本示例将演示如何在系统中添加自定义JS API,达到能通过JS应用关闭灯、开启灯以及翻转灯的状态。希望通过本教程的学习,开发者能掌握如何在OpenHarmony中自定义JS API。 + +**注:** 在学习本教程之前,请确保已经完成[编写一个点亮LED灯程序](./编写一个点亮LED灯程序.md)实验,因为本实验将依赖[编写一个点亮LED灯程序](./编写一个点亮LED灯程序.md)实验中开发的驱动,以下教程篇幅较长,请耐心仔细阅读。 + +## 一、JS API接口开发 + +1. 添加控制LED灯的JS API接口 + + 修改`foundation\ace\ace_engine_lite\frameworks\src\core\modules\app_module.h`,加入toggleLed JS API,("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行) + + ``` + public: + ACE_DISALLOW_COPY_AND_MOVE(AppModule); + AppModule() = default; + ~AppModule() = default; + static JSIValue GetInfo(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue Terminate(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + ##start## + static JSIValue ToggleLed(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); + ##end## + #ifdef FEATURE_SCREEN_ON_VISIBLE + static JSIValue ScreenOnVisible(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + #endif + ``` + + ``` + void InitAppModule(JSIValue exports) + { + JSI::SetModuleAPI(exports, "getInfo", AppModule::GetInfo); + JSI::SetModuleAPI(exports, "terminate", AppModule::Terminate); + ##start## + JSI::SetModuleAPI(exports, "ledcontrol", AppModule::ToggleLed); + ##end## + #ifdef FEATURE_SCREEN_ON_VISIBLE + JSI::SetModuleAPI(exports, "screenOnVisible", AppModule::ScreenOnVisible); + #endif + } + ``` + +2. 编写控制LED灯c++ 业务代码 + + 在`foundation\ace\ace_engine_lite\frameworks\src\core\modules\app_module.cpp`中加入控制LED灯c++ 业务代码("##start##"和"##end##"仅用来标识位置,添加完配置后删除这两行)。 + + ``` + #include "app_module.h" + #include "ace_log.h" + #include "js_app_context.h" + #ifdef FEATURE_SCREEN_ON_VISIBLE + #include "js_async_work.h" + #include "product_adapter.h" + #endif + + ##start## + #include "hdf_sbuf.h" + #include "hdf_io_service_if.h" + + #define LED_WRITE_READ 1 + #define LED_SERVICE "hdf_led" + ##end## + + namespace OHOS { + namespace ACELite { + const char * const AppModule::FILE_MANIFEST = "manifest.json"; + const char * const AppModule::KEY_APP_NAME = "appName"; + const char * const AppModule::KEY_VERSION_NAME = "versionName"; + const char * const AppModule::KEY_VERSION_CODE = "versionCode"; + + ``` + + ``` + struct AsyncParams : public MemoryHeap { + ACE_DISALLOW_COPY_AND_MOVE(AsyncParams); + AsyncParams() : result(nullptr), callback(nullptr), context(nullptr) {} + + JSIValue result; + JSIValue callback; + JSIValue context; + }; + #endif + + ##start## + + static int OnDevEventReceived(void *priv, uint32_t id, struct HdfSBuf *data) + { + uint32_t value; + HdfSbufReadUint32(data, &value); + HILOG_ERROR(HILOG_MODULE_ACE,"%s: dev event received: %u %u\n", (char *)priv, id, value); + + return HDF_SUCCESS; + } + + static int GpioWriteRead(struct HdfIoService *serv, int32_t eventData, int32_t *val) + { + int ret = HDF_FAILURE; + struct HdfSBuf *data = HdfSBufObtainDefaultSize(); + struct HdfSBuf *reply = HdfSBufObtainDefaultSize(); + + if (data == NULL || reply == NULL) { + HILOG_ERROR(HILOG_MODULE_ACE,"fail to obtain sbuf data\n"); + return ret; + } + + if (!HdfSbufWriteUint8(data, (uint8_t)eventData)) + { + HILOG_ERROR(HILOG_MODULE_ACE,"fail to write sbuf\n"); + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return ret; + } + + ret = serv->dispatcher->Dispatch(&serv->object, LED_WRITE_READ, data, reply); + if (ret != HDF_SUCCESS) + { + HILOG_ERROR(HILOG_MODULE_ACE,"fail to send service call\n"); + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return ret; + } + if (!HdfSbufReadInt32(reply, val)) + { + HILOG_ERROR(HILOG_MODULE_ACE,"fail to get service call reply\n"); + ret = HDF_ERR_INVALID_OBJECT; + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return ret; + } + HILOG_ERROR(HILOG_MODULE_ACE,"Get reply is: %d\n", val); + + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return ret; + } + + JSIValue AppModule::ToggleLed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) + { + HILOG_ERROR(HILOG_MODULE_ACE, "led button pressed."); + + struct HdfIoService *serv = HdfIoServiceBind(LED_SERVICE); + if (serv == NULL) + { + HILOG_ERROR(HILOG_MODULE_ACE,"fail to get service2 %s\n", LED_SERVICE); + return JSI::CreateUndefined(); + } + + if ((args == nullptr) || (argsNum == 0) || (JSI::ValueIsUndefined(args[0]))) { + return JSI::CreateUndefined(); + } + + JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS); + JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL); + JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE); + + int32_t num = (int32_t)JSI::GetNumberProperty(args[0], "code"); + + int32_t replyData = 0; + + if (GpioWriteRead(serv, num, &replyData)) + { + HILOG_ERROR(HILOG_MODULE_ACE,"fail to send event\n"); + JSI::CallFunction(fail, thisVal, nullptr, 0); + JSI::CallFunction(complete, thisVal, nullptr, 0); + JSI::ReleaseValueList(success, fail, complete); + return JSI::CreateUndefined(); + } + + JSIValue result = JSI::CreateObject(); + JSI::SetNumberProperty(result, "led_status", replyData); + + JSIValue argv[ARGC_ONE] = {result}; + JSI::CallFunction(success, thisVal, argv, ARGC_ONE); + JSI::CallFunction(complete, thisVal, nullptr, 0); + JSI::ReleaseValueList(success, fail, complete, result); + + HdfIoServiceRecycle(serv); + + return JSI::CreateUndefined(); + } + + ##end## + + JSIValue AppModule::GetInfo(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) + { + JSIValue result = JSI::CreateUndefined(); + + cJSON *manifest = ReadManifest(); + if (manifest == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "Fail to get the content of manifest."); + return result; + } + + cJSON *appName = cJSON_GetObjectItem(manifest, KEY_APP_NAME); + cJSON *versionName = cJSON_GetObjectItem(manifest, KEY_VERSION_NAME); + cJSON *versionCode = cJSON_GetObjectItem(manifest, KEY_VERSION_CODE); + + result = JSI::CreateObject(); + if (appName != nullptr && appName->type == cJSON_String) { + JSI::SetStringProperty(result, KEY_APP_NAME, appName->valuestring); + } + if (versionName != nullptr && versionName->type == cJSON_String) { + JSI::SetStringProperty(result, KEY_VERSION_NAME, versionName->valuestring); + } + if (versionCode != nullptr && versionCode->type == cJSON_Number) { + JSI::SetNumberProperty(result, KEY_VERSION_CODE, versionCode->valuedouble); + } + cJSON_Delete(manifest); + manifest = nullptr; + return result; + } + ``` +3. 配置HDF头文件路径 + + 在`foundation\ace\ace_engine_lite\ace_lite.gni`中添加HDF头文件路径。 + + ``` + ace_lite_include_dirs += [ + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/include/core", + "//drivers/framework/include/utils", + "//drivers/adapter/uhdf/posix/include", + ] + ``` +4. 添加编译依赖 + + 修改`foundation\ace\ace_engine_lite\frameworks\BUILD.gn`,在`public_deps`中添加以下代码 + ``` + "//drivers/adapter/uhdf/manager:hdf_core", + ``` + + 修改`foundation\ace\ace_engine_lite\test\ace_test_config.gni`,在`extra_deps`中添加以下代码 + ``` + "//drivers/adapter/uhdf/manager:hdf_core", + ``` + +## 二、运行结果 + +示例代码[编译、烧录](BearPi-HM_Micro开发板编译调试.md)后,参考[如何在开发板上安装HAP应用](如何在开发板上安装HAP应用.md)教程安装device/board/bearpi/bearpi_hm_micro/tools/hap_tools/hap_example目录下的LED.hap应用进行测试 + + + + diff --git a/bearpi_hm_micro/docs/figures/BearPi-HM_Micro_Info.png b/bearpi_hm_micro/docs/figures/BearPi-HM_Micro_Info.png new file mode 100644 index 0000000..9af7eb6 Binary files /dev/null and b/bearpi_hm_micro/docs/figures/BearPi-HM_Micro_Info.png differ diff --git a/bearpi_hm_micro/docs/figures/bearpi_club_wechat.jpg b/bearpi_hm_micro/docs/figures/bearpi_club_wechat.jpg new file mode 100644 index 0000000..87a9452 Binary files /dev/null and b/bearpi_hm_micro/docs/figures/bearpi_club_wechat.jpg differ diff --git a/bearpi_hm_micro/hal/display/BUILD.gn b/bearpi_hm_micro/hal/display/BUILD.gn new file mode 100644 index 0000000..481e51a --- /dev/null +++ b/bearpi_hm_micro/hal/display/BUILD.gn @@ -0,0 +1,116 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +shared_library("display_layer") { + output_name = "display_layer" + sources = [ + "//drivers/peripheral/display/hal/disp_hal.c", + "display_layer.c", + ] + include_dirs = [ + "//drivers/peripheral/base", + "//drivers/peripheral/display/hal", + "//drivers/peripheral/display/interfaces/include", + "//drivers/adapter/uhdf/posix/include", + "//drivers/framework/include/utils", + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/utils/include", + "//drivers/framework/include/core", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + ] + deps = [ + "//drivers/adapter/uhdf/manager:hdf_core", + "//drivers/adapter/uhdf/posix:hdf_posix_osal", + "//third_party/bounds_checking_function:libsec_shared", + ] + defines = [ "__USER__" ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + "-Wno-format", + "-Wno-format-extra-args", + ] +} + +shared_library("display_gralloc") { + output_name = "display_gralloc" + sources = [ "display_gralloc.c" ] + include_dirs = [ + "//drivers/peripheral/base", + "//drivers/peripheral/display/hal", + "//drivers/drivers/peripheral/display/hal", + "//drivers/peripheral/display/interfaces/include", + "//drivers/framework/include/utils", + "//drivers/framework/include/core", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + "//drivers/adapter/uhdf/posix/include", + ] + + deps = [ + "//drivers/adapter/uhdf/manager:hdf_core", + "//drivers/adapter/uhdf/posix:hdf_posix_osal", + "//third_party/bounds_checking_function:libsec_shared", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + "-Wno-format", + "-Wno-format-extra-args", + ] +} + +shared_library("display_gfx") { + output_name = "display_gfx" + sources = [ "display_gfx.c" ] + include_dirs = [ + "//drivers/peripheral/base", + "//drivers/peripheral/display/hal", + "//drivers/peripheral/display/interfaces/include", + "//drivers/framework/include/utils", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + "//drivers/adapter/uhdf/posix/include", + ] + + deps = [ + "//drivers/adapter/uhdf/posix:hdf_posix_osal", + "//third_party/bounds_checking_function:libsec_shared", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + "-Wno-format", + "-Wno-format-extra-args", + ] +} + +lite_component("hdi_display") { + features = [ + ":display_layer", + ":display_gralloc", + ":display_gfx", + ] +} diff --git a/bearpi_hm_micro/hal/display/display_gfx.c b/bearpi_hm_micro/hal/display/display_gfx.c new file mode 100644 index 0000000..ffc929a --- /dev/null +++ b/bearpi_hm_micro/hal/display/display_gfx.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_gfx.h" +#include +#include "hdf_log.h" +#include "display_type.h" + +int32_t GfxInitialize(GfxFuncs **funcs) +{ + if (funcs == NULL) { + HDF_LOGE("%s: funcs is null", __func__); + return DISPLAY_NULL_PTR; + } + *funcs = NULL; + HDF_LOGI("%s: gfx initialize success", __func__); + return DISPLAY_SUCCESS; +} + +int32_t GfxUninitialize(GfxFuncs *funcs) +{ + if (funcs == NULL) { + HDF_LOGE("%s: funcs is null", __func__); + return DISPLAY_NULL_PTR; + } + free(funcs); + HDF_LOGI("%s: gfx uninitialize success", __func__); + return DISPLAY_SUCCESS; +} \ No newline at end of file diff --git a/bearpi_hm_micro/hal/display/display_gralloc.c b/bearpi_hm_micro/hal/display/display_gralloc.c new file mode 100644 index 0000000..3c72699 --- /dev/null +++ b/bearpi_hm_micro/hal/display/display_gralloc.c @@ -0,0 +1,470 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_layer.h" +#include +#include +#include +#include +#include +#include +#include "display_type.h" +#include "hdf_log.h" +#include "hdf_io_service_if.h" +#include "hdf_sbuf.h" +#include "osal_mem.h" + +#define DEV_ID 0 +#define LAYER_ID 0 +#define FB_PATH "/dev/fb0" +#define DISP_WIDTH 800 +#define DISP_HEIGHT 480 +#define BITS_PER_PIXEL 32 +#define BITS_TO_BYTE 8 +#define DISP_SERVICE_NAME "hdf_disp" +#define DISP_CMD_GET_PANELINFO 1 + +#undef HDF_LOG_TAG +#define HDF_LOG_TAG display_layer_c + +/* output timing */ +enum IntfSync { + OUTPUT_USER = 0, /* User timing */ + OUTPUT_PAL, /* PAL standard */ + OUTPUT_NTSC, /* NTSC standard */ + OUTPUT_1080P24, /* 1920 x 1080 at 24 Hz. */ + OUTPUT_1080P25, /* 1920 x 1080 at 25 Hz. */ + OUTPUT_1080P30, /* 1920 x 1080 at 30 Hz. */ + OUTPUT_720P50, /* 1280 x 720 at 50 Hz. */ + OUTPUT_720P60, /* 1280 x 720 at 60 Hz. */ + OUTPUT_1080I50, /* 1920 x 1080 at 50 Hz, interlace. */ + OUTPUT_1080I60, /* 1920 x 1080 at 60 Hz, interlace. */ + OUTPUT_1080P50, /* 1920 x 1080 at 50 Hz. */ + OUTPUT_1080P60, /* 1920 x 1080 at 60 Hz. */ + OUTPUT_576P50, /* 720 x 576 at 50 Hz. */ + OUTPUT_480P60, /* 720 x 480 at 60 Hz. */ + OUTPUT_800X600_60, /* VESA 800 x 600 at 60 Hz (non-interlaced) */ + OUTPUT_1024X768_60, /* VESA 1024 x 768 at 60 Hz (non-interlaced) */ + OUTPUT_1280X1024_60, /* VESA 1280 x 1024 at 60 Hz (non-interlaced) */ + OUTPUT_1366X768_60, /* VESA 1366 x 768 at 60 Hz (non-interlaced) */ + OUTPUT_1440X900_60, /* VESA 1440 x 900 at 60 Hz (non-interlaced) CVT Compliant */ + OUTPUT_1280X800_60, /* 1280*800@60Hz VGA@60Hz */ + OUTPUT_1600X1200_60, /* VESA 1600 x 1200 at 60 Hz (non-interlaced) */ + OUTPUT_1680X1050_60, /* VESA 1680 x 1050 at 60 Hz (non-interlaced) */ + OUTPUT_1920X1200_60, /* VESA 1920 x 1600 at 60 Hz (non-interlaced) CVT (Reduced Blanking) */ + OUTPUT_640X480_60, /* VESA 640 x 480 at 60 Hz (non-interlaced) CVT */ + OUTPUT_960H_PAL, /* ITU-R BT.1302 960 x 576 at 50 Hz (interlaced) */ + OUTPUT_960H_NTSC, /* ITU-R BT.1302 960 x 480 at 60 Hz (interlaced) */ + OUTPUT_1920X2160_30, /* 1920x2160_30 */ + OUTPUT_2560X1440_30, /* 2560x1440_30 */ + OUTPUT_2560X1440_60, /* 2560x1440_60 */ + OUTPUT_2560X1600_60, /* 2560x1600_60 */ + OUTPUT_3840X2160_24, /* 3840x2160_24 */ + OUTPUT_3840X2160_25, /* 3840x2160_25 */ + OUTPUT_3840X2160_30, /* 3840x2160_30 */ + OUTPUT_3840X2160_50, /* 3840x2160_50 */ + OUTPUT_3840X2160_60, /* 3840x2160_60 */ + OUTPUT_4096X2160_24, /* 4096x2160_24 */ + OUTPUT_4096X2160_25, /* 4096x2160_25 */ + OUTPUT_4096X2160_30, /* 4096x2160_30 */ + OUTPUT_4096X2160_50, /* 4096x2160_50 */ + OUTPUT_4096X2160_60, /* 4096x2160_60 */ + OUTPUT_320X240_60, /* For ota5182 at 60 Hz (8bit) */ + OUTPUT_320X240_50, /* For ili9342 at 50 Hz (6bit) */ + OUTPUT_240X320_50, /* Hi3559AV100: For ili9341 at 50 Hz (6bit) */ + OUTPUT_240X320_60, /* For ili9341 at 60 Hz (16bit) */ + OUTPUT_800X600_50, /* For LCD at 50 Hz (24bit) */ + OUTPUT_720X1280_60, /* For MIPI DSI Tx 720 x1280 at 60 Hz */ + OUTPUT_1080X1920_60, /* For MIPI DSI Tx 1080x1920 at 60 Hz */ + OUTPUT_7680X4320_30, /* For HDMI2.1 at 30 Hz */ +}; + +struct DispInfo { + uint32_t width; + uint32_t hbp; + uint32_t hfp; + uint32_t hsw; + uint32_t height; + uint32_t vbp; + uint32_t vfp; + uint32_t vsw; + uint32_t frameRate; + uint32_t intfType; + enum IntfSync intfSync; + uint32_t minLevel; + uint32_t maxLevel; + uint32_t defLevel; +}; + +struct LayerPrivate { + int32_t fd; + uint32_t width; + uint32_t height; + int32_t pitch; + void *fbAddr; + uint32_t fbSize; + void *layerAddr; + PixelFormat pixFmt; +}; + +struct LayerManager { + pthread_mutex_t mutex; + pthread_mutexattr_t mutexattr; + int32_t count; +}; + +static struct LayerManager g_layerManager; + +static void GetLayerMgr(void) +{ + g_layerManager.count++; +} + +static int32_t PutLayerMgr(void) +{ + g_layerManager.count--; + return g_layerManager.count; +} + +static void LockLayerMgr(void) +{ + pthread_mutex_lock(&g_layerManager.mutex); +} + +static void UnlockLayerMgr(void) +{ + pthread_mutex_unlock(&g_layerManager.mutex); +} + +static int32_t DispCmdSend(const uint32_t cmd, struct HdfSBuf *reqData, struct HdfSBuf *respData) +{ + struct HdfIoService *dispService = NULL; + + dispService = HdfIoServiceBind(DISP_SERVICE_NAME); + if ((dispService == NULL) || (dispService->dispatcher == NULL) || (dispService->dispatcher->Dispatch == NULL)) { + HDF_LOGE("%s:bad remote service found", __func__); + return DISPLAY_FAILURE; + } + int32_t ret = dispService->dispatcher->Dispatch(&dispService->object, cmd, reqData, respData); + if (ret != DISPLAY_SUCCESS) { + HDF_LOGE("%s: cmd=%u, ret=%d", __func__, cmd, ret); + HdfIoServiceRecycle(dispService); + return DISPLAY_FAILURE; + } + HdfIoServiceRecycle(dispService); + return DISPLAY_SUCCESS; +} + +static int32_t GetInfo(uint32_t devId, struct DispInfo *info) +{ + struct DispInfo *tmpInfo = NULL; + struct HdfSBuf *data = NULL; + struct HdfSBuf *reply = NULL; + + if (info == NULL) { + HDF_LOGE("%s: invalid param", __func__); + return DISPLAY_FAILURE; + } + data = HdfSbufObtainDefaultSize(); + if (data == NULL) { + HDF_LOGE("%s: obtain data sbuf fail", __func__); + return DISPLAY_FAILURE; + } + reply = HdfSbufObtainDefaultSize(); + if (reply == NULL) { + HDF_LOGE("%s: obtain reply sbuf fail", __func__); + HdfSbufRecycle(data); + return DISPLAY_FAILURE; + } + if (!HdfSbufWriteUint32(data, devId)) { + HDF_LOGE("HdfSbufWriteUint32 failure"); + goto ERR; + } + if (DispCmdSend(DISP_CMD_GET_PANELINFO, data, reply) != DISPLAY_SUCCESS) { + HDF_LOGE("cmd:DISP_CMD_GET_PANEL_INFO failure"); + goto ERR; + } + uint32_t dataSize = 0; + if (!HdfSbufReadBuffer(reply, (const void **)(&tmpInfo), &dataSize) || dataSize != sizeof(struct DispInfo)) { + HDF_LOGE("HdfSbufReadBuffer failure"); + goto ERR; + } + if (memcpy_s(info, sizeof(struct DispInfo), tmpInfo, dataSize) != EOK) { + HDF_LOGE("memcpy_s failure"); + goto ERR; + } + HdfSbufRecycle(data); + HdfSbufRecycle(reply); + return DISPLAY_SUCCESS; + +ERR: + HdfSbufRecycle(data); + HdfSbufRecycle(reply); + return DISPLAY_FAILURE; +} + +static struct LayerPrivate *GetLayerInstance(void) +{ + static int32_t count; + static struct DispInfo info; + static struct LayerPrivate layerPriv = { + .fd = -1, + .width = DISP_WIDTH, + .height = DISP_HEIGHT, + .pixFmt = PIXEL_FMT_RGBA_8888, + }; + + if (count == 0) { + count = 1; + if (GetInfo(DEV_ID, &info) == DISPLAY_SUCCESS) { + layerPriv.width = info.width; + layerPriv.height = info.height; + } else { + HDF_LOGI("%s: GetInfo failed, use default setting", __func__); + } + } + return &layerPriv; +} + +static int32_t InitDisplay(uint32_t devId) +{ + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + return DISPLAY_SUCCESS; +} + +static int32_t DeinitDisplay(uint32_t devId) +{ + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + return DISPLAY_SUCCESS; +} + +static void SetBackground(void) +{ + struct LayerPrivate *priv = GetLayerInstance(); + uint32_t i; + uint32_t j; + uint32_t *framebuffer = (uint32_t *)priv->fbAddr; + for (j = 0; j < priv->height; j++) { + for (i = 0; i < priv->width; i++) { + framebuffer[i + j * priv->width] = 0xFF00FF00; // Green background + } + } +} + +static int32_t CreateLayer(uint32_t devId, const LayerInfo *layerInfo, uint32_t *layerId) +{ + if (layerInfo == NULL || layerId == NULL) { + HDF_LOGE("%s: pointer is null", __func__); + return DISPLAY_NULL_PTR; + } + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + LockLayerMgr(); + struct LayerPrivate *priv = GetLayerInstance(); + priv->fd = open(FB_PATH, O_RDWR, 0); + if (priv->fd < 0) { + HDF_LOGE("%s: open fb dev failed", __func__); + UnlockLayerMgr(); + return DISPLAY_FD_ERR; + } + priv->pitch = layerInfo->width * BITS_PER_PIXEL / BITS_TO_BYTE; + priv->fbSize = ((priv->pitch * priv->height) + 0xfff) & (~0xfff); + priv->fbAddr = (void *)mmap(NULL, priv->fbSize, PROT_READ | PROT_WRITE, MAP_SHARED, priv->fd, 0); + if (priv->fbAddr == NULL) { + HDF_LOGE("%s: mmap fb address failure, errno: %d", __func__, errno); + close(priv->fd); + priv->fd = -1; + priv->pitch = 0; + priv->fbSize = 0; + UnlockLayerMgr(); + return DISPLAY_FAILURE; + } + SetBackground(); + *layerId = LAYER_ID; + HDF_LOGI("%s: open layer success", __func__); + UnlockLayerMgr(); + return DISPLAY_SUCCESS; +} + +static int32_t CloseLayer(uint32_t devId, uint32_t layerId) +{ + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + if (layerId != LAYER_ID) { + HDF_LOGE("%s: layerId invalid", __func__); + return DISPLAY_FAILURE; + } + LockLayerMgr(); + struct LayerPrivate *priv = GetLayerInstance(); + if (priv->fd >= 0) { + close(priv->fd); + } + if (priv->layerAddr != NULL) { + free(priv->layerAddr); + priv->layerAddr = NULL; + } + if (priv->fbAddr != NULL) { + munmap(priv->fbAddr, priv->fbSize); + } + priv->fd = -1; + UnlockLayerMgr(); + return DISPLAY_SUCCESS; +} + +static int32_t GetDisplayInfo(uint32_t devId, DisplayInfo *dispInfo) +{ + if (dispInfo == NULL) { + HDF_LOGE("%s: dispInfo is null", __func__); + return DISPLAY_NULL_PTR; + } + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + LockLayerMgr(); + struct LayerPrivate *priv = GetLayerInstance(); + dispInfo->width = priv->width; + dispInfo->height = priv->height; + dispInfo->rotAngle = ROTATE_NONE; + HDF_LOGD("%s: width = %u, height = %u, rotAngle = %u", __func__, dispInfo->width, + dispInfo->height, dispInfo->rotAngle); + UnlockLayerMgr(); + return DISPLAY_SUCCESS; +} + +static int32_t Flush(uint32_t devId, uint32_t layerId, LayerBuffer *buffer) +{ + int32_t ret; + + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + if (layerId != LAYER_ID) { + HDF_LOGE("%s: layerId invalid", __func__); + return DISPLAY_FAILURE; + } + if (buffer == NULL) { + HDF_LOGE("%s: buffer is null", __func__); + return DISPLAY_FAILURE; + } + + LockLayerMgr(); + struct LayerPrivate *priv = GetLayerInstance(); + ret = memcpy_s(priv->fbAddr, priv->fbSize, buffer->data.virAddr, priv->fbSize); + if (ret != EOK) { + HDF_LOGE("%s: memcpy_s fail, ret %d", __func__, ret); + UnlockLayerMgr(); + return ret; + } + UnlockLayerMgr(); + return DISPLAY_SUCCESS; +} + +static int32_t GetLayerBuffer(uint32_t devId, uint32_t layerId, LayerBuffer *buffer) +{ + if (buffer == NULL) { + HDF_LOGE("%s: buffer is null", __func__); + return DISPLAY_NULL_PTR; + } + if (devId != DEV_ID) { + HDF_LOGE("%s: devId invalid", __func__); + return DISPLAY_FAILURE; + } + if (layerId != LAYER_ID) { + HDF_LOGE("%s: layerId invalid", __func__); + return DISPLAY_FAILURE; + } + LockLayerMgr(); + struct LayerPrivate *priv = GetLayerInstance(); + if (priv->fd < 0) { + HDF_LOGE("%s: fd invalid", __func__); + UnlockLayerMgr(); + return DISPLAY_FAILURE; + } + buffer->fenceId = 0; + buffer->width = priv->width; + buffer->height = priv->height; + buffer->pixFormat = priv->pixFmt; + buffer->pitch = priv->pitch; + buffer->data.virAddr = malloc(priv->fbSize); + if (buffer->data.virAddr == NULL) { + HDF_LOGE("%s: malloc failure", __func__); + UnlockLayerMgr(); + return DISPLAY_FAILURE; + } + priv->layerAddr = buffer->data.virAddr; + (void)memset_s(buffer->data.virAddr, priv->fbSize, 0x00, priv->fbSize); + HDF_LOGD("%s: fenceId = %d, width = %d, height = %d, pixFormat = %d, pitch = %d", __func__, buffer->fenceId, + buffer->width, buffer->height, buffer->pixFormat, buffer->pitch); + UnlockLayerMgr(); + return DISPLAY_SUCCESS; +} + +int32_t LayerInitialize(LayerFuncs **funcs) +{ + static LayerFuncs *lFuncs = NULL; + + if (funcs == NULL) { + HDF_LOGE("%s: funcs is null", __func__); + return DISPLAY_NULL_PTR; + } + if (lFuncs == NULL) { + lFuncs = (LayerFuncs *)OsalMemCalloc(sizeof(LayerFuncs)); + if (lFuncs == NULL) { + HDF_LOGE("%s: lFuncs is null", __func__); + return DISPLAY_NULL_PTR; + } + pthread_mutexattr_init(&g_layerManager.mutexattr); + pthread_mutexattr_setpshared(&g_layerManager.mutexattr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(&g_layerManager.mutex, &g_layerManager.mutexattr); + lFuncs->InitDisplay = InitDisplay; + lFuncs->DeinitDisplay = DeinitDisplay; + lFuncs->GetDisplayInfo = GetDisplayInfo; + lFuncs->CreateLayer = CreateLayer; + lFuncs->CloseLayer = CloseLayer; + lFuncs->Flush = Flush; + lFuncs->GetLayerBuffer = GetLayerBuffer; + } + *funcs = lFuncs; + GetLayerMgr(); + HDF_LOGI("%s: success", __func__); + return DISPLAY_SUCCESS; +} + +int32_t LayerUninitialize(LayerFuncs *funcs) +{ + if (funcs == NULL) { + HDF_LOGE("%s: funcs is null", __func__); + return DISPLAY_NULL_PTR; + } + if (PutLayerMgr() == 0) { + pthread_mutexattr_destroy(&g_layerManager.mutexattr); + pthread_mutex_destroy(&g_layerManager.mutex); + OsalMemFree(funcs); + } + HDF_LOGI("%s: layer uninitialize success", __func__); + return DISPLAY_SUCCESS; +} diff --git a/bearpi_hm_micro/hal/display/display_layer.c b/bearpi_hm_micro/hal/display/display_layer.c new file mode 100644 index 0000000..fe854f3 --- /dev/null +++ b/bearpi_hm_micro/hal/display/display_layer.c @@ -0,0 +1,371 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_gralloc.h" +#include +#include +#include +#include +#include +#include +#include +#include "buffer_handle.h" +#include "display_type.h" +#include "disp_common.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define DEFAULT_READ_WRITE_PERMISSIONS 0666 +#define MAX_MALLOC_SIZE 0x10000000L +#define SHM_MAX_KEY 10000 +#define SHM_START_KEY 1 +#define INVALID_SHMID -1 +#define BITS_PER_BYTE 8 + +#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d)) +#define ALIGN_UP(x, a) ((((x) + ((a)-1)) / (a)) * (a)) +#define HEIGHT_ALIGN 2U +#define WIDTH_ALIGN 8U +#define MAX_PLANES 3 + +#undef HDF_LOG_TAG +#define HDF_LOG_TAG display_gralloc_c + +typedef struct { + BufferHandle hdl; + int32_t shmid; +} PriBufferHandle; + +typedef struct { + uint32_t numPlanes; + uint32_t radio[MAX_PLANES]; +} PlaneLayoutInfo; + +typedef struct { + uint32_t format; + uint32_t bitsPerPixel; // bits per pixel for first plane + const PlaneLayoutInfo *planes; +} FormatInfo; + +struct GrallocManager { + pthread_mutex_t mutex; + pthread_mutexattr_t mutexattr; + int32_t count; +}; + +static struct GrallocManager g_grallocManager; + +static const PlaneLayoutInfo g_yuv420SPLayout = { + .numPlanes = 2, + .radio = { 4, 2 }, +}; + +static const PlaneLayoutInfo g_yuv420PLayout = { + .numPlanes = 3, + .radio = { 4, 1, 1 }, +}; + +static void GetGrallocMgr(void) +{ + g_grallocManager.count++; +} + +static int32_t PutGrallocMgr(void) +{ + g_grallocManager.count--; + return g_grallocManager.count; +} + +static void LockGrallocMgr(void) +{ + pthread_mutex_lock(&g_grallocManager.mutex); +} + +static void UnlockGrallocMgr(void) +{ + pthread_mutex_unlock(&g_grallocManager.mutex); +} + +static const FormatInfo *GetFormatInfo(uint32_t format) +{ + static const FormatInfo fmtInfos[] = { + {PIXEL_FMT_RGBX_8888, 32, NULL}, {PIXEL_FMT_RGBA_8888, 32, NULL}, + {PIXEL_FMT_BGRX_8888, 32, NULL}, {PIXEL_FMT_BGRA_8888, 32, NULL}, + {PIXEL_FMT_RGB_888, 24, NULL}, {PIXEL_FMT_BGR_565, 16, NULL}, + {PIXEL_FMT_RGBA_5551, 16, NULL}, {PIXEL_FMT_RGB_565, 16, NULL}, + {PIXEL_FMT_BGRX_4444, 16, NULL}, {PIXEL_FMT_BGRA_4444, 16, NULL}, + {PIXEL_FMT_RGBA_4444, 16, NULL}, {PIXEL_FMT_RGBX_4444, 16, NULL}, + {PIXEL_FMT_BGRX_5551, 16, NULL}, {PIXEL_FMT_BGRA_5551, 16, NULL}, + {PIXEL_FMT_YCBCR_420_SP, 8, &g_yuv420SPLayout}, {PIXEL_FMT_YCRCB_420_SP, 8, &g_yuv420SPLayout}, + {PIXEL_FMT_YCBCR_420_P, 8, &g_yuv420PLayout}, {PIXEL_FMT_YCRCB_420_P, 8, &g_yuv420PLayout}, + }; + + for (uint32_t i = 0; i < sizeof(fmtInfos) / sizeof(FormatInfo); i++) { + if (fmtInfos[i].format == format) { + return &fmtInfos[i]; + } + } + HDF_LOGE("the format can not support %d %d", format, PIXEL_FMT_RGBA_8888); + return NULL; +} + +static uint32_t AdjustStrideFromFormat(uint32_t format, uint32_t width) +{ + const FormatInfo *fmtInfo = GetFormatInfo(format); + if ((fmtInfo != NULL) && (fmtInfo->planes != NULL)) { + uint32_t sum = fmtInfo->planes->radio[0]; + for (uint32_t i = 1; (i < fmtInfo->planes->numPlanes) && (i < MAX_PLANES); i++) { + sum += fmtInfo->planes->radio[i]; + } + if (sum > 0) { + width = DIV_ROUND_UP((width * sum), fmtInfo->planes->radio[0]); + } + } + return width; +} + +static int32_t InitBufferHandle(PriBufferHandle* buffer, const AllocInfo* info) +{ + int32_t size; + int32_t stride; + int32_t h = ALIGN_UP(info->height, HEIGHT_ALIGN); + const FormatInfo *fmtInfo = GetFormatInfo(info->format); + if (fmtInfo == NULL) { + HDF_LOGE("can not get format information : %d", buffer->hdl.format); + return DISPLAY_FAILURE; + } + + stride = ALIGN_UP(AdjustStrideFromFormat(info->format, info->width), WIDTH_ALIGN) * + fmtInfo->bitsPerPixel / BITS_PER_BYTE; + size = h * stride; + buffer->hdl.width = info->width; + buffer->hdl.stride = stride; + buffer->hdl.height = info->height; + buffer->hdl.size = size; + buffer->hdl.usage = info->usage; + buffer->hdl.fd = -1; + buffer->shmid = INVALID_SHMID; + buffer->hdl.format = info->format; + buffer->hdl.reserveInts = (sizeof(PriBufferHandle) - sizeof(BufferHandle) - + buffer->hdl.reserveFds * sizeof(uint32_t)) / sizeof(uint32_t); + return DISPLAY_SUCCESS; +} + +static int32_t AllocShm(BufferHandle *buffer) +{ + static int32_t key = SHM_START_KEY; + int32_t shmid; + + while ((shmid = shmget(key, buffer->size, IPC_CREAT | IPC_EXCL | DEFAULT_READ_WRITE_PERMISSIONS)) < 0) { + if (errno != EEXIST) { + HDF_LOGE("%s: fail to alloc the shared memory, errno = %d", __func__, errno); + return DISPLAY_FAILURE; + } + key++; + if (key >= SHM_MAX_KEY) { + key = SHM_START_KEY; + } + } + void *pBase = shmat(shmid, NULL, 0); + if (pBase == ((void *)-1)) { + HDF_LOGE("%s: Fail to attach the shared memory, errno = %d", __func__, errno); + if (shmctl(shmid, IPC_RMID, 0) == -1) { + HDF_LOGE("%s: Fail to free shmid, errno = %d", __func__, errno); + } + return DISPLAY_FAILURE; + } + buffer->virAddr = pBase; + buffer->key = key; + ((PriBufferHandle*)buffer)->shmid = shmid; + key++; + if (memset_s(pBase, buffer->size, 0x0, buffer->size) != EOK) { + HDF_LOGE("memset_s failure"); + if (shmctl(shmid, IPC_RMID, 0) == -1) { + HDF_LOGE("%s: Fail to free shmid, errno = %d", __func__, errno); + } + return DISPLAY_FAILURE; + } + if (key >= SHM_MAX_KEY) { + key = SHM_START_KEY; + } + return DISPLAY_SUCCESS; +} + +static int32_t AllocMem(const AllocInfo* info, BufferHandle **buffer) +{ + int32_t ret; + + DISPLAY_CHK_RETURN((buffer == NULL), DISPLAY_NULL_PTR, HDF_LOGE("%s: in buffer is null", __func__)); + DISPLAY_CHK_RETURN((info == NULL), DISPLAY_NULL_PTR, HDF_LOGE("%s: in info is null", __func__)); + PriBufferHandle* priBuffer = calloc(1, sizeof(PriBufferHandle)); + DISPLAY_CHK_RETURN((priBuffer == NULL), DISPLAY_NULL_PTR, HDF_LOGE("%s: can not calloc errno : %d", + __func__, errno)); + ret = InitBufferHandle(priBuffer, info); + DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, HDF_LOGE("%s: can not init buffe handle", + __func__); goto OUT); + + BufferHandle *bufferHdl = &priBuffer->hdl; + DISPLAY_CHK_RETURN(((bufferHdl->size > MAX_MALLOC_SIZE) || (bufferHdl->size == 0)), + DISPLAY_FAILURE, HDF_LOGE("%s: size is invalid %d ", __func__, bufferHdl->size); goto OUT); + LockGrallocMgr(); + + ret = AllocShm(bufferHdl); + + +OUT: + if ((ret != DISPLAY_SUCCESS) && (bufferHdl != NULL)) { + free(bufferHdl); + bufferHdl = NULL; + } + *buffer = bufferHdl; + UnlockGrallocMgr(); + return ret; +} + +static void FreeShm(BufferHandle *buffer) +{ + CHECK_NULLPOINTER_RETURN(buffer->virAddr); + if (shmdt(buffer->virAddr) == -1) { + HDF_LOGE("%s: Fail to free shared memory, errno = %d", __func__, errno); + } + if (shmctl(((PriBufferHandle*)buffer)->shmid, IPC_RMID, 0) == -1) { + HDF_LOGE("%s: Fail to free shmid, errno = %d", __func__, errno); + } +} + +static void FreeMem(BufferHandle *buffer) +{ + CHECK_NULLPOINTER_RETURN(buffer); + if ((buffer->size > MAX_MALLOC_SIZE) || (buffer->size == 0)) { + HDF_LOGE("%s: size is invalid, buffer->size = %d", __func__, buffer->size); + return; + } + + LockGrallocMgr(); + + FreeShm(buffer); + + UnlockGrallocMgr(); +} + +static void *MmapShm(BufferHandle *buffer) +{ + int32_t shmid; + + shmid = shmget(buffer->key, buffer->size, IPC_EXCL | DEFAULT_READ_WRITE_PERMISSIONS); + if (shmid < 0) { + HDF_LOGE("%s: Fail to mmap the shared memory, errno = %d", __func__, errno); + return NULL; + } + void *pBase = shmat(shmid, NULL, 0); + if (pBase == ((void *)-1)) { + HDF_LOGE("%s: Fail to attach the shared memory, errno = %d", __func__, errno); + return NULL; + } + ((PriBufferHandle*)buffer)->shmid = shmid; + return pBase; +} + +static void *Mmap(BufferHandle *buffer) +{ + void *temp = NULL; + + CHECK_NULLPOINTER_RETURN_VALUE(buffer, NULL); + if ((buffer->size > MAX_MALLOC_SIZE) || (buffer->size == 0)) { + HDF_LOGE("%s: size is invalid, buffer->size = %d", __func__, buffer->size); + return NULL; + } + + LockGrallocMgr(); + + temp = MmapShm(buffer); + + UnlockGrallocMgr(); + return temp; +} + +static int32_t UnmapShm(BufferHandle *buffer) +{ + if (shmdt(buffer->virAddr) == -1) { + HDF_LOGE("%s: Fail to unmap shared memory errno = %d", __func__, errno); + return DISPLAY_FAILURE; + } + int32_t shmid = ((PriBufferHandle*)buffer)->shmid; + if ((shmid != INVALID_SHMID) && (shmctl(shmid, IPC_RMID, 0) == -1)) { + + } + return DISPLAY_SUCCESS; +} + +static int32_t Unmap(BufferHandle *buffer) +{ + int32_t ret; + + CHECK_NULLPOINTER_RETURN_VALUE(buffer, DISPLAY_NULL_PTR); + CHECK_NULLPOINTER_RETURN_VALUE(buffer->virAddr, DISPLAY_NULL_PTR); + if ((buffer->size > MAX_MALLOC_SIZE) || (buffer->size == 0)) { + HDF_LOGE("%s: size is invalid, buffer->size = %d", __func__, buffer->size); + return DISPLAY_FAILURE; + } + LockGrallocMgr(); + + ret = UnmapShm(buffer); + + UnlockGrallocMgr(); + return ret; +} + +int32_t GrallocInitialize(GrallocFuncs **funcs) +{ + static GrallocFuncs *gFuncs = NULL; + + if (funcs == NULL) { + HDF_LOGE("%s: funcs is null", __func__); + return DISPLAY_NULL_PTR; + } + if (gFuncs == NULL) { + gFuncs = (GrallocFuncs *)OsalMemCalloc(sizeof(GrallocFuncs)); + if (gFuncs == NULL) { + HDF_LOGE("%s: gFuncs is null", __func__); + return DISPLAY_NULL_PTR; + } + pthread_mutexattr_init(&g_grallocManager.mutexattr); + pthread_mutexattr_setpshared(&g_grallocManager.mutexattr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(&g_grallocManager.mutex, &g_grallocManager.mutexattr); + gFuncs->AllocMem = AllocMem; + gFuncs->FreeMem = FreeMem; + gFuncs->Mmap = Mmap; + gFuncs->Unmap = Unmap; + } + *funcs = gFuncs; + GetGrallocMgr(); + HDF_LOGI("%s: gralloc initialize success", __func__); + return DISPLAY_SUCCESS; +} + +int32_t GrallocUninitialize(GrallocFuncs *funcs) +{ + if (funcs == NULL) { + HDF_LOGE("%s: funcs is null", __func__); + return DISPLAY_NULL_PTR; + } + if (PutGrallocMgr() == 0) { + pthread_mutexattr_destroy(&g_grallocManager.mutexattr); + pthread_mutex_destroy(&g_grallocManager.mutex); + OsalMemFree(funcs); + } + HDF_LOGI("%s: gralloc uninitialize success", __func__); + return DISPLAY_SUCCESS; +} diff --git a/bearpi_hm_micro/hal/update/BUILD.gn b/bearpi_hm_micro/hal/update/BUILD.gn new file mode 100644 index 0000000..eb000ca --- /dev/null +++ b/bearpi_hm_micro/hal/update/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +shared_library("hal_update") { + output_name = "hal_update" + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + "-Wno-format", + "-Wno-format-extra-args", + ] +} diff --git a/bearpi_hm_micro/liteos_a/BUILD.gn b/bearpi_hm_micro/liteos_a/BUILD.gn new file mode 100644 index 0000000..31d5825 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmd = "if [ -f $product_path/hdf_config/BUILD.gn ]; then echo true; else echo false; fi" +HAVE_PRODUCT_CONFIG = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + +group("liteos_a") { + deps = [ + "board", + "drivers", + ] + if (HAVE_PRODUCT_CONFIG) { + deps += [ "$product_path/hdf_config" ] + } else { + deps += [ "hdf_config" ] + } +} + +config("public") { + configs = [ + "board:public", + "drivers:public", + ] +} diff --git a/bearpi_hm_micro/liteos_a/board/BUILD.gn b/bearpi_hm_micro/liteos_a/board/BUILD.gn new file mode 100644 index 0000000..0d40223 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/board/BUILD.gn @@ -0,0 +1,31 @@ +# Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//kernel/liteos_a/liteos.gni") + +module_name = "bsp_config" + +kernel_module(module_name) { + sources = [] + if (defined(LOSCFG_PLATFORM_ADAPT)) { + sources += [ "os_adapt/os_adapt.c" ] + } +} + +config("public") { + include_dirs = [ "." ] + include_dirs += [ "include" ] + include_dirs += [ "$LITEOSTOPDIR/drivers/block/disk/include" ] + include_dirs += + [ "$LITEOSTOPDIR/../../drivers/adapter/khdf/liteos/osal/include" ] +} diff --git a/bearpi_hm_micro/liteos_a/board/Makefile b/bearpi_hm_micro/liteos_a/board/Makefile new file mode 100644 index 0000000..956fabc --- /dev/null +++ b/bearpi_hm_micro/liteos_a/board/Makefile @@ -0,0 +1,28 @@ +# Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include $(LITEOSTOPDIR)/config.mk + +MODULE_NAME := bsp_config + +LOCAL_SRCS += $(wildcard *.c) + +ifeq ($(LOSCFG_PLATFORM_ADAPT), y) +LOCAL_SRCS += $(wildcard os_adapt/*.c) +endif + +LOCAL_CFLAGS += -I$(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/osal/include + +LOCAL_FLAGS := $(LOCAL_INCLUDE) + +include $(MODULE) diff --git a/bearpi_hm_micro/liteos_a/board/include/hisoc/random.h b/bearpi_hm_micro/liteos_a/board/include/hisoc/random.h new file mode 100644 index 0000000..8ee56e2 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/board/include/hisoc/random.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __SOC_RANDOM_H__ +#define __SOC_RANDOM_H__ + +/* + * When kernel decoupled with specific devices, + * these code can be removed. + */ +VOID HiRandomHwInit(VOID); +VOID HiRandomHwDeinit(VOID); +INT32 HiRandomHwGetInteger(UINT32 *result); +INT32 HiRandomHwGetNumber(CHAR *buffer, size_t bufLen); + +#ifdef LOSCFG_HW_RANDOM_ENABLE +void Mp1xxRngInit(void); +#endif + +#endif diff --git a/bearpi_hm_micro/liteos_a/board/os_adapt/os_adapt.c b/bearpi_hm_micro/liteos_a/board/os_adapt/os_adapt.c new file mode 100644 index 0000000..d129601 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/board/os_adapt/os_adapt.c @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "target_config.h" +#include "los_typedef.h" +#include +#include "stdlib.h" +#include "stdio.h" +#include "los_process_pri.h" +#include "disk.h" +#include "sys/mount.h" +#include "los_rootfs.h" + +#include "los_config.h" +#include "gic_common.h" +#include "los_printf.h" +#include "los_smp.h" +#include "los_vm_map.h" +#include "los_vm_zone.h" +#include "los_vm_boot.h" +#include "los_mmu_descriptor_v6.h" +#include "los_init.h" + +#include "mtd_partition.h" + +#ifdef LOSCFG_FS_VFS +#include "disk.h" +#endif +#include "los_rootfs.h" +#ifdef LOSCFG_SHELL +#include "shell.h" +#include "shcmd.h" +#endif + +#ifdef LOSCFG_DRIVERS_MEM +#include "los_dev_mem.h" +#endif + +#ifdef LOSCFG_DRIVERS_HDF +#include "devmgr_service_start.h" +#endif + +#ifdef LOSCFG_DRIVERS_HDF_PLATFORM_WATCHDOG +#include "watchdog_if.h" +#endif + +#include "los_task.h" + +#include "los_bootargs.h" +#include "los_rootfs.h" + + +#ifdef LOSCFG_DRIVERS_RANDOM +#include "los_random.h" +#include "hisoc/random.h" +#endif + +#ifdef LOSCFG_DRIVERS_MEM +int mem_dev_register(void) +{ + return DevMemRegister(); +} +#endif + +VOID HalClockIrqClear(VOID) {} + +#include "lwip/tcpip.h" +#include "lwip/netif.h" +#include "lwip/netifapi.h" + +#define SLEEP_TIME_MS 60 +#define NETIF_SETUP_OVERTIME 100 + +void net_init(void) +{ + extern void tcpip_init(tcpip_init_done_fn initfunc, void *arg); + dprintf("\ntcpip_init start\n"); + tcpip_init(NULL, NULL); + dprintf("\ntcpip_init end\n"); + PRINTK("Ethernet start."); +} + + +#ifdef LOSCFG_KERNEL_SMP +extern int Mp1SmpSecCoreOn(UINT32 cpuid, UINTPTR entry_point); +STATIC struct SmpOps g_stm32Mp1SmpOps = { + .SmpCpuOn = Mp1SmpSecCoreOn, +}; + +STATIC VOID stm32Mp1SmpInit(VOID) +{ + LOS_SmpOpsSet(&g_stm32Mp1SmpOps); +} + +LOS_MODULE_INIT(stm32Mp1SmpInit, LOS_INIT_LEVEL_EARLIEST); +#endif + +void SystemInit(void) +{ +#ifdef LOSCFG_DRIVERS_RANDOM + dprintf("dev random init ...\n"); + Mp1xxRngInit(); +#endif +#ifdef LOSCFG_DRIVERS_MEM + dprintf("mem dev init ...\n"); + extern int mem_dev_register(void); + mem_dev_register(); +#endif + + dprintf("Date:%s.\n", __DATE__); + dprintf("Time:%s.\n", __TIME__); + +#ifdef LOSCFG_DRIVERS_HDF + dprintf("DeviceManagerStart start ...\n"); + if (DeviceManagerStart()) { + PRINT_ERR("No drivers need load by hdf manager!"); + } + dprintf("DeviceManagerStart end ...\n"); +#endif + net_init(); + +#ifdef LOSCFG_PLATFORM_ROOTFS + dprintf("OsMountRootfs start ...\n"); + if (LOS_GetCmdLine()) { + dprintf("get cmdline error!\n"); + } + if (LOS_ParseBootargs()) { + dprintf("parse bootargs error!\n"); + } + if (OsMountRootfs()) { + dprintf("mount rootfs error!\n"); + } + dprintf("OsMountRootfs end ...\n"); +#endif + + dprintf("Before PLATFORM_UART ...\n"); + +#ifdef LOSCFG_DRIVERS_HDF_PLATFORM_UART + if (virtual_serial_init(TTY_DEVICE) != 0) { + PRINT_ERR("virtual_serial_init failed"); + } + if (system_console_init(SERIAL) != 0) { + PRINT_ERR("system_console_init failed\n"); + } +#endif + + dprintf("After PLATFORM_UART ...\n"); + + if (OsUserInitProcess()) { + PRINT_ERR("Create user init process failed!\n"); + return; + } + dprintf("cat log shell end\n"); + return; +} + +#ifdef LOSCFG_KERNEL_MMU +LosArchMmuInitMapping g_archMmuInitMapping[] = { + { + .phys = SYS_MEM_BASE, + .virt = KERNEL_VMM_BASE, + .size = KERNEL_VMM_SIZE, + .flags = MMU_DESCRIPTOR_KERNEL_L1_PTE_FLAGS, + .name = "KernelCached", + }, + { + .phys = SYS_MEM_BASE, + .virt = UNCACHED_VMM_BASE, + .size = UNCACHED_VMM_SIZE, + .flags = MMU_INITIAL_MAP_NORMAL_NOCACHE, + .name = "KernelUncached", + }, + { + .phys = PERIPH_PMM_BASE, + .virt = PERIPH_DEVICE_BASE, + .size = PERIPH_DEVICE_SIZE, + .flags = MMU_INITIAL_MAP_DEVICE, + .name = "PeriphDevice", + }, + { + .phys = PERIPH_PMM_BASE, + .virt = PERIPH_CACHED_BASE, + .size = PERIPH_CACHED_SIZE, + .flags = MMU_DESCRIPTOR_KERNEL_L1_PTE_FLAGS, + .name = "PeriphCached", + }, + { + .phys = PERIPH_PMM_BASE, + .virt = PERIPH_UNCACHED_BASE, + .size = PERIPH_UNCACHED_SIZE, + .flags = MMU_INITIAL_MAP_STRONGLY_ORDERED, + .name = "PeriphStronglyOrdered", + }, + { + .phys = GIC_PHY_BASE, + .virt = GIC_VIRT_BASE, + .size = GIC_VIRT_SIZE, + .flags = MMU_INITIAL_MAP_DEVICE, + .name = "GIC", + }, + { + .phys = FB_PHY_BASE, + .virt = FB_VIRT_BASE, + .size = FB_SIZE, + .flags = MMU_INITIAL_MAP_DEVICE, + .name = "FB", + }, + {0} +}; + +struct ArchMmuInitMapping *OsGetMmuPartition(const char *name) +{ + UINT32 index = 0; + UINT32 mmuBlockCount = sizeof(g_archMmuInitMapping) / sizeof(g_archMmuInitMapping[0]); + + for (index = 0; index < (mmuBlockCount - 1); index++) { + if (strncmp(name, g_archMmuInitMapping[index].name, 20) == 0) { + return &g_archMmuInitMapping[index]; + } + } + return NULL; +} + +#endif diff --git a/bearpi_hm_micro/liteos_a/board/os_adapt/os_adapt.h b/bearpi_hm_micro/liteos_a/board/os_adapt/os_adapt.h new file mode 100644 index 0000000..5a8da63 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/board/os_adapt/os_adapt.h @@ -0,0 +1,36 @@ + +/* + * Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __OS_ADAPT_H__ +#define __OS_ADAPT_H__ + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_KERNEL_MMU +struct ArchMmuInitMapping *OsGetMmuPartition(const char *name); +#endif + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif diff --git a/bearpi_hm_micro/liteos_a/board/target_config.h b/bearpi_hm_micro/liteos_a/board/target_config.h new file mode 100644 index 0000000..d83e427 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/board/target_config.h @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __BOARD_CONFIG_H__ +#define __BOARD_CONFIG_H__ + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/* +MMU内存映射表 +| DDR | DDR | PMM | PMM | PMM | GIC | ... | +| 256MB | 256MB | 512MB | 512MB | 512MB | 1M | xxx | +*/ + +#define OS_SYS_CLOCK (24000000) +#define SYS_CTRL_REG_BASE IO_DEVICE_ADDR(0x12020000) +#define REG_SC_CTRL 0 + +/* smp */ +#define CRG_REG_ADDR (0x12010000) +#define CRG_REG_BASE IO_DEVICE_ADDR(CRG_REG_ADDR) +#define PERI_CRG30_BASE IO_DEVICE_ADDR(CRG_REG_ADDR + 0x0078) /* cpu freq-mode & reset CRG */ + +/* memory config */ +#define CACHE_ALIGNED_SIZE (64) + +// 256MB +#ifdef LOSCFG_TEE_ENABLE +#define DDR_MEM_ADDR 0xC1000000 +#define DDR_MEM_SIZE 0xf000000 +#else +#define DDR_MEM_ADDR 0xBF000000 +#define DDR_MEM_SIZE 0x11000000 +#endif + +#define SYS_MEM_BASE DDR_MEM_ADDR +#define SYS_MEM_SIZE_DEFAULT 0x07f00000 +#define SYS_MEM_END (SYS_MEM_BASE + SYS_MEM_SIZE_DEFAULT) + +/* Peripheral register address base and size */ +#define PERIPH_PMM_BASE 0x40000000 +#define PERIPH_PMM_SIZE 0x20000000 + +/* kernel load address */ +#define KERNEL_LOAD_ADDRESS (0xC0100000) + +/* hwi */ +/** + * 查手册获取中断号列表 + * Maximum number of supported hardware devices that generate hardware interrupts. + * The maximum number of hardware devices that generate hardware interrupts is 288. + */ +#define OS_HWI_MAX_NUM 288 +/** + * Maximum interrupt number. + */ +#define OS_HWI_MAX ((OS_HWI_MAX_NUM) - 1) +/** + * Minimum interrupt number. + */ +#define OS_HWI_MIN 0 +/** + * Maximum usable interrupt number. + */ +#define OS_USER_HWI_MAX OS_HWI_MAX +/** + * Minimum usable interrupt number. + */ +#define OS_USER_HWI_MIN OS_HWI_MIN + +/* + 通用定时器中断号设置 + 使用 Non-secure 的时钟中断 +*/ +#define NUM_HAL_INTERRUPT_CNTPSIRQ 29 /* Secure physical timer event. */ +#define NUM_HAL_INTERRUPT_CNTPNSIRQ 30 /* Non-secure physical timer event. */ +#ifdef LOSCFG_TEE_ENABLE +#define OS_TICK_INT_NUM NUM_HAL_INTERRUPT_CNTPNSIRQ // use non-secure physical timer for now +#else +#define OS_TICK_INT_NUM NUM_HAL_INTERRUPT_CNTPSIRQ // use secure physical timer for now +#endif + +#define NUM_HAL_INTERRUPT_TIMER0 37 +#define NUM_HAL_INTERRUPT_TIMER3 38 +#define NUM_HAL_INTERRUPT_UART4 84 + +/* GIC base and size : 1M-align */ +#define GIC_PHY_BASE 0xA0000000 +#define GIC_PHY_SIZE 0x100000 + +/* GIC */ +#define GIC_VIRT_BASE (PERIPH_UNCACHED_BASE + PERIPH_UNCACHED_SIZE) +#define GIC_BASE_ADDR (GIC_VIRT_BASE + 0x20000) +#define GIC_VIRT_SIZE U32_C(GIC_PHY_SIZE) +#define GICD_OFFSET 0x1000 /* interrupt distributor offset */ +#define GICC_OFFSET 0x2000 /* CPU interface register offset */ + +/* FB */ +#define FB_VIRT_BASE (GIC_VIRT_BASE + GIC_VIRT_SIZE) /* 虚拟地址放在GIC_VIRT后面 */ +#define FB_SIZE (0x400000 * 2) /* 4M * 2 */ +#define FB_PHY_BASE (DDR_MEM_ADDR + DDR_MEM_SIZE) /* 物理地址放在DDR后面 */ + +#define BIT(n) (1U << (n)) + +/* use UART4 as debug uart */ +#define UART4_REG_ADDR 0x40010000 +#define UART4_REG_PBASE (UART4_REG_ADDR + 0x0000) +#define UART4_REG_BASE IO_DEVICE_ADDR(UART4_REG_PBASE) +#define TTY_DEVICE "/dev/uartdev-4" +#define UART_REG_BASE UART4_REG_BASE +#define DEBUG_UART_BASE UART4_REG_ADDR +#define NUM_HAL_INTERRUPT_UART NUM_HAL_INTERRUPT_UART4 + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif diff --git a/bearpi_hm_micro/liteos_a/config.gni b/bearpi_hm_micro/liteos_a/config.gni new file mode 100644 index 0000000..5fc712b --- /dev/null +++ b/bearpi_hm_micro/liteos_a/config.gni @@ -0,0 +1,61 @@ +# Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Kernel type, e.g. "linux", "liteos_a", "liteos_m". +kernel_type = "liteos_a" + +# Kernel version. +kernel_version = "" + +# Board CPU type, e.g. "cortex-a7", "riscv32". +board_cpu = "cortex-a7" + +# Board arch, e.g. "armv7-a", "rv32imac". +board_arch = "" + +# Toolchain name used for system compiling. +# E.g. gcc-arm-none-eabi, arm-linux-harmonyeabi-gcc, ohos-clang, riscv32-unknown-elf. +# Note: The default toolchain is "ohos-clang". It's not mandatory if you use the default toolchain. +board_toolchain = "" + +# The toolchain path installed, it's not mandatory if you have added toolchain path to your ~/.bashrc. +board_toolchain_path = "" + +# Compiler prefix. +board_toolchain_prefix = "" + +# Compiler type, "gcc" or "clang". +board_toolchain_type = "clang" + +# Board related common compile flags. +board_cflags = [ + "-mfloat-abi=softfp", + "-mfpu=neon-vfpv4", +] +board_cxx_flags = [ + "-mfloat-abi=softfp", + "-mfpu=neon-vfpv4", +] +board_ld_flags = [] + +# Board related headfiles search path. +board_include_dirs = [] + +# Board adapter dir for OHOS components. +board_adapter_dir = "//device/board/bearpi/bearpi_hm_micro/hal" + +# Sysroot path. +board_configed_sysroot = "" + +# Board storage type, it used for file system generation. +storage_type = "emmc" diff --git a/bearpi_hm_micro/liteos_a/drivers/BUILD.gn b/bearpi_hm_micro/liteos_a/drivers/BUILD.gn new file mode 100644 index 0000000..dfcf7fe --- /dev/null +++ b/bearpi_hm_micro/liteos_a/drivers/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2021 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//drivers/adapter/khdf/liteos/hdf.gni") + +group("drivers") { + public_deps = [ "//device/soc/st/common/platform:drivers" ] +} + +config("public") { + configs = [ "//device/soc/st/common/platform:public" ] +} diff --git a/bearpi_hm_micro/liteos_a/drivers/Kconfig b/bearpi_hm_micro/liteos_a/drivers/Kconfig new file mode 100644 index 0000000..2ff30e8 --- /dev/null +++ b/bearpi_hm_micro/liteos_a/drivers/Kconfig @@ -0,0 +1,20 @@ +source "../../device/soc/st/common/platform/Kconfig" + +config PLATFORM + string + default "stm32mp157" if PLATFORM_STM32MP157 + +config PRODUCT_NAME + string "product name" + default "bearpi_hm_micro" if PRODUCT_BEARPI_HM_MICRO + +config DEVICE_COMPANY + string "vendor name" + default "st" if PLATFORM_STM32MP157 + +config TEE_ENABLE + bool "Enable TEE" + default n + depends on PLATFORM_STM32MP157 + help + Enable teeos in platform \ No newline at end of file diff --git a/bearpi_hm_micro/tools/README.md b/bearpi_hm_micro/tools/README.md new file mode 100644 index 0000000..69d1f98 --- /dev/null +++ b/bearpi_hm_micro/tools/README.md @@ -0,0 +1,6 @@ +# 目录结构 + +| 目录名 | 描述 | +| ---- | ------ | +| download_img | tf-a、uboot及烧录配置文件 | +| hap_tools | 应用安装工具及可安装hap包 | diff --git a/bearpi_hm_micro/tools/download_img/arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-serialboot.stm32 b/bearpi_hm_micro/tools/download_img/arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-serialboot.stm32 new file mode 100644 index 0000000..e29f37f Binary files /dev/null and b/bearpi_hm_micro/tools/download_img/arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-serialboot.stm32 differ diff --git a/bearpi_hm_micro/tools/download_img/arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-trusted.stm32 b/bearpi_hm_micro/tools/download_img/arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-trusted.stm32 new file mode 100644 index 0000000..a301d19 Binary files /dev/null and b/bearpi_hm_micro/tools/download_img/arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-trusted.stm32 differ diff --git a/bearpi_hm_micro/tools/download_img/bootloader/u-boot.stm32 b/bearpi_hm_micro/tools/download_img/bootloader/u-boot.stm32 new file mode 100644 index 0000000..1c64dca Binary files /dev/null and b/bearpi_hm_micro/tools/download_img/bootloader/u-boot.stm32 differ diff --git a/bearpi_hm_micro/tools/download_img/bootloader/u-boot_bootloader.stm32 b/bearpi_hm_micro/tools/download_img/bootloader/u-boot_bootloader.stm32 new file mode 100644 index 0000000..f6c0401 Binary files /dev/null and b/bearpi_hm_micro/tools/download_img/bootloader/u-boot_bootloader.stm32 differ diff --git a/bearpi_hm_micro/tools/download_img/flashlayout/bearpi-hm_micro.tsv b/bearpi_hm_micro/tools/download_img/flashlayout/bearpi-hm_micro.tsv new file mode 100644 index 0000000..5419a4f --- /dev/null +++ b/bearpi_hm_micro/tools/download_img/flashlayout/bearpi-hm_micro.tsv @@ -0,0 +1,9 @@ +#Opt Id Name Type IP Offset Binary +- 0x01 fsbl1-boot Binary none 0x0 arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-serialboot.stm32 +- 0x03 ssbl-boot Binary none 0x0 bootloader/u-boot_bootloader.stm32 +P 0x04 fsbl1 Binary mmc1 boot1 arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-trusted.stm32 +P 0x05 fsbl2 Binary mmc1 boot2 arm-trusted-firmware/tf-a-stm32mp157a-bearpi-hm_micro-mx-trusted.stm32 +PD 0x06 ssbl Binary mmc1 0x00080000 bootloader/u-boot.stm32 +P 0x21 boot System mmc1 0x00300000 kernel/OHOS_Image.stm32 +P 0x22 rootfs Binary mmc1 0x02280000 kernel/rootfs_vfat.img +P 0x23 userfs FileSystem mmc1 0x04280000 kernel/userfs_vfat.img \ No newline at end of file diff --git a/bearpi_hm_micro/tools/download_img/kernel/.keep b/bearpi_hm_micro/tools/download_img/kernel/.keep new file mode 100644 index 0000000..e69de29 diff --git a/bearpi_hm_micro/tools/hap_tools/bm b/bearpi_hm_micro/tools/hap_tools/bm new file mode 100644 index 0000000..2ccc228 Binary files /dev/null and b/bearpi_hm_micro/tools/hap_tools/bm differ diff --git a/bearpi_hm_micro/tools/hap_tools/hap_example/Airquality_1.0.0.hap b/bearpi_hm_micro/tools/hap_tools/hap_example/Airquality_1.0.0.hap new file mode 100644 index 0000000..7d60306 Binary files /dev/null and b/bearpi_hm_micro/tools/hap_tools/hap_example/Airquality_1.0.0.hap differ diff --git a/bearpi_hm_micro/tools/hap_tools/hap_example/Alarm_1.0.0.hap b/bearpi_hm_micro/tools/hap_tools/hap_example/Alarm_1.0.0.hap new file mode 100644 index 0000000..22c876f Binary files /dev/null and b/bearpi_hm_micro/tools/hap_tools/hap_example/Alarm_1.0.0.hap differ diff --git a/bearpi_hm_micro/tools/hap_tools/hap_example/Calculator_1.0.0.hap b/bearpi_hm_micro/tools/hap_tools/hap_example/Calculator_1.0.0.hap new file mode 100644 index 0000000..9a8623e Binary files /dev/null and b/bearpi_hm_micro/tools/hap_tools/hap_example/Calculator_1.0.0.hap differ diff --git a/bearpi_hm_micro/tools/hap_tools/hap_example/Led_1.0.0.hap b/bearpi_hm_micro/tools/hap_tools/hap_example/Led_1.0.0.hap new file mode 100644 index 0000000..f81aaf0 Binary files /dev/null and b/bearpi_hm_micro/tools/hap_tools/hap_example/Led_1.0.0.hap differ diff --git a/bearpi_hm_micro/tools/hap_tools/hap_example/Showcase_1.0.0.hap b/bearpi_hm_micro/tools/hap_tools/hap_example/Showcase_1.0.0.hap new file mode 100644 index 0000000..3258784 Binary files /dev/null and b/bearpi_hm_micro/tools/hap_tools/hap_example/Showcase_1.0.0.hap differ