views:

89

answers:

2

I am writing an application in Qt to be deployed on Symbian S60 platform. Unfortunately, it needs to have Bluetooth functionality - nothing really advanced, just simple RFCOMM client socket and device discovery. To be exact, the application is expected to work on two platforms - Windows PC and aforementioned S60.

Of course, since Qt lacks Bluetooth support, it has to be coded in native API - Winsock2 on Windows and Symbian C++ on S60 - I'm coding a simple abstraction layer. And I have some problems with the discovery part on Symbian.

The discovery call in the abstraction layer should work synchronously - it blocks until the end of the discovery and returns all the devices as a QList. I don't have the exact code right now, but I had something like that:

RHostResolver resolver;
TInquirySockAddr addr;
// OMITTED: resolver and addr initialization

TRequestStatus err;
TNameEntry entry;
resolver.GetByAddress(addr, entry, err);
while (true) {
    User::WaitForRequest(err);
    if (err == KErrHostResNoMoreResults) {
       break;
    } else if (err != KErrNone) {
        // OMITTED: error handling routine, not very important right now
    }

    // OMITTED: entry processing, adding to result QList

    resolver.Next(entry, err);
}
resolver.Close();

Yes, I know that User::WaitForRequest is evil, that coding Symbian-like, I should use active objects, and so on. But it's just not what I need. I need a simple, synchronous way of doing device discovery.

And the code above does work. There's one quirk, however - I'd like to have a timeout during the discovery. That is, I want the discovery to take no more than, say, 15 seconds - parametrized in a function call. I tried to do something like this:

RTimer timer;
TRequestStatus timerStatus;
timer.CreateLocal();

RHostResolver resolver;
TInquirySockAddr addr;
// OMITTED: resolver and addr initialization

TRequestStatus err;
TNameEntry entry;

timer.After(timerStatus, timeout*1000000);

resolver.GetByAddress(addr, entry, err);
while (true) {
    User::WaitForRequest(err, timerStatus);

    if (timerStatus != KRequestPending) { // timeout
        resolver.Cancel();
        User::WaitForRequest(err);
        break;
    }

    if (err == KErrHostResNoMoreResults) {
        timer.Cancel();
        User::WaitForRequest(timerStatus);
        break;
    } else if (err != KErrNone) {
        // OMITTED: error handling routine, not very important right now
    }

    // OMITTED: entry processing, adding to result QList

    resolver.Next(entry, err);
}
timer.Close();
resolver.Close();

And this code kinda works. Even more, the way it works is functionally correct - the timeout works, the devices discovered so far are returned, and if the discovery ends earlier, then it exits without waiting for the timer. The problem is - it leaves a stray thread in the program. That means, when I exit my app, its process is still loaded in background, doing nothing. And I'm not the type of programmer who would be satisfied with a "fix" like making the "exit" button kill the process instead of exiting gracefully. Leaving a stray thread seems a too serious resource leak.

Is there any way to solve this? I don't mind rewriting everything from scratch, even using totally different APIs (as long as we're talking about native Symbian APIs), I just want it to work. I've read a bit about active objects, but it doesn't seem like what I need, since I just need this to work synchronously... In the case of bigger changes, I would appreciate more detailed explanations, since I'm new to Symbian C++, and I don't really need to master it - this little Bluetooth module is probably everything I'll need to write in it in foreseeable future.

Thanks in advance for any help! :)

A: 

The code you have looks ok to me. You've missed the usual pitfall of not consuming all the requests that you've issued. Assuming that you also cancel the timer and do a User::WaitForRequest(timerStatus) inside you're error handing condition, it should work.

I'm guessing that what you're worrying about is that there's no way for your main thread to request that this thread exit. You can do this roughly as follows:

  • Pass a pointer to a TRequestStatus into the thread when it is created by your main thread. Call this exitStatus.
  • When you do the User::WaitForRequest, also wait on exitStatus.
  • The main thread will do a bluetoothThread.RequestComplete(exitStatus, KErrCancel) when it wants the subthread to exit, where bluetoothThread is the RThread object that the main thread created.
  • in the subthread, when exitStatus is signalled, exit the loop to terminate the thread. You need to make sure you cancel and consume the timer and bluetooth requests.
  • the main thread should do a bluetoothThread.Logon and wait for the signal to wait for the bluetooth thread to exit.

There will likely be some more subtleties to deal correctly with all the error cases and so on.

I hope I'm not barking up the wrong tree altogether here...

MathewI
Of course, there is no User::WaitForRequest that accepts three TRequestStatus's. However, in this case, you could get away with using User::WaitForAnyRequest() since you know that only the three TRequestStatus's you have will ever be used to signal the thread. Not quite as pretty but should work. It may be better to redo it using active objects, but that makes initializing exitStatus harder (since it will be owned by one of your active objects, so would have to be passed from the subthread back to the main thread).
MathewI
Thanks for your help... I was thinking about creating a separate thread to perform the discovery, and using active objects instead of `User::WaitForRequest` there... The main thread would be waiting for it then. What do you think about that kind of approach?
kFYatek
Oh, and by the way - as far as I understand the API documentation, there _is_ a `WaitForRequest` for three or more `TRequestStatus` objects - I'm talking about `WaitForNRequest` - it takes an array of pointers to `TRequestStatus` and size of this array. However, I don't really get your approach - am I supposed to create a new `RThread` just to cancel it later? What's the point in that? I suppose there's some thread implicitly created by the API, to which I don't have explicit access...
kFYatek
When I looked at my real code right now, I saw few addidional `WaitForRequest` calls I added to "make sure I won't miss something" - when I removed them and left the code in exactly the same state as in the question, it... worked. Oh well, I don't understand Symbian :/
kFYatek
A: 

The question is already answered, but... If you'd use active objects, I'd propose you to use nested active scheduler (class CActiveSchedulerWait). You could then pass it to your active objects (CPeriodic for timer and some other CActive for Bluetooth), and one of them would stop this nested scheduler in its RunL() method. More than this, with this approach your call becomes synchronous for the caller, and your thread will be gracefully closed after performing the call.

If you're interested in the solution, search for examples of CActiveSchedulerWait, or just ask me and I'll give your some code sample.

Haspemulator