示例#1
0
文件: busevents.c 项目: pi2p/libyasdi
//! An Peer was removed. Remove this device from the current list of devices
void TSMADataMaster_OnEventPeerRemoved(TGenDriverEvent * event)
{
   TNetDevice * dev;
   WORD netaddr = 0;

   //try to resolve from "DriverDeviceHandle" to the right device pointer
   if (TRoute_FindAddrByDriverDevicePeer(event->DriverID, event->EventData.DriverDeviceHandle, &netaddr) &&
      (dev = TPlant_FindDevAddr(netaddr)) != NULL)
   {
      /*Master command to detect the new device...*/
      TMasterCmdReq * mc = TMasterCmdFactory_GetMasterCmd( MC_REMOVE_DEVICE );
      mc->OnEnd          = TSMADataMaster_OnEventMasterCmdEnded;
      mc->Param.DevHandle  = TNetDevice_GetHandle( dev );

       YASDI_DEBUG((VERBOSE_ERROR,"TSMADataMaster_OnEventPeerRemoved(): "
                "Removing Device '%s', DriverDevHandle=%d\n",
                TNetDevice_GetName( dev ), event->EventData.DriverDeviceHandle  ));


      /*Send master command (async)...*/
      TSMADataMaster_AddCmd( mc );
   }
   else
   {
      YASDI_DEBUG((VERBOSE_ERROR,"TSMADataMaster_OnEventPeerRemoved(): "
                   "Unknown device to remove, DriverDevHandle=0x%x...\n",
                   event->EventData.DriverDeviceHandle));
   }
}
示例#2
0
void TStateConfig_CheckNextDevice( TMasterCmdReq * mc )
{
   TNetDevice * dev;
   assert(mc->NewFoundDevList);
   dev = GET_NEXT_DEVICE(mc->NewFoundDevListIter, mc->NewFoundDevList->DevList);
   if (dev)
  	{
      BOOL bAddressMustChanged = false;
  		WORD OrigNetAddr, NewNetAddr;
  		OrigNetAddr = TNetDevice_GetNetAddr( dev );

  		/* collision of network address? => changing... */
  		if (TPlant_CheckNetAddrCollision( &Plant, dev ))
  		{
			YASDI_DEBUG((VERBOSE_MASTER,
                      "TStateConfig::CheckNextDevice(): "
                      "Net address collision of Device '%s'! Changing...\n",
                      TNetDevice_GetName(dev) ));
         bAddressMustChanged = true;
  		}

      /* Device has the same adress of the master? => Changing */
      if (TNetDevice_GetNetAddr(dev) == 0)
      {
			YASDI_DEBUG((VERBOSE_MASTER,
                      "TStateConfig::CheckNextDevice(): "
                      "Device '%s' has the same address of "
                      "the master! => Changing...\n",
                      TNetDevice_GetName(dev) ));
         bAddressMustChanged = true;
      }

      /* Check the address range of the device */
      if (((TNetDevice_GetNetAddr(dev) & 0x00ff) < (int)Master.DeviceAddrRangeLow) ||
          ((TNetDevice_GetNetAddr(dev) & 0x00ff) > (int)Master.DeviceAddrRangeHigh) )
      {
			YASDI_DEBUG((VERBOSE_MASTER,
                      "TStateConfig::CheckNextDevice(): "
                      "Net address of Device '%s' is out of the allowed range!"
                      " Allowed:[0x%x..0x%x] => Changing...\n",
                      TNetDevice_GetName(dev),
                      (int)Master.DeviceAddrRangeLow,
                      (int)Master.DeviceAddrRangeHigh ));
         bAddressMustChanged = true;
      }

      /* Need to change the device address? => Changing... */
      if (bAddressMustChanged)
         TNetDevice_SetNetAddr(dev, TPlant_GetUniqueNetAddr( &Plant, dev,
                                                     Master.DeviceAddrRangeLow,
                                                     Master.DeviceAddrRangeHigh )
                        );
      /* BUS-ID ok? */
      if ((TNetDevice_GetNetAddrBus(dev) < (int)Master.DeviceAddrBusRangeLow) ||
          (TNetDevice_GetNetAddrBus(dev) > (int)Master.DeviceAddrBusRangeHigh))
      {
         TNetDevice_SetNetAddrBus(dev, Master.DeviceAddrBusRangeLow);
      }

      /* String-ID ok?*/
      if ((TNetDevice_GetNetAddrString(dev) < (int)Master.DeviceAddrBusRangeLow) ||
          (TNetDevice_GetNetAddrString(dev) > (int)Master.DeviceAddrBusRangeHigh))
      {
         TNetDevice_SetNetAddrString(dev, Master.DeviceAddrBusRangeLow);
      }

      /*restore original net address and get the new one */
      NewNetAddr = TNetDevice_GetNetAddr(dev);
      TNetDevice_SetNetAddr( dev, OrigNetAddr );


   	/* Erzeuge IORequest fuer die Konfiguration */
   	/* CMD_GET_CFG_NETADR */
   	TSMAData_InitReqCfgNetAddr(
                  mc->IOReq,
	               Master.SrcAddr,		         /* eigene Netzadresse */
                  TNetDevice_GetSerNr( dev ),   /* das angeprochene Geraet */
                  NewNetAddr,			            /* die neue Netzadresse des Geraetes */
                  4,				                  /* Timeout */
                  5,                            /* Repeat */
                  dev->prodID                   //the transportprotocol....
                  );

		YASDI_DEBUG(( VERBOSE_MASTER,
                    "TStateConfig::CheckNextDevice(): Configure Device "
                    "'%s' to address 0x%x...\n",
                    TNetDevice_GetName( dev ),
                    NewNetAddr ));

  		TSMAData_AddIORequest( mc->IOReq );

  	}
  	else
 	{
 		/* alle erfassten Geraete konfiguriert => In den Status
         "Detection" zurueckkehren */

 		//TDevListIter_Destructor( Master.DevIter );
		TSMADataCmd_ChangeState( mc, TStateDetect_GetInstance() );
 	}
}