热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

深入解析android5.1healthd

这篇文章主要为大家详细介绍了android5.1healthd的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

healthd主要是读取电池节点的信息,传给BatteryService。或者在关机充电等使用。注意healthd中使用的是kernel的log。

下面先从main函数分析

int main(int argc, char **argv) { 
 int ch; 
 int ret; 
 
 klog_set_level(KLOG_LEVEL); 
 healthd_mode_ops = &android_ops; 
 
 if (!strcmp(basename(argv[0]), "charger")) {//解析输入参数如果是charger的使用charger_ops,这里就不做介绍 
  healthd_mode_ops = &charger_ops; 
 } else { 
  while ((ch = getopt(argc, argv, "cr")) != -1) {//分析输入命令,各个命令对应不同的charger_ops 
   switch (ch) { 
   case 'c': 
    healthd_mode_ops = &charger_ops; 
    break; 
   case 'r': 
    healthd_mode_ops = &recovery_ops; 
    break; 
   case '?': 
   default: 
    KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", 
       optopt); 
    exit(1); 
   } 
  } 
 } 
 
 ret = healthd_init();//healthd做初始化 
 if (ret) { 
  KLOG_ERROR("Initialization failed, exiting\n"); 
  exit(2); 
 } 
 
 healthd_mainloop();//主函数 
 KLOG_ERROR("Main loop terminated, exiting\n"); 
 return 3; 
} 

如果是正常开机,不走关机充电等,healthd_mode_ops = &android_ops;而这里面具体的函数在后面进行详细的介绍。

static struct healthd_mode_ops android_ops = { 
 .init = healthd_mode_android_init, 
 .preparetowait = healthd_mode_android_preparetowait, 
 .heartbeat = healthd_mode_nop_heartbeat, 
 .battery_update = healthd_mode_android_battery_update, 
}; 

下面分析下healthd_init函数,heathd使用了epoll进行IO复用。

static int healthd_init() { 
 epollfd = epoll_create(MAX_EPOLL_EVENTS); 
 if (epollfd == -1) { 
  KLOG_ERROR(LOG_TAG, 
     "epoll_create failed; errno=%d\n", 
     errno); 
  return -1; 
 } 
 
 healthd_board_init(&healthd_config); 
 healthd_mode_ops->init(&healthd_config); 
 wakealarm_init(); 
 uevent_init(); 
 gBatteryMOnitor= new BatteryMonitor(); 
 gBatteryMonitor->init(&healthd_config); 
 return 0; 
} 

这里的healthd_mode_ops->init的函数是android_ops 的healthd_mode_android_init函数,这里主要是将binder通信的fd也加入epoll,而不像普通binder进程最后使用IPCThreadState::self()->joinThreadPool。这样所有的fd全在epoll管理,只用了一个线程

int healthd_mode_android_preparetowait(void) { 
 IPCThreadState::self()->flushCommands(); 
 return -1; 
} 
 
static void binder_event(uint32_t /*epevents*/) { 
 IPCThreadState::self()->handlePolledCommands(); 
} 
 
void healthd_mode_android_init(struct healthd_config* /*config*/) { 
 ProcessState::self()->setThreadPoolMaxThreadCount(0); 
 IPCThreadState::self()->disableBackgroundScheduling(true); 
 IPCThreadState::self()->setupPolling(&gBinderFd); 
 
 if (gBinderFd >= 0) { 
  if (healthd_register_event(gBinderFd, binder_event)) 
   KLOG_ERROR(LOG_TAG, 
      "Register for binder events failed\n"); 
 } 
 
 gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar(); 
 gBatteryPropertiesRegistrar->publish(); 
} 

gBatteryPropertiesRegistrar->publish将"batteryproperties"这个Service加入到ServiceManager中

void BatteryPropertiesRegistrar::publish() { 
 defaultServiceManager()->addService(String16("batteryproperties"), this); 
} 

接下来再来看下wakealarm_init

static void wakealarm_init(void) { 
 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK); 
 if (wakealarm_fd == -1) { 
  KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n"); 
  return; 
 } 
 
 if (healthd_register_event(wakealarm_fd, wakealarm_event)) 
  KLOG_ERROR(LOG_TAG, 
     "Registration of wakealarm event failed\n"); 
 
 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast); 
} 

wakealarm_init设置alarm唤醒的interval,再来看下时间处理函数

