aboutsummaryrefslogtreecommitdiffstats
path: root/sysfs.c
blob: 5a50b0e84f08f029436d3f7d7a84b60aad760b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*  This file is part of cputemp2maxfreq.

    Copyright (C) 2023-2024 pa4wdh

    cputemp2maxfreq is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License , or
    (at your option) any later version.

    cputemp2maxfreq is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with cputemp2maxfreq; see the file COPYING.  If not, see
    <http://www.gnu.org/licenses>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "debug.h"
#include "cputemp2maxfreq.h"

extern struct s_config config;

int sysfs_read_str(char *sysfs_file,char *buf,size_t bufsize)
{
 int sysfs_handle;
 ssize_t datalen;

 DEBUG1_SYSFS("Reading sysfs file %s\n",sysfs_file);

// Open the file
 sysfs_handle=open(sysfs_file,O_RDONLY);
 DEBUG2_SYSFS("Open returned %d\n",sysfs_handle);
 if (sysfs_handle<0) return -1;

// Read the content of the file
 datalen=read(sysfs_handle,buf,bufsize);
 HEXDUMP2_SYSFS(buf,datalen);
 close(sysfs_handle);
 buf[datalen]=0;

// Strip newline
 if (buf[datalen-1]=='\n')
 {
  DEBUG2_SYSFS("Newline stripped\n");
  datalen--;
  buf[datalen]=0;
 }

 return (int) datalen;
}

long int sysfs_read_long_int(char *sysfs_file)
{
 char buf[255];
 int datalen;

// Read data
 datalen=sysfs_read_str(sysfs_file,buf,sizeof(buf));
 if (datalen<0) return 0;

// Convert to long int
 return strtoll((char*)&buf,NULL,10);
}

int sysfs_write_str(char *sysfs_file,char *value,long int checkdelay)
{
 int sysfs_handle;
 ssize_t len;
 char buf[255];

 DEBUG1_SYSFS("Writing %s to sysfs file %s\n",value,sysfs_file);

// Open the file
 sysfs_handle=open(sysfs_file,O_WRONLY);
 DEBUG2_SYSFS("Open returned %d\n",sysfs_handle);
 if (sysfs_handle<0) return -1;

// Write the data
 len=write(sysfs_handle,value,strlen(value));
 if (len<0)
 {
  config.logger("Error %d occoured while writing to sysfs",errno);
 }

 close(sysfs_handle);
 DEBUG2_SYSFS("Written %zd bytes\n",len);

// Wait if instructed to
 if (checkdelay>0) usleep((unsigned int) checkdelay);

// Validate we actually set what we want to set
 sysfs_read_str(sysfs_file,buf,sizeof(buf));
 DEBUG2_SYSFS("Content is now %s\n",buf);

 if (strncmp(value,buf,255)!=0)
 {
  DEBUG2_SYSFS("Set failed\n");
  return -1;
 }
 DEBUG2_SYSFS("Set successfull\n");

 return 0;
}