Casio personal-I (H-802)

Sometimes you're rewarded for looking around in second hand shops. Today, I found this Casio personal-I calculator in mint condition!

Not bad for the princely sum of £2.00, yes two whole pounds. Since I like a bit of retro, I had to own it, although I'll probably use it. It will look great on my desk at work.

As you can see here:

...it was boxed (the box is in near perfect condition) and has the original instructions and batteries as well. I won't use the batteries, they're pretty old. The actual calculator was still in the protective plastic bag inside the box. It's in such perfect condition I don't think it has ever been switched on.

Of course, the buttons make that nice clicky noise that you only get from electronics of this era. I found more information about this model here.

Islay adventure

We recently had a fantastic holiday on Islay, in the Hebredes. We stayed in a B&B called The Old Excise House which is next door to Laphroaig distillery, and also in walking distance to the Lagavulin and Ardbeg distilleries. It was a brilliant holiday, and The Old Excise House is probably the best accommodation I have ever stayed in.

To give you an idea, here is a photo we took from our bedroom:

...and when you visit the distilleries, they make you very welcome, here is the visitors tasting room at Laphroaig which is just a short walk up the road:

It was totally worth a days driving up from Cambridge, the scenery gets better and better as you go, and when you get past Glasgow it gets really good. So we're already making plans to go back.

Sony MEX-BT3900U Car Stereo Head Unit

I like it when things just work. Recently I replaced the standard Honda stereo head unit in my car for a better one. I wanted something that would work with my iPhone (both to play music and make phone calls) and that I could plug a USB stick into ... as well as the normal CD player, FM radio type stuff.

I managed to get one of these Sony MEX-BT3900U units on special offer (less than half price in fact).

I'm very pleased with it, everything just worked out of the box. It talks nicely to my phone over bluetooth (I can use the USB cable too which means I can charge my phone whilst driving).

I even fitted it myself. If like me, you have steering wheel controls on your car make sure you get the right adaptor. At first I got a basic Autoleads PC2-68-4 which worked fine, without any steering wheel controls. But now that I have the correct adaptor for my car everything works. Very cool.

Formula One and Lego

What better way to mark the impending start of a new season of Formula One than with some Lego:

Yes, we've been buying Lego. Some second hand stuff from eBay and some of the new 'Lego Creator' kits. Such fun. We made the car and then decided that it needed a garage to be kept in. Not a bad way to spend part of a Sunday afternoon.

Simple queuing with SQL Server and C#

I've been working on some simple queuing code using a SQL Server database and some C#. Items are queued by being inserted into a SQL table and then taken off the queue in batches. Multiple processes should be able to remove items from the queue at the same time. Oh, and you should be able to remove a subset of queued records in case you only want to dequeue records that match some criteria. What I came up with is surprisingly simple, the key part is the following SQL:

DELETE FROM TestQueue WITH (READPAST) OUTPUT DELETED.*
WHERE TestQueue.Id IN
(
  SELECT TOP(10) Id -- maximum batch size
  FROM TestQueue
  WHERE Data >= 0 AND Data <= 1000 -- selection criteria
  ORDER BY Id ASC
)

...this way you can set a batch size, meaning you can remove a number of records at a time and you can specify a criteria when you only wish to remove certain items from the queue. The beauty of the above SQL command is that it deletes the records and returns them at the same time. If there aren't enough records in the queue to fill a batch then it returns whatever it finds.

Here is how you could try it out in a C# program:

  1. Create a new Console App in Visual Studio 2012
  2. Add the NuGet package called 'PetaPoco' (which is a micro-ORM)
  3. Create the Queue Table, like this
  4. Set a connection string called "SQLq" in the App.Config of your Console App
  5. Use this program code

The example code will clear the table, then queue 1000 items. Next it will spin up six threads each dequeuing different ranges of items using a variery of different batch sizes. After the process is complete the table should be empty - all the items should have been removed.

Well, OK, this is just test code to show how it works, you'd write it differently in a live system, but hopefully it shows the idea...

Using my WCF http Basic Authentication code in VS 2012

I blogged about the implementation of http basic authentication that I made in this post here, but this is a more detailed description of how to get it working in a simple WCF service in Visual Studio 2012.

First, you'll need to set up a basic out-of-the-box WCF service in Visual Studio 2012:

...and change it slightly so that it can be used from a browser, as described in this post here. You should now be able to call your service from any web browser to see it's working properly.

Personally, I just stuck to the default Microsoft code example and added a [WebGet] attribute, like this:

[WebGet(UriTemplate="getdata?num={value}")]
public string GetData(int value)
{
   return string.Format("You entered: {0}", value);
}

...which means that I can call http://localhost:8080/Service1.svc/getdata?num=1 to pass a querystring value to my method.

Now, add these files to your project (BasicAuthenticationAttribute.cs, BasicAuthenticationHttpHeaderInjector.cs and BasicAuthenticationPasswordValidator.cs). When you've done that, just add the [BasicAuthentication] attribute to your service class. That's it! Try running the project again.

You should be asked for a username and password by the browser. Now, the WCF service will only return some data when you have used these details: username='user' and password='password'. You can change the BasicAuthenticationPasswordValidator class to implement whatever means you like to check that the user is authorised.

You now have a code-only implementation of http basic authentication for WCF. If you're using IIS, you don't need to configure anything to make this work, as far as IIS is concerned you are using anonymous access. But it should also work in IIS Express or the Visual Studio Development Server.

NOTE: this code makes no attempt to encrypt the username and password you type into the browser. The details will be sent in clear text as an http header. You should at least consider using https to encrypt the data. The purpose of this code is just to show how http basic authentication works, and how it could be done in code.