static void wakealarm_event(uint32_t /*epevents*/) { 
 unsigned long long wakeups; 
 
 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {//出错结束 
  KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n"); 
  return; 
 } 
 KLOG_ERROR(LOG_TAG, "wakealarm_event\n"); 
 periodic_chores(); 
} 
static void periodic_chores() { 
 healthd_battery_update(); 
} 
void healthd_battery_update(void) { 
 // Fast wake interval when on charger (watch for overheat); 
 // slow wake interval when on battery (watch for drained battery). 
 KLOG_ERROR(LOG_TAG, "healthd_battery_update enter\n"); 
 int new_wake_interval = gBatteryMonitor->update() ?//调用主要的update函数,根据返回值,如果当前在充电返回true 
  healthd_config.periodic_chores_interval_fast ://时间设置1分钟 
   healthd_config.periodic_chores_interval_slow; 
 KLOG_ERROR(LOG_TAG, "healthd_battery_update after\n"); 
 if (new_wake_interval != wakealarm_wake_interval) 
   wakealarm_set_interval(new_wake_interval); 
 
 // During awake periods poll at fast rate. If wake alarm is set at fast 
 // rate then just use the alarm; if wake alarm is set at slow rate then 
 // poll at fast rate while awake and let alarm wake up at slow rate when 
 // asleep. 
 
 if (healthd_config.periodic_chores_interval_fast == -1) 
  awake_poll_interval = -1; 
 else 
  awake_poll_interval = 
   new_wake_interval == healthd_config.periodic_chores_interval_fast ?//当前时间是一分钟,epoll为永远阻塞,否则为1分钟 
    -1 : healthd_config.periodic_chores_interval_fast * 1000; 
} 

接下来再来看看uEvent的,

static void uevent_init(void) { 
 uevent_fd = uevent_open_socket(64*1024, true); 
 
 if (uevent_fd <0) { 
  KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n"); 
  return; 
 } 
  
 fcntl(uevent_fd, F_SETFL, O_NONBLOCK); 
 if (healthd_register_event(uevent_fd, uevent_event)) 
  KLOG_ERROR(LOG_TAG, 
     "register for uevent events failed\n"); 
} 

看看uevent_event的处理函数,获取uevent后主要判断是否是电源系统的,如果是调用healthd_battery_update函数

static void uevent_event(uint32_t /*epevents*/) { 
 char msg[UEVENT_MSG_LEN+2]; 
 char *cp; 
 int n; 
 
 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN); 
 if (n <= 0) 
  return; 
 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */ 
  return; 
 
 msg[n] = '\0'; 
 msg[n+1] = '\0'; 
 cp = msg; 
 KLOG_ERROR(LOG_TAG, "uevent_event\n"); 
 while (*cp) { 
  if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {//是这个子系统的调用healthd_battery_update函数 
   healthd_battery_update(); 
   break; 
  } 
 
  /* advance to after the next \0 */ 
  while (*cp++) 
   ; 
 } 
} 

下面分析下healthd_mainloop这个主函数,主函数主要是epoll函数监听3个fd,有事件就处理。

