2 *----------------------------------------------------------------------------
3 * Power Management Tool for Poseidon
4 *----------------------------------------------------------------------------
5 * By Chris Hodges <chrisly@platon42.de>
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"
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
;
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
))
50 /*psdAddErrorMsg(RETURN_WARN, (STRPTR) prgname,
52 Signal(nch
->nch_Task
, SIGBREAKF_CTRL_C
);
56 struct NepClassUPS
* SetupUPS(void)
58 struct NepClassUPS
*nch
;
59 struct PsdDevice
*pd
= NULL
;
60 struct PsdAppBinding
*pab
;
63 if(ArgsArray
[ARGS_UNIT
])
65 unit
= *((ULONG
*) ArgsArray
[ARGS_UNIT
]);
73 pd
= psdFindDevice(pd
,
78 } while(pd
&& (unit
--));
82 PutStr("No GemBird PowerManager found!\n");
85 if((nch
= psdAllocVec(sizeof(struct NepClassUPS
))))
88 nch
->nch_ReleaseHook
.h_Entry
= (APTR
) releasehook
;
90 pab
= psdClaimAppBinding(ABA_Device
, pd
,
91 ABA_ReleaseHook
, &nch
->nch_ReleaseHook
,
93 ABA_ForceRelease
, TRUE
,
101 PutStr("Couldn't allocate PowerManager...\n");
103 psdReleaseAppBinding(pab
);
105 PutStr("Couldn't claim binding!\n");
109 PutStr("Hohum...\n");
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
)))
124 PutStr("Couldn't allocate default pipe\n");
126 DeleteMsgPort(nch
->nch_TaskMsgPort
);
132 void FreeUPS(struct NepClassUPS
*nch
)
136 psdGetAttrs(PGA_DEVICE
, nch
->nch_Device
,
139 psdReleaseAppBinding(pab
);
140 psdFreePipe(nch
->nch_EP0Pipe
);
141 DeleteMsgPort(nch
->nch_TaskMsgPort
);
145 BOOL
SendCommand(struct NepClassUPS
*nch
, ULONG outlet
, ULONG 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);
155 Printf("Error sending cmd %s: %s (%ld)\n",
157 psdNumToStr(NTS_IOERR
, ioerr
, "unknown"), ioerr
);
163 BOOL
GetStatus(struct NepClassUPS
*nch
, ULONG outlet
)
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);
171 Printf("Error getting status: %s (%ld)\n",
172 psdNumToStr(NTS_IOERR
, ioerr
, "unknown"), ioerr
);
178 /**************************************************************************/
180 int main(int argc
, char *argv
[])
182 struct NepClassUPS
*nch
;
186 if(!(ArgsHook
= ReadArgs(template, ArgsArray
, NULL
)))
188 PutStr("Wrong arguments!\n");
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");
198 return(RETURN_ERROR
);
201 ps
= OpenLibrary("poseidon.library", 4);
207 if(!(nch
= SetupUPS()))
211 return(RETURN_ERROR
);
221 if(ArgsArray
[ARGS_TOGGLE
])
223 if(GetStatus(nch
, outlet
))
225 SendCommand(nch
, outlet
, 0x00);
227 SendCommand(nch
, outlet
, 0x03);
230 if(ArgsArray
[ARGS_STATUS
])
232 if(ArgsArray
[ARGS_OFF
])
234 ret
|= GetStatus(nch
, outlet
) ? RETURN_WARN
: RETURN_OK
;
236 ret
|= GetStatus(nch
, outlet
) ? RETURN_OK
: RETURN_WARN
;
239 if(ArgsArray
[ARGS_ON
])
241 SendCommand(nch
, outlet
, 0x03);
243 else if(ArgsArray
[ARGS_OFF
])
245 SendCommand(nch
, outlet
, 0x00);
249 } while(all
&& (outlet
<= 4));