view src/cs/drivers/drv_app/pwr/pwr_api.c @ 516:1ed9de6c90bd

src/g23m-gsm/sms/sms_for.c: bogus malloc removed The new error handling code that was not present in TCS211 blob version contains a malloc call that is bogus for 3 reasons: 1) The memory allocation in question is not needed in the first place; 2) libc malloc is used instead of one of the firmware's proper ways; 3) The memory allocation is made inside a function and then never freed, i.e., a memory leak. This bug was caught in gcc-built FreeCalypso fw projects (Citrine and Selenite) because our gcc environment does not allow any use of libc malloc (any reference to malloc produces a link failure), but this code from TCS3.2 is wrong even for Magnetite: if this code path is executed repeatedly over a long time, the many small allocations made by this malloc call without a subsequent free will eventually exhaust the malloc heap provided by the TMS470 environment, malloc will start returning NULL, and the bogus code will treat it as an error. Because the memory allocation in question is not needed at all, the fix entails simply removing it.
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 22 Jul 2018 06:04:49 +0000
parents c93a236e0d50
children
line wrap: on
line source

/*******************************************************************************
 *
 * pwr_api.c
 *
 * Purpose: This file contains functions that can be accessed by any SWE
 *          to use power management services.
 *
 * Author    Candice Bazanegue (c-brille@ti.com)
 *
 *
 * (C) Texas Instruments 2001
 *
 ******************************************************************************/

#include "rv/rv_defined_swe.h"	   // for RVM_PWR_SWE

#ifdef RVM_PWR_SWE

#include "rvm/rvm_use_id_list.h"
#include "spi/spi_api.h"
#include "spi/spi_env.h"
#include "pwr/pwr_env.h"
#include "pwr/pwr_cust.h"
#include "pwr/pwr_messages.h"
#include "pwr/pwr_messages_i.h"

/* Define a pointer to the PWR Environment control block.   */
extern T_PWR_ENV_CTRL_BLK *pwr_env_ctrl_blk;


/*******************************************************************************
**
** Function         power_info_register
**
** Description      Function called by the MMI to enable (or disable) the receipt
**                  of several events related to the charging process or the 
**                  discharge levels.
**
**
*******************************************************************************/
T_POWER_RET power_info_register(BOOLEAN info_enable, T_RV_RETURN return_path)
{
   if (((pwr_env_ctrl_blk->power_info.return_path.callback_func)!= NULL) || 
      (pwr_env_ctrl_blk->power_info.return_path.addr_id != RVF_INVALID_ADDR_ID))
   {
      rvf_send_trace("power_info_register : info already registered, performs another registration",
                     76,
                     NULL_PARAM,
                     RV_TRACE_LEVEL_DEBUG_HIGH,
                     PWR_USE_ID);
   }
   pwr_env_ctrl_blk->power_info.info_enable                 = info_enable;
   pwr_env_ctrl_blk->power_info.return_path.addr_id         = return_path.addr_id;
   pwr_env_ctrl_blk->power_info.return_path.callback_func   = return_path.callback_func;

   return (POWER_OK);

}



/*******************************************************************************
**
** Function         power_alert_register
**
** Description      Function called by the MMI to configure the threshold of 
**                  remaining energy in the battery, under which the MMI will
**                  receive low battery alerts.
**
**
*******************************************************************************/
T_POWER_RET power_alert_register(T_PWR_PERCENT remain_capa_threshold,
                                 T_RV_RETURN return_path)
{

   if (((pwr_env_ctrl_blk->power_alert.return_path.callback_func)!= NULL) || 
      (pwr_env_ctrl_blk->power_alert.return_path.addr_id != RVF_INVALID_ADDR_ID))
   {
      rvf_send_trace("power_alert_register : info already registered, performs another registration",
                     77,
                     NULL_PARAM,
                     RV_TRACE_LEVEL_DEBUG_HIGH,
                     PWR_USE_ID);
   }
   pwr_env_ctrl_blk->power_alert.remain_capa_threshold      = remain_capa_threshold;
   pwr_env_ctrl_blk->power_alert.return_path.addr_id        = return_path.addr_id;
   pwr_env_ctrl_blk->power_alert.return_path.callback_func  = return_path.callback_func;

   return (POWER_OK);

}


