sp_cfg.cpp 98 KB

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