Пример #1
0
/*********************************************************************************************
函数名:writeEpdat
参  数:无
返回值:无
备  注:EEPROM  读数据
**********************************************************************************************/
void writeEpdat(unsigned int addr, unsigned char* dat, unsigned char len)
{
	unsigned char i;
	
	//清除数据
	IapEraseSector(addr);
	for(i = 0; i < len; i++)
	{
		if(IapReadByte(addr + i) != 0xff)
		{
			UART_Send_String("ERASE ERR!");
		}
	}

	//写入数据
	for(i = 0; i < len; i++)
	{
		IapProgramByte(addr + i, (unsigned char)dat[i]);
	}
}
Пример #2
0
void sysPingRsp(void)
{
  uint8 pBuf[7];
  
  // Start of Frame Delimiter
  pBuf[0] = CPT_SOP;
  
  // Length
  pBuf[1] = 2; 
  
  // Command type
  pBuf[2] = LO_UINT16(SYS_PING_RESPONSE); 
  pBuf[3] = HI_UINT16(SYS_PING_RESPONSE);
  
  // Stack profile
  pBuf[4] = LO_UINT16(STACK_PROFILE);
  pBuf[4+ 1] = HI_UINT16(STACK_PROFILE);
  
  // Frame Check Sequence
  pBuf[7 - 1] = calcFCS(&pBuf[1], (7 - 2));
  
  // Write frame to UART
  UART_Send_String (pBuf, 7);
}
Пример #3
0
/*********************************************************************
 * @fn      GenericApp_MessageMSGCB
 *
 * @brief   Data message processor callback.  This function processes
 *          any incoming data - probably from other devices.  So, based
 *          on cluster ID, perform the intended action.
 *
 * @param   none
 *
 * @return  none
 */
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  unsigned char buff[12];
  char*p;
  byte nr;
  uint8 i;
  uint8 ass_num;
  uint8* ieeeaddr;
  
  
  uint8 yy1;
  uint16 devlist[ NWK_MAX_DEVICES + 1];
  
  afAddrType_t myaddr; // use for p2p
  
  ass_num = 0;
  
  switch ( pkt->clusterId )
  {
    case GENERICAPP_CLUSTERID:
      //sprintf(buff,"%hu", pkt->cmd.DataLength);
     ///if( p[4] =='l' && p[5] =='e' && p[6] =='d' && p[7] =='1')
     // {
//       uprint( (uint8*)pkt->srcAddr );
//        uprint( (uint8*)pkt->endPoint);
       // UART_Send_String( &pkt->LinkQuality, 1);
        //uprint( pkt->cmd.Data);
     // }
     //UART_Send_String( (uint8*)&pkt->cmd.DataLength, 1);
     //UART_Send_String( &pkt->cmd.TransSeqNumber,1 );

    if( pkt->cmd.DataLength == 1)
    {      
        yy1 = 1;
#if !defined( ZDO_COORDINATOR ) 
        if(pkt->cmd.Data[0] == 0x6d)
        {
          /// return mac address : ac xx xx ieee addr
               myaddr.addrMode = (afAddrMode_t)Addr16Bit;
               myaddr.endPoint = GENERICAPP_ENDPOINT;          
               myaddr.addr.shortAddr = 0x0000;
               
            ieeeaddr = NLME_GetExtAddr();
            osal_cpyExtAddr(buff+3, ieeeaddr);
            buff[0] = 0xac;
            buff[1] = LO_UINT16(_NIB.nwkDevAddress);
            buff[2] = HI_UINT16(_NIB.nwkDevAddress);
            if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID,  11,
                          &buff[0],
                          &GenericApp_TransID,
                          AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS )
            {
                 AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID,  11,
                          &buff[0],
                          &GenericApp_TransID,
                          AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
              // send twice only, if it is still not ok, f**k it
            }            
            
        }
        // only routers return devlist
        if( pkt->cmd.Data[0] == 0xCA )
        {
               myaddr.addrMode = (afAddrMode_t)Addr16Bit;
               myaddr.endPoint = GENERICAPP_ENDPOINT;          
               myaddr.addr.shortAddr = 0x0000;
          
        ass_num = AssocCount(1, 4);
      //  if( ass_num > 0)
        //{
          for(i=0;i< NWK_MAX_DEVICES; i++)
          {
             nr = AssociatedDevList[ i ].nodeRelation;
              if(nr > 0 && nr < 5) //CHILD_RFD CHILD_RFD_RX_IDLE CHILD_FFD CHILD_FFD_RX_IDLE
             //if( nr != 0XFF)
             {
             //  if( AssociatedDevList[ i ].shortAddr != 0x0000)
             //  {
                  //if( AssocIsChild( AssociatedDevList[ i ].shortAddr ) != 1 || AssociatedDevList[ i ].age > NWK_ROUTE_AGE_LIMIT)
                  //if( AssocIsChild( AssociatedDevList[ i ].shortAddr ) == 1 && AssociatedDevList[ i ].age > NWK_ROUTE_AGE_LIMIT )
                 // {
                    //  myaddr.addr.shortAddr = AssociatedDevList[ i ].shortAddr;
                      /*
                      if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID,(byte)osal_strlen( theMessageData ) + 1,
                              (byte *)&theMessageData,
                              &GenericApp_TransID,
                              AF_ACK_REQUEST, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS )
                        {
                          uprint("delete asso");
                        */
                   //       delete_asso( AssociatedDevList[ i ]. addrIdx);
                          
                  // }
                    //    else
                     //   {
                          devlist[yy1] = AssociatedDevList[ i ].shortAddr;
                          yy1++;
                     //   }
                  
                  // }
             }
              else { break; }
          }
       // }else
        //{
          //devlist[yy1] = 0;
        //}
          devlist[0] = BUILD_UINT16(0xce,  ass_num );
          if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, yy1*2,
                          (byte *)&devlist,
                          &GenericApp_TransID,
                          AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS )
          {
              AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, yy1*2,
                          (byte *)&devlist,
                          &GenericApp_TransID,
                          AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
             // send twice only, if it is still not ok, f**k it
          }
        
          
          //cb_SendDataRequest( 0x0000, GENERICAPP_CLUSTERID, yy1*2+1, (uint8*)&devlist[0], 0, AF_DISCV_ROUTE, 0 );
          
          //UART_Send_String( (uint8*)&devlist[0], yy1*2);
          
            //p1 = AssocMakeList( &cnt );
            //UART_Send_String( (uint8*)p1,  AssocCount(1, 4)*2);
            //osal_mem_free(p1);
        }
