#!/usr/bin/perl
#############################################################################
#
#		Copyright (c) 2007, Broadcom Corporation.
#		All rights reserved.
#		Confidential Property of Broadcom Corporation.
#
#  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
#  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
#  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
#
# $brcm_Workfile: bapi_thunks.pm $
# $brcm_Revision: 1 $
# $brcm_Date: 7/9/07 1:30p $
#
# File Description:
#
# Revision History:
#
# $brcm_Log: /BSEAV/api/build/proxy/bapi_thunks.pm $
# 
# 1   7/9/07 1:30p erickson
# PR32377: autogen the thunk layer
# 
#
#############################################################################
use strict;
use bapi_callbacks;

package bapi_thunks;

sub build_thunks
{
	my ($filename, @funcs) = @_;
	my $func;
	open FILE, ">$filename";

	print FILE "/*********************************\n";
	print FILE "*\n";
	print FILE "* This file is autogenerated. Rebuild with BSEAV/api/build/proxy/autogen.\n";
	print FILE "*\n";
	print FILE "* This file acquires b_lock for every function call into the Settop API. The actual impl of each function is remapped to _impl.\n";
	print FILE "*\n";
	print FILE "*********************************/\n";
	print FILE "#define BSETTOP_NO_API_THUNKS\n";
	print FILE "#include \"bsettop_impl.h\"\n\n";

	# generate prototypes for all the _impl functions
	for $func (@funcs) {
		# NOTE: If there's an exception and a function does not belong in the thunk layer, add it here.

		my $impl = $func->{PROTOTYPE};
		$impl =~ s/$func->{FUNCNAME}/$func->{FUNCNAME}_impl/;
		print FILE "$impl;\n";
	}
	
	# generate the actual thunk layer functions which call the impl functions
	for $func (@funcs) {
		my $params = $func->{PARAMS};
		my $param;
		
		# NOTE: If there's an exception and a function does not belong in the thunk layer, add it here.

		print FILE "$func->{PROTOTYPE}\n\{\n";
		if ($func->{RETTYPE} eq "void") {
			print FILE "  b_lock();\n";
			print FILE "  $func->{FUNCNAME}_impl(";
		}
		else {
			print FILE "  $func->{RETTYPE} result;\n";
			print FILE "  b_lock();\n";
			print FILE "  result = $func->{FUNCNAME}_impl(";
		}
		for $param (@$params) {
			print FILE "$param->{NAME}";
			if ($param != $params->[-1]) 
			{
				print FILE ", ";
			}
		}
		print FILE ");\n";
		print FILE "  b_unlock();\n";
		if ($func->{RETTYPE} eq "void") {
			print FILE "  return;\n";
		}
		else {
			print FILE "  return result;\n";
		}
		print FILE "}\n\n";
	}
	close FILE;
}

sub build_remapping
{
	my ($filename, @funcs) = @_;
	my $func;
	open FILE, ">$filename";

	print FILE "/*********************************\n";
	print FILE "*\n";
	print FILE "* This file is autogenerated. Rebuild with BSEAV/api/build/proxy/autogen.\n";
	print FILE "*\n";
	print FILE "* This file remaps every public Settop API function to _impl. This allows the Settop API to call its own public API without reacquiring b_lock.\n";
	print FILE "*\n";
	print FILE "*********************************/\n";

	for $func (@funcs) {
		# NOTE: If there's an exception and a function does not belong in the thunk layer, add it here.
		print FILE "#define $func->{FUNCNAME} $func->{FUNCNAME}_impl\n";
	}
	print FILE "\n";
	close FILE;
}


sub build_kernel_export
{
	my ($filename, @funcs) = @_;
	my $func;
	open FILE, ">$filename";

	print FILE "/*********************************\n";
	print FILE "*\n";
	print FILE "* This file is autogenerated. Rebuild with BSEAV/api/build/proxy/autogen.\n";
	print FILE "*\n";
	print FILE "*********************************/\n";
	print FILE "#include \"bsettop.h\"\n";
	print FILE "#include <linux/config.h>\n";
	print FILE "#include <linux/module.h>\n";

	for $func (@funcs) {
		# NOTE: If there's an exception and a function does not belong in the thunk layer, add it here.
		print FILE "EXPORT_SYMBOL($func->{FUNCNAME});\n";
	}
	print FILE "\n";
	close FILE;
}

1;
