protocol.c 85 KB

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