示例#1
0
文件: ZDiags.c 项目: Daan1992/WSN-Lab
/****************************************************************************
 * @fn          ZDiagsInitStats
 *
 * @brief       Initialize the statistics table in NV or restore values from
 *              NV into the Statistics table in RAM
 *
 * @param       none.
 *
 * @return      ZSuccess - if NV data was initialized successfully.
 *              ZFailure - Otherwise
 */
uint8 ZDiagsInitStats( void )
{
  uint8 retValue = ZSuccess;

#if defined ( FEATURE_SYSTEM_STATS )
  uint8 status;

  // Initialize structure with default values, and do not try to write to NV
  // because item does not exist yet
  (void)ZDiagsClearStats( FALSE );

  status = osal_nv_item_init( ZCD_NV_DIAGNOSTIC_STATS,
                              (uint16)sizeof( DiagStatistics_t ),
                              &DiagsStatsTable );

  if ( status == NV_OPER_FAILED )
  {
    retValue = ZFailure;
  }
  else
  {
    // Item existed, restore NV values into RAM table
    if ( status == SUCCESS )
    {
      if ( NV_OPER_FAILED == ZDiagsRestoreStatsFromNV() )
      {
        retValue = ZFailure;
      }
    }
  }
#endif // FEATURE_SYSTEM_STATS

  return ( retValue );
}
示例#2
0
void ID_Init(void)
{
  uint16 Init_ID=0x1234;       //默认PANID,自己可以任意修改
  uint16 preID;                    //这个是指向ZCD_NV_PANID的值
  uint16 ID;                       //这个想ZCD_NV_PANID1的值
  
  osal_nv_read(ZCD_NV_ID,0,2,&preID);
  osal_nv_read(ZCD_NV_ID1,0,2,&ID);
  //如果用串口调试助手修改了,则同时修改ZCD_NV_PANID和ZCD_NV_PANID1
  //并且这两个值是相同的,所以当修改后则不会进入下面这个条件语句中
  //而在修改之前通过下面的语句将默认PANIDID存入NV中,使得设备启动的
  //时候有一个固定的PANID
  if(preID!=ID)
  {
   if ( osal_nv_item_init( ZCD_NV_ID,
                              2,
                              &Init_ID ) == ZSUCCESS )
  
    {  
        osal_nv_write(ZCD_NV_ID,0,2,&Init_ID);
        osal_nv_read(ZCD_NV_ID,0,2,&Init_ID);
        zgID=Init_ID;
    } 
  }
}
示例#3
0
/*********************************************************************
 * @fn          zgInit
 *
 * @brief
 *
 *   Initialize the Z-Stack Globals. If an item doesn't exist in
 *   NV memory, write the system default into NV memory. But if
 *   it exists, set the item to the value stored in NV memory.
 *
 * NOTE: The Startup Options (ZCD_NV_STARTUP_OPTION) indicate
 *       that the Config state items (zgItemTable) need to be
 *       set to defaults (ZCD_STARTOPT_DEFAULT_CONFIG_STATE). The
 *
 * @param       none
 *
 * @return      ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
 *              exist in NV, NV_OPER_FAILED if failure.
 */
