Example #1
0
        List<FoscamScannedNetwork> ParseScanResults(string scanResultStr)
        {
            List<FoscamScannedNetwork> scanResults = new List<FoscamScannedNetwork>();

            Regex regex = new Regex(@"ap_(.+)\[(\d+)\]=(.+);");

            var lines = scanResultStr.Split(new[] { '\r', '\n' });

            foreach (string line in lines)
            {
                Match match = regex.Match(line);

                if (match.Success)
                {
                    string key = match.Groups[1].Value;
                    int num = int.Parse(match.Groups[2].Value);
                    string value = match.Groups[3].Value;

                    FoscamScannedNetwork network;

                    if (key.Equals("bssid"))
                    {
                        //we expect bssid to be listed first
                        if (scanResults.Count != num)
                            throw new Exception("counts don't match up");

                        network = new FoscamScannedNetwork();
                        network.bssid = StripQuotes(value);

                        scanResults.Add(network);

                    }
                    else
                    {
                        network = scanResults[num];

                        switch (key)
                        {
                            case "ssid":
                                network.ssid = StripQuotes(value);
                                break;
                            case "mode":
                                network.mode = int.Parse(value);
                                break;
                            case "security":
                                network.security = int.Parse(value);
                                break;
                            default:
                                throw new Exception("unknown key type");

                        }
                    }
                }

            }

            return scanResults;
        }
Example #2
0
        public bool SendWifiCredentials(FoscamScannedNetwork network, string passPhrase, string cameraIp, string username, string password)
        {
            string url = @"set_wifi.cgi?user=undefined&pwd=undefined&next_url=rebootme.htm&channel=5&mode=0";
            string enable_p = @"&enable=0";
            string ssid_p = @"&ssid=";
            string encrypt_p = "&encrypt=0";
            string authtype_p = "&authtype=0";
            string keyformat_p = "&keyformat=0";
            string defkey_p = "&defkey=0";
            string key1_p = "&key1=";
            string key2_p = "&key2=";
            string key3_p = "&key3=";
            string key4_p = "&key4=";
            string key1_bits_p = "&key1_bits=0";
            string key2_bits_p = "&key2_bits=0";
            string key3_bits_p = "&key3_bits=0";
            string key4_bits_p = "&key4_bits=0";
            string wpa_psk_p = "&wpa_psk=";

            enable_p = "&enable=1";
            ssid_p += Uri.EscapeDataString(network.ssid);
            encrypt_p = "&encrypt=" + network.security;

            if (network.security == 1)
            {
                throw new Exception("I don't deal with WEP");
                //authtype_p="&authtype="+authtype.selectedIndex;
                //keyformat_p="&keyformat="+keyformat.selectedIndex;
                //defkey_p="&defkey="+defkey.selectedIndex;
                //key1_p+=encodeURIComponent(key1.value);
                //key2_p+=encodeURIComponent(key2.value);
                //key3_p+=encodeURIComponent(key3.value);
                //key4_p+=encodeURIComponent(key4.value);
                //key1_bits_p="&key1_bits="+key1_bits.selectedIndex;
                //key2_bits_p="&key2_bits="+key2_bits.selectedIndex;
                //key3_bits_p="&key3_bits="+key3_bits.selectedIndex;
                //key4_bits_p="&key4_bits="+key4_bits.selectedIndex;
            }
            else if (network.security > 1)
            {
                wpa_psk_p += Uri.EscapeDataString(passPhrase);
            }

            string location = url + enable_p + ssid_p + encrypt_p + authtype_p + keyformat_p + defkey_p + key1_p + key2_p + key3_p + key4_p + key1_bits_p + key2_bits_p + key3_bits_p + key4_bits_p + wpa_psk_p;

            string urlToCall = String.Format("http://{0}/{1}", cameraIp, location);

            HttpWebResponse webResponse = SendHttpRequest(urlToCall, username, password);

            //we don't need the response, but lets eat it.
            GetHttpResponseStr(webResponse);

            if (webResponse.StatusCode != HttpStatusCode.OK)
            {
                logger.Log("FoscamScout got bad status code {0} while setting wifi", webResponse.StatusCode.ToString());
                return false;
            }

            return true;
        }
