20151228_jeffrey - silenceuncrio/diary GitHub Wiki

Index

  • 0900 - review diary and prepare engineering notebook
  • 0940 - mail - Continue your Apple Developer Program enrollment.
  • 0945 - 請款單 - 之前購買樹梅派
  • 1015 - 跨年的假, 先前買的零件, engineering notebook
  • 1020 - IOT Platform - Message Exchange - Handler - review
  • 1030 - IOT Platform - Message Exchange - Handler - RX Handler
  • 1105 - IOT Platform - Message Exchange - Handler - RX Handler - 解析 POST 的字串
  • 1235 - 買 Kingston 8GB DDR3 1600筆記型記憶體 給筆電用
  • 1305 - IOT Platform - Message Exchange - Handler - RX Handler - 繼續解析 POST 的字串
  • 1420 - IOT Platform - Message Exchange - Handler - RX Handler - 解出 POST 的變數
  • 1500 - MonkeyJJ - 繼電器 - 研究
  • 1700 - IOT Platform - Jammy 問到 AMEBA 怎麼送 RESTful 給 Control Center
  • 1850 - IOT Platform - 利用工讀生他們的環境來做事

0900

review diary and prepare engineering notebook

0940

Continue your Apple Developer Program enrollment.

From: Apple Developer Support [email protected]
Date: 2015-12-25 14:44 GMT+08:00
Subject: Continue your Apple Developer Program enrollment.
To: [email protected]

Dear TSUNG YI LEE,

Your enrollment request has been accepted. You may now review the Program License Agreement to continue your organization's enrollment in the Apple Developer Program.

Review now

Best regards, Apple Developer Program Support

TM and copyright © 2015 Apple Inc. 1 Infinite Loop, MS 96-DM, Cupertino, CA 95014. All Rights Reserved / Privacy Policy / My Apple ID

公司的 Apple 帳號已經可以進入付款的階段
charlie 表示 bpoint 的 iOS APP 要到一月底左右才會做好
先不急著付款

0945

寫一下之前購買樹梅派的請款單

  • 2015-10-14
    • UGreen HDMI轉VGA 含音源輸出 外接電源 x 1 - $460
    • 郵局快捷貨到付款 - $120
    • Pi2套件組 - 主機+外殼+散熱片+電源組+8G x 1 - $1920
    • 營業稅 - $125
  • 2015-10-16
    • TF 8G x 5 - 139 x 5 = 695
    • USB 讀卡機 - 115
  • 2015-11-27
    • 名片式跑馬燈 - 560

總計 2625 + 810 + 560 = 3995

1015

  • 跨年的假 - 請好了
  • 先前買的零件 - 請款了
  • engineering notebook - 交了

1020

review IOT Platform

這禮拜的目標是 Message Exchange - Handler
收送都要做完

參考 2015-12-25 1120 - IoT Platform - 與 JS9331 的 FastCGI 互動的環境構建 - 結論

  • JS9331 - HTTP Server - nginx with FastCGI - RX Handler
    • 公司電腦 - HTTP Client - Postman - TX Handler
  • JS9331 - HTTP Client - curl - TX Handler
    • 公司電腦 - HTTP Server - Node.js - RX Handler

1030

先做 RX Handler

  • JS9331 - HTTP Server - nginx with FastCGI - RX Handler
    • 公司電腦 - HTTP Client - Postman - TX Handler

參考 2015-12-18 1050 - JS9331 - 再準備一次 Nginx

fcgi_test.c

#include <stdio.h>
#include <json-c/json.h>
#include "fcgiapp.h"

json_object *obj;

int main () {
    FCGX_Stream *in, *out, *err;
    FCGX_ParamArray envp;
    while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
        FCGX_FPrintF(out, "Content-type: application/json\r\n\r\n");
        obj = json_object_new_object();
        json_object_object_add(obj, "ok", json_object_new_boolean(TRUE));
        FCGX_FPrintF(out, "%s", json_object_to_json_string(obj));
        json_object_put(obj);
    }
    return 0;
}
mips-openwrt-linux-uclibc-gcc \
-I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ \
-L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ \
-I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ \
-L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ \
-lm \
-ldl \
-lpthread \
-lfcgi \
-ljson-c \
-o fcgi_test fcgi_test.c

先來寫個 Makefile

參考 The GNU Make Book

有點進階

參考 Managing Projects with GNU Make, 3rd Edition

寫個最基本的就好了... 有空再說

makefile

fcgi_test: fcgi_test.c
	mips-openwrt-linux-uclibc-gcc \
	-I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ \
	-L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ \
	-I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ \
	-L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ \
	-lm -ldl -lpthread -lfcgi -ljson-c \
	-o fcgi_test fcgi_test.c

1105

一步一步前進

來解析 POST 的字串

1235

買 Kingston 8GB DDR3 1600筆記型記憶體 給筆電用

1305

繼續解析 POST 的字串

參考 CGI Developer's Guide

cgihtml

Processing CGI input in C is more complex than it is in Perl; consequently, cgihtml is more complex internally.
As you will shortly see, however, your CGI programs in C can be just as simple as the ones in Perl from the preceding section.

First, you need to define a data structure. cgihtml defines a linked list in llist.h as seen in Listing 5.7.

