Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 221 lines (202 sloc) 5.015 kb
1604798 @DaveDavenport Initial import.
authored
1 requires 2.0.0
2
3 %headertop{
4 #include <stdio.h>
5 #include <string.h>
6 #include <usb.h>
7 #include "wsl-transmitter.h"
8 %}
9
10 %{
11 typedef struct _data_package
12 {
13 unsigned char msg1;
14 unsigned int id : 16;
15 unsigned char msg3;
16 } data_package;
17
18 %}
19 class Wsl:Main from G:Object {
20 private struct usb_dev_handle *wsl_handle = {NULL};
21 public GList *transmitter_list = {NULL};
22 public
23 WslMain * new(void)
24 {
25 return (WslMain *)GET_NEW;
26 }
27 /**
28 * Initialize. Make sure usb is running, and we 'intergrate' in main loop
29 */
30 init (self)
31 {
32 usb_init();
33 /** Check for updates every 1/2 second */
34 g_timeout_add(100, (GSourceFunc )(self_check_device), self);
35 printf("WslMain init\n");
36 }
37 /**
38 * Usb handling Functions
39 */
40 private
41 gboolean get_and_open_weasel(self)
42 {
43 struct usb_device *wsl = self_find_weasel();
44 if(wsl)
45 {
46 printf("Found weasel\n");
47 self->_priv->wsl_handle = usb_open(wsl);
48 if(self->_priv->wsl_handle) {
49 if(usb_set_configuration(self->_priv->wsl_handle, 1) >= 0){
50 if(usb_claim_interface(self->_priv->wsl_handle, 0) >=0){
51 /* We have the device, now let us all rejoice */
52 char string[256];
53 int ret;
54 printf("device found\n");
55 /** Try to get device name, it's nice if a popup can show that nicely */
56 ret = usb_get_string_simple(self->_priv->wsl_handle, wsl->descriptor.iProduct, string, 256);
57 if(ret >0)
58 self_device_found(self,string);
59 else
60 self_device_found(self,NULL);
61 return TRUE;
62 } else { /* usb claim interface */
63 printf("Failed to cleam weasel: '%s'\n", usb_strerror());
64 }
65 } else { /* usb_set_configuration */
66 printf("Failed to set configuration: '%s'\n", usb_strerror());
67 }
68 usb_close(self->_priv->wsl_handle);
69 } else { /* usb_open */
70 printf("failed to open weasel: '%s'\n", usb_strerror());
71 }
72 }
73 return FALSE;
74 }
75
76 private
77 struct usb_device * find_weasel(void)
78 {
79 struct usb_bus *bus;
80 struct usb_device *dev;
81
82 usb_find_busses();
83 usb_find_devices();
84 for (bus=usb_busses; bus; bus=bus->next) {
85 for (dev=bus->devices; dev; dev=dev->next) {
86 if ((dev->descriptor.idVendor==0x04b4) &&
87 (dev->descriptor.idProduct==0x7417))
88 return dev;
89 }
90 }
91 return NULL;
92 }
93
94 private
95 gboolean check_device (self)
96 {
97 if(self->transmitter_list)
98 {
99 g_list_foreach(self->transmitter_list, (GFunc)(wsl_transmitter_update),NULL);
100 }
101 /* No dongle allready found */
102 if(self->_priv->wsl_handle == NULL)
103 {
104 if(!self_get_and_open_weasel(self))
105 {
106 /* No new device found, no use continuing */
107 return TRUE;
108 }
109 }
110 else
111 {
112 /* We have a device */
113 // char buffer[4];
114 data_package buffer;
115 int cnt;
116 do{
117 cnt = usb_interrupt_read(self->_priv->wsl_handle,1,&buffer, 4, 50);
118 if(cnt < 0)
119 {
120 /** Error handling */
121 if(cnt == -19) /** unplug */
122 {
123 self_device_removed(self);
124 usb_close(self->_priv->wsl_handle);
125 self->_priv->wsl_handle = NULL;
126 }
127
128 }
129 else if(cnt == 4)
130 {
131 int act = (buffer.msg1 == 8 && buffer.msg3 ==1); /** the message type */
132 /**
133 * check if begin and end values are valid options
134 * So if it's not, we can safely discard
135 */
d112f47 @DaveDavenport Small update.
authored
136 if(buffer.id >0 && (buffer.msg1==0 || buffer.msg1== 8) &&
1604798 @DaveDavenport Initial import.
authored
137 (buffer.msg3 == 1 || buffer.msg3 == 0))
138 {
139
140
d112f47 @DaveDavenport Small update.
authored
141 WslTransmitter *wt = self_get_transmitter_from_id(self,buffer.id);
1604798 @DaveDavenport Initial import.
authored
142 if(act && wt == NULL) /** A trasnmitter is turned on, and no transmitter found */
143 {
d112f47 @DaveDavenport Small update.
authored
144 wt = self_add_transmitter(self, buffer.id);
1604798 @DaveDavenport Initial import.
authored
145 wsl_transmitter_set_in_range(wt, TRUE);
146 /**
147 * Send signal */
148 self_transmitter_found(self, wt);
149 }else if(wt){ /* heartbeat */
150 wsl_transmitter_heartbeat(wt);
151 }
152 }
153 else
154 {
155 printf("Failed packet\n");
156 }
157 }
158 }while(cnt>0);
159 }
160 return TRUE;
161 }
162
163 signal last NONE (POINTER)
164 void transmitter_found(self, WslTransmitter *wt)
165 {
166 printf("***SIGNAL*** Transmitter added: '%i'\n", wsl_transmitter_get_id(wt));
167
168 }
169
170 public
171 WslTransmitter *add_transmitter(self, int id)
172 {
173 WslTransmitter *wt = wsl_transmitter_new(id);
174 self->transmitter_list = g_list_append(self->transmitter_list,wt);
175 printf("%p test\n", self->transmitter_list);
176 return wt;
177 }
178 private
179 WslTransmitter * get_transmitter_from_id(self, int id)
180 {
181 WslTransmitter *wt= NULL;
182 if(self->transmitter_list)
183 {
184 GList *node = g_list_first(self->transmitter_list);
185 for(;node;node = g_list_next(node))
186 {
187 wt = node->data;
188 if(wsl_transmitter_get_id(wt) == id)
189 return wt;
190 }
191 }
192 return NULL;
193 }
194 signal last NONE (NONE)
195 void device_removed(self)
196 {
197 printf("**SIGNAL** Device unplugged\n");
198
199
200 }
201
202
203 signal last NONE (STRING)
204 void
205 device_found(self, gchar *name)
206 {
207 printf("**SIGNAL** Device found: '%s'\n",name);
208
209
210 }
211 public
212 gboolean has_dongle(self)
213 {
214 if(self->_priv->wsl_handle) {
215 return TRUE;
216 } else {
217 return FALSE;
218 }
219 }
220 }
Something went wrong with that request. Please try again.