Skip to contentSkip to conversation

IBM HD Shock detection in Linux

WISHLIST Niel Lambrechts 8 participants 11 messages Dec 12, 2004 – Dec 14, 2004

lore
Share & tools
lore
#0 lore raw

I picked this up from somewhere on the net, pity that it is not c code...

The code apparently can display the horizon, but cannot prevent shocks :(

Signature · 1 line
Niel Lambrechts <[email protected]>

using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

public sealed class Accelerometer : IDisposable

{
	private const uint GENERIC_READ = 0x80000000;
	private const uint FILE_SHARE_READ = 1;
	private const uint FILE_SHARE_WRITE = 2;
	private const uint OPEN_EXISTING = 3;
	private readonly static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
	private const uint IOCTL_SHOCKMGR_READ_ACCELEROMETER_DATA = 0x733fc;
	private const int FACILITY_WIN32 = unchecked((int)0x80070000);
	private IntPtr handle = INVALID_HANDLE_VALUE;
	private AccelerometerData sample;

	private static int HRESULT_FROM_WIN32(int x)
	{
		return x <= 0 ? x : ((x & 0x0000FFFF) | FACILITY_WIN32);
	}

	private struct AccelerometerData
	{
		internal int status;
		internal short x0;
		internal short y0;
		short x1;
		short y1;
		short x2;
		short y2;
		short x3;
		short y3;
		short x4;
		short y4;
		short x5;
		short y5;
		short x6;
		short y6;
		short x7;
		short y7;
		short x8;
		short y8;
		short x9;
		short y9;
		short x10;
		short y10;
		short x11;
		short y11;
		short x12;
		short y12;
		short x13;
		short y13;
		short unknown0;
		short unknown1;
	}

	[DllImport("kernel32.dll", SetLastError = true)]
	private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

	[DllImport("kernel32.dll")]
	private static extern void CloseHandle(IntPtr handle);

	[DllImport("kernel32.dll", SetLastError = true)]
	private static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref AccelerometerData lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);

	public Accelerometer()
	{
		handle = CreateFile(@"\\.\ShockMgr", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
		if(handle == INVALID_HANDLE_VALUE)
		{
			GC.SuppressFinalize(this);
			Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Marshal.GetLastWin32Error()));
		}
	}

	~Accelerometer()
	{
		CloseImpl();
	}

	private void CloseImpl()
	{
		IntPtr h = handle;
		if(h != INVALID_HANDLE_VALUE)
		{
			handle = INVALID_HANDLE_VALUE;
			CloseHandle(h);
		}
	}

	public void ReadSample()
	{
		uint dwRead = 0;
		if(!DeviceIoControl(handle, IOCTL_SHOCKMGR_READ_ACCELEROMETER_DATA, IntPtr.Zero, 0, ref sample, 0x24, ref dwRead, IntPtr.Zero))
		{
			Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Marshal.GetLastWin32Error()));
		}
	}

	public int Status
	{
		get
		{
			return sample.status;
		}
	}

	public int X
	{
		get
		{
			return sample.x0;
		}
	}

	public int Y
	{
		get
		{
			return sample.y0;
		}
	}

	public void Dispose()
	{
		CloseImpl();
		GC.SuppressFinalize(this);
	}
}

namespace Horizon

{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Timer timer1;
		private Accelerometer sensor = new Accelerometer();

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			sensor.ReadSample();
			Invalidate();
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			float horizontal = 640;
			float vertical = 800;
			float y = sensor.Y;
			float angle = 90 * (y - horizontal) / (vertical - horizontal);
			e.Graphics.DrawString(angle.ToString(), Font, Brushes.Black, 0, 0);
			e.Graphics.TranslateTransform(ClientRectangle.Width / 2, ClientRectangle.Height / 2);
			e.Graphics.RotateTransform(- angle);
			e.Graphics.DrawLine(Pens.Black, - ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, 0);
		}
	}
}
#1 lore raw
Quoted 2 lines

I picked this up from somewhere on the net, pity that it is not c code...

