hmi.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-26 Administrator the first version
  9. */
  10. /* [2026-10-15] 日志配置必须放在本文件所有 include 之前: spi_flash_sfud.h ->
  11. * sfud_cfg.h 会抢先 #include <rtdbg.h>(该头带 include guard) 并以 DBG_LVL=DBG_INFO
  12. * 钉死本文件的日志等级 —— 放在后面会使 LOG_D 被整体裁剪
  13. * (现象: 屏幕接收原始数据等 LOG_D 打印消失)。此处直接定义 DBG_SECTION_NAME 而非
  14. * DBG_TAG, 避免 sfud_cfg.h 的 DBG_TAG 重定义("SFUD")影响本文件日志节名。 */
  15. #define DBG_ENABLE
  16. #define DBG_COLOR
  17. #define DBG_SECTION_NAME "hmi"
  18. #define DBG_LEVEL DBG_LOG
  19. #include <rtdbg.h>
  20. #include <rtthread.h>
  21. #include "hmi.h"
  22. #include <board.h>
  23. #include <control.h>
  24. #include <rtdevice.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "stdarg.h"
  29. #include "string.h"
  30. #include "sht30_collect.h"
  31. #include "beep.h"
  32. #include "easyflash.h"
  33. #include "sound.h"
  34. #include "ymodem.h"
  35. #include <shell.h>
  36. #include <msh.h> /* msh_exec:屏幕 debug1 调试页控制台命令执行 */
  37. #include "dtu_uart2.h"
  38. #include <fal.h>
  39. #include <spi_flash_sfud.h> /* debug1 页外挂Flash信息(与 sys_monitor.c 相同方式读取) */
  40. #include "selfcheck.h"
  41. #include <user_mb_app.h>
  42. #include "sys_monitor.h"
  43. #include "param_backup.h"
  44. #define HMI_THREAD_PRIORITY 5 //线程优先级
  45. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  46. #define HMI_THREAD_TIMESLICE 10 //时间片
  47. #define CONTROL_MOTOR_PRIORITY 18
  48. #define CONTROL_MOTOR_STACK_SIZE 2048
  49. #define CONTROL_MOTOR_TIMESLICE 5
  50. static char msg_pool[512];
  51. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  52. static char rx_pkt[HMI_UART_RXBUFFER_SIZE + 1];
  53. int mb_master_samlpe();
  54. /**/
  55. /* device_status 已收敛为全工程唯一定义(在 main.c), 此处不再重复定义, 见 control.h 的 extern 声明 */
  56. /* system_status 已收敛为全工程唯一定义(在 sys_monitor.c), 见 sys_monitor.h 的 extern 声明 */
  57. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  58. time_t time(time_t *t);
  59. /* 串口接收消息结构*/
  60. struct rx_msg
  61. {
  62. rt_device_t dev;
  63. rt_size_t size;
  64. };
  65. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  66. ////
  67. static rt_thread_t hmi1 = RT_NULL;
  68. static rt_thread_t ctr_motor = RT_NULL;
  69. rt_device_t serial; /*串口设备句柄*/
  70. rt_timer_t timer_uart2 = RT_NULL;
  71. static upgrade_state_t current_state = STATE_IDLE;
  72. uint32_t total_file_size = 0;
  73. static uint32_t bytes_received = 0;
  74. static uint8_t recv_buffer[RECV_BUFFER_SIZE];
  75. static uint32_t recv_write_pos = 0;
  76. /* 消息队列控制块 */
  77. static struct rt_messagequeue rx_mq;//接收数据消息队列
  78. static void HMI_Decode(void);
  79. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  80. /* P2-14 把开机等联网搬进独立线程时新增的两个函数, 定义在文件后段
  81. (Check_ParamConfig 之后), 但 net_wait_start() 在前面的 Check_ParamConfig 里
  82. 就被调用 -> 必须先有前向声明, 否则报 implicit declaration。 */
  83. static void net_wait_thread_entry(void);
  84. /* 开机"联网检测线程"(net_wait)是否仍在运行。
  85. net_wait 在等网期间自己每秒发一帧 read 去刷新网络状态;
  86. refresh_hmi_Component() 每 5 个节拍也会发一帧 read —— 两者同时发就是重复上报,
  87. RTU 会连续收到两倍 read 帧。用本标志让 HMI 侧在 net_wait 退出前让路。 */
  88. static volatile uint8_t net_wait_running = 0;
  89. #ifdef RT_USING_SERIAL_V2
  90. static int HMI_uart4_init(void)
  91. {
  92. serial = rt_device_find(UART_NAME);
  93. /*修改串口配置参数*/
  94. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  95. config.data_bits = DATA_BITS_8; //数据位8
  96. config.stop_bits = STOP_BITS_1; //停止位1
  97. #ifdef RT_USING_SERIAL_V2
  98. config.rx_bufsz = 512;
  99. config.tx_bufsz = 1024;
  100. #else
  101. config.bufsz = 512; //缓冲区128字节
  102. #endif
  103. config.parity = PARITY_NONE; //无奇偶校验
  104. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  105. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  106. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  107. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  108. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  109. #ifdef RT_USING_SERIAL_V2
  110. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  111. #else
  112. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  113. #endif
  114. if(uart_res!=RT_EOK)
  115. {
  116. LOG_E("%s打开失败!",UART_NAME);
  117. }else{
  118. LOG_I("%s Open Success!",UART_NAME);
  119. }
  120. return RT_EOK;
  121. }
  122. #endif
  123. /* ==================== UART4(屏幕)发送:互斥 / 升级闸门 / 批量 / 去重 ====================
  124. * 来源: master 分支 2026-10-10 发送优化(HMI_WRITE 签名不变, 调用方无感):
  125. * ① 批量发送:HMI_BATCH_BEGIN()/END() 之间累积成整块再一次写入(缓冲满自动 flush);
  126. * 整页刷新 60+ 条属性由 60+ 次写合并为 1~3 次写。
  127. * ② 重复过滤:同一调用点(用格式化串地址唯一标识)输出内容与上次相同则不发送;
  128. * 直接映射表, 槽位冲突按"未命中"处理——只会多发、绝不会漏发。
  129. * ③ 过滤表每 HMI_DEDUPE_RESYNC_SEC 秒整体复位, 并在 Swversion 握手串变化
  130. * (换型号/换页面/屏幕重启)时立即复位, 保证屏幕不长期停在旧值。
  131. * ④ 升级闸门: 屏幕升级传输期间屏蔽普通刷新指令(见 Hmi_Upgrade_Begin/End),
  132. * 避免业务数据混入升级固件流; 60s 无新升级数据自动解除。
  133. * 平台差异(apm32f1): 保留 g_hmi_port_closed 早退(OTA 前 uart4 已 close,
  134. * 继续写会解引用已释放的 tx_fifo 而 HardFault, 见 hmi.h 注释)。
  135. * 维护提示:新增刷新项直接用 HMI_WRITE 即可, 无需关心批量/去重。 */
  136. /* ---------- 发送互斥与升级状态 ---------- */
  137. static struct rt_mutex hmi_write_mutex;
  138. static rt_bool_t hmi_mutex_inited = RT_FALSE;
  139. static volatile rt_tick_t s_hmi_upgrade_tick = 0; /* 0=不在升级; 非0=最近一次升级数据时间 */
  140. /* 串口4(HMI屏)在 OTA 升级前被关闭, 置 1 后所有发送路径直接 return。
  141. 为什么必须有这个标志而不能只靠句柄判空:
  142. rt_device_close() 真正关掉设备时, serial_v2 的 rt_serial_rx/tx_disable()
  143. 会 rt_free 掉 rx/tx fifo 并把 serial->serial_rx / serial_tx 置 NULL,
  144. 但 dev 指针本身依然非空、依然能被 rt_device_write() 调到;
  145. 而 rt_serial_write() 会直接 `serial->serial_tx->activated` 解引用 →
  146. 空指针, 整机 HardFault。所以必须靠这个显式标志拦住。 */
  147. rt_uint8_t g_hmi_port_closed = 0;
  148. static rt_bool_t hmi_write_lock(void)
  149. {
  150. /* 中断上下文不加锁(互斥锁不允许在中断中获取) */
  151. if (rt_interrupt_get_nest() != 0)
  152. {
  153. return RT_FALSE;
  154. }
  155. if (!hmi_mutex_inited)
  156. {
  157. rt_base_t level = rt_hw_interrupt_disable();
  158. if (!hmi_mutex_inited)
  159. {
  160. if (rt_mutex_init(&hmi_write_mutex, "hmi_mtx", RT_IPC_FLAG_FIFO) == RT_EOK)
  161. {
  162. hmi_mutex_inited = RT_TRUE;
  163. }
  164. }
  165. rt_hw_interrupt_enable(level);
  166. }
  167. if (hmi_mutex_inited)
  168. {
  169. rt_mutex_take(&hmi_write_mutex, RT_WAITING_FOREVER);
  170. return RT_TRUE;
  171. }
  172. return RT_FALSE;
  173. }
  174. static void hmi_write_unlock(void)
  175. {
  176. if (hmi_mutex_inited)
  177. {
  178. rt_mutex_release(&hmi_write_mutex);
  179. }
  180. }
  181. void Hmi_Upgrade_Begin(void)
  182. {
  183. s_hmi_upgrade_tick = rt_tick_get();
  184. LOG_I("HMI upgrade begin");
  185. }
  186. void Hmi_Upgrade_End(void)
  187. {
  188. s_hmi_upgrade_tick = 0;
  189. LOG_I("HMI upgrade end");
  190. }
  191. /* 升级传输期间返回真:屏蔽普通属性刷新,避免混入固件数据流;
  192. * 60s 内没有新的升级数据自动解除(防止升级中断后屏幕刷新永久失效)。 */
  193. static rt_bool_t hmi_upgrade_busy(void)
  194. {
  195. rt_tick_t t = s_hmi_upgrade_tick;
  196. if (t == 0)
  197. {
  198. return RT_FALSE;
  199. }
  200. if ((rt_tick_get() - t) > (60 * RT_TICK_PER_SECOND))
  201. {
  202. s_hmi_upgrade_tick = 0;
  203. LOG_W("HMI upgrade timeout, auto clear busy flag");
  204. return RT_FALSE;
  205. }
  206. return RT_TRUE;
  207. }
  208. /* ---------- 重复过滤(CRC32 直接映射表) ---------- */
  209. #define HMI_DEDUPE_EN 1 /* 置 0 可整体关闭重复过滤(调试用) */
  210. #define HMI_DEDUPE_ENTRIES 96 /* 槽位数; 冲突只影响压缩率, 不影响正确性 */
  211. #define HMI_DEDUPE_RESYNC_SEC 10 /* 定期全量刷新周期(s) */
  212. typedef struct {
  213. const char *key; /* 格式化串地址:每个调用点唯一 */
  214. rt_uint32_t crc; /* 上次发送内容的 CRC32 */
  215. rt_uint16_t len; /* 上次发送内容长度 */
  216. } hmi_dedupe_item_t;
  217. static hmi_dedupe_item_t hmi_dedupe_tab[HMI_DEDUPE_ENTRIES];
  218. static rt_uint16_t hmi_dedupe_tick_cnt = 0;
  219. static rt_uint32_t hmi_crc32(const char *s, rt_uint16_t len)
  220. {
  221. rt_uint32_t crc = 0xFFFFFFFFu;
  222. rt_uint16_t i;
  223. rt_uint8_t k;
  224. for (i = 0; i < len; i++)
  225. {
  226. crc ^= (rt_uint8_t)s[i];
  227. for (k = 0; k < 8; k++)
  228. {
  229. crc = (crc >> 1) ^ (0xEDB88320u & (rt_uint32_t)(-(rt_int32_t)(crc & 1u)));
  230. }
  231. }
  232. return ~crc;
  233. }
  234. static rt_uint16_t hmi_dedupe_index(const char *key)
  235. {
  236. return (rt_uint16_t)(((rt_uint32_t)key >> 2) % HMI_DEDUPE_ENTRIES);
  237. }
  238. /* 返回 RT_TRUE:本次内容与上次完全相同,可跳过发送 */
  239. static rt_bool_t hmi_dedupe_skip(const char *key, const char *line, rt_uint16_t len)
  240. {
  241. rt_uint16_t idx = hmi_dedupe_index(key);
  242. hmi_dedupe_item_t *it = &hmi_dedupe_tab[idx];
  243. rt_uint32_t crc = hmi_crc32(line, len);
  244. if (it->key == key && it->len == len && it->crc == crc)
  245. {
  246. return RT_TRUE;
  247. }
  248. /* 写入顺序:crc/len 先写、key 最后写; 读取时先比 key——
  249. * 这样并发时"命中"的条目其 crc/len 一定是完整的 */
  250. it->crc = crc;
  251. it->len = len;
  252. it->key = key;
  253. return RT_FALSE;
  254. }
  255. static void hmi_dedupe_reset(void)
  256. {
  257. rt_memset(hmi_dedupe_tab, 0, sizeof(hmi_dedupe_tab)); /* key=NULL, 任何真实调用点都不会命中 */
  258. hmi_dedupe_tick_cnt = 0;
  259. }
  260. /* 由 Ctr_Mortor_entry 每 100ms 调用一次:到点复位过滤表(等价于下一次刷新全量重发) */
  261. static void hmi_dedupe_tick(void)
  262. {
  263. if (++hmi_dedupe_tick_cnt >= (HMI_DEDUPE_RESYNC_SEC * 10))
  264. {
  265. hmi_dedupe_reset();
  266. }
  267. }
  268. /* ---------- ① 批量发送 ---------- */
  269. #define HMI_BATCH_BUF_SIZE 1024 /* 累积缓冲; 满则自动 flush, 不丢指令 */
  270. static char hmi_batch_buf[HMI_BATCH_BUF_SIZE];
  271. static rt_uint16_t hmi_batch_len = 0;
  272. static volatile rt_bool_t hmi_batch_on = RT_FALSE;
  273. static rt_thread_t hmi_batch_owner = RT_NULL; /* 批量会话所属线程 */
  274. #define HMI_BATCH_BEGIN() Hmi_Batch_Begin()
  275. #define HMI_BATCH_END() Hmi_Batch_End()
  276. /* ---------- 完整写入(nonblocking TX 的环形缓冲保护,2026-10-15) ----------
  277. * uart4 以 TX_NON_BLOCKING 打开(见 HMI_uart4_init):数据先进 1024B 的 TX
  278. * 环形缓冲,rt_device_write 只接受能容纳的部分并立即返回,**超出的静默丢弃**。
  279. * 2.48 时代逐条发送(约30B)+ 每条 mdelay(1) 自然节流,缓冲几乎不积压;
  280. * 2.49 引入批量发送后单次 flush 可达 1024B:第 1 批写满缓冲并启动 DMA
  281. * (1024B@115200 ≈ 89ms 才发完),同一刷新内的第 2/3 批到达时缓冲仍满 →
  282. * **整批丢失**。现象:屏幕后段控件(menu.va_net / menu.cam_sta 等)收不到
  283. * 指令;且发送去重表已记录"值未变不再重发" → 该控件永久不更新
  284. * (每 10s 全量轮的重发又落回被丢批次)。
  285. * 本函数分块写入并检查实际写入量,未写完时让出等待 TX 缓冲释放后重试;
  286. * 中断上下文不等待(保持 HMI_WRITE 的中断可调用性),仅尽力发送。
  287. * 返回实际写入字节数。 */
  288. #define HMI_TX_RETRY_MAX 400 /* 最长等待约 400ms:覆盖升级块 4KB@115200≈356ms;普通刷新远小于此 */
  289. static rt_uint16_t hmi_write_all(const void *data, rt_uint16_t len)
  290. {
  291. const char *pbuf = (const char *)data;
  292. rt_uint16_t sent = 0;
  293. rt_uint16_t retry = 0;
  294. while (sent < len)
  295. {
  296. rt_size_t n = rt_device_write(serial, 0, pbuf + sent, len - sent);
  297. sent += (rt_uint16_t)n;
  298. if (sent >= len)
  299. {
  300. break;
  301. }
  302. /* 中断上下文不能等待:放弃剩余(正常业务发送都走线程上下文) */
  303. if (rt_interrupt_get_nest() != 0)
  304. {
  305. break;
  306. }
  307. if (++retry > HMI_TX_RETRY_MAX)
  308. {
  309. LOG_W("hmi tx wait timeout, dropped %u bytes", (unsigned)(len - sent));
  310. break;
  311. }
  312. rt_thread_mdelay(1);
  313. }
  314. system_status.uart4_tx_bytes += sent;
  315. return sent;
  316. }
  317. void Hmi_Batch_Begin(void)
  318. {
  319. hmi_batch_len = 0;
  320. hmi_batch_owner = rt_thread_self();
  321. hmi_batch_on = RT_TRUE;
  322. }
  323. void Hmi_Batch_End(void)
  324. {
  325. hmi_batch_on = RT_FALSE;
  326. hmi_batch_owner = RT_NULL;
  327. if (hmi_batch_len > 0)
  328. {
  329. if (!g_hmi_port_closed && serial != RT_NULL)
  330. {
  331. rt_bool_t locked = hmi_write_lock();
  332. hmi_write_all(hmi_batch_buf, hmi_batch_len); /* 完整写入: TX 缓冲满时等待重试, 不丢数据 */
  333. if (locked)
  334. {
  335. hmi_write_unlock();
  336. }
  337. }
  338. hmi_batch_len = 0;
  339. }
  340. }
  341. /* 访问批量缓冲只允许批量会话的属主线程(见 HMI_WRITE):
  342. * 其它线程即便在批量期间调用 HMI_WRITE 也直接立即发送,
  343. * 避免两线程同时往同一缓冲追加导致内容错乱。 */
  344. static rt_bool_t hmi_batch_owned(void)
  345. {
  346. return (hmi_batch_on && hmi_batch_owner == rt_thread_self()) ? RT_TRUE : RT_FALSE;
  347. }
  348. /* 单条立即发送(批量会话外的默认行为) */
  349. static void hmi_send_now(const char *data, rt_uint16_t len)
  350. {
  351. rt_bool_t locked = hmi_write_lock();
  352. hmi_write_all(data, len); /* 完整写入: TX 缓冲满时等待重试, 不丢数据 */
  353. if (locked)
  354. {
  355. hmi_write_unlock();
  356. }
  357. }
  358. void HMI_WRITE(char *fmt, ...)
  359. {
  360. va_list ap;
  361. char str[256 + 3]; /* 预留 3 字节结束符 0xFF 0xFF 0xFF */
  362. rt_size_t str_length;
  363. /* OTA 期间串口4已关闭(见 control.c 的 uart_stop_for_ota)必须最先返回:
  364. 不能只靠 serial 判空——rt_device_close 释放的是 serial_v2 内部 fifo,
  365. 设备指针本身仍非空, 继续写会解引用已释放的 tx_fifo 而 HardFault。 */
  366. if (g_hmi_port_closed || serial == RT_NULL || fmt == NULL) {
  367. return;
  368. }
  369. /* 升级传输期间屏蔽普通业务指令 */
  370. if (hmi_upgrade_busy()) {
  371. return;
  372. }
  373. va_start(ap, fmt);
  374. str_length = rt_vsnprintf(str, sizeof(str) - 3, fmt, ap);
  375. va_end(ap);
  376. if (str_length == 0 || str_length >= sizeof(str) - 3) {
  377. if (str_length > 0)
  378. {
  379. LOG_W("HMI_WRITE: frame too long(%d), dropped", (int)str_length);
  380. }
  381. return;
  382. }
  383. #if HMI_DEDUPE_EN
  384. /* ② 同一调用点内容未变化则不重发 */
  385. if (hmi_dedupe_skip(fmt, str, (rt_uint16_t)str_length))
  386. {
  387. return;
  388. }
  389. #endif
  390. /* 结束符与指令合并为一次写入, 减少写入次数、避免发送被拆散 */
  391. str[str_length] = (char)0xFF;
  392. str[str_length + 1] = (char)0xFF;
  393. str[str_length + 2] = (char)0xFF;
  394. if (hmi_batch_owned())
  395. {
  396. rt_uint16_t need = (rt_uint16_t)(str_length + 3);
  397. if ((rt_uint32_t)hmi_batch_len + need > sizeof(hmi_batch_buf))
  398. {
  399. Hmi_Batch_End(); /* 缓冲不足先落地一批 */
  400. Hmi_Batch_Begin(); /* 继续留在批量会话内(重新取回属主) */
  401. }
  402. if ((rt_uint32_t)hmi_batch_len + need <= sizeof(hmi_batch_buf))
  403. {
  404. rt_memcpy(hmi_batch_buf + hmi_batch_len, str, need);
  405. hmi_batch_len += need;
  406. }
  407. else
  408. {
  409. /* 单条本身就超过批量缓冲(异常情况): 直接发, 宁可多一次写也不丢指令 */
  410. hmi_send_now(str, need);
  411. }
  412. return;
  413. }
  414. hmi_send_now(str, (rt_uint16_t)(str_length + 3));
  415. }
  416. /* 接收数据回调函数 */
  417. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  418. {
  419. struct rx_msg msg;
  420. rt_err_t result;
  421. msg.dev = dev;
  422. msg.size = size;
  423. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  424. if ( result == -RT_EFULL)
  425. {
  426. /* 消息队列满 */
  427. LOG_E("hmi message queue full!\n");
  428. rt_mq_control(&rx_mq, RT_IPC_CMD_RESET, RT_NULL); // 清空队列
  429. }
  430. return result;
  431. }
  432. // 预计算转换表
  433. static const int8_t hex_to_byte[256] = {
  434. [0 ... 255] = -1,
  435. ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
  436. ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9,
  437. ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
  438. ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15
  439. };
  440. /**
  441. * @brief 快速HEX字符串转二进制
  442. * @param hex HEX字符串
  443. * @param bin 输出缓冲区
  444. * @param max_len 最大输出长度
  445. * @return 转换的字节数,<0表示错误
  446. */
  447. static inline int fast_hex_to_bin(const char* hex, uint8_t* bin, int max_len)
  448. {
  449. if (!hex || !bin || max_len <= 0) {
  450. return -1;
  451. }
  452. int hex_len = strlen(hex);
  453. if (hex_len % 2 != 0) {
  454. return -1; // HEX长度必须是偶数
  455. }
  456. int bin_len = hex_len / 2;
  457. if (bin_len > max_len) {
  458. return -1; // 输出缓冲区太小
  459. }
  460. for (int i = 0; i < bin_len; i++) {
  461. int high = hex_to_byte[(unsigned char)hex[i * 2]];
  462. int low = hex_to_byte[(unsigned char)hex[i * 2 + 1]];
  463. if (high < 0 || low < 0) {
  464. return -1; // 无效HEX字符
  465. }
  466. bin[i] = (high << 4) | low;
  467. }
  468. return bin_len;
  469. }
  470. // 发送原始数据到HMI
  471. static int send_raw_to_hmi(const uint8_t* data, uint32_t size)
  472. {
  473. if (g_hmi_port_closed || !serial || !data || size == 0) return -1;
  474. /* 4KB 块远大于 1024B TX 环形缓冲:必须完整写入(等待重试),
  475. 原实现单次写只进 1KB、返回 -1,升级数据静默丢失 3/4 */
  476. return (hmi_write_all(data, (rt_uint16_t)size) == (rt_uint16_t)size) ? 0 : -1;
  477. }
  478. // 最简洁版本 - 每4096字节直接清空
  479. void process_hex_payload(const char* hex_str, int req_id) {
  480. if (!hex_str) return;
  481. if (current_state != STATE_RECEIVING)
  482. {
  483. /* 新一轮屏幕升级开始: 复位重复过滤表, 升级结束后屏幕控件恢复全量刷新 */
  484. hmi_dedupe_reset();
  485. /* ★ 原来只复位去重表: 第二轮升级时 bytes_received 仍是上一轮的值,
  486. 首包就满足 >= total_file_size -> 立刻判"升级完成"并退出 HMI 线程。 */
  487. bytes_received = 0;
  488. recv_write_pos = 0;
  489. }
  490. /* 升级传输期间屏蔽普通刷新指令(见 HMI_WRITE 升级闸门) */
  491. Hmi_Upgrade_Begin();
  492. current_state = STATE_RECEIVING;
  493. // 转换HEX
  494. uint8_t temp[JSON_CHUNK_SIZE];
  495. int data_len = fast_hex_to_bin(hex_str, temp, JSON_CHUNK_SIZE);
  496. if (data_len <= 0) return;
  497. // 添加到缓冲区
  498. if (recv_write_pos + data_len > sizeof(recv_buffer)) {
  499. LOG_E("缓冲区溢出");
  500. return;
  501. }
  502. memcpy(recv_buffer + recv_write_pos, temp, data_len);
  503. recv_write_pos += data_len;
  504. bytes_received += data_len;
  505. LOG_I("收到 %d 字节,缓冲: %u/%u",
  506. data_len, recv_write_pos, sizeof(recv_buffer));
  507. // 每4096字节发送一次
  508. if (recv_write_pos >= 4096) {
  509. LOG_I("发送 4096 字节到HMI...");
  510. if (send_raw_to_hmi(recv_buffer, 4096) != 0) {
  511. LOG_E("发送失败");
  512. return;
  513. }
  514. LOG_I("发送成功");
  515. /* ★ 原来发送后直接 recv_write_pos = 0: 本块解码长度不是 512 整数倍时,
  516. 超出的尾部(最多 511 字节)被整块丢掉 -> 屏幕收到的固件流缺字节。
  517. 发完 4096 字节后把剩余字节前移保留。 */
  518. if (recv_write_pos > 4096) {
  519. memmove(recv_buffer, recv_buffer + 4096, recv_write_pos - 4096);
  520. recv_write_pos -= 4096;
  521. } else {
  522. recv_write_pos = 0;
  523. }
  524. /* 不再 return: 文件长度正好是 4096 整数倍时, 最后一包之后不会再有新数据,
  525. 原来在这里返回会让升级永远停在 RECEIVING。继续走下面的完成判断。 */
  526. }
  527. // 检查完成
  528. if (bytes_received >= total_file_size && total_file_size > 0) {
  529. if (recv_write_pos > 0) {
  530. LOG_I("发送最后 %u 字节", recv_write_pos);
  531. send_raw_to_hmi(recv_buffer, recv_write_pos);
  532. recv_write_pos = 0; // 清空
  533. }
  534. current_state = STATE_COMPLETED;
  535. Hmi_Upgrade_End();
  536. LOG_I("升级完成!");
  537. return;
  538. }
  539. // 发送进度
  540. int progress = total_file_size > 0 ?
  541. (bytes_received * 100 / total_file_size) : 0;
  542. char progress_json[256];
  543. rt_snprintf(progress_json, sizeof(progress_json),
  544. "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
  545. "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
  546. bytes_received, total_file_size, progress, req_id);
  547. /* ★ progress_json 是栈上数组(不能交给 dtu_write_json 释放), 但要加 "%s" 防止被当格式串 */
  548. DTU_Write("%s", progress_json);
  549. }
  550. /* ==================== UART4(屏幕)接收:帧组装 ====================
  551. * 屏幕每条指令以 "\r\n" 结尾, 而串口按 DMA 包分段:
  552. * ① 一条指令可能被拆到两次 read —— 旧实现靠 strstr 前缀匹配, 残段会误命中别的
  553. * 分支(2026-10-08 的 SwversionV4/V43 误切版本类型即此问题, 见 hmi_cmd_match);
  554. * ② 一次 read 可能含多条指令 —— 旧实现是 if/else 链, 只处理第一条匹配的分支,
  555. * 例如 "getFullData\r\n" 与 "putTest.switchHeat=1\r\n" 同包时, 按键会被丢掉。
  556. * 现在: 整包先喂入组装缓冲, 按 '\n' 切出完整帧逐条交给 HMI_Decode(帧内容保留
  557. * "\r\n", 与旧行为一致); 残段留待下次 read 拼接; 超长无结束符的帧丢弃并告警。 */
  558. #define HMI_FRAME_MAX 512
  559. static char hmi_frame[HMI_FRAME_MAX + 1];
  560. static rt_uint16_t hmi_frame_len = 0;
  561. /* 处理一帧完整指令; 返回 RT_TRUE 表示 HMI 线程应退出(屏幕升级完成) */
  562. static rt_bool_t Hmi_Frame_Handle(void)
  563. {
  564. rt_uint16_t copy_len;
  565. if (hmi_frame_len == 0)
  566. {
  567. return RT_FALSE;
  568. }
  569. hmi_frame[hmi_frame_len] = '\0';
  570. /* 屏幕升级应答: 单字节 0x05 */
  571. if (hmi_frame_len == 1 && (rt_uint8_t)hmi_frame[0] == HMI_ACK_BYTE)
  572. {
  573. LOG_W("屏幕响应信号!");
  574. hmi_frame_len = 0;
  575. if (current_state == STATE_COMPLETED)
  576. {
  577. Hmi_Upgrade_End();
  578. return RT_TRUE; /* 升级完成, Hmi_thread_entry 收到后退出线程 */
  579. }
  580. /* RTU 可以发送升级文件了 */
  581. dtu_write_json(makeJsonRpc2_0("upgradehmi",NULL,1));
  582. return RT_FALSE;
  583. }
  584. /* 交给解析入口(rx_buffer 为解析缓冲, HMI_Decode 内部会清空它) */
  585. copy_len = (hmi_frame_len < HMI_UART_RXBUFFER_SIZE) ? hmi_frame_len : HMI_UART_RXBUFFER_SIZE;
  586. rt_memcpy(rx_buffer, hmi_frame, copy_len);
  587. rx_buffer[copy_len] = '\0';
  588. hmi_frame_len = 0;
  589. HMI_Decode();
  590. return RT_FALSE;
  591. }
  592. /* 喂入串口收到的原始数据; 返回 RT_TRUE 表示升级完成、HMI 线程应退出 */
  593. static rt_bool_t Hmi_Rx_Feed(const char *data, rt_uint16_t len)
  594. {
  595. rt_uint16_t i;
  596. for (i = 0; i < len; i++)
  597. {
  598. char c = data[i];
  599. /* 升级应答 0x05: 单字节, 不参与帧组装 */
  600. if (hmi_frame_len == 0 && (rt_uint8_t)c == HMI_ACK_BYTE)
  601. {
  602. hmi_frame[0] = c;
  603. hmi_frame_len = 1;
  604. if (Hmi_Frame_Handle())
  605. {
  606. return RT_TRUE;
  607. }
  608. continue;
  609. }
  610. if (hmi_frame_len >= HMI_FRAME_MAX)
  611. {
  612. LOG_W("hmi frame too long(%d), drop!", hmi_frame_len);
  613. hmi_frame_len = 0;
  614. }
  615. hmi_frame[hmi_frame_len++] = c;
  616. if (c == '\n')
  617. {
  618. if (Hmi_Frame_Handle())
  619. {
  620. return RT_TRUE;
  621. }
  622. }
  623. }
  624. return RT_FALSE;
  625. }
  626. /*入口函数*/
  627. static void Hmi_thread_entry(void *parameter)
  628. {
  629. struct rx_msg msg;
  630. rt_err_t result;
  631. rt_uint32_t rx_length;
  632. if(device_status.dtype==3){
  633. device_status.collt_box_sta = 0;
  634. device_status.run_sta = 0;
  635. }
  636. device_status.upan_sta = 1;
  637. device_status.camsta = 1;
  638. while(1)
  639. {
  640. rt_memset(&msg, 0, sizeof(msg));
  641. /* 从消息队列中读取消息*/
  642. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  643. if (result == RT_EOK)
  644. {
  645. /* 读到本次 DMA 收到的原始数据(rx_pkt), 按帧组装后再解析 */
  646. rx_length = rt_device_read(msg.dev, 0, rx_pkt, HMI_UART_RXBUFFER_SIZE);
  647. if (rx_length > 0)
  648. {
  649. system_status.uart4_rx_bytes += rx_length;
  650. rx_pkt[rx_length] = '\0';
  651. /* 屏幕每帧原始数据打印: 保持 LOG_I 可见级(2.48 行为)。
  652. * master 减噪版曾降为 LOG_D —— 控制台调屏/排查依赖本行直接可见, 勿降级 */
  653. LOG_I("hmi rx(%u): %s", (unsigned)rx_length, rx_pkt);
  654. if (Hmi_Rx_Feed(rx_pkt, (rt_uint16_t)rx_length))
  655. {
  656. return; /* 屏幕升级完成, 线程退出 */
  657. }
  658. }
  659. }
  660. /*********************************************************************************************************************/
  661. //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  662. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  663. }
  664. }
  665. /*******************************************************************************************/
  666. void Start_timer_Uart2(void)
  667. {
  668. device_status.mock_uart2_timer_flag = 1;
  669. }
  670. void Stop_timer_Uart2(void)
  671. {
  672. device_status.mock_uart2_timer_flag = 0;
  673. }
  674. static void refresh_hmi_Component(struct tm tm)
  675. {
  676. if(device_status.refresh_num>=5)
  677. {
  678. device_status.refresh_num = 0;
  679. /* ★ 开机联网检测线程(net_wait)还没退出时, 它自己每秒都在发 read 刷状态:
  680. 这里再发就是重复上报。等它退出(联网成功/超时)后由本函数接管刷新。
  681. 顺带补上 makeJson_ReadInfo() 的空指针判断(原实现会把 NULL 送进 DTU_Write)。 */
  682. if (net_wait_running == 1)
  683. {
  684. char *readinfo = makeJson_ReadInfo();
  685. if (readinfo != RT_NULL)
  686. {
  687. DTU_Write("%s", readinfo);
  688. rt_free(readinfo);
  689. }
  690. }
  691. device_status.hmi_fresh = 1;
  692. rt_thread_delay(100);
  693. }
  694. //在解析getinfo中清除超时0
  695. if(device_status.refresh_timeout==15)
  696. {
  697. rt_sprintf(device_status.sim,"1");
  698. rt_sprintf(device_status.net,"1");
  699. rt_sprintf(device_status.csq,"0");
  700. device_status.camsta = 1;
  701. device_status.link_sta=0;
  702. device_status.refresh_timeout=0;
  703. HMI_WRITE("menu.warn.txt=\"%s\"","[RTU/4G模块]通信失败,检查连接/固件!");
  704. }
  705. device_status.refresh_num++;
  706. device_status.refresh_timeout++;
  707. if(device_status.version_type!=6)
  708. {
  709. HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid);
  710. if(strstr(device_status.mqttcfg1,"8.136.98.49")!=NULL)
  711. {
  712. HMI_WRITE("uploadSet.plt.val=%d",RESET);
  713. }
  714. else if (strstr(device_status.mqttcfg1,"120.27.222.26")!=NULL) {
  715. HMI_WRITE("uploadSet.plt.val=%d",SET);
  716. }
  717. //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1);
  718. HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg1);
  719. //更新固件版本号
  720. HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver);
  721. //更新固件编译日期
  722. HMI_WRITE("debug.t4.txt=\"%s\"",device_status.driver_compile);
  723. //
  724. HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl);
  725. HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile);
  726. //更新dtu/rtu版本
  727. HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g);
  728. //更新时间
  729. HMI_WRITE("menu.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  730. HMI_WRITE("workSta.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  731. HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour);
  732. HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min);
  733. HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec);
  734. //更新电压
  735. HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
  736. //更新电流
  737. HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
  738. //更新温湿度数据
  739. HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity));
  740. HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture));
  741. //更新电池状态
  742. HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status);
  743. //更新上仓门状态
  744. HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status);
  745. //更新下仓门状态
  746. HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status);
  747. //更新工作状态
  748. HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status);
  749. //更新温控状态
  750. HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status);
  751. //更新雨控状态
  752. HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status);
  753. //更新光控状态
  754. HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status);
  755. //更新收集时间
  756. HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time);
  757. //更新加热时间
  758. HMI_WRITE("workSta.heatTime.val=%d",device_status.heattime);
  759. HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime);
  760. //
  761. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  762. //更新加热时间和温度
  763. HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem);
  764. HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue);
  765. //更新加热状态
  766. HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
  767. //更新密码
  768. HMI_WRITE("inputPasswd.pwdData.txt=\"8888\"");
  769. //更新工作进度
  770. //HMI_WRITE("menu.workProgress.val=%d",);
  771. //更新定时模式
  772. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  773. //更新光控定时时长
  774. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  775. //更新时控开始
  776. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  777. //更新时控结束
  778. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  779. //SIM卡状态
  780. HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim));
  781. //ID
  782. HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
  783. HMI_WRITE("menu.deviceId.txt=\"%s\"",device_status.deviceId);
  784. //HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
  785. //数据上传间隔
  786. HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim);
  787. //语音音量
  788. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  789. //更新熄屏时间
  790. if(device_status.thsp<60) device_status.thsp = device_status.thsp * 60;
  791. HMI_WRITE("thsp=%d",device_status.thsp);
  792. if(strstr(device_status.link_ip,".")!=RT_NULL){
  793. HMI_WRITE("menu.t0.txt=\"%s\"",device_status.link_ip);
  794. }
  795. #if MATCH_DEVICE == 1
  796. HMI_WRITE("menu.mode.val=%d",device_status.work_mode);
  797. HMI_WRITE("menu.upan.val=%d",device_status.upan_sta);
  798. HMI_WRITE("menu.alarm_tg.txt=\"%s\"",device_status.alarm_tg);
  799. //更新有线连接状态
  800. HMI_WRITE("menu.va_net.val=%d",0);
  801. #else
  802. HMI_WRITE("menu.va_net.val=%d",device_status.link_sta);
  803. #endif
  804. }
  805. HMI_WRITE("menu.cam_sta.val=%d",device_status.camsta);
  806. HMI_WRITE("menu.version.txt=\"%s\"",device_status.driver);
  807. if(atoi(device_status.gps)==1){
  808. HMI_WRITE("menu.gps_state.txt=\"定位成功:(GPS+BD)%d/%d\"",device_status.gps_inuse,device_status.gps_view);
  809. }else if(atoi(device_status.gps)==2){
  810. HMI_WRITE("menu.gps_state.txt=\"定位成功:(基站)\"");
  811. }
  812. //更新联网状态
  813. if(atoi(device_status.net)==1)
  814. {
  815. HMI_WRITE("menu.net.val=1");
  816. HMI_WRITE("workSta.net.val=1");
  817. }
  818. else {
  819. #if MATCH_DEVICE == 0
  820. HMI_WRITE("workSta.net.val=0");
  821. HMI_WRITE("menu.net.val=0");
  822. //更新CSQ
  823. HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
  824. HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
  825. HMI_WRITE("workSta.csq_value.txt=\"%s\"",device_status.csq);
  826. #else
  827. HMI_WRITE("workSta.net.val=1");
  828. HMI_WRITE("menu.net.val=1");
  829. HMI_WRITE("menu.va_csq.val=0");
  830. #endif
  831. }
  832. if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
  833. {
  834. HMI_WRITE("menu.net.val=1");
  835. HMI_WRITE("workSta.net.val=2");
  836. }
  837. /**********************************************************************/
  838. }
  839. /***************************** 解析从屏幕收到的数据 begin **********************************/
  840. /* ==================== 屏幕测试加热:定时自动关闭 ====================
  841. * 需求:屏幕测试页打开加热(putTest.switchHeat=1)后,10 分钟自动关闭,避免忘记关导致干烧。
  842. * 实现:一次性软定时器;只在"本次加热由屏幕测试打开"时生效——手动关闭(switchHeat=0)
  843. * 或定时到点都会撤销;若定时到点时设备正处于工作流程中(work_status==1),
  844. * 加热由工作循环(calorstat)托管,则本定时器不干预,仅撤销自己。
  845. * 维护提示:如需改自动关闭时长,只改 HMI_TEST_HEAT_TIMEOUT 一处。 */
  846. #define HMI_TEST_HEAT_TIMEOUT (10 * 60) /* 秒 */
  847. static rt_timer_t s_test_heat_timer = RT_NULL;
  848. static volatile rt_bool_t s_test_heat_on = RT_FALSE;
  849. static void hmi_test_heat_timeout(void *parameter)
  850. {
  851. if (!s_test_heat_on)
  852. {
  853. return;
  854. }
  855. s_test_heat_on = RT_FALSE;
  856. if (device_status.work_status == 1)
  857. {
  858. LOG_I("test heat timeout ignored (work in progress)");
  859. return;
  860. }
  861. ctr_Heat(OFF);
  862. device_status.heat_status = 0;
  863. HMI_WRITE("sysTest.switchHeat.val=0");
  864. HMI_WRITE("workSta.heatStatus.val=0");
  865. LOG_W("屏幕测试加热已自动关闭(超时 %d 秒)", HMI_TEST_HEAT_TIMEOUT);
  866. }
  867. static void hmi_test_heat_start(void)
  868. {
  869. if (s_test_heat_timer == RT_NULL)
  870. {
  871. s_test_heat_timer = rt_timer_create("t_testheat", hmi_test_heat_timeout, RT_NULL,
  872. HMI_TEST_HEAT_TIMEOUT * RT_TICK_PER_SECOND,
  873. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  874. if (s_test_heat_timer == RT_NULL)
  875. {
  876. LOG_E("rt_timer_create t_testheat failed!");
  877. return;
  878. }
  879. }
  880. s_test_heat_on = RT_TRUE;
  881. rt_timer_stop(s_test_heat_timer);
  882. rt_timer_start(s_test_heat_timer); /* 每次打开都重新计时 */
  883. }
  884. static void hmi_test_heat_stop(void)
  885. {
  886. s_test_heat_on = RT_FALSE;
  887. if (s_test_heat_timer != RT_NULL)
  888. {
  889. rt_timer_stop(s_test_heat_timer);
  890. }
  891. }
  892. /* ==================== workSta 工作过程刷新(屏幕 t1~t6 + 拍照/清扫状态) ====================
  893. * 数据来源:control.c 各控制流程实时写入 device_status.work_*、photo_left、clear_left;
  894. * hmi.c Ctr_Mortor_entry 写入 work_door_left / photo_left;progress 由
  895. * Work_Progress_Get() 按时间加权计算。
  896. * 下发条件:HMI_WORK_PROC_VT(vt) 当前为 (vt)!=6 —— 除吸虫塔外的页面都下发,
  897. * 孢子仪(dtype!=3)在前一行条件已被挡掉。 */
  898. #define HMI_WORK_PROC_VT(vt) ((vt) != 6) /* 需要显示工作过程的版本类型 */
  899. /* t1~t6 数值朗读:为 0 时下发空字符串(屏幕该栏显示空白),非 0 下发数值;
  900. * percent=RT_TRUE 时数值后带 % 号(t4 进度显示形如 "45%")。
  901. * 实现要点:每个控件只保留**一个** HMI_WRITE 调用点(数值先格式化成字符串、统一走 %s)。
  902. * 因为 HMI_WRITE 的重复过滤按调用点记录"上次内容"——只有单调用点时,
  903. * "上次内容"才恒等于"屏幕当前显示"。若拆成 空串/数值 两个调用点,两个缓存互相独立:
  904. * 从"有值(如 1)"切回 0 时会命中空串调用点的旧缓存而被跳过,屏幕会残留上一个数字
  905. * (要等到过滤表 10s 周期复位才纠正)。 */
  906. static void hmi_write_num_or_blank(char *fmt, uint16_t v, rt_bool_t percent)
  907. {
  908. char num[8];
  909. if (v == 0)
  910. {
  911. num[0] = '\0'; /* 0 → 空串,屏幕显示空白 */
  912. }
  913. else if (percent)
  914. {
  915. rt_snprintf(num, sizeof(num), "%u%%", v);
  916. }
  917. else
  918. {
  919. rt_snprintf(num, sizeof(num), "%u", v);
  920. }
  921. HMI_WRITE(fmt, num);
  922. }
  923. static void hmi_refresh_work_proc(void)
  924. {
  925. if(device_status.dtype != 3)
  926. {
  927. return;
  928. }
  929. /* 拍照状态:device_status.photo_ws(0-停止 1-拍照中) */
  930. HMI_WRITE("workSta.photoSta.val=%d", (device_status.photo_ws != 0) ? 1 : 0);
  931. /* 清扫状态:device_status.clear_status(0-停止 1-清扫中,2-转仓动作) */
  932. HMI_WRITE("workSta.clearSta.val=%d", (device_status.clear_status != 0) ? 1 : 0);
  933. /* t1~t6:工作过程实时数据(文本控件;数值为 0 时下发空串,该栏显示空白) */
  934. hmi_write_num_or_blank("workSta.t1.txt=\"%s\"", device_status.work_heat_left, RT_FALSE); /* 剩余加热时间(s) */
  935. hmi_write_num_or_blank("workSta.t2.txt=\"%s\"", device_status.work_clt_left, RT_FALSE); /* 收集倒计时(s) */
  936. hmi_write_num_or_blank("workSta.t3.txt=\"%s\"", device_status.work_door_left, RT_FALSE); /* 仓门开/关倒计时(s) */
  937. hmi_write_num_or_blank("workSta.t4.txt=\"%s\"", Work_Progress_Get(), RT_TRUE); /* 工作进度(%) */
  938. hmi_write_num_or_blank("workSta.t5.txt=\"%s\"", device_status.photo_left, RT_FALSE); /* 拍照结束剩余(s) */
  939. hmi_write_num_or_blank("workSta.t6.txt=\"%s\"", device_status.clear_left, RT_FALSE); /* 清扫结束剩余(s) */
  940. }
  941. /* ==================== 屏幕命令严格匹配(2026-10-08 修复) ====================
  942. * 屏幕命令以 \r 结尾, 但串口按包分段: 一条 "SwversionV43" 可能被拆成
  943. * "SwversionV4" + "3" 两段先后到达。原实现用 strstr() 前缀包含匹配,
  944. * 前段 "SwversionV4" 会误命中"水稻"分支, 把 version_type 错误切换(并误报 RTU、
  945. * 重复写 Flash、随后完整帧又切回, 配置来回翻转)。
  946. * 本函数要求: 命令字完整相等, 且其后紧跟 \r(完整帧结束符, 兼容 \r\n);
  947. * 分包残段没有 \r, 会被拒绝, 等屏幕后续完整帧(心跳重发)正常处理。
  948. * 用法: 替换存在"短命令是长命令前缀"关系的分支(SwversionV1/V4)。 */
  949. static rt_bool_t hmi_cmd_match(const char *buf, const char *cmd)
  950. {
  951. size_t n = rt_strlen(cmd);
  952. if (rt_strncmp(buf, cmd, n) != 0)
  953. {
  954. return RT_FALSE;
  955. }
  956. return (buf[n] == '\r') ? RT_TRUE : RT_FALSE;
  957. }
  958. /* ==================== 屏幕 debug1 调试页(2026-10-13 新增) ====================
  959. * 对应陶晶驰(USART HMI)工程中的调试页 debug1,功能与协议如下:
  960. * ① 信息区(t0 RT-Thread版本 / t1 内核 / t2 BSP型号 / t13 运行时长 / t5 BL版本 /
  961. * t3 固件版本 / t4 编译时间 / t11+j0 CPU占用 / t12+j1 RAM占用 / t10 外挂Flash):
  962. * 屏幕侧在该页定时器(或进入页面)下发 "getDebugData",固件整批刷新(HMI_WRITE 带重复过滤,
  963. * 未变化的项不会重复占用串口带宽);
  964. * ② 按钮区 b0~b9(屏幕弹起事件下发文本命令,以 "\r\n" 结束):
  965. * reboot 重启(沿用既有分支 rt_hw_cpu_reset)
  966. * bootloader 启动BL:置 stop_in_bootloader 并重启进入 BootLoader(沿用既有分支)
  967. * startapp 启动APP:清除 stop_in_bootloader 并重启(下次直接启动 APP)
  968. * dbg_on 打开调试:把 RT-Thread 控制台输出镜像到 debug1 底部文本框
  969. * dbg_off 关闭调试:恢复原控制台设备
  970. * dbg_clr 清空固件侧控制台缓存与屏幕文本框
  971. * dbg_erase 擦除参数:恢复出厂默认参数(按供电类型走交流/电池两套默认值)
  972. * dbg_restore 恢复固件:置 restore_pending + stop_in_bootloader 并重启进 BL,
  973. * 由 BL 执行 df_area→app 恢复(BL 侧支持见交付说明;未支持时不会误动作)
  974. * dbg:<命令> 把 <命令> 作为 msh 命令在本机执行,输出回传文本框(b4 发送按钮)
  975. * ③ 调试控制台实现:注册只写"捕获设备" dbgcap,打开调试时 rt_console_set_device("dbgcap"),
  976. * 此后 rt_kprintf / finsh / msh 的输出全部经其 write 回调缓存并推送屏幕文本框
  977. * (换行按 TJC 官方约定用 "\r" 两字符转义下发,不能用 \n);同时经"转发设备"
  978. * DBG1_CHAIN_DEV(默认 uart1) 原样转发,保证本机调试口日志不停流。命令执行放在
  979. * 独立线程 dbg_exec_thread,避免 top 之类的长命令把屏幕刷新线程卡死。
  980. * 注意:转发设备只 find 句柄、不自行 open/close。(本工程 apm32f1 用 serial_v2:
  981. * open 已打开设备直接返回、标志位只增不覆盖,不存在 v1 的接收位被洗问题,
  982. * 故未移植 dbg_chain_fixup / hmi_uart4_fixup_rx)
  983. * ④ 屏幕文本框收发约定("单框收发"设计,与截图的底部 msh>> 框一致):
  984. * - 固件每次下发的内容尾部都会附加一行提示符 "msh >> "(宏 DBG1_PROMPT);
  985. * - 用户点框→键盘输入→按[发送](b4);b4 弹起事件发送:printh 64 62 67 3A("dbg:")
  986. * + prints "debug1.<框名>.txt",0 + printh 0d 0a;
  987. * - 固件收到 "dbg:"+整个框内容后,取**最后一个 ">>" 之后**的文本作为命令执行
  988. * (无 ">>" 时按纯命令处理,兼容"独立输入框"的接法),执行结果回填框内。
  989. * ⑤ 推送节流与页面联动:输出推送带最小间隔节流;且仅在"该页有心跳"(最近
  990. * DBG1_PAGE_ALIVE_TICK 内收到过 getDebugData)时才推送——离开调试页后自动停推,
  991. * 返回调试页后自动恢复(缓存不丢,回页即见最新内容)。
  992. * 注意:打开调试期间本机 UART1 调试口日志经转发保留;控制台设备在"关闭调试"后恢复原样。
  993. * 维护提示:屏幕控件名若变化,仅需改 DBG1_CON_OBJ 宏及下方各 HMI_WRITE 中的控件名。 */
  994. #define DBG1_CON_OBJ "msh" /* 底部调试文本框对象名(改名只需改这一处) */
  995. #define DBG1_PROMPT "msh >> " /* 文本框尾部提示符(兼作命令提取锚点) */
  996. #define DBG1_CHAIN_DEV "uart1" /* 日志转发设备(本机调试口);置 "" 可关闭转发 */
  997. #define DBG1_CAP_DEV_NAME "dbgcap" /* 控制台捕获设备名(仅固件内部使用) */
  998. #define DBG1_CON_RAW_MAX 200 /* 行缓存上限(约文本框一屏可见字符数,可调) */
  999. #define DBG1_CON_ESC_MAX 220 /* 单次下发转义后上限(受 HMI_WRITE 内部 256B 缓冲限制) */
  1000. #define DBG1_CON_PUSH_MAX 120 /* 单次下发取缓存尾部长度(转义前)。受"b4 发送会回传整框"
  1001. * 约束:中文按 UTF-8 3B/字,120 字符最坏 360B,
  1002. * 加提示符与用户输入仍小于单帧上限 HMI_FRAME_MAX(512) */
  1003. #define DBG1_CMD_MAX 100 /* 屏幕控制台单条命令最大长度 */
  1004. #define DBG1_EXEC_PRIO 22 /* 调试命令执行线程优先级(低于屏幕线程) */
  1005. #define DBG1_EXEC_STACK_SIZE 3072 /* 调试命令执行线程栈 */
  1006. #define DBG1_FLUSH_MIN_TICK 30 /* 输出推送最小间隔(tick,10ms/tick;纯节流不丢行) */
  1007. #define DBG1_PAGE_ALIVE_TICK 500 /* 调试页心跳存活窗口(5s):超过则暂停推送,离开页面不再占带宽 */
  1008. #define DBG1_BSP_NAME "APM32F103RET6" /* t2 BSP型号显示(按实际板卡) */
  1009. #define DBG1_CORE_NAME "Cortex-M3" /* t1 内核显示(如需改成内核版本号改这里) */
  1010. #define DBG1_PART_OBJ "t14" /* 分区表文本框对象名(屏幕侧新增控件;改名只改这一处) */
  1011. #define DBG1_PART_MAX 200 /* 分区表下发字符串上限(单条 HMI_WRITE 上限约 250B,留余量) */
  1012. static char dbg_con_raw[DBG1_CON_RAW_MAX + 2]; /* 控制台行缓存(屏幕文本框内容源) */
  1013. static rt_uint16_t dbg_con_len = 0;
  1014. static rt_bool_t dbg_mirror_on = RT_FALSE; /* 控制台是否已镜像到屏幕(打开调试) */
  1015. static char dbg_saved_console[RT_NAME_MAX + 1]; /* 打开调试前的控制台设备名 */
  1016. static struct rt_device dbg_con_dev; /* 只写捕获设备(dbgcap) */
  1017. static rt_bool_t dbg_con_dev_ready = RT_FALSE;
  1018. static rt_device_t dbg_chain_dev = RT_NULL; /* 日志转发设备句柄(只 find,不持有引用;见 dbg_chain_fixup) */
  1019. static volatile rt_tick_t s_dbg1_page_tick = 0; /* 最近一次收到 getDebugData 的时刻(0=从未) */
  1020. static struct rt_thread dbg_exec_thread;
  1021. static rt_uint8_t dbg_exec_stack[DBG1_EXEC_STACK_SIZE];
  1022. static struct rt_semaphore dbg_exec_sem;
  1023. static char dbg_cmd_buf[DBG1_CMD_MAX + 1];
  1024. static volatile rt_uint8_t dbg_exec_busy = 0;
  1025. static rt_bool_t dbg_exec_inited = RT_FALSE;
  1026. static rt_bool_t dbg_exec_ready = RT_FALSE;
  1027. /* ---------- 控制台行缓存与屏幕推送 ---------- */
  1028. static void dbg_con_append(const char *s, rt_uint16_t n)
  1029. {
  1030. if (n == 0)
  1031. {
  1032. return;
  1033. }
  1034. /* 缓存满:丢弃最旧内容(优先从行边界截断),保留最新输出 */
  1035. if ((rt_uint32_t)dbg_con_len + n > DBG1_CON_RAW_MAX)
  1036. {
  1037. rt_uint16_t cut = (dbg_con_len > (DBG1_CON_RAW_MAX / 3)) ? (dbg_con_len - DBG1_CON_RAW_MAX / 3) : 0;
  1038. while (cut < dbg_con_len && dbg_con_raw[cut] != '\n')
  1039. {
  1040. cut++;
  1041. }
  1042. cut = (cut < dbg_con_len) ? (cut + 1) : dbg_con_len;
  1043. if (cut > 0)
  1044. {
  1045. rt_memmove(dbg_con_raw, dbg_con_raw + cut, dbg_con_len - cut);
  1046. dbg_con_len -= cut;
  1047. }
  1048. }
  1049. if (n > DBG1_CON_RAW_MAX - dbg_con_len) /* 单次超长:只保留尾部 */
  1050. {
  1051. s += (n - (DBG1_CON_RAW_MAX - dbg_con_len));
  1052. n = DBG1_CON_RAW_MAX - dbg_con_len;
  1053. }
  1054. rt_memcpy(dbg_con_raw + dbg_con_len, s, n);
  1055. dbg_con_len += n;
  1056. dbg_con_raw[dbg_con_len] = '\0';
  1057. }
  1058. static void dbg_con_puts(const char *s)
  1059. {
  1060. dbg_con_append(s, (rt_uint16_t)rt_strlen(s));
  1061. }
  1062. /* 把行缓存转义后推送到屏幕文本框。
  1063. * TJC 字符串转义(官方 wiki 约定):换行 -> "\r"(\ 和 r 两个字符,不要用 \n);双引号 -> \"。
  1064. * 下发内容 = [缓存尾部] + 换行 + 提示符 DBG1_PROMPT —— 提示符固定收尾,用户直接在框尾
  1065. * 输入命令后按[发送],固件即可按"最后一个 >> 之后"提取命令(见 dbg_exec_submit)。
  1066. * force=RT_FALSE 时按 DBG1_FLUSH_MIN_TICK 做最小间隔节流(只影响推送时机,不丢内容);
  1067. * 页面心跳消失(离开调试页)超过 DBG1_PAGE_ALIVE_TICK 时暂存不推,回页后自动恢复。 */
  1068. static void dbg_con_flush(rt_bool_t force)
  1069. {
  1070. static rt_tick_t s_last_flush = 0;
  1071. char esc[DBG1_CON_ESC_MAX + 1];
  1072. rt_uint16_t i, n = 0, raw_len = dbg_con_len;
  1073. const char *p = dbg_con_raw;
  1074. if (raw_len == 0 || rt_interrupt_get_nest() != 0) /* 中断上下文不推送,等下次 */
  1075. {
  1076. return;
  1077. }
  1078. /* 页面联动:从未收到心跳或心跳已停(离开调试页)时暂不推送 */
  1079. if (s_dbg1_page_tick == 0 ||
  1080. (rt_tick_get() - s_dbg1_page_tick) > DBG1_PAGE_ALIVE_TICK)
  1081. {
  1082. return;
  1083. }
  1084. if (force == RT_FALSE && (rt_tick_get() - s_last_flush) < DBG1_FLUSH_MIN_TICK)
  1085. {
  1086. return;
  1087. }
  1088. s_last_flush = rt_tick_get();
  1089. if (raw_len > DBG1_CON_PUSH_MAX) /* 超长只保留尾部(屏幕一屏可见有限) */
  1090. {
  1091. p += (raw_len - DBG1_CON_PUSH_MAX);
  1092. raw_len = DBG1_CON_PUSH_MAX;
  1093. }
  1094. for (i = 0; i < raw_len && n < DBG1_CON_ESC_MAX - 16; i++)
  1095. {
  1096. char c = p[i];
  1097. if (c == '\n')
  1098. {
  1099. if (n < DBG1_CON_ESC_MAX - 16) { esc[n++] = '\\'; esc[n++] = 'r'; }
  1100. }
  1101. else if (c == '"')
  1102. {
  1103. if (n < DBG1_CON_ESC_MAX - 16) { esc[n++] = '\\'; esc[n++] = '"'; }
  1104. }
  1105. else
  1106. {
  1107. esc[n++] = c;
  1108. }
  1109. }
  1110. if (n > 0 && esc[n - 1] == '\\') /* 截断落在转义符中间时丢弃半个转义 */
  1111. {
  1112. n--;
  1113. }
  1114. /* 追加换行 + 提示符(预留空间已在循环上限扣除) */
  1115. esc[n++] = '\\';
  1116. esc[n++] = 'r';
  1117. for (i = 0; DBG1_PROMPT[i] != '\0' && n < DBG1_CON_ESC_MAX; i++)
  1118. {
  1119. esc[n++] = DBG1_PROMPT[i];
  1120. }
  1121. esc[n] = '\0';
  1122. HMI_WRITE("debug1." DBG1_CON_OBJ ".txt=\"%s\"", esc);
  1123. }
  1124. /* ---------- 控制台捕获设备(只写,替代 uart1 作为 rt_console_set_device 目标) ---------- */
  1125. static rt_err_t dbg_con_dev_open(rt_device_t dev, rt_uint16_t oflag)
  1126. {
  1127. (void)dev; (void)oflag;
  1128. return RT_EOK;
  1129. }
  1130. static rt_err_t dbg_con_dev_close(rt_device_t dev)
  1131. {
  1132. (void)dev;
  1133. return RT_EOK;
  1134. }
  1135. static rt_size_t dbg_con_dev_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  1136. {
  1137. (void)dev; (void)pos; (void)buffer; (void)size;
  1138. return 0;
  1139. }
  1140. /* 捕获 rt_kprintf/finsh/msh 的全部输出:① 转发到 DBG1_CHAIN_DEV(本机调试口,保持日志不停流);
  1141. * ② 过滤 ANSI 转义与编辑控制字符后按行入缓存,行尾或节流点推送屏幕(页面心跳存在时)。 */
  1142. static rt_size_t dbg_con_dev_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  1143. {
  1144. static rt_uint8_t s_esc = 0; /* ANSI 转义过滤状态机:0-普通 1-已见ESC 2-ESC[ 序列 */
  1145. const char *p = (const char *)buffer;
  1146. rt_size_t i;
  1147. (void)dev; (void)pos;
  1148. /* ① 本机调试口转发(best effort:设备未打开则静默跳过) */
  1149. if (dbg_chain_dev != RT_NULL)
  1150. {
  1151. rt_device_write(dbg_chain_dev, 0, buffer, size);
  1152. }
  1153. for (i = 0; i < size; i++)
  1154. {
  1155. char c = p[i];
  1156. if (s_esc == 0)
  1157. {
  1158. if ((rt_uint8_t)c == 0x1B) { s_esc = 1; continue; }
  1159. }
  1160. else if (s_esc == 1)
  1161. {
  1162. s_esc = (c == '[') ? 2 : 0;
  1163. continue;
  1164. }
  1165. else
  1166. {
  1167. if ((rt_uint8_t)c >= 0x40 && (rt_uint8_t)c <= 0x7E) s_esc = 0;
  1168. continue;
  1169. }
  1170. if (c == '\n')
  1171. {
  1172. dbg_con_append("\n", 1);
  1173. if (dbg_mirror_on || dbg_exec_busy) dbg_con_flush(RT_FALSE);
  1174. }
  1175. else if (c == '\t')
  1176. {
  1177. dbg_con_append(" ", 1);
  1178. }
  1179. else if (c == '\r' || (rt_uint8_t)c < 0x20 || (rt_uint8_t)c == 0x7F)
  1180. {
  1181. /* \r 与其它控制字符(含 finsh 编辑用的 \b)不显示 */
  1182. }
  1183. else
  1184. {
  1185. dbg_con_append(&c, 1);
  1186. }
  1187. }
  1188. return size;
  1189. }
  1190. static void dbg_con_dev_init(void)
  1191. {
  1192. if (dbg_con_dev_ready)
  1193. {
  1194. return;
  1195. }
  1196. rt_memset(&dbg_con_dev, 0, sizeof(dbg_con_dev));
  1197. dbg_con_dev.type = RT_Device_Class_Char;
  1198. dbg_con_dev.rx_indicate = RT_NULL;
  1199. dbg_con_dev.tx_complete = RT_NULL;
  1200. dbg_con_dev.init = RT_NULL;
  1201. dbg_con_dev.open = dbg_con_dev_open;
  1202. dbg_con_dev.close = dbg_con_dev_close;
  1203. dbg_con_dev.read = dbg_con_dev_read;
  1204. dbg_con_dev.write = dbg_con_dev_write;
  1205. dbg_con_dev.control = RT_NULL;
  1206. if (rt_device_register(&dbg_con_dev, DBG1_CAP_DEV_NAME, RT_DEVICE_FLAG_STREAM) == RT_EOK)
  1207. {
  1208. dbg_con_dev_ready = RT_TRUE;
  1209. }
  1210. }
  1211. /* ---------- (apm32f1) 本机口/屏幕口"接收位保护":无需移植 ----------
  1212. * master(v1 serial) 的 dbg_chain_fixup()/hmi_uart4_fixup_rx() 用于修复
  1213. * "rt_serial_open 用 open_flag=oflag&0xff 覆盖标志位、洗掉 INT_RX/DMA_RX 导致
  1214. * 无法输入"的问题。本工程用 serial_v2 驱动:rt_serial_open 对已打开设备直接返回、
  1215. * 标志位一律 |= 不覆盖(serial_v2.c),不存在该缺陷,故整段未移植。 */
  1216. static void dbg_console_save(void)
  1217. {
  1218. rt_device_t cur = rt_console_get_device();
  1219. /* 已处于捕获设备(如执行线程临时切换期间被并发调用)时不覆盖记录,
  1220. * 否则"关闭调试"会恢复到 dbgcap 上,永远回不到真实控制台 */
  1221. if (cur != RT_NULL && rt_strcmp(cur->parent.name, DBG1_CAP_DEV_NAME) == 0)
  1222. {
  1223. return;
  1224. }
  1225. if (cur != RT_NULL) rt_strncpy(dbg_saved_console, cur->parent.name, RT_NAME_MAX);
  1226. else rt_strncpy(dbg_saved_console, "uart1", RT_NAME_MAX);
  1227. dbg_saved_console[RT_NAME_MAX] = '\0';
  1228. }
  1229. static void dbg_console_restore(void)
  1230. {
  1231. if (dbg_saved_console[0] != '\0') rt_console_set_device(dbg_saved_console);
  1232. else rt_console_set_device("uart1");
  1233. }
  1234. /* ---------- 屏幕控制台:命令执行(独立线程,防长命令卡死屏幕线程) ---------- */
  1235. static rt_bool_t dbg_cmd_is_blocking(const char *cmd)
  1236. {
  1237. while (*cmd == ' ' || *cmd == '\t')
  1238. {
  1239. cmd++;
  1240. }
  1241. /* top/Msh_DisplaySysMonitor 为持续刷新命令,在屏幕控制台里无法按键退出,直接拒绝 */
  1242. if (rt_strncmp(cmd, "top", 3) == 0 && (cmd[3] == '\0' || cmd[3] == ' '))
  1243. {
  1244. return RT_TRUE;
  1245. }
  1246. if (rt_strncmp(cmd, "Msh_DisplaySysMonitor", 21) == 0)
  1247. {
  1248. return RT_TRUE;
  1249. }
  1250. return RT_FALSE;
  1251. }
  1252. static void dbg_exec_run(void)
  1253. {
  1254. rt_bool_t switched = RT_FALSE;
  1255. char cmd[DBG1_CMD_MAX + 1];
  1256. rt_memcpy(cmd, dbg_cmd_buf, DBG1_CMD_MAX + 1);
  1257. cmd[DBG1_CMD_MAX] = '\0';
  1258. if (dbg_cmd_is_blocking(cmd))
  1259. {
  1260. dbg_con_puts("(该命令为持续刷新命令,不支持屏幕控制台执行)\n");
  1261. dbg_con_flush(RT_TRUE);
  1262. return;
  1263. }
  1264. /* 未开镜像时临时把控制台切到捕获设备,保证 msh 输出可被捕获 */
  1265. if (dbg_mirror_on == RT_FALSE)
  1266. {
  1267. dbg_con_dev_init();
  1268. if (rt_device_find(DBG1_CAP_DEV_NAME) == RT_NULL)
  1269. {
  1270. dbg_con_puts("(捕获设备未就绪,命令未执行)\n");
  1271. dbg_con_flush(RT_TRUE);
  1272. return;
  1273. }
  1274. dbg_console_save();
  1275. rt_console_set_device(DBG1_CAP_DEV_NAME); /* 转发只需设备引用,无需自行 open */
  1276. switched = RT_TRUE;
  1277. }
  1278. dbg_con_puts("msh> ");
  1279. dbg_con_puts(cmd);
  1280. dbg_con_append("\n", 1);
  1281. dbg_con_flush(RT_FALSE);
  1282. msh_exec(cmd, rt_strlen(cmd)); /* 输出经控制台进入捕获设备 -> 行缓存 -> 屏幕 */
  1283. if (switched)
  1284. {
  1285. dbg_console_restore(); /* 恢复控制台;其内部 open 会洗掉接收位 */
  1286. }
  1287. dbg_con_flush(RT_TRUE);
  1288. }
  1289. static void dbg_exec_thread_entry(void *parameter)
  1290. {
  1291. (void)parameter;
  1292. while (1)
  1293. {
  1294. if (rt_sem_take(&dbg_exec_sem, RT_WAITING_FOREVER) != RT_EOK)
  1295. {
  1296. continue;
  1297. }
  1298. dbg_exec_busy = 1;
  1299. dbg_exec_run();
  1300. dbg_exec_busy = 0;
  1301. }
  1302. }
  1303. static void dbg_exec_init(void)
  1304. {
  1305. if (dbg_exec_inited)
  1306. {
  1307. return;
  1308. }
  1309. dbg_exec_inited = RT_TRUE;
  1310. rt_sem_init(&dbg_exec_sem, "dbgsem", 0, RT_IPC_FLAG_FIFO);
  1311. if (rt_thread_init(&dbg_exec_thread, "dbgexec", dbg_exec_thread_entry, RT_NULL,
  1312. dbg_exec_stack, sizeof(dbg_exec_stack), DBG1_EXEC_PRIO, 10) == RT_EOK)
  1313. {
  1314. rt_thread_startup(&dbg_exec_thread);
  1315. dbg_exec_ready = RT_TRUE;
  1316. }
  1317. }
  1318. /* 从 "dbg:" 之后的载荷中提取命令并提交执行线程。
  1319. * 载荷 = 屏幕文本框的整个内容("单框收发"设计),提取规则:
  1320. * 取**最后一个 ">>"** 之后的文本作为命令(框尾固定有提示符 "msh >> ",用户在其后输入);
  1321. * 载荷中不含 ">>" 时按"纯命令"处理(兼容另接独立命令输入框的用法);
  1322. * 再裁剪:跳过前导空白、截至行尾/上限、去尾部空白。 */
  1323. static void dbg_exec_submit(const char *payload)
  1324. {
  1325. const char *cmd = payload;
  1326. const char *p = payload;
  1327. const char *last = RT_NULL;
  1328. rt_uint16_t n = 0;
  1329. dbg_exec_init();
  1330. while ((p = rt_strstr(p, ">>")) != RT_NULL) /* 定位最后一个 ">>" */
  1331. {
  1332. last = p;
  1333. p += 2;
  1334. }
  1335. if (last != RT_NULL)
  1336. {
  1337. cmd = last + 2;
  1338. }
  1339. while (*cmd == ' ' || *cmd == '\t') /* 跳过提示符后的空白 */
  1340. {
  1341. cmd++;
  1342. }
  1343. while (cmd[n] != '\0' && cmd[n] != '\r' && cmd[n] != '\n' && n < DBG1_CMD_MAX)
  1344. {
  1345. dbg_cmd_buf[n] = cmd[n];
  1346. n++;
  1347. }
  1348. while (n > 0 && (dbg_cmd_buf[n - 1] == ' ' || dbg_cmd_buf[n - 1] == '\t'))
  1349. {
  1350. n--;
  1351. }
  1352. dbg_cmd_buf[n] = '\0';
  1353. if (n == 0)
  1354. {
  1355. return; /* 空命令(只发了提示符)不处理 */
  1356. }
  1357. if (dbg_exec_ready == RT_FALSE)
  1358. {
  1359. dbg_con_puts("(调试执行线程创建失败)\n");
  1360. dbg_con_flush(RT_TRUE);
  1361. return;
  1362. }
  1363. if (dbg_exec_busy)
  1364. {
  1365. dbg_con_puts("(上一条命令仍在执行,请稍候)\n");
  1366. dbg_con_flush(RT_TRUE);
  1367. return;
  1368. }
  1369. rt_sem_release(&dbg_exec_sem);
  1370. }
  1371. /* ---------- debug1 按钮处理 ---------- */
  1372. static void dbg_console_on(void)
  1373. {
  1374. dbg_con_dev_init();
  1375. if (rt_device_find(DBG1_CAP_DEV_NAME) == RT_NULL)
  1376. {
  1377. dbg_con_puts("(捕获设备注册失败,无法打开屏幕调试)\n");
  1378. dbg_con_flush(RT_TRUE);
  1379. return;
  1380. }
  1381. if (dbg_mirror_on == RT_FALSE)
  1382. {
  1383. dbg_console_save(); /* 记录当前控制台(uart1) */
  1384. rt_console_set_device(DBG1_CAP_DEV_NAME); /* 输出切到捕获设备;日志仍转发本机口 */
  1385. dbg_mirror_on = RT_TRUE;
  1386. }
  1387. dbg_con_puts("--- 屏幕调试已打开:日志同步到本框;在提示符后输入命令按[发送] ---\n");
  1388. dbg_con_flush(RT_TRUE);
  1389. }
  1390. static void dbg_console_off(void)
  1391. {
  1392. if (dbg_mirror_on)
  1393. {
  1394. dbg_console_restore(); /* 恢复控制台:内部 open 会洗掉接收模式位 */
  1395. dbg_mirror_on = RT_FALSE;
  1396. }
  1397. dbg_con_puts("--- 屏幕调试已关闭(控制台已恢复) ---\n");
  1398. dbg_con_flush(RT_TRUE);
  1399. }
  1400. static void dbg_con_clear(void)
  1401. {
  1402. dbg_con_len = 0;
  1403. dbg_con_raw[0] = '\0';
  1404. /* 复位重复过滤表:否则本调用点缓存仍是上次发出的内容,
  1405. * 二次点击"清空"(期间文本框已被日志刷新)会被去重逻辑跳过 */
  1406. hmi_dedupe_reset();
  1407. HMI_WRITE("debug1." DBG1_CON_OBJ ".txt=\"" DBG1_PROMPT "\""); /* 清空后仅保留提示符便于继续输入 */
  1408. }
  1409. static void dbg_erase_params(void)
  1410. {
  1411. LOG_I("debug1: 擦除参数(恢复出厂默认)");
  1412. if (device_status.Power_Type == 1) ResetParamDefault_DC(); /* 电池供电默认值 */
  1413. else ResetParamDefault(); /* 交流供电默认值 */
  1414. dbg_con_puts("--- 参数已恢复出厂默认值 ---\n");
  1415. dbg_con_flush(RT_TRUE);
  1416. }
  1417. static void dbg_start_app(void)
  1418. {
  1419. LOG_I("debug1: 启动APP(清除BL停留标志), 重启");
  1420. device_status.stop_in_bootloader = 0;
  1421. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader,
  1422. sizeof(device_status.stop_in_bootloader));
  1423. rt_thread_mdelay(200);
  1424. rt_hw_cpu_reset();
  1425. }
  1426. static void dbg_restore_fw(void)
  1427. {
  1428. uint8_t pending = 1;
  1429. LOG_I("debug1: 恢复固件(置标志并重启进BL)");
  1430. /* APP 正在 app 分区运行,不能自擦运行分区;置标志重启进 BL 恢复。
  1431. * restore_pending:供 BL 读取(BL 支持后自动执行 df_area→app 恢复)。 */
  1432. device_status.stop_in_bootloader = 1;
  1433. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader,
  1434. sizeof(device_status.stop_in_bootloader));
  1435. ef_set_env_blob("restore_pending", &pending, sizeof(pending));
  1436. rt_thread_mdelay(200);
  1437. rt_hw_cpu_reset();
  1438. }
  1439. /* debug1 页指令分发:返回 RT_TRUE 表示指令属于 debug1 并已处理。
  1440. * 注意:调用点必须放在 HMI_Decode 的 if/else 链最前 —— "dbg:<命令>" 的
  1441. * 命令内容里可能包含 reboot/clear 等既有分支的关键字,不能让它们先匹配。
  1442. * 另:能收到本页按钮指令即证明用户停留在调试页,匹配成功时顺带刷新页面心跳
  1443. * (s_dbg1_page_tick),使按钮在未配 1 秒心跳定时器时也能独立工作。 */
  1444. static rt_bool_t hmi_debug1_handle(const char *frame)
  1445. {
  1446. if (rt_strncmp(frame, "dbg:", 4) == 0) /* b4 发送:执行控制台命令 */
  1447. {
  1448. s_dbg1_page_tick = rt_tick_get();
  1449. dbg_exec_submit(frame + 4);
  1450. return RT_TRUE;
  1451. }
  1452. if (strstr(frame, "dbg_on") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_console_on(); return RT_TRUE; }
  1453. if (strstr(frame, "dbg_off") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_console_off(); return RT_TRUE; }
  1454. if (strstr(frame, "dbg_clr") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_con_clear(); return RT_TRUE; }
  1455. if (strstr(frame, "dbg_erase") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_erase_params(); return RT_TRUE; }
  1456. if (strstr(frame, "dbg_restore") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_restore_fw(); return RT_TRUE; }
  1457. if (strstr(frame, "startapp") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_start_app(); return RT_TRUE; }
  1458. return RT_FALSE;
  1459. }
  1460. /* ---------- debug1 信息区整批刷新("getDebugData" 触发) ---------- */
  1461. /* JEDEC 厂商 ID → 名称(取值与 SFUD 内置 mf_table 一致,仅供显示用)。
  1462. * 2026-10-13 新增:芯片走 SFDP 识别路径时 SFUD 不填 chip.name(见 t10 注释),
  1463. * 用厂商名兜底可读性最好。 */
  1464. static const char *dbg1_flash_vendor(uint8_t mf_id)
  1465. {
  1466. static const struct { uint8_t id; const char *name; } tbl[] = {
  1467. {0x01, "Cypress"}, {0x04, "Fujitsu"}, {0x1C, "EON"}, {0x1F, "Atmel"},
  1468. {0x20, "Micron"}, {0x37, "AMIC"}, {0x62, "Sanyo"}, {0x89, "Intel"},
  1469. {0x8C, "ESMT"}, {0xA1, "Fudan"}, {0xAD, "Hyundai"}, {0xBF, "SST"},
  1470. {0xC2, "Micronix"}, {0xC8, "GigaDevice"}, {0xD5, "ISSI"}, {0xEF, "Winbond"},
  1471. };
  1472. rt_size_t i;
  1473. for (i = 0; i < sizeof(tbl) / sizeof(tbl[0]); i++)
  1474. {
  1475. if (tbl[i].id == mf_id)
  1476. {
  1477. return tbl[i].name;
  1478. }
  1479. }
  1480. return RT_NULL;
  1481. }
  1482. /* 组装 FAL 分区表文本(t14 下发用):每行 "分区名 所属Flash 起址K+大小K",
  1483. * 行间用 TJC 换行转义 "\r"(两个字符)。例:
  1484. * bl onchip_flash 0K+200K / app onchip_flash 200K+312K
  1485. * easyflash MYFLASH16 0K+3072K / fm_area ... / df_area ...
  1486. * 超出 cap 时按整行截断(宁少一行,不给屏幕发半行)。 */
  1487. static void dbg1_build_part_txt(char *out, rt_uint16_t cap)
  1488. {
  1489. size_t part_num = 0;
  1490. const struct fal_partition *tbl = fal_get_partition_table(&part_num);
  1491. rt_uint16_t n = 0;
  1492. size_t i;
  1493. out[0] = '\0';
  1494. if (tbl == RT_NULL || part_num == 0)
  1495. {
  1496. rt_snprintf(out, cap, "%s", "分区表不可用");
  1497. return;
  1498. }
  1499. for (i = 0; i < part_num; i++)
  1500. {
  1501. char line[56];
  1502. int ln = rt_snprintf(line, sizeof(line), "%s %s %luK+%luK",
  1503. tbl[i].name, tbl[i].flash_name,
  1504. (unsigned long)(tbl[i].offset / 1024),
  1505. (unsigned long)(tbl[i].len / 1024));
  1506. if (ln <= 0)
  1507. {
  1508. continue;
  1509. }
  1510. if (ln > (int)sizeof(line) - 1) /* rt_snprintf 返回"应有长度",先夹取再参与判界/拷贝 */
  1511. {
  1512. ln = (int)sizeof(line) - 1;
  1513. }
  1514. if ((rt_uint32_t)n + (rt_uint32_t)ln + 2 + 1 > cap) /* +2 换行 +1 结束符 */
  1515. {
  1516. break;
  1517. }
  1518. if (n > 0)
  1519. {
  1520. out[n++] = '\\';
  1521. out[n++] = 'r';
  1522. }
  1523. rt_memcpy(out + n, line, ln);
  1524. n += (rt_uint16_t)ln;
  1525. out[n] = '\0';
  1526. }
  1527. }
  1528. static void hmi_refresh_debug1(void)
  1529. {
  1530. char buf[48];
  1531. rt_uint32_t total = 0, used = 0, max_used = 0, sec, pct = 0;
  1532. sfud_flash_t nor;
  1533. sys_monitor_refresh(); /* CPU 利用率 / 运行时长(非阻塞,任意线程可用) */
  1534. /* t0 RT-Thread版本 / t1 内核 / t2 BSP型号 */
  1535. rt_snprintf(buf, sizeof(buf), "%d.%d.%d", (int)RT_VERSION, (int)RT_SUBVERSION, (int)RT_REVISION);
  1536. HMI_WRITE("debug1.t0.txt=\"%s\"", buf);
  1537. HMI_WRITE("debug1.t1.txt=\"%s\"", DBG1_CORE_NAME);
  1538. HMI_WRITE("debug1.t2.txt=\"%s\"", DBG1_BSP_NAME);
  1539. /* t13 运行时长:总小时:分:秒(如 72:05:11,小时持续累加、不跨天回绕)。
  1540. * 2026-10-13 修正:① 秒位原为累计秒(屏幕显示形如 2:03:7397),改为 sec%60;
  1541. * ② 时位去掉 %86400 掩码——去掉"天"字段后若保留掩码,24h 处会静默回绕、
  1542. * 长跑设备无法区分"已运行 2 天还是 26 天"。 */
  1543. sec = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
  1544. rt_snprintf(buf, sizeof(buf), "%lu:%02lu:%02lu",
  1545. (unsigned long)(sec / 3600),
  1546. (unsigned long)((sec % 3600) / 60),
  1547. (unsigned long)(sec % 60));
  1548. HMI_WRITE("debug1.t13.txt=\"%s\"", buf);
  1549. /* 版本区:t5 BL版本 / t3 固件版本 / t4 编译时间 */
  1550. HMI_WRITE("debug1.t5.txt=\"%s\"", device_status.driver_bl);
  1551. HMI_WRITE("debug1.t3.txt=\"%s\"", device_status.driver);
  1552. HMI_WRITE("debug1.t4.txt=\"%s\"", device_status.driver_compile);
  1553. /* CPU:t11 文本 + j0 进度条(0-100) */
  1554. HMI_WRITE("debug1.t11.txt=\"%d%%\"", system_status.cpu_usage);
  1555. HMI_WRITE("debug1.j0.val=%d", system_status.cpu_usage);
  1556. /* RAM:t12 文本 + j1 进度条(0-100) */
  1557. rt_memory_info(&total, &used, &max_used);
  1558. if (total > 0)
  1559. {
  1560. pct = (rt_uint32_t)used * 100 / total;
  1561. rt_snprintf(buf, sizeof(buf), "%lu%%", (unsigned long)pct);
  1562. HMI_WRITE("debug1.t12.txt=\"%s\"", buf);
  1563. HMI_WRITE("debug1.j1.val=%d", (int)pct);
  1564. }
  1565. else
  1566. {
  1567. HMI_WRITE("debug1.t12.txt=\"%s\"", "N/A");
  1568. HMI_WRITE("debug1.j1.val=%d", 0);
  1569. }
  1570. /* t10 外挂Flash(SFUD 探测结果)。
  1571. * 2026-10-13 修复:芯片支持 JESD216(SFDP) 时,SFUD 走 SFDP 识别路径并**显式置
  1572. * chip.name = NULL**(sfud.c:279,SFDP 不含型号名;实测本机 16MB 闪存即此路径),
  1573. * 原实现直接 %s 打印会输出 "(NULL)"。现按三级兜底保证内容有效:
  1574. * ① 型号表命中 → "W25Q128 16MB"(原样保留);
  1575. * ② 型号为空 → "Winbond EF4018 16MB"(厂商名 + JEDEC ID + 容量,信息全部实读自芯片);
  1576. * ③ 厂商也未知 → "JEDEC EF4018 16MB"。 */
  1577. nor = rt_sfud_flash_find_by_dev_name(NOR_FLASH_DEV_NAME);
  1578. if (nor != RT_NULL)
  1579. {
  1580. const char *vendor = dbg1_flash_vendor(nor->chip.mf_id);
  1581. unsigned cap_mb = (unsigned)(nor->chip.capacity / (1024 * 1024));
  1582. if (nor->chip.name != RT_NULL && nor->chip.name[0] != '\0')
  1583. {
  1584. rt_snprintf(buf, sizeof(buf), "%s %uMB", nor->chip.name, cap_mb);
  1585. }
  1586. else if (vendor != RT_NULL)
  1587. {
  1588. rt_snprintf(buf, sizeof(buf), "%s %02X%02X%02X %uMB", vendor,
  1589. (unsigned)nor->chip.mf_id, (unsigned)nor->chip.type_id,
  1590. (unsigned)nor->chip.capacity_id, cap_mb);
  1591. }
  1592. else
  1593. {
  1594. rt_snprintf(buf, sizeof(buf), "JEDEC %02X%02X%02X %uMB",
  1595. (unsigned)nor->chip.mf_id, (unsigned)nor->chip.type_id,
  1596. (unsigned)nor->chip.capacity_id, cap_mb);
  1597. }
  1598. }
  1599. else
  1600. {
  1601. rt_snprintf(buf, sizeof(buf), "未检测到");
  1602. }
  1603. HMI_WRITE("debug1.t10.txt=\"%s\"", buf);
  1604. /* t14 分区表详情(FAL 分区表:分区名 所属Flash 起址K+大小K,多行)。
  1605. * 内容静态,配合发送去重仅首次(及换页/10s 复位后)实际占用串口带宽。 */
  1606. {
  1607. char part[DBG1_PART_MAX + 2];
  1608. dbg1_build_part_txt(part, sizeof(part));
  1609. HMI_WRITE("debug1." DBG1_PART_OBJ ".txt=\"%s\"", part);
  1610. }
  1611. }
  1612. /* 屏幕版本握手串变化检测。
  1613. * 说明:屏幕心跳里 SwversionV** 可能与 getFullData 一样每秒都发,
  1614. * 若每次都复位重复过滤表会让去重完全失效;因此只在"版本串发生变化"时复位
  1615. * (换型号/换页面工程才会变)。屏幕重启但版本不变的情况由 HMI_DEDUPE_RESYNC_SEC
  1616. * 周期复位兜底(最坏 10s 后恢复全量刷新)。
  1617. * 2026-10-15:版本串变化还用于触发版本类型切换(hmi_switch_version),
  1618. * 见 HMI_Decode 的 SwversionV 分支。 */
  1619. static char s_last_hmi_ver[16] = {0};
  1620. static rt_bool_t hmi_ver_changed(const char *frame)
  1621. {
  1622. char cur[16];
  1623. int i = 0;
  1624. while (frame[i] != '\0' && frame[i] != '\r' && frame[i] != '\n' && i < (int)sizeof(cur) - 1)
  1625. {
  1626. cur[i] = frame[i];
  1627. i++;
  1628. }
  1629. cur[i] = '\0';
  1630. if (rt_strcmp(cur, s_last_hmi_ver) != 0)
  1631. {
  1632. rt_strncpy(s_last_hmi_ver, cur, sizeof(s_last_hmi_ver) - 1);
  1633. s_last_hmi_ver[sizeof(s_last_hmi_ver) - 1] = '\0';
  1634. LOG_I("hmi version frame changed: %s", s_last_hmi_ver);
  1635. return RT_TRUE;
  1636. }
  1637. return RT_FALSE;
  1638. }
  1639. /* ==================== 屏幕版本串 → 版本类型切换(2026-10-15) ====================
  1640. * 触发:HMI_Decode 的 SwversionV 分支中,hmi_ver_changed() 检测到屏幕版本握手串
  1641. * 发生变化时调用一次——即连接/更换不同版本的屏幕工程时,按串切换
  1642. * dtype/version_type/driver 并写 EF(MA_save/param_mirror_sync)。
  1643. * 历史:本逻辑原为 HMI_Decode 内一段 else-if 链(V12/V11/V1/V43/V42/V41/V4/V6/
  1644. * V30/V7),自 SwversionV 整页刷新分支加入后所有版本帧都被其先命中,
  1645. * 原链永远不可达(死代码);现迁移为本函数并改由版本串变化事件触发,
  1646. * 逐分支语义与原链一致(分包防误切仍用 hmi_cmd_match 严格匹配)。 */
  1647. static void hmi_switch_version(const char *frame)
  1648. {
  1649. //高空-带拍照
  1650. if (strstr(frame,"SwversionV12")!=NULL)
  1651. {
  1652. device_status.dtype = 3;
  1653. if(device_status.version_type != 12)
  1654. {
  1655. device_status.version_type = 12;
  1656. sprintf(device_status.driver, "%s",VERSION_GK);
  1657. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1658. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1659. MA_save();
  1660. param_mark_dirty();
  1661. }
  1662. else {
  1663. }
  1664. }
  1665. //高空
  1666. else if (strstr(frame,"SwversionV11")!=NULL)
  1667. {
  1668. device_status.dtype = 3;
  1669. if(device_status.version_type != 11)
  1670. {
  1671. device_status.version_type = 11;
  1672. sprintf(device_status.driver, "%s",VERSION_GK);
  1673. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1674. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1675. MA_save();
  1676. param_mark_dirty();
  1677. }
  1678. else {
  1679. }
  1680. return;
  1681. }
  1682. //1.0
  1683. /* 注意:必须用严格匹配——"SwversionV1" 是 "SwversionV11/12" 的前缀,
  1684. * 串口分包时残段会误切到本分支(同 V4 的问题,见 hmi_cmd_match 说明) */
  1685. else if (hmi_cmd_match(frame,"SwversionV1"))
  1686. {
  1687. if(device_status.version_type != 1)
  1688. {
  1689. device_status.dtype = 3;
  1690. device_status.version_type = 1;
  1691. sprintf(device_status.driver, "%s",VERSION_ZP);
  1692. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1693. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1694. MA_save();
  1695. param_mark_dirty();
  1696. }
  1697. else {
  1698. }
  1699. return;
  1700. }
  1701. //中端款
  1702. else if (strstr(frame,"SwversionV43")!=NULL) {
  1703. device_status.dtype = 3;
  1704. if(device_status.version_type != 43)
  1705. {
  1706. device_status.dtype = 3;
  1707. device_status.version_type = 43;
  1708. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1709. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1710. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1711. MA_save();
  1712. param_mark_dirty();
  1713. }
  1714. return;
  1715. }
  1716. //低端款
  1717. else if (strstr(frame,"SwversionV42")!=NULL) {
  1718. device_status.dtype = 3;
  1719. if(device_status.version_type != 42)
  1720. {
  1721. device_status.dtype = 3;
  1722. device_status.version_type = 42;
  1723. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1724. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1725. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1726. MA_save();
  1727. param_mark_dirty();
  1728. }
  1729. return;
  1730. }
  1731. //4.0
  1732. else if (strstr(frame,"SwversionV41")!=NULL) {
  1733. device_status.dtype = 3;
  1734. if(device_status.version_type != 41)
  1735. {
  1736. device_status.dtype = 3;
  1737. device_status.version_type = 41;
  1738. sprintf(device_status.driver, "%s",VERSION_HK);
  1739. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1740. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1741. DTU_Write("SwversionV41");
  1742. MA_save();
  1743. param_mark_dirty();
  1744. }
  1745. else {
  1746. }
  1747. return;
  1748. }
  1749. //水稻
  1750. /* 注意:必须用严格匹配——"SwversionV4" 是 "SwversionV41/42/43" 的前缀。
  1751. * 实测(2026-10-08):串口分包时 "SwversionV43" 的前段 "SwversionV4" 会误切到
  1752. * 本分支(version_type 43→4、误报 RTU、写 Flash),随后完整帧又切回 43,
  1753. * 造成配置来回翻转。见 hmi_cmd_match 说明。 */
  1754. else if (hmi_cmd_match(frame,"SwversionV4"))
  1755. {
  1756. device_status.dtype = 3;
  1757. if(device_status.version_type != 4)
  1758. {
  1759. device_status.version_type = 4;
  1760. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1761. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1762. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1763. MA_save();
  1764. param_mark_dirty();
  1765. }
  1766. else {
  1767. }
  1768. return;
  1769. }
  1770. else if((strstr(frame,"SwversionV7")!=NULL)) {
  1771. if(device_status.dtype!=7)
  1772. {
  1773. LOG_D("设置版本类型:7");
  1774. device_status.dtype = 7;
  1775. device_status.version_type = 7;
  1776. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //设备类型
  1777. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1778. DTU_Write("SwversionV7");
  1779. MA_save();
  1780. param_mark_dirty();
  1781. }
  1782. }
  1783. }
  1784. static void HMI_Decode(void)
  1785. {
  1786. time_t now = time(RT_NULL);
  1787. static struct tm tm;
  1788. gmtime_r(&now, &tm);
  1789. static char *json_temp;
  1790. static char *var;
  1791. /* 屏幕页刷新与版本握手统一入口(SwversionV* / getFullData 两类帧):
  1792. * ① SwversionV*:先检测并持久化版本类型(原独立 else-if 分支链并入本块,
  1793. * 保证"先更新版本、再刷新页面",刷新内容即时反映新版本);
  1794. * ② 版本串变化(换型号/换页面工程) → 复位重复过滤表,立即恢复全量刷新;
  1795. * ③ getFullData / SwversionV* 均触发整页刷新:60+ 条属性合并为 1~3 次
  1796. * 串口写(见 HMI_WRITE 发送优化);
  1797. * ④ 处理完直接 return(沿用原版本分支的语义),不再落入下方指令链。
  1798. * 注意:显式排除 "dbg:" 控制台发送帧——b4[发送]会回传整框文本,其中可能内嵌
  1799. * 历史日志里的版本串(如本机口镜像的 hmi rx 行),若不排除会抢在 hmi_debug1_handle
  1800. * 之前吞掉命令帧,导致命令不再执行。 */
  1801. if(strstr(rx_buffer,"SwversionV") != NULL || strstr(rx_buffer,"getFullData") != NULL)
  1802. {
  1803. /* 屏幕版本握手串变化(换型号/换页面工程) → 复位重复过滤表 + 按串切换版本类型,
  1804. * 并立即整页全量刷新 */
  1805. if (strstr(rx_buffer,"SwversionV") != NULL && hmi_ver_changed(rx_buffer))
  1806. {
  1807. hmi_dedupe_reset();
  1808. /* 2026-10-15:版本串变化后按字符设置版本类型(见 hmi_switch_version 说明) */
  1809. hmi_switch_version(rx_buffer);
  1810. }
  1811. /* 整页刷新 60+ 条属性合并为 1~3 次串口写(见 HMI_WRITE 发送优化) */
  1812. HMI_BATCH_BEGIN();
  1813. refresh_hmi_Component(tm);
  1814. hmi_refresh_work_proc(); /* 拍照/清扫状态 + t1~t6 工作过程显示 */
  1815. HMI_BATCH_END();
  1816. return; /* 握手/刷新帧处理完毕, 不再落入下方指令链 */
  1817. }
  1818. /* ---- 屏幕 debug1 调试页(心跳刷新 + 按钮/控制台指令)----
  1819. * 必须放在 if/else 链最前:dbg:<命令> 的内容里可能包含 reboot/clear 等
  1820. * 既有分支的关键字,让它们先匹配会导致误动作。 */
  1821. if (strstr(rx_buffer, "getDebugData") != RT_NULL)
  1822. {
  1823. s_dbg1_page_tick = rt_tick_get(); /* 调试页心跳:离开该页(超时)后暂停日志推送 */
  1824. HMI_BATCH_BEGIN();
  1825. hmi_refresh_debug1();
  1826. dbg_con_flush(RT_FALSE); /* 回页/持续在页:把最新控制台内容补推一次(带去重) */
  1827. HMI_BATCH_END();
  1828. }
  1829. else if (hmi_debug1_handle(rx_buffer))
  1830. {
  1831. /* debug1 指令已处理(dbg:/dbg_on/dbg_off/dbg_clr/dbg_erase/dbg_restore/startapp),
  1832. * 页面心跳已在处理函数内随之刷新。 */
  1833. }
  1834. else if (strstr(rx_buffer,"getClockData")!=NULL) {
  1835. //更新定时模式
  1836. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  1837. //更新光控定时时长
  1838. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  1839. //更新时控开始
  1840. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  1841. //更新时控结束
  1842. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  1843. }
  1844. else if (strstr(rx_buffer,"button")!=NULL) {
  1845. LOG_D("button!");
  1846. DTU_Write("button");
  1847. }
  1848. //拍照
  1849. else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) {
  1850. #if MATCH_DEVICE == 1
  1851. ctr_led(ON);
  1852. //发送拍照指令
  1853. char *takephotojson = makeJson_takephoto();
  1854. DTU_Write("%s",takephotojson);
  1855. rt_free(takephotojson);
  1856. HMI_WRITE("sysTest.switchPhoto.val=0");
  1857. #else
  1858. device_status.test_flag=1;
  1859. device_status.photo_ws = 1;
  1860. #endif
  1861. }
  1862. else if (strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
  1863. #if MATCH_DEVICE == 1
  1864. ctr_led(ON);
  1865. //发送拍照指令
  1866. char *takephotojson = makeJson_takephoto();
  1867. DTU_Write("%s",takephotojson);
  1868. rt_free(takephotojson);
  1869. HMI_WRITE("sysTest.switchPhoto.val=0");
  1870. #else
  1871. device_status.photo_ws = 1;
  1872. #endif
  1873. }
  1874. //打开补光
  1875. else if (strstr(rx_buffer,"putTest.switchLed=1")!=NULL) {
  1876. LOG_I("打开补光!!");
  1877. ctr_led(ON);
  1878. }
  1879. //关闭补光
  1880. else if (strstr(rx_buffer,"putTest.switchLed=0")!=NULL) {
  1881. LOG_I("关闭补光!!!");
  1882. ctr_led(OFF);
  1883. }
  1884. //关闭加热
  1885. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  1886. device_status.heat_status = 0;
  1887. ctr_Heat(OFF);
  1888. hmi_test_heat_stop(); /* 手动关闭:撤销自动关闭定时器 */
  1889. LOG_I("Close Heat!");
  1890. }
  1891. //打开加热
  1892. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  1893. device_status.heat_status = 1;
  1894. ctr_Heat(ON);
  1895. hmi_test_heat_start(); /* 屏幕测试加热:10分钟自动关闭(防忘记关干烧) */
  1896. LOG_I("Open Heat!");
  1897. }
  1898. else if (strstr(rx_buffer,"turnzp")!=NULL) {
  1899. LOG_W("转仓!");
  1900. device_status.clear_status=2;
  1901. }
  1902. //清扫
  1903. else if (strstr(rx_buffer,"clear")!=NULL) {
  1904. LOG_W("清扫!");
  1905. device_status.clear_status=1;
  1906. }
  1907. //震动
  1908. else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
  1909. LOG_I("震动!");
  1910. if(device_status.version_type==6)
  1911. {
  1912. Motor_shake(ON, device_status.shake_sec);
  1913. }
  1914. else{
  1915. ctr_shake(ON,device_status.shake_sec);
  1916. ctr_router(ON);
  1917. }
  1918. }
  1919. //打开补光
  1920. else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
  1921. rt_kprintf("\n打开补光!!!\n");
  1922. ctr_led(ON);
  1923. }
  1924. //关闭补光
  1925. else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
  1926. rt_kprintf("\n关闭补光!!!\n");
  1927. ctr_led(OFF);
  1928. }
  1929. //打开灯管
  1930. else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
  1931. {
  1932. device_status.test_flag=1;
  1933. rt_kprintf("Open Lamp!\n");
  1934. ctr_lamp(ON,1);
  1935. device_status.rain_status = 0;
  1936. }
  1937. //关闭灯管
  1938. else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
  1939. {
  1940. device_status.test_flag=0;
  1941. rt_kprintf("Close Lamp1!\n");
  1942. ctr_lamp(OFF,1);
  1943. }
  1944. //打开上仓门
  1945. else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
  1946. if(device_status.dtype==7)
  1947. {
  1948. ctr_lamp(ON, 1);
  1949. ctr_Motor_Driver(1, 0,1000000,500);
  1950. LOG_I("开始步进上行!");
  1951. }
  1952. else
  1953. {
  1954. rt_kprintf("Open UP!\n");
  1955. ctr_cangmen_up(OFF);
  1956. device_status.ctr_up_motor = 1;
  1957. }
  1958. }
  1959. //关闭上仓门
  1960. else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
  1961. if(device_status.dtype==7)
  1962. {
  1963. LOG_I("停止步进上行!");
  1964. ctr_lamp(OFF, 1);
  1965. ctr_Motor_Driver(0, 0,0,0);
  1966. }
  1967. else
  1968. {
  1969. rt_kprintf("CLOSE UP!\n");
  1970. ctr_cangmen_up(OFF);
  1971. device_status.ctr_up_motor = 2;
  1972. }
  1973. }
  1974. //打开下仓门
  1975. else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
  1976. if(device_status.dtype==7)
  1977. {
  1978. LOG_I("开始步进下行!");
  1979. ctr_lamp(ON, 1);
  1980. ctr_Motor_Driver(1, 1,1000000,500);
  1981. }
  1982. else
  1983. {
  1984. ctr_cangmen_down(OFF);
  1985. device_status.ctr_down_motor = 1;
  1986. rt_kprintf("Open down!\n");
  1987. }
  1988. }
  1989. //关闭下仓门
  1990. else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
  1991. if(device_status.dtype==7)
  1992. {
  1993. ctr_lamp(OFF, 1);
  1994. ctr_Motor_Driver(0, 0,0,0);
  1995. rt_kprintf("停止步进下行!\n");
  1996. }
  1997. else
  1998. {
  1999. ctr_cangmen_down(OFF);
  2000. device_status.ctr_down_motor = 2;
  2001. rt_kprintf("CLOSE down!\n");
  2002. }
  2003. }
  2004. else if(strstr(rx_buffer,"putTest.Testpaichong")!=NULL){
  2005. if(device_status.collt_box_sta==0){
  2006. device_status.collt_box_sta = 111;
  2007. }else{
  2008. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  2009. }
  2010. LOG_I("排虫测试!");
  2011. }
  2012. //排虫
  2013. else if(strstr(rx_buffer,"putTest.paichong")!=NULL){
  2014. if(device_status.run_sta == 0){
  2015. device_status.run_sta = 111;
  2016. }else{
  2017. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  2018. }
  2019. LOG_I("开始排虫!");
  2020. device_status.handle_bugger = 0;
  2021. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2022. }
  2023. else if(strstr(rx_buffer,"putTest.shouchong")!=NULL){
  2024. if(device_status.run_sta == 0){
  2025. device_status.run_sta = 112;
  2026. }else{
  2027. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  2028. }
  2029. LOG_I("开始收虫!");
  2030. device_status.handle_bugger = 1;
  2031. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2032. }
  2033. //设置设备时间
  2034. else if (strstr(rx_buffer,"stamp=")!=NULL)
  2035. {
  2036. var = strstr(rx_buffer,"=");
  2037. var ++;
  2038. LOG_I("设置时间:%s",var);
  2039. static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
  2040. strncpy(year_s, var, 4);
  2041. strncpy(month_s,var+4,2);
  2042. strncpy(day_s,var+6,2);
  2043. strncpy(hour_s,var+8,2);
  2044. strncpy(min_s,var+10,2);
  2045. strncpy(sec_s,var+12,2);
  2046. if(atoi(year_s)>=2024&&atoi(year_s)<3000&&atoi(month_s)>0&&atoi(month_s)<13&&atoi(day_s)>0&&atoi(day_s)<32)
  2047. {
  2048. if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
  2049. {
  2050. LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
  2051. /* ★ 原来是 >0: 合法的 00:xx:xx 永远设不进去(只改了日期)。且 24 点非法。 */
  2052. if(atoi(hour_s)>=0&&atoi(hour_s)<24&&atoi(min_s)>=0&&atoi(min_s)<=59&&atoi(sec_s)>=0&&atoi(sec_s)<=59)
  2053. {
  2054. LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
  2055. set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
  2056. MA_save();
  2057. }
  2058. //上传最新数据
  2059. dtu_request_report(); //立即请求一次上报(上报后按 dattim 自动重排定时器), 更新RTU的时间
  2060. }
  2061. else {
  2062. LOG_E("set_date failed!");
  2063. }
  2064. }
  2065. }
  2066. //设置闹钟时间
  2067. else if (strstr(rx_buffer,"ALARM=")!=NULL)
  2068. {
  2069. char *var = strstr(rx_buffer,"=");
  2070. if (var == NULL) {
  2071. return;
  2072. }
  2073. var ++;
  2074. // ===================== 修复:去掉末尾的 \r 或 换行 =====================
  2075. // 遍历字符串,把 \r \n 全部替换成结束符
  2076. for (int i = 0; var[i] != '\0'; i++) {
  2077. if (var[i] == '\r' || var[i] == '\n') {
  2078. var[i] = '\0';
  2079. break;
  2080. }
  2081. }
  2082. /* ★ 原来无条件写 var[6]: 载荷短于 6 位时读写越过字符串结尾,
  2083. 帧被截断时 var+6 甚至越过 rx_buffer 末尾。先判长度再截断。 */
  2084. if(strlen(var) > 6) {
  2085. var[6] = '\0';
  2086. }
  2087. // 现在 var 只剩下干净的 6 位数字了
  2088. int len = strlen(var);
  2089. int is_valid = 1;
  2090. // 1. 必须是 6 个字符(已经去掉\r)
  2091. if (len != 6) {
  2092. is_valid = 0;
  2093. }
  2094. // 2. 必须全部是数字 0-9
  2095. else {
  2096. for (int i = 0; i < 6; i++) {
  2097. if (!(var[i] >= '0' && var[i] <= '9')) {
  2098. is_valid = 0;
  2099. break;
  2100. }
  2101. }
  2102. }
  2103. // 校验不通过:打印报错,不赋值
  2104. if (!is_valid) {
  2105. LOG_E("闹钟格式错误!必须是 6 位数字时间(如 150000),当前值:%s", var);
  2106. return;
  2107. }
  2108. // ======================================================================
  2109. // 格式正确才赋值
  2110. LOG_I("设置闹钟时间:%s",var);
  2111. rt_sprintf(device_status.alarm_tg, "%s", var);
  2112. ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
  2113. }
  2114. //设备编码设置
  2115. else if (strstr(rx_buffer,"deviceId=")!=NULL)
  2116. {
  2117. char value_str[20];
  2118. memset(value_str,0,sizeof(value_str));
  2119. LOG_I("设备编码 长度:%d",strlen(rx_buffer));
  2120. if(strlen(rx_buffer)!=19){
  2121. LOG_E("编码长度错误!");
  2122. return;
  2123. }
  2124. var = strstr(rx_buffer,"=");
  2125. var ++;
  2126. strncpy(value_str,var,8); //设备编码为16位
  2127. if(strstr(value_str,"00000000")!=RT_NULL){
  2128. char *config_reset = makeJson_Config_Reset();
  2129. //发送json到RTU
  2130. DTU_Write("%s",config_reset);
  2131. rt_free(config_reset);
  2132. //恢复RTU的默认配置参数
  2133. }else{
  2134. rt_sprintf(device_status.deviceId,"10300101%s",value_str); //设备大类编码103-虫情 设备小类编码 001 生产商代码01
  2135. ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
  2136. LOG_I("解析到设备编码:%s",device_status.deviceId);
  2137. char *device_id = makeJson_Config_BDH(device_status.deviceId);
  2138. //发送json到RTU
  2139. DTU_Write("%s",device_id);
  2140. rt_free(device_id);
  2141. }
  2142. }
  2143. //定时设置
  2144. else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
  2145. char value_str[10];
  2146. memset(value_str,0,sizeof(value_str));
  2147. if(device_status.dtype==7)
  2148. {
  2149. var = strstr(rx_buffer,"=");
  2150. var ++;
  2151. strncpy(value_str,var,2);
  2152. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2153. {
  2154. device_status.collecting_sh1 = atoi(value_str);
  2155. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  2156. }
  2157. else {
  2158. LOG_E("Set collecting_sh1 Failed!");
  2159. }
  2160. memset(value_str,0,sizeof(value_str));
  2161. strncpy(value_str,var+2,2);
  2162. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2163. {
  2164. device_status.collecting_eh1 = atoi(value_str);
  2165. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  2166. }
  2167. else {
  2168. LOG_E("Set collecting_eh1 Failed!");
  2169. }
  2170. memset(value_str,0,sizeof(value_str));
  2171. strncpy(value_str,var+4,2);
  2172. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2173. {
  2174. device_status.collecting_sh2 = atoi(value_str);
  2175. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  2176. }
  2177. else {
  2178. LOG_E("Set collecting_sh2 Failed!");
  2179. }
  2180. memset(value_str,0,sizeof(value_str));
  2181. strncpy(value_str,var+6,2);
  2182. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2183. {
  2184. device_status.collecting_eh2 = atoi(value_str);
  2185. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  2186. }
  2187. else {
  2188. LOG_E("Set collecting_eh2 Failed!");
  2189. }
  2190. memset(value_str,0,sizeof(value_str));
  2191. strncpy(value_str,var+8,2);
  2192. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2193. {
  2194. device_status.shake_sec = atoi(value_str);
  2195. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  2196. }
  2197. else {
  2198. LOG_E("Set shake_sec Failed!");
  2199. }
  2200. memset(value_str,0,sizeof(value_str));
  2201. strncpy(value_str,var+10,2);
  2202. if(atoi(value_str)>0&&atoi(value_str)<=24)
  2203. {
  2204. device_status.cul_time = atoi(value_str);
  2205. ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time));
  2206. }
  2207. else {
  2208. LOG_E("Set cul_time Failed!");
  2209. }
  2210. MA_save();
  2211. LOG_I("参数已保存:%d-%d-%d-%d-%d-%d",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2,device_status.shake_sec,device_status.cul_time);
  2212. return;
  2213. }
  2214. LOG_I("时间设置!");
  2215. var=strstr(rx_buffer,"=");
  2216. var++;
  2217. strncpy(value_str,var,2);
  2218. if(atoi(value_str) > 0 && atoi(value_str) <60)
  2219. {
  2220. if(device_status.version_type!=6)
  2221. {
  2222. device_status.sunBar=atoi(value_str);
  2223. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  2224. }
  2225. else {
  2226. device_status.shake_sec = atoi(value_str);
  2227. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  2228. }
  2229. }
  2230. else {
  2231. LOG_E("参数设置异常(光控时长):%d!!!",atoi(value_str));
  2232. }
  2233. memset(value_str,0,sizeof(value_str));
  2234. //时控开始时间
  2235. strncpy(value_str,var+2,2);
  2236. if(atoi(value_str) >= 0 && atoi(value_str) < 24)
  2237. {
  2238. #if 1
  2239. device_status.startBar=atoi(value_str);
  2240. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  2241. #endif
  2242. }
  2243. else {
  2244. LOG_E("参数设置异常(开始):%d!!!",atoi(value_str));
  2245. }
  2246. memset(value_str,0,sizeof(value_str));
  2247. //
  2248. strncpy(value_str,var+4,2);
  2249. if(atoi(value_str) >= 0&& atoi(value_str) < 24)
  2250. {
  2251. #if 1
  2252. device_status.stopBar=atoi(value_str);
  2253. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  2254. #endif
  2255. }
  2256. else {
  2257. LOG_E("参数设置异常(结束):%d!!!",atoi(value_str));
  2258. }
  2259. memset(value_str,0,sizeof(value_str));
  2260. //
  2261. strncpy(value_str,var+6,2);
  2262. if(atoi(value_str) >= 0 && atoi(value_str) <= 1)
  2263. {
  2264. device_status.timeSwitch=atoi(value_str);
  2265. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2266. }
  2267. else {
  2268. if(device_status.version_type==6)
  2269. {
  2270. device_status.timeSwitch=1;
  2271. device_status.dattim=atoi(value_str);
  2272. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  2273. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2274. }
  2275. else {
  2276. LOG_E("参数设置异常(模式):%d!!!",atoi(value_str));
  2277. }
  2278. }
  2279. //2022/06/23,add: 操作记录改为局部缓冲(原 device_status.json_w_clock)
  2280. {
  2281. char json_warn[192];
  2282. rt_snprintf(json_warn, sizeof(json_warn),"屏幕操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)操作时间:%s",device_status.startBar,device_status.stopBar,device_status.timeSwitch,device_status.stamp);
  2283. json_temp = makeJson_Warn(json_warn, "修改工作时间");
  2284. ef_set_env_blob("json_w_clock", json_warn, strlen(json_warn) + 1); //将修改屏幕参数操作记录保存到Flash中
  2285. if(atoi(device_status.net)==0)
  2286. DTU_Write("%s",json_temp);
  2287. rt_free(json_temp);
  2288. }
  2289. rt_thread_delay(2000);
  2290. MA_save();
  2291. rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
  2292. }
  2293. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  2294. static char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
  2295. static char value_temp[5];
  2296. LOG_I("系统设置!");
  2297. if(device_status.dtype==7)
  2298. {
  2299. LOG_I("系统设置,孢子仪!");
  2300. var = strstr(rx_buffer,"=");
  2301. var ++;
  2302. memset(value_temp,0,sizeof(value_temp));
  2303. strncpy(value_temp,var,2);
  2304. if(atoi(value_temp)==0 || atoi(value_temp) == 1)
  2305. {
  2306. device_status.cold_sw = atoi(value_temp); //制冷开关
  2307. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  2308. }
  2309. else {
  2310. LOG_E("制冷开关参数设置错误!");
  2311. }
  2312. memset(value_temp,0,sizeof(value_temp));
  2313. strncpy(value_temp,var+2,2);
  2314. if(atoi(value_temp)>=0 && atoi(value_temp) <= 7)
  2315. {
  2316. device_status.vol = atoi(value_temp); //音量
  2317. }
  2318. else {
  2319. LOG_E("音量参数设置错误!");
  2320. }
  2321. memset(value_temp,0,sizeof(value_temp));
  2322. strncpy(value_temp,var+4,2);
  2323. if(atoi(value_temp)>=10 && atoi(value_temp) <= 50)
  2324. {
  2325. device_status.heatvalue = atoi(value_temp); //设定的培养温度
  2326. ef_set_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2327. }
  2328. else {
  2329. LOG_E("培养温度参数设置失败!");
  2330. }
  2331. memset(value_temp,0,sizeof(value_temp));
  2332. strncpy(value_temp,var+6,2);
  2333. if(atoi(value_temp)>=1 && atoi(value_temp) <= 60)
  2334. {
  2335. device_status.dattim = atoi(value_temp); //设定的数据上传间隔,单位:分钟
  2336. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  2337. }
  2338. else {
  2339. LOG_E("数据上传间隔参数设置失败!");
  2340. }
  2341. memset(value_temp,0,sizeof(value_temp));
  2342. strncpy(value_temp,var+8,2);
  2343. if(atoi(value_temp)>=1 && atoi(value_temp) <= 30)
  2344. {
  2345. device_status.thsp = atoi(value_temp); //设定的息屏间隔,单位:分钟
  2346. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  2347. }
  2348. else {
  2349. LOG_E("数据息屏间隔参数设置失败!");
  2350. }
  2351. LOG_I("制冷开关:%d",device_status.cold_sw);
  2352. LOG_I("系统设置已保存:\n自动熄屏-%d\n音量-%d\n培养温度-%d\n数据上传间隔-%d",device_status.thsp,device_status.vol,device_status.heatvalue,device_status.dattim);
  2353. MA_save();
  2354. return;
  2355. }
  2356. var = strstr(rx_buffer,"=");
  2357. var ++;
  2358. strncpy(clt_time_str,var,2);
  2359. if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
  2360. {
  2361. device_status.clt_time = atoi(clt_time_str); //收集时间
  2362. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  2363. }
  2364. strncpy(heattime_str,var+2,2);
  2365. if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
  2366. {
  2367. device_status.heattime = atoi(heattime_str); //加热时间
  2368. LOG_I("设置加热时间:%d",atoi(heattime_str));
  2369. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  2370. }
  2371. strncpy(heatvale_str,var+4,3);
  2372. if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
  2373. {
  2374. device_status.heatvalue=atoi(heatvale_str);
  2375. LOG_I("设置加热温度:%d",device_status.heatvalue);
  2376. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2377. }
  2378. strncpy(vol_str,var+7,3);
  2379. if(atoi(vol_str)>0&&atoi(vol_str)<8)
  2380. {
  2381. device_status.vol = atoi(vol_str); //音量
  2382. LOG_I("设置音量:%d",device_status.vol);
  2383. //DTU_Write("volume=");
  2384. }
  2385. strncpy(thsp_str,var+10,4);
  2386. if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
  2387. {
  2388. device_status.thsp = atoi(thsp_str); //熄屏时间
  2389. LOG_I("设置熄屏时间:%d",device_status.thsp);
  2390. }
  2391. else {
  2392. LOG_E("参数设置失败!!!");
  2393. }
  2394. rt_thread_delay(1000);
  2395. MA_save();
  2396. //rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
  2397. }
  2398. else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
  2399. /* ★ 原来是 char rtc_h[2] + strncpy(rtc_h,var,2): 拷贝满 2 字节不补 '\0',
  2400. atoi() 会越界读栈; 而且小时上限写成 24(非法)。直接解析并做范围校验。 */
  2401. int v;
  2402. var = strstr(rx_buffer,"=");
  2403. var++;
  2404. v = atoi(var);
  2405. if(v>=0 && v<=23)
  2406. {
  2407. set_time(v, 99, 99);
  2408. }
  2409. }
  2410. else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
  2411. int v;
  2412. var = strstr(rx_buffer,"=");
  2413. var++;
  2414. v = atoi(var);
  2415. if(v>=0 && v<=59)
  2416. {
  2417. set_time(99, v, 99);
  2418. }
  2419. }
  2420. else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
  2421. int v;
  2422. var = strstr(rx_buffer,"=");
  2423. var++;
  2424. v = atoi(var);
  2425. if(v>=0 && v<=59)
  2426. {
  2427. set_time(99, 99, v);
  2428. }
  2429. }
  2430. else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
  2431. {
  2432. static char dattim_str[3];
  2433. rt_kprintf("上传间隔设置!\n");
  2434. var = strstr(rx_buffer,"=");
  2435. var ++;
  2436. strncpy(dattim_str,var,2);
  2437. if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
  2438. {
  2439. device_status.dattim =atoi(dattim_str); //数据上传间隔
  2440. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  2441. MA_save();
  2442. }
  2443. else {
  2444. rt_kprintf("设置失败!!!\n");
  2445. }
  2446. rt_kprintf("数据上传设置:%d\n",device_status.dattim);
  2447. }
  2448. //光控测试
  2449. else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
  2450. {
  2451. rt_kprintf("光控测试!!!");
  2452. ctr_lamp(ON,1);
  2453. device_status.sun_test = 1;
  2454. }
  2455. else if (strstr(rx_buffer,"setshake_sec=")!=NULL)
  2456. {
  2457. static char shakesec_str[4];
  2458. LOG_I("震动时间设置!");
  2459. var = strstr(rx_buffer,"=");
  2460. var ++;
  2461. strncpy(shakesec_str,var,2);
  2462. if(atoi(shakesec_str)>=0&&atoi(shakesec_str)<=10)
  2463. {
  2464. device_status.shake_sec =atoi(shakesec_str); //2026-0608:取消*10
  2465. }
  2466. else {
  2467. LOG_E("设置失败!!!");
  2468. }
  2469. LOG_D("震动时间设置:%d",device_status.shake_sec);
  2470. }
  2471. //恢复出厂设置电池
  2472. else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
  2473. {
  2474. ResetParamDefault_DC();
  2475. rt_thread_delay(1000);
  2476. //2022/06/23,add: 操作记录改为局部缓冲(原 device_status.json_w_reset)
  2477. {
  2478. char json_warn[192];
  2479. rt_snprintf(json_warn, sizeof(json_warn),"屏幕操作:系统重置:电池供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2480. json_temp = makeJson_Warn(json_warn, "系统重置");
  2481. ef_set_env_blob("json_w_reset", json_warn, strlen(json_warn) + 1); //将修改屏幕参数操作记录保存到Flash中
  2482. if(atoi(device_status.net)==0)
  2483. DTU_Write("%s",json_temp);
  2484. rt_free(json_temp);
  2485. }
  2486. }
  2487. //恢复出厂设置-交流
  2488. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  2489. {
  2490. ResetParamDefault();
  2491. rt_thread_delay(1000);
  2492. //2022/06/23,add: 操作记录改为局部缓冲(原 device_status.json_w_reset)
  2493. {
  2494. char json_warn[192];
  2495. rt_snprintf(json_warn, sizeof(json_warn),"屏幕操作:系统重置:交流供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2496. json_temp = makeJson_Warn(json_warn, "系统重置");
  2497. ef_set_env_blob("json_w_reset", json_warn, strlen(json_warn) + 1); //将修改屏幕参数操作记录保存到Flash中
  2498. if(atoi(device_status.net)==0)
  2499. DTU_Write("%s",json_temp);
  2500. rt_free(json_temp);
  2501. }
  2502. }
  2503. //获取SIM卡信息
  2504. else if(strstr(rx_buffer,"getSim")!=NULL)
  2505. {
  2506. //http://sim.openluat.com/api/wechat/openapidoc
  2507. DTU_Write("getsiminfo");
  2508. }
  2509. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  2510. Debug_log_ON();
  2511. }
  2512. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  2513. Debug_log_OFF();
  2514. }
  2515. /* Swversion 版本类型检测已并入本函数开头"页刷新/版本握手"统一入口块(先检测后刷新),原分支链移除 */
  2516. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  2517. {
  2518. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  2519. HMI_WRITE("sysTest.heat_txt.txt=\"%s\"",device_status.heat_power);
  2520. hmi_write_num_or_blank("sysTest.t1.txt=\"%s\"", device_status.work_heat_left, RT_FALSE); /* 剩余加热时间(s) */
  2521. hmi_write_num_or_blank("sysTest.t3.txt=\"%s\"", device_status.work_door_left, RT_FALSE); /* 仓门开/关倒计时(s) */
  2522. hmi_write_num_or_blank("sysTest.t0.txt=\"%s\"", device_status.photo_left, RT_FALSE); /* 拍照结束剩余(s) */
  2523. hmi_write_num_or_blank("sysTest.t5.txt=\"%s\"", device_status.clear_left, RT_FALSE); /* 清扫结束剩余(s) */
  2524. // HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
  2525. }
  2526. //立即上传
  2527. else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
  2528. {
  2529. UpLoad_Data();
  2530. }
  2531. else if (strstr(rx_buffer,"checkupdate")!=NULL)
  2532. {
  2533. time_t now = time(RT_NULL);
  2534. static struct tm tm;
  2535. gmtime_r(&now, &tm);
  2536. HMI_WRITE("hardcheck.tip_rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2537. HMI_WRITE("hardcheck.p0.pic=%d",70);
  2538. //RTC时间判断,如果当前年份小于2020,提示需要设置时间
  2539. if(tm.tm_year+1900<2020)
  2540. {
  2541. rt_kprintf("时间未设置:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2542. HMI_WRITE("hardcheck.tip_rtc.txt=%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2543. }
  2544. }
  2545. //进入bootloader模式
  2546. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  2547. device_status.stop_in_bootloader = 1;
  2548. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  2549. rt_hw_cpu_reset();
  2550. }
  2551. /* ★ "dtu_reboot" 里包含 "reboot", 原来 reboot 分支在前 -> 屏幕下发 dtu_reboot
  2552. 会走整机复位, RTU 重启指令永远发不出去。dtu_reboot 必须先判。 */
  2553. else if (strstr(rx_buffer,"dtu_reboot")!=NULL) {
  2554. DTU_Write("{\"cmd\": \"dtu_reboot\"}");
  2555. }
  2556. //软件重启
  2557. else if (strstr(rx_buffer,"reboot")!=NULL) {
  2558. rt_hw_cpu_reset();
  2559. }
  2560. else if (strstr(rx_buffer,"saveLog_usb")!=NULL) {
  2561. DTU_Write("{\"cmd\": \"saveLog_usb\"}");
  2562. }
  2563. //屏幕上触发远程升级
  2564. else if (strstr(rx_buffer,"hmi_update")!=NULL) {
  2565. }else if (strstr(rx_buffer,"SaveRecordParam")!=NULL){
  2566. //"SaveRecordParam:10000-200"
  2567. int param1, param2;
  2568. // 从冒号后开始扫描
  2569. if (sscanf(rx_buffer + strlen("SaveRecordParam:"), "%d-%d", &param1, &param2) == 2) {
  2570. /* ★ 原 itoa(param1, temp_val_pic, 10) 写 char[10]: 10 位十进制(或负值 11 字节)
  2571. 直接踩栈, itoa 不做容量检查。负数是非法参数, 直接丢弃。 */
  2572. char temp_val_mqtt[12],temp_val_pic[12];
  2573. if (param1 < 0 || param2 < 0) {
  2574. LOG_E("Invalid record param:%d-%d", param1, param2);
  2575. return;
  2576. }
  2577. rt_snprintf(temp_val_pic, sizeof(temp_val_pic), "%d", param1);
  2578. rt_snprintf(temp_val_mqtt, sizeof(temp_val_mqtt), "%d", param2);
  2579. /* ★ 原来 DTU_Write(write_record_param) 把 JSON 当格式串(内容含 '-' 等无所谓,
  2580. 但一旦含 '%' 就会被 vsnprintf 当格式说明符) -> 统一走 dtu_write_json */
  2581. dtu_write_json(makeJson_record_write(temp_val_mqtt,temp_val_pic));
  2582. } else {
  2583. LOG_E("Parse failed.\n");
  2584. }
  2585. }
  2586. else if (strstr(rx_buffer,"ReadRecordParam")!=NULL){
  2587. LOG_I("读离线存储参数");
  2588. dtu_write_json(makeJson_record_read("data"));
  2589. }
  2590. else if (strstr(rx_buffer,"checkcamera")!=NULL){
  2591. DTU_Write("{\"ctr_cam\":\"checkcamera\"}");
  2592. //校准屏幕的RTC
  2593. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  2594. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  2595. HMI_WRITE("rtc2=%d",tm.tm_mday);
  2596. HMI_WRITE("rtc3=%d",tm.tm_hour);
  2597. HMI_WRITE("rtc4=%d",tm.tm_min);
  2598. HMI_WRITE("rtc5=%d",tm.tm_sec);
  2599. //开启超时定时器
  2600. Start_timer_Uart2();
  2601. }else if (strstr(rx_buffer,"ReadCamParam")!=NULL){
  2602. DTU_Write("{\"ctr_cam\":\"ReadParam\"}");
  2603. //开启超时定时器
  2604. Start_timer_Uart2();
  2605. }//SaveCamParam:2070.00000-0.7-144-30-0-0-5472*5472
  2606. else if (strstr(rx_buffer,"SaveCamParam")!=NULL){
  2607. const char *prefix ="SaveCamParam:";
  2608. size_t prefix_len = strlen(prefix);
  2609. const char *start_pos = strstr(rx_buffer, prefix);
  2610. if(start_pos!=NULL)
  2611. {
  2612. // 移动到前缀之后的位置
  2613. start_pos += prefix_len;
  2614. /* ★ makeJson_Cam 的返回值原来没人释放 -> 每次点"保存相机参数"漏一块堆。
  2615. (start_pos 是 const char*, 与 makeJson_Cam(char*,char*) 的形参有 const 冲突,
  2616. 这里做一次显式转换消除 -Wdiscarded-qualifiers) */
  2617. dtu_write_json(makeJson_Cam("SaveParam", (char *)start_pos));
  2618. }
  2619. //开启超时定时器
  2620. Start_timer_Uart2();
  2621. }else if (strstr(rx_buffer,"GrabOn")!=NULL){
  2622. DTU_Write("{\"ctr_cam\":\"GrabOn\"}");
  2623. //开启超时定时器
  2624. Start_timer_Uart2();
  2625. }else if (strstr(rx_buffer,"GrabOff")!=NULL){
  2626. DTU_Write("{\"ctr_cam\":\"GrabOff\"}");
  2627. //开启超时定时器
  2628. Start_timer_Uart2();
  2629. }
  2630. else if(strstr(rx_buffer,"SelfCheck")!=NULL)
  2631. {
  2632. //校准屏幕的RTC
  2633. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  2634. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  2635. HMI_WRITE("rtc2=%d",tm.tm_mday);
  2636. HMI_WRITE("rtc3=%d",tm.tm_hour);
  2637. HMI_WRITE("rtc4=%d",tm.tm_min);
  2638. HMI_WRITE("rtc5=%d",tm.tm_sec);
  2639. if(device_status.selfcheck_ws==2||device_status.selfcheck_ws==3)
  2640. {
  2641. LOG_I("自检进行中!");
  2642. }else {
  2643. device_status.selfcheck_ws = 2;
  2644. LOG_W("开始自检:%d",device_status.selfcheck_ws);
  2645. }
  2646. }else if(strstr(rx_buffer,"SwitchMode")!=NULL)
  2647. {
  2648. device_status.work_mode = !device_status.work_mode;
  2649. ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
  2650. LOG_W("修改工作模式:%d",device_status.work_mode);
  2651. }else if(strstr(rx_buffer,"DeleteUimage")!=NULL){
  2652. /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串; 统一走 dtu_write_json("%s" + 判空 + 自动释放) */
  2653. dtu_write_json(makeJsonRpc2_0("upan_delete_image", NULL, 1));
  2654. }else if(strstr(rx_buffer,"MOVE_IMAGE")!=NULL){
  2655. dtu_write_json(makeJsonRpc2_0("MOVE_IMAGE", NULL, 1));
  2656. }else if(strstr(rx_buffer,"COPY_IMAGE")!=NULL){
  2657. cJSON *params = cJSON_CreateObject(); // params 子对象
  2658. if (params == RT_NULL) {
  2659. LOG_E("COPY_IMAGE: create params failed (no memory)");
  2660. } else {
  2661. cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
  2662. cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
  2663. dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1));
  2664. }
  2665. }else if(strstr(rx_buffer,"UMOUNT_UPAN")!=NULL){
  2666. dtu_write_json(makeJsonRpc2_0("UMOUNT_UPAN", NULL, 1));
  2667. }
  2668. /* 一屏指令处理完, 若有参数变更则由落盘线程合并写入备份分区 */
  2669. param_mark_dirty();
  2670. }
  2671. //入口函数-电机控制及
  2672. static void Ctr_Mortor_entry(void* parameter)
  2673. {
  2674. rt_uint16_t num_up_open = 200; //20s 上仓门打开时间
  2675. rt_uint16_t num_up_close = 200; // 上仓门关闭时间
  2676. rt_uint16_t num_down_open = 200; // 下仓门打开时间
  2677. rt_uint16_t num_down_close = 200; // 下仓门关闭时间
  2678. rt_int16_t photo_led_sec = 900; //55S
  2679. static rt_uint16_t mock_uart2_timer_cnt = 0;
  2680. static rt_uint16_t route_off_t = 0;
  2681. device_status.route_off_t = 0;
  2682. static uint8_t is_rain = 0;
  2683. static uint8_t isnot_rain = 0;
  2684. rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
  2685. while(1)
  2686. {
  2687. #if 0
  2688. time_t time_reset12V;
  2689. time(&time_reset12V);
  2690. static struct tm alarm_reset12V;
  2691. alarm_reset12V =*localtime(&time_reset12V);
  2692. if((alarm_reset12V.tm_hour)==8&&(alarm_reset12V.tm_min==18)&&(alarm_reset12V.tm_sec==00))
  2693. {
  2694. char *rep = makeJsonRpc2_0("reboot",NULL, 1);
  2695. /* ★ 第四批: DTU_Write 是变参函数, 原来直接传 rep 等于"拿 JSON 当格式串"。
  2696. 统一走 dtu_write_json(固定 "%s" + 判空 + 自动释放)。 */
  2697. dtu_write_json(rep);
  2698. rt_thread_delay(1000);
  2699. }
  2700. if(device_status.mock_uart2_timer_flag==1){
  2701. mock_uart2_timer_cnt++;
  2702. if(mock_uart2_timer_cnt>=200){
  2703. mock_uart2_timer_cnt = 0;
  2704. device_status.mock_uart2_timer_flag = 0;
  2705. }
  2706. }
  2707. #endif
  2708. if(device_status.ctr_up_motor==1)
  2709. {
  2710. ctr_cangmen_up(OPEN);
  2711. num_up_open--;
  2712. if (num_up_open == 0)
  2713. {
  2714. #if JINGJIKUAN == 0
  2715. ctr_cangmen_up(OFF);
  2716. #else
  2717. device_status.ctr_up_motor = 0;
  2718. #endif
  2719. num_up_open = 200;
  2720. system_status.upper_door_open_count +=1;
  2721. }
  2722. num_up_close = 200;
  2723. }
  2724. else if(device_status.ctr_up_motor==2)
  2725. {
  2726. ctr_cangmen_up(CLOSE);
  2727. num_up_close --;
  2728. if (num_up_close == 0)
  2729. {
  2730. #if JINGJIKUAN == 0
  2731. ctr_cangmen_up(OFF);
  2732. #else
  2733. device_status.ctr_up_motor = 0;
  2734. #endif
  2735. num_up_close= 200;
  2736. system_status.upper_door_close_count +=1;
  2737. }
  2738. num_up_open = 200;
  2739. }
  2740. if (device_status.ctr_down_motor == 1)
  2741. {
  2742. ctr_cangmen_down(OPEN);
  2743. num_down_open--;
  2744. #if TG_MODULE ==1
  2745. if(rt_pin_read(CTR_UPC_LIMIT)==1){
  2746. ctr_cangmen_down(OFF);
  2747. Beep_Tip(2000);
  2748. ctr_cangmen_down(OPEN);
  2749. Beep_Tip(2000);
  2750. num_down_open = 200;
  2751. device_status.ctr_down_motor = 1;
  2752. }
  2753. #endif
  2754. if (num_down_open == 0) //2025-07-07,增加限位检测
  2755. {
  2756. #if JINGJIKUAN == 0
  2757. ctr_cangmen_down(OFF);
  2758. #else
  2759. device_status.ctr_down_motor = 0;
  2760. #endif
  2761. num_down_open = 200;
  2762. system_status.lower_door_open_count +=1;
  2763. }
  2764. num_down_close = 200;
  2765. }
  2766. else if (device_status.ctr_down_motor == 2)
  2767. {
  2768. ctr_cangmen_down(CLOSE);
  2769. num_down_close--;
  2770. if (num_down_close == 0)
  2771. {
  2772. #if JINGJIKUAN == 0
  2773. ctr_cangmen_down(OFF);
  2774. #else
  2775. device_status.ctr_down_motor = 0;
  2776. #endif
  2777. num_down_close = 200;
  2778. system_status.lower_door_close_count += 1;
  2779. }
  2780. num_down_open = 200;
  2781. }
  2782. /* 屏幕 workSta.t3:仓门开(关)动作倒计时(s);无动作时清零 */
  2783. if(device_status.ctr_up_motor == 1) device_status.work_door_left = (uint16_t)(num_up_open / 10);
  2784. else if(device_status.ctr_up_motor == 2) device_status.work_door_left = (uint16_t)(num_up_close / 10);
  2785. else if(device_status.ctr_down_motor == 1) device_status.work_door_left = (uint16_t)(num_down_open / 10);
  2786. else if(device_status.ctr_down_motor == 2) device_status.work_door_left = (uint16_t)(num_down_close / 10);
  2787. else device_status.work_door_left = 0;
  2788. if(device_status.sun_test==1)
  2789. {
  2790. SunTest();
  2791. }
  2792. //拍照
  2793. if(device_status.photo_ws==1)
  2794. {
  2795. struct timeval tv;
  2796. gettimeofday(&tv, NULL);
  2797. device_status.photo_stamp = tv.tv_sec-8*3600;
  2798. if(photo_led_sec==900)
  2799. ctr_led(ON);
  2800. //打开补光灯
  2801. if(photo_led_sec==800)
  2802. {
  2803. system_status.photo_taken_count+=1;
  2804. LOG_I("测试拍照!");
  2805. #if 1
  2806. char *takephotojson = makeJson_takephoto();
  2807. DTU_Write("%s",takephotojson);
  2808. rt_free(takephotojson);
  2809. #endif
  2810. rt_thread_delay(5000); //延时
  2811. }
  2812. //15S后执行拍照
  2813. else if (photo_led_sec==750)
  2814. {
  2815. //DTU_Write("takephoto.mp3");
  2816. //延时
  2817. rt_thread_delay(1000);
  2818. LOG_I("测试拍照!");
  2819. Hk_Take_io();
  2820. }
  2821. photo_led_sec--;
  2822. /* 屏幕 workSta.t5:拍照结束剩余(s)。photo_led_sec 900->300 段倒计时 60s->0 */
  2823. device_status.photo_left = (photo_led_sec > 300) ? (uint16_t)((photo_led_sec - 300) / 10) : 0;
  2824. if(photo_led_sec==300) //
  2825. {
  2826. ctr_led(OFF);
  2827. device_status.photo_ws = 0;
  2828. photo_led_sec = 900;
  2829. device_status.photo_left = 0;
  2830. if(device_status.test_flag==0){
  2831. device_status.clear_status = 1;
  2832. }else{
  2833. device_status.test_flag = 0;
  2834. }
  2835. HMI_WRITE("sysTest.switchPhoto.val=0");
  2836. HMI_WRITE("sysTest.switchFill.val=0");
  2837. }
  2838. }else{
  2839. photo_led_sec = 900;
  2840. device_status.photo_left = 0;
  2841. }
  2842. if(device_status.selfcheck_ws==2)
  2843. {
  2844. selfcheck(1);
  2845. }else if (device_status.selfcheck_ws==3) {
  2846. selfcheck(0);
  2847. }
  2848. if(device_status.route_off_t!=0)
  2849. {
  2850. ctr_router(OFF);
  2851. route_off_t = device_status.route_off_t * 60 * 10;
  2852. device_status.route_off_t = 0;
  2853. }
  2854. if(route_off_t>0)
  2855. {
  2856. route_off_t--;
  2857. if(route_off_t==1)
  2858. {
  2859. ctr_router(ON);
  2860. route_off_t = 0;
  2861. }
  2862. }
  2863. #if JINGJIKUAN == 1
  2864. if(device_status.rain_status==1){
  2865. isnot_rain = 0;
  2866. if(is_rain==0){
  2867. ctr_Collect_Fan(OFF); //触发雨控
  2868. rt_thread_delay(2000);
  2869. device_status.ctr_up_motor = 2; //关闭上仓门
  2870. is_rain = 1;
  2871. }
  2872. }else{
  2873. is_rain = 0;
  2874. if(isnot_rain == 0){
  2875. ctr_Collect_Fan(ON); //解除雨控
  2876. isnot_rain = 1;
  2877. }
  2878. }
  2879. #endif
  2880. /* 屏幕刷新重复过滤表:到点(每 HMI_DEDUPE_RESYNC_SEC 秒)整体复位一次,
  2881. 保证屏幕重启/换页后最迟该周期内恢复全量刷新(详见 HMI_WRITE 处说明) */
  2882. hmi_dedupe_tick();
  2883. rt_thread_delay(100);
  2884. }
  2885. }
  2886. /*屏幕写数据*/
  2887. int Hmi_Fresh(void)
  2888. {
  2889. //初始化串口4
  2890. //HMI_uart4_init();
  2891. rt_err_t ret = RT_EOK;
  2892. /* 初始化消息队列 */
  2893. rt_mq_init(&rx_mq, "rx_mq",
  2894. msg_pool, /* 存放消息的缓冲区 */
  2895. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  2896. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  2897. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  2898. /* 设置接收回调函数(★ 原来未判空: 屏幕串口设备名配错/未注册时
  2899. rt_device_find() 返回 NULL, 这里直接解引用空设备句柄) */
  2900. if (serial != RT_NULL)
  2901. {
  2902. rt_device_set_rx_indicate(serial, uart_input);
  2903. }
  2904. /* 创建线程*/
  2905. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  2906. Hmi_thread_entry,
  2907. RT_NULL,
  2908. HMI_THREAD_STACK_SIZE,
  2909. HMI_THREAD_PRIORITY,
  2910. HMI_THREAD_TIMESLICE);
  2911. /*部分控制的线程*/
  2912. ctr_motor = rt_thread_create("Ctr motor",
  2913. Ctr_Mortor_entry,
  2914. RT_NULL,
  2915. CONTROL_MOTOR_STACK_SIZE,
  2916. CONTROL_MOTOR_PRIORITY,
  2917. CONTROL_MOTOR_TIMESLICE);
  2918. if(ctr_motor != RT_NULL)
  2919. {
  2920. rt_thread_startup(ctr_motor);
  2921. }
  2922. /* 如果获得线程控制块,启动这个线程 */
  2923. if (hmi1 != RT_NULL)
  2924. {
  2925. rt_thread_startup(hmi1);
  2926. }
  2927. else
  2928. {
  2929. ret = RT_ERROR;
  2930. }
  2931. return ret;
  2932. }
  2933. void Check_ParamConfig(void)
  2934. {
  2935. time_t now = time(RT_NULL);
  2936. static struct tm tm;
  2937. gmtime_r(&now, &tm);
  2938. /* 双分区参数备份: 上电先把 param_a / param_b 里有效的那份读出来备用。
  2939. ★ 必须是 static: param_persist_t 有 820 字节,而 main 线程栈只有 2048,
  2940. 这个函数里还有第二个(param 自检用的 cur),两个放栈上再加调用栈必爆。 */
  2941. static param_persist_t param_boot_snap;
  2942. uint32_t param_boot_seq = 0;
  2943. int param_boot_snap_ok = 0;
  2944. int ef_ok = 0; /* EasyFlash 是否可用 */
  2945. int up_flag_ef = 0; /* 通道①: EasyFlash 里的升级请求 */
  2946. int up_flag_snap = 0; /* 通道②: 参数快照里的升级请求 */
  2947. int up_flag_bkp = 0; /* 通道③: RTC 备份寄存器里的升级请求 */
  2948. int up_request = 0; /* 三通道裁决结果 */
  2949. /* ★ flash 健康检查门控(2026-09-24 加固)
  2950. SPI flash 探测失败(虚焊/击穿/SPI 不通)时, 原链路会一路"假装正常":
  2951. fal 建出合法分区 -> easyflash_init 返回成功 -> 第一次读就 sfud_read(NULL,...)
  2952. 空指针解引用 -> hard fault, 而日志停在 ENV 打印之后, 跟栈溢出极难区分。
  2953. 这里在开机最前面显式判 flash 可用性: 不 ready 就完全不碰 EasyFlash,
  2954. 用 RAM 默认值降级启动, 设备照样能干活、能上报故障, 而不是变砖。 */
  2955. if(!nor_flash0_is_ready())
  2956. {
  2957. LOG_E("!! SPI FLASH NOT READY - degraded boot, params live in RAM only");
  2958. /* 三声短鸣: 现场靠声音就知道是存储故障, 不用接串口 */
  2959. Beep_Tip(200);
  2960. rt_thread_mdelay(300);
  2961. Beep_Tip(200);
  2962. rt_thread_mdelay(300);
  2963. Beep_Tip(200);
  2964. }
  2965. else if(easyflash_init()==EF_NO_ERR) //初始化成功
  2966. {
  2967. ef_ok = 1;
  2968. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  2969. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  2970. up_flag_ef = (device_status.update_flag != 0) ? 1 : 0;
  2971. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  2972. device_status.rebootcnt++;
  2973. /* ★ 原代码在这里立即 ef_set_env_blob("reboot_time") 落 flash。
  2974. 这是每次开机必然执行的一笔写,正好落在上电/复位最不稳的时刻:
  2975. 写一半被复位,该条 env 下次启动会被判成 ENV_ERR_HDR 而永久丢失;
  2976. 若这一笔又触发 GC,中断的搬移会让整个 sector 被 format,成片丢参数。
  2977. 开机计数改为随参数快照一起延迟落盘(param_mark_dirty),
  2978. 计数照样保存,但消掉了开机瞬间这个高危 flash 写窗口。 */
  2979. LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  2980. }
  2981. else {
  2982. LOG_E("easyflash init ERRPR!!!");
  2983. }
  2984. if(param_backup_init()==RT_EOK)
  2985. {
  2986. param_boot_snap_ok = (param_backup_read(&param_boot_snap, &param_boot_seq) == 0);
  2987. LOG_I("[param] boot snapshot %s, seq=%d",
  2988. param_boot_snap_ok ? "VALID" : "NONE", (int)param_boot_seq);
  2989. }
  2990. /* ★ iSave 是整机参数的唯一哨兵: 它一旦丢失, 下面这个分支会把全部参数
  2991. 刷成出厂值(现场表现为"重启后参数全没")。这里先用备份分区抢救一次,
  2992. 把"丢一条 iSave -> 全量回出厂"拦在前面。 */
  2993. if(ef_ok && (device_status.iSave != FLASH_FLAG) && param_boot_snap_ok
  2994. && (param_boot_snap.iSave == FLASH_FLAG))
  2995. {
  2996. LOG_W("[param] iSave lost in easyflash, recover from backup seq=%d", (int)param_boot_seq);
  2997. param_unpack(&param_boot_snap);
  2998. param_write_back_to_easyflash(&param_boot_snap);
  2999. param_mark_dirty();
  3000. }
  3001. //保存默认配置
  3002. if(device_status.iSave!=FLASH_FLAG)
  3003. {
  3004. device_status.iSave=FLASH_FLAG;
  3005. device_status.iFlag = 1;
  3006. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  3007. device_status.dtype = 3;
  3008. device_status.version_type = 4; //4.0
  3009. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  3010. device_status.clt_time = 6; //收集时间
  3011. device_status.heattime = 8; //加热时间
  3012. device_status.heatvalue=100; //加热温度
  3013. device_status.thsp = 300; //默认熄屏时间5分钟
  3014. device_status.sunBar = 9; //光控定时时长
  3015. device_status.startBar = 20; //定时开始时间
  3016. device_status.stopBar = 0; //定时结束时间
  3017. device_status.tph = 70; //高温保护阈值
  3018. device_status.tpl = 5; //低温保护阈值
  3019. device_status.shake_ws = 1; //震动开关,默认开
  3020. #if TG_MODULE == 0
  3021. device_status.shake_sec = 1; //震动时间,s
  3022. #else
  3023. device_status.shake_sec = 2; //震动时间,s
  3024. #endif
  3025. device_status.vol = 7; //音量
  3026. device_status.timeSwitch = 0; //工作模式:光控
  3027. device_status.passWord = 8888; //屏幕密码
  3028. device_status.dattim = 10; //数据上传间隔
  3029. device_status.ds = 1; //设备状态
  3030. /* ★ 原代码在这里无条件把 update_flag 清 0。
  3031. 走到这个分支只说明 iSave 这条 env 丢了(EasyFlash 局部损坏),
  3032. 并不代表升级请求是假的 —— 现场表现就是:
  3033. 参数丢了 -> 升级标志被顺手抹掉 -> 触发升级后重启静静跳过升级。
  3034. 这里改为保留已读到的升级请求, 交给下面的三通道统一裁决。 */
  3035. device_status.update_flag = ((up_flag_ef != 0)
  3036. || (param_boot_snap_ok && param_boot_snap.update_flag != 0)) ? 1 : 0;
  3037. /*孢子仪*/
  3038. device_status.collecting_sh1 = 9;
  3039. device_status.collecting_eh1 = 10;
  3040. device_status.collecting_sh2 = 14;
  3041. device_status.collecting_eh2 = 16;
  3042. device_status.motor_active = 0;
  3043. device_status.heat_sw = 0;
  3044. device_status.cul_time = 1;
  3045. device_status.run_sta = 0;
  3046. device_status.handle_bugger = 0; //默认排虫
  3047. device_status.work_mode = 0;
  3048. sprintf(device_status.driver, "%s",VERSION_HK);
  3049. sprintf(device_status.deviceId,"0");
  3050. rt_sprintf(device_status.tempture, "-15");
  3051. rt_sprintf(device_status.humidity, "31");
  3052. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  3053. rt_sprintf(device_status.alarm_tg,"220000");
  3054. /* ★ 以下 32 连写只在 EasyFlash 可用时执行。
  3055. flash 坏了硬写只会拿到一堆 EF_WRITE_ERR, 还可能触发 EasyFlash 内部 GC
  3056. (GC 中途失败会 format 掉整个 sector)。默认值已经写进 RAM 了,
  3057. 设备用出厂参数降级运行即可。 (内部未重新缩进, 保持与原代码逐行对齐) */
  3058. if (ef_ok)
  3059. {
  3060. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  3061. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  3062. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  3063. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  3064. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  3065. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  3066. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  3067. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  3068. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3069. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  3070. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  3071. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  3072. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  3073. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  3074. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  3075. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  3076. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  3077. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  3078. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  3079. ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
  3080. ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  3081. ef_set_env_blob("motor_active",&device_status.motor_active,sizeof(device_status.motor_active)); //制冷开关
  3082. ef_set_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //加热开关
  3083. ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
  3084. ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
  3085. ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
  3086. ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
  3087. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
  3088. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  3089. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  3090. ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
  3091. ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
  3092. } /* if (ef_ok) —— flash 不可用时整段跳过 */
  3093. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  3094. LOG_I("Default paramconf is:");
  3095. LOG_I("stime:%d",device_status.iSave);
  3096. LOG_I("stime:%d",device_status.clt_time);
  3097. LOG_I("startBar:%d",device_status.startBar);
  3098. LOG_I("stopBar:%d",device_status.stopBar);
  3099. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  3100. LOG_I("sunBar:%d",device_status.sunBar);
  3101. LOG_I("jtime:%d",device_status.heattime);
  3102. LOG_I("jheat:%d",device_status.heatvalue);
  3103. LOG_I("thsp:%d", device_status.thsp);
  3104. LOG_I("passwd:%d",device_status.passWord);
  3105. LOG_I("alarm_tg:%s",device_status.alarm_tg);
  3106. }//从easyflash读取参数配置
  3107. else if (device_status.iSave==FLASH_FLAG) {
  3108. device_status.iFlag = 0;
  3109. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  3110. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  3111. ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
  3112. ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
  3113. ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
  3114. ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
  3115. ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
  3116. ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
  3117. /* heatvalue 这一个字段承载了两种语义, 写入侧也分成了两个 key:
  3118. 测报灯(dtype!=7): 加热温度 -> key "jheat" (hmi.c 系统设置页)
  3119. 孢子仪(dtype==7): 培养温度 -> key "heatvalue" (hmi.c 孢子仪系统设置)
  3120. 老代码开机一律读 jheat, 孢子仪设好的培养温度重启后会被 jheat 的值顶掉,
  3121. 现场表现就是"温度设了没保存住"。这里按 dtype 分开读;
  3122. 老设备 flash 里还没有 heatvalue 这个 key 时回退读 jheat, 避免读到 0。 */
  3123. if (device_status.dtype == 7) {
  3124. if (ef_get_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL) == 0) {
  3125. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //兼容老固件
  3126. }
  3127. } else {
  3128. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
  3129. }
  3130. ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
  3131. ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
  3132. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  3133. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  3134. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  3135. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  3136. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  3137. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  3138. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  3139. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  3140. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  3141. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  3142. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  3143. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  3144. /* json_w_clock/json_w_reset/json_w_platform 已无对应全局字段,开机不再回读 */
  3145. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  3146. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  3147. ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //平台地址
  3148. /*孢子仪*/
  3149. ef_get_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time),NULL); //培养时间
  3150. ef_get_env_blob("motor_active",&device_status.motor_active,sizeof(device_status.motor_active),NULL); //电机
  3151. ef_get_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw),NULL); //加热开关
  3152. /* 制冷开关: 孢子仪设置页(hmi.c 系统设置, dtype==7)只写了这个 key,
  3153. 老代码开机却从来不读 —— 设置完一重启就丢。这里补上读取。
  3154. flash 里没有该 key 时 ef_get_env_blob 返回 0 且不清零 buf, 保持原值即可。 */
  3155. ef_get_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw),NULL); //制冷开关
  3156. ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
  3157. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  3158. ef_get_env_blob("deviceId",&device_status.deviceId,sizeof(device_status.deviceId),NULL); //设备编码
  3159. ef_get_env_blob("handle_bugger",&device_status.handle_bugger,sizeof(device_status.handle_bugger),NULL); //排虫
  3160. ef_get_env_blob("work_mode",&device_status.work_mode,sizeof(device_status.work_mode),NULL); //工作模式
  3161. ef_get_env_blob("alarm_tg",&device_status.alarm_tg,sizeof(device_status.alarm_tg),NULL); //闹钟 //光控计数
  3162. device_status.work_done = 0;
  3163. /* ===== 参数健康自检 + 双分区恢复 =====
  3164. EasyFlash 读出来的结果先体检; 只要有关键项为 0(说明对应 env 已损坏/丢失),
  3165. 就用备份分区的快照整包恢复, 并回写 EasyFlash 修好 flash 侧的坏条目。
  3166. 这样"丢参数"只会被修复一次, 不会像原来那样每重启都丢一遍。 */
  3167. {
  3168. /* ★ 同 param_boot_snap: 820 字节必须走静态区, 不能放 main 线程栈上 */
  3169. static param_persist_t cur;
  3170. int perr;
  3171. param_pack(&cur);
  3172. perr = param_check_health(&cur);
  3173. if(perr != 0)
  3174. {
  3175. LOG_E("[param] easyflash param BROKEN, err=0x%02x", perr);
  3176. if(param_boot_snap_ok && (param_check_health(&param_boot_snap) == 0))
  3177. {
  3178. uint32_t rc = device_status.rebootcnt; /* 保留本次开机累加 */
  3179. param_unpack(&param_boot_snap);
  3180. if(rc > device_status.rebootcnt)
  3181. {
  3182. device_status.rebootcnt = rc;
  3183. }
  3184. LOG_W("[param] restored from backup seq=%d, write back to easyflash", (int)param_boot_seq);
  3185. param_write_back_to_easyflash(&param_boot_snap);
  3186. }
  3187. else
  3188. {
  3189. LOG_E("[param] no usable backup(seq=%d), fall back to factory patch", (int)param_boot_seq);
  3190. }
  3191. }
  3192. else if(param_boot_snap_ok && (param_boot_snap.rebootcnt > device_status.rebootcnt))
  3193. {
  3194. /* 两侧都正常时, 开机次数取较大者, 防止 reboot_time 这条 env 单独丢失 */
  3195. device_status.rebootcnt = param_boot_snap.rebootcnt;
  3196. }
  3197. /* 无论走哪条路, 都让快照侧保持最新 */
  3198. param_mark_dirty();
  3199. }
  3200. //丢参数的处理
  3201. if(device_status.sunBar==0) device_status.sunBar = 4;
  3202. if(device_status.dattim==0) device_status.dattim = 10;
  3203. if(device_status.thsp==0) device_status.thsp = 300;
  3204. if(device_status.clt_time==0) device_status.clt_time = 10;
  3205. if(device_status.heattime==0) device_status.heattime = 10;
  3206. if(device_status.heatvalue==0) device_status.heatvalue = 100;
  3207. if(device_status.tph==0){ //参数丢失
  3208. device_status.tpl = 5;
  3209. device_status.tph = 70;
  3210. device_status.ds = 1;
  3211. device_status.startBar = 20;
  3212. device_status.stopBar = 0;
  3213. }
  3214. if(device_status.work_mode>1) device_status.work_mode = 0;
  3215. /* ★ 上面这段补丁原来只改 RAM 不落盘, 所以每次重启都要重复丢、重复修。
  3216. 现在补一个 dirty 标记, 修正后的值会进备份分区, 下次开机再回写 EasyFlash,
  3217. 把 flash 侧的错误值彻底修掉。 */
  3218. param_mark_dirty();
  3219. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  3220. if(device_status.dtype!=7)
  3221. {
  3222. device_status.dtype = 3;
  3223. }
  3224. else {
  3225. device_status.version_type = 7;
  3226. }
  3227. if(device_status.version_type==0)
  3228. {
  3229. device_status.version_type = 4;
  3230. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
  3231. }
  3232. else if(device_status.version_type==1)
  3233. {
  3234. rt_sprintf(device_status.driver, "%s",VERSION_ZP);
  3235. }
  3236. else if (device_status.version_type==11)
  3237. {
  3238. rt_sprintf(device_status.driver, "%s",VERSION_GK);
  3239. }
  3240. else if (device_status.version_type==4||device_status.version_type==42||device_status.version_type==43)
  3241. {
  3242. rt_sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  3243. }
  3244. else if (device_status.version_type==41) {
  3245. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  3246. }
  3247. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  3248. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  3249. LOG_I("开机次数:%d",device_status.rebootcnt);
  3250. LOG_I("收集时间:%d",device_status.clt_time);
  3251. LOG_I("定时开始时间:%d",device_status.startBar);
  3252. LOG_I("定时结束时间:%d",device_status.stopBar);
  3253. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  3254. LOG_I("光控定时时长:%d",device_status.sunBar);
  3255. LOG_I("加热开关:%d",device_status.heat_sw);
  3256. LOG_I("加热时间:%d",device_status.heattime);
  3257. LOG_I("加热温度:%d",device_status.heatvalue);
  3258. LOG_I("震动开关:%d",device_status.shake_ws);
  3259. LOG_I("震动时间:%d",device_status.shake_sec);
  3260. LOG_I("高温保护:%d",device_status.tph);
  3261. LOG_I("低温保护:%d",device_status.tpl);
  3262. LOG_I("thsp:%d", device_status.thsp);
  3263. LOG_I("密码:%d",device_status.passWord);
  3264. LOG_I("数据上传间隔:%d",device_status.dattim);
  3265. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  3266. LOG_I("上传平台:%s",device_status.mqttcfg1);
  3267. LOG_I("孢子仪运行状态:%d",device_status.run_sta);
  3268. LOG_I("孢子仪采集时间:[%d-%d,%d-%d]",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2);
  3269. LOG_I("PowerType:%d",device_status.Power_Type);
  3270. LOG_I("deviceId:%s",device_status.deviceId);
  3271. LOG_I("handle_bugger:%d",device_status.handle_bugger);
  3272. LOG_I("alarm_tg:%s",device_status.alarm_tg);
  3273. LOG_I("motor_active:%d",device_status.motor_active);
  3274. }
  3275. /* 启动参数延迟落盘线程: 之后所有 param_mark_dirty() 的变更,
  3276. 会在静默 500ms 后合并成一次 4KB 擦写写入备份分区,
  3277. 把"改一条参数擦一次 flash"变成"一批改动擦一次"。 */
  3278. param_backup_start();
  3279. {
  3280. device_status.update_flag = 0;
  3281. //初始化联网状态
  3282. rt_sprintf(device_status.net,"1");
  3283. rt_sprintf(device_status.csq,"0");
  3284. rt_sprintf(device_status.sim,"1");
  3285. /* Sys_Motor_Task() 已取消(同步 master):sys_monitor 后台线程删除,
  3286. 统计改为 sys_monitor_refresh() 现场采集,见 sys_monitor.c。 */
  3287. ctr_router(ON);
  3288. #if JINGJIKUAN == 1
  3289. ctr_Cold(ON); //球阀电机电源
  3290. ctr_Collect_Fan(ON); //雨控解除
  3291. #endif
  3292. Hmi_Fresh();
  3293. Control_clear();
  3294. Control_Check(); //自检
  3295. dtu_data_play();
  3296. sht30_collect();
  3297. #if SENSOR_EN==1
  3298. mb_master_samlpe();
  3299. #endif
  3300. net_wait_thread_entry();
  3301. dtu_send_data_start();
  3302. Workstatus_check();
  3303. Work_Loop_Task();
  3304. }
  3305. }
  3306. /* ★ P2-14: 开机等联网的独立线程。
  3307. 原来这两个 while 直接写在 main 线程(Check_ParamConfig)里, 最长阻塞 180s + 10s:
  3308. while(atoi(net)==1) { ... rt_thread_delay(1000) ... } // 最多 180 次 = 180s
  3309. while(atoi(net)==0) { ... rt_thread_delay(2000) ... } // 最多 6 次 = 12s
  3310. 期间 main 线程后面的 dtu_send_data_start() / Workstatus_check() / Work_Loop_Task()
  3311. 全部延后 —— 现场表现是"开机后近 3 分钟内不自检、不进工作循环、不上报"。
  3312. 现在把等待搬到本线程, main 立刻往下走。
  3313. ⚠️ 本线程里所有用到 tm 的地方都用本函数自己的时间快照, 不再借用
  3314. Check_ParamConfig() 的 static struct tm(那里开机后还会被别处改写)。 */
  3315. static void net_wait_thread_entry(void)
  3316. {
  3317. uint16_t connect_num = 0;
  3318. time_t nnow = time(RT_NULL);
  3319. static struct tm ntm;
  3320. gmtime_r(&nnow, &ntm);
  3321. /* 等待联网 */
  3322. while (atoi(device_status.net) == 1)
  3323. {
  3324. char *readinfo = makeJson_ReadInfo();
  3325. if (readinfo != RT_NULL)
  3326. {
  3327. DTU_Write("%s", readinfo);
  3328. rt_free(readinfo);
  3329. }
  3330. rt_thread_delay(2000);
  3331. connect_num++;
  3332. LOG_I("联网中......%d", connect_num);
  3333. /* 超时时间为 3 分钟 */
  3334. #if MATCH_DEVICE == 1
  3335. if (connect_num == 60) /* 180 */
  3336. #else
  3337. if (connect_num == 90) /* 180 */
  3338. #endif
  3339. {
  3340. device_status.timeSwitch = 0;
  3341. LOG_W("Time not set:%04d-%02d-%02d %02d:%02d:%02d",
  3342. ntm.tm_year + 1900, ntm.tm_mon + 1, ntm.tm_mday,
  3343. ntm.tm_hour, ntm.tm_min, ntm.tm_sec);
  3344. #if 0
  3345. set_date(2020, 1, 1);
  3346. set_time(0, 0, 0);
  3347. #endif
  3348. break;
  3349. }
  3350. }
  3351. connect_num = 0;
  3352. rt_thread_delay(2000);
  3353. #if MATCH_DEVICE == 1
  3354. device_status.timeSwitch = 1;
  3355. #endif
  3356. /* ★ 原来 makeJson_Warn() 的返回值从未释放 -> 每次开机都漏一块堆 */
  3357. dtu_write_json(makeJson_Warn("turnon", "device_stm32"));
  3358. rt_thread_delay(1000);
  3359. /* 本线程退出(联网成功或 3 分钟超时): 交还 read 刷新职责给 refresh_hmi_Component() */
  3360. net_wait_running = 1;
  3361. LOG_I("net wait thread exit");
  3362. }
  3363. void ResetParamDefault(void)
  3364. {
  3365. #if MATCH_DEVICE == 1
  3366. rt_sprintf(device_status.tempture,"%d",30);
  3367. #else
  3368. rt_sprintf(device_status.tempture,"%d",-15);
  3369. #endif
  3370. device_status.ds = 1; //设备状态开机
  3371. device_status.thsp = 300;
  3372. device_status.passWord = 8888;
  3373. device_status.dattim = 10;
  3374. device_status.collecting_sh1 = 9;
  3375. device_status.collecting_eh1 = 10;
  3376. device_status.collecting_sh2 = 14;
  3377. device_status.collecting_eh2 = 16;
  3378. device_status.Power_Type = 0;
  3379. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  3380. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  3381. if(device_status.dtype==3)
  3382. {
  3383. device_status.clt_time = 6; //收集时间
  3384. device_status.startBar = 20; //定时开始时间
  3385. device_status.stopBar = 5; //定时结束时间
  3386. device_status.heattime = 8; //加热时间
  3387. device_status.shake_ws = 1; //针对开关
  3388. #if TG_MODULE == 0
  3389. device_status.shake_sec = 1; //震动时间,s
  3390. #else
  3391. device_status.shake_sec = 2; //震动时间,s
  3392. #endif
  3393. device_status.sunBar = 8;
  3394. device_status.tph = 70; //高温保护阈值
  3395. device_status.tpl = -5; //低温保护阈值
  3396. device_status.timeSwitch = 0; //定时模式:光控
  3397. device_status.heatvalue=100; //加热温度
  3398. device_status.heat_sw = 1;
  3399. ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
  3400. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  3401. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  3402. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  3403. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  3404. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  3405. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  3406. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3407. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  3408. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  3409. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  3410. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  3411. device_status.handle_bugger = 0;
  3412. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  3413. }
  3414. else if(device_status.dtype==7)
  3415. {
  3416. device_status.run_sta = 0;
  3417. device_status.heatvalue = 25; //培养温度
  3418. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  3419. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  3420. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  3421. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  3422. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  3423. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3424. }
  3425. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  3426. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  3427. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  3428. /* 恢复出厂是一次 24 连写, 最容易被打断导致只写一半。
  3429. 这里同步落一份整包快照到备份分区, 保证出厂值也有可信副本。
  3430. 用 force 版本跳过节流, 保证立刻生效。 */
  3431. param_mark_dirty();
  3432. param_flush_force();
  3433. HMI_WRITE("play %d,%d,%d",1,5,0);
  3434. DTU_Write("reset.mp3");
  3435. rt_thread_delay(1000);
  3436. }
  3437. void ResetParamDefault_DC(void)
  3438. {
  3439. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  3440. rt_sprintf(device_status.tempture,"%d",-15);
  3441. device_status.clt_time = 6; //收集时间
  3442. device_status.dattim = 10; //数据上传间隔
  3443. device_status.startBar = 20; //定时开始时间
  3444. device_status.stopBar = 0; //定时结束时间
  3445. device_status.heattime = 8; //加热时间
  3446. device_status.shake_ws = 1; //针对开关
  3447. #if TG_MODULE == 0
  3448. device_status.shake_sec = 1; //震动时间,s
  3449. #else
  3450. device_status.shake_sec = 2; //震动时间,s
  3451. #endif
  3452. device_status.sunBar = 4;
  3453. device_status.tph = 70; //高温保护阈值
  3454. device_status.tpl = -5; //低温保护阈值
  3455. device_status.timeSwitch = 0; //定时模式
  3456. device_status.heatvalue=100;
  3457. device_status.ds = 1; //设备状态开机
  3458. device_status.thsp = 300;
  3459. device_status.passWord = 8888;
  3460. device_status.timeSwitch = 0;
  3461. device_status.Power_Type = 1;
  3462. device_status.handle_bugger = 0;
  3463. device_status.heat_sw = 0;
  3464. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  3465. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  3466. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  3467. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  3468. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  3469. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  3470. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  3471. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  3472. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  3473. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  3474. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3475. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  3476. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  3477. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  3478. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  3479. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  3480. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  3481. ef_set_env_blob("heat_sw", &device_status.heat_sw,sizeof(device_status.heat_sw));
  3482. /* 同上, 18 连写后同步一份整包快照(force 立即生效) */
  3483. param_mark_dirty();
  3484. param_flush_force();
  3485. DTU_Write("reset.mp3");
  3486. rt_thread_delay(1000);
  3487. }
  3488. //固件备份
  3489. void app_partion_backup(void)
  3490. {
  3491. rt_kprintf("固件恢复!!!\n");
  3492. const char *dst_part_name = "df_area";
  3493. const char *src_part_name = "app";
  3494. const struct fal_partition *dst_part;
  3495. const struct fal_partition *src_part;
  3496. /* ★ FAL 没建表成功时 fal_partition_find() 会撞 assert(init_ok) 死循环,
  3497. 先查 fal_init_check() 门控。 */
  3498. if (fal_init_check() != 1)
  3499. {
  3500. rt_kprintf("FAL is NOT initialized, partition backup aborted!\n");
  3501. return;
  3502. }
  3503. dst_part = fal_partition_find(dst_part_name);
  3504. src_part = fal_partition_find(src_part_name);
  3505. if (dst_part == RT_NULL || src_part == RT_NULL)
  3506. {
  3507. if (dst_part == RT_NULL)
  3508. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  3509. if (src_part == RT_NULL)
  3510. rt_kprintf("%s partition is not exist!\n", src_part_name);
  3511. }
  3512. else
  3513. {
  3514. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  3515. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  3516. {
  3517. int clone_pos = 0;
  3518. int clone_len = 0, clone_tol_len;
  3519. rt_uint8_t *buf = rt_malloc(4096);
  3520. if (dst_part->len < src_part->len)
  3521. clone_tol_len = dst_part->len;
  3522. else
  3523. clone_tol_len = src_part->len;
  3524. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  3525. {
  3526. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  3527. if (clone_len < 0)
  3528. {
  3529. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  3530. break;
  3531. }
  3532. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  3533. {
  3534. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  3535. break;
  3536. }
  3537. HMI_WRITE("#");
  3538. rt_kprintf("#");
  3539. clone_pos += clone_len;
  3540. }
  3541. if (clone_pos >= clone_tol_len)
  3542. {
  3543. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  3544. }
  3545. else
  3546. {
  3547. rt_kprintf("Clone partition failed!");
  3548. }
  3549. if (buf)
  3550. rt_free(buf);
  3551. }
  3552. }
  3553. }
  3554. void Debug_log_ON(void)
  3555. {
  3556. rt_console_set_device("uart4");
  3557. finsh_set_device("uart4");
  3558. }
  3559. void Debug_log_OFF(void)
  3560. {
  3561. rt_console_set_device("uart1");
  3562. finsh_set_device("uart1");
  3563. /* ★ 原来这里无条件再 rt_mq_init() 一次 + 再建一个 "Hmi_Fresh_thread":
  3564. ① 对已初始化的消息队列重复 init 会重置其等待链表/读写指针(返回 -RT_ERROR),
  3565. 正在排队的屏幕指令直接丢;
  3566. ② 线程重复创建后同一消息队列有两个消费者, 每条屏幕指令会被处理两遍
  3567. (reboot / 写 Flash 等动作执行两次), 且每次泄漏一份线程栈。
  3568. 消息队列与接收回调在 INIT_BOARD 阶段已建立, 这里只需在升级结束线程退出后
  3569. 重建(rt_thread_find 为空), 否则保持现状。 */
  3570. if (serial != RT_NULL)
  3571. {
  3572. rt_device_set_rx_indicate(serial, uart_input);
  3573. }
  3574. if (rt_thread_find("Hmi_Fresh_thread") == RT_NULL)
  3575. {
  3576. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  3577. Hmi_thread_entry,
  3578. RT_NULL,
  3579. HMI_THREAD_STACK_SIZE,
  3580. HMI_THREAD_PRIORITY,
  3581. HMI_THREAD_TIMESLICE);
  3582. if (hmi1 != RT_NULL)
  3583. {
  3584. rt_thread_startup(hmi1);
  3585. }
  3586. }
  3587. }
  3588. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  3589. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  3590. //
  3591. INIT_BOARD_EXPORT(HMI_uart4_init);