Hi3861 OpenHarmony嵌入式應用入門--HTTPD

httpd 是 Apache HTTP Server 的守護進程名稱,Apache HTTP Server 是一種廣泛使用的開源網頁服務器軟件。

本項目是從LwIP中抽取的HTTP服務器代碼;

`Hi3861 SDK`中已經包含了一份預編譯的lwip,但沒有開啟HTTP服務器功能(靜態庫無法修改);

HTTP 服務器特性

目前已測試通過的功能有:

  1. 通過HTTP訪問靜態資源文件
  2. 通過cgi回調函數處理表單和頁面跳轉

靜態資源文件:

  • 本服務器實現不支持實際的服務端本地文件系統訪問;
  • 取而代之的是,一種簡單的虛擬文件系統:
    • \\third_party\httpd\src\http\fs.c 文件提供了一套文件系統接口,使用數組直接存儲文件數據
    • \\third_party\httpd\src\http\fsdata.c文件是由 makefsdata命令生成的虛擬文件系統元數據文件
    • makefsdata 命令行工具,用于將一個目錄下的靜態資源文件轉換為 fsdata.c文件

代碼編寫

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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("//build/lite/config/component/lite_component.gni")lite_component("demo") {features = [#"base_00_helloworld:base_helloworld_example",#"base_01_led:base_led_example",#"base_02_loopkey:base_loopkey_example",#"base_03_irqkey:base_irqkey_example",#"base_04_adc:base_adc_example",#"base_05_pwm:base_pwm_example",#"base_06_ssd1306:base_ssd1306_example",#"kernel_01_task:kernel_task_example",#"kernel_02_timer:kernel_timer_example",#"kernel_03_event:kernel_event_example",#"kernel_04_mutex:kernel_mutex_example",#"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",#"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",#"kernel_07_semaphore_for_count:kernel_semaphore_for_count_example",#"kernel_08_message_queue:kernel_message_queue_example",#"wifi_09_hotspot:wifi_hotspot_example",#"wifi_10_sta:wifi_sta_example",#"tcp_11_server:tcp_server_example",#"tcp_12_client:tcp_client_example",#"udp_13_server:udp_server_example",#"udp_14_client:udp_client_example",#"network_15_mqtt:network_mqtt_example",#"network_16_sntp:network_sntp_example","network_17_httpd:network_httpd_example",]
}

創建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_17_httpd文件夾

文件夾中創建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_17_httpd\BUILD.gn文件

# Copyright (c) 2020, HiHope Community.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.static_library("network_httpd_example") {sources = ["network_httpd_example.c","wifi_connecter.c","//third_party/httpd/src/core/altcp.c","//third_party/httpd/src/core/altcp_tcp.c","//third_party/httpd/src/http/fs.c","//third_party/httpd/src/http/httpd.c","//third_party/httpd/src/tcp_port.c",# "//third_party/httpd/src/pbuf_port.c",]defines = ["LWIP_DEBUG","HTTPD_DEBUG=0xA0","LWIP_DBG_TYPES_ON=LWIP_DBG_ON","LWIP_HTTPD_SSI"]include_dirs = ["//third_party/httpd/include","//third_party/httpd/include/lwip/apps","//foundation/communication/wifi_lite/interfaces/wifiservice","//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",]
}

文件夾中創建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_17_httpd\wifi_connecter.h文件,該頭文件包含wifi連接的宏。文件同network_16_sntp\wifi_connecter.h

文件夾中創建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_17_httpd\wifi_connecter.c文件,文件同network_16_sntp\wifi_connecter.c

文件夾中創建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\network_17_httpd\network_httpd_example.c文件