uint8 zgInit( void )
{
  uint8  setDefault = FALSE;

  // Do we want to default the Config state values
  if ( zgReadStartupOptions() & ZCD_STARTOPT_DEFAULT_CONFIG_STATE )
  {
    setDefault = TRUE;
  }

#if defined ( FEATURE_SYSTEM_STATS )
  // This sections tracks the number of resets
  uint16 bootCnt = 0;

  // Update the Boot Counter
  if ( osal_nv_item_init( ZCD_NV_BOOTCOUNTER, sizeof(bootCnt), &bootCnt ) == ZSUCCESS )
  {
    // Get the old value from NV memory
    osal_nv_read( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt );
  }

  // Increment the Boot Counter and store it into NV memory
  if ( setDefault )
  {
    bootCnt = 0;
  }
  else
  {
    bootCnt++;
  }

  osal_nv_write( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt );
#endif  // FEATURE_SYSTEM_STATS

  // Initialize the Extended PAN ID as my own extended address
  ZMacGetReq( ZMacExtAddr, zgExtendedPANID );

  // Initialize the items table
  zgInitItems( setDefault );

#ifndef NONWK
  if ( ZG_SECURE_ENABLED )
  {
    // Initialize the Pre-Configured Key to the default key
    zgPreconfigKeyInit( setDefault );

    // Initialize NV items for all Keys: NWK, APS, TCLK and Master
    ZDSecMgrInitNVKeyTables( setDefault );
  }
#endif // NONWK

  // Clear the Config State default
  if ( setDefault )
  {
    zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_CONFIG_STATE );
  }

  return ( ZSUCCESS );
}
示例#4
0
/*********************************************************************
 * @fn          zgInit
 *
 * @brief
 *
 *   Initialize the Z-Stack Globals. If an item doesn't exist in
 *   NV memory, write the system default into NV memory. But if
 *   it exists, set the item to the value stored in NV memory.
 *
 * NOTE: The Startup Options (ZCD_NV_STARTUP_OPTION) indicate
 *       that the Config state items (zgItemTable) need to be
 *       set to defaults (ZCD_STARTOPT_DEFAULT_CONFIG_STATE). The
 *
 *
 * @param       none
 *
 * @return      ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
 *              exist in NV, NV_OPER_FAILED if failure.
 */
uint8 zgInit( void )
{
  uint8  i = 0;
  uint8  setDefault = FALSE;

  // Do we want to default the Config state values
  if ( zgReadStartupOptions() & ZCD_STARTOPT_DEFAULT_CONFIG_STATE )
  {
    setDefault = TRUE;
  }

#if 0
  // Enable this section if you need to track the number of resets
  // This section is normally disabled to minimize "wear" on NV memory
  uint16 bootCnt = 0;

  // Update the Boot Counter
  if ( osal_nv_item_init( ZCD_NV_BOOTCOUNTER, sizeof(bootCnt), &bootCnt ) == ZSUCCESS )
  {
    // Get the old value from NV memory
    osal_nv_read( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt );
  }

  // Increment the Boot Counter and store it into NV memory
  if ( setDefault )
    bootCnt = 0;
  else
    bootCnt++;
  osal_nv_write( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt );
#endif

  // Initialize the Extended PAN ID as my own extended address
  ZMacGetReq( ZMacExtAddr, zgExtendedPANID );

#ifndef NONWK
  // Initialize the Pre-Configured Key to the default key
  osal_memcpy( zgPreConfigKey, defaultKey, SEC_KEY_LEN );  // Do NOT Change!!!
#endif // NONWK

  while ( zgItemTable[i].id != 0x00 )
  {
    // Initialize the item
    zgItemInit( zgItemTable[i].id, zgItemTable[i].len, zgItemTable[i].buf, setDefault  );

    // Move on to the next item
    i++;
  }

  // Clear the Config State default
  if ( setDefault )
  {
    zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_CONFIG_STATE );
  }

  return ( ZSUCCESS );
}
示例#5
0
/**************************************************************************************************
 * @fn          znpTestRF
 *
 * @brief       This function initializes and checks the ZNP RF Test Mode NV items. It is designed
 *              to be invoked before/instead of MAC radio initialization.
 *
 * input parameters
 *
 * None.
 *
 * output parameters
 *
 * None.
 *
 * @return      None.
 */
