We can now monitor the state of various cameras attached to our PC. Now we would like to send the status to a device on the LAN that will turn a light red when a camera is in use. This will signal not to enter my office. While a green light will signal that camera is off and you may enter my office. To communicate my PC's device statuses, I thought about sending a typical JSON or XML payload. But thinking about it more I thought it would be more fun to design my own protocol that way a resource constrained micro-controller could be used more efficiently.
For this discussion the Device Monitor application running on a PC will be referred to as a server. Any micro-controllers controlling the light will be referred to as clients.
DM Protocol Design
To start off with I want the protocol to be able to provide an easy way for a device to identify it. I'll use the idea of Magic Numbers in file formats to identify the protocol easily. The Magic Numbers will be the two characters: 'D' and 'M'.
Next, let's do some very simple future proofing and give it a version number. That way if we ever need to introduce a breaking change, we can keep backwards compatibility.
That's it for majority of the boiler plate, let's get into the content. The content is simply a list of devices and their associated state. We need to identify the device type and its status. First, we want to start off with a count of the devices. This tells the clients when to stop parsing. For device type and device status, Let's make some simple enums.
The Device Type enumeration will reserve 0 as an Unknown device and we can add device types by incrementing the value.
| Value | Description |
|---|---|
| 0 | Unknown |
| 1 | Camera |
| 2 | Microphone |
For the Device Status enumeration, these enumerations will be associated by a particular Device Type. See the example for a Camera Device Type below.
| Value | Description |
|---|---|
| 0 | Unknown |
| 1 | Off |
| 2 | On |
Lastly, we send a checksum calculated using a CRC-8 algorithm for error checking.
Below is an example of a DM packet sent with 2 Camera devices!
| Byte Number | Value | Datatype | Description |
|---|---|---|---|
| 0 | D | ASCII Character | Marker Byte |
| 1 | M | ASCII Character | Marker Byte |
| 2 | 1 | Unsigned byte | Version number |
| 3 | 2 | Unsigned byte | This is the count of devices sent in the message. In this example there are 2 devices |
| 4 | 1 | Unsigned byte | Device Type Enum Value (I.E. Camera) for device 1 |
| 5 | 2 | Unsigned byte | Device Status Enum Value for a particular device type (I.E. Camera is ON) for device #1 |
| 6 | 1 | Unsigned byte | Device Type Enum Value (I.E. Camera) for device #2 |
| 7 | 1 | Unsigned byte | Device Status Enum Value for a particular device type (I.E. Camera is OFF) for device 2 |
| 8 | 170 | Unsigned byte | CRC-8 Checksum value |
| Total bytes | 9 |
For my next post, in the Device Monitor application I will implement a way to send a UDP packet on the network. That way, any clients can parse the message and change the color of the busy light appropriately.