MCPcopy Create free account
hub / github.com/TactilityProject/Tactility / CASChecksum

Function CASChecksum

Tactility/Source/hal/gps/GpsInit.cpp:27–46  ·  view source on GitHub ↗

Calculate the checksum for a CAS packet

Source from the content-addressed store, hash-verified

25
26// Calculate the checksum for a CAS packet
27static void CASChecksum(uint8_t *message, size_t length)
28{
29 uint32_t cksum = ((uint32_t)message[5] << 24); // Message ID
30 cksum += ((uint32_t)message[4]) << 16; // Class
31 cksum += message[2]; // Payload Len
32
33 // Iterate over the payload as a series of uint32_t's and
34 // accumulate the cksum
35 for (size_t i = 0; i < (length - 10) / 4; i++) {
36 uint32_t pl = 0;
37 memcpy(&pl, (message + 6) + (i * sizeof(uint32_t)), sizeof(uint32_t)); // avoid pointer dereference
38 cksum += pl;
39 }
40
41 // Place the checksum values in the message
42 message[length - 4] = (cksum & 0xFF);
43 message[length - 3] = (cksum & (0xFF << 8)) >> 8;
44 message[length - 2] = (cksum & (0xFF << 16)) >> 16;
45 message[length - 1] = (cksum & (0xFF << 24)) >> 24;
46}
47
48// Function to create a CAS packet for editing in memory
49static uint8_t makeCASPacket(uint8_t* buffer, uint8_t class_id, uint8_t msg_id, uint8_t payload_size, const uint8_t *msg)

Callers 1

makeCASPacketFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected