MTK平臺-添加閃光燈測試節點

綜述

項目需求:客戶有一個老化apk,自動測試閃光燈。
新增以下節點,控制閃光燈亮滅。
/sys/devices/virtual/torch/torch/torch_level
寫入1 ,后手電筒亮
寫入0 ,后手電筒滅
/sys/devices/virtual/sub_torch/sub_torch/sub_torch_level
寫入1 ,前手電筒亮
寫入0 ,前手電筒滅

實現思路
1.創建/sys/devices/virtual/torch/torch/torch_level節點
2.在寫入1的時候,打開閃光燈,寫入0的時候,關閉閃光燈
················································································
怎么在/sys/devices/virtual/路徑下創建節點呢?很蒙逼
怎么在底層調用打開或者關閉閃光燈呢?很懵逼

學習本文,講解決以上問題。

1.創建節點

這里以/sys/devices/virtual/torch/torch/torch_level節點創建為例子,

kernel-3.18/drivers/misc/mediatek/flashlight/src/mt6580/kd_flashlightlist.c

#define TORCH_DEVNAME "torch"http://節點名稱
static dev_t torch_devno;//設備號
static struct class *torch_class;//class類
static struct device *torch_device;//設備
static int main_torch_level = 0;

//show函數
static ssize_t torch_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
{
    logI("zcf torch_show is call,main_torch_level=%d\n",main_torch_level);
    return snprintf(buf, PAGE_SIZE, "%d\n", main_torch_level);
}
//store函數
static ssize_t torch_store(struct device *dev,
               struct device_attribute *attr, const char *buf, size_t count)
{
    //添加閃光燈控制邏輯
    return count;
}
//定義設備屬性節點torch_level,和show函數,store函數
static DEVICE_ATTR(torch_level, S_IRUGO|S_IWUSR, torch_show, torch_store);

//初始化函數
static void torch_create(void)
{  
    int ret = 0;
    logI("zcf [%s] is init\n",__func__);
    //申請設備號--下面需要用的這個設備號
    ret = alloc_chrdev_region(&torch_devno, 0, 1, TORCH_DEVNAME);
    if (ret) {//申請成功
        logI("zcf [torch_init] alloc_chrdev_region fail: %d ~", ret);
        //goto torch_chrdev_error;   這里應該跳到unregister函數,懶得寫
    } else {//申請失敗
        logI("zcf [torch_init] major: %d, minor: %d ~", MAJOR(torch_devno),
             MINOR(torch_devno));
    } 
    //在sys/class創建torch_class 類
    torch_class = class_create(THIS_MODULE, TORCH_DEVNAME);
    if (IS_ERR(torch_class)) {//創建失敗
        logI("zcf  Unable to create class:torch_class\n");
    }
    //在sys/class/torch_class/下創建設備節點
    torch_device =device_create(torch_class, NULL, torch_devno, 
                                                  NULL, TORCH_DEVNAME);
    if(NULL == torch_device)//創建失敗
        logI("zcf [torch_init] device_create torch_device fail \n");
    //在sys/class/torch_class/torch_class下創建屬性文件torch_level
    ret = device_create_file(torch_device,&dev_attr_torch_level);
    if(ret < 0)
        logI("zcf Failed to create attribute torch_levle\n");
}

static int flashlight_probe(struct platform_device *dev)
{
    //省略代碼
    torch_create();//創建節點
}

需要注意的地方
1.創建節點,最好在probe函數中創建!!!
2關于device_create_file函數
device_create_file(torch_device,&dev_attr_torch_level);
這個函數,前面的參數是device ,后面的參數加你要創建的節點名稱dev_attr_xxxx。
device_create_file(torch_devicedev, &dev_attr_xxx);
dev_attr_xxx就是在xxx(要創建的節點)前加上dev_attr_

DEVICE_ATTR,device_create_file的使用

到此,我們的節點就創建出來了,有人可能會有疑問,上面代碼中創建的節點是
sys/class/torch_class/torch_class/torch_level
但要求的節點路徑是
/sys/devices/virtual/sub_torch/sub_torch/sub_torch_level
不對勁啊!
實際上,我們創建sys/class/torch_class/torch_class/torch_level節點后,系統會默認也在/sys/devices/virtual/torch_class/torch_class/torch_level下創建一樣的節點,使用adb命令可以查看!
(為啥會自動創建這個節點呢,這就需要跟蹤源碼啦)

