Windows Mobile – A Simple Web Server with Extended Features
Windows Mobile - a simple web server with extended features
The attached web server offers some options to control the device or show information about it. The web server runs on the device and enables direct control or delivers information about the device.
The project enclosed default web page offers all implemented commands. The following is a list of what is currently implemented:
SystemInfo
: Get OS version infoBEEP
: Let the device beepSTART
: Starts a processKILL
: Kills a processSETLED
: Light/unlight/blink a LEDVIBRATE
: Special LED case, this sets a LED which is a placeholder for the vibrate motorSIP
: Show/hide the Software KeyboardSHOW
: ShowWindow implementation (show normal, hide, minimize, etc.)ENABLEWINDOW
: Enable or disable a window
The Default Web Page
If you click a function (command), the output is displayed at the bottom of the example default page:
You can also call the functions directly from any web browser that has access to the devices network and get only the answer back.
In example, calling “http://192.168.128.100:8088/ahah.html?SystemInfo” will give you “‘SystemInfo’ ”, ‘Microsoft Windows CE 5.2.29040′ = OK” or so.
Reflection Instead of switch() or If/ElseIf
These commands can be easily extended as the code uses reflection to look up the commands you send. This is a better approach than doing a If
/ElseIf
or Switch()
search for known commands:
/// <summary>
/// here the myCommands object is queried for the recognized command
/// </summary>
/// <param name="sFunc">name of the command</param>
/// <param name="sArg">arguments to the command</param>
/// <param name="sResponse">return string of a command</param>
/// <returns>True if existing command was found</returns>
public static bool execCmd(string sFunc, string sArg, ref string sResponse)
{
bool bRet = true;
try
{
// Instantiate this class
myCommands cmbn = new myCommands(sFunc, sArg);
// Get the desired method by name: DisplayName
//MethodInfo methodInfo = typeof(CallMethodByName).GetMethod("DisplayName");
MethodInfo methodInfo = typeof(myCommands).GetMethod(sFunc);
// Use the instance to call the method without arguments
methodInfo.Invoke(cmbn, null);
sResponse = cmbn.response;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine
("Exception in execCmd for '" + sFunc + "' and '" + sArg + "' " + ex.Message);
bRet = false;
}
return bRet;
}
...
The myCommands
class looks like this and is easy to extend:
/// <summary>
/// this class holds all know commands
/// the class is queried using reflection to lookup a command
/// </summary>
class myCommands
{
string name;
string arg="";
public string response="";
public myCommands(string name, string sArg)
{
this.name = name;
this.arg = sArg;
}
/// <summary>
/// return some System information
/// </summary>
public void SystemInfo()
{
string sOS = System.Environment.OSVersion.ToString();
response = sOS;
}
public void BEEP()
{
beepType beep = (beepType)Enum.Parse(typeof(beepType), arg, false);
MessageBeep(beep);
}
...
So, if you need to add an command, simply add a new function inside myCommands
class using arg
and response to communicate with the web server.
Code and Binary
The Visual Studio 2008 source code is written against Compact Framework 2.0. There are two projects, one is the great CompactWeb
web server and the other is the webCommand
project. The webCommand
project includes a directory inetpub, which is deployed below the applications directory.
inetpub Directory
- default.html
This is the default web site containing examples for all commands. For example:
<html> <script language="javascript" type="text/javascript" src="ahah.js"></script> <script language="javascript" type="text/javascript"> //Calls the library function 'ahah' - defined in 'ahah.js' file. function load(filename) { ahah(filename,"content"); } </script> <body> <p>WebCommands TestPage</p> <h2>Informations</h2> <ul id="SystemInfo"> <li><a href="javascript:load ('ahah.html?SystemInfo');">OS Version</a></li> </ul> ...
- javascript.js
Simply loads ahah.js and implements theload
function (asynchronously call (AJAX) technology). - ahah.js
This JavaScript library does asynchronously call the ‘web commands’ (AJAX).
The rest of the files are included for testing. The same is valid for the server dir, which contains example files which will work on an external web server (i.e., XAMPP on windows), if cross-side scripting is allowed. The server files contain fixed IP address for the device and have to be changed if you would like to test cross-side scripting.
I insist on your fantasy, what else is possible. Some examples: get a screenshot, get extended device information, get a process list…….