/***************************************************************************************************
 * @fn      MT_SysSetTxPower
 *
 * @brief   Set the transmit power.
 *
 * @param   pBuf - MT message containing the ZMacTransmitPower_t power level to set.
 *
 * @return  None
 ***************************************************************************************************/
void MT_SysSetTxPower(uint8 *pBuf)
{
  /* A local variable to hold the signed dBm value of TxPower that is being requested. */
  uint8 signed_dBm_of_TxPower_requeseted;

  /*
   * A local variable to hold the signed dBm value of TxPower that can be set which is closest to
   * the requested dBm value of TxPower, but which is also valid according to a complex set of
   * compile-time and run-time configuration which is interpreted by the macRadioSetTxPower()
   * function.
   */
  uint8 signed_dBm_of_TxPower_range_corrected;

  /* Parse the requested dBm from the RPC message. */
  signed_dBm_of_TxPower_requeseted = pBuf[MT_RPC_POS_DAT0];

  /*
   * MAC_MlmeSetReq() will store an out-of-range dBm parameter value into the NIB. So it is not
   * possible to learn the actual dBm value that will be set by invoking MACMlmeGetReq().
   * But this actual dBm value is a required return value in the SRSP to this SREQ. Therefore,
   * it is necessary to make this redundant pre-call to macRadioSetTxPower() here in order to run
   * the code that will properly constrain the requested dBm to a valid range based on both the
   * compile-time and the run-time configurations that affect the available valid ranges
   * (i.e. MAC_MlmeSetReq() itself will invoke for a second time the macRadioSetTxPower() function).
   */
  signed_dBm_of_TxPower_range_corrected = macRadioSetTxPower(signed_dBm_of_TxPower_requeseted);

  /*
   * Call the function to store the requested dBm in the MAC PIB and to set the TxPower as closely
   * as possible within the TxPower range that is valid for the compile-time and run-time
   * configuration.
   */
  (void)MAC_MlmeSetReq(MAC_PHY_TRANSMIT_POWER_SIGNED, &signed_dBm_of_TxPower_requeseted);

  /* Build and send back the response that includes the actual dBm TxPower that can be set. */
  MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
                                       MT_SYS_SET_TX_POWER, 1,
                                       &signed_dBm_of_TxPower_range_corrected);
}
Exemple #2
0
/**************************************************************************************************
 * @fn          MAC_MlmeSetReq
 *
 * @brief       This direct execute function sets an attribute value
 *              in the MAC PIB.
 *
 * input parameters
 *
 * @param       pibAttribute - The attribute identifier.
 * @param       pValue - pointer to the attribute value.
 *
 * output parameters
 *
 * None.
 *
 * @return      The status of the request, as follows:
 *              MAC_SUCCESS Operation successful.
 *              MAC_UNSUPPORTED_ATTRIBUTE Attribute not found.
 *
 **************************************************************************************************
 */
uint8 MAC_MlmeSetReq(uint8 pibAttribute, void *pValue)
{
  uint8         i;
  halIntState_t intState;

  if (pibAttribute == MAC_BEACON_PAYLOAD)
  {
    macPib.pBeaconPayload = pValue;
    return MAC_SUCCESS;
  }

  /* look up attribute in PIB table */
  if ((i = macPibIndex(pibAttribute)) == MAC_PIB_INVALID)
  {
    return MAC_UNSUPPORTED_ATTRIBUTE;
  }

  /* do range check; no range check if min and max are zero */
  if ((macPibTbl[i].min != 0) || (macPibTbl[i].max != 0))
  {
    /* if min == max, this is a read-only attribute */
    if (macPibTbl[i].min == macPibTbl[i].max)
    {
      return MAC_READ_ONLY;
    }

    /* check for special cases */
    if (pibAttribute == MAC_MAX_FRAME_TOTAL_WAIT_TIME)
    {
      if ((*((uint16 *) pValue) < MAC_MAX_FRAME_RESPONSE_MIN) ||
          (*((uint16 *) pValue) > MAC_MAX_FRAME_RESPONSE_MAX))
      {
        return MAC_INVALID_PARAMETER;
      }
    }

    /* range check for general case */
    if ((*((uint8 *) pValue) < macPibTbl[i].min) || (*((uint8 *) pValue) > macPibTbl[i].max))
    {
      return MAC_INVALID_PARAMETER;
    }

  }

  /* set value in PIB */
  HAL_ENTER_CRITICAL_SECTION(intState);
  osal_memcpy((uint8 *) &macPib + macPibTbl[i].offset, pValue, macPibTbl[i].len);
  HAL_EXIT_CRITICAL_SECTION(intState);

  /* handle special cases */
  switch (pibAttribute)
  {
    case MAC_PAN_ID:
      /* set pan id in radio */
      macRadioSetPanID(macPib.panId);
      break;

    case MAC_SHORT_ADDRESS:
      /* set short address in radio */
      macRadioSetShortAddr(macPib.shortAddress);
      break;

    case MAC_RX_ON_WHEN_IDLE:
      /* turn rx on or off */
      if (macPib.rxOnWhenIdle)
      {
        macRxEnable(MAC_RX_WHEN_IDLE);
      }
      else
      {
        macRxDisable(MAC_RX_WHEN_IDLE);
      }
      break;

    case MAC_LOGICAL_CHANNEL:
      macRadioSetChannel(macPib.logicalChannel);
      break;

    case MAC_EXTENDED_ADDRESS:
      /* set ext address in radio */
      macRadioSetIEEEAddr(macPib.extendedAddress.addr.extAddr);
      break;

#ifndef MAC_OBSOLETE_PHY_TRANSMIT_POWER
    /* Define MAC_OBSOLETE_PHY_TRANSMIT_POWER to save some code */

    case MAC_PHY_TRANSMIT_POWER:
      /* Legacy transmit power attribute */
#if !defined HAL_MAC_USE_REGISTER_POWER_VALUES && \
  !defined HAL_PA_LNA && !defined HAL_PA_LNA_CC2590
      /* Legacy transmit power attribute value for CC2530 alone,
       * or runtime selection support build means a negative absolute value.
       * However, when used as register power values or
       * with HAL_PA_LNAxxx definition (without runtime selection)
       * the attribute value is not a negative absolute value. */
      macPib.phyTransmitPower = (uint8)(-(int8)macPib.phyTransmitPower);
#endif /* !defined HAL_MAC_USE_REGISTER_POWER_VALUES && ... */
      /* pass through to next case -- do not break*/

#endif /* MAC_OBSOLETE_PHY_TRANSMIT_POWER */

    case MAC_PHY_TRANSMIT_POWER_SIGNED:
      (void)macRadioSetTxPower(macPib.phyTransmitPower);
      break;

    default:
      break;
  }

  return MAC_SUCCESS;
}
Exemple #3
0
/**************************************************************************************************
 * @fn          MAC_MlmeSetReq
 *
 * @brief       This direct execute function sets an attribute value
 *              in the MAC PIB.
 *
 * input parameters
 *
 * @param       pibAttribute - The attribute identifier.
 * @param       pValue - pointer to the attribute value.
 *
 * output parameters
 *
 * None.
 *
 * @return      The status of the request, as follows:
 *              MAC_SUCCESS Operation successful.
 *              MAC_UNSUPPORTED_ATTRIBUTE Attribute not found.
 *
 **************************************************************************************************
 */
