/*
  Copyright (c) 2004,   Beijing Senselock Data Security Centre
  All rights reserved.
  
  File£ºsample_23_hw.c
  
  Briefs£ºThis code will 
    
*/

#include <string.h>
#include "..\..\inc\hardware\ses_v3.h"
#include "..\inc\sample_23.h"

#define _DEBUG

/* global variable defination. */
unsigned char last_ses_error = 0; /* last SES error */
IO_PACKAGE out_pkg;  /* output package */
IO_PACKAGE *output = &out_pkg; 
IO_PACKAGE *input = (IO_PACKAGE *)pbInBuff; /* input package */

/*
	Exit upon an error.
  
	Parameters:
		errcode[in]: error code
		msg[in]: additional error message
		msglen[in]: message length
  
	Return:
		none.

	Remarks:
		none.
*/
void error_exit(unsigned char errcode, unsigned char *msg, int msglen)
{
	output->tag = errcode;
	output->len = 0;
	
	/* for debug purpose only */
#ifdef  _DEBUG
	if (msglen != 0 && msg != NULL)
	{
		memcpy(output->buff, msg, msglen);
		output->len = msglen;
	}
#endif

	_set_response(2+output->len, (unsigned char *)output);
	_exit();
}

float calc_tri_side(float side1, float side2, float radian)
{
	
	return _sqrtf(_subf(_addf(_powf(side1, 2),_powf(side2, 2)),
	              _multf(2,_multf(_multf(side1, side2),_cosf(radian)))));
}

#define PI 3.14159
void main()
{
	unsigned char ret = 0;
	int length = 0;
	DATA_BLOCK *dblk = (DATA_BLOCK *)input->buff;
    float result = 0;
	float radian = 0;

	LE32_TO_CC(&dblk->side1);
	LE32_TO_CC(&dblk->side2);
	LE32_TO_CC(&dblk->angle);
    radian = _divf(_multf(dblk->angle,PI), 180);// from angle to radian 

	result = calc_tri_side(dblk->side1, dblk->side2, radian);

	length = sizeof(result);
	CC_TO_LE32(&result);

	ret = _mem_copy(&output->buff, &result, length);
	if (ret != SES_SUCCESS)
	{
		error_exit(ERR_SES, &ret, 1);
	}


	output->tag = ERR_SUCCESS;
	output->len = (unsigned char)length;
	_set_response(IO_PACKAGE_HEADER_SIZE+output->len, (unsigned char *)output);
	_exit();  
}