void znpTestRF(void)
{
  uint8 rfTestParms[4] = { 0, 0, 0, 0 };

  if ((SUCCESS != osal_nv_item_init(ZNP_NV_RF_TEST_PARMS, 4, rfTestParms))  ||
      (SUCCESS != osal_nv_read(ZNP_NV_RF_TEST_PARMS, 0, 4, rfTestParms)) ||
      (rfTestParms[0] == 0))
  {
    return;
  }

  // Settings from SmartRF Studio
  MDMCTRL0 = 0x85;
  RXCTRL = 0x3F;
  FSCTRL = 0x5A;
  FSCAL1 = 0x2B;
  AGCCTRL1 = 0x11;
  ADCTEST0 = 0x10;
  ADCTEST1 = 0x0E;
  ADCTEST2 = 0x03;

  FRMCTRL0 = 0x43;
  FRMCTRL1 = 0x00;

  MAC_RADIO_RXTX_OFF();
  MAC_RADIO_SET_CHANNEL(rfTestParms[1]);
  MAC_RADIO_SET_TX_POWER(rfTestParms[2]);
  TX_PWR_TONE_SET(rfTestParms[3]);

  switch (rfTestParms[0])
  {
  case 1:  // Rx promiscuous mode.
    MAC_RADIO_RX_ON();
    break;

  case 2:  // Un-modulated Tx.
    TX_PWR_MOD__SET(1);
    // no break;

  case 3:  // Modulated Tx.
    // Modulated is default register setting, so no special action.

    // Now turn on Tx power for either mod or un-modulated Tx test.
    MAC_RADIO_TX_ON();
    break;

  default:  // Not expected.
    break;
  }

  // Clear the RF test mode.
  (void)osal_memset(rfTestParms, 0, 4);
  (void)osal_nv_write(ZNP_NV_RF_TEST_PARMS, 0, 4, rfTestParms);

  while (1);  // Spin in RF test mode until a hard reset.
}
示例#6
0
文件: protocol.c 项目: cuu/weiyi
static void start_dev(void)
{
  
 #if defined ( HOLD_AUTO_START )
    ZDOInitDevice(0);
    osal_nv_item_init( 0x0201, sizeof(start_flag), &start_flag);
    start_flag = 1;
    osal_nv_write(0x0201,0,  sizeof(start_flag), &start_flag);
    
 #endif     

}
示例#7
0
void deviceManagerInit(void){
	ZDO_DeviceAnnce_t * iter, *end;
	end = table +TABLE_SIZE;
	for(iter = table; iter != end; iter++){
		iter->nwkAddr=INVALID;
	}
	osal_nv_item_init(NV_ID, sizeof(ZDO_DeviceAnnce_t)*TABLE_SIZE, table);
	uint16 size = osal_nv_item_len(NV_ID);
	if (size > TABLE_SIZE*sizeof(ZDO_DeviceAnnce_t)){
		size  = TABLE_SIZE*sizeof(ZDO_DeviceAnnce_t);
	}
	osal_nv_read(NV_ID, 0, size, table);
}
示例#8
0
void Setid(void)
{
  uint16 gu16RecBuffLen;
  
   if ( osal_nv_item_init( ZCD_NV_ID,
                              2,
                              &gu16RecBuffLen ) == ZSUCCESS )
  
  {
    osal_nv_read(ZCD_NV_ID,0,2,&gu16RecBuffLen);
  }
  zgID=gu16RecBuffLen;
}
/***************************************************************************************************
 * @fn      MT_SysOsalNVItemInit
 *
 * @brief   Attempt to create an NV item
 *
 * @param   uint8 pData - pointer to the data
 *
 * @return  None
 ***************************************************************************************************/
void MT_SysOsalNVItemInit(uint8 *pBuf)
{
  uint8 ret;
  uint8 idLen;
  uint16 nvId;
  uint16 nvLen;

  /* Skip over RPC header */
  pBuf += MT_RPC_FRAME_HDR_SZ;

  /* NV item ID */
  nvId = BUILD_UINT16(pBuf[0], pBuf[1]);
  /* NV item length */
  nvLen = BUILD_UINT16(pBuf[2], pBuf[3]);
  /* Initialization data length */
  idLen = pBuf[4];
  pBuf += 5;

  if ( idLen < nvLen )
  {
    /* Attempt to create a new NV item */
    ret = osal_nv_item_init( nvId, nvLen, NULL );
    if ( (ret == NV_ITEM_UNINIT) && (idLen > 0) )
    {
      /* Write initialization data to first part of new item */
      (void) osal_nv_write( nvId, 0, (uint16)idLen, pBuf );
    }
  }
  else
  {
    /* Attempt to create/initialize a new NV item */
    ret = osal_nv_item_init( nvId, nvLen, pBuf );
  }

  /* Build and send back the response */
  MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
                                 MT_SYS_OSAL_NV_ITEM_INIT, 1, &ret);
}
示例#10
0
/*********************************************************************
 * @fn      zclCCServer_InitStartupParametersInNV
 *
 * @brief   Initialize the Startup Parameters in NV.
 *
 * @param   none
 *
 * @return  none
 */
