kbdpower.c

Go to the documentation of this file.
00001 /*
00002  * File Name  : kbdpower.c
00003  *
00004  * Description: Keyboard power mode changer
00005  */
00006 
00007 /*
00008  * This file is part of kbdupgrade.
00009  *
00010  * kbdupgrade is free software: you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation, either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * kbdupgrade is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00022  */
00023 
00024 /**
00025  * Copyright (C) 2008 iRex Technologies B.V.
00026  * All rights reserved.
00027  */
00028 
00029 //----------------------------------------------------------------------------
00030 // Include Files
00031 //----------------------------------------------------------------------------
00032 
00033 // system include files, between < >
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <stdlib.h>
00037 #include <unistd.h>
00038 #include <termios.h>
00039 #include "proto.h"
00040 
00041 
00042 //----------------------------------------------------------------------------
00043 // Type Declarations
00044 //----------------------------------------------------------------------------
00045 
00046 
00047 //----------------------------------------------------------------------------
00048 // Global Constants
00049 //----------------------------------------------------------------------------
00050 
00051 #define ION_SERIO_FLAGS CS8
00052 #define ION_SERIO_SPEED B115200
00053 
00054 #define ION_POWER_MODES 7
00055 
00056 char *modes[ION_POWER_MODES] = {
00057     "reboot",
00058     "on",
00059     "idle",
00060     "standby",
00061     "hibernate",
00062     "off",
00063     "warmreboot",
00064 };
00065 
00066 
00067 //----------------------------------------------------------------------------
00068 // Static Variables
00069 //----------------------------------------------------------------------------
00070 
00071 
00072 //============================================================================
00073 // Local Function Definitions
00074 //============================================================================
00075 
00076 static void usage();
00077 static void parse_args(int argc, char **argv);
00078 
00079 
00080 //============================================================================
00081 // Functions Implementation
00082 //============================================================================
00083 
00084 int main(int argc, char **argv)
00085 {
00086     uint8_t rcmd, rlen,i, len;
00087     uint8_t data[16], rdata[16];
00088     int powermode, delay, param;
00089 
00090     param = -1;
00091     len = 2;
00092     // check arguments
00093     parse_args(argc, argv);
00094     if ((argc-optind)<3)
00095     {
00096         usage();
00097     }
00098     // open device
00099     port_open(argv[optind], ION_SERIO_FLAGS, ION_SERIO_SPEED);
00100     powermode = 1;
00101     delay = atoi(argv[optind+2]);
00102     if ((argc-optind)==4)
00103     {
00104         param = atoi(argv[optind+3]);
00105         len = 3;
00106     }
00107     // find mode number
00108     for (i=0;i<ION_POWER_MODES;i++)
00109     {
00110         if (strcmp(modes[i], argv[optind+1])==0)
00111         {
00112            break;
00113         }
00114         powermode <<= 1;
00115     }
00116     if (i==ION_POWER_MODES)
00117     {
00118         fatal("Power mode '%s' not understood", argv[optind+1]);
00119     }
00120     // setup and send request
00121     data[0] = powermode;
00122     data[1] = delay;
00123     data[2] = param;
00124     //printf("sending command [%d]: %d %d %d\n", len, powermode, delay, param);
00125     printf("Switching to power mode '%s' (%d)\n", modes[i], powermode);
00126     i = 0;
00127     while (i<5)
00128     {
00129         if (!ionkbd_send_request(
00130                     IONKBD_REQ_SET_POWER, len, 
00131                     data, &rcmd, &rlen, rdata))
00132         {
00133             if (rcmd==IONKBD_RESP_STATUS)
00134             {
00135                 printf("Mode change successful\n");
00136                 return 0;
00137             }
00138             else if (rcmd>=0x80)
00139             {
00140                 printf("Ignoring update packet: 0x%02X\n", rcmd);
00141             }
00142             else
00143             {
00144                 printf("Unexpected response: 0x%02X\n", rcmd);
00145                 i++;
00146             }
00147         }
00148         else
00149         {
00150             i++;
00151         }
00152     }
00153     printf("Mode change failed\n");
00154 
00155     return 1;
00156 }
00157 
00158 
00159 static void usage()
00160 {
00161     int i;
00162     printf("kbdpower - \n");
00163     printf(" usage: kbdpower <options> <serial port> <power mode> <delay>\n");
00164     printf(" options:\n");
00165     printf("  -h     This text\n");
00166     printf(" power modes:\n");
00167     for (i=0;i<ION_POWER_MODES;i++)
00168         printf("  %s\n", modes[i]);
00169     exit(1);
00170 }
00171 
00172 
00173 static void parse_args(int argc, char **argv)
00174 {
00175     int c;
00176 
00177     while (( c = getopt(argc, argv, "h")) != -1)
00178     {
00179         switch (c)
00180         {
00181    case 'h':
00182    default:
00183        usage();
00184        break;
00185         }
00186     }
00187 }
Generated by  doxygen 1.6.2-20100208