|Design| Communicate with Kernel driver

User space can not exchange data with kernel space directly.
They need to use system call.
e.g. fopen, ioctl, write, read …… etc.

In Kernel Space ……
1. Register your device with “register_chrdev”, defined in linux/fs.h .
 https://ithelp.ithome.com.tw/articles/10159749
2. Implement driver functions, ioctl, open,…
 struct file_operations fops = {
  .owner = THIS_MODULE,
  .read = device1_read,
  .write = device1_write,
  .ioctl = device1_ioctl,
  .open = device1_open,
  .release = device1_release,
 };

 int ioctl(struct inode *, struct file *, unsigned int, unsigned long);
 is in linux/ioctl.h
 http://ccckmit.wikidot.com/lk:io
 開發 driver 需要的基礎知識
 user space/kernel space 的IO觀念及實作

Copy data ……
 Kernel Space to User Space: copy_to_user()
 User Space to Kernel Space: copy_from_user()

In User Space ……
Use system call to control kernel driver.
 fopen (open)
 write
 read
 close
 seek
 poll / select
 ioctl
 mmap
 fcntl


e.g.

int main(int argc, char *argv[]){
  int devfd;
  int num = 0;

  if (argc > 1) num = atoi(argv[1]);
  if (num < 0) num = 0xff;

  devfd = open("/dev/debug", O_RDONLY);
  if (devfd == -1) {
    printf("Can't open /dev/debug\n");
    return -1;
  }

  printf("Write 0x%02x...\n", num);
  ioctl(devfd, IOCTL_WRITE, num);
  printf("Done. Wait 5 seconds...\n");
  sleep(5);
  close(devfd);

  return 0;
}

|Code| IPC methods

Originally, I though it is a basic interview question and everyone has at least a common answer .
Until I found someone did not know , I know I was wrong.

Threads can shares the same resources such as address space, file descriptors, stack and other process related attributes in the same process.
Processes can not share resources directly.

Processes will communicate to each other by IPC method.
There are two ways:
 1. Shared Memory
 2. Message passing

The following methods, which we often see, belong to message passing.
Pipe
Socket
Remote Procedural calls (RPCs)



https://www.geeksforgeeks.org/inter-process-communication-ipc/
https://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread

|Tools| Git v.s Repo

一般跟別人合作開發都是使用 git。那 repo 是什麼?
repo 是 Google 開發出來的工具,用來管理 Android 的。

一份 Android code 是由許多 project 組成的,每個 project 就是一個 git project。
repo 這個工具,就是可以管理 branches 以及眾多的 git project!

> repo init 後,可以找到 .repo/manifest.xml 這會記錄你控制的 projects

*基本工作流程與對照:

repo git
repo init -u URL -m ooxx.xml -b remote_branch git clone URL
repo sync -c git pull URL remote_branch:local_branch
repo start branch_name –all git branch branch_name
git status git status
git add git add
git commit git commit
repo upload . git push


*git 好用指令:
 > 查詢遠端的server 位置及名字
 git remote -v
 > 新增一個遠短 server
 git remote add {given_name} URL
 > 強迫 reset
 git reset –hard {local_branch}
 > 把 commit 推到遠端 branch
 git push {given_name} {local_branch}: refs/for/{remote_branch}
 > 從遠端 branch 拉 code 下來
 git pull {given_name} {remote_branch}:{local_branch}
 > 當前一個修改已 commit,又想把這次修改跟上一個修改一起 commit
 git commit –amend
 > 想上 patch 到某個已經 push 出去的 change
 git push {given_name} {local_branch}:refs/changes/{change_id}
 > 查詢某行的歷史
 git blame -L [行數] [檔案名]
 > 檢別 branch 的 commit 到目前的 branch
 git cherry-pick [sha-1]

*好用的配置
 > git commit 的模板:
 新增一個  ~/.git-template
 然後 git config commit.template ~/.git-template

 > 設置reviewers, git push rest,就會自動帶上 reviewers
 到 .git/config 編輯
 [remote “test”]
 pushurl = ssh://account@server_address:port/project_name
 push = {local_branch}:refs/for/{remote_branch}%r=someone1@mail, r=someone2@mail

*製作 patch,打 patch
 (在 branch A)
 git log
 git format-patch -1 [sha-1]
 mv ooxx.patch ~/
 (換到 branch B)
 git am [~/ooxx.patch]


值得注意的是:
pull 的 server / review 的 server / push 的 server 可能不同,
設置 push 的 server 時,請去確認 repo upload 時,到底是推到哪?


ref. [Coding] repo & git 的使用方法

|Design| bit map

如果你的某個宣告有多重意義,
但你一次只想知道某的欄位的狀態,可以善加利用 bit map

舉例:

#define status1 (uint_8) 1<<0
#define status2 (uint_8) 1<<1
#define status3 (uint_8) 1<<2

uint_8 test_case = 2;
int test_hex_case = 0xFA;

If( test_case & status1 ){ // false; 00000000 <- 00000010 & 00000001
  //do nothing
}
If( test_case & status2 ){ // true, not zero; 00000010 <- 00000010 & 00000010
  //do something
}
If( test_hex_case & status2 ){ // true, not zero;
// 0xFA = 1111 1010
// 00000010 <- 1111 1010 & 00000010
  // do something
}

|Job| Application No Response Analysis

