Armadilloフォーラム

コンテナ内でのユーザスイッチON/OFF状態取得について

h-yuusuke

2022年2月18日 18時33分

お世話になっております。

コンテナ内でユーザスイッチのON/OFF状態を取得しようとしているのですが、ユーザスイッチを押してもON状態を取得出来ておりません。
手順としては以下の通りに行っております。

①下記の内容でコンテナを起動 (lcalhost/testはat-debian-mageをベースにgcc等を追加したコンテナイメージです)

podman run -itd --name=test \
  --device=/dev/input \
  --volume /dev/input/by-path:/dev/input/by-path \
  --volume /var/app/volumes/workspace:/var/app/volumes/workspace \
  localhost/test:latest /bin/bash

②コンテナ内で下記のプログラムを実行

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <linux/input.h>
 
char *devices[] = {
#if 1
    "/dev/input/by-path/platform-gpio-keys-event",
#else
    "/dev/input/event1",
#endif
};
 
int show_key_status(const char *device)
{
    printf("%s\n", device);
 
    int fd = open(device, O_RDONLY);
    if (fd < 0) {
        perror("open");
        return 0;
    }
 
    uint8_t keys[16];
 
    if (ioctl(fd, EVIOCGKEY(sizeof keys), &keys) < 0) {
        perror("ioctl");
        close(fd);
        return 0;
    }
 
    close(fd);
 
    int count = 0;
    int i = 0;
    int j = 0;
 
    for (i = 0; i < sizeof keys; i++) {
        for (j = 0; j < 8; j++) {
            if (keys[i] & (1 << j)) {
                count++;
                printf ("  key code %d\n", (i*8) + j);
            }
        }
    }
    if (count == 0) {
        printf("  no key\n");
    }
 
    return 1;
}
 
int main(int argc, char **argv)
{
    int i = 0;
    for (i = 0; i < sizeof devices / sizeof devices[0]; i++) {
        if (!show_key_status(devices[i])) {
            return 1;
        }
    }
 
    return 0;
}

実行内容として不足していることなどはありますでしょうか?

コメント

at_makoto.sato

2022年2月18日 19時10分

佐藤です。

プログラムコード内の

uint8_t keys[16];

としている箇所を

uint8_t keys[19];

にするとどうでしょうか。

h-yuusuke

2022年3月1日 18時05分

ご回答ありがとうございます。
教えて頂いた修正を行い、ユーザスイッチの押下状態を取得することができました。