Listing 5.7. Linked list in llist.h (from Eugene Kim's cgihtml).

typedef struct {
  char *name;
  char *value;
} entrytype;
typedef struct _node {
  entrytype entry;
  struct _node* next;
} node;
typedef struct {
  node* head;
} llist;

懶得寫了

參考 cgihtml

下載了 cgihtml-1.69.tar.gz

解開在 D:\ubuntu_share\js9331_event_cgi\cgihtml-1.69

修改了兩隻 Makefile

D:\ubuntu_share\js9331_event_cgi\cgihtml-1.69\Makefile`

# macros and variables
CC= mips-openwrt-linux-uclibc-gcc
AR= mips-openwrt-linux-uclibc-ar
RANLIB= mips-openwrt-linux-uclibc-ranlib
CFLAGS= -g -Wall -DUNIX #-DUPLOADDIR='"/tmp"'

OBJS = string-lib.o cgi-llist.o cgi-lib.o html-lib.o
LIB = cgihtml.a

# targets
$(LIB): $(OBJS)
	$(AR) cr $@ $(OBJS)
	$(RANLIB) $@

examples: $(LIB)
	cd examples; make all

all: examples

clean:
	rm -f *.o $(LIB)

clobber:
	rm -f *.o $(LIB)
	cd examples; make clean

D:\ubuntu_share\js9331_event_cgi\cgihtml-1.69\examples\Makefile


# macros and variables
CC= mips-openwrt-linux-uclibc-gcc
CFLAGS= -g -Wall -DUNIX #-DUPLOADDIR=/tmp
LIB = ../cgihtml.a

TARGETS = query-results mail.cgi index-sample.cgi ignore.cgi test.cgi

# targets
query-results: $(LIB) query-results.o
	$(CC) -o $@ query-results.o $(LIB)

mail.cgi: $(LIB) mail.cgi.o
	$(CC) -o $@ mail.cgi.o $(LIB)

index-sample.cgi: $(LIB) index-sample.cgi.o
	$(CC) -o $@ index-sample.cgi.o $(LIB)

ignore.cgi: $(LIB) ignore.cgi.o
	$(CC) -o $@ ignore.cgi.o $(LIB)

test.cgi: $(LIB) test.cgi.o
	$(CC) -o $@ test.cgi.o $(LIB)

$(LIB):
	cd ..; make cgihtml.a

all: $(TARGETS)

clean:
	rm -f *.o $(TARGETS)

一次就過了

jeffrey@jeffrey-virtual-machine:/mnt/hgfs/share/js9331_event_cgi/cgihtml-1.69$ make
mips-openwrt-linux-uclibc-gcc -g -Wall -DUNIX    -c -o string-lib.o string-lib.c
mips-openwrt-linux-uclibc-gcc -g -Wall -DUNIX    -c -o cgi-llist.o cgi-llist.c
mips-openwrt-linux-uclibc-gcc -g -Wall -DUNIX    -c -o cgi-lib.o cgi-lib.c
cgi-lib.c: In function 'parse_form_encoded':
cgi-lib.c:205:8: warning: variable 'content_length' set but not used [-Wunused-but-set-variable]
   long content_length;
        ^
mips-openwrt-linux-uclibc-gcc -g -Wall -DUNIX    -c -o html-lib.o html-lib.c
mips-openwrt-linux-uclibc-ar cr cgihtml.a string-lib.o cgi-llist.o cgi-lib.o html-lib.o
mips-openwrt-linux-uclibc-ranlib cgihtml.a

1420

看看怎麼來解出 POST 的變數

cgi_test02.c

#include <stdio.h>
#include "html-lib.h"
#include "cgi-lib.h"

int main() {
    llist entries;
    int status;

    html_header();
    html_begin("Test CGI");
    h1("CGI Test Program");
    printf("<hr>\n");
    h2("CGI Environment Variables");
    print_cgi_env();
    status = read_cgi_input(&entries);
    printf("<h2>Status = %d</h2>\n",status);
    h2("CGI Entries");
    printf("<p>a: %s</p>", cgi_val(entries,"a"));
    printf("<p>b: %s</p>", cgi_val(entries,"b"));
    printf("<p>c: %s</p>", cgi_val(entries,"c"));
    html_end();
    list_clear(&entries);
    return 0;
}

makefile

cgi_test02: cgi_test02.c
	mips-openwrt-linux-uclibc-gcc \
	-I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ \
	-L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ \
	-I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ \
	-L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ \
	-lm -ldl -lpthread -lfcgi -ljson-c \
	-o cgi_test02 cgi_test02.c cgihtml-1.69/cgihtml.a

實際執行的結果

POST http://192.168.1.251/cgi-bin/cgi_test02 image

結果

CGI Environment Variables

SERVER_SOFTWARE = uhttpd
SERVER_NAME = 192.168.1.251
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.1
SERVER_PORT = 80
REQUEST_METHOD = POST
SCRIPT_NAME = /cgi-bin/cgi_test02
QUERY_STRING = 
REMOTE_HOST = 192.168.1.48
REMOTE_ADDR = 192.168.1.48
CONTENT_TYPE = multipart/form-data; boundary=----WebKitFormBoundaryWw8Hq2QDay4RZ2fU
CONTENT_LENGTH = 312

HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Status = 3

CGI Entries

a: 1

b: 2

c: 55

關鍵就在於 read_cgi_input(&entries);

然後就可已使用 cgi_val(entries,"key")) 來取得 key 的值

1500

MonkeyJJ - 繼電器 - 研究

1700

Jammy 問到 AMEBA 怎麼送 RESTful 給 Control Center

考慮到禮拜四那天請假... 幫弟弟他們寫個範例好了

1850

之前都還會 work 的開發環境
現在都動不起來

索性利用工讀生他們的環境來做事... 反正他們禮拜四才會來

Jammy 告知 AMEBA 情報

⚠️ **GitHub.com Fallback** ⚠️