There are three steps to creating a SIM application:
Ø First, you write your code
in an appropriate emulator (for example JVM)
Ø Next, you cross compile the
code to suit the SIM you are using
Ø Lastly, you transfer the
code to the card
Obviously, as an intermediate step, you should test the code.
The exact coding style and software used will depend on the type of
SIM being used. Both Multos and JavaCard can be deployed on
GSM-compatible smart cards, so we will look at an example of each
to demonstrate how the code is structured.
JavaCard Example
This first example shows an empty framework for a JavaCard
application, showing the methods that are necessary to use the
application:
public class Loyalty extends javacard.framework.Applet {
public void static install(APDU _apdu) {}
public boolean select() {}
public void process(APDU _apdu) {}
public void deselect() {}
}
The first method is run when the application is installed, while
select() and deselect() are triggered by the 'select' ADPU.
However, it is process() that we are really interested in (with
thanks to Dr Dobb's [http://www.ddj.com]):
public void process(APDU _apdu) {
byte[] baBuffer = _apdu.getBuffer();
if(baBuffer[ISO.OFFSET_CLA] != CLA_LOYALTY)
ISOException.throwIt(ISO.SW_CLA_NOT_SUPPORTED);
switch(baBuffer[ISO.OFFSET_INS]) {
case INS_VALIDATE_PIN:
this.validatePIN(_apdu);
break;
case INS_DEBIT:
this.debit(_apdu);
break;
case INS_CREDIT:
this.credit(_apdu);
break;
case INS_GETBALANCE:
this.getBalance(_apdu);
break;
}
}
Here you can see that an ADPU has been passed into the
application, and is evaluated to make a decision as to what to do.
(In this case, it takes the form of a loyalty application that
increases the points accumulated.)
Multos Example
This example shows a very basic Multos application, which again
(as the above example) simply takes an ADPU in and evaluates its
contents. This example does not take advantage of any of the Multos
APIs that make it such a powerful system, such as those allowing
access to strong cryptography and the Mondex stored cash
system:
#include "multos.h"
#pragma melpublic
unsigned char pData[10]; /* Data
received from terminal */
#pragma melstatic
unsigned char sData[10]; /* Data stored
on card for application. */
void main(void)
{
if(CLA != 0x90) ExitSW(0x6402);
switch(INS)
{
case
0x10:
/* Command_Get Memory */
CheckCase(2);
prmMemoryCopyFixedLength(0x0a,
pData, sData);
ExitLa(0x0a);
break;
case
0x20:
/* Command_Set Memory */
CheckCase(3);
prmMemoryCopyFixedLength(0x0a,
sData, pData);
break;
default:
Exit(0x6404);
}
}
SIM Programming Example Notes
Both of these examples are very simple, but they show that
programming a SIM application is neither complex, nor very
different from any other application. It is important to understand
that the only communication with the outside world is via APDUs
(with all the limitations that involves), and that memory is
very tight (it's measured in bytes, and most applications will
run out of memory in their first draft).
Also note that the output from your SIM application will need to
be formatted into a data block followed by 2 bytes containing hex
90,00 for successful operation.