Mass Configuration

I have to go onto the buses in this tropical paradise, and do a bunch of things to configure my electronic fareboxes. We are switching WiFi providers, so I have to write a new network identification (SSID), and the password or encryption key along with it. I have to input a static IP address for the wireless adapter. I have to copy over new software, and I have to record the hardware address or MAC address for MAC address filtering security. And I have to take a copy of the farefile.

I wrote a program in C# to do all of this, and I just carry the program on an SD Memory card, which I stick into the PDA and run it all from the memory card (I borrowed the memory card from my digital camera).

Here is the source code listing for the program. Each button does a function described above. I downloaded the OpenNETCF libraries to do some of the stuff:


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Net;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace massConfig
{
using DWORD = System.UInt32;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// button1_Click
///

///
///
/// this button sets the ssid and WEP key
private void button1_Click(object sender, EventArgs e)
{
AdapterCollection adapters = Networking.GetAdapters();
foreach (Adapter adapter in adapters)
{
try
{
if (adapter.IsWireless)
{
EAPParameters eapp = new EAPParameters();
eapp.Enable8021x = true;
eapp.AuthData = IntPtr.Zero;
eapp.AuthDataLen = 0;
eapp.EapType = EAPType.PEAP;
eapp.EapFlags = EAPFlags.Enabled;
bool abc = false;
abc = adapter.SetWirelessSettingsAddEx("NameOfSSID", true, "WEPKEY", 1, AuthenticationMode.Ndis802_11AuthModeOpen, WEPStatus.Ndis802_11WEPEnabled, eapp);
adapter.BindAdapter();
MessageBox.Show("SSID written");
}
}
catch (Exception mm)
{
MessageBox.Show(mm.Message);
}
}
}
///
/// button3_click
///

///
///
/// This button moves the fare file to the SD card
private void button3_Click(object sender, EventArgs e)
{
RegistryKey masterKey = Registry.CurrentUser.CreateSubKey("ControlPanel\\Owner");
string ownerName = (string)masterKey.GetValue("Name");
String fareFile = "/ftpfiles/fares.csv";
string ill = "/Storage Card/fares/" + ownerName + "fares.csv";
File.Move(fareFile, ill);
MessageBox.Show("Fare file moved.");
masterKey.Close();
}
///
/// button4_click
///

///
///
/// THis sets the static IP address of the adapter
/// I have to increment the IP address so I do it from a file
private void button4_Click(object sender, EventArgs e)
{
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("Comm\\SDIO86861\\Parms\\TcpIp");
string fileName = "/Storage Card/ipnos.txt";
StreamReader SR;
SR = File.OpenText(fileName);
string str = SR.ReadLine();
SR.Close();
int lastNum = Int16.Parse(str);
string cdd = lastNum.ToString();
string[] ipArray = new string[1];
ipArray[0] = "192.168.54."+cdd;
masterKey.SetValue("IpAddress", ipArray, RegistryValueKind.MultiString);

DWORD enableDhcp = 0;
DWORD autocfg = 1;
DWORD useZeroBroadcast = 0;
masterKey.SetValue("EnableDHCP", enableDhcp, RegistryValueKind.DWord);
masterKey.SetValue("AutoCfg", autocfg, RegistryValueKind.DWord);
masterKey.SetValue("UseZeroBroadcast", useZeroBroadcast, RegistryValueKind.DWord);
string[] gateArray = new string[1];
gateArray[0] = "192.168.54.1";
masterKey.SetValue("DefaultGateway", gateArray, RegistryValueKind.MultiString);
string[] subnetArray = new string[1];
subnetArray[0] = "255.255.255.0";
masterKey.SetValue("Subnetmask", subnetArray, RegistryValueKind.MultiString);
masterKey.Close();
lastNum = lastNum + 1;
System.IO.File.Delete(fileName);
StreamWriter SW;
SW = File.CreateText(fileName);
SW.WriteLine(lastNum.ToString());
SW.Close();
MessageBox.Show("Static IP written.");
}
///
/// Copy the new software over
///

///
///
private void button5_Click(object sender, EventArgs e)
{
System.IO.File.Delete("/Program Files/Farebox1/Farebox1.exe");
System.IO.File.Copy("/Storage Card/exefiles/Farebox1.exe", "/Program Files/Farebox1/Farebox1.exe");
System.IO.File.Copy("/Storage Card/exefiles/OpenNETCF.dll", "/Program Files/Farebox1/OpenNETCF.dll");
System.IO.File.Copy("/Storage Card/exefiles/OpenNETCF.Net.dll", "/Program Files/Farebox1/OpenNETCF.Net.dll");
System.IO.File.Copy("/Storage Card/exefiles/saniTize.exe", "/Program Files/Farebox1/saniTize.exe");
System.IO.File.Copy("/Storage Card/exefiles/hider.exe", "/Program Files/Farebox1/hider.exe");
System.IO.File.Copy("/Storage Card/exefiles/NETCFv35.Messages.EN.wm.cab", "/Program Files/NETCFv35.Messages.EN.wm.cab");
MessageBox.Show("Software Copied.");
}
///
/// button6_click
///

///
///
/// collect the mac address and the owner ID of the IPAQ and write it to a file
private void button6_Click(object sender, EventArgs e)
{
RegistryKey masterKey = Registry.CurrentUser.CreateSubKey("ControlPanel\\Owner");
string ownerName = (string)masterKey.GetValue("Name");
masterKey.Close();
AdapterCollection adapters = Networking.GetAdapters();
foreach (Adapter adapter in adapters)
{
try
{
if (adapter.IsWireless)
{
string macadd = BitConverter.ToString(adapter.MacAddress);
StreamWriter SW;
SW = File.AppendText("/Storage Card/macs.txt");
SW.WriteLine(ownerName + " , " + macadd);
SW.Close();
MessageBox.Show("mac address written");
}
}
catch (Exception mm)
{
MessageBox.Show(mm.Message);
}
}
}
}
}

No comments: