Creating a ChargeInhibit Assertion (OS X 10.6.8)

Discussion in 'Mac Programming' started by Mocaisata, Oct 7, 2011.

  1. Mocaisata macrumors newbie

    Oct 7, 2011
    Platform (OS X 10.6.8) -
    [Macbook Pro - this is important as I want to deal with the battery handling - not applicable for a desktop]

    Forgive me if I've made a basic mistake that I don't see, as I haven't written any C/C++ in five years or more, and I'm not fully following the way apple APIs want you to deal with them, so this is what my question regards. Basically, I want to be able to inhibit charging from the AC adaptor at my command, so that I can choose whether to charge my laptop or not while it's in use and plugged in. I hadn't been able to find any utilities that do this, so I had at first written it off as something that was on the hardware level and couldn't be changed from software, but then I ran across something encouraging:

    If you open a Terminal window and run

        pmset -g assertionslog
    you get a list of assertions, and a 0 or 1 for each, indicating if some process has asserted that assertion. The first of them is titled ChargeInhibit, and I discovered after some digging that this does exactly what I want, at the software level. I just have to figure out how to assert it.

    I copied some code from a file in the apple source archive called SetActive.c (link) I copied the function sendSmartBatteryCommand into my XCode project, and spent time linking other headers and copying over definitions until I could get it to compile correctly. Here is the copied function:
        // The copied function, 
        // modified very slightly:
        // to return a success/fail value instead of void
        kern_return_t sendSmartBatteryCommand(uint32_t which, uint32_t level)
            io_service_t    sbmanager = MACH_PORT_NULL;
            io_connect_t    sbconnection = MACH_PORT_NULL;
            kern_return_t   kret = 99;
            uint32_t        output_count = 1;
            uint64_t        uc_return = kIOReturnError;
            uint64_t        level_64 = level;
            // Find SmartBattery manager
            sbmanager = IOServiceGetMatchingService(MACH_PORT_NULL,
            if(MACH_PORT_NULL == sbmanager) {
                goto bail;
            kret = IOServiceOpen( sbmanager, mach_task_self(), 0, &sbconnection);
            if(kIOReturnSuccess != kret) {
                goto bail;
            kret = IOConnectCallMethod(
        		sbconnection, // connection
        		which,      // selector
        		&level_64,  // uint64_t *input
        		1,          // input Count
        		NULL,       // input struct count
        		0,          // input struct count
        		&uc_return, // output
        		&output_count,  // output count
        		NULL,       // output struct
        		0);         // output struct count
            if (MACH_PORT_NULL != sbconnection) {
            if (MACH_PORT_NULL != sbmanager) {
            return kret;

    I get success values back when I try to use it to send assertions for charge inhibiting and inflow disabling, but the log from pmset doesn't show any changes, and neither does the actual state of my battery charging / not-charging.

    I've also tried modifying the name of the service to look for from "AppleSmartBatteryManager" to a nonsense word just to see if the function returns failure, and it does, so that indicates I'm connecting to a real service.

    Any tips on how I can achieve this as simply as possible?

    By the way, I've tried to recompile the AppleSmartBatteryManager in the PowerManagement project from the source package on the apple source site, but I way more errors in XCode than I can deal with. I'm looking to try to interact with the existing service in some way that does not make me recompile the AppleSmartBatteryManager source alongside my own project.

    By the way, this is an example of me calling the function:

       int CInh()
        	kern_return_t kret = sendSmartBatteryCommand( kSBUCChargeInhibit, 255);  // zero:uninhibit, non-zero:inhibit
        	if(kret == KERN_SUCCESS)
        		return 1;
        		return 0;
    Where options for the "which" parameter are defined in an enum in my header file (also copied from SetActive.c):

       enum {
            kSBUCInflowDisable = 0,
            kSBUCChargeInhibit = 1
  2. thehub macrumors newbie

    Jun 30, 2010
    Has anyone figured this out yet?

    It would be great to force my battery to stop charging at a certain percentage and allow me to continue to use the computer while plugged in. I tend to use my laptops on the couch more often than away from home. Keeping a battery at 100% charge is bad for the longevity of the cells.

    The original poster abandoned this thread (after receiving no replies) and another he posted on More Recently, he received a response on stack flow

    Can anyone take a look at that update and see what else it might take to get a utility to inhibit battery charging while plugged in?
  3. gnasher729 macrumors P6


    Nov 25, 2005
    Says who?
  4. thehub macrumors newbie

    Jun 30, 2010
    Apple, other computer manufacturers, hybrid/electric car manufacturers, the US military, chemists all agree that batteries lose capacity if frequently charged to 100%.

    Keeping the cells at 100% is bad for the battery. We need a utility like is bundled with many PC laptops to control the charging of our batteries.

    An example of Sony's settings for battery charging. This article says Sony defaults to stop charging at 80%:


    "If you don’t plan on using your notebook for more than six months, Apple recommends that you store the battery with a 50% charge. If you store a battery when it’s fully discharged, it could fall into a deep discharge state, which renders it incapable of holding any charge. Conversely, if you store it fully charged for an extended period of time, the battery may experience some loss of battery capacity, meaning it will have a shorter life."


    "Chargers for cellular phones, laptops, tablets and digital cameras bring the Li-ion battery to 4.20V/cell. This allows maximum capacity, because the consumer wants nothing less than optimal runtime. Industry, on the other hand, is more concerned about longevity and may choose lower voltage thresholds. Satellites and electric vehicles are examples where longevity is more important than capacity.
    Charging to 4.10V/cell the battery holds about 10 percent less capacity than going all the way to 4.20V. In terms of optimal longevity, a voltage limit of 3.92V/cell works best but the capacity would only be about half compared to a 4.20V/cell charge (3.92V/cell is said to eliminate all voltage-related stresses).

    Commercial chargers do not allow changing the charge voltage limit. Adding this feature would have advantages, especially for laptops as a means to prolong battery life. When running on extended AC mode, the user could select the “long life” mode and the battery would charge to 4.00V/cell for a standby capacity of about 70 percent. Before traveling, the user would apply the “full charge mode” to bring the charge to 100%. Some laptop manufacturers may offer this feature but often only computer geeks discover them."

    US Army study

    "The second is when the cell was maintained at a high state of charge (Figures 3 and 4). The capacity of these cells faded at an increased rate. This is especially true for the cells maintained at a state-of-charge of 50% to 100%."


    Here's another long thread on the macworld forums from 2009 where the nature of battery chemistry is discussed in response to another request for an app to control the charging of Macbook batteries:
  5. gnasher729, Mar 28, 2013
    Last edited: Mar 28, 2013

    gnasher729 macrumors P6


    Nov 25, 2005
    Nonsense. You should discharge the battery in a MacBook once a month, that's it. Keeping it fully charged the rest of the time doesn't hurt the MacBook one bit.

    Apple's website is about _storing_ a laptop. This proposed software doesn't know that you are going to store the laptop for six months. The user does, and they can very easily discharge to 50% themselves.

    The "battery university" study doesn't actually support what you are saying. And the voltage is not controlled by the charger, which is just a dumb charger; it is controlled by sophisticated electronics that is part of the battery, which controls charging voltage and amperage in a rather complicated way.

    The US army study seems to be done around 2006, and refers to commercially available battery cells with primitive chargers. No relevance to a 2013 MacBook.

    Finally, some Mac users in 2009 asked for what you are asking for. That's four years out of date, and just because you find other misguided people, that doesn't help your argument.
  6. thehub macrumors newbie

    Jun 30, 2010
    Perhaps you do not have a Macbook where you can see first hand how quickly battery capacity diminishes when a Macbook is left on the charger for days/weeks at a time? And you somehow manage to disregard and misinterpret Apple's message, the other studies, and Sony's inclusion of a charge limiter.

    Apple specifically says don't store the battery at 100% because you will lose capacity. Perhaps the word "storage" is causing you to be short-sighted about this very valid concern. Keeping a laptop plugged into power maintains the battery at 100% charge. When a laptop rarely leaves the house and stays connected to a charger, you are "storing" the battery at 100%, depleting charge.

    Using a laptop primarily as a desktop computer entails keeping the charger plugged in constantly for long periods. Yes, the charger is "smart" in the sense that it will not overcharge the battery. That's not the problem here. The problem is keeping the battery at 100% charge, whether it's in a box or plugged into a charger constantly, reduces the capacity.

    Users like myself and many others would like to leave our Macbooks plugged in at our desk or beside the couch for days or weeks at a time. Currently, that means the battery is stored at 100% charge, depleting capacity.

    We would like to maintain the battery at a low-state of charge during this extended period of home use so that we aren't continuously diminishing the ultimate battery capacity. I don't need an app to do advanced behavioral analysis to determine how I'm going to use my laptop. I just want to activate the utility and use my laptop on A/C current while the battery stays at ~50% charge. I would obviously disable the utility and charge to 100% before I need to take my laptop mobile.

    Most people don't care to understand the chemistry of lithium ion batteries. The select few who have raised this issue as it regards Macbooks specifically are not misguided but rather more informed.

    Do you happen to have any data that shows that leaving a lithium ion battery on any charger which maintains the charge at 100% will not diminish the capacity of the battery? I'd genuinely like for you to be correct, but I think you are dismissing this issue because 1. you think that smart chargers have remedied the issue, and 2. you fail to realize that leaving a battery inactive in a computer while using A/C power for long periods is equivalent to storage.

    All I want is the same level of charge control that Sony offers:
  7. thehub macrumors newbie

    Jun 30, 2010
    Also from the Apple page on batteries:

    Standard Maintenance
    For proper maintenance of a lithium-based battery, it’s important to keep the electrons in it moving occasionally. Apple does not recommend leaving your portable plugged in all the time. An ideal use would be a commuter who uses her notebook on the train, then plugs it in at the office to charge. This keeps the battery juices flowing. If on the other hand, you use a desktop computer at work, and save a notebook for infrequent travel, Apple recommends charging and discharging its battery at least once per month. Need a reminder? Add an event to your desktop’s iCal. When your battery no longer holds sufficient charge to meet your needs, you may choose to replace it. If your notebook came with a built-in battery, you should have the battery replaced only by an Apple Authorized Service Provider.

    And from Apple's developer resources about the power management functions:
    -g with a 'batt' or 'ps' argument will show the state of all attached power sources. A health estimate
    for the battery is appended to the end of the line, in the form (Fair/Good), indicating the battery is
    in 'Fair' health and we have 'Good' confidence in that estimate.

    Certain Apple battery calibration tools may temporarily disable battery charging, even though your
    Apple portable is plugged into AC power. Such tools will never surprise you with this behavior; you
    will know it's happening. This state will be reflected in an additional line of output to 'batt' or
    'ps' readings, including the pid of the app inducing this behavior.

    'ChargeInhibit': 1432

    -g with a 'pslog' or 'rawlog' argument is normally used for debugging, such as isolating a problem with
    an aging battery.

    Maybe I can just add this ChargeInhibit line to the charger section of the file /Library/Preferences/SystemConfiguration/

Share This Page