static void zclCCServer_InitStartupParametersInNV( void )
{
  uint8 key[SEC_KEY_LEN];

  for ( uint8 i = 0; nvItemTable[i].id != 0x00; i++ )
  {
    // Initialize the item
    osal_nv_item_init( nvItemTable[i].id, nvItemTable[i].len, nvItemTable[i].buf );
  }

  // Use the default key values in the NV
  osal_nv_read( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, key );
  osal_nv_item_init( ZCD_NV_SAS_TC_MASTER_KEY, SEC_KEY_LEN, key );

  osal_nv_read( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, key );
  osal_nv_item_init( ZCD_NV_SAS_NWK_KEY, SEC_KEY_LEN, key );

  osal_nv_read( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, key );
  osal_nv_item_init( ZCD_NV_SAS_PRECFG_LINK_KEY, SEC_KEY_LEN, key );

  // Clear copy of key in RAM
  osal_memset( key, 0x00, SEC_KEY_LEN );
}
示例#11
0
/*********************************************************************
 * @fn          BindInitNV
 *
 * @brief       Initialize the Binding NV Item
 *
 * @param       none
 *
 * @return      ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
 *              exist in NV, NV_OPER_FAILED if failure.
 */
byte BindInitNV( void )
{
  byte ret;

  // Initialize the device list
  ret = osal_nv_item_init( ZCD_NV_BINDING_TABLE,
                  (uint16)( sizeof( nvBindingHdr_t ) + NV_BIND_ITEM_SIZE ), NULL );

  if ( ret != ZSUCCESS )
  {
    BindSetDefaultNV();
  }

  return ( ret );
}
示例#12
0
/*********************************************************************
 * @fn          zclCCServer_Init
 *
 * @brief       Initialization function for the ZCL Commissioing Cluster
 *              Server Application.
 *
 * @param       task_id - task id
 *
 * @return      none
 */
