/*
  Copyright (c) 2004,   Beijing Senselock Data Security Centre
  All rights reserved.
  
  File£ºsample_18_hw.c
  
  Briefs£ºThis code ...    
          
          
  
*/
#include <string.h>
#include "..\..\inc\hardware\ses_v3.h"
#include "..\inc\sample_18.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 */


  
DEFINE_AT(unsigned char, less_than_60, 0x400, RAM_EXT);
DEFINE_AT(unsigned char, btw_60_80, 0x401, RAM_EXT);
DEFINE_AT(unsigned char, btw_80_90, 0x402, RAM_EXT);
DEFINE_AT(unsigned char, larger_than_90, 0x403, RAM_EXT);

/*
	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();
}


void reset_share_var()
{
	less_than_60 = 0; 
	btw_60_80 = 0;
	btw_80_90 = 0;
	larger_than_90 = 0;	
}

void main()
{
	int length = 0;
	
	int i = 0;
	
    switch (input->tag)
	{
		case CMD_RESET:
			reset_share_var();	
		break;
		case CMD_COUNT:
			for (; i< input->len; i++)
			{
				if (*(input->buff + i) > 100)
				{
					error_exit(ERR_INVALID_PARAMETER, NULL, 0);
				}
				if (*(input->buff + i) >= 90)
				{
					larger_than_90++;
				}
				else if ((*(input->buff + i) < 90) && (*(input->buff + i) >= 80))
				{
					btw_80_90++;
				}
				else if ((*(input->buff + i) < 80) && (*(input->buff + i) >= 60))
				{
					btw_60_80++;
				}
				else if (*(input->buff + i) < 60) 		
				{
					less_than_60++;
				}
			}
		break;
		case CMD_GET:
			length = 4;
			output->buff[0] = larger_than_90;
			output->buff[1] = btw_80_90;
			output->buff[2] = btw_60_80;
			output->buff[3] = less_than_60;
			break;
		default:
			error_exit(ERR_INVALID_PARAMETER, NULL, 0);
			
	}
	output->tag = ERR_SUCCESS;
	output->len = (unsigned char)length;
	_set_response(IO_PACKAGE_HEADER_SIZE+output->len, (unsigned char *)output);
	_exit();  
}
