Merge branch 'master' of git://git.openpandora.org/pandora-libraries
[pandora-libraries.git] / lib / pnd_device.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h>
5
6 #include <sys/types.h> /* for open */
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10
11 #include "pnd_device.h"
12
13 unsigned char pnd_device_open_write_close ( char *name, char *v ) {
14   int f;
15
16   if ( ( f = open ( PND_DEVICE_PROC_CLOCK, O_WRONLY /*O_RDONLY*/ ) ) < 0 ) {
17     return ( 0 );
18   }
19
20   if ( write ( f, buffer, strlen ( buffer ) ) < strlen ( buffer ) ) {
21     return ( 0 );
22   }
23
24   close ( f );
25
26   return ( 1 );
27 }
28
29 unsigned char pnd_device_open_read_close ( char *name, char *r_buffer, unsigned int buffer_len ) {
30   FILE *f;
31
32   f = fopen ( name, "r" );
33
34   if ( ! f ) {
35     return ( 0 );
36   }
37
38   if ( ! fgets ( r_buffer, buffer_len, f ) ) {
39     fclose ( f );
40     return ( 0 );
41   }
42
43   fclose ( f );
44
45   return ( 1 );
46 }
47
48 unsigned char pnd_device_set_clock ( unsigned int c ) {
49   char buffer [ 100 ];
50
51   sprint ( buffer, "%u", c );
52
53   return ( pnd_device_open_write_close ( PND_DEVICE_PROC_CLOCK, buffer ) );
54 }
55
56 unsigned int pnd_device_get_clock ( void ) {
57   char buffer [ 100 ];
58
59   if ( pnd_device_open_read_close ( PND_DEVICE_PROC_CLOCK, buffer, 100 ) ) {
60     return ( atoi ( buffer ) );
61   }
62
63   return ( 0 );
64 }
65
66 unsigned char pnd_device_set_backlight ( unsigned int c ) {
67   char buffer [ 100 ];
68
69   sprint ( buffer, "%u", c );
70
71   return ( pnd_device_open_write_close ( PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS, buffer ) );
72 }
73
74 unsigned int pnd_device_get_backlight ( void ) {
75   char buffer [ 100 ];
76
77   if ( pnd_device_open_read_close ( PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS, buffer, 100 ) ) {
78     return ( atoi ( buffer ) );
79   }
80
81   return ( 0 );
82 }