sp_cfg.cpp 99 KB

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