123


/*********************************************************************
 * INCLUDES
 */
#include "OSAL.h"
#include "ZGlobals.h"
#include "AF.h"
#include "aps_groups.h"
#include "ZDApp.h"
#include <string.h>

#include "SampleApp.h"
#include "SampleAppHw.h"

#include "OnBoard.h"

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "MT_UART.h"
#include "MT_APP.h"
#include "MT.h"

/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

// This list should be filled with Application specific Cluster IDs.
const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
{
  SAMPLEAPP_PERIODIC_CLUSTERID,
  SAMPLEAPP_FLASH_CLUSTERID,
  SAMPLEAPP_NETREQ_CLUSTERID
};

const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
{
  SAMPLEAPP_ENDPOINT,              //  int Endpoint;
  SAMPLEAPP_PROFID,                //  uint16 AppProfId[2];
  SAMPLEAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  SAMPLEAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  SAMPLEAPP_FLAGS,                 //  int   AppFlags:4;
  SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;
  (cId_t *)SampleApp_ClusterList,  //  uint8 *pAppInClusterList;
  SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;
  (cId_t *)SampleApp_ClusterList   //  uint8 *pAppInClusterList;
};

// This is the Endpoint/Interface description.  It is defined here, but
// filled-in in SampleApp_Init().  Another way to go would be to fill
// in the structure here and make it a "const" (in code space).  The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t SampleApp_epDesc;

/*********************************************************************
 * EXTERNAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */

/*********************************************************************
 * LOCAL VARIABLES
 */
uint8 SampleApp_TaskID;   // Task ID for internal task/event processing
                          // This variable will be received when
                          // SampleApp_Init() is called.
devStates_t SampleApp_NwkState;

uint8 SampleApp_TransID;  // This is the unique message ID (counter)

afAddrType_t SampleApp_P2P_DstAddr;
afAddrType_t SampleApp_Flash_DstAddr;
afAddrType_t SampleApp_P2C_DstAddr;

aps_Group_t SampleApp_Group;

uint8 SampleAppPeriodicCounter = 0;
uint8 SampleAppFlashCounter = 0;
unsigned char r[500];       // 可以直接修改r地址处的内容,可以建立100个下级,
unsigned char *sta;
int count_rece = 0;
int flag_rece[100] ={0};
int flag_send[8] = {0};
int count_send = 0;
uint16 FaAdd;
unsigned int len;

/*********************************************************************
 * LOCAL FUNCTIONS
 */
void SampleApp_HandleKeys( uint8 shift, uint8 keys );
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
void SampleApp_SendPeriodicMessage( void );
void SampleApp_SendFlashMessage( uint16 flashTime );
void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg);
void SampleApp_NetReqMessage(void);
void SampleApp_NetSend(void);

/*********************************************************************
 * NETWORK LAYER CALLBACKS
 */

/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      SampleApp_Init
 *
 * @brief   Initialization function for the Generic App Task.
 *          This is called during initialization and should contain
 *          any application specific initialization (ie. hardware
 *          initialization/setup, table initialization, power up
 *          notificaiton ... ).
 *
 * @param   task_id - the ID assigned by OSAL.  This ID should be
 *                    used to send messages and set timers.
 *
 * @return  none
 */
void SampleApp_Init( uint8 task_id )
{ 
  SampleApp_TaskID = task_id;
  SampleApp_NwkState = DEV_INIT;
  SampleApp_TransID = 0;
  
  MT_UartInit();                  //串口初始化
  MT_UartRegisterTaskID(task_id); //注册串口任务
  HalUARTWrite(0,"BIT UartInit OK\n", sizeof("BIT UartInit OK\n"));//提示信息
  
  // Device hardware initialization can be added here or in main() (Zmain.c).
  // If the hardware is application specific - add it here.
  // If the hardware is other parts of the device add it in main().

 #if defined ( BUILD_ALL_DEVICES )
  // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
  // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
  // together - if they are - we will start up a coordinator. Otherwise,
  // the device will start as a router.
  if ( readCoordinatorJumper() )
    zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
  else
    zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif // BUILD_ALL_DEVICES

#if defined ( HOLD_AUTO_START )
  // HOLD_AUTO_START is a compile option that will surpress ZDApp
  //  from starting the device and wait for the application to
  //  start the device.
  ZDOInitDevice(0);
#endif

  // Setup for the periodic message's destination address
  // Broadcast to everyone  周期性广播
  
  SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_P2P_DstAddr.addr.shortAddr = 0xffff;           //在这里无法进行父节点地址的获取

  // Setup for the flash command's destination address - Group 1
  SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;   //AddrBroadcast
  SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_Flash_DstAddr.addr.shortAddr = 0xffff;
  
  
  SampleApp_P2C_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;   //AddrBroadcast
  SampleApp_P2C_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_P2C_DstAddr.addr.shortAddr = 0x0000;
  
  // Fill out the endpoint description.
  SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_epDesc.task_id = &SampleApp_TaskID;
  SampleApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
  SampleApp_epDesc.latencyReq = noLatencyReqs;

  // Register the endpoint description with the AF
  afRegister( &SampleApp_epDesc );

  // Register for all key events - This app will handle all key events
  RegisterForKeys( SampleApp_TaskID );

  // By default, all devices start out in Group 1
  SampleApp_Group.ID = 0x0001;
  osal_memcpy( SampleApp_Group.name, "Group 1", 7 );
  aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );

