protocol.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. ////must modify
  12. #include "./mcu_sdk/zigbee.h" //directory by ddw
  13. //#include "GLOBAL.h"
  14. #include <SIM_EEPROM.h>
  15. #include "systick.h"
  16. #include "gd32f30x.h"
  17. #include "GLOBAL.h"
  18. #include "gpio_ctl.h"
  19. #include "uart.h"
  20. #include "key_proc.h"
  21. #include "lcd_task.h"
  22. #include "key_operate.h"
  23. #include "MB_RTU_deal_task.h"
  24. #include "gd32f30x_it.h"
  25. #include "All_define.h"
  26. #include "modbus.h"
  27. #include "MB_RTU_deal_task.h"
  28. #include "led.h"
  29. #include "MB_RTU_task.h"
  30. Zigbee_cmd zigbee_cmd;
  31. // #include "../uart.h"
  32. extern unsigned char gRelayOnOff;
  33. ////must modify
  34. /******************************************************************************
  35. 移植须知:
  36. 1:MCU必须在while中直接调用mcu_api.c内的zigbee_uart_service()函数
  37. 2:程序正常初始化完成后,建议不进行关串口中断,如必须关中断,关中断时间必须短,关中断会引起串口数据包丢失
  38. 3:请勿在中断/定时器中断内调用上报函数
  39. ******************************************************************************/
  40. /******************************************************************************
  41. 第一步:初始化
  42. 1:在需要使用到zigbee相关文件的文件中include "zigbee.h"
  43. 2:在MCU初始化中调用mcu_api.c文件中的zigbee_protocol_init()函数
  44. 3:将MCU串口单字节发送函数填入protocol.c文件中uart_transmit_output函数内,并删除#error
  45. 4:在MCU串口接收函数中调用mcu_api.c文件内的uart_receive_input函数,并将接收到的字节作为参数传入
  46. 5:单片机进入while循环后调用mcu_api.c文件内的zigbee_uart_service()函数
  47. 6:mcu何时开启zigbee模块配网,当zigbee模块查询到mcu的产品信息之后,或者zigbee模块发送了当前网络状态
  48. 给到mcu,查询pid信息会在上电5秒之后发送,对接部分zigbee老版本通用对接固件低功耗版本尤其需要注意不要
  49. 在第一次确认波特率的时候提前唤醒或者发送串口给zigbee模组,有概率导致zigbee模组波特率存储失败。
  50. 7:上电之后,mcu何时上报数据给网关合适,在收到网络状态为已配网,或者接收到读取dp数据的时候。
  51. 8: 在mcu ota过程中发送固件内容请求未收到zigbee模块的应答时,需要mcu端做好超时重新请求的处理,和存储image
  52. 处理,校验和可以自行设计或者使用参考。
  53. ******************************************************************************/
  54. /******************************************************************************
  55. 1:dp数据点序列类型对照表
  56. **此为自动生成代码,如在开发平台有相关修改请重新下载MCU_SDK**
  57. ******************************************************************************/
  58. #ifdef ZIGBEE_ZS_T4
  59. const DOWNLOAD_CMD_S download_cmd[] =
  60. {
  61. {DPID_SWITCH, DP_TYPE_BOOL},
  62. {DPID_MODE, DP_TYPE_ENUM},
  63. {DPID_WORK_STATE, DP_TYPE_ENUM},
  64. {DPID_TEMP_SET, DP_TYPE_VALUE},
  65. {DPID_TEMP_CURRENT, DP_TYPE_VALUE},
  66. {DPID_LEVEL, DP_TYPE_ENUM},
  67. {DPID_WORK_STATE1, DP_TYPE_ENUM},
  68. {DPID_TEMP_DN, DP_TYPE_VALUE},
  69. {DPID_SWITCH_FLOOR_HEAT, DP_TYPE_BOOL},
  70. {DPID_SWITCH_FAN, DP_TYPE_BOOL},
  71. {DPID_LEVEL_FAN, DP_TYPE_ENUM},
  72. };
  73. #endif
  74. #ifdef FLOORHEATER_T4_CS
  75. const DOWNLOAD_CMD_S download_cmd[] =
  76. {
  77. {DPID_SWITCH, DP_TYPE_BOOL},
  78. {DPID_MODE, DP_TYPE_ENUM},
  79. {DPID_WORK_STATE, DP_TYPE_ENUM},
  80. {DPID_TEMP_SET, DP_TYPE_VALUE},
  81. {DPID_TEMP_CURRENT, DP_TYPE_VALUE},
  82. {DPID_LEVEL, DP_TYPE_ENUM},
  83. {DPID_SET_LIGHT_3, DP_TYPE_ENUM},
  84. };
  85. #endif
  86. /******************************************************************************
  87. 2:串口单字节发送函数
  88. 请将MCU串口发送函数填入该函数内,并将接收到的数据作为参数传入串口发送函数
  89. ******************************************************************************/
  90. ////must modify
  91. extern void start_usart0_tx(unsigned char tx_buffer[], unsigned char length);
  92. ////must modify
  93. static void report_mcu_ota_result(unsigned char res);
  94. /**
  95. * @brief encapsulates a generic send function, developer should use their own function to completing this fuction
  96. * @param[in] {value} send signle data
  97. * @return void
  98. */
  99. ////must modify
  100. void uart_transmit_output(unsigned char value)
  101. {
  102. //printf("uart_transmit_output 0x%x\r\n",value);
  103. static unsigned char buf;
  104. buf = value;
  105. // #error "please use your own uart send fuction complete this fuction, exmaple"
  106. /*
  107. * //demo:
  108. * extern void Uart_PutChar(unsigned char value);
  109. * Uart_PutChar(value);
  110. */
  111. start_usart0_tx(&buf, 1); // 串口发送单字节函数
  112. }
  113. ////must modify
  114. /******************************************************************************
  115. 第二步:实现具体用户函数
  116. 1:APP下发数据处理
  117. 2:数据上报处理
  118. ******************************************************************************/
  119. /******************************************************************************
  120. 1:所有数据上报处理
  121. 当前函数处理全部数据上报(包括可下发/可上报和只上报)
  122. 需要用户按照实际情况实现:
  123. 1:需要实现可下发/可上报数据点上报
  124. 2:需要实现只上报数据点上报
  125. 此函数为MCU内部必须调用
  126. 用户也可调用此函数实现全部数据上报
  127. ******************************************************************************/
  128. // 自动化生成数据上报函数
  129. /**
  130. * @brief Upload all dp information of the system, and realize the synchronization of APP and muc data
  131. * @param[in] {void}
  132. * @return void
  133. */
  134. ////must modify
  135. #if (defined ZIGBEE_ZS_T4) || (defined ZIGBEE_ZS_T4_CS)
  136. // #if (defined ZIGBEE_ZS_T4) || (defined ZIGBEE_ZS_T3) || (defined ZIGBEE_ZS_T2) || (defined ZIGBEE_ZS_T1)
  137. void all_data_update(void)
  138. {
  139. /*dis_air_Conditioning = 0,
  140. dis_floor_heating ,
  141. dis_fresh_air ,
  142. * these fucntion will be generated by cloud platform, but when use these function must reference the function in mcu_api.c
  143. */
  144. //zigbee_cmd.Is_From_485=0;
  145. uint16_t level;
  146. if(zigbee_cmd.Is_From_485==1||zigbee_cmd.Is_zigbee_cmd==0)
  147. zigbee_cmd.work_state_cur=dis_page_num.display_page ;
  148. mcu_dp_enum_update(DPID_WORK_STATE1, zigbee_cmd.work_state_cur);
  149. zigbee_cmd.work_state_info.air_condition.switch_onoff=dis_page_param.face_plate_param.Air_condition_switch;
  150. zigbee_cmd.work_state_info.air_condition.mode=dis_page_param.face_plate_param.Air_condition_mode;
  151. zigbee_cmd.work_state_info.air_condition.temp_set=dis_page_param.face_plate_param.Air_set_temp;
  152. switch(dis_page_param.face_plate_param.Air_wind_speed)
  153. {
  154. case 0x0100:
  155. zigbee_cmd.work_state_info.air_condition.level=0x0100;
  156. level=0x00;
  157. break;
  158. case 0x0001:
  159. zigbee_cmd.work_state_info.air_condition.level=0x0001;
  160. level=0x01;
  161. break;
  162. case 0x0002:
  163. zigbee_cmd.work_state_info.air_condition.level=0x0002;
  164. level=0x02;
  165. break;
  166. case 0x0003:
  167. zigbee_cmd.work_state_info.air_condition.level=0x0003;
  168. level=0x03;
  169. break;
  170. default:
  171. break;
  172. }
  173. //zigbee_cmd.work_state_info.air_condition.level=dis_page_param.face_plate_param.Air_wind_speed;
  174. zigbee_cmd.work_state_info.floor_condition.switch_onoff=dis_page_param.face_plate_param.Floor_heating_switch;
  175. zigbee_cmd.work_state_info.floor_condition.temp_set=dis_page_param.face_plate_param.Floor_heating_set_temp;
  176. zigbee_cmd.work_state_info.fresh_air_condition.switch_onoff=dis_page_param.face_plate_param.Fresh_air_switch;
  177. zigbee_cmd.work_state_info.fresh_air_condition.level=dis_page_param.face_plate_param.Fresh_air_speed;
  178. switch(zigbee_cmd.work_state_cur)
  179. {
  180. case 0:
  181. mcu_dp_bool_update(DPID_SWITCH, zigbee_cmd.work_state_info.air_condition.switch_onoff);
  182. mcu_dp_enum_update(DPID_MODE, zigbee_cmd.work_state_info.air_condition.mode);
  183. mcu_dp_value_update(DPID_TEMP_SET, zigbee_cmd.work_state_info.air_condition.temp_set);
  184. mcu_dp_enum_update(DPID_LEVEL, level);
  185. break;
  186. case 1:
  187. mcu_dp_bool_update(DPID_SWITCH_FLOOR_HEAT, zigbee_cmd.work_state_info.floor_condition.switch_onoff);
  188. mcu_dp_value_update(DPID_TEMP_DN, zigbee_cmd.work_state_info.floor_condition.temp_set);
  189. break;
  190. case 2:
  191. mcu_dp_bool_update(DPID_SWITCH_FAN, zigbee_cmd.work_state_info.fresh_air_condition.switch_onoff);
  192. mcu_dp_enum_update(DPID_LEVEL_FAN, zigbee_cmd.work_state_info.fresh_air_condition.level);
  193. break;
  194. default:
  195. break;
  196. }
  197. mcu_dp_value_update(DPID_TEMP_CURRENT, dis_page_param.face_plate_param.Real_time_Temp);
  198. /* mcu_dp_bool_update(DPID_SWITCH, Smartbox_On_Off_State); // BOOL型数据上报;
  199. if (ModeType != 0)
  200. {
  201. mcu_dp_enum_update(DPID_MODE, ModeType - 1); // 枚举型数据上报;
  202. }
  203. else
  204. {
  205. mcu_dp_enum_update(DPID_MODE, ModeType); // 枚举型数据上报;
  206. }
  207. if (DeviceType != 0)
  208. {
  209. mcu_dp_enum_update(DPID_WORK_STATE, DeviceType - 1); // 枚举型数据上报;
  210. }
  211. else
  212. {
  213. mcu_dp_enum_update(DPID_WORK_STATE, DeviceType); // 枚举型数据上报;
  214. }
  215. mcu_dp_value_update(DPID_TEMP_SET, Set_Temperature_IntegerData / 2); // VALUE型数据上报;
  216. // mcu_dp_value_update(DPID_TEMP_CURRENT, Now_Temperature_IntegerData); // VALUE型数据上报;
  217. mcu_dp_value_update(DPID_TEMP_CURRENT, dis_page_param.face_plate_param.Real_time_Temp); // VALUE型数据上报;
  218. if (WindSpeedData != 0)
  219. {
  220. mcu_dp_enum_update(DPID_LEVEL, WindSpeedData - 1); // 枚举型数据上报;
  221. }
  222. else
  223. {
  224. mcu_dp_enum_update(DPID_LEVEL, WindSpeedData); // 枚举型数据上报;
  225. }
  226. mcu_dp_enum_update(DPID_WORK_STATE1,zigbee_cmd.work_state); //枚举型数据上报;*/
  227. }
  228. #endif
  229. /******************************************************************************
  230. WARNING!!!
  231. 2:所有数据上报处理
  232. 自动化代码模板函数,具体请用户自行实现数据处理
  233. ******************************************************************************/
  234. // #ifdef ZIGBEE_ZS_T4
  235. /*****************************************************************************
  236. 函数名称 : dp_download_switch_1_handle
  237. 功能描述 : 针对DPID_SWITCH_1的处理函数
  238. 输入参数 : value:数据源数据
  239. : length:数据长度
  240. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  241. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  242. *****************************************************************************/
  243. static unsigned char dp_download_switch_handle(const unsigned char value[], unsigned short length)
  244. {
  245. zigbee_cmd.Is_From_485=0;
  246. // 示例:当前DP类型为BOOL
  247. unsigned char ret;
  248. // 0:off/1:on
  249. unsigned char switch_1;
  250. //zigbee_cmd.Is_Out_sleep=1;
  251. //zigbee_cmd.Is_Wakeup=1;
  252. PullOutOf_SleepPage();
  253. zigbee_cmd.Is_zigbee_cmd=1;
  254. switch_1 = mcu_get_dp_download_bool(value, length);
  255. /* if (switch_1 == 0)
  256. {
  257. switch(zigbee_cmd.work_state_cur)
  258. {
  259. case 0:
  260. dis_page_param.face_plate_param.Open_device_mode = 0x00;
  261. dis_page_param.face_plate_param.Air_condition_switch = 0;
  262. Air_Open_data_Send();
  263. zigbee_cmd.work_state_info.air_condition.switch_onoff=0;
  264. break;
  265. case 1:
  266. dis_page_param.face_plate_param.Floor_heating_switch =0;
  267. Floor_heating_Open_data_Send();
  268. zigbee_cmd.work_state_info.floor_condition.switch_onoff=0;
  269. break;
  270. case 2:
  271. dis_page_param.face_plate_param.Fresh_air_switch=0;
  272. Fresh_air_Open_data_Send();
  273. zigbee_cmd.work_state_info.fresh_air_condition.switch_onoff=0;
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. else
  280. {
  281. switch(zigbee_cmd.work_state_cur)
  282. {
  283. case 0:
  284. dis_page_param.face_plate_param.Open_device_mode = 0x00;
  285. dis_page_param.face_plate_param.Air_condition_switch = 1;
  286. Air_Open_data_Send();
  287. zigbee_cmd.work_state_info.air_condition.switch_onoff=1;
  288. break;
  289. case 1:
  290. dis_page_param.face_plate_param.Floor_heating_switch =1;
  291. Floor_heating_Open_data_Send();
  292. zigbee_cmd.work_state_info.floor_condition.switch_onoff=1;
  293. break;
  294. case 2:
  295. dis_page_param.face_plate_param.Fresh_air_switch=1;
  296. Fresh_air_Open_data_Send();
  297. zigbee_cmd.work_state_info.fresh_air_condition.switch_onoff=1;
  298. break;
  299. default:
  300. break;
  301. }
  302. }*/
  303. if (switch_1 == 0)
  304. {
  305. dis_page_param.face_plate_param.Open_device_mode = 0x00;
  306. dis_page_param.face_plate_param.Air_condition_switch = 0;
  307. Air_Open_data_Send();
  308. zigbee_cmd.work_state_info.air_condition.switch_onoff=0;
  309. }
  310. else
  311. {
  312. zigbee_cmd.Is_clear_lcd=1;
  313. dis_page_param.face_plate_param.Open_device_mode = 0x00;
  314. dis_page_param.face_plate_param.Air_condition_switch = 1;
  315. Air_Open_data_Send();
  316. zigbee_cmd.work_state_info.air_condition.switch_onoff=1;
  317. }
  318. xTaskNotify((TaskHandle_t )LEDTask_Handler,
  319. (uint32_t )Wake_up,
  320. (eNotifyAction )eSetBits);
  321. // There should be a report after processing the DP
  322. ret = mcu_dp_bool_update(DPID_SWITCH, switch_1);
  323. if (ret == SUCCESS)
  324. return SUCCESS;
  325. else
  326. return ERROR;
  327. }
  328. /*****************************************************************************
  329. 函数名称 : dp_download_mode_handle
  330. 功能描述 : 针对DPID_MODE的处理函数
  331. 输入参数 : value:数据源数据
  332. : length:数据长度
  333. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  334. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  335. *****************************************************************************/
  336. static unsigned char dp_download_mode_handle(const unsigned char value[], unsigned short length)
  337. {
  338. zigbee_cmd.Is_From_485=0;
  339. // 示例:当前DP类型为ENUM
  340. unsigned char ret;
  341. unsigned char mode;
  342. PullOutOf_SleepPage();
  343. mode = mcu_get_dp_download_enum(value, length);
  344. zigbee_cmd.Is_zigbee_cmd=1;
  345. //zigbee_cmd.mode=mode;
  346. zigbee_cmd.Is_Out_sleep=1;
  347. switch(zigbee_cmd.work_state_cur)
  348. {
  349. case 0:
  350. zigbee_cmd.work_state_info.air_condition.mode=(uint32_t)mode;
  351. dis_page_param.face_plate_param.Air_condition_mode = (uint16_t)mode;
  352. Air_Mode_data_Send();
  353. break;
  354. default:
  355. break;
  356. }
  357. if(mode==2&&dis_page_param.face_plate_param.Air_wind_speed==0x0100)
  358. dis_page_param.face_plate_param.Air_wind_speed=0x0001;
  359. all_data_update();
  360. return SUCCESS;
  361. // ModeType = mode + 1; // 模式图标显示 value:1-制冷,2-制热,3-除湿,4-吹风, 0-关闭
  362. // There should be a report after processing the DP
  363. /* ret = mcu_dp_enum_update(DPID_MODE, mode);
  364. if (ret == SUCCESS)
  365. return SUCCESS;
  366. else
  367. return ERROR;*/
  368. }
  369. /*****************************************************************************
  370. 函数名称 : dp_download_temp_set_handle
  371. 功能描述 : 针对DPID_TEMP_SET的处理函数
  372. 输入参数 : value:数据源数据
  373. : length:数据长度
  374. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  375. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  376. *****************************************************************************/
  377. static unsigned char dp_download_temp_set_handle(const unsigned char value[], unsigned short length)
  378. {
  379. zigbee_cmd.Is_From_485=0;
  380. // 示例:当前DP类型为VALUE
  381. unsigned char ret;
  382. unsigned long temp_set;
  383. zigbee_cmd.Is_Out_sleep=1;
  384. temp_set = mcu_get_dp_download_value(value, length);
  385. PullOutOf_SleepPage();
  386. zigbee_cmd.Is_zigbee_cmd=1;
  387. /*
  388. //VALUE type data processing
  389. */
  390. switch(zigbee_cmd.work_state_cur)
  391. {
  392. case 0:
  393. zigbee_cmd.work_state_info.air_condition.temp_set=(uint32_t)temp_set;
  394. dis_page_param.face_plate_param.Air_set_temp=(uint16_t)temp_set;
  395. Air_set_temp_data_Send();
  396. break;
  397. case 1:
  398. //zigbee_cmd.work_state_info.floor_condition.temp_set=(uint32_t)temp_set;
  399. // dis_page_param.face_plate_param.Floor_heating_set_temp=(uint32_t)temp_set;
  400. //Floor_heating_set_temp_data_Send();
  401. break;
  402. default:
  403. break;
  404. }
  405. // Set_Temperature_IntegerData = temp_set * 2; ////设置温度整数部分
  406. // switchState.setcounter = 20; // 2秒倒计时
  407. // There should be a report after processing the DP
  408. ret = mcu_dp_value_update(DPID_TEMP_SET, temp_set);
  409. if (ret == SUCCESS)
  410. return SUCCESS;
  411. else
  412. return ERROR;
  413. }
  414. /*****************************************************************************
  415. 函数名称 : dp_download_level_handle
  416. 功能描述 : 针对DPID_LEVEL的处理函数
  417. 输入参数 : value:数据源数据
  418. : length:数据长度
  419. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  420. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  421. *****************************************************************************/
  422. static unsigned char dp_download_level_handle(const unsigned char value[], unsigned short length)
  423. {
  424. zigbee_cmd.Is_From_485=0;
  425. // 示例:当前DP类型为ENUM
  426. unsigned char ret;
  427. unsigned char level;
  428. zigbee_cmd.Is_Out_sleep=1;
  429. zigbee_cmd.Is_zigbee_cmd=1;
  430. PullOutOf_SleepPage();
  431. level = mcu_get_dp_download_enum(value, length);
  432. //printf("\r\n112122331\r\n");
  433. switch(zigbee_cmd.work_state_cur)
  434. {
  435. case 0:
  436. dis_page_num.display_page=dis_air_Conditioning;
  437. zigbee_cmd.work_state_info.air_condition.level=level;
  438. switch(level)
  439. {
  440. case 0:
  441. zigbee_cmd.work_state_info.air_condition.level=0x0100;
  442. dis_page_param.face_plate_param.Air_wind_speed=0x0100;
  443. Air_speed_data_Send();
  444. // Air_speed_data_Sendtemp(0x01,0x00);
  445. break;
  446. case 1:
  447. zigbee_cmd.work_state_info.air_condition.level=0x0001;
  448. dis_page_param.face_plate_param.Air_wind_speed=0x0001;
  449. Air_speed_data_Send();
  450. // Air_speed_data_Sendtemp(0x00,0x01);
  451. break;
  452. case 2:
  453. zigbee_cmd.work_state_info.air_condition.level=0x0002;
  454. dis_page_param.face_plate_param.Air_wind_speed=0x0002;
  455. Air_speed_data_Send();
  456. // Air_speed_data_Sendtemp(0x00,0x02);
  457. break;
  458. case 3:
  459. zigbee_cmd.work_state_info.air_condition.level=0x0003;
  460. dis_page_param.face_plate_param.Air_wind_speed=0x0003;
  461. Air_speed_data_Send();
  462. // Air_speed_data_Sendtemp(0x00,0x03);
  463. break;
  464. default:
  465. break;
  466. }
  467. break;
  468. case 2:
  469. zigbee_cmd.work_state_info.fresh_air_condition.level=level;
  470. switch(level)
  471. {
  472. case 0:
  473. dis_page_param.face_plate_param.Fresh_air_speed=0;
  474. Fresh_air_speed_data_Sendtemp(0x01,0x00);
  475. break;
  476. case 1:
  477. dis_page_param.face_plate_param.Fresh_air_speed=1;
  478. Fresh_air_speed_data_Sendtemp(0x00,0x01);
  479. break;
  480. case 2:
  481. dis_page_param.face_plate_param.Fresh_air_speed=2;
  482. Fresh_air_speed_data_Sendtemp(0x00,0x02);
  483. break;
  484. case 3:
  485. dis_page_param.face_plate_param.Fresh_air_speed=3;
  486. Fresh_air_speed_data_Sendtemp(0x00,0x03);
  487. break;
  488. default:
  489. break;
  490. }
  491. break;
  492. default:
  493. break;
  494. }
  495. // WindSpeedData = level + 1; // 风速显示界面, value,1-自动,2-低,3-中,4-高,0-关闭所有图标
  496. // There should be a report after processing the DP
  497. ret = mcu_dp_enum_update(DPID_LEVEL, level);
  498. if (ret == SUCCESS)
  499. return SUCCESS;
  500. else
  501. return ERROR;
  502. }
  503. /*****************************************************************************
  504. 函数名称 : dp_download_work_state1_handle
  505. 功能描述 : 针对DPID_WORK_STATE1的处理函数
  506. 输入参数 : value:数据源数据
  507. : length:数据长度
  508. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  509. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  510. *****************************************************************************/
  511. static unsigned char dp_download_work_state1_handle(const unsigned char value[], unsigned short length)
  512. {
  513. zigbee_cmd.Is_From_485=0;
  514. unsigned char ret;
  515. unsigned char work_state1;
  516. zigbee_cmd.Is_Out_sleep=1;
  517. work_state1 = mcu_get_dp_download_enum(value, length);
  518. zigbee_cmd.Is_zigbee_cmd=1;
  519. PullOutOf_SleepPage();
  520. zigbee_cmd.work_state_cur=work_state1;
  521. switch (work_state1)
  522. {
  523. case 0:
  524. break;
  525. case 1:
  526. break;
  527. case 2:
  528. break;
  529. case 3:
  530. break;
  531. default:
  532. break;
  533. }
  534. all_data_update();
  535. return SUCCESS;
  536. //There should be a report after processing the DP
  537. // ret = mcu_dp_enum_update(DPID_WORK_STATE1, work_state1);
  538. // if(ret == SUCCESS)
  539. // return SUCCESS;
  540. // else
  541. // return ERROR;
  542. }
  543. /*****************************************************************************
  544. 函数名称 : dp_download_temp_dn_handle
  545. 功能描述 : 针对DPID_TEMP_DN的处理函数
  546. 输入参数 : value:数据源数据
  547. : length:数据长度
  548. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  549. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  550. *****************************************************************************/
  551. static unsigned char dp_download_temp_dn_handle(const unsigned char value[], unsigned short length)
  552. {
  553. // 示例:当前DP类型为VALUE
  554. unsigned char ret;
  555. unsigned long temp_set;
  556. zigbee_cmd.Is_Out_sleep=1;
  557. zigbee_cmd.Is_zigbee_cmd=1;
  558. temp_set = mcu_get_dp_download_value(value, length);
  559. PullOutOf_SleepPage();
  560. /*
  561. //VALUE type data processing
  562. */
  563. switch(zigbee_cmd.work_state_cur)
  564. {
  565. case 0:
  566. //zigbee_cmd.work_state_info.air_condition.temp_set=(uint32_t)temp_set;
  567. // dis_page_param.face_plate_param.Air_set_temp=(uint16_t)temp_set;
  568. // Air_set_temp_data_Send();
  569. break;
  570. case 1:
  571. zigbee_cmd.work_state_info.floor_condition.temp_set=(uint32_t)temp_set;
  572. dis_page_param.face_plate_param.Floor_heating_set_temp=(uint32_t)temp_set;
  573. Floor_heating_set_temp_data_Send();
  574. break;
  575. default:
  576. break;
  577. }
  578. // Set_Temperature_IntegerData = temp_set * 2; ////设置温度整数部分
  579. // switchState.setcounter = 20; // 2秒倒计时
  580. // There should be a report after processing the DP
  581. ret = mcu_dp_value_update(DPID_TEMP_DN, temp_set);
  582. if (ret == SUCCESS)
  583. return SUCCESS;
  584. else
  585. return ERROR;
  586. }
  587. /*****************************************************************************
  588. 函数名称 : dp_download_switch_floor_heat_handle
  589. 功能描述 : 针对DPID_SWITCH_FLOOR_HEAT的处理函数
  590. 输入参数 : value:数据源数据
  591. : length:数据长度
  592. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  593. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  594. *****************************************************************************/
  595. static unsigned char dp_download_switch_floor_heat_handle(const unsigned char value[], unsigned short length)
  596. {
  597. zigbee_cmd.Is_From_485=0;
  598. // 示例:当前DP类型为BOOL
  599. unsigned char ret;
  600. // 0:off/1:on
  601. unsigned char switch_1;
  602. //zigbee_cmd.Is_Out_sleep=1;
  603. //zigbee_cmd.Is_Wakeup=1;
  604. PullOutOf_SleepPage();
  605. zigbee_cmd.Is_zigbee_cmd=1;
  606. switch_1 = mcu_get_dp_download_bool(value, length);
  607. if (switch_1 == 0)
  608. {
  609. dis_page_param.face_plate_param.Floor_heating_switch =0;
  610. Floor_heating_Open_data_Send();
  611. zigbee_cmd.work_state_info.floor_condition.switch_onoff=0;
  612. }
  613. else
  614. {
  615. zigbee_cmd.Is_clear_lcd=1;
  616. dis_page_param.face_plate_param.Floor_heating_switch =1;
  617. Floor_heating_Open_data_Send();
  618. zigbee_cmd.work_state_info.floor_condition.switch_onoff=1;
  619. }
  620. xTaskNotify((TaskHandle_t )LEDTask_Handler,
  621. (uint32_t )Wake_up,
  622. (eNotifyAction )eSetBits);
  623. // There should be a report after processing the DP
  624. ret = mcu_dp_bool_update(DPID_SWITCH_FLOOR_HEAT, switch_1);
  625. if (ret == SUCCESS)
  626. return SUCCESS;
  627. else
  628. return ERROR;
  629. }
  630. /*****************************************************************************
  631. 函数名称 : dp_download_switch_fan_handle
  632. 功能描述 : 针对DPID_SWITCH_FAN的处理函数
  633. 输入参数 : value:数据源数据
  634. : length:数据长度
  635. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  636. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  637. *****************************************************************************/
  638. static unsigned char dp_download_switch_fan_handle(const unsigned char value[], unsigned short length)
  639. {
  640. zigbee_cmd.Is_From_485=0;
  641. // 示例:当前DP类型为BOOL
  642. unsigned char ret;
  643. // 0:off/1:on
  644. unsigned char switch_1;
  645. //zigbee_cmd.Is_Out_sleep=1;
  646. //zigbee_cmd.Is_Wakeup=1;
  647. PullOutOf_SleepPage();
  648. zigbee_cmd.Is_zigbee_cmd=1;
  649. switch_1 = mcu_get_dp_download_bool(value, length);
  650. if (switch_1 == 0)
  651. {
  652. dis_page_param.face_plate_param.Fresh_air_switch=0;
  653. Fresh_air_Open_data_Send();
  654. zigbee_cmd.work_state_info.fresh_air_condition.switch_onoff=0;
  655. }
  656. else
  657. {
  658. zigbee_cmd.Is_clear_lcd=1;
  659. dis_page_param.face_plate_param.Fresh_air_switch=1;
  660. Fresh_air_Open_data_Send();
  661. zigbee_cmd.work_state_info.fresh_air_condition.switch_onoff=1;
  662. }
  663. xTaskNotify((TaskHandle_t )LEDTask_Handler,
  664. (uint32_t )Wake_up,
  665. (eNotifyAction )eSetBits);
  666. // There should be a report after processing the DP
  667. ret = mcu_dp_bool_update(DPID_SWITCH_FAN, switch_1);
  668. if (ret == SUCCESS)
  669. return SUCCESS;
  670. else
  671. return ERROR;
  672. }
  673. /*****************************************************************************
  674. 函数名称 : dp_download_level_fan_handle
  675. 功能描述 : 针对DPID_LEVEL_FAN的处理函数
  676. 输入参数 : value:数据源数据
  677. : length:数据长度
  678. 返回参数 : 成功返回:SUCCESS/失败返回:ERROR
  679. 使用说明 : 可下发可上报类型,需要在处理完数据后上报处理结果至app
  680. *****************************************************************************/
  681. static unsigned char dp_download_level_fan_handle(const unsigned char value[], unsigned short length)
  682. {
  683. zigbee_cmd.Is_From_485=0;
  684. // 示例:当前DP类型为ENUM
  685. unsigned char ret;
  686. unsigned char level;
  687. zigbee_cmd.Is_Out_sleep=1;
  688. zigbee_cmd.Is_zigbee_cmd=1;
  689. PullOutOf_SleepPage();
  690. level = mcu_get_dp_download_enum(value, length);
  691. //printf("\r\n112122331\r\n");
  692. zigbee_cmd.work_state_info.fresh_air_condition.level=level;
  693. switch(level)
  694. {
  695. case 0:
  696. dis_page_param.face_plate_param.Fresh_air_speed=0;
  697. Fresh_air_speed_data_Sendtemp(0x01,0x00);
  698. break;
  699. case 1:
  700. dis_page_param.face_plate_param.Fresh_air_speed=1;
  701. Fresh_air_speed_data_Sendtemp(0x00,0x01);
  702. break;
  703. case 2:
  704. dis_page_param.face_plate_param.Fresh_air_speed=2;
  705. Fresh_air_speed_data_Sendtemp(0x00,0x02);
  706. break;
  707. case 3:
  708. dis_page_param.face_plate_param.Fresh_air_speed=3;
  709. Fresh_air_speed_data_Sendtemp(0x00,0x03);
  710. break;
  711. default:
  712. break;
  713. }
  714. zigbee_cmd.work_state_info.fresh_air_condition.level=level;
  715. // WindSpeedData = level + 1; // 风速显示界面, value,1-自动,2-低,3-中,4-高,0-关闭所有图标
  716. // There should be a report after processing the DP
  717. ret = mcu_dp_enum_update(DPID_LEVEL_FAN, level);
  718. if (ret == SUCCESS)
  719. return SUCCESS;
  720. else
  721. return ERROR;
  722. }
  723. #if (defined ZIGBEE_ZS_T4) || (defined ZIGBEE_ZS_T4_CS)
  724. #endif
  725. #ifdef SUPPORT_MCU_RTC_CHECK
  726. /**
  727. * @brief mcu check local RTC time
  728. * @param[in] {time} timestamp
  729. * @return void
  730. */
  731. void mcu_write_rtctime(unsigned char time[])
  732. {
  733. #error "mcu should realize RTC time wtriting fuction, and delete this line"
  734. /*
  735. time[0]~time[3]:standard time
  736. time[4]~time[7]: Local time
  737. */
  738. my_memcpy((void *)timestamp, (const char *)time, 4); // get timestamp
  739. zigbee_timestamp_to_time();
  740. /*
  741. year = _time.w_year; //year
  742. month = _time.w_month; //month
  743. date = _time.w_date; //date
  744. hour = _time.hour + 8; //hour(8:BeiJing time)
  745. min = _time.min; //minute
  746. sec = _time.sec; //second
  747. */
  748. }
  749. #endif
  750. /**
  751. * @brief Zigbee functional test feedback
  752. * @param[in] {void}
  753. * @return void
  754. */
  755. void zigbee_test_result(void)
  756. {
  757. ////must modify
  758. // #error "this test is makesure the rf fuction of zigbee module, if test pass or not should do something, mcu should realize"
  759. unsigned char rssi = zigbee_uart_rx_buf[DATA_START + 1];
  760. if (zigbee_uart_rx_buf[DATA_START] == 0x01)
  761. {
  762. if (rssi > 0x3C)
  763. {
  764. // test sucess the range of rssi is 0% ~ 100%
  765. }
  766. else
  767. {
  768. // test failure
  769. }
  770. }
  771. else
  772. {
  773. // test failure
  774. }
  775. }
  776. /******************************************************************************
  777. WARNING!!!
  778. 以下函数用户请勿修改!!
  779. ******************************************************************************/
  780. /**
  781. * @brief this function will handle uart received frame data
  782. * @param[in] {dpid} dp id
  783. * @param[in] {value} dp data
  784. * @param[in] {length} lenght of dp data
  785. * @return handle result
  786. */
  787. // #ifdef ZIGBEE_ZS_T4
  788. #if (defined ZIGBEE_ZS_T4) || (defined ZIGBEE_ZS_T4_CS)
  789. unsigned char dp_download_handle(unsigned char dpid, const unsigned char value[], unsigned short length)
  790. {
  791. /* only list of function, mcu need realize these fuction*/
  792. unsigned char ret;
  793. switch(dpid){
  794. case DPID_SWITCH:
  795. //开关处理函数
  796. ret = dp_download_switch_handle(value,length);
  797. break;
  798. case DPID_MODE:
  799. //工作模式处理函数
  800. ret = dp_download_mode_handle(value,length);
  801. break;
  802. case DPID_TEMP_SET:
  803. //目标温度处理函数
  804. ret = dp_download_temp_set_handle(value,length);
  805. break;
  806. case DPID_LEVEL:
  807. //档位处理函数
  808. ret = dp_download_level_handle(value,length);
  809. break;
  810. case DPID_WORK_STATE1:
  811. //工作状态处理函数
  812. ret = dp_download_work_state1_handle(value,length);
  813. break;
  814. case DPID_TEMP_DN:
  815. //地暖目标温度处理函数
  816. ret = dp_download_temp_dn_handle(value,length);
  817. break;
  818. case DPID_SWITCH_FLOOR_HEAT:
  819. //地暖开关处理函数
  820. ret = dp_download_switch_floor_heat_handle(value,length);
  821. break;
  822. case DPID_SWITCH_FAN:
  823. //新风开关处理函数
  824. ret = dp_download_switch_fan_handle(value,length);
  825. break;
  826. case DPID_LEVEL_FAN:
  827. //新风风速档位处理函数
  828. ret = dp_download_level_fan_handle(value,length);
  829. break;
  830. default:
  831. break;}
  832. return ret;
  833. }
  834. #endif
  835. /**
  836. * @brief get received cmd total number
  837. * @param[in] {void}
  838. * @return received cmd total number
  839. */
  840. unsigned char get_download_cmd_total(void)
  841. {
  842. return (sizeof(download_cmd) / sizeof(download_cmd[0]));
  843. }
  844. /**
  845. * @brief received zigbee net_work state handle
  846. * @param[in] {zigbee_work_state} zigbee current network state
  847. * @return void
  848. */
  849. void zigbee_work_state_event(unsigned char zigbee_work_state)
  850. {
  851. unsigned short length = 0;
  852. zigbee_uart_write_frame(ZIGBEE_STATE_CMD, length);
  853. switch (zigbee_work_state)
  854. {
  855. case ZIGBEE_NOT_JION: // 设备不在网
  856. // if(ZG_Joining == CONDITION_BEFOR_JOINING_TO_JOINING)
  857. // {
  858. // switchState.zigbee_work_state = 0;
  859. // switchState.time = 1000; // 10秒
  860. // ledSetParam(25, 200);
  861. // }
  862. break;
  863. case ZIGBEE_JOIN_GATEWAY: // 入网成功
  864. Is_blink=0;
  865. // ZG_JoinCounter = 1000; // 入网成功,入网计数器置最大值
  866. //switchState.zigbee_work_state = 1;
  867. break;
  868. case ZIGBEE_JOIN_ERROR:
  869. switchState.zigbee_work_state = 0;
  870. switchState.time = 1000; // 10秒
  871. ledSetParam(25, 200);
  872. break;
  873. case ZIGBEE_JOINING:
  874. break;
  875. default:
  876. break;
  877. }
  878. }
  879. /**
  880. * @brief received reset zigbee response
  881. * @param[in] {state} response state
  882. * @return void
  883. */
  884. void mcu_reset_zigbee_event(unsigned char state)
  885. {
  886. switch (state)
  887. {
  888. case RESET_ZIGBEE_OK:
  889. break;
  890. case RESET_ZIGBEE_ERROR:
  891. break;
  892. default:
  893. break;
  894. }
  895. }
  896. /**
  897. * @brief check mcu version response
  898. * @param[in] {void}
  899. * @return void
  900. */
  901. void response_mcu_ota_version_event(void)
  902. {
  903. unsigned short length = 0;
  904. length = set_zigbee_uart_byte(length, get_current_mcu_fw_ver()); // current fw version
  905. zigbee_uart_write_frame(MCU_OTA_VERSION_CMD, length);
  906. }
  907. #ifdef SUPPORT_MCU_OTA
  908. /**
  909. * @brief mcu ota update notify response
  910. * @param[in] {offset} data offset
  911. * @return void
  912. */
  913. void response_mcu_ota_notify_event(unsigned char offset)
  914. {
  915. unsigned char i = 0;
  916. unsigned short length = 0;
  917. unsigned char update = 0;
  918. unsigned long app_newcode_add = 0;
  919. unsigned char j = 0;
  920. uint32_t temp_buff[256];
  921. int k=0;
  922. current_mcu_fw_pid(); // current PID
  923. while (i < 8)
  924. {
  925. ota_fw_info.mcu_ota_pid[i] = zigbee_uart_rx_buf[offset + DATA_START + i]; // ota fw PID
  926. i++;
  927. }
  928. ota_fw_info.mcu_ota_ver = zigbee_uart_rx_buf[offset + DATA_START + 8]; // ota fw version
  929. ota_fw_info.mcu_ota_fw_size = zigbee_uart_rx_buf[offset + DATA_START + 9] << 24 |
  930. zigbee_uart_rx_buf[offset + DATA_START + 10] << 16 |
  931. zigbee_uart_rx_buf[offset + DATA_START + 11] << 8 |
  932. zigbee_uart_rx_buf[offset + DATA_START + 12]; // ota fw size
  933. ota_fw_info.mcu_ota_checksum = zigbee_uart_rx_buf[offset + DATA_START + 13] << 24 |
  934. zigbee_uart_rx_buf[offset + DATA_START + 14] << 16 |
  935. zigbee_uart_rx_buf[offset + DATA_START + 15] << 8 |
  936. zigbee_uart_rx_buf[offset + DATA_START + 16]; // ota fw checksum
  937. if ((!strcmp_barry(&ota_fw_info.mcu_ota_pid[0], &current_mcu_pid[0], 8)) &&
  938. (ota_fw_info.mcu_ota_ver > get_current_mcu_fw_ver() &&
  939. ota_fw_info.mcu_ota_fw_size > 0))
  940. { // check fw pid and fw version and fw size
  941. length = set_zigbee_uart_byte(length, 0x00); // OK
  942. update = 1; // 校验通过,可进行固件升级
  943. }
  944. else
  945. {
  946. length = set_zigbee_uart_byte(length, 0x01); // error
  947. }
  948. ota_fw_info.mcu_current_offset = 0;
  949. zigbee_uart_write_frame(MCU_OTA_NOTIFY_CMD, length);
  950. delay_1ms(20);
  951. // add by zzw 升级通知接收成功,第一次调用接收固件命令
  952. if (update)
  953. {
  954. // 在读取数据之前先对FLASH进行擦除,从第38页开始
  955. app_newcode_add = OTA_NEW_CODE_START_ADD;
  956. fmc_unlock();
  957. /*for (k = 0; k < 256; k++)
  958. {
  959. temp_buff[k] = flash_word_read(OTA_STATE_DATA_ADD + (4 * k)); // 1.取出程序运行区代码数据
  960. }
  961. temp_buff[0] = 0;//参数初始化
  962. //delay_1ms(10);
  963. flash_page_write(OTA_STATE_DATA_ADD, temp_buff); // 2.将取出的运行区代码写入临时区
  964. delay_1ms(20);*/
  965. for (i = 0; i < 26; i++)
  966. {
  967. fmc_page_erase(app_newcode_add + i * 1024);
  968. }
  969. fmc_lock();
  970. mcu_ota_fw_request(); // 固件升级
  971. }
  972. }
  973. /**
  974. * @brief received mcu ota data request response
  975. * @param[in] {fw_offset} offset of file
  976. * @param[in] {data} received data
  977. * @return void
  978. */
  979. /*void reveived_mcu_ota_data_handle(unsigned int fw_offset, char *data)
  980. {
  981. // "received frame data, should save in flash, mcu should realize this fuction, and delete this line "
  982. //未使用
  983. }*/
  984. /**
  985. * @brief mcu send ota data request
  986. * @param[in] {void}
  987. * @return void
  988. */
  989. void mcu_ota_fw_request_event(unsigned char offset)
  990. {
  991. unsigned int fw_offset = 0;
  992. char fw_data[FW_SINGLE_PACKET_SIZE] = {-1}; //
  993. unsigned char i = 0;
  994. unsigned char last_package_len = 0;
  995. static unsigned int current_checksum = 0;
  996. if (zigbee_uart_rx_buf[offset + DATA_START] == 0x01) // status check
  997. return;
  998. while (i < 8)
  999. {
  1000. if (current_mcu_pid[i] != zigbee_uart_rx_buf[offset + DATA_START + 1 + i]) // pid check
  1001. return;
  1002. i++;
  1003. }
  1004. if (ota_fw_info.mcu_ota_ver != zigbee_uart_rx_buf[offset + DATA_START + 9]) // version check
  1005. return;
  1006. i = 0;
  1007. while (i < 4)
  1008. {
  1009. fw_offset |= (zigbee_uart_rx_buf[offset + DATA_START + 10 + i] << (24 - i * 8)); // offset
  1010. i++;
  1011. }
  1012. i = 0;
  1013. if (ota_fw_info.mcu_current_offset == fw_offset)
  1014. {
  1015. // if ((ota_fw_info.mcu_ota_fw_size - fw_offset) / FW_SINGLE_PACKET_SIZE != 0)
  1016. 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)))
  1017. // 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))
  1018. {
  1019. if (ota_fw_info.mcu_current_offset == 0)
  1020. {
  1021. current_checksum = 0;
  1022. }
  1023. while (i < FW_SINGLE_PACKET_SIZE)
  1024. {
  1025. fw_data[i] = zigbee_uart_rx_buf[offset + DATA_START + 14 + i]; // fw data
  1026. current_checksum += fw_data[i];
  1027. i++;
  1028. }
  1029. ota_fw_info.mcu_current_offset += FW_SINGLE_PACKET_SIZE;
  1030. ota_fw_data_handle(fw_offset, &fw_data[0], FW_SINGLE_PACKET_SIZE); // OTA paket data handle
  1031. }
  1032. else
  1033. {
  1034. i = 0;
  1035. // last_package_len = ota_fw_info.mcu_ota_fw_size - fw_offset;
  1036. if ((ota_fw_info.mcu_ota_fw_size - fw_offset) % FW_SINGLE_PACKET_SIZE != 0)
  1037. {
  1038. last_package_len = ota_fw_info.mcu_ota_fw_size - fw_offset;
  1039. }
  1040. else
  1041. {
  1042. last_package_len = FW_SINGLE_PACKET_SIZE;
  1043. }
  1044. while (i < last_package_len)
  1045. {
  1046. fw_data[i] = zigbee_uart_rx_buf[offset + DATA_START + 14 + i];
  1047. current_checksum += fw_data[i];
  1048. i++;
  1049. }
  1050. ota_fw_info.mcu_current_offset += last_package_len;
  1051. if (ota_fw_info.mcu_ota_checksum != current_checksum)
  1052. {
  1053. // ota failure report ota failure and clear ota struct
  1054. mcu_ota_result_report(1); // failed
  1055. }
  1056. else
  1057. {
  1058. // ota sucess
  1059. mcu_ota_result_report(0); // seccess
  1060. }
  1061. ota_fw_data_handle(fw_offset, &fw_data[0], last_package_len); // OTA paket data handle
  1062. current_checksum = 0;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. // ota request timeout, then restart ota request from ota_fw_info.mcu_ota_fw_size
  1068. }
  1069. }
  1070. static void report_mcu_ota_result(unsigned char res)
  1071. {
  1072. unsigned short length;
  1073. if ((res == 0) || (res == 1))
  1074. {
  1075. length = set_zigbee_uart_byte(length, res);
  1076. zigbee_uart_write_frame(MCU_OTA_NOTIFY_CMD, length);
  1077. }
  1078. }
  1079. /**
  1080. * @brief mcu ota data result notify
  1081. * @param[in] {void}
  1082. * @return void
  1083. */
  1084. void mcu_ota_result_event(unsigned char offset)
  1085. {
  1086. unsigned short k, Ret;
  1087. uint32_t temp_buff[256];
  1088. unsigned char status = zigbee_uart_rx_buf[offset + DATA_START];
  1089. if (status == 0x00)
  1090. {
  1091. // 升级成功,用户提示
  1092. IAPUpdateFlag = 1;
  1093. /*for (k = 0; k < 256; k++)
  1094. {
  1095. temp_buff[k] = flash_word_read(OTA_STATE_DATA_ADD + (4 * k)); // 1.取出程序运行区代码数据
  1096. }
  1097. temp_buff[0] = IAPUpdateFlag;
  1098. delay_1ms(10);
  1099. Ret = flash_page_write(OTA_STATE_DATA_ADD, temp_buff); // 2.将取出的运行区代码写入临时区
  1100. delay_1ms(20);*/
  1101. flash_word_write((OTA_STATE_DATA_ADD), IAPUpdateFlag);
  1102. /*if(Ret == 0)
  1103. {
  1104. Ret = 0;
  1105. }*/
  1106. // flash_word_write (OTA_STATE_DATA_ADD,IAPUpdateFlag);
  1107. delay_1ms(500);
  1108. NVIC_SystemReset(); // add by zzw 系统复位函数
  1109. }
  1110. else if (status == 0x01)
  1111. {
  1112. }
  1113. }
  1114. /**
  1115. * @brief mcu ota data handle
  1116. * @param[in] {fw_offset} frame offset
  1117. * @param[in] {data} received data
  1118. * @return void
  1119. */
  1120. void ota_fw_data_handle(unsigned int fw_offset, char *data, unsigned char data_len) // 一整包数据48字节
  1121. {
  1122. unsigned short i = 0;
  1123. unsigned short update = 1;
  1124. unsigned long app_newcode_add = 0; // 读flash的时候的地址变量,新代码区
  1125. unsigned short temp = 0;
  1126. unsigned long write_data;
  1127. //"请在该函数处理固件包数据"
  1128. //"received frame data, should save in flash, mcu should realize this fuction, and delete this line "
  1129. // 取出数据存入falsh,建议延用‘ota_fw_info’全局变量,不需定义其他全局变量,可维护数据包长度等
  1130. if (fw_offset == 0)
  1131. {
  1132. app_newcode_add = OTA_NEW_CODE_START_ADD;
  1133. /*fmc_unlock();
  1134. for (i = 0; i < 26; i++)
  1135. {
  1136. fmc_page_erase(app_newcode_add + i * 1024);
  1137. }
  1138. fmc_lock();*/
  1139. }
  1140. OTA_Reciv_Datalen = OTA_Reciv_Datalen + data_len;
  1141. app_newcode_add = OTA_NEW_CODE_START_ADD + fw_offset;
  1142. if (data_len == FW_SINGLE_PACKET_SIZE) // 整包数据
  1143. {
  1144. for (i = 0; i < (FW_SINGLE_PACKET_SIZE / 4); i++)
  1145. {
  1146. write_data = 0;
  1147. write_data = data[i * 4 + 3];
  1148. write_data = (write_data << 8) + data[i * 4 + 2];
  1149. write_data = (write_data << 8) + data[i * 4 + 1];
  1150. write_data = (write_data << 8) + data[i * 4];
  1151. flash_word_write(app_newcode_add, write_data);
  1152. app_newcode_add = app_newcode_add + 4;
  1153. }
  1154. }
  1155. else // 最后一包非整包数据
  1156. {
  1157. temp = data_len % 4;
  1158. if (temp == 0)
  1159. {
  1160. for (i = 0; i < (data_len / 4); i++)
  1161. {
  1162. write_data = 0;
  1163. write_data = data[i * 4 + 3];
  1164. write_data = (write_data << 8) + data[i * 4 + 2];
  1165. write_data = (write_data << 8) + data[i * 4 + 1];
  1166. write_data = (write_data << 8) + data[i * 4];
  1167. flash_word_write(app_newcode_add, write_data);
  1168. app_newcode_add = app_newcode_add + 4;
  1169. }
  1170. }
  1171. }
  1172. // if(OTA_Reciv_Datalen == ota_fw_info.mcu_ota_fw_size) //接收数据完成停止获取
  1173. // {
  1174. // update = 0;
  1175. // }
  1176. if (update) // 数据存储完毕,继续调用获取数据命令
  1177. {
  1178. mcu_ota_fw_request(); // 固件升级
  1179. }
  1180. }
  1181. #endif
  1182. #ifdef BEACON_TEST
  1183. /**
  1184. * @brief beacon test notify,which used in testing
  1185. * @param[in] {void}
  1186. * @return void
  1187. */
  1188. void mcu_received_beacon_test_handle(void)
  1189. {
  1190. unsigned short length = 0;
  1191. length = set_zigbee_uart_byte(length, 0x00);
  1192. zigbee_uart_write_frame(SEND_BEACON_NOTIFY_CMD, length);
  1193. // then start test
  1194. }
  1195. #endif
  1196. #ifdef CHECK_ZIGBEE_NETWORK
  1197. /**
  1198. * @brief check zigbee nwkstate,before start join nwk, check nwk if state is not 0x02,can start jion,
  1199. * else delay 5s
  1200. *
  1201. * @param[in] {void}
  1202. * @return void
  1203. */
  1204. void mcu_check_zigbee_nwk_state(void)
  1205. {
  1206. unsigned short length = 0;
  1207. zigbee_uart_write_frame(CHECK_ZIGBEE_NETWORK_CMD, length);
  1208. // then start test
  1209. }
  1210. #endif
  1211. void zigbee_notify_factory_new_hanlde(void)
  1212. {
  1213. unsigned short length = 0;
  1214. length = set_zigbee_uart_byte(length, 0x01);
  1215. zigbee_uart_write_frame(ZIGBEE_FACTORY_NEW_CMD, length);
  1216. // then start test
  1217. }
  1218. #ifdef READ_DP_DATA_NOTIFY
  1219. /**
  1220. * @brief when gateway repower or relink clould, or zigbee module join sucess, repower, this commod will notify
  1221. * mcu, to sys dp data, mcu itself decide whether report.
  1222. *
  1223. * @param[in] {void}
  1224. * @return void
  1225. */
  1226. void read_dp_data_notify_hanlde(void)
  1227. {
  1228. unsigned short length = 0;
  1229. length = set_zigbee_uart_byte(length, 0x01);
  1230. zigbee_uart_write_frame(ZIGBEE_FACTORY_NEW_CMD, length);
  1231. // then start test
  1232. }
  1233. #endif