sp_cfg.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  1. #include "precompile.h"
  2. #include "sp_cfg.h"
  3. #include "sp_def.h"
  4. //#include "sp_sal.h"
  5. #include "sp_dir.h"
  6. #include "sp_env.h"
  7. #include "sp_var.h"
  8. #include "iniutil.h"
  9. #include "strutil.h"
  10. #include "memutil.h"
  11. #include "shm_mem.h"
  12. #include "fileutil.h"
  13. #include "SpBase.h"
  14. #include "CodeSignVerify.h"
  15. #include <fstream>
  16. #include <ostream>
  17. #include <string>
  18. #include <vector>
  19. #include <map>
  20. #include <algorithm>
  21. #include <stdio.h>
  22. #include <winpr/ini.h>
  23. #include <winpr/string.h>
  24. #include <winpr/sysinfo.h>
  25. #include "dbgutil.h"
  26. #include "sp_httpDefine.h"
  27. #include "sp_logwithlink.h"
  28. #include "RestfulFunc.h"
  29. #if defined(_MSC_VER)
  30. #include "AutoLock.h"
  31. #include <memory>
  32. #include <iostream>
  33. #include <fstream>
  34. #include "RVCEventCode.h"
  35. #include <vsscanf.h>
  36. #include <ctime>
  37. #include <sstream>
  38. #include <iomanip>
  39. #include <windows.h>
  40. #define UPDATE_ROOT_BY_HTTP
  41. #else
  42. #include "JsonConvertHelper.hpp"
  43. #endif //_MSC_VER
  44. #include "sp_checkEntity.h"
  45. #include <log_producer_config.h>
  46. #if defined(_MSC_VER)
  47. std::map<std::string, std::string> g_specialRunInfoArr;
  48. class sp_cfg_share
  49. {
  50. private:
  51. LARGE_TIME m_shellFirstStart;
  52. public:
  53. static sp_cfg_share* getInstance();
  54. void sp_cfg_setShellFirstStartTime(LARGE_TIME t_shellFirstStart)
  55. {
  56. m_shellFirstStart = t_shellFirstStart;
  57. };
  58. LARGE_TIME sp_cfg_getShellFirstStartTime()
  59. {
  60. return m_shellFirstStart;
  61. };
  62. };
  63. sp_cfg_share* sp_cfg_share::getInstance()
  64. {
  65. static sp_cfg_share* Instance = NULL;
  66. if (NULL == Instance)
  67. Instance = new sp_cfg_share();
  68. return Instance;
  69. }
  70. void sp_cfg_setShellFirstStartTime(LARGE_TIME t_shellFirstStart)
  71. {
  72. sp_cfg_share::getInstance()->sp_cfg_setShellFirstStartTime(t_shellFirstStart);
  73. };
  74. LARGE_TIME sp_cfg_getShellFirstStartTime()
  75. {
  76. return sp_cfg_share::getInstance()->sp_cfg_getShellFirstStartTime();
  77. };
  78. auto fileExist = [](const char* path) ->bool {
  79. fstream f2;
  80. f2.open(path);
  81. if (!f2)
  82. {
  83. f2.close();
  84. return false;
  85. }
  86. else
  87. {
  88. f2.close();
  89. return true;
  90. }
  91. };
  92. #else
  93. ///*TODO(80374374@3/14/2023): */
  94. #endif //_MSC_VER
  95. ErrorCodeEnum init_shell_byHttp(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, sp_cfg_root_ini_t* root, const sp_cfg_start_args_t* args,
  96. const char* shellVarIni, std::map<std::string, std::map<std::string, std::string>> shellConfig);
  97. int sp_tryReadFromCacheConfig(int cfgType, const char* section, const char* key, char* dst, int max_len);
  98. int sp_tryRefreshLogLevelFromCacheConfig();
  99. void shell_getHttpToken(std::string& channelId, std::string& token, std::string& terminalNo, std::string& reserve1)
  100. {
  101. sp_env_t* env = sp_get_env();
  102. if (NULL != env && NULL != env->cfg && NULL != env->cfg->shell_ini &&
  103. env->cfg->shell_ini->channelId != NULL && env->cfg->shell_ini->token != NULL)
  104. {
  105. channelId = env->cfg->shell_ini->channelId;
  106. token = env->cfg->shell_ini->token;
  107. terminalNo = env->cfg->root_ini->terminal_no;
  108. }
  109. else if(NULL == env)
  110. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env == NULL");
  111. else if(NULL == env->cfg)
  112. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg == NULL");
  113. else if(NULL == env->cfg->shell_ini)
  114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini == NULL");
  115. else if(NULL == env->cfg->shell_ini->channelId)
  116. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini->channelId == NULL");
  117. else if(NULL == env->cfg->shell_ini->token)
  118. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini->token == NULL");
  119. }
  120. #define MAX_ENTITY_LEN 33
  121. #ifdef _WIN32
  122. #define MOD_LIB_SUFFIX ".dll"
  123. #else
  124. #define MOD_LIB_SUFFIX ".so"
  125. #endif //_WIN32
  126. #define T_MALLOC_T(type, shm) \
  127. shm ? (type*)shm_malloc(sizeof(type)) : (type*)malloc(sizeof(type))
  128. #define T_MALLOC(size, shm) \
  129. shm ? shm_malloc(size) : malloc(size)
  130. #define T_FREE(p, shm) \
  131. shm ? shm_free(p) : free(p)
  132. #define T_STRDUP(s, shm) \
  133. shm ? shm_strdup(s) : _strdup(s)
  134. #define T_ARRAY_MAKE(n, size, shm) \
  135. shm ? shm_array_make(n, size) : array_make(n, size)
  136. std::vector<std::string> g_arrKill;
  137. std::vector<std::string> g_arrStart;
  138. std::vector<std::string> getKillArr() {
  139. if(g_arrKill.size() > 0)
  140. return g_arrKill;
  141. std::vector<std::string> ret;
  142. const int maxKillNum = 10;
  143. const int MAX_LEN = 256;
  144. for (size_t i = 1; i < maxKillNum; i++)
  145. {
  146. char tmp[MAX_LEN] = "";
  147. if (Error_Succeed == sp_tryReadFromCacheConfig(ConfigTypeEnum::Config_Shell, "killProcess", std::to_string(i).c_str(), tmp, MAX_LEN))
  148. ret.push_back(tmp);
  149. else
  150. break;
  151. }
  152. return ret;
  153. }
  154. std::vector<std::string> getStartArr() {
  155. if(g_arrStart.size() > 0)
  156. return g_arrStart;
  157. std::vector<std::string> ret;
  158. const int maxStartNum = 10;
  159. const int MAX_LEN = 256;
  160. for (size_t i = 1; i < maxStartNum; i++)
  161. {
  162. char tmp[MAX_LEN] = "";
  163. if (Error_Succeed == sp_tryReadFromCacheConfig(ConfigTypeEnum::Config_Shell, "RunScript", std::to_string(i).c_str(), tmp, MAX_LEN))
  164. ret.push_back(tmp);
  165. else
  166. break;
  167. }
  168. return ret;
  169. }
  170. static int verify_entity_name(const char *name)
  171. {
  172. const char *p = name;
  173. int len = 0;
  174. while (*p) {
  175. if (!isalnum(*p)) {
  176. return FALSE;
  177. }
  178. len++;
  179. if (len > MAX_ENTITY_LEN)
  180. return FALSE;
  181. p++;
  182. }
  183. return TRUE;
  184. }
  185. static int parse_install_state(const char *str)
  186. {
  187. const struct {
  188. int ch;
  189. int val;
  190. } tbl[] = {
  191. {'A', Install_Active},
  192. {'I', Install_Pending},
  193. {'S', Install_SetToStart},
  194. {'F', Install_FailRun},
  195. {'R', Install_RollBack},
  196. {'U', Install_Upgraded},
  197. {'C',Install_Cancelled},
  198. {'W',Install_WaitConfirm},
  199. {'D', Install_Installed},
  200. };
  201. int i;
  202. for (i = 0; i < array_size(tbl); ++i) {
  203. if (tbl[i].ch == str[0]) {
  204. return tbl[i].val;
  205. }
  206. }
  207. return -1; // error
  208. }
  209. int read_ini_install_state(const char *file, const char *section, const char *key)
  210. {
  211. char *p = inifile_read_str(file, section, key, "");
  212. int ret = parse_install_state(p);
  213. FREE(p);
  214. return ret;
  215. }
  216. static __inline int file_write(FILE *fp, const void *buf, int n)
  217. {
  218. size_t t = fwrite(buf, n, 1, fp);
  219. return t == n ? 0 : Error_IO;
  220. }
  221. static __inline int file_read(FILE *fp, void *buf, int n)
  222. {
  223. size_t t = fread(buf, n, 1, fp);
  224. return t == n ? 0 : Error_IO;
  225. }
  226. static void destroy_entity(sp_cfg_shell_entity_t *entity)
  227. {
  228. if (entity) {
  229. if (entity->name)
  230. shm_free(entity->name);
  231. if (entity->argv)
  232. shm_free((void*)entity->argv);
  233. if (entity->cmdline)
  234. shm_free(entity->cmdline);
  235. shm_free(entity);
  236. }
  237. }
  238. static void destroy_module(sp_cfg_shell_module_t* mod)
  239. {
  240. if (mod) {
  241. if (mod->name)
  242. shm_free(mod->name);
  243. if (mod->author)
  244. shm_free(mod->author);
  245. if (mod->company)
  246. shm_free(mod->company);
  247. shm_free(mod);
  248. }
  249. }
  250. static void destroy_sysevent(sp_cfg_shell_sysevent_t *sysevent)
  251. {
  252. if (sysevent) {
  253. if (sysevent->name)
  254. shm_free(sysevent->name);
  255. if (sysevent->arr_owner_entity)
  256. shm_array_free(sysevent->arr_owner_entity);
  257. shm_free(sysevent);
  258. }
  259. }
  260. /*find path from root.ini which loaded at begin*/
  261. static sp_cfg_path_t *find_path(sp_cfg_t *cfg, const char *path_prefix)
  262. {
  263. int i;
  264. for (i = 0; cfg->root_ini->arr_path != NULL && i < cfg->root_ini->arr_path->nelts; ++i) {
  265. sp_cfg_path_t *tpath = ARRAY_IDX(cfg->root_ini->arr_path, i, sp_cfg_path_t*);
  266. if (strcmp(tpath->name, path_prefix) == 0) {
  267. return tpath;
  268. }
  269. }
  270. return NULL;
  271. }
  272. static sp_cfg_shell_module_t* find_module(sp_cfg_shell_ini_t *shell, const char *mod_name)
  273. {
  274. int i;
  275. sp_cfg_shell_module_t* mod;
  276. const int len = (strstr(mod_name, "lib") == mod_name) ? 3 : 0; //linux .so name
  277. ARRAY_FOR_EACH_ENTRY(mod, i, shell->arr_module, sp_cfg_shell_module_t*) {
  278. if (_stricmp(mod->name, mod_name + len) == 0)
  279. return mod;
  280. }
  281. return NULL;
  282. }
  283. static sp_cfg_shell_entity_t *find_entity(sp_cfg_shell_ini_t *shell, const char *entity_name)
  284. {
  285. int i;
  286. sp_cfg_shell_entity_t *entity;
  287. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  288. if (_stricmp(entity->name, entity_name) == 0)
  289. return entity;
  290. }
  291. return NULL;
  292. }
  293. static int read_ini_version(const char *file, const char *section, const char *key, int *major, int *minor, int *revision, int *build)
  294. {
  295. int n;
  296. int ret = -1;
  297. char *str = inifile_read_str(file, section, key, "");
  298. if (str) {
  299. const int len = strlen(str);
  300. FREE(str);
  301. if (len == 0) {
  302. if (major)
  303. *major = 0;
  304. if (minor)
  305. *minor = 0;
  306. if (revision)
  307. *revision = 0;
  308. if (build)
  309. *build = 0;
  310. return 0;
  311. }
  312. }
  313. if (build) {
  314. n = inifile_format_read(file, section, key, "%d.%d.%d.%d", major, minor, revision, build);
  315. if (n == 4)
  316. ret = 0;
  317. }
  318. else if (revision) {
  319. n = inifile_format_read(file, section, key, "%d.%d.%d", major, minor, revision);
  320. if (n == 3)
  321. ret = 0;
  322. }
  323. else {
  324. n = inifile_format_read(file, section, key, "%d.%d", major, minor);
  325. if (n == 2)
  326. ret = 0;
  327. }
  328. return ret;
  329. }
  330. static int read_ini_str(const char *file, const char *section, const char *key, char **pstr, int shm)
  331. {
  332. int rc = Error_Param;
  333. char* str = inifile_read_str(file, section, key, "");
  334. if (str) {
  335. if (shm) {
  336. *pstr = shm_strdup(str);
  337. rc = 0;
  338. FREE(str);
  339. }
  340. else {
  341. *pstr = str;
  342. rc = 0;
  343. }
  344. }
  345. return rc;
  346. }
  347. static int read_ini_date(const char *file, const char *section, const char *key,
  348. int *year, int *month, int *day, int *hour, int *minute, int *second, int shm)
  349. {
  350. int ret;
  351. if (second)
  352. *second = 0;
  353. ret = inifile_format_read(file, section, key, "%d-%d-%d %d:%d", year, month, day, hour, minute);
  354. return ret == 5 ? 0 : Error_Param;
  355. }
  356. static int read_ini_position(const char *file, const char *section, const char *key, float *x, float *y, int shm)
  357. {
  358. int ret;
  359. ret = inifile_format_read(file, section, key, "%f,%f", x, y);
  360. return ret == 2 ? 0 : Error_Param;
  361. }
  362. static int read_ini_path(const char *file, const char *section, const char *key, char **path, int shm)
  363. {
  364. int rc = read_ini_str(file, section, key, path, shm);
  365. if (rc == 0) {
  366. char *s = *path;
  367. int slen = strlen(s);
  368. if (s[slen - 1] == SPLIT_SLASH)
  369. s[slen - 1] = 0;
  370. }
  371. return rc;
  372. }
  373. static int read_terminal(const char *file, sp_cfg_root_ini_t *root, int shm)
  374. {
  375. int rc = 0;
  376. rc = read_ini_str(file, "Terminal", "TerminalNo", &root->terminal_no, shm);
  377. if (rc != 0) {
  378. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read TerminalNo failed!");
  379. return rc;
  380. }
  381. else if (strlen(root->terminal_no) == 0) {
  382. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read TerminalNo empty!");
  383. return Error_NotConfig;
  384. }
  385. rc = read_ini_str(file, "Terminal", "MachineType", &root->machine_type, shm);
  386. if (rc != 0)
  387. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineType failed!");
  388. else if (strlen(root->machine_type) == 0)
  389. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineType empty!");
  390. rc = read_ini_str(file, "Terminal", "MachineModel", &root->machine_model, shm);
  391. if (rc != 0) {
  392. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineModel failed!");
  393. }
  394. rc = read_ini_str(file, "Terminal", "SN", &root->machine_sn, shm);
  395. if (rc != 0) {
  396. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read SN failed!");
  397. }
  398. rc = read_ini_str(file, "Terminal", "Manufacturer", &root->manufacturer, shm);
  399. if (rc != 0)
  400. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Manufacturer failed!");
  401. else if (strlen(root->manufacturer) == 0)
  402. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Manufacturer empty!");
  403. rc = read_ini_version(file, "Terminal", "MachineVersion",
  404. &root->machine_version.major, &root->machine_version.minor, NULL, NULL);
  405. rc = read_ini_str(file, "Terminal", "Site", &root->site, shm);
  406. root->screen = inifile_read_int(file, "Terminal", "Screen", -1);
  407. if (root->screen == -1)
  408. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Screen failed!");
  409. rc = read_ini_position(file, "Terminal", "EnrolGPS", &root->enroll_gps_x, &root->enroll_gps_y, shm);
  410. if (rc != 0)
  411. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read EnrolGPS failed!");
  412. rc = read_ini_str(file, "Terminal", "EnrolAddr", &root->enroll_address, shm);
  413. if (rc != 0)
  414. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read EnrolAddr failed!");
  415. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read_terminal success, TerminalNo:%s, MachineType:%s, MachineModel:%s, MachineVersion:%d.%d, site:%s, screen:%d",
  416. root->terminal_no, root->machine_type == NULL ? "" : root->machine_type
  417. , root->machine_model == NULL ? "" : root->machine_model
  418. , root->machine_version.major, root->machine_version.minor
  419. , root->site == NULL ? "" : root->site, root->screen);
  420. return rc;
  421. }
  422. int safeStringToInt(const std::string& str) {
  423. if (str.empty()) {
  424. // 返回默认值,或者抛出自定义异常,或者进行其他错误处理
  425. return 0; // 返回默认值 0
  426. }
  427. else {
  428. try {
  429. return std::stoi(str);
  430. }
  431. catch (const std::invalid_argument& e) {
  432. // 返回默认值,或者抛出自定义异常,或者进行其他错误处理
  433. return 0; // 返回默认值 0
  434. }
  435. }
  436. }
  437. bool update_rootCfg(sp_cfg_root_ini_t* root, std::map <std::string, std::map<std::string, std::string>>& root_cfg)
  438. {
  439. if (root_cfg.find(CONFIG_TERMINALINFO_HEAD) == root_cfg.end())
  440. {
  441. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_TERMINALINFO_HEAD);
  442. return false;
  443. }
  444. auto terminalinfo_root = root_cfg[CONFIG_TERMINALINFO_HEAD];
  445. if (terminalinfo_root.find(CONFIG_NODE_MACHINETYPE) != terminalinfo_root.end())
  446. {
  447. shm_free(root->machine_type);
  448. root->machine_type = shm_strdup(terminalinfo_root[CONFIG_NODE_MACHINETYPE].c_str());
  449. }
  450. else
  451. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINETYPE);
  452. if (terminalinfo_root.find(CONFIG_NODE_MACHINEVERSION) != terminalinfo_root.end())
  453. sscanf(terminalinfo_root[CONFIG_NODE_MACHINETYPE].c_str(), "%d.%d", &root->machine_version.major, &root->machine_version.minor);
  454. else
  455. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINEVERSION);
  456. if (terminalinfo_root.find(CONFIG_NODE_SITES) != terminalinfo_root.end())
  457. {
  458. shm_free(root->site);
  459. root->site = shm_strdup(terminalinfo_root[CONFIG_NODE_SITES].c_str());
  460. }
  461. else
  462. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_SITES);
  463. if (terminalinfo_root.find(CONFIG_NODE_SCREEN) != terminalinfo_root.end())
  464. root->screen = safeStringToInt(terminalinfo_root[CONFIG_NODE_SCREEN]);
  465. else
  466. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_SCREEN);
  467. if (terminalinfo_root.find(CONFIG_NODE_ENROLADDR) != terminalinfo_root.end())
  468. {
  469. shm_free(root->enroll_address);
  470. root->enroll_address = shm_strdup(terminalinfo_root[CONFIG_NODE_ENROLADDR].c_str());
  471. }
  472. else
  473. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_ENROLADDR);
  474. if (terminalinfo_root.find(CONFIG_NODE_MANUFACTURER) != terminalinfo_root.end())
  475. {
  476. shm_free(root->manufacturer);
  477. root->manufacturer = shm_strdup(terminalinfo_root[CONFIG_NODE_MANUFACTURER].c_str());
  478. }
  479. else
  480. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MANUFACTURER);
  481. if (terminalinfo_root.find(CONFIG_NODE_MACHINEMODEL) != terminalinfo_root.end())
  482. {
  483. shm_free(root->machine_model);
  484. root->machine_model = shm_strdup(terminalinfo_root[CONFIG_NODE_MACHINEMODEL].c_str());
  485. }
  486. else
  487. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINEMODEL);
  488. return true;
  489. }
  490. SPBASE_API int sp_cfg_getVer(char *ver)
  491. {
  492. static CSimpleStringA s_terminalVer;
  493. if (s_terminalVer.GetLength() == 0)//only init s_terminalVer one time
  494. {
  495. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "active.txt", sp_get_env()->dir->root_ver_path);
  496. std::ifstream iFile(strActiveFile.GetData());
  497. if (!iFile.is_open()) {
  498. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  499. return Error_Unexpect;
  500. }
  501. std::ostringstream buffer;
  502. buffer << iFile.rdbuf();
  503. std::string myString = buffer.str();
  504. // 去除空格
  505. myString.erase(std::remove(myString.begin(), myString.end(), ' '), myString.end());
  506. // 去除换行符
  507. myString.erase(std::remove(myString.begin(), myString.end(), '\r'), myString.end());
  508. myString.erase(std::remove(myString.begin(), myString.end(), '\n'), myString.end());
  509. s_terminalVer = myString.c_str();
  510. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("read active.txt, version[%s]", s_terminalVer.GetData());
  511. }
  512. //TODO: CrossPlaform [Gifur@202572]
  513. #ifdef RVC_OS_LINUX
  514. sprintf(ver, "%s", s_terminalVer.GetData());
  515. #else
  516. sprintf_s(ver, SP_MAX_VER_LEN, "%s", s_terminalVer.GetData());
  517. #endif
  518. return Error_Succeed;
  519. }
  520. SPBASE_API int sp_cfg_getDepVer(char* ver)
  521. {
  522. auto cfg = sp_get_env()->cfg;
  523. auto dir = sp_get_env()->dir;
  524. int rc = Error_Succeed;
  525. if (cfg == NULL || dir == NULL)
  526. return Error_Null;
  527. char path[MAX_PATH] = "";
  528. rc = sp_dir_get_path(dir, SP_DIR_DEPVER_TXT, NULL, path, MAX_PATH);
  529. if (rc != 0) {
  530. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get depver.txt path failed!");
  531. return rc;
  532. }
  533. char version[SP_MAX_VER_LEN];
  534. int result;
  535. if ((result = sp_dir_inner_get_content(path, version)) == 0) {
  536. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("read dep version text, version[%s]", version);
  537. sprintf(ver, "%s", version);
  538. }
  539. return result;
  540. }
  541. void get_upload_info(unsigned long* t_upload_TerminalSys_Suc, unsigned long* t_upload_TerminalUser_Suc,
  542. unsigned long* t_upload_BussinessSys_Suc, unsigned long* t_upload_BussinessUser_Suc, unsigned long* t_upload_beidou_Suc,
  543. unsigned long* t_upload_TerminalSys_Err, unsigned long* t_upload_TerminalUser_Err,
  544. unsigned long* t_upload_BussinessSys_Err, unsigned long* t_upload_BussinessUser_Err, unsigned long* t_upload_beidou_Err)
  545. {
  546. log_producer_config_get_upload_info(t_upload_TerminalSys_Suc, t_upload_TerminalUser_Suc, t_upload_BussinessSys_Suc, t_upload_BussinessUser_Suc, t_upload_beidou_Suc,
  547. t_upload_TerminalSys_Err, t_upload_TerminalUser_Err, t_upload_BussinessSys_Err, t_upload_BussinessUser_Err, t_upload_beidou_Err);
  548. }
  549. void get_upload_info2(unsigned long* t_discard_full, unsigned long* t_discard_RTI1002, unsigned long* curLogNum)
  550. {
  551. log_producer_config_get_upload_info2(t_discard_full, t_discard_RTI1002, curLogNum);
  552. }
  553. int sp_TryUpdateToken(char *oldToken, char *newToken)
  554. {
  555. auto cfg = sp_get_env()->cfg;
  556. if (cfg == NULL || cfg->shell_ini == NULL || cfg->root_ini == NULL)
  557. return Error_Null;
  558. std::string terminalNoInRoot = (cfg->root_ini->terminal_no == NULL) ? "" : cfg->root_ini->terminal_no;
  559. std::string channelId = (cfg->shell_ini->channelId == NULL) ? "" : cfg->shell_ini->channelId;
  560. std::string tokenScret = (cfg->shell_ini->tokenSecret == NULL) ? "" : cfg->shell_ini->tokenSecret;
  561. std::string commonUrl = (cfg->shell_ini->CommonLaunchUrl == NULL) ? "" : cfg->shell_ini->CommonLaunchUrl;
  562. char terminalVer[SP_MAX_VER_LEN] = "";
  563. sp_cfg_getVer(terminalVer);
  564. if (cfg->shell_ini->token != NULL)
  565. strcpy(oldToken, cfg->shell_ini->token);
  566. if (commonUrl.length() == 0)
  567. {
  568. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("TryUpdateToken").setLogCode("QLR0402Z10A00001")("can not get CommonLaunchUrl from shell");
  569. return ErrorCodeEnum::Error_NotConfig;
  570. }
  571. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_USER)("TryUpdateToken by %s, %s", terminalNoInRoot.c_str(), terminalVer);
  572. auto tokenRet = refreshToken(terminalNoInRoot, terminalVer, channelId, tokenScret, commonUrl);
  573. if (tokenRet.first)
  574. {
  575. if (cfg->shell_ini->token != NULL)
  576. shm_free(cfg->shell_ini->token);
  577. cfg->shell_ini->token = shm_strdup(tokenRet.second.c_str());
  578. strcpy(newToken, cfg->shell_ini->token);
  579. return ErrorCodeEnum::Error_Succeed;
  580. }
  581. else
  582. {
  583. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("TryUpdateToken").setLogCode("QLR0402Z10A00003")("total getToken failed, commonUrl:%s", commonUrl.c_str());
  584. return ErrorCodeEnum::Error_Failed;
  585. }
  586. }
  587. bool checkRootIniParam(sp_cfg_root_ini_t* root)
  588. {
  589. if (root == NULL)
  590. {
  591. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("checkRootIniParam root == NULL");
  592. return false;
  593. }
  594. else if (root->machine_type == NULL)
  595. {
  596. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  597. ("create cfg object failed!, some cfg in rootCfg not exist, machine_type:%d", root->machine_type != NULL);
  598. return false;
  599. }
  600. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("checkRootIniParam terminal_no:%s, machine_type:%s, manufacturer:%s, machine_model:%s"
  601. , (root->terminal_no == NULL ? "" : root->terminal_no), (root->machine_type == NULL ? "" : root->machine_type)
  602. , (root->manufacturer == NULL ? "" : root->manufacturer), (root->machine_model == NULL ? "" : root->machine_model));
  603. return true;
  604. }
  605. int sp_tryReadFromCacheConfig(int cfgType, const char* section, const char* key, char* dst, int max_len)
  606. {
  607. char tmp[MAX_PATH];
  608. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  609. *strrchr(tmp, SPLIT_SLASH) = 0;
  610. *strrchr(tmp, SPLIT_SLASH) = 0;
  611. *strrchr(tmp, SPLIT_SLASH) = 0;
  612. *strrchr(tmp, SPLIT_SLASH) = 0;
  613. switch (cfgType)
  614. {
  615. case ConfigTypeEnum::Config_Run:
  616. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "rootCfg", tmp);
  617. break;
  618. case ConfigTypeEnum::Config_Shell:
  619. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "ShellCfg", tmp);
  620. break;
  621. case ConfigTypeEnum::Config_Cache:
  622. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  623. break;
  624. default:
  625. return ErrorCodeEnum::Error_NotConfig;
  626. }
  627. if (!ExistsFile(tmp))
  628. return ErrorCodeEnum::Error_NotConfig;
  629. std::string cfgInfo = TerminalCfgRet::readStrFromFile(tmp);
  630. std::map<std::string, std::map<std::string, std::string>> tmp_Config;
  631. ConvertStrToDeviceConfigMap(cfgInfo, tmp_Config);
  632. if (tmp_Config.find(section) == tmp_Config.end())
  633. return ErrorCodeEnum::Error_NoTarget;
  634. auto curSection = tmp_Config[section];
  635. if (curSection.find(key) == curSection.end())
  636. return ErrorCodeEnum::Error_NoDefine;
  637. else
  638. {
  639. //TODO: CrossPlaform [Gifur@202572]
  640. #ifdef RVC_OS_LINUX
  641. sprintf(dst, "%s", curSection[key].c_str());
  642. #else
  643. sprintf_s(dst, max_len, "%s", curSection[key].c_str());
  644. #endif
  645. return Error_Succeed;
  646. }
  647. }
  648. int sp_tryRefreshLogLevelFromCacheConfig()
  649. {
  650. char tmp[MAX_PATH];
  651. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  652. *strrchr(tmp, SPLIT_SLASH) = 0;
  653. *strrchr(tmp, SPLIT_SLASH) = 0;
  654. *strrchr(tmp, SPLIT_SLASH) = 0;
  655. *strrchr(tmp, SPLIT_SLASH) = 0;
  656. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  657. if (!ExistsFile(tmp))
  658. return ErrorCodeEnum::Error_NotConfig;
  659. std::string cfgInfo = TerminalCfgRet::readStrFromFile(tmp);
  660. std::map<std::string, std::map<std::string, std::string>> tmp_Config;
  661. ConvertStrToDeviceConfigMap(cfgInfo, tmp_Config);
  662. load_debugLevelInMem(tmp_Config);
  663. return 0;
  664. }
  665. int sp_tryReadFromCenterSetting(const char* section, const char* key, char *dst, int max_len)
  666. {
  667. char tmp[MAX_PATH];
  668. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  669. *strrchr(tmp, SPLIT_SLASH) = 0;
  670. *strrchr(tmp, SPLIT_SLASH) = 0;
  671. *strrchr(tmp, SPLIT_SLASH) = 0;
  672. *strrchr(tmp, SPLIT_SLASH) = 0;
  673. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  674. if (!ExistsFile(tmp))
  675. return ErrorCodeEnum::Error_NotConfig;
  676. std::string centerCfg = TerminalCfgRet::readStrFromFile(tmp);
  677. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig;
  678. ConvertStrToDeviceConfigMap(centerCfg, tmp_centerConfig);
  679. if (tmp_centerConfig.find(section) == tmp_centerConfig.end())
  680. return ErrorCodeEnum::Error_NoTarget;
  681. auto curSection = tmp_centerConfig[section];
  682. if (curSection.find(key) == curSection.end())
  683. return ErrorCodeEnum::Error_NoDefine;
  684. else
  685. {
  686. //TODO: CrossPlaform [Gifur@202572]
  687. #ifdef RVC_OS_LINUX
  688. sprintf(dst, "%s", curSection[key].c_str());
  689. #else
  690. sprintf_s(dst, max_len, "%s", curSection[key].c_str());
  691. #endif
  692. return Error_Succeed;
  693. }
  694. }
  695. SPBASE_API int sp_ModifyMemCfg(const char* configType, const char* module, const char* name, const char* value)
  696. {
  697. auto cfg = sp_get_env()->cfg;
  698. char **dstConfig = NULL;
  699. if (0 == CSimpleString(configType).Compare("centersetting", true))
  700. dstConfig = &(cfg->root_ini->center_config);
  701. else if (0 == CSimpleString(configType).Compare("root", true))
  702. dstConfig = &(cfg->root_ini->root_config);
  703. else if (0 == CSimpleString(configType).Compare("shell", true))
  704. dstConfig = &(cfg->root_ini->shell_config);
  705. else
  706. return ErrorCodeEnum::Error_Param;
  707. std::map<std::string, std::map<std::string, std::string>> tmp_config;
  708. ConvertStrToDeviceConfigMap(*dstConfig == NULL ? "" : *dstConfig, tmp_config);
  709. if (tmp_config.find(module) == tmp_config.end())
  710. {
  711. //not exist module, insert std::map<std::string, std::string>
  712. std::map<std::string, std::string> tmp_param;
  713. tmp_param[name] = value;
  714. tmp_config.insert(std::make_pair(module, tmp_param));
  715. }
  716. else
  717. tmp_config[module][name] = value;
  718. std::string modifyConfig = TerminalCfgRet::ConvertMapMapConfigToStr(tmp_config);
  719. shm_free(*dstConfig);
  720. *dstConfig = shm_strdup(modifyConfig.c_str());
  721. return ErrorCodeEnum::Error_Succeed;
  722. }
  723. int sp_TryUpdate_vtm_err_msg()
  724. {
  725. auto cfg = sp_get_env()->cfg;
  726. auto dir = sp_get_env()->dir;
  727. if (cfg == NULL || dir == NULL)
  728. return Error_Null;
  729. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  730. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  731. CSimpleStringA strVtmErrMsgCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  732. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(VTMERRMSG_FILENAME);
  733. std::string terminalNoInRoot = cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no;
  734. CSimpleStringA CenterConfigTotal = cfg->shell_ini->CenterConfigTotal;
  735. auto vtmErrMsgList = GetVTMErrMsgCfgFromUrl(CenterConfigTotal, terminalNoInRoot.c_str());
  736. if (!vtmErrMsgList.first)//falat, get err msg list from file
  737. {
  738. std::string versionNo = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, VTMERRMSG_VERSION, "");
  739. std::string errMsgStr = TerminalCfgRet::readStrFromFile(strVtmErrMsgCfgPath.GetData());
  740. if (versionNo.length() > 0 && errMsgStr.length() > 0)
  741. {
  742. cfg->root_ini->vtm_err_msg_version = shm_strdup(versionNo.c_str());
  743. cfg->root_ini->vtm_err_msg_config = shm_strdup(errMsgStr.c_str());
  744. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read vtm_err_msg from shellvar.ini");
  745. return ErrorCodeEnum::Error_Succeed;
  746. }
  747. else
  748. {
  749. cfg->root_ini->vtm_err_msg_version = shm_strdup("");
  750. cfg->root_ini->vtm_err_msg_config = shm_strdup("");
  751. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set vtm_err_msg and vtm_err_msg_version to null");
  752. return ErrorCodeEnum::Error_Null;
  753. }
  754. }
  755. //get err msg list success, write mem and write file
  756. auto writeRet = vtmErrMsgList.second.saveVTMErrToFile(strVtmErrMsgCfgPath.GetData());
  757. if (!writeRet.first)
  758. {
  759. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("saveVTMErrToFile failed, %s", writeRet.second.c_str());
  760. return ErrorCodeEnum::Error_IO;
  761. }
  762. cfg->root_ini->vtm_err_msg_version = shm_strdup(vtmErrMsgList.second.version_no.c_str());
  763. cfg->root_ini->vtm_err_msg_config = shm_strdup(writeRet.second.c_str());
  764. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read vtm_err_msg from http server");
  765. return ErrorCodeEnum::Error_Succeed;
  766. }
  767. int sp_TryUpdateCfg()
  768. {
  769. auto cfg = sp_get_env()->cfg;
  770. auto dir = sp_get_env()->dir;
  771. int rc = Error_Succeed;
  772. if (cfg == NULL || dir == NULL || cfg->shell_ini == NULL || cfg->root_ini == NULL)
  773. return Error_Null;
  774. std::string terminalNoInRoot = cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no;
  775. CSimpleStringA CenterConfigTotal = cfg->shell_ini->CenterConfigTotal;
  776. char shellvar_path[MAX_PATH] = "";
  777. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shellvar_path, MAX_PATH);
  778. if (rc != 0) {
  779. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  780. return rc;
  781. }
  782. //do get config
  783. do
  784. {
  785. clock_t t_begin = clock();//微服务访问计时
  786. //read old config list
  787. std::string old_centerVer, old_centerConfig, old_rootVer, old_rootConfig, old_shellVer, old_shellConfig;
  788. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  789. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  790. CSimpleStringA strRootCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  791. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("rootCfg");
  792. CSimpleStringA strCenterCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  793. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("CenterCfg");
  794. CSimpleStringA strShellCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  795. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("ShellCfg");
  796. old_rootVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, "");
  797. old_centerVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, "");
  798. old_shellVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_SHELL_INI_VERSION, "");
  799. old_rootConfig = TerminalCfgRet::readStrFromFile(strRootCfgPath.GetData());
  800. old_centerConfig = TerminalCfgRet::readStrFromFile(strCenterCfgPath.GetData());
  801. old_shellConfig = TerminalCfgRet::readStrFromFile(strShellCfgPath.GetData());
  802. if (CenterConfigTotal.GetLength() == 0)
  803. {
  804. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rootCfg from root.ini");
  805. break;
  806. }
  807. //从微服务获取集中配置和root配置
  808. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig, tmp_rootConfig, tmp_shellConfig;
  809. std::map<std::string, int> logLevelArr;
  810. ConvertStrToDeviceConfigMap(old_centerConfig, tmp_centerConfig);
  811. ConvertStrToDeviceConfigMap(old_rootConfig, tmp_rootConfig);
  812. ConvertStrToDeviceConfigMap(old_shellConfig, tmp_shellConfig);
  813. auto runInfoRet = GetTerminalCfgFromUrl(CenterConfigTotal, terminalNoInRoot, "", "", "", tmp_centerConfig, tmp_rootConfig, tmp_shellConfig);
  814. if (!runInfoRet.first)//只有解析出错时才会进入该入口,比如获取配置失败;收到的值中无关键节点;不会因root、center、shell中缺少某一配置而返回错误
  815. {
  816. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl err, env:%s, url:%s",
  817. cfg->shell_ini->env, cfg->shell_ini->CenterConfigTotal);
  818. //从微服务获取配置失败,使用当前shellVar中配置
  819. if (tmp_centerConfig.size() > 0)
  820. {
  821. cfg->root_ini->center_version = shm_strdup(old_centerVer.c_str());
  822. cfg->root_ini->center_config = shm_strdup(old_centerConfig.c_str());
  823. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read center_config from shellvar.ini");
  824. load_debugLevelInMem(tmp_centerConfig);
  825. }
  826. if (tmp_rootConfig.size() > 0)
  827. {
  828. cfg->root_ini->root_version = shm_strdup(old_rootVer.c_str());
  829. cfg->root_ini->root_config = shm_strdup(old_rootConfig.c_str());
  830. update_rootCfg(cfg->root_ini, tmp_rootConfig);
  831. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from shellvar.ini");
  832. checkRootIniParam(cfg->root_ini);
  833. }
  834. if (tmp_shellConfig.size() > 0)
  835. {
  836. cfg->root_ini->shell_version = shm_strdup(old_shellVer.c_str());
  837. cfg->root_ini->shell_config = shm_strdup(old_shellConfig.c_str());
  838. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, tmp_shellConfig);
  839. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from shellvar.ini");
  840. if (rc != 0) {
  841. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  842. return rc;
  843. }
  844. }
  845. else
  846. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config err, no config exist");
  847. break;
  848. }
  849. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl success, cost:%d",
  850. (clock() - t_begin) * 1000 / CLOCKS_PER_SEC);
  851. //判断UseRootCfg,是否缓存或者清空配置
  852. auto cur_centerConfig = runInfoRet.second.center_config;
  853. auto cur_rootConfig = runInfoRet.second.root_config;
  854. auto cur_shellConfig = runInfoRet.second.shell_config;
  855. bool t_UseRootCfg = cur_centerConfig.find("Common") != cur_centerConfig.end() &&
  856. cur_centerConfig["Common"].find("UseRootCfg") != cur_centerConfig["Common"].end() &&
  857. cur_centerConfig["Common"]["UseRootCfg"] == "1";/*判断UseRootCfg == 1*/
  858. bool t_rootUpdate = runInfoRet.second.root_update;
  859. if (t_UseRootCfg && t_rootUpdate)//认为返回的root配置无问题
  860. {
  861. //save file
  862. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, runInfoRet.second.root_version.c_str());
  863. TerminalCfgRet::saveMapMapToFile(strRootCfgPath.GetData(), cur_rootConfig);
  864. //save mem, 使用从server获取的配置
  865. cfg->root_ini->root_version = shm_strdup(runInfoRet.second.root_version.c_str());
  866. cfg->root_ini->root_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_rootConfig).c_str());
  867. update_rootCfg(cfg->root_ini, cur_rootConfig);
  868. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from http server");
  869. }
  870. else if (t_UseRootCfg && tmp_rootConfig.size() > 0)//认为返回的root配置有问题
  871. {
  872. cfg->root_ini->root_version = shm_strdup(old_rootVer.c_str());
  873. cfg->root_ini->root_config = shm_strdup(old_rootConfig.c_str());
  874. update_rootCfg(cfg->root_ini, tmp_rootConfig);
  875. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from shellvar.ini");
  876. }
  877. else
  878. {
  879. //clear file
  880. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, "");
  881. std::map<std::string, std::map<std::string, std::string>> null_config;
  882. TerminalCfgRet::saveMapMapToFile(strRootCfgPath.GetData(), null_config);
  883. //clear mem
  884. cfg->root_ini->root_config = shm_strdup("");
  885. cfg->root_ini->root_version = shm_strdup("");
  886. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UseRootCfg maybe err, root_update:%d, clear all rootCfg, use default root.ini",
  887. runInfoRet.second.root_update);
  888. }
  889. checkRootIniParam(cfg->root_ini);
  890. //判断UseCenterCfg,是否缓存或者清空配置
  891. bool t_CheckCenterCfg = cur_centerConfig.find("Common") != cur_centerConfig.end() &&
  892. cur_centerConfig["Common"].find("UseCenterCfg") != cur_centerConfig["Common"].end();
  893. if (t_CheckCenterCfg && cur_centerConfig["Common"]["UseCenterCfg"] == "1")
  894. {
  895. //save file
  896. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, runInfoRet.second.center_version.c_str());
  897. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), cur_centerConfig);
  898. load_debugLevelInMem(cur_centerConfig);
  899. //save mem
  900. cfg->root_ini->center_version = shm_strdup(runInfoRet.second.center_version.c_str());
  901. cfg->root_ini->center_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_centerConfig).c_str());
  902. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read center_config from http server");
  903. }
  904. else
  905. {
  906. //clear file
  907. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, "");
  908. std::map<std::string, std::map<std::string, std::string>> null_config;
  909. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), null_config);
  910. //clear mem
  911. cfg->root_ini->center_config = shm_strdup("");
  912. cfg->root_ini->center_version = shm_strdup("");
  913. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UseCenterCfg not 1, clear all CenterCfg, use default centersetting.ini");
  914. }
  915. //shell的读取
  916. if (cur_shellConfig.size() > 0)
  917. {
  918. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, cur_shellConfig);
  919. if (rc != 0) {
  920. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  921. return rc;
  922. }
  923. cfg->root_ini->shell_version = shm_strdup(runInfoRet.second.shell_version.c_str());
  924. cfg->root_ini->shell_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_shellConfig).c_str());
  925. //save file
  926. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_SHELL_INI_VERSION, runInfoRet.second.shell_version.c_str());
  927. TerminalCfgRet::saveMapMapToFile(strShellCfgPath.GetData(), runInfoRet.second.shell_config);
  928. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from htpp server");
  929. }
  930. else if (tmp_shellConfig.size() > 0)
  931. {
  932. cfg->root_ini->shell_version = shm_strdup(old_shellVer.c_str());
  933. cfg->root_ini->shell_config = shm_strdup(old_shellConfig.c_str());
  934. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, tmp_shellConfig);
  935. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from shellvar.ini");
  936. if (rc != 0) {
  937. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  938. return rc;
  939. }
  940. }
  941. else
  942. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config err, no config exist");
  943. t_begin = clock();//微服务访问计时
  944. auto uploadVerRet = UploadTerminalVersionFromUrl(CenterConfigTotal, terminalNoInRoot,
  945. cfg->root_ini->center_version != NULL ? cfg->root_ini->center_version : "",
  946. cfg->root_ini->root_version != NULL ? cfg->root_ini->root_version : "",
  947. cfg->root_ini->shell_version != NULL ? cfg->root_ini->shell_version : "");
  948. if (!uploadVerRet)
  949. {
  950. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl err, env:%s, url:%s",
  951. cfg->shell_ini->env, cfg->shell_ini->CenterConfigTotal);
  952. break;
  953. }
  954. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl success, cost:%d",
  955. (clock() - t_begin) * 1000 / CLOCKS_PER_SEC);
  956. } while (false);
  957. if (!checkRootIniParam(cfg->root_ini))
  958. return ErrorCodeEnum::Error_NotExist;
  959. if (0 != (rc = load_mod_mgr(sp_get_env()->mod_mgr, sp_get_env()->cfg)))
  960. return rc;
  961. return Error_Succeed;
  962. }
  963. int sp_TryUpdateCenterCfg(int* isUpdate, int* isReset, char* version)
  964. {
  965. sp_env_t* env = sp_get_env();
  966. std::string terminalNoInRoot = env->cfg->root_ini->terminal_no == NULL ? "" : env->cfg->root_ini->terminal_no;
  967. std::string old_centerVer, old_centerConfig, old_rootVer, old_rootConfig, old_shellConfig, old_shellVer;
  968. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  969. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  970. CSimpleStringA strRootCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  971. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("rootCfg");
  972. CSimpleStringA strCenterCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  973. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("CenterCfg");
  974. CSimpleStringA strShellCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  975. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("ShellCfg");
  976. old_rootVer = env->cfg->root_ini->root_version == NULL ? "" : env->cfg->root_ini->root_version;
  977. old_centerVer = env->cfg->root_ini->center_version == NULL ? "" : env->cfg->root_ini->center_version;
  978. old_rootConfig = env->cfg->root_ini->root_config == NULL ? "" : env->cfg->root_ini->root_config;
  979. old_centerConfig = env->cfg->root_ini->center_config == NULL ? "" : env->cfg->root_ini->center_config;
  980. old_shellConfig = env->cfg->root_ini->shell_config == NULL ? "" : env->cfg->root_ini->shell_config;
  981. old_shellVer = env->cfg->root_ini->shell_version == NULL ? "" : env->cfg->root_ini->shell_version;
  982. CSimpleStringA CenterConfigTotal = env->cfg->shell_ini->CenterConfigTotal;
  983. if (CenterConfigTotal.GetLength() == 0)
  984. {
  985. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rootCfg from root.ini");
  986. return Error_Break;
  987. }
  988. //从微服务获取集中配置和root配置
  989. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig, tmp_rootConfig, tmp_shellConfig;
  990. ConvertStrToDeviceConfigMap(old_centerConfig, tmp_centerConfig);
  991. ConvertStrToDeviceConfigMap(old_rootConfig, tmp_rootConfig);
  992. ConvertStrToDeviceConfigMap(old_shellConfig, tmp_shellConfig);
  993. auto runInfoRet = GetTerminalCfgFromUrl(CenterConfigTotal, terminalNoInRoot, old_centerVer, old_rootVer, old_shellVer, tmp_centerConfig, tmp_rootConfig, tmp_shellConfig);
  994. if (!runInfoRet.first)
  995. {
  996. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl err, env:%s, url:%s",
  997. env->cfg->shell_ini->env, env->cfg->shell_ini->CenterConfigTotal);
  998. return Error_NetBroken;
  999. }
  1000. //是否update和是否reset
  1001. if (runInfoRet.second.center_update)
  1002. {
  1003. if (env->cfg->root_ini->center_version != NULL)
  1004. shm_free(env->cfg->root_ini->center_version);
  1005. if (env->cfg->root_ini->center_config != NULL)
  1006. shm_free(env->cfg->root_ini->center_config);
  1007. //save file
  1008. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, runInfoRet.second.center_version.c_str());
  1009. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), runInfoRet.second.center_config);
  1010. env->cfg->root_ini->center_version = shm_strdup(runInfoRet.second.center_version.c_str());
  1011. env->cfg->root_ini->center_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(runInfoRet.second.center_config).c_str());
  1012. *isUpdate = true;
  1013. }
  1014. *isReset = runInfoRet.second.center_reset;
  1015. if (version != NULL)
  1016. strcpy(version, runInfoRet.second.center_version.c_str());
  1017. std::string cur_rootVer = env->cfg->root_ini->root_version == NULL ? "" : env->cfg->root_ini->root_version;
  1018. std::string cur_centerVer = env->cfg->root_ini->center_version == NULL ? "" : env->cfg->root_ini->center_version;
  1019. std::string cur_shellVer = env->cfg->root_ini->shell_version == NULL ? "" : env->cfg->root_ini->shell_version;
  1020. if (cur_centerVer.length() > 0 && cur_centerVer != old_centerVer)
  1021. UploadTerminalVersionFromUrl(CenterConfigTotal, terminalNoInRoot, cur_centerVer, cur_rootVer, cur_shellVer);
  1022. return ErrorCodeEnum::Error_Succeed;
  1023. }
  1024. static int read_path(sp_dir_t* dir, const char* file, sp_cfg_root_ini_t* root, int shm)
  1025. {
  1026. int rc = 0;
  1027. root->arr_path = T_ARRAY_MAKE(32, sizeof(sp_cfg_path_t*), shm);
  1028. if (root->ref_sysroot_path == NULL)
  1029. {
  1030. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Run", dir->rvc_path);
  1031. char* shm_path = shm_strdup(tmp);
  1032. root->ref_sysroot_path = shm_path;
  1033. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1034. path->name = shm_strdup("SysRoot");
  1035. path->path = shm_path;
  1036. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1037. if (!ExistsDirA(shm_path))
  1038. CreateDirRecursiveA(shm_path);
  1039. }
  1040. if (root->ref_addata_path == NULL)
  1041. {
  1042. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "adData", dir->rvc_path);
  1043. char* shm_path = shm_strdup(tmp);
  1044. root->ref_addata_path = shm_path;
  1045. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1046. path->name = shm_strdup("ADData");
  1047. path->path = shm_path;
  1048. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1049. if (!ExistsDirA(shm_path))
  1050. CreateDirRecursiveA(shm_path);
  1051. }
  1052. if (root->ref_syslog_path == NULL)
  1053. {
  1054. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "SysLog", dir->rvc_path);
  1055. char* shm_path = shm_strdup(tmp);
  1056. root->ref_syslog_path = shm_path;
  1057. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1058. path->name = shm_strdup("SysLog");
  1059. path->path = shm_path;
  1060. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1061. if (!ExistsDirA(shm_path))
  1062. CreateDirRecursiveA(shm_path);
  1063. }
  1064. if (root->ref_intlog_path == NULL)
  1065. {
  1066. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "InterLog", dir->rvc_path);
  1067. char* shm_path = shm_strdup(tmp);
  1068. root->ref_intlog_path = shm_path;
  1069. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1070. path->name = shm_strdup("InterLog");
  1071. path->path = shm_path;
  1072. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1073. if (!ExistsDirA(shm_path))
  1074. CreateDirRecursiveA(shm_path);
  1075. }
  1076. if (root->ref_uploadvideo_path == NULL)
  1077. {
  1078. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "UploadVideo", dir->rvc_path);
  1079. char* shm_path = shm_strdup(tmp);
  1080. root->ref_uploadvideo_path = shm_path;
  1081. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1082. path->name = shm_strdup("UploadVideo");
  1083. path->path = shm_path;
  1084. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1085. if (!ExistsDirA(shm_path))
  1086. CreateDirRecursiveA(shm_path);
  1087. }
  1088. if (root->ref_uploadphoto_path == NULL)
  1089. {
  1090. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "UploadPhoto", dir->rvc_path);
  1091. char* shm_path = shm_strdup(tmp);
  1092. root->ref_uploadphoto_path = shm_path;
  1093. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1094. path->name = shm_strdup("UploadPhoto");
  1095. path->path = shm_path;
  1096. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1097. if (!ExistsDirA(shm_path))
  1098. CreateDirRecursiveA(shm_path);
  1099. }
  1100. if (root->ref_localvideo_path == NULL)
  1101. {
  1102. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Video", dir->rvc_path);
  1103. char* shm_path = shm_strdup(tmp);
  1104. root->ref_localvideo_path = shm_path;
  1105. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1106. path->name = shm_strdup("LocalVideo");
  1107. path->path = shm_path;
  1108. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1109. if (!ExistsDirA(shm_path))
  1110. CreateDirRecursiveA(shm_path);
  1111. }
  1112. if (root->ref_downloads_path == NULL)
  1113. {
  1114. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Downloads", dir->rvc_path);
  1115. char* shm_path = shm_strdup(tmp);
  1116. root->ref_downloads_path = shm_path;
  1117. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1118. path->name = shm_strdup("Downloads");
  1119. path->path = shm_path;
  1120. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1121. if (!ExistsDirA(shm_path))
  1122. CreateDirRecursiveA(shm_path);
  1123. }
  1124. if (root->ref_upgraded_path == NULL)
  1125. {
  1126. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Upgraded", dir->rvc_path);
  1127. char* shm_path = shm_strdup(tmp);
  1128. root->ref_upgraded_path = shm_path;
  1129. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1130. path->name = shm_strdup("Upgraded");
  1131. path->path = shm_path;
  1132. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1133. if (!ExistsDirA(shm_path))
  1134. CreateDirRecursiveA(shm_path);
  1135. }
  1136. if (root->ref_tmp_path == NULL)
  1137. {
  1138. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Temp", dir->rvc_path);
  1139. char* shm_path = shm_strdup(tmp);
  1140. root->ref_tmp_path = shm_path;
  1141. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1142. path->name = shm_strdup("Temp");
  1143. path->path = shm_path;
  1144. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1145. if (!ExistsDirA(shm_path))
  1146. CreateDirRecursiveA(shm_path);
  1147. }
  1148. if (root->ref_centersetting_path == NULL)
  1149. {
  1150. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "CenterSetting", dir->rvc_path);
  1151. char* shm_path = shm_strdup(tmp);
  1152. root->ref_centersetting_path = shm_path;
  1153. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1154. path->name = shm_strdup("CenterSetting");
  1155. path->path = shm_path;
  1156. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1157. if (!ExistsDirA(shm_path))
  1158. CreateDirRecursiveA(shm_path);
  1159. }
  1160. return rc;
  1161. }
  1162. static sp_cfg_pack_info_t *read_pack_info(const char *file, const char *pack, int shm)
  1163. {
  1164. sp_cfg_pack_info_t *pack_info = T_MALLOC_T(sp_cfg_pack_info_t, shm);
  1165. memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
  1166. pack_info->name = T_STRDUP(pack, shm);
  1167. pack_info->install_time = inifile_read_int(file, pack, "InstalledDate", 0);
  1168. pack_info->state = read_ini_install_state(file, pack, "PackState");
  1169. return pack_info;
  1170. }
  1171. static array_header_t *parse_packs(const char *file, const char *str, int shm)
  1172. {
  1173. array_header_t *arr = T_ARRAY_MAKE(-1, sizeof(sp_cfg_pack_info_t*), shm);
  1174. char *packs = _strdup(str);
  1175. if (packs) {
  1176. char *token;
  1177. const char *seps = " \t,";
  1178. token = strtok(packs, seps);
  1179. while (token != NULL) {
  1180. sp_cfg_pack_info_t *pack_info = read_pack_info(file, token, shm);
  1181. if (pack_info) {
  1182. if (shm) {
  1183. SHM_ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  1184. }
  1185. else {
  1186. ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  1187. }
  1188. }
  1189. token = strtok(NULL, seps);
  1190. }
  1191. free(packs);
  1192. }
  1193. return arr;
  1194. }
  1195. static int load_root_ini(sp_dir_t *dir, sp_cfg_root_ini_t *root, const char *file, int shm)
  1196. {
  1197. int rc;
  1198. rc = read_path(dir, file, root, shm);
  1199. if (rc != 0) {
  1200. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read [Path] section failed!");
  1201. }
  1202. if (!ExistsFileA(file)) {
  1203. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("[root.ini] file not exists!");
  1204. //启动一个http server,用于接收终端号
  1205. return Error_NotExist;
  1206. }
  1207. rc = read_terminal(file, root, shm);
  1208. if (rc != 0) {
  1209. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read [Terminal] section failed!");
  1210. return rc;
  1211. }
  1212. return rc;
  1213. }
  1214. static int unload_root_ini(sp_cfg_root_ini_t *root, int shm)
  1215. {
  1216. int i;
  1217. if (shm) {
  1218. shm_free(root->terminal_no);
  1219. shm_free(root->machine_type);
  1220. for (i = 0; i < root->arr_path->nelts; ++i) {
  1221. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  1222. shm_free(path->name);
  1223. shm_free(path->path);
  1224. shm_free(path);
  1225. }
  1226. shm_array_free(root->arr_path);
  1227. } else {
  1228. free(root->terminal_no);
  1229. free(root->machine_type);
  1230. for (i = 0; i < root->arr_path->nelts; ++i) {
  1231. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  1232. free(path->name);
  1233. free(path->path);
  1234. free(path);
  1235. }
  1236. array_free(root->arr_path);
  1237. }
  1238. memset(root, 0, sizeof(sp_cfg_root_ini_t));
  1239. return 0;
  1240. }
  1241. #if defined(_MSC_VER)
  1242. void load_specialRunInfoInCentersetting(const char* fileName)
  1243. {
  1244. array_header_t* arr_section = inifile_read_section_all(fileName);
  1245. g_specialRunInfoArr.clear();
  1246. if (!arr_section)
  1247. return;
  1248. for (int i = 0; i < arr_section->nelts; ++i) {
  1249. char* sec = ARRAY_IDX(arr_section, i, char*);
  1250. array_header_t* arr_key = inifile_read_section_key_all(fileName, sec);
  1251. if (!arr_key) continue;
  1252. for (int j = 0; j < arr_key->nelts; ++j) {
  1253. char* key = ARRAY_IDX(arr_key, j, char*);
  1254. if (_stricmp(key, "specialRunInfo") == 0) {
  1255. std::string secName = sec;
  1256. std::transform(secName.begin(), secName.end(), secName.begin(), ::tolower);
  1257. std::string tmpStr = inifile_read_str(fileName, sec, "specialRunInfo", "");
  1258. if (tmpStr.length() > 0)
  1259. g_specialRunInfoArr[secName] = tmpStr;
  1260. }
  1261. }
  1262. }
  1263. }
  1264. #endif //_MSC_VER
  1265. static inline int shell_ini__load_debug_level(const char* shell_ini_path, const char* entity_name)
  1266. {
  1267. int level = inifile_read_int(shell_ini_path, "Debug", entity_name, 0);
  1268. /*to see: DebugLevelEnum*/
  1269. if (level > 2 || level < 0)
  1270. level = 0;
  1271. return level;
  1272. }
  1273. /*add intrinsic spshell module*/
  1274. static inline void init_shell_module(sp_cfg_shell_ini_t* shell, const CVersionInfo* shell_ver, int mem_trace, int debug_level)
  1275. {
  1276. sp_cfg_shell_entity_t* ent = NULL;
  1277. sp_cfg_shell_module_t* mod = NULL;
  1278. mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
  1279. TOOLKIT_ASSERT(mod != NULL);
  1280. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  1281. sp_version_copy(&mod->version, &shell->software_version);
  1282. mod->author = shm_strdup(shell_ver->strAuthorName);
  1283. mod->company = shm_strdup(shell_ver->strCompanyName);
  1284. mod->name = shm_strdup(SPSHELL_NAME);
  1285. mod->mem_trace = mem_trace;
  1286. mod->idx = shell->arr_module->nelts; //the first mod id is spshell
  1287. /*increment netls at the same time*/
  1288. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  1289. ent = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  1290. memset(ent, 0, sizeof(sp_cfg_shell_entity_t));
  1291. ent->mod = mod;
  1292. ent->name = shm_strdup(SPSHELL_NAME);
  1293. ent->idx = shell->arr_entity->nelts;
  1294. ent->privilege = 1;
  1295. sp_version_init(&ent->version);
  1296. ent->debug_level = debug_level;
  1297. ent->m_startTimes++;
  1298. #if defined(RVC_OS_WIN)
  1299. ZeroMemory(&ent->m_EntityStartTime, sizeof(LARGE_TIME));
  1300. ZeroMemory(&ent->m_EntityStartEndTime, sizeof(LARGE_TIME));
  1301. ZeroMemory(&ent->m_EntityInitEndTime, sizeof(LARGE_TIME));
  1302. SYSTEMTIME t_cur;
  1303. GetLocalTime(&t_cur);
  1304. SystemTimeToFileTime(&t_cur, &ent->m_EntityStartTime.filetimePart);//spshell, idx 0, 初始化启动时间
  1305. sp_cfg_setShellFirstStartTime(ent->m_EntityStartTime);
  1306. ent->m_EntityInitEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000; //FILETIME精度为100纳秒
  1307. #else
  1308. ZeroMemory(&ent->m_EntityStartTime, sizeof(SYSTEMTIME));
  1309. ZeroMemory(&ent->m_EntityStartEndTime, sizeof(SYSTEMTIME));
  1310. ZeroMemory(&ent->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  1311. GetLocalTime(&ent->m_EntityStartTime); //spshell, idx 0, 初始化启动时间
  1312. const ULONGLONG curTickCount = GetTickCount64();
  1313. memcpy(&ent->m_EntityInitEndTime, &curTickCount, sizeof(ULONGLONG));
  1314. #endif //RVC_OS_WIN
  1315. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = ent;
  1316. }
  1317. static inline void shell_ini__get_mod_version(sp_cfg_shell_module_t* mod, const char* mod_path)
  1318. {
  1319. #ifdef _WIN32
  1320. CCodeSignVerify ver;
  1321. CVersionInfo verInfo;
  1322. if (!ver.GetVersionInfo(mod_path, verInfo))
  1323. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read file %s version info fail, %s", mod_path, (const char*)ver.GetErrorMsg());
  1324. mod->author = shm_strdup(verInfo.strAuthorName);
  1325. mod->company = shm_strdup(verInfo.strCompanyName);
  1326. mod->version.major = verInfo.FileVersion.GetMajor();
  1327. mod->version.minor = verInfo.FileVersion.GetMajor();
  1328. mod->version.revision = verInfo.FileVersion.GetRevision();
  1329. mod->version.build = verInfo.FileVersion.GetBuild();
  1330. #endif
  1331. }
  1332. static inline int shell_ini__veritify_core_file_legality(sp_dir_t* dir, sp_cfg_shell_ini_t* shell)
  1333. {
  1334. #ifdef _WIN32
  1335. // SpBase.dll、SpShell.exe、SpHost.exe代码签名校验
  1336. char sal_tmp[MAX_PATH] = { 0 };
  1337. const char* sal_files[] = { "SpBase.dll", "SpHost.exe", "SpShell.exe" };
  1338. int i;
  1339. CCodeSignVerify ver;
  1340. for (i = 0; i < sizeof(sal_files) / sizeof(sal_files[0]); i++) {
  1341. sprintf(sal_tmp, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, sal_files[i]);
  1342. CSignInfo signInfo;
  1343. if (!ver.VerifySignature(sal_tmp, signInfo)) {
  1344. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify fail, %s", sal_files[i], (const char*)ver.GetErrorMsg());
  1345. return Error_FailVerify;
  1346. }
  1347. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", sal_files[i],
  1348. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  1349. if (shell->spbase_sign_cert_hash == NULL) {
  1350. shell->spbase_sign_cert_hash = shm_strdup(signInfo.strSignCertHash);
  1351. }
  1352. else if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
  1353. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sign cert hash not equal, last : [%s], current: [%s]", shell->spbase_sign_cert_hash, (const char*)signInfo.strSignCertHash);
  1354. return Error_FailVerify;
  1355. }
  1356. }
  1357. #endif
  1358. return 0;
  1359. }
  1360. static inline int shell_ini__vertify_mod_legality(sp_cfg_shell_ini_t* shell, const char* lib_file_path, const char* mod_name)
  1361. {
  1362. int rc = 0;
  1363. #ifdef _WIN32
  1364. CCodeSignVerify ver;
  1365. CSignInfo signInfo;
  1366. if (!ver.VerifySignature(lib_file_path, signInfo)) {
  1367. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify fail, %s", mod_name, (const char*)ver.GetErrorMsg());
  1368. rc = Error_FailVerify;
  1369. }
  1370. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", mod_name,
  1371. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  1372. if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
  1373. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("entity %s sign cert hash [%s] not equal to spbase sign cert hash [%s]",
  1374. mod_name,
  1375. (const char*)signInfo.strSignCertHash,
  1376. shell->spbase_sign_cert_hash);
  1377. rc = Error_FailVerify;
  1378. }
  1379. #endif
  1380. return rc;
  1381. }
  1382. static int shell_ini__load_entities_list(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, sp_cfg_root_ini_t* root, bool vertify_or_not,
  1383. std::map<std::string, std::string> &entityArr, std::map<std::string, int> &memTraceArr, std::map<std::string, int> &debugLevelArr,
  1384. std::vector<std::string> &exist_entity)
  1385. {
  1386. int rc = 0;
  1387. do {
  1388. for (auto cur_it = entityArr.begin(); cur_it != entityArr.end(); cur_it++) {
  1389. char tmp[MAX_PATH];
  1390. int ret;
  1391. if (exist_entity.end() != std::find(exist_entity.begin(), exist_entity.end(), cur_it->first))
  1392. continue;
  1393. sp_cfg_shell_entity_t* entity = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  1394. memset(entity, 0, sizeof(sp_cfg_shell_entity_t));
  1395. entity->name = shm_strdup(cur_it->first.c_str());
  1396. if (!verify_entity_name(entity->name)) {
  1397. rc = Error_Unexpect;
  1398. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("verify entity %s name failed!", entity->name);
  1399. break;
  1400. }
  1401. ZeroMemory(&entity->m_EntityStartTime, sizeof(SYSTEMTIME));
  1402. ZeroMemory(&entity->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  1403. ZeroMemory(&entity->m_EntityStartEndTime, sizeof(SYSTEMTIME));//初始化实体启动时间
  1404. #if defined(_MSC_VER)
  1405. entity->m_EntityStartTime.longPart = entity->m_EntityInitEndTime.longPart = entity->m_EntityStartEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart;
  1406. #endif //_MSC_VER
  1407. entity->debug_level = debugLevelArr.find(entity->name) == debugLevelArr.end() ? 0 : debugLevelArr[cur_it->first];
  1408. entity->log_record_level = getEntityLogLevel(entity->name);
  1409. entity->runType = 1;//1,Debug;2,Release
  1410. //judge if run in release or debug
  1411. std::string tmpEntityName = entity->name, entityInfo = "";
  1412. std::transform(tmpEntityName.begin(), tmpEntityName.end(), tmpEntityName.begin(), ::tolower);
  1413. #if defined(_MSC_VER)
  1414. if (g_specialRunInfoArr.find(tmpEntityName) != g_specialRunInfoArr.end())
  1415. entityInfo = g_specialRunInfoArr[tmpEntityName];
  1416. else
  1417. entityInfo = cur_it->second;
  1418. #else
  1419. entityInfo = cur_it->second;
  1420. #endif
  1421. int pos1 = entityInfo.find("Debug"), pos2 = entityInfo.find("Release");
  1422. if (pos1 > 0 || pos2 > 0) {
  1423. char runInfo[MAX_PATH] = "";
  1424. ret = sscanf(entityInfo.c_str(), "%d,%[^,],%x, %s", &entity->privilege, tmp, &entity->devel_id, runInfo);
  1425. if (CSimpleStringA(runInfo).Compare("Release") == 0)
  1426. entity->runType = 2;
  1427. ret = 3;
  1428. }
  1429. else
  1430. ret = sscanf(entityInfo.c_str(), "%d,%[^,],%x", &entity->privilege, tmp, &entity->devel_id);
  1431. if (ret == 3) {
  1432. sp_cfg_shell_module_t* mod;
  1433. if (str_has_suffix(tmp, MOD_LIB_SUFFIX)) {
  1434. const int len = strlen(tmp);
  1435. tmp[len - strlen(MOD_LIB_SUFFIX)] = 0;
  1436. }
  1437. mod = find_module(shell, tmp);
  1438. if (!mod) {
  1439. char mod_lib_path[MAX_PATH];
  1440. const int len = (strstr(tmp, "lib") == tmp) ? 3 : 0;
  1441. mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
  1442. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  1443. mod->name = shm_strdup(tmp + len);
  1444. CSimpleString dstMod;
  1445. #if defined(_MSC_VER)
  1446. dstMod = CSimpleString::Format("%s.dll", tmp);
  1447. #else
  1448. dstMod = CSimpleString::Format("lib%s.so", tmp);
  1449. #endif //_MSC_VER
  1450. sprintf(mod_lib_path, "%s" SPLIT_SLASH_STR "%s", dir->mod_path, dstMod.GetData());
  1451. mod->debugFileExist = EntityResource::getSaveFile();
  1452. if (vertify_or_not) {
  1453. rc = shell_ini__vertify_mod_legality(shell, mod_lib_path, mod->name);
  1454. }
  1455. if (rc == 0) {
  1456. shell_ini__get_mod_version(mod, mod_lib_path);
  1457. #if defined(_MSC_VER)
  1458. mod->runType = entity->runType;
  1459. #endif //_MSC_VER
  1460. mod->mem_trace = memTraceArr.find(entity->name) == memTraceArr.end() ? 0 : memTraceArr[cur_it->first];
  1461. mod->idx = shell->arr_module->nelts;
  1462. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("mod idx, %s : %d", entity->name, mod->idx);
  1463. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  1464. }
  1465. else {
  1466. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sal verify file %s failed!", mod_lib_path);
  1467. destroy_module(mod);
  1468. destroy_entity(entity);
  1469. break;
  1470. }
  1471. }
  1472. entity->mod = mod;
  1473. entity->idx = shell->arr_entity->nelts;
  1474. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity idx, %s : %d", entity->name, entity->idx);
  1475. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = entity;
  1476. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity: %s, id: %d", entity->name, entity->idx);
  1477. }
  1478. else {
  1479. std::string entityInfo = cur_it->second;
  1480. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read entity line failed! bad format!, EntityName:%s, info:%s", entity->name, entityInfo.c_str());
  1481. rc = Error_Param;
  1482. destroy_entity(entity);
  1483. break;
  1484. }
  1485. }
  1486. if (rc != 0)
  1487. break;
  1488. } while (false);
  1489. return rc;
  1490. }
  1491. static int shell_ini__init_sysevent(sp_cfg_shell_ini_t* shell)
  1492. {
  1493. int rc = 0;
  1494. if (NULL == shell->arr_sysevent)
  1495. {
  1496. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));
  1497. rc = 1;
  1498. }
  1499. else if (shell->arr_sysevent->nelts == 0) {
  1500. rc = 1;
  1501. }
  1502. if (rc == 1) {
  1503. //单独添加一个框架级别的系统变量
  1504. sp_cfg_shell_sysevent_t* sysevent = T_MALLOC_T(sp_cfg_shell_sysevent_t, 1);
  1505. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  1506. sysevent->name = T_STRDUP(VAR_RSERVERD_KEY_TERM_STATE, 1);
  1507. sysevent->init_value[0] = '0', sysevent->init_value[1] = '\0';
  1508. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1509. sp_cfg_shell_entity_t* owner_entity = find_entity(shell, SPSHELL_NAME);
  1510. if (owner_entity) {
  1511. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("add framework level sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
  1512. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  1513. sysevent->idx = shell->arr_sysevent->nelts;
  1514. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  1515. }
  1516. else {
  1517. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cannot find specified entity for sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
  1518. destroy_sysevent(sysevent);
  1519. }
  1520. }
  1521. return rc;
  1522. }
  1523. static int shell_ini__load_sysevent(sp_cfg_shell_ini_t* shell, const char* shell_var_path, std::map<std::string, std::string> &sysEventArr)
  1524. {
  1525. int rc = 0;
  1526. shell_ini__init_sysevent(shell);
  1527. for (auto it = sysEventArr.begin(); it != sysEventArr.end(); it++)
  1528. {
  1529. char tmp[1024], tmp2[1024];
  1530. char initVar[SP_CFG_MAX_SYSEVT_BUF];
  1531. sp_cfg_shell_sysevent_t* sysevent = (sp_cfg_shell_sysevent_t*)shm_malloc(sizeof(sp_cfg_shell_sysevent_t));
  1532. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  1533. sysevent->name = shm_strdup(it->first.c_str());
  1534. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1535. int ret = sscanf(it->second.c_str(), "%[^,], \"%[^\"]", tmp, sysevent->init_value);
  1536. auto varRet = inifile_format_read(shell_var_path, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp2, initVar);
  1537. if (varRet == 2) {
  1538. /** 缺陷修复,以shell.ini的实体内容为准 Gifur@2023426]*/
  1539. /**TODO(Gifur): BUG: 如果在已有的系统变量上添加已有的实体,会因运行时存在文件而导致添加失败 [5/23/2020 Gifur] */
  1540. //memcpy(tmp, tmp2, sizeof(tmp));
  1541. memcpy(sysevent->init_value, initVar, sizeof(sysevent->init_value)); //如果在shellvar.ini中也能找到该数据,则替换该数值
  1542. //情况1:shell.ini添加一个长期有效变量,则在write时在shellvar.ini中生成一个相应的副本。下次读取时从shellvar.ini中读取
  1543. //情况2:shell.ini中删除一个长期有效变量,shellvar.ini中并不会相应删除.优先从shell.ini中读取相应变量
  1544. }
  1545. // 为安全起见,准入实体状态强制设为初始状态I
  1546. if (stricmp(sysevent->name, "EntryPermit") == 0)
  1547. strcpy(sysevent->init_value, "I");
  1548. if (ret > 0) {
  1549. const char* seps = ";\t ";
  1550. char* tok = strtok(tmp, seps);
  1551. while (tok) {
  1552. sp_cfg_shell_entity_t* owner_entity = find_entity(shell, tok);
  1553. if (owner_entity) {
  1554. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  1555. }
  1556. else {
  1557. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cannot find entity %s", tok);
  1558. rc = Error_NotExist;
  1559. destroy_sysevent(sysevent);
  1560. break;
  1561. }
  1562. tok = strtok(NULL, seps);
  1563. }
  1564. if (rc != 0)
  1565. break;
  1566. sysevent->idx = shell->arr_sysevent->nelts;
  1567. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  1568. }
  1569. else {
  1570. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read SysEvent failed! bad format!");
  1571. rc = Error_Param;
  1572. destroy_sysevent(sysevent);
  1573. break;
  1574. }
  1575. }
  1576. return rc;
  1577. }
  1578. static int load_entity_list_byHttp(sp_cfg_shell_ini_t* shell, array_header_t** arr, int test_mode, std::map<std::string, std::string> &startupArr)
  1579. {
  1580. int rc = Error_Succeed;
  1581. char section_name[128] = {SHELL_STARTUP_HEAD};
  1582. if (test_mode) {
  1583. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("test mode: override startup list");
  1584. strcpy(section_name, "Test");
  1585. }
  1586. TOOLKIT_ASSERT(arr);
  1587. TOOLKIT_ASSERT(*arr == NULL);
  1588. *arr = shm_array_make(startupArr.size(), sizeof(char*));
  1589. TOOLKIT_ASSERT(*arr);
  1590. for (auto it = startupArr.begin(); it != startupArr.end(); it++)
  1591. {
  1592. std::string startupEntity = it->second;
  1593. sp_cfg_shell_entity_t* ent = find_entity(shell, startupEntity.c_str());
  1594. char* s = _strdup(startupEntity.c_str());
  1595. char* cmdline = strchr(s, ' ');
  1596. if (s) {
  1597. if (strlen(s)) {
  1598. int numargs, numchars;
  1599. char** argv = NULL;
  1600. char* p;
  1601. sp_cfg_shell_entity_t* ent;
  1602. char* cmdline = strchr(s, ' ');
  1603. if (cmdline) {
  1604. *cmdline = 0;
  1605. cmdline++;
  1606. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  1607. p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
  1608. argv = (char**)p;
  1609. str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
  1610. }
  1611. ent = find_entity(shell, s);
  1612. if (ent) {
  1613. if (cmdline) {
  1614. //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
  1615. ent->argc = numargs;
  1616. ent->argv = argv;
  1617. if (numargs) {
  1618. ent->cmdline = shm_strdup(cmdline);
  1619. }
  1620. }
  1621. SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
  1622. }
  1623. else {
  1624. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! find startup list [%s] defined entity [%s] fail!",
  1625. it->first.c_str(), s);
  1626. rc = Error_Param;
  1627. }
  1628. }
  1629. else {
  1630. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", it->first.c_str());
  1631. rc = Error_Param;
  1632. }
  1633. FREE(s);
  1634. }
  1635. else {
  1636. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", it->first.c_str());
  1637. rc = Error_Param;
  1638. }
  1639. FREE(s);
  1640. if (rc != 0)
  1641. break;
  1642. }
  1643. return rc;
  1644. }
  1645. static int shell_ini__load_entity_list(sp_cfg_shell_ini_t* shell, array_header_t** arr, const char* shell_ini_path, int test_mode)
  1646. {
  1647. int i;
  1648. int rc = 0;
  1649. char section_name[128] = { "Startup" };
  1650. if (test_mode) {
  1651. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("test mode: override startup list");
  1652. strcpy(section_name, "Test");
  1653. }
  1654. TOOLKIT_ASSERT(arr);
  1655. TOOLKIT_ASSERT(*arr == NULL);
  1656. i = inifile_read_int(shell_ini_path, section_name, "Number", 0);
  1657. if (i >= 0) {
  1658. int n = i;
  1659. *arr = shm_array_make(i, sizeof(char*));
  1660. TOOLKIT_ASSERT(*arr);
  1661. for (i = 1; i <= n; ++i) {
  1662. char key[512];
  1663. char* s;
  1664. _itoa(i, key, 10);
  1665. s = inifile_read_str(shell_ini_path, section_name, key, "");
  1666. if (s) {
  1667. if (strlen(s)) {
  1668. int numargs, numchars;
  1669. char** argv = NULL;
  1670. char* p;
  1671. sp_cfg_shell_entity_t* ent;
  1672. char* cmdline = strchr(s, ' ');
  1673. if (cmdline) {
  1674. *cmdline = 0;
  1675. cmdline++;
  1676. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  1677. p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
  1678. argv = (char**)p;
  1679. str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
  1680. }
  1681. ent = find_entity(shell, s);
  1682. if (ent) {
  1683. if (cmdline) {
  1684. //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
  1685. ent->argc = numargs;
  1686. ent->argv = argv;
  1687. if (numargs) {
  1688. ent->cmdline = shm_strdup(cmdline);
  1689. }
  1690. }
  1691. SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
  1692. } else {
  1693. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! find startup list [%s] defined entity [%s] fail!", key, s);
  1694. rc = Error_Param;
  1695. }
  1696. } else {
  1697. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", key);
  1698. rc = Error_Param;
  1699. }
  1700. FREE(s);
  1701. } else {
  1702. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", key);
  1703. rc = Error_Param;
  1704. }
  1705. if (rc != 0)
  1706. break;
  1707. }
  1708. } else {
  1709. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read %s failed! startup number invalid!", section_name);
  1710. rc = Error_Param;
  1711. }
  1712. return rc;
  1713. }
  1714. static inline int shell_ini__load_software_version(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const char* shell_runinfo_file)
  1715. {
  1716. int rc = 0;
  1717. shell->software_version.build = 0;
  1718. if (NULL != shell_runinfo_file)
  1719. rc = read_ini_version(shell_runinfo_file, "Main", "SoftwareVersion",
  1720. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  1721. if (rc != 0)
  1722. rc = read_ini_version(shell_ini_path, "Main", "SoftwareVersion",
  1723. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  1724. if (rc != 0) {
  1725. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Main::SoftwareVersion failed!");
  1726. }
  1727. return rc;
  1728. }
  1729. void init_shellVer(sp_dir_t* dir, CVersionInfo &shellVerInfo)
  1730. {
  1731. #ifdef _WIN32
  1732. CCodeSignVerify signVerify;
  1733. char spshell_path[MAX_PATH] = { 0 };
  1734. sprintf(spshell_path, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, "SpShell.exe");
  1735. if (!signVerify.GetVersionInfo(spshell_path, shellVerInfo)) {
  1736. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read file %s version info fail, %s", spshell_path, (const char*)signVerify.GetErrorMsg());
  1737. }
  1738. #endif //_WIN32
  1739. }
  1740. template<class T>
  1741. std::map<std::string, T> clearUnExistConfig(std::map<std::string, T>& src)
  1742. {
  1743. std::map<std::string, T> dst;
  1744. for (auto it = src.begin(); it != src.end(); it++)
  1745. {
  1746. if (it->first[0] == ';')
  1747. continue;
  1748. else
  1749. dst.insert(std::make_pair(it->first, it->second));
  1750. }
  1751. return dst;
  1752. }
  1753. int init_defaultShellCfg(sp_dir_t* dir, sp_cfg_t* cfg)
  1754. {
  1755. char shellvar_path[MAX_PATH] = "";
  1756. auto rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shellvar_path, MAX_PATH);
  1757. if (rc != 0) {
  1758. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  1759. return rc;
  1760. }
  1761. std::map<std::string, std::map<std::string, std::string>> default_shellConfig;
  1762. std::map<std::string, std::string> mainSection;
  1763. mainSection.insert(std::make_pair("SoftwareVersion", "1.1.0"));
  1764. default_shellConfig.insert(std::make_pair("Main", mainSection));
  1765. std::map<std::string, std::string> entitySection;
  1766. entitySection.insert(std::make_pair("VtmLoader", "1,mod_VtmLoader,0x10F"));
  1767. entitySection.insert(std::make_pair("Chromium", "1,mod_chromium,0x402"));
  1768. entitySection.insert(std::make_pair("GUIConsole", "1,mod_GUIConsole,0x508"));
  1769. default_shellConfig.insert(std::make_pair("Entity", entitySection));
  1770. std::map<std::string, std::string> startupSection;
  1771. long long index = 1;
  1772. //should not change those orders
  1773. startupSection.insert(std::make_pair(std::to_string(index++), "GUIConsole"));
  1774. startupSection.insert(std::make_pair(std::to_string(index++), "Chromium"));
  1775. startupSection.insert(std::make_pair(std::to_string(index++), "VtmLoader"));
  1776. default_shellConfig.insert(std::make_pair("Startup", startupSection));
  1777. return init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, default_shellConfig);
  1778. }
  1779. ErrorCodeEnum init_shell_byHttp(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, sp_cfg_root_ini_t* root, const sp_cfg_start_args_t* args,
  1780. const char* shellVarIni, std::map<std::string, std::map<std::string, std::string>> shellConfig)
  1781. {
  1782. int rc = -1;
  1783. if(shellConfig.find(SHELL_MAIN_HEAD) != shellConfig.end() && shellConfig[SHELL_MAIN_HEAD].find(SHELL_MAIN_SOFTWAREVERSION) != shellConfig[SHELL_MAIN_HEAD].end())
  1784. sscanf(shellConfig[SHELL_MAIN_HEAD][SHELL_MAIN_SOFTWAREVERSION].c_str(), "%d.%d.%d",
  1785. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision);
  1786. else
  1787. {
  1788. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s::%s", SHELL_MAIN_HEAD, SHELL_MAIN_SOFTWAREVERSION);
  1789. return Error_NotConfig;
  1790. }
  1791. if (shellConfig.find(SHELL_ENTITY_HEAD) == shellConfig.end())
  1792. {
  1793. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_ENTITY_HEAD);
  1794. return Error_NotConfig;
  1795. }
  1796. if (args->sign_verifity && (rc = shell_ini__veritify_core_file_legality(dir, shell)) != 0)
  1797. return Error_NotConfig;
  1798. CVersionInfo shellVerInfo;
  1799. init_shellVer(dir, shellVerInfo);
  1800. //init spshell module
  1801. std::vector<std::string> exist_entity, exist_module;
  1802. if(NULL == shell->arr_entity)
  1803. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1804. else
  1805. {
  1806. sp_cfg_shell_entity_t* entity;
  1807. int i;
  1808. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  1809. exist_entity.push_back(entity->name);
  1810. }
  1811. }
  1812. if(NULL == shell->arr_module)
  1813. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  1814. else
  1815. {
  1816. sp_cfg_shell_module_t* entity;
  1817. int i;
  1818. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_module, sp_cfg_shell_module_t*) {
  1819. exist_module.push_back(entity->name);
  1820. }
  1821. }
  1822. //init entity arr,去除以;开头的实体
  1823. std::map<std::string, std::string> entityArr = clearUnExistConfig(shellConfig[SHELL_ENTITY_HEAD]);
  1824. #ifdef _WIN32
  1825. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", SHELL_ENTITY_HEAD, root->machine_type);
  1826. #else
  1827. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", "UOSEntity", root->machine_type);
  1828. #endif
  1829. if (shellConfig.find(sectionNameWithType.GetData()) != shellConfig.end()) //有带机型的实体
  1830. {
  1831. std::map<std::string, std::string> entityTypeArr = clearUnExistConfig(shellConfig[sectionNameWithType.GetData()]);
  1832. for (auto it = entityTypeArr.begin(); it != entityTypeArr.end(); it++)
  1833. entityArr.insert(std::make_pair(it->first, it->second));
  1834. }
  1835. else
  1836. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find section:%s", sectionNameWithType.GetData());
  1837. std::map<std::string, int> memTraceArr, debugLevelArr;
  1838. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1839. {
  1840. int mem_trace = 0, debug_level = 0;
  1841. if (shellConfig.find(SHELL_MEMTRACE_HEAD) != shellConfig.end() && shellConfig[SHELL_MEMTRACE_HEAD].find(it->first) != shellConfig[SHELL_MEMTRACE_HEAD].end())
  1842. mem_trace = std::atoi(shellConfig[SHELL_MEMTRACE_HEAD][it->first].c_str());
  1843. else
  1844. mem_trace = 0;
  1845. if (shellConfig.find(SHELL_DEBUG_HEAD) != shellConfig.end() && shellConfig[SHELL_DEBUG_HEAD].find(it->first) != shellConfig[SHELL_DEBUG_HEAD].end())
  1846. debug_level = std::atoi(shellConfig[SHELL_DEBUG_HEAD][it->first].c_str());
  1847. else
  1848. debug_level = 0;
  1849. memTraceArr.insert(std::make_pair(it->first, mem_trace));
  1850. debugLevelArr.insert(std::make_pair(it->first, debug_level));
  1851. }
  1852. memTraceArr = clearUnExistConfig(memTraceArr);
  1853. debugLevelArr = clearUnExistConfig(debugLevelArr);
  1854. int mem_trace = 0, debug_level = 0;
  1855. if (exist_entity.end() == std::find(exist_entity.begin(), exist_entity.end(), "Spshell"))
  1856. {
  1857. if (shellConfig.find(SHELL_MEMTRACE_HEAD) != shellConfig.end() && shellConfig[SHELL_MEMTRACE_HEAD].find(SPSHELL_NAME) != shellConfig[SHELL_MEMTRACE_HEAD].end())
  1858. mem_trace = std::atoi(shellConfig[SHELL_MEMTRACE_HEAD][SPSHELL_NAME].c_str());
  1859. if (shellConfig.find(SHELL_DEBUG_HEAD) != shellConfig.end() && shellConfig[SHELL_DEBUG_HEAD].find(SPSHELL_NAME) != shellConfig[SHELL_DEBUG_HEAD].end())
  1860. debug_level = std::atoi(shellConfig[SHELL_DEBUG_HEAD][SPSHELL_NAME].c_str());
  1861. init_shell_module(shell, &shellVerInfo, mem_trace, debug_level);
  1862. }
  1863. if((rc = shell_ini__load_entities_list(dir, shell, root, args->sign_verifity, entityArr, memTraceArr, debugLevelArr, exist_entity)))
  1864. return Error_NotConfig;
  1865. //load sysEventArr
  1866. if (shellConfig.find(SHELL_SYSEVENT_HEAD) == shellConfig.end())
  1867. {
  1868. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_SYSEVENT_HEAD);
  1869. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));//避免错误,也需要建立一个相关sysevnet存储
  1870. }
  1871. else
  1872. {
  1873. std::map<std::string, std::string> sysEventArr = shellConfig[SHELL_SYSEVENT_HEAD];
  1874. if ((rc = shell_ini__load_sysevent(shell, shellVarIni, sysEventArr)) != 0) {
  1875. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("load sysevent from shellvar.ini fail");
  1876. return Error_NotConfig;
  1877. }
  1878. }
  1879. //init entity debug mode
  1880. if (args->debug_mode && shell->arr_entity) {
  1881. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override entities' debug level!");
  1882. for (int i = 0; i < shell->arr_entity->nelts; ++i) {
  1883. sp_cfg_shell_entity_t* entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  1884. entity->debug_level = 2; /*Debug_High*/
  1885. }
  1886. }
  1887. //init startup entity
  1888. if (shellConfig.find(SHELL_STARTUP_HEAD) == shellConfig.end())
  1889. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_STARTUP_HEAD);
  1890. else if (shell->arr_startlist == NULL)
  1891. {
  1892. std::map<std::string, std::string> startupArr = shellConfig[SHELL_STARTUP_HEAD];
  1893. if (args->start_entities != NULL) {
  1894. char* value;
  1895. char* name;
  1896. char* context = NULL;
  1897. array_header_t* t = NULL;
  1898. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override start entity list!");
  1899. value = _strdup(args->start_entities);
  1900. if (!value) {
  1901. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("_strdup start_entities failed!");
  1902. return Error_NotConfig;
  1903. }
  1904. if(NULL ==shell->arr_startlist)
  1905. shell->arr_startlist = shm_array_make(1, sizeof(char*));
  1906. name = strtok_s(value, ";", &context);
  1907. while (name != NULL) {
  1908. sp_cfg_shell_entity_t* ent = find_entity(shell, name);
  1909. if (ent)
  1910. SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
  1911. else {
  1912. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cannot find entity name %s defined at cmdline!", name);
  1913. rc = Error_Param;
  1914. }
  1915. name = strtok_s(NULL, ";", &context);
  1916. }
  1917. free(value);
  1918. if (rc != 0)
  1919. return Error_NotConfig;
  1920. }
  1921. else if ((rc = load_entity_list_byHttp(shell, &shell->arr_startlist, 0, startupArr)) != 0)
  1922. return Error_NotConfig;
  1923. if (is_own_test_mode(args->test_mode)) {
  1924. array_header_t* arr = NULL;
  1925. if ((rc = load_entity_list_byHttp(shell, &arr, 1, startupArr)) != 0)
  1926. return Error_NotConfig;
  1927. if (arr && arr->nelts > 0) {
  1928. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("append additional test entity: %d", arr->nelts);
  1929. shm_array_cat(shell->arr_startlist, arr);
  1930. }
  1931. shm_array_free(arr);
  1932. }
  1933. }
  1934. shell->shell_debug_level = args->debug_mode ? 2 : debug_level;
  1935. shell->log_record_level = getEntityLogLevel(SPSHELL_NAME);
  1936. if (args->telnet_port == 0)
  1937. shell->nConsolePort = shellConfig[SHELL_MAIN_HEAD].find(SHELL_MAIN_CONSOLEPORT) != shellConfig[SHELL_MAIN_HEAD].end() ?
  1938. atoi(shellConfig[SHELL_MAIN_HEAD][SHELL_MAIN_CONSOLEPORT].c_str()) : 0;
  1939. else {
  1940. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override telnet console port: %d", args->telnet_port);
  1941. shell->nConsolePort = args->telnet_port;
  1942. }
  1943. //init kill arr
  1944. if (shellConfig.find(SHELL_KILLPROCESS_HEAD) == shellConfig.end())
  1945. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_KILLPROCESS_HEAD);
  1946. else
  1947. {
  1948. std::map<std::string, std::string> killArr = shellConfig[SHELL_KILLPROCESS_HEAD];
  1949. g_arrKill.clear();
  1950. for (auto it = killArr.begin(); it != killArr.end(); it++)
  1951. g_arrKill.push_back(it->second);
  1952. }
  1953. //init start arr
  1954. if (shellConfig.find(SHELL_RUNSCRIPT_HEAD) == shellConfig.end())
  1955. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_RUNSCRIPT_HEAD);
  1956. else
  1957. {
  1958. std::map<std::string, std::string> startArr = shellConfig[SHELL_RUNSCRIPT_HEAD];
  1959. g_arrStart.clear();
  1960. for (auto it = startArr.begin(); it != startArr.end(); it++)
  1961. g_arrStart.push_back(it->second);
  1962. }
  1963. return Error_Succeed;
  1964. }
  1965. static int shell_ini__load_mod_entity(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, const char* shell_ini_path, sp_cfg_root_ini_t* root, const sp_cfg_start_args_t* args, const CVersionInfo* ver)
  1966. {
  1967. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1968. // BugFix: replace char* with sp_cfg_shell_module_t* [Gifur@2020422]
  1969. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  1970. std::map<std::string, std::string> entityArr, entityInfoArr;
  1971. std::map<std::string, int> memTraceArr, debugLevelArr;
  1972. //init Entity
  1973. char sectionName[MAX_PATH] = "Entity";
  1974. array_header_t* arr = inifile_read_section_key_all(shell_ini_path, sectionName);
  1975. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", sectionName, root->machine_type);
  1976. array_header_t* arr2 = inifile_read_section_key_all(shell_ini_path, sectionNameWithType);
  1977. for (int i = 0; i < arr->nelts; ++i)
  1978. entityArr.insert(std::make_pair(ARRAY_IDX(arr, i, char*), sectionName));
  1979. for (int i = 0; i < arr2->nelts; ++i)
  1980. entityArr.insert(std::make_pair(ARRAY_IDX(arr2, i, char*), sectionNameWithType));
  1981. if (arr)
  1982. toolkit_array_free2(arr);
  1983. if (arr2)
  1984. toolkit_array_free2(arr2);
  1985. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1986. entityInfoArr.insert(std::make_pair(it->first, inifile_read_str(shell_ini_path, it->second.c_str(), it->first.c_str(), "")));
  1987. //init memTraceArr
  1988. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1989. memTraceArr.insert(std::make_pair(it->second, inifile_read_int(shell_ini_path, "MemTrace", it->second.c_str(), 0)));
  1990. //init debugLevel
  1991. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1992. debugLevelArr.insert(std::make_pair(it->second, shell_ini__load_debug_level(shell_ini_path, it->second.c_str())));
  1993. //init shell module
  1994. memTraceArr.insert(std::make_pair(SPSHELL_NAME, inifile_read_int(shell_ini_path, "MemTrace", SPSHELL_NAME, 0)));
  1995. debugLevelArr.insert(std::make_pair(SPSHELL_NAME, shell_ini__load_debug_level(shell_ini_path, SPSHELL_NAME)));
  1996. init_shell_module(shell, ver, memTraceArr[SPSHELL_NAME], debugLevelArr[SPSHELL_NAME]);
  1997. std::vector<std::string> null_arr;
  1998. return shell_ini__load_entities_list(dir, shell, root, args->sign_verifity, entityInfoArr, memTraceArr, debugLevelArr, null_arr);
  1999. }
  2000. static int unload_shell_ini(sp_cfg_shell_ini_t *shell)
  2001. {
  2002. int i;
  2003. if (shell->arr_entity) {
  2004. for (i = 0; i< shell->arr_entity->nelts; ++i) {
  2005. sp_cfg_shell_entity_t *entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  2006. destroy_entity(entity);
  2007. }
  2008. shm_array_free(shell->arr_entity);
  2009. }
  2010. if (shell->arr_module) {
  2011. for (i = 0; i < shell->arr_module->nelts; ++i) {
  2012. sp_cfg_shell_module_t *mod = ARRAY_IDX(shell->arr_module, i, sp_cfg_shell_module_t*);
  2013. destroy_module(mod);
  2014. }
  2015. shm_array_free(shell->arr_module);
  2016. }
  2017. if (shell->arr_sysevent) {
  2018. for (i = 0; i < shell->arr_sysevent->nelts; ++i) {
  2019. sp_cfg_shell_sysevent_t *sysevent = ARRAY_IDX(shell->arr_sysevent, i, sp_cfg_shell_sysevent_t*);
  2020. destroy_sysevent(sysevent);
  2021. }
  2022. shm_array_free(shell->arr_sysevent);
  2023. }
  2024. if (shell->arr_startlist) {
  2025. shm_array_free(shell->arr_startlist);
  2026. }
  2027. return 0;
  2028. }
  2029. static int load_install_ini(sp_dir_t *dir, sp_cfg_install_ini_t *inst, const char *file, int shm)
  2030. {
  2031. int rc = 0;
  2032. array_header_t *arr_section = NULL;
  2033. int nLastStartTime =0;
  2034. sp_version_t active_version = { 0 };
  2035. rc = read_ini_version(file, "Main", "InstallVersion",
  2036. &inst->install_version.major,
  2037. &inst->install_version.minor,
  2038. &inst->install_version.revision,
  2039. &inst->install_version.build);
  2040. if (rc != 0) {
  2041. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::InstallVersion failed!", file);
  2042. return rc;
  2043. }
  2044. // 校验版本目录名与软件版本是否一致
  2045. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2046. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  2047. if (sp_version_cmp(&active_version, &inst->install_version) != 0)
  2048. {
  2049. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("version dir %d.%d.%d.%d not equals install version %d.%d.%d.%d",
  2050. active_version.major, active_version.minor, active_version.revision, active_version.build,
  2051. inst->install_version.major, inst->install_version.minor, inst->install_version.revision, inst->install_version.build);
  2052. return Error_NotMeetCondition;
  2053. }
  2054. inst->install_time = inifile_read_int(file, "Main", "CreateDate", 0);
  2055. if (inst->install_time == 0) {
  2056. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::CreateDate failed", file);
  2057. return -1;
  2058. }
  2059. rc = read_ini_version(file, "Main", "LatterInstallVersion",
  2060. &inst->latter_install_version.major,
  2061. &inst->latter_install_version.minor,
  2062. &inst->latter_install_version.revision,
  2063. &inst->latter_install_version.build);
  2064. if (rc != 0) {
  2065. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::LatterInstallVersion failed!", file);
  2066. return rc;
  2067. }
  2068. rc = read_ini_str(file, "Main", "LightPack", &inst->light_packs, shm);
  2069. if (rc != 0 || inst->light_packs == NULL) {
  2070. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::LightPack", file);
  2071. return rc;
  2072. }
  2073. inst->arr_light_pack = parse_packs(file, inst->light_packs, shm);
  2074. nLastStartTime = inifile_read_int(file, "Main", "CurrentTime", 0);
  2075. inst->current_startup_time = y2k_time_now();
  2076. {
  2077. char tmp[32];
  2078. sprintf(tmp, "0x%08X", inst->current_startup_time);
  2079. inifile_write_str(file, "Main", "CurrentTime", tmp);
  2080. }
  2081. inst->today_run_count = inifile_read_int(file, "Main", "TodayRunCount", 0);
  2082. if (inst->today_run_count >=0)
  2083. {
  2084. if (y2k_time_is_today(nLastStartTime))
  2085. inst->today_run_count++;
  2086. else
  2087. inst->today_run_count = 1;
  2088. inifile_write_int(file, "Main", "TodayRunCount", inst->today_run_count);
  2089. }
  2090. inst->total_run_count = inifile_read_int(file, "Main", "TotalRunCount", 0);
  2091. if (inst->total_run_count >=0)
  2092. {
  2093. inst->total_run_count++;
  2094. inifile_write_int(file, "Main", "TotalRunCount", inst->total_run_count);
  2095. }
  2096. // read all history version
  2097. arr_section = inifile_read_section_all(file);
  2098. if (arr_section) {
  2099. int i;
  2100. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), shm);
  2101. for (i = 0; i < arr_section->nelts; ++i) {
  2102. char *sec = ARRAY_IDX(arr_section, i, char*);
  2103. if (_stricmp(sec, "Main") != 0) {
  2104. if (isdigit(sec[0]) && ('.' == sec[1] || '.' == sec[2])) //
  2105. { // skip light pack
  2106. sp_cfg_version_info_t *ver_info = T_MALLOC_T(sp_cfg_version_info_t, shm);
  2107. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  2108. sscanf(sec, "%d.%d.%d.%d", &ver_info->version.major, &ver_info->version.minor,
  2109. &ver_info->version.revision, &ver_info->version.build);
  2110. rc = read_ini_version(file, sec, "PreviousInstallVersion",
  2111. &ver_info->previous_version.major, &ver_info->previous_version.minor,
  2112. &ver_info->previous_version.revision, &ver_info->previous_version.build);
  2113. if (rc != 0)
  2114. break;
  2115. rc = read_ini_str(file, sec, "InstallPack", &ver_info->install_pack, shm);
  2116. if (rc != 0)
  2117. break;
  2118. // 轻量安装包没有安装历史
  2119. //ver_info->arr_pack = parse_packs(file, ver_info->packs, shm);
  2120. ver_info->switch_time = inifile_read_int(file, sec, "SwitchOverDate", 0);
  2121. ver_info->install_state = read_ini_install_state(file, sec, "InstallState");
  2122. if (ver_info->install_state == -1)
  2123. break;
  2124. if (shm) {
  2125. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2126. } else {
  2127. ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2128. }
  2129. }
  2130. }
  2131. }
  2132. toolkit_array_free2(arr_section);
  2133. } else {
  2134. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s, section all failed", file);
  2135. return -1;
  2136. }
  2137. //////////////////////////////
  2138. return rc;
  2139. }
  2140. static void unload_install_ini(sp_cfg_install_ini_t *inst, int shm)
  2141. {
  2142. //...
  2143. }
  2144. /**直接从当前运行路径中拆分版本号*/
  2145. static int generate_install_memory(sp_dir_t* dir, sp_cfg_install_ini_t* inst)
  2146. {
  2147. if (dir == NULL || inst == NULL)
  2148. return -1;
  2149. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2150. &inst->install_version.major,
  2151. &inst->install_version.minor,
  2152. &inst->install_version.revision,
  2153. &inst->install_version.build);
  2154. y2k_time_t now = y2k_time_now();
  2155. inst->install_time = now;
  2156. inst->latter_install_version.major = inst->latter_install_version.minor = inst->latter_install_version.revision = inst->latter_install_version.build = 0;
  2157. inst->light_packs = shm_strdup("");
  2158. inst->latter_install_pack = shm_strdup("");
  2159. inst->total_run_count = inst->today_run_count = 0;
  2160. inst->current_startup_time = now;
  2161. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), 1);
  2162. sp_cfg_version_info_t* ver_info = T_MALLOC_T(sp_cfg_version_info_t, 1);
  2163. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  2164. ver_info->version.major = inst->install_version.major;
  2165. ver_info->version.minor = inst->install_version.minor;
  2166. ver_info->version.revision = inst->install_version.revision;
  2167. ver_info->version.build = inst->install_version.build;
  2168. ver_info->previous_version.major = ver_info->previous_version.minor = ver_info->previous_version.build = ver_info->previous_version.revision = 0;
  2169. ver_info->switch_time = now;
  2170. ver_info->install_state = Install_Active;
  2171. ver_info->install_pack = shm_strdup("");
  2172. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2173. inst->arr_light_pack = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), 1);
  2174. return 0;
  2175. }
  2176. int sp_cfg_create(sp_dir_t* dir, const sp_cfg_start_args_t* args, sp_cfg_t** p_cfg)
  2177. {
  2178. sp_cfg_t *cfg;
  2179. int rc = Error_Unexpect;
  2180. char tmp[MAX_PATH], shell_var_path[MAX_PATH], env_path[MAX_PATH];
  2181. char* shellvarPath = NULL;
  2182. cfg = (sp_cfg_t *)shm_malloc(sizeof(sp_cfg_t));
  2183. memset(cfg, 0, sizeof(sp_cfg_t));
  2184. cfg->root_ini = (sp_cfg_root_ini_t *)shm_malloc(sizeof(sp_cfg_root_ini_t));
  2185. memset(cfg->root_ini, 0, sizeof(sp_cfg_root_ini_t));
  2186. cfg->shell_ini = (sp_cfg_shell_ini_t *)shm_malloc(sizeof(sp_cfg_shell_ini_t));
  2187. memset(cfg->shell_ini, 0, sizeof(sp_cfg_shell_ini_t));
  2188. cfg->install_ini = (sp_cfg_install_ini_t*)shm_malloc(sizeof(sp_cfg_install_ini_t));
  2189. memset(cfg->install_ini, 0, sizeof(sp_cfg_install_ini_t));
  2190. cfg->run_info = (sp_cfg_run_info_t*)shm_malloc(sizeof(sp_cfg_run_info_t));
  2191. memset(cfg->run_info, 0, sizeof(sp_cfg_run_info_t));
  2192. {
  2193. cfg->args = (sp_cfg_start_args_t*)shm_malloc(sizeof(sp_cfg_start_args_t));
  2194. memset(cfg->args, 0, sizeof(sp_cfg_start_args_t));
  2195. cfg->args->start_entities = shm_strdup(args->start_entities);
  2196. cfg->args->test_mode = args->test_mode;
  2197. #ifndef DEVOPS_ON_PRD
  2198. //非生产情况下,才支持测试模式
  2199. cfg->args->auto_test = args->auto_test;
  2200. #endif
  2201. cfg->args->debug_mode = args->debug_mode;
  2202. cfg->args->guardian_mode = args->guardian_mode;
  2203. cfg->args->sign_verifity = args->sign_verifity;
  2204. cfg->args->ipc_type = args->ipc_type;
  2205. cfg->args->telnet_port = args->telnet_port;
  2206. cfg->args->root = args->root;
  2207. cfg->args->program = shm_strdup(args->program);
  2208. cfg->args->arguments = shm_strdup(args->arguments);
  2209. }
  2210. cfg->run_info->startup_time = y2k_time_now();
  2211. rc = sp_dir_get_path_new(dir, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH, detect_env_test_mode(cfg->args->test_mode));
  2212. if (rc != 0) {
  2213. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get root ini path failed!");
  2214. return rc;
  2215. }
  2216. rc = load_root_ini(dir, cfg->root_ini, tmp, 1);
  2217. if (rc != 0)
  2218. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load root.ini failed!");
  2219. cfg->root_ini_path = shm_strdup(tmp);
  2220. rc = sp_dir_get_path(dir, SP_DIR_SHELL_INI, NULL, tmp, MAX_PATH);
  2221. if (rc != 0) {
  2222. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shell.ini path failed!");
  2223. return rc;
  2224. }
  2225. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shell_var_path, MAX_PATH);
  2226. if (rc != 0) {
  2227. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  2228. return rc;
  2229. }
  2230. rc = sp_dir_get_path(dir, SP_DIR_ENV_INI, NULL, env_path, MAX_PATH);
  2231. if (rc != 0) {
  2232. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get env.ini path failed!");
  2233. return rc;
  2234. }
  2235. std::fstream shellVarFile;
  2236. shellVarFile.open(shell_var_path, std::ios::in);
  2237. if (!shellVarFile) {
  2238. /*cannot found shellVar.ini, then create one.*/
  2239. std::ofstream file(shell_var_path, std::fstream::out);
  2240. }
  2241. shellvarPath = shell_var_path;
  2242. /*override*/
  2243. cfg->args->sign_verifity = 0;
  2244. if (cfg->args->sign_verifity) {
  2245. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("verify code sign required.");
  2246. }
  2247. SetTokenCallBack(shell_getHttpToken);
  2248. SetRestfulTokenCallBack(shell_getHttpToken);
  2249. if (Error_Succeed != (rc = init_defaultShellCfg(dir, cfg)))
  2250. return rc;
  2251. cfg->shell_ini_path = shm_strdup(tmp);
  2252. cfg->shellvar_ini_path = shm_strdup(shell_var_path);
  2253. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
  2254. if (rc != 0) {
  2255. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get install.ini path failed!");;
  2256. return rc;
  2257. }
  2258. cfg->install_ini_path = shm_strdup(tmp);
  2259. if (ExistsFileA(tmp)) {
  2260. rc = load_install_ini(dir, cfg->install_ini, tmp, 1);
  2261. if (rc != 0) {
  2262. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load install.ini failed!");
  2263. return rc;
  2264. }
  2265. }
  2266. else
  2267. {
  2268. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get install info from memory");
  2269. rc = generate_install_memory(dir, cfg->install_ini);
  2270. if (rc != 0) {
  2271. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "generate install.ini failed!");
  2272. return rc;
  2273. }
  2274. }
  2275. spinlock_init(&cfg->lock);
  2276. *p_cfg = cfg;
  2277. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object ok! %p, lock = %d", cfg, cfg->lock);
  2278. rc = 0;
  2279. return rc;
  2280. }
  2281. void sp_cfg_destroy(sp_cfg_t *cfg)
  2282. {
  2283. unload_root_ini(cfg->root_ini, 1);
  2284. unload_shell_ini(cfg->shell_ini);
  2285. unload_install_ini(cfg->install_ini, 1);
  2286. shm_free(cfg->root_ini);
  2287. shm_free(cfg->root_ini_path);
  2288. shm_free(cfg->shell_ini);
  2289. shm_free(cfg->shell_ini_path);
  2290. shm_free(cfg->install_ini);
  2291. shm_free(cfg->args->start_entities);
  2292. shm_free(cfg->args->program);
  2293. shm_free(cfg->args->arguments);
  2294. shm_free(cfg->args);
  2295. shm_free(cfg);
  2296. }
  2297. void sp_cfg_lock(sp_cfg_t *cfg)
  2298. {
  2299. spinlock_enter(&cfg->lock, 0);
  2300. }
  2301. void sp_cfg_unlock(sp_cfg_t *cfg)
  2302. {
  2303. spinlock_leave(&cfg->lock);
  2304. }
  2305. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_name(sp_cfg_t *cfg, const char *name)
  2306. {
  2307. return find_entity(cfg->shell_ini, name);
  2308. }
  2309. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_idx(sp_cfg_t *cfg, int idx)
  2310. {
  2311. if (idx >= 0 && idx < cfg->shell_ini->arr_entity->nelts) {
  2312. return ARRAY_IDX(cfg->shell_ini->arr_entity, idx, sp_cfg_shell_entity_t*);
  2313. } else {
  2314. return NULL;
  2315. }
  2316. }
  2317. sp_cfg_shell_entity_t *sp_cfg_get_entity_shell(sp_cfg_t *cfg)
  2318. {
  2319. return ARRAY_IDX(cfg->shell_ini->arr_entity, 0, sp_cfg_shell_entity_t*);
  2320. }
  2321. sp_cfg_shell_module_t *sp_cfg_get_module_by_name(sp_cfg_t *cfg, const char *name)
  2322. {
  2323. return find_module(cfg->shell_ini, name);
  2324. }
  2325. sp_cfg_shell_module_t *sp_cfg_get_module_by_idx(sp_cfg_t *cfg, int idx)
  2326. {
  2327. if (idx >= 0 && idx < cfg->shell_ini->arr_module->nelts) {
  2328. return ARRAY_IDX(cfg->shell_ini->arr_module, idx, sp_cfg_shell_module_t*);
  2329. } else {
  2330. return NULL;
  2331. }
  2332. }
  2333. sp_cfg_shell_module_t *sp_cfg_get_module_shell(sp_cfg_t *cfg)
  2334. {
  2335. return ARRAY_IDX(cfg->shell_ini->arr_module, 0, sp_cfg_shell_module_t*);
  2336. }
  2337. sp_cfg_shell_sysevent_t*sp_cfg_get_sysevent(sp_cfg_t *cfg, const char *name)
  2338. {
  2339. array_header_t *arr_sysevent = cfg->shell_ini->arr_sysevent;
  2340. int i;
  2341. for (i = 0; i < arr_sysevent->nelts; ++i) {
  2342. sp_cfg_shell_sysevent_t *tmp = ARRAY_IDX(arr_sysevent, i, sp_cfg_shell_sysevent_t *);
  2343. if (_stricmp(tmp->name, name) == 0)
  2344. return tmp;
  2345. }
  2346. return NULL;
  2347. }
  2348. char *sp_cfg_get_path(sp_cfg_t *cfg, const char *path_prefix)
  2349. {
  2350. sp_cfg_path_t *path = find_path(cfg, path_prefix);
  2351. if (path) {
  2352. return path->path;
  2353. }
  2354. return NULL;
  2355. }
  2356. int sp_cfg_refresh_debug_level(sp_cfg_t *cfg, sp_cfg_shell_entity_t *cfg_ent)
  2357. {
  2358. //TODO: shell.ini cannot be written!!
  2359. char tmp[32];
  2360. _itoa(cfg_ent->debug_level, tmp, 10);
  2361. return WritePrivateProfileStringA("Debug", cfg_ent->name, tmp, cfg->shell_ini_path) ? 0 : Error_IO;
  2362. }
  2363. int sp_cfg_refresh_ent_version(sp_cfg_shell_entity_t* cfg_ent, const char* new_version)
  2364. {
  2365. if (new_version == NULL || strlen(new_version) == 0)
  2366. return Error_Param;
  2367. if (sp_version_is_valid(&cfg_ent->version)
  2368. ||
  2369. (!sp_version_is_valid(&cfg_ent->version)
  2370. && strstr(new_version, "0.0.0") == new_version)
  2371. ) {
  2372. return 0;
  2373. }
  2374. char* tok = NULL;
  2375. long major = 0;
  2376. long minor = 0;
  2377. long patch = 0;
  2378. int count = 0;
  2379. char kernel[32] = {0};
  2380. const char* seps = ".-";
  2381. strcpy(kernel, new_version);
  2382. tok = strtok(kernel, seps);
  2383. while (tok)
  2384. {
  2385. switch (count)
  2386. {
  2387. case 0:
  2388. major = strtol(tok, NULL, 0);
  2389. break;
  2390. case 1:
  2391. minor = strtol(tok, NULL, 0);
  2392. break;
  2393. case 2:
  2394. patch = strtol(tok, NULL, 0);
  2395. break;
  2396. }
  2397. tok = strtok(NULL, seps);
  2398. count++;
  2399. }
  2400. sp_version_fulfill(&cfg_ent->version, major, minor, patch, 0);
  2401. return 0;
  2402. fail:
  2403. return Error_Unexpect;
  2404. }
  2405. int sp_cfg_util_read_root_ini_version(sp_cfg_root_ini_t *ini, int major, int minor, int revision, int build)
  2406. {
  2407. char tmp[MAX_PATH];
  2408. sp_env_t *env = sp_get_env();
  2409. sp_dir_get_path_version(env->dir, major, minor, revision, build, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH, detect_env_test_mode(env->cfg->args->test_mode));
  2410. return load_root_ini(env->dir, ini, tmp, 0);
  2411. }
  2412. int sp_cfg_util_free_root_ini(sp_cfg_root_ini_t *ini)
  2413. {
  2414. unload_root_ini(ini, 0);
  2415. return 0;
  2416. }
  2417. int sp_cfg_get_devel_id(sp_cfg_t *cfg, int idx)
  2418. {
  2419. sp_env_t *env = sp_get_env();
  2420. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, idx);
  2421. return cfg_ent->devel_id;
  2422. }
  2423. sp_cfg_version_info_t* sp_cfg_find_previous_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2424. {
  2425. sp_cfg_version_info_t* ver_info = sp_cfg_find_version_info(cfg, version);
  2426. if (ver_info) {
  2427. sp_version_t *previous_version = &ver_info->previous_version;
  2428. return sp_cfg_find_version_info(cfg, previous_version);
  2429. }
  2430. return NULL;
  2431. }
  2432. sp_cfg_version_info_t* sp_cfg_find_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2433. {
  2434. int i;
  2435. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i) {
  2436. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2437. if (sp_version_equal(&ver_info->version, version))
  2438. return ver_info;
  2439. }
  2440. return NULL;
  2441. }
  2442. int sp_cfg_is_pack_installed(sp_cfg_t *cfg, const char*pack)
  2443. {
  2444. // 先查找轻量表历史
  2445. int i;
  2446. for(i =0; i<cfg->install_ini->arr_light_pack->nelts; i++)
  2447. {
  2448. sp_cfg_pack_info_t *pack_info = ARRAY_IDX(cfg->install_ini->arr_light_pack, i, sp_cfg_pack_info_t *);
  2449. if (stricmp(pack_info->name, pack) == 0)
  2450. return 0;
  2451. }
  2452. // 再查找版本安装包
  2453. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i)
  2454. {
  2455. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2456. if (stricmp(ver_info->install_pack, pack) ==0)
  2457. return 0;
  2458. }
  2459. return 1;
  2460. }