static Kvrlib.Status discoverDevices(Kvrlib.DiscoveryHnd discoveryHandle) { Kvrlib.Status status; Kvrlib.DeviceInfo[] device_info = new Kvrlib.DeviceInfo[64]; Int32 devices; Int32 delay_ms = 500; Int32 timeout_ms = 300; String buf = ""; status = Kvrlib.DiscoveryStart(discoveryHandle, delay_ms, timeout_ms); if (!status.Equals(Kvrlib.Status.OK)) { Kvrlib.GetErrorText(status, out buf); Console.WriteLine("kvrDiscoveryStart() FAILED - " + buf); return status; } devices = 0; while (status.Equals(Kvrlib.Status.OK)) { status = Kvrlib.DiscoveryGetResults(discoveryHandle, out device_info[devices]); if (status.Equals(Kvrlib.Status.OK)) { dumpDeviceInfo(device_info[devices]); if (!Kvrlib.DiscoverySetPassword(device_info[devices], password).Equals(Kvrlib.Status.OK)) { Console.WriteLine("Unable to set password: {0:s} ({1:d})", password, password.Length); } if (!Kvrlib.DiscoverySetEncryptionKey(device_info[devices], "testkey").Equals(Kvrlib.Status.OK)) { Console.WriteLine("Unable to set Ecryption key"); } if (!Kvrlib.DiscoverySetEncryptionKey(device_info[devices], "").Equals(Kvrlib.Status.OK)) { Console.WriteLine("Unable to set Ecryption key"); } devices++; } else { if (status.Equals(Kvrlib.Status.BLANK)) { Console.WriteLine("kvrDiscoveryGetResults() failed " + status); } } } status = Kvrlib.DiscoveryStoreDevices(device_info); if (!status.Equals(Kvrlib.Status.OK)) { Kvrlib.GetErrorText(status, out buf); Console.WriteLine("Device store failed: " + buf); return status; } return status; }
static Kvrlib.Status doConfigure(Kvrlib.ConfigHnd handle) { Kvrlib.Status status; string new_xml_config = ""; string old_xml_config = ""; string xml_error = ""; status = Kvrlib.ConfigGet(handle, out old_xml_config); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not read configuration from device (" + status + ")"); Kvrlib.ConfigClose(handle); return status; } Console.WriteLine("Old configuration: " + old_xml_config); new_xml_config = old_xml_config; // Check that the new configuration is valid status = Kvrlib.ConfigVerifyXml(new_xml_config, out xml_error); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("The XML configuration is not valid (" + status + "):\n" + xml_error); Kvrlib.ConfigClose(handle); return status; } // Download new configuration status = Kvrlib.ConfigSet(handle , old_xml_config); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not write configuration to device (" + status + ")"); Kvrlib.ConfigClose(handle); return status; } return status; }
static void dumpDeviceInfo(Kvrlib.DeviceInfo di) { String service_text = ""; String buf = ""; Kvrlib.Status status; String addr_buf = ""; Int32 i; Kvrlib.ServiceState service_state = 0; Kvrlib.StartInfo service_sub_state = 0; Console.Write("--------------------------------------------------------------------------\n"); Console.Write("Device information\n"); Console.Write("EAN: {0:X}{1:X}\n", di.ean_hi, di.ean_lo); Console.WriteLine("FW version: " + di.fw_major_ver +"." + di.fw_minor_ver +"." + di.fw_build_ver); Console.WriteLine("Serial: " + di.ser_no); Console.WriteLine("Name: " + di.name); Console.WriteLine("Host name: " + di.host_name); Console.Write("Password: "******"None\n"); } else { for (i = 0; (i < di.accessibility_pwd.Length); i++) { Console.Write("*"); } Console.Write("\n"); } Kvrlib.StringFromAddress(out addr_buf, di.device_address); Console.WriteLine("IP: " + addr_buf); Kvrlib.StringFromAddress(out addr_buf, di.client_address); Console.WriteLine("Client IP: " + addr_buf); Console.WriteLine("Usage: " + di.usage); if ((!di.usage.Equals(Kvrlib.DeviceUsage.FREE)) && (isUsedByMe(di) > 0)) { Console.Write(" - Used by Me!\n"); } else if ((!di.usage.Equals(Kvrlib.DeviceUsage.FREE)) && (!di.usage.Equals(Kvrlib.DeviceUsage.UNKNOWN))) { Console.Write(" - Used by other!\n"); } else { Console.Write("\n"); } Console.WriteLine("Alive: " + ((di.availability & Kvrlib.Availability.FOUND_BY_SCAN) > 0 ? "Yes" : "No")); Console.WriteLine("Stored: " + ((di.availability & Kvrlib.Availability.STORED) > 0 ? "Yes" : "No")); // Ask service for status service_text status = Kvrlib.DeviceGetServiceStatusText(di, out service_text); if (!status.Equals(Kvrlib.Status.OK)) { Kvrlib.GetErrorText(status, out buf); Console.WriteLine("Service: FAILED - " + buf); } else if (service_text.StartsWith("Service: ")) { Console.WriteLine("Service: " + service_text.Substring(9 , service_text.Length -9)); } else { Console.Write("%s\n", service_text); } status = Kvrlib.DeviceGetServiceStatus(di, out service_state, out service_sub_state); if (status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("service_state: " + service_state +"." + service_sub_state); } else { Console.WriteLine("service_state: unknown"); } }
static Kvrlib.Status setupBroadcast(Kvrlib.DiscoveryHnd discoveryHandle) { String buf = ""; Kvrlib.Status status; Kvrlib.Address[] addr_list = new Kvrlib.Address[20]; Int32 no_addrs = 0; Int32 i; status = Kvrlib.DiscoveryGetDefaultAddresses(out addr_list, Kvrlib.AddressTypeFlag.ALL); if (!status.Equals(Kvrlib.Status.OK)) { Kvrlib.GetErrorText(status, out buf); Console.WriteLine("kvrDiscoveryGetDefaultAddresses() FAILED - " + buf); return status; } if (no_addrs < addr_list.Length) { String tmp_addr = "192.168.3.66"; status = Kvrlib.AddressFromString(Kvrlib.AddressType.IPV4, out addr_list[no_addrs], tmp_addr); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("ERROR: kvrAddressFromString(" + no_addrs + ", " + tmp_addr + ") failed"); return status; } else { no_addrs++; } } else { Console.WriteLine("NOTE: We don't have room for all devices in addr_list[" + addr_list.Length + "]"); } for (i = 0; i < no_addrs; i++ ) { status = Kvrlib.StringFromAddress(out buf, addr_list[i]); Console.WriteLine("Looking for device using: " + buf); } //Create address from string. Kvrlib.Address addressFromString; String addr_string = "127.0.0.1"; status = Kvrlib.AddressFromString(Kvrlib.AddressType.IPV4, out addressFromString, addr_string); if (!status.Equals(Kvrlib.Status.OK)) { Kvrlib.GetErrorText(status, out buf); Console.WriteLine("AddressFromString() FAILED - " + buf); return status; } Kvrlib.StringFromAddress(out addr_string, addressFromString); Console.WriteLine("Created address from string: " + addr_string); status = Kvrlib.DiscoverySetAddresses(discoveryHandle, addr_list); if (!status.Equals(Kvrlib.Status.OK)) { Kvrlib.GetErrorText(status, out buf); Console.WriteLine("DiscoverySetAddresses() FAILED - " + buf); return status; } return status; }
static Int32 isUsedByMe(Kvrlib.DeviceInfo di) { Int32 i; Int32 channel_count; UInt64 ean; Canlib.canStatus status; UInt32 ean_hi = 0; UInt32 ean_lo = 0; UInt32 serial = 0; Canlib.canInitializeLibrary(); status = Canlib.canGetNumberOfChannels(out channel_count); if (!status.Equals(Canlib.canStatus.canOK)) { Console.WriteLine("ERROR: canGetNumberOfChannels failed " + status); return -1; } for (i = 0; (status.Equals(Canlib.canStatus.canOK)) && (i < channel_count); i++) { object ean0; status = Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CARD_UPC_NO, out ean0); if (!status.Equals(Canlib.canStatus.canOK)) { Console.WriteLine("ERROR: canCHANNELDATA_CARD_UPC_NO failed: " + status); return -1; } ean = UInt64.Parse(ean0.ToString()); ean_hi = (UInt32) ((ean >> 32) & 0xffffffff); ean_lo = (UInt32) ((ean) & 0xffffffff); object serial0; status = Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CARD_SERIAL_NO, out serial0); if (!status.Equals(Canlib.canStatus.canOK)) { Console.WriteLine("ERROR: canCHANNELDATA_CARD_SERIAL_NO failed: " + status); return -1; } if ((di.ean_hi.Equals(ean_hi)) && (di.ean_lo.Equals(ean_lo)) && (di.ser_no.Equals(serial))) { return 1; } } return 0; }
static Kvrlib.Status doScanNetwork(Kvrlib.ConfigHnd handle) { Kvrlib.Status status; Kvrlib.Status stat; Int32 active = 0; //is a passive scan Kvrlib.Bss bss_type = Kvrlib.Bss.ANY; //infrastruction and adhoc Kvrlib.RegulatoryDomain domain = Kvrlib.RegulatoryDomain.WORLD; string ssid = ""; string securityString = ""; Int32 rssi = 0; Int32 channel = 0; Kvrlib.Address mac; Int32 capability; Int32 type_wpa; Kvrlib.CipherInfoElement wpa_info = new Kvrlib.CipherInfoElement(); Kvrlib.CipherInfoElement rsn_info = new Kvrlib.CipherInfoElement(); status = Kvrlib.WlanStartScan(handle, active, bss_type, domain); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not start scan " + Convert.ToInt32(status)); return status; } do { status = Kvrlib.WlanGetScanResults(handle, ref rssi, ref channel, out mac, out bss_type, out ssid, out capability, out type_wpa, out wpa_info, out rsn_info); if(status.Equals(Kvrlib.Status.OK)) { String tmp_address; Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); Console.WriteLine("SSID: " + ssid); Console.WriteLine("RSSI: " + rssi +" dBm"); Console.WriteLine("WLAN Channel: " + channel); status = Kvrlib.StringFromAddress(out tmp_address, mac); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could convert string from address (" + status + ")"); return status; } Console.WriteLine("MAC address: " + tmp_address); Console.WriteLine("BSS type: " + bss_type); Console.WriteLine("Capabilities: {0:X}", capability); stat = Kvrlib.WlanGetSecurityText(out securityString, capability, type_wpa, wpa_info, rsn_info); Console.Write("Security"); if (stat.Equals(Kvrlib.Status.PARAMETER)) { Console.Write("(Truncated)"); } Console.WriteLine(": " + securityString); } } while(status.Equals(Kvrlib.Status.OK) || status.Equals(Kvrlib.Status.NO_ANSWER)); // Kvrlib.Status.BLANK => no more networks => OK return (status.Equals(Kvrlib.Status.BLANK) ? Kvrlib.Status.OK : status); }
static Kvrlib.Status doTryConfigure(Kvrlib.ConfigHnd handle, Int32 seconds, object ean, object serial) { Kvrlib.Status status; Kvrlib.Address address; Kvrlib.Address mac; Kvrlib.Address netmask; Kvrlib.Address gateway; String tmp_address = ""; Boolean dhcp; Kvrlib.NetworkState state; Int32 tx_rate; Int32 rx_rate; Int32 channel; Int32 rssi_mean; Int32 tx_power; UInt32 ean_hi; UInt32 ean_lo; Int32[] rssi; Int32[] rtt; status = Kvrlib.NetworkConnectionTest(handle, 1); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not start ping(" + status + ")"); return status; } do { System.Threading.Thread.Sleep(1000); // Ask for RSSI and RTT so that we get updated // values when calling kvrNetworkGetConnectionStatus() status = Kvrlib.NetworkGetRssiRtt(handle, out rssi, out rtt); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not get RSSI / RTT (" + status + ")"); break; } status = Kvrlib.NetworkGetConnectionStatus(handle, out state, out tx_rate, out rx_rate, out channel, out rssi_mean, out tx_power); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not get status (" + status + ")"); break; } Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); Console.WriteLine("Connection state: " + state); Console.WriteLine("Transmit rate: " + tx_rate + " kbit/s"); Console.WriteLine("Receive rate: " + rx_rate + " kbit/s"); Console.WriteLine("Channel: " + channel); Console.WriteLine("Receive Signal Strength Indicator: " + rssi_mean + " dBm"); Console.WriteLine("Transmit power level: " + tx_power + " dB"); } while(--seconds > 0); status = Kvrlib.NetworkConnectionTest(handle, 0); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not start ping(" + status + ")"); return status; } status = Kvrlib.NetworkGetAddressInfo(handle, out address, out mac, out netmask, out gateway, out dhcp); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could not get IP info(" + status + ")"); return status; } // Assume IP v.4, i.e. address/netmask/gateway.type is kvrAddressType_IPV4 Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); Console.WriteLine("DHCP: " + dhcp); status = Kvrlib.StringFromAddress(out tmp_address, mac); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could convert string from address (" + status + ")"); return status; } Console.WriteLine("MAC address: " + tmp_address); status = Kvrlib.StringFromAddress(out tmp_address, address); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could convert string from address (" + status + ")"); return status; } Console.WriteLine("IP Address: " + tmp_address); status = Kvrlib.StringFromAddress(out tmp_address, netmask); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could convert string from address (" + status + ")"); return status; } Console.WriteLine("Netmask: " + tmp_address); status = Kvrlib.StringFromAddress(out tmp_address, gateway); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could convert string from address (" + status + ")"); return status; } Console.WriteLine("Gateway: " + tmp_address); status = Kvrlib.NetworkGetHostName(handle, out tmp_address); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could get hostname (" + status + ")"); return status; } Console.WriteLine("Hostname: " + tmp_address); ean_hi = (UInt32) Convert.ToUInt64(ean) >> 32; ean_lo = (UInt32) Convert.ToUInt64(ean) & 0xffffffff; status = Kvrlib.HostName(ean_hi, ean_lo, Convert.ToUInt32(serial), out tmp_address); if (!status.Equals(Kvrlib.Status.OK)) { Console.WriteLine("Could create hostname (" + status + ")"); return status; } Console.WriteLine("Default hostname: " + tmp_address); return status; }