mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Backup:FxHttpServer module.
This commit is contained in:
parent
24de40d2f6
commit
a224a5a387
54
middleware/AppManager/CMakeLists.txt
Normal file
54
middleware/AppManager/CMakeLists.txt
Normal file
|
@ -0,0 +1,54 @@
|
|||
|
||||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
||||
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
||||
|
||||
include_directories(
|
||||
./src
|
||||
./include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
${UTILS_SOURCE_PATH}/LedControl/include
|
||||
${HAL_SOURCE_PATH}/include
|
||||
)
|
||||
#do not rely on any other library
|
||||
#link_directories(
|
||||
#)
|
||||
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME AppManager)
|
||||
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
|
||||
|
||||
target_link_libraries(${TARGET_NAME} FxHttpServer StatusCode Log)
|
||||
|
||||
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
|
||||
add_custom_target(
|
||||
AppManager_code_check
|
||||
COMMAND ${CLANG_TIDY_EXE}
|
||||
-checks='${CLANG_TIDY_CHECKS}'
|
||||
--header-filter=.*
|
||||
--system-headers=false
|
||||
${SRC_FILES}
|
||||
${CLANG_TIDY_CONFIG}
|
||||
-p ${PLATFORM_PATH}/cmake-shell
|
||||
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/AppManager
|
||||
)
|
||||
file(GLOB_RECURSE HEADER_FILES *.h)
|
||||
add_custom_target(
|
||||
AppManager_code_format
|
||||
COMMAND ${CLANG_FORMAT_EXE}
|
||||
-style=file
|
||||
-i ${SRC_FILES} ${HEADER_FILES}
|
||||
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/AppManager
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET ${TARGET_NAME}
|
||||
PRE_BUILD
|
||||
COMMAND make AppManager_code_check
|
||||
COMMAND make AppManager_code_format
|
||||
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
|
||||
)
|
||||
endif()
|
||||
|
||||
define_file_name(${TARGET_NAME})
|
1
middleware/AppManager/README.md
Normal file
1
middleware/AppManager/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# 手机APP对接
|
32
middleware/AppManager/include/IAppManager.h
Normal file
32
middleware/AppManager/include/IAppManager.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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 IDEVICEMANAGER_H
|
||||
#define IDEVICEMANAGER_H
|
||||
#include "StatusCode.h"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
bool CreateAppManagerModule(void);
|
||||
bool DestroyAppManagerModule(void);
|
||||
class IAppManager
|
||||
{
|
||||
public:
|
||||
IAppManager() = default;
|
||||
virtual ~IAppManager() = default;
|
||||
static std::shared_ptr<IAppManager> &GetInstance(std::shared_ptr<IAppManager> *impl = nullptr);
|
||||
virtual const StatusCode Init(void);
|
||||
virtual const StatusCode UnInit(void);
|
||||
};
|
||||
#endif
|
19
middleware/AppManager/src/AppManager.cpp
Normal file
19
middleware/AppManager/src/AppManager.cpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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 "AppManager.h"
|
||||
#include "ILog.h"
|
||||
#include <vector>
|
||||
const StatusCode AppManager::Init(void) { return CreateStatusCode(STATUS_CODE_OK); }
|
||||
const StatusCode AppManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
|
31
middleware/AppManager/src/AppManager.h
Normal file
31
middleware/AppManager/src/AppManager.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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 APP_MANAGER_H
|
||||
#define APP_MANAGER_H
|
||||
#include "IAppManager.h"
|
||||
class AppManager : public IAppManager
|
||||
{
|
||||
public:
|
||||
AppManager() = default;
|
||||
virtual ~AppManager() = default;
|
||||
|
||||
const StatusCode Init(void) override;
|
||||
const StatusCode UnInit(void) override;
|
||||
|
||||
private:
|
||||
// std::vector<std::shared_ptr<LedManager>> mLedManagers;
|
||||
};
|
||||
|
||||
#endif
|
55
middleware/AppManager/src/AppManagerMakePtr.cpp
Normal file
55
middleware/AppManager/src/AppManagerMakePtr.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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 "AppManagerMakePtr.h"
|
||||
#include "AppManager.h"
|
||||
#include "ILog.h"
|
||||
bool CreateAppManagerModule(void)
|
||||
{
|
||||
auto instance = std::make_shared<IAppManager>();
|
||||
StatusCode code = AppManagerMakePtr::GetInstance()->CreateAppManager(instance);
|
||||
if (IsCodeOK(code)) {
|
||||
LogInfo("CreateAppManager is ok.\n");
|
||||
IAppManager::GetInstance(&instance);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool DestroyAppManagerModule(void)
|
||||
{
|
||||
auto instance = std::make_shared<IAppManager>();
|
||||
IAppManager::GetInstance()->UnInit();
|
||||
IAppManager::GetInstance(&instance);
|
||||
return true;
|
||||
}
|
||||
std::shared_ptr<AppManagerMakePtr> &AppManagerMakePtr::GetInstance(std::shared_ptr<AppManagerMakePtr> *impl)
|
||||
{
|
||||
static auto instance = std::make_shared<AppManagerMakePtr>();
|
||||
if (impl) {
|
||||
if (instance.use_count() == 1) {
|
||||
LogInfo("Instance changed succeed.\n");
|
||||
instance = *impl;
|
||||
}
|
||||
else {
|
||||
LogError("Can't changing the instance becase of using by some one.\n");
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
const StatusCode AppManagerMakePtr::CreateAppManager(std::shared_ptr<IAppManager> &impl)
|
||||
{
|
||||
auto tmp = std::make_shared<AppManager>();
|
||||
impl = tmp;
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
28
middleware/AppManager/src/AppManagerMakePtr.h
Normal file
28
middleware/AppManager/src/AppManagerMakePtr.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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 DEVICE_MANAGER_MAKE_PTR_H
|
||||
#define DEVICE_MANAGER_MAKE_PTR_H
|
||||
#include "IAppManager.h"
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
class AppManagerMakePtr
|
||||
{
|
||||
public:
|
||||
AppManagerMakePtr() = default;
|
||||
virtual ~AppManagerMakePtr() = default;
|
||||
static std::shared_ptr<AppManagerMakePtr> &GetInstance(std::shared_ptr<AppManagerMakePtr> *impl = nullptr);
|
||||
virtual const StatusCode CreateAppManager(std::shared_ptr<IAppManager> &impl);
|
||||
};
|
||||
#endif
|
32
middleware/AppManager/src/IAppManager.cpp
Normal file
32
middleware/AppManager/src/IAppManager.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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 "IAppManager.h"
|
||||
#include "ILog.h"
|
||||
std::shared_ptr<IAppManager> &IAppManager::GetInstance(std::shared_ptr<IAppManager> *impl)
|
||||
{
|
||||
static auto instance = std::make_shared<IAppManager>();
|
||||
if (impl) {
|
||||
if (instance.use_count() == 1) {
|
||||
LogInfo("Instance changed succeed.\n");
|
||||
instance = *impl;
|
||||
}
|
||||
else {
|
||||
LogError("Can't changing the instance becase of using by some one.\n");
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
const StatusCode IAppManager::Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
|
||||
const StatusCode IAppManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
|
|
@ -3,4 +3,5 @@ add_subdirectory(IpcConfig)
|
|||
add_subdirectory(DeviceManager)
|
||||
add_subdirectory(McuManager)
|
||||
add_subdirectory(McuAskBase)
|
||||
add_subdirectory(MediaManager)
|
||||
add_subdirectory(MediaManager)
|
||||
add_subdirectory(AppManager)
|
|
@ -15,8 +15,6 @@ include_directories(
|
|||
#link_directories(
|
||||
#)
|
||||
|
||||
|
||||
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME DeviceManager)
|
||||
|
|
|
@ -12,13 +12,11 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "DeviceManager.h"
|
||||
#include "ILog.h"
|
||||
#include "KeyManager.h"
|
||||
#include "LedManager.h"
|
||||
#include <vector>
|
||||
|
||||
const StatusCode DeviceManager::Init(void)
|
||||
{
|
||||
KeyManager::GetInstance()->Init();
|
||||
|
@ -27,7 +25,6 @@ const StatusCode DeviceManager::Init(void)
|
|||
LedManager::GetInstance()->StartTimer();
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
|
||||
const StatusCode DeviceManager::UnInit(void)
|
||||
{
|
||||
KeyManager::GetInstance()->UnInit();
|
||||
|
|
|
@ -12,8 +12,8 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef DEVICEMANAGER_H
|
||||
#define DEVICEMANAGER_H
|
||||
#ifndef DEVICE_MANAGER_H
|
||||
#define DEVICE_MANAGER_H
|
||||
#include "IDeviceManager.h"
|
||||
// #include "LedManager.h"
|
||||
|
||||
|
|
|
@ -19,12 +19,29 @@
|
|||
#include <thread>
|
||||
namespace FxHttpServerTest
|
||||
{
|
||||
static const char *gResponse = " {\
|
||||
\"result\":0,\
|
||||
\"info\":{\
|
||||
\"status\":0,\
|
||||
\"free\":12000,\
|
||||
\"total\":\"64000\"\
|
||||
}\
|
||||
}";
|
||||
void HttpHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context)
|
||||
{
|
||||
if (url) {
|
||||
LogInfo("url = %s\n", url);
|
||||
if (memcmp(url, "/set", strlen("/set")) == 0) {
|
||||
responseHandle(gResponse, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
// ../output_files/test/bin/FxHttpServerTest --gtest_filter=FxHttpServerTest.Demo
|
||||
TEST(FxHttpServerTest, Demo)
|
||||
{
|
||||
CreateLogModule();
|
||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||
FxHttpServerInit();
|
||||
FxHttpServerInit(HttpHandle);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 10));
|
||||
FxHttpServerUnInit();
|
||||
ILogUnInit();
|
||||
|
|
|
@ -18,7 +18,9 @@
|
|||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
StatusCode FxHttpServerInit(void);
|
||||
typedef void (*ResponseHandle)(const char *, void *);
|
||||
typedef void (*HttpHandleCallback)(const char *, const unsigned int, ResponseHandle, void *);
|
||||
StatusCode FxHttpServerInit(HttpHandleCallback httpHandle);
|
||||
StatusCode FxHttpServerUnInit(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -18,30 +18,33 @@
|
|||
#include <string.h>
|
||||
static struct http_server_s *server = NULL;
|
||||
static struct http_server_s *poll_server = NULL;
|
||||
|
||||
static int request_target_is(struct http_request_s *request, char const *target)
|
||||
static HttpHandleCallback gHttpHandle = NULL;
|
||||
static void response_handle(const char *responseStr, void *context)
|
||||
{
|
||||
http_string_t url = http_request_target(request);
|
||||
LogInfo("sssssssssssssssssssssss url.buf = %s\n", url.buf);
|
||||
int len = strlen(target);
|
||||
return len == url.len && memcmp(url.buf, target, url.len) == 0;
|
||||
struct http_response_s *response = (struct http_response_s *)context;
|
||||
if (NULL != responseStr) {
|
||||
http_response_header(response, "Content-Type", "text/plain");
|
||||
http_response_body(response, responseStr, strlen(responseStr));
|
||||
}
|
||||
}
|
||||
static void handle_request(struct http_request_s *request)
|
||||
{
|
||||
http_request_connection(request, HTTP_AUTOMATIC);
|
||||
struct http_response_s *response = http_response_init();
|
||||
http_response_status(response, 200);
|
||||
if (request_target_is(request, "/set")) {
|
||||
LogInfo("======================================== set\n");
|
||||
}
|
||||
http_response_header(response, "Content-Type", "text/plain");
|
||||
http_response_body(response, "RESPONSE", sizeof("RESPONSE") - 1);
|
||||
http_string_t url = http_request_target(request);
|
||||
gHttpHandle(url.buf, url.len, response_handle, response);
|
||||
http_respond(request, response);
|
||||
}
|
||||
StatusCode FxHttpServerInit(void)
|
||||
StatusCode FxHttpServerInit(HttpHandleCallback httpHandle)
|
||||
{
|
||||
server = http_server_init(8080, handle_request);
|
||||
poll_server = http_server_init(8081, handle_request);
|
||||
if (NULL == httpHandle) {
|
||||
LogError("FxHttpServerInit failed. Callback function is nullptr.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
gHttpHandle = httpHandle;
|
||||
http_server_listen_poll(poll_server);
|
||||
http_server_listen(server);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
|
@ -49,6 +52,8 @@ StatusCode FxHttpServerInit(void)
|
|||
StatusCode FxHttpServerUnInit(void)
|
||||
{
|
||||
free(server);
|
||||
server = NULL;
|
||||
free(poll_server);
|
||||
poll_server = NULL;
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
Loading…
Reference in New Issue
Block a user