2020年9月5日 星期六

Linux Kernel(20.2)- uinput module


uinput是kernel module透過寫入/dev/uinput從userpace模擬input device裝置,並且驅動特定的event。
其kernel configuration的path為Input device support -> Miscellaneous devices -> User level driver support
設定簡單,可以參考以下範例大概就知道如何操控了。
#include <stdio.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <unistd.h>

#include <string.h>

#include <linux/input.h>

#include <linux/uinput.h>

void emit(int fd, int type, int code, int val)
{
  struct input_event ie;

  ie.type = type;
  ie.code = code;
  ie.value = val;
  /* timestamp values below are ignored */
  ie.time.tv_sec = 0;
  ie.time.tv_usec = 0;

  write(fd, &ie, sizeof(ie));
}

int main(void)
{
  struct uinput_setup usetup;

  int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);

  /*
   * The ioctls below will enable the device that is about to be
   * created, to pass key events, in this case the space key.
   **/
  ioctl(fd, UI_SET_EVBIT, EV_KEY);
  ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);

  memset(&usetup, 0, sizeof(usetup));
  usetup.id.bustype = BUS_USB;
  usetup.id.vendor = 0x1234;    /* sample vendor */
  usetup.id.product = 0x5678;   /* sample product */
  strcpy(usetup.name, "Example device");

  ioctl(fd, UI_DEV_SETUP, &usetup);
  ioctl(fd, UI_DEV_CREATE);

  /*
   * On UI_DEV_CREATE the kernel will create the device node for this
   * device. We are inserting a pause here so that userspace has time
   * to detect, initialize the new device, and can start listening to
   * the event, otherwise it will not notice the event we are about
   * to send. This pause is only needed in our example code!
   **/
  printf("will report the event after 10 sec\n");
  sleep(10);

  /* Key press, report the event, send key release, and report again */
  emit(fd, EV_KEY, KEY_SPACE, 1);
  emit(fd, EV_SYN, SYN_REPORT, 0);
  emit(fd, EV_KEY, KEY_SPACE, 0);
  emit(fd, EV_SYN, SYN_REPORT, 0);

  /*
   * Give userspace some time to read the events before we destroy the
   * device with UI_DEV_DESTOY.
   **/
  sleep(1);

  ioctl(fd, UI_DEV_DESTROY);
  close(fd);

  return 0;
}


執行結果,input_test為先前章節的user space程式,uinput為該章節範例,執行uinput之後,會在/dev/input/長出對應的device node。
/ # zcat /proc/config.gz | grep UINPUT
CONFIG_INPUT_UINPUT=y
/ # ./uinput &
/ # [   99.433163] input: Example device as /devices/virtual/input/input4
/ # ls /dev/input/
event0  event1
/ # ./input_test /dev/input/event1
will report the event after 10 sec
type:1, code:57, val:1
type:0, code:0, val:0
type:1, code:57, val:0
type:0, code:0, val:0


    參考資料:
  • Documentation/input/uinput.rst



Linux Kernel(20.1)- Input device user program


這裡簡單的描述一下如何寫一個user program去讀去input device data,基本上,就是直接open /dev/inputN,然後將data mapping成struct input_event。
struct input_event {
    struct timeval time;
    __u16 type;
    __u16 code;
    __s32 value;
};
type: event type
code: key code for EV_KEY
摘入一段/usr/include/linux/input-event-codes.h
/*
 * Event types
 */
#define EV_SYN          0x00
#define EV_KEY          0x01
#define EV_REL          0x02
#define EV_ABS          0x03
#define EV_MSC          0x04
#define EV_SW           0x05
#define EV_LED          0x11
#define EV_SND          0x12
#define EV_REP          0x14
#define EV_FF           0x15
#define EV_PWR          0x16
#define EV_FF_STATUS        0x17
#define EV_MAX          0x1f
#define EV_CNT          (EV_MAX+1)

/*
 * Key code
 */
#define KEY_RESERVED        0
#define KEY_ESC         1
#define KEY_1           2
#define KEY_2           3
...

#define BTN_MISC        0x100
#define BTN_0           0x100
#define BTN_1           0x101
#define BTN_2           0x102
...

#define KEY_OK          0x160
#define KEY_SELECT      0x161
...


#include <stdio.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <unistd.h>

#include <string.h>

#include <linux/input.h>

int main(int argc, char *argv[])
{
  struct input_event evt;
  int fd;
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    printf("open %s failed: %s/%d\n", argv[0], strerror(errno), errno);
    return -1;
  }

  while (1) {
    if (sizeof(evt) == read(fd, &evt, sizeof(evt))) {
      printf("type:%d, code:%d, val:%d\n", evt.type, evt.code, evt.value);
    } else {
      printf("read failed: %s/%d\n", strerror(errno), errno);
    }
  }
  return 0;
}


    參考資料:
  • Documentation/input/event-codes.rst



Citizens Broadband Radio Service (CBRS)


全球都將3.5GHz頻段的頻譜視為新一代電信服務的理想選擇,因為它既具備長距離傳輸的優勢,又有大量且連續的可用頻寬,包括中國、歐洲與南韓都已將它作為5G之用,台灣自去年底展開的5G頻譜競標,不只在今年初創下1,380億元新台幣的總標金紀錄,且最熱門的3.5GHz頻段的頻譜,總標金即達1364.33億元。

在FCC的3.5 GHz CBRS頻段釋出計畫中,引進了三層式頻譜接取架構。在此架構下,當Incumbents 與 PALs未使用該頻段時,GAAs皆可以使用。而當有很多GAAs處於同一場所時,要按照公平的原則使用可用頻率。因此,FCC將以SAS(Spectrum Access System),實現不同用戶及應用之間的頻譜共享。

Incumbents: 指的是那些原本在3.5G運作的使用者, 如 fixed satellite service, US Navy Radar等。
PALs(Priority Access Licenses): 指的是那些標下這個頻段使用者, 如service providers。
GAAs(General Authorized Access): 其他人。






熱門文章