diff --git a/middleware/AppManager/CMakeLists.txt b/middleware/AppManager/CMakeLists.txt new file mode 100644 index 00000000..873dbbcf --- /dev/null +++ b/middleware/AppManager/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/middleware/AppManager/README.md b/middleware/AppManager/README.md new file mode 100644 index 00000000..11fbc355 --- /dev/null +++ b/middleware/AppManager/README.md @@ -0,0 +1 @@ +# 手机APP对接 \ No newline at end of file diff --git a/middleware/AppManager/include/IAppManager.h b/middleware/AppManager/include/IAppManager.h new file mode 100644 index 00000000..cfc80171 --- /dev/null +++ b/middleware/AppManager/include/IAppManager.h @@ -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 +#include +#include +bool CreateAppManagerModule(void); +bool DestroyAppManagerModule(void); +class IAppManager +{ +public: + IAppManager() = default; + virtual ~IAppManager() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode Init(void); + virtual const StatusCode UnInit(void); +}; +#endif \ No newline at end of file diff --git a/middleware/AppManager/src/AppManager.cpp b/middleware/AppManager/src/AppManager.cpp new file mode 100644 index 00000000..a09d61dc --- /dev/null +++ b/middleware/AppManager/src/AppManager.cpp @@ -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 +const StatusCode AppManager::Init(void) { return CreateStatusCode(STATUS_CODE_OK); } +const StatusCode AppManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } diff --git a/middleware/AppManager/src/AppManager.h b/middleware/AppManager/src/AppManager.h new file mode 100644 index 00000000..28f7de4b --- /dev/null +++ b/middleware/AppManager/src/AppManager.h @@ -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> mLedManagers; +}; + +#endif diff --git a/middleware/AppManager/src/AppManagerMakePtr.cpp b/middleware/AppManager/src/AppManagerMakePtr.cpp new file mode 100644 index 00000000..a556ce48 --- /dev/null +++ b/middleware/AppManager/src/AppManagerMakePtr.cpp @@ -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(); + 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::GetInstance()->UnInit(); + IAppManager::GetInstance(&instance); + return true; +} +std::shared_ptr &AppManagerMakePtr::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + 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 &impl) +{ + auto tmp = std::make_shared(); + impl = tmp; + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/middleware/AppManager/src/AppManagerMakePtr.h b/middleware/AppManager/src/AppManagerMakePtr.h new file mode 100644 index 00000000..a00112df --- /dev/null +++ b/middleware/AppManager/src/AppManagerMakePtr.h @@ -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 +class AppManagerMakePtr +{ +public: + AppManagerMakePtr() = default; + virtual ~AppManagerMakePtr() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode CreateAppManager(std::shared_ptr &impl); +}; +#endif \ No newline at end of file diff --git a/middleware/AppManager/src/IAppManager.cpp b/middleware/AppManager/src/IAppManager.cpp new file mode 100644 index 00000000..a4f0ac12 --- /dev/null +++ b/middleware/AppManager/src/IAppManager.cpp @@ -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::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + 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); } \ No newline at end of file diff --git a/middleware/CMakeLists.txt b/middleware/CMakeLists.txt index 6b9db00f..b8baeeea 100644 --- a/middleware/CMakeLists.txt +++ b/middleware/CMakeLists.txt @@ -3,4 +3,5 @@ add_subdirectory(IpcConfig) add_subdirectory(DeviceManager) add_subdirectory(McuManager) add_subdirectory(McuAskBase) -add_subdirectory(MediaManager) \ No newline at end of file +add_subdirectory(MediaManager) +add_subdirectory(AppManager) \ No newline at end of file diff --git a/middleware/DeviceManager/CMakeLists.txt b/middleware/DeviceManager/CMakeLists.txt index 25318534..d33fa64d 100644 --- a/middleware/DeviceManager/CMakeLists.txt +++ b/middleware/DeviceManager/CMakeLists.txt @@ -15,8 +15,6 @@ include_directories( #link_directories( #) - - aux_source_directory(./src SRC_FILES) set(TARGET_NAME DeviceManager) diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp index a38d5ac3..c97eea37 100644 --- a/middleware/DeviceManager/src/DeviceManager.cpp +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -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 - 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(); diff --git a/middleware/DeviceManager/src/DeviceManager.h b/middleware/DeviceManager/src/DeviceManager.h index 2e43326f..faf003c7 100644 --- a/middleware/DeviceManager/src/DeviceManager.h +++ b/middleware/DeviceManager/src/DeviceManager.h @@ -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" diff --git a/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp b/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp index 2ce9dadf..15ce352c 100644 --- a/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp +++ b/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp @@ -19,12 +19,29 @@ #include 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(); diff --git a/utils/FxHttpServer/include/FxHttpServer.h b/utils/FxHttpServer/include/FxHttpServer.h index ca791d26..a3ecb011 100644 --- a/utils/FxHttpServer/include/FxHttpServer.h +++ b/utils/FxHttpServer/include/FxHttpServer.h @@ -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 } diff --git a/utils/FxHttpServer/src/FxHttpServer.c b/utils/FxHttpServer/src/FxHttpServer.c index a0cb38b1..d083bd72 100644 --- a/utils/FxHttpServer/src/FxHttpServer.c +++ b/utils/FxHttpServer/src/FxHttpServer.c @@ -18,30 +18,33 @@ #include 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); } \ No newline at end of file