protocol.c 87 KB

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