Bluetooth

Syrus 4G allows application developers to create low energy (BLE) applications for their IoT projects. The version of BLE that Syrus 4G uses is 4.2.

Developers can interact with the Syrus 4 device through the following defined Bluetooth services and characteristics:

Service: Communication

  • UUID: 00000000-dc70-0080-dc70-a07ba85ee4d6
  • Description: All the communication Protocol is going here

Characteristic: S4GBT Main Console

  • UUID: 00000000-dc74-0180-dc74-a07ba85ee4d6
  • Description: Allows outside users to command the Syrus 4 with either Linux or apx-tool commands. Users will receive the response generated by these commands.
  • Functions: Allows write, and notification.

Characteristic: S4GBT Destination Point

  • UUID: 00000000-dc74-0280-dc74-a07ba85ee4d6
  • Description: Will act as a Bluetooth Destination Point where all events generated within Apex will be sent to the Bluetooth device that is using this characteristic.
  • Functions: Notifications

Characteristic: S4GBT User Application Console

  • UUID: 00000000-dc74-0380-dc74-a07ba85ee4d6
  • Description: Allows a "user developed application" that runs internally in Apex to communicate with a "user developed application" that runs externally in another device.
  • Functions: Allows write, read, and notification.

Security

Communication between the user and the S4GBT Main Console must be authenticated. The authentication process consists of using the corresponding apx-bt command with the last five digits of the IMEI for the default password. Users are recommended to change this default password immediately, the new password can be up to 30 characters long with only alphanumeric characters allowed.

Explanation

Advertising

By default Syrus 4 will go advertise for 5 minutes after it's turned on, when it does advertising it can be found with the name Syrus 4 BT xxxxx where xxxxx are the last five digits of the IMEI. The configuration of the advertising can be done with the apx-bt tool, for example:

$ sudo apx-bt advertising mode,flag,duration
mode,flag,durationdescription
modeEither 0 or 1 for disable and enable respectively.
flagTo get the desired on state flag, simply add the hex numbers from flag Number.
durationA value in seconds between 30 to 300 seconds. A number more than 300 will force advertising duration to be 600 which means advertising is continuous, meaning advertising will always be on. Default it continuous (600).
flag NumberNot addedAdded
0x001Local Random AddressLocal Public Address
0x002Non DiscoverableDiscoverable
0x004Non ConnectableConnectable
0x010Advertise Name OffAdvertise Name
0x020Advertise Tx Pwr OffAdvertise Tx Pwr
0x040No AppearanceAdvertise Appearance
0x100Peer Random AddressPeer Public Address

Note that the default flag Number used when this key is not defined is 0x0016 where a random local address is used that is discoverable and connectable, and exposes the device name

Services and Characteristics

The communication service contains three different characteristics:

S4GBT Main Console

  • Users must first authenticate the connection by sending apx-bt auth xxxxx where xxxxx is the security password. By default, the security password will be the last five digits of the IMEI.
  • Once secure connection is established, the user may proceed to send any apx-tool related command to the device. Furthermore, only a limited amount of BLE commands are allowed, refer to table below.
  • Users must have notifications enabled to receive immediate responses from the device.
  • Large messages, more than 20 bytes, will be broken into packets of 20 bytes and all will be received.
  • Employs a communication protocol where all messages received must be JSON format and contain the following keys: len, id, cmd, and crc.
  • Messages sent also use JSON format and contain the following keys: len, id, res, and crc.
Valid BLE CommandsDescription
apx-System tool commands
uname -aReturns linux information on the name of the device
dateReturns a date in ISOFORMAT

S4GBT Destination Point

  • Will act as a destination point that will send notifications to all of the devices connected, authenticated, and are subscribed to receive notifications.
  • Notifications are events that were generated, managed, and are ready to be dispatched.
  • By default all event messages are sent in JSON format.
  • If a different format is needed, the message must first go to a user created application that will translate that message to the desired format then send to the Bluetooth core application for notification to the connected devices.
  • The user created application must use the apx-bt send command to notify outside applications.

