pihwm
A lightweight C library for Raspberry Pi hardware modules.
 All Data Structures Files Functions Groups Pages
pihwm.c
Go to the documentation of this file.
1 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <linux/types.h>
31 #include "pihwm.h"
32 
109 board_t
111 {
112  FILE *info;
113  char rev_hex[5];
114  unsigned int rev_int = 0;
115 
116  board_t board;
117  board.model = -1;
118  board.rev = -1;
119  board.mem = -1;
120 
121  char *cmd = "cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}'";
122 
123  if( !(info = popen(cmd, "r")) )
124  {
125  return board;
126  }
127 
128  fgets(rev_hex, 5, info);
129  sscanf(rev_hex, "%x", &rev_int);
130 
131  // Data from: http://raspberryalphaomega.org.uk/?p=428
132  switch(rev_int){
133  case 2:
134  case 3:
135  board.model = MODEL_B;
136  board.rev = REV_1;
137  board.mem = MEM_256;
138  break;
139 
140  case 4:
141  case 5:
142  case 6:
143  board.model = MODEL_B;
144  board.rev = REV_2;
145  board.mem = MEM_256;
146  break;
147 
148  case 7:
149  case 8:
150  case 9:
151  board.model = MODEL_A;
152  board.rev = REV_2;
153  board.mem = MEM_256;
154  break;
155 
156  case 13:
157  case 14:
158  case 15:
159  board.model = MODEL_B;
160  board.rev = REV_2;
161  board.mem = MEM_512;
162  break;
163 
164  default:
165  // Default values (-1) already set.
166  break;
167  }
168 
169  return board;
170 
171 }
172 
178 int
180 {
181  board_t b = board_info();
182  return b.model;
183 }
184 
190 int
192 {
193  board_t b = board_info();
194  return b.rev;
195 }
196 
202 int
204 {
205  board_t b = board_info();
206  return b.mem;
207 }
208 
216 int
217 check_kernel_module (char* modulename)
218 {
219  FILE *lsmod;
220  char cmd[100];
221  char modcount[2];
222  unsigned int modcount_int = 0;
223 
224  sprintf(cmd, "lsmod | grep %s | wc -l", modulename);
225 
226  if( !(lsmod = popen(cmd, "r")) )
227  {
228  return -1;
229  }
230 
231  fgets(modcount, 2, lsmod);
232  modcount_int = atoi(modcount);
233 
234  if ( modcount_int > 0 )
235  {
236  return 1;
237  }
238  else
239  {
240  return -1;
241  }
242 
243 }
244