Example #3
0
        public bool SendWifiCredentials(FoscamScannedNetwork network, string passPhrase, string cameraIp, string username, string password)
        {
            string url         = @"set_wifi.cgi?user=undefined&pwd=undefined&next_url=rebootme.htm&channel=5&mode=0";
            string enable_p    = @"&enable=0";
            string ssid_p      = @"&ssid=";
            string encrypt_p   = "&encrypt=0";
            string authtype_p  = "&authtype=0";
            string keyformat_p = "&keyformat=0";
            string defkey_p    = "&defkey=0";
            string key1_p      = "&key1=";
            string key2_p      = "&key2=";
            string key3_p      = "&key3=";
            string key4_p      = "&key4=";
            string key1_bits_p = "&key1_bits=0";
            string key2_bits_p = "&key2_bits=0";
            string key3_bits_p = "&key3_bits=0";
            string key4_bits_p = "&key4_bits=0";
            string wpa_psk_p   = "&wpa_psk=";

            enable_p  = "&enable=1";
            ssid_p   += Uri.EscapeDataString(network.ssid);
            encrypt_p = "&encrypt=" + network.security;

            if (network.security == 1)
            {
                throw new Exception("I don't deal with WEP");
                //authtype_p="&authtype="+authtype.selectedIndex;
                //keyformat_p="&keyformat="+keyformat.selectedIndex;
                //defkey_p="&defkey="+defkey.selectedIndex;
                //key1_p+=encodeURIComponent(key1.value);
                //key2_p+=encodeURIComponent(key2.value);
                //key3_p+=encodeURIComponent(key3.value);
                //key4_p+=encodeURIComponent(key4.value);
                //key1_bits_p="&key1_bits="+key1_bits.selectedIndex;
                //key2_bits_p="&key2_bits="+key2_bits.selectedIndex;
                //key3_bits_p="&key3_bits="+key3_bits.selectedIndex;
                //key4_bits_p="&key4_bits="+key4_bits.selectedIndex;
            }
            else if (network.security > 1)
            {
                wpa_psk_p += Uri.EscapeDataString(passPhrase);
            }


            string location = url + enable_p + ssid_p + encrypt_p + authtype_p + keyformat_p + defkey_p + key1_p + key2_p + key3_p + key4_p + key1_bits_p + key2_bits_p + key3_bits_p + key4_bits_p + wpa_psk_p;

            string urlToCall = String.Format("http://{0}/{1}", cameraIp, location);

            HttpWebResponse webResponse = SendHttpRequest(urlToCall, username, password);

            //we don't need the response, but lets eat it.
            GetHttpResponseStr(webResponse);

            if (webResponse.StatusCode != HttpStatusCode.OK)
            {
                logger.Log("FoscamScout got bad status code {0} while setting wifi", webResponse.StatusCode.ToString());
                return(false);
            }

            return(true);
        }