adb命令查看節點

adb

可以看到,確實系統默認創建了/sys/devices/virtual/torch_class/torch_class/torch_level節點。

2.打開、關閉 閃光燈

當我們獲取torch_level節點的值(例如:使用adb命令 cat torch_level)時,就會調用show函數

//show函數
static ssize_t torch_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
{
    logI("zcf torch_show is call,main_torch_level=%d\n",main_torch_level);
    return snprintf(buf, PAGE_SIZE, "%d\n", main_torch_level);
}

當我們往torch_level節點寫入值(例如:使用adb命令 echo 1 > torch_level)時,就會調用store函數

//store函數
static ssize_t torch_store(struct device *dev,
              struct device_attribute *attr, const char *buf, size_t count)
{
   //添加閃光燈控制邏輯
   return count;
}

因此,我們需要在store函數中添加閃光燈的控制邏輯,那么,如何打開或者關閉閃光燈呢?
kernel-3.18/drivers/misc/mediatek/flashlight/src/{平臺}/{項目}/constant_flashlight/leds_strobe.c

int FL_Enable(void)//打開閃光燈
{
    if (g_duty>=1)
    {   
        mflash_set_gpio_output(GPIO_ENT,GPIO_OUT_ONE);
        mflash_set_gpio_output(GPIO_ENF,GPIO_OUT_ONE);
        FL_ALOGD(" FL_Enable enable in flash mode,duty=%d.", g_duty);
    }        
    else
    {   
        mflash_set_gpio_output(GPIO_ENT,GPIO_OUT_ONE);
        mflash_set_gpio_output(GPIO_ENF,GPIO_OUT_ZERO);
        FL_ALOGD(" FL_Enable enable in torch mode,duty=%d.", g_duty);
    }   

    return 0;
}

int FL_Disable(void)//關閉閃關燈
{
    FL_ALOGF();
    mflash_set_gpio_output(GPIO_ENT,GPIO_OUT_ZERO);
    mflash_set_gpio_output(GPIO_ENF,GPIO_OUT_ZERO);
    return 0;
}
//初始化GPIO引腳
void main_camera_flash_gpio_init(void) 
{
    static struct device_node *main_flash_node;
    FL_ALOGF();
    main_flash_node = of_find_compatible_node(NULL, NULL, 
                                                "mediatek,mainflashlight");
    GPIO_ENT = of_get_named_gpio(main_flash_node, 
                                                "flashlight_en_gpio", 0);
    GPIO_ENF = of_get_named_gpio(main_flash_node, 
                                                "flashlight_mode_gpio", 0);
}

從源碼中,可以看到,調用了FL_Enable和FL_Disable去打開、關閉閃光燈,原理就是通過拉高相應的GPIO腳,使能閃光燈,拉低GPIO腳,關閉閃光燈。
需要注意的是,每次調用之前,我們需要先調用GPIO初始化函數,找到相應的GPIO引腳

extern int FL_Enable(void);//聲明FL_Enable是外部函數
extern int FL_Disable(void);
extern void main_camera_flash_gpio_init(void);

static ssize_t torch_store(struct device *dev,
                 struct device_attribute *attr, const char *buf, size_t count)
{
    int temp;
    temp = simple_strtol(buf, NULL, 10);//把字符串轉為數字
    main_torch_level = temp;
    main_camera_flash_gpio_init();//初始化gpio引腳
    if(main_torch_level == 1)//如果寫1
    {   
        FL_Enable();//打開閃光燈
    }
    else if(main_torch_level == 0)//如果寫0
        FL_Disable();//關閉閃光燈
    else//無效參數
        logI("zcf invalid parameter,the torch_level must be 1 or 0\n");

    logI("zcf  main_torch_level=%d , count =%d\n",main_torch_level,count);
    return count;
}