#if defined ( LCD_SUPPORTED )
  HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 );
#endif
}

/*********************************************************************
 * @fn      SampleApp_ProcessEvent
 *
 * @brief   Generic Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  none
 */
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  unsigned char *uart_str; 
  unsigned char *judge_str = "Net-Find";          // 非常奇怪,这里使用[]的形式就不可以,会导致串口发送的东西错误
  (void)task_id;  // Intentionally unreferenced parameter
  
  
  
  // 用来测试
 /* int i = 1;
  while(i)
  {
    if(i%5 == 0)
    {SampleApp_NetReqMessage();
      //HalUARTWrite(0, "test", 4);
    }
    i++;
  }*/
  
  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {        
        // Received when a key is pressed
        case KEY_CHANGE:
          SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
          break;

        // Received when a messages is received (OTA) for this endpoint
        case AF_INCOMING_MSG_CMD:  // 接收到消息的事件
          SampleApp_MessageMSGCB( MSGpkt );   // 通过串口进行发送
          break;
          
       case CMD_SERIAL_MSG:
          mtOSALSerialData_t * UartMsg;
          UartMsg = (mtOSALSerialData_t *)MSGpkt;
          strcpy(uart_str,'\0');
          strcpy(uart_str,UartMsg->msg);
          uart_str[8] = '\0';
          HalUARTWrite(0,uart_str,strlen(uart_str));
          HalUARTWrite(0, "\n", 1);
          if(strcmp(uart_str,judge_str)==0)
           SampleApp_NetReqMessage();         // 测试正常
            /*{HalUARTWrite(0, "uart_succe", 10);
            HalUARTWrite(0, "\n", 1);}*/
          break;
        // Received whenever the device changes state in the network
        case ZDO_STATE_CHANGE:   // 获取网络状态
          SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
          if ( //(SampleApp_NwkState == DEV_ZB_COORD) ||
                 (SampleApp_NwkState == DEV_ROUTER)
              || (SampleApp_NwkState == DEV_END_DEVICE) )
          {
            // Start sending the periodic message in a regular interval.
            /*osal_start_timerEx( SampleApp_TaskID,
                              SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
                              SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );*/
          }
          else
          {
            // Device is no longer in the network
          }
          if (SampleApp_NwkState == DEV_ZB_COORD)
          {
            sta = "coord";
          }
          if (SampleApp_NwkState == DEV_ROUTER)
          {
            sta = "router";
          }
          if (SampleApp_NwkState == DEV_END_DEVICE)
          {
            sta = "end";
          }
          
          
          break;

        default:
          break;
      }

      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );

      // Next - if one is available
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  // Send a message out - This event is generated by a timer
  //  (setup in SampleApp_Init()).
  if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  {
    // Send the periodic message
    SampleApp_SendPeriodicMessage();

    // Setup to send message again in normal period (+ a little jitter)
    /*osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,  //用来设置定时器,使某任务能够定时运行的函数
        (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );*/

    // return unprocessed events
    return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  }

  // Discard unknown events
  return 0;
}

/*********************************************************************
 * Event Generation Functions
 */
/*********************************************************************
 * @fn      SampleApp_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys - bit field for key events. Valid entries:
 *                 HAL_KEY_SW_2
 *                 HAL_KEY_SW_1
 *
 * @return  none
 */
void SampleApp_HandleKeys( uint8 shift, uint8 keys )
{
  (void)shift;  // Intentionally unreferenced parameter
  
  if ( keys & HAL_KEY_SW_1 )
  {
    /* This key sends the Flash Command is sent to Group 1.
     * This device will not receive the Flash Command from this
     * device (even if it belongs to group 1).
     */
    SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );
  }

  if ( keys & HAL_KEY_SW_2 )
  {
    /* The Flashr Command is sent to Group 1.
     * This key toggles this device in and out of group 1.
     * If this device doesn't belong to group 1, this application
     * will not receive the Flash command sent to group 1.
     */
    aps_Group_t *grp;
    grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
    if ( grp )
    {
      // Remove from the group
      aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
    }
    else
    {
      // Add to the flash group
      aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );
    }
  }
}

