Programmatic Radio Control for Smartphones and Pocket PCs

Lengthy title, yes. I’m working on an application for my Windows Mobile 6.0 Phone, and I needed to toggle the radio (phone receiver) on and off. It’s not very difficult to disable and enable the radio programmatically, but it did take quite a bit of research since I’m new to this platform. I couldn’t find any good, well-written examples on this using Google, so I decided to discuss my solution.

I used Visual Studio’s “Windows Mobile 5.0 Smartphone SDK (ARMV4I)” emulator and architecture for testing. The code is written in straight C. The process is pretty simple:

  1. Initialize with the TAPI DLL.
  2. Enumerate all the line devices. Find the one called “Cellular Line”.
  3. Open the line, change its equipment state, close the line.
  4. Shutdown from the TAPI DLL.

For initializing and shutting down access to TAPI, I made a few helper functions.

Select All Code:
#include <tapi.h>
#include <extapi.h>
#include <string.h>
 
HLINEAPP hLineApp = NULL;
DWORD g_NumDevices = 0;
 
int InitializeLineUtils(LINECALLBACK callback)
{
	int ret;
 
	/* Init TAPI, get device count */
	if ((ret = lineInitialize(&hLineApp,
			g_hInstance,  /* From WinMain */
			callback,
			NULL,
			&g_NumDevices))
		!= 0)
	{
		return ret;
	}
 
	return 0;
}
 
void ShutdownLineUtils()
{
	if (hLineApp != NULL)
	{
		lineShutdown(hLineApp);
		hLineApp = NULL;
	}
}

The next step is to find a device matching a specific name. Device names can be either in Unicode or ASCII, so my helper function takes both to eliminate any conversion. First the device API version is negotiated, then the device capabilities are queried. Note that the structure to do this is pretty low-level — you have to reallocate memory until the device decides it’s big enough to squirrel all its data past the end of the struct definition.

This function finds a matching device and returns its negotiated API version:

Select All Code:
DWORD FindLineDeviceByName(LPCWSTR wstr, LPCSTR cstr, DWORD *pAPIVersion)
{
	LONG ret;
	DWORD api_version;
	LINEDEVCAPS *dev_caps;
	LINEEXTENSIONID ext_id;
	DWORD device_id, dev_cap_size;
 
	dev_cap_size = sizeof(LINEDEVCAPS);
	dev_caps = (LINEDEVCAPS *)malloc(dev_cap_size);
	dev_caps->dwTotalSize = dev_cap_size;
 
	/* For each device... */
	for (device_id = 0; device_id < g_NumDevices; device_id++)
	{
		/* Negotiate an API version (0x00020000 is my CURRENT) */
		if ((ret = lineNegotiateAPIVersion(hLineApp,
				device_id,
				0x00020000,
				TAPI_CURRENT_VERSION,
				&api_version,
				&ext_id))
			!= 0)
		{
			continue;
		}
 
		while (TRUE)
		{
			if ((ret = lineGetDevCaps(hLineApp, 
					device_id,
					api_version,
					0,
					dev_caps)) == 0
				&& dev_caps->dwNeededSize <= dev_caps->dwTotalSize)
			{
				break;
			}
 
			if (ret == 0 || ret == LINEERR_STRUCTURETOOSMALL)
			{
				dev_cap_size = dev_caps->dwNeededSize;
				dev_caps = (LINEDEVCAPS *)realloc(dev_caps, dev_cap_size);
				dev_caps->dwTotalSize = dev_cap_size;
			}
			else
			{
				break;
			}
		}
 
		if (ret != 0)
		{
			continue;
		}
 
		if (dev_caps->dwStringFormat == STRINGFORMAT_UNICODE)
		{
			const wchar_t *ptr = (wchar_t *)((BYTE *)dev_caps 
				+ dev_caps->dwLineNameOffset);
			if (wcscmp(ptr, wstr) == 0)
			{
				*pAPIVersion = api_version;
				return device_id;
			}
		}
		else if (dev_caps->dwStringFormat == STRINGFORMAT_ASCII)
		{
			const char *ptr = (char *)dev_caps + dev_caps->dwLineNameOffset;
			if (strcmp(ptr, cstr) == 0)
			{
				*pAPIVersion = api_version;
				return device_id;
			}
		}
	}
 
	return -1;
}

Lastly, helper functions for opening and closing lines:

Select All Code:
LONG OpenLine(DWORD device_id, 
	      DWORD api_version, 
	      HLINE *pLine, 
	      DWORD inst,
	      DWORD privs,
	      DWORD media)
{
	return lineOpen(hLineApp,
		device_id,
		pLine,
		api_version,
		0,
		inst,
		privs,
		media,
		NULL);
}
 
void CloseLine(HLINE line)
{
	lineClose(line);
}

Now we can write a simple program to toggle the radio:

Select All Code:
VOID FAR PASCAL lineCallbackFunc(DWORD hDevice, 
				 DWORD dwMsg, 
				 DWORD dwCallbackInstance, 
				 DWORD dwParam1, 
				 DWORD dwParam2, 
				 DWORD dwParam3)
{
}
 
