<commit_before>/**
   @file thermalmanager.c

   This file implements part of the device thermal management policy
   by providing the current thermal state for interested sw components.
   <p>
   Copyright (C) 2009-2010 Nokia Corporation

   @author <NAME> <<EMAIL>>

   This file is part of Dsme.

   Dsme is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License
   version 2.1 as published by the Free Software Foundation.

   Dsme is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Dsme.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * An example command line to obtain thermal state over D-Bus:
 * $ dbus-send --system --print-reply --dest=com.nokia.thermalmanager /com/nokia/thermalmanager com.nokia.thermalmanager.get_thermal_state
 *
 * TODO:
 * - use a single timer for all thermal objects
 *   i.e. use the shortest interval of all thermal objects
 */

#define _GNU_SOURCE

#include "thermalmanager.h"

#include <iphbd/iphb_internal.h>

#include "dbusproxy.h"
#include "dsme_dbus.h"

#include "dsme/modules.h"
#include "dsme/modulebase.h"
#include "dsme/logging.h"
#include "heartbeat.h"

#include <dsme/state.h>
#include <dsme/thermalmanager_dbus_if.h>

#include <glib.h>
#include <stdlib.h>


static void receive_temperature_response(thermal_object_t* thermal_object,
                                         int               temperature);
static void thermal_object_polling_interval_expired(void* object);

#ifdef DSME_THERMAL_TUNING
static void thermal_object_try_to_read_config(thermal_object_t* thermal_object);
static thermal_object_t* thermal_object_copy(
  const thermal_object_t* thermal_object);
#endif

#ifdef DSME_THERMAL_LOGGING
static void log_temperature(int temperature, const thermal_object_t* thermal_object);
#endif


static module_t* this_module = 0;

static GSList* thermal_objects = 0;

static const char* const service   = thermalmanager_service;
static const char* const interface = thermalmanager_interface;
static const char* const path      = thermalmanager_path;

static THERMAL_STATUS current_status = THERMAL_STATUS_NORMAL;

#ifdef DSME_THERMAL_TUNING
static bool is_in_ta_test = false;
#endif

static const char* const thermal_status_name[] = {
  "low-temp-warning", "normal", "warning", "alert", "fatal"
};


static const char* current_status_name()
{
  return thermal_status_name[current_status];
}

static THERMAL_STATUS worst_current_thermal_object_status(void)
{
  THERMAL_STATUS overall_status = THERMAL_STATUS_NORMAL;
  THERMAL_STATUS highest_status = THERMAL_STATUS_NORMAL;
  THERMAL_STATUS lowest_status = THERMAL_STATUS_NORMAL;
  GSList*        node;

  for (node = thermal_objects; node != 0; node = g_slist_next(node)) {
      /* Find highest status */
      if (((thermal_object_t*)(node->data))->status > highest_status) {
          highest_status = ((thermal_object_t*)(node->data))->status;
      }
      /* Find lowest status */
      if (((thermal_object_t*)(node->data))->status < lowest_status) {
          lowest_status = ((thermal_object_t*)(node->data))->status;
      }
  }
  /* Decide overall status */
  /* If we have any ALERT of FATAL then that decides overall status */
  /* During LOW, NORMAL or WARNING, any LOW wins */
  /* Else status is highest */
  if (highest_status >= THERMAL_STATUS_ALERT) {
      overall_status = highest_status;
  } else if (lowest_status == THERMAL_STATUS_LOW) {
      overall_status = THERMAL_STATUS_LOW;
  } else {
      overall_status = highest_status;
  }
  return overall_status;
}

static void send_thermal_status(t_dsme_thermal_status status)
{
  DSM_MSGTYPE_SET_THERMAL_STATUS msg =
    DSME_MSG_INIT(DSM_MSGTYPE_SET_THERMAL_STATUS);

  msg.status = status;

  broadcast_internally(&msg);
}

