sp_cfg.cpp 97 KB

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