/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "kbluetoothutils.h"
#include <QString>

quint32 KBluetoothUtils::stringToType(const QString& stringType)
{
    if(stringType == "any") {
        return KBluetoothUtils::BLUETOOTH_TYPE_ANY;
    } else if(stringType == "mouse") {
        return KBluetoothUtils::BLUETOOTH_TYPE_MOUSE;
    } else if(stringType == "keyboard") {
        return KBluetoothUtils::BLUETOOTH_TYPE_KEYBOARD;
    } else if(stringType == "headset") {
        return KBluetoothUtils::BLUETOOTH_TYPE_HEADSET;
    } else if(stringType == "headphones") {
        return KBluetoothUtils::BLUETOOTH_TYPE_HEADPHONES;
    } else if(stringType == "audio") {
        return KBluetoothUtils::BLUETOOTH_TYPE_OTHER_AUDIO;
    } else if(stringType == "printer") {
        return KBluetoothUtils::BLUETOOTH_TYPE_PRINTER;
    } else if(stringType == "network") {
        return KBluetoothUtils::BLUETOOTH_TYPE_NETWORK;
    }

    return KBluetoothUtils::BLUETOOTH_TYPE_ANY;
}

quint32 KBluetoothUtils::classToType(quint32 classNum)
{
    switch((classNum & 0x1f00) >> 8) {
    case 0x01:
        return KBluetoothUtils::BLUETOOTH_TYPE_COMPUTER;
    case 0x02:
        switch((classNum & 0xfc) >> 2) {
        case 0x01:
        case 0x02:
        case 0x03:
        case 0x05:
            return KBluetoothUtils::BLUETOOTH_TYPE_PHONE;
        case 0x04:
            return KBluetoothUtils::BLUETOOTH_TYPE_MODEM;
        }
        break;
    case 0x03:
        return KBluetoothUtils::BLUETOOTH_TYPE_NETWORK;
    case 0x04:
        switch((classNum & 0xfc) >> 2) {
        case 0x01:
        case 0x02:
            return KBluetoothUtils::BLUETOOTH_TYPE_HEADSET;
        case 0x06:
            return KBluetoothUtils::BLUETOOTH_TYPE_HEADPHONES;
        default:
            return KBluetoothUtils::BLUETOOTH_TYPE_OTHER_AUDIO;
        }
        break;
    case 0x05:
        switch((classNum & 0xc0) >> 6) {
        case 0x00:
            switch((classNum & 0x1e) >> 2) {
            case 0x01:
            case 0x02:
                return KBluetoothUtils::BLUETOOTH_TYPE_JOYPAD;
            }
            break;
        case 0x01:
            return KBluetoothUtils::BLUETOOTH_TYPE_KEYBOARD;
        case 0x02:
            switch((classNum & 0x1e) >> 2) {
            case 0x05:
                return KBluetoothUtils::BLUETOOTH_TYPE_TABLET;
            default:
                return KBluetoothUtils::BLUETOOTH_TYPE_MOUSE;
            }
        }
        break;
    case 0x06:
        if(classNum & 0x80)
            return KBluetoothUtils::BLUETOOTH_TYPE_PRINTER;
        if(classNum & 0x20)
            return KBluetoothUtils::BLUETOOTH_TYPE_CAMERA;
        break;
    }

    return 0;
}