static void send_thermal_indication(void)
{
  /* first send an indication to D-Bus */
  {
      DsmeDbusMessage* sig =
          dsme_dbus_signal_new(path,<commit_msg>Fix typo<commit_after>/**
   @file thermalmanager.c

   This file implements part of the device thermal management policy
   by providing the current thermal state for interested sw components.
   <p>
   Copyright (C) 2009-2010 Nokia Corporation

   @author <NAME> <<EMAIL>>

   This file is part of Dsme.

   Dsme is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License
   version 2.1 as published by the Free Software Foundation.

   Dsme is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Dsme.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * An example command line to obtain thermal state over D-Bus:
 * $ dbus-send --system --print-reply --dest=com.nokia.thermalmanager /com/nokia/thermalmanager com.nokia.thermalmanager.get_thermal_state
 *
 * TODO:
 * - use a single timer for all thermal objects
 *   i.e. use the shortest interval of all thermal objects
 */

#define _GNU_SOURCE

#include "thermalmanager.h"

#include <iphbd/iphb_internal.h>

#include "dbusproxy.h"
#include "dsme_dbus.h"

#include "dsme/modules.h"
#include "dsme/modulebase.h"
#include "dsme/logging.h"
#include "heartbeat.h"

#include <dsme/state.h>
#include <dsme/thermalmanager_dbus_if.h>

#include <glib.h>
#include <stdlib.h>


static void receive_temperature_response(thermal_object_t* thermal_object,
                                         int               temperature);
static void thermal_object_polling_interval_expired(void* object);

#ifdef DSME_THERMAL_TUNING
static void thermal_object_try_to_read_config(thermal_object_t* thermal_object);
static thermal_object_t* thermal_object_copy(
  const thermal_object_t* thermal_object);
#endif

#ifdef DSME_THERMAL_LOGGING
static void log_temperature(int temperature, const thermal_object_t* thermal_object);
#endif


static module_t* this_module = 0;

static GSList* thermal_objects = 0;

static const char* const service   = thermalmanager_service;
static const char* const interface = thermalmanager_interface;
static const char* const path      = thermalmanager_path;

static THERMAL_STATUS current_status = THERMAL_STATUS_NORMAL;

#ifdef DSME_THERMAL_TUNING
static bool is_in_ta_test = false;
#endif

static const char* const thermal_status_name[] = {
  "low-temp-warning", "normal", "warning", "alert", "fatal"
};


static const char* current_status_name()
{
  return thermal_status_name[current_status];
}

static THERMAL_STATUS worst_current_thermal_object_status(void)
{
  THERMAL_STATUS overall_status = THERMAL_STATUS_NORMAL;
  THERMAL_STATUS highest_status = THERMAL_STATUS_NORMAL;
  THERMAL_STATUS lowest_status = THERMAL_STATUS_NORMAL;
  GSList*        node;

  for (node = thermal_objects; node != 0; node = g_slist_next(node)) {
      /* Find highest status */
      if (((thermal_object_t*)(node->data))->status > highest_status) {
          highest_status = ((thermal_object_t*)(node->data))->status;
      }
      /* Find lowest status */
      if (((thermal_object_t*)(node->data))->status < lowest_status) {
          lowest_status = ((thermal_object_t*)(node->data))->status;
      }
  }
  /* Decide overall status */
  /* If we have any ALERT of FATAL then that decides overall status */
  /* During LOW, NORMAL or WARNING, any LOW wins */
  /* Else status is highest */
  if (highest_status >= THERMAL_STATUS_ALERT) {
      overall_status = highest_status;
  } else if (lowest_status == THERMAL_STATUS_LOW) {
      overall_status = THERMAL_STATUS_LOW;
  } else {
      overall_status = highest_status;
  }
  return overall_status;
}

static void send_thermal_status(dsme_thermal_status_t status)
{
  DSM_MSGTYPE_SET_THERMAL_STATUS msg =
    DSME_MSG_INIT(DSM_MSGTYPE_SET_THERMAL_STATUS);

  msg.status = status;

  broadcast_internally(&msg);
}

static void send_thermal_indication(void)
{
  /* first send an indication to D-Bus */
  {
      DsmeDbusMessage* sig =
          dsme_dbus_signal_new(path,
{'fraction_non_alphanumeric': 0.08761723851359374, 'fraction_numerical': 0.0030867861806957143, 'mean_word_length': 4.056422569027611, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4299115', 'n_tokens_mistral': 2735, 'n_tokens_neox': 2636, 'n_words': 748}