Example #4
0
        internal List <string> SendWifiCredentials(string uniqueDeviceId)
        {
            Device device = currDeviceList.GetDevice(uniqueDeviceId);

            if (device == null)
            {
                return new List <string>()
                       {
                           "Could not find the device in my current list"
                       }
            }
            ;

            //get the username and password for the camera
            var driverParams = platform.GetDeviceDriverParams(device);

            if (driverParams.Count != 3)
            {
                return new List <string>()
                       {
                           "DriverParams are in unknown state. Param count " + driverParams.Count.ToString()
                       }
            }
            ;

            string username = driverParams[1];
            string password = driverParams[2];

            //get the wifi credentials
            string wifiSsid = platform.GetPrivateConfSetting("WifiSsid");
            string wifiKey  = platform.GetPrivateConfSetting("WifiKey");

            if (string.IsNullOrWhiteSpace(wifiSsid))
            {
                return new List <string>()
                       {
                           "WifiSsid is not configured in the hub"
                       }
            }
            ;


            //now get scan results
            int maxScansLeft = 3;
            List <FoscamScannedNetwork> scanResults = null;

            while (maxScansLeft >= 1)
            {
                try
                {
                    scanResults = GetScanResult(device.DeviceIpAddress, username, password);
                }
                catch (Exception)
                {
                    return(new List <string>()
                    {
                        "Error in getting scan results from the camera"
                    });
                }

                if (scanResults.Count == 0)
                {
                    maxScansLeft--;
                }
                else
                {
                    break;
                }
            }

            if (scanResults == null || scanResults.Count == 0)
            {
                logger.Log("Skipping {0} as Wifi scan did not find any nework", device.UniqueName);

                return(new List <string>()
                {
                    "Wifi scan by camera did not find any network"
                });
            }

            //check if we the network we know was seen
            FoscamScannedNetwork targetNetwork = null;

            foreach (var network in scanResults)
            {
                if (network.ssid.Equals(wifiSsid))
                {
                    targetNetwork = network;
                    break;
                }
            }

            if (targetNetwork == null)
            {
                logger.Log("Skipping {0} as Wifi scan did not find our ssid {1}", device.UniqueName, wifiSsid);

                return(new List <string>()
                {
                    "Wifi scan by camera did not find our ssid " + wifiSsid
                });
            }

            // now configure the thing!
            bool result = false;

            try
            {
                result = SendWifiCredentials(targetNetwork, wifiKey, device.DeviceIpAddress, username, password);
            }
            catch (Exception)
            {
                return(new List <string>()
                {
                    "Error in configuring wifi for the camera"
                });
            }
            if (result)
            {
                logger.Log("Foscam configuration (seems to have) succeeded!");

                //we do not reboot, because we expect the user to power cycle the camera
                //in the process of moving it someplace else

                //Reboot(cameraIp, username, password);
                //System.Threading.Thread.Sleep(30 * 1000);

                return(new List <string>()
                {
                    ""
                });
            }
            else
            {
                logger.Log("Configuration failed!");

                return(new List <string>()
                {
                    "configuration failed due to unknown reason"
                });
            }
        }
    }
}
Example #5
0
        public List <string> AreCameraCredentialsValid(string uniqueDeviceId, string username, string password)
        {
            Device device = currDeviceList.GetDevice(uniqueDeviceId);

            if (device == null)
            {
                return new List <string>()
                       {
                           "Could not find the device in my current list", ""
                       }
            }
            ;

            string requestStr = String.Format("http://{0}/get_camera_params.cgi", device.DeviceIpAddress);

            try
            {
                HttpWebResponse webResponse = SendHttpRequest(requestStr, username, password);

                //we get the string just so we can eat it
                GetHttpResponseStr(webResponse);

                // if the request went through we are good
                return(new List <string>()
                {
                    "", "true"
                });
            }
            catch (Exception webEx)
            {
                //if the exception contains 401, that implies unauthorized access
                if (webEx.Message.Contains("401"))
                {
                    return new List <string>()
                           {
                               "", "false"
                           }
                }
                ;

                //otherwise, we don't know what is going on; propagate this exception up
                logger.Log("Got exception while checking foscam credentials: ", webEx.ToString());

                return(new List <string>()
                {
                    webEx.Message, ""
                });
            }
        }

        List <FoscamScannedNetwork> ParseScanResults(string scanResultStr)
        {
            List <FoscamScannedNetwork> scanResults = new List <FoscamScannedNetwork>();

            Regex regex = new Regex(@"ap_(.+)\[(\d+)\]=(.+);");

            var lines = scanResultStr.Split(new[] { '\r', '\n' });

            foreach (string line in lines)
            {
                Match match = regex.Match(line);

                if (match.Success)
                {
                    string key = match.Groups[1].Value;

                    int    num   = int.Parse(match.Groups[2].Value);
                    string value = match.Groups[3].Value;

                    FoscamScannedNetwork network;



                    if (key.Equals("bssid"))
                    {
                        //we expect bssid to be listed first
                        if (scanResults.Count != num)
                        {
                            throw new Exception("counts don't match up");
                        }

                        network       = new FoscamScannedNetwork();
                        network.bssid = StripQuotes(value);

                        scanResults.Add(network);
                    }
                    else
                    {
                        network = scanResults[num];


                        switch (key)
                        {
                        case "ssid":
                            network.ssid = StripQuotes(value);
                            break;

                        case "mode":
                            network.mode = int.Parse(value);
                            break;

                        case "security":
                            network.security = int.Parse(value);
                            break;

                        default:
                            throw new Exception("unknown key type");
                        }
                    }
                }
            }

            return(scanResults);
        }

        string StripQuotes(string input)
        {
            return(input.Substring(1, input.Length - 2));
        }