Android 如何利用proc由上層向kernel寫文件時候我們需要不經(jīng)過Android的上層代碼調(diào)用kernel的標準的機制,往往為了一步到位的話就可以采用proc的機制,就是在kernel里面創(chuàng)建一個設(shè)備節(jié)點,
然后android的上層代碼,可以通過讀寫文件的方式,來由上層直接到kernel來達到我們想要的目的。
先講上層的代碼:
可以在Android的代碼里面,可以在應(yīng)用代碼里面 應(yīng)用架構(gòu)里面 或HAL層應(yīng)該都可以吧,不過我的是在應(yīng)用層里面,是java代碼里面。
import android.os.SystemProperties;
import android.util.Log;
import android.widget.Toast; //wang
import java.util.Timer;
import java.util.TimerTask;
import android.os.PowerManager;
import java.io.*;
import android.os.Looper;
上面是java代碼需要的庫
if (true) log("updateScreen()...");
if (true) log("/*************************lu updateScreen Phone ***************************/ ");
File awakeTimeFile = new File("proc/sound8976/sound8976_galley_select");
FileWriter fr;
try{
?? fr = new FileWriter(awakeTimeFile);
?? fr.write("1");
?? if (true) log("/************************* updateScreen write success ***************************/ ");
?? fr.close();
}catch (IOException e) {
?? e.printStackTrace();
}
上面是java的寫文件代碼,是向/proc/sound8976/sound8976_galley_select 寫1?? 直接就可以調(diào)用kernel里面的proc的寫函數(shù),留意下面的寫函數(shù)。
if (!mIsForegroundActivity) { //wang add
?? if (DBG) log("onPhoneStateChanged: Activity not in foreground! Bailing out...");
?? if (DBG) log("onPhoneStateChanged: Activity not in foreground! Bailing out...");
?? /*************lu add down************/
?? File awakeTimeFile = new File("proc/sound8976/sound8976_galley_select");
?? FileWriter fr;
?? try {
????? fr = new FileWriter(awakeTimeFile);
????? fr.write("0");
????? if (true) log("/*************************ulu updateScreen write success ***************************/ ");
????? fr.close();
?? } catch (IOException e) {
????? e.printStackTrace();
?? }
?? return;
}
上面是java的寫文件代碼,是向/proc/sound8976/sound8976_galley_select 寫0? 直接就可以調(diào)用kernel里面的proc的寫函數(shù),留意下面的寫函數(shù)。
其實還有讀文件的函數(shù),我在這里就不列舉了。
下層代碼:
這個主要是kernel里面的代碼:
/*********lu add down******/
extern char g_selected_codec[];
if(!strcmp(g_selected_codec, "wm8976")){
#defineMODEM_SWITCH_PROC_NAME"sound8976_galley_select"
#definePROC_NAME"sound8976"
?? extern struct proc_dir_entry proc_root;
?? struct proc_dir_entry *root_entry;
?? struct proc_dir_entry *entry;
?? root_entry = proc_mkdir(PROC_NAME, &proc_root);
?? s_proc = create_proc_entry(MODEM_SWITCH_PROC_NAME, 0666, root_entry);
?? if (s_proc != NULL){
????? s_proc->write_proc = modem_switch_writeproc;
????? s_proc->read_proc = modem_switch_readproc;
?? }
}
printk(KERN_INFO "Initializing wm8976_modinit...\n");
/********lu add up********/
上面是在kernel里面建立文件的節(jié)點/proc/sound8976/sound8976_galley_select
/**************lu add down********/
/********* creat mode8976 in proc ******/
int sound8976_galley_select_flag = 0;
static struct proc_dir_entry * s_proc = NULL;
static int modem_switch_writeproc(struct file *file,const char *buffer,unsigned long count, void *data)
{
??? int value;
??? value = 0;
??? sscanf(buffer, "%d", &value);
??? switch(value)
??? {
???????? case 0:
????????? wm8976_set_register_sound(value);
????????? break;
???????? case 1:
????????? wm8976_set_register_sound(value);
????????? break;
???????? default:
????????? printk("************************luu sound8976_galley_select_flag ==null***********************\n");
????????? break;
?????? }
?????? return count;
}
上面是proc的寫函數(shù),主要是響應(yīng)java的寫文件函數(shù)。
static int modem_switch_readproc(char *page, char **start, off_t off,int count, int *eof, void *data)
{
?? int len;
?? len = sprintf(page, "%d\n", sound8976_galley_select_flag==0?0:(sound8976_galley_select_flag==1?1:(sound8976_galley_select_flag==2?2:3))); //wangyulu
?? if (off + count >= len)
???? *eof = 1;
?? if (len < off)
???? return 0;
? *start = page + off;
? return ((count < len - off) ? count : len - off);
}
經(jīng)過上面的操作,想看看寫的是否正確 就可以在終端用 下滿的指令,就知道文件里面是0 或是 1了。
/ # cat /proc/sound8976/sound8976_galley_select
0
其實這個方法還是不錯的,希望對用android的朋友有幫助,如果有事嗎疑問可以可我的博客留言,我們一起探討,權(quán)當拋磚引玉,謝謝
Android上層怎樣讀寫proc節(jié)點(示例)
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
import android.os.SystemProperties;
import android.util.Log;
import android.widget.Toast; //wang
import java.util.Timer;
import java.util.TimerTask;
import android.os.PowerManager;
import java.io.*;
import android.os.Looper;
public class DebugOptionActivity extends Activity{
?? private static final String TAG = "tpDebug";
?? private int checkValue = 0;
?? @Override
?? public void onCreate(Bundle savedInstanceState){
????? super.onCreate(savedInstanceState);
????? setContentView(R.layout.debug);
????? ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle);
????? try{
????????? File readFile = new File("proc/tp_debug/debug_switch");
????????? FileReader inCmd = new FileReader(readFile);
????????? try{
????????????? checkValue = inCmd.read();
????????????? if(checkValue != 0){
???????????????? toggle.setChecked(true);
??????????????? } else{
????????????????? toggle.setChecked(false);
??????????????? }
???????????? }catch (IOException e){
?????????????? e.printStackTrace();
???????????? }
?????????? } catch (FileNotFoundException e){
???????????? e.printStackTrace();
?????????? }
?????????? //ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle);
?????????? toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){
??????????? public void onCheckedChanged(CompoundButton arg0, boolean arg1){
???????????? if (arg1) {
???????????????? //open
???????????????? File awakeTimeFile = new File("proc/tp_debug/debug_switch");
???????????????? FileWriter fr;
???????????????? try {
?????????????????? fr = new FileWriter(awakeTimeFile);
?????????????????? fr.write("1");
?????????????????? fr.close();
???????????????? }catch (IOException e) {
?????????????????? e.printStackTrace();
???????????????? }
????????????? }else{
???????????????? //close
???????????????? File awakeTimeFile = new File("proc/tp_debug/debug_switch");
???????????????? FileWriter fr;
???????????????? try{
??????????????????? fr = new FileWriter(awakeTimeFile);
??????????????????? fr.write("0");
??????????????????? fr.close();
???????????????? } catch (IOException e) {
??????????????????? e.printStackTrace();
???????????????? }
????????????? }
?????????? }
?????????? });
???? }
}