/*
 * Copyright (c) 2002, Peter Bentley
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *   Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 *   Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 *   The name Peter Bentley may not be used to endorse or promote
 * products derived from this software without specific prior
 * written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 *
 * ** omd_disc.c - Disc related functions
 */

#include "omd.h"
#include <string.h>

CVSID( "$Id: omd_disc.c,v 1.11 2002/11/24 19:45:30 pete Exp $" );

omd_disc_info_t *omd_disc_get_info( omd_unit_t *unit,
				    omd_disc_info_t *info)
{
    int		allocated = FALSE;
    
    if( unit == NULL )
    {
	slog( LOG_ERROR, "omd_disc_get_info: NULL unit" );
	return NULL;
    }

    /* Allocate info structure if needed */
    if( info == NULL )
	{
	info = malloc( sizeof( omd_disc_info_t ));
	if( info == NULL )
	{
	    slog( LOG_ERROR, "omd_disc_get_info: Malloc error" );
	    return NULL;
	}
	allocated = TRUE;
    }

    /* Zero out the info */
    memset( info, '\0', sizeof( omd_disc_info_t ));


    /* Get Disc flags */
    if( netmd_disc_flags( unit, &info->flags ) < 0 )
    {
	slog( LOG_ERROR, "Disc flags command failed" );
	goto errout;
    }
    slog( LOG_DEBUG, "Disk flags 0x%02x", info->flags );


    /* Get track count */
    if( netmd_track_count( unit, info->track_info,
			   OMD_TRACK_INFO_RAW_SIZE ) < 0 )
    {
	slog( LOG_ERROR, "Track count command failed" );
	goto errout;
    }
    /*
     * Decode raw track info ... format is
     * 00 10 00 02 00 nn
     * nn is track count, everything else unknown (const?)
     */
    if( info->track_info[4] != 0x00 )
    {
	slog( LOG_ERROR, "Unexpected raw track info" );
	goto errout;
    }
    info->track_count = (uint16) info->track_info[5];
    slog( LOG_DEBUG, "Disk has %d track(s)", info->track_count );

    

    /* Get disc capacity info */
    if( netmd_disc_capacity( unit, info->capacity_info,
			     OMD_DISC_CAPACITY_RAW_SIZE ) < 0 )
    {
	slog( LOG_ERROR, "Capacity info command failed" );
	goto errout;
    }
    /*
     *  Decode what we know... format is:-
     * 80 03 00 17 80 00 	Unknown (const?)
     * 00 05 00 TT TT TT TT     Time used (actual seconds of music)
     * 00 05 00 TT TT TT TT     Total disc size (if recorded in SP)
     * 00 05 00 TT TT TT TT     Available time (if recorded in SP)
     *
     * Time codings seem to be HH:MM:SS:FF in BCD (unlike track lengths!)
     *
     * do a vague sanity check first
     */
    if( info->capacity_info[0] != 0x80 || info->capacity_info[6] != 0 ||
	info->capacity_info[7] != 0x05 )
    {
	slog( LOG_ERROR, "Unexpected raw track info" );
	goto errout;
    }
    if((omd_time_from_bytes( &info->time_used, info->capacity_info + 9) < 0) ||
       (omd_time_from_bytes( &info->time_max, info->capacity_info +16) < 0 ) ||
       (omd_time_from_bytes( &info->time_avail, info->capacity_info+23) < 0 ))
    {
	slog( LOG_ERROR, "Time decode failure" );
	goto errout;
    }
    return info;

 errout:
    if( allocated )
    {
	free( info );
    }
    return NULL;
}


/*****************************************************************************
 * Extract the number of tracks from a discinfo structure (getting
 * a fresh discino structure if NULL
 */
int omd_disc_get_track_count( omd_unit_t *unit, omd_disc_info_t *discinfo )
{
    int ntrack;
    
    if( discinfo )
    {
	return discinfo->track_count;
    }

    if( unit == NULL )
    {
	slog( LOG_ERROR, "omd_toc_get_track_count: NULL unit passed in" );
	return -1;
    }
    
    discinfo = omd_disc_get_info( unit, NULL );
    if( discinfo == NULL )
    {
	slog( LOG_ERROR, "Unable to determine disc info" );
	return -1;
    }
    ntrack = discinfo->track_count;
    free( discinfo );

    return ntrack;
}

/*****************************************************************************
 * set the raw title of a disc - gets the old title first to count
 * it's length for the protocol
 */

int omd_disc_set_raw_title( omd_unit_t *unit, const uint8 *newtitle )
{
    uint8 oldtitle[OMD_RAW_DISC_TITLE_MAX + 1];

    /* Read raw disc title */
    if( netmd_disc_get_title( unit, oldtitle, OMD_RAW_DISC_TITLE_MAX ) < 0 )
    {
	slog( LOG_ERROR,"omd_disc_set_raw_title:Unable to retrieve old title");
	return -1;
    }

    if( netmd_disc_set_title( unit, strlen( oldtitle ), newtitle ) < 0 )
    {
	slog( LOG_ERROR, "Netmd set title failed" );
	return -1;
    }
    return 0;
}

