Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52402776 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use enum class for InputMessage type

Using enum class helps with type safety, and allows compile-time
detection of missing switch/case statements.

Bug: 70668286
Test: compile-time only
Change-Id: I5ae95cfbf2b38330fa34eca1c9f444a90190cd25
parent a6a58048
Loading
Loading
Loading
Loading
+5 −5
Original line number Original line Diff line number Diff line
@@ -60,14 +60,14 @@ class Parcel;
 * in StructLayout_test should be made.
 * in StructLayout_test should be made.
 */
 */
struct InputMessage {
struct InputMessage {
    enum {
    enum class Type : uint32_t {
        TYPE_KEY = 1,
        KEY,
        TYPE_MOTION = 2,
        MOTION,
        TYPE_FINISHED = 3,
        FINISHED,
    };
    };


    struct Header {
    struct Header {
        uint32_t type;
        Type type; // 4 bytes
        // We don't need this field in order to align the body below but we
        // We don't need this field in order to align the body below but we
        // leave it here because InputMessage::size() and other functions
        // leave it here because InputMessage::size() and other functions
        // compute the size of this structure as sizeof(Header) + sizeof(Body).
        // compute the size of this structure as sizeof(Header) + sizeof(Body).
+54 −55
Original line number Original line Diff line number Diff line
@@ -93,12 +93,11 @@ inline static bool isPointerEvent(int32_t source) {
bool InputMessage::isValid(size_t actualSize) const {
bool InputMessage::isValid(size_t actualSize) const {
    if (size() == actualSize) {
    if (size() == actualSize) {
        switch (header.type) {
        switch (header.type) {
        case TYPE_KEY:
            case Type::KEY:
                return true;
                return true;
        case TYPE_MOTION:
            case Type::MOTION:
            return body.motion.pointerCount > 0
                return body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
                    && body.motion.pointerCount <= MAX_POINTERS;
            case Type::FINISHED:
        case TYPE_FINISHED:
                return true;
                return true;
        }
        }
    }
    }
@@ -107,11 +106,11 @@ bool InputMessage::isValid(size_t actualSize) const {


size_t InputMessage::size() const {
size_t InputMessage::size() const {
    switch (header.type) {
    switch (header.type) {
    case TYPE_KEY:
        case Type::KEY:
            return sizeof(Header) + body.key.size();
            return sizeof(Header) + body.key.size();
    case TYPE_MOTION:
        case Type::MOTION:
            return sizeof(Header) + body.motion.size();
            return sizeof(Header) + body.motion.size();
    case TYPE_FINISHED:
        case Type::FINISHED:
            return sizeof(Header) + body.finished.size();
            return sizeof(Header) + body.finished.size();
    }
    }
    return sizeof(Header);
    return sizeof(Header);
@@ -129,7 +128,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {


    // Write the body
    // Write the body
    switch(header.type) {
    switch(header.type) {
        case InputMessage::TYPE_KEY: {
        case InputMessage::Type::KEY: {
            // uint32_t seq
            // uint32_t seq
            msg->body.key.seq = body.key.seq;
            msg->body.key.seq = body.key.seq;
            // nsecs_t eventTime
            // nsecs_t eventTime
@@ -156,7 +155,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            msg->body.key.downTime = body.key.downTime;
            msg->body.key.downTime = body.key.downTime;
            break;
            break;
        }
        }
        case InputMessage::TYPE_MOTION: {
        case InputMessage::Type::MOTION: {
            // uint32_t seq
            // uint32_t seq
            msg->body.motion.seq = body.motion.seq;
            msg->body.motion.seq = body.motion.seq;
            // nsecs_t eventTime
            // nsecs_t eventTime
@@ -212,7 +211,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            }
            }
            break;
            break;
        }
        }
        case InputMessage::TYPE_FINISHED: {
        case InputMessage::Type::FINISHED: {
            msg->body.finished.seq = body.finished.seq;
            msg->body.finished.seq = body.finished.seq;
            msg->body.finished.handled = body.finished.handled;
            msg->body.finished.handled = body.finished.handled;
            break;
            break;
@@ -457,7 +456,7 @@ status_t InputPublisher::publishKeyEvent(
    }
    }


    InputMessage msg;
    InputMessage msg;
    msg.header.type = InputMessage::TYPE_KEY;
    msg.header.type = InputMessage::Type::KEY;
    msg.body.key.seq = seq;
    msg.body.key.seq = seq;
    msg.body.key.deviceId = deviceId;
    msg.body.key.deviceId = deviceId;
    msg.body.key.source = source;
    msg.body.key.source = source;
@@ -511,7 +510,7 @@ status_t InputPublisher::publishMotionEvent(
    }
    }


    InputMessage msg;
    InputMessage msg;
    msg.header.type = InputMessage::TYPE_MOTION;
    msg.header.type = InputMessage::Type::MOTION;
    msg.body.motion.seq = seq;
    msg.body.motion.seq = seq;
    msg.body.motion.deviceId = deviceId;
    msg.body.motion.deviceId = deviceId;
    msg.body.motion.source = source;
    msg.body.motion.source = source;
@@ -553,7 +552,7 @@ status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandle
        *outHandled = false;
        *outHandled = false;
        return result;
        return result;
    }
    }
    if (msg.header.type != InputMessage::TYPE_FINISHED) {
    if (msg.header.type != InputMessage::Type::FINISHED) {
        ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
        ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
                mChannel->getName().c_str(), msg.header.type);
                mChannel->getName().c_str(), msg.header.type);
        return UNKNOWN_ERROR;
        return UNKNOWN_ERROR;
@@ -614,7 +613,7 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
        }
        }


        switch (mMsg.header.type) {
        switch (mMsg.header.type) {
        case InputMessage::TYPE_KEY: {
            case InputMessage::Type::KEY: {
                KeyEvent* keyEvent = factory->createKeyEvent();
                KeyEvent* keyEvent = factory->createKeyEvent();
                if (!keyEvent) return NO_MEMORY;
                if (!keyEvent) return NO_MEMORY;


@@ -628,7 +627,7 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
            break;
            break;
            }
            }


        case InputMessage::TYPE_MOTION: {
            case InputMessage::Type::MOTION: {
                ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
                ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
                if (batchIndex >= 0) {
                if (batchIndex >= 0) {
                    Batch& batch = mBatches.editItemAt(batchIndex);
                    Batch& batch = mBatches.editItemAt(batchIndex);
@@ -653,8 +652,8 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
                        // We cannot append to the batch in progress, so we need to consume
                        // We cannot append to the batch in progress, so we need to consume
                        // the previous batch right now and defer the new message until later.
                        // the previous batch right now and defer the new message until later.
                        mMsgDeferred = true;
                        mMsgDeferred = true;
                    status_t result = consumeSamples(factory,
                        status_t result = consumeSamples(factory, batch, batch.samples.size(),
                            batch, batch.samples.size(), outSeq, outEvent);
                                                         outSeq, outEvent);
                        mBatches.removeAt(batchIndex);
                        mBatches.removeAt(batchIndex);
                        if (result) {
                        if (result) {
                            return result;
                            return result;
@@ -1074,7 +1073,7 @@ status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {


status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
    InputMessage msg;
    InputMessage msg;
    msg.header.type = InputMessage::TYPE_FINISHED;
    msg.header.type = InputMessage::Type::FINISHED;
    msg.body.finished.seq = seq;
    msg.body.finished.seq = seq;
    msg.body.finished.handled = handled;
    msg.body.finished.handled = handled;
    return mChannel->sendMessage(&msg);
    return mChannel->sendMessage(&msg);
+4 −4
Original line number Original line Diff line number Diff line
@@ -86,7 +86,7 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
    // Server->Client communication
    // Server->Client communication
    InputMessage serverMsg;
    InputMessage serverMsg;
    memset(&serverMsg, 0, sizeof(InputMessage));
    memset(&serverMsg, 0, sizeof(InputMessage));
    serverMsg.header.type = InputMessage::TYPE_KEY;
    serverMsg.header.type = InputMessage::Type::KEY;
    serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
    serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
    EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
    EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
            << "server channel should be able to send message to client channel";
            << "server channel should be able to send message to client channel";
@@ -102,7 +102,7 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
    // Client->Server communication
    // Client->Server communication
    InputMessage clientReply;
    InputMessage clientReply;
    memset(&clientReply, 0, sizeof(InputMessage));
    memset(&clientReply, 0, sizeof(InputMessage));
    clientReply.header.type = InputMessage::TYPE_FINISHED;
    clientReply.header.type = InputMessage::Type::FINISHED;
    clientReply.body.finished.seq = 0x11223344;
    clientReply.body.finished.seq = 0x11223344;
    clientReply.body.finished.handled = true;
    clientReply.body.finished.handled = true;
    EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
    EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
@@ -161,7 +161,7 @@ TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
    serverChannel.clear(); // close server channel
    serverChannel.clear(); // close server channel


    InputMessage msg;
    InputMessage msg;
    msg.header.type = InputMessage::TYPE_KEY;
    msg.header.type = InputMessage::Type::KEY;
    EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
    EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
            << "sendMessage should have returned DEAD_OBJECT";
            << "sendMessage should have returned DEAD_OBJECT";
}
}
@@ -180,7 +180,7 @@ TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
    };
    };


    InputMessage serverMsg = {}, clientMsg;
    InputMessage serverMsg = {}, clientMsg;
    serverMsg.header.type = InputMessage::TYPE_MOTION;
    serverMsg.header.type = InputMessage::Type::MOTION;
    serverMsg.body.motion.seq = 1;
    serverMsg.body.motion.seq = 1;
    serverMsg.body.motion.pointerCount = 1;
    serverMsg.body.motion.pointerCount = 1;