/** Copyright (c) 2020, HiHope Community.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice, this*    list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright notice,*    this list of conditions and the following disclaimer in the documentation*    and/or other materials provided with the distribution.** 3. Neither the name of the copyright holder nor the names of its*    contributors may be used to endorse or promote products derived from*    this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
// #include "wifiiot_at.h"#include "httpd.h"
#include "wifi_connecter.h"static int g_netId = -1;
#define STACK_SIZE  (4096)#ifdef LWIP_HTTPD_CGI
const char *HelloHandler(int iIndex, int nParams, char *pcParam[], char *pcValue[])
{printf("HelloHandler\r\n");printf("iIndex = %d\r\n", iIndex);for (int i = 0; i < nParams; i++) {printf("pcParam[%d] = '%s'\r\n", i, pcParam[i]);printf("pcValue[%d] = '%s'\r\n", i, pcValue[i]);}return "/index.html"; // forward to home page.
}static tCGI g_cgiHandlers[] = {{ "/hello", HelloHandler },
};
#endif#if LWIP_HTTPD_SSI
u16_t FooSsiHandler(
#if LWIP_HTTPD_SSI_RAWconst char* tag,
#else /* LWIP_HTTPD_SSI_RAW */int tag,
#endif /* LWIP_HTTPD_SSI_RAW */char *pcInsert, int iInsertLen
#if LWIP_HTTPD_SSI_MULTIPART, u16_t currentTagPart, u16_t *nextTagPart
#endif /* LWIP_HTTPD_SSI_MULTIPART */
#if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE, void *connectionState
#endif /* LWIP_HTTPD_FILE_STATE */
)
{printf("FooSsiHandler\r\n");#if LWIP_HTTPD_SSI_RAWprintf("tag = %s\r\n", tag);
#elseprintf("tag = %d\r\n", tag);
#endifprintf("insertLen = %d\r\n", iInsertLen);printf("insertText = %s\r\n", pcInsert);return 0;
}
#endifstatic void HttpdTask(void)
{WifiDeviceConfig config = {0};// 準備AP的配置參數// strcpy(config.ssid, PARAM_HOTSPOT_SSID);// strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);strcpy_s(config.ssid, WIFI_MAX_SSID_LEN, PARAM_HOTSPOT_SSID);strcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, PARAM_HOTSPOT_PSK);config.securityType = PARAM_HOTSPOT_TYPE;g_netId = ConnectToHotspot(&config);printf("netId = %d\r\n", g_netId);#ifdef LWIP_HTTPD_CGIhttp_set_cgi_handlers(g_cgiHandlers, LWIP_ARRAYSIZE(g_cgiHandlers));
#endif#ifdef LWIP_HTTPD_SSIhttp_set_ssi_handler(FooSsiHandler, NULL, 0);
#endifhttpd_init();
}static void HttpdEntry(void)
{osThreadAttr_t attr = {0};attr.name = "HttpdTask";attr.stack_size = STACK_SIZE;attr.priority = osPriorityNormal;if (osThreadNew(HttpdTask, NULL, &attr) == NULL) {printf("[HttpdEntry] create HttpdTask failed!\n");}
}
SYS_RUN(HttpdEntry);

使用build,編譯成功后,使用upload進行燒錄。

訪問網頁

串口輸出

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/45142.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/45142.shtml
英文地址,請注明出處:http://en.pswp.cn/web/45142.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

NiFi1.25版本HTTPS模式下RestAPI使用入門

Apache NiFi 是一個強大的數據流處理工具&#xff0c;通過其 REST API&#xff0c;用戶可以遠程管理和控制數據流處理器。本文將介紹如何使用 NiFi 1.25 版本HTTPS 模式下Rest API&#xff0c;包括獲取 token、獲取組件信息、啟動和停止組件、以及更改組件的調度頻率等操作。 …

Linux vim文本編輯器

Vim&#xff08;Vi IMproved&#xff09;是一個高度可配置的文本編輯器&#xff0c;它是Vi編輯器的增強版本&#xff0c;廣泛用于程序開發和系統管理。Vim不僅保留了Vi的所有功能&#xff0c;還增加了許多新特性&#xff0c;使其更加強大和靈活。 Vim操作模式 普通模式&#xf…

科普文:微服務之Apollo配置中心

1. 基本概念 由于Apollo 概念比較多&#xff0c;剛開始使用比較復雜&#xff0c;最好先過一遍概念再動手實踐嘗試使用。 1.1、背景 隨著程序功能的日益復雜&#xff0c;程序的配置日益增多&#xff0c;各種功能的開關、參數的配置、服務器的地址……對程序配置的期望值也越來…

在 C++中,如何使用智能指針來有效地管理動態分配的內存,并避免內存泄漏的問題?

在C中&#xff0c;可以使用智能指針來有效地管理動態分配的內存&#xff0c;避免內存泄漏的問題。下面是一些常用的智能指針類型和操作&#xff1a; std::unique_ptr&#xff1a; std::unique_ptr是C11引入的一種獨占式智能指針&#xff0c;它擁有對分配的內存的唯一所有權。當…

026-GeoGebra中級篇-曲線(2)_極坐標曲線、參數化曲面、分段函數曲線、分形曲線、復數平面上的曲線、隨機曲線、非線性動力系統的軌跡

除了參數曲線、隱式曲線和顯式曲線之外&#xff0c;還有其他類型的曲線表示方法。本篇主要概述一下極坐標曲線、參數化曲面、分段函數曲線、分形曲線、復數平面上的曲線、隨機曲線、和非線性動力系統的軌跡&#xff0c;可能沒有那么深&#xff0c;可以先了解下。 目錄 1. 極坐…

「網絡通信」HTTP 協議

HTTP &#x1f349;簡介&#x1f349;抓包工具&#x1f349;報文結構&#x1f34c;請求&#x1f34c;響應&#x1f34c;URL&#x1f95d;URL encode &#x1f34c;方法&#x1f34c;報文字段&#x1f95d;Host&#x1f95d;Content-Length & Content-Type&#x1f95d;User…

運動控制問題

第一類運動控制問題是指被控制對象的空間位置或軌跡運動發生改變的運動控制系統的控制問題。這類運動控制問題在理論上完全遵循牛頓力學定律和運動學原則。 1、運動控制問題 第1類運動控制的核心是研究被控對象的運動軌跡 、分析運動路徑、運動速度、加速度與時間的關系,常用…

深入解析PHP框架:Symfony框架詳解與應用