static void healthd_mainloop(void) { 
 while (1) { 
  struct epoll_event events[eventct]; 
  int nevents; 
  int timeout = awake_poll_interval; 
  int mode_timeout; 
 
  mode_timeout = healthd_mode_ops->preparetowait(); 
  if (timeout <0 || (mode_timeout > 0 && mode_timeout heartbeat(); 
 } 
 
 return; 
} 

init函数主要将healthd_config 对象传入,并且将里面的成员的一些地址信息去初始化保存起来。主要是保存一些地址信息,以及充电方式。

void BatteryMonitor::init(struct healthd_config *hc) { 
 String8 path; 
 char pval[PROPERTY_VALUE_MAX]; 
 
 mHealthdCOnfig= hc;//将外面传进来的heathdconfig的指针赋给成员变量 
 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);//打开地址 /sys/class/power_supply 
 if (dir == NULL) { 
  KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); 
 } else { 
  struct dirent* entry; 
 
  while ((entry = readdir(dir))) { 
   const char* name = entry->d_name; 
 
   if (!strcmp(name, ".") || !strcmp(name, "..")) 
    continue; 
 
   char buf[20]; 
   // Look for "type" file in each subdirectory 
   path.clear(); 
   path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); 
   switch(readPowerSupplyType(path)) {//读取各个目录下type的值,比如/sys/class/power_supply/battery 下type的值为Battery,在readPowerSupplyType读取并且转化为ANDROID_POWER_SUPPLY_TYPE_BATTERY 
   case ANDROID_POWER_SUPPLY_TYPE_AC: 
    if (mHealthdConfig->acChargeHeathPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->acChargeHeathPath = path;//配置路径 
    } 
    path.clear(); 
    path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); 
    if (access(path.string(), R_OK) == 0) 
     mChargerNames.add(String8(name));//chargername 就是当前目录名字:ac 
    break; 
 
   case ANDROID_POWER_SUPPLY_TYPE_USB://usb 类似ac 
    if (mHealthdConfig->usbChargeHeathPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->usbChargeHeathPath = path; 
    } 
    path.clear(); 
    path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); 
    if (access(path.string(), R_OK) == 0) 
     mChargerNames.add(String8(name)); 
    break; 
 
   case ANDROID_POWER_SUPPLY_TYPE_WIRELESS://类似 
    path.clear(); 
    path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); 
    if (access(path.string(), R_OK) == 0) 
     mChargerNames.add(String8(name)); 
    break; 
 
   case ANDROID_POWER_SUPPLY_TYPE_BATTERY://battery 
    mBatteryDevicePresent = true; 
 
    if (mHealthdConfig->batteryStatusPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryStatusPath = path; 
    } 
 
    if (mHealthdConfig->batteryHealthPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryHealthPath = path; 
    } 
 
    if (mHealthdConfig->batteryPresentPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryPresentPath = path; 
    } 
 
    if (mHealthdConfig->batteryCapacityPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryCapacityPath = path; 
    } 
 
    if (mHealthdConfig->batteryVoltagePath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/voltage_now", 
          POWER_SUPPLY_SYSFS_PATH, name); 
     if (access(path, R_OK) == 0) { 
      mHealthdConfig->batteryVoltagePath = path; 
     } else { 
      path.clear(); 
      path.appendFormat("%s/%s/batt_vol", 
           POWER_SUPPLY_SYSFS_PATH, name); 
      if (access(path, R_OK) == 0) 
       mHealthdConfig->batteryVoltagePath = path; 
     } 
    } 
 
    if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/current_now", 
          POWER_SUPPLY_SYSFS_PATH, name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryCurrentNowPath = path; 
    } 
 
    if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/current_avg", 
          POWER_SUPPLY_SYSFS_PATH, name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryCurrentAvgPath = path; 
    } 
 
    if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/charge_counter", 
          POWER_SUPPLY_SYSFS_PATH, name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryChargeCounterPath = path; 
    } 
 
    if (mHealthdConfig->batteryTemperaturePath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, 
          name); 
     if (access(path, R_OK) == 0) { 
      mHealthdConfig->batteryTemperaturePath = path; 
     } else { 
      path.clear(); 
      path.appendFormat("%s/%s/batt_temp", 
           POWER_SUPPLY_SYSFS_PATH, name); 
      if (access(path, R_OK) == 0) 
       mHealthdConfig->batteryTemperaturePath = path; 
     } 
    } 
 
    if (mHealthdConfig->batteryTechnologyPath.isEmpty()) { 
     path.clear(); 
     path.appendFormat("%s/%s/technology", 
          POWER_SUPPLY_SYSFS_PATH, name); 
     if (access(path, R_OK) == 0) 
      mHealthdConfig->batteryTechnologyPath = path; 
    } 
 
    break; 
 
   case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: 
    break; 
   } 
  } 
  closedir(dir); 
 } 
 
 if (!mChargerNames.size()) 
  KLOG_ERROR(LOG_TAG, "No charger supplies found\n"); 
 if (!mBatteryDevicePresent) {//主要由battery该成员变量就为true 
  KLOG_WARNING(LOG_TAG, "No battery devices found\n"); 
  hc->periodic_chores_interval_fast = -1; 
  hc->periodic_chores_interval_slow = -1; 
 } else { 
  if (mHealthdConfig->batteryStatusPath.isEmpty()) 
   KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n"); 
。。。。。。。。。。。。。//这里都是一些警告 
 } 
 
 if (property_get("ro.boot.fake_battery", pval, NULL) > 0 
            && strtol(pval, NULL, 10) != 0) { 
  mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY; 
  mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; 
 } 
} 

下面就是update函数,将数据封装在BatteryProperties 中,并且通过healthd_mode_ops->battery_update把BatteryProperties 发给上层。

