// File: /workspace/github_c_makefile_star2_O1/bokunimowakaru[P]petit15term/petit15writer.c
// Function: main
int main(int argc,char **argv){
    int fd=-1,loop=1,i,com=0,ret,ver,ok,wait;
    time_t time_val,time_prev;
    char c;
    char s[256],ss[16];
    struct termios uartattr;
    //                    0123456789ABC
    char UART_DEVICE[15]="/dev/ttyUSB10";

    printf("-------------------------------------\n");
    printf("Petit Ichigo Writer  by Wataru KUNINO\n");
    printf("-------------------------------------\n");
    printf("注意：ファームウェアを書き換えます。\n");
    printf("This software updates firmware on LPC\n");
    printf("-------------------------------------\n");
    
    if( argc == 3 ) TARGET_VERSION = atoi(argv[2]);
    if( argc >= 2){
        i=atoi(argv[1]);
        if( i>0 && i<= 64){
            snprintf(&UART_DEVICE[5],8,"ttyS%1d",i-1);  // Cygwin PC用
            fd = uart_open(UART_DEVICE, &uartattr);
            com=i;
        }
    }
    if(com==0) for(i=22;i>=0;i--){
        if(i>=20){
            snprintf(&UART_DEVICE[5],8,"rfcomm%1d",i-20);   // ポート探索(rfcomm0-2)
        }else if(i>=10){
            snprintf(&UART_DEVICE[5],8,"ttyUSB%1d",i-10);  // ポート探索(USB0～9)
        }else if(i==9) snprintf(&UART_DEVICE[5],8,"ttyAMA0");   // 拡張IOのUART端子に設定
        else{
            snprintf(&UART_DEVICE[5],8,"ttyS%1d",i);            // Cygwin PC用
            com=i+1;
        }
    //  log_date(UART_DEVICE);
        fd = uart_open(UART_DEVICE, &uartattr);
        if(fd >=0 ) break;
    }
    
    if (fd < 0){
        log_date("ERROR @ UART OPEN");
        return 1;
    }else{
		if(com) sprintf(s,"COM PORT = COM%d (%s)",com,UART_DEVICE);
        else sprintf(s,"COM PORT = %s",UART_DEVICE);
        log_date(s);
    }
    if(close_serial_port()){            // 一旦閉じる
        log_date("ERROR @ UART CLOSE");
        return 1;
    }
    
    while(loop){
		if(com) sprintf(ss,"com%d",com);
		else sprintf(ss,"%s",UART_DEVICE);
        sprintf(s,"lpc21isp/lpc21isp -verify -donotstart firmware/ichigojam-xtal.hex %s 115200 12000",ss);
        log_date(s);
        log_date("Please connect a LPC Device to your jig.");
        log_date("LPCマイコンをボードにセットしてISPボタンを押してください");
        ret=system(s);
        sprintf(s,"lpc21isp (%d)",ret);         // 256:ファームウェア無し、2560 ユーザ終了 65280
        log_date(s);
        switch(ret){
            case 2560:
                log_date("[Esc]キーが押されたので書き換えを中断しました");
                return 1;
                break;
            case 0:
                log_date("SUCCESS");
                log_date("書き込みに成功しました");
                break;
            default:
                log_date("エラーが発生したので終了します");
                return 1;
                break;
        }

        log_date("Please press reset button on IchigoJam.");
        log_date("マイコンボードのリセットボタンを押してください");
        uart_open(UART_DEVICE, &uartattr);

        wait=0; ok=0; ver=0;
        while(ok>=0){
            if( kbhit() ){
                log_date("Aborted by user.");
                close_serial_port();
                return 1;
            }
            c=read_serial_port();
            if(c){
                if( wait > 3 ){
                    printf("\n");
                }
                wait =0;
                if( isprint((int)c) ) printf("%c",c);
                if( c =='\n' ){
                    printf("\n");
                    if(c =='\n' && ok == 2 && ver == TARGET_VERSION) ok=-1; // ループ終了
                }else if( isdigit((int)c) ){
                    ver *=10;
                    ver += (int)(c - '0');
                }else if( ver !=0 && ver != TARGET_VERSION && c !='.' ){
                    printf("\n");
                    log_date("IchigoJam Version ERROR.");
                    log_date("期待バージョンと異なるので終了します");
                    close_serial_port();
                    return 1;
                }else if( ok==0 && c=='O' ){
                    ok++;
                }else if( ok==1 && c=='K' ){
                    ok++;
                }else if( ok && isprint((int)c) ) ok=0;
            }
            time(&time_val);
            if( time_prev != time_val ){
                time_prev = time_val;
                wait++;
                if( wait>30 ){
                    printf("\n");
                    log_date("No response from IchigoJam.");
                    log_date("IchigoJamからの応答がありません");
                    close_serial_port();
                    return 1;
                }
                if( wait > 3) printf(".");
            }
        }
        if(close_serial_port()){
            log_date("ERROR @ UART CLOSE");
            return 1;
        }
        log_date("CLOSED");
    }
    return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rsi, %r14
movl %edi, %ebp
movabsq $0x303142535579, %rax   # imm = 0x303142535579
movq %rax, 0x1f(%rsp)
movabsq $0x7974742f7665642f, %rax # imm = 0x7974742F7665642F
movq %rax, 0x18(%rsp)
leaq 0xe6c(%rip), %rbx       # 0x23c1
movq %rbx, %rdi
callq 0x1060
leaq 0xdda(%rip), %rdi       # 0x233e
callq 0x1060
movq %rbx, %rdi
callq 0x1060
leaq 0xdec(%rip), %rdi       # 0x2364
callq 0x1060
leaq 0xe17(%rip), %rdi       # 0x239b
callq 0x1060
movq %rbx, %rdi
callq 0x1060
cmpl $0x3, %ebp
jne 0x15a5
movq 0x10(%r14), %rdi
callq 0x1170
movl %eax, 0x2b43(%rip)      # 0x40e8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
cmpl $0x2, %ebp
jl 0x15ee
movq 0x8(%r14), %rdi
callq 0x1170
movl %eax, %r14d
leal -0x1(%r14), %ecx
cmpl $0x3f, %ecx
ja 0x15ee
leaq 0x1d(%rsp), %rdi
leaq 0xa6e(%rip), %rdx       # 0x2041
movl $0x8, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x18(%rsp), %rdi
callq 0x13bc
movl %eax, %ebx
movl %r14d, %r15d
movl %r15d, 0xc(%rsp)
testl %r15d, %r15d
je 0x162b
testl %ebx, %ebx
js 0x16d9
movl 0xc(%rsp), %ebx
testl %ebx, %ebx
je 0x16e2
leaq 0xa5c(%rip), %rsi       # 0x206f
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rcx
movl %ebx, %edx
xorl %eax, %eax
callq 0x1180
jmp 0x16fa
leaq 0x1d(%rsp), %r14
movl $0x17, %r13d
leaq 0xa0c(%rip), %r15       # 0x2049
leaq 0x18(%rsp), %r12
movl $0x0, 0xc(%rsp)
leal -0x1(%r13), %ebp
cmpl $0x14, %ebp
jb 0x1664
leal -0x15(%r13), %ecx
movl $0x8, %esi
movq %r14, %rdi
movq %r15, %rdx
jmp 0x167c
cmpl $0xa, %ebp
jb 0x16a3
leal -0xb(%r13), %ecx
movl $0x8, %esi
movq %r14, %rdi
leaq 0x9d7(%rip), %rdx       # 0x2053
xorl %eax, %eax
callq 0x10c0
movq %r12, %rdi
callq 0x13bc
movl %eax, %ebx
testl %eax, %eax
jns 0x15f8
cmpl $0x1, %r13d
movl %ebp, %r13d
jne 0x164a
jmp 0x15f8
cmpl $0xa, %r13d
jne 0x16ba
movabsq $0x30414d41797474, %rax # imm = 0x30414D41797474
movq %rax, 0x1d(%rsp)
jmp 0x1683
movl $0x8, %esi
movq %r14, %rdi
leaq 0x978(%rip), %rdx       # 0x2041
movl %ebp, %ecx
xorl %eax, %eax
callq 0x10c0
movl %r13d, 0xc(%rsp)
jmp 0x1683
leaq 0x97d(%rip), %rdi       # 0x205d
jmp 0x172e
leaq 0x99c(%rip), %rsi       # 0x2085
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1180
leaq 0x40(%rsp), %rdi
callq 0x136e
movl 0x29e6(%rip), %edi      # 0x40f0
leaq 0x29e3(%rip), %rdx      # 0x40f4
xorl %esi, %esi
callq 0x1140
movl 0x29d2(%rip), %edi      # 0x40f0
callq 0x10d0
testl %eax, %eax
je 0x174a
leaq 0x965(%rip), %rdi       # 0x2093
callq 0x136e
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r15, %rdi
testl %ebx, %ebx
je 0x176d
leaq 0x944(%rip), %rsi       # 0x20a6
movl %ebx, %edx
xorl %eax, %eax
callq 0x1180
jmp 0x1775
movq %r12, %rsi
callq 0x1050
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x928(%rip), %rsi       # 0x20ac
movq %r15, %rdx
xorl %eax, %eax
callq 0x1180
movq %r13, %rdi
callq 0x136e
leaq 0x961(%rip), %rdi       # 0x20fe
callq 0x136e
leaq 0x97e(%rip), %rdi       # 0x2127
callq 0x136e
movq %r13, %rdi
callq 0x1090
movl %eax, %ebx
movq %r13, %rdi
leaq 0x9b7(%rip), %rsi       # 0x2179
movl %eax, %edx
xorl %eax, %eax
callq 0x1180
movq %r13, %rdi
callq 0x136e
testl %ebx, %ebx
jne 0x1a02
leaq 0x9e7(%rip), %rdi       # 0x21c9
callq 0x136e
leaq 0x9e3(%rip), %rdi       # 0x21d1
callq 0x136e
leaq 0xa27(%rip), %rdi       # 0x2221
callq 0x136e
leaq 0xa43(%rip), %rdi       # 0x2249
callq 0x136e
movq %r12, %rdi
callq 0x13bc
xorl %ebx, %ebx
xorl %r12d, %r12d
movq $0x0, 0x10(%rsp)
callq 0x12ac
testl %eax, %eax
jne 0x19b3
callq 0x144f
testb %al, %al
je 0x18fd
movl %eax, %ebp
cmpl $0x4, %ebx
jl 0x184c
movl $0xa, %edi
callq 0x1030
callq 0x1190
movq %rax, %r15
movq (%rax), %rax
movsbl %bpl, %ebx
movsbq %bpl, %r13
testb $0x40, 0x1(%rax,%r13,2)
je 0x186e
movl %ebx, %edi
callq 0x1030
cmpb $0xa, %bpl
jne 0x18a1
movl $0xa, %edi
callq 0x1030
movl %r12d, %eax
xorl $0x2, %eax
movl 0x285e(%rip), %ecx      # 0x40e8
xorl 0x10(%rsp), %ecx
orl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
je 0x189a
movl %r12d, %eax
xorl %ebx, %ebx
movl %eax, %r12d
jmp 0x18fd
movq (%r15), %rax
movzwl (%rax,%r13,2), %eax
btl $0xb, %eax
jb 0x18e8
cmpl $0x0, 0x10(%rsp)
je 0x18cd
cmpb $0x2e, %bpl
je 0x18cd
movq 0x10(%rsp), %rcx
cmpl 0x2821(%rip), %ecx      # 0x40e8
jne 0x1a16
testl %r12d, %r12d
sete %cl
cmpb $0x4f, %bpl
sete %dl
xorl %ebx, %ebx
testb %dl, %cl
je 0x1940
movl $0x1, %r12d
jmp 0x18fd
movq 0x10(%rsp), %rax
leal (%rax,%rax,4), %eax
leal (%rbx,%rax,2), %eax
addl $-0x30, %eax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
callq 0x1100
movq 0x28(%rsp), %r15
cmpq %r15, %r14
jne 0x1915
movl %ebx, %ebp
jmp 0x1933
cmpl $0x1e, %ebx
jge 0x19bc
leal 0x1(%rbx), %ebp
cmpl $0x3, %ebx
jl 0x1930
movl $0x2e, %edi
callq 0x1030
movq %r15, %r14
movl %ebp, %ebx
testl %r12d, %r12d
jns 0x1821
jmp 0x196d
cmpl $0x1, %r12d
setne %cl
cmpb $0x4b, %bpl
setne %dl
orb %cl, %dl
jne 0x1957
incl %r12d
jmp 0x18fd
testl %r12d, %r12d
je 0x1968
xorl %ebx, %ebx
btl $0xe, %eax
cmovbl %ebx, %r12d
jmp 0x18fd
xorl %r12d, %r12d
jmp 0x18fb
movl 0x277d(%rip), %edi      # 0x40f0
xorl %esi, %esi
leaq 0x2778(%rip), %rdx      # 0x40f4
callq 0x1140
movl 0x2769(%rip), %edi      # 0x40f0
callq 0x10d0
testl %eax, %eax
movl 0xc(%rsp), %ebx
jne 0x1727
leaq 0x998(%rip), %rdi       # 0x2337
callq 0x136e
leaq 0x30(%rsp), %r15
leaq 0x18(%rsp), %r12
jmp 0x1754
leaq 0x8d5(%rip), %rdi       # 0x228f
jmp 0x19d9
movl $0xa, %edi
callq 0x1030
leaq 0x923(%rip), %rdi       # 0x22f0
callq 0x136e
leaq 0x933(%rip), %rdi       # 0x230c
callq 0x136e
movl 0x270c(%rip), %edi      # 0x40f0
leaq 0x2709(%rip), %rdx      # 0x40f4
xorl %esi, %esi
callq 0x1140
movl 0x26f8(%rip), %edi      # 0x40f0
callq 0x10d0
jmp 0x1733
cmpl $0xa00, %ebx            # imm = 0xA00
jne 0x1a42
leaq 0x776(%rip), %rdi       # 0x2187
jmp 0x172e
movl $0xa, %edi
callq 0x1030
leaq 0x879(%rip), %rdi       # 0x22a0
callq 0x136e
leaq 0x886(%rip), %rdi       # 0x22b9
callq 0x136e
callq 0x14f9
jmp 0x1733
leaq 0x7aa(%rip), %rdi       # 0x21f3
jmp 0x172e