pndevmapperd: support multiple charge devices
[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 ( name, O_WRONLY /*O_RDONLY*/ ) ) < 0 ) {
17     return ( 0 );
18   }
19
20   if ( write ( f, v, strlen ( v ) ) < strlen ( v ) ) {
21     close ( f );
22     return ( 0 );
23   }
24
25   close ( f );
26
27   return ( 1 );
28 }
29
30 unsigned char pnd_device_open_read_close ( char *name, char *r_buffer, unsigned int buffer_len ) {
31   FILE *f;
32
33   f = fopen ( name, "r" );
34
35   if ( ! f ) {
36     return ( 0 );
37   }
38
39   if ( ! fgets ( r_buffer, buffer_len, f ) ) {
40     fclose ( f );
41     return ( 0 );
42   }
43
44   fclose ( f );
45
46   return ( 1 );
47 }
48
49 unsigned char pnd_device_set_clock ( unsigned int c ) {
50   char buffer [ 100 ];
51
52   sprintf ( buffer, "%u", c );
53
54   return ( pnd_device_open_write_close ( PND_DEVICE_PROC_CLOCK, buffer ) );
55 }
56
57 unsigned int pnd_device_get_clock ( void ) {
58   char buffer [ 100 ];
59
60   if ( pnd_device_open_read_close ( PND_DEVICE_PROC_CLOCK, buffer, 100 ) ) {
61     return ( atoi ( buffer ) );
62   }
63
64   return ( 0 );
65 }
66
67 unsigned char pnd_device_set_backlight ( unsigned int c ) {
68   char buffer [ 100 ];
69
70   sprintf ( buffer, "%u", c );
71
72   return ( pnd_device_open_write_close ( PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS, buffer ) );
73 }
74
75 unsigned int pnd_device_get_backlight ( void ) {
76   char buffer [ 100 ];
77
78   if ( pnd_device_open_read_close ( PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS, buffer, 100 ) ) {
79     return ( atoi ( buffer ) );
80   }
81
82   return ( 0 );
83 }
84
85 int pnd_device_get_battery_gauge_perc ( void ) {
86   char buffer [ 100 ];
87
88   if ( pnd_device_open_read_close ( PND_DEVICE_BATTERY_GAUGE_PERC, buffer, 100 ) ) {
89     return ( atoi ( buffer ) );
90   }
91
92   return ( -1 );
93 }
94
95 unsigned char pnd_device_get_charge_current ( int *result ) {
96   char buffer [ 100 ];
97
98   if ( pnd_device_open_read_close ( PND_DEVICE_CHARGE_CURRENT, buffer, 100 ) ) {
99     *result = atoi ( buffer );
100     return ( 1 );
101   }
102
103   return ( 0 );
104 }
105
106 int pnd_device_get_charger_enable ( const char *devices ) {
107   char fullname [ 100 ];
108   char buffer [ 100 ];
109
110   while ( 1 ) {
111     if ( sscanf ( devices, "%99s", buffer ) != 1 ) {
112       break;
113     }
114
115     while ( isspace ( *devices ) )
116       devices++;
117     devices += strlen ( buffer );
118     snprintf ( fullname, sizeof ( fullname ), PND_DEVICE_POWER_BASE "/%s/enable", buffer );
119
120     /* XXX: only ckecks first good device, but that should be enough for our needs */
121     if ( pnd_device_open_read_close ( fullname, buffer, 100 ) ) {
122       return ( atoi ( buffer ) );
123     }
124   }
125
126   return ( -1 );
127 }
128
129 unsigned char pnd_device_set_charger_enable ( const char *devices, unsigned char v ) {
130   char fullname [ 100 ];
131   char buffer [ 100 ];
132   int ret = 0;
133
134   while ( 1 ) {
135     if ( sscanf ( devices, "%99s", buffer ) != 1 ) {
136       break;
137     }
138
139     while ( isspace ( *devices ) )
140       devices++;
141     devices += strlen ( buffer );
142     snprintf ( fullname, sizeof ( fullname ), PND_DEVICE_POWER_BASE "/%s/enable", buffer );
143
144     sprintf ( buffer, "%u", v );
145     ret |= pnd_device_open_write_close ( fullname, buffer );
146   }
147
148   return ( ret );
149 }
150
151 unsigned char pnd_device_set_led_power_brightness ( unsigned char v ) {
152   char buffer [ 100 ];
153
154   sprintf ( buffer, "%u", v );
155
156   return ( pnd_device_open_write_close ( PND_DEVICE_LED_POWER PND_DEVICE_LED_SUFFIX_BRIGHTNESS, buffer ) );
157 }
158
159 unsigned char pnd_device_set_led_charger_brightness ( unsigned char v ) {
160   char buffer [ 100 ];
161
162   sprintf ( buffer, "%u", v );
163
164   return ( pnd_device_open_write_close ( PND_DEVICE_LED_CHARGER PND_DEVICE_LED_SUFFIX_BRIGHTNESS, buffer ) );
165 }