/*********************************************************************
 * LOCAL FUNCTIONS
 */

/*********************************************************************
 * @fn      SampleApp_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
 */
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) //接收数据
{
  unsigned char *rssi_buf;
  unsigned char *temp;
  unsigned int rssi;
  rssi=~(pkt->rssi)+1;     //补码转原码
 // rssi = pkt->LinkQuality
  
   // 用来测试   
  /* HalUARTWrite(0, "NetReqRece", 10);
   HalUARTWrite(0, "\n", 1);
   SampleApp_NetSend();*/
     
     
     
  switch ( pkt->clusterId )
  {
    case SAMPLEAPP_PERIODIC_CLUSTERID:    
      {
        strcpy(r, "RSSI: ");
        *(rssi_buf)='-';
        *(rssi_buf+1)=(rssi/100)+'0';
        *(rssi_buf+2)=(rssi%100)/10+'0';
        *(rssi_buf+3)=(rssi%10)+'0';
        *(rssi_buf+4)=32;
        *(rssi_buf+5)='\0';
        temp = pkt->cmd.Data;
        strcat(r, rssi_buf);
        len = pkt->cmd.DataLength+strlen(r);
        strcat(r, pkt->cmd.Data);
        
        if((sta=="router")&&(strlen(r)!=0))          // 如果是路由器并且已经收到了信息,则进行包装发送
        {
          unsigned char *theMessageData,s[200],temp[100];
          uint16 i=0,n=0;
          uint16 mod=0;
          uint16 FaAdd,a;
          unsigned char s1[10],s2[10];//存储当前节点以及父节点的地址
          FaAdd =NLME_GetCoordShortAddr();//获取十进制的父节点地址
          SampleApp_P2P_DstAddr.addr.shortAddr = FaAdd;          // 进行一下地址的更新
          a=NLME_GetShortAddr();//十进制的当前节点地址
          strcpy(s," RIT:");  //字符串复制
          if(!FaAdd)//将父节点地址转化为字符串,如果存在父节点
          {
            strcpy(s2,"0000");
            i=4;
          }
          while(FaAdd)
          {
            mod=FaAdd%16;
            if(mod<=9)
              *(s2+i)=mod+'0';
            else
              *(s2+i)=mod+55;
              i++;
              FaAdd/=16;
          }
          s2[i]='\0';
          n=strlen(s2);
          if(n<=3)   
            strcat(s1,"0");  //在s1之后补一个0
          for(i=0;i<n/2;i++)
          {
              char t;
              t=s2[i];
              s2[i]=s2[n-1-i];
              s2[n-1-i]=t;
           }
          i=0;
          while(a)
          {
            mod = a%16;
            if(mod<=9)
                *(s1+i)=mod+'0';
            else
                *(s1+i)=mod+55;
                i++;
                a/=16;
          }
          s1[i]='\0';  // 到此一个16位的地址已经装换成字符形式的数组,字符串
          n=osal_strlen(s1); //可能是1,2,3
          if(n<=3)   
            strcat(s1,"0");  //在s1之后补一个0
          // 如果n是1,不改变,表示是协调器
          // 如果n是2,则是s1的0位和1位互换
          // 如果n是3,则是s1的0位和2位互换
          for(i=0;i<n/2;i++)   
          {
            char t;
              t=s1[i];
              s1[i]=s1[n-1-i];
              s1[n-1-i]=t;
          }
          strcat(s,s2);
          strcat(s," -> ");
          strcat(s,s1);
          theMessageData=s;
          strcpy(temp, theMessageData);
          strcat(temp," -> ");
          strcat(temp,r);
          n = (int)(strlen(theMessageData)+len+4);
          AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
                     SAMPLEAPP_PERIODIC_CLUSTERID,
                     (byte)n,
                     temp,
                     &SampleApp_TransID,
                     AF_DISCV_ROUTE,
                     AF_DEFAULT_RADIUS );
          //strcpy(r, "\0");
          count_rece ++;
          if(count_rece>100)
            count_rece = 1;
          
        }
        
        if(sta=="coord")              //如果是协调器,则进行串口发送
        {
          HalUARTWrite(0, r, len);
          HalUARTWrite(0, "\n", 1);         //回车换行
        }
        break;
      case SAMPLEAPP_NETREQ_CLUSTERID:      // 问题是在其中调用函数SampleApp_SendPeriodicMessage回导致重启
        HalUARTWrite(0, "NetReqRece", 10);
        HalUARTWrite(0, "\n", 1);
        SampleApp_NetSend();
        break;
      }
  }
}