void zclCCServer_Init( uint8 task_id )
{
  zclCCServer_TaskID = task_id;

  leaveInitiated = FALSE;

  // This app is part of the Home Automation Profile
  zba_Init( &zclCCServer_SimpleDesc );

  // Register the ZCL Commissioning Cluster Library callback functions
  zclCC_RegisterCmdCallbacks( CCSERVER_ENDPOINT, &zclCCServer_CmdCallbacks );

  // Register the application's attribute list
  zcl_registerAttrList( CCSERVER_ENDPOINT, CCSERVER_MAX_ATTRIBUTES, zclCCServer_Attrs );

  // Register the application's attribute data validation callback function
  zcl_registerValidateAttrData( zclCCServer_ValidateAttrDataCB );

  // Register the application's callback function to read/write attribute data
  zcl_registerReadWriteCB( CCSERVER_ENDPOINT, zclCCServer_ReadWriteCB, zclCCServer_AuthorizeCB );

  // Register for Initiator to receive Leave Confirm
  ZDO_RegisterForZdoCB( ZDO_LEAVE_CNF_CBID, zclCCServer_ZdoLeaveCnfCB );

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

  // Initialize ZBA Startup Attributes Set (SAS)
  zclCCServer_InitStartupParameters( TRUE );

  // See if the device is factory new
  if ( !ZDApp_DeviceConfigured() )
  {
    osal_nv_item_init( ZCD_NV_NWKMGR_ADDR, sizeof( zclCCServer_NwkManagerAddr ),
                       (void *)&zclCCServer_NwkManagerAddr );

    // On startup, attempt to join the network specified by the startup SAS
    // on all channels at least once

    // ZBA Default Settings with the default ZBA Key Material and ZBA EPID
    if ( nullExtendedPANID( zgApsUseExtendedPANID ) )
    {
      osal_cpyExtAddr( zgApsUseExtendedPANID, zbaGlobalCommissioningEPID );
    }

    // Default Network Key and Pre-configured Link Key should already be set
  }
}
示例#13
0
文件: protocol.c 项目: cuu/weiyi
void set_coordi(void)
{
    uint8 logicalType;

        osal_nv_item_init( ZCD_NV_LOGICAL_TYPE, sizeof(logicalType), &logicalType );
        logicalType = ZG_DEVICETYPE_COORDINATOR;
        if( osal_nv_write( ZCD_NV_LOGICAL_TYPE, 0 ,sizeof(logicalType), &logicalType) != ZSUCCESS)
        {
          uprint("set device to coordi failed");
        }else
        {
          zgWriteStartupOptions (ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);
          uprint("set device to coordi,restart it");
        }         
        return; 
}
示例#14
0
文件: ZGlobals.c 项目: 12019/hellowsn
/*********************************************************************
 * @fn          zgReadStartupOptions
 *
 * @brief       Reads the ZCD_NV_STARTUP_OPTION NV Item.
 *
 * @param       none
 *
 * @return      the ZCD_NV_STARTUP_OPTION NV item
 */
uint8 zgReadStartupOptions( void )
{
  // Default to Use Config State and Use Network State
  uint8 startupOption = 0;

  // This should have been done in ZMain.c, but just in case.
  if ( osal_nv_item_init( ZCD_NV_STARTUP_OPTION,
                              sizeof(startupOption),
                              &startupOption ) == ZSUCCESS )
  {
    // Read saved startup control
    osal_nv_read( ZCD_NV_STARTUP_OPTION,
                  0,
                  sizeof( startupOption ),
                  &startupOption);
  }
  return ( startupOption );
}
示例#15
0
文件: protocol.c 项目: cuu/weiyi
void set_router(void)
{
    uint8 logicalType;
          osal_nv_item_init( ZCD_NV_LOGICAL_TYPE, sizeof(logicalType), &logicalType );
          logicalType = ZG_DEVICETYPE_ROUTER;
          if( osal_nv_write( ZCD_NV_LOGICAL_TYPE, 0 ,sizeof(logicalType), &logicalType) != ZSUCCESS)
          {
            uprint("set device to router failed");
          }else
          {
            zgWriteStartupOptions (ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);
            uprint("set device to router,restart it");
          }
          
//          zgWriteStartupOptions (ZG_STARTUP_SET, 0x02);
//          zgInit();
//          ZDOInitDevice( 0 );
//          SystemReset();   
          return;  
}
示例#16
0
/*********************************************************************
 * @fn       zgItemInit()
 *
 * @brief
 *
 *   Initialize a global item. If the item doesn't exist in NV memory,
 *   write the system default (value passed in) into NV memory. But if
 *   it exists, set the item to the value stored in NV memory.
 *
 *   Also, if setDefault is TRUE and the item exists, we will write
 *   the default value to NV space.
 *
 * @param   id - item id
 * @param   len - item len
 * @param   buf - pointer to the item
 * @param   setDefault - TRUE to set default, not read
 *
 * @return  ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
 *          exist in NV, NV_OPER_FAILED if failure.
 */
