sp_cfg.cpp 99 KB

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