tsDeviceMonitor Darwin
@@ -5,5 +5,7 @@ | ||
5 | 5 | |
6 | 6 | #include <net/if.h> |
7 | 7 | #include <IOKit/hid/IOHIDLib.h> |
8 | +#include <CoreFoundation/CoreFoundation.h> | |
9 | +#include <IOKit/IOKitLib.h> | |
8 | 10 | |
9 | 11 | #endif |
@@ -0,0 +1,20 @@ | ||
1 | + | |
2 | +#ifndef ___co_tsDeviceMonitor_cpp_H___ | |
3 | +#define ___co_tsDeviceMonitor_cpp_H___ | |
4 | + | |
5 | +#include "ts/c++/tsDeviceMonitor.h" | |
6 | +#include "ts/c++/tsThread.h" | |
7 | + | |
8 | +class co_tsDeviceMonitor; | |
9 | +typedef struct __MyDriverData { | |
10 | + io_service_t service; | |
11 | + io_object_t notification; | |
12 | + co_tsDeviceMonitor * mon; | |
13 | + struct __MyDriverData * next; | |
14 | +} MyDriverData; | |
15 | + | |
16 | + | |
17 | +#include "_ts/c++/co_tsDeviceMonitor_pb.h" | |
18 | + | |
19 | +#endif | |
20 | + |
@@ -0,0 +1,199 @@ | ||
1 | + | |
2 | + | |
3 | +#include "_ts/c++/co_tsDeviceMonitor_.h" | |
4 | + | |
5 | +CLASS_TINYSTATE(ts/c++/co_tsDeviceMonitor,ts/c++/tinyState) | |
6 | + | |
7 | + | |
8 | +#if 0 | |
9 | + | |
10 | +TS_BEGIN_IMPLEMENT | |
11 | + | |
12 | + | |
13 | +class TS_THISCLASS : public TS_BASECLASS { | |
14 | +public: | |
15 | + co_tsDeviceMonitor_( | |
16 | + tsDeviceMonitor * parent); | |
17 | + void inherit( | |
18 | + tsDeviceMonitor * parent); | |
19 | + tsDeviceMonitor*& parent; | |
20 | + | |
21 | + static void DeviceNotification( | |
22 | + void * refCon, | |
23 | + io_service_t service, | |
24 | + natural_t messageType, | |
25 | + void * messageArgument); | |
26 | + void DeviceNotification( | |
27 | + MyDriverData * refCon, | |
28 | + io_service_t service, | |
29 | + natural_t messageType, | |
30 | + void * messageArgument); | |
31 | + static void DeviceAdded(void * refCon, io_iterator_t iterator); | |
32 | + void DeviceAdded(io_iterator_t iterator); | |
33 | + virtual void destroy(); | |
34 | +private: | |
35 | +protected: | |
36 | + CFRunLoopRef rl; | |
37 | + IONotificationPortRef notificationPort; | |
38 | + MyDriverData * dlist; | |
39 | +}; | |
40 | + | |
41 | +TS_END_IMPLEMENT | |
42 | + | |
43 | +#endif | |
44 | + | |
45 | + | |
46 | +co_tsDeviceMonitor_::co_tsDeviceMonitor_( | |
47 | + tsDeviceMonitor * _parent) | |
48 | + : tinyState_(_parent), | |
49 | + parent((tsDeviceMonitor*&)TS_BASECLASS::parent) | |
50 | +{ | |
51 | +} | |
52 | + | |
53 | +void | |
54 | +co_tsDeviceMonitor_::inherit( | |
55 | + tsDeviceMonitor * _parent) | |
56 | +{ | |
57 | + this->TS_BASECLASS::inherit(_parent); | |
58 | +} | |
59 | + | |
60 | + | |
61 | + | |
62 | +/******************************************* | |
63 | + INSTANCE FUNCTIONS | |
64 | +********************************************/ | |
65 | + | |
66 | + | |
67 | +void | |
68 | +co_tsDeviceMonitor_::destroy() | |
69 | +{ | |
70 | + TS_BASECLASS::destroy(); | |
71 | + if ( rl ) | |
72 | + CFRunLoopStop(rl); | |
73 | +} | |
74 | + | |
75 | + | |
76 | +/******************************************* | |
77 | + STATE MACHINE | |
78 | +********************************************/ | |
79 | + | |
80 | + | |
81 | + | |
82 | +void | |
83 | +co_tsDeviceMonitor_::DeviceNotification( | |
84 | + void * refCon, | |
85 | + io_service_t service, | |
86 | + natural_t messageType, | |
87 | + void * messageArgument) | |
88 | +{ | |
89 | + MyDriverData * myDriverData = (MyDriverData*)refCon; | |
90 | + myDriverData->mon->DeviceNotification( | |
91 | + myDriverData,service,messageType,messageArgument); | |
92 | +} | |
93 | + | |
94 | +void | |
95 | +co_tsDeviceMonitor_::DeviceNotification( | |
96 | + MyDriverData * myDriverData, | |
97 | + io_service_t service, | |
98 | + natural_t messageType, | |
99 | + void * messageArgument) | |
100 | +{ | |
101 | + kern_return_t kr; | |
102 | + | |
103 | + if (messageType == kIOMessageServiceIsTerminated) { | |
104 | + io_name_t name; | |
105 | + | |
106 | + IORegistryEntryGetName(service, name); | |
107 | + // interrupted; | |
108 | + kr = IOObjectRelease(myDriverData->notification); | |
109 | + IOObjectRelease(myDriverData->service); | |
110 | + MyDriverData ** dd; | |
111 | + for ( dd = &dlist ; *dd ; dd = &(*dd)->next ) | |
112 | + if ( myDriverData == *dd ) | |
113 | + break; | |
114 | + if ( *dd ) | |
115 | + *dd = myDriverData->next; | |
116 | + free(myDriverData); | |
117 | + | |
118 | + THR_CALL(ACT_INVOKE,0) | |
119 | + } | |
120 | +} | |
121 | + | |
122 | +void | |
123 | +co_tsDeviceMonitor_::DeviceAdded(void * refCon, io_iterator_t iterator) | |
124 | +{ | |
125 | +co_tsDeviceMonitor * THIS = (co_tsDeviceMonitor*)refCon; | |
126 | + THIS->DeviceAdded(iterator); | |
127 | +} | |
128 | + | |
129 | +void | |
130 | +co_tsDeviceMonitor_::DeviceAdded(io_iterator_t iterator) | |
131 | +{ | |
132 | + io_service_t service = 0; | |
133 | + while ( (service = IOIteratorNext(iterator) ) != 0 ) { | |
134 | + MyDriverData * myDriverData; | |
135 | + kern_return_t kr; | |
136 | + | |
137 | + myDriverData = (MyDriverData*)malloc(sizeof(MyDriverData)); | |
138 | + myDriverData->service = service; | |
139 | + myDriverData->mon = ifThis; | |
140 | + myDriverData->next = dlist; | |
141 | + dlist = myDriverData; | |
142 | + | |
143 | + kr = IOServiceAddInterestNotification( | |
144 | + notificationPort, | |
145 | + service, | |
146 | + kIOGeneralInterest, | |
147 | + DeviceNotification, | |
148 | + myDriverData, | |
149 | + &myDriverData->notification); | |
150 | + THR_CALL(ACT_INVOKE,0) | |
151 | + } | |
152 | +} | |
153 | + | |
154 | + | |
155 | + | |
156 | +TS_THREAD(ACT_START) | |
157 | +{ | |
158 | + CFDictionaryRef matchingDict = NULL; | |
159 | + io_iterator_t iter = 0; | |
160 | + CFRunLoopSourceRef runLoopSource; | |
161 | + kern_return_t kr; | |
162 | + | |
163 | + notificationPort = NULL; | |
164 | + | |
165 | + | |
166 | + matchingDict = IOServiceMatching("IOUSBDevice"); | |
167 | + | |
168 | + notificationPort = IONotificationPortCreate(kIOMasterPortDefault); | |
169 | + runLoopSource = IONotificationPortGetRunLoopSource(notificationPort); | |
170 | + CFRunLoopAddSource(rl = CFRunLoopGetCurrent(),runLoopSource,kCFRunLoopDefaultMode); | |
171 | + | |
172 | + kr = IOServiceAddMatchingNotification(notificationPort,kIOFirstMatchNotification, | |
173 | + matchingDict,DeviceAdded,(void*)ifThis,&iter); | |
174 | + DeviceAdded((void*)ifThis,iter); | |
175 | + | |
176 | + CFRunLoopRun(); | |
177 | + | |
178 | + IONotificationPortDestroy(notificationPort); | |
179 | + IOObjectRelease(iter); | |
180 | + | |
181 | + for ( ; dlist ; ) { | |
182 | + MyDriverData * n; | |
183 | + n = dlist; | |
184 | + dlist = n->next; | |
185 | + IOObjectRelease(n->service); | |
186 | + free(n); | |
187 | + } | |
188 | + | |
189 | + return rDO|FIN_START; | |
190 | +} | |
191 | + | |
192 | +TS_STATE(ACT_INVOKE) | |
193 | +{ | |
194 | + parent->eventHandler( | |
195 | + NEW stdEvent(TSE_INVOKE, | |
196 | + ifThis, | |
197 | + (INTEGER64)0)); | |
198 | + return ACT_START; | |
199 | +} |
@@ -4,6 +4,7 @@ | ||
4 | 4 | #include "_ts/c++/tsDeviceMonitor_.h" |
5 | 5 | #include "ts/c++/tsReadLine.h" |
6 | 6 | #include "ts/c++/tsLog.h" |
7 | +#include "ts/c++/co_tsDeviceMonitor.h" | |
7 | 8 | |
8 | 9 | CLASS_TINYSTATE(ts/c++/tsDeviceMonitor,ts/c++/tinyState) |
9 | 10 |
@@ -17,15 +18,8 @@ | ||
17 | 18 | #include "ts/c++/tsDevNull.h" |
18 | 19 | #include "ts/c++/tsCallBuffer.h" |
19 | 20 | #include "ts/c++/sTimer.h" |
20 | -#include "ts/c++/tsSystem.h" | |
21 | -#include <libusb-1.0/libusb.h> | |
22 | 21 | |
23 | -typedef struct detect_list { | |
24 | - int vendor; | |
25 | - int product; | |
26 | -} DETECT_LIST; | |
27 | - | |
28 | - | |
22 | +class co_tsDeviceMonitor; | |
29 | 23 | class TS_THISCLASS : public TS_BASECLASS { |
30 | 24 | public: |
31 | 25 | tsDeviceMonitor_( |
@@ -43,21 +37,9 @@ | ||
43 | 37 | private: |
44 | 38 | protected: |
45 | 39 | int mode; |
46 | - tsIOstring * ior; | |
47 | - tsIOstring * iow; | |
48 | - tsIOstring * ioe; | |
49 | - tsCallBuffer * cb; | |
50 | - tsSystem * sys; | |
51 | - | |
52 | - static const char * commandList[]; | |
53 | - const char ** commandListPtr; | |
54 | 40 | sTimer timer; |
55 | 41 | |
56 | - unsigned device_updated:1; | |
57 | - | |
58 | - DETECT_LIST * dlist; | |
59 | - int dlist_length; | |
60 | - | |
42 | + co_tsDeviceMonitor * mon; | |
61 | 43 | }; |
62 | 44 | |
63 | 45 | TS_END_IMPLEMENT |
@@ -99,7 +81,6 @@ | ||
99 | 81 | TS_STATE(INI_START) |
100 | 82 | { |
101 | 83 | tsDeviceMonitor * master; |
102 | - | |
103 | 84 | master = dynamic_cast<tsDeviceMonitor*>(application |
104 | 85 | ->get_global(getClass())); |
105 | 86 | if ( master ) { |
@@ -130,7 +111,7 @@ | ||
130 | 111 | } |
131 | 112 | application->set_global(getClass(),ifThis); |
132 | 113 | check_listener = 1; |
133 | - libusb_init(NULL); | |
114 | + REF_SET(mon,NEW co_tsDeviceMonitor(ifThis)); | |
134 | 115 | return rDO|ACT_WAIT_HOOK; |
135 | 116 | } |
136 | 117 | TS_STATE(ACT_WAIT_HOOK) |
@@ -139,82 +120,51 @@ | ||
139 | 120 | return 0; |
140 | 121 | return rDO|ACT_START; |
141 | 122 | } |
142 | -TS_THREAD(ACT_START) | |
123 | +TS_STATE(ACT_START) | |
143 | 124 | { |
144 | -DETECT_LIST * new_dlist; | |
145 | -int i,count; | |
146 | -libusb_device ** list; | |
147 | - | |
148 | - if ( listenerCounter(TSE_SIGNAL) == 0 ) | |
149 | - return rDO|FIN_MASTER_START; | |
150 | - device_updated = 0; | |
151 | - count = libusb_get_device_list(NULL, &list); | |
152 | - if ( count <= 0 ) { | |
153 | - count = 0; | |
154 | - new_dlist = 0; | |
155 | - } | |
156 | - else { | |
157 | - new_dlist = (DETECT_LIST*)malloc(sizeof(DETECT_LIST)*count); | |
158 | - for ( i = 0 ; i < count ; i ++ ) { | |
159 | - libusb_device *device = list[i]; | |
160 | - libusb_device_descriptor desc = {0}; | |
161 | - libusb_get_device_descriptor(device, &desc); | |
162 | - new_dlist[i].vendor = desc.idVendor; | |
163 | - new_dlist[i].product = desc.idProduct; | |
125 | + if ( ev->type != TSE_INVOKE ) { | |
126 | + if ( timer.is_expire(ifThis) ) { | |
127 | + timer.stop(); | |
128 | + if ( debug == DM_DEBUG ) | |
129 | + tsLog::log_printf(ifThis,LOG_DEBUG,"DeviceMonitor :: INTERRUPT !!\n"); | |
130 | + invoke_listen( | |
131 | + NEW stdEvent(TSE_SIGNAL, | |
132 | + ifThis, | |
133 | + ((INTEGER64)0))); | |
164 | 134 | } |
135 | + if ( is_destroyed() ) | |
136 | + return rDO|FIN_MASTER_START; | |
137 | + if ( listenerCounter(TSE_SIGNAL) == 0 ) | |
138 | + return rDO|FIN_MASTER_START; | |
139 | + return 0; | |
165 | 140 | } |
166 | - libusb_free_device_list(list,0); | |
167 | - if ( count != dlist_length ) | |
168 | - device_updated = 1; | |
169 | - else for ( i = 0 ; i < count ; i ++ ) | |
170 | - if ( new_dlist[i].vendor != dlist[i].vendor || | |
171 | - new_dlist[i].product != dlist[i].product ) { | |
172 | - device_updated = 1; | |
173 | - break; | |
174 | - } | |
175 | - if ( dlist ) | |
176 | - free(dlist); | |
177 | - dlist = new_dlist; | |
178 | - dlist_length = count; | |
179 | - return rDO|ACT_SEND_EVENT; | |
141 | + timer.start(ifThis,100*1000); | |
142 | + return 0; | |
180 | 143 | } |
181 | -TS_STATE(ACT_SEND_EVENT) | |
144 | +TS_STATE(FIN_MASTER_START) | |
182 | 145 | { |
183 | - if ( device_updated ) { | |
184 | - if ( debug == DM_DEBUG ) | |
185 | - tsLog::log_printf(ifThis,LOG_DEBUG,"DeviceMonitor :: INTERRUPT !!\n"); | |
186 | - invoke_listen( | |
187 | - NEW stdEvent(TSE_SIGNAL, | |
188 | - ifThis, | |
189 | - ((INTEGER64)0))); | |
190 | - } | |
191 | - timer.start(ifThis,1000*1000); | |
192 | - return ACT_WAIT; | |
146 | + mon->listen(ifThis,TSE_DESTROY); | |
147 | + return rDO|FIN_DESTROY; | |
193 | 148 | } |
194 | -TS_STATE(ACT_WAIT) | |
149 | +TS_STATE(FIN_DESTROY) | |
195 | 150 | { |
196 | - if ( destroy_flag || ref_destroy_flag ) | |
197 | - return rDO|FIN_MASTER_START; | |
198 | - if ( !timer.is_expire(ifThis) ) | |
199 | - return 0; | |
200 | - return rDO|ACT_START; | |
151 | + mon->destroy(); | |
152 | + timer.start(ifThis,100*1000); | |
153 | + return rDO|FIN_WAIT; | |
201 | 154 | } |
202 | -TS_STATE(FIN_MASTER_START) | |
155 | +TS_STATE(FIN_WAIT) | |
203 | 156 | { |
204 | - if ( dlist ) | |
205 | - free(dlist); | |
206 | - libusb_exit(NULL); | |
207 | - return rDO|FIN_START; | |
157 | + if ( C_TEST(mon->state,C_ZOM) ) | |
158 | + return rDO|FIN_START; | |
159 | + if ( timer.is_expire(ifThis) ) | |
160 | + return rDO|FIN_DESTROY; | |
161 | + return 0; | |
208 | 162 | } |
209 | 163 | |
210 | 164 | TS_STATE(FIN_START) |
211 | 165 | { |
212 | 166 | |
213 | - REF_SET(ior,0); | |
214 | - REF_SET(iow,0); | |
215 | - REF_SET(ioe,0); | |
216 | - REF_SET(cb,0); | |
217 | - REF_SET(sys,0); | |
167 | + REF_SET(mon,0); | |
218 | 168 | return rDO|FIN_TINYSTATE_START; |
219 | 169 | } |
220 | 170 |