forked from 47-studio-org/openbmc-acm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacm_pwm.c
103 lines (91 loc) · 3.14 KB
/
acm_pwm.c
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
/*************************************************************************************
(C) Copyright (2012-2021) Hewlett PAckard Enterprise Development LP
\*************************************************************************************
File: acm_pwm.c
Objective:
Provide support for ACM(Apollo Chassis Manager) interface from user application/deamon.
Polling for packets from ACM.
Polling for FAN PWM value changes
Handler for SD-Bus client
**************************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <errno.h>
#include "acm.h"
extern ACM_BLK_INDX acm_idx[];
extern int acm_rd_req(unsigned short blk_num);
void *pwm_thread(void *vargp)
{
long int pwm_val=0x7f;
char *out_ptr;
unsigned char *ptr;
int input_fd, cnt, sz,i;
unsigned char buff[8];
unsigned char temp[32];
static long int last_pwm_val=0;
struct thread_info *tinfo = vargp;
printf("PWM thread starting!!!!!!!!!!!!!!!!!!!!!!!!\n");
cnt = 0;
do
{
for(i = 0; i < 3; i++)
{
memset(buff, 0, sizeof(buff));
strncpy(temp, ACM_PWM_IN, sizeof(temp));
buff[0] = '0' + i; //hwmonx x = 0-2
strncat(&buff[1], ACM_PWM_FILE, 6); //add /pwm0 file to the end
strncat(temp, buff, 7);
input_fd = open(temp, O_RDONLY);
if(input_fd >= 0)
break;
printf("Opening sysfs input interface for polling fan pwm: %s, r: %s\n",temp, strerror(errno));
sleep(2);
}
// cnt = cnt++%7;
}while(input_fd == -1); //wait forever
memset(buff, 0, sizeof(buff));
for(cnt=0;;)
{
sz = read(input_fd, buff, 3);
if(sz)
{
pwm_val = strtol(buff, &out_ptr, 10);
//timed out on poll but read anyway and look for new data
if(last_pwm_val != pwm_val)
{
printf("Found new pwm value: %lx\n", pwm_val);
ptr = (unsigned char *)acm_idx[ACM_FAN_PWM_BLK].blk_ptr;
*ptr++ = pwm_val; //primary
*ptr = pwm_val; //secondary
last_pwm_val = pwm_val;
acm_rd_req(ACM_FAN_PWM_BLK); //This will send data to acm
cnt = 0;
}
else
{
cnt++;
if(cnt > 256)
{
printf("Force PWM update\n");
last_pwm_val = 0;
}
}
}
else
{
printf("Error reading PWM value. sz: %x\n", sz);
}
lseek(input_fd, 0, SEEK_SET);
sleep(2);
}
if(input_fd)
close(input_fd);
return(NULL);
}