這樣,我們在store函數中,就添加好了控制閃光燈的邏輯了。當然,僅僅這樣,客戶提供的第三方測試apk是無法訪問節點的,會有selinux錯誤

 avc: denied { write } for name="torch_level" dev="sysfs" ino=9205 
scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 
tclass=file permissive=0
avc錯誤

因此,我們要往系統加入相應的權限。

3.添加節點訪問的權限

1.device/mediatek/mt6580/init.mt6580.rc
使所以用戶對節點torch_level、sub_torch_level均有讀寫權限


    # start-add by zcf in 2018.10.23 for torch_test
    chmod 0666 /sys/devices/virtual/torch/torch/torch_level
    chown root system /sys/devices/virtual/torch/torch/torch_level

    chmod 0666 /sys/devices/virtual/sub_torch/sub_torch/sub_torch_level
    chown root system /sys/devices/virtual/sub_torch/sub_torch/sub_torch_level
    # end-add by zcf in 2018.10.23 for torch_test

2.device/mediatek/sepolicy/basic/non_plat/system_app.te

# add by zcf in 2018.10.23 for torch test
allow system_app sysfs:file { write read };

3.system/sepolicy/private/app.te
注釋掉以下代碼

app.te


以上這種添加SELinux權限的方式會導致GMS測試失敗,因為改動了谷歌官方的邏輯-system/sepolicy/private/app.te。因此,換以下方式去改動:

1.device/mediatek/mt6580/init.mt6580.rc
使所以用戶對節點torch_level、sub_torch_level均有讀寫權限

    # start-add by zcf in 2018.10.23 for torch_test
    chmod 0666 /sys/devices/virtual/torch/torch/torch_level
    chown root system /sys/devices/virtual/torch/torch/torch_level

    chmod 0666 /sys/devices/virtual/sub_torch/sub_torch/sub_torch_level
    chown root system /sys/devices/virtual/sub_torch/sub_torch/sub_torch_level
    # end-add by zcf in 2018.10.23 for torch_test

2.自定義類型: 定義torch SELinux type
device/mediatek/sepolicy/bsp/non_plat/file.te

#add by zcf in 2018.11.1 for flashilight test
type system_app_torch_file,fs_type,sysfs_type;
type system_app_sub_torch_file,fs_type,sysfs_type;

3.file_contexts 綁定 torch對應的label
注意對應的節點是實際節點,而不是鏈接.以及整個目錄路徑中也絕不能包含鏈接(無數同仁有犯這個錯誤,特意提醒)
device/mediatek/sepolicy/bsp/non_plat/file_contexts

#add by zcf in 2018.11.01 for torch test
/sys/devices/virtual/torch/torch/torch_level u:object_r:system_app_torch_file:s0
/sys/devices/virtual/sub_torch/sub_torch/sub_torch_level u:object_r:system_app_sub_torch_file:s0

4.申請相關的權限
device/mediatek/sepolicy/basic/non_plat/system_app.te

# add by zcf in 2018.10.23 for torch test
allow system_app system_app_torch_file:file rw_file_perms;
allow system_app system_app_sub_torch_file:file rw_file_perms;

4.完整源碼

kernel-3.18/drivers/misc/mediatek/flashlight/src/mt6580/kd_flashlightlist.c

//start-add by zcf in 2018.10.23 for torch test
#define TORCH_DEVNAME "torch"
#define SUB_TORCH_DEVNAME "sub_torch"
static dev_t torch_devno,sub_torch_devno;
static struct class *torch_class,*sub_torch_class;
static struct device *torch_device,*sub_torch_device;

extern void main_camera_flash_gpio_init(void);
extern void sub_camera_flash_gpio_init(void);

extern int FL_Enable(void);
extern int FL_Disable(void);
extern int FL_Sub_Enable(void);
extern int FL_Sub_Disable(void);

static int main_torch_level = 0;
static int sub_torch_level = 0;
static ssize_t torch_show(struct device *dev,struct device_attribute *attr, char *buf)
{
    logI("zcf torch_show is call,main_torch_level=%d\n",main_torch_level);
    return snprintf(buf, PAGE_SIZE, "%d\n", main_torch_level);
}