static uint8 zgItemInit( uint16 id, uint16 len, void *buf, uint8 setDefault )
{
  uint8 status;

  // If the item doesn't exist in NV memory, create and initialize
  // it with the value passed in.
  status = osal_nv_item_init( id, len, buf );
  if ( status == ZSUCCESS )
  {
    if ( setDefault )
    {
      // Write the default value back to NV
      status = osal_nv_write( id, 0, len, buf );
    }
    else
    {
      // The item exists in NV memory, read it from NV memory
      status = osal_nv_read( id, 0, len, buf );
    }
  }

  return (status);
}
示例#17
0
//static uint8 zgPreconfigKeyInit( uint8 setDefault )
uint8 zgPreconfigKeyInit( uint8 setDefault )
{
  uint8 zgPreConfigKey[SEC_KEY_LEN];
  uint8 status;

  // Initialize the Pre-Configured Key to the default key
  osal_memcpy( zgPreConfigKey, defaultKey, SEC_KEY_LEN );

  // If the item doesn't exist in NV memory, create and initialize it
  status = osal_nv_item_init( ZCD_NV_PRECFGKEY, SEC_KEY_LEN, zgPreConfigKey );
  if ( status == ZSUCCESS )
  {
    if ( setDefault )
    {
      // Write the default value back to NV
      status =  osal_nv_write( ZCD_NV_PRECFGKEY, 0, SEC_KEY_LEN, zgPreConfigKey );
    }
  }

  // clear local copy of default key
  osal_memset(zgPreConfigKey, 0x00, SEC_KEY_LEN);

  return (status);
}
示例#18
0
/*********************************************************************
 * @fn          BindCopyBackupToNewNV
 *
 * @brief       Creates the New NV item, copies the backup data into
 *              the New NV ID, and Deletes the duplicate NV item.
 *
 * @param       dupLen - NV item length of the old Binding table.
 * @param       newLen - NV item length of the new Binding table to be created.
 *
 * @return      ZSuccess - All the actions were successful.
 *              ZFailure - Any of the actions failed.
 */
static uint8 BindCopyBackupToNewNV( uint16 dupLen, uint16 newLen )
{
  uint8 status = ZSuccess;
  uint16 bindLen;

  bindLen = osal_nv_item_len( ZCD_NV_BINDING_TABLE );


  if ( ( bindLen > 0 ) && ( bindLen != newLen ) )
  {
    // The existing item does not match the New length
    osal_nv_delete( ZCD_NV_BINDING_TABLE, bindLen );
  }

  // Create Binding Table NV item with the NEW legth
  if ( osal_nv_item_init( ZCD_NV_BINDING_TABLE, newLen, NULL ) != NV_OPER_FAILED )
  {
    nvBindingHdr_t hdrBackup;

    // Copy ONLY the valid records from the duplicate NV table into the new table
    // at the end of this process the table content will be compacted
    if ( osal_nv_read( ZCD_NV_DUPLICATE_BINDING_TABLE, 0, sizeof(nvBindingHdr_t), &hdrBackup ) == ZSuccess )
    {
      bindTableIndex_t i;
      uint16 validBackupRecs = 0;
      BindingEntry_t backupRec;

      // Read in the device list. This loop will stop when:
      // The total number of valid records has been reached either because:
      //          The new table is full of valid records OR
      //          The old table has less valid records than the size of the table
      for ( i = 0; ( validBackupRecs < gNWK_MAX_BINDING_ENTRIES ) && ( validBackupRecs < hdrBackup.numRecs ); i++ )
      {
        if ( osal_nv_read( ZCD_NV_DUPLICATE_BINDING_TABLE,
                          (uint16)(sizeof(nvBindingHdr_t) + (i * NV_BIND_REC_SIZE)),
                          NV_BIND_REC_SIZE, &backupRec ) == ZSuccess )
        {
          if ( backupRec.srcEP != NV_BIND_EMPTY )
          {
            // Save the valid record into the NEW NV table.
            if ( osal_nv_write( ZCD_NV_BINDING_TABLE,
                                (uint16)((sizeof(nvBindingHdr_t)) + (validBackupRecs * NV_BIND_REC_SIZE)),
                                NV_BIND_REC_SIZE, &backupRec ) != ZSuccess )
            {
               status = ZFailure;
               break; // Terminate the loop as soon as a problem with NV is detected
            }

            validBackupRecs++;
          }
        }
        else
        {
           status = ZFailure;
           break; // Terminate the loop as soon as a problem with NV is detected
        }
      }

      // Only save the header and delete the duplicate element if the previous
      // process was successful
      if ( status == ZSuccess )
      {
        // Save off the header
        if ( osal_nv_write( ZCD_NV_BINDING_TABLE, 0,
                            sizeof(nvBindingHdr_t), &validBackupRecs ) == ZSuccess )
        {
          // Delete the duplicate NV Item, once the data has been stored in the NEW table
          if ( osal_nv_delete( ZCD_NV_DUPLICATE_BINDING_TABLE, dupLen ) != ZSuccess )
          {
            status = ZFailure;
          }
        }
        else
        {
          status = ZFailure;
        }
      }
    }
    else
    {
      status = ZFailure;
    }
  }
  else
  {
    status = ZFailure;
  }

  return ( status );
}
示例#19
0
/*********************************************************************
 * @fn      zclCCServer_InitStartupParameters
 *
 * @brief   Initialize the current Startup Parameters Set (SAS) to 
 *          their default values.
 *
 * @param   initNv - whether to initialize keys in NV
 *
 * @return  none
 */
