Hongwei Zhang | 90cb34f | 2019-05-29 19:06:33 -0400 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Pwmtachtool Application |
| 4 | * This application provides functions to get/set fan speed / PWM dutycycle. |
| 5 | * Copyright (C) <2019> <American Megatrends International LLC> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <errno.h> |
| 12 | #include <string.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <ctype.h> |
| 15 | #include <unistd.h> |
| 16 | #include <sys/select.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/poll.h> |
| 19 | #include <sys/time.h> |
| 20 | #include <stdint.h> |
| 21 | #include <limits.h> |
| 22 | #include "libpwmtach.h" |
| 23 | |
| 24 | #define VERSION_STR "1.0" |
| 25 | typedef enum { |
| 26 | SET_FAN_SPEED, |
| 27 | GET_FAN_SPEED, |
| 28 | SET_PWM_DUTYCYCLE, |
| 29 | SET_PWM_DUTYCYCLE_VALUE, |
| 30 | GET_PWM_DUTYCYCLE, |
| 31 | END_OF_FUNCLIST |
| 32 | }ePwmTachactions; |
| 33 | |
| 34 | |
| 35 | ePwmTachactions action = END_OF_FUNCLIST; |
| 36 | |
| 37 | static int verbose = 0; |
| 38 | |
| 39 | static void ShowUsage ( void ) |
| 40 | /*@globals fileSystem@*/ |
| 41 | /*@modifies fileSystem@*/ |
| 42 | { |
| 43 | printf ("PWMTACH Test Tool (Version %s)\n",VERSION_STR); |
| 44 | printf ("Copyright (c) 2009-2015 American Megatrends Inc.\n"); |
| 45 | printf( "Usage : pwmtachtool <device_id> <command-option> <fannum>\n" ); |
| 46 | printf( "\t--set-fan-speed: Set Fan's speed. Takes the RPM value as the last argument\n" ); |
| 47 | printf("\t\tparameters: <Fan_Number> <Fan_Speed>\n"); |
| 48 | printf( "\t--set-pwm-dutycycle: Set Fan's dutycycle. dutycycle_percentage value should be between 1 to 100\n" ); |
| 49 | printf( "\t--set-pwm-dutycycle-value: Set Fan's dutycycle. dutycycle_value should be between 0 to 255\n" ); |
| 50 | printf("\t\tparameters: <pwm_number> <dutycycle value>\n"); |
| 51 | printf( "\t--get-pwm-dutycycle: Get Fan's dutycycle\n"); |
| 52 | printf( "\t--get-fan-speed: Get Fan's speed\n" ); |
| 53 | printf( "\t--verbose: Enable Debug messages\n" ); |
| 54 | printf( "\n" ); |
| 55 | } |
| 56 | |
| 57 | static void Verbose ( char * msg ) |
| 58 | { |
| 59 | if (verbose ) printf ( "%s\n" , msg ); |
| 60 | } |
| 61 | |
| 62 | static int process_arguments( int argc, char **argv, |
| 63 | unsigned char* fan_num,unsigned int* rpm_value, |
| 64 | unsigned int* dev_id ) |
| 65 | { |
| 66 | int i = 1; |
| 67 | |
| 68 | if (argc < 3) |
| 69 | { |
| 70 | printf("need Device Name and Command to process request\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | *dev_id = (unsigned char)strtol( argv[ i++ ], NULL, 10); |
| 75 | |
| 76 | if( strcmp( argv[ i ], "--set-fan-speed" ) == 0 ) |
| 77 | { |
| 78 | if (argc < 5) |
| 79 | { |
| 80 | printf("need Fan Number and RPM value to process request\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | *fan_num = (unsigned char)strtol( argv[ ++i ], NULL, 10); |
| 84 | *rpm_value = (unsigned int)strtol( argv[ ++i ], NULL, 10); |
| 85 | action = SET_FAN_SPEED; |
| 86 | } |
| 87 | else if( strcmp( argv[ i ], "--set-pwm-dutycycle" ) == 0 ) |
| 88 | { |
| 89 | if (argc < 5) |
| 90 | { |
| 91 | printf("need Fan Number and Dutycycle value to process request\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | *fan_num = (unsigned char)strtol( argv[ ++i ], NULL, 10); |
| 95 | *rpm_value = (unsigned int)strtol( argv[ ++i ], NULL, 10); |
| 96 | action = SET_PWM_DUTYCYCLE; |
| 97 | } |
| 98 | else if( strcmp( argv[ i ], "--set-pwm-dutycycle-value" ) == 0 ) |
| 99 | { |
| 100 | if (argc < 5) |
| 101 | { |
| 102 | printf("need Fan Number and Dutycycle value to process request\n"); |
| 103 | return -1; |
| 104 | } |
| 105 | *fan_num = (unsigned char)strtol( argv[ ++i ], NULL, 10); |
| 106 | *rpm_value = (unsigned int)strtol( argv[ ++i ], NULL, 10); |
| 107 | action = SET_PWM_DUTYCYCLE_VALUE; |
| 108 | } |
| 109 | else if( strcmp( argv[i], "--get-pwm-dutycycle" ) == 0) |
| 110 | { |
| 111 | if (argc < 4) |
| 112 | { |
| 113 | printf("need PWM Number to process request\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | *fan_num = (unsigned char)strtol( argv[ ++i ], NULL, 10); |
| 117 | action = GET_PWM_DUTYCYCLE; |
| 118 | } |
| 119 | |
| 120 | else if( strcmp( argv[ i ], "--get-fan-speed" ) == 0 ) |
| 121 | { |
| 122 | if (argc < 4) |
| 123 | { |
| 124 | printf("need more parameters to process request\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | *fan_num = (unsigned char)strtol( argv[ ++i ], NULL, 10); |
| 128 | action = GET_FAN_SPEED; |
| 129 | } |
| 130 | |
| 131 | else if( strcmp( argv[ i ], "--verbose" ) == 0 ) |
| 132 | verbose = 1; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int main ( int argc , char* argv [] ) |
| 138 | { |
| 139 | unsigned char fannum = 0, property_id = 0; |
| 140 | unsigned int rpmvalue = 0; |
| 141 | unsigned char dutycycle = 0; |
| 142 | int Value = 0; |
| 143 | int ret = 0; |
| 144 | unsigned int dev_id = 0; |
| 145 | |
| 146 | if (argc < 2) |
| 147 | { |
| 148 | ShowUsage(); |
| 149 | return 0; |
| 150 | } |
| 151 | ret = process_arguments( argc , argv , &fannum, &rpmvalue, &dev_id ); |
| 152 | if (ret != 0) |
| 153 | { |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | if (END_OF_FUNCLIST == action) |
| 158 | { |
| 159 | ShowUsage (); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | switch ( action ) |
| 164 | { |
| 165 | |
| 166 | case SET_FAN_SPEED: |
| 167 | Verbose ("Inside Set Fan Speed \n"); |
| 168 | Value = set_fan_speed (dev_id, fannum, rpmvalue); |
| 169 | if ( -1 == Value ) |
| 170 | { |
| 171 | printf ( "Set Fan Speed Failed \n"); |
| 172 | return -1; |
| 173 | } |
| 174 | printf ( "Fan Speed set Successfully\n"); |
| 175 | break; |
| 176 | case GET_FAN_SPEED: |
| 177 | Verbose ("Inside Get Fan Speed \n"); |
| 178 | Value = get_fan_speed (dev_id, fannum, &rpmvalue); |
| 179 | if ( -1 == Value) |
| 180 | { |
| 181 | printf ( "Get Fan Speed Failed \n"); |
| 182 | return -1; |
| 183 | } |
| 184 | printf("Fan %d speed is %d \n", fannum, rpmvalue); |
| 185 | break; |
| 186 | |
| 187 | case SET_PWM_DUTYCYCLE: |
| 188 | Verbose ("Inside Set PWM Dutycycle \n"); |
| 189 | Value = set_pwm_dutycycle (dev_id, fannum, rpmvalue); |
| 190 | if ( -1 == Value ) |
| 191 | { |
| 192 | printf ( "Set PWM Dutycycle Failed \n"); |
| 193 | return -1; |
| 194 | } |
| 195 | printf ( "Fan PWM set dutycycle Successfully\n"); |
| 196 | break; |
| 197 | case SET_PWM_DUTYCYCLE_VALUE: |
| 198 | Verbose ("Inside Set PWM Dutycycle Value\n"); |
| 199 | Value = set_pwm_dutycycle_value (dev_id, fannum, rpmvalue); |
| 200 | if ( -1 == Value ) |
| 201 | { |
| 202 | printf ( "Set PWM Dutycycle Value Failed \n"); |
| 203 | return -1; |
| 204 | } |
| 205 | printf ( "Fan PWM set dutycycle value Successfully\n"); |
| 206 | break; |
| 207 | case GET_PWM_DUTYCYCLE: |
| 208 | Verbose ("Inside Get PWM Dutycycle \n"); |
| 209 | Value = get_pwm_dutycycle (dev_id, fannum, &dutycycle); |
| 210 | if ( -1 == Value ) |
| 211 | { |
| 212 | printf ( "Set PWM Dutycycle Failed \n"); |
| 213 | return -1; |
| 214 | } |
| 215 | printf ( "PWM %d Dutycycle is %d\n",fannum, dutycycle); |
| 216 | break; |
| 217 | |
| 218 | default: |
| 219 | printf("Invalid PWMTACH Function Call\n"); |
| 220 | break; |
| 221 | } |
| 222 | return 0; |
| 223 | } |