• R/O
  • SSH
  • HTTPS

globalbase: Commit


Commit MetaInfo

Revisão5969 (tree)
Hora2019-08-10 20:19:16
Autorjoshua

Mensagem de Log

tsDeviceMonitor Linux

Mudança Sumário

Diff

--- modules/tinyState/trunk/src/arch/posix/machine/Linux/h/std/m_include.h (revision 5968)
+++ modules/tinyState/trunk/src/arch/posix/machine/Linux/h/std/m_include.h (revision 5969)
@@ -5,6 +5,7 @@
55
66 #include <linux/if.h>
77 #include <linux/if_tun.h>
8+#include <linux/netlink.h>
89
910 #include "std/mm_include.h"
1011
--- modules/tinyState/trunk/src/arch/posix/machine/Linux/ts/c++/tsDeviceMonitor.cpp (revision 5968)
+++ modules/tinyState/trunk/src/arch/posix/machine/Linux/ts/c++/tsDeviceMonitor.cpp (revision 5969)
@@ -4,6 +4,7 @@
44 #include "_ts/c++/tsDeviceMonitor_.h"
55 #include "ts/c++/tsReadLine.h"
66 #include "ts/c++/tsLog.h"
7+#include "ts/c++/fwIO.h"
78
89 CLASS_TINYSTATE(ts/c++/tsDeviceMonitor,ts/c++/tinyState)
910
@@ -17,15 +18,11 @@
1718 #include "ts/c++/tsDevNull.h"
1819 #include "ts/c++/tsCallBuffer.h"
1920 #include "ts/c++/sTimer.h"
20-#include "ts/c++/tsSystem.h"
21-#include <libusb-1.0/libusb.h>
2221
23-typedef struct detect_list {
24- int vendor;
25- int product;
26-} DETECT_LIST;
22+#define NL_MAX_PAYLOAD 8192
2723
2824
25+
2926 class TS_THISCLASS : public TS_BASECLASS {
3027 public:
3128 tsDeviceMonitor_(
@@ -43,21 +40,11 @@
4340 private:
4441 protected:
4542 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;
5443 sTimer timer;
5544
56- unsigned device_updated:1;
57-
58- DETECT_LIST * dlist;
59- int dlist_length;
60-
45+ int nl_socket;
46+ const char * err_msg;
47+ fwIO * io;
6148 };
6249
6350 TS_END_IMPLEMENT
@@ -100,6 +87,7 @@
10087 {
10188 tsDeviceMonitor * master;
10289
90+ nl_socket = -1;
10391 master = dynamic_cast<tsDeviceMonitor*>(application
10492 ->get_global(getClass()));
10593 if ( master ) {
@@ -128,9 +116,35 @@
128116 ifThis,
129117 ((INTEGER64)1)));
130118 }
119+trace("M");
131120 application->set_global(getClass(),ifThis);
132121 check_listener = 1;
133- libusb_init(NULL);
122+
123+ REF_SET(io,dynamic_cast<fwIO*>(this->application->getFrameWork()));
124+ return rDO|INI_SETUP;
125+}
126+TS_THREAD(INI_SETUP)
127+{
128+ struct sockaddr_nl src_addr;
129+ int ret;
130+
131+ // Prepare source address
132+ memset(&src_addr, 0, sizeof(src_addr));
133+ src_addr.nl_family = AF_NETLINK;
134+ src_addr.nl_pid = getpid();
135+ src_addr.nl_groups = -1;
136+
137+ nl_socket = socket(AF_NETLINK, (SOCK_DGRAM | SOCK_CLOEXEC), NETLINK_KOBJECT_UEVENT);
138+ if (nl_socket < 0) {
139+ err_msg = "DeviceMonitor :: cannot open NETLINK socket\n";
140+ return rDO|FIN_START;
141+ }
142+
143+ ret = bind(nl_socket, (struct sockaddr*) &src_addr, sizeof(src_addr));
144+ if (ret) {
145+ err_msg = "DeviceMonitor :: cannot open NETLINK socket\n";
146+ return rDO|FIN_START;
147+ }
134148 return rDO|ACT_WAIT_HOOK;
135149 }
136150 TS_STATE(ACT_WAIT_HOOK)
@@ -139,82 +153,62 @@
139153 return 0;
140154 return rDO|ACT_START;
141155 }
142-TS_THREAD(ACT_START)
156+TS_STATE(ACT_START)
143157 {
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;
158+ io->read(ifThis,nl_socket);
159+ return ACT_WAIT;
160+}
161+TS_STATE(ACT_WAIT)
162+{
163+ if ( ev->type != TSE_RETURN ) {
164+ if ( is_destroyed() )
165+ return rDO|FIN_MASTER_START;
166+ if ( timer.is_expire(ifThis) ) {
167+ timer.stop();
168+ return rDO|ACT_SEND_EVENT;
164169 }
170+ return 0;
165171 }
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;
172+stdString * msg;
173+ msg = NEW stdString(NL_MAX_PAYLOAD);
174+ int r = recv(nl_socket,(char*)msg->get_str(),NL_MAX_PAYLOAD, 0);
175+ if ( r < 0 )
176+ return rDO|ACT_START;
177+
178+// if ( msg->rx("m",NEW stdRx("(.*)[@]")) ) {
179+ timer.start(ifThis,10*1000);
180+ return rDO|ACT_START;
181+// }
182+
183+ return rDO|ACT_START;
180184 }
181185 TS_STATE(ACT_SEND_EVENT)
182186 {
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);
187+ if ( debug == DM_DEBUG )
188+ tsLog::log_printf(ifThis,LOG_DEBUG,"DeviceMonitor :: INTERRUPT !!\n");
189+ invoke_listen(
190+ NEW stdEvent(TSE_SIGNAL,
191+ ifThis,
192+ ((INTEGER64)0)));
192193 return ACT_WAIT;
193194 }
194-TS_STATE(ACT_WAIT)
195-{
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;
201-}
202195 TS_STATE(FIN_MASTER_START)
203196 {
204- if ( dlist )
205- free(dlist);
206- libusb_exit(NULL);
207197 return rDO|FIN_START;
208198 }
209199
210-TS_STATE(FIN_START)
200+TS_THREAD(FIN_START)
211201 {
212-
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);
202+ if ( nl_socket >= 0 ) {
203+ soCLOSE(nl_socket);
204+ }
205+ return rDO|FIN_FINISH;
206+}
207+TS_STATE(FIN_FINISH)
208+{
209+ if ( err_msg )
210+ tsLog::log_printf(ifThis,LOG_ERROR,err_msg);
211+ REF_SET(io,0);
218212 return rDO|FIN_TINYSTATE_START;
219213 }
220214
Show on old repository browser