S4GBT User Application Console

  • Allows an over the air link between an internal user created application that runs within Apex and an external user created application.
  • This will allow an external application to send messages to the Syrus 4 through BLE and these messages are internally published to the channel bluetooth/user_apps_console/MESSAGE
  • The external application must be able to break the messages into packets of 20 bytes and send them to Syrus 4. Only applications that are authenticated and connected can use this
  • Messages received from an external application must include a header to let the Bluetooth core application know to which user internal application the message must go to
  • Messages coming from the user internal application must include a header to let the Bluetooth core application know to which device the message must go to
  • Internal user applications must subscribe to a Redis channel bluetooth/user_apps_console/path. Where path is the path given to the user application.
  • User external applications should handle some form of encryption to encrypt messages when communicating with user internal applications. This is due to messages being sent through public channels.
  • User external application must be able to break the messages into packets of 20 bytes and send them to Syrus 4.

Header: sample_terminal

# Sample download of an application and installation using syrus-apps-manager
$ wget https://server.com/applications/terminal_1.0.zip -O /data/downloads/terminal_1.0.zip
$ syrus-apps-manager install-app terminal /data/downloads/terminal_1.0.zip
$ syrus-apps-manager create-instance sample_terminal terminal 1.0
$ Syrus-apps-manager start sample_terminal 

Subscribe:

bluetooth/user_apps_console/sample_terminal

bluetooth/user_apps_console/incoming only when header is not present

Syrus 4 publishes a message when the external application subscribes itself for notifications

"PUBLISH" "bluetooth" "{\"address\":\"602C7E1A683E\",\"user_apps_console\":true}" 

"PUBLISH" "bluetooth" "{\"address\":\"602C7E1A683E\",\"events\":true}"

Security

Two commands will be used to handle the security for using the S4GBT Main Console. One for authenticating and another to change the password.

  • Any device that wishes to use the other characteristics must be authenticated.
  • The very first action a user must do before utilizing the S4GBT Main Console is to authenticate the connection.
  • If the connection is not authenticated within two minutes, then the user trying to use S4GBT Main Console will be disconnected. The user needs to constantly send a message within 2 min to the main console in order to maintain the connection alive.
  • Failing to authenticate three or more times will cause the user to be disconnected.
  • A user may use the correct command to change the password.

Tool Schema

Advertising

The following commands can be used to edit and view the key values related with Advertising.

$sudo apx-bt advertising

The above command will return all three elements corresponding to advertising, refer to advertising.

$sudo apx-bt advertising 1,0x16,200

The above command will take a string separated by commas where the first parameter is the mode where 0 means disabled and 1 means enabled. The second parameter is the flag for the type of advertising to do, refer to advertising. The third parameter is the duration for how long to keep advertising; this takes a number from 30 to 300 seconds and any number above 300 will declare the advertising to be in continuous mode where Syrus 4 will constantly keep advertising.

Services and Characteristics

A new command must be introduced so that messages can go to their corresponding characteristics.

$sudo apx-bt send characteristic message

The new command send will expect one of the characteristics that are already defined: events and u_console. The message is already a formatted message that is ready to be written or sent as a notification.

Security

To use the S4GBT Main Console, the user must authenticate the connection by sending the following command through the Bluetooth connection:

$sudo apx-bt auth 01234

Where 01234 are the last five digits of the IMEI.
The user may change this password by sending the command:

$sudo apx-bt change_auth 01234 passphrase

Where 01234 is the old password and passphrase is the new password to use. The passphrase can be up to 30 alphanumeric characters long.

Walkthrough

Detailed Explanation

The following section is a detailed explanation of the communication protocol used by each of the characteristics.

Using the BLE functions of the apx-bt tool, you can place the device in advertising mode so that it's discoverable and connectable for example.

$sudo apx-bt advertising 1,0x16,600

Using the following service UUID 00000000-dc70-0080-dc70-a07ba85ee4d6, an application developer can connect to the services and get the characteristics.

S4GBT Main Console

  • UUID: 00000000-dc74-0180-dc74-a07ba85ee4d6
  • Send authentication message in JSON format before sending any command
  • The JSON object must have the following keys:
    • len
      • Will contain a string representing a numerical value of characters of the entire message. The number must have leading zeros. The number of characters for this string must always be five characters. Max "99999".
      • Example: "len":"00071"
      • This key must be the first key in the message.
    • id
      • Will contain a number of ten digits long, this number will be the epoch when the message was created or sent.
      • Example: "id":1614876607
      • This key must be the second key in the message.
    • cmd
      • Will be the command the Syrus 4 should attempt to execute.
      • This command must be enclosed in double quotes.
      • The command send inside the double quotes can have spaces which will count towards the len.
      • Example: "cmd":"apx-bt auth 99733"
      • This key must be the third key in the message.
    • crc
      • Will be the CRC-16/ARC calculated from the cmd sent. This includes the double quotes.
      • This will be a number of exactly five characters long and the number is treated as a string with leading zeros when applicable.
      • Example: "crc":"42606"
      • This key must be the fourth key in the message.
      • This is the decimal representation of the number.
  • Note that the JSON message is minified, so a complete valid message will look like this:
