/* Copyright 2009 Palm, Inc.  All rights reserved. */

var COD_SERVICE_MASK = 0xffe000;
var COD_MAJOR_MASK = 0x001f00;
var COD_MINOR_MASK = 0x0000fc;
	 	
// Major Service classes
var COD_LIMITED_DISCOVERABLE_MODE = 0x00002000;
var COD_NETWORKING = 0x00020000;
var COD_RENDERING = 0x00040000;
var COD_CAPTURING = 0x00080000;
var COD_OBJECT_TRANSFER = 0x00100000;
var COD_AUDIO = 0x00200000;
var COD_TELEPHONY = 0x00400000;
var COD_INFORMATION = 0x00800000;
var COD_SERVICE_ANY = 0x00ffE000;
	
// Major device classes
var COD_MAJOR_MISC = 0x00000000;
var COD_MAJOR_COMPUTER = 0x00000100;
var COD_MAJOR_PHONE = 0x00000200;
var COD_MAJOR_LAN = 0x00000300;
var COD_MAJOR_AUDIO = 0x00000400;
var COD_MAJOR_PERIPHERAL = 0x00000500;
var COD_MAJOR_IMAGING = 0x00000600;
var COD_MAJOR_WEARABLE = 0x00000700;
var COD_MAJOR_TOY = 0x00000800;
var COD_MAJOR_UNCLASSIFIED = 0x00001F00;
var COD_MAJOR_ANY = 0x00001F00;
var COD_MINOR_ANY = 0x000000FC;
			
// Minor Device Class - Computer Minor class
var COD_MINOR_COMP_UNCLASSIFIED = 0x00000000;
var COD_MINOR_COMP_DESKTOP = 0x00000004;
var COD_MINOR_COMP_SERVER = 0x00000008;
var COD_MINOR_COMP_LAPTOP = 0x0000000C;
var COD_MINOR_COMP_HANDHELD = 0x00000010;
var COD_MINOR_COMP_PALM = 0x00000014;
			
// Minor Device Class - Phone Minor class
var COD_MINOR_PHONE_UNCLASSIFIED = 0x00000000;
var COD_MINOR_PHONE_CELLULAR = 0x00000004;
var COD_MINOR_PHONE_CORDLESS = 0x00000008;
var COD_MINOR_PHONE_SMART = 0x0000000C;
var COD_MINOR_PHONE_MODEM = 0x00000010;
var COD_MINOR_PHONE_ISDN = 0x00000014;
		
// Minor Device Class - Audio Major class
var COD_MINOR_AUDIO_UNCLASSIFIED = 0x00000000;
var COD_MINOR_AUDIO_HEADSET = 0x00000004;
var COD_MINOR_AUDIO_HANDFREE = 0x00000008;
var COD_MINOR_AUDIO_MICROPHONE = 0x00000010;
var COD_MINOR_AUDIO_LOUDSPEAKER = 0x00000014;
var COD_MINOR_AUDIO_HEADPHONE = 0x00000018;
var COD_MINOR_AUDIO_PORTABLE_AUDIO = 0x0000001C;
var COD_MINOR_AUDIO_CAR_AUDIO = 0x00000020;
var COD_MINOR_AUDIO_SET_TOP_BOX = 0x00000024;
var COD_MINOR_AUDIO_HIFI_AUDIO = 0x00000028;
var COD_MINOR_AUDIO_VCR = 0x0000002C;
var COD_MINOR_AUDIO_VIDEO_CAMERA = 0x00000030;
var COD_MINOR_AUDIO_CAMCORDER = 0x00000034;
var COD_MINOR_AUDIO_VIDEO_MONITOR = 0x00000038;
var COD_MINOR_AUDIO_VIDEO_DISPLAY = 0x0000003C;
var COD_MINOR_AUDIO_VIDEO_CONFERENCE = 0x00000040;
var COD_MINOR_AUDIO_GAME_TOY = 0x00000048;
			
// Minor Device Class - Peripheral Major class
var COD_MINOR_PERIPHERAL_UNCLASSIFIED = 0x00000000;
var COD_MINOR_PERIPHERAL_KEYBOARD = 0x00000040;
var COD_MINOR_PERIPHERAL_MOUSE = 0x00000080;
var COD_MINOR_PERIPHERAL_COMBO = 0x000000B0;
		