/*********************************************************************
 * @fn      SampleApp_SendPeriodicMessage
 *
 * @brief   Send the periodic message.
 *
 * @param   none
 *
 * @return  none
 */
void SampleApp_SendPeriodicMessage( void )
{
//  flag_send[count_send] = count_rece;
//  count_send ++;
//  if (count_send == 6)   //0 1 2 3 4 5 6 7
//    count_send = 0;
//  if (((strlen(r)==0)||(count_rece==0)||(flag_send[2]==flag_send[5]))&&(sta!="coord"))            //该设备没收到信息,要在发送函数中进行直接发送  接收函数没有执行过,连续多次接收函数未执行
//  {
    unsigned char *theMessageData,s[200],temp[100];
    uint16 i=0,n=0;
    uint16 mod=0;
    uint16 FaAdd,a;
    unsigned char s1[10],s2[10];//存储当前节点以及父节点的地址
    FaAdd =NLME_GetCoordShortAddr();//获取十进制的父节点地址
    SampleApp_P2P_DstAddr.addr.shortAddr = FaAdd;          // 进行一下地址的更新
    a=NLME_GetShortAddr();//十进制的当前节点地址
    strcpy(s," RIT:");  //字符串复制
    if(!FaAdd)//将父节点地址转化为字符串,如果存在父节点
    {
        strcpy(s2,"0000");
        i=4;
    }
    while(FaAdd)
    {
        mod=FaAdd%16;
        if(mod<=9)
          *(s2+i)=mod+'0';
        else
          *(s2+i)=mod+55;
          i++;
          FaAdd/=16;
    }
    s2[i]='\0';
    n=strlen(s2);
    if(n<=3)   
      strcat(s1,"0");  //在s1之后补一个0
    for(i=0;i<n/2;i++)
    {
        char t;
        t=s2[i];
        s2[i]=s2[n-1-i];
        s2[n-1-i]=t;
     }
    i=0;
    while(a)
    {
      mod = a%16;
      if(mod<=9)
          *(s1+i)=mod+'0';
      else
          *(s1+i)=mod+55;
          i++;
          a/=16;
    }
    s1[i]='\0';  // 到此一个16位的地址已经装换成字符形式的数组,字符串
    n=osal_strlen(s1); //可能是1,2,3
    if(n<=3)   
      strcat(s1,"0");  //在s1之后补一个0
    // 如果n是1,不改变,表示是协调器
    // 如果n是2,则是s1的0位和1位互换
    // 如果n是3,则是s1的0位和2位互换
    for(i=0;i<n/2;i++)   
    {
      char t;
        t=s1[i];
        s1[i]=s1[n-1-i];
        s1[n-1-i]=t;
    }
    strcat(s,s2);
    strcat(s," -> ");
    strcat(s,s1);
    theMessageData=s;
    AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
                         SAMPLEAPP_PERIODIC_CLUSTERID,
                         (byte)osal_strlen( theMessageData ),
                         theMessageData,
                         &SampleApp_TransID,
                         AF_DISCV_ROUTE,
                         AF_DEFAULT_RADIUS );
//  }
}

