Refactor to use new protocol module and sipo.
[bertos.git] / examples / randpool / randpool_demo.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Test function for randpool API.
34  *
35  * This program return a rand number generate from randpool function.
36  * For use randpool function you need a souce of entropy. In this
37  * program you can choose from:
38  *      - /dev/random
39  *      - /dev/urandom
40  *      - /dev/input/mouse0
41  *      
42  * There are 3 mode of output:
43  *     - binmode: program generate a sequenze of random byte.
44  *     - Matrix of random number for octave program.
45  *     - Vector of random number for octave program.
46  * 
47  * \author Daniele Basile <asterix@develer.com>
48  */
49
50 /*#*
51  *#* $Log$
52  *#* Revision 1.1  2007/02/15 18:17:51  asterix
53  *#* Add randpool test program.
54  *#*
55  *#*/
56
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <cfg/compiler.h>
61 #include <drv/timer.h>
62 #include <algo/md2.h> 
63 #include <algo/randpool.h>
64 #include <unistd.h>
65
66 #define LEN 256      //Size of buffer containing a random number.
67 #define SAMPLE 1000  //Defoult number of byte that put in entropy pool.
68
69 int main (int argc, char *argv[])
70 {
71         EntropyPool pool;
72         uint8_t ch;
73         uint8_t buff[LEN];
74         FILE *pdev;
75         int opt = getopt (argc, argv, "murh");
76         int samp = SAMPLE;
77         int round = 10;
78         int mode = 0;
79         int  pass = 0;
80
81         timer_init();
82
83         randpool_init(&pool, NULL, 0); //Init a entropy pool.
84
85         /*
86          * Chose a source of entropy.
87          */
88         switch(opt)
89         {
90                 case 'm':
91                 {
92                         pdev = fopen("/dev/input/mouse0", "r");
93                         break;
94                 }
95                 case 'u':
96                 {
97                         pdev = fopen("/dev/urandom", "r");
98                         break;
99                 }
100                 case 'r':
101                 {
102                         pdev = fopen("/dev/random", "r");
103                         break;
104                 }
105                 case 'h':
106                 {
107                 }
108                 default:
109                 {
110                         printf("\n");   
111                         printf("\n");   
112                         printf("randpool_demo [OPTION] [SAMPLE] [ROUND] [MODE]\n");     
113                         printf("\n");   
114                         printf("OPTION:\n");    
115                         printf("  -r  /dev/random\n");  
116                         printf("  -u  /dev/urandom\n"); 
117                         printf("  -m  /dev/input/mouse0\n");    
118                         printf("\n");   
119                         printf("SAMPLE:\n");    
120                         printf("  num  number of entropy byte to put in etropy pool.\n");       
121                         printf("\n");   
122                         printf("ROUND:\n");     
123                         printf("  num  number call of randpool_get function.\n");       
124                         printf("\n");
125                         printf("MODE:\n");      
126                         printf("  0  binmode\n");       
127                         printf("  1  vector mode\n");   
128                         printf("  2  matrix mode\n");   
129                         printf("\n");
130                         printf("Test program of randpool API.\n");      
131                         printf("This program create an entropy pool of 256byte, and fill it\n");        
132                         printf("with entropy from a exsternal source. This source can be:\n");  
133                         printf("  - /dev/random (option: -r)\n");       
134                         printf("  - /dev/urandom (option: -u)\n");      
135                         printf("  - /dev/input/mouse0 (option: -m)\n"); 
136                         printf("\n");   
137                         printf("Once the pool is full, program call a randpool_get ROUND time,\n");     
138                         printf("printing on STDOUT random value, in accord with a select mode.\n");     
139                         printf("The mode are:\n");      
140                         printf("  - binmode: program print on STDOUT random byte. (option: 0 (defaul mode)\n"); 
141                         printf("  - vector mode: program print on STDOUT a ROUND vector of random value.\n");   
142                         printf("                 The output is format for octave program. (option: 1)\n");      
143                         printf("  - matrix mode: program print on STDOUT a matrix of random value.\n"); 
144                         printf("                 The output is format for octave program. (option: 2)\n");      
145                         printf("\n");
146                         exit(1);
147                 }
148
149         }
150
151         /*
152          *  
153          */
154         if(argc > 2)
155         {
156                 if(argv[2])
157                         samp = atoi(argv[2]);  //Number of byte take from entropy source
158                 if(argv[3])
159                         round = atoi(argv[3]); //Number of time we call randpool_get.
160                 if(argv[4])
161                 {       
162                         switch(atoi(argv[4]))
163                         {
164                                 case 1:
165                                 {
166                                         mode = 1;
167                                         printf("# Created by Asterix.\n");
168                                         printf("# name: __nargin__\n");
169                                         printf("# type: scalar\n");
170                                         printf("0\n");
171                                         break;
172                                 }
173                                 case 2:
174                                 {
175                                         mode = 2;
176                                         printf("# Created by Asterix.\n");
177                                         printf("# name: __nargin__\n");
178                                         printf("# type: scalar\n");
179                                         printf("0\n");
180                                         break;
181                                 }
182                                 default:
183                                 {
184                                         break;
185                                 }
186
187                         }
188
189                 }
190
191         }
192
193         /*
194          * Add byte to entropy pool from a source of entropy.
195          */
196         for(int i = 0; i < samp; i++)
197         {
198
199                 ch = fgetc(pdev);
200                 randpool_add(&pool, &ch, sizeof(ch));
201                 
202         }
203
204         fclose(pdev);
205
206         
207         for(int k = 0; k < round; k++)
208         {       
209                 switch(mode)
210                 {
211                         case 1:
212                         {
213                                 printf("\n");
214                                 printf("\n# name: vet%d",k);
215                                 printf("\n# type: matrix");
216                                 printf("\n# rows: 1");
217                                 printf("\n# columns: %d\n", LEN);
218                                 pass = 1;
219                                 break;
220                         }
221                         case 2:
222                         {
223                                 printf("\n");
224                                 printf("\n# name: randMatrix");
225                                 printf("\n# type: matrix");
226                                 printf("\n# rows: %d",round);
227                                 printf("\n# columns: %d\n", LEN);
228                                 pass = 1;
229                                 mode = 0;
230                                 break;
231                         }
232                 }
233
234                 randpool_get(&pool, buff, LEN);
235
236                 if(pass)
237                 {
238                         for(int j = 0; j < LEN; j++)
239                         {
240                                 printf("%d ", buff[j]);
241                         }
242                         printf("\n");
243                 }
244                 else
245                         fwrite(buff, sizeof(uint8_t), LEN, stdout);
246         }
247         
248
249 }
250