#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <ioctl.h>
#include <sys/asound.h>

#ifdef __USAGE
#endif

int setup(void) 
{
	int fd;
	snd_pcm_format_t pbformat;
	snd_pcm_playback_params_t pbparams;
	snd_pcm_record_params_t recparams;
	int pbtime=1;

	pbformat.rate =11025 ;
	pbformat.channels = 1;
	pbformat.format = SND_PCM_SFMT_S16_LE;

	pbparams.fragment_size = 1102; //((11025*(1*(16/8)))/20)
	pbparams.fragments_max = 1;
	pbparams.fragments_room = 1;

	recparams.fragment_size=pbparams.fragment_size;
	recparams.fragments_min = 1;

	if ((fd = open("/dev/pcm00", O_RDONLY)) >= 0 ) {
		if ( ioctl( fd, SND_PCM_IOCTL_RECORD_FORMAT, &pbformat) != -1) {
			 if ( ioctl( fd, SND_PCM_IOCTL_RECORD_PARAMS, &recparams) != -1){
				  if (ioctl(fd, SND_PCM_IOCTL_RECORD_TIME, &pbtime) != -1)
					   return fd;
				  else{
					   perror("Can't set time\n");
					   return -1;
				  }
			 } else {
				   perror("Can't set recorder param\n");
				   return -1;
			 }
		} else {
			 perror("Can't set record format\n");
			 return -1;
		}
	} else {
		 perror("open\n");
		 return -1;
	}
}

dump_data(unsigned char *data, int num )
{
    int j,i;
    unsigned char out;
		
    for( j = 0; j < num+16; j+=16) {
        for( i = j; i < j+16 ; i++ ) {
           if ( i < num )
              printf("%2.2x ",data[i]);
          else
              printf("   ");
      }
       for( i = j; i < j+16 && i < num; i++ ) {
           if ( data[i] < ' ' || data[i] >= 0x7f )
               out = '.';
            else
           out = data[i];
       printf("%c",out);
   }
   printf("\n");
}
																																													}
char buf[1102];

int main(int argc, char *argv[])
{
	int fd, n;	
	if ((fd = setup()) == -1)
		return -1;
	if ((n = read(fd, buf, sizeof(buf))) < 0 )
		perror("read");

	printf("read: %d bytes\n",n);
	dump_data(buf, sizeof(buf));
	return 0;
}                                      
