/* * 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 "SixFrameHandle.h" #include "ILog.h" #include #include using std::placeholders::_1; using std::placeholders::_2; using std::placeholders::_3; // using std::placeholders::_4; const char *CJSON_INFO_STRING = "info"; const char *APP_GET_PRODUCT_INFO = "/app/getproductinfo"; const char *APP_GET_DEVICE_ATTR = "/app/getdeviceattr"; const char *APP_GET_MEDIA_INFO = "/app/getmediainfo"; const char *APP_GET_SD_CARD_INFO = "/app/getsdinfo"; const char *APP_GET_BATTERY_INFO = "/app/getbatteryinfo"; const char *APP_SET_DATE_TIME = "/app/setsystime"; const char *APP_SET_TIME_ZONE = "/app/settimezone"; const char *APP_UPLOAD_FILE = "/upload"; constexpr bool SET_REQUEST_RESPONSE = true; SixFrameHandle::SixFrameHandle() { mAppMonitor = std::make_shared(); mResquesHandleFunc[APP_GET_PRODUCT_INFO] = std::bind(&SixFrameHandle::RequestGetProductInfo, this, _1, _2, _3); mResquesHandleFunc[APP_GET_DEVICE_ATTR] = std::bind(&SixFrameHandle::RequestGetDeviceAttr, this, _1, _2, _3); mResquesHandleFunc[APP_GET_MEDIA_INFO] = std::bind(&SixFrameHandle::RequestGetMediaInfo, this, _1, _2, _3); mResquesHandleFunc[APP_GET_SD_CARD_INFO] = std::bind(&SixFrameHandle::RequestGetSdCardInfo, this, _1, _2, _3); mResquesHandleFunc[APP_GET_BATTERY_INFO] = std::bind(&SixFrameHandle::RequestGetBatteryInfo, this, _1, _2, _3); mResquesHandleFunc[APP_SET_DATE_TIME] = std::bind(&SixFrameHandle::RequestSetDateTime, this, _1, _2, _3); mResquesHandleFunc[APP_SET_TIME_ZONE] = std::bind(&SixFrameHandle::RequestSetTimeZone, this, _1, _2, _3); mResquesHandleFunc[APP_UPLOAD_FILE] = std::bind(&SixFrameHandle::RequestUpload, this, _1, _2, _3); // mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _); } void SixFrameHandle::RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) { const std::string urlStr2 = url; LogInfo("URL = %s\n", urlStr2.c_str()); size_t queryStartPos = urlStr2.find('?'); std::string command = ""; if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr2.length()) { command = urlStr2.substr(0, queryStartPos); } else { command = urlStr2.substr(0, urlStr2.length()); } LogInfo("command = %s\n", command.c_str()); // ExtractParamsFromUrl(urlStr2, paramsMap); RequestHandle2(command, urlStr2, responseHandle, context); } void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, ParseUrlResultFunc resultHandle, std::shared_ptr &context) { size_t queryStartPos = url.find('?'); if (queryStartPos != std::string::npos && queryStartPos + 1 < url.length()) { std::string paramsStr = url.substr(queryStartPos + 1); std::istringstream iss(paramsStr); std::string token; while (getline(iss, token, '&')) { size_t equalSignPos = token.find('='); if (equalSignPos != std::string::npos) { std::string key = token.substr(0, equalSignPos); std::string value = token.substr(equalSignPos + 1); LogInfo("url get [%s] = %s\n", key.c_str(), value.c_str()); resultHandle(key, value, context); // paramsMap.insert({key, value}); } } } } void SixFrameHandle::RequestHandle2(const std::string command, const std::string &url, ResponseHandle responseHandle, void *context) { auto result = mResquesHandleFunc.find(command); if (result != mResquesHandleFunc.end()) { (*result).second(url, responseHandle, context); } else { LogError("Unknown command.\n"); DoNothing(url, responseHandle, context); } } void SixFrameHandle::DoNothing(const std::string &url, ResponseHandle responseHandle, void *context) { // responseHandle("Unknown command.", context); } void SixFrameHandle::RequestGetProductInfo(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetProductInfo.\n"); char *resultStr = nullptr; AppGetProductInfo param; mAppMonitor->GetProductInfo(param); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL); ResponseGetProductInfo(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } void inline SixFrameHandle::ResponseGetProductInfo(cJSON *result, const AppGetProductInfo ¶m) { cJSON *info = nullptr; cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject()); cJSON_AddStringToObject(info, "model", param.mModel.c_str()); cJSON_AddStringToObject(info, "company", param.mCompany.c_str()); cJSON_AddStringToObject(info, "soc", param.mSoc.c_str()); cJSON_AddStringToObject(info, "sp", param.mSp.c_str()); } void SixFrameHandle::RequestGetDeviceAttr(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetDeviceAttr.\n"); char *resultStr = nullptr; AppGetDeviceAttr param; mAppMonitor->GetDeviceAttr(param); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL); ResponseGetDeviceAttr(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } void inline SixFrameHandle::ResponseGetDeviceAttr(cJSON *result, const AppGetDeviceAttr ¶m) { cJSON *info = nullptr; cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject()); cJSON_AddStringToObject(info, "uuid", param.mUUID.c_str()); cJSON_AddStringToObject(info, "softver", param.mSoftVersion.c_str()); cJSON_AddStringToObject(info, "otaver", param.mOtaVersion.c_str()); cJSON_AddStringToObject(info, "hwver", param.mHardwareVersion.c_str()); cJSON_AddStringToObject(info, "ssid", param.mSSID.c_str()); cJSON_AddStringToObject(info, "bssid", param.mBSSID.c_str()); cJSON_AddStringToObject(info, "camnum", param.mCameraNumber.c_str()); cJSON_AddStringToObject(info, "curcamid", param.mCurrentCameraID.c_str()); cJSON_AddStringToObject(info, "wifireboot", param.mWifiReboot.c_str()); } void SixFrameHandle::RequestGetMediaInfo(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetDeviceAttr.\n"); char *resultStr = nullptr; AppGetMeidaInfo param; mAppMonitor->GetMediaInfo(param); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL); ResponseGetMediaInfo(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } void inline SixFrameHandle::ResponseGetMediaInfo(cJSON *result, const AppGetMeidaInfo ¶m) { cJSON *info = nullptr; cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject()); cJSON_AddStringToObject(info, "uuid", param.mRtspUrl.c_str()); cJSON_AddStringToObject(info, "softver", param.mTransport.c_str()); cJSON_AddNumberToObject(info, "otaver", param.mPort); } void SixFrameHandle::RequestGetSdCardInfo(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetDeviceAttr.\n"); char *resultStr = nullptr; AppGetSdCardInfo param; mAppMonitor->GetSdCardInfo(param); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL); ResponseGetSdCardInfo(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } void inline SixFrameHandle::ResponseGetSdCardInfo(cJSON *result, const AppGetSdCardInfo ¶m) { cJSON *info = nullptr; cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject()); cJSON_AddNumberToObject(info, "status", static_cast(param.mStatus)); cJSON_AddNumberToObject(info, "free", param.mFree); cJSON_AddNumberToObject(info, "total", param.mTotal); } void SixFrameHandle::RequestGetBatteryInfo(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetDeviceAttr.\n"); char *resultStr = nullptr; AppGetBatteryInfo param; mAppMonitor->GetBatteryInfo(param); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL); ResponseGetBatteryInfo(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } void inline SixFrameHandle::ResponseGetBatteryInfo(cJSON *result, const AppGetBatteryInfo ¶m) { cJSON *info = nullptr; cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject()); cJSON_AddNumberToObject(info, "charge", static_cast(param.mChargeStatus)); cJSON_AddNumberToObject(info, "capacity", param.mCapacity); } AppSetDateTime inline SixFrameHandle::RequestSetDateTimeParse(const std::string &url) { auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr &parse) { std::shared_ptr> parseyImpl = std::dynamic_pointer_cast>(parse); if ("date" == key) { parseyImpl->mData = value; } }; std::shared_ptr parse = std::make_shared>(); ExtractParamsFromUrl(url, parseFunc, parse); std::shared_ptr> parseyImpl = std::dynamic_pointer_cast>(parse); if (14 != parseyImpl->mData.length()) { LogError("date parse failed.\n"); return AppSetDateTime(0, 0, 0, 0, 0, 0); } std::string yearStr = parseyImpl->mData.substr(0, 4); std::string monthStr = parseyImpl->mData.substr(4, 2); std::string dayStr = parseyImpl->mData.substr(6, 2); std::string hourStr = parseyImpl->mData.substr(8, 2); std::string minuteStr = parseyImpl->mData.substr(10, 2); std::string secondStr = parseyImpl->mData.substr(12, 2); unsigned int year = std::stoi(yearStr); unsigned int month = std::stoi(monthStr); unsigned int day = std::stoi(dayStr); unsigned int hour = std::stoi(hourStr); unsigned int minute = std::stoi(minuteStr); unsigned int second = std::stoi(secondStr); return AppSetDateTime(year, month, day, hour, minute, second); } void SixFrameHandle::RequestSetDateTime(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetDeviceAttr.\n"); char *resultStr = nullptr; AppSetDateTime param = RequestSetDateTimeParse(url); mAppMonitor->SetDateTime(param); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE); // ResponseGetBatteryInfo(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } int inline SixFrameHandle::RequestSetTimeZoneParse(const std::string &url) { auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr &parse) { std::shared_ptr> parseyImpl = std::dynamic_pointer_cast>(parse); if ("timezone" == key) { parseyImpl->mData = value; } }; std::shared_ptr parse = std::make_shared>(); ExtractParamsFromUrl(url, parseFunc, parse); std::shared_ptr> parseyImpl = std::dynamic_pointer_cast>(parse); if (2 <= parseyImpl->mData.length()) { LogError("date parse failed.\n"); return 0; } int zone = std::stoi(parseyImpl->mData); return zone; } void SixFrameHandle::RequestSetTimeZone(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetDeviceAttr.\n"); char *resultStr = nullptr; int zone = RequestSetTimeZoneParse(url); mAppMonitor->SetTimeZone(zone); cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE); // ResponseGetBatteryInfo(result, param); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } void SixFrameHandle::RequestUpload(const std::string &url, ResponseHandle responseHandle, void *context) { LogInfo("RequestUpload.\n"); char *resultStr = nullptr; AppUploadFile info("path", UploadCommand::UPGRADE_CPU); mAppMonitor->UploadFile(info); cJSON *result = MakeResponseResult(info.mResult); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } cJSON *SixFrameHandle::MakeResponseResult(const ResposeResult result, const bool requestSet) { const char *RESPONSE_RESULT = "result"; cJSON *resultCJSON = cJSON_CreateObject(); cJSON_AddNumberToObject(resultCJSON, RESPONSE_RESULT, static_cast(result)); if (false == requestSet) { return resultCJSON; } if (ResposeResult::SUCCESSFUL == result) { cJSON_AddStringToObject(resultCJSON, CJSON_INFO_STRING, "set success."); } else { cJSON_AddStringToObject(resultCJSON, CJSON_INFO_STRING, "set failed."); // TODO: what the failed string is? } return resultCJSON; } void SixFrameHandle::SetAppMonitor(std::shared_ptr &monitor) { if (monitor) { mAppMonitor = monitor; } else { LogError("SetAppMonitor failed.\n"); } }