Added AROS port of all shell applications:
[cake.git] / rom / usb / shellapps / DRadioTool.c
blob5efcd521668bc431b6f361ba122ba6a7117d20b5
1 /*
2 *----------------------------------------------------------------------------
3 * DRadio Tool for Poseidon
4 *----------------------------------------------------------------------------
5 * By Chris Hodges <chrisly@platon42.de>
6 */
8 #include "debug.h"
10 #include <proto/dos.h>
11 #include <proto/exec.h>
12 #include <proto/poseidon.h>
14 #include "DRadioTool.h"
15 #include <string.h>
17 #define ARGS_ON 0
18 #define ARGS_OFF 1
19 #define ARGS_FREQ 2
20 #define ARGS_SCAN 3
21 #define ARGS_AUTO 4
22 #define ARGS_SIGNAL 5
23 #define ARGS_UNIT 6
24 #define ARGS_SIZEOF 7
26 static const char *prgname = "DRadioTool";
27 static const char *template = "ON/S,OFF/S,FREQ/K/N,SCAN/S,AUTO/S,SIGNAL/S,UNIT/N/K";
28 static const char *version = "$VER: DRadioTool 1.1 (12.06.09) by Chris Hodges <chrisly@platon42.de>";
29 static IPTR ArgsArray[ARGS_SIZEOF];
30 static struct RDArgs *ArgsHook = NULL;
32 struct Library *ps;
34 AROS_UFP3(void, releasehook,
35 AROS_UFPA(struct Hook *, hook, A0),
36 AROS_UFPA(APTR, pab, A2),
37 AROS_UFPA(struct NepClassDRadio *, nch, A1));
39 struct NepClassDRadio * SetupDRadio(void);
40 struct NepClassDRadio * AllocDRadio(struct NepClassDRadio *nch);
41 void FreeDRadio(struct NepClassDRadio *nch);
43 AROS_UFH3(void, releasehook,
44 AROS_UFHA(struct Hook *, hook, A0),
45 AROS_UFHA(APTR, pab, A2),
46 AROS_UFHA(struct NepClassDRadio *, nch, A1))
48 AROS_USERFUNC_INIT
49 /*psdAddErrorMsg(RETURN_WARN, (STRPTR) prgname,
50 "DRadio killed!");*/
51 Signal(nch->nch_Task, SIGBREAKF_CTRL_C);
52 AROS_USERFUNC_EXIT
55 struct NepClassDRadio * SetupDRadio(void)
57 struct NepClassDRadio *nch;
58 struct PsdDevice *pd = NULL;
59 struct PsdAppBinding *pab;
60 ULONG unit;
62 if(ArgsArray[ARGS_UNIT])
64 unit = *((ULONG *) ArgsArray[ARGS_UNIT]);
65 } else {
66 unit = 0;
72 pd = psdFindDevice(pd,
73 DA_VendorID, 0x04b4,
74 DA_ProductID, 0x1002,
75 TAG_END);
76 } while(pd && (unit--));
78 if(!pd)
80 PutStr("No D-Link/GemTek Radio found!\n");
81 return(NULL);
83 if((nch = psdAllocVec(sizeof(struct NepClassDRadio))))
85 nch->nch_Device = pd;
86 nch->nch_ReleaseHook.h_Entry = (APTR) releasehook;
88 pab = psdClaimAppBinding(ABA_Device, pd,
89 ABA_ReleaseHook, &nch->nch_ReleaseHook,
90 ABA_UserData, nch,
91 TAG_END);
92 if(pab)
94 if(AllocDRadio(nch))
96 return(nch);
97 } else {
98 PutStr("Couldn't allocate DRadio...\n");
100 psdReleaseAppBinding(pab);
101 } else {
102 PutStr("Couldn't claim binding!\n");
104 psdFreeVec(nch);
106 PutStr("Hohum...\n");
107 } while(TRUE);
108 return(NULL);
111 struct NepClassDRadio * AllocDRadio(struct NepClassDRadio *nch)
113 nch->nch_Task = FindTask(NULL);
115 if((nch->nch_TaskMsgPort = CreateMsgPort()))
117 if((nch->nch_EP0Pipe = psdAllocPipe(nch->nch_Device, nch->nch_TaskMsgPort, NULL)))
119 return(nch);
120 } else {
121 PutStr("Couldn't allocate default pipe\n");
123 DeleteMsgPort(nch->nch_TaskMsgPort);
125 return(NULL);
129 void FreeDRadio(struct NepClassDRadio *nch)
131 APTR pab;
133 psdGetAttrs(PGA_DEVICE, nch->nch_Device,
134 DA_Binding, &pab,
135 TAG_END);
136 psdReleaseAppBinding(pab);
137 psdFreePipe(nch->nch_EP0Pipe);
138 DeleteMsgPort(nch->nch_TaskMsgPort);
139 psdFreeVec(nch);
142 void SetFreq(struct NepClassDRadio *nch, ULONG pll_div)
144 LONG ioerr;
145 psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_SETFREQ, (pll_div>>8) & 0xff, pll_div & 0xff);
146 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
147 if(ioerr)
149 Printf("Error sending set freq request: %s (%ld)\n",
150 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
152 /*psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_CTRL, 0x9c, 0xb7);
153 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
154 if(ioerr)
156 Printf("Error sending set freq request: %s (%ld)\n",
157 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
161 /**************************************************************************/
163 int main(int argc, char *argv[])
165 struct NepClassDRadio *nch;
166 LONG ioerr;
167 ULONG pll_div = (MIN_FREQ*1000 + PLLFREQ) / PLLSTEP;
168 ULONG ret = RETURN_OK;
170 if(!(ArgsHook = ReadArgs(template, ArgsArray, NULL)))
172 PutStr("Wrong arguments!\n");
173 return(RETURN_FAIL);
175 ps = OpenLibrary("poseidon.library", 1);
176 if(!ps)
178 FreeArgs(ArgsHook);
179 return(RETURN_FAIL);
181 if(!(nch = SetupDRadio()))
183 FreeArgs(ArgsHook);
184 CloseLibrary(ps);
185 return(RETURN_ERROR);
187 if(ArgsArray[ARGS_ON])
189 /*psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_GETSTEREO, 0x00, 0xC7);
190 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
191 if(ioerr)
193 Printf("Error sending on request: %s (%ld)\n",
194 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
196 psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_POWER, 0x01, 0x00);
197 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
198 if(ioerr)
200 Printf("Error sending power on request: %s (%ld)\n",
201 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
204 if(ArgsArray[ARGS_OFF])
206 /*psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_GETSTEREO, 0x16, 0x1C);
207 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
208 if(ioerr)
210 Printf("Error sending off request: %s (%ld)\n",
211 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
213 psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_POWER, 0x00, 0x00);
214 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
215 if(ioerr)
217 Printf("Error sending mute request: %s (%ld)\n",
218 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
221 if(ArgsArray[ARGS_FREQ])
223 ULONG freq;
224 freq = *((ULONG *) ArgsArray[ARGS_FREQ]);
225 if((freq < MIN_FREQ) || (freq > MAX_FREQ))
227 Printf("Value out of range. Must be between %ldKHz and %ldKHz.\n", MIN_FREQ, MAX_FREQ);
228 } else {
229 pll_div = (freq*1000 + PLLFREQ) / PLLSTEP;
230 SetFreq(nch, pll_div);
233 if(ArgsArray[ARGS_SCAN])
235 ULONG freq;
236 ULONG last_pll = 0;
237 for(; pll_div < (MAX_FREQ*1000 + PLLFREQ) / PLLSTEP; pll_div += 16)
239 freq = ((pll_div * PLLSTEP) - PLLFREQ) / 1000;
240 SetFreq(nch, pll_div);
241 //Printf("Pll_Div: %ld\r", pll_div);
242 Delay(13);
243 psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_GETSTEREO, 0x00, 0x00);
244 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
245 if(ioerr)
247 Printf("Error sending status request: %s (%ld)\n",
248 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
249 break;
251 if(!(nch->nch_Buf[0] & 0x01))
253 last_pll = pll_div;
254 if(ArgsArray[ARGS_AUTO])
256 Printf("Found station at %ld.%ld MHz. Press Ctrl-C to keep.\n", freq/1000, freq % 1000);
257 Delay(150);
258 } else {
259 Printf("%ld\n", freq);
262 if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
264 break;
267 if(last_pll)
269 SetFreq(nch, last_pll);
272 if(ArgsArray[ARGS_SIGNAL])
274 psdPipeSetup(nch->nch_EP0Pipe, URTF_IN|URTF_VENDOR|URTF_DEVICE, CMDID_GETSTEREO, 0x00, 0x00);
275 ioerr = psdDoPipe(nch->nch_EP0Pipe, nch->nch_Buf, 1);
276 if(ioerr)
278 Printf("Error sending status request: %s (%ld)\n",
279 psdNumToStr(NTS_IOERR, ioerr, "unknown"), ioerr);
281 if(nch->nch_Buf[0] & 0x01)
283 ret = RETURN_WARN;
286 FreeDRadio(nch);
287 FreeArgs(ArgsHook);
288 CloseLibrary(ps);
289 return(ret);