Description of the Example
The standard slave program allows other Bluetooth devices such as cell phones, Palmpilots, PDAs, laptops, and PCs to connect to the Bluetooth serial profile of the AIRcable Industrial. To program the AIRcable Industrial with this new program see Programming AIRcable OS.
Operation Principle
When we end this example you will know how to:
- Open the incoming SPP port to allow other Bluetooth devices to connect
- Allow the user to start a shell for manual commands if required
- Configure the UART for 9600 baud
- Link the wireless SPP port to the UART for streaming data
Code Explanations
This simple slave program demonstrates some of the routines and how to program the AIRcable Industrial. We will first explain you each part of the code, and then will show it as hole so you can see it and try it. We had divided the code in 3 parts, and each one is controlled by a different interrupt.
First part is device intialization, as any embeded solution when the device turns on there are other devices that needs to be initializated, in this simple example we will turn on a LED and intializate the RS232 port, but you can do more complex tasks like for example initializating a Display, a GPS device, or what ever you have connected to the device.
Second part is slave channel manipulation this part will open the slave channel and blink a LED to show that the code is running.
The third and final part is slave connection managing this part of the code will accept all the incoming connections, let the user choose between the Shell or RS232 connection, and will turn on both leds.
There are other routines that we handle on this code, which are not exclusive necessary for an slave connection, but we include them to show how the real code should look like.
Device Intialization
The slave program starts with @ERASE. This is a command to the BASIC loader in the AIRcable when a new program is transmitted via wireless FTP. It will cause the AIRcable to erase its BASIC memory completely. The first routine is @INIT, it runs only once when the system is booted or a new BASIC program has been uploaded. In the following example the green LED is first switched on to indicate that the AIRcable boots. The lines 12 to 19 configure the power to the RS232 level shifter and schedule a hardware interrupt on PIO4, this is DSR input on the RS232 connector.
@ERASE
@INIT 10
0 REM LED output and on
10 A=pioout 2
11 A=pioset 2
0 REM RS232_off set
12 A=pioout 5
13 A=pioset 5
0 REM RS232_on set
14 A=pioout 3
15 A=pioset 3
0 REM DTR output clear
16 A=pioout 6
17 A=pioclr 6
0 REM DSR input
18 A=pioin 4
0 REM set DSR to IRQ so that PIO_IRQ is called
19 A=pioirq "P00010000000"
20 RETURN
Slave Channel Manipulation
This part of the code will open the slave channel to accept incoming connections, and will put the device in discoverable mode. The interrupt we are going to use is @IDLE as it is called when ever there is no bluetooth activity, that means whenever the slave channels closes this can be due to slave connection timeout or lost of a slave connection. This routine will also be the first one to be called as soon as @INIT ends.
The BASIC program opens up the incoming serial slave port (SPP) for a number of seconds (A = slave 5) and then exits immediately. The serial slave port (SPP) stays open for other Bluetooth devices to make connections. If a negative value is being used (for example, A = slave -5) the AIRcable Industrial will not be discoverable during this time, and until slave is called again with a possitive value, in this case other Bluetooth devices must know the address to be able to make connections.
@IDLE 30
0 REM disconnect RS232
30 B = unlink 1
31 B = slave 5
0 REM blink LED
32 B = pioset 2;
33 B = pioclr 2
34 RETURN
Slave connection managing
When a connection to the slave port is successful, meaning another Bluetooth device has initiated a connection to the AIRcable, the @SLAVE routine starts up. The BASIC program can find out who is connecting using the built-in function getconn (for example, A = getconn $0). For a simple slave application the program would link the wireless SPP port to the UART. This routine allows also user to start an interactive session with the built-in shell. When a '+' and an enter is typed in immediately after the connection has been established the AIRcable Industrial starts with a BASIC interpreter shell. It is possible to change lines of the BASIC program, start or stop routines, print variables and call built-in functions.
@SLAVE 400
0 REM 5 seconds timeout to start shell with '+' and enter
400 TIMEOUTS 5
401 INPUTS $0
402 IF $0[0] = 43 THEN 407
403 B = pioset 2
0 REM set baud rate to 9600
404 C = baud 96
0 REM connect RS232
405 C = link 1
406 RETURN
407 A = shell
408 RETURN
Extras
A successful connection requires that the two partners are paired. PIN code is exchanged and then a unique link key is stored on both ends to indicate a successful pairing. The pairing information is stored persistently for future connections that will no longer need a PIN code. Pairing information can be erased, when more than 8 peers are stored, or manually with the unpair function (single peers), or the @UNPAIR command (all peers) at the beginning of a BASIC program upload.
The PIN code that is used for all connections (slave, master, ftp and obex) is configured here in the BASIC program. This way, the application has more control over security. When @PIN_CODE is called, the Bluetooth address of the device requesting connection is available in the $0 string variable. This variable is being used for the PIN code response as well.
@PIN_CODE 440
0 REM fixed PIN code
440 $0="1234"
441 RETURN
The last 2 routines @PIO_IRQ and @CONTROL deal with the handshake lines on the RS232 ports. If the DSR input on the DB9 connection changes, @PIO_IRQ is called. This was already initialized with the earlier instruction "A=pioirq "P00010000000" in the @INIT routine.
The function modemctl transmits a message through an established SPP connection to the other end. Receiving this message will schedule the @CONTROL routine to be executed. In this example we set or reset the DTR pin of our RS232 connector.
@PIO_IRQ 490
0 REM local request for DSR
490 IF $0[4] = 48 THEN 483
0 REM modem control to other side
491 A = modemctl 1
492 RETURN
493 A = modemctl 0
494 RETURN
@CONTROL 495
0 REM remote request for DTR
495 IF $0[0] = 49 THEN 498
496 A=pioset 6
497 RETURN
498 A=pioclr 6
499 RETURN
Entire Program
@ERASE
@INIT 10
0 REM LED output and on
10 A=pioout 2
11 A=pioset 2
0 REM RS232_off set
12 A=pioout 5
13 A=pioset 5
0 REM RS232_on set
14 A=pioout 3
15 A=pioset 3
0 REM DTR output clear
16 A=pioout 6
17 A=pioclr 6
0 REM DSR input
18 A=pioin 4
0 REM set DSR to IRQ so that PIO_IRQ is called
19 A=pioirq "P00010000000"
20 RETURN
31 B = slave 5
0 REM blink LED
32 B = pioset 2;
33 B = pioclr 2
34 RETURN
@SLAVE 400
0 REM 5 seconds timeout to start shell with '+' and enter
400 TIMEOUTS 5
401 INPUTS $0
402 IF $0[0] = 43 THEN 407
403 B = pioset 2
0 REM set baud rate to 9600
404 C = baud 96
0 REM connect RS232
405 C = link 1
406 RETURN
407 A = shell
408 RETURN
@PIN_CODE 440
0 REM fixed PIN code
440 $0="1234"
441 RETURN
@PIO_IRQ 480
0 REM when DSR on the RS232 changes
480 IF $0[4]=48 THEN 483
0 REM modem control to other side
481 A=modemctl 1
482 RETURN
483 A=modemctl 0
484 RETURN
@CONTROL 495
0 REM remote request for DTR pin on the RS232
495 IF $0[0] = 49 THEN 498
496 A=pioset 6
497 RETURN
498 A=pioclr 6
499 RETURN
@IDLE 30
0 REM disconnect RS232
30 B = unlink 1