int WINAPI WinMain(HINSTANCE hInstance,
		   HINSTANCE hPrevInstance,
		   LPTSTR lpCmdLine,
		   int nCmdShow)
{
	HLINE hLine;
	DWORD state, radio_support;
	DWORD cell_api, cell_device;
 
	if (InitializeLineUtils(lineCallbackFunc) != 0)
	{
		return 1;
	}
 
	if ((cell_device = FindLineDeviceByName(
			L"Cellular Line",
			"Cellular Line",
			&cell_api))
		== -1)
	{
		ShutdownLineUtils();
		return 1;
	}
 
	if (OpenLine(cell_device, cell_api, &hLine, 0, LINECALLPRIVILEGE_NONE, 0) != 0)
	{
		ShutdownLineUtils();
		return 1;
	}
 
	if (lineGetEquipmentState(hLine, &state, &radio_support) == 0)
	{
		if (state != LINEEQUIPSTATE_FULL)
		{
			state = LINEEQUIPSTATE_FULL;
		}
		else
		{
			state = LINEEQUIPSTATE_MINIMUM;
		}
 
		lineSetEquipmentState(hLine, state);
	}
 
	CloseLine(hLine);
	ShutdownLineUtils();
 
	return 0;
}

Side note: LINEEQUIPSTATE_NOTXRX seemed to have no effect for me. My device turned the radio off on “MINIMUM” but refused the other option as unsupported.

As excellent and generally seamless as Visual Studio’s emulator was, it did not respond to lineSetEquipmentState at all. I guess since it’s not an actual device, maybe it doesn’t bother implementing the “equipment.”

12 thoughts on “Programmatic Radio Control for Smartphones and Pocket PCs

  1. Bobbye

    Ask your plumber to choose those newest products for you that can be energy efficient.
    It is also possible that there may be dead rodents that can cause blockage in one of the pipes of the plumbing system or objects like
    toys flushed down your bathroom’s toilet. Or you can even use a
    continual pump for that water to recycle
    on its own.

  2. make a gmail

    , make a gmail brand new Google add-on that
    operates like Canned Responses but without any on the above limitations.
    However, as useful as Gmail and Google’s other free Web-based services is usually,
    is going to be Gmail password isn’t as straightforward of any process since you might expect it to become.

  3. Alpha Complex Extreme Review

    Hiya very cool web site!! Guy .. Excellent ..
    Superb .. I will bookmark your site and take the feeds additionally…I’m glad to search out so
    many useful info right here in the post, we need develop extra techniques in this regard, thank you for sharing.

  4. Stacie

    Along as soon as the times, business developments have after that developed throughout the world.

    The number of new businesses that we don’t even know of exist because
    there are too many of them. Similarly, the gambling business,
    of course there are plus many names of gambling agents that have sprung up.
    However, the emergence of these agents does not always find the money for a good unconventional for gamblers, both upscale and beginner gamblers.
    with consequently many gambling agents popping up, extremely not all agents
    can be trusted because too many gambling concern worlds desire to understand more
    profits without thinking about the long-term
    impact. Both members and for their own business.

    Ensuring Bandar Legality

    Therefore approved agent2018 wants to present an easy pretension to find a trusted gambling agent site.

    The target is that you can have an idea, how to handle the situation. with you are in a allow
    in already trapped by fraud traps. Because we all know in that slant it’s impossible to
    acquire out soon. You are plus complacent, suitably you are
    not aware that you are yet under the have an effect on of fraud from irresponsible
    agents.

    To locate out the ascribed gambling agent is very
    easy, make distinct considering you visit the gambling site, whether it’s a gambling site or similar, it’s
    a good idea to ask the customer encourage whether the site is certified or not
    by asking for proof of ID number or member that can prove the existence of the site official soccer gambling is authenticated and doable to operate,
    usually an certified online gambling site. Always has an office
    in the Philippines and is below the aegis of Cagayan or Pagcor, and has legitimate immunity and
    authority. Which is protected by the declare of the Philippines, because the isolated country
    in Asia that is competent to manage to pay for licenses for online gambling thing sites, is on your own in the
    Philippines. easy acceptable right? To find a trusted online soccer betting site.

  5. linksbo

    Listing of Alternative Linksbo Links
    Today is increasingly developing, making it less difficult for folks to accessibility computerized networks.

    Until now there are several on the internet gambling games that may be
    played by anyone online. Therefore, betting lovers no extended require to
    typically the casino to learn gambling, this is pretty simple for them to play anytime plus anywhere without limitations.

  6. bandar-judi.com

    Daftar Situs Judi Online Terbaik 2020

    Because we all know that you have many problematic situs judi online in Indonesia,
    there are many obstructions that must be faced and many players find it
    hard to find their favorite taruhan online place.
    Here Depobola presents daftar situs judi online Sbobet terbaik, we have collaborated with supplier companies to make it easy for domestic gamblers
    who want to enjoy their exclusive video games.

  7. ubobet.win

    Ubobet is an internationally trusted gambling dealer since 2001 until now.
    By holding qualified admission for online gambling sites that manage to pay for various types of
    betting games. Games simple tally up Sportsbook, stir Casino and
    Cockfight. Ubobet has reached the entire world gambling puff
    including the Indonesian market. At present, all morning there are more than 100 thousand players who entrust their bets in the city of
    Ubobet.

    Ubobet List Site

    To register an account, players cannot attain it directly upon the Ubobet site.
    In further words, prospective players must register through the endorsed agent site for Ubobet listings such as Pandora188.
    By using Pandora188 ID the artist can bet on this city.
    How to register is quite easy, just by filling out the registration form
    correctly. After that make a addition of at
    least 25 thousand, later the artiste can bet upon Ubobet.
    all player who joins as a zealot at Pandora188 is entitled to
    a freebet further and with a daily growth added without exception.

    Ubobet Login

    After registering, players can log in on Ubobet in two ways.
    The first is through official buddies who have collaborations such as
    Pandora188. Because the two sites have already formed a partnership, which means that
    betting upon the Pandora188 site is the thesame as betting on Ubobet directly.
    Or by the second artifice you can directly log in to the
    Ubobet site if you already have an account.
    How to log in Ubobet is quite easy, just enter your ID & password correctly.

Comments are closed.