1 #include <Application.h>
7 #include <FindDirectory.h>
9 #include <NodeMonitor.h>
16 #include "DeviceManager.h"
17 #include "LocalDeviceImpl.h"
20 #include "BluetoothServer.h"
22 #include <bluetoothserver_p.h>
26 DeviceManager::MessageReceived(BMessage
* msg
)
28 if (msg
->what
== B_NODE_MONITOR
) {
30 if (msg
->FindInt32("opcode", &opcode
) == B_OK
) {
39 TRACE_BT("Something new in the bus ... ");
41 if ((msg
->FindInt32("device", &ref
.device
)!=B_OK
)
42 || (msg
->FindInt64("directory", &ref
.directory
)!=B_OK
)
43 || (msg
->FindString("name", &name
) != B_OK
))
46 TRACE_BT("DeviceManager: -> %s\n", name
);
50 // Check if the entry is a File or a directory
51 if (dir
.SetTo(&ref
) == B_OK
) {
52 printf("%s: Entry %s is taken as a dir\n", __FUNCTION__
, name
);
54 dir
.GetNodeRef(&nref
);
58 printf("%s: Entry %s is taken as a file\n", __FUNCTION__
, name
);
65 TRACE_BT("Something removed from the bus ...\n");
71 case B_DEVICE_MOUNTED
:
72 case B_DEVICE_UNMOUNTED
:
74 BLooper::MessageReceived(msg
);
83 DeviceManager::AddDirectory(node_ref
*nref
)
85 BDirectory
directory(nref
);
86 status_t status
= directory
.InitCheck();
88 TRACE_BT("AddDirectory::Initcheck Failed\n");
92 status
= watch_node(nref
, B_WATCH_DIRECTORY
, this);
94 TRACE_BT("AddDirectory::watch_node Failed\n");
99 // BString str(path.Path());
101 // TRACE_BT("DeviceManager: Exploring entries in %s\n", str.String());
105 while ((error
= directory
.GetNextRef(&ref
)) == B_OK
) {
106 // its suposed to be devices ...
110 TRACE_BT("DeviceManager: Finished exploring entries(%s)\n", strerror(error
));
112 return (error
== B_OK
|| error
== B_ENTRY_NOT_FOUND
)?B_OK
:error
;
117 DeviceManager::RemoveDirectory(node_ref
* nref
)
119 BDirectory
directory(nref
);
120 status_t status
= directory
.InitCheck();
124 status
= watch_node(nref
, B_STOP_WATCHING
, this);
129 while (directory
.GetNextEntry(&entry
, true) == B_OK
) {
132 BMessage
msg(B_NODE_MONITOR
);
133 msg
.AddInt32("opcode", B_ENTRY_REMOVED
);
134 msg
.AddInt32("device", nref
->device
);
135 msg
.AddInt64("directory", nref
->node
);
136 msg
.AddString("name", ref
.name
);
137 //addon->fDevice->Control(NULL, NULL, msg.what, &msg);
145 DeviceManager::AddDevice(entry_ref
* ref
)
148 BString
* str
= new BString(path
.Path());
150 BMessage
* msg
= new BMessage(BT_MSG_ADD_DEVICE
);
151 msg
->AddInt32("opcode", B_ENTRY_CREATED
);
152 msg
->AddInt32("device", ref
->device
);
153 msg
->AddInt64("directory", ref
->directory
);
155 msg
->AddString("name", *str
);
157 TRACE_BT("DeviceManager: Device %s registered\n", path
.Path());
158 return be_app_messenger
.SendMessage(msg
);
162 DeviceManager::DeviceManager() :
163 fLock("device manager")
169 DeviceManager::~DeviceManager()
176 DeviceManager::LoadState()
186 DeviceManager::SaveState()
193 DeviceManager::StartMonitoringDevice(const char *device
)
198 BDirectory directory
;
202 if ((err
= path
.Append(device
)) != B_OK
) {
203 printf("DeviceManager::StartMonitoringDevice BPath::Append() error %s: %s\n", path
.Path(), strerror(err
));
208 if ((err
= directory
.SetTo(path
.Path())) != B_OK
) {
209 /* Entry not there ... */
210 if (err
!= B_ENTRY_NOT_FOUND
) { // something else we cannot handle
211 printf("DeviceManager::StartMonitoringDevice SetTo error %s: %s\n", path
.Path(), strerror(err
));
215 if ((err
= create_directory(path
.Path(), S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)) != B_OK
216 || (err
= directory
.SetTo(path
.Path())) != B_OK
) {
217 printf("DeviceManager::StartMonitoringDevice CreateDirectory error %s: %s\n", path
.Path(), strerror(err
));
223 if ((err
= directory
.GetNodeRef(&nref
)) != B_OK
) {
224 printf("DeviceManager::StartMonitoringDevice GetNodeRef error %s: %s\n", path
.Path(), strerror(err
));
228 // start monitoring the root
229 status_t error
= watch_node(&nref
, B_WATCH_DIRECTORY
, this);
233 TRACE_BT("DeviceManager: %s path being monitored\n", path
.Path());
235 // We are monitoring the root we may have already directories inside
238 while ((error
= directory
.GetNextRef(&driverRef
)) == B_OK
) {
240 // its suposed to be directories that needs to be monitored...
241 BNode
driverNode(&driverRef
);
243 driverNode
.GetNodeRef(&driverNRef
);
244 AddDirectory(&driverNRef
);
247 TRACE_BT("DeviceManager: Finished exploring entries(%s)\n", strerror(error
));
250 HCIDelegate
*tmphd
= NULL
;
253 // TODO!! ask the server if this needs to be monitored
255 while ((tmphd
= (HCIDelegate
*)fDelegatesList
.ItemAt(i
++)) !=NULL
) {
257 /* Find out the reference*/
258 node_ref
*dnref
= (node_ref
*)tmphd
->fMonitoredRefs
;
259 if (*dnref
== nref
) {
260 printf("StartMonitoringDevice already monitored\n");
261 alreadyMonitored
= true;
273 DeviceManager::StopMonitoringDevice(const char *device
)
277 BDirectory directory
;
279 if (((err
= path
.Append(device
)) != B_OK
)
280 || ((err
= directory
.SetTo(path
.Path())) != B_OK
)
281 || ((err
= directory
.GetNodeRef(&nref
)) != B_OK
))
284 // test if still monitored
286 bool stillMonitored = false;
288 while ((tmpaddon = (_BDeviceAddOn_ *)fDeviceAddons.ItemAt(i++)) !=NULL) {
289 if (addon == tmpaddon)
293 node_ref *dnref = NULL;
294 while ((dnref = (node_ref *)tmpaddon->fMonitoredRefs.ItemAt(j++)) != NULL) {
295 if (*dnref == nref) {
296 stillMonitored = true;
305 node_ref *dnref = NULL;
307 while ((dnref = (node_ref *)addon->fMonitoredRefs.ItemAt(j)) != NULL) {
308 if (*dnref == nref) {
309 addon->fMonitoredRefs.RemoveItem(j);
316 // stop monitoring if needed
317 if (!stillMonitored) {
318 if ((err = RemoveDirectory(&nref, addon)) != B_OK)