revert previous incorrect version of commit and recommit correct version to allow...
[AROS.git] / rom / usb / shellapps / PowManTool.c
blob5d3b02b5e75580fb374ddfabf6c60da9797a1b92
1 /*
2 *----------------------------------------------------------------------------
3 * Power Management Tool for Poseidon
4 *----------------------------------------------------------------------------
5 * By Chris Hodges <chrisly@platon42.de>
6 */
8 #include "debug.h"
10 #include <devices/usb_hid.h>
12 #include <proto/dos.h>
13 #include <proto/exec.h>
14 #include <proto/poseidon.h>
16 #include "PowManTool.h"
17 #include <string.h>
19 #define ARGS_OUTLET 0
20 #define ARGS_ON 1
21 #define ARGS_OFF 2
22 #define ARGS_TOGGLE 3
23 #define ARGS_STATUS 4
24 #define ARGS_UNIT 5
25 #define ARGS_SIZEOF 6
27 const char PowManTool_prgname[] = "PowManTool";
28 static const char *template = "SOCKET=OUTLET/N,ON/S,OFF/S,TOGGLE/S,STATUS/S,UNIT/N/K";
29 const char PowManTool_version[] = "$VER: PowManTool 1.0 (12.06.09) by Chris Hodges <chrisly@platon42.de>";
30 static IPTR ArgsArray[ARGS_SIZEOF];
31 static struct RDArgs *ArgsHook = NULL;
33 struct Library *ps;
35 AROS_UFP3(void, releasehook,
36 AROS_UFPA(struct Hook *, hook, A0),
37 AROS_UFPA(APTR, pab, A2),
38 AROS_UFPA(struct NepClassUPS *, nch, A1));
40 struct NepClassUPS * SetupUPS(void);
41 struct NepClassUPS * AllocUPS(struct NepClassUPS *nch);
42 void FreeUPS(struct NepClassUPS *nch);
44 AROS_UFH3(void, releasehook,
45 AROS_UFHA(struct Hook *, hook, A0),
46 AROS_UFHA(APTR, pab, A2),
47 AROS_UFHA(struct NepClassUPS *, nch, A1))
49 AROS_USERFUNC_INIT
50 /*psdAddErrorMsg(RETURN_WARN, (STRPTR) prgname,
51 "PowMan killed!");*/
52 Signal(nch->nch_Task, SIGBREAKF_CTRL_C);
53 AROS_USERFUNC_EXIT
56 struct NepClassUPS * SetupUPS(void)
58 struct NepClassUPS *nch;
59 struct PsdDevice *pd = NULL;
60 struct PsdAppBinding *pab;
61 ULONG unit;
63 if(ArgsArray[ARGS_UNIT])
65 unit = *((ULONG *) ArgsArray[ARGS_UNIT]);
66 } else {
67 unit = 0;
73 pd = psdFindDevice(pd,
74 DA_VendorID, 0x04b4,
75 DA_ProductID, 0xfd11,
76 TAG_END);
78 } while(pd && (unit--));
80 if(!pd)
82 PutStr("No GemBird PowerManager found!\n");
83 return(NULL);
85 if((nch = psdAllocVec(sizeof(struct NepClassUPS))))
87 nch->nch_Device = pd;
88 nch->nch_ReleaseHook.h_Entry = (APTR) releasehook;
90 pab = psdClaimAppBinding(ABA_Device, pd,
91 ABA_ReleaseHook, &nch->nch_ReleaseHook,
92 ABA_UserData, nch,
93 ABA_ForceRelease, TRUE,
94 TAG_END);
95 if(pab)
97 if(AllocUPS(nch))
99 return(nch);
100 } else {
101 PutStr("Couldn't allocate PowerManager...\n");
103 psdReleaseAppBinding(pab);
104 } else {
105 PutStr("Couldn't claim binding!\n");
107 psdFreeVec(nch);
109 PutStr("Hohum...\n");
110 } while(TRUE);
111 return(NULL);
114 struct NepClassUPS * AllocUPS(struct NepClassUPS *nch)
116 nch->nch_Task = FindTask(NULL);
118 if((nch->nch_TaskMsgPort = CreateMsgPort()))
120 if((nch->nch_EP0Pipe = psdAllocPipe(nch->nch_Device, nch->nch_TaskMsgPort, NULL)))
122 return(nch);
123 } else {
124 PutStr("Couldn't allocate default pipe\n");
126 DeleteMsgPort(nch->nch_TaskMsgPort);
128 return(NULL);
132 void FreeUPS(struct NepClassUPS *nch)
134 APTR pab;
136 psdGetAttrs(PGA_DEVICE, nch->nch_Device,
137 DA_Binding, &pab,
138 TAG_END);
139 psdReleaseAppBinding(pab);
140 psdFreePipe(nch->nch_EP0Pipe);
141 DeleteMsgPort(nch->nch_TaskMsgPort);
142 psdFreeVec(nch);
145 BOOL SendCommand(struct NepClassUPS *nch, ULONG outlet, ULONG cmd)
147 LONG ioerr;
148 UBYTE buf[2];
149 buf[0] = outlet*3;
150 buf[1] = cmd;
151 psdPipeSetup(nch->nch_EP0Pipe, URTF_OUT|URTF_CLASS|URTF_INTERFACE, UHR_SET_REPORT, (ULONG) 0x0300|buf[0], 0);
152 ioerr = psdDoPipe(nch->nch_EP0Pipe, buf, 2);
153 if(ioerr)
155 Printf("Error sending cmd %s: %s (%ld)\n",
156 cmd,
157 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
158 return(FALSE);
160 return(TRUE);
163 BOOL GetStatus(struct NepClassUPS *nch, ULONG outlet)
165 LONG ioerr;
166 UBYTE buf[2];
167 psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_CLASS|URTF_INTERFACE, UHR_GET_REPORT, 0x0300|(outlet*3), 0);
168 ioerr = psdDoPipe(nch->nch_EP0Pipe, buf, 2);
169 if(ioerr)
171 Printf("Error getting status: %s (%ld)\n",
172 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
173 return(FALSE);
175 return(buf[1]);
178 /**************************************************************************/
180 int main(int argc, char *argv[])
182 struct NepClassUPS *nch;
183 ULONG ret;
184 LONG outlet = 0;
185 BOOL all = FALSE;
186 if(!(ArgsHook = ReadArgs(template, ArgsArray, NULL)))
188 PutStr("Wrong arguments!\n");
189 return(RETURN_FAIL);
191 if(ArgsArray[ARGS_OUTLET])
193 outlet = *((LONG *) ArgsArray[ARGS_OUTLET]);
194 if((outlet < 1) || (outlet > 4))
196 PutStr("Only outlets from 1 to 4 are supported!\n");
197 FreeArgs(ArgsHook);
198 return(RETURN_ERROR);
201 ps = OpenLibrary("poseidon.library", 4);
202 if(!ps)
204 FreeArgs(ArgsHook);
205 return(RETURN_FAIL);
207 if(!(nch = SetupUPS()))
209 FreeArgs(ArgsHook);
210 CloseLibrary(ps);
211 return(RETURN_ERROR);
213 if(!outlet)
215 all = TRUE;
216 outlet = 1;
218 ret = RETURN_OK;
221 if(ArgsArray[ARGS_TOGGLE])
223 if(GetStatus(nch, outlet))
225 SendCommand(nch, outlet, 0x00);
226 } else {
227 SendCommand(nch, outlet, 0x03);
230 if(ArgsArray[ARGS_STATUS])
232 if(ArgsArray[ARGS_OFF])
234 ret |= GetStatus(nch, outlet) ? RETURN_WARN : RETURN_OK;
235 } else {
236 ret |= GetStatus(nch, outlet) ? RETURN_OK : RETURN_WARN;
238 } else {
239 if(ArgsArray[ARGS_ON])
241 SendCommand(nch, outlet, 0x03);
243 else if(ArgsArray[ARGS_OFF])
245 SendCommand(nch, outlet, 0x00);
248 outlet++;
249 } while(all && (outlet <= 4));
251 FreeUPS(nch);
252 FreeArgs(ArgsHook);
253 CloseLibrary(ps);
254 return(ret);