tool.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. #include "appl.h"
  2. #include "plt.h"
  3. #include <fcntl.h>
  4. #include <semaphore.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/shm.h>
  10. #include <syslog.h>
  11. #include <unistd.h>
  12. #define shm_lock() pthread_mutex_lock(&SHM->mutex)
  13. #define shm_unlock() pthread_mutex_unlock(&SHM->mutex)
  14. static const char *pstr_sem_name_ping = "sta_sem_ping";
  15. static const char *pstr_sem_name_pong = "sta_sem_pong";
  16. static int bmain_run = 1;
  17. static int bmenu_sta_run = 1;
  18. static int bmenu_ctn_run = 1;
  19. static int bmenu_chan_run = 1;
  20. static int bmenu_misc_run = 1;
  21. static int bmenu_meter_run = 1;
  22. static int bmenu_mqtt_run = 1;
  23. static int bmenu_cloud_run = 1;
  24. static int bmenu_ems_run = 1;
  25. typedef struct
  26. {
  27. int bHide;
  28. const char *szInfo;
  29. void (*func)(void);
  30. } menu_t;
  31. static struct shm_t *SHM = NULL;
  32. static struct shm_t SHMBUF;
  33. static sem_t *semPing;
  34. static sem_t *semPong;
  35. // 0 : confirm
  36. //-1 : not confirem
  37. static int getConfirm(void)
  38. {
  39. int ret = 0;
  40. char c;
  41. printf("Confirm?y/n:");
  42. getchar();
  43. c = getchar();
  44. return (c == 'y') ? (0) : (-1);
  45. }
  46. static int tool_send_shmcmd(int cmd, struct shmparam_t *param)
  47. {
  48. int ret = 0;
  49. int rc;
  50. struct shm_t *shm = SHM;
  51. shm_lock();
  52. shm->cmd = cmd;
  53. if (param != NULL)
  54. {
  55. shm->param = *param;
  56. }
  57. rc = sem_post(semPing);
  58. if (rc < 0)
  59. {
  60. ret = -1;
  61. goto leave;
  62. }
  63. sem_wait(semPong);
  64. if (rc == 0)
  65. {
  66. memcpy((void *)&SHMBUF, (void *)SHM, sizeof(struct shm_t));
  67. }
  68. else
  69. {
  70. ret = -2;
  71. }
  72. while (sem_trywait(semPong) == 0)
  73. ;
  74. leave:
  75. shm_unlock();
  76. return ret;
  77. }
  78. static int update_station(void)
  79. {
  80. int ret = 0;
  81. if (tool_send_shmcmd(CMD_STA_FETCH, NULL) < 0)
  82. {
  83. ret = -1;
  84. goto leave;
  85. }
  86. else
  87. {
  88. MDL = *(struct mdl_t *)SHMBUF.buf;
  89. }
  90. leave:
  91. if (ret < 0)
  92. {
  93. printf("%s, ret:%d\n", __func__, ret);
  94. }
  95. return ret;
  96. }
  97. static void print_chan()
  98. {
  99. int i;
  100. struct mdl_t *sta = &MDL;
  101. struct chan_t *chan;
  102. int *channbr = &MDL.channbr;
  103. printf(REVERSE "CHAN" NONE " nbr:%d\n", *channbr);
  104. for (i = 1; i <= *channbr; i++)
  105. {
  106. chan = &(sta->chan[i]);
  107. printf(" [%d]info:%s mode:%s dev:%s port:%d servip:%s servport:%d baud:%d dbg:%d en:%d rstcnt:%.0f\n",
  108. i, chan->szinfo, chan->szmode, chan->szdev, chan->port, chan->servip, chan->servport,
  109. chan->baud, chan->dbg, chan->en, chan->rstcnt);
  110. }
  111. }
  112. static void print_mqtt()
  113. {
  114. struct mqtt_t *dev = &MDL.mqtt;
  115. printf(REVERSE "MQTT" NONE " \n");
  116. printf(" stat:" YELLOW "%s" NONE " err:%s stp:%d servip:%s servport:%d clientid:%s username:%s passwd:%s\n",
  117. dev->sm.szstate, dev->sm.szerrcode, dev->sm.step, dev->szservip, dev->servport, dev->szclientid, dev->szusername, dev->szpasswd);
  118. printf("txbufusage:%.1f rxbufusage:%.1f sm tick:%03d ave:%.0f max:%.0fms pub_tot:%.0f ave:%.0f max:%.0f\n",
  119. dev->txbuf_usage, dev->rxbuf_usage, dev->sm.tick, dev->sm.timing_ave, dev->sm.timing_max,
  120. dev->pub_totalcnt, dev->pub_ave, dev->pub_max);
  121. }
  122. static void print_tbmqtt()
  123. {
  124. struct tbmqtt_t *dev = &MDL.tbmqtt;
  125. printf(REVERSE "TB MQTT" NONE " \n");
  126. printf(" stat:" YELLOW "%s" NONE " err:%s stp:%d servip:%s servport:%d clientid:%s accesstoken:%s \n",
  127. dev->sm.szstate, dev->sm.szerrcode, dev->sm.step, dev->szservip, dev->servport, dev->szclientid, dev->szaccesstoken);
  128. printf("txbufusage:%.1f rxbufusage:%.1f sm tick:%03d ave:%.0f max:%.0fms pub_total:%.0f ave:%.0f max:%.0f\n",
  129. dev->txbuf_usage, dev->rxbuf_usage, dev->sm.tick, dev->sm.timing_ave, dev->sm.timing_max,
  130. dev->pubTotalCnt, dev->pubAvg, dev->pubMax);
  131. }
  132. static void print_chan_bytab()
  133. {
  134. int i;
  135. struct mdl_t *sta = &MDL;
  136. struct chan_t *chan;
  137. int *channbr = &MDL.channbr;
  138. //
  139. // CHAN moved to print_chan
  140. //
  141. // head
  142. printf(REVERSE "CHAN" NONE " %s\n", sta->sztime);
  143. printf("%-4s%-8s%-14s%-14s%-9s\
  144. %-8s%-10s%-4s\n",
  145. "idx", "info", "mode", "servip", "servport",
  146. "baud", "port", "Dbg");
  147. // main status
  148. for (i = 1; i <= *channbr; i++)
  149. {
  150. chan = &(sta->chan[i]);
  151. printf("%-4d%-8s%-14s%-14s%-9d\
  152. %-8d%-10s%-4d\n",
  153. i, chan->szinfo, chan->szmode, chan->servip, chan->servport,
  154. chan->baud, chan->szdev, chan->dbg);
  155. }
  156. }
  157. static void print_sta(void)
  158. {
  159. struct mdl_t *dev = &MDL;
  160. printf(REVERSE "STA" NONE " prj_id:%s sw_ver:%d.%d.%d time:%s time_zone:%d\n",
  161. MDL.szprojId, MDL.version[0], MDL.version[1], MDL.version[2], dev->sztime, dev->time_zone);
  162. printf(" stat:" YELLOW "%s" NONE " err:%s stp:%d info:%s ap:" L_GREEN "%d" NONE " aps:%d soc:" L_GREEN "%.1f" NONE " d|c:%d|%d norm_cap:%d pow:%d\n",
  163. dev->sm.szstate, dev->sm.szerrcode, dev->sm.step,
  164. dev->szinfo, dev->pow.active_p, dev->pow.active_p_set, dev->pow.soc,
  165. dev->pow.bdhgable, dev->pow.bchgable,
  166. dev->pow.norm_cap, dev->pow.norm_pow);
  167. printf(" Cloud upload_en:%d sys_intv[0]:%d [1]:%d [2]:%d\n",
  168. MDL.cloud.upload_enable, MDL.cloud.sys_intv[0], MDL.cloud.sys_intv[1], MDL.cloud.sys_intv[2]);
  169. printf(" MAC cpu_occupy:%.1f mem:%.1f disk:%.1f\n",
  170. MDL.mac.cpu_occupy, MDL.mac.mem_occupy, MDL.mac.disk_occupy);
  171. print_chan();
  172. print_mqtt();
  173. print_tbmqtt();
  174. }
  175. static void mfunc_exit(void)
  176. {
  177. bmain_run = 0;
  178. }
  179. static void mfunc_print_sta(void)
  180. {
  181. while (1)
  182. {
  183. update_station();
  184. print_sta();
  185. sleep(1);
  186. system("clear");
  187. }
  188. }
  189. static void mfunc_chan_return(void)
  190. {
  191. bmenu_chan_run = 0;
  192. }
  193. static void mfunc_chan_print(void)
  194. {
  195. while (1)
  196. {
  197. update_station();
  198. print_chan_bytab();
  199. sleep(1);
  200. system("clear");
  201. }
  202. }
  203. static void mfunc_chan_set_dbg(void)
  204. {
  205. int idx = 1;
  206. struct shmparam_t param;
  207. int i;
  208. int start;
  209. int nb;
  210. int enable;
  211. printf("start_idx nb dbg\n");
  212. printf("dbg: 0-disable 1-enable\n");
  213. printf(">");
  214. scanf("%d %d %d", &start, &nb, &enable);
  215. for (i = 0; i < nb; i++)
  216. {
  217. param.idx = start + i;
  218. param.val = enable;
  219. if (tool_send_shmcmd(CMD_CHAN_SET_DBG, &param) < 0)
  220. {
  221. printf("tool send cmd fail\n");
  222. }
  223. else
  224. {
  225. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  226. }
  227. }
  228. }
  229. static void mfunc_chan_set_en(void)
  230. {
  231. int idx = 1;
  232. struct shmparam_t param;
  233. int i;
  234. int start;
  235. int nb;
  236. int enable;
  237. printf("start_idx nb en\n");
  238. printf("en: 0-disable 1-enable\n");
  239. printf(">");
  240. scanf("%d %d %d", &start, &nb, &enable);
  241. for (i = 0; i < nb; i++)
  242. {
  243. param.idx = start + i;
  244. param.val = enable;
  245. if (tool_send_shmcmd(CMD_CHAN_SET_EN, &param) < 0)
  246. {
  247. printf("tool send cmd fail\n");
  248. }
  249. else
  250. {
  251. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  252. }
  253. }
  254. }
  255. static void mfunc_chan_reset(void)
  256. {
  257. int idx = 1;
  258. struct shmparam_t param;
  259. int i;
  260. int start;
  261. int nb;
  262. int enable;
  263. printf("start_idx nb\n");
  264. printf(">");
  265. scanf("%d %d", &start, &nb);
  266. for (i = 0; i < nb; i++)
  267. {
  268. param.idx = start + i;
  269. if (tool_send_shmcmd(CMD_CHAN_RESET, &param) < 0)
  270. {
  271. printf("tool send cmd fail\n");
  272. }
  273. else
  274. {
  275. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  276. }
  277. }
  278. }
  279. static menu_t menu_chan[] = {
  280. {0, "return", mfunc_chan_return},
  281. {0, "print", mfunc_chan_print},
  282. {0, "set dbg", mfunc_chan_set_dbg},
  283. {0, "set en", mfunc_chan_set_en},
  284. {0, "reset", mfunc_chan_reset},
  285. };
  286. static void mfunc_chan(void)
  287. {
  288. int i;
  289. int iinput;
  290. bmenu_chan_run = 1;
  291. while (bmenu_chan_run)
  292. {
  293. printf("\n\n-----------------\n CHAN MENU:\n");
  294. for (i = 0; i < sizeof(menu_chan) / sizeof(menu_t); i++)
  295. {
  296. if (menu_chan[i].bHide != 1)
  297. {
  298. printf(" %d->%s\n", i, menu_chan[i].szInfo);
  299. }
  300. }
  301. printf(">");
  302. scanf("%d", &iinput);
  303. if (iinput < sizeof(menu_chan) / sizeof(menu_t))
  304. {
  305. if (menu_chan[iinput].bHide != 1)
  306. {
  307. menu_chan[iinput].func();
  308. }
  309. else
  310. {
  311. printf("Illegal selection\n");
  312. }
  313. }
  314. else
  315. {
  316. printf("Illegal selection\n");
  317. }
  318. }
  319. }
  320. static void mfunc_meter_return(void)
  321. {
  322. bmenu_meter_run = 0;
  323. }
  324. static void mfunc_meter_print(void)
  325. {
  326. while (1)
  327. {
  328. update_station();
  329. // print_chan_bytab();
  330. sleep(1);
  331. system("clear");
  332. }
  333. }
  334. static void mfunc_meter_set_com_active_e(void)
  335. {
  336. int idx = 1;
  337. int cmd;
  338. struct shmparam_t param;
  339. int i;
  340. // int bidx;
  341. int start;
  342. int nb;
  343. double dval;
  344. printf("start_idx nb com_active_e\n");
  345. printf("com_active_e:double\n");
  346. printf(">");
  347. scanf("%d %d %lf", &start, &nb, &dval);
  348. for (i = 0; i < nb; i++)
  349. {
  350. param.idx = start + i;
  351. param.dval = dval;
  352. if (tool_send_shmcmd(CMD_METER_SET_COMACTIVE, &param) < 0)
  353. {
  354. printf("tool send cmd fail\n");
  355. }
  356. else
  357. {
  358. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  359. }
  360. }
  361. }
  362. static void mfunc_meter_set_com_active_p(void)
  363. {
  364. int idx = 1;
  365. int cmd;
  366. struct shmparam_t param;
  367. int i;
  368. // int bidx;
  369. int start;
  370. int nb;
  371. double dval;
  372. printf("start_idx nb com_active_p\n");
  373. printf("com_active_p:double\n");
  374. printf(">");
  375. scanf("%d %d %lf", &start, &nb, &dval);
  376. for (i = 0; i < nb; i++)
  377. {
  378. param.idx = start + i;
  379. param.dval = dval;
  380. if (tool_send_shmcmd(CMD_METER_SET_COMACTIVP, &param) < 0)
  381. {
  382. printf("tool send cmd fail\n");
  383. }
  384. else
  385. {
  386. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  387. }
  388. }
  389. }
  390. static void mfunc_meter_set_pt_mdl(void)
  391. {
  392. int idx = 1;
  393. int cmd;
  394. struct shmparam_t param;
  395. int i;
  396. // int bidx;
  397. int start;
  398. int nb;
  399. int val;
  400. printf("start_idx nb PT\n");
  401. printf("PT:int\n");
  402. printf(">");
  403. scanf("%d %d %d", &start, &nb, &val);
  404. for (i = 0; i < nb; i++)
  405. {
  406. param.idx = start + i;
  407. param.val = val;
  408. if (tool_send_shmcmd(CMD_METER_SET_PT_MDL, &param) < 0)
  409. {
  410. printf("tool send cmd fail\n");
  411. }
  412. else
  413. {
  414. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  415. }
  416. }
  417. }
  418. static void mfunc_meter_set_ct_mdl(void)
  419. {
  420. int idx = 1;
  421. int cmd;
  422. struct shmparam_t param;
  423. int i;
  424. // int bidx;
  425. int start;
  426. int nb;
  427. int val;
  428. printf("start_idx nb CT\n");
  429. printf("CT:int\n");
  430. printf(">");
  431. scanf("%d %d %d", &start, &nb, &val);
  432. for (i = 0; i < nb; i++)
  433. {
  434. param.idx = start + i;
  435. param.val = val;
  436. if (tool_send_shmcmd(CMD_METER_SET_CT_MDL, &param) < 0)
  437. {
  438. printf("tool send cmd fail\n");
  439. }
  440. else
  441. {
  442. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  443. }
  444. }
  445. }
  446. static void mfunc_meter_sendcmd(void)
  447. {
  448. int cmd;
  449. struct shmparam_t param;
  450. int start;
  451. int nb;
  452. int i;
  453. printf("meter cmd list:\n");
  454. printf("%d : launch\n", CMD_SM_LAUNCH);
  455. printf("%d : ready\n", CMD_SM_READY);
  456. printf("%d : ack\n", CMD_SM_ACK);
  457. printf("%d : nack\n", CMD_SM_NACK);
  458. printf("start_idx nb cmd\n");
  459. printf(">");
  460. scanf("%d %d %d", &start, &nb, &cmd);
  461. for (i = 0; i < nb; i++)
  462. {
  463. param.idx = start + i;
  464. param.val = cmd;
  465. if (tool_send_shmcmd(CMD_METER_SENDCMD, &param) < 0)
  466. {
  467. printf("tool send cmd fail\n");
  468. }
  469. else
  470. {
  471. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  472. }
  473. }
  474. }
  475. static void mfunc_meter_set_pt(void)
  476. {
  477. int idx = 1;
  478. int cmd;
  479. struct shmparam_t param;
  480. int i;
  481. // int bidx;
  482. int start;
  483. int nb;
  484. int val;
  485. printf("start_idx nb PT\n");
  486. printf("PT:int\n");
  487. printf(">");
  488. scanf("%d %d %d", &start, &nb, &val);
  489. for (i = 0; i < nb; i++)
  490. {
  491. param.idx = start + i;
  492. param.val = val;
  493. if (tool_send_shmcmd(CMD_METER_SET_PT, &param) < 0)
  494. {
  495. printf("tool send cmd fail\n");
  496. }
  497. else
  498. {
  499. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  500. }
  501. }
  502. }
  503. static void mfunc_meter_set_ct(void)
  504. {
  505. int idx = 1;
  506. int cmd;
  507. struct shmparam_t param;
  508. int i;
  509. // int bidx;
  510. int start;
  511. int nb;
  512. int val;
  513. printf("start_idx nb CT\n");
  514. printf("CT:int\n");
  515. printf(">");
  516. scanf("%d %d %d", &start, &nb, &val);
  517. for (i = 0; i < nb; i++)
  518. {
  519. param.idx = start + i;
  520. param.val = val;
  521. if (tool_send_shmcmd(CMD_METER_SET_CT, &param) < 0)
  522. {
  523. printf("tool send cmd fail\n");
  524. }
  525. else
  526. {
  527. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  528. }
  529. }
  530. }
  531. static menu_t menu_meter[] = {
  532. {0, "return", mfunc_meter_return},
  533. {0, "send cmd", mfunc_meter_sendcmd},
  534. {0, "set PT", mfunc_meter_set_pt},
  535. {0, "set CT", mfunc_meter_set_ct},
  536. };
  537. static void mfunc_meter(void)
  538. {
  539. int i;
  540. int iinput;
  541. bmenu_meter_run = 1;
  542. while (bmenu_meter_run)
  543. {
  544. printf("\n\n-----------------\n METER MENU:\n");
  545. for (i = 0; i < sizeof(menu_meter) / sizeof(menu_t); i++)
  546. {
  547. if (menu_meter[i].bHide != 1)
  548. {
  549. printf(" %d->%s\n", i, menu_meter[i].szInfo);
  550. }
  551. }
  552. printf(">");
  553. scanf("%d", &iinput);
  554. if (iinput < sizeof(menu_meter) / sizeof(menu_t))
  555. {
  556. if (menu_meter[iinput].bHide != 1)
  557. {
  558. menu_meter[iinput].func();
  559. }
  560. else
  561. {
  562. printf("Illegal selection\n");
  563. }
  564. }
  565. else
  566. {
  567. printf("Illegal selection\n");
  568. }
  569. }
  570. }
  571. static void mfunc_mqtt_return(void)
  572. {
  573. bmenu_mqtt_run = 0;
  574. }
  575. static void mfunc_mqtt_sendcmd(void)
  576. {
  577. int cmd;
  578. struct shmparam_t param;
  579. printf("mqtt cmd list:\n");
  580. printf("%d : offline\n", CMD_SM_OFFLINE);
  581. printf("%d : stdby\n", CMD_SM_READY);
  582. printf("%d : ack\n", CMD_SM_ACK);
  583. printf("%d : nack\n", CMD_SM_NACK);
  584. printf("cmd\n");
  585. printf(">");
  586. scanf("%d", &cmd);
  587. param.val = cmd;
  588. if (tool_send_shmcmd(CMD_MQTT_SENDCMD, &param) < 0)
  589. {
  590. printf("tool send cmd fail\n");
  591. }
  592. else
  593. {
  594. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  595. }
  596. }
  597. static menu_t menu_mqtt[] = {
  598. {0, "return", mfunc_mqtt_return},
  599. {0, "send cmd", mfunc_mqtt_sendcmd},
  600. };
  601. static void mfunc_mqtt(void)
  602. {
  603. int i;
  604. int iinput;
  605. bmenu_mqtt_run = 1;
  606. while (bmenu_mqtt_run)
  607. {
  608. printf("\n\n-----------------\n MQTT MENU:\n");
  609. for (i = 0; i < sizeof(menu_mqtt) / sizeof(menu_t); i++)
  610. {
  611. if (menu_mqtt[i].bHide != 1)
  612. {
  613. printf(" %d->%s\n", i, menu_mqtt[i].szInfo);
  614. }
  615. }
  616. printf(">");
  617. scanf("%d", &iinput);
  618. if (iinput < sizeof(menu_mqtt) / sizeof(menu_t))
  619. {
  620. if (menu_mqtt[iinput].bHide != 1)
  621. {
  622. menu_mqtt[iinput].func();
  623. }
  624. else
  625. {
  626. printf("Illegal selection\n");
  627. }
  628. }
  629. else
  630. {
  631. printf("Illegal selection\n");
  632. }
  633. }
  634. }
  635. static void mfunc_misc_return(void)
  636. {
  637. bmenu_misc_run = 0;
  638. }
  639. static void mfunc_misc_lockshm(void)
  640. {
  641. printf("try to lock...");
  642. shm_lock();
  643. printf("done\n");
  644. }
  645. static void mfunc_misc_unlockshm(void)
  646. {
  647. printf("try to unlock...");
  648. shm_unlock();
  649. printf("done\n");
  650. }
  651. static void mfunc_misc_post_semping(void)
  652. {
  653. int rc = 0;
  654. printf("try to post semPing...");
  655. rc = sem_post(semPing);
  656. printf("rc:%d\n", rc);
  657. }
  658. static void mfunc_misc_wait_sempong(void)
  659. {
  660. int rc = 0;
  661. printf("try to wait semPong...");
  662. rc = sem_wait(semPong);
  663. printf("rc:%d\n", rc);
  664. }
  665. static void mfunc_misc_printmac(void)
  666. {
  667. int i;
  668. struct timeval tv;
  669. struct timezone tz;
  670. time_t timep;
  671. struct tm *tsp;
  672. double starttime;
  673. double endtime;
  674. double exetime;
  675. float fval;
  676. struct cpu_info_t cpu_info;
  677. printf("char:%d,short:%d,int:%d,long:%d,long long:%d,float:%d,double:%d,station_t:%d\n",
  678. sizeof(char), sizeof(short), sizeof(int), sizeof(long),
  679. sizeof(long long), sizeof(float), sizeof(double), sizeof(struct mdl_t));
  680. /* cal gettimeofday */
  681. gettimeofday(&tv, &tz);
  682. starttime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  683. for (i = 0; i < 100; i++)
  684. {
  685. misc_gettimeofday();
  686. }
  687. gettimeofday(&tv, &tz);
  688. endtime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  689. exetime = endtime - starttime;
  690. printf("gettimeofday exe time:%.3fms\n", exetime / 100.0);
  691. /* cal time and localtime */
  692. gettimeofday(&tv, &tz);
  693. starttime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  694. for (i = 0; i < 100; i++)
  695. {
  696. time(&timep);
  697. // tsp = gmtime(&timep);
  698. tsp = localtime(&timep);
  699. }
  700. gettimeofday(&tv, &tz);
  701. endtime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  702. exetime = endtime - starttime;
  703. printf("time&localtime exe time:%.3fms\n", exetime / 100.0);
  704. /* get cpu occupy */
  705. printf("getting cpu occupy, wait 5 seconds\n");
  706. gettimeofday(&tv, &tz);
  707. starttime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  708. for (i = 0; i < 100; i++)
  709. {
  710. mac_get_cpu_info(&cpu_info);
  711. }
  712. gettimeofday(&tv, &tz);
  713. endtime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  714. exetime = endtime - starttime;
  715. printf("mac_get_cpu_info exe time:%.3fms val:%.1f\n", exetime / 100.0, mac_get_cpu_occupy());
  716. /* get mem occupy */
  717. gettimeofday(&tv, &tz);
  718. starttime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  719. for (i = 0; i < 100; i++)
  720. {
  721. fval = mac_get_mem_occupy();
  722. }
  723. gettimeofday(&tv, &tz);
  724. endtime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  725. exetime = endtime - starttime;
  726. printf("mac_get_mem_occupy exe time:%.3fms val:%.1f\n", exetime / 100.0, mac_get_mem_occupy());
  727. /* get disk occupy */
  728. gettimeofday(&tv, &tz);
  729. starttime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  730. for (i = 0; i < 100; i++)
  731. {
  732. fval = mac_get_disk_occupy();
  733. }
  734. gettimeofday(&tv, &tz);
  735. endtime = (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  736. exetime = endtime - starttime;
  737. printf("mac_get_disk_occupy exe time:%.3fms val:%.1f\n", exetime / 100.0, mac_get_disk_occupy());
  738. }
  739. static void mfunc_misc_cjson_ex()
  740. {
  741. cJSON *cjson_test = NULL;
  742. cJSON *cjson_address = NULL;
  743. cJSON *cjson_skill = NULL;
  744. char *str = NULL;
  745. /* 创建一个JSON数据对象(链表头结点) */
  746. cjson_test = cJSON_CreateObject();
  747. /* 添加一条字符串类型的JSON数据(添加一个链表节点) */
  748. cJSON_AddStringToObject(cjson_test, "name", "mculover666");
  749. /* 添加一条整数类型的JSON数据(添加一个链表节点) */
  750. cJSON_AddNumberToObject(cjson_test, "age", 22);
  751. /* 添加一条浮点类型的JSON数据(添加一个链表节点) */
  752. cJSON_AddNumberToObject(cjson_test, "weight", 55.5);
  753. /* 添加一个嵌套的JSON数据(添加一个链表节点) */
  754. cjson_address = cJSON_CreateObject();
  755. cJSON_AddStringToObject(cjson_address, "country", "China");
  756. cJSON_AddNumberToObject(cjson_address, "zip-code", 111111);
  757. cJSON_AddItemToObject(cjson_test, "address", cjson_address);
  758. /* 添加一个数组类型的JSON数据(添加一个链表节点) */
  759. cjson_skill = cJSON_CreateArray();
  760. cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("C"));
  761. cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("Java"));
  762. cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("Python"));
  763. cJSON_AddItemToObject(cjson_test, "skill", cjson_skill);
  764. /* 添加一个值为 False 的布尔类型的JSON数据(添加一个链表节点) */
  765. cJSON_AddFalseToObject(cjson_test, "student");
  766. /* 打印JSON对象(整条链表)的所有数据 */
  767. str = cJSON_Print(cjson_test);
  768. printf("%s\n", str);
  769. cJSON_Delete(cjson_test);
  770. }
  771. static void mfunc_misc_mem_leak_ex()
  772. {
  773. cJSON *cjson_test = NULL;
  774. cJSON *cjson_address = NULL;
  775. cJSON *cjson_skill = NULL;
  776. char *str = NULL;
  777. while (1)
  778. {
  779. /* 创建一个JSON数据对象(链表头结点) */
  780. cjson_test = cJSON_CreateObject();
  781. /* 添加一条字符串类型的JSON数据(添加一个链表节点) */
  782. cJSON_AddStringToObject(cjson_test, "name", "mculover666");
  783. /* 添加一条整数类型的JSON数据(添加一个链表节点) */
  784. cJSON_AddNumberToObject(cjson_test, "age", 22);
  785. /* 添加一条浮点类型的JSON数据(添加一个链表节点) */
  786. cJSON_AddNumberToObject(cjson_test, "weight", 55.5);
  787. /* 添加一个嵌套的JSON数据(添加一个链表节点) */
  788. cjson_address = cJSON_CreateObject();
  789. cJSON_AddStringToObject(cjson_address, "country", "China");
  790. cJSON_AddNumberToObject(cjson_address, "zip-code", 111111);
  791. cJSON_AddItemToObject(cjson_test, "address", cjson_address);
  792. /* 添加一个数组类型的JSON数据(添加一个链表节点) */
  793. cjson_skill = cJSON_CreateArray();
  794. cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("C"));
  795. cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("Java"));
  796. cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("Python"));
  797. cJSON_AddItemToObject(cjson_test, "skill", cjson_skill);
  798. /* 添加一个值为 False 的布尔类型的JSON数据(添加一个链表节点) */
  799. cJSON_AddFalseToObject(cjson_test, "student");
  800. /* 打印JSON对象(整条链表)的所有数据 */
  801. str = cJSON_Print(cjson_test);
  802. printf("%s\n", str);
  803. usleep(5000);
  804. }
  805. cJSON_Delete(cjson_test);
  806. }
  807. static void mfunc_misc_ringbuffer_ex()
  808. {
  809. // test_ring_buffer_element_t e;
  810. // int i, rc;
  811. // test_ring_buffer_init(ptestrb);
  812. // printf("test ring buffer inited, queue used:%d, q_size:%d\n",
  813. // test_ring_buffer_num_items(ptestrb),
  814. // test_ring_buffer_size(ptestrb));
  815. // printf("peek from 0 to 2\n");
  816. // for (i = 0; i < 3; i++)
  817. // {
  818. // rc = test_ring_buffer_peek(ptestrb, &e, i);
  819. // printf(" index:%d rc:%d", i, rc);
  820. // if (rc == 1)
  821. // {
  822. // printf(" val:%d\n", e.val);
  823. // }
  824. // else
  825. // {
  826. // printf("\n");
  827. // }
  828. // }
  829. // printf("queue 3 values\n");
  830. // for (i = 0; i < 3; i++)
  831. // {
  832. // e.val = 11 + i;
  833. // test_ring_buffer_queue(ptestrb, e);
  834. // printf(" val:%d\n", e.val);
  835. // }
  836. // printf("\n");
  837. // printf("peek from 0 to 2\n");
  838. // for (i = 0; i < 3; i++)
  839. // {
  840. // rc = test_ring_buffer_peek(ptestrb, &e, i);
  841. // printf(" index:%d rc:%d", i, rc);
  842. // if (rc == 1)
  843. // {
  844. // printf(" val:%d\n", e.val);
  845. // }
  846. // else
  847. // {
  848. // printf("\n");
  849. // }
  850. // }
  851. // test_ring_buffer_dequeue(ptestrb, &e);
  852. // printf("dequeue 1 value : %d\n", e.val);
  853. // printf("peek from 0 to 2\n");
  854. // for (i = 0; i < 3; i++)
  855. // {
  856. // rc = test_ring_buffer_peek(ptestrb, &e, i);
  857. // printf(" index:%d rc:%d", i, rc);
  858. // if (rc == 1)
  859. // {
  860. // printf(" val:%d\n", e.val);
  861. // }
  862. // else
  863. // {
  864. // printf("\n");
  865. // }
  866. // }
  867. // e.val = 14;
  868. // test_ring_buffer_queue(ptestrb, e);
  869. // printf("queue 1 val:%d\n", e.val);
  870. // printf("peek from 0 to 2\n");
  871. // for (i = 0; i < 3; i++)
  872. // {
  873. // rc = test_ring_buffer_peek(ptestrb, &e, i);
  874. // printf(" index:%d rc:%d", i, rc);
  875. // if (rc == 1)
  876. // {
  877. // printf(" val:%d\n", e.val);
  878. // }
  879. // else
  880. // {
  881. // printf("\n");
  882. // }
  883. // }
  884. }
  885. static void mfunc_misc_snap_del_test()
  886. {
  887. }
  888. static menu_t menu_misc[] = {
  889. {0, "return", mfunc_misc_return},
  890. {0, "print mac", mfunc_misc_printmac},
  891. {0, "lock shm", mfunc_misc_lockshm},
  892. {0, "unlock shm", mfunc_misc_unlockshm},
  893. {0, "post semPing", mfunc_misc_post_semping},
  894. {0, "wait semPong", mfunc_misc_wait_sempong},
  895. {0, "cJSON example", mfunc_misc_cjson_ex},
  896. {0, "ringbuffer example", mfunc_misc_ringbuffer_ex},
  897. {0, "mem leak example", mfunc_misc_mem_leak_ex},
  898. {0, "snap del test", mfunc_misc_snap_del_test},
  899. };
  900. static void mfunc_misc(void)
  901. {
  902. int iinput;
  903. int i;
  904. bmenu_misc_run = 1;
  905. while (bmenu_misc_run)
  906. {
  907. printf("\n\n-----------------\n misc menu:\n");
  908. for (i = 0; i < sizeof(menu_misc) / sizeof(menu_t); i++)
  909. {
  910. if (menu_misc[i].bHide != 1)
  911. {
  912. printf(" %d->%s\n", i, menu_misc[i].szInfo);
  913. }
  914. }
  915. printf(">");
  916. scanf("%d", &iinput);
  917. if (iinput < sizeof(menu_misc) / sizeof(menu_t))
  918. {
  919. if (menu_misc[iinput].bHide != 1)
  920. {
  921. menu_misc[iinput].func();
  922. }
  923. else
  924. {
  925. printf("Illegal selection\n");
  926. }
  927. }
  928. else
  929. {
  930. printf("Illegal selection\n");
  931. }
  932. }
  933. }
  934. static void mfunc_ctn_return(void)
  935. {
  936. bmenu_ctn_run = 0;
  937. }
  938. static void mfunc_ctn_sendcmd(void)
  939. {
  940. int cmd;
  941. struct shmparam_t param;
  942. int start;
  943. int nb;
  944. int i;
  945. printf("cmd list:\n");
  946. printf("%d : offline\n", CMD_SM_OFFLINE);
  947. printf("%d : stdby\n", CMD_SM_STDBY);
  948. printf("%d : stop\n", CMD_SM_STOP);
  949. printf("%d : ready\n", CMD_SM_READY);
  950. // printf("%d : chg\n", CMD_SM_CHG);
  951. printf("%d : ack\n", CMD_SM_ACK);
  952. printf("%d : nack\n", CMD_SM_NACK);
  953. printf("start_idx nb cmd\n");
  954. printf(">");
  955. scanf("%d %d %d", &start, &nb, &cmd);
  956. for (i = 0; i < nb; i++)
  957. {
  958. param.idx = start + i;
  959. param.val = cmd;
  960. if (tool_send_shmcmd(CMD_CTN_SENDCMD, &param) < 0)
  961. {
  962. printf("tool send cmd fail\n");
  963. }
  964. else
  965. {
  966. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  967. }
  968. }
  969. }
  970. static void mfunc_ctn_set_aps(void)
  971. {
  972. double dval;
  973. struct shmparam_t param;
  974. int start;
  975. int nb;
  976. int i;
  977. printf("start_idx nb aps\n");
  978. printf("type:double unit:kW pos:dhg neg:chg 0:idle\n");
  979. printf(">");
  980. scanf("%d %d %lf", &start, &nb, &dval);
  981. for (i = 0; i < nb; i++)
  982. {
  983. param.idx = start + i;
  984. param.dval = dval;
  985. if (tool_send_shmcmd(CMD_CTN_SET_ACTIVEPSET, &param) < 0)
  986. {
  987. printf("tool send cmd fail\n");
  988. }
  989. else
  990. {
  991. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  992. }
  993. }
  994. }
  995. static menu_t menu_ctn[] = {
  996. {0, "return", mfunc_ctn_return},
  997. {0, "send cmd", mfunc_ctn_sendcmd}, //
  998. {0, "set aps", mfunc_ctn_set_aps}, //
  999. };
  1000. static void mfunc_ctn(void)
  1001. {
  1002. int i;
  1003. int iinput;
  1004. bmenu_ctn_run = 1;
  1005. while (bmenu_ctn_run)
  1006. {
  1007. printf("\n\n-----------------\nCTN MENU:\n");
  1008. for (i = 0; i < sizeof(menu_ctn) / sizeof(menu_t); i++)
  1009. {
  1010. if (menu_ctn[i].bHide != 1)
  1011. {
  1012. printf(" %d->%s\n", i, menu_ctn[i].szInfo);
  1013. }
  1014. }
  1015. printf(">");
  1016. scanf("%d", &iinput);
  1017. if (iinput < sizeof(menu_ctn) / sizeof(menu_t))
  1018. {
  1019. if (menu_ctn[iinput].bHide != 1)
  1020. {
  1021. menu_ctn[iinput].func();
  1022. }
  1023. else
  1024. {
  1025. printf("Illegal selection\n");
  1026. }
  1027. }
  1028. else
  1029. {
  1030. printf("Illegal selection\n");
  1031. }
  1032. }
  1033. }
  1034. static void mfunc_sta_return(void)
  1035. {
  1036. bmenu_sta_run = 0;
  1037. }
  1038. static void mfunc_sta_sendcmd(void)
  1039. {
  1040. int cmd;
  1041. struct shmparam_t param;
  1042. int start;
  1043. int nb;
  1044. int i;
  1045. printf("cmd list:\n");
  1046. printf("%d : offline\n", CMD_SM_OFFLINE);
  1047. printf("%d : stdby\n", CMD_SM_STDBY);
  1048. printf("%d : stop\n", CMD_SM_STOP);
  1049. printf("%d : ready\n", CMD_SM_READY);
  1050. printf("%d : ack\n", CMD_SM_ACK);
  1051. printf("%d : nack\n", CMD_SM_NACK);
  1052. printf("cmd\n");
  1053. printf(">");
  1054. scanf("%d", &cmd);
  1055. param.val = cmd;
  1056. if (tool_send_shmcmd(CMD_STA_SENDCMD, &param) < 0)
  1057. {
  1058. printf("tool send cmd fail\n");
  1059. }
  1060. else
  1061. {
  1062. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1063. }
  1064. }
  1065. static void mfunc_sta_send_stdby_cmd(void)
  1066. {
  1067. int cmd = CMD_SM_STDBY;
  1068. struct shmparam_t param;
  1069. param.val = cmd;
  1070. if (tool_send_shmcmd(CMD_STA_SENDCMD, &param) < 0)
  1071. {
  1072. printf("tool send cmd fail\n");
  1073. }
  1074. else
  1075. {
  1076. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1077. }
  1078. }
  1079. static void mfunc_sta_send_stop_cmd(void)
  1080. {
  1081. int cmd = CMD_SM_STOP;
  1082. struct shmparam_t param;
  1083. param.val = cmd;
  1084. if (tool_send_shmcmd(CMD_STA_SENDCMD, &param) < 0)
  1085. {
  1086. printf("tool send cmd fail\n");
  1087. }
  1088. else
  1089. {
  1090. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1091. }
  1092. }
  1093. static void mfunc_sta_send_ready_cmd(void)
  1094. {
  1095. int cmd = CMD_SM_READY;
  1096. struct shmparam_t param;
  1097. param.val = cmd;
  1098. if (tool_send_shmcmd(CMD_STA_SENDCMD, &param) < 0)
  1099. {
  1100. printf("tool send cmd fail\n");
  1101. }
  1102. else
  1103. {
  1104. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1105. }
  1106. }
  1107. static void mfunc_sta_set_active_p_set(void)
  1108. {
  1109. int val;
  1110. struct shmparam_t param;
  1111. printf("aps \nint kW pos:dhg neg:chg 0:idle\n");
  1112. printf(">");
  1113. scanf("%d", &val);
  1114. param.val = val;
  1115. if (tool_send_shmcmd(CMD_STA_SET_ACTIVEPSET, &param) < 0)
  1116. {
  1117. printf("tool send cmd fail\n");
  1118. }
  1119. else
  1120. {
  1121. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1122. }
  1123. }
  1124. static menu_t menu_sta[] = {
  1125. {0, "return", mfunc_sta_return},
  1126. {0, "send cmd", mfunc_sta_sendcmd}, //
  1127. {0, "set aps", mfunc_sta_set_active_p_set}, //
  1128. };
  1129. static void mfunc_sta(void)
  1130. {
  1131. int i;
  1132. int iinput;
  1133. bmenu_sta_run = 1;
  1134. while (bmenu_sta_run)
  1135. {
  1136. printf("\n\n-----------------\nSTA MENU:\n");
  1137. for (i = 0; i < sizeof(menu_sta) / sizeof(menu_t); i++)
  1138. {
  1139. if (menu_sta[i].bHide != 1)
  1140. {
  1141. printf(" %d->%s\n", i, menu_sta[i].szInfo);
  1142. }
  1143. }
  1144. printf(">");
  1145. scanf("%d", &iinput);
  1146. if (iinput < sizeof(menu_sta) / sizeof(menu_t))
  1147. {
  1148. if (menu_sta[iinput].bHide != 1)
  1149. {
  1150. menu_sta[iinput].func();
  1151. }
  1152. else
  1153. {
  1154. printf("Illegal selection\n");
  1155. }
  1156. }
  1157. else
  1158. {
  1159. printf("Illegal selection\n");
  1160. }
  1161. }
  1162. }
  1163. static void mfunc_cloud_return(void)
  1164. {
  1165. bmenu_cloud_run = 0;
  1166. }
  1167. static void mfunc_cloud_set_ess_sys_en_intval(void)
  1168. {
  1169. int en, intval;
  1170. struct shmparam_t param;
  1171. int start = 1;
  1172. int nb = 1;
  1173. int i;
  1174. printf("enable interval\n");
  1175. printf("enable 0:disable 1:enable\n");
  1176. printf("interval unit:seconds\n");
  1177. printf(">");
  1178. scanf("%d %d", &en, &intval);
  1179. for (i = 0; i < nb; i++)
  1180. {
  1181. param.idx = start + i;
  1182. param.val = en;
  1183. param.val2 = intval;
  1184. if (tool_send_shmcmd(CMD_CLOUD_SET_ESSSYS_ENINTVAL, &param) < 0)
  1185. {
  1186. printf("tool send cmd fail\n");
  1187. }
  1188. else
  1189. {
  1190. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1191. }
  1192. }
  1193. }
  1194. static menu_t menu_cloud[] = {
  1195. {0, "return", mfunc_cloud_return},
  1196. {0, "set ess_sys enable and interval", mfunc_cloud_set_ess_sys_en_intval},
  1197. };
  1198. static void mfunc_cloud(void)
  1199. {
  1200. int i;
  1201. int iinput;
  1202. bmenu_cloud_run = 1;
  1203. while (bmenu_cloud_run)
  1204. {
  1205. printf("\n\n-----------------\nCLOUD MENU:\n");
  1206. for (i = 0; i < sizeof(menu_cloud) / sizeof(menu_t); i++)
  1207. {
  1208. if (menu_cloud[i].bHide != 1)
  1209. {
  1210. printf(" %d->%s\n", i, menu_cloud[i].szInfo);
  1211. }
  1212. }
  1213. printf(">");
  1214. scanf("%d", &iinput);
  1215. if (iinput < sizeof(menu_cloud) / sizeof(menu_t))
  1216. {
  1217. if (menu_cloud[iinput].bHide != 1)
  1218. {
  1219. menu_cloud[iinput].func();
  1220. }
  1221. else
  1222. {
  1223. printf("Illegal selection\n");
  1224. }
  1225. }
  1226. else
  1227. {
  1228. printf("Illegal selection\n");
  1229. }
  1230. }
  1231. }
  1232. static void mfunc_ems_return(void)
  1233. {
  1234. bmenu_ems_run = 0;
  1235. }
  1236. static void mfunc_ems_set_pow_regulate_high_low(void)
  1237. {
  1238. int high, low;
  1239. struct shmparam_t param;
  1240. int start = 1;
  1241. int nb = 1;
  1242. int i;
  1243. printf("high low\n");
  1244. printf("double kW\n");
  1245. printf(">");
  1246. scanf("%d %d", &high, &low);
  1247. for (i = 0; i < nb; i++)
  1248. {
  1249. param.idx = start + i;
  1250. param.val_arr[0] = high;
  1251. param.val_arr[1] = low;
  1252. if (tool_send_shmcmd(CMD_EMS_SET_POWREG_HIGHLOW, &param) < 0)
  1253. {
  1254. printf("tool send cmd fail\n");
  1255. }
  1256. else
  1257. {
  1258. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1259. }
  1260. }
  1261. }
  1262. static void mfunc_ems_set_pcurvidx(void)
  1263. {
  1264. int pidx;
  1265. struct shmparam_t param;
  1266. int start = 1;
  1267. int nb = 1;
  1268. int i;
  1269. printf("pcurv_idx\n");
  1270. printf("int [1 16]\n");
  1271. printf(">");
  1272. scanf("%d", &pidx);
  1273. for (i = 0; i < nb; i++)
  1274. {
  1275. param.idx = start + i;
  1276. param.val = pidx;
  1277. if (tool_send_shmcmd(CMD_EMS_SET_PIDX, &param) < 0)
  1278. {
  1279. printf("tool send cmd fail\n");
  1280. }
  1281. else
  1282. {
  1283. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1284. }
  1285. }
  1286. }
  1287. static void mfunc_ems_reset_pcurv(void)
  1288. {
  1289. int pidx;
  1290. struct shmparam_t param;
  1291. int start = 1;
  1292. int nb = 1;
  1293. int i;
  1294. for (i = 0; i < nb; i++)
  1295. {
  1296. if (tool_send_shmcmd(CMD_EMS_RESET_PCURV, &param) < 0)
  1297. {
  1298. printf("tool send cmd fail\n");
  1299. }
  1300. else
  1301. {
  1302. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1303. }
  1304. }
  1305. }
  1306. static void mfunc_ems_print_pcurv(void)
  1307. {
  1308. }
  1309. static void mfunc_ems_edit_pcurv(void)
  1310. {
  1311. struct shmparam_t param;
  1312. int start = 1;
  1313. int nb = 1;
  1314. int i;
  1315. int starthh, startnn;
  1316. int endhh, endnn;
  1317. int aps;
  1318. printf("start_hour start_minute end_hour end_minute aps\n");
  1319. printf(">");
  1320. scanf("%d %d %d %d %d", &starthh, &startnn, &endhh, &endnn, &aps);
  1321. for (i = 0; i < nb; i++)
  1322. {
  1323. param.val_arr[0] = starthh;
  1324. param.val_arr[1] = startnn;
  1325. param.val_arr[2] = endhh;
  1326. param.val_arr[3] = endnn;
  1327. param.val = aps;
  1328. if (tool_send_shmcmd(CMD_EMS_EDITPCURV, &param) < 0)
  1329. {
  1330. printf("tool send cmd fail\n");
  1331. }
  1332. else
  1333. {
  1334. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1335. }
  1336. }
  1337. }
  1338. static void mfunc_ems_save_pcurv(void)
  1339. {
  1340. int pidx;
  1341. struct shmparam_t param;
  1342. int start = 1;
  1343. int nb = 1;
  1344. int i;
  1345. for (i = 0; i < nb; i++)
  1346. {
  1347. if (tool_send_shmcmd(CMD_EMS_SAVEPCURV, &param) < 0)
  1348. {
  1349. printf("tool send cmd fail\n");
  1350. }
  1351. else
  1352. {
  1353. printf("tool send cmd ok, rsp:%d\n", SHMBUF.rsp);
  1354. }
  1355. }
  1356. }
  1357. static menu_t menu_ems[] = {
  1358. {0, "return", mfunc_ems_return},
  1359. {0, "set pow regulate high and low", mfunc_ems_set_pow_regulate_high_low},
  1360. {0, "set pcurv idx", mfunc_ems_set_pcurvidx},
  1361. {0, "reset pcurv", mfunc_ems_reset_pcurv},
  1362. {0, "print pcurv", mfunc_ems_print_pcurv},
  1363. {0, "edit pcurv", mfunc_ems_edit_pcurv},
  1364. {0, "save pcurv", mfunc_ems_save_pcurv},
  1365. };
  1366. static void mfunc_ems(void)
  1367. {
  1368. int i;
  1369. int iinput;
  1370. bmenu_ems_run = 1;
  1371. while (bmenu_ems_run)
  1372. {
  1373. printf("\n\n-----------------\nEMS MENU:\n");
  1374. for (i = 0; i < sizeof(menu_ems) / sizeof(menu_t); i++)
  1375. {
  1376. if (menu_ems[i].bHide != 1)
  1377. {
  1378. printf(" %d->%s\n", i, menu_ems[i].szInfo);
  1379. }
  1380. }
  1381. printf(">");
  1382. scanf("%d", &iinput);
  1383. if (iinput < sizeof(menu_ems) / sizeof(menu_t))
  1384. {
  1385. if (menu_ems[iinput].bHide != 1)
  1386. {
  1387. menu_ems[iinput].func();
  1388. }
  1389. else
  1390. {
  1391. printf("Illegal selection\n");
  1392. }
  1393. }
  1394. else
  1395. {
  1396. printf("Illegal selection\n");
  1397. }
  1398. }
  1399. }
  1400. static menu_t menu_main[] = {
  1401. /* 0 - 9 quick launch */
  1402. {0, "exit", mfunc_exit}, // 0
  1403. {0, "print sta", mfunc_print_sta}, // 1
  1404. {0, "sta send stdby cmd", mfunc_sta_send_stdby_cmd}, // 2
  1405. {0, "sta send stop cmd", mfunc_sta_send_stop_cmd}, // 3
  1406. {0, "sta send ready cmd", mfunc_sta_send_ready_cmd}, // 4
  1407. {0, "sta set aps", mfunc_sta_set_active_p_set}, // 5
  1408. {1, "r", mfunc_exit}, // 6
  1409. {1, "r", mfunc_exit}, // 7
  1410. {1, "r", mfunc_exit}, // 8
  1411. {1, "r", mfunc_exit}, // 9
  1412. /* 10 - 19 sta */
  1413. {0, "sta", mfunc_sta},
  1414. {0, "ems", mfunc_ems},
  1415. {0, "cloud", mfunc_cloud},
  1416. {1, "reserved", mfunc_exit},
  1417. {1, "reserved", mfunc_exit},
  1418. {1, "reserved", mfunc_exit},
  1419. {1, "reserved", mfunc_exit},
  1420. {1, "reserved", mfunc_exit},
  1421. {1, "reserved", mfunc_exit},
  1422. {1, "reserved", mfunc_exit},
  1423. /* 20 - 29 ctn */
  1424. {0, "ctn", mfunc_ctn},
  1425. {1, "reserved", mfunc_exit},
  1426. {1, "reserved", mfunc_exit},
  1427. {1, "reserved", mfunc_exit},
  1428. {1, "reserved", mfunc_exit},
  1429. {1, "reserved", mfunc_exit},
  1430. {1, "reserved", mfunc_exit},
  1431. {1, "reserved", mfunc_exit},
  1432. {1, "reserved", mfunc_exit},
  1433. {1, "reserved", mfunc_exit},
  1434. /* 30 - 39 chan */
  1435. {0, "chan", mfunc_chan}, // 16
  1436. {1, "reserved", mfunc_exit},
  1437. {1, "reserved", mfunc_exit},
  1438. {1, "reserved", mfunc_exit},
  1439. {1, "reserved", mfunc_exit},
  1440. {1, "reserved", mfunc_exit},
  1441. {1, "reserved", mfunc_exit},
  1442. {1, "reserved", mfunc_exit},
  1443. {1, "reserved", mfunc_exit},
  1444. {1, "reserved", mfunc_exit},
  1445. /* 40 - 49 pcs */
  1446. {1, "pcs", mfunc_exit},
  1447. {1, "reserved", mfunc_exit},
  1448. {1, "reserved", mfunc_exit},
  1449. {1, "reserved", mfunc_exit},
  1450. {1, "reserved", mfunc_exit},
  1451. {1, "reserved", mfunc_exit},
  1452. {1, "reserved", mfunc_exit},
  1453. {1, "reserved", mfunc_exit},
  1454. {1, "reserved", mfunc_exit},
  1455. {1, "reserved", mfunc_exit},
  1456. /* 50 - 59 pack */
  1457. {1, "pack", mfunc_exit},
  1458. {1, "reserved", mfunc_exit},
  1459. {1, "reserved", mfunc_exit},
  1460. {1, "reserved", mfunc_exit},
  1461. {1, "reserved", mfunc_exit},
  1462. {1, "reserved", mfunc_exit},
  1463. {1, "reserved", mfunc_exit},
  1464. {1, "reserved", mfunc_exit},
  1465. {1, "reserved", mfunc_exit},
  1466. {1, "reserved", mfunc_exit},
  1467. /* 60 - 69 meter */
  1468. {0, "meter", mfunc_meter},
  1469. {1, "reserved", mfunc_exit},
  1470. {1, "reserved", mfunc_exit},
  1471. {1, "reserved", mfunc_exit},
  1472. {1, "reserved", mfunc_exit},
  1473. {1, "reserved", mfunc_exit},
  1474. {1, "reserved", mfunc_exit},
  1475. {1, "reserved", mfunc_exit},
  1476. {1, "reserved", mfunc_exit},
  1477. {1, "reserved", mfunc_exit},
  1478. /* 70 - 79 mqtt*/
  1479. {0, "mqtt", mfunc_mqtt},
  1480. {1, "reserved", mfunc_exit},
  1481. {1, "reserved", mfunc_exit},
  1482. {1, "reserved", mfunc_exit},
  1483. {1, "reserved", mfunc_exit},
  1484. {1, "reserved", mfunc_exit},
  1485. {1, "reserved", mfunc_exit},
  1486. {1, "reserved", mfunc_exit},
  1487. {1, "reserved", mfunc_exit},
  1488. {1, "reserved", mfunc_exit},
  1489. /* 80 - 89 */
  1490. {0, "misc", mfunc_misc},
  1491. {1, "reserved", mfunc_exit},
  1492. {1, "reserved", mfunc_exit},
  1493. {1, "reserved", mfunc_exit},
  1494. {1, "reserved", mfunc_exit},
  1495. {1, "reserved", mfunc_exit},
  1496. {1, "reserved", mfunc_exit},
  1497. {1, "reserved", mfunc_exit},
  1498. {1, "reserved", mfunc_exit},
  1499. };
  1500. int tool_main()
  1501. {
  1502. int ret = 0;
  1503. int i;
  1504. int shm_id = -1;
  1505. void *shared_memory = NULL;
  1506. int iinput;
  1507. shm_id = shmget((key_t)SHMID, 0, 0666);
  1508. if (shm_id < 0)
  1509. {
  1510. ret = -1;
  1511. goto leave;
  1512. }
  1513. else
  1514. {
  1515. shared_memory = shmat(shm_id, NULL, 0);
  1516. if (shared_memory == NULL)
  1517. {
  1518. ret = -2;
  1519. goto leave;
  1520. }
  1521. else
  1522. {
  1523. SHM = (struct shm_t *)shared_memory;
  1524. semPing = sem_open(pstr_sem_name_ping, 0, 0644, 0);
  1525. if (semPing == SEM_FAILED)
  1526. {
  1527. ret = -3;
  1528. goto leave;
  1529. }
  1530. else
  1531. {
  1532. semPong = sem_open(pstr_sem_name_pong, 0, 0644, 0);
  1533. if (semPong == SEM_FAILED)
  1534. {
  1535. ret = -4;
  1536. goto leave;
  1537. }
  1538. else
  1539. {
  1540. printf("****************\n");
  1541. printf("tool init success\n");
  1542. printf("****************\n");
  1543. }
  1544. }
  1545. }
  1546. }
  1547. bmain_run = 1;
  1548. while (bmain_run)
  1549. {
  1550. printf("\n\n-----------------\nMain MENU:\n");
  1551. for (i = 0; i < sizeof(menu_main) / sizeof(menu_t); i++)
  1552. {
  1553. if (menu_main[i].bHide != 1)
  1554. {
  1555. printf(" %d->%s\n", i, menu_main[i].szInfo);
  1556. }
  1557. }
  1558. printf(">");
  1559. scanf("%d", &iinput);
  1560. if (iinput < sizeof(menu_main) / sizeof(menu_t))
  1561. {
  1562. if (menu_main[iinput].bHide != 1)
  1563. {
  1564. menu_main[iinput].func();
  1565. }
  1566. else
  1567. {
  1568. printf("Illegal selection\n");
  1569. }
  1570. }
  1571. else
  1572. {
  1573. printf("Illegal selection\n");
  1574. }
  1575. }
  1576. leave:
  1577. printf("%s, --", __func__);
  1578. return 0;
  1579. }