文章目錄 深入解析PHP框架&#xff1a;Symfony框架詳解與應用一、什么是Symfony&#xff1f;Symfony的優勢 二、Symfony的核心概念1. 控制器2. 路由3. 模板4. 服務容器5. 事件調度器 三、Symfony的主要功能1. 表單處理2. 數據庫集成3. 安全性4. 國際化5. 調試與日志 四、開發流…

記一次docker容器安裝MySQL,navicat無法連接報錯(10060錯誤)

今天在云服務器上使用docker部署mysql 8.0.11時&#xff0c;遇到了一個詭異的問題&#xff0c;在云服務器的docker容器內可以連接上mysql&#xff0c;然而在自己電腦上連接mysql時報錯&#xff1a;Can‘t connect to MySQL server on localhost (10060) 下面是網上搜尋的幾種可…

SpringMVC框架--個人筆記步驟總結

一、步驟 1.創建工程 2.加入springmvc依賴--pom.xml <!--springmvc依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </depend…

Camunda如何通過外部任務與其他系統自動交互

文章目錄 簡介流程圖外部系統pom.xmllogback.xml監聽類 啟動流程實例常見問題Public Key Retrieval is not allowed的解決方法java.lang.reflect.InaccessibleObjectException 流程圖xml 簡介 前面我們已經介紹了Camunda的基本操作、任務、表&#xff1a; Camunda組件與服務與…

Linux命令更新-Vim 編輯器

簡介 Vim 是 Linux 系統中常用的文本編輯器&#xff0c;功能強大、可擴展性強&#xff0c;支持多種編輯模式和操作命令&#xff0c;被廣泛應用于程序開發、系統管理等領域。 1. Vim 命令模式 Vim 啟動后默認進入命令模式&#xff0c;此時鍵盤輸入的命令將用于控制編輯器本身&…

Android ImageDecoder把瘦高/扁平大圖相當于fitCenter模式decode成目標小尺寸Bitmap,Kotlin

Android ImageDecoder把瘦高/扁平大圖相當于fitCenter模式decode成目標小尺寸Bitmap&#xff0c;Kotlin val sz Size(MainActivity.SIZE, MainActivity.SIZE)val src ImageDecoder.createSource(mContext?.contentResolver!!, uri)val bitmap ImageDecoder.decodeBitmap(sr…

【Playwright+Python】系列 Pytest 插件在Playwright中的使用

一、命令行使用詳解 使用 Pytest 插件在Playwright 中來編寫端到端的測試。 1、命令行執行測試 pytest --browser webkit --headed 2、使用 pytest.ini 文件配置 內容如下&#xff1a; [pytest] # Run firefox with UIaddopts --headed --browser firefox效果&#xff1…

云計算【第一階段(31)】PXE高效批量網絡裝機

一、系統安裝 1.1、系統裝機的三種引導方式 1. 硬盤 2. 光驅&#xff08; u 盤&#xff09; 3. 網絡啟動 pxe 1.2、系統安裝過程 加載boot loader Boot Loader 是在操作系統內核運行之前運行的一段小程序。通過這段小程序&#xff0c;我們可以初始化硬件設備、建立內存空間的映…

【CSS in Depth 2 精譯】3.1.2 邏輯屬性 + 3.1.3 用好邏輯屬性的簡寫形式

當前內容所在位置&#xff08;可進入專欄查看其他譯好的章節內容&#xff09; 第一章 層疊、優先級與繼承&#xff08;已完結&#xff09; 1.1 層疊1.2 繼承1.3 特殊值1.4 簡寫屬性1.5 CSS 漸進式增強技術1.6 本章小結 第二章 相對單位&#xff08;已完結&#xff09; 2.1 相對…

深入探討:CPU問題的深度分析與調優

引言 你是否曾經遇到過這樣的情況:系統運行突然變慢,用戶抱怨不斷,檢查后發現CPU使用率居高不下?這時候,你會如何解決?本文將詳細解析CPU問題的分析與調優方法,幫助你在面對類似問題時游刃有余。 案例分析:一次CPU性能瓶頸的解決過程 某知名互聯網公司在一次促銷活動…

《Python數據科學之一:初見數據科學與環境》

《Python數據科學之一&#xff1a;初見數據科學與環境》 歡迎來到“Python數據科學”系列的第一篇文章。在這個系列中&#xff0c;我們將通過Python的鏡頭&#xff0c;深入探索數據科學的豐富世界。首先&#xff0c;讓我們設置和理解數據科學的基本概念以及在開始任何數據科學項…

實變函數精解【4】

文章目錄 說明點集與測度可數集定義性質示例與有限集的關系應用 可列集定義種類不可列集性質應用與意義 有限集性質示例與無限集的區別應用 可數集&#xff08;Countable set&#xff09;和可列集&#xff08;Countably infinite set 或 Enumerable set&#xff09;可數集可列集…

怎樣把pptx課件轉換成word文檔

如果你還沒有安裝python-pptx和python-docx&#xff0c;請先運行以下命令&#xff1a; pip install python-pptx python-docx from pptx import Presentation from docx import Document import re# 函數&#xff1a;清理文本&#xff0c;移除特殊字符和控制字符 def clean…