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

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

    diff -p -r -u -N -X /tmp/diff.exclude.8482 -x myth.20713.0616a -x myth.20713.0616b myth.20713.0616a/mythtv/libs/libmythtv/dbcheck.cpp myth.20713.0616b/mythtv/libs/libmythtv/dbcheck.cpp
    using namespace std; 
    1414#include "mythdb.h"
    1515#include "mythverbose.h"
    1616
     17// HDHomeRun headers
     18#ifdef USING_HDHOMERUN
     19#include "hdhomerun.h"
     20#endif
     21
    1722
    1823#define MINIMUM_DBMS_VERSION 5,0,15
    1924
    2025/// This is the DB schema version expected by the running MythTV instance.
    21 const QString currentDatabaseVersion = "1235";
     26const QString currentDatabaseVersion = "1236";
    2227
    2328static bool UpdateDBVersionNumber(const QString &newnumber);
    2429static bool performActualUpdate(
    NULL 
    45434548            return false;
    45444549    }
    45454550
     4551    if (dbver == "1235")
     4552    {
     4553#ifdef USING_HDHOMERUN
     4554        VERBOSE(VB_IMPORTANT, "In 1235 upg (HDhomerun tunerid change)");
     4555
     4556        // First discover all HDhomreun devices on the network
     4557        // This will be cached and used to match up with the database query
     4558
     4559        uint32_t  target_ip   = 0; // WILDCARD
     4560        uint32_t  device_type = HDHOMERUN_DEVICE_TYPE_TUNER;
     4561        uint32_t  device_id   = HDHOMERUN_DEVICE_ID_WILDCARD;
     4562        const int max_count   = 50;
     4563        hdhomerun_discover_device_t hdhr_device_list[max_count];
     4564
     4565        int hdhr_device_count = hdhomerun_discover_find_devices_custom(
     4566                                  target_ip,
     4567                                  device_type,
     4568                                  device_id,
     4569                                  hdhr_device_list,
     4570                                  max_count);
     4571
     4572        if (hdhr_device_count == -1)
     4573        {
     4574            // Can only check for existing devices
     4575            VERBOSE(VB_IMPORTANT, "Error finding HDHomerun devices");
     4576            VERBOSE(VB_IMPORTANT, "All configured HDHomerun devices must be accessible");
     4577            return false;
     4578        }
     4579
     4580        MSqlQuery query(MSqlQuery::InitCon());
     4581        query.prepare("SELECT cardid, videodevice, dbox2_port "
     4582                      "FROM capturecard "
     4583                      "WHERE cardtype = 'HDHOMERUN' "
     4584                      "ORDER BY cardid");
     4585        bool ok = query.exec();
     4586
     4587        MSqlQuery query2(MSqlQuery::InitCon());
     4588        QRegExp newstyle = QRegExp("[0-9A-Z]{8}-[0-9]", Qt::CaseInsensitive);
     4589        QRegExp newwildcard = QRegExp("F{8}-[0-9]", Qt::CaseInsensitive); // "FFFFFFFF-n"
     4590
     4591        while (ok && query.next())
     4592        {
     4593            uint    cardid  = query.value(0).toUInt();
     4594            QString device  = query.value(1).toString();
     4595            uint    tunerid = query.value(2).toUInt();
     4596
     4597            // First check if this is the new style already
     4598            if (device.contains(newstyle))
     4599            {
     4600                QString new_device = "";
     4601                if (device.contains(newwildcard)) // FFFFFFFF-1
     4602                {
     4603                    // Must convert to an actual HDHR
     4604                    // Use the first HDHR found.
     4605
     4606                    QString new_device_id = QString("%1").arg(hdhr_device_list[0].device_id, 8, 16);
     4607                    new_device = device;
     4608                    new_device.replace("ffffffff", new_device_id, Qt::CaseInsensitive);
     4609                } else if (device.toUpper() == device)
     4610                    VERBOSE(VB_GENERAL, QString("Retaining card %1: HDhomerun %2")
     4611                                               .arg(cardid).arg(device));
     4612                else
     4613                {
     4614                    // Convert to upper case
     4615                    new_device = device;
     4616                }
     4617
     4618                if (new_device.length() > 0)
     4619                {
     4620                    QString updatequery = QString("UPDATE capturecard SET videodevice = '%1' "
     4621                                                  "WHERE cardid = %2;")
     4622                                                 .arg(new_device.toUpper())
     4623                                                 .arg(cardid);
     4624                    VERBOSE(VB_GENERAL, QString("Converting card %1: HDhomerun %2 to %3")
     4625                                               .arg(cardid).arg(device).arg(new_device.toUpper()));
     4626
     4627                    if (!query2.exec(updatequery))
     4628                    {
     4629                        MythDB::DBError(
     4630                            "Could not perform update for '1236'", query2);
     4631                        ok = false;
     4632                    }
     4633                }
     4634            }
     4635            else
     4636            {
     4637
     4638                // change from device, tuner to device-tuner
     4639                // i.e.:  AABBCCDD, 1 -> AABBCCDD-1
     4640                // If device is xxxxxxxx then use it directly
     4641                // If device is FFFFFFFF then try to discover the HDHR to get the actual value
     4642                // If device is x.x.x.x (ip address) then try to discover the HDHR to get the actual value
     4643
     4644                bool    valid;
     4645                uint    device_id = device.toUInt(&valid, 16);
     4646
     4647                QString new_device = "";
     4648                if (valid && device_id != HDHOMERUN_DEVICE_ID_WILDCARD
     4649                          && hdhomerun_discover_validate_device_id(device_id))
     4650                {
     4651                    // Valid, non-wildcard device id
     4652                    // Update it to "xxxxxxxx-#"
     4653                    new_device = QString("%1-%2").arg(device).arg(tunerid);
     4654
     4655                }
     4656                else if (valid && device_id == HDHOMERUN_DEVICE_ID_WILDCARD)
     4657                {
     4658                    // Use the first HDHR found.  It should be the only one if this
     4659                    // worked before.
     4660
     4661                    new_device = QString("%1-%2")
     4662                                         .arg(hdhr_device_list[0].device_id, 8, 16)
     4663                                         .arg(tunerid);
     4664                }
     4665                else
     4666                {
     4667
     4668                    // Check for IP address;
     4669
     4670                    struct in_addr address;
     4671                    uint tmp_device_ip;
     4672                    if (inet_aton(device.toUtf8(), &address))
     4673                    {
     4674                        tmp_device_ip = ntohl(address.s_addr);
     4675                        int i;
     4676                        for (i = 0; i < hdhr_device_count; i++)
     4677                        {
     4678                            if (tmp_device_ip == hdhr_device_list[i].ip_addr)
     4679                            {
     4680                                new_device = QString("%1-%2")
     4681                                                     .arg(hdhr_device_list[1].device_id, 8, 16)
     4682                                                     .arg(tunerid);
     4683                                break;
     4684                            }
     4685                        }
     4686                    }
     4687                }
     4688
     4689                // If we identified the HDhomerun device, update it.
     4690                // Otherwise delete it
     4691
     4692                QString updatequery;
     4693                if (new_device.length() > 0)
     4694                {
     4695                    updatequery = QString("UPDATE capturecard SET videodevice = '%1', dbox2_port = 31338 "
     4696                                          "WHERE cardid = %2;")
     4697                                         .arg(new_device.toUpper())
     4698                                         .arg(cardid);
     4699                    VERBOSE(VB_GENERAL, QString("Converting card %1: HDhomerun %2 tuner %3 to %4")
     4700                                               .arg(cardid).arg(device)
     4701                                               .arg(tunerid).arg(new_device.toUpper()));
     4702                }
     4703                else
     4704                {
     4705                    updatequery = QString("DELETE FROM capturecard "
     4706                                          "WHERE cardid = %1;"
     4707                                          "AND dbox2_port = %2;")
     4708                                         .arg(cardid);
     4709                    VERBOSE(VB_IMPORTANT, QString("Couldn't find card %1: HDHomerun %2 tuner %3 - deleting")
     4710                                                 .arg(cardid).arg(device).arg(tunerid));
     4711                }
     4712   
     4713                if (!query2.exec(updatequery))
     4714                {
     4715                    MythDB::DBError(
     4716                        "Could not perform update for '1236'", query2);
     4717                    ok = false;
     4718                }
     4719            }
     4720        }
     4721
     4722        if (!ok)
     4723            return false;
     4724
     4725#endif // USING_HDHOMERUN
     4726        const char * updates[] = { NULL };
     4727
     4728        if (!performActualUpdate(updates, "1236", dbver))
     4729            return false;
     4730        VERBOSE(VB_IMPORTANT, "DBCheck: done with HDhomerun upgrade");
     4731    }
    45464732
    45474733    return true;
    45484734}