Ticket #6138: 136-hdhr.conversion.fixes.3.patch

File 136-hdhr.conversion.fixes.3.patch, 9.2 KB (added by anonymous, 15 years ago)
  • mythtv/libs/libmythtv/dbcheck.cpp

    diff -p -r -u -N -X /tmp/diff.exclude.2720 -x release.20697.0614base -x release.20697.0614c release.20697.0614base/mythtv/libs/libmythtv/dbcheck.cpp release.20697.0614c/mythtv/libs/libmythtv/dbcheck.cpp
     
    11#include <qsqldatabase.h>
    22#include <qstring.h>
     3#include <qregexp.h>
    34
    45#include <iostream>
    56using namespace std;
    using namespace std; 
    1213#include "dbutil.h"
    1314#include "videodisplayprofile.h" // for "1214"
    1415
     16// HDHomeRun headers
     17#ifdef USING_HDHOMERUN
     18#include "hdhomerun_includes.h"
     19#endif
     20
     21
    1522#define MINIMUM_DBMS_VERSION 5
    1623
    1724/// This is the DB schema version expected by the running MythTV instance.
    18 const QString currentDatabaseVersion = "1215";
     25const QString currentDatabaseVersion = "1216";
    1926
    2027static bool UpdateDBVersionNumber(const QString &newnumber);
    2128static bool performActualUpdate(const QString updates[], QString version,
    thequery, 
    36343641            return false;
    36353642    }
    36363643
     3644    if (dbver == "1215")
     3645    {
     3646#ifdef USING_HDHOMERUN
     3647        VERBOSE(VB_IMPORTANT, "In 1215 upg (HDhomerun tunerid change)");
     3648
     3649        // First discover all HDhomreun devices on the network
     3650        // This will be cached and used to match up with the database query
     3651
     3652        uint32_t  target_ip   = 0; // WILDCARD
     3653        uint32_t  device_type = HDHOMERUN_DEVICE_TYPE_TUNER;
     3654        uint32_t  device_id   = HDHOMERUN_DEVICE_ID_WILDCARD;
     3655        const int max_count   = 50;
     3656        hdhomerun_discover_device_t hdhr_device_list[max_count];
     3657
     3658        int hdhr_device_count = hdhomerun_discover_find_devices_custom(
     3659                                  target_ip,
     3660                                  device_type,
     3661                                  device_id,
     3662                                  hdhr_device_list,
     3663                                  max_count);
     3664
     3665        if (hdhr_device_count == -1)
     3666        {
     3667            // Can only check for existing devices
     3668            VERBOSE(VB_IMPORTANT, "Error finding HDHomerun devices");
     3669            VERBOSE(VB_IMPORTANT, "All configured HDHomerun devices must be accessible");
     3670            return false;
     3671        }
     3672
     3673        MSqlQuery query(MSqlQuery::InitCon());
     3674        query.prepare("SELECT cardid, videodevice, dbox2_port "
     3675                      "FROM capturecard "
     3676                      "WHERE cardtype = 'HDHOMERUN' "
     3677                      "ORDER BY cardid");
     3678        bool ok = query.exec();
     3679
     3680        MSqlQuery query2(MSqlQuery::InitCon());
     3681        QRegExp newstyle = QRegExp("[0-9A-Z]{8}-[0-9]", false);
     3682        QRegExp newwildcard = QRegExp("F{8}-[0-9]", false); // "FFFFFFFF-n"
     3683
     3684        while (ok && query.next())
     3685        {
     3686            uint    cardid   = query.value(0).toUInt();
     3687            QString device   = query.value(1).toString();
     3688            uint    tunerid  = query.value(2).toUInt();
     3689
     3690            // First check if this is the new style already
     3691            if (device.contains(newstyle) > 0)
     3692            {
     3693                QString new_device = "";
     3694                if (device.contains(newwildcard) > 0) // FFFFFFFF-1
     3695                {
     3696                    // Must convert to an actual HDHR
     3697                    // Use the first HDHR found.
     3698
     3699                    QString new_device_id = QString("%1").arg(hdhr_device_list[0].device_id, 8, 16);
     3700                    new_device = device;
     3701                    new_device.replace("ffffffff", new_device_id, false);
     3702                } else if (device.upper() == device)
     3703                    VERBOSE(VB_GENERAL, QString("Retaining card %1: HDhomerun %2")
     3704                                               .arg(cardid).arg(device));
     3705                else
     3706                {
     3707                    // Convert to upper case
     3708                    new_device = device;
     3709                }
     3710
     3711                if (new_device.length() > 0)
     3712                {
     3713                    QString updatequery = QString("UPDATE capturecard SET videodevice = '%1' "
     3714                                                  "WHERE cardid = %2;")
     3715                                                 .arg(new_device.upper())
     3716                                                 .arg(cardid);
     3717                    VERBOSE(VB_GENERAL, QString("Converting card %1: HDhomerun %2 to %3")
     3718                                               .arg(cardid).arg(device).arg(new_device.upper()));
     3719
     3720                    if (!query2.exec(updatequery))
     3721                    {
     3722                       QString msg =
     3723                             QString("DB Error (Performing database upgrade): \n"
     3724                                     "Query was: %1 \nError was: %2")
     3725                                    .arg(updatequery)
     3726                                    .arg(MythContext::DBErrorMessage(query2.lastError()));
     3727                        VERBOSE(VB_IMPORTANT, msg);
     3728                        ok = false;
     3729                    }
     3730                }
     3731            }
     3732            else
     3733            {
     3734                // change from device, tuner to device-tuner
     3735                // i.e.:  AABBCCDD, 1 -> AABBCCDD-1
     3736                // If device is xxxxxxxx then use it directly
     3737                // If device is FFFFFFFF then try to discover the HDHR to get the actual value
     3738                // If device is x.x.x.x (ip address) then try to discover the HDHR to get the actual value
     3739
     3740                bool    valid;
     3741                uint    device_id = device.toUInt(&valid, 16);
     3742
     3743                QString new_device = "";
     3744                if (valid && device_id != HDHOMERUN_DEVICE_ID_WILDCARD
     3745                          && hdhomerun_discover_validate_device_id(device_id))
     3746                {
     3747                    // Valid, non-wildcard device id
     3748                    // Update it to "xxxxxxxx-#"
     3749                    new_device = QString("%1-%2").arg(device).arg(tunerid);
     3750   
     3751                }
     3752                else if (valid && device_id == HDHOMERUN_DEVICE_ID_WILDCARD)
     3753                {
     3754                    // Use the first HDHR found.  It should be the only one if this
     3755                    // worked before.
     3756
     3757                    new_device = QString("%1-%2")
     3758                                         .arg(hdhr_device_list[0].device_id, 8, 16)
     3759                                         .arg(tunerid);
     3760                }
     3761                else
     3762                {
     3763
     3764                    // Check for IP address;
     3765
     3766                    struct in_addr address;
     3767                    uint tmp_device_ip;
     3768                    if (inet_aton(device, &address))
     3769                    {
     3770                        tmp_device_ip = ntohl(address.s_addr);
     3771                        int i;
     3772                        for (i = 0; i < hdhr_device_count; i++)
     3773                        {
     3774                            if (tmp_device_ip == hdhr_device_list[i].ip_addr)
     3775                            {
     3776                                new_device = QString("%1-%2")
     3777                                                     .arg(hdhr_device_list[1].device_id, 8, 16)
     3778                                                     .arg(tunerid);
     3779                                break;
     3780                            }
     3781                        }
     3782                    }
     3783                }
     3784   
     3785                // If we identified the HDhomerun device, update it.
     3786                // Otherwise delete it
     3787
     3788                QString updatequery;
     3789                if (new_device.length() > 0)
     3790                {
     3791                    updatequery = QString("UPDATE capturecard SET videodevice = '%1', dbox2_port = 31338 "
     3792                                          "WHERE cardid = %2;")
     3793                                         .arg(new_device.upper())
     3794                                         .arg(cardid);
     3795                    VERBOSE(VB_GENERAL, QString("Converting card %1: HDhomerun %2 tuner %3 to %4")
     3796                                               .arg(cardid).arg(device)
     3797                                               .arg(tunerid).arg(new_device.upper()));
     3798                }
     3799                else
     3800                {
     3801                    updatequery = QString("DELETE FROM capturecard "
     3802                                          "WHERE cardid = %1;")
     3803                                         .arg(cardid);
     3804                    VERBOSE(VB_IMPORTANT, QString("Couldn't find card %1: HDHomerun %2 tuner %3 - deleting")
     3805                                                .arg(cardid).arg(device).arg(tunerid));
     3806                }
     3807
     3808                if (!query2.exec(updatequery))
     3809                {
     3810                   QString msg =
     3811                         QString("DB Error (Performing database upgrade): \n"
     3812                                 "Query was: %1 \nError was: %2")
     3813                                .arg(updatequery)
     3814                                .arg(MythContext::DBErrorMessage(query2.lastError()));
     3815                    VERBOSE(VB_IMPORTANT, msg);
     3816                    ok = false;
     3817                }
     3818            }
     3819        }
     3820#endif // USING_HDHOMERUN
     3821        const QString updates[] = { "" };
     3822
     3823        if (!performActualUpdate(updates, "1216", dbver))
     3824            return false;
     3825        VERBOSE(VB_IMPORTANT, "DBCheck: done with HDhomerun upgrade");
     3826    }
    36373827
    36383828
    36393829//"ALTER TABLE cardinput DROP COLUMN preference;" in 0.22