November 26th, 2014

Random API of the Day

Today’s random API is pow(), from the C standard library. The exp() function is used to take a real number to the power of a real number, which you may remember from high school algebra as exponentiation.

Over the past week, I’ve been adding a feature to wicrawl which allows the network discovery engine to capture frame metadata from the radio driver. This is handy to get things like the power level of the received packet, the signal-to-noise ratio, and other things that don’t show up in the 802.11 header of the captured frames.

In the radio world, the power is described in a unit called dBm, or decibels referenced to one milliwatt. The radiotap header provided by the ipw2200 driver for my Dell’s wireless card uses dBm when reporting the power of a rx packet, and I wanted to know how many milliwatts each packet represented.

The formula to convert dBm to milliwatts is mW = 10dBm/10, which requires use of the pow() function.  The conversion routine is:

inline double dBm_to_mW(int dBm) { return pow(10.0, ((double) dBm/10.0)); }

One problem I create by doing this is breaking wicrawl on targets like the Linksys WRT devices.  Like most cheap embedded platforms, the WRT series have a 32-bit processor that doesn’t including floating point hardware.   Without this hardware, we can’t use the float and double types in our program.

So what now?  Well, consider the input data we’re getting from the radio.  The dBm figure is provided as an integer, and it’s guaranteed to be within about -100 and 0 dBm.  Because there is only about a hundred values for the dBm figure, we can create a conversion table to go between dBm and nanowatts.  This way, we don’t have to do any floating-point work at all, at the expense of a little precision.

Leave a Response

Imhotep theme designed by Chris Lin. Proudly powered by Wordpress.
XHTML | CSS | RSS | Comments RSS