protocol.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  1. /**
  2. * @file protocol.c
  3. * @brief this file contains protocol analysis and construct response function when received zigbee module send message
  4. * @author qinlang
  5. * @date 2022.05.06
  6. * @par email:qinlang.chen@tuya.com
  7. * @copyright HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
  8. * @par company
  9. * http://www.tuya.com
  10. */
  11. #include <REGtenxTM52F1386.h>
  12. #include "TM52F1386_bsp.h"
  13. #include <intrins.h>
  14. #include "zigbee.h"
  15. #include "uart.h"
  16. #include "GLOBAL.h"
  17. #include "gpio_ctrl.h"
  18. #include "EEPROM.h"
  19. #include <string.h>
  20. #include "iap.h"
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. unsigned char string_H16_data[32]; // 16进制字符串
  24. unsigned char hex_data[32]; // 转换后的16进制字节数据
  25. /******************************************************************************
  26. 移植须知:
  27. 1:MCU必须在while中直接调用mcu_api.c内的zigbee_uart_service()函数
  28. 2:程序正常初始化完成后,建议不进行关串口中断,如必须关中断,关中断时间必须短,关中断会引起串口数据包丢失
  29. 3:请勿在中断/定时器中断内调用上报函数
  30. ******************************************************************************/
  31. /******************************************************************************
  32. 第一步:初始化
  33. 1:在需要使用到zigbee相关文件的文件中include "zigbee.h"
  34. 2:在MCU初始化中调用mcu_api.c文件中的zigbee_protocol_init()函数
  35. 3:将MCU串口单字节发送函数填入protocol.c文件中uart_transmit_output函数内,并删除#error
  36. 4:在MCU串口接收函数中调用mcu_api.c文件内的uart_receive_input函数,并将接收到的字节作为参数传入
  37. 5:单片机进入while循环后调用mcu_api.c文件内的zigbee_uart_service()函数
  38. 6:mcu何时开启zigbee模块配网,当zigbee模块查询到mcu的产品信息之后,或者zigbee模块发送了当前网络状态
  39. 给到mcu,查询pid信息会在上电5秒之后发送,对接部分zigbee老版本通用对接固件低功耗版本尤其需要注意不要
  40. 在第一次确认波特率的时候提前唤醒或者发送串口给zigbee模组,有概率导致zigbee模组波特率存储失败。
  41. 7:上电之后,mcu何时上报数据给网关合适,在收到网络状态为已配网,或者接收到读取dp数据的时候。
  42. 8: 在mcu ota过程中发送固件内容请求未收到zigbee模块的应答时,需要mcu端做好超时重新请求的处理,和存储image
  43. 处理,校验和可以自行设计或者使用参考。
  44. ******************************************************************************/
  45. /******************************************************************************
  46. 1:dp数据点序列类型对照表
  47. **此为自动生成代码,如在开发平台有相关修改请重新下载MCU_SDK**
  48. ******************************************************************************/
  49. ///> dp data list, this will be generated by cloud platform
  50. #ifdef ZIGBEE_ZTU_T3_SW
  51. const DOWNLOAD_CMD_S download_cmd[] =
  52. {
  53. {DPID_SWITCH_1, DP_TYPE_BOOL},
  54. {DPID_SWITCH_2, DP_TYPE_BOOL},
  55. {DPID_SWITCH_3, DP_TYPE_BOOL},
  56. {DPID_RELAY_STATUS, DP_TYPE_ENUM},
  57. {DPID_LIGHT_MODE, DP_TYPE_ENUM},
  58. {DPID_BACKLIGHT_SWITCH, DP_TYPE_BOOL},
  59. {DPID_SWITCH_INCHING, DP_TYPE_STRING},
  60. {DPID_RELAY_STATUS_1, DP_TYPE_ENUM},
  61. {DPID_RELAY_STATUS_2, DP_TYPE_ENUM},
  62. {DPID_RELAY_STATUS_3, DP_TYPE_ENUM},
  63. {DPID_ADD_ELE, DP_TYPE_VALUE},
  64. {DPID_CUR_CURRENT, DP_TYPE_VALUE},
  65. {DPID_CUR_POWER, DP_TYPE_VALUE},
  66. {DPID_CUR_VOLTAGE, DP_TYPE_VALUE},
  67. };
  68. #endif
  69. #ifdef ZIGBEE_ZTU_T2_SW
  70. const DOWNLOAD_CMD_S download_cmd[] =
  71. {
  72. {DPID_SWITCH_1, DP_TYPE_BOOL},
  73. {DPID_SWITCH_3, DP_TYPE_BOOL},
  74. {DPID_RELAY_STATUS, DP_TYPE_ENUM},
  75. {DPID_LIGHT_MODE, DP_TYPE_ENUM},
  76. {DPID_BACKLIGHT_SWITCH, DP_TYPE_BOOL},
  77. {DPID_SWITCH_INCHING, DP_TYPE_STRING},
  78. {DPID_RELAY_STATUS_1, DP_TYPE_ENUM},
  79. {DPID_RELAY_STATUS_3, DP_TYPE_ENUM},
  80. };
  81. #endif
  82. #ifdef ZIGBEE_ZTU_T1_SW
  83. const DOWNLOAD_CMD_S download_cmd[] =
  84. {
  85. {DPID_SWITCH_2, DP_TYPE_BOOL},
  86. {DPID_RELAY_STATUS, DP_TYPE_ENUM},
  87. {DPID_LIGHT_MODE, DP_TYPE_ENUM},
  88. {DPID_BACKLIGHT_SWITCH, DP_TYPE_BOOL},
  89. {DPID_SWITCH_INCHING, DP_TYPE_STRING},
  90. {DPID_RELAY_STATUS_2, DP_TYPE_ENUM},
  91. };
  92. #endif
  93. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  94. const DOWNLOAD_CMD_S download_cmd[] =
  95. {
  96. {DPID_SCENE_1, DP_TYPE_ENUM},
  97. {DPID_SCENE_2, DP_TYPE_ENUM},
  98. {DPID_SCENE_3, DP_TYPE_ENUM},
  99. {DPID_SCENE_4, DP_TYPE_ENUM},
  100. {DPID_SCENE_5, DP_TYPE_ENUM},
  101. {DPID_SCENE_6, DP_TYPE_ENUM},
  102. {DPID_MODE_1, DP_TYPE_ENUM},
  103. {DPID_MODE_2, DP_TYPE_ENUM},
  104. {DPID_MODE_3, DP_TYPE_ENUM},
  105. {DPID_SWITCH_1, DP_TYPE_BOOL},
  106. {DPID_SWITCH_2, DP_TYPE_BOOL},
  107. {DPID_SWITCH_3, DP_TYPE_BOOL},
  108. {DPID_BACKLIGHT_SWITCH, DP_TYPE_BOOL},
  109. {DPID_LIGHT_MODE, DP_TYPE_ENUM},
  110. {DPID_RELAY_STATUS, DP_TYPE_ENUM},
  111. {DPID_RELAY_STATUS_1, DP_TYPE_ENUM},
  112. {DPID_RELAY_STATUS_2, DP_TYPE_ENUM},
  113. {DPID_RELAY_STATUS_3, DP_TYPE_ENUM},
  114. {DPID_SWITCH_INCHING, DP_TYPE_STRING},
  115. };
  116. #endif
  117. /******************************************************************************
  118. 2:串口单字节发送函数
  119. 请将MCU串口发送函数填入该函数内,并将接收到的数据作为参数传入串口发送函数
  120. ******************************************************************************/
  121. static void report_mcu_ota_result(unsigned char res);
  122. /**
  123. * @brief encapsulates a generic send function, developer should use their own function to completing this fuction
  124. * @param[in] {value} send signle data
  125. * @return void
  126. */
  127. ////must modify
  128. void uart_transmit_output(unsigned char value)
  129. {
  130. // static unsigned char buf;
  131. // buf = value;
  132. // #error "please use your own uart send fuction complete this fuction, exmaple"
  133. /*
  134. * //demo:
  135. * extern void Uart_PutChar(unsigned char value);
  136. * Uart_PutChar(value);
  137. */
  138. UART0_send(value);
  139. }
  140. /******************************************************************************
  141. 第二步:实现具体用户函数
  142. 1:APP下发数据处理
  143. 2:数据上报处理
  144. ******************************************************************************/
  145. /******************************************************************************
  146. 1:所有数据上报处理
  147. 当前函数处理全部数据上报(包括可下发/可上报和只上报)
  148. 需要用户按照实际情况实现:
  149. 1:需要实现可下发/可上报数据点上报
  150. 2:需要实现只上报数据点上报
  151. 此函数为MCU内部必须调用
  152. 用户也可调用此函数实现全部数据上报
  153. ******************************************************************************/
  154. // 自动化生成数据上报函数
  155. /**
  156. * @brief Upload all dp information of the system, and realize the synchronization of APP and muc data
  157. * @param[in] {void}
  158. * @return void
  159. */
  160. #ifdef ZIGBEE_ZTU_T1_SW
  161. // #ifdef ZIGBEE_ZTU_T3_SW
  162. void all_data_update(void)
  163. {
  164. // #error "mcu must realize function internal function"
  165. /*
  166. * these fucntion will be generated by cloud platform, but when use these function must reference the function in mcu_api.c
  167. */
  168. u08 i = 0;
  169. mcu_dp_bool_update(DPID_SWITCH_2, switchState.SWITCH[1]); // BOOL型数据上报;
  170. mcu_dp_bool_update(DPID_SWITCH_1, switchState.SWITCH[0]); // BOOL型数据上报;
  171. mcu_dp_bool_update(DPID_BACKLIGHT_SWITCH, switchState.human); // BOOL型数据上报;
  172. mcu_dp_enum_update(DPID_RELAY_STATUS_1, switchState.relay_status[0]); // 枚举型数据上报;
  173. mcu_dp_enum_update(DPID_RELAY_STATUS_2, switchState.relay_status[1]); // 枚举型数据上报;
  174. mcu_dp_enum_update(DPID_LIGHT_MODE, switchState.led_level); // 枚举型数据上报;
  175. mcu_dp_enum_update(DPID_RELAY_STATUS, switchState.relay_allstatus); // 枚举型数据上报;
  176. /*
  177. mcu_dp_string_update(DPID_SWITCH_INCHING,当前点动开关(延时关)指针,当前点动开关(延时关)数据长度); //STRING型数据上报;
  178. */
  179. for (i = 0; i < 3; i++)
  180. {
  181. hex_data[i * 3] = 0xff;
  182. hex_data[i * 3 + 1] = 0xff;
  183. hex_data[i * 3 + 2] = 0xff;
  184. }
  185. hex_data[0 * 3] = switchState.plus_mod[1];
  186. hex_data[0 * 3 + 1] = (u08)((switchState.plus_auto_time[1] / 10) >> 8);
  187. hex_data[0 * 3 + 2] = (u08)((switchState.plus_auto_time[1] / 10) & 0x00ff); // 实际2号键做1号开关用
  188. HexToDisHex(string_H16_data, hex_data, 9); // 转回16进制字符串
  189. mcu_dp_string_update(DPID_SWITCH_INCHING, string_H16_data, 18); // 点动开关(延时关)STRING型数据上报;
  190. }
  191. #endif
  192. #ifdef ZIGBEE_ZTU_T2_SW
  193. // #ifdef ZIGBEE_ZTU_T3_SW
  194. void all_data_update(void)
  195. {
  196. // #error "mcu must realize function internal function"
  197. /*
  198. * these fucntion will be generated by cloud platform, but when use these function must reference the function in mcu_api.c
  199. */
  200. u08 i = 0;
  201. mcu_dp_bool_update(DPID_SWITCH_3, switchState.SWITCH[2]); // BOOL型数据上报;
  202. mcu_dp_bool_update(DPID_SWITCH_2, switchState.SWITCH[1]); // BOOL型数据上报;
  203. mcu_dp_bool_update(DPID_SWITCH_1, switchState.SWITCH[0]); // BOOL型数据上报;
  204. mcu_dp_bool_update(DPID_BACKLIGHT_SWITCH, switchState.human); // BOOL型数据上报;
  205. mcu_dp_enum_update(DPID_RELAY_STATUS_1, switchState.relay_status[0]); // 枚举型数据上报;
  206. mcu_dp_enum_update(DPID_RELAY_STATUS_2, switchState.relay_status[1]); // 枚举型数据上报;
  207. mcu_dp_enum_update(DPID_RELAY_STATUS_3, switchState.relay_status[2]); // 枚举型数据上报;
  208. mcu_dp_enum_update(DPID_LIGHT_MODE, switchState.led_level); // 枚举型数据上报;
  209. mcu_dp_enum_update(DPID_RELAY_STATUS, switchState.relay_allstatus); // 枚举型数据上报;
  210. /*
  211. mcu_dp_string_update(DPID_SWITCH_INCHING,当前点动开关(延时关)指针,当前点动开关(延时关)数据长度); //STRING型数据上报;
  212. */
  213. for (i = 0; i < 3; i++)
  214. {
  215. hex_data[i * 3] = 0xff;
  216. hex_data[i * 3 + 1] = 0xff;
  217. hex_data[i * 3 + 2] = 0xff;
  218. }
  219. hex_data[0 * 3] = switchState.plus_mod[0];
  220. hex_data[0 * 3 + 1] = (u08)((switchState.plus_auto_time[0] / 10) >> 8);
  221. hex_data[0 * 3 + 2] = (u08)((switchState.plus_auto_time[0] / 10) & 0x00ff); // 实际3号键做2号开关用
  222. hex_data[1 * 3] = switchState.plus_mod[2];
  223. hex_data[1 * 3 + 1] = (u08)((switchState.plus_auto_time[2] / 10) >> 8);
  224. hex_data[1 * 3 + 2] = (u08)((switchState.plus_auto_time[2] / 10) & 0x00ff); // 实际3号键做2号开关用
  225. HexToDisHex(string_H16_data, hex_data, 9); // 转回16进制字符串
  226. mcu_dp_string_update(DPID_SWITCH_INCHING, string_H16_data, 18); // 点动开关(延时关)STRING型数据上报;
  227. }
  228. #endif
  229. // #if (defined ZIGBEE_ZTU_T3_SW) || (defined ZIGBEE_ZTU_T2_SW) || (defined ZIGBEE_ZTU_T1_SW)
  230. #ifdef ZIGBEE_ZTU_T3_SW
  231. void all_data_update(void)
  232. {
  233. // #error "mcu must realize function internal function"
  234. /*
  235. * these fucntion will be generated by cloud platform, but when use these function must reference the function in mcu_api.c
  236. */
  237. u08 i = 0;
  238. mcu_dp_bool_update(DPID_SWITCH_3, switchState.SWITCH[2]); // BOOL型数据上报;
  239. mcu_dp_bool_update(DPID_SWITCH_2, switchState.SWITCH[1]); // BOOL型数据上报;
  240. mcu_dp_bool_update(DPID_SWITCH_1, switchState.SWITCH[0]); // BOOL型数据上报;
  241. mcu_dp_bool_update(DPID_BACKLIGHT_SWITCH, switchState.human); // BOOL型数据上报;
  242. mcu_dp_enum_update(DPID_RELAY_STATUS_1, switchState.relay_status[0]); // 枚举型数据上报;
  243. mcu_dp_enum_update(DPID_RELAY_STATUS_2, switchState.relay_status[1]); // 枚举型数据上报;
  244. mcu_dp_enum_update(DPID_RELAY_STATUS_3, switchState.relay_status[2]); // 枚举型数据上报;
  245. mcu_dp_enum_update(DPID_LIGHT_MODE, switchState.led_level); // 枚举型数据上报;
  246. mcu_dp_enum_update(DPID_RELAY_STATUS, switchState.relay_allstatus); // 枚举型数据上报;
  247. //test
  248. // switchState.dp_add_ele = 100*100;//100 kW·h
  249. // switchState.dp_cur_current = 1000;//1000mA
  250. // switchState.dp_cur_power = 330*10;//330w
  251. // switchState.dp_cur_voltage = 220*10;//220v
  252. mcu_dp_value_update(DPID_ADD_ELE,switchState.dp_add_ele); //VALUE型数据上报;
  253. mcu_dp_value_update(DPID_CUR_CURRENT,switchState.dp_cur_current); //VALUE型数据上报;
  254. mcu_dp_value_update(DPID_CUR_POWER,switchState.dp_cur_power); //VALUE型数据上报;
  255. mcu_dp_value_update(DPID_CUR_VOLTAGE,switchState.dp_cur_voltage); //VALUE型数据上报;
  256. /*
  257. mcu_dp_string_update(DPID_SWITCH_INCHING,当前点动开关(延时关)指针,当前点动开关(延时关)数据长度); //STRING型数据上报;
  258. */
  259. for (i = 0; i < 3; i++)
  260. {
  261. hex_data[i * 3] = switchState.plus_mod[i];
  262. hex_data[i * 3 + 1] = (u08)((switchState.plus_auto_time[i] / 10) >> 8);
  263. hex_data[i * 3 + 2] = (u08)((switchState.plus_auto_time[i] / 10) & 0x00ff);
  264. }
  265. HexToDisHex(string_H16_data, hex_data, 9); // 转回16进制字符串
  266. mcu_dp_string_update(DPID_SWITCH_INCHING, string_H16_data, 18); // 点动开关(延时关)STRING型数据上报;
  267. }
  268. #endif
  269. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  270. void all_data_update(void)
  271. {
  272. // #error "mcu must realize function internal function"
  273. /*
  274. * these fucntion will be generated by cloud platform, but when use these function must reference the function in mcu_api.c
  275. */
  276. u08 i = 0;
  277. mcu_dp_bool_update(DPID_BACKLIGHT_SWITCH, switchState.human); // BOOL型数据上报;
  278. mcu_dp_enum_update(DPID_RELAY_STATUS_1, switchState.relay_status[0]); // 枚举型数据上报;
  279. mcu_dp_enum_update(DPID_RELAY_STATUS_2, switchState.relay_status[1]); // 枚举型数据上报;
  280. mcu_dp_enum_update(DPID_RELAY_STATUS_3, switchState.relay_status[2]); // 枚举型数据上报;
  281. mcu_dp_enum_update(DPID_LIGHT_MODE, switchState.led_level); // 枚举型数据上报;
  282. mcu_dp_enum_update(DPID_RELAY_STATUS, switchState.relay_allstatus); // 枚举型数据上报;
  283. mcu_dp_enum_update(DPID_MODE_1, switchState.mod[0]); // 枚举型数据上报;
  284. mcu_dp_enum_update(DPID_MODE_2, switchState.mod[1]); // 枚举型数据上报;
  285. mcu_dp_enum_update(DPID_MODE_3, switchState.mod[2]); // 枚举型数据上报;
  286. if (switchState.mod[2] == 0)
  287. mcu_dp_bool_update(DPID_SWITCH_3, switchState.SWITCH[2]); // BOOL型数据上报;
  288. if (switchState.mod[1] == 0)
  289. mcu_dp_bool_update(DPID_SWITCH_2, switchState.SWITCH[1]); // BOOL型数据上报;
  290. if (switchState.mod[0] == 0)
  291. mcu_dp_bool_update(DPID_SWITCH_1, switchState.SWITCH[0]); // BOOL型数据上报;
  292. // 场景不需要主动上报,APP不需要查询状态
  293. /*
  294. mcu_dp_enum_update(DPID_SCENE_1,当前场景1); //枚举型数据上报;
  295. mcu_dp_enum_update(DPID_SCENE_2,当前场景2); //枚举型数据上报;
  296. mcu_dp_enum_update(DPID_SCENE_3,当前场景3); //枚举型数据上报;
  297. mcu_dp_enum_update(DPID_SCENE_4,当前场景4); //枚举型数据上报;
  298. mcu_dp_enum_update(DPID_SCENE_5,当前场景5); //枚举型数据上报;
  299. mcu_dp_enum_update(DPID_SCENE_6,当前场景6); //枚举型数据上报;
  300. */
  301. /*
  302. mcu_dp_string_update(DPID_SWITCH_INCHING,当前点动开关(延时关)指针,当前点动开关(延时关)数据长度); //STRING型数据上报;
  303. */
  304. // switchState.plus_mod[0] = 1;
  305. // switchState.plus_auto_time[0] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  306. for (i = 0; i < 3; i++)
  307. {
  308. hex_data[i * 3] = switchState.plus_mod[i];
  309. hex_data[i * 3 + 1] = (u08)((switchState.plus_auto_time[i] / 10) >> 8);
  310. hex_data[i * 3 + 2] = (u08)((switchState.plus_auto_time[i] / 10) & 0x00ff);
  311. }
  312. HexToDisHex(string_H16_data, hex_data, 9); // 转回16进制字符串
  313. mcu_dp_string_update(DPID_SWITCH_INCHING, string_H16_data, 18); // 点动开关(延时关)STRING型数据上报;
  314. }
  315. #endif
  316. /******************************************************************************
  317. WARNING!!!
  318. 2:所有数据上报处理
  319. 自动化代码模板函数,具体请用户自行实现数据处理
  320. ******************************************************************************/
  321. ///> this will realize by cloud platform
  322. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  323. /*****************************************************************************
  324. 函数名称 : dp_download_scene_1_handle
  325. 功能描述 : 针对DPID_SCENE_1的处理函数
  326. 输入参数 : value:数据源数据
  327. : length:数据长度
  328. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  329. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  330. *****************************************************************************/
  331. static unsigned char dp_download_scene_1_handle(const unsigned char value[], unsigned short length)
  332. {
  333. // 示例:当前DP类型为ENUM
  334. unsigned char ret;
  335. unsigned char scene_1;
  336. scene_1 = mcu_get_dp_download_enum(value, length);
  337. switch (scene_1)
  338. {
  339. case 0:
  340. break;
  341. default:
  342. break;
  343. }
  344. // There should be a report after processing the DP
  345. ret = mcu_dp_enum_update(DPID_SCENE_1, scene_1);
  346. if (ret == SUCCESS)
  347. return SUCCESS;
  348. else
  349. return ERROR;
  350. }
  351. /*****************************************************************************
  352. 函数名称 : dp_download_scene_2_handle
  353. 功能描述 : 针对DPID_SCENE_2的处理函数
  354. 输入参数 : value:数据源数据
  355. : length:数据长度
  356. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  357. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  358. *****************************************************************************/
  359. static unsigned char dp_download_scene_2_handle(const unsigned char value[], unsigned short length)
  360. {
  361. // 示例:当前DP类型为ENUM
  362. unsigned char ret;
  363. unsigned char scene_2;
  364. scene_2 = mcu_get_dp_download_enum(value, length);
  365. switch (scene_2)
  366. {
  367. case 0:
  368. break;
  369. default:
  370. break;
  371. }
  372. // There should be a report after processing the DP
  373. ret = mcu_dp_enum_update(DPID_SCENE_2, scene_2);
  374. if (ret == SUCCESS)
  375. return SUCCESS;
  376. else
  377. return ERROR;
  378. }
  379. /*****************************************************************************
  380. 函数名称 : dp_download_scene_3_handle
  381. 功能描述 : 针对DPID_SCENE_3的处理函数
  382. 输入参数 : value:数据源数据
  383. : length:数据长度
  384. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  385. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  386. *****************************************************************************/
  387. static unsigned char dp_download_scene_3_handle(const unsigned char value[], unsigned short length)
  388. {
  389. // 示例:当前DP类型为ENUM
  390. unsigned char ret;
  391. unsigned char scene_3;
  392. scene_3 = mcu_get_dp_download_enum(value, length);
  393. switch (scene_3)
  394. {
  395. case 0:
  396. break;
  397. default:
  398. break;
  399. }
  400. // There should be a report after processing the DP
  401. ret = mcu_dp_enum_update(DPID_SCENE_3, scene_3);
  402. if (ret == SUCCESS)
  403. return SUCCESS;
  404. else
  405. return ERROR;
  406. }
  407. /*****************************************************************************
  408. 函数名称 : dp_download_scene_4_handle
  409. 功能描述 : 针对DPID_SCENE_4的处理函数
  410. 输入参数 : value:数据源数据
  411. : length:数据长度
  412. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  413. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  414. *****************************************************************************/
  415. static unsigned char dp_download_scene_4_handle(const unsigned char value[], unsigned short length)
  416. {
  417. // 示例:当前DP类型为ENUM
  418. unsigned char ret;
  419. unsigned char scene_4;
  420. scene_4 = mcu_get_dp_download_enum(value, length);
  421. switch (scene_4)
  422. {
  423. case 0:
  424. break;
  425. default:
  426. break;
  427. }
  428. // There should be a report after processing the DP
  429. ret = mcu_dp_enum_update(DPID_SCENE_4, scene_4);
  430. if (ret == SUCCESS)
  431. return SUCCESS;
  432. else
  433. return ERROR;
  434. }
  435. /*****************************************************************************
  436. 函数名称 : dp_download_scene_5_handle
  437. 功能描述 : 针对DPID_SCENE_5的处理函数
  438. 输入参数 : value:数据源数据
  439. : length:数据长度
  440. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  441. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  442. *****************************************************************************/
  443. static unsigned char dp_download_scene_5_handle(const unsigned char value[], unsigned short length)
  444. {
  445. // 示例:当前DP类型为ENUM
  446. unsigned char ret;
  447. unsigned char scene_5;
  448. scene_5 = mcu_get_dp_download_enum(value, length);
  449. switch (scene_5)
  450. {
  451. case 0:
  452. break;
  453. default:
  454. break;
  455. }
  456. // There should be a report after processing the DP
  457. ret = mcu_dp_enum_update(DPID_SCENE_5, scene_5);
  458. if (ret == SUCCESS)
  459. return SUCCESS;
  460. else
  461. return ERROR;
  462. }
  463. /*****************************************************************************
  464. 函数名称 : dp_download_scene_6_handle
  465. 功能描述 : 针对DPID_SCENE_6的处理函数
  466. 输入参数 : value:数据源数据
  467. : length:数据长度
  468. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  469. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  470. *****************************************************************************/
  471. static unsigned char dp_download_scene_6_handle(const unsigned char value[], unsigned short length)
  472. {
  473. // 示例:当前DP类型为ENUM
  474. unsigned char ret;
  475. unsigned char scene_6;
  476. scene_6 = mcu_get_dp_download_enum(value, length);
  477. switch (scene_6)
  478. {
  479. case 0:
  480. break;
  481. default:
  482. break;
  483. }
  484. // There should be a report after processing the DP
  485. ret = mcu_dp_enum_update(DPID_SCENE_6, scene_6);
  486. if (ret == SUCCESS)
  487. return SUCCESS;
  488. else
  489. return ERROR;
  490. }
  491. /*****************************************************************************
  492. 函数名称 : dp_download_mode_1_handle
  493. 功能描述 : 针对DPID_MODE_1的处理函数
  494. 输入参数 : value:数据源数据
  495. : length:数据长度
  496. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  497. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  498. *****************************************************************************/
  499. static unsigned char dp_download_mode_1_handle(const unsigned char value[], unsigned short length)
  500. {
  501. // 示例:当前DP类型为ENUM
  502. unsigned char ret;
  503. unsigned char mode_1;
  504. mode_1 = mcu_get_dp_download_enum(value, length);
  505. switch (mode_1)
  506. {
  507. case 0:
  508. break;
  509. case 1:
  510. break;
  511. default:
  512. break;
  513. }
  514. // add by zzw 20240304
  515. switchState.mod[0] = mode_1; // 开关、场景设置
  516. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  517. write_mileage[6] = switchState.mod[0];
  518. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  519. // add by zzw 20240304
  520. // add by zzw 20240312 增加eeprom参数配置区
  521. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  522. write_mileage_backup[6] = switchState.mod[0];
  523. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  524. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  525. if (read_mileage_backup[6] != switchState.mod[0]) // 再次读取不相等,则再次写入
  526. {
  527. read_mileage_backup[6] = switchState.mod[0];
  528. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  529. }
  530. // add by zzw 20240312 增加eeprom参数配置区
  531. // There should be a report after processing the DP
  532. ret = mcu_dp_enum_update(DPID_MODE_1, mode_1);
  533. if (ret == SUCCESS)
  534. return SUCCESS;
  535. else
  536. return ERROR;
  537. }
  538. /*****************************************************************************
  539. 函数名称 : dp_download_mode_2_handle
  540. 功能描述 : 针对DPID_MODE_2的处理函数
  541. 输入参数 : value:数据源数据
  542. : length:数据长度
  543. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  544. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  545. *****************************************************************************/
  546. static unsigned char dp_download_mode_2_handle(const unsigned char value[], unsigned short length)
  547. {
  548. // 示例:当前DP类型为ENUM
  549. unsigned char ret;
  550. unsigned char mode_2;
  551. mode_2 = mcu_get_dp_download_enum(value, length);
  552. switch (mode_2)
  553. {
  554. case 0:
  555. break;
  556. case 1:
  557. break;
  558. default:
  559. break;
  560. }
  561. // add by zzw 20240304
  562. switchState.mod[1] = mode_2; // 开关、场景设置
  563. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  564. write_mileage[7] = switchState.mod[1];
  565. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  566. // add by zzw 20240304
  567. // add by zzw 20240312 增加eeprom参数配置区
  568. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  569. write_mileage_backup[7] = switchState.mod[1];
  570. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  571. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  572. if (read_mileage_backup[7] != switchState.mod[1]) // 再次读取不相等,则再次写入
  573. {
  574. read_mileage_backup[7] = switchState.mod[1];
  575. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  576. }
  577. // add by zzw 20240312 增加eeprom参数配置区
  578. // There should be a report after processing the DP
  579. ret = mcu_dp_enum_update(DPID_MODE_2, mode_2);
  580. if (ret == SUCCESS)
  581. return SUCCESS;
  582. else
  583. return ERROR;
  584. }
  585. /*****************************************************************************
  586. 函数名称 : dp_download_mode_3_handle
  587. 功能描述 : 针对DPID_MODE_3的处理函数
  588. 输入参数 : value:数据源数据
  589. : length:数据长度
  590. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  591. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  592. *****************************************************************************/
  593. static unsigned char dp_download_mode_3_handle(const unsigned char value[], unsigned short length)
  594. {
  595. // 示例:当前DP类型为ENUM
  596. unsigned char ret;
  597. unsigned char mode_3;
  598. mode_3 = mcu_get_dp_download_enum(value, length);
  599. switch (mode_3)
  600. {
  601. case 0:
  602. break;
  603. case 1:
  604. break;
  605. default:
  606. break;
  607. }
  608. // add by zzw 20240304
  609. switchState.mod[2] = mode_3; // 开关、场景设置
  610. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  611. write_mileage[8] = switchState.mod[2];
  612. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  613. // add by zzw 20240304
  614. // add by zzw 20240312 增加eeprom参数配置区
  615. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  616. write_mileage_backup[8] = switchState.mod[2];
  617. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  618. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  619. if (read_mileage_backup[8] != switchState.mod[2]) // 再次读取不相等,则再次写入
  620. {
  621. read_mileage_backup[8] = switchState.mod[2];
  622. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  623. }
  624. // add by zzw 20240312 增加eeprom参数配置区
  625. // There should be a report after processing the DP
  626. ret = mcu_dp_enum_update(DPID_MODE_3, mode_3);
  627. if (ret == SUCCESS)
  628. return SUCCESS;
  629. else
  630. return ERROR;
  631. }
  632. #endif
  633. /*****************************************************************************
  634. 函数名称 : dp_download_switch_1_handle
  635. 功能描述 : 针对DPID_SWITCH_1的处理函数
  636. 输入参数 : value:数据源数据
  637. : length:数据长度
  638. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  639. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  640. *****************************************************************************/
  641. static unsigned char dp_download_switch_1_handle(const unsigned char value[], unsigned short length)
  642. {
  643. // 示例:当前DP类型为BOOL
  644. unsigned char ret;
  645. // 0:off/1:on
  646. unsigned char switch_1;
  647. switch_1 = mcu_get_dp_download_bool(value, length);
  648. if (switch_1 == 0)
  649. {
  650. // bool off
  651. gRelayOnOff &= (~0x01);
  652. gLedState = gLedState & (~0x01); // 按键1清除
  653. }
  654. else
  655. {
  656. // bool on
  657. gRelayOnOff |= (0x01);
  658. gLedState = gLedState | 0x01; // 按键1设置
  659. if (appControlLedEnable) // add by zzw 开灯点亮按键背光10秒
  660. {
  661. gRadarKeepTimer = 100; // 10s
  662. }
  663. if (switchState.plus_mod[0] == 0x01) // 点动开启
  664. {
  665. switchState.plus_en[0] = 0x01;
  666. switchState.plus_counter[0] = switchState.plus_auto_time[0];
  667. // write_mileage[0] = switchState.plus_en;
  668. // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
  669. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  670. write_mileage[14] = switchState.plus_en[0];
  671. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  672. }
  673. }
  674. // There should be a report after processing the DP
  675. ret = mcu_dp_bool_update(DPID_SWITCH_1, switch_1);
  676. if (ret == SUCCESS)
  677. return SUCCESS;
  678. else
  679. return ERROR;
  680. }
  681. /*****************************************************************************
  682. 函数名称 : dp_download_switch_2_handle
  683. 功能描述 : 针对DPID_SWITCH_2的处理函数
  684. 输入参数 : value:数据源数据
  685. : length:数据长度
  686. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  687. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  688. *****************************************************************************/
  689. static unsigned char dp_download_switch_2_handle(const unsigned char value[], unsigned short length)
  690. {
  691. // 示例:当前DP类型为BOOL
  692. unsigned char ret;
  693. // 0:off/1:on
  694. unsigned char switch_2;
  695. switch_2 = mcu_get_dp_download_bool(value, length);
  696. if (switch_2 == 0)
  697. {
  698. // bool off
  699. gRelayOnOff &= (~0x02);
  700. gLedState = gLedState & (~0x02); // 按键1清除
  701. }
  702. else
  703. {
  704. // bool on
  705. gRelayOnOff |= (0x02);
  706. gLedState = gLedState | 0x02; // 按键2设置
  707. if (appControlLedEnable) // add by zzw 开灯点亮按键背光10秒
  708. {
  709. gRadarKeepTimer = 100; // 10s
  710. }
  711. if (switchState.plus_mod[1] == 0x01) // 点动开启
  712. {
  713. switchState.plus_en[1] = 0x01;
  714. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  715. // write_mileage[1] = switchState.plus_en;
  716. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  717. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  718. write_mileage[15] = switchState.plus_en[1];
  719. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  720. }
  721. }
  722. // There should be a report after processing the DP
  723. ret = mcu_dp_bool_update(DPID_SWITCH_2, switch_2);
  724. if (ret == SUCCESS)
  725. return SUCCESS;
  726. else
  727. return ERROR;
  728. }
  729. /*****************************************************************************
  730. 函数名称 : dp_download_switch_3_handle
  731. 功能描述 : 针对DPID_SWITCH_3的处理函数
  732. 输入参数 : value:数据源数据
  733. : length:数据长度
  734. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  735. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  736. *****************************************************************************/
  737. static unsigned char dp_download_switch_3_handle(const unsigned char value[], unsigned short length)
  738. {
  739. // 示例:当前DP类型为BOOL
  740. unsigned char ret;
  741. // 0:off/1:on
  742. unsigned char switch_3;
  743. switch_3 = mcu_get_dp_download_bool(value, length);
  744. if (switch_3 == 0)
  745. {
  746. // bool off
  747. gRelayOnOff &= (~0x04);
  748. gLedState = gLedState & (~0x04); // 按键3清除
  749. }
  750. else
  751. {
  752. // bool on
  753. gRelayOnOff |= (0x04);
  754. gLedState = gLedState | 0x04; // 按键3设置
  755. if (appControlLedEnable) // add by zzw 开灯点亮按键背光10秒
  756. {
  757. gRadarKeepTimer = 100; // 10s
  758. }
  759. if (switchState.plus_mod[2] == 0x01) // 点动开启
  760. {
  761. switchState.plus_en[2] = 0x01;
  762. switchState.plus_counter[2] = switchState.plus_auto_time[2];
  763. // write_mileage[2] = switchState.plus_en;
  764. // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
  765. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  766. write_mileage[16] = switchState.plus_en[2];
  767. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  768. }
  769. }
  770. // There should be a report after processing the DP
  771. ret = mcu_dp_bool_update(DPID_SWITCH_3, switch_3);
  772. if (ret == SUCCESS)
  773. return SUCCESS;
  774. else
  775. return ERROR;
  776. }
  777. /*****************************************************************************
  778. 函数名称 : dp_download_relay_status_handle
  779. 功能描述 : 针对DPID_RELAY_STATUS的处理函数
  780. 输入参数 : value:数据源数据
  781. : length:数据长度
  782. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  783. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  784. *****************************************************************************/
  785. static unsigned char dp_download_relay_status_handle(const unsigned char value[], unsigned short length)
  786. {
  787. // 示例:当前DP类型为ENUM
  788. unsigned char ret;
  789. unsigned char relay_status;
  790. relay_status = mcu_get_dp_download_enum(value, length);
  791. switch (relay_status) // 全局设置3路的状态
  792. {
  793. case 0:
  794. break;
  795. case 1:
  796. break;
  797. case 2:
  798. break;
  799. default:
  800. break;
  801. }
  802. // add by zzw 20240302
  803. switchState.relay_allstatus = relay_status;
  804. switchState.relay_status[0] = relay_status;
  805. switchState.relay_status[1] = relay_status;
  806. switchState.relay_status[2] = relay_status;
  807. // iap_eeprom_write(1, write_mileage, 4); // 写入4字节
  808. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  809. write_mileage[1] = relay_status;
  810. write_mileage[2] = relay_status;
  811. write_mileage[3] = relay_status;
  812. write_mileage[4] = relay_status;
  813. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  814. // add by zzw 20240302
  815. // add by zzw 20240312 增加eeprom参数配置区
  816. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  817. write_mileage_backup[1] = relay_status;
  818. write_mileage_backup[2] = relay_status;
  819. write_mileage_backup[3] = relay_status;
  820. write_mileage_backup[4] = relay_status;
  821. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  822. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  823. if (read_mileage_backup[1] != relay_status) // 再次读取不相等,则再次写入
  824. {
  825. read_mileage_backup[1] = relay_status;
  826. read_mileage_backup[2] = relay_status;
  827. read_mileage_backup[3] = relay_status;
  828. read_mileage_backup[4] = relay_status;
  829. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  830. }
  831. // add by zzw 20240312 增加eeprom参数配置区
  832. // There should be a report after processing the DP
  833. ret = mcu_dp_enum_update(DPID_RELAY_STATUS, relay_status);
  834. if (ret == SUCCESS)
  835. return SUCCESS;
  836. else
  837. return ERROR;
  838. }
  839. /*****************************************************************************
  840. 函数名称 : dp_download_light_mode_handle
  841. 功能描述 : 针对DPID_LIGHT_MODE的处理函数
  842. 输入参数 : value:数据源数据
  843. : length:数据长度
  844. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  845. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  846. *****************************************************************************/
  847. static unsigned char dp_download_light_mode_handle(const unsigned char value[], unsigned short length)
  848. {
  849. // 示例:当前DP类型为ENUM
  850. unsigned char ret;
  851. unsigned char light_mode;
  852. light_mode = mcu_get_dp_download_enum(value, length);
  853. switch (light_mode) // 枚举范围:Level1,Level2,Level3
  854. {
  855. case 0:
  856. switchState.led_level = 0;
  857. break;
  858. case 1:
  859. switchState.led_level = 1;
  860. break;
  861. case 2:
  862. switchState.led_level = 2;
  863. break;
  864. default:
  865. break;
  866. }
  867. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  868. write_mileage[5] = switchState.led_level;
  869. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  870. // add by zzw 20240312 增加eeprom参数配置区
  871. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  872. write_mileage_backup[5] = switchState.led_level;
  873. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  874. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  875. if (read_mileage_backup[5] != switchState.led_level) // 再次读取不相等,则再次写入
  876. {
  877. read_mileage_backup[5] = switchState.led_level;
  878. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  879. }
  880. // add by zzw 20240312 增加eeprom参数配置区
  881. // There should be a report after processing the DP
  882. ret = mcu_dp_enum_update(DPID_LIGHT_MODE, light_mode);
  883. if (ret == SUCCESS)
  884. return SUCCESS;
  885. else
  886. return ERROR;
  887. }
  888. /*****************************************************************************
  889. 函数名称 : dp_download_backlight_switch_handle
  890. 功能描述 : 针对DPID_BACKLIGHT_SWITCH的处理函数
  891. 输入参数 : value:数据源数据
  892. : length:数据长度
  893. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  894. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  895. *****************************************************************************/
  896. static unsigned char dp_download_backlight_switch_handle(const unsigned char value[], unsigned short length)
  897. {
  898. // 示例:当前DP类型为BOOL
  899. unsigned char ret;
  900. // 0:off/1:on
  901. unsigned char backlight_switch;
  902. backlight_switch = mcu_get_dp_download_bool(value, length);
  903. if (backlight_switch == 0)
  904. {
  905. // bool off
  906. // switchState.bluelight = 0;
  907. switchState.human = 0;
  908. }
  909. else
  910. {
  911. // bool on
  912. // switchState.bluelight = 1;
  913. switchState.human = 1;
  914. }
  915. // add by zzy 20240309
  916. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  917. // switchState.led_level = write_mileage[5];//重新读取刷新背光亮度值
  918. write_mileage[9] = switchState.human;
  919. iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
  920. // add by zzy 20240309
  921. // add by zzw 20240312 增加eeprom参数配置区
  922. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  923. switchState.led_level = write_mileage_backup[5]; // 重新读取刷新背光亮度值
  924. write_mileage_backup[9] = switchState.human;
  925. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  926. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  927. if (read_mileage_backup[9] != switchState.human) // 再次读取不相等,则再次写入
  928. {
  929. read_mileage_backup[9] = switchState.human;
  930. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  931. }
  932. // add by zzw 20240312 增加eeprom参数配置区
  933. // There should be a report after processing the DP
  934. ret = mcu_dp_bool_update(DPID_BACKLIGHT_SWITCH, backlight_switch);
  935. if (ret == SUCCESS)
  936. return SUCCESS;
  937. else
  938. return ERROR;
  939. }
  940. /*****************************************************************************
  941. 函数名称 : dp_download_switch_inching_handle
  942. 功能描述 : 针对DPID_SWITCH_INCHING的处理函数
  943. 输入参数 : value:数据源数据
  944. : length:数据长度
  945. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  946. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  947. *****************************************************************************/
  948. static unsigned char dp_download_switch_inching_handle(const unsigned char value[], unsigned short length)
  949. {
  950. // 示例:当前DP类型为STRING
  951. unsigned char ret;
  952. int i = 0;
  953. int j = 0;
  954. // 后面两位表示秒数,前面的1字节的0位表示开/关,1开,0关,bit7-bit1表示通道号
  955. // length=0,表示全部删除
  956. //"AQAF"的Base64解码结果是01,00,05
  957. //"AwAF"的Base64解码结果是03,00,05
  958. //"BQAF"的Base64解码结果是05, 00, 05
  959. // STRING type data processing
  960. unsigned char string_H16_data[32]; // 16进制字符串
  961. unsigned char string_base64_data[32]; // base64标码字符串
  962. unsigned char hex_data[32]; // 转换后的16进制字节数据
  963. if (length > 12) // 自定义协议,一共18字节
  964. {
  965. // 010001ffffff000002,字符串下来就是,1号灯1秒后关,2号没有延时关,3号使能关闭延时时间是2秒
  966. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  967. write_mileage_backup[20] = 0;
  968. write_mileage_backup[21] = 0;
  969. write_mileage_backup[22] = 0;
  970. write_mileage_backup[23] = 0;
  971. write_mileage_backup[24] = 0;
  972. write_mileage_backup[25] = 0;
  973. write_mileage_backup[26] = 0;
  974. write_mileage_backup[27] = 0;
  975. write_mileage_backup[28] = 0; // 每次清空数据,重新赋值
  976. switchState.plus_mod[0] = 0;
  977. switchState.plus_mod[1] = 0;
  978. switchState.plus_mod[2] = 0; // 每次清空数据,重新赋值
  979. DisHexToHex(hex_data, value, 9); // 字符串转16进制数组
  980. #ifdef ZIGBEE_ZTU_T1_SW // 希智账户,3键零火开关
  981. if (hex_data[0] == 0x01) // 开关2使能
  982. {
  983. switchState.plus_mod[1] = 1;
  984. switchState.plus_auto_time[1] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  985. write_mileage_backup[23] = switchState.plus_mod[1];
  986. write_mileage_backup[24] = hex_data[1];
  987. write_mileage_backup[25] = hex_data[2];
  988. }
  989. else if (hex_data[0] == 0x00) // 开关2关闭
  990. {
  991. switchState.plus_mod[1] = 0;
  992. switchState.plus_auto_time[1] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  993. write_mileage_backup[23] = switchState.plus_mod[1];
  994. write_mileage_backup[24] = hex_data[1];
  995. write_mileage_backup[25] = hex_data[2];
  996. }
  997. else
  998. {
  999. switchState.plus_mod[1] = 0xff; // 清除
  1000. write_mileage_backup[23] = switchState.plus_mod[1];
  1001. write_mileage_backup[24] = 0xff;
  1002. write_mileage_backup[25] = 0xff;
  1003. }
  1004. #endif
  1005. #ifdef ZIGBEE_ZTU_T2_SW // 希智账户,3键零火开关
  1006. if (hex_data[0] == 0x01) // 开关1使能
  1007. {
  1008. switchState.plus_mod[0] = 1;
  1009. switchState.plus_auto_time[0] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1010. write_mileage_backup[20] = switchState.plus_mod[0];
  1011. write_mileage_backup[21] = hex_data[1];
  1012. write_mileage_backup[22] = hex_data[2];
  1013. }
  1014. else if (hex_data[0] == 0x00) // 开关1关闭
  1015. {
  1016. switchState.plus_mod[0] = 0;
  1017. switchState.plus_auto_time[0] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1018. write_mileage_backup[20] = switchState.plus_mod[0];
  1019. write_mileage_backup[21] = hex_data[1];
  1020. write_mileage_backup[22] = hex_data[2];
  1021. }
  1022. else
  1023. {
  1024. switchState.plus_mod[0] = 0xff; // 清除
  1025. write_mileage_backup[20] = switchState.plus_mod[0];
  1026. write_mileage_backup[21] = 0xff;
  1027. write_mileage_backup[22] = 0xff;
  1028. }
  1029. if (hex_data[3] == 0x01) // 开关3使能
  1030. {
  1031. switchState.plus_mod[2] = 1;
  1032. switchState.plus_auto_time[2] = (hex_data[4] * 256 + hex_data[5]) * 10; // 高字节在前,秒转100ms为基数
  1033. write_mileage_backup[26] = switchState.plus_mod[2];
  1034. write_mileage_backup[27] = hex_data[4];
  1035. write_mileage_backup[28] = hex_data[5];
  1036. }
  1037. else if (hex_data[3] == 0x00) // 开关3关闭
  1038. {
  1039. switchState.plus_mod[2] = 0;
  1040. switchState.plus_auto_time[2] = (hex_data[4] * 256 + hex_data[5]) * 10; // 高字节在前,秒转100ms为基数
  1041. write_mileage_backup[26] = switchState.plus_mod[2];
  1042. write_mileage_backup[27] = hex_data[4];
  1043. write_mileage_backup[28] = hex_data[5];
  1044. }
  1045. else
  1046. {
  1047. switchState.plus_mod[2] = 0xff; // 清除
  1048. write_mileage_backup[26] = switchState.plus_mod[2];
  1049. write_mileage_backup[27] = 0xff;
  1050. write_mileage_backup[28] = 0xff;
  1051. }
  1052. #endif
  1053. #if (defined ZIGBEE_ZTU_T3_SW) || (defined ZIGBEE_ZTU_T6_SCENCE_SW)
  1054. if (hex_data[0] == 0x01) // 开关1使能
  1055. {
  1056. switchState.plus_mod[0] = 1;
  1057. switchState.plus_auto_time[0] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1058. write_mileage_backup[20] = switchState.plus_mod[0];
  1059. write_mileage_backup[21] = hex_data[1];
  1060. write_mileage_backup[22] = hex_data[2];
  1061. }
  1062. else if (hex_data[0] == 0x00) // 开关1关闭
  1063. {
  1064. switchState.plus_mod[0] = 0;
  1065. switchState.plus_auto_time[0] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1066. write_mileage_backup[20] = switchState.plus_mod[0];
  1067. write_mileage_backup[21] = hex_data[1];
  1068. write_mileage_backup[22] = hex_data[2];
  1069. }
  1070. else
  1071. {
  1072. switchState.plus_mod[0] = 0xff; // 清除
  1073. write_mileage_backup[20] = switchState.plus_mod[0];
  1074. write_mileage_backup[21] = 0xff;
  1075. write_mileage_backup[22] = 0xff;
  1076. }
  1077. if (hex_data[3] == 0x01) // 开关2使能
  1078. {
  1079. switchState.plus_mod[1] = 1;
  1080. switchState.plus_auto_time[1] = (hex_data[4] * 256 + hex_data[5]) * 10; // 高字节在前,秒转100ms为基数
  1081. write_mileage_backup[23] = switchState.plus_mod[1];
  1082. write_mileage_backup[24] = hex_data[4];
  1083. write_mileage_backup[25] = hex_data[5];
  1084. }
  1085. else if (hex_data[3] == 0x00) // 开关2关闭
  1086. {
  1087. switchState.plus_mod[1] = 0;
  1088. switchState.plus_auto_time[1] = (hex_data[4] * 256 + hex_data[5]) * 10; // 高字节在前,秒转100ms为基数
  1089. write_mileage_backup[23] = switchState.plus_mod[1];
  1090. write_mileage_backup[24] = hex_data[4];
  1091. write_mileage_backup[25] = hex_data[5];
  1092. }
  1093. else
  1094. {
  1095. switchState.plus_mod[1] = 0xff; // 清除
  1096. write_mileage_backup[23] = switchState.plus_mod[1];
  1097. write_mileage_backup[24] = 0xff;
  1098. write_mileage_backup[25] = 0xff;
  1099. }
  1100. if (hex_data[6] == 0x01) // 开关3使能
  1101. {
  1102. switchState.plus_mod[2] = 1;
  1103. switchState.plus_auto_time[2] = (hex_data[7] * 256 + hex_data[8]) * 10; // 高字节在前,秒转100ms为基数
  1104. write_mileage_backup[26] = switchState.plus_mod[2];
  1105. write_mileage_backup[27] = hex_data[7];
  1106. write_mileage_backup[28] = hex_data[8];
  1107. }
  1108. else if (hex_data[6] == 0x00) // 开关3关闭
  1109. {
  1110. switchState.plus_mod[2] = 0;
  1111. switchState.plus_auto_time[2] = (hex_data[7] * 256 + hex_data[8]) * 10; // 高字节在前,秒转100ms为基数
  1112. write_mileage_backup[26] = switchState.plus_mod[2];
  1113. write_mileage_backup[27] = hex_data[7];
  1114. write_mileage_backup[28] = hex_data[8];
  1115. }
  1116. else
  1117. {
  1118. switchState.plus_mod[2] = 0xff; // 清除
  1119. write_mileage_backup[26] = switchState.plus_mod[2];
  1120. write_mileage_backup[27] = 0xff;
  1121. write_mileage_backup[28] = 0xff;
  1122. }
  1123. #endif
  1124. }
  1125. else // 涂鸦官方标准协议,最大12字节
  1126. {
  1127. // char source[32] = "abc";
  1128. // char dest[32];
  1129. /*将字符串abc进行base64编码*/
  1130. // Base64Encode(dest, source, strlen(source));
  1131. // printf("%s\n", dest);
  1132. /*将字符串abc的编码字符串解码,还原成abc*/
  1133. // Base64Decode(source, dest, strlen(dest));
  1134. // printf("%s\n", source);
  1135. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  1136. write_mileage_backup[20] = 0;
  1137. write_mileage_backup[21] = 0;
  1138. write_mileage_backup[22] = 0;
  1139. write_mileage_backup[23] = 0;
  1140. write_mileage_backup[24] = 0;
  1141. write_mileage_backup[25] = 0;
  1142. write_mileage_backup[26] = 0;
  1143. write_mileage_backup[27] = 0;
  1144. write_mileage_backup[28] = 0; // 每次清空数据,重新赋值
  1145. switchState.plus_mod[0] = 0;
  1146. switchState.plus_mod[1] = 0;
  1147. switchState.plus_mod[2] = 0;
  1148. if (length == 0) // 清空点动开关
  1149. {
  1150. switchState.plus_mod[0] = 0;
  1151. switchState.plus_mod[1] = 0;
  1152. switchState.plus_mod[2] = 0;
  1153. }
  1154. else
  1155. {
  1156. for (i = 0; i < (length / 4); i++) // 4字符一个数据集
  1157. {
  1158. for (j = 0; j < 4; j++) // 4个字符转换一次
  1159. {
  1160. string_base64_data[j] = value[i * 4 + j];
  1161. }
  1162. // base64_decode_three_bytes(string_base64_data, hex_data);
  1163. Base64Decode(hex_data, string_base64_data, strlen(string_base64_data));
  1164. if (hex_data[0] == 0x01) // 开关1使能
  1165. {
  1166. switchState.plus_mod[0] = 1;
  1167. switchState.plus_auto_time[0] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1168. write_mileage_backup[20] = switchState.plus_mod[0];
  1169. write_mileage_backup[21] = hex_data[1];
  1170. write_mileage_backup[22] = hex_data[2];
  1171. }
  1172. else if (hex_data[0] == 0x00) // 开关1关闭
  1173. {
  1174. switchState.plus_mod[0] = 0;
  1175. write_mileage_backup[20] = switchState.plus_mod[0];
  1176. write_mileage_backup[21] = 0;
  1177. write_mileage_backup[22] = 0;
  1178. }
  1179. else if (hex_data[0] == 0x03) // 开关2使能
  1180. {
  1181. switchState.plus_mod[1] = 1;
  1182. switchState.plus_auto_time[1] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1183. write_mileage_backup[23] = switchState.plus_mod[1];
  1184. write_mileage_backup[24] = hex_data[1];
  1185. write_mileage_backup[25] = hex_data[2];
  1186. }
  1187. else if (hex_data[0] == 0x02) // 开关2关闭
  1188. {
  1189. switchState.plus_mod[1] = 0;
  1190. write_mileage_backup[23] = switchState.plus_mod[1];
  1191. write_mileage_backup[24] = 0;
  1192. write_mileage_backup[25] = 0;
  1193. }
  1194. else if (hex_data[0] == 0x05) // 开关3使能
  1195. {
  1196. switchState.plus_mod[2] = 1;
  1197. switchState.plus_auto_time[2] = (hex_data[1] * 256 + hex_data[2]) * 10; // 高字节在前,秒转100ms为基数
  1198. write_mileage_backup[26] = switchState.plus_mod[2];
  1199. write_mileage_backup[27] = hex_data[1];
  1200. write_mileage_backup[28] = hex_data[2];
  1201. }
  1202. else if (hex_data[0] == 0x04) // 开关3关闭
  1203. {
  1204. switchState.plus_mod[2] = 0;
  1205. write_mileage_backup[26] = switchState.plus_mod[2];
  1206. write_mileage_backup[27] = 0;
  1207. write_mileage_backup[28] = 0;
  1208. }
  1209. }
  1210. }
  1211. }
  1212. // add by zzw 20240313 增加eeprom参数配置区
  1213. // 点动开关2个字节(使能1,时间2)-20-21-22,-23-24-25,-26-27-28
  1214. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  1215. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  1216. if (read_mileage_backup[20] != switchState.plus_mod[0]) // 再次读取不相等,则再次写入
  1217. {
  1218. iap_eeprom_write_backup(0, write_mileage_backup, 30); //
  1219. }
  1220. // add by zzw 20240313 增加eeprom参数配置区
  1221. // There should be a report after processing the DP
  1222. ret = mcu_dp_string_update(DPID_SWITCH_INCHING, value, length);
  1223. if (ret == SUCCESS)
  1224. return SUCCESS;
  1225. else
  1226. return ERROR;
  1227. }
  1228. /*****************************************************************************
  1229. 函数名称 : dp_download_relay_status_1_handle
  1230. 功能描述 : 针对DPID_RELAY_STATUS_1的处理函数
  1231. 输入参数 : value:数据源数据
  1232. : length:数据长度
  1233. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  1234. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  1235. *****************************************************************************/
  1236. static unsigned char dp_download_relay_status_1_handle(const unsigned char value[], unsigned short length)
  1237. {
  1238. // 示例:当前DP类型为ENUM
  1239. unsigned char ret;
  1240. unsigned char relay_status_1;
  1241. relay_status_1 = mcu_get_dp_download_enum(value, length);
  1242. switch (relay_status_1)
  1243. {
  1244. case 0: // 断电
  1245. // switchState.relay_status[0]=0;
  1246. break;
  1247. case 1: // 通电
  1248. // switchState.relay_status[0]=1;
  1249. break;
  1250. case 2: // 断电记忆
  1251. // switchState.relay_status[0]=2;
  1252. break;
  1253. default:
  1254. break;
  1255. }
  1256. // add by zzw 20240207
  1257. switchState.relay_status[0] = relay_status_1;
  1258. // add by zzw 20240207
  1259. // add by zzy 20240309
  1260. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1261. write_mileage[2] = switchState.relay_status[0];
  1262. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  1263. // add by zzy 20240309
  1264. // add by zzw 20240312 增加eeprom参数配置区
  1265. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  1266. write_mileage_backup[2] = switchState.relay_status[0];
  1267. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  1268. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  1269. if (read_mileage_backup[2] != switchState.relay_status[0]) // 再次读取不相等,则再次写入
  1270. {
  1271. read_mileage_backup[2] = switchState.relay_status[0];
  1272. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  1273. }
  1274. // add by zzw 20240312 增加eeprom参数配置区
  1275. // There should be a report after processing the DP
  1276. ret = mcu_dp_enum_update(DPID_RELAY_STATUS_1, relay_status_1);
  1277. if (ret == SUCCESS)
  1278. return SUCCESS;
  1279. else
  1280. return ERROR;
  1281. }
  1282. /*****************************************************************************
  1283. 函数名称 : dp_download_relay_status_2_handle
  1284. 功能描述 : 针对DPID_RELAY_STATUS_2的处理函数
  1285. 输入参数 : value:数据源数据
  1286. : length:数据长度
  1287. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  1288. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  1289. *****************************************************************************/
  1290. static unsigned char dp_download_relay_status_2_handle(const unsigned char value[], unsigned short length)
  1291. {
  1292. // 示例:当前DP类型为ENUM
  1293. unsigned char ret;
  1294. unsigned char relay_status_2;
  1295. relay_status_2 = mcu_get_dp_download_enum(value, length);
  1296. switch (relay_status_2)
  1297. {
  1298. case 0:
  1299. break;
  1300. case 1:
  1301. break;
  1302. case 2:
  1303. break;
  1304. default:
  1305. break;
  1306. }
  1307. // add by zzw 20240207
  1308. switchState.relay_status[1] = relay_status_2;
  1309. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1310. write_mileage[3] = switchState.relay_status[1];
  1311. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  1312. // add by zzw 20240207
  1313. // add by zzw 20240312 增加eeprom参数配置区
  1314. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  1315. write_mileage_backup[3] = switchState.relay_status[1];
  1316. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  1317. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  1318. if (read_mileage_backup[3] != switchState.relay_status[1]) // 再次读取不相等,则再次写入
  1319. {
  1320. read_mileage_backup[3] = switchState.relay_status[1];
  1321. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  1322. }
  1323. // add by zzw 20240312 增加eeprom参数配置区
  1324. // There should be a report after processing the DP
  1325. ret = mcu_dp_enum_update(DPID_RELAY_STATUS_2, relay_status_2);
  1326. if (ret == SUCCESS)
  1327. return SUCCESS;
  1328. else
  1329. return ERROR;
  1330. }
  1331. /*****************************************************************************
  1332. 函数名称 : dp_download_relay_status_3_handle
  1333. 功能描述 : 针对DPID_RELAY_STATUS_3的处理函数
  1334. 输入参数 : value:数据源数据
  1335. : length:数据长度
  1336. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  1337. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  1338. *****************************************************************************/
  1339. static unsigned char dp_download_relay_status_3_handle(const unsigned char value[], unsigned short length)
  1340. {
  1341. // 示例:当前DP类型为ENUM
  1342. unsigned char ret;
  1343. unsigned char relay_status_3;
  1344. relay_status_3 = mcu_get_dp_download_enum(value, length);
  1345. switch (relay_status_3)
  1346. {
  1347. case 0:
  1348. break;
  1349. case 1:
  1350. break;
  1351. case 2:
  1352. break;
  1353. default:
  1354. break;
  1355. }
  1356. // add by zzw 20240207
  1357. switchState.relay_status[2] = relay_status_3;
  1358. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1359. write_mileage[4] = switchState.relay_status[2];
  1360. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  1361. // add by zzw 20240207
  1362. // add by zzw 20240312 增加eeprom参数配置区
  1363. iap_eeprom_read_backup(0, write_mileage_backup, 30); // 读取30字节
  1364. write_mileage_backup[4] = switchState.relay_status[2];
  1365. iap_eeprom_write_backup(0, write_mileage_backup, 30); // 写入1字节
  1366. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节
  1367. if (read_mileage_backup[4] != switchState.relay_status[2]) // 再次读取不相等,则再次写入
  1368. {
  1369. read_mileage_backup[4] = switchState.relay_status[2];
  1370. iap_eeprom_write_backup(0, read_mileage_backup, 30); // 写入1字节
  1371. }
  1372. // add by zzw 20240312 增加eeprom参数配置区
  1373. // There should be a report after processing the DP
  1374. ret = mcu_dp_enum_update(DPID_RELAY_STATUS_3, relay_status_3);
  1375. if (ret == SUCCESS)
  1376. return SUCCESS;
  1377. else
  1378. return ERROR;
  1379. }
  1380. /*****************************************************************************
  1381. 函数名称 : dp_download_add_ele_handle
  1382. 功能描述 : 针对DPID_ADD_ELE的处理函数
  1383. 输入参数 : value:数据源数据
  1384. : length:数据长度
  1385. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  1386. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  1387. *****************************************************************************/
  1388. static unsigned char dp_download_add_ele_handle(const unsigned char value[], unsigned short length)
  1389. {
  1390. //示例:当前DP类型为VALUE
  1391. unsigned char ret;
  1392. unsigned long add_ele;
  1393. add_ele = mcu_get_dp_download_value(value,length);
  1394. /*
  1395. //VALUE type data processing
  1396. */
  1397. switchState.dp_add_ele=value[1]<<8+value[0];//100倍,去除小数的值
  1398. //实际值,需要/100,带小数的
  1399. //There should be a report after processing the DP
  1400. ret = mcu_dp_value_update(DPID_ADD_ELE,add_ele);
  1401. if(ret == SUCCESS)
  1402. return SUCCESS;
  1403. else
  1404. return ERROR;
  1405. }
  1406. #ifdef SUPPORT_MCU_RTC_CHECK
  1407. /**
  1408. * @brief mcu check local RTC time
  1409. * @param[in] {time} timestamp
  1410. * @return void
  1411. */
  1412. void mcu_write_rtctime(unsigned char time[])
  1413. {
  1414. // #error "mcu should realize RTC time wtriting fuction, and delete this line"
  1415. /*
  1416. time[0]~time[3]:standard time
  1417. time[4]~time[7]: Local time
  1418. */
  1419. my_memcpy((void *)timestamp, (const char *)time, 4); // get timestamp
  1420. zigbee_timestamp_to_time();
  1421. /*
  1422. year = _time.w_year; //year
  1423. month = _time.w_month; //month
  1424. date = _time.w_date; //date
  1425. hour = _time.hour + 8; //hour(8:BeiJing time)
  1426. min = _time.min; //minute
  1427. sec = _time.sec; //second
  1428. */
  1429. }
  1430. #endif
  1431. /**
  1432. * @brief Zigbee functional test feedback
  1433. * @param[in] {void}
  1434. * @return void
  1435. */
  1436. void zigbee_test_result(void)
  1437. {
  1438. // #error "this test is makesure the rf fuction of zigbee module, if test pass or not should do something, mcu should realize"
  1439. unsigned char rssi = zigbee_uart_rx_buf[DATA_START + 1];
  1440. if (zigbee_uart_rx_buf[DATA_START] == 0x01)
  1441. {
  1442. if (rssi > 0x3C)
  1443. {
  1444. // test sucess the range of rssi is 0% ~ 100%
  1445. }
  1446. else
  1447. {
  1448. // test failure
  1449. }
  1450. }
  1451. else
  1452. {
  1453. // test failure
  1454. }
  1455. }
  1456. /******************************************************************************
  1457. WARNING!!!
  1458. 以下函数用户请勿修改!!
  1459. ******************************************************************************/
  1460. /**
  1461. * @brief this function will handle uart received frame data
  1462. * @param[in] {dpid} dp id
  1463. * @param[in] {value} dp data
  1464. * @param[in] {length} lenght of dp data
  1465. * @return handle result
  1466. */
  1467. #ifdef ZIGBEE_ZTU_T1_SW
  1468. unsigned char dp_download_handle(unsigned char dpid, const unsigned char value[], unsigned short length)
  1469. {
  1470. /* only list of function, mcu need realize these fuction*/
  1471. unsigned char ret;
  1472. // 可收到控制信号,说明配网肯定成功
  1473. ZG_JoinCounter = 18000; // 入网成功,入网计数器置最大值,180秒
  1474. switchState.zigbee_work_state = 1;
  1475. // 可收到控制信号,说明配网肯定成功
  1476. switch (dpid)
  1477. {
  1478. case DPID_SWITCH_1:
  1479. case DPID_SWITCH_2:
  1480. // 开关 2处理函数
  1481. ret = dp_download_switch_2_handle(value, length);
  1482. break;
  1483. case DPID_SWITCH_3:
  1484. // 开关 3处理函数
  1485. ret = dp_download_switch_3_handle(value, length);
  1486. break;
  1487. case DPID_RELAY_STATUS:
  1488. // 上电状态设置处理函数
  1489. ret = dp_download_relay_status_handle(value, length);
  1490. break;
  1491. case DPID_LIGHT_MODE:
  1492. // 指示灯状态设置处理函数
  1493. ret = dp_download_light_mode_handle(value, length);
  1494. break;
  1495. case DPID_BACKLIGHT_SWITCH:
  1496. // 背光开关处理函数
  1497. ret = dp_download_backlight_switch_handle(value, length);
  1498. break;
  1499. case DPID_SWITCH_INCHING:
  1500. // 点动开关(延时关)处理函数
  1501. ret = dp_download_switch_inching_handle(value, length);
  1502. break;
  1503. case DPID_RELAY_STATUS_1:
  1504. // 开关1上电状态设置处理函数
  1505. ret = dp_download_relay_status_1_handle(value, length);
  1506. break;
  1507. case DPID_RELAY_STATUS_2:
  1508. // 开关2上电状态设置处理函数
  1509. ret = dp_download_relay_status_2_handle(value, length);
  1510. break;
  1511. case DPID_RELAY_STATUS_3:
  1512. // 开关3上电状态设置处理函数
  1513. ret = dp_download_relay_status_3_handle(value, length);
  1514. break;
  1515. default:
  1516. break;
  1517. }
  1518. return ret;
  1519. }
  1520. #endif
  1521. #ifdef ZIGBEE_ZTU_T2_SW
  1522. unsigned char dp_download_handle(unsigned char dpid, const unsigned char value[], unsigned short length)
  1523. {
  1524. /* only list of function, mcu need realize these fuction*/
  1525. unsigned char ret;
  1526. // 可收到控制信号,说明配网肯定成功
  1527. ZG_JoinCounter = 18000; // 入网成功,入网计数器置最大值,180秒
  1528. switchState.zigbee_work_state = 1;
  1529. // 可收到控制信号,说明配网肯定成功
  1530. switch (dpid)
  1531. {
  1532. case DPID_SWITCH_1:
  1533. // 开关 1处理函数
  1534. ret = dp_download_switch_1_handle(value, length);
  1535. break;
  1536. case DPID_SWITCH_2:
  1537. case DPID_SWITCH_3:
  1538. // 开关 3处理函数
  1539. ret = dp_download_switch_3_handle(value, length);
  1540. break;
  1541. case DPID_RELAY_STATUS:
  1542. // 上电状态设置处理函数
  1543. ret = dp_download_relay_status_handle(value, length);
  1544. break;
  1545. case DPID_LIGHT_MODE:
  1546. // 指示灯状态设置处理函数
  1547. ret = dp_download_light_mode_handle(value, length);
  1548. break;
  1549. case DPID_BACKLIGHT_SWITCH:
  1550. // 背光开关处理函数
  1551. ret = dp_download_backlight_switch_handle(value, length);
  1552. break;
  1553. case DPID_SWITCH_INCHING:
  1554. // 点动开关(延时关)处理函数
  1555. ret = dp_download_switch_inching_handle(value, length);
  1556. break;
  1557. case DPID_RELAY_STATUS_1:
  1558. // 开关1上电状态设置处理函数
  1559. ret = dp_download_relay_status_1_handle(value, length);
  1560. break;
  1561. case DPID_RELAY_STATUS_2:
  1562. // 开关2上电状态设置处理函数
  1563. ret = dp_download_relay_status_2_handle(value, length);
  1564. break;
  1565. case DPID_RELAY_STATUS_3:
  1566. // 开关3上电状态设置处理函数
  1567. ret = dp_download_relay_status_3_handle(value, length);
  1568. break;
  1569. default:
  1570. break;
  1571. }
  1572. return ret;
  1573. }
  1574. #endif
  1575. #ifdef ZIGBEE_ZTU_T3_SW
  1576. // #if (defined ZIGBEE_ZTU_T3_SW) || (defined ZIGBEE_ZTU_T2_SW) || (defined ZIGBEE_ZTU_T1_SW)
  1577. unsigned char dp_download_handle(unsigned char dpid, const unsigned char value[], unsigned short length)
  1578. {
  1579. /* only list of function, mcu need realize these fuction*/
  1580. unsigned char ret;
  1581. // 可收到控制信号,说明配网肯定成功
  1582. ZG_JoinCounter = 18000; // 入网成功,入网计数器置最大值,180秒
  1583. switchState.zigbee_work_state = 1;
  1584. // 可收到控制信号,说明配网肯定成功
  1585. switch (dpid)
  1586. {
  1587. case DPID_SWITCH_1:
  1588. // 开关 1处理函数
  1589. ret = dp_download_switch_1_handle(value, length);
  1590. break;
  1591. case DPID_SWITCH_2:
  1592. // 开关 2处理函数
  1593. ret = dp_download_switch_2_handle(value, length);
  1594. break;
  1595. case DPID_SWITCH_3:
  1596. // 开关 3处理函数
  1597. ret = dp_download_switch_3_handle(value, length);
  1598. break;
  1599. case DPID_RELAY_STATUS:
  1600. // 上电状态设置处理函数
  1601. ret = dp_download_relay_status_handle(value, length);
  1602. break;
  1603. case DPID_LIGHT_MODE:
  1604. // 指示灯状态设置处理函数
  1605. ret = dp_download_light_mode_handle(value, length);
  1606. break;
  1607. case DPID_BACKLIGHT_SWITCH:
  1608. // 背光开关处理函数
  1609. ret = dp_download_backlight_switch_handle(value, length);
  1610. break;
  1611. case DPID_SWITCH_INCHING:
  1612. // 点动开关(延时关)处理函数
  1613. ret = dp_download_switch_inching_handle(value, length);
  1614. break;
  1615. case DPID_RELAY_STATUS_1:
  1616. // 开关1上电状态设置处理函数
  1617. ret = dp_download_relay_status_1_handle(value, length);
  1618. break;
  1619. case DPID_RELAY_STATUS_2:
  1620. // 开关2上电状态设置处理函数
  1621. ret = dp_download_relay_status_2_handle(value, length);
  1622. break;
  1623. case DPID_RELAY_STATUS_3:
  1624. // 开关3上电状态设置处理函数
  1625. ret = dp_download_relay_status_3_handle(value, length);
  1626. break;
  1627. case DPID_ADD_ELE:
  1628. //增加电量处理函数
  1629. ret = dp_download_add_ele_handle(value,length);
  1630. default:
  1631. break;
  1632. }
  1633. return ret;
  1634. }
  1635. #endif
  1636. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  1637. unsigned char dp_download_handle(unsigned char dpid, const unsigned char value[], unsigned short length)
  1638. {
  1639. /* only list of function, mcu need realize these fuction*/
  1640. unsigned char ret;
  1641. switchState.zigbee_work_state = 1; // 有dp控制指令,说明设备肯定在线//add by zzw
  1642. switch (dpid)
  1643. {
  1644. case DPID_SCENE_1:
  1645. // 场景1处理函数
  1646. ret = dp_download_scene_1_handle(value, length);
  1647. break;
  1648. case DPID_SCENE_2:
  1649. // 场景2处理函数
  1650. ret = dp_download_scene_2_handle(value, length);
  1651. break;
  1652. case DPID_SCENE_3:
  1653. // 场景3处理函数
  1654. ret = dp_download_scene_3_handle(value, length);
  1655. break;
  1656. case DPID_SCENE_4:
  1657. // 场景4处理函数
  1658. ret = dp_download_scene_4_handle(value, length);
  1659. break;
  1660. case DPID_SCENE_5:
  1661. // 场景5处理函数
  1662. ret = dp_download_scene_5_handle(value, length);
  1663. break;
  1664. case DPID_SCENE_6:
  1665. // 场景6处理函数
  1666. ret = dp_download_scene_6_handle(value, length);
  1667. break;
  1668. case DPID_MODE_1:
  1669. // 模式1处理函数
  1670. ret = dp_download_mode_1_handle(value, length);
  1671. break;
  1672. case DPID_MODE_2:
  1673. // 模式2处理函数
  1674. ret = dp_download_mode_2_handle(value, length);
  1675. break;
  1676. case DPID_MODE_3:
  1677. // 模式3处理函数
  1678. ret = dp_download_mode_3_handle(value, length);
  1679. break;
  1680. case DPID_SWITCH_1:
  1681. // 开关1处理函数
  1682. ret = dp_download_switch_1_handle(value, length);
  1683. break;
  1684. case DPID_SWITCH_2:
  1685. // 开关2处理函数
  1686. ret = dp_download_switch_2_handle(value, length);
  1687. break;
  1688. case DPID_SWITCH_3:
  1689. // 开关3处理函数
  1690. ret = dp_download_switch_3_handle(value, length);
  1691. break;
  1692. case DPID_BACKLIGHT_SWITCH:
  1693. // 人感开关处理函数
  1694. ret = dp_download_backlight_switch_handle(value, length);
  1695. break;
  1696. case DPID_LIGHT_MODE:
  1697. // 指示灯亮度处理函数
  1698. ret = dp_download_light_mode_handle(value, length);
  1699. break;
  1700. case DPID_RELAY_STATUS:
  1701. // 上电状态设置处理函数
  1702. ret = dp_download_relay_status_handle(value, length);
  1703. break;
  1704. case DPID_RELAY_STATUS_1:
  1705. // 开关1上电状态设置处理函数
  1706. ret = dp_download_relay_status_1_handle(value, length);
  1707. break;
  1708. case DPID_RELAY_STATUS_2:
  1709. // 开关2上电状态设置处理函数
  1710. ret = dp_download_relay_status_2_handle(value, length);
  1711. break;
  1712. case DPID_RELAY_STATUS_3:
  1713. // 开关3上电状态设置处理函数
  1714. ret = dp_download_relay_status_3_handle(value, length);
  1715. break;
  1716. case DPID_SWITCH_INCHING:
  1717. // 点动开关(延时关)处理函数
  1718. ret = dp_download_switch_inching_handle(value, length);
  1719. break;
  1720. default:
  1721. break;
  1722. }
  1723. return ret;
  1724. }
  1725. #endif
  1726. /**
  1727. * @brief get received cmd total number
  1728. * @param[in] {void}
  1729. * @return received cmd total number
  1730. */
  1731. unsigned char get_download_cmd_total(void)
  1732. {
  1733. return (sizeof(download_cmd) / sizeof(download_cmd[0]));
  1734. }
  1735. /**
  1736. * @brief received zigbee net_work state handle
  1737. * @param[in] {zigbee_work_state} zigbee current network state
  1738. * @return void
  1739. */
  1740. void zigbee_work_state_event(unsigned char zigbee_work_state)
  1741. {
  1742. unsigned short length = 0;
  1743. zigbee_uart_write_frame(ZIGBEE_STATE_CMD, length);
  1744. switch (zigbee_work_state)
  1745. {
  1746. case ZIGBEE_NOT_JION: // 设备从在线切换到不在线则报警,脱网报警(离线状态) 慢闪60s 3s一闪烁
  1747. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 入网前几入网阶段不进入报警闪烁,防止入网指令发送后的自动脱网导致闪烁混乱
  1748. {
  1749. switchState.zigbee_work_state = 2; // 脱网报警启动
  1750. switchState.time = 6000; // 60秒
  1751. ledSetParam(150, 300); // 3s一闪烁
  1752. }
  1753. break;
  1754. case ZIGBEE_JOIN_GATEWAY: // 新设备入网成功
  1755. ZG_JoinCounter = 18000; // 入网成功,入网计数器置最大值,180秒
  1756. switchState.zigbee_work_state = 1;
  1757. break;
  1758. case ZIGBEE_JOIN_ERROR: // 已经入网设备,发起再次入网,会进入这里
  1759. // 配网超时3分钟,触发误触重连,只要APP不操作添加设备确定按键,设备重新回连至上一个账号下
  1760. // ZG_JoinCounter = 18000; // 配网结束,入网计数器置最大值,180秒
  1761. // switchState.zigbee_work_state = 1;
  1762. break;
  1763. case ZIGBEE_JOINING:
  1764. break;
  1765. default:
  1766. break;
  1767. }
  1768. }
  1769. /**
  1770. * @brief received reset zigbee response
  1771. * @param[in] {state} response state
  1772. * @return void
  1773. */
  1774. void mcu_reset_zigbee_event(unsigned char state)
  1775. {
  1776. switch (state)
  1777. {
  1778. case RESET_ZIGBEE_OK:
  1779. break;
  1780. case RESET_ZIGBEE_ERROR:
  1781. break;
  1782. default:
  1783. break;
  1784. }
  1785. }
  1786. /**
  1787. * @brief check mcu version response
  1788. * @param[in] {void}
  1789. * @return void
  1790. */
  1791. void response_mcu_ota_version_event(void)
  1792. {
  1793. unsigned short length = 0;
  1794. length = set_zigbee_uart_byte(length, get_current_mcu_fw_ver()); // current fw version
  1795. zigbee_uart_write_frame(MCU_OTA_VERSION_CMD, length);
  1796. }
  1797. #ifdef SUPPORT_MCU_OTA
  1798. /**
  1799. * @brief mcu ota update notify response
  1800. * @param[in] {offset} data offset
  1801. * @return void
  1802. */
  1803. void response_mcu_ota_notify_event(unsigned char offset)
  1804. {
  1805. unsigned char i = 0;
  1806. unsigned short length = 0;
  1807. unsigned char update = 0;
  1808. unsigned long app_newcode_add = 0;
  1809. unsigned char j = 0;
  1810. current_mcu_fw_pid(); // current PID
  1811. while (i < 8)
  1812. {
  1813. ota_fw_info.mcu_ota_pid[i] = zigbee_uart_rx_buf[offset + DATA_START + i]; // ota fw PID
  1814. i++;
  1815. }
  1816. ota_fw_info.mcu_ota_ver = zigbee_uart_rx_buf[offset + DATA_START + 8]; // ota fw version
  1817. ota_fw_info.mcu_ota_fw_size = zigbee_uart_rx_buf[offset + DATA_START + 9] << 24 |
  1818. zigbee_uart_rx_buf[offset + DATA_START + 10] << 16 |
  1819. zigbee_uart_rx_buf[offset + DATA_START + 11] << 8 |
  1820. zigbee_uart_rx_buf[offset + DATA_START + 12]; // ota fw size
  1821. ota_fw_info.mcu_ota_checksum = zigbee_uart_rx_buf[offset + DATA_START + 13] << 24 |
  1822. zigbee_uart_rx_buf[offset + DATA_START + 14] << 16 |
  1823. zigbee_uart_rx_buf[offset + DATA_START + 15] << 8 |
  1824. zigbee_uart_rx_buf[offset + DATA_START + 16]; // ota fw checksum
  1825. if ((!strcmp_barry(&ota_fw_info.mcu_ota_pid[0], &current_mcu_pid[0], 8)) &&
  1826. (ota_fw_info.mcu_ota_ver > get_current_mcu_fw_ver() &&
  1827. ota_fw_info.mcu_ota_fw_size > 0))
  1828. { // check fw pid and fw version and fw size
  1829. length = set_zigbee_uart_byte(length, 0x00); // OK
  1830. update = 1; // 校验通过,可进行固件升级
  1831. }
  1832. else
  1833. {
  1834. length = set_zigbee_uart_byte(length, 0x01); // error
  1835. }
  1836. ota_fw_info.mcu_current_offset = 0;
  1837. zigbee_uart_write_frame(MCU_OTA_NOTIFY_CMD, length); // 校验PID一致性,版本号大于当前版本则可执行升级,并给zigbee应答01或00
  1838. // add by zzw 升级通知接收成功,第一次调用接收固件命令
  1839. if (update)
  1840. {
  1841. // zzw ota must add 20240326 OTA 存入标志位
  1842. // mcu_ota_fw_request(); // 固件升级,取消直接升级,进入boot升级
  1843. // TM52的特性决定了,需要跳转到BOOT去接收OTA数据
  1844. update = 0;
  1845. // 先写入升级的标志再跳转到UBOOT
  1846. // INFO5地址存储升级标志0x55、0xaa、0x5a
  1847. CheckBuf[0] = 0x55;
  1848. CheckBuf[1] = 0xaa;
  1849. CheckBuf[2] = 0x5a;
  1850. for (i = 0; i < 8; i++)
  1851. {
  1852. CheckBuf[3 + i] = ota_fw_info.mcu_ota_pid[i];
  1853. }
  1854. CheckBuf[11] = ota_fw_info.mcu_ota_ver;
  1855. for (i = 0; i < 8; i++)
  1856. {
  1857. CheckBuf[12 + i] = zigbee_uart_rx_buf[offset + DATA_START + 9 + i];
  1858. }
  1859. user_write_INFO_byte(INFO5_ADDR, CheckBuf, 20);
  1860. // while (!com0send_over); //等待uart回复帧发送结束再执行跳转指令
  1861. // com0send_over = 0;
  1862. bsp_delay_ms(5); // 延时5ms等待串口应答结束
  1863. EA = 0; // 关闭中断
  1864. BOOTV = 0x04; // 设置热复位后跳转到uboot
  1865. SWCMD = 0x56; // 软件复位
  1866. // TM52的特性决定了,需要跳转到BOOT去接收OTA数据
  1867. }
  1868. }
  1869. /**
  1870. * @brief received mcu ota data request response
  1871. * @param[in] {fw_offset} offset of file
  1872. * @param[in] {data} received data
  1873. * @return void
  1874. */
  1875. /*void reveived_mcu_ota_data_handle(unsigned int fw_offset, char *data)
  1876. {
  1877. //#error "received frame data, should save in flash, mcu should realize this fuction, and delete this line "
  1878. //未使用
  1879. }*/
  1880. /**
  1881. * @brief mcu send ota data request
  1882. * @param[in] {void}
  1883. * @return void
  1884. */
  1885. void mcu_ota_fw_request_event(unsigned char offset)
  1886. {
  1887. unsigned int fw_offset = 0;
  1888. char fw_data[FW_SINGLE_PACKET_SIZE] = {-1}; //
  1889. unsigned char i = 0;
  1890. unsigned char last_package_len = 0;
  1891. static unsigned int current_checksum = 0;
  1892. if (zigbee_uart_rx_buf[offset + DATA_START] == 0x01) // status check
  1893. return;
  1894. while (i < 8)
  1895. {
  1896. if (current_mcu_pid[i] != zigbee_uart_rx_buf[offset + DATA_START + 1 + i]) // pid check
  1897. return;
  1898. i++;
  1899. }
  1900. if (ota_fw_info.mcu_ota_ver != zigbee_uart_rx_buf[offset + DATA_START + 9]) // version check
  1901. return;
  1902. i = 0;
  1903. while (i < 4)
  1904. {
  1905. fw_offset |= (zigbee_uart_rx_buf[offset + DATA_START + 10 + i] << (24 - i * 8)); // offset
  1906. i++;
  1907. }
  1908. i = 0;
  1909. if (ota_fw_info.mcu_current_offset == fw_offset)
  1910. {
  1911. // if ((ota_fw_info.mcu_ota_fw_size - fw_offset) / FW_SINGLE_PACKET_SIZE != 0)
  1912. if (((ota_fw_info.mcu_ota_fw_size - fw_offset) / FW_SINGLE_PACKET_SIZE != 0) && (ota_fw_info.mcu_ota_fw_size != (fw_offset + FW_SINGLE_PACKET_SIZE)))
  1913. // if (((ota_fw_info.mcu_ota_fw_size - fw_offset) / FW_SINGLE_PACKET_SIZE != 0)&&((ota_fw_info.mcu_ota_fw_size - fw_offset) % FW_SINGLE_PACKET_SIZE != 0))
  1914. {
  1915. if (ota_fw_info.mcu_current_offset == 0)
  1916. {
  1917. current_checksum = 0;
  1918. }
  1919. while (i < FW_SINGLE_PACKET_SIZE)
  1920. {
  1921. fw_data[i] = zigbee_uart_rx_buf[offset + DATA_START + 14 + i]; // fw data
  1922. current_checksum += fw_data[i];
  1923. i++;
  1924. }
  1925. ota_fw_info.mcu_current_offset += FW_SINGLE_PACKET_SIZE;
  1926. ota_fw_data_handle(fw_offset, &fw_data[0], FW_SINGLE_PACKET_SIZE); // OTA paket data handle
  1927. }
  1928. else
  1929. {
  1930. i = 0;
  1931. // last_package_len = ota_fw_info.mcu_ota_fw_size - fw_offset;
  1932. if ((ota_fw_info.mcu_ota_fw_size - fw_offset) % FW_SINGLE_PACKET_SIZE != 0)
  1933. {
  1934. last_package_len = ota_fw_info.mcu_ota_fw_size - fw_offset;
  1935. }
  1936. else
  1937. {
  1938. last_package_len = FW_SINGLE_PACKET_SIZE;
  1939. }
  1940. while (i < last_package_len)
  1941. {
  1942. fw_data[i] = zigbee_uart_rx_buf[offset + DATA_START + 14 + i];
  1943. current_checksum += fw_data[i];
  1944. i++;
  1945. }
  1946. ota_fw_info.mcu_current_offset += last_package_len;
  1947. if (ota_fw_info.mcu_ota_checksum != current_checksum)
  1948. {
  1949. // ota failure report ota failure and clear ota struct
  1950. mcu_ota_result_report(1); // failed
  1951. }
  1952. else
  1953. {
  1954. // ota sucess
  1955. mcu_ota_result_report(0); // seccess
  1956. }
  1957. ota_fw_data_handle(fw_offset, &fw_data[0], last_package_len); // OTA paket data handle
  1958. current_checksum = 0;
  1959. }
  1960. }
  1961. else
  1962. {
  1963. // ota request timeout, then restart ota request from ota_fw_info.mcu_ota_fw_size
  1964. }
  1965. }
  1966. static void report_mcu_ota_result(unsigned char res)
  1967. {
  1968. unsigned short length;
  1969. if ((res == 0) || (res == 1))
  1970. {
  1971. length = set_zigbee_uart_byte(length, res);
  1972. zigbee_uart_write_frame(MCU_OTA_NOTIFY_CMD, length);
  1973. }
  1974. }
  1975. /**
  1976. * @brief mcu ota data result notify
  1977. * @param[in] {void}
  1978. * @return void
  1979. */
  1980. void mcu_ota_result_event(unsigned char offset)
  1981. {
  1982. unsigned short k, Ret;
  1983. u32 temp_buff[256];
  1984. unsigned char status = zigbee_uart_rx_buf[offset + DATA_START];
  1985. if (status == 0x00)
  1986. {
  1987. // 升级成功,用户提示
  1988. IAPUpdateFlag = 1;
  1989. // zzw ota must add
  1990. /* for (k = 0; k < 256; k++)
  1991. {
  1992. temp_buff[k] = flash_word_read(OTA_STATE_DATA_ADD + (4 * k)); // 1.取出程序运行区代码数据
  1993. }
  1994. temp_buff[0] = IAPUpdateFlag;
  1995. delay_1ms(10);
  1996. Ret = flash_page_write(OTA_STATE_DATA_ADD, temp_buff); // 2.将取出的运行区代码写入临时区
  1997. delay_1ms(20);
  1998. flash_word_write (OTA_STATE_DATA_ADD,IAPUpdateFlag);
  1999. delay_1ms(500);
  2000. NVIC_SystemReset(); // add by zzw 系统复位函数
  2001. */
  2002. }
  2003. else if (status == 0x01)
  2004. {
  2005. }
  2006. }
  2007. /**
  2008. * @brief mcu ota data handle
  2009. * @param[in] {fw_offset} frame offset
  2010. * @param[in] {data} received data
  2011. * @return void
  2012. */
  2013. void ota_fw_data_handle(unsigned int fw_offset, char *data1, unsigned char data_len)
  2014. {
  2015. unsigned short i = 0;
  2016. unsigned short update = 1;
  2017. unsigned long app_newcode_add = 0; // 读flash的时候的地址变量,新代码区
  2018. unsigned short temp = 0;
  2019. unsigned long write_data;
  2020. //"请在该函数处理固件包数据"
  2021. //"received frame data, should save in flash, mcu should realize this fuction, and delete this line "
  2022. // 取出数据存入falsh,建议延用‘ota_fw_info’全局变量,不需定义其他全局变量,可维护数据包长度等
  2023. if (fw_offset == 0)
  2024. {
  2025. // zzw ota must add
  2026. /*app_newcode_add = OTA_NEW_CODE_START_ADD;
  2027. fmc_unlock();
  2028. for (i = 0; i < 26; i++)
  2029. {
  2030. fmc_page_erase(app_newcode_add + i * 1024);
  2031. }
  2032. fmc_lock();*/
  2033. }
  2034. OTA_Reciv_Datalen = OTA_Reciv_Datalen + data_len;
  2035. // zzw ota must add
  2036. // app_newcode_add = OTA_NEW_CODE_START_ADD + fw_offset;
  2037. if (data_len == FW_SINGLE_PACKET_SIZE) // 整包数据
  2038. {
  2039. // zzw ota must add
  2040. /* for (i = 0; i < (FW_SINGLE_PACKET_SIZE / 4); i++)
  2041. {
  2042. write_data = 0;
  2043. write_data = data[i * 4 + 3];
  2044. write_data = (write_data << 8) + data[i * 4 + 2];
  2045. write_data = (write_data << 8) + data[i * 4 + 1];
  2046. write_data = (write_data << 8) + data[i * 4];
  2047. flash_word_write(app_newcode_add, write_data);
  2048. app_newcode_add = app_newcode_add + 4;
  2049. }*/
  2050. }
  2051. else // 最后一包非整包数据
  2052. {
  2053. temp = data_len % 4;
  2054. if (temp == 0)
  2055. {
  2056. // zzw ota must add
  2057. /*for (i = 0; i < (data_len / 4); i++)
  2058. {
  2059. write_data = 0;
  2060. write_data = data[i * 4 + 3];
  2061. write_data = (write_data << 8) + data[i * 4 + 2];
  2062. write_data = (write_data << 8) + data[i * 4 + 1];
  2063. write_data = (write_data << 8) + data[i * 4];
  2064. flash_word_write(app_newcode_add, write_data);
  2065. app_newcode_add = app_newcode_add + 4;
  2066. }*/
  2067. }
  2068. }
  2069. // if(OTA_Reciv_Datalen == ota_fw_info.mcu_ota_fw_size) //接收数据完成停止获取
  2070. // {
  2071. // update = 0;
  2072. // }
  2073. if (update) // 数据存储完毕,继续调用获取数据命令
  2074. {
  2075. mcu_ota_fw_request(); // 固件升级
  2076. }
  2077. // #error "请在该函数处理固件包数据,并删除该行"
  2078. }
  2079. #endif
  2080. #ifdef BEACON_TEST
  2081. /**
  2082. * @brief beacon test notify,which used in testing
  2083. * @param[in] {void}
  2084. * @return void
  2085. */
  2086. void mcu_received_beacon_test_handle(void)
  2087. {
  2088. unsigned short length = 0;
  2089. length = set_zigbee_uart_byte(length, 0x00);
  2090. zigbee_uart_write_frame(SEND_BEACON_NOTIFY_CMD, length);
  2091. // then start test
  2092. // add by zzw 产测
  2093. /*if(burn_in_test_timeout<=300)//开机30S内
  2094. {
  2095. burn_in_test_start=1;
  2096. //burn_in_test_counter=0;
  2097. }*/
  2098. }
  2099. #endif
  2100. #ifdef CHECK_ZIGBEE_NETWORK
  2101. /**
  2102. * @brief check zigbee nwkstate,before start join nwk, check nwk if state is not 0x02,can start jion,
  2103. * else delay 5s
  2104. *
  2105. * @param[in] {void}
  2106. * @return void
  2107. */
  2108. void mcu_check_zigbee_nwk_state(void)
  2109. {
  2110. unsigned short length = 0;
  2111. zigbee_uart_write_frame(CHECK_ZIGBEE_NETWORK_CMD, length);
  2112. // then start test
  2113. }
  2114. #endif
  2115. void zigbee_notify_factory_new_hanlde(void)
  2116. {
  2117. unsigned short length = 0;
  2118. length = set_zigbee_uart_byte(length, 0x01);
  2119. zigbee_uart_write_frame(ZIGBEE_FACTORY_NEW_CMD, length);
  2120. // then start test
  2121. // add by zzw 20240207 APP删除设备后自动入网开启
  2122. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  2123. ZG_JoinCounter = 0;
  2124. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  2125. // mcu_join_zigbee(); // 发送入网指令
  2126. switchState.zigbee_work_state = 0; // 设备被删除
  2127. }
  2128. #ifdef READ_DP_DATA_NOTIFY
  2129. /**
  2130. * @brief when gateway repower or relink clould, or zigbee module join sucess, repower, this commod will notify
  2131. * mcu, to sys dp data, mcu itself decide whether report.
  2132. *
  2133. * @param[in] {void}
  2134. * @return void
  2135. */
  2136. void read_dp_data_notify_hanlde(void)
  2137. {
  2138. unsigned short length = 0;
  2139. length = set_zigbee_uart_byte(length, 0x01);
  2140. zigbee_uart_write_frame(ZIGBEE_FACTORY_NEW_CMD, length);
  2141. // then start test
  2142. }
  2143. #endif
  2144. /*
  2145. * 函数:HexToDisHex
  2146. * 功能:将十六进制数转换成十六进制字符形式
  2147. * pbDest:存放目标十六进制字符串
  2148. * pbSrc:被转化的十六进制数
  2149. * nLen:被转化的十六进制数的长度
  2150. */
  2151. void HexToDisHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
  2152. {
  2153. unsigned char c0, c1;
  2154. unsigned char buf[2];
  2155. unsigned char *dst = pbDest;
  2156. unsigned char *src = pbSrc;
  2157. int index;
  2158. for (index = 0; index < nLen; index++)
  2159. {
  2160. c0 = *src >> 4;
  2161. if (c0 >= 0x0 && c0 <= 0x9)
  2162. {
  2163. buf[0] = c0 - 0 + '0';
  2164. }
  2165. else
  2166. {
  2167. buf[0] = c0 - 10 + 'a';
  2168. }
  2169. c1 = *src++ & 0x0F;
  2170. if (c1 >= 0x0 && c1 <= 0x9)
  2171. {
  2172. buf[1] = c1 - 0 + '0';
  2173. }
  2174. else
  2175. {
  2176. buf[1] = c1 - 10 + 'a';
  2177. }
  2178. *dst = buf[0];
  2179. dst++;
  2180. *dst = buf[1];
  2181. dst++;
  2182. }
  2183. }
  2184. /*
  2185. * DisHexToHex
  2186. * 功能:将字符串形式的十六进制数转换成十六进制
  2187. * pbDest:存放转换后的十六进制数
  2188. * pbSrc:源字符数组,存放要转换的十六进制字符串形式的数据
  2189. * nLen:被转化的十六进制数字符串数据的长度
  2190. */
  2191. void DisHexToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
  2192. {
  2193. unsigned char *dst = pbDest;
  2194. unsigned char *src = pbSrc;
  2195. unsigned char h1, h2;
  2196. unsigned char s1, s2;
  2197. int i;
  2198. for (i = 0; i < nLen; i++)
  2199. {
  2200. h1 = src[2 * i];
  2201. h2 = src[2 * i + 1];
  2202. s1 = toupper(h1) - 0x30;
  2203. if (s1 > 9)
  2204. s1 -= 7;
  2205. s2 = toupper(h2) - 0x30;
  2206. if (s2 > 9)
  2207. s2 -= 7;
  2208. *dst = (s1 << 4) + s2;
  2209. dst++;
  2210. }
  2211. }