Skip to content

Commit 12ea959

Browse files
committed
Accelerometer: Check CrosEcReadMemU8 return values
CrosEcReadMemU8 returns 0 on IOCTL failure but we never check this. If the memmap read fails, the buffers stay uninitialized/unchanged, which makes the busy bit logic fail and the sensor report the wrong data. Add an EC_READ_U8 helper macro to checks for failure and jump to Exit with STATUS_IO_DEVICE_ERROR, so that broken EC communication is properly reported instead of producing invalid readings. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent fc95b8f commit 12ea959

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

FrameworkSensors/AccelerometerClient.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,17 @@ AccelerometerDevice::GetData(
539539
#define BUSY_WAIT_SLEEP_INTERVAL 5
540540
#define BUSY_WAIT_SLEEP_MS 25
541541

542+
// Helper: read a byte from EC memory map, goto Exit on failure.
543+
// Note: CrosEcReadMemU8 already traces its own errors internally.
544+
// We cannot use WPP trace macros inside #define because WPP generates
545+
// per-line identifiers that break when the macro is expanded elsewhere.
546+
#define EC_READ_U8(offset, dest) do { \
547+
if (CrosEcReadMemU8(Handle, (offset), (dest)) == 0) { \
548+
Status = STATUS_IO_DEVICE_ERROR; \
549+
goto Exit; \
550+
} \
551+
} while (0)
552+
542553
UINT8 lid_angle_bytes[2] = {0};
543554
UINT16 lid_angle = 0;
544555
UINT SensorOffset = 6 * m_LidSensorIndex + EC_MEMMAP_ACC_DATA + 2;
@@ -557,7 +568,7 @@ AccelerometerDevice::GetData(
557568
// Poll ACC_STATUS until the EC is not busy
558569
//
559570
int busy_attempts = 0;
560-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
571+
EC_READ_U8(EC_MEMMAP_ACC_STATUS, &acc_status);
561572

562573
while (acc_status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
563574
if (busy_attempts++ >= MAX_BUSY_WAIT_ATTEMPTS) {
@@ -569,7 +580,7 @@ AccelerometerDevice::GetData(
569580
if (busy_attempts % BUSY_WAIT_SLEEP_INTERVAL == 0)
570581
Sleep(BUSY_WAIT_SLEEP_MS);
571582

572-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
583+
EC_READ_U8(EC_MEMMAP_ACC_STATUS, &acc_status);
573584
}
574585

575586
TraceInformation("Status: (%02x), Present: %d, Busy: %d\n",
@@ -585,22 +596,24 @@ AccelerometerDevice::GetData(
585596
//
586597
// Read all sensor data (unsafe - EC could update mid-read)
587598
//
588-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 0, &lid_angle_bytes[0]);
589-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 1, &lid_angle_bytes[1]);
599+
EC_READ_U8(EC_MEMMAP_ACC_DATA + 0, &lid_angle_bytes[0]);
600+
EC_READ_U8(EC_MEMMAP_ACC_DATA + 1, &lid_angle_bytes[1]);
590601

591-
CrosEcReadMemU8(Handle, SensorOffset + 0, &Sensor1[0]);
592-
CrosEcReadMemU8(Handle, SensorOffset + 1, &Sensor1[1]);
593-
CrosEcReadMemU8(Handle, SensorOffset + 2, &Sensor1[2]);
594-
CrosEcReadMemU8(Handle, SensorOffset + 3, &Sensor1[3]);
595-
CrosEcReadMemU8(Handle, SensorOffset + 4, &Sensor1[4]);
596-
CrosEcReadMemU8(Handle, SensorOffset + 5, &Sensor1[5]);
602+
EC_READ_U8(SensorOffset + 0, &Sensor1[0]);
603+
EC_READ_U8(SensorOffset + 1, &Sensor1[1]);
604+
EC_READ_U8(SensorOffset + 2, &Sensor1[2]);
605+
EC_READ_U8(SensorOffset + 3, &Sensor1[3]);
606+
EC_READ_U8(SensorOffset + 4, &Sensor1[4]);
607+
EC_READ_U8(SensorOffset + 5, &Sensor1[5]);
597608

598609
//
599610
// Re-read ACC_STATUS to verify data consistency
600611
//
601-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &status);
612+
EC_READ_U8(EC_MEMMAP_ACC_STATUS, &status);
602613
}
603614

615+
#undef EC_READ_U8
616+
604617
//
605618
// Data is now consistent - process it
606619
//

0 commit comments

Comments
 (0)