`{"len":"00071","id":1614876607,"cmd":"apx-bt auth 99733","crc":"42606"}`
  • Message responses from the Syrus 4 device will also be formatted in JSON and will contain the following keys:
    • len
      • Same as the len for receiving messages.
    • id
      • The same id that was received.
    • res
      • Will be a JSON formatted response from the command that was executed. This could be a string enclosed in {} or " ".
      • Example: "res":"authenticated"
      • This key will be the third key in the message.
    • crc
      • Will be the CRC-16/ARC calculated from the res to be received. This includes double quotes or {}.
      • Same format as message received.
      • This key will be the fourth key in the message.
  • A complete valid response message will look like this:
`{"len":"00067","id":1614876607,"res":"authenticated","crc":"11272"}`
  • Messages received from the Syrus 4 will be broken into packets of 20 bytes, (int(len/20) + 1) packets. Meaning that there will be (int(len/20) + 1) notifications made to send the entire message.
  • To validate the message, notice how many opening and closing { } exist and the count must be equals between opening and closing. Also the len can be used to assure that the length of the entire message is correct. The id can be used to confirm that the response belongs to the cmd message sent. Finally, the crc can be checked to see that the res message was valid and no errors forming the entire message occurred.
  • Since messages sent and received have fixed key and value lengths; except cmd and res, the base length of messages will be 52 characters long. The additional length is the length of the entire cmd or res including { } or " ".

S4GBT Destination Point

  • UUID: 00000000-dc74-0280-dc74-a07ba85ee4d6
  • Authentication is required through the S4GBT Main Console characteristic before receiving notification messages from Syrus 4.
  • Must only allow subscription to notifications
  • All messages received from this characteristic will be JSON.

S4GBT User Application Console

  • Allows a user external application to communicate with a user created internal application that runs inside of the Syrus 4 device.
  • Anything written to this characteristic will be forwarded to the internal user application via Redis publishes.
  • Users can define their own communication protocol between internal and external applications if desired.
  • Any messages from the internal user application that need to go to the external application will be notified as is; meaning they are not edited or modified by Syrus 4.

Developer Notes

To properly distinguish between devices that provide custom services, Syrus 4 will provide the following service with its corresponding characteristics:

ServiceID: 00000000-dc70-0080-dc70-a07ba85ee4d6
ServiceName: Syrus Communication Service

UUID Characteristic: 00000000-dc74-0180-dc74-a07ba85ee4d6
Characteristic Name: S4GBT Main Console

UUID Characteristic: 00000000-dc74-0280-dc74-a07ba85ee4d6
Characteristic Name: S4GBT Destination Point

UUID Characteristic: 00000000-dc74-0380-dc74-a07ba85ee4d6
Characteristic Name: S4GBT User Application Console

  • Developers creating Interface Application can give the service and characteristics their correct name based on the UUID read.
  • BLE connection between an external device and Syrus 4 requires that the external device uses the S4GBT Main Console at least once every two minutes. If this is not done, then the external device will be disconnected from the Syrus 4.
  • Devices that are connected to Syrus 4 must be authenticated before using any of the characteristics offered. A device only has three chances to authenticate before being disconnected by Syrus 4.
  • The default password to authenticate a BLE connection is the last five digits of the Syrus 4 IMEI.
  • The authentication message needs to be the first message the developer’s application has to write to the Syrus 4 through the Main Console characteristic.
    • Authentication Message: {"len":"00071","id":1614876607,"cmd":"apx-bt auth 99733","crc":"42606"}. Where 99733 is the last five digits of the IMEI in this particular Syrus 4 example.
  • Developer’s application need to be subscribed to notifications from Main Console and expect the following message once authentication was successful:
    {"len":"00067","id":1614876607,"res":"authenticated","crc":"11272"}