ANR 的發生情形,通常是時間到了還沒有回應!所以掌握這個想法,就比較好找 Code。

比較常見有三種情形:

  1. Service Timeout
  2. BroadcastQueue Timeout
  3. ContentProvider Timeout

 

Case 1. Service Timeout

 a. Service 啟動後,會發送 SERVICE_TIMEOUT_MSG 消息
 b. Service 刪除後,就會刪除 SERVICE_TIMEOUT_MSG 消息
 c. 如果 SERVICE_TIMEOUT_MSG 未删除,時間到後就會 ANR

 呼叫 ActiveServices 的 serviceTimeout 方法進行處理,
 serviceTimeout方法邏輯:

 if (anrMessage != null) {
  mAm.appNotResponding(proc, null, null, false, anrMessage);
 }

 

Case 2. BroadcastQueue Timeout

 BroadcastQueue 中的 mHandler 收到 BROADCAST_TIMEOUT_MSG 消息時會觸發

 // 正常要發送消息前
 broadcastTimeoutLocked(false);

 // 發送消息,鎖住
 case BROADCAST_TIMEOUT_MSG: {
  synchronized (mService) {
   broadcastTimeoutLocked(true);

 // ANR
 在 broadcastTimeoutLocked 方法中,首先取得 anrMessage 字串

 anrMessage = “Broadcast of ” + r.intent.toString();

 mHandler.post(new AppNotResponding(app, anrMessage));
 mService.appNotResponding(mApp, null, null, false, mAnnotation);

 

Case 3. ContentProvider Timeout

 MainHandler 的 handleMessage CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG

 private final void processContentProviderPublishTimedOutLocked(ProcessRecord app) {
cleanupAppInLaunchingProvidersLocked(app, true);
 removeProcessLocked(app, false, true, “timeout publishing content providers”);
 }

 app.kill(reason, true);

 

ref.

https://blog.csdn.net/u012439416/article/category/7261623

 

|Jobs| How to write your own daemon?

要怎樣寫一個服務:他能夠

在一開機啟動、

不易死掉(你可以寫 script 一直啟動他)、

fork() 後終止父行程並和呼叫的終端機(TTY)脫離關係?

 

首先,我們可以寫個服務(執行檔或是一個 script),處理我們要做的事。

接著,我們再準備一個script,放到正確的位置。而這份草稿他的目的最主要就是: 告訴系統,在一開機啟動我們的服務!

以下最主要就是介紹這個 script 該如何寫:



.Method 1: traditional

1. WRITE: /etc/init.d/triggerd
    to run script or daemon: $TRIGGERD_HOME/bin/triggerMyService.sh

 #!/bin/bash
 start_triggerd=$TRIGGERD_HOME/bin/triggerMyService.sh
 stop_triggerd=$TRIGGERD_HOME/bin/triggerMyService.sh

 start() {
  echo -n “Starting MyService”
 }

 stop() {
  echo -n “Shutdown MyService”
 }

 #how we were called
 case “$1” in
  start)
     start
   ;;
  stop)
   stop
   ;;
  restart)
   stop
   sleep 10
   start
   ;;
  *)
   echo “Usage: $0 {start|stop|restart}”
 esac
 exit 0


2. chmod 777 /etc/init.d/triggerd



.Method 2: systemctl

1. 建立一個名為 triggerd.service 的系統服務指令碼
 vim /usr/lib/systemd/system/triggerd.service

 [Unit]
 Description=triggerMyService.sh

 [Service]
 Type=forking
 PIDFile=…
 Environment=CATALINA_HOME=/usr/local/triggerMyService.sh

 ExecStart=/usr/local/triggerMyService.sh start
 ExecStop=/usr/local/triggerMyService.sh stop
 User=…
 Group=…
 PrivateTmp=true

 [Install]
 WantedBy=multi-user.target

2. 重新載入一下配置
 systemctl daemon-reload

3. 設定允許開機自啟動
 systemctl enable triggerd

 #啟動服務
 systemctl start triggerd

 

ref.

創建 Daemon 程式

start-stop-daemon 啟動停止系統守護進程

[開機啟動]Linux開機自啟和運行級別

centos7下配置Tomcat開機啟動(service方式和daemon方式)

Linux systemd 寫 可自動啟動的 Daemon Service

 

|Jobs| How do I teach a Trainee?

哇 工作才剛三年,竟然有機會要帶新人。(因為大家太忙了,我相對離新人不久 XD)

我想我能做的就是:

帶他使用工具(系統廠的 debugger)

克服環境設定的任何問題(這個沒弄好很容易讓人放棄寫程式)

學會看 Code(執行檔要從 main 開始看哦 ^.<)

哪裡找文件

軟體架構上下層輸出輸入的關係

請他找一個自己喜歡的看 Code 軟體

其實在這過程,就會想起之前學長怎麼帶的及前老闆精闢的指點。

大家對我真的是蠻有耐心的,十分感謝。

希望我也能在新人心中留下好印象。

|Tools| Useful Linux Commands

.在特定類型的文件裡找字串
grep -ir “ooxx“ –include=”*.java”

.掛載外接硬碟
sudo fdisk -l
mkdir /media/usb
sudo mount -v -t auto /dev/sdb1 /media/usb

.找特定類型的檔案

find -iname *.git

.確認編譯出來檔案的 ELF

readelf

.列出所有執行的東西

ps -Al

WordPress.com.

Up ↑