bool BatteryMonitor::update(void) { 
 bool logthis; 
 
 props.chargerAcOnline= false; 
 props.chargerUsbOnline= false; 
 props.chargerWirelessOnline= false; 
 props.batteryStatus = BATTERY_STATUS_UNKNOWN; 
 props.batteryHealth = BATTERY_HEALTH_UNKNOWN; 
//都是从之前配置的mHealthd中取地址,读取节点信息,保存到props成员变量中 
 if (!mHealthdConfig->batteryPresentPath.isEmpty()) 
  props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath); 
 else 
  props.batteryPresent = mBatteryDevicePresent; 
 
 props.batteryLevel = mBatteryFixedCapacity &#63; 
  mBatteryFixedCapacity : 
  getIntField(mHealthdConfig->batteryCapacityPath); 
 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000; 
 
 props.batteryTemperature = mBatteryFixedTemperature &#63; 
  mBatteryFixedTemperature : 
  getIntField(mHealthdConfig->batteryTemperaturePath); 
 
 const int SIZE = 128; 
 char buf[SIZE]; 
 String8 btech; 
 
 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) 
  props.batteryStatus = getBatteryStatus(buf); 
 
 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) 
  props.batteryHealth = getBatteryHealth(buf); 
 
 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) 
  props.batteryTechnology = String8(buf); 
 
 if (readFromFile(mHealthdConfig->acChargeHeathPath, buf, SIZE) > 0) 
  props.acChargeHeath= String8(buf); 
 
 if (readFromFile(mHealthdConfig->usbChargeHeathPath, buf, SIZE) > 0) 
  props.usbChargeHeath= String8(buf); 
 
 unsigned int i; 
 
 for (i = 0; i  0) { 
   if (buf[0] != '0') {//读取online里面的内容,如果当前在usb线上充电,那么usb下online里面的内容为1 
    path.clear(); 
    path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, 
         mChargerNames[i].string());//这里看看是哪个type的 
    switch(readPowerSupplyType(path)) { 
    case ANDROID_POWER_SUPPLY_TYPE_AC: 
     props.chargerAcOnline= true; 
     break; 
    case ANDROID_POWER_SUPPLY_TYPE_USB://将其值赋成true 
     props.chargerUsbOnline= true; 
     break; 
    case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: 
     props.chargerWirelessOnline= true; 
     break; 
    default: 
     KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", 
         mChargerNames[i].string()); 
    } 
   } 
  } 
 } 
 
 logthis = !healthd_board_battery_update(&props); 
 
 if (logthis) { 
  char dmesgline[256]; 
 
  if (props.batteryPresent) { 
   snprintf(dmesgline, sizeof(dmesgline), 
     "battery l=%d v=%d t=%s%d.%d h=%d st=%d", 
     props.batteryLevel, props.batteryVoltage, 
     props.batteryTemperature <0 &#63; "-" : "", 
     abs(props.batteryTemperature / 10), 
     abs(props.batteryTemperature % 10), props.batteryHealth, 
     props.batteryStatus); 
 
   if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { 
    int c = getIntField(mHealthdConfig->batteryCurrentNowPath); 
    char b[20]; 
 
    snprintf(b, sizeof(b), " c=%d", c / 1000); 
    strlcat(dmesgline, b, sizeof(dmesgline)); 
   } 
  } else { 
   snprintf(dmesgline, sizeof(dmesgline), 
     "battery none"); 
  } 
 
  KLOG_WARNING(LOG_TAG, "%s chg=%s%s%s\n", dmesgline, 
      props.chargerAcOnline &#63; "a" : "", 
      props.chargerUsbOnline &#63; "u" : "", 
      props.chargerWirelessOnline &#63; "w" : ""); 
 } 
 
 healthd_mode_ops->battery_update(&props);//将数据传到上层的BatteryService 
 return props.chargerAcOnline | props.chargerUsbOnline |//返回当前是否属于充电 
   props.chargerWirelessOnline; 
} 

接下来看看healthd_mode_ops->battery_update是怎么把数据传到上层的

void healthd_mode_android_battery_update( 
 struct android::BatteryProperties *props) { 
 if (gBatteryPropertiesRegistrar != NULL) 
  gBatteryPropertiesRegistrar->notifyListeners(*props); 
 
 return; 
} 

上层会通过binder通信,注册一个回调到BatteryPropertiesRegistrar

void BatteryPropertiesRegistrar::registerListener(const sp& listener) { 
 { 
  if (listener == NULL) 
   return; 
  Mutex::Autolock _l(mRegistrationLock); 
  // check whether this is a duplicate 
  for (size_t i = 0; i asBinder() == listener->asBinder()) { 
    return; 
   } 
  } 
 
  mListeners.add(listener); 
  listener->asBinder()->linkToDeath(this); 
 } 
 healthd_battery_update(); 
} 

而update函数就是调用了notifyListeners遍历各个listener传到上层BatteryService

void BatteryPropertiesRegistrar::notifyListeners(struct BatteryProperties props) { 
 Mutex::Autolock _l(mRegistrationLock); 
 for (size_t i = 0; i batteryPropertiesChanged(props); 
 } 
} 