#endif  
    } //if( pkt->cmd.DataLength == 1)
    
    if( pkt->cmd.DataLength == 3)
    { 
        //ed fa 01 ,turn on led 1
        //ed ff 01  turn off led 1
        if( pkt->cmd.Data[0] == 0xed  && pkt->cmd.Data[1] == 0xfa )
        {
            switch(pkt->cmd.Data[2])
            {
              case 0x01:
                led1 = 1;
                break;
              case 0x05:
                // led5 = 1;
                break;
              default:break;
            }
                   
        }
        if( pkt->cmd.Data[0] == 0xed  && pkt->cmd.Data[1] == 0xff )
        {
            switch(pkt->cmd.Data[2])
            {
              case 0x01:
                led1 = 0;
                break;
              case 0x05:
               //  led5 = 0;
                break;
              default:break;
            }          
        }
        
    } // if( pkt->cmd.DataLength == 3)
    
     // led1 = !led1;
     // led5 = !led5;
      break;
  }
  
#if defined( LCD_SUPPORTED )
      UART_Send_String( &pkt->cmd.Data[0], pkt->cmd.DataLength);
#endif

}
Пример #4
0
uint16 uprint( uint8 * Data)
{
  UART_Send_String( Data, strlen(Data) );
}
Пример #5
0
void Serial_callBack(uint8 port, uint8 event)
{
  char theMessageData[] = "Hello";
  zAddrType_t dstAddr;
  zAddrType_t ZAddr;
  
  afAddrType_t myaddr; // use for p2p
  
  char pbuf[3];
  char pbuf1[3];
  uint16 cmd;
  
  uint8 buff[128];
  uint8 readBytes = 0;
  
  uint16 short_ddr;
  uint16 panid;
  uint8 *ieeeAddr;
  
  uint16 *p1;
  
  byte cnt = 0;
  uint8 yy1;
  uint8 yy2;
  
  uint8 i;
  byte nr;
  uint16 devlist[ NWK_MAX_DEVICES + 1];
  associated_devices_t *adp; // delete devices
  
  //short_ddr = GenericApp_DstAddr.addr.shortAddr;
  uint8 startOptions;    
  uint8 logicalType;
  
  logicalType = (uint8)ZDO_Config_Node_Descriptor.LogicalType;
  
            
  readBytes = HalUARTRead(SER_PORT, buff, 127);
  if (readBytes > 0)
  {
    //HalUARTWrite( SER_PORT, "DataRead: ",10);
    // HalUARTWrite( SER_PORT, buff, readBytes);
    if(readBytes == 4)
    {
      if(buff[0] == 'p' && buff[1] == 'i' && buff[2]=='n' && buff[3] == 'g')
      {
          UART_Send_String( "pong", 4 );
      }
    }
    
    if( readBytes == 1)
    {
        yy1 = 1;
        if(buff[0]== 's')
        {
          /// short address
          short_ddr = _NIB.nwkDevAddress;
          
          UART_Send_String( (uint8*)&short_ddr, 2);
          
        }
        if(buff[0] == 'p')
        {
          /// pan id
          panid = _NIB.nwkPanId;
          UART_Send_String( (uint8*)&panid, 2);          
        }
        if(buff[0] == 'c')/// channel
        {
            yy2 = _NIB.nwkLogicalChannel;
            UART_Send_String( (uint8*)&yy2, 1);
            
        }
        if(buff[0] =='m') // mac address
        {
            ieeeAddr = NLME_GetExtAddr();
            UART_Send_String( ieeeAddr, 8);
            
        }

        if( buff[0] ==0xc0) // coordinator 
        {

          set_coordi();       
          return;
        }

        if( buff[0] ==0xe0) // router
        {
          set_router();
          
          return;
        }
        
        if(buff[0] == 0xCA)
        {
          // 使命的招唤
          // read self AssociatedDevList
          for(i=0;i< NWK_MAX_DEVICES; i++)
          {
             nr = AssociatedDevList[ i ].nodeRelation;
             if(nr > 0 && nr < 5) //CHILD_RFD CHILD_RFD_RX_IDLE CHILD_FFD CHILD_FFD_RX_IDLE
             //if( nr != 0XFF)
             {
               //   myaddr.addrMode = (afAddrMode_t)Addr16Bit;
               //   myaddr.endPoint = GENERICAPP_ENDPOINT;
             //  if( AssociatedDevList[ i ].shortAddr != 0x0000)
             //  {
                  //if( AssocIsChild( AssociatedDevList[ i ].shortAddr ) != 1 || AssociatedDevList[ i ].age > NWK_ROUTE_AGE_LIMIT)
                  //if( AssocIsChild( AssociatedDevList[ i ].shortAddr ) == 1 && AssociatedDevList[ i ].age > NWK_ROUTE_AGE_LIMIT )
                 // {
                    //  myaddr.addr.shortAddr = AssociatedDevList[ i ].shortAddr;
                      /*
                      if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID,(byte)osal_strlen( theMessageData ) + 1,
                              (byte *)&theMessageData,
                              &GenericApp_TransID,
                              AF_ACK_REQUEST, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS )
                        {
                          uprint("delete asso");
                        */
                   //       delete_asso( AssociatedDevList[ i ]. addrIdx);
                          
                  // }
                    //    else
                     //   {
                          devlist[yy1] = AssociatedDevList[ i ].shortAddr;
                          yy1++;
                     //   }
                  
                  // }
             }//else {break;}
          }
          devlist[0] = BUILD_UINT16(0xce,  AssocCount(1, 4) );
          UART_Send_String( (uint8*)&devlist[0], yy1*2);
            //p1 = AssocMakeList( &cnt );
            //UART_Send_String( (uint8*)p1,  AssocCount(1, 4)*2);
            //osal_mem_free(p1);
          return;
        }
    }
    
#if defined( ZDO_COORDINATOR )
    // only coordinator can have this function
    if(readBytes == 3)
    {
      
      if( buff[0] == 0xCA || buff[0] == 0x6d)
      {
        // CA xx xx ,send CA to a node ,with it's short address
        ///uprint("it's CA ");
        short_ddr = BUILD_UINT16( buff[1], buff[2] );
        myaddr.addrMode = (afAddrMode_t)Addr16Bit;
        myaddr.endPoint = GENERICAPP_ENDPOINT;
        myaddr.addr.shortAddr = short_ddr;
        
        if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, 1,
                          (byte *)&buff,
                          &GenericApp_TransID,
                          AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS )
        {
             AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, 1,
                          (byte *)&buff,
                          &GenericApp_TransID,
                          AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
             // send twice only, if it is still not ok, f**k it
          
        }
       return;
      }
    }
          
#endif 
          
    if( readBytes >= 2)
    {
     
      if( buff[0] == 'e' && buff[1] == '#')
      {
          uprint("EndDevice match");
          HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );

          dstAddr.addrMode = Addr16Bit;
          dstAddr.addr.shortAddr = 0x0000; // Coordinator
          ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), GenericApp_epDesc.endPoint,
                            GENERICAPP_PROFID,
                            GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                            GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                            FALSE );
      
      }
 
      if( buff[0] == 'r' && buff[1] == '#')
      {
          uprint("Router Device match");
          
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_FLASH );

      dstAddr.addrMode = AddrBroadcast;
      dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
      ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
                        GENERICAPP_PROFID,
                        GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                        GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                        FALSE );
      
      }
      if(readBytes == 6)
      {
        if(buff[0] == 'p' && buff[1]==':') // pan id
        {
            strncpy(pbuf, &buff[2],2); pbuf[2] = '\0';
            strncpy(pbuf1, &buff[4],2); pbuf1[2] = '\0';
            
            set_panid( BUILD_UINT16( strtol(pbuf1,NULL,16),strtol(pbuf,NULL,16) ));
            if(_NIB.nwkPanId == 0xffff)
            {
              zgWriteStartupOptions (ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);           
              SystemReset();
            }            
            //SystemResetSoft();
        }
        
        if(buff[0] == 's' && buff[1]==':') // short address
        {
            /*
            strncpy(pbuf, &buff[2],2); pbuf[2] = '\0';
            strncpy(pbuf1, &buff[4],2); pbuf1[2] = '\0';
            _NIB.nwkDevAddress = BUILD_UINT16( strtol(pbuf1,NULL,16),strtol(pbuf,NULL,16));
            */
            
        }
      }
      
      
      cmd = BUILD_UINT16(buff[ 1 + 1], buff[1]);
      if( ( buff[ 0 ] == CPT_SOP) && (cmd == SYS_PING_REQUEST) ) 
      {
        sysPingReqRcvd();
        return;
      }

      if( readBytes == 2)
      {        
        if( buff[0] == 0xcc )
        {
          if( buff[1] > 0x0a && buff[1] < 0x1b )
          {

            _NIB.nwkLogicalChannel = buff[1];
            NLME_UpdateNV(0x01);
            ZMacSetReq( ZMacChannel, &buff[1]);
            
            osal_nv_item_init( ZCD_NV_CHANLIST, sizeof(zgDefaultChannelList), &zgDefaultChannelList);
            
            if( buff[1] == 0x0b)
            {
              zgDefaultChannelList = 0x00000800;
              
            } 
            if (buff[1] == 0x0c )
            {
              zgDefaultChannelList = 0x00001000;
            }            
            if (buff[1] == 0x0d )
            {
              zgDefaultChannelList = 0x00002000;
            }
            if (buff[1] == 0x0e )
            {
              zgDefaultChannelList = 0x00004000;
            }
            if (buff[1] == 0x0f )
            {
              zgDefaultChannelList = 0x00008000;
            }
            if (buff[1] == 0x10 )
            {
              zgDefaultChannelList = 0x00010000;
            }
            if (buff[1] == 0x11 )
            {
              zgDefaultChannelList = 0x00020000;
            }
            if (buff[1] == 0x12 )
            {
              zgDefaultChannelList = 0x00040000;
            }            
            if (buff[1] == 0x13 )
            {
              zgDefaultChannelList = 0x00080000;
            }
            if (buff[1] == 0x14 )
            {
              zgDefaultChannelList = 0x00100000;
            }
            if (buff[1] == 0x15 )
            {
              zgDefaultChannelList = 0x00200000;
            }            
            if (buff[1] == 0x16 )
            {
              zgDefaultChannelList = 0x00400000;
            }
            if (buff[1] == 0x17 )
            {
              zgDefaultChannelList = 0x00800000;
            }
            if (buff[1] == 0x18 )
            {
              zgDefaultChannelList = 0x01000000;
            }

            if (buff[1] == 0x19 )
            {
              zgDefaultChannelList = 0x02000000;
            }
            if (buff[1] == 0x1a )
            {
              zgDefaultChannelList = 0x04000000;
            }
            
            osal_nv_write(  ZCD_NV_CHANLIST, 0 ,sizeof(zgDefaultChannelList), &zgDefaultChannelList);
            
            UART_Send_String( (uint8*)&zgDefaultChannelList, sizeof(zgDefaultChannelList) );

            /*
            _NIB.nwkLogicalChannel = buff[1];
            NLME_UpdateNV(0x01);
            if( osal_nv_write(ZCD_NV_CHANLIST, 0, osal_nv_item_len( ZCD_NV_CHANLIST ), &tmp32) !=  ZSUCCESS)
            {
              uprint("change channel to nv failed");
            }
            */
            /*
            _NIB.nwkLogicalChannel = buff[1];
            ZMacSetReq( ZMacChannel, &buff[1]);
            ZDApp_NwkStateUpdateCB();
            _NIB.nwkTotalTransmissions = 0;
            nwkTransmissionFailures( TRUE );
            */
          }
        }
        
      }// readBytes==2


      ser_process( buff,readBytes );// 中讯威易的协议最小也是2 字节
    }//if( readBytes >= 2)


    AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
                       GENERICAPP_CLUSTERID,
                       readBytes,
                       (byte *)&buff,
                       &GenericApp_TransID,
                       AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
    
      
  } //if (readBytes > 0) 
}