static void zclCCServer_InitStartupParameters( uint8 initNv )
{
  uint8 zbaDefaultKey[SEC_KEY_LEN] = DEFAULT_TC_LINK_KEY;
  
  zclCCServer_ShortAddress = CC_DEFAULT_SHORT_ADDR;

  osal_cpyExtAddr( zclCCServer_ExtendedPanId, zbaGlobalCommissioningEPID );

  zclCCServer_PanId = CC_DEFAULT_PANID;
  zclCCServer_ChannelMask = DEFAULT_CHANLIST;
  zclCCServer_ProtocolVersion = ZB_PROT_VERS;
  zclCCServer_StackProfile = STACK_PROFILE_ID;

  if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  {
    // Form the Commissioning Network
    zclCCServer_StartUpControl = CC_STARTUP_CONTROL_OPTION_1;
  }
  else
  {
    // Join the Commissioning Network
    zclCCServer_StartUpControl = CC_STARTUP_CONTROL_OPTION_3;
  }

  osal_memcpy( zclCCServer_TrustCenterAddr, 0x00, Z_EXTADDR_LEN);

  if ( initNv )
  {
    // If the item doesn't exist in NV memory, create and initialize it
    if ( osal_nv_item_init( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 
                            SEC_KEY_LEN, zbaDefaultKey ) == SUCCESS )
    {
      // Write the default value back to NV
      osal_nv_write( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
    }

    osal_memset( zbaDefaultKey, 0x00, SEC_KEY_LEN );
  
    if ( osal_nv_item_init( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 
                            SEC_KEY_LEN, zbaDefaultKey ) == SUCCESS )
    {
      // Write the default value back to NV
      osal_nv_write( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
    }

    if ( osal_nv_item_init( ZCD_NV_SAS_CURR_NWK_KEY,
                            SEC_KEY_LEN, zbaDefaultKey ) == SUCCESS )
    {
      // Write the default value back to NV
      osal_nv_write( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
    }
  }
  else
  {
    // Save the default keys directly in the NV
    osal_nv_write( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  
    osal_memset( zbaDefaultKey, 0x00, SEC_KEY_LEN );
  
    osal_nv_write( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
    osal_nv_write( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  }

  zclCCServer_UseInsecureJoin = TRUE;

  zclCCServer_NetworkKeySeqNum = CC_DEFAULT_NETWORK_KEY_SEQ_NUM;
  zclCCServer_NetworkKeyType = KEY_TYPE_NWK;
  zclCCServer_NwkManagerAddr = CC_DEFAULT_NETWORK_MANAGER_ADDR;
}
示例#20
0
文件: GenericApp.c 项目: cuu/weiyi
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) 
}