0

I am new to all this, but here goes:

There is an apple file that I would like to use parts of for my app. It is called BatteryTimeRemaining.c.

http://www.opensource.apple.com/source/PowerManagement/PowerManagement-211/pmconfigd/BatteryTimeRemaining.c

I want to be able to use some of the calculations in it, the problem being I don't to know how to add and use it properly. I get countless errors e.g. undeclared: first use of this function.

Do I need to add all the header files which are included in the .c file? Do I compile it and then add?

Many thanks, Stuart

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
Stumf
  • 941
  • 1
  • 11
  • 26

3 Answers3

1

Yeh you would need to include all the header files mentioned in BatteryTimeRemaining.c. So all of these files...

#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <notify.h>
#include <mach/mach.h>
#include <mach/mach_port.h>
#include <servers/bootstrap.h>
#include <asl.h>

#include "powermanagementServer.h" // mig generated
#include "BatteryTimeRemaining.h"
#include "SetActive.h"
#include "PrivateLib.h"

All the triangle bracket includes should be system include files and so should be on your system already. The quoted include files though are likely part of that project you are retrieving BatteryTimeRemaining.c from. You would need to copy them out of that project to your own project.

Likely its easier to copy out the functions you want and paste them into your own code base. No doubt you'll have some dependency issues but its probably easier to get one or two functions building on your system then the whole file. It's pretty long.

What function did you want to use exactly out of BatteryTimeRemaining.c?

Rob Segal
  • 7,515
  • 12
  • 43
  • 72
  • I wanted to get the value for capRatio = ((double)b->maxCap + kSmartBattReserve_mAh) / (double)b->designCap; Do you know what files and bits of code I will need? capRatio is all I want... – Stumf Feb 08 '10 at 17:57
  • Maybe I'm misunderstanding but if all you want is the calculations in that line then it follows the format of... capRatio = (a + b)/c You can easily replace a, b and c with your own variables. – Rob Segal Feb 08 '10 at 18:11
  • Yes, but where would I get the values for maxCap and designCap? They must be declared somewhere and I can't figure out where their values come from and how I can get them. It was for this reason that I thought I had to add all the header files etc. Is this necessary or am I missing some easy way to do this? – Stumf Feb 08 '10 at 18:22
  • maxCap and designCap are just variables though. Those values will be different in your code. You just want to perform the calculation right? – Rob Segal Feb 08 '10 at 18:30
  • Yes I would like to perform the calculation. In that case how do I get the values which are going to be contained in these two variables. Where do the numbers for the calculation come from, and how would I get them so i can find capRatio? – Stumf Feb 08 '10 at 18:38
  • I know the variables are declared in PrivateLib int maxCap; int designCap; – Stumf Feb 08 '10 at 18:41
  • The variables are definitely declared in PrivateLib.h but when you say where do you get values contained in these variables that's confusing. You should know what those values are in your own program. If not I would ask you to post some sample code. More specifically what I am thinking is to duplicate the line of code that you want in your own program you need the following... double a; double b; double c; capRatio = ((double)a + b) / (double)c; The values of a, b, c don't really matter here. They can be anything although c can't be zero or you will get divide by zero errors possibly. – Rob Segal Feb 08 '10 at 18:52
  • Hmmm... i thought these values maxCap and designCap were not decided by me as they will be different depending on what device the code is run on. maxCap for instance is the maximum capacity of the battery contained within a device. I was under the impression that the OS would get these values and I could calculate capRatio with what ever the values the variables designCap and maxCap are replaced with. This is why I thought the .c and .h files had to be added. Am I wrong here? If that is the case is there any way I could obtain the maximum and design capacities of a battery? – Stumf Feb 08 '10 at 19:04
  • Ah ok I was getting confused a bit. Sorry about that. The OS it seems will get those values yes but I think you are phrasing your question incorrectly to get the answer you want. You want information about the battery on the iphone right? Perhaps some of the battery properties and methods from the UIDevice class will help... http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/batteryState If all you want is maximum and design capacity numbers you can probably find that on Apple's website. – Rob Segal Feb 08 '10 at 19:18
  • My apologies if my question was confusing. Your suggestions above though will not work in this case. designCap is a constant value and I agree that it can be discovered online (I have actually already done this) maxCap however, varies with the age of the battery. Can the BatteryTimeRemaining.c and the various header files not be used to see what values the OS returns and then use them? In that file the OS must return values from somewhere or link to a file that does. Any ideas how I can get capRatio with design and maxCap being returned by the OS? – Stumf Feb 08 '10 at 19:26
  • Asking what capRatio seems like a bit of asking the wrong question to me. Ultimately you want to get how much battery time is left in an iPhone battery correct? There have been some discussions on this already... http://stackoverflow.com/questions/1469549/iphone-calculating-battery-life If you still want to get the value for capRatio I suggest tracing back through the flow of code to who calls the function "_setBatteryHealthConfidence" which contains the calculation for capRatio. This will likely lead you to a higher level function which can give you the calculation you want. – Rob Segal Feb 08 '10 at 19:37
  • No problem sorry I couldn't get you a full answer. – Rob Segal Feb 08 '10 at 19:57
  • Rob, if you include all the header files mentioned in BatteryTimeRemaining.c do you also need to include any files that these files you have added refer to? – Stumf Feb 12 '10 at 23:32
  • If I understand you correctly then yes. Let me try and clarify what you are getting at because I'm not 100% sure. If say "powermanagementServer.h" contained other include files you would have to include those as well. That is what your asking correct? – Rob Segal Feb 16 '10 at 20:31
0

Try adding just

#import "BatteryTimeRemaining.h"

to the file you want to use these functions in. It might be enough..

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
0

Unfortunately, most of the functions contained in this file are mac-specific, hence my problems using some of it's contents.

Stumf
  • 941
  • 1
  • 11
  • 26