In the previous article, I described how I prepared a development environment for the Allwinner V851, configured C++ cross-compilation, and ran the first programs directly on the Carlinkit U2W Mini.
I also verified communication with the built-in MFi chip over I²C. The existing CaRaBack implementation worked with almost no changes — I only needed to specify the correct I²C device.
The next step was to port the remaining backend components: Bluetooth, iAP2, AirPlay, and IPC.
Originally, CaRaBack ran on a Raspberry Pi, while Carlinkit was used only to access the MFi chip. The new architecture was designed to run the entire backend directly on the Allwinner V851, with the CaRadio client application connecting to it over the network.
Unexpectedly, Bluetooth turned out to be the most difficult part of the port.
Tina Linux Without BlueZ
On Raspberry Pi, I used the existing BlueZ Bluetooth stack. It handled the controller and low-level protocols, so CaRaBack only needed to communicate through the appropriate interfaces.
The situation on Carlinkit was completely different.
The device ran a heavily modified version of Tina Linux. The system lacked not only SSH, which I installed later, but also BlueZ.
At the same time, the built-in AIC8800D Bluetooth controller had already been initialized by the original system. It was being used by the sdksdl process, so the interface was occupied.
After freeing the controller, I was able to communicate with it directly through the existing device:
/dev/ttyS2
No additional driver modifications or controller firmware loading were required.
The original sdksdl and sdDongle components were later removed from the system.
However, the absence of BlueZ meant that simply porting the existing Raspberry Pi Bluetooth code was not an option.
I had to implement the necessary layers of the Bluetooth stack myself.
Communicating with the Controller Through HCI
The first component was HciTransport, which provides direct communication with the Bluetooth controller over UART.
Communication used the H4 protocol, which allows HCI commands, events, and ACL packets to be transferred over a serial interface.
On Linux, UART communication used a regular file descriptor and termios.
A fragment of the serial interface configuration:
::cfmakeraw(&tty);
::cfsetispeed(&tty, B1500000);
::cfsetospeed(&tty, B1500000);
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag |= CRTSCTS;
This configures a baud rate of 1,500,000, an 8N1 format, and RTS/CTS hardware flow control.
HciTransport itself turned out to be a relatively small class. Its job is to open the device, send and receive bytes, and handle the file descriptor correctly.
But this was only the transport layer. Everything above it still had to be implemented.
Implementing the Bluetooth Stack
Most of the logic gradually moved into BluetoothManager and several supporting classes.
Instead of relying on the Linux Bluetooth stack, the application now handled the required protocols directly.
The architecture looked roughly like this:
CaRaBack
|
v
BluetoothTransport
|
v
RFCOMM
|
SDP / L2CAP
|
HCI / H4
|
v
/dev/ttyS2
|
v
AIC8800D
This is a simplified diagram: SDP is used for service discovery, not for carrying RFCOMM user data.
HCI and Connection Management
BluetoothManager handles controller initialization, HCI events, connection establishment, and authentication.
It also manages pairing keys and connection encryption.
ACL and L2CAP
Once a connection is established, the application needs to receive ACL packets, parse L2CAP data, and handle requests to create and configure logical channels.
Unlike the Raspberry Pi version, these operations were now handled directly by CaRaBack.
SDP
I introduced separate SdpServer and SdpClient classes for service discovery.
They handle SDP requests and retrieve information about available services, including RFCOMM parameters.
RFCOMM
The next layer was RfcommServer.
It handles RFCOMM channel establishment, frame processing, and data flow control.
The implementation supports both incoming connections and the ability to initiate outgoing RFCOMM connections.
Finally, BluetoothTransport provides a read/write interface for higher-level code.
This allows the iAP2 logic to communicate over Bluetooth without directly handling HCI, L2CAP, or RFCOMM.
This is not a universal replacement for BlueZ. I implemented only the parts of the Bluetooth stack required by CaRaBack.
Even so, the amount of code grew considerably.
While the Bluetooth part of the Raspberry Pi version consisted of four files, the Allwinner V851 implementation gradually expanded to thirteen.
The First Connection with an iPhone
Initially, I implemented the most essential scenario — a connection initiated by the phone.
CaRaBack waited for an incoming Bluetooth connection, handled the necessary pairing procedures, and established a channel for further data exchange.
At this stage, automatic reconnection to a previously paired iPhone was not yet implemented.
That was not a problem for development: I could initiate the connection manually from the phone.
The important part was that the Bluetooth stack was now functional enough to move on to the next protocol — iAP2.
iAP2 and AirPlay: Porting with Minimal Changes
After completing the basic Bluetooth implementation, I moved on to porting iAP2 and AirPlay.
This part turned out to be much easier.
The core logic of these protocols had already been implemented and tested on Raspberry Pi. Thanks to the separation between the Bluetooth transport and higher-level components, most of the existing code worked on the Allwinner V851 with almost no changes.
Minor adjustments were needed in platform-dependent components responsible for retrieving system information, including the MAC address.
Otherwise, the port mainly involved compiling the existing project for the new architecture and verifying that it worked.
However, a successful Bluetooth connection and iAP2 communication did not yet mean that the entire CarPlay system was functional. The remaining connection stages still had to be completed, and an actual video stream had to be received from the iPhone.
After getting AirPlay running, I was able to connect to CaRaBack from my computer and see the CarPlay screen.
This was the first confirmation that the backend was actually running on the Allwinner V851, rather than merely using its built-in MFi chip.
The computer acted as the client, while Carlinkit handled the server side.
This was exactly the architecture I had been working toward since deciding to remove CaRaBack's dependency on Raspberry Pi.
Automatic Phone Reconnection
Once AirPlay was working and I could see the CarPlay screen on my computer, I returned to improving Bluetooth.
Until then, connections had been initiated by the iPhone. That was inconvenient for everyday use.
So I added automatic reconnection to a previously paired phone.
This required mechanisms for saving and loading pairing keys, initiating an HCI connection, restoring the necessary channels, and establishing an RFCOMM connection.
In BluetoothManager, this functionality is separated into several methods:
void reconnectStoredDevice();
bool connectStoredDevice();
void reconnectLoop();
bool loadLinkKey();
bool saveLinkKey();
RfcommServer also gained the ability to initiate an outgoing connection:
bool startOutgoingConnection(
uint16_t connection,
uint8_t serverChannel);
CaRaBack could now not only accept incoming connections but also connect to a known device on its own.
Importantly, this functionality was added after the first successful CarPlay session on the V851. It was not required to verify that the system worked, but it was essential for convenient everyday use in a car.
From Adapter to Standalone Server
Porting CaRaBack to the Allwinner V851 turned out to be much more complicated than simply recompiling the project for a different architecture.
MFi required almost no changes, but the absence of BlueZ forced me to implement the Bluetooth subsystem almost from scratch.
At the same time, the project's modular architecture proved its value.
Bluetooth provided the transport for iAP2, while the remaining connection logic and AirPlay could evolve independently of the specific Bluetooth controller.
Seeing the first CarPlay image on my computer was an important milestone in the project's development.
The Carlinkit U2W Mini was no longer just a device with an MFi chip connected to a Raspberry Pi. It was now running CaRaBack independently and handling the iPhone connection.
The next task was to further integrate the server with the CaRadio client and test the entire system under real-world conditions.