- there is "c" in "c#".
- the code looks really ugly (is this always the case with something that's written under windows?); make a silent rewrite if license allows :)

Quoted 2 lines

The code apparently can display the horizon, but cannot prevent shocks :(

How can something prevent a shock if it does not know before? What I mean is that if I smack a harddrive, it can hardly evade it... nor can it prevent me from smacking it.

Jan Engelhardt

Signature · 1 line
ENOSPC
#2 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 2 lines

I picked this up from somewhere on the net, pity that it is not c code...

- there is "c" in "c#".
- the code looks really ugly (is this always the case with something that's written under windows?); make a silent rewrite if license allows :)

Quoted 6 lines
Quoted 2 lines

The code apparently can display the horizon, but cannot prevent shocks :(

How can something prevent a shock if it does not know before? What I mean is that if I smack a harddrive, it can hardly evade it... nor can it prevent me from smacking it.

It can only prevent shocks when it detects tilts... like when the laptop shakes in a moving vehicle for example..

Niel

#3 lore raw
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

The code apparently can display the horizon, but cannot prevent shocks :(

How can something prevent a shock if it does not know before? What I mean is that if I smack a harddrive, it can hardly evade it... nor can it prevent me from smacking it.

It can only prevent shocks when it detects tilts... like when the laptop shakes in a moving vehicle for example..

Ah that's reasonable, like I'm dropping it (will probably tilt due to physics) and then hit the ground.
But what will it do to prevent against the schock, now that it knows it is tilted?

Jan Engelhardt

Signature · 1 line
ENOSPC
#4 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

The code apparently can display the horizon, but cannot prevent shocks :(

How can something prevent a shock if it does not know before? What I mean is that if I smack a harddrive, it can hardly evade it... nor can it prevent me from smacking it.

Quoted 8 lines
Quoted 2 lines

It can only prevent shocks when it detects tilts... like when the laptop shakes in a moving vehicle for example..

Ah that's reasonable, like I'm dropping it (will probably tilt due to physics) and then hit the ground.
But what will it do to prevent against the schock, now that it knows it is tilted?

Knowing only what I could read here:
http://www.pc.ibm.com/us/thinkpad/xseries/index.html#aps it would seem that it stops the harddrive. How effective that is I don't know - I have no further knowledge or experience with this.

Signature · 1 line
Jesper Juhl
#5 lore raw
Quoted 2 lines

that it stops the harddrive. How effective that is I don't know - I have no further knowledge or experience with this.

stop is a good operation, but I doubt the heads won't scratch the cyls when the disk is falling from a desk's height.

Jan Engelhardt

Signature · 1 line
ENOSPC
#10 lore raw
In reply to Niel Lambrechts
Quoted 2 lines · In article <[email protected]> you wrote:

The code apparently can display the horizon, but cannot prevent shocks :(

This code is not helpfull since it uses a win32 ioctl. One would have to know what this ioctl is doing with the disk. Personally I think it is either using smart or its an april fools joke.

Greetings
Bernd

#8 lore raw
In reply to Jan Engelhardt
Quoted 7 lines · Jan Engelhardt
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

The code apparently can display the horizon, but cannot prevent shocks :(

How can something prevent a shock if it does not know before? What I mean is that if I smack a harddrive, it can hardly evade it... nor can it prevent me from smacking it.

Quoted 7 lines
Quoted 2 lines

It can only prevent shocks when it detects tilts... like when the laptop shakes in a moving vehicle for example..

Ah that's reasonable, like I'm dropping it (will probably tilt due to physics) and then hit the ground.
But what will it do to prevent against the schock, now that it knows it is tilted?

When a tilt is detected, it would park the heads ? so that it would not affected from the larger shock ?

The platter/head is affected in a case where the arm swings away, even from the powerful magnet to crash against the platter. If the arm is locked, such that it does not move when a tilt is detected, (The tilt before the shock) the arm is parked and locked (more locking than the simple magnet) ?

Manu

Quoted 4 lines

Jan Engelhardt

#9 lore raw
In reply to Niel Lambrechts

Niel Lambrechts wrote (ao):

Quoted 10 lines · Jan Engelhardt
Quoted 6 lines
Quoted 2 lines

The code apparently can display the horizon, but cannot prevent shocks :(

How can something prevent a shock if it does not know before? What I mean is that if I smack a harddrive, it can hardly evade it... nor can it prevent me from smacking it.

It can only prevent shocks when it detects tilts... like when the laptop shakes in a moving vehicle for example..

Of course it can't prevent shocks :-)

It can try to minimalize the impact of a shock though.

I am a happy owner of a X40. Just got it, so it still runs XP. The software parks the heads of the disk if it detects movement. It is very sensitive and quick so I believe it can protect the harddisk if the notebook gets dropped during use.

In a car the software adapts to the usual bumps so the notebook is still useful :-)

The windows software shows a picture of the notebook in 3D which tilts if you tilt the actual notebook. Neat :-)

#6 lore raw
In reply to Jan Engelhardt
Quoted 5 lines · Jan Engelhardt
Quoted 2 lines

that it stops the harddrive. How effective that is I don't know - I have no further knowledge or experience with this.

stop is a good operation, but I doubt the heads won't scratch the cyls when the disk is falling from a desk's height.

It's not going to get the platter spun down, but it might survive if it managed to get the head off the platter and into the cradle. If we figure it's got a 20ms seek time (laptop drive, should be ~right) it should be able to get the heads off to the side within about 4cm... clever :-)

Quoted 1 line

Jan Engelhardt

#7 lore raw

[OT] IBM Active Protection System (Re: [WISHLIST] IBM HD Shock detection in Linux)

In reply to Jesper Juhl

In article <[email protected]> (at Sun, 12 Dec 2004 23:48:13 +0100 (CET)), Jesper Juhl <[email protected]> says:

Quoted 7 lines
Quoted 3 lines

But what will it do to prevent against the schock, now that it knows it is tilted?

Knowing only what I could read here:
http://www.pc.ibm.com/us/thinkpad/xseries/index.html#aps it would seem that it stops the harddrive. How effective that is I don't know - I have no further knowledge or experience with this.

FYI: You can find their descriptions and stories in Japanese at:
<http://www-6.ibm.com/jp/pc/design/haps.html>
<http://www-6.ibm.com/jp/pc/interview/haps/>.

--yoshfuji