Skip to content

Commit

Permalink
Send integers in little endian format instead of network order
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Oct 11, 2023
1 parent 458a186 commit 5203587
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/lib/WIFI/wifiJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ IPAddress WifiJoystick::remoteIP;
uint8_t WifiJoystick::channelCount = JOYSTICK_DEFAULT_CHANNEL_COUNT;
bool WifiJoystick::active = false;

static inline uint16_t htole16(uint16_t val)
{
#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return __builtin_bswap16(val);
#else
return val;
#endif
}

void WifiJoystick::StartJoystickService()
{
if (!udp)
Expand Down Expand Up @@ -95,15 +104,15 @@ void WifiJoystick::Loop(unsigned long now)
last = now;

struct ElrsUdpAdvertisement_s {
uint32_t magic; // ['E', 'L', 'R', 'S']
uint32_t magic; // char[4] = ['E', 'L', 'R', 'S']
uint8_t version; // JOYSTICK_VERSION
uint16_t port; // JOYSTICK_PORT, network byte order
uint16_t port; // JOYSTICK_PORT, little endian
uint8_t name_len; // length of the device name that follows
char name[0]; // device name
} eua = {
.magic = htobe32(0x454C5253),
.magic = htobe32(0x454C5253), // always big-endian so ELRS is in order
.version = JOYSTICK_VERSION,
.port = htons(JOYSTICK_PORT),
.port = htole16(JOYSTICK_PORT),
.name_len = (uint8_t)strlen(device_name)
};

Expand All @@ -126,7 +135,7 @@ void WifiJoystick::UpdateValues()
udp->write(channelCount);
for (uint8_t i = 0; i < channelCount; i++)
{
uint16_t channel = htons(map(
uint16_t channel = htole16(map(
constrain(ChannelData[i], CRSF_CHANNEL_VALUE_MIN, CRSF_CHANNEL_VALUE_MAX),
CRSF_CHANNEL_VALUE_MIN, CRSF_CHANNEL_VALUE_MAX, 0, 0xffff));
udp->write((uint8_t*)&channel, 2);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/WIFI/wifiJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Alternative: Listen for UDP broadcasts that contains a frame in the structure of
* 4 bytes: ['E', 'L', 'R', 'S']
* 1 byte: Protocol Version
* 2 byte unsigned: PORT, network byte order
* 2 byte unsigned: PORT, little-endian
* 1 byte: length of Device Name
* ASCII Text: Device Name
*
Expand All @@ -39,7 +39,7 @@
* receive frames in the format of:
* 1 byte: Frame type (WifiJoystickFrameType_e)CHANNELS
* 1 byte: Number of channels that follow
* 2 bytes unsigned * channel count: Channel data in range 0 to 0xffff, network byte order
* 2 bytes unsigned * channel count: Channel data in range 0 to 0xffff, little-endian
*
* Step 4:
* To end joystick data being sent, POST to the control URL
Expand Down

0 comments on commit 5203587

Please sign in to comment.