void SampleApp_NetSend()
{
  
  unsigned char *theMessageData,s[20],temp[20];
  uint16 i=0,n=0;
  uint16 mod=0;
  uint16 FaAdd,a;
  unsigned char s1[10],s2[10];//存储当前节点以及父节点的地址
  FaAdd =NLME_GetCoordShortAddr();//获取十进制的父节点地址
  SampleApp_P2P_DstAddr.addr.shortAddr = FaAdd;          // 进行一下地址的更新
  
  HalUARTWrite(0, "In_NetSend", 10);
  HalUARTWrite(0, "\n", 1);
  /*AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_PERIODIC_CLUSTERID,
                       (byte)osal_strlen( "SuccNetSend" ),
                       "SuccNetSend",
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );*/
    
  a=NLME_GetShortAddr();//十进制的当前节点地址
  strcpy(s," RIT:");  //字符串复制
  if(!FaAdd)//将父节点地址转化为字符串,如果存在父节点
  {
      strcpy(s2,"0000");
      i=4;
  }
  while(FaAdd)
  {
      mod=FaAdd%16;
      if(mod<=9)
        *(s2+i)=mod+'0';
      else
        *(s2+i)=mod+55;
        i++;
        FaAdd/=16;
  }
  s2[i]='\0';
  n=strlen(s2);
  if(n<=3)   
    strcat(s1,"0");  //在s1之后补一个0
  for(i=0;i<n/2;i++)
  {
      char t;
      t=s2[i];
      s2[i]=s2[n-1-i];
      s2[n-1-i]=t;
   }
  
  i=0;    //  开始处理自己的地址信息
  while(a)
  {
    mod = a%16;
    if(mod<=9)
        *(s1+i)=mod+'0';
    else
        *(s1+i)=mod+55;
        i++;
        a/=16;
  }
  s1[i]='\0';  // 到此一个16位的地址已经装换成字符形式的数组,字符串
  n=osal_strlen(s1); //可能是1,2,3
  if(n<=3)   
    strcat(s1,"0");  //在s1之后补一个0
  // 如果n是1,不改变,表示是协调器
  // 如果n是2,则是s1的0位和1位互换
  // 如果n是3,则是s1的0位和2位互换
  for(i=0;i<n/2;i++)   
  {
    char t;
      t=s1[i];
      s1[i]=s1[n-1-i];
      s1[n-1-i]=t;
  }
  strcat(s,s2);
  strcat(s," -> ");
  strcat(s,s1);
  theMessageData=s;   // 该信息可以通过串口发送但是就是无法通过AF_DataRequest
  HalUARTWrite(0, theMessageData, osal_strlen( theMessageData ));
  HalUARTWrite(0, "\n", 1);
  /*AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_PERIODIC_CLUSTERID,
                       (byte)osal_strlen( theMessageData ),
                       theMessageData,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );*/
  /*AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,   // succeed
                       SAMPLEAPP_PERIODIC_CLUSTERID,
                       (byte)osal_strlen( "SuccNetSend" ),
                       "SuccNetSend",
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );*/
  AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_PERIODIC_CLUSTERID,
                       (byte)osal_strlen( theMessageData ),
                       theMessageData,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS );

}
/*********************************************************************
 * @fn      SampleApp_SendFlashMessage
 *
 * @brief   Send the flash message to group 1.
 *
 * @param   flashTime - in milliseconds
 *
 * @return  none
 */
void SampleApp_NetReqMessage()           // 如果接收到了令自己发送广播指令的命令则进行广播
{
  unsigned char buffer[10] = "NetSendReq";

  if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_NETREQ_CLUSTERID,
                       10,
                       buffer,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  {
    HalUARTWrite(0, "SucBroadSend", 12);
    HalUARTWrite(0, "\n", 1);
  }
  else
  {
    // Error occurred in request to send.
    HalUARTWrite(0, "FailBroadSend", 13);
    HalUARTWrite(0, "\n", 1);
  }
}


/*********************************************************************
 * @fn      SampleApp_SendFlashMessage
 *
 * @brief   Send the flash message to group 1.
 *
 * @param   flashTime - in milliseconds
 *
 * @return  none
 */
void SampleApp_SendFlashMessage( uint16 flashTime )
{
  uint8 buffer[3];
  buffer[0] = (uint8)(SampleAppFlashCounter++);
  buffer[1] = LO_UINT16( flashTime );
  buffer[2] = HI_UINT16( flashTime );

  /*if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_FLASH_CLUSTERID,
                       3,
                       buffer,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  {
  }
  else
  {
    // Error occurred in request to send.
  }*/
}



void SampleApp_NetSend()
{
uint16 faAdd,selfAdd;
unsigned char messageAdd[20];
unsigned char faStr[20],selfStr[20];//存储当前节点以及父节点的地址
faAdd =NLME_GetCoordShortAddr();//获取十进制的父节点地址
selfAdd = NLME_GetShortAddr();//十进制的当前节点地址
SampleApp_P2P_DstAddr.addr.shortAddr = faAdd; // 进行一下地址的更新

HalUARTWrite(0, “In_NetSend”, 10);
HalUARTWrite(0, “\n”, 1);

strcpy(messageAdd," RIT:"); //字符串复制
sprintf(faStr, “%x”, faAdd);
sprintf(selfStr, “%x”, selfAdd);
strcat(messageAdd, faStr);
strcat(messageAdd, “>”);
strcat(messageAdd, selfStr);
HalUARTWrite(0, messageAdd, osal_strlen( messageAdd ));
HalUARTWrite(0, “\n”, 1);
AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
(byte)osal_strlen( messageAdd ),
messageAdd,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS );

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值