static ssize_t torch_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t count)
{
    int temp;
    temp = simple_strtol(buf, NULL, 10);
    main_torch_level = temp;
    main_camera_flash_gpio_init();
    if(main_torch_level == 1)
    {   
        FL_Enable();
    }
    else if(main_torch_level == 0)
        FL_Disable();
    else
        logI("zcf invalid parameter,the torch_level must be 1 or 0\n");

    logI("zcf torch_store is call,main_torch_level=%d , count =%d\n",main_torch_level,count);
    return count;
}
static DEVICE_ATTR(torch_level, S_IRUGO|S_IWUSR, torch_show, torch_store);


static ssize_t sub_torch_show(struct device *dev,struct device_attribute *attr, char *buf)
{
    logI("zcf torch_show is call,main_torch_level=%d\n",main_torch_level);
    return snprintf(buf, PAGE_SIZE, "%d\n", main_torch_level);
}

static ssize_t sub_torch_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t count)
{
    int temp;
    temp = simple_strtol(buf, NULL, 10);
    sub_torch_level = temp;
    sub_camera_flash_gpio_init();
    if(sub_torch_level == 1)
    {   
        FL_Sub_Enable();
    }
    else if(sub_torch_level == 0)
        FL_Sub_Disable();
    else
        logI("zcf invalid parameter,the sub_torch_level must be 1 or 0\n");

    logI("zcf torch_store is call,sub_torch_level=%d , count =%d\n",sub_torch_level,count);
    return count;
}

static DEVICE_ATTR(sub_torch_level, S_IRUGO|S_IWUSR, sub_torch_show, sub_torch_store);

static void torch_create(void)
{   
    int ret = 0;
    logI("zcf [%s] is init\n",__func__);
    ret = alloc_chrdev_region(&torch_devno, 0, 1, TORCH_DEVNAME);
    if (ret) {
        logI("zcf [torch_init] alloc_chrdev_region fail: %d ~", ret);
        //goto torch_init_error;
    } else {
        logI("zcf [torch_init] major: %d, minor: %d ~", MAJOR(torch_devno),
             MINOR(torch_devno));
    } 
    ret = alloc_chrdev_region(&sub_torch_devno, 0, 1, SUB_TORCH_DEVNAME);

    if (ret) {
        logI("zcf [torch_init] alloc_chrdev_region fail: %d ~", ret);
        //goto torch_init_error;
        logI("zcf [torch_init] major: %d, minor: %d ~", MAJOR(sub_torch_devno),
             MINOR(sub_torch_devno));
    } 
    torch_class = class_create(THIS_MODULE, TORCH_DEVNAME);
    if (IS_ERR(torch_class)) {
        logI("zcf  Unable to create class:torch_class\n");
    }

    torch_device =device_create(torch_class, NULL, torch_devno, NULL, TORCH_DEVNAME);
    if(NULL == torch_device)
        logI("zcf [torch_init] device_create torch_device fail \n");
    ret = device_create_file(torch_device,&dev_attr_torch_level);
    if(ret < 0)
        logI("zcf Failed to create attribute torch_levle\n");
    
    sub_torch_class = class_create(THIS_MODULE, SUB_TORCH_DEVNAME);
    if (IS_ERR(sub_torch_class)) {
        logI("zcf  Unable to create class:sub_torch_class\n");
    }

    sub_torch_device =device_create(sub_torch_class, NULL, sub_torch_devno, NULL, SUB_TORCH_DEVNAME);
    if(NULL == sub_torch_device)
        logI("zcf [torch_init] device_create torch_device fail \n");
    ret = device_create_file(sub_torch_device,&dev_attr_sub_torch_level);
    if(ret < 0)
        logI("zcf Failed to create attribute sub_torch_levle\n");
}
//end-add by zcf in 2018.10.23 for torch test.

static int flashlight_probe(struct platform_device *dev)
{
    int ret = 0, err = 0;
    logI("[flashlight_probe] start ~")
    //省略部分代碼
    torch_create();
}

荊軻刺秦王

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,797評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,179評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,628評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,642評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,444評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,948評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,040評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,185評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,717評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,794評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,418評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,414評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,750評論 2 370

推薦閱讀更多精彩內容