uint8 MAC_MlmeSetReq(uint8 pibAttribute, void *pValue)
{
    uint8         i;
    halIntState_t intState;

    if (pibAttribute == MAC_BEACON_PAYLOAD)
    {
        pMacPib->pBeaconPayload = pValue;
        return MAC_SUCCESS;
    }

    /* look up attribute in PIB table */
    if ((i = MAP_macPibIndex(pibAttribute)) == MAC_PIB_INVALID)
    {
        return MAC_UNSUPPORTED_ATTRIBUTE;
    }

    /* do range check; no range check if min and max are zero */
    if ((macPibTbl[i].min != 0) || (macPibTbl[i].max != 0))
    {
        /* if min == max, this is a read-only attribute */
        if (macPibTbl[i].min == macPibTbl[i].max)
        {
            return MAC_READ_ONLY;
        }

        /* check for special cases */
        if (pibAttribute == MAC_MAX_FRAME_TOTAL_WAIT_TIME)
        {
            if ((*((uint16 *) pValue) < MAC_MAX_FRAME_RESPONSE_MIN) ||
                    (*((uint16 *) pValue) > MAC_MAX_FRAME_RESPONSE_MAX))
            {
                return MAC_INVALID_PARAMETER;
            }
        }

        /* range check for general case */
        if ((*((uint8 *) pValue) < macPibTbl[i].min) || (*((uint8 *) pValue) > macPibTbl[i].max))
        {
            return MAC_INVALID_PARAMETER;
        }

    }

    /* set value in PIB */
    HAL_ENTER_CRITICAL_SECTION(intState);
    osal_memcpy((uint8 *) pMacPib + macPibTbl[i].offset, pValue, macPibTbl[i].len);
    HAL_EXIT_CRITICAL_SECTION(intState);

    /* handle special cases */
    switch (pibAttribute)
    {
    case MAC_PAN_ID:
        /* set pan id in radio */
        macRadioSetPanID(pMacPib->panId);
        break;

    case MAC_SHORT_ADDRESS:
        /* set short address in radio */
        macRadioSetShortAddr(pMacPib->shortAddress);
        break;

    case MAC_RX_ON_WHEN_IDLE:
        /* turn rx on or off */
        if (pMacPib->rxOnWhenIdle)
        {
            macRxEnable(MAC_RX_WHEN_IDLE);
        }
        else
        {
            macRxDisable(MAC_RX_WHEN_IDLE);
        }
        break;

    case MAC_LOGICAL_CHANNEL:
        macRadioSetChannel(pMacPib->logicalChannel);
        break;

    case MAC_EXTENDED_ADDRESS:
        /* set ext address in radio */
        macRadioSetIEEEAddr(pMacPib->extendedAddress.addr.extAddr);
        break;

    case MAC_PHY_TRANSMIT_POWER_SIGNED:
        (void)macRadioSetTxPower(pMacPib->phyTransmitPower);
        break;

    case MAC_RF4CE_POWER_SAVINGS:
        pMacPib->rf4cepowerSavings = *(uint8 *)pValue;
        break;

    case MAC_FRAME_VERSION_SUPPORT:
        pMacPib->frameVersionSupport = *(uint8 *)pValue;
        break;

    default:
        break;
    }

    return MAC_SUCCESS;
}
/*********************************************************************
 * @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 )
{
  macRadioSetTxPower(20);
  SampleApp_TaskID = task_id;
  SampleApp_NwkState = DEV_INIT;
  SampleApp_TransID = 0;

   /***********串口初始化****************/
  MT_UartInit(); //串口配置初始化
  MT_UartRegisterTaskID(task_id);//登记串口任务号
  HalUARTWrite(0,"Hello World\r\n",13); // (串口 0,'字符',字符个数)
  // 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_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
  SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
  
  // Setup for the flash command's destination address - Group 1
  SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;
  SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;

  // 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 );

  /*分组信息初始化*/
  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
  
}