libvcomm  1.0
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
vactions.c
1 /*
2  * vactions.c
3  *
4  * Created on: 11.09.2012
5  * Author: tarmo
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <malloc.h>
12 #include "hidapi.h"
13 #include "proto.h"
14 #include "vcomm.h"
15 
16 
17 VCOMMAPI int VCOMMCALL Vget_event(hid_device **vhandle, irevent_t *ev, int eventnum) {
18 
19  int resp;
20  response_t response;
21  request_t request;
22 
23  request.type = REQ_GET_EVENT;
24  request.data[0] = eventnum & 0xFF;
25  request.data[1] = (eventnum & 0xFF00) >> 8;
26 
27  if ((resp = Vcomm(vhandle, &request, &response)))
28  return resp;
29 
30  memcpy(ev, &response.data[0], sizeof(irevent_t));
31  return SUCCESS;
32 
33 }
34 
36 
37  int resp;
38  response_t response;
39  request_t request;
40 
41  request.type = REQ_GET_EVENTMAP;
42 
43  if ((resp = Vcomm(vhandle, &request, &response)))
44  // FAIL
45  return resp;
46 
47  memcpy(em, &response.data[0], sizeof(eventmap_t));
48  return SUCCESS;
49 }
50 
52 
53  int resp;
54  response_t response;
55  request_t request;
56 
57  request.type = REQ_GET_IRCODE;
58  resp = Vcomm(vhandle, &request, &response);
59  if (resp)
60  // FAIL
61  return resp;
62 
63  memcpy(ircode, &response.data[0], sizeof(ircode_t));
64  return SUCCESS;
65 }
66 
68  int resp;
69  response_t response;
70  request_t request;
71 
72  request.type = REQ_GET_IR_FRAME;
73  resp = Vcomm(vhandle, &request, &response);
74  if (resp)
75  return resp;
76 
77  memcpy(irframe, &response.data[0], sizeof(irframe_t));
78  return SUCCESS;
79 }
80 
82  int resp;
83  response_t response;
84  request_t request;
85 
86  request.type = REQ_GET_ID;
87  resp = Vcomm(vhandle, &request, &response);
88  if (resp)
89  return resp;
90 
91  memcpy(id, &response.data[0], sizeof(hwid_t));
92  return SUCCESS;
93 }
94 
95 VCOMMAPI int VCOMMCALL Vget_fw_version(hid_device **vhandle, uint32_t *fwver) {
96  int resp;
97  response_t response;
98  request_t request;
99 
100  request.type = REQ_GET_FW_VERSION;
101  if ((resp = Vcomm(vhandle, &request, &response)))
102  return resp;
103 
104  memcpy(fwver, &response.data[0], sizeof(uint32_t));
105  return SUCCESS;
106 }
107 
108 VCOMMAPI int VCOMMCALL Vget_fw(hid_device **vhandle, uint8_t *fwdata) {
109  int resp;
110  response_t response;
111  request_t request;
112 
113  request.type = REQ_GET_FW;
114  if ((resp = Vcomm(vhandle, &request, &response)))
115  return resp;
116 
117  memcpy(fwdata, &response.data[0], 32 * 1024);
118  return SUCCESS;
119 }
120 
121 VCOMMAPI int VCOMMCALL Vset_mode(hid_device **vhandle, int mode){
122  int resp;
123  response_t response;
124  request_t request;
125 
126  if(mode != REQ_SET_MODE_NORMAL && mode != REQ_SET_MODE_CAPT && mode != REQ_SET_MODE_CFG){
127  return ERR_ARGUMENT_ERROR;
128  }
129  request.type = mode;
130 
131  if ((resp = Vcomm(vhandle, &request, &response)))
132  return resp;
133 
134  return response.data[0];
135 }
136 
137 VCOMMAPI int VCOMMCALL Vset_led(hid_device **vhandle, int ledstate){
138  int resp;
139  response_t response;
140  request_t request;
141 
142  if(ledstate != 0 && ledstate != 1){
143  return ERR_ARGUMENT_ERROR;
144  }
145  request.type = REQ_SET_LED;
146  request.data[0] = ledstate;
147 
148  if ((resp = Vcomm(vhandle, &request, &response)))
149  return resp;
150 
151  return response.data[0];
152 }
153 
155  int resp;
156  response_t response;
157  request_t request;
158 
159  request.type = REQ_CLEAR_LAST;
160 
161  if ((resp = Vcomm(vhandle, &request, &response)))
162  return resp;
163 
164  return response.data[0];
165 }
166 
168  int resp;
169  response_t response;
170  request_t request;
171 
172  request.type = REQ_CLEAR_EVENTMAP;
173 
174  if ((resp = Vcomm(vhandle, &request, &response)))
175  return resp;
176 
177  return response.data[0];
178 }
179 
180 
181 VCOMMAPI int VCOMMCALL Vset_ircode(hid_device **vhandle, ircode_t *ircode, cmdstat_t *cmdstat ){
182  int resp;
183  response_t response;
184  request_t request;
185 
186  // create request
187  request.type = REQ_SET_IRCODE;
188  memcpy(&request.data[0], ircode, sizeof(ircode_t));
189 
190 
191  if ((resp = Vcomm(vhandle, &request, &response)))
192  // FAIL
193  return resp;
194 
195  cmdstat->type = response.type;
196  cmdstat->err_code = response.data[0];
197 
198  return SUCCESS;
199 
200 }
201 
202 VCOMMAPI int VCOMMCALL Vset_event(hid_device **vhandle, ircode_t *from_ircode, ircode_t *to_ircode, cmdstat_t *cmdstat ){
203  int resp;
204  response_t response;
205  request_t request;
206 
207  // create IRevent
208  irevent_t *ev;
209  ev = (irevent_t *)&request.data[0];
210  // fill in ircode
211  memcpy(&ev->in, from_ircode, sizeof(ircode_t));
212  memcpy(&ev->out, to_ircode, sizeof(ircode_t));
213 
214  // set request type
215  request.type = REQ_SET_EVENT;
216 
217 
218  if ((resp = Vcomm(vhandle, &request, &response)))
219  // FAIL
220  return resp;
221 
222  cmdstat->type = response.type;
223  cmdstat->err_code = response.data[0];
224 
225  return SUCCESS;
226 
227 }
228 
229 VCOMMAPI int VCOMMCALL Vset_irframe_page(hid_device **vhandle, uint8_t page_num, uint32_t *page_data, cmdstat_t *cmdstat ){
230 
231  int resp;
232  response_t response;
233  request_t request;
234 
235  // set request type
236  request.type = REQ_SET_IRFRAME_PAGE;
237  request.data[0] = page_num;
238  memcpy(&request.data[1], page_data, 48);
239 
240  if ((resp = Vcomm(vhandle, &request, &response)))
241  // FAIL
242  return resp;
243 
244  cmdstat->type = response.type;
245  cmdstat->err_code = response.data[0];
246 
247  return SUCCESS;
248 
249 }
250 VCOMMAPI int VCOMMCALL Vset_irframe_info(hid_device **vhandle, uint16_t num_pulses, cmdstat_t *cmdstat ){
251 
252  int resp;
253  response_t response;
254  request_t request;
255 
256  // set request type
257  request.type = REQ_SET_IRFRAME_INFO;
258  request.data[0] = num_pulses & 0xFF;
259  request.data[1] = (num_pulses & 0xFF00) >> 8;
260 
261  if ((resp = Vcomm(vhandle, &request, &response)))
262  // FAIL
263  return resp;
264 
265  cmdstat->type = response.type;
266  cmdstat->err_code = response.data[0];
267 
268  return SUCCESS;
269 
270 }
271 
273 
274  uint32_t numpages = 0;
275  uint8_t pagenum = 0;
276  cmdstat_t cmdstat;
277  int resp;
278 
279  numpages = irframe->num_pulses / 12;
280  if(irframe->num_pulses % 12) numpages++;
281 
282  for (pagenum = 0; pagenum < numpages; ++pagenum) {
283  if( (resp = Vset_irframe_page(vhandle, pagenum, &irframe->length[pagenum * 12], &cmdstat))){
284  return resp;
285  }
286  }
287 
288  if((resp=Vset_irframe_info(vhandle, irframe->num_pulses, &cmdstat))) return resp;
289 
290  return SUCCESS;
291 
292 }
293 
294 
296  int resp;
297  response_t response;
298  request_t request;
299 
300  memcpy(&request.data[0], ircode, sizeof(ircode_t));
301 
302 
303  // set request type
304  request.type = REQ_CLEAR_EVENT_IRCODE;
305 
306 
307  if ((resp = Vcomm(vhandle, &request, &response)))
308  // FAIL
309  return resp;
310 
311  cmdstat->type = response.type;
312  cmdstat->err_code = response.data[0];
313 
314  return SUCCESS;
315 
316 }
317 
318 VCOMMAPI int VCOMMCALL Vclear_event_num(hid_device **vhandle, int eventnum, cmdstat_t *cmdstat ){
319  int resp;
320  response_t response;
321  request_t request;
322 
323 
324  // set request type
325  request.type = REQ_CLEAR_EVENT_NUM;
326 
327  request.data[0] = eventnum & 0xFF;
328  request.data[1] = (eventnum & 0xFF00) >> 8;
329 
330 
331  if ((resp = Vcomm(vhandle, &request, &response)))
332  // FAIL
333  return resp;
334 
335  cmdstat->type = response.type;
336  cmdstat->err_code = response.data[0];
337 
338  return SUCCESS;
339 
340 }
341 
343  int resp;
344  response_t response;
345  request_t request;
346 
347  // set request type
348  request.type = REQ_GET_CONFIG;
349 
350 
351  if ((resp = Vcomm(vhandle, &request, &response)))
352  // FAIL
353  return resp;
354 
355  memcpy(config, &response.data[0], sizeof(config_t));
356  return SUCCESS;
357 }
358 
360  int resp;
361  response_t response;
362  request_t request;
363 
364  // set request type
365  request.type = REQ_SET_CONFIG;
366 
367  memcpy(&request.data[0], config, sizeof(config_t));
368 
369  if ((resp = Vcomm(vhandle, &request, &response)))
370  // FAIL
371  return resp;
372 
373  return SUCCESS;
374 }
375 
377  int resp;
378  response_t response;
379  request_t request;
380 
381  // set request type
382  request.type = REQ_RESET_CONFIG;
383 
384  if ((resp = Vcomm(vhandle, &request, &response)))
385  // FAIL
386  return resp;
387 
388  return SUCCESS;
389 }
390 
392 
393  int resp;
394  response_t response;
395  request_t request;
396 
397  // set request type
398  request.type = REQ_GET_STAT;
399 
400 
401  if ((resp = Vcomm(vhandle, &request, &response)))
402  // FAIL
403  return resp;
404 
405  memcpy(stat, &response.data[0], sizeof(stat_t));
406 
407  return SUCCESS;
408 
409 }
410 
411 VCOMMAPI int VCOMMCALL Venter_isp(hid_device **vhandle, uint8_t time ){
412 
413  response_t response;
414  request_t request;
415 
416  // set request type
417  request.type = REQ_ENTER_ISP;
418 
419  if (time < 10) time = 10;
420  request.data[0] = time;
421  Vcomm(vhandle, &request, &response);
422 
423  Vclose(vhandle);
424 
425  return SUCCESS;
426 
427 }
428 
429 VCOMMAPI int VCOMMCALL Vget_btn(hid_device **vhandle, uint8_t *state ){
430 
431  int resp;
432  response_t response;
433  request_t request;
434 
435  // set request type
436  request.type = REQ_GET_BTN;
437 
438 
439  if ((resp = Vcomm(vhandle, &request, &response)))
440  // FAIL
441  return resp;
442 
443  *state = response.data[0];
444 
445  return SUCCESS;
446 }
447