Sleep &*Wake up control

I always wanted to be able to use regular sleep of the Mac, but some of the issues were holding me back and making me stick to the deep sleep. The main problem was the risk of waking the Mac up at wrong moment. It is quite easy to bump into the controller when putting my stuff into the car. And if the ignition is not on at that moment (which is pretty likely), the P1900 detects high current and kills the mini.

Another issue we had to solve was the connection of the power button for the dock. Carnetix supplies an "Y" cable which allows the power button to be controlled by the P1900. Unfortunately, this connection is very difficult to impossible to incorporate into a dock. We did not want to install additional connectors to the mini's back side. So we had to find another solution. And well, we did find it.

It is well known, that any activity on the USB wakes Mac up if it is in the normal sleep mode. Pressing a button on a keyboard or a mouse, adding or removing a device. We can use this behaviour to both wake the mini up at the right time and prevent it from waking up when not appropriate.

The key is in the way the USB devices are powered. They can be either powered from the bus itself, or they can use an external power supply. Most of the hubs are bus-powered, but also allow connection of external power supply. Some of them use this power supply to completely power the hub, some only use the external power supply to distribute power to the connected devices, while the hub's own logical circuits are always powered from the bus.

If a USB hub can be completely powered by an external power supply (such as P5V or secondary output of P1900), it is possible to cut the power that the hub gets over the USB cable either by cutting the red wire in the cable or by insulating the pin 1 on the USB connector by piece of adhesive tape or similar material. When this is done, the hub's power state can be controlled simply by turning the external power on and off. If the power is on, the hub operates normall. If the power is off, the hub is also off and is not visible on the USB. This is also true for any devices connected to the hub. So if the Mac is asleep and a keyboard is connected to hub, which is turned off, the press of a key on this keyboard will not wake up the Mac.

And it get even better. When the hub is powered up, it becomes visible to the Mac over the USB. If the Mac is sleeping at the time, this will trigger wake-up. Therefore if the power to the hub is supplied by a source, that follows the state of the ignition/acc line, the Mac will never wake up when the ignition is off and will always wake up when the ignition is turned on. This leaves us with the last problem - how to put the computer to sleep?

The solution is again pretty simple. When the ignition is turned off, the USB devices are powered down and become invisible to the computer. The solution is to run a simple application on the Mac, that controls the presence of certain device(s). When the devices are not accessible, it means the hub has been powered down because the ignition is down. So when the application detects the device it watches is missing, it puts the computer to sleep.

The "USB watchdog" module for QCar is just being tested. I will post more details about the wiring and the relevant pieces of code probably over the next weekend. It should be easy to use this principle in other applications as well.

The solution is to run a simple application on the Mac, that controls the presence of certain device(s). When the devices are not accessible, it means the hub has been powered down because the ignition is down. So when the application detects the device it watches is missing, it puts the computer to sleep.

Jirka,
Unless there is something else you are doing with USB Watchdog, you could have it watch the Hub itself. The main benefits are that A) if they just disconnect the watched device (say a HDD, GPS, etc...) to do something else with it, down goes the Mac. Also if they they plug it into the Mac directly to it's USB power supply without changing their configuration, then the Mac doesn't sleep.

It's been awhile since I looked, but I believe that there was USB browser example in the XCode examples or on the Apple Developer site in the IOKit section.

Unless there is something else you are doing with USB Watchdog, you could have it watch the Hub itself.

that's what I did in the end, because I was too lazy to play around with the IOKit :-)

So I wrote a simple script:

Code:

#!/bin/sh
while [ 1 ]
do
ioreg | grep -c USBHub
sleep 1
done

which runs in the background and every second reports the number of connected USB hubs. The "correct" number is set in the preferences of the watchdog and the sleep action is triggered whenever the watchdog detects hub removal.

that's what I did in the end, because I was too lazy to play around with the IOKit :-)

Why do you think I don't mess around with IOKit and prefer to use USB -> Serial

You still run some risks that way (say a keyboard with a hub (like the Apple keyboard) gets plugged directly into the Mac and left plugged in for some reason). I'm just anal retentive about things like that though, so you can ignore me

You still run some risks that way (say a keyboard with a hub (like the Apple keyboard) gets plugged directly into the Mac and left plugged in for some reason).

Yes, that is a risk and this situation must be prevented by appropriate installation planning. In my case, only the CDMA modem is plugged directly into the Mac, everything else goes thru a hub (including the connectors in the dashboard).

I'm just anal retentive about things like that though, so you can ignore me