#include "precompile.h" #include "sp_mod.h" #include "svc.h" #include "app.h" #include "SpBase.h" #include "SpHelper.h" #include "sp_btr.h" #include "sp_gui.h" #include "sp_dbg_export.h" #include "sp_iom.h" #include "sp_svc.h" #include "sp_def.h" #include "sp_cfg.h" #include "sp_rpc.h" #include "sp_env.h" #include #include #include "osutil.h" #include "fileutil.h" #include "toolkit.h" #include #include #include #include #include "StartUpBase.h" static void shutdown_app(void* arg) { app_stop(EXIT_FROM_REBOOT_CMD); } void on_bluesceen_display(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { app_t *app = get_app_instance(); char *msg = NULL; //int iBlueScreen=0; //iobuffer_format_read(*info_pkt, "s4", &msg, &iBlueScreen); iobuffer_format_read(*info_pkt, "s", &msg); #if defined(_MSC_VER) sp_gui_show_running_info(app->bsc_gui, msg, GUI_DISPLAY_ELEM_BLUESCREEN); #else if (app->bsc_gui != NULL) app->bsc_gui->show_running_info(app->bsc_gui->gui_inst, msg, GUI_DISPLAY_ELEM_BLUESCREEN); #endif //_MSC_VER FREE(msg); } ///*TODO(80374374@3/23/2023): 抛送内容格式的处理,看与操作系统是否有关 */ void on_fatal_error_display(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { app_t *app = get_app_instance(); char *msg = NULL; int level = 0; #if defined(_MSC_VER) iobuffer_format_read(*info_pkt, "s", &msg); sp_gui_show_running_info(app->bsc_gui, msg, GUI_DISPLAY_ELEM_FATAL_ERROR); #else iobuffer_format_read(*info_pkt, "4s", &level, &msg); if (app->bsc_gui != NULL) { app->bsc_gui->show_running_info(app->bsc_gui->gui_inst, msg, GUI_DISPLAY_ELEM_FATAL_ERROR); } #endif //_MSC_VER FREE(msg); } void on_startup_info_display(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { app_t *app = get_app_instance(); char *msg = NULL; iobuffer_format_read(*info_pkt, "s", &msg); #if defined(_MSC_VER) sp_gui_show_running_info(app->bsc_gui, msg, GUI_DISPLAY_ELEM_STARTUP_INFO); #else if (app->bsc_gui != NULL) app->bsc_gui->show_running_info(app->bsc_gui->gui_inst, msg, GUI_DISPLAY_ELEM_STARTUP_INFO); #endif //_MSC_VER FREE(msg); } void on_bluesceen_undisplay(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { app_t *app = get_app_instance(); #if defined(_MSC_VER) sp_gui_undisplay(app->bsc_gui); #else if (app->bsc_gui != NULL) app->bsc_gui->hide(app->bsc_gui->gui_inst); #endif //_MSC_VER } #if defined(_MSC_VER) ///*TODO(80374374@3/23/2023): 移动到 libtoolkit */ static BOOL RestartWindows(BOOL fShutdownOnly = FALSE) { HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure BOOL fResult; // system shutdown flag if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get proc token fail GLE=%u", GetLastError()); return FALSE; } LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0)) { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("adjust proc token privilege fail GLE=%u", GetLastError()); return FALSE; } fResult = InitiateSystemShutdown( NULL, // shut down local computer NULL, // message for user 0, // time-out period, in seconds FALSE, // ask user to close apps !fShutdownOnly); // reboot after shutdown if (!fResult) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("request windows reboot fail GLE=%u", GetLastError()); return FALSE; } tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); return TRUE; } #endif //_MSC_VER DWORD getExePath(char *exePath) { char pathbuf[MAX_PATH] = ""; int pathlen = ::GetModuleFileName(NULL, pathbuf, MAX_PATH); while (pathlen > 0) { if (pathbuf[pathlen--] == SPLIT_SLASH) break; } pathbuf[++pathlen] = '\0'; sprintf(exePath, "%s", pathbuf); return strlen(exePath); } #define RESTART_FRAMEWORK 1 #define SHUTDOWN_FRAMEWORK 2 #define RESTART_PC 3 #define SHUTDOWN_PC 4 static int KickoffSpRestartInner(int options) { #ifdef _WIN32 if (options == RESTART_PC || options == SHUTDOWN_PC) { return RestartWindows(options == SHUTDOWN_PC) ? 0 : -1; } else { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); // Start the child process. char exepath[MAX_PATH] = ""; getExePath(exepath); CSimpleStringA csRestart, csVerPath, csAll, csBlank(" "), csReFlag("r"); csRestart = CSimpleStringA(exepath) + "\\sprestart.exe"; sp_env_t* env = sp_get_env(); csVerPath = env->dir->root_ver_path; csVerPath += "\\VTM.exe"; if (options == SHUTDOWN_FRAMEWORK) csReFlag = "n"; csAll = csRestart + csBlank + csVerPath + csBlank + csReFlag; DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("allpath[%s]", (LPCTSTR)csAll); LPTSTR szCmdline = _strdup(csAll); if (!CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateProcess failed (%d).", GetLastError()); return -1; } DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateProcess Success PID:%d.", pi.dwProcessId); DWORD dwErr = GetLastError(); // Close process and thread handles. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } #else sp_env_t* env = sp_get_env(); if (env && env->dir) { tk_process_t* process = NULL; tk_process_option_t option; char app[MAX_PATH] = { '\0' }; char shell_scripts[MAX_PATH] = { '\0' }; sprintf(shell_scripts, "%s/%s", env->dir->root_ver_path, "spexplorer.sh"); if (options == RESTART_FRAMEWORK) { if (env && env->cfg && env->cfg->args && env->cfg->args->arguments) { sprintf(app, "%s %s -Rwait", shell_scripts, env->cfg->args->arguments); } else { sprintf(app, "%s -Rwait", shell_scripts); } } else if (options == SHUTDOWN_FRAMEWORK) { sprintf(app, "%s --shutdown", shell_scripts); } else if (options == RESTART_PC) { sprintf(app, "%s --reboot", shell_scripts); } else if (options == SHUTDOWN_PC) { sprintf(app, "%s --systemoff", shell_scripts); } else { DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("invalid restart pararm: %d", options); return -1; } option.exit_cb = NULL; option.file = NULL; option.flags = 0; option.params = app; char szldPath[1024] = { '\0' }; size_t szldLen = 1023; const int ldRet = toolkit_getenv("LD_LIBRARY_PATH", szldPath, &szldLen); if (ldRet == 0) { DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get library path: %s", szldPath); toolkit_unsetenv("LD_LIBRARY_PATH"); } else { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GetEnv of LD_LIBRARY_PATH failed: %s", toolkit_strerror(ldRet)); } if (0 == process_spawn(&option, &process)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create new process succ pid=%u! %s", process->pid, app); FREE(process); return 0; } else { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("create new process failed! %s", app); if (ldRet == 0) { toolkit_setenv("LD_LIBRARY_PATH", szldPath); } } } else { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Env has no any dir information!"); } return -1; #endif //_WIN32 } int KickoffSpRestart(bool bRestart) { return KickoffSpRestartInner(bRestart ? RESTART_FRAMEWORK : SHUTDOWN_FRAMEWORK); } int KickoffSpRestartPC(bool bRestart) { return KickoffSpRestartInner(bRestart ? RESTART_PC : SHUTDOWN_PC); } #if 1 static const int max_entity_total_about_num = 100; static const int each_thread_close_entity_num = 10; int total_active_module_num = 0; int entity_close_thread_num = 0; toolkit_barrier_t blocker; void hare(void* arg) { const int startpos = (int)(intptr_t)((int*)arg); sp_env_t* env = sp_get_env(); bool bAllSuc = true; sp_mod_mgr_t* mod_mgr = env->mod_mgr; sp_mod_t* pos; int skipnum = 0, dealnum = 0; sp_dbg_debug("threshold: %d", startpos); list_for_each_entry(pos, sp_mod_mgr_get_module_list_head(mod_mgr), sp_mod_t, entry) { if (pos->closing) { if (++skipnum < startpos) continue; if (++dealnum > each_thread_close_entity_num) break; sp_entity_t* ent; list_for_each_entry(ent, &pos->entity_list, sp_entity_t, entry) { const int state = ent->state; if (state != EntityState_NoStart && state != EntityState_Close && state != EntityState_Killed) { sp_dbg_debug("to stop entity %s ...", ent->cfg->name); int result = sp_mod_mgr_stop_entity(env->mod_mgr, ent->cfg->idx, SP_SHELL_SVC_ID, CloseCause_ShellQuit); if (result == 0) { sp_dbg_info("to stop entity %s succ, pre state: %s, curr state: %s", ent->cfg->name, SpStrEntityState((EntityStateEnum)state), SpStrEntityState((EntityStateEnum)ent->state)); } else { sp_dbg_error("to stop entity %s failed !! %s, pre state: %s, curr state: %s", ent->cfg->name, SpStrError((ErrorCodeEnum)result), SpStrEntityState((EntityStateEnum)state), SpStrEntityState((EntityStateEnum)ent->state)); bAllSuc = false; } } else { sp_dbg_info("ignore to stop %s, curr state: %d", ent->cfg->name, state); } } } } if (!bAllSuc) { sp_dbg_error("some booted up entities has been closed failed!"); } else { sp_dbg_info("all booted up entities has been closed succ."); } toolkit_barrier_wait(&blocker); } int stop_all_stated_entity_sync() { sp_env_t* env = sp_get_env(); bool bAllSuc = true; sp_mod_mgr_t* mod_mgr = env->mod_mgr; sp_mod_t* pos; list_for_each_entry(pos, sp_mod_mgr_get_module_list_head(mod_mgr), sp_mod_t, entry) { if (pos->loaded) { sp_entity_t* ent; list_for_each_entry(ent, &pos->entity_list, sp_entity_t, entry) { const int state = ent->state; if (state != EntityState_NoStart && state != EntityState_Close && state != EntityState_Killed) { int result = sp_mod_mgr_stop_entity(env->mod_mgr, ent->cfg->idx, SP_SHELL_SVC_ID, CloseCause_ShellQuit); if (result == 0) { sp_dbg_debug("to stop entity succ: %s, pre state: %s, curr state: %s", ent->cfg->name, result, SpStrEntityState((EntityStateEnum)state), SpStrEntityState((EntityStateEnum)ent->state)); } else { sp_dbg_error("to stop entity failed: %s !! %d, pre state: %s, curr state: %s", ent->cfg->name, SpStrError((ErrorCodeEnum)result), SpStrEntityState((EntityStateEnum)state), SpStrEntityState((EntityStateEnum)ent->state)); bAllSuc = false; } } else { sp_dbg_info("ignore to stop %s, curr state: %d", ent->cfg->name, state); } } } } if (!bAllSuc) { sp_dbg_warn("======================================================"); sp_dbg_warn("!!!!!! 部分实体退出失败 !!!!!!"); sp_dbg_warn("======================================================"); return -1; } else { sp_dbg_info("all booted up entities has been closed succ."); return 0; } } int stop_all_stated_entity(void* param) { sp_env_t* env = sp_get_env(); sp_mod_mgr_t* mod_mgr = env->mod_mgr; sp_mod_t* pos; total_active_module_num = 0; sp_dbg_info(">>>>>> start to stop all started entities <<<<<<"); list_for_each_entry(pos, sp_mod_mgr_get_module_list_head(mod_mgr), sp_mod_t, entry) { if (pos->loaded) { ++total_active_module_num; pos->closing = 1; } } entity_close_thread_num = total_active_module_num / each_thread_close_entity_num; if (total_active_module_num % each_thread_close_entity_num) entity_close_thread_num++; sp_dbg_info("total moudule: %d, thread num: %d, each thread close module num: %d", total_active_module_num, entity_close_thread_num, each_thread_close_entity_num); if (total_active_module_num > max_entity_total_about_num) { return stop_all_stated_entity_sync(); } toolkit_barrier_init(&blocker, entity_close_thread_num + 1); toolkit_thread_t threads[max_entity_total_about_num]; for (int i = 0; i < entity_close_thread_num; ++i) { int nums = each_thread_close_entity_num * i; toolkit_thread_create(&threads[i], hare, (void*)(uintptr_t)nums); } const DWORD begin = GetTickCount(); sp_dbg_info("wait working thread done..."); toolkit_barrier_wait(&blocker); toolkit_barrier_destroy(&blocker); const DWORD end = GetTickCount(); sp_dbg_info(">>>>>> working thread done !! consumed: %u ms <<<<<<", end - begin); return 0; } #else int stop_all_stated_entity(void* param) { sp_dbg_warn("%s: do nothing!!", __FUNCTION__); return 0; } #endif void on_machine_reboot(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { static int retry = 0; int result = 0; int reason(0), way(0); sp_env_t *env = sp_get_env(); sp_cfg_t *cfg = env->cfg; //stop from duplicating if(retry != 0) { return; } retry = 1; iobuffer_read(*info_pkt, IOBUF_T_I4, &reason, 0); iobuffer_read(*info_pkt, IOBUF_T_I4, &way, 0); const RebootWayEnum eRebootWay = (RebootWayEnum)way; const RebootTriggerEnum eRebootTrigger = (RebootTriggerEnum)reason; DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("now reboot: reason = %s, way = %s", SpStrRebootTrigger(eRebootTrigger), SpStrRebootWay(eRebootWay)); CSimpleStringA strPath = env->dir->root_runinfo_path; strPath += SPLIT_SLASH_STR "BootLog"; sp_btr_write_on_shutdown(strPath, env->btr_ctx, reason, way); if (eRebootWay == RebootWay_Framework) { if (eRebootTrigger == RebootTrigger_DeadForever) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shutdown framework now"); result = KickoffSpRestart(false); } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reboot framework now"); result = KickoffSpRestart(true); } } else if (eRebootWay == RebootWay_OS || eRebootWay == RebootWay_Power) /** UPS 下线后,Power类型也兼容到框架 [Gifur@2024511]*/ { if (eRebootTrigger != RebootTrigger_DeadForever) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reboot operation system now"); result = KickoffSpRestartPC(true); //result = osutil_restart_system(); } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shutdown operation system now"); result = KickoffSpRestartPC(false); //result = osutil_shutdown_system(); } } if (result == 0) { stop_all_stated_entity(NULL); app_t* app = get_app_instance(); sp_iom_post_quit(app->iom); } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Stop from quit the framework, bcz preceed task failed!"); } } void on_entity_quit(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { int entity_id; sp_env_t *env = sp_get_env(); iobuffer_read(*info_pkt, IOBUF_T_I4, &entity_id, NULL); sp_mod_mgr_stop_entity(env->mod_mgr, entity_id, svc_id, CloseCause_Self); } void on_output_console_on(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set output console on"); auto app = get_app_instance(); #if defined(_MSC_VER) sp_gui_display(app->bsc_gui); #else if (app->bsc_gui != NULL) app->bsc_gui->display(app->bsc_gui->gui_inst); #endif //_MSC_VER } void on_output_console_off(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set output console off"); auto app = get_app_instance(); #if defined(_MSC_VER) sp_gui_undisplay(app->bsc_gui); #else if (app->bsc_gui != NULL) app->bsc_gui->hide(app->bsc_gui->gui_inst); #endif //_MSC_VER } void on_start_upload_log(sp_rpc_server_t* server, int epid, int svc_id, int call_type, iobuffer_t** info_pkt) { static bool isStarted = false; if (isStarted) return; sp_env_t* env = sp_get_env(); char* endpoint = NULL, * topicSys = NULL, * topicUser = NULL, * topicBeidou = NULL, * bussSys = NULL, * bussUser = NULL , *vtmWeb = NULL; iobuffer_format_read(*info_pkt, "sssssss", &endpoint, &topicSys, &topicUser, &topicBeidou, &bussSys, &bussUser, &vtmWeb); char dstVer[SP_MAX_VER_LEN] = ""; auto ret = sp_cfg_getVer(dstVer); if (ret != Error_Succeed) sprintf_s(dstVer, SP_MAX_VER_LEN, "Unknown"); auto logSender = create_log_producer_send(endpoint, topicSys, topicUser, topicBeidou, bussSys, bussUser, vtmWeb, env->cfg->root_ini->terminal_no, dstVer, "", 10000); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create log produce sender %s-%s-%s-%s-%s-%s-%s %s", endpoint, topicSys, topicUser, topicBeidou, bussSys, bussUser, vtmWeb, logSender == NULL ? "failed" : "success"); toolkit_free(endpoint); toolkit_free(topicSys); toolkit_free(topicUser); toolkit_free(topicBeidou); toolkit_free(bussSys); toolkit_free(bussUser); toolkit_free(vtmWeb); } void on_terminal_stage_change(sp_rpc_server_t* server, int epid, int svc_id, int call_type, iobuffer_t** info_pkt) { int result = 0; int newStage(0), oldStage(0); sp_env_t* env = sp_get_env(); sp_cfg_t* cfg = env->cfg; iobuffer_read(*info_pkt, IOBUF_T_I4, &newStage, 0); iobuffer_read(*info_pkt, IOBUF_T_I4, &oldStage, 0); const AppBootStateEnum eNewStage = (AppBootStateEnum)newStage; const AppBootStateEnum eOldStage = (AppBootStateEnum)oldStage; DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("change teriminal state from %s to %s", SpStrAppBootState(eOldStage), SpStrAppBootState(eNewStage)); auto app = get_app_instance(); #if defined(RVC_OS_LINUX) if (app->bsc_gui != NULL) { app->bsc_gui->post_message(app->bsc_gui->gui_inst, GUI_DISPLAY_ELEM_LOG_OTHER, newStage); } #else ///*TODO(80374374@3/24/2023): */ #endif //RVC_OS_LINUX } void on_start_update_token(sp_rpc_server_t* server, int epid, int svc_id, int call_type, iobuffer_t** info_pkt) { sp_env_t* env = sp_get_env(); char* channelId = NULL, * token = NULL; iobuffer_format_read(*info_pkt, "ss", &channelId, &token); auto t_cfg = sp_get_env()->cfg->shell_ini; if (t_cfg->channelId != NULL) { shm_free(t_cfg->channelId); t_cfg->channelId = NULL; } if (t_cfg->token != NULL) { shm_free(t_cfg->token); t_cfg->token = NULL; } t_cfg->channelId = shm_strdup(channelId); t_cfg->token = shm_strdup(token); toolkit_free(channelId); toolkit_free(token); } void on_fresh_env(sp_rpc_server_t* server, int epid, int svc_id, int call_type, iobuffer_t** info_pkt) { LOG_FUNCTION(); sp_env_t* env = sp_get_env(); char path[2049]; size_t size; #if defined(_MSC_VER) const char* name = "PATH"; #else const char* name = "LD_LIBRARY_PATH"; #endif //_MSC_VER memset(path, '\0', sizeof(path)); size = 2048; toolkit_getenv(name, path, &size); CAutoArray values = CSimpleStringA(path).Split(ENV_SEP_CHAR); CSimpleStringA strNewPath(true); CSimpleStringA strPrefix(env->dir->dep_base_path); for (int i = 0; i < values.GetCount(); ++i) { if (!values[i].IsStartWith(strPrefix)) { if (!strNewPath.IsNullOrEmpty()) strNewPath += ENV_SEP_STR; strNewPath += values[i]; } } if (env->dir->dep_ver_path != NULL) { if (!strNewPath.IsNullOrEmpty()) strNewPath += ENV_SEP_STR; strNewPath += env->dir->dep_ver_path; } char vendorName[128]; size = 127; memset(vendorName, '\0', sizeof(vendorName)); toolkit_getenv("RVC_MANUFACTURER_NAME", vendorName, &size); if (strlen(vendorName) > 0) { CSimpleStringA strSubDep(env->dir->dep_ver_path); strSubDep += SPLIT_SLASH_STR; strSubDep += vendorName; if (!strNewPath.IsNullOrEmpty()) strNewPath += ENV_SEP_STR; strNewPath += strSubDep; } toolkit_setenv(name, strNewPath.GetData()); } typedef struct { sp_rpc_server_t* server; int epid; int rpc_id; int svc_id; int Entity_id; char cmdline[1024]; }EntityStartParam; static unsigned int __stdcall on_entity_startThread(void* param) { EntityStartParam *dst = (EntityStartParam*)param; auto ans_pkt = on_entity_start(dst->svc_id, dst->Entity_id, dst->cmdline); if (ans_pkt) { int rc = sp_rpc_server_send_answer(dst->server, dst->epid, dst->svc_id, dst->rpc_id, &ans_pkt); if (rc != 0) DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("send answer failed!"); if (ans_pkt) iobuffer_dec_ref(ans_pkt); } return 0; } iobuffer_t* on_entity_startEx(sp_rpc_server_t* server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t** req_pkt) { int entity_id; char* cmdline = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("handle start entityEx req: %d", entity_id); iobuffer_format_read(*req_pkt, "s", &cmdline); EntityStartParam *param = new EntityStartParam(); param->server = server; param->epid = epid; param->rpc_id = rpc_id; param->svc_id = svc_id; param->Entity_id = entity_id; sprintf(param->cmdline, "%s", cmdline); FREE(cmdline); CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&on_entity_startThread, param, 0, NULL)); return NULL; } iobuffer_t *on_entity_start(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); int entity_id; int result; char *cmdline = NULL; iobuffer_t *ans_pkt = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); sp_entity_t* ent; ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, entity_id); std::string entityName; if (ent) entityName = ent->mod->cfg->name; addStartupLog(entityName.c_str(), entity_id); addStartupStep(entity_id, "handle_req", "", 0, 0, 0); iobuffer_format_read(*req_pkt, "s", &cmdline); result = sp_mod_mgr_start_entity(env->mod_mgr, entity_id, cmdline, svc_id); FREE(cmdline); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); return ans_pkt; } iobuffer_t* on_entity_start(int svc_id, int entity_id, const char* cmdline) { sp_env_t* env = sp_get_env(); int result; iobuffer_t* ans_pkt = NULL; result = sp_mod_mgr_start_entity(env->mod_mgr, entity_id, cmdline, svc_id); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); return ans_pkt; } iobuffer_t *on_entity_stop(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); int entity_id; int result; int cause_code; iobuffer_t *ans_pkt = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); if (svc_id == SP_SHELL_SVC_ID) { cause_code = CloseCause_ShellQuit; } else if (svc_id == SP_INVALID_SVC_ID) { cause_code = CloseCause_Lost; } else if (svc_id == entity_id) { cause_code = CloseCause_Self; } else { cause_code = CloseCause_Other; } DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("handle stop entity req: %d", entity_id); result = sp_mod_mgr_stop_entity(env->mod_mgr, entity_id, svc_id, cause_code); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); return ans_pkt; } iobuffer_t *on_entity_continue(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); int entity_id; int result; iobuffer_t *ans_pkt = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("handle continue entity req: %d", entity_id); result = sp_mod_mgr_continue_entity(env->mod_mgr, entity_id, svc_id); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); return ans_pkt; } iobuffer_t *on_entity_pause(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); int entity_id; int result; iobuffer_t *ans_pkt = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("handle pause entity req: %d", entity_id); result = sp_mod_mgr_pause_entity(env->mod_mgr, entity_id, svc_id); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); return ans_pkt; } iobuffer_t *on_entity_test(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); int entity_id; int test_type; int result; int statistic; iobuffer_t *ans_pkt = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); iobuffer_read(*req_pkt, IOBUF_T_I4, &test_type, NULL); statistic = 0; //sp_dbg_debug("handle test entity req: %d", entity_id); result = sp_mod_mgr_test_entity(env->mod_mgr, entity_id, svc_id, test_type, &statistic); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &statistic, 0); return ans_pkt; } iobuffer_t *on_entity_termination(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); int entity_id; int result; iobuffer_t *ans_pkt = NULL; iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("handle terminate entity req: %d", entity_id); result = sp_mod_mgr_terminate_entity(env->mod_mgr, entity_id, svc_id); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); return ans_pkt; } iobuffer_t *on_get_bcast_receiver(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt) { sp_env_t *env = sp_get_env(); iobuffer_t *ans_pkt = NULL; int result = 0; int i; array_header_t *arr = sp_bcm_daemon_get_receiver(get_app_instance()->bcm_daemon, svc_id); ans_pkt = iobuffer_create(-1, -1); iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &arr->nelts, 0); for (i = 0; i < arr->nelts; ++i) { sp_bcm_receiver_t *recver = ARRAY_IDX(arr, i, sp_bcm_receiver_t*); iobuffer_write(ans_pkt, IOBUF_T_I8, &recver->id, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &recver->receiver_id, 0); iobuffer_write(ans_pkt, IOBUF_T_STR, &recver->param, -1); } sp_bcm_daemon_free_receiver_array(arr); return ans_pkt; } iobuffer_t* on_try_update_VTMERRMSG(sp_rpc_server_t* server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t** req_pkt) { iobuffer_t* ans_pkt = iobuffer_create(-1, -1); auto ret = sp_TryUpdate_vtm_err_msg(); iobuffer_write(ans_pkt, IOBUF_T_I4, &ret, 0);//需要sysError和UserError iobuffer_write(ans_pkt, IOBUF_T_I4, &ret, 0); return ans_pkt; } iobuffer_t* on_try_update_cfg(sp_rpc_server_t* server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t** req_pkt) { iobuffer_t* ans_pkt = iobuffer_create(-1, -1); auto ret = sp_TryUpdateCfg(); int rc = Error_Succeed; sp_mod_mgr_init(sp_get_env()->mod_mgr); /* //add workitem daemon_on_pkt and daemon_on_sys if (0 != (rc = sp_var_daemon_create(get_app_instance()->svc, &get_app_instance()->var_daemon))) DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("var daemon create failed!"); else DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("var daemon create ok!"); if (0 != (rc = sp_bcm_daemon_create(get_app_instance()->svc, &get_app_instance()->bcm_daemon))) DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("bcm daemon create failed!"); else DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("bcm daemon create ok!"); sp_mod_mgr_bind_bcm_daemon(sp_get_env()->mod_mgr, get_app_instance()->bcm_daemon); */ sp_bcm_deamon_refresh(get_app_instance()->bcm_daemon); auto env = sp_get_env(); if (!env) { rc = Error_Unexpect; DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("on_try_update_cfg env error"); } else set_ld_library_path(env); iobuffer_write(ans_pkt, IOBUF_T_I4, &ret, 0);//需要sysError和UserError iobuffer_write(ans_pkt, IOBUF_T_I4, &ret, 0); return ans_pkt; } iobuffer_t* on_write_terminalNo(sp_rpc_server_t* server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t** req_pkt) { iobuffer_t* ans_pkt = iobuffer_create(-1, -1); char* terminalNo = NULL; iobuffer_format_read(*req_pkt, "s", &terminalNo); std::string t_terminalNo = terminalNo; FREE(terminalNo); auto env = sp_get_env(); auto cfg = env->cfg; CSimpleStringA strRootPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", env->dir->root_hardwarecfg_path, "root.ini"); int nRet = inifile_format_write((const char*)strRootPath, "Terminal", "TerminalNo", t_terminalNo.c_str()); iobuffer_write(ans_pkt, IOBUF_T_I4, &nRet, 0);//需要sysError和UserError iobuffer_write(ans_pkt, IOBUF_T_I4, &nRet, 0); return ans_pkt; } iobuffer_t* on_modify_mem_cfg(sp_rpc_server_t* server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t** req_pkt) { iobuffer_t* ans_pkt = iobuffer_create(-1, -1); char *configType = NULL, * module = NULL, *name = NULL, *value = NULL; std::shared_ptr delHandleFun((void*)0, [&](void*) { if (configType) FREE(configType); if (module) FREE(module); if (name) FREE(name); if (value) FREE(value); }); iobuffer_format_read(*req_pkt, "s", &configType); iobuffer_format_read(*req_pkt, "s", &module); iobuffer_format_read(*req_pkt, "s", &name); iobuffer_format_read(*req_pkt, "s", &value); auto nRet = sp_ModifyMemCfg(configType, module, name, value); iobuffer_write(ans_pkt, IOBUF_T_I4, &nRet, 0);//需要sysError和UserError iobuffer_write(ans_pkt, IOBUF_T_I4, &nRet, 0); return ans_pkt; } iobuffer_t* on_try_upload_logs_info(sp_rpc_server_t* server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t** req_pkt) { iobuffer_t* ans_pkt = iobuffer_create(-1, -1); unsigned long t_upload_TerminalSys_Suc = 0, t_upload_TerminalUser_Suc = 0, t_upload_BussinessSys_Suc = 0, t_upload_BussinessUser_Suc = 0, t_upload_beidou_Suc = 0, t_upload_TerminalSys_Err = 0, t_upload_TerminalUser_Err = 0, t_upload_BussinessSys_Err = 0, t_upload_BussinessUser_Err = 0, t_upload_beidou_Err = 0; unsigned long t_discard_full = 0, t_discard_RTI1002 = 0, curLogNum = 0; get_upload_info(&t_upload_TerminalSys_Suc, &t_upload_TerminalUser_Suc, &t_upload_BussinessSys_Suc, &t_upload_BussinessUser_Suc, &t_upload_beidou_Suc, &t_upload_TerminalSys_Err, &t_upload_TerminalUser_Err, &t_upload_BussinessSys_Err, &t_upload_BussinessUser_Err, &t_upload_beidou_Err); get_upload_info2(&t_discard_full, &t_discard_RTI1002, &curLogNum); ErrorCodeEnum nRet = Error_Succeed; int end = 0; //iobuffer_write(ans_pkt, IOBUF_T_I4, &nRet, 0);//dwSysError iobuffer_write(ans_pkt, IOBUF_T_I4, &nRet, 0);//UserError //char* tmpStr = ""; //iobuffer_write(ans_pkt, IOBUF_T_STR, tmpStr, 0); //iobuffer_write(ans_pkt, IOBUF_T_I4, &end, 0);//end unsigned int i_upload_TerminalSys_Suc = t_upload_TerminalSys_Suc; unsigned int i_upload_TerminalUser_Suc = t_upload_TerminalUser_Suc; unsigned int i_upload_BussinessSys_Suc = t_upload_BussinessSys_Suc; unsigned int i_upload_BussinessUser_Suc = t_upload_BussinessUser_Suc; unsigned int i_upload_beidou_Suc = t_upload_beidou_Suc; unsigned int i_upload_TerminalSys_Err = t_upload_TerminalSys_Err; unsigned int i_upload_TerminalUser_Err = t_upload_TerminalUser_Err; unsigned int i_upload_BussinessSys_Err = t_upload_BussinessSys_Err; unsigned int i_upload_BussinessUser_Err = t_upload_BussinessUser_Err; unsigned int i_upload_beidou_Err = t_upload_beidou_Err; unsigned int i_discard_full = t_discard_full; unsigned int i_discard_RTI1002 = t_discard_RTI1002; unsigned int i_curLogNum = curLogNum; iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_TerminalSys_Suc, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_TerminalUser_Suc, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_BussinessSys_Suc, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_BussinessUser_Suc, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_beidou_Suc, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_TerminalSys_Err, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_TerminalUser_Err, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_BussinessSys_Err, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_BussinessUser_Err, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_upload_beidou_Err, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_discard_full, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_discard_RTI1002, 0); iobuffer_write(ans_pkt, IOBUF_T_I4, &i_curLogNum, 0); /* DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("checkUrlActive::GetSendLogInfo, TS_Suc:%d, TU_Suc:%d, BS_Suc:%d, BU_Suc:%d, beidou_Suc:%d, TS_Err:%d, TU_Err:%d, BS_Err:%d, BU_Err:%d, beidou_Err:%d, discard_forFull:%d, discard_forRTI1002:%d, curNum:%d" , t_upload_TerminalSys_Suc, t_upload_TerminalUser_Suc, t_upload_BussinessSys_Suc, t_upload_BussinessUser_Suc, t_upload_beidou_Suc, t_upload_TerminalSys_Err, t_upload_TerminalUser_Err, t_upload_BussinessSys_Err, t_upload_BussinessUser_Err, t_upload_beidou_Err , t_discard_full, t_discard_RTI1002, curLogNum); */ return ans_pkt; }