Home | History | Annotate | Download | only in wrapsim
      1 /*
      2  * Copyright 2007 The Android Open Source Project
      3  *
      4  * Fake device support.
      5  */
      6 #ifndef _WRAPSIM_FAKEDEV_H
      7 #define _WRAPSIM_FAKEDEV_H
      8 
      9 #include <sys/types.h>
     10 #include <sys/uio.h>
     11 #include <errno.h>
     12 
     13 typedef struct FakeDev FakeDev;
     14 
     15 typedef int      (*Fake_close)(FakeDev* dev, int);
     16 typedef FakeDev* (*Fake_dup)(FakeDev* dev, int);
     17 typedef ssize_t  (*Fake_read)(FakeDev* dev, int, void*, size_t);
     18 typedef ssize_t  (*Fake_readv)(FakeDev* dev, int, const struct iovec*, int);
     19 typedef ssize_t  (*Fake_write)(FakeDev* dev, int, const void*, size_t);
     20 typedef ssize_t  (*Fake_writev)(FakeDev* dev, int, const struct iovec*, int);
     21 typedef void*    (*Fake_mmap)(FakeDev* dev, void*, size_t, int, int, int, __off_t);
     22 typedef int      (*Fake_ioctl)(FakeDev* dev, int, int, void*);
     23 
     24 /*
     25  * An open fake device entry.
     26  */
     27 struct FakeDev {
     28     /* string, for debugging; usually orig. device name */
     29     char*   debugName;
     30 
     31     /* state bucket */
     32     void*   state;
     33 
     34     /* the file descriptor we're associated with */
     35     int     fd;
     36 
     37     /* in some cases we use a pair; this is the other one */
     38     int     otherFd;
     39 
     40     /*
     41      * Device functions we provide.
     42      *
     43      * All other file descriptor operations should fail, usually with EBADF.
     44      */
     45     Fake_close  close;
     46     Fake_dup  dup;
     47     Fake_read   read;
     48     Fake_readv  readv;
     49     Fake_write  write;
     50     Fake_writev writev;
     51     Fake_mmap   mmap;       // handles both mmap() and mmap64()
     52     Fake_ioctl  ioctl;
     53 };
     54 
     55 /*
     56  * If a handler isn't defined for a syscall, we return EMLINK so that it's
     57  * obvious when the error is generated by us.
     58  */
     59 #define kNoHandlerError EMLINK
     60 
     61 /*
     62  * Fake file descriptors begin here.  This should be chosen such that no
     63  * real descriptor is ever at or above this value.
     64  */
     65 #define kFakeFdBase     512
     66 #define kMaxFakeFdCount 256
     67 
     68 /*
     69  * Create a new, completely fake device entry.
     70  */
     71 FakeDev* wsCreateFakeDev(const char* debugName);
     72 
     73 /*
     74  * Create a new, mostly fake device entry.
     75  */
     76 FakeDev* wsCreateRealFakeDev(const char* debugName);
     77 
     78 /*
     79  * Free a fake device entry.
     80  */
     81 void wsFreeFakeDev(FakeDev* dev);
     82 
     83 /*
     84  * Given a file descriptor, find the corresponding fake device.  Returns
     85  * NULL if the fd doesn't correspond to one of our entries.
     86  */
     87 FakeDev* wsFakeDevFromFd(int fd);
     88 
     89 /*
     90  * Check to see if this open() call is on a device we want to spoof.
     91  *
     92  * Returns a "fake" file descriptor on success, <0 on error.
     93  */
     94 int wsInterceptDeviceOpen(const char* pathName, int flags);
     95 
     96 /*
     97  * Check to see if this access() call is on a device we want to spoof.
     98  *
     99  * Returns 0 if the device can be fake-accessed, -1 if it can't, -2
    100  * if it can't and we don't want to allow fallback to the host-device.
    101  */
    102 int wsInterceptDeviceAccess(const char* pathName, int flags);
    103 
    104 /*
    105  * Devices.
    106  */
    107 FakeDev* wsOpenDevAudio(const char* pathName, int flags);
    108 FakeDev* wsOpenDevConsoleTty(const char* pathName, int flags);
    109 FakeDev* wsOpenDevEvent(const char* pathName, int flags);
    110 FakeDev* wsOpenDevFb(const char* pathName, int flags);
    111 FakeDev* wsOpenDevLog(const char* pathName, int flags);
    112 FakeDev* wsOpenDevPower(const char* pathName, int flags);
    113 FakeDev* wsOpenSysPower(const char* pathName, int flags);
    114 FakeDev* wsOpenDevVibrator(const char* pathName, int flags);
    115 
    116 /*
    117  * Performs key remapping and sends the event to the global input event device.
    118  */
    119 void wsSendSimKeyEvent(int key, int isDown);
    120 
    121 /*
    122  * Send a touch event to the global input event device.
    123  */
    124 void wsSendSimTouchEvent(int action, int x, int y);
    125 
    126 #endif /*_WRAPSIM_FAKEDEV_H*/
    127