SerialApp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "AF.h"
  7. #include "OnBoard.h"
  8. #include "OSAL_Tasks.h"
  9. #include "SerialApp.h"
  10. #include "ZDApp.h"
  11. #include "ZDObject.h"
  12. #include "ZDProfile.h"
  13. #include "hal_drivers.h"
  14. #include "hal_key.h"
  15. #if defined ( LCD_SUPPORTED )
  16. #include "hal_lcd.h"
  17. #endif
  18. #include "hal_led.h"
  19. #include "hal_uart.h"
  20. #include "DHT11.h"
  21. #include "nwk_globals.h"
  22. /*********************************************************************
  23. * MACROS
  24. */
  25. #define COORD_ADDR 0x00
  26. #define ED_ADDR 0x01
  27. #define UART0 0x00
  28. #define MAX_NODE 0x04
  29. #define UART_DEBUG 0x01 //调试宏,通过串口输出协调器和终端的IEEE、短地址
  30. #define LAMP_PIN P0_4 //定义P0.4口为继电器输入端
  31. #define GAS_PIN P0_5 //定义P0.5口为烟雾传感器的输入端
  32. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr)[0])
  33. //---------------------------------------------------------------------
  34. //标准版不同的终端需要修改此ID,用于识别协调器发过来的数据,ID相同则处理
  35. //专业版自动从Flash获得地址,所有终端固件相同,适合量产
  36. static uint16 EndDeviceID = 0x0001; //终端ID,重要
  37. //---------------------------------------------------------------------
  38. /*********************************************************************
  39. * CONSTANTS
  40. */
  41. #if !defined( SERIAL_APP_PORT )
  42. #define SERIAL_APP_PORT 0
  43. #endif
  44. #if !defined( SERIAL_APP_BAUD )
  45. #define SERIAL_APP_BAUD HAL_UART_BR_38400
  46. //#define SERIAL_APP_BAUD HAL_UART_BR_115200
  47. #endif
  48. // When the Rx buf space is less than this threshold, invoke the Rx callback.
  49. #if !defined( SERIAL_APP_THRESH )
  50. #define SERIAL_APP_THRESH 64
  51. #endif
  52. #if !defined( SERIAL_APP_RX_SZ )
  53. #define SERIAL_APP_RX_SZ 128
  54. #endif
  55. #if !defined( SERIAL_APP_TX_SZ )
  56. #define SERIAL_APP_TX_SZ 128
  57. #endif
  58. // Millisecs of idle time after a byte is received before invoking Rx callback.
  59. #if !defined( SERIAL_APP_IDLE )
  60. #define SERIAL_APP_IDLE 6
  61. #endif
  62. // Loopback Rx bytes to Tx for throughput testing.
  63. #if !defined( SERIAL_APP_LOOPBACK )
  64. #define SERIAL_APP_LOOPBACK FALSE
  65. #endif
  66. // This is the max byte count per OTA message.
  67. #if !defined( SERIAL_APP_TX_MAX )
  68. #define SERIAL_APP_TX_MAX 20
  69. #endif
  70. #define SERIAL_APP_RSP_CNT 4
  71. // This list should be filled with Application specific Cluster IDs.
  72. const cId_t SerialApp_ClusterList[SERIALAPP_MAX_CLUSTERS] =
  73. {
  74. SERIALAPP_CLUSTERID
  75. };
  76. const SimpleDescriptionFormat_t SerialApp_SimpleDesc =
  77. {
  78. SERIALAPP_ENDPOINT, // int Endpoint;
  79. SERIALAPP_PROFID, // uint16 AppProfId[2];
  80. SERIALAPP_DEVICEID, // uint16 AppDeviceId[2];
  81. SERIALAPP_DEVICE_VERSION, // int AppDevVer:4;
  82. SERIALAPP_FLAGS, // int AppFlags:4;
  83. SERIALAPP_MAX_CLUSTERS, // byte AppNumInClusters;
  84. (cId_t *)SerialApp_ClusterList, // byte *pAppInClusterList;
  85. SERIALAPP_MAX_CLUSTERS, // byte AppNumOutClusters;
  86. (cId_t *)SerialApp_ClusterList // byte *pAppOutClusterList;
  87. };
  88. const endPointDesc_t SerialApp_epDesc =
  89. {
  90. SERIALAPP_ENDPOINT,
  91. &SerialApp_TaskID,
  92. (SimpleDescriptionFormat_t *)&SerialApp_SimpleDesc,
  93. noLatencyReqs
  94. };
  95. /*********************************************************************
  96. * TYPEDEFS
  97. */
  98. /*********************************************************************
  99. * GLOBAL VARIABLES
  100. */
  101. uint8 SerialApp_TaskID; // Task ID for internal task/event processing.
  102. /*********************************************************************
  103. * EXTERNAL VARIABLES
  104. */
  105. /*********************************************************************
  106. * EXTERNAL FUNCTIONS
  107. */
  108. /*********************************************************************
  109. * LOCAL VARIABLES
  110. */
  111. static bool SendFlag = 0;
  112. static uint8 SerialApp_MsgID;
  113. static afAddrType_t SerialApp_TxAddr;
  114. static afAddrType_t Broadcast_DstAddr;
  115. static uint8 SerialApp_TxSeq;
  116. static uint8 SerialApp_TxBuf[SERIAL_APP_TX_MAX+1];
  117. static uint8 SerialApp_TxLen;
  118. static afAddrType_t SerialApp_RxAddr;
  119. static uint8 SerialApp_RspBuf[SERIAL_APP_RSP_CNT];
  120. static devStates_t SerialApp_NwkState;
  121. static afAddrType_t SerialApp_TxAddr;
  122. static uint8 SerialApp_MsgID;
  123. uint8 NodeData[MAX_NODE][5]; //终端数据缓冲区 0=温度 1=湿度 2=气体 3=灯
  124. /*********************************************************************
  125. * LOCAL FUNCTIONS
  126. */
  127. static void SerialApp_HandleKeys( uint8 shift, uint8 keys );
  128. static void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt );
  129. static void SerialApp_Send(void);
  130. static void SerialApp_Resp(void);
  131. static void SerialApp_CallBack(uint8 port, uint8 event);
  132. static void PrintAddrInfo(uint16 shortAddr, uint8 *pIeeeAddr);
  133. static void AfSendAddrInfo(void);
  134. static void GetIeeeAddr(uint8 * pIeeeAddr, uint8 *pStr);
  135. static void SerialApp_SendPeriodicMessage( void );
  136. static uint8 GetDataLen(uint8 fc);
  137. static uint8 GetLamp( void );
  138. static uint8 GetGas( void );
  139. static uint8 XorCheckSum(uint8 * pBuf, uint8 len);
  140. uint8 SendData(uint8 addr, uint8 FC);
  141. /*********************************************************************
  142. * @fn SerialApp_Init
  143. *
  144. * @brief This is called during OSAL tasks' initialization.
  145. *
  146. * @param task_id - the Task ID assigned by OSAL.
  147. *
  148. * @return none
  149. */
  150. void SerialApp_Init( uint8 task_id )
  151. {
  152. halUARTCfg_t uartConfig;
  153. P0SEL &= 0xEf; //设置P0.4口为普通IO
  154. P0DIR |= 0x10; //设置P0.4为输出
  155. LAMP_PIN = 1; //高电平继电器断开;低电平继电器吸合
  156. P0SEL &= ~0x20; //设置P0.5为普通IO口
  157. P0DIR &= ~0x20; //P0.5定义为输入口
  158. P0SEL &= 0x7f; //P0_7配置成通用io
  159. SerialApp_TaskID = task_id;
  160. //SerialApp_RxSeq = 0xC3;
  161. afRegister( (endPointDesc_t *)&SerialApp_epDesc );
  162. RegisterForKeys( task_id );
  163. uartConfig.configured = TRUE; // 2x30 don't care - see uart driver.
  164. uartConfig.baudRate = SERIAL_APP_BAUD;
  165. uartConfig.flowControl = FALSE;
  166. uartConfig.flowControlThreshold = SERIAL_APP_THRESH; // 2x30 don't care - see uart driver.
  167. uartConfig.rx.maxBufSize = SERIAL_APP_RX_SZ; // 2x30 don't care - see uart driver.
  168. uartConfig.tx.maxBufSize = SERIAL_APP_TX_SZ; // 2x30 don't care - see uart driver.
  169. uartConfig.idleTimeout = SERIAL_APP_IDLE; // 2x30 don't care - see uart driver.
  170. uartConfig.intEnable = TRUE; // 2x30 don't care - see uart driver.
  171. uartConfig.callBackFunc = SerialApp_CallBack;
  172. HalUARTOpen (UART0, &uartConfig);
  173. #if defined ( LCD_SUPPORTED )
  174. HalLcdWriteString( "SerialApp", HAL_LCD_LINE_2 );
  175. #endif
  176. //HalUARTWrite(UART0, "Init", 4);
  177. //ZDO_RegisterForZDOMsg( SerialApp_TaskID, End_Device_Bind_rsp );
  178. //ZDO_RegisterForZDOMsg( SerialApp_TaskID, Match_Desc_rsp );
  179. }
  180. /*********************************************************************
  181. * @fn SerialApp_ProcessEvent
  182. *
  183. * @brief Generic Application Task event processor.
  184. *
  185. * @param task_id - The OSAL assigned task ID.
  186. * @param events - Bit map of events to process.
  187. *
  188. * @return Event flags of all unprocessed events.
  189. */
  190. UINT16 SerialApp_ProcessEvent( uint8 task_id, UINT16 events )
  191. {
  192. (void)task_id; // Intentionally unreferenced parameter
  193. if ( events & SYS_EVENT_MSG )
  194. {
  195. afIncomingMSGPacket_t *MSGpkt;
  196. while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SerialApp_TaskID )) )
  197. {
  198. switch ( MSGpkt->hdr.event )
  199. {
  200. case ZDO_CB_MSG:
  201. //SerialApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
  202. break;
  203. case KEY_CHANGE:
  204. SerialApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
  205. break;
  206. case AF_INCOMING_MSG_CMD:
  207. SerialApp_ProcessMSGCmd( MSGpkt );
  208. break;
  209. case ZDO_STATE_CHANGE:
  210. SerialApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
  211. if ( (SerialApp_NwkState == DEV_ZB_COORD)
  212. || (SerialApp_NwkState == DEV_ROUTER)
  213. || (SerialApp_NwkState == DEV_END_DEVICE) )
  214. {
  215. #if defined(ZDO_COORDINATOR) //协调器通过串口输出自身短地址、IEEE
  216. Broadcast_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
  217. Broadcast_DstAddr.endPoint = SERIALAPP_ENDPOINT;
  218. Broadcast_DstAddr.addr.shortAddr = 0xFFFF;
  219. #if UART_DEBUG
  220. PrintAddrInfo( NLME_GetShortAddr(), aExtendedAddress + Z_EXTADDR_LEN - 1);
  221. #endif
  222. //初始化灯的状态,1为熄灭状态,0为点亮
  223. NodeData[0][3] = 1;
  224. NodeData[1][3] = 1;
  225. NodeData[2][3] = 1;
  226. NodeData[3][3] = 1;
  227. #else //终端无线发送短地址、IEEE
  228. AfSendAddrInfo();
  229. #endif
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. osal_msg_deallocate( (uint8 *)MSGpkt );
  236. }
  237. return ( events ^ SYS_EVENT_MSG );
  238. }
  239. //在此事件中可以定时向协调器发送节点传感器参数信息
  240. if ( events & SERIALAPP_SEND_PERIODIC_EVT )
  241. {
  242. SerialApp_SendPeriodicMessage();
  243. osal_start_timerEx( SerialApp_TaskID, SERIALAPP_SEND_PERIODIC_EVT,
  244. (SERIALAPP_SEND_PERIODIC_TIMEOUT + (osal_rand() & 0x00FF)) );
  245. return (events ^ SERIALAPP_SEND_PERIODIC_EVT);
  246. }
  247. if ( events & SERIALAPP_SEND_EVT )
  248. {
  249. SerialApp_Send();
  250. return ( events ^ SERIALAPP_SEND_EVT );
  251. }
  252. if ( events & SERIALAPP_RESP_EVT )
  253. {
  254. SerialApp_Resp();
  255. return ( events ^ SERIALAPP_RESP_EVT );
  256. }
  257. return ( 0 );
  258. }
  259. /*********************************************************************
  260. * @fn SerialApp_HandleKeys
  261. *
  262. * @brief Handles all key events for this device.
  263. *
  264. * @param shift - true if in shift/alt.
  265. * @param keys - bit field for key events.
  266. *
  267. * @return none
  268. */
  269. void SerialApp_HandleKeys( uint8 shift, uint8 keys )
  270. {
  271. zAddrType_t txAddr;
  272. if ( keys & HAL_KEY_SW_6 ) //按S1键启动或停止终端定时上报数据
  273. {
  274. if(SendFlag == 0)
  275. {
  276. SendFlag = 1;
  277. HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
  278. osal_start_timerEx( SerialApp_TaskID,
  279. SERIALAPP_SEND_PERIODIC_EVT,
  280. SERIALAPP_SEND_PERIODIC_TIMEOUT );
  281. }
  282. else
  283. {
  284. SendFlag = 0;
  285. HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
  286. osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_SEND_PERIODIC_EVT);
  287. }
  288. }
  289. if ( keys & HAL_KEY_SW_1 ) //按S2
  290. {
  291. LAMP_PIN = ~LAMP_PIN;
  292. }
  293. if ( keys & HAL_KEY_SW_2 )
  294. {
  295. HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
  296. // Initiate an End Device Bind Request for the mandatory endpoint
  297. txAddr.addrMode = Addr16Bit;
  298. txAddr.addr.shortAddr = 0x0000; // Coordinator
  299. ZDP_EndDeviceBindReq( &txAddr, NLME_GetShortAddr(),
  300. SerialApp_epDesc.endPoint,
  301. SERIALAPP_PROFID,
  302. SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList,
  303. SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList,
  304. FALSE );
  305. }
  306. if ( keys & HAL_KEY_SW_3 )
  307. {
  308. }
  309. if ( keys & HAL_KEY_SW_4 )
  310. {
  311. HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
  312. // Initiate a Match Description Request (Service Discovery)
  313. txAddr.addrMode = AddrBroadcast;
  314. txAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
  315. ZDP_MatchDescReq( &txAddr, NWK_BROADCAST_SHORTADDR,
  316. SERIALAPP_PROFID,
  317. SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList,
  318. SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList,
  319. FALSE );
  320. }
  321. }
  322. void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
  323. {
  324. uint16 shortAddr;
  325. uint8 *pIeeeAddr;
  326. uint8 delay;
  327. uint8 afRxData[30]={0};
  328. //查询单个终端上所有传感器的数据 3A 00 01 02 39 23 响应:3A 00 01 02 00 00 00 00 xor 23
  329. switch ( pkt->clusterId )
  330. {
  331. // A message with a serial data block to be transmitted on the serial port.
  332. case SERIALAPP_CLUSTERID:
  333. osal_memcpy(afRxData, pkt->cmd.Data, pkt->cmd.DataLength);
  334. switch(afRxData[0]) //简单协议命令字解析
  335. {
  336. #if defined(ZDO_COORDINATOR)
  337. case 0x3B: //收到终端无线发过来的短地址和IEEE地址,通过串口输出显示
  338. shortAddr=(afRxData[1]<<8)|afRxData[2];
  339. pIeeeAddr = &afRxData[3];
  340. #if UART_DEBUG
  341. PrintAddrInfo(shortAddr, pIeeeAddr + Z_EXTADDR_LEN - 1);
  342. #endif
  343. break;
  344. case 0x3A:
  345. if(afRxData[3] == 0x02) //收到终端传过来的传感器数据并保存
  346. {
  347. NodeData[afRxData[2]-1][0] = afRxData[4];
  348. NodeData[afRxData[2]-1][1] = afRxData[5];
  349. NodeData[afRxData[2]-1][2] = afRxData[6];
  350. NodeData[afRxData[2]-1][3] = afRxData[7];
  351. NodeData[afRxData[2]-1][4] = 0x00;
  352. }
  353. #if UART_DEBUG
  354. HalUARTWrite (UART0, NodeData[afRxData[3]-1], 4); //调试时通过串口输出
  355. HalUARTWrite (UART0, "\n", 1);
  356. #endif
  357. break;
  358. #else
  359. case 0x3A: //开关灯设备
  360. if(afRxData[3] == 0x0A || afRxData[3] == 0x0B || afRxData[3] == 0x0C) //控制终端
  361. {
  362. if(EndDeviceID == afRxData[2] || afRxData[2]==0xFF)
  363. {
  364. if(afRxData[4] == 0)
  365. {
  366. LAMP_PIN = 0;
  367. HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
  368. }
  369. else
  370. {
  371. LAMP_PIN = 1;
  372. HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
  373. }
  374. }
  375. break;
  376. }
  377. #endif
  378. default :
  379. break;
  380. }
  381. break;
  382. // A response to a received serial data block.
  383. case SERIALAPP_CLUSTERID2:
  384. if ((pkt->cmd.Data[1] == SerialApp_TxSeq) &&
  385. ((pkt->cmd.Data[0] == OTA_SUCCESS) || (pkt->cmd.Data[0] == OTA_DUP_MSG)))
  386. {
  387. SerialApp_TxLen = 0;
  388. osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_SEND_EVT);
  389. }
  390. else
  391. {
  392. // Re-start timeout according to delay sent from other device.
  393. delay = BUILD_UINT16( pkt->cmd.Data[2], pkt->cmd.Data[3] );
  394. osal_start_timerEx( SerialApp_TaskID, SERIALAPP_SEND_EVT, delay );
  395. }
  396. break;
  397. default:
  398. break;
  399. }
  400. }
  401. uint8 TxBuffer[128];
  402. uint8 SendData(uint8 addr, uint8 FC)
  403. {
  404. uint8 ret, i, index=4;
  405. TxBuffer[0] = 0x3A;
  406. TxBuffer[1] = 0x00;
  407. TxBuffer[2] = addr;
  408. TxBuffer[3] = FC;
  409. switch(FC)
  410. {
  411. case 0x01: //查询所有终端传感器的数据
  412. for (i=0; i<MAX_NODE; i++)
  413. {
  414. osal_memcpy(&TxBuffer[index], NodeData[i], 4);
  415. index += 4;
  416. }
  417. TxBuffer[index] = XorCheckSum(TxBuffer, index);
  418. TxBuffer[index+1] = 0x23;
  419. HalUARTWrite(UART0, TxBuffer, index+2);
  420. ret = 1;
  421. break;
  422. case 0x02: //查询单个终端上所有传感器的数据
  423. osal_memcpy(&TxBuffer[index], NodeData[addr-1], 4);
  424. index += 4;
  425. TxBuffer[index] = XorCheckSum(TxBuffer, index);
  426. TxBuffer[index+1] = 0x23;
  427. HalUARTWrite(UART0, TxBuffer, index+2);
  428. ret = 1;
  429. break;
  430. default:
  431. ret = 0;
  432. break;
  433. }
  434. return ret;
  435. }
  436. /*********************************************************************
  437. * @fn SerialApp_Send
  438. *
  439. * @brief Send data OTA.
  440. *
  441. * @param none
  442. *
  443. * @return none
  444. */
  445. static void SerialApp_Send(void)
  446. {
  447. uint8 len=0, addr, FC;
  448. uint8 checksum=0;
  449. #if SERIAL_APP_LOOPBACK
  450. if (SerialApp_TxLen < SERIAL_APP_TX_MAX)
  451. {
  452. SerialApp_TxLen += HalUARTRead(SERIAL_APP_PORT, SerialApp_TxBuf+SerialApp_TxLen+1,
  453. SERIAL_APP_TX_MAX-SerialApp_TxLen);
  454. }
  455. if (SerialApp_TxLen)
  456. {
  457. (void)SerialApp_TxAddr;
  458. if (HalUARTWrite(SERIAL_APP_PORT, SerialApp_TxBuf+1, SerialApp_TxLen))
  459. {
  460. SerialApp_TxLen = 0;
  461. }
  462. else
  463. {
  464. osal_set_event(SerialApp_TaskID, SERIALAPP_SEND_EVT);
  465. }
  466. }
  467. #else
  468. if (!SerialApp_TxLen &&
  469. (SerialApp_TxLen = HalUARTRead(UART0, SerialApp_TxBuf, SERIAL_APP_TX_MAX)))
  470. {
  471. if (SerialApp_TxLen)
  472. {
  473. SerialApp_TxLen = 0;
  474. if(SerialApp_TxBuf[0] == 0x3A)
  475. {
  476. addr = SerialApp_TxBuf[2];
  477. FC = SerialApp_TxBuf[3];
  478. len = GetDataLen(FC);
  479. len += 4;
  480. checksum = XorCheckSum(SerialApp_TxBuf, len);
  481. //接收数据正确返回相应数据
  482. if(checksum == SerialApp_TxBuf[len] && SerialApp_TxBuf[len+1] == 0x23)
  483. {
  484. if(FC == 0x0A || FC == 0x0B || FC == 0x0C) //控制终端
  485. {
  486. if (afStatus_SUCCESS == AF_DataRequest(&Broadcast_DstAddr,
  487. (endPointDesc_t *)&SerialApp_epDesc,
  488. SERIALAPP_CLUSTERID,
  489. len+2, SerialApp_TxBuf,
  490. &SerialApp_MsgID, 0, AF_DEFAULT_RADIUS))
  491. {
  492. if(FC == 0x0A) //如果开启自动刷新则不需要这步操作
  493. NodeData[addr-1][3] = SerialApp_TxBuf[len-1]; //更新缓冲区灯的状态
  494. HalUARTWrite(UART0, SerialApp_TxBuf, len+2); //无线发送成功后原样返回给上位机
  495. //osal_set_event(SerialApp_TaskID, SERIALAPP_SEND_EVT);
  496. }
  497. else //暂时没发现错误,关闭终端发送也正常。无线发送失败后将数据位和校验位置0返给上位机
  498. {
  499. SerialApp_TxBuf[len-1] = 0x00;
  500. SerialApp_TxBuf[len] = 0x00;
  501. HalUARTWrite(UART0, SerialApp_TxBuf, len+2);
  502. }
  503. }
  504. else
  505. {
  506. SendData(addr, FC); //查询操作
  507. }
  508. }
  509. }
  510. }
  511. }
  512. #endif
  513. }
  514. /*********************************************************************
  515. * @fn SerialApp_Resp
  516. *
  517. * @brief Send data OTA.
  518. *
  519. * @param none
  520. *
  521. * @return none
  522. */
  523. static void SerialApp_Resp(void)
  524. {
  525. if (afStatus_SUCCESS != AF_DataRequest(&SerialApp_RxAddr,
  526. (endPointDesc_t *)&SerialApp_epDesc,
  527. SERIALAPP_CLUSTERID2,
  528. SERIAL_APP_RSP_CNT, SerialApp_RspBuf,
  529. &SerialApp_MsgID, 0, AF_DEFAULT_RADIUS))
  530. {
  531. osal_set_event(SerialApp_TaskID, SERIALAPP_RESP_EVT);
  532. }
  533. }
  534. /*********************************************************************
  535. * @fn SerialApp_CallBack
  536. *
  537. * @brief Send data OTA.
  538. *
  539. * @param port - UART port.
  540. * @param event - the UART port event flag.
  541. *
  542. * @return none
  543. */
  544. static void SerialApp_CallBack(uint8 port, uint8 event)
  545. {
  546. (void)port;
  547. if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
  548. #if SERIAL_APP_LOOPBACK
  549. (SerialApp_TxLen < SERIAL_APP_TX_MAX))
  550. #else
  551. !SerialApp_TxLen)
  552. #endif
  553. {
  554. SerialApp_Send();
  555. }
  556. }
  557. //------------------------------------------------------------------------------------------------------------------------------------------
  558. //查询单个终端上所有传感器的数据 3A 00 01 02 XX 23 响应:3A 00 01 02 00 00 00 00 xor 23
  559. void SerialApp_SendPeriodicMessage( void )
  560. {
  561. uint8 SendBuf[11]={0};
  562. SendBuf[0] = 0x3A;
  563. SendBuf[1] = HI_UINT16( EndDeviceID );
  564. SendBuf[2] = LO_UINT16( EndDeviceID );
  565. SendBuf[3] = 0x02; //FC
  566. Delay_ms(500);
  567. DHT11(); //获取温湿度
  568. Delay_ms(500);
  569. SendBuf[4] = wendu;
  570. SendBuf[5] = shidu;
  571. SendBuf[6] = GetGas(); //获取气体传感器的状态
  572. SendBuf[7] = GetLamp(); //获得灯的状态
  573. SendBuf[8] = XorCheckSum(SendBuf, 9);
  574. SendBuf[9] = 0x23;
  575. SerialApp_TxAddr.addrMode = (afAddrMode_t)Addr16Bit;
  576. SerialApp_TxAddr.endPoint = SERIALAPP_ENDPOINT;
  577. SerialApp_TxAddr.addr.shortAddr = 0x00;
  578. if ( AF_DataRequest( &SerialApp_TxAddr, (endPointDesc_t *)&SerialApp_epDesc,
  579. SERIALAPP_CLUSTERID,
  580. 10,
  581. SendBuf,
  582. &SerialApp_MsgID,
  583. 0,
  584. AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  585. {
  586. // Successfully requested to be sent.
  587. }
  588. else
  589. {
  590. // Error occurred in request to send.
  591. }
  592. }
  593. //通过串口输出短地址 IEEE
  594. void PrintAddrInfo(uint16 shortAddr, uint8 *pIeeeAddr)
  595. {
  596. uint8 strIeeeAddr[17] = {0};
  597. char buff[30] = {0};
  598. //获得短地址
  599. sprintf(buff, "shortAddr:%04X IEEE:", shortAddr);
  600. //获得IEEE地址
  601. GetIeeeAddr(pIeeeAddr, strIeeeAddr);
  602. HalUARTWrite (UART0, (uint8 *)buff, strlen(buff));
  603. Delay_ms(10);
  604. HalUARTWrite (UART0, strIeeeAddr, 16);
  605. HalUARTWrite (UART0, "\n", 1);
  606. }
  607. void AfSendAddrInfo(void)
  608. {
  609. uint16 shortAddr;
  610. uint8 strBuf[11]={0};
  611. SerialApp_TxAddr.addrMode = (afAddrMode_t)Addr16Bit;
  612. SerialApp_TxAddr.endPoint = SERIALAPP_ENDPOINT;
  613. SerialApp_TxAddr.addr.shortAddr = 0x00;
  614. shortAddr=NLME_GetShortAddr();
  615. strBuf[0] = 0x3B; //发送地址给协调器 可用于点播
  616. strBuf[1] = HI_UINT16( shortAddr ); //存放短地址高8位
  617. strBuf[2] = LO_UINT16( shortAddr ); //存放短地址低8位
  618. osal_memcpy(&strBuf[3], NLME_GetExtAddr(), 8);
  619. if ( AF_DataRequest( &SerialApp_TxAddr, (endPointDesc_t *)&SerialApp_epDesc,
  620. SERIALAPP_CLUSTERID,
  621. 11,
  622. strBuf,
  623. &SerialApp_MsgID,
  624. 0,
  625. AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  626. {
  627. }
  628. else
  629. {
  630. // Error occurred in request to send.
  631. }
  632. }
  633. void GetIeeeAddr(uint8 * pIeeeAddr, uint8 *pStr)
  634. {
  635. uint8 i;
  636. uint8 *xad = pIeeeAddr;
  637. for (i = 0; i < Z_EXTADDR_LEN*2; xad--)
  638. {
  639. uint8 ch;
  640. ch = (*xad >> 4) & 0x0F;
  641. *pStr++ = ch + (( ch < 10 ) ? '0' : '7');
  642. i++;
  643. ch = *xad & 0x0F;
  644. *pStr++ = ch + (( ch < 10 ) ? '0' : '7');
  645. i++;
  646. }
  647. }
  648. uint8 XorCheckSum(uint8 * pBuf, uint8 len)
  649. {
  650. uint8 i;
  651. uint8 byRet=0;
  652. if(len == 0)
  653. return byRet;
  654. else
  655. byRet = pBuf[0];
  656. for(i = 1; i < len; i ++)
  657. byRet = byRet ^ pBuf[i];
  658. return byRet;
  659. }
  660. uint8 GetDataLen(uint8 fc)
  661. {
  662. uint8 len=0;
  663. switch(fc)
  664. {
  665. case 0x0A:
  666. case 0x0B:
  667. case 0x0C:
  668. case 0x0D:
  669. len = 1;
  670. break;
  671. }
  672. return len;
  673. }
  674. //获得P0_4 继电器引脚的电平
  675. uint8 GetLamp( void )
  676. {
  677. uint8 ret;
  678. if(LAMP_PIN == 0)
  679. ret = 0;
  680. else
  681. ret = 1;
  682. return ret;
  683. }
  684. //获得P0_5 MQ-2气体传感器的数据
  685. uint8 GetGas( void )
  686. {
  687. uint8 ret;
  688. if(GAS_PIN == 0)
  689. ret = 0;
  690. else
  691. ret = 1;
  692. return ret;
  693. }
  694. //-------------------------------------------------------------------
  695. /*********************************************************************
  696. *********************************************************************/