// Minor sub Device Class - Peripheral Major class
var COD_MINOR_PERIPHERAL_JOYSTICK = 0x00000004;
var COD_MINOR_PERIPHERAL_GAMEPAD = 0x00000008;
var COD_MINOR_PERIPHERAL_REMOTE_CONTROL = 0x0000000C;
var COD_MINOR_PERIPHERAL_SENSING = 0x00000010;
var COD_MINOR_PERIPHERAL_DIGITIZER = 0x00000014;
var COD_MINOR_PERIPHERAL_CARD_READER = 0x00000018;
		
// Group: Minor Device Class - Imaging Major class (Select multiple is possible)
var COD_MINOR_IMAGING_UNCLASSIFIED = 0x00000000;
var COD_MINOR_IMAGING_DISPLAY = 0x00000010;
var COD_MINOR_IMAGING_CAMERA = 0x00000020;
var COD_MINOR_IMAGING_SCANNER = 0x00000040;
var COD_MINOR_IMAGING_PRINTER = 0x00000080;


//Other defines

var CARKIT_DEFAULT_CONN_TIMEOUT = 10000;

function isAudioDevice(cod) {
	return ((cod & COD_AUDIO) == COD_AUDIO && (cod & COD_MAJOR_AUDIO) == COD_MAJOR_AUDIO);
}

function isKeyboard(cod) {
	return ((cod & COD_MAJOR_PERIPHERAL) == COD_MAJOR_PERIPHERAL && (cod & COD_MINOR_PERIPHERAL_KEYBOARD) == COD_MINOR_PERIPHERAL_KEYBOARD);
}

function isHFGSupported(cod) {
	return (isAudioDevice(cod) && (cod & (COD_MINOR_AUDIO_HEADSET + COD_MINOR_AUDIO_HANDFREE))); 
}

function isA2DPSupported(cod) {
	return (isAudioDevice(cod) && (cod & COD_RENDERING)); 
}

function isOPPSupported(cod) {
	// For now, assume all carkits support OPP
	return isCarkit(cod); 
}

function isCarkit(cod) {
	return (isAudioDevice(cod) && (cod & COD_MINOR_AUDIO_HANDFREE));
}

function isHeadset(cod) {
	return (isAudioDevice(cod) && (cod & COD_MINOR_AUDIO_HEADSET));
}

function shouldAutoPairBeAttempted(cod, name) {
	if (!name || !cod)
		return false;
	// Don't autopair to Parrot DriveBlue
	if (name.substring(0, 9) == 'DriveBlue')
		return false;
	// Autopairing should also be attempted for some car kits/speakers
	if (name == 'Parrot MK6100' || name == 'SRS-BT100' || name == 'DR-BT22')
		return true;
	// Soundmatters A2DP speakers used for CES demo
	if (name.substring(0, 5) == 'Foxl-')
		return true;
	// Auto-pairing should be attempted for headsets
	if (isHeadset(cod))
		return true;
	return false;
}

function getIconFile(cod, name) {
	if (!cod)
		return "other";
		
	// Adjust the CoD for some devices
	if (name) {
		// Soundmatters A2DP speakers used for CES demo
		if (name.substring(0, 5) == 'Foxl-') 
			return "headset_a2dp";
		if (name == 'DR-BT22') 
			return "headset_a2dp";
		if (name == 'Motorola T505' || name == 'Motorola T605' || name == 'Motorola T305' || name == 'Motorola IHF1000')
			return "carkit";
		if (name == 'Jabra SP700')
			return "carkit";
		if (name.substring(0, 9) == 'ANYCOM HS')
			return 'headset';
		if (name == 'Jabra BT150' || name == 'Jabra BT160')
			return "headset";
		if (name == '9xxPlantronics')
			return "headset";
	}

	if (cod & COD_MAJOR_COMPUTER)
		return "computer";
	if (cod & COD_MAJOR_PHONE)
		return "phone";
	if (isAudioDevice(cod)) {
		if (isCarkit(cod))
			return "carkit";
		if (isHeadset(cod)) {
			if (isA2DPSupported(cod))
				return "headset_a2dp";
			return "headset";
		}
		if (isA2DPSupported(cod))
			return "a2dp";	
	}
		
	return "other";	
}

// After pairing, some car kits allow you to connect immediately while others need a delay
function postPairConnectDelay(name) {
	//For Future: Adjust timeout by carkit
    return CARKIT_DEFAULT_CONN_TIMEOUT;
}

	