WCF HTTP Basic Authentication in Code

So I wanted to build a WCF service with a very simple authentication mechanism, and I thought that it would be good to start with my own implementation of http basic authentication - and then I could go from there, adding my own extras to the protocol if I wanted. But I thought that basic authentication over https would at least be a start.

And even though the username and password are sent in clear text, doing that over an https connection should be safe since it's encrypted before going over the wire.

Finally, I wanted to do the whole lot in code, so that I can deploy it to some hosted space without fiddling around in IIS. But also because I like code :-)

The good thing is that basic authentication is supported by most browsers, so I could expose the WCF service as web methods and test it very easily. I have assumed that since it works with all the major browsers I have implemented the protocol correctly.

The end result was a custom attribute which can be added to a WCF service and does all the work for you. The only other thing that needs to be done is modify the method which decides if a user is valid or not.

Here is the source code (click the image to download):

I'll post an example of how to use it (in Visual Studio 2012) in another blog.

Virtualise, Wipe and Reinstall

My home PC had been getting slower over time ... over the years I have installed a lot of software that I don't really use anymore, and there were still a bunch of Lenovo tools on there from when it was new. So it was time for a clean reinstall - a reformatted disk with a fresh copy of Windows can work wonders. But I still wanted a full backup of the old machine, just in case. So I thought that it would be good to virtualise the whole machine before I wiped it. Then I could always go back to the old version if I needed anything. Which is normally the case...

Anyway, I used the excellent Disk2VHD tool which can virtualise a hard disk whilst it is still running. So you can create the VHD on the machine that you're virtualising. Very cool.

So I'm now using VirtualBox to boot up the Virtual Hard Disk which has been created. It was a very nice way to achieve a clean install without losing anything, and it was pretty painless. I might do the same thing on my laptop actually...

Halfords Car Battery Problem

If you go to your favourite search engine and search for the term "halfords battery unserviceable warranty", you’ll likely find many threads on various forums from people who have experienced the same problem as me.

A few weeks ago, my car battery, which I bought from Halfords with a 3 year guarantee stopped holding a charge. I would get in the car in the morning and it would not have enough power to start the engine. So I took it back for replacement under the guarantee. The people in the store tested it, and the test machine said the battery was 'unserviceable'.

However, according to the staff at Halfords, this means that the battery had failed for some reason which is "not covered by the guarantee". I could not understand how the battery can fail to hold a charge after normal use and not come under the guarantee. What seemed worse to me, was that they were happy to solve the problem by selling me an identical battery. That just didn't feel right to me.

But... I felt like I didn't have much choice, so under protest I paid for the new battery, even though I thought that it should have been done under the guarantee. But I decided to take this up with Halfords by sending an e-mail to their customer services department when I got home.

An e-mail discussion followed for about two weeks. At first they held their position saying that the fault with the battery was not covered by the guarantee. However, I started pointing out that under the Sale Of Goods Act it would be reasonable for me to expect the battery to last for 3 years, since the battery has "Maintenance Free Battery 3 Year Guarantee" written on the side in big letters:

In the end, whilst Halfords stuck to their guns and even suggested that something may be wrong with the electrics in my car, they decided to refund the cost of the replacement battery anyway. Personally, I don't have any reason to think there is anything wrong with the car. It's a well maintained Honda and everything seems to be fine. But Halfords did indeed send me a cheque for the full price of the battery - so fair play to them. I didn't end up out of pocket.

So if you have a similar problem, then complian to customer services and don't give up. A few e-mails to Halfords customer services may get you your money back. When writing to them, be nice, but firm... and persistent. It may come out in your favor, just for the effort of writing a few e-mails.

Unit Tests in Pelles C on Windows

I said before that I might try and do some unit testing in C, rather than use the features in Visual Studio. It would mean that for H2D2 I can do everything inside Pelles C and won't need to have two IDEs running. But I've never done any unit testing in C before (at least if I have it was so long ago I've forgotten). So I needed to go off and find some means of doing that.

So I found the libtap library which is a unit testing framework for C programs. Well, it is something that implements the Test Anything Protocol which you can read about here. I'm no expert on the Test Anything Protocol, this is the first time I've used it, but for my purposes it looks like it will do fine. As long as I can run code and check for expected results and have the Passes and Failures shown in some sort of output then I'll be happy... But for the moment it looks ideal. I've had to make some minor modifications to get it working in my project (in Pelles C for Windows) but it was not very much effort really. I've just made my own version of tap.c and included it in my project. I had to include an implementation of vasprintf() which I found here rather than rolling my own, but that seems to work quite well for me.

I found libtap after searching for C unit testing on stack overflow in case you're wondering how I found it in the first place.

What I've ended up with is a #define which tells the compiler which version of main() to include. One version is my usual code and the other runs my test suite. I haven't began porting my actual H2D2 unit tests back over from C# yet, but I do have a trivial example running just to check that I can run some unit tests. So this this is all I've got so far:

I've also set it up so that the content of tap.c is not included when I compile normally, so this means that my code isn't bloated with the test framework when I'm not using it.

I've resisted the temptation to mess around with libtap too much, I've only made enough changes to get it working in my project. Thanks need to go to Nik Clayton, who wrote the library and to whoever River Tarnell-7 is for posting his vasprintf() implementation. Thanks! In case it helps any other people using Pelles C who would like to add units tests to their project, here are the files:

Download libtap, adapted for Pelles C on Windows.

I'll probably try this out on the Raspberry Pi soon, because I think it may be useful to have a lightweight unit testing framework for GCC on the Raspberry Pi too. I'll post an update if anything comes of that idea.