/*******************************************************************************
**
** Function         power_emergency_register
**
** Description      Function called by the MMI to define the call-back function 
**                  which will be called in case of emergency switch OFF.
**
**
*******************************************************************************/
T_POWER_RET power_emergency_register(T_RV_RETURN return_path)
{

   if (((pwr_env_ctrl_blk->power_emergency.callback_func)!= NULL) || 
      (pwr_env_ctrl_blk->power_emergency.addr_id != RVF_INVALID_ADDR_ID))
   {
      rvf_send_trace("power_emergency_register : info already registered, performs another registration",
                     80,
                     NULL_PARAM,
                     RV_TRACE_LEVEL_DEBUG_HIGH,
                     PWR_USE_ID);
   }
   pwr_env_ctrl_blk->power_emergency.addr_id       = return_path.addr_id;
   pwr_env_ctrl_blk->power_emergency.callback_func = return_path.callback_func;

   return (POWER_OK);

}



/*******************************************************************************
**
** Function         pwr_callback_func
**
** Description      
**
**
**
*******************************************************************************/
void pwr_callback_func(void *ptr)
{
   T_PWR_CHARGING_STATE charge_started;


   /* Charge started? */
   charge_started = pwr_env_ctrl_blk->charging_state;

   if (charge_started == CHARGE_STOPPED)
   {
      /* Ask for battery temperature measurement */
      pwr_get_battery_temperature();
   }

   else if ((charge_started == CI_CHARGE_STARTED) ||
            (charge_started == CV_CHARGE_STARTED))
   {
      /* The battery temperature has been measured during the charge */
      pwr_get_bat_info();
   }

}




/*******************************************************************************
**
** Function         pwr_get_bat_info
**
** Description      
**
**
*******************************************************************************/
void pwr_get_bat_info(void)
{
   UINT16 bat_voltage, bat_madc_voltage;
   UINT16 charger_current, bat_madc_current;
   UINT16 status;
   BOOLEAN charger_plugged;
   T_PWR_CHARGING_STATE charge_started;
   T_PWR_PERCENT remain_capacity;
   INT16 bat_temperature;


   /* Voltage and current */
   /* Read the conversion results in the analog device registers */
   bat_madc_voltage = ABB_Read_Register_on_page(PAGE0, VBATREG);
   bat_madc_current = ABB_Read_Register_on_page(PAGE0, ICHGREG);

   bat_voltage = pwr_adc_to_mvolt(bat_madc_voltage);
   charger_current = pwr_adc_to_mA(bat_madc_current);

   /* Remaining capacity in the battery */
   remain_capacity = pwr_get_capacity_vs_voltage(bat_voltage);

   /* Charger plugged? */
   status = ABB_Read_Status();
   if (status & CHGPRES)
   {
      charger_plugged = TRUE;
   }
   else
   {
      charger_plugged = FALSE;
   }

   /* Charge started? */
   charge_started = pwr_env_ctrl_blk->charging_state;

   /* The battery temperature is already measured during the charge */
   /* Use the last result */
   bat_temperature = pwr_env_ctrl_blk->bat_celsius_temp;

   /* Send the results to the upper layer */
   pwr_send_battery_info(bat_voltage, bat_temperature, charger_current,
                         charger_plugged, charge_started,
                         remain_capacity);
}




/*******************************************************************************
**
** Function         power_battery_information
**
** Description      Function called by the MMI to get information concerning
**                  the battery.
**
**
*******************************************************************************/
T_POWER_RET power_battery_information(void)
{
   T_RV_RET spi_return;

   spi_return = pwr_spi_battery_info(pwr_callback_func);
   if (spi_return != RV_OK)
   {
      return (POWER_ERROR);
   }

   return (POWER_OK);
}

#endif /* #ifdef RVM_PWR_SWE */