再来看看BatteryService中,在onStart中通过ServiceManager,和batteryproperties这个Service通信,将BatteryListener这个listenter注册到batteryproperties中去

@Override 
public void onStart() { 
 IBinder b = ServiceManager.getService("batteryproperties"); 
 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar = 
   IBatteryPropertiesRegistrar.Stub.asInterface(b); 
 try { 
  batteryPropertiesRegistrar.registerListener(new BatteryListener()); 
 } catch (RemoteException e) { 
  // Should never happen. 
 } 
 
 publishBinderService("battery", new BinderService()); 
 publishLocalService(BatteryManagerInternal.class, new LocalService()); 
} 

再来看看BatteryListener 的batteryPropertiesChanged接口,当下面调这个接口,就会调用BatteryService的update函数,然后就是BatteryService的一些主要流程就不分析了。

private final class BatteryListener extends IBatteryPropertiesListener.Stub { 
 @Override 
 public void batteryPropertiesChanged(BatteryProperties props) { 
  final long identity = Binder.clearCallingIdentity(); 
  try { 
   BatteryService.this.update(props); 
  } finally { 
   Binder.restoreCallingIdentity(identity); 
  } 
 } 
} 

BatteryService接受healthd的数据都是被动的,healthd穿过来的。有没有主动去healthd查询的。

在BatteryManager中就有主动去healthd查询的,代码如下

private long queryProperty(int id) { 
 long ret; 
 
 if (mBatteryPropertiesRegistrar == null) { 
  IBinder b = ServiceManager.getService("batteryproperties");//获取batteryproperties Service 
  mBatteryPropertiesRegistrar = 
   IBatteryPropertiesRegistrar.Stub.asInterface(b);//接口转化下 
 
  if (mBatteryPropertiesRegistrar == null) 
   return Long.MIN_VALUE; 
 } 
 
 try { 
  BatteryProperty prop = new BatteryProperty(); 
 
  if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)//prop是输出 
   ret = prop.getLong(); 
  else 
   ret = Long.MIN_VALUE; 
 } catch (RemoteException e) { 
  ret = Long.MIN_VALUE; 
 } 
 
 return ret; 
} 

再到healthd看看对应的接口

status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) { 
 return healthd_get_property(id, val); 
} 

status_t healthd_get_property(int id, struct BatteryProperty *val) { 
 return gBatteryMonitor->getProperty(id, val); 
} 

java的BatteryProperty对象对应到这边是指针

status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) { 
 status_t ret = BAD_VALUE; 
 
 val->valueInt64 = LONG_MIN; 
 
 switch(id) { 
 case BATTERY_PROP_CHARGE_COUNTER://根据不同ID,返回不同值 
  if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { 
   val->valueInt64 = 
    getIntField(mHealthdConfig->batteryChargeCounterPath); 
   ret = NO_ERROR; 
  } else { 
   ret = NAME_NOT_FOUND; 
  } 
  break; 
 
 case BATTERY_PROP_CURRENT_NOW: 
  if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { 
   val->valueInt64 = 
    getIntField(mHealthdConfig->batteryCurrentNowPath); 
   ret = NO_ERROR; 
  } else { 
   ret = NAME_NOT_FOUND; 
  } 
  break; 
 
 case BATTERY_PROP_CURRENT_AVG: 
  if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { 
   val->valueInt64 = 
    getIntField(mHealthdConfig->batteryCurrentAvgPath); 
   ret = NO_ERROR; 
  } else { 
   ret = NAME_NOT_FOUND; 
  } 
  break; 
 
 case BATTERY_PROP_CAPACITY: 
  if (!mHealthdConfig->batteryCapacityPath.isEmpty()) { 
   val->valueInt64 = 
    getIntField(mHealthdConfig->batteryCapacityPath); 
   ret = NO_ERROR; 
  } else { 
   ret = NAME_NOT_FOUND; 
  } 
  break; 
 
 case BATTERY_PROP_ENERGY_COUNTER: 
  if (mHealthdConfig->energyCounter) { 
   ret = mHealthdConfig->energyCounter(&val->valueInt64); 
  } else { 
   ret = NAME_NOT_FOUND; 
  } 
  break; 
 
 default: 
  break; 
 } 
 
 return ret; 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 国内BI工具迎战国际巨头Tableau,稳步崛起
    尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • MySQL中枚举类型的所有可能值获取方法
    本文介绍了一种在MySQL数据库中查询枚举(ENUM)类型字段所有可能取值的方法,帮助开发者更好地理解和利用这一数据类型。 ... [详细]
author-avatar
小树苗
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有