Changeset 279


Ignore:
Timestamp:
Apr 29, 2017, 4:33:37 PM (7 years ago)
Author:
coas-nagasima
Message:

ファイルを追加、更新。

Location:
EcnlProtoTool/trunk
Files:
412 added
8 deleted
35 edited

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/asp3_dcre/arch/arm_gcc/common/core_support.S

    r270 r279  
    267267#else /* TOPPERS_CUSTOM_IDLE */
    268268        msr             cpsr_c, #CPSR_SVC_MODE  /* 割込みを許可(スーパバイザモード)*/
    269 ALABEL(dispatcher_2)
    270         b               dispatcher_2                    /* 割込みå¾
     269#endif /* TOPPERS_CUSTOM_IDLE */
     270        b               dispatcher_1                    /* 割込みå¾
    271271ち */
    272 #endif /* TOPPERS_CUSTOM_IDLE */
    273272
    274273/*
  • EcnlProtoTool/trunk/asp3_dcre/arch/arm_gcc/common/gic_support.S

    r270 r279  
    55 *  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
    66 *                              Toyohashi Univ. of Technology, JAPAN
    7  *  Copyright (C) 2006-2016 by Embedded and Real-Time Systems Laboratory
     7 *  Copyright (C) 2006-2017 by Embedded and Real-Time Systems Laboratory
    88 *              Graduate School of Information Science, Nagoya Univ., JAPAN
    99 *
  • EcnlProtoTool/trunk/asp3_dcre/arch/arm_gcc/rza1/chip_timer.c

    r270 r279  
    44 *      Advanced Standard Profile Kernel
    55 *
    6  *  Copyright (C) 2006-2016 by Embedded and Real-Time Systems Laboratory
     6 *  Copyright (C) 2006-2017 by Embedded and Real-Time Systems Laboratory
    77 *              Graduate School of Information Science, Nagoya Univ., JAPAN
    88 *
  • EcnlProtoTool/trunk/asp3_dcre/kernel/dataqueue.c

    r270 r279  
    66 *  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
    77 *                              Toyohashi Univ. of Technology, JAPAN
    8  *  Copyright (C) 2005-2016 by Embedded and Real-Time Systems Laboratory
     8 *  Copyright (C) 2005-2017 by Embedded and Real-Time Systems Laboratory
    99 *              Graduate School of Information Science, Nagoya Univ., JAPAN
    1010 *
     
    612612        CHECK_ID(VALID_DTQID(dtqid));
    613613        p_dtqcb = get_dtqcb(dtqid);
    614         CHECK_ILUSE(p_dtqcb->p_dtqinib->dtqcnt > 0U);
    615614
    616615        lock_cpu();
    617616        if (p_dtqcb->p_dtqinib->dtqatr == TA_NOEXS) {
    618617                ercd = E_NOEXS;
     618        }
     619        else if (!(p_dtqcb->p_dtqinib->dtqcnt > 0U)) {
     620                ercd = E_ILUSE;
    619621        }
    620622        else {
     
    628630                        }
    629631                }
    630         }
    631632        ercd = E_OK;
     633        }
    632634        unlock_cpu();
    633635
  • EcnlProtoTool/trunk/asp3_dcre/kernel/eventflag.c

    r270 r279  
    66 *  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
    77 *                              Toyohashi Univ. of Technology, JAPAN
    8  *  Copyright (C) 2005-2016 by Embedded and Real-Time Systems Laboratory
     8 *  Copyright (C) 2005-2017 by Embedded and Real-Time Systems Laboratory
    99 *              Graduate School of Information Science, Nagoya Univ., JAPAN
    1010 *
     
    250250                ercd = FLGID(p_flgcb);
    251251        }
     252        unlock_cpu();
    252253
    253254  error_exit:
  • EcnlProtoTool/trunk/asp3_dcre/kernel/task_manage.c

    r270 r279  
    388388                ercd = E_NOEXS;                                                 /*ï¼»NGKI3617ï¼½*/
    389389        }
    390         else if (TSTAT_DORMANT(tstat)) {                        /*ï¼»NGKI3620ï¼½*/
    391                 *p_tskstat = TTS_DMT;
    392         }
    393         else if (TSTAT_SUSPENDED(tstat)) {
    394                 if (TSTAT_WAITING(tstat)) {
    395                         *p_tskstat = TTS_WAS;
     390        else {
     391                if (TSTAT_DORMANT(tstat)) {                             /*ï¼»NGKI3620ï¼½*/
     392                        *p_tskstat = TTS_DMT;
     393                }
     394                else if (TSTAT_SUSPENDED(tstat)) {
     395                        if (TSTAT_WAITING(tstat)) {
     396                                *p_tskstat = TTS_WAS;
     397                        }
     398                        else {
     399                                *p_tskstat = TTS_SUS;
     400                        }
     401                }
     402                else if (TSTAT_WAITING(tstat)) {
     403                        *p_tskstat = TTS_WAI;
     404                }
     405                else if (p_tcb == p_runtsk) {
     406                        *p_tskstat = TTS_RUN;
    396407                }
    397408                else {
    398                         *p_tskstat = TTS_SUS;
    399                 }
    400         }
    401         else if (TSTAT_WAITING(tstat)) {
    402                 *p_tskstat = TTS_WAI;
    403         }
    404         else if (p_tcb == p_runtsk) {
    405                 *p_tskstat = TTS_RUN;
    406         }
    407         else {
    408                 *p_tskstat = TTS_RDY;
    409         }
    410         ercd = E_OK;
     409                        *p_tskstat = TTS_RDY;
     410                }
     411                ercd = E_OK;
     412        }
    411413        unlock_cpu();
    412414
  • EcnlProtoTool/trunk/asp3_dcre/kernel/time_manage.c

    r270 r279  
    66 *  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
    77 *                              Toyohashi Univ. of Technology, JAPAN
    8  *  Copyright (C) 2005-2015 by Embedded and Real-Time Systems Laboratory
     8 *  Copyright (C) 2005-2017 by Embedded and Real-Time Systems Laboratory
    99 *              Graduate School of Information Science, Nagoya Univ., JAPAN
    1010 *
     
    280280                }
    281281                switch (p_nfyinfo->nfymode & ~0x0fU) {
     282                case 0:
     283                        break;
    282284                case TENFY_SETVAR:
    283285                        CHECK_PAR(INTPTR_ALIGN(p_nfyinfo->enfy.setvar.p_var));
     
    352354                break;
    353355        case TNFY_SNDDTQ:
    354                 ercd = snd_dtq(p_nfyinfo->nfy.snddtq.dtqid,
     356                ercd = psnd_dtq(p_nfyinfo->nfy.snddtq.dtqid,
    355357                                                        p_nfyinfo->nfy.snddtq.data);
    356358                break;
     
    384386                        break;
    385387                case TENFY_SNDDTQ:
    386                         (void) snd_dtq(p_nfyinfo->enfy.snddtq.dtqid, (intptr_t) ercd);
     388                        (void) psnd_dtq(p_nfyinfo->enfy.snddtq.dtqid, (intptr_t) ercd);
    387389                        break;
    388390                default:
  • EcnlProtoTool/trunk/asp3_dcre/library/vasyslog.c

    r270 r279  
    6666
    6767void
    68 syslog(uint_t prio, const char *format, ...)
     68vsyslog(uint_t prio, const char *format, va_list ap)
    6969{
    7070        SYSLOG  logbuf;
    71         va_list ap;
    7271        uint_t  i;
    7372        char    sz;
     
    7776        logbuf.logpar[0] = (LOGPAR) format;
    7877        i = 1U;
    79         va_start(ap, format);
    8078
    8179        while ((c = *format++) != '\0' && i < TNUM_LOGPAR) {
     
    124122                }
    125123        }
    126         va_end(ap);
    127124        (void) tSysLog_eSysLog_write(prio, &logbuf);
    128125}
    129126
     127void
     128syslog(uint_t prio, const char *format, ...)
     129{
     130        va_list ap;
     131        va_start(ap, format);
     132        vsyslog(prio, format, ap);
     133        va_end(ap);
     134}
     135
     136/* mbed weak error function */
     137void
     138error(const char* format, ...) {
     139        va_list arg;
     140        va_start(arg, format);
     141        vsyslog(LOG_ERROR, format, arg);
     142        va_end(arg);
     143        ext_ker();
     144}
     145
    130146#endif /* TOPPERS_OMIT_SYSLOG */
  • EcnlProtoTool/trunk/asp3_dcre/mbed/Makefile.mbed

    r270 r279  
    6161START_OBJS = startup_RZ1AH.o
    6262SYSSVC_COBJS := $(SYSSVC_COBJS)\
    63         assert.o board.o error.o gpio.o lp_ticker_api.o mbed_interface.o\
     63        assert.o board.o gpio.o lp_ticker_api.o mbed_interface.o\
    6464        pinmap_common.o rtc_time.o semihost_api.o ticker_api.o us_ticker_api.o\
    6565        RZ_A1_Init.o cmsis_nvic.o gic.o mbed_sf_boot.o mmu_Renesas_RZ_A1.o\
  • EcnlProtoTool/trunk/asp3_dcre/mbed/targets/cmsis/TARGET_RENESAS/TARGET_RZ_A1H/mmu_Renesas_RZ_A1.c

    r270 r279  
    160160    //Define Image
    161161    __TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$RO_DATA$$Base, RO_DATA_SIZE, Sect_Normal_RO);
     162    __TTSection (&Image$$TTB$$ZI$$Base, 0x18200000, 4, Sect_Normal_Cod);
    162163    __TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$VECTORS$$Base, VECTORS_SIZE, Sect_Normal_Cod);
    163164    __TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$RW_DATA$$Base, RW_DATA_SIZE, Sect_Normal_RW);
  • EcnlProtoTool/trunk/asp3_dcre/target/gr_peach_gcc/target_kernel_impl.c

    r270 r279  
    298298target_exit(void)
    299299{
     300        static int first = 1;
     301
    300302        /*
    301303         *  チップ依存の終了処理
     
    306308         *  bkpt命令によりデバッガに制御を移す(パラメータが何が良いか未検討)
    307309         */
    308         Asm("bkpt #0");
     310        if (first){
     311                first = 0;
     312                Asm("bkpt #0");
     313        }
    309314
    310315        while (true) ;
  • EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/udp_usrreq.c

    r270 r279  
    485485                return E_NOEXS;
    486486
    487         return E_PAR;
     487        return E_OK/*E_PAR*/;
    488488        }
    489489
     
    518518                return E_NOEXS;
    519519
    520         return E_PAR;
     520        return E_OK/*E_PAR*/;
    521521        }
    522522
  • EcnlProtoTool/trunk/asp3_dcre/tinet/tcpip_api.def

    r270 r279  
    55UDP_CRE_CEP  #udp4_cepid* { .udp4_cepatr { &ipv4addr &portno } &callback }
    66UDP6_CRE_CEP #udp6_cepid* { .udp6_cepatr { &ipv6addr &portno } &callback }
    7 VRID_TCP_REP  #vrid_tcp4_repid*
    8 VRID_TCP_CEP  #vrid_tcp4_cepid*
     7VRID_TCP4_REP  #vrid_tcp4_repid*
     8VRID_TCP4_CEP  #vrid_tcp4_cepid*
    99VRID_TCP6_REP #vrid_tcp6_repid*
    1010VRID_TCP6_CEP #vrid_tcp6_cepid*
    11 VRID_UDP_CEP  #vrid_udp4_cepid*
     11VRID_UDP4_CEP  #vrid_udp4_cepid*
    1212VRID_UDP6_CEP #vrid_udp6_cepid*
  • EcnlProtoTool/trunk/asp3_dcre/tinet/tinet.trb

    r270 r279  
    7878        $tinetCfgH.add("#define #{params[:udp6_cepid]}  #{params[:udp6_cepid].val}")
    7979end
    80 $cfgData[:VRID_TCP_REP].each do |key, params|
    81         $tinetCfgH.add("#define #{params[:tcp4_repid]}  #{params[:tcp4_repid].val + $cfgData[:TCP_CRE_REP].size}")
    82 end
    83 $cfgData[:VRID_TCP_CEP].each do |key, params|
    84         $tinetCfgH.add("#define #{params[:tcp4_cepid]}  #{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}")
     80$cfgData[:VRID_TCP4_REP].each do |key, params|
     81        $tinetCfgH.add("#define #{params[:vrid_tcp4_repid]}     #{params[:vrid_tcp4_repid].val + $cfgData[:TCP_CRE_REP].size}")
     82end
     83$cfgData[:VRID_TCP4_CEP].each do |key, params|
     84        $tinetCfgH.add("#define #{params[:vrid_tcp4_cepid]}     #{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}")
    8585end
    8686$cfgData[:VRID_TCP6_REP].each do |key, params|
    87         $tinetCfgH.add("#define #{params[:tcp6_repid]}  #{params[:tcp6_repid].val + $cfgData[:TCP6_CRE_REP].size}")
     87        $tinetCfgH.add("#define #{params[:vrid_tcp6_repid]}     #{params[:vrid_tcp6_repid].val + $cfgData[:TCP6_CRE_REP].size}")
    8888end
    8989$cfgData[:VRID_TCP6_CEP].each do |key, params|
    90         $tinetCfgH.add("#define #{params[:tcp6_cepid]}  #{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}")
    91 end
    92 $cfgData[:VRID_UDP_CEP].each do |key, params|
    93         $tinetCfgH.add("#define #{params[:udp4_cepid]}  #{params[:udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size}")
     90        $tinetCfgH.add("#define #{params[:vrid_tcp6_cepid]}     #{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}")
     91end
     92$cfgData[:VRID_UDP4_CEP].each do |key, params|
     93        $tinetCfgH.add("#define #{params[:vrid_udp4_cepid]}     #{params[:vrid_udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size}")
    9494end
    9595$cfgData[:VRID_UDP6_CEP].each do |key, params|
    96         $tinetCfgH.add("#define #{params[:udp6_cepid]}  #{params[:udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size}")
     96        $tinetCfgH.add("#define #{params[:vrid_udp6_cepid]}     #{params[:vrid_udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size}")
    9797end
    9898$tinetCfgH.add()
     
    144144
    145145#       // TCP (IPv4) 受付口
    146 if $cfgData[:TCP_CRE_REP].size > 0 || $cfgData[:VRID_TCP_REP].size > 0
    147         $tinetCfgC.add()
    148         $tinetCfgC.add("#define TNUM_TCP_REPID  #{$cfgData[:TCP_CRE_REP].size + $cfgData[:VRID_TCP_REP].size}")
     146if $cfgData[:TCP_CRE_REP].size > 0 || $cfgData[:VRID_TCP4_REP].size > 0
     147        $tinetCfgC.add()
     148        $tinetCfgC.add("#define TNUM_TCP_REPID  #{$cfgData[:TCP_CRE_REP].size + $cfgData[:VRID_TCP4_REP].size}")
    149149        $tinetCfgC.add()
    150150        $tinetCfgC.add("const ID tmax_tcp_repid = (TMIN_TCP_REPID + TNUM_TCP_REPID - 1);")
     
    161161                        $tinetCfgC.add("\t\t},")
    162162        end
    163         $cfgData[:VRID_TCP_REP].each do |key, params|
     163        $cfgData[:VRID_TCP4_REP].each do |key, params|
    164164                $tinetCfgC.add("\t{")
    165165                        $tinetCfgC.add("\t\t0,")
     
    167167        $tinetCfgC.add("#if defined(TCP_CFG_EXTENTIONS)")
    168168                        $tinetCfgC.add("\t\tTCP_REP_FLG_DYNAMIC,")
    169                         $tinetCfgC.add("\t\tSEM_TCP_REP_LOCK#{params[:tcp4_repid].val + $cfgData[:TCP_CRE_REP].size},")
     169                        $tinetCfgC.add("\t\tSEM_TCP_REP_LOCK#{params[:vrid_tcp4_repid].val + $cfgData[:TCP_CRE_REP].size},")
    170170        $tinetCfgC.add("#endif")
    171171                        $tinetCfgC.add("\t\t},")
     
    198198        $tinetCfgC.add("#if defined(TCP_CFG_EXTENTIONS)")
    199199                        $tinetCfgC.add("\t\tTCP_REP_FLG_DYNAMIC,")
    200                         $tinetCfgC.add("\t\tSEM_TCP_REP_LOCK#{params[:tcp6_repid].val + $cfgData[:TCP6_CRE_REP].size},")
     200                        $tinetCfgC.add("\t\tSEM_TCP_REP_LOCK#{params[:vrid_tcp6_repid].val + $cfgData[:TCP6_CRE_REP].size},")
    201201        $tinetCfgC.add("#endif")
    202202                        $tinetCfgC.add("\t\t},")
     
    206206
    207207#       // TCP (IPv4) 通信端点
    208 if $cfgData[:TCP_CRE_CEP].size > 0 || $cfgData[:VRID_TCP_CEP].size > 0
    209         $tinetCfgC.add()
    210         $tinetCfgC.add("#define TNUM_TCP_CEPID  #{$cfgData[:TCP_CRE_CEP].size + $cfgData[:VRID_TCP_CEP].size}")
     208if $cfgData[:TCP_CRE_CEP].size > 0 || $cfgData[:VRID_TCP4_CEP].size > 0
     209        $tinetCfgC.add()
     210        $tinetCfgC.add("#define TNUM_TCP_CEPID  #{$cfgData[:TCP_CRE_CEP].size + $cfgData[:VRID_TCP4_CEP].size}")
    211211        $tinetCfgC.add()
    212212        $tinetCfgC.add("const ID tmax_tcp_cepid = (TMIN_TCP_CEPID + TNUM_TCP_CEPID - 1);")
     
    228228                        $tinetCfgC.add("\t\t},")
    229229        end
    230         $cfgData[:VRID_TCP_CEP].each do |key, params|
     230        $cfgData[:VRID_TCP4_CEP].each do |key, params|
    231231                $tinetCfgC.add("\t{")
    232232                        $tinetCfgC.add("\t\t0,")
     
    237237                        $tinetCfgC.add("\t\t(t_tcp_callback)(FP)NULL,")
    238238                        $tinetCfgC.add("\t\tTCP_CEP_FLG_DYNAMIC,")
    239                         $tinetCfgC.add("\t\tSEM_TCP_CEP_LOCK#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
    240                         $tinetCfgC.add("\t\tFLG_TCP_CEP_EST#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
    241                         $tinetCfgC.add("\t\tFLG_TCP_CEP_SND#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
    242                         $tinetCfgC.add("\t\tFLG_TCP_CEP_RCV#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
     239                        $tinetCfgC.add("\t\tSEM_TCP_CEP_LOCK#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
     240                        $tinetCfgC.add("\t\tFLG_TCP_CEP_EST#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
     241                        $tinetCfgC.add("\t\tFLG_TCP_CEP_SND#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
     242                        $tinetCfgC.add("\t\tFLG_TCP_CEP_RCV#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size},")
    243243                        $tinetCfgC.add("\t\t},")
    244244        end
     
    278278                        $tinetCfgC.add("\t\t(t_tcp_callback)(FP)NULL,")
    279279                        $tinetCfgC.add("\t\tTCP_CEP_FLG_DYNAMIC,")
    280                         $tinetCfgC.add("\t\tSEM_TCP_CEP_LOCK#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
    281                         $tinetCfgC.add("\t\tFLG_TCP_CEP_EST#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
    282                         $tinetCfgC.add("\t\tFLG_TCP_CEP_SND#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
    283                         $tinetCfgC.add("\t\tFLG_TCP_CEP_RCV#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
     280                        $tinetCfgC.add("\t\tSEM_TCP_CEP_LOCK#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
     281                        $tinetCfgC.add("\t\tFLG_TCP_CEP_EST#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
     282                        $tinetCfgC.add("\t\tFLG_TCP_CEP_SND#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
     283                        $tinetCfgC.add("\t\tFLG_TCP_CEP_RCV#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size},")
    284284                        $tinetCfgC.add("\t\t},")
    285285        end
     
    288288
    289289#       // UDP (IPv4) 通信端点
    290 if $cfgData[:UDP_CRE_CEP].size > 0 || $cfgData[:VRID_UDP_CEP].size > 0
    291         $tinetCfgC.add()
    292         $tinetCfgC.add("#define TNUM_UDP_CEPID  #{$cfgData[:UDP_CRE_CEP].size + $cfgData[:VRID_UDP_CEP].size}")
     290if $cfgData[:UDP_CRE_CEP].size > 0 || $cfgData[:VRID_UDP4_CEP].size > 0
     291        $tinetCfgC.add()
     292        $tinetCfgC.add("#define TNUM_UDP_CEPID  #{$cfgData[:UDP_CRE_CEP].size + $cfgData[:VRID_UDP4_CEP].size}")
    293293        $tinetCfgC.add()
    294294        $tinetCfgC.add("const ID tmax_udp_cepid = (TMIN_UDP_CEPID + TNUM_UDP_CEPID - 1);")
     
    307307                        $tinetCfgC.add("\t\t},")
    308308        end
    309         $cfgData[:VRID_UDP_CEP].each do |key, params|
     309        $cfgData[:VRID_UDP4_CEP].each do |key, params|
    310310                $tinetCfgC.add("\t{")
    311311                        $tinetCfgC.add("\t\t0,")
     
    313313                        $tinetCfgC.add("\t\t(t_udp_callback)(FP)NULL,")
    314314                        $tinetCfgC.add("\t\tUDP_CEP_FLG_DYNAMIC,")
    315                         $tinetCfgC.add("\t\tSEM_UDP_CEP_LOCK#{params[:udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size},")
    316                         $tinetCfgC.add("\t\tTA_NULL,")
    317                         $tinetCfgC.add("\t\tTA_NULL,")
    318                         $tinetCfgC.add("\t\tDTQ_UDP_RCVQ#{params[:udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size},")
     315                        $tinetCfgC.add("\t\tSEM_UDP_CEP_LOCK#{params[:vrid_udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size},")
     316                        $tinetCfgC.add("\t\tTA_NULL,")
     317                        $tinetCfgC.add("\t\tTA_NULL,")
     318                        $tinetCfgC.add("\t\tDTQ_UDP_RCVQ#{params[:vrid_udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size},")
    319319                        $tinetCfgC.add("\t\t},")
    320320        end
     
    348348                        $tinetCfgC.add("\t\t(t_udp_callback)(FP)NULL,")
    349349                        $tinetCfgC.add("\t\tUDP_CEP_FLG_DYNAMIC,")
    350                         $tinetCfgC.add("\t\tSEM_UDP_CEP_LOCK#{params[:udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size},")
    351                         $tinetCfgC.add("\t\tTA_NULL,")
    352                         $tinetCfgC.add("\t\tTA_NULL,")
    353                         $tinetCfgC.add("\t\tDTQ_UDP_RCVQ#{params[:udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size},")
     350                        $tinetCfgC.add("\t\tSEM_UDP_CEP_LOCK#{params[:vrid_udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size},")
     351                        $tinetCfgC.add("\t\tTA_NULL,")
     352                        $tinetCfgC.add("\t\tTA_NULL,")
     353                        $tinetCfgC.add("\t\tDTQ_UDP_RCVQ#{params[:vrid_udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size},")
    354354                        $tinetCfgC.add("\t\t},")
    355355        end
     
    362362
    363363#       // TCP (IPv4) 受付口
    364 if $cfgData[:TCP_CRE_REP].size > 0 || $cfgData[:VRID_TCP_REP].size > 0
     364if $cfgData[:TCP_CRE_REP].size > 0 || $cfgData[:VRID_TCP4_REP].size > 0
    365365        $cfgData[:TCP_CRE_REP].each do |key, params|
    366366                cfgInfo = { APINAME: "CRE_SEM" }
     
    371371                $cfgFileInfo.push(cfgInfo)
    372372        end
    373         $cfgData[:VRID_TCP_REP].each do |key, params|
    374                 cfgInfo = { APINAME: "CRE_SEM" }
    375                 cfgInfo["semid"] = "SEM_TCP_REP_LOCK#{params[:tcp4_repid].val + $cfgData[:TCP_CRE_REP].size}"
     373        $cfgData[:VRID_TCP4_REP].each do |key, params|
     374                cfgInfo = { APINAME: "CRE_SEM" }
     375                cfgInfo["semid"] = "SEM_TCP_REP_LOCK#{params[:vrid_tcp4_repid].val + $cfgData[:TCP_CRE_REP].size}"
    376376                cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
    377377                cfgInfo["isemcnt"] = 1
     
    393393        $cfgData[:VRID_TCP6_REP].each do |key, params|
    394394                cfgInfo = { APINAME: "CRE_SEM" }
    395                 cfgInfo["semid"] = "SEM_TCP_REP_LOCK#{params[:tcp6_repid].val + $cfgData[:TCP6_CRE_REP].size}"
     395                cfgInfo["semid"] = "SEM_TCP_REP_LOCK#{params[:vrid_tcp6_repid].val + $cfgData[:TCP6_CRE_REP].size}"
    396396                cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
    397397                cfgInfo["isemcnt"] = 1
     
    402402
    403403#       // TCP (IPv4) 通信端点
    404 if $cfgData[:TCP_CRE_CEP].size > 0 || $cfgData[:VRID_TCP_CEP].size > 0
     404if $cfgData[:TCP_CRE_CEP].size > 0 || $cfgData[:VRID_TCP4_CEP].size > 0
    405405        $cfgData[:TCP_CRE_CEP].each do |key, params|
    406406                cfgInfo = { APINAME: "CRE_SEM" }
     
    429429                $cfgFileInfo.push(cfgInfo)
    430430        end
    431         $cfgData[:VRID_TCP_CEP].each do |key, params|
    432                 cfgInfo = { APINAME: "CRE_SEM" }
    433                 cfgInfo["semid"] = "SEM_TCP_CEP_LOCK#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
    434                 cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
    435                 cfgInfo["isemcnt"] = 1
    436                 cfgInfo["maxsem"] = 1
    437                 $cfgFileInfo.push(cfgInfo)
    438 
    439                 cfgInfo = { APINAME: "CRE_FLG" }
    440                 cfgInfo["flgid"] = "FLG_TCP_CEP_EST#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
    441                 cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
    442                 cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
    443                 $cfgFileInfo.push(cfgInfo)
    444 
    445                 cfgInfo = { APINAME: "CRE_FLG" }
    446                 cfgInfo["flgid"] = "FLG_TCP_CEP_SND#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
    447                 cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
    448                 cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
    449                 $cfgFileInfo.push(cfgInfo)
    450 
    451                 cfgInfo = { APINAME: "CRE_FLG" }
    452                 cfgInfo["flgid"] = "FLG_TCP_CEP_RCV#{params[:tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
     431        $cfgData[:VRID_TCP4_CEP].each do |key, params|
     432                cfgInfo = { APINAME: "CRE_SEM" }
     433                cfgInfo["semid"] = "SEM_TCP_CEP_LOCK#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
     434                cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
     435                cfgInfo["isemcnt"] = 1
     436                cfgInfo["maxsem"] = 1
     437                $cfgFileInfo.push(cfgInfo)
     438
     439                cfgInfo = { APINAME: "CRE_FLG" }
     440                cfgInfo["flgid"] = "FLG_TCP_CEP_EST#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
     441                cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
     442                cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
     443                $cfgFileInfo.push(cfgInfo)
     444
     445                cfgInfo = { APINAME: "CRE_FLG" }
     446                cfgInfo["flgid"] = "FLG_TCP_CEP_SND#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
     447                cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
     448                cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
     449                $cfgFileInfo.push(cfgInfo)
     450
     451                cfgInfo = { APINAME: "CRE_FLG" }
     452                cfgInfo["flgid"] = "FLG_TCP_CEP_RCV#{params[:vrid_tcp4_cepid].val + $cfgData[:TCP_CRE_CEP].size}"
    453453                cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
    454454                cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
     
    487487        $cfgData[:VRID_TCP6_CEP].each do |key, params|
    488488                cfgInfo = { APINAME: "CRE_SEM" }
    489                 cfgInfo["semid"] = "SEM_TCP_CEP_LOCK#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
    490                 cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
    491                 cfgInfo["isemcnt"] = 1
    492                 cfgInfo["maxsem"] = 1
    493                 $cfgFileInfo.push(cfgInfo)
    494 
    495                 cfgInfo = { APINAME: "CRE_FLG" }
    496                 cfgInfo["flgid"] = "FLG_TCP_CEP_EST#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
    497                 cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
    498                 cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
    499                 $cfgFileInfo.push(cfgInfo)
    500 
    501                 cfgInfo = { APINAME: "CRE_FLG" }
    502                 cfgInfo["flgid"] = "FLG_TCP_CEP_SND#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
    503                 cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
    504                 cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
    505                 $cfgFileInfo.push(cfgInfo)
    506 
    507                 cfgInfo = { APINAME: "CRE_FLG" }
    508                 cfgInfo["flgid"] = "FLG_TCP_CEP_RCV#{params[:tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
     489                cfgInfo["semid"] = "SEM_TCP_CEP_LOCK#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
     490                cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
     491                cfgInfo["isemcnt"] = 1
     492                cfgInfo["maxsem"] = 1
     493                $cfgFileInfo.push(cfgInfo)
     494
     495                cfgInfo = { APINAME: "CRE_FLG" }
     496                cfgInfo["flgid"] = "FLG_TCP_CEP_EST#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
     497                cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
     498                cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
     499                $cfgFileInfo.push(cfgInfo)
     500
     501                cfgInfo = { APINAME: "CRE_FLG" }
     502                cfgInfo["flgid"] = "FLG_TCP_CEP_SND#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
     503                cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
     504                cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
     505                $cfgFileInfo.push(cfgInfo)
     506
     507                cfgInfo = { APINAME: "CRE_FLG" }
     508                cfgInfo["flgid"] = "FLG_TCP_CEP_RCV#{params[:vrid_tcp6_cepid].val + $cfgData[:TCP6_CRE_CEP].size}"
    509509                cfgInfo["flgatr"] = NumStr.new($TA_TFIFO | $TA_WSGL, "TA_TFIFO|TA_WSGL")
    510510                cfgInfo["iflgptn"] = NumStr.new($TCP_CEP_EVT_CLOSED, "TCP_CEP_EVT_CLOSED")
     
    514514
    515515#       // UDP (IPv4) 通信端点
    516 if $cfgData[:UDP_CRE_CEP].size > 0 || $cfgData[:VRID_UDP_CEP].size > 0
     516if $cfgData[:UDP_CRE_CEP].size > 0 || $cfgData[:VRID_UDP4_CEP].size > 0
    517517        $cfgData[:UDP_CRE_CEP].each do |key, params|
    518518                cfgInfo = { APINAME: "CRE_SEM" }
     
    530530                $cfgFileInfo.push(cfgInfo)
    531531        end
    532         $cfgData[:VRID_UDP_CEP].each do |key, params|
    533                 cfgInfo = { APINAME: "CRE_SEM" }
    534                 cfgInfo["semid"] = "SEM_UDP_CEP_LOCK#{params[:udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size}"
     532        $cfgData[:VRID_UDP4_CEP].each do |key, params|
     533                cfgInfo = { APINAME: "CRE_SEM" }
     534                cfgInfo["semid"] = "SEM_UDP_CEP_LOCK#{params[:vrid_udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size}"
    535535                cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
    536536                cfgInfo["isemcnt"] = 1
     
    539539
    540540                cfgInfo = { APINAME: "CRE_DTQ" }
    541                 cfgInfo["dtqid"] = "DTQ_UDP_RCVQ#{params[:udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size}"
     541                cfgInfo["dtqid"] = "DTQ_UDP_RCVQ#{params[:vrid_udp4_cepid].val + $cfgData[:UDP_CRE_CEP].size}"
    542542                cfgInfo["dtqatr"] = NumStr.new($TA_TFIFO, "TA_TFIFO")
    543543                cfgInfo["dtqcnt"] = NumStr.new($NUM_DTQ_UDP_RCVQ, "NUM_DTQ_UDP_RCVQ")
     
    566566        $cfgData[:VRID_UDP6_CEP].each do |key, params|
    567567                cfgInfo = { APINAME: "CRE_SEM" }
    568                 cfgInfo["semid"] = "SEM_UDP_CEP_LOCK#{params[:udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size}"
     568                cfgInfo["semid"] = "SEM_UDP_CEP_LOCK#{params[:vrid_udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size}"
    569569                cfgInfo["sematr"] = NumStr.new($TA_TPRI, "TA_TPRI")
    570570                cfgInfo["isemcnt"] = 1
     
    573573
    574574                cfgInfo = { APINAME: "CRE_DTQ" }
    575                 cfgInfo["dtqid"] = "DTQ_UDP_RCVQ#{params[:udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size}"
     575                cfgInfo["dtqid"] = "DTQ_UDP_RCVQ#{params[:vrid_udp6_cepid].val + $cfgData[:UDP6_CRE_CEP].size}"
    576576                cfgInfo["dtqatr"] = NumStr.new($TA_TFIFO, "TA_TFIFO")
    577577                cfgInfo["dtqcnt"] = NumStr.new($NUM_DTQ_UDP_RCVQ, "NUM_DTQ_UDP_RCVQ")
  • EcnlProtoTool/trunk/mruby-1.2.0/Makefile

    r270 r279  
    33# codebase.
    44
    5 RAKE = ruby ./minirake
     5RAKE = ruby -Eutf-8 ./minirake
    66
    77all :
  • EcnlProtoTool/trunk/mruby-1.2.0/build_config.rb

    r270 r279  
    152152  conf.cc.defines << %w(MRB_GC_STRESS)
    153153  #conf.cc.defines << %w(DISABLE_STDIO)
     154  conf.cc.defines = %w(MRB_ENABLE_DEBUG_HOOK)
    154155
    155156  conf.build_mrbtest_lib_only
    156157
     158  conf.gem "#{root}/mrbgems/mruby-compiler"
    157159  conf.gem "#{root}/mrbgems/mruby-numeric-ext"
    158160  conf.gem "#{root}/mrbgems/mruby-string-ext"
     161  conf.gem '../mrbgems/mruby-onig-regexp'  do |g|
     162    g.cc.include_paths  << "../onigmo-5.15.0/src"
     163  end
    159164  conf.gem "#{root}/../mrbgems/mruby-blockly"
    160   conf.gem "#{root}/../mrbgems/gr_peach" do |g|
    161     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/api"
    162     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/hal"
    163     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/cmsis"
    164     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/cmsis/TARGET_RENESAS/TARGET_RZ_A1H"
    165     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/cmsis/TARGET_RENESAS/TARGET_RZ_A1H/inc"
    166     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/cmsis/TARGET_RENESAS/TARGET_RZ_A1H/inc/iodefines"
    167     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H"
    168     g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/TARGET_MBED_MBRZA1H"
     165  conf.gem "#{root}/../mrbgems/mruby-io"
     166  conf.gem "#{root}/../mrbgems/mruby-dir" do |g|
     167    g.cc.defines = %w(__NEWLIB__=2)
     168    g.cc.include_paths << "#{g.dir}/../../musl-1.1.12/include"
     169  end
     170  conf.gem "#{root}/../mrbgems/mruby-pack"
     171  conf.gem "#{root}/../mrbgems/mruby-socket" do |g|
     172    g.cc.include_paths << "#{g.dir}/../../musl-1.1.12/include"
    169173  end
    170174  conf.gem "#{root}/../mrbgems/mruby-arduino" do |g|
     
    177181    g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H"
    178182    g.cc.include_paths << "#{g.dir}/../../asp3_dcre/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/TARGET_MBED_MBRZA1H"
    179     g.cc.include_paths << "#{g.dir}/../../mrbgems/gr_peach/src"
     183    g.cc.include_paths << "#{g.dir}/../../sample/gr_peach"
     184    g.cc.include_paths << "#{g.dir}/../../prototool/src"
    180185  end
    181186  conf.gem "#{root}/../mrbgems/mruby-ecnl"
  • EcnlProtoTool/trunk/mruby-1.2.0/include/mruby.h

    r270 r279  
    859859 *      Pointer to the newly created mrb_state.
    860860 */
    861 MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud);
     861MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud, mrb_bool disable_gems);
    862862
    863863/**
  • EcnlProtoTool/trunk/mruby-1.2.0/src/state.c

    r270 r279  
    9797mrb_open(void)
    9898{
    99   mrb_state *mrb = mrb_open_allocf(mrb_default_allocf, NULL);
     99  mrb_state *mrb = mrb_open_allocf(mrb_default_allocf, NULL, FALSE);
    100100
    101101  return mrb;
     
    103103
    104104MRB_API mrb_state*
    105 mrb_open_allocf(mrb_allocf f, void *ud)
     105mrb_open_allocf(mrb_allocf f, void *ud, mrb_bool disable_gems)
    106106{
    107107  mrb_state *mrb = mrb_open_core(f, ud);
     
    111111  }
    112112
    113 #ifndef DISABLE_GEMS
    114   mrb_init_mrbgems(mrb);
    115   mrb_gc_arena_restore(mrb, 0);
    116 #endif
     113  if (!disable_gems) {
     114    mrb_init_mrbgems(mrb);
     115    mrb_gc_arena_restore(mrb, 0);
     116  }
    117117  return mrb;
    118118}
  • EcnlProtoTool/trunk/ntshell/fatfs/ccsbcs.c

    r278 r279  
    266266
    267267
    268 #if !_TBLDEF || !_USE_LFN
     268#if _CODE_PAGE != 65001 && (!_TBLDEF || !_USE_LFN)
    269269#error This file is not needed at current configuration. Remove from the project.
    270270#endif
     
    272272
    273273
    274 
     274#if _CODE_PAGE != 65001
    275275WCHAR ff_convert (      /* Converted character, Returns zero on error */
    276276        WCHAR   chr,    /* Character code to be converted */
     
    298298        return c;
    299299}
    300 
     300#endif
    301301
    302302
  • EcnlProtoTool/trunk/ntshell/fatfs/diskio.c

    r278 r279  
    1515#define MAX_DRIVES      1       /* Max number of physical drives to be used */
    1616
    17 unsigned char RamDisk[SZ_RAMDISK * 1024];
     17#define RamDisk _binary____webserver_httpd_fs_bin_start
     18extern unsigned char RamDisk[];
    1819
    1920typedef struct {
  • EcnlProtoTool/trunk/ntshell/fatfs/ff.c

    r278 r279  
    314314#endif
    315315#define _DF1S   0
     316
     317#elif _CODE_PAGE == 65001 /* UTF-8 */
     318#if _LFN_UNICODE
     319#error Cannot use LFN_UNICODE feature without valid code page.
     320#endif
     321#define _DF1S   0
     322
     323void Utf16_to_Utf8(unsigned char *ret_code, int *ret_size, UINT chr)
     324{
     325        ret_code[0] = ret_code[1] = ret_code[2] = ret_code[3] = 0;
     326
     327        if (chr <= 0x7f) {  // ASCII互換
     328                ret_code[0] = chr;
     329                *ret_size = 1;
     330                return;
     331        }
     332
     333        if (chr <= 0x7ff) {
     334                ret_code[1] = (0x80 | (chr & 0x3f));
     335                ret_code[0] = (0xc0 | (chr >> 6));
     336                *ret_size = 2;
     337                return;
     338        }
     339
     340        if (chr <= 0xffff) {
     341                ret_code[2] = (0x80 | (chr & 0x3f));
     342                ret_code[1] = (0x80 | ((chr >> 6) & 0x3f));
     343                ret_code[0] = (0xe0 | ((chr >> 12) & 0x0f));
     344                *ret_size = 3;
     345                return;
     346        }
     347
     348        if (chr <= 0x1fffff) {
     349                ret_code[3] = (0x80 | (chr & 0x3f));
     350                ret_code[2] = (0x80 | ((chr >> 6) & 0x3f));
     351                ret_code[1] = (0x80 | ((chr >> 12) & 0x3f));
     352                ret_code[0] = (0xf0 | ((chr >> 18) & 0x07));
     353                *ret_size = 4;
     354                return;
     355        }
     356
     357        if (chr <= 0x3ffffff) {
     358                ret_code[4] = (0x80 | (chr & 0x3f));
     359                ret_code[3] = (0x80 | ((chr >> 6) & 0x3f));
     360                ret_code[2] = (0x80 | ((chr >> 12) & 0x3f));
     361                ret_code[1] = (0x80 | ((chr >> 18) & 0x3f));
     362                ret_code[0] = (0xf8 | ((chr >> 24) & 0x03));
     363                *ret_size = 5;
     364                return;
     365        }
     366
     367        ret_code[5] = (0x80 | (chr & 0x3f));
     368        ret_code[4] = (0x80 | ((chr >> 6) & 0x3f));
     369        ret_code[3] = (0x80 | ((chr >> 12) & 0x3f));
     370        ret_code[2] = (0x80 | ((chr >> 18) & 0x3f));
     371        ret_code[1] = (0x80 | ((chr >> 24) & 0x3f));
     372        ret_code[0] = (0xfc | ((chr >> 30) & 0x01));
     373        *ret_size = 6;
     374        return;
     375}
     376
     377//2バイトのUTF-16コードが得られる
     378WCHAR Utf8_to_Utf16(const char *src, int *code_size)
     379{
     380        int i;
     381        unsigned int uc = 0;
     382        unsigned char len = 0;
     383
     384        len = 0;
     385        if ((src[0] & 0x80) == 0) { uc = src[0] & 0x7F; len = 0; }
     386        else if ((src[0] & 0xE0) == 0xC0) { uc = src[0] & 0x1F; len = 1; }
     387        else if ((src[0] & 0xF0) == 0xE0) { uc = src[0] & 0x0F; len = 2; }
     388        else if ((src[0] & 0xF8) == 0xF0) { uc = src[0] & 0x07; len = 3; }
     389        else if ((src[0] & 0xFC) == 0xF8) { uc = src[0] & 0x03; len = 4; }
     390        else if ((src[0] & 0xFE) == 0xFC) { uc = src[0] & 0x01; len = 5; }
     391
     392        i = 1;
     393        while ((i <= len) && ((src[i] & 0xC0) == 0x80)) {
     394                uc = (uc << 6) | (src[i] & 0x3F);
     395                i++;
     396        }
     397
     398        //消費文字数設定
     399        *code_size = i;
     400
     401        //現状、2バイト限定
     402        return uc;
     403}
    316404
    317405#else
     
    17121800        WCHAR w, *lfn;
    17131801#endif
     1802#if _CODE_PAGE == 65001
     1803        unsigned char utf8_code[6];
     1804        int code_size;
     1805#endif
    17141806
    17151807        p = fno->fname;
     
    17481840                        while ((w = *lfn++) != 0) {             /* Get an LFN character */
    17491841#if !_LFN_UNICODE
     1842#if _CODE_PAGE == 65001
     1843                                Utf16_to_Utf8(utf8_code, &code_size, w);
     1844                                for (int j = 0; j < code_size - 1; j++) {
     1845                                        p[i++] = utf8_code[j];
     1846                                }
     1847                                w = utf8_code[code_size - 1];
     1848#else
    17501849                                w = ff_convert(w, 0);           /* Unicode -> OEM */
    17511850                                if (!w) { i = 0; break; }       /* No LFN if it could not be converted */
     
    17531852                                        p[i++] = (TCHAR)(w >> 8);
    17541853#endif
     1854#endif
    17551855                                if (i >= fno->lfsize - 1) { i = 0; break; }     /* No LFN if buffer overflow */
    17561856                                p[i++] = (TCHAR)w;
     
    17781878
    17791879#if !_LFN_UNICODE
     1880#if _CODE_PAGE == 65001
     1881        int code_size;
     1882        chr = Utf8_to_Utf16(*ptr, &code_size);
     1883        (*ptr) += code_size;
     1884#else
    17801885        chr = (BYTE)*(*ptr)++;                                  /* Get a byte */
    17811886        if (IsLower(chr)) chr -= 0x20;                  /* To upper ASCII char */
    17821887        if (IsDBCS1(chr) && IsDBCS2(**ptr))             /* Get DBC 2nd byte if needed */
    17831888                chr = chr << 8 | (BYTE)*(*ptr)++;
     1889#endif
    17841890#ifdef _EXCVT
    17851891        if (chr >= 0x80) chr = ExCvt[chr - 0x80];       /* To upper SBCS extended char */
     
    18511957        UINT i, ni, si, di;
    18521958        const TCHAR *p;
    1853 
     1959#if _CODE_PAGE == 65001
     1960        char utf8_code[6];
     1961        int code_size;
     1962#endif
    18541963        /* Create LFN in Unicode */
    18551964        for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
     
    18621971                        return FR_INVALID_NAME;
    18631972#if !_LFN_UNICODE
     1973#if _CODE_PAGE == 65001
     1974                w = Utf8_to_Utf16(&p[si - 1], &code_size);
     1975                si += code_size - 1;
     1976#else
    18641977                w &= 0xFF;
    18651978                if (IsDBCS1(w)) {                               /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
     
    18711984                w = ff_convert(w, 1);                   /* Convert ANSI/OEM to Unicode */
    18721985                if (!w) return FR_INVALID_NAME; /* Reject invalid code */
     1986#endif
    18731987#endif
    18741988                if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal characters for LFN */
     
    19252039                        if (w) w = ExCvt[w - 0x80];     /* Convert extended character to upper (SBCS) */
    19262040#else
     2041#if _CODE_PAGE == 65001
     2042                        Utf16_to_Utf8(utf8_code, &code_size, ff_wtoupper(w));
     2043#else
    19272044                        w = ff_convert(ff_wtoupper(w), 0);      /* Upper converted Unicode -> OEM code */
    19282045#endif
     2046#endif
    19292047                        cf |= NS_LFN;                           /* Force create LFN entry */
    19302048                }
    1931 
     2049#if _CODE_PAGE == 65001
     2050                else
     2051                        code_size = 1;
     2052#endif
     2053
     2054#if _CODE_PAGE == 65001
     2055                if (code_size > 1) {            /* Is this DBC? (always false at SBCS cfg) */
     2056                        if (i >= ni - 1) {
     2057                                cf |= NS_LOSS | NS_LFN; i = ni; continue;
     2058                        }
     2059                        for (int j = 0; j < code_size; j++) {
     2060                                dp->fn[i++] = (BYTE)utf8_code[j];
     2061                        }
     2062                }
     2063                else {                                          /* SBC */
     2064                        if (!w || chk_chr("+,;=[]", w)) {       /* Replace illegal characters for SFN */
     2065                                w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
     2066                        }
     2067                        else {
     2068                                if (IsUpper(w)) {               /* ASCII large capital */
     2069                                        b |= 2;
     2070                                }
     2071                                else {
     2072                                        if (IsLower(w)) {       /* ASCII small capital */
     2073                                                b |= 1; w -= 0x20;
     2074                                        }
     2075                                }
     2076                        }
     2077                        dp->fn[i++] = (BYTE)w;
     2078                }
     2079#else
    19322080                if (_DF1S && w >= 0x100) {              /* Is this DBC? (always false at SBCS cfg) */
    19332081                        if (i >= ni - 1) {
     
    19492097                }
    19502098                dp->fn[i++] = (BYTE)w;
     2099#endif
    19512100        }
    19522101
     
    31783327}
    31793328
    3180 
     3329FRESULT f_seek(FIL* fp, DWORD ofs, BYTE mode)
     3330{
     3331  switch (mode) {
     3332  case F_SEEK_SET:
     3333    return f_lseek((fp), ofs);
     3334  case F_SEEK_CUR:
     3335    return f_lseek((fp), (fp)->fptr + ofs);
     3336  case F_SEEK_END:
     3337    return f_lseek((fp), (fp)->fsize - ofs);
     3338  default:
     3339    return FR_INVALID_PARAMETER;
     3340  }
     3341}
    31813342
    31823343#if _FS_MINIMIZE <= 1
     
    37193880        DWORD dw;
    37203881        DEFINE_NAMEBUF;
     3882        TCHAR *temp_new_path = 0;
    37213883
    37223884
     
    37423904                                else
    37433905                                        res = FR_INVALID_DRIVE;
    3744                                 if (res == FR_OK) res = FR_EXIST;               /* The new object name is already existing */
     3906                                if (res == FR_OK) {
     3907                                        res = FR_EXIST;         /* The new object name is already existing */
     3908                                        dir = djn.dir;
     3909                                        if (dir[DIR_Attr] & AM_DIR) {   /* The new object is a directory */
     3910                                                temp_new_path = (TCHAR *)ff_memalloc((_MAX_LFN + 1) * sizeof(WCHAR));
     3911                                                snprintf(temp_new_path, _MAX_LFN, "%s/%s", path_new, basename((char *)path_old));
     3912                                                res = follow_path(&djn, temp_new_path);
     3913                                        }
     3914                                }
    37453915                                if (res == FR_NO_FILE) {                                /* It is a valid path and no name collision */
    37463916                                        res = dir_register(&djn);                       /* Register the new entry */
     
    37773947        }
    37783948
     3949        if (temp_new_path != 0) ff_memfree(temp_new_path);
     3950
    37793951        LEAVE_FF(djo.fs, res);
    37803952}
     
    39124084        WCHAR w;
    39134085        DWORD tm;
    3914 
     4086#if _CODE_PAGE == 65001
     4087        int code_size;
     4088        char utf8_code[6];
     4089#endif
    39154090
    39164091        /* Get logical drive number */
     
    39254100                i = j = 0;
    39264101                do {
     4102#if _CODE_PAGE == 65001
     4103                        w = ff_wtoupper(Utf8_to_Utf16(&label[i], &code_size));
     4104                        i += code_size;
     4105                        Utf16_to_Utf8(utf8_code, &code_size, w);
     4106                        if (!w || chk_chr("\"*+,.:;<=>\?[]|\x7F", w) || j >= sizeof(vn) - code_size) /* Reject invalid characters for volume label */
     4107                                LEAVE_FF(dj.fs, FR_INVALID_NAME);
     4108                        for (int k = 0; k < code_size; k++)
     4109                                vn[j++] = utf8_code[k];
     4110#else
    39274111#if _USE_LFN && _LFN_UNICODE
    39284112                        w = ff_convert(ff_wtoupper(label[i++]), 0);
     
    39464130                        if (w >= 0x100) vn[j++] = (BYTE)(w >> 8);
    39474131                        vn[j++] = (BYTE)w;
     4132#endif
    39484133                } while (i < sl);
    39494134                while (j < 11) vn[j++] = ' ';   /* Fill remaining name field */
  • EcnlProtoTool/trunk/ntshell/fatfs/ff.h

    r278 r279  
    143143/* Directory object structure (DIR) */
    144144
    145 typedef struct {
     145typedef struct __dirstream {
    146146        FATFS*  fs;                             /* Pointer to the owner file system object (**do not change order**) */
    147147        WORD    id;                             /* Owner file system mount ID (**do not change order**) */
     
    162162        const TCHAR*    pat;    /* Pointer to the name matching pattern */
    163163#endif
     164        struct dirent *dirent;
    164165} DIR;
    165166
     
    252253#define f_rewind(fp) f_lseek((fp), 0)
    253254#define f_rewinddir(dp) f_readdir((dp), 0)
    254 #define f_flush(fp)
     255#define f_flush(fp) (FR_OK)
    255256
    256257#define F_SEEK_SET 1
    257258#define F_SEEK_CUR 2
    258259#define F_SEEK_END 3
    259 static FRESULT f_seek(FIL* fp, DWORD ofs, BYTE mode)
    260 {
    261   switch (mode) {
    262   case F_SEEK_SET:
    263     return f_lseek((fp), ofs);
    264   case F_SEEK_CUR:
    265     return f_lseek((fp), (fp)->fptr + ofs);
    266   case F_SEEK_END:
    267     return f_lseek((fp), (fp)->fsize - ofs);
    268   default:
    269     return FR_INVALID_PARAMETER;
    270   }
    271 }
     260FRESULT f_seek(FIL* fp, DWORD ofs, BYTE mode);
    272261
    273262#ifndef EOF
  • EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c

    r278 r279  
    5656#include <kernel.h>
    5757#include <stdlib.h>
    58 #include <errno.h>
    59 #include <sys/types.h>
    6058#include <string.h>
    6159#include "t_stdlib.h"
     60#include "syssvc/serial.h"
    6261#include "syssvc/syslog.h"
    6362#include "kernel_cfg.h"
     
    6564#include "sdfs.h"
    6665#include "ff.h"
     66#include "tlsf.h"
     67#include "ntstdio.h"
     68
     69#define SIO_PORTID 1
     70extern ntstdio_t ntstdio;
     71
     72static tlsf_t sys_tlsf;
     73static pool_t sys_pool;
     74
     75uint32_t  __HeapBase;
     76uint32_t  __HeapLimit;
     77FATFS RomDisk;
    6778
    6879gpio_t ins;
     
    7081#define WP() false
    7182
    72 extern FATFS RomDisk;
    73 extern unsigned char RamDisk[SZ_RAMDISK * 1024];
    74 
     83typedef struct SD {
     84        DSTATUS dst;
     85        BYTE type;
     86        FATFS FatFs;
     87} SD;
     88SD Sd;
     89
     90void sys_init(void);
    7591bool_t romdisk_init();
     92bool_t SD_begin();
    7693
    7794int mruby_arduino_init()
    7895{
    7996        int result = -1;
     97
     98        sys_init();
    8099
    81100        /* SD_CD */
     
    85104        sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd");
    86105
    87         SD_begin();
    88 
    89         FIL fd;
    90         UINT rlen = 0;
    91         memset(&fd, 0, sizeof(fd));
    92         if (f_open(&fd, "1:/httpd-fs.bin", FA_OPEN_EXISTING | FA_READ) == FR_OK) {
    93                 f_read(&fd, RamDisk, SZ_RAMDISK * 1024, &rlen);
    94                 f_close(&fd);
    95                 if (romdisk_init())
    96                         result = 0;
    97         }
    98 
     106        result = SD_begin() ? 0 : -1;
    99107        if (result == 0) {
    100                 syslog(LOG_NOTICE, "ramdisk ok!");
     108                ntstdio_printf(&ntstdio, "SD card (1:) OK!\n");
    101109        }
    102110        else {
    103                 syslog(LOG_NOTICE, "ramdisk ng!");
    104         }
    105 
    106         arduino_init();
     111                ntstdio_printf(&ntstdio, "SD card (1:) NG!\n");
     112        }
     113        sta_cyc(SDFS_CYC);
     114
     115        if (romdisk_init())
     116                result = 0;
     117
     118        if (result == 0) {
     119                ntstdio_printf(&ntstdio, "ROM disk (0:) OK!\n");
     120        }
     121        else {
     122                ntstdio_printf(&ntstdio, "ROM disk (0:) NG!\n");
     123        }
     124
     125        /* uploadディレクトリを作成しておく */
     126        f_mkdir("1:/upload");
     127
     128        serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
    107129
    108130        return result;
     131}
     132
     133void sys_init(void)
     134{
     135        sys_tlsf = tlsf_create(&__HeapBase);
     136        if (sys_tlsf == NULL)
     137                return;
     138
     139        sys_pool = tlsf_add_pool(sys_tlsf, ((uint8_t *)&__HeapBase) + tlsf_size(), ((size_t)&__HeapLimit - (size_t)&__HeapBase) - tlsf_size());
     140}
     141
     142void sys_fini(void)
     143{
     144        tlsf_destroy(sys_tlsf);
     145}
     146
     147void *sys_malloc(size_t size)
     148{
     149        return tlsf_malloc(sys_tlsf, size);
     150}
     151
     152void *sys_calloc(size_t size, size_t count)
     153{
     154        void *result = tlsf_malloc(sys_tlsf, count * size);
     155        memset(result, 0, count * size);
     156        return result;
     157}
     158
     159void *sys_realloc(void *ptr, size_t size)
     160{
     161        return tlsf_realloc(sys_tlsf, ptr, size);
     162}
     163
     164void sys_free(void *ptr)
     165{
     166        tlsf_free(sys_tlsf, ptr);
    109167}
    110168
     
    122180
    123181        if ((res = f_mount(&RomDisk, "0:", 1)) != FR_OK) {
     182                return false;
     183        }
     184
     185        return true;
     186}
     187
     188bool_t SD_begin()
     189{
     190        DSTATUS dst;
     191        FRESULT res;
     192        BYTE pdrv = 1, type;
     193
     194        if (Sd.FatFs.fs_type != 0)
     195                return true;
     196
     197        if ((dst = disk_initialize(pdrv)) != RES_OK) {
     198                return false;
     199        }
     200
     201        if ((dst = disk_ioctl(pdrv, MMC_GET_TYPE, &type)) != RES_OK) {
     202                Sd.dst = dst;
     203                Sd.type = 0;
     204        }
     205        else {
     206                Sd.dst = RES_OK;
     207                Sd.type = type;
     208        }
     209
     210        if ((res = f_mount(&Sd.FatFs, "1:", 1)) != FR_OK) {
    124211                return false;
    125212        }
     
    207294}
    208295
    209 static int lock[TNUM_TSKID];
    210 static int stack_pos[TNUM_TSKID];
    211 
    212 void __malloc_lock(struct _reent *a)
    213 {
    214         ID tskid;
     296int ff_cre_syncobj(BYTE vol, _SYNC_t* sobj)
     297{
     298        return 1;
     299}
     300
     301int ff_req_grant(_SYNC_t sobj)
     302{
    215303        ER ret;
    216         int count, sp;
    217 
    218         ret = get_tid(&tskid);
    219         if (ret != E_OK) {
    220                 /*syslog(LOG_DEBUG, "get_tid %s", itron_strerror(ret));*/
    221                 Asm("bkpt #0");
    222         }
    223 
    224         Asm("mov %0, sp" : "=r"(sp) :);
    225         if ((stack_pos[tskid - 1] == 0) || (stack_pos[tskid - 1] < sp))
    226                 stack_pos[tskid - 1] = sp;
    227 
    228         count = ++lock[tskid - 1];
    229         if (count != 1)
    230                 return;
    231 
    232         ret = wai_sem(SEM_MALLOC);
    233         if (ret != E_OK) {
    234                 /*syslog(LOG_DEBUG, "wai_sem %s", itron_strerror(ret));*/
    235                 Asm("bkpt #0");
    236         }
    237 }
    238 
    239 void __malloc_unlock(struct _reent *a)
    240 {
    241         ID tskid;
    242         ER ret;
    243         int count;
    244 
    245         ret = get_tid(&tskid);
    246         if (ret != E_OK) {
    247                 /*syslog(LOG_DEBUG, "get_tid %s", itron_strerror(ret));*/
    248                 Asm("bkpt #0");
    249         }
    250 
    251         count = --lock[tskid - 1];
    252         if(count != 0)
    253                 return;
    254 
    255         ret = sig_sem(SEM_MALLOC);
    256         if (ret != E_OK) {
    257                 /*syslog(LOG_DEBUG, "sig_sem %s", itron_strerror(ret));*/
    258                 Asm("bkpt #0");
    259         }
    260 }
    261 
    262 // Provide implementation of _sbrk (low-level dynamic memory allocation
    263 // routine) for GCC_ARM which compares new heap pointer with MSP instead of
    264 // SP.  This make it compatible with RTX RTOS thread stacks.
    265 
    266 // Linker defined symbol used by _sbrk to indicate where heap should start.
    267 int __end__;
    268 uint32_t  __HeapLimit;
    269 
    270 // Turn off the errno macro and use actual global variable instead.
    271 #undef errno
    272 int errno;
    273 
    274 static unsigned char* heap = (unsigned char*)&__end__;
    275 
    276 // Dynamic memory allocation related syscall.
    277 caddr_t _sbrk(int incr) {
    278         unsigned char*        prev_heap = heap;
    279         unsigned char*        new_heap = heap + incr;
    280 
    281         if (new_heap >= (unsigned char*)&__HeapLimit) {     /* __HeapLimit is end of heap section */
    282                 errno = ENOMEM;
    283                 return (caddr_t)-1;
    284         }
    285 
    286         heap = new_heap;
    287         return (caddr_t) prev_heap;
    288 }
    289 
    290 void reset_heap()
    291 {
    292         // .data
    293         extern uint8_t __malloc_av_[0x408];
    294         extern uint8_t __malloc_sbrk_base[0x4];
    295         extern uint8_t __malloc_trim_threshold[0x4];
    296         // .bss
    297         extern uint8_t __malloc_top_pad[0x4];
    298         extern uint8_t __malloc_current_mallinfo[0x28];
    299         extern uint8_t __malloc_max_sbrked_mem[0x4];
    300         extern uint8_t __malloc_max_total_mem[0x4];
    301         //
    302         extern void *__etext;
    303         extern void *__data_start__;
    304 
    305         int offset = (int)&__etext - (int)&__data_start__;
    306 
    307         __malloc_lock(_REENT);
    308 
    309         memcpy(__malloc_av_, &__malloc_av_[offset], sizeof(__malloc_av_));
    310         memcpy(__malloc_sbrk_base, &__malloc_sbrk_base[offset], sizeof(__malloc_sbrk_base));
    311         memcpy(__malloc_trim_threshold, &__malloc_trim_threshold[offset], sizeof(__malloc_trim_threshold));
    312 
    313         memset(__malloc_top_pad, 0, sizeof(__malloc_top_pad));
    314         memset(__malloc_current_mallinfo, 0, sizeof(__malloc_current_mallinfo));
    315         memset(__malloc_max_sbrked_mem, 0, sizeof(__malloc_max_sbrked_mem));
    316         memset(__malloc_max_total_mem, 0, sizeof(__malloc_max_total_mem));
    317 
    318         heap = (unsigned char*)&__end__;
    319 
    320         __malloc_unlock(_REENT);
    321 }
    322 
    323 void exit(int return_code) {
    324         Asm("bkpt #0");
    325         reset_heap();
    326         ext_tsk();
    327         for(;;);
    328 }
     304        ret = wai_sem(SEM_FILESYSTEM);
     305        return ret == E_OK;
     306}
     307
     308void ff_rel_grant(_SYNC_t sobj)
     309{
     310        sig_sem(SEM_FILESYSTEM);
     311}
     312
     313int ff_del_syncobj(_SYNC_t sobj)
     314{
     315        return 1;
     316}
     317
     318void* ff_memalloc (UINT msize)
     319{
     320        return sys_malloc(msize);
     321}
     322
     323void ff_memfree (void* mblock)
     324{
     325        sys_free(mblock);
     326}
  • EcnlProtoTool/trunk/ntshell/fatfs/ffarch.cfg

    r278 r279  
    5151 */
    5252
    53 #include "mruby_arduino.h"
     53#include "ffarch.h"
    5454
    55 CRE_CYC(SDFS_CYC, { TA_NULL, { TNFY_HANDLER, 0, sdfs_cychdr }, 1000, 0 });
     55CRE_CYC(SDFS_CYC, { TA_NULL, { TNFY_HANDLER, 0, sdfs_cychdr }, 1000000, 0 });
    5656
    5757CRE_SEM(SEM_MALLOC, { TA_TPRI, 1, 1 });
  • EcnlProtoTool/trunk/ntshell/fatfs/ffarch.h

    r278 r279  
    5454
    5555int mruby_arduino_init();
    56 void reset_heap();
    5756void sdfs_cychdr(intptr_t exinf);
    5857
  • EcnlProtoTool/trunk/ntshell/fatfs/ffconf.h

    r278 r279  
    6262/---------------------------------------------------------------------------*/
    6363
    64 #define _CODE_PAGE 437
     64#define _CODE_PAGE 65001
    6565/* This option specifies the OEM code page to be used on the target system.
    6666/  Incorrect setting of the code page can cause a file open failure.
     
    9191
    9292
    93 #define _USE_LFN        1
     93#define _USE_LFN        3
    9494#define _MAX_LFN        255
    9595/* The _USE_LFN option switches the LFN feature.
     
    227227
    228228
    229 #define _FS_REENTRANT   0
     229#define _FS_REENTRANT   1
    230230#define _FS_TIMEOUT             1000
    231 #define _SYNC_t                 HANDLE
     231#define _SYNC_t                 unsigned int
    232232/* The _FS_REENTRANT option switches the re-entrancy (thread safe) of the FatFs
    233233/  module itself. Note that regardless of this option, file access to different
  • EcnlProtoTool/trunk/ntshell/webserver/httpd-fs.c

    r270 r279  
    6060#include "kernel_cfg.h"
    6161#include "syssvc/syslog.h"
    62 #include "arduino.h"
     62#include "ntstdio.h"
    6363
    6464#ifndef _MSC_VER
     
    9696                        fn = fno.fname;
    9797                        if (fno.fattrib & AM_DIR) {
    98                                 sprintf(&path[i], "0:/%s", fn);
     98                                ntstdio_sprintf(&path[i], "0:/%s", fn);
    9999                                res = scan_files(path, size);
    100100                                if (res != FR_OK) break;
     
    112112/*-----------------------------------------------------------------------------------*/
    113113int
    114 httpd_fs_open_ul(char *name, int len, struct httpd_fs_file *file)
     114httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
    115115{
    116116        FRESULT res;
     
    157157}
    158158
    159 int
    160 httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
    161 {
    162         int result;
    163 
    164         wai_sem(SEM_FILESYSTEM);
    165 
    166         result = httpd_fs_open_ul(name, len, file);
    167 
    168         sig_sem(SEM_FILESYSTEM);
    169 
    170         return result;
    171 }
    172 /*-----------------------------------------------------------------------------------*/
    173 int
    174 httpd_fs_create_ul(char *name, struct httpd_fs_file *file)
     159/*-----------------------------------------------------------------------------------*/
     160int
     161httpd_fs_create(char *name, struct httpd_fs_file *file)
    175162{
    176163        FRESULT res;
     
    196183}
    197184
    198 int
    199 httpd_fs_create(char *name, struct httpd_fs_file *file)
    200 {
    201         int result;
    202 
    203         wai_sem(SEM_FILESYSTEM);
    204 
    205         result = httpd_fs_create_ul(name, file);
    206 
    207         sig_sem(SEM_FILESYSTEM);
    208 
    209         return result;
    210 }
    211 /*-----------------------------------------------------------------------------------*/
    212 int
    213 httpd_fs_read_ul(struct httpd_fs_file *file, void *dst, int len)
     185/*-----------------------------------------------------------------------------------*/
     186int
     187httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
    214188{
    215189        FRESULT ret;
     
    236210}
    237211
    238 int
    239 httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
    240 {
    241         int result;
    242 
    243         wai_sem(SEM_FILESYSTEM);
    244 
    245         result = httpd_fs_read_ul(file, dst, len);
    246 
    247         sig_sem(SEM_FILESYSTEM);
    248 
    249         return result;
    250 }
    251 /*-----------------------------------------------------------------------------------*/
    252 int
    253 httpd_fs_write_ul(struct httpd_fs_file *file, const void *src, int len)
     212/*-----------------------------------------------------------------------------------*/
     213int
     214httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
    254215{
    255216        FRESULT ret;
     
    279240}
    280241
    281 int
    282 httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
    283 {
    284         int result;
    285 
    286         wai_sem(SEM_FILESYSTEM);
    287 
    288         result = httpd_fs_write_ul(file, src, len);
    289 
    290         sig_sem(SEM_FILESYSTEM);
    291 
    292         return result;
    293 }
    294 /*-----------------------------------------------------------------------------------*/
    295 int httpd_fs_close_ul(struct httpd_fs_file *file)
     242/*-----------------------------------------------------------------------------------*/
     243int httpd_fs_close(struct httpd_fs_file *file)
    296244{
    297245        FRESULT ret;
     
    307255        return 1;
    308256}
    309 
    310 int httpd_fs_close(struct httpd_fs_file *file)
    311 {
    312         int result;
    313 
    314         wai_sem(SEM_FILESYSTEM);
    315 
    316         result = httpd_fs_close_ul(file);
    317 
    318         sig_sem(SEM_FILESYSTEM);
    319 
    320         return result;
    321 }
  • EcnlProtoTool/trunk/ntshell/webserver/httpd-fs.h

    r270 r279  
    44 *  Copyright (C) 2017 Cores Co., Ltd. Japan
    55 *
    6  *  ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
    7  *  ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
    8  *  •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
    9  *  (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
    10  *      Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
    11  *      ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
    12  *  (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
    13  *      —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ
    14 ƒƒ“ƒgi—˜—p
    15  *      ŽÒƒ}ƒjƒ
    16 ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
    17  *      ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
    18  *  (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
    19  *      —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
    20  *      ‚ƁD
    21  *    (a) Ä”z•z‚É”º‚¤ƒhƒLƒ
    22 ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ
    23 ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
    24  *        ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
    25  *    (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
    26  *        •ñ‚·‚邱‚ƁD
    27  *  (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
    28  *      ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
    29  *      ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
    30  *      —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
    31  *      –Ɛӂ·‚邱‚ƁD
     6 *  上記著作権è€
     7は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
     8 *  ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
     9 *  変・再é
     10å¸ƒï¼ˆä»¥ä¸‹ï¼Œåˆ©ç”¨ã¨å‘¼ã¶ï¼‰ã™ã‚‹ã“とを無償で許諾する.
     11 *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
     12 *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
     13 *      スコード中に含まれていること.
     14 *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
     15 *      用できる形で再é
     16å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é
     17å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
     18 *      è€
     19マニュアルなど)に,上記の著作権表示,この利用条件および下記
     20 *      の無保証規定を掲載すること.
     21 *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
     22 *      用できない形で再é
     23å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
     24 *      と.
     25 *    (a) 再é
     26å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€
     27マニュアルなど)に,上記の著
     28 *        作権表示,この利用条件および下記の無保証規定を掲載すること.
     29 *    (b) 再é
     30å¸ƒã®å½¢æ
     31‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
     32 *        報告すること.
     33 *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
     34 *      害からも,上記著作権è€
     35およびTOPPERSプロジェクトをå
     36è²¬ã™ã‚‹ã“と.
     37 *      また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
     38 *      由に基づく請求からも,上記著作権è€
     39およびTOPPERSプロジェクトを
     40 *      å
     41è²¬ã™ã‚‹ã“と.
    3242 *
    33  *  –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
    34  *  ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
    35  *  ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
    36  *  ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
    37  *  ‚̐ӔC‚𕉂í‚È‚¢D
     43 *  本ソフトウェアは,無保証で提供されているものである.上記著作権è€
     44お
     45 *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
     46 *  に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
     47 *  アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
     48 *  の責任を負わない.
    3849 *
    3950 *  @(#) $Id$
  • EcnlProtoTool/trunk/ntshell/webserver/httpd.c

    r270 r279  
    5454#include <string.h>
    5555#include <stdlib.h>
     56#include "kernel_cfg.h"
     57#include "tinet_cfg.h"
    5658#include "syssvc/syslog.h"
    5759#include "http-strings.h"
    5860#include "netinet/in.h"
    59 #include "arduino.h"
     61#include "netinet/in_var.h"
     62#include "httpd.h"
     63#include "httpd-fs.h"
     64#include "http-strings.h"
     65#include "base64.h"
     66#include "sha1.h"
     67#include "ntstdio.h"
    6068
    6169#define TRUE 1
    6270#define FALSE 0
     71
     72extern ntstdio_t ntstdio;
     73SYSTIM httpd_time;
     74struct httpd_state *uploding;
     75extern char command[256];
     76
     77extern int execute_command(int wait);
     78
     79/*  TCP 送受信ウィンドバッファ  */
     80uint8_t tcp_swbuf1[TCP_SWBUF_SIZE];
     81uint8_t tcp_rwbuf1[TCP_RWBUF_SIZE];
     82uint8_t tcp_swbuf2[TCP_SWBUF_SIZE];
     83uint8_t tcp_rwbuf2[TCP_RWBUF_SIZE];
     84
     85#define ISO_nl      0x0a
     86#define ISO_space   0x20
     87#define ISO_bang    0x21
     88#define ISO_percent 0x25
     89#define ISO_period  0x2e
     90#define ISO_slash   0x2f
     91#define ISO_colon   0x3a
     92
     93struct httpd_state httpd_state[2] = {
     94        { HTTPD1_TASK, TCP_CEPID1 },
     95        { HTTPD2_TASK, TCP_CEPID2 },
     96};
    6397
    6498#ifndef _MSC_VER
     
    147181        return ncpy;
    148182}
    149 
    150 extern struct httpd_state *uploding;
    151183
    152184int websvr_message_begin(http_parser *p)
     
    306338ˆã¯SDカード
    307339                        s->filename[0] = '1';
    308                         syslog(LOG_NOTICE, "create:    %s.%d %s", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     340                        ntstdio_printf(&ntstdio, "create:    %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    309341                        if (!httpd_fs_create(s->filename, &s->file)) {
    310342                                goto error;
     
    314346                }
    315347                else if (strcmp(s->filename, uploding->filename) == 0) {
    316                         syslog(LOG_NOTICE, "collision: %s.%d %s", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     348                        ntstdio_printf(&ntstdio, "collision: %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    317349                        goto error;
    318350                }
     
    359391
    360392        if (s->message.body_is_final) {
    361                 syslog(LOG_NOTICE, "close:     %s.%d %s", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     393                ntstdio_printf(&ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    362394                httpd_fs_close(&s->file);
    363395                memset(&s->file, 0, sizeof(s->file));
    364396
    365                 strcpy_s(RubyFilename, sizeof(RubyFilename), s->filename);
     397                strcpy_s(command, sizeof(command), "mruby -b ");
     398                strcat_s(command, sizeof(command), s->filename);
    366399                s->reset = 1;
    367400
     
    413446        websvr_message_complete,
    414447};
     448
     449/*
     450 *  ネットワーク層の選択
     451 */
     452
     453#ifdef SUPPORT_INET6
     454
     455#define TCP_ACP_CEP(c,r,d,t)    tcp6_acp_cep(c,r,d,t)
     456#define IP2STR(s,a)             ipv62str(s,a)
     457
     458#else   /* of #ifdef SUPPORT_INET6 */
     459
     460#ifdef SUPPORT_INET4
     461
     462#define TCP_ACP_CEP(c,r,d,t)    tcp_acp_cep(c,r,d,t)
     463#define IP2STR(s,a)             ip2str(s,a)
     464
     465#endif  /* of #ifdef SUPPORT_INET4 */
     466
     467#endif  /* of #ifdef SUPPORT_INET6 */
     468
     469struct httpd_state *get_httpd(ID cepid)
     470{
     471        for (int i = 0; i < 2; i++) {
     472                if (httpd_state[i].cepid != cepid)
     473                        continue;
     474
     475                return &httpd_state[i];
     476        }
     477        return NULL;
     478}
     479
     480struct websocket *websocket_getws(ID wbsid)
     481{
     482        for (int i = 0; i < 2; i++) {
     483                if (httpd_state[i].websocket.wbsid != wbsid)
     484                        continue;
     485
     486                return &httpd_state[i].websocket;
     487        }
     488        return NULL;
     489}
     490
     491void send_file(struct httpd_state *s)
     492{
     493        char *buf;
     494        int len, slen;
     495
     496        while (s->file.len > 0) {
     497                slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
     498                if (slen < 0) {
     499                        syslog(LOG_ERROR, "send_file#tcp_get_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     500                        s->state = STATE_CLOSING;
     501                        break;
     502                }
     503                if (slen == 0)
     504                        return;
     505
     506                len = s->file.len;
     507                if (len > slen)
     508                        len = slen;
     509
     510                len = httpd_fs_read(&s->file, buf, len);
     511                if (len <= 0) {
     512                        syslog(LOG_ERROR, "send_file#httpd_fs_read(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     513                        break;
     514                }
     515
     516                s->file.len -= len;
     517                s->file.pos += len;
     518
     519                if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
     520                        syslog(LOG_ERROR, "send_file#tcp_snd_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     521                        s->state = STATE_CLOSING;
     522                        break;
     523                }
     524        }
     525
     526        ntstdio_printf(&ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     527        httpd_fs_close(&s->file);
     528        s->file.len = 0;
     529        s->file.pos = 0;
     530
     531        s->out.state = OUT_STATE_SEND_END;
     532}
     533
     534void send_data(struct httpd_state *s)
     535{
     536        char *buf;
     537        int len, slen;
     538
     539        while (s->response_len > 0) {
     540                slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
     541                if (slen < 0) {
     542                        syslog(LOG_ERROR, "send_data#tcp_get_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     543                        s->state = STATE_CLOSING;
     544                        break;
     545                }
     546                if (slen == 0)
     547                        return;
     548
     549                len = s->response_len;
     550                if (len > slen)
     551                        len = slen;
     552
     553                memcpy(buf, &s->response_body[s->response_pos], len);
     554
     555                s->response_len -= len;
     556                s->response_pos += len;
     557
     558                if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
     559                        syslog(LOG_ERROR, "send_data#tcp_snd_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     560                        s->state = STATE_CLOSING;
     561                        break;
     562                }
     563        }
     564
     565        s->response_body = NULL;
     566        s->response_len = 0;
     567        s->response_pos = 0;
     568
     569        s->out.state = OUT_STATE_SEND_END;
     570}
     571
     572void send_headers(struct httpd_state *s, const char *statushdr)
     573{
     574        int len;
     575        char *ptr;
     576
     577        len = strlen(statushdr);
     578        tcp_snd_dat(s->cepid, (void *)statushdr, len, TMO_FEVR);
     579
     580        if ((s->filename[0] == '0') && (s->file.len > 0)) {
     581                len = sizeof(http_content_encoding_gzip) - 1;
     582                tcp_snd_dat(s->cepid, (void *)http_content_encoding_gzip, len, TMO_FEVR);
     583        }
     584
     585        if (s->file.redirect) {
     586                len = sizeof(http_location) - 1;
     587                tcp_snd_dat(s->cepid, (void *)http_location, len, TMO_FEVR);
     588                if (s->filename[0] == '1') {
     589                        len = 2;
     590                        tcp_snd_dat(s->cepid, "/~", len, TMO_FEVR);
     591                }
     592                len = strlen(s->filename);
     593                tcp_snd_dat(s->cepid, s->filename, len, TMO_FEVR);
     594                if (s->query != NULL) {
     595                        tcp_snd_dat(s->cepid, "?", 1, TMO_FEVR);
     596                        len = strlen(s->query);
     597                        tcp_snd_dat(s->cepid, s->query, len, TMO_FEVR);
     598                }
     599                len = 2;
     600                tcp_snd_dat(s->cepid, "\r", len, TMO_FEVR);
     601        }
     602
     603        ptr = strrchr(s->filename, ISO_period);
     604        if (ptr == NULL) {
     605                len = sizeof(http_content_type_binary) - 1;
     606                tcp_snd_dat(s->cepid, (void *)http_content_type_binary, len, TMO_FEVR);
     607        }
     608        else if (strncmp(http_html, ptr, sizeof(http_html) - 1) == 0 ||
     609                strncmp(http_htm, ptr, sizeof(http_htm) - 1) == 0) {
     610                len = sizeof(http_content_type_html) - 1;
     611                tcp_snd_dat(s->cepid, (void *)http_content_type_html, len, TMO_FEVR);
     612        }
     613        else if (strncmp(http_css, ptr, sizeof(http_css) - 1) == 0) {
     614                len = sizeof(http_content_type_css) - 1;
     615                tcp_snd_dat(s->cepid, (void *)http_content_type_css, len, TMO_FEVR);
     616        }
     617        else if (strncmp(http_js, ptr, sizeof(http_js) - 1) == 0) {
     618                len = sizeof(http_content_type_js) - 1;
     619                tcp_snd_dat(s->cepid, (void *)http_content_type_js, len, TMO_FEVR);
     620        }
     621        else if (strncmp(http_json, ptr, sizeof(http_json) - 1) == 0) {
     622                len = sizeof(http_content_type_json) - 1;
     623                tcp_snd_dat(s->cepid, (void *)http_content_type_json, len, TMO_FEVR);
     624        }
     625        else if (strncmp(http_png, ptr, sizeof(http_png) - 1) == 0) {
     626                len = sizeof(http_content_type_png) - 1;
     627                tcp_snd_dat(s->cepid, (void *)http_content_type_png, len, TMO_FEVR);
     628        }
     629        else if (strncmp(http_gif, ptr, sizeof(http_gif) - 1) == 0) {
     630                len = sizeof(http_content_type_gif) - 1;
     631                tcp_snd_dat(s->cepid, (void *)http_content_type_gif, len, TMO_FEVR);
     632        }
     633        else if (strncmp(http_jpg, ptr, sizeof(http_jpg) - 1) == 0) {
     634                len = sizeof(http_content_type_jpg) - 1;
     635                tcp_snd_dat(s->cepid, (void *)http_content_type_jpg, len, TMO_FEVR);
     636        }
     637        else if (strncmp(http_svg, ptr, sizeof(http_svg) - 1) == 0) {
     638                len = sizeof(http_content_type_svg) - 1;
     639                tcp_snd_dat(s->cepid, (void *)http_content_type_svg, len, TMO_FEVR);
     640        }
     641        else if (strncmp(http_text, ptr, sizeof(http_text) - 1) == 0) {
     642                len = sizeof(http_content_type_text) - 1;
     643                tcp_snd_dat(s->cepid, (void *)http_content_type_text, len, TMO_FEVR);
     644        }
     645        else if (strncmp(http_eot, ptr, sizeof(http_eot) - 1) == 0) {
     646                len = sizeof(http_content_type_eot) - 1;
     647                tcp_snd_dat(s->cepid, (void *)http_content_type_eot, len, TMO_FEVR);
     648        }
     649        else if (strncmp(http_ttf, ptr, sizeof(http_ttf) - 1) == 0) {
     650                len = sizeof(http_content_type_ttf) - 1;
     651                tcp_snd_dat(s->cepid, (void *)http_content_type_ttf, len, TMO_FEVR);
     652        }
     653        else if (strncmp(http_woff, ptr, sizeof(http_woff) - 1) == 0) {
     654                len = sizeof(http_content_type_woff) - 1;
     655                tcp_snd_dat(s->cepid, (void *)http_content_type_woff, len, TMO_FEVR);
     656        }
     657        else if (strncmp(http_woff2, ptr, sizeof(http_woff2) - 1) == 0) {
     658                len = sizeof(http_content_type_woff2) - 1;
     659                tcp_snd_dat(s->cepid, (void *)http_content_type_woff2, len, TMO_FEVR);
     660        }
     661        else if (strncmp(http_ico, ptr, sizeof(http_ico) - 1) == 0) {
     662                len = sizeof(http_content_type_ico) - 1;
     663                tcp_snd_dat(s->cepid, (void *)http_content_type_ico, len, TMO_FEVR);
     664        }
     665        else {
     666                len = sizeof(http_content_type_plain) - 1;
     667                tcp_snd_dat(s->cepid, (void *)http_content_type_plain, len, TMO_FEVR);
     668        }
     669
     670        if (s->file.len > 0) {
     671                len = sizeof(http_content_length) - 1;
     672                tcp_snd_dat(s->cepid, (void *)http_content_length, len, TMO_FEVR);
     673                ntstdio_snprintf(s->temp, sizeof(s->temp), "%d\r\n", s->file.len);
     674                tcp_snd_dat(s->cepid, (void *)s->temp, strlen(s->temp), TMO_FEVR);
     675        }
     676
     677        if (s->message.should_keep_alive && s->reset == 0) {
     678                len = sizeof(http_connection_keep_alive) - 1;
     679                tcp_snd_dat(s->cepid, (void *)http_connection_keep_alive, len, TMO_FEVR);
     680        }
     681        else {
     682                len = sizeof(http_connection_close) - 1;
     683                tcp_snd_dat(s->cepid, (void *)http_connection_close, len, TMO_FEVR);
     684        }
     685
     686        tcp_snd_dat(s->cepid, (void *)http_crnl, 2, TMO_FEVR);
     687
     688        if (s->filename != NULL) {
     689                s->out.state = OUT_STATE_SEND_FILE;
     690        }
     691        else {
     692                s->out.state = OUT_STATE_SEND_DATA;
     693        }
     694}
     695
     696void handle_output(struct httpd_state *s)
     697{
     698        s->out.wait = false;
     699
     700        switch (s->out.state) {
     701        case OUT_STATE_WAIT_REQUEST:
     702                s->out.wait = true;
     703                break;
     704        case OUT_STATE_OPEN_GET_FILE:
     705                ntstdio_printf(&ntstdio, "open:      %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     706                if (!httpd_fs_open(s->filename, sizeof(s->message.request_url), &s->file)) {
     707                        s->filename = NULL;
     708                        s->response_body = http_content_404;
     709                        s->response_pos = 0;
     710                        s->response_len = sizeof(http_content_403) - 1;
     711                        s->out.statushdr = http_header_404;
     712                }
     713                else {
     714                        s->out.statushdr = s->file.redirect ? http_header_301 : http_header_200;
     715                }
     716                s->out.state = OUT_STATE_SEND_HEADER;
     717                break;
     718        case OUT_STATE_WAIT_POST_BODY:
     719                s->out.wait = true;
     720                break;
     721        case OUT_STATE_BODY_RECEIVED:
     722                s->out.statushdr = http_header_200;
     723                s->out.state = OUT_STATE_SEND_HEADER;
     724                break;
     725        case OUT_STATE_SEND_HEADER:
     726                send_headers(s, s->out.statushdr);
     727                break;
     728        case OUT_STATE_SEND_FILE:
     729                send_file(s);
     730                break;
     731        case OUT_STATE_SEND_DATA:
     732                send_data(s);
     733                break;
     734        case OUT_STATE_SEND_END:
     735                s->out.wait = true;
     736                if (s->message.should_keep_alive && s->reset == 0) {
     737                        s->out.state = OUT_STATE_WAIT_REQUEST;
     738                }
     739                else {
     740                        s->state = STATE_CLOSING;
     741                }
     742                break;
     743        }
     744}
     745
     746void send_ws_headers(struct httpd_state *s, const char *statushdr)
     747{
     748        int len;
     749
     750        len = strlen(statushdr);
     751        tcp_snd_dat(s->cepid, (void *)statushdr, len, TMO_FEVR);
     752
     753        len = sizeof(http_upgrade) - 1;
     754        tcp_snd_dat(s->cepid, (void *)http_upgrade, len, TMO_FEVR);
     755        len = strlen(s->message.upgrade);
     756        tcp_snd_dat(s->cepid, s->message.upgrade, len, TMO_FEVR);
     757        len = sizeof(http_crnl) - 1;
     758        tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
     759
     760        len = sizeof(http_connection) - 1;
     761        tcp_snd_dat(s->cepid, (void *)http_connection, len, TMO_FEVR);
     762        len = strlen(s->message.connection);
     763        tcp_snd_dat(s->cepid, s->message.connection, len, TMO_FEVR);
     764        len = sizeof(http_crnl) - 1;
     765        tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
     766
     767        len = sizeof(http_sec_websocket_accept) - 1;
     768        tcp_snd_dat(s->cepid, (void *)http_sec_websocket_accept, len, TMO_FEVR);
     769        len = strlen(s->message.response_key);
     770        tcp_snd_dat(s->cepid, s->message.response_key, len, TMO_FEVR);
     771        len = sizeof(http_crnl) - 1;
     772        tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
     773
     774        len = sizeof(http_sec_websocket_protocol) - 1;
     775        tcp_snd_dat(s->cepid, (void *)http_sec_websocket_protocol, len, TMO_FEVR);
     776        len = strlen(s->message.sec_websocket_protocol);
     777        tcp_snd_dat(s->cepid, s->message.sec_websocket_protocol, len, TMO_FEVR);
     778        len = sizeof(http_crnl) - 1;
     779        tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
     780
     781        len = sizeof(http_crnl) - 1;
     782        tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
     783}
     784
     785void send_ws_data(struct httpd_state *s)
     786{
     787        char *buf;
     788        int slen;
     789
     790        slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
     791        if (slen < 0) {
     792                syslog(LOG_ERROR, "send_ws_data#tcp_get_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     793                return;
     794        }
     795
     796        websocket_output(&s->websocket, buf, slen);
     797}
     798
     799void handle_ws_output(struct httpd_state *s)
     800{
     801        char shaHash[20];
     802        SHA_CTX sha1;
     803        int len;
     804
     805        strlncat(s->message.response_key, sizeof(s->message.response_key),
     806                s->message.sec_websocket_key, sizeof(s->message.sec_websocket_key));
     807        len = strlncat(s->message.response_key, sizeof(s->message.response_key),
     808                http_websocket_guid, sizeof(http_websocket_guid));
     809        memset(shaHash, 0, sizeof(shaHash));
     810        SHA1_Init(&sha1);
     811        SHA1_Update(&sha1, (sha1_byte *)s->message.response_key, len);
     812        SHA1_Final((sha1_byte *)shaHash, &sha1);
     813        base64_encode((unsigned char *)s->message.response_key,
     814                sizeof(s->message.response_key), (unsigned char *)shaHash, sizeof(shaHash));
     815
     816        send_ws_headers(s, http_header_101);
     817
     818        s->message.response_key[0] = '\0';
     819
     820        do {
     821                while (!websocket_newdata(&s->websocket))
     822                        slp_tsk();
     823
     824                send_ws_data(s);
     825        } while ((s->state == STATE_CONNECTED) && (!s->close_req));
     826        s->state = STATE_DISCONNECTED;
     827        websocket_destroy(&s->websocket);
     828        s->close_req = 0;
     829
     830        s->state = STATE_CLOSING;
     831}
     832
     833void handle_input(struct httpd_state *s)
     834{
     835        size_t done;
     836        int len;
     837
     838        s->in.wait = false;
     839
     840        switch (s->in.state) {
     841        case IN_STATE_START:
     842                http_parser_init(&s->parser, HTTP_REQUEST);
     843                s->in.state = IN_STATE_REQUEST;
     844                break;
     845        case IN_STATE_REQUEST:
     846        case IN_STATE_RESPONSE:
     847        case IN_STATE_UPLOAD:
     848                if ((len = tcp_rcv_buf(s->cepid, (void **)&s->in.data, TMO_POL)) <= 0) {
     849                        if ((len == E_TMOUT) || (len == 0)) {
     850                                // 3秒はå¾
     851つ
     852                                //if (httpd_time - s->in.timer < 30000000) {
     853                                        s->in.wait = true;
     854                                        break;
     855                                //}
     856                        }
     857                        syslog(LOG_ERROR, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     858                        uploding = NULL;
     859                        s->state = STATE_CLOSING;
     860                        return;
     861                }
     862                done = http_parser_execute(&s->parser, &websvr_settings, s->in.data, len);
     863                tcp_rel_buf(s->cepid, done);
     864                if (s->parser.http_errno != HPE_OK) {
     865                        syslog(LOG_ERROR, "http_parser error %s.%d => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, s->parser.http_errno);
     866                        uploding = NULL;
     867                        s->state = STATE_CLOSING;
     868                        return;
     869                }
     870
     871                s->parse_pos = done;
     872                s->parse_len = len - done;
     873                break;
     874        case IN_STATE_UPLOAD_WAIT:
     875                if (uploding != NULL) {
     876                        s->in.wait = true;
     877                }
     878                else {
     879                        uploding = s;
     880                        s->in.state = IN_STATE_UPLOAD;
     881                }
     882                break;
     883        case IN_STATE_WEBSOCKET:
     884                if (s->parse_len <= 0) {
     885                        if ((len = tcp_rcv_buf(s->cepid, (void **)&s->in.data, TMO_POL)) <= 0) {
     886                                if ((len == E_TMOUT) || (len == 0)) {
     887                                        s->in.wait = true;
     888                                        break;
     889                                }
     890                                syslog(LOG_ERROR, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     891                                s->state = STATE_CLOSING;
     892                                break;
     893                        }
     894
     895                        s->parse_pos = 0;
     896                        s->parse_len = len;
     897                }
     898                else
     899                        len = s->parse_len;
     900                done = websocket_input(&s->websocket, (void *)s->in.data, s->parse_len);
     901                tcp_rel_buf(s->cepid, done);
     902                if ((done != 0) || (s->websocket.rstate.opecode == connection_close)) {
     903                        s->close_req = 1;
     904                        s->state = STATE_CLOSING;
     905                        break;
     906                }
     907                s->parse_pos = done;
     908                s->parse_len -= done;
     909                break;
     910        case IN_STATE_END:
     911                s->in.wait = true;
     912                break;
     913        default:
     914                s->state = STATE_CLOSING;
     915                break;
     916        }
     917}
     918
     919/*
     920 *  ノンブロッキングコールのコールバック関数
     921 */
     922ER
     923callback_nblk_tcp(ID cepid, FN fncd, void *p_parblk)
     924{
     925        struct httpd_state *s = get_httpd(cepid);
     926
     927        if (s == NULL)
     928                ntstdio_printf(&ntstdio, "callback_nblk_tcp(%d, %d)\n", fncd, cepid);
     929        else
     930                ntstdio_printf(&ntstdio, "callback_nblk_tcp(%d, %s.%d)\n", fncd, s->addr, ((T_IPV4EP *)s->dst)->portno);
     931
     932        return E_PAR;
     933}
     934
     935/*
     936 * HTTPサーバータスク
     937 */
     938void httpd_task(intptr_t exinf)
     939{
     940        ER ret, ret2;
     941        struct httpd_state *s = &httpd_state[exinf];
     942
     943        for (;;) {
     944                ret2 = get_tim(&httpd_time);
     945                if (ret2 != E_OK) {
     946                        syslog(LOG_ERROR, "get_tim");
     947                        return;
     948                }
     949
     950                switch (s->state) {
     951                case STATE_DISCONNECTED:
     952                        memset(&s->dst, 0, sizeof(s->dst));
     953                        if ((ret = TCP_ACP_CEP(s->cepid, TCP_REPID, (T_IPV4EP *)s->dst, TMO_FEVR)) != E_OK) {
     954                                syslog(LOG_ERROR, "tcp_acp_cep(%d) => %d", s->cepid, ret);
     955                                tslp_tsk(100);  // TODO
     956                                s->state = STATE_CLOSING;
     957                                break;
     958                        }
     959                        IP2STR(s->addr, &((T_IPV4EP *)s->dst)->ipaddr);
     960                        ntstdio_printf(&ntstdio, "connected: %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
     961                        memset(&s->in, 0, sizeof(s->in));
     962                        memset(&s->out, 0, sizeof(s->out));
     963                        s->in.timer = httpd_time;
     964                        s->state = STATE_CONNECTED;
     965                        break;
     966                case STATE_CONNECTED:
     967                        handle_input(s);
     968                        handle_output(s);
     969                        break;
     970                case STATE_WEBSOCKET:
     971                        handle_input(s);
     972                        handle_ws_output(s);
     973                        break;
     974                case STATE_CLOSING:
     975                        ntstdio_printf(&ntstdio, "close:     %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
     976                        tcp_sht_cep(s->cepid);
     977                        tcp_cls_cep(s->cepid, TMO_FEVR);
     978
     979                        if (s->reset) {
     980                                s->reset = 0;
     981                                s->state = STATE_RESET;
     982                        }
     983                        else {
     984                                s->state = STATE_DISCONNECTED;
     985                        }
     986                        break;
     987                case STATE_RESET:
     988                        execute_command(0);
     989                        s->state = STATE_DISCONNECTED;
     990                        break;
     991                }
     992
     993                if (s->in.wait && s->out.wait) {
     994                        tslp_tsk(100);
     995                }
     996        }
     997}
  • EcnlProtoTool/trunk/ntshell/webserver/httpd.h

    r270 r279  
    44 *  Copyright (C) 2017 Cores Co., Ltd. Japan
    55 *
    6  *  ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
    7  *  ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
    8  *  •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
    9  *  (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
    10  *      Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
    11  *      ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
    12  *  (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
    13  *      —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ
    14 ƒƒ“ƒgi—˜—p
    15  *      ŽÒƒ}ƒjƒ
    16 ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
    17  *      ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
    18  *  (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
    19  *      —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
    20  *      ‚ƁD
    21  *    (a) Ä”z•z‚É”º‚¤ƒhƒLƒ
    22 ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ
    23 ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
    24  *        ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
    25  *    (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
    26  *        •ñ‚·‚邱‚ƁD
    27  *  (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
    28  *      ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
    29  *      ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
    30  *      —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
    31  *      –Ɛӂ·‚邱‚ƁD
    32  *
    33  *  –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
    34  *  ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
    35  *  ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
    36  *  ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
    37  *  ‚̐ӔC‚𕉂í‚È‚¢D
     6 *  上記著作権è€
     7は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
     8 *  ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
     9 *  変・再é
     10å¸ƒï¼ˆä»¥ä¸‹ï¼Œåˆ©ç”¨ã¨å‘¼ã¶ï¼‰ã™ã‚‹ã“とを無償で許諾する.
     11 *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
     12 *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
     13 *      スコード中に含まれていること.
     14 *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
     15 *      用できる形で再é
     16å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é
     17å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
     18 *      è€
     19マニュアルなど)に,上記の著作権表示,この利用条件および下記
     20 *      の無保証規定を掲載すること.
     21 *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
     22 *      用できない形で再é
     23å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
     24 *      と.
     25 *    (a) 再é
     26å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€
     27マニュアルなど)に,上記の著
     28 *        作権表示,この利用条件および下記の無保証規定を掲載すること.
     29 *    (b) 再é
     30å¸ƒã®å½¢æ
     31‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
     32 *        報告すること.
     33 *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
     34 *      害からも,上記著作権è€
     35およびTOPPERSプロジェクトをå
     36è²¬ã™ã‚‹ã“と.
     37 *      また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
     38 *      由に基づく請求からも,上記著作権è€
     39およびTOPPERSプロジェクトを
     40 *      å
     41è²¬ã™ã‚‹ã“と.
     42 *
     43 *  本ソフトウェアは,無保証で提供されているものである.上記著作権è€
     44お
     45 *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
     46 *  に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
     47 *  アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
     48 *  の責任を負わない.
    3849 *
    3950 *  @(#) $Id$
     
    4354#define __HTTPD_H__
    4455
     56/*
     57 *  ターゲット依存の定義
     58 */
     59#include <kernel.h>
    4560#include "httpd-fs.h"
    4661#include "http_parser.h"
    4762#include "websocket.h"
     63
     64/*
     65 *  各タスクの優å
     66ˆåº¦ã®å®šç¾©
     67 */
     68
     69#define HTTPD_PRIORITY  5               /* HTTPサーバータスクの優å
     70ˆåº¦ */
     71
     72#define HTTPD_STACK_SIZE                1024    /* HTTPサーバータスクのスタック領域のサイズ */
     73
     74 /*  TCP 送受信ウィンドバッファサイズ  */
     75
     76#define TCP_SWBUF_SIZE  512
     77#define TCP_RWBUF_SIZE  512
     78
     79/*
     80 *  関数のプロトタイプ宣言
     81 */
     82#ifndef TOPPERS_MACRO_ONLY
     83
     84 /*  TCP 送受信ウィンドバッファ  */
     85
     86extern uint8_t tcp_swbuf1[];
     87extern uint8_t tcp_rwbuf1[];
     88extern uint8_t tcp_swbuf2[];
     89extern uint8_t tcp_rwbuf2[];
     90
     91/* HTTPサーバータスク */
     92extern void httpd_task(intptr_t exinf);
    4893
    4994#define MAX_ELEMENT_SIZE 256
     
    81126
    82127typedef enum httpd_state_t {
    83         STATE_DISCONNECTED,             /* Ø’f’† */
    84         STATE_CONNECTED,                /* Ú‘±’† */
    85         STATE_WEBSOCKET,                /* WebSocket’ʐM’† */
    86         STATE_CLOSING,                  /* Ø’fˆ—’† */
    87         STATE_RESET,                    /* mruby‹N“®‚Ì‚½‚߂̃ŠƒZƒbƒg */
     128        STATE_DISCONNECTED,             /* 切断中 */
     129        STATE_CONNECTED,                /* 接続中 */
     130        STATE_WEBSOCKET,                /* WebSocket通信中 */
     131        STATE_CLOSING,                  /* 切断処理中 */
     132        STATE_RESET,                    /* mruby起動のためのリセット */
    88133} httpd_state_t;
    89134
     
    155200#define get_context(p) (struct httpd_state *)((intptr_t)p - (intptr_t)&((struct httpd_state *)0)->parser)
    156201
     202/*
     203 * ノンブロッキングコールのコールバック関数
     204 */
     205extern ER callback_nblk_tcp(ID cepid, FN fncd, void *p_parblk);
     206
     207#endif /* TOPPERS_MACRO_ONLY */
     208
    157209#endif /* __HTTPD_H__ */
  • EcnlProtoTool/trunk/ntshell/webserver/websocket.c

    r270 r279  
    5757#include "syssvc/syslog.h"
    5858
    59 static ECN_FBS_ID cur_out_msg;
     59static WS_FBS_ID cur_out_msg;
    6060
    6161void websocket_init(struct websocket *ws, ID wbsid)
     
    6969        if ((cur_out_msg.ptr != NULL) && (((ID *)cur_out_msg.ptr->_gap)[0] == ws->wbsid))
    7070        {
    71                 _ecn_fbs_del(cur_out_msg);
     71                _ws_fbs_del(cur_out_msg);
    7272                memset(&cur_out_msg, 0, sizeof(cur_out_msg));
    7373        }
    7474
    7575        if (ws->cur_in_msg.ptr != NULL) {
    76                 _ecn_fbs_del(ws->cur_in_msg);
     76                _ws_fbs_del(ws->cur_in_msg);
    7777                memset(&ws->cur_in_msg, 0, sizeof(ws->cur_in_msg));
    7878        }
     
    136136                        }
    137137                        else {
    138                                 _ecn_fbs_cre(1, &ws->cur_in_msg);
     138                                _ws_fbs_cre(1, &ws->cur_in_msg);
    139139                                s->state++;
    140140                        }
     
    143143                default:
    144144                        if(ws->cur_in_msg.ptr != NULL)
    145                                 _ecn_fbs_poke(ws->cur_in_msg, s->data_pos, *pos ^ s->masking_key[s->data_pos % 4]);
     145                                _ws_fbs_poke(ws->cur_in_msg, s->data_pos, *pos ^ s->masking_key[s->data_pos % 4]);
    146146
    147147                        s->data_pos++;
     
    152152                                if (ws->cur_in_msg.ptr != NULL) {
    153153                                        ((ID *)ws->cur_in_msg.ptr->_gap)[0] = ws->wbsid;
    154                                         ret = psnd_dtq(ecn_api_mailboxid, (intptr_t)ws->cur_in_msg.ptr);
     154                                        ret = psnd_dtq(ws_api_mailboxid, (intptr_t)ws->cur_in_msg.ptr);
    155155                                        if (ret != E_OK) {
    156                                                 syslog(LOG_WARNING, "websocket_input() : psnd_dtq(%d) result = %d", ecn_api_mailboxid, ret);
    157                                                 _ecn_fbs_del(ws->cur_in_msg);
     156                                                syslog(LOG_WARNING, "websocket_input() : psnd_dtq(%d) result = %d", ws_api_mailboxid, ret);
     157                                                _ws_fbs_del(ws->cur_in_msg);
    158158                                        }
    159159                                }
     
    172172        struct websocket *mws;
    173173        ER ret;
    174         ECN_FBS_ID data;
     174        WS_FBS_ID data;
    175175        ID wbsid;
    176176
     
    196196                        break;
    197197
    198                 _ecn_fbs_del(cur_out_msg);
     198                _ws_fbs_del(cur_out_msg);
    199199        }
    200200
     
    217217                                return (intptr_t)pos - (intptr_t)data;
    218218
    219                         s->payload_len = _ecn_fbs_get_datalen(cur_out_msg);
     219                        s->payload_len = _ws_fbs_get_datalen(cur_out_msg);
    220220                        if(s->payload_len == 0){
    221221                                goto next;
     
    281281                // Payload buffer
    282282                default:
    283                         *pos = _ecn_fbs_peek(cur_out_msg, s->data_pos) ^ s->masking_key[s->data_pos % 4];
     283                        *pos = _ws_fbs_peek(cur_out_msg, s->data_pos) ^ s->masking_key[s->data_pos % 4];
    284284
    285285                        s->data_pos++;
     
    289289                        break;
    290290                next:
    291                         _ecn_fbs_del(cur_out_msg);
     291                        _ws_fbs_del(cur_out_msg);
    292292                        memset(&cur_out_msg, 0, sizeof(cur_out_msg));
    293293                        s->state = 0;
  • EcnlProtoTool/trunk/ntshell/webserver/websocket.h

    r270 r279  
    5858#include "t_stddef.h"
    5959
    60 #define NUM_ECHONET_MEMPOOL_BLOCK (60)          /* 電文用メモリブロックの数 */
    61 #define ECHONET_MEMPOOL_BLOCK_SIZE (256)        /* 電文用メモリブロックのサイズ */
     60#define NUM_WEBSOCKET_MBXID     2
    6261
    63 /* T_EDATA管理領域サイズ */
    64 #define DEF_ECN_EDT_HDR_LEN (sizeof(intptr_t))
     62#ifndef TOPPERS_MACRO_ONLY
     63#include "websocket_fbs.h"
    6564
    66 /* 管理領域サイズ(64byte - sizeof(T_MSG)) */
    67 /* #define DEF_ECN_FBS_LIB_HDR_LEN ((ECHONET_MEMPOOL_BLOCK_SIZE + sizeof(int) - 1)>> 2) // */
    68 #define DEF_ECN_FBS_LIB_HDR_LEN (64 - DEF_ECN_EDT_HDR_LEN)
    69 
    70 /* 管理領域を含むブロックに保持するデータサイズ */
    71 #define DEF_ECN_FBS_FST_DAT_LEN (64)
    72 
    73 /* リンクポインタé
    74 åˆ—のサイズ(byte) */
    75 #define DEF_ECN_FBS_LNK_LEN (ECHONET_MEMPOOL_BLOCK_SIZE - DEF_ECN_EDT_HDR_LEN - DEF_ECN_FBS_LIB_HDR_LEN - DEF_ECN_FBS_FST_DAT_LEN)
    76 
    77 /* 固定長バッファ最大サイズ */
    78 #define DEF_ECN_FBS_BUF_MAXLEN ((DEF_ECN_FBS_LNK_LEN / sizeof(void *)) * ECHONET_MEMPOOL_BLOCK_SIZE + DEF_ECN_FBS_FST_DAT_LEN)
    79 
    80 /* 子要素バッファのサイズ */
    81 #define DEF_ECN_FBS_SUB_BUF_LEN ECHONET_MEMPOOL_BLOCK_SIZE
    82 
    83 /* リンクポインタé
    84 åˆ—に列挙される、子要素バッファ */
    85 typedef struct {
    86         uint8_t payload[ECHONET_MEMPOOL_BLOCK_SIZE];
    87 } T_ECN_SUB_BLK;
    88 
    89 /* リンクポインタé
    90 åˆ— */
    91 typedef union {
    92         T_ECN_SUB_BLK   *p_sub[DEF_ECN_FBS_LNK_LEN / sizeof(T_ECN_SUB_BLK *)];
    93         uint8_t                 bin[DEF_ECN_FBS_LNK_LEN];
    94 } T_ECN_FBS_LNK;
    95 
    96 /* 管理領域(64byte - sizeof(T_MSG)) */
    97 typedef struct {
    98         uint16_t        length;                 /* メッセージ長 */
    99         uint16_t        type;                   /* メッセージタイプ */
    100         union {
    101                 ID mbxid;                               /* 送信å
    102 ƒãƒ¡ãƒ¼ãƒ«ãƒœãƒƒã‚¯ã‚¹ID(å†
    103 éƒ¨ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ï¼‰ */
    104         }sender;
    105         union {
    106                 ID mbxid;                               /* 送信å
    107 ˆãƒ¡ãƒ¼ãƒ«ãƒœãƒƒã‚¯ã‚¹ID(å†
    108 éƒ¨ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ï¼‰ */
    109         }target;
    110         union {
    111                 ID mbxid;                               /* 返信å
    112 ˆãƒ¡ãƒ¼ãƒ«ãƒœãƒƒã‚¯ã‚¹ID(å†
    113 éƒ¨ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ï¼‰ */
    114         }reply;
    115 
    116         int                     rd;                             /*      読み取りヘッドæƒ
    117 å ±     */
    118         int                     wr;                             /*      書き込みヘッドæƒ
    119 å ±     */
    120 } T_ECN_FBS_HDR;
    121 
    122 /* 管理領域を含むブロックの構造 */
    123 typedef struct {
    124         uint8_t _msg[DEF_ECN_EDT_HDR_LEN];      /* T_MSG */
    125         T_ECN_FBS_HDR   hdr;    /*      管理領域  64byte  - sizeof(T_MSG)   */
    126         uint8_t                 _gap[DEF_ECN_FBS_LIB_HDR_LEN - sizeof(T_ECN_FBS_HDR)];
    127         T_ECN_FBS_LNK   lnk;    /*      リンクポインタé
    128 åˆ—    128byte                 */
    129         uint8_t                 payload[DEF_ECN_FBS_FST_DAT_LEN];
    130 } T_ECN_FST_BLK;
    131 
    132 /* FBS-ID */
    133 typedef struct ecn_fbs_id_strc {
    134         T_ECN_FST_BLK   *ptr;
    135 } ECN_FBS_ID;
    136 
    137 ER _ecn_fbs_cre(size_t fa_req_size, ECN_FBS_ID *fp_id);
    138 ER _ecn_fbs_del(ECN_FBS_ID fa_id);
    139 size_t _ecn_fbs_get_datalen(ECN_FBS_ID fa_id);
    140 int _ecn_fbs_peek(ECN_FBS_ID fa_id, size_t fa_seek);
    141 ER _ecn_fbs_poke(ECN_FBS_ID fa_id, size_t fa_seek, int fa_val);
    142 
    143 extern ID ecn_api_mailboxid;
    144 extern ID WEBSOCKET_MBXID;
     65extern ID ws_api_mailboxid;
    14566
    14667typedef struct ws_state
     
    16889        ws_state_t wstate;
    16990        ws_state_t rstate;
    170         ECN_FBS_ID cur_in_msg;
     91        WS_FBS_ID cur_in_msg;
    17192};
    17293
     
    179100int websocket_output(struct websocket *ws, void *data, int len);
    180101
     102#endif /* TOPPERS_MACRO_ONLY */
     103
    181104#endif  /* WEBSOCKET_H */
  • EcnlProtoTool/trunk/prototool/src/arduino.c

    r278 r279  
    11#include <arduino.h>
    22#include <mruby.h>
     3#include <stdlib.h>
     4#include <stdio.h>
     5#include <fcntl.h>
    36#include "../llbruby.h"
    47
     
    1619i2c_t Wire5;
    1720
    18 FATFS RomDisk;
    19 SD Sd;
    20 
    2121char ExeFilename[64];
    2222
     
    2727void randomSeed(long seed)
    2828{
     29        srand(seed);
    2930}
    3031
    3132long arduino_random(long min, long max)
    3233{
    33         return min;
     34        return (long)(((double)rand() / (double)RAND_MAX) / ((double)max - (double)min)) + min;
    3435}
    3536
     
    223224int EEP_fopen(FILEEEP *fp, const char *str, char mode)
    224225{
    225         FRESULT ret;
    226         BYTE fm;
     226        int ret;
     227        int fm;
    227228
    228229        switch (mode){
    229230        case 0:
    230                 fm = FA_READ;
     231                fm = O_RDONLY;
    231232                break;
    232233        case 1:
    233                 fm = FA_READ | FA_WRITE | FA_CREATE_NEW;
     234                fm = O_RDWR | O_CREAT;
    234235                break;
    235236        case 2:
    236                 fm = FA_READ | FA_WRITE;
     237                fm = O_RDWR;
    237238                break;
    238239        default:
     
    240241        }
    241242
    242         ret = f_open(&fp->fp, str, fm);
    243 
    244         return (ret == FR_OK) ? 0 : -1;
     243        ret = open(str, fm);
     244        if (ret < 0) {
     245                fp->fd = 0;
     246                return -1;
     247        }
     248        fp->fd = ret;
     249
     250        return 0;
    245251}
    246252
    247253int EEP_fwrite(FILEEEP *fp, byte *data, int *len)
    248254{
    249         FRESULT ret;
    250 
    251         ret = f_write(&fp->fp, data, (UINT)*len, (UINT *)len);
    252 
    253         return (ret == FR_OK) ? 1 : -1;
     255        int ret;
     256
     257        ret = write(fp->fd, data, *len);
     258        if (ret < 0) {
     259                *len = 0;
     260                return -1;
     261        }
     262        *len = ret;
     263
     264        return 0;
    254265}
    255266
    256267int EEP_fread(FILEEEP *fp)
    257268{
    258         FRESULT ret;
     269        int ret;
    259270        char c;
    260         UINT br;
    261 
    262         ret = f_read(&fp->fp, &c, 1, &br);
    263         if (ret != FR_OK)
     271
     272        ret = read(fp->fd, &c, 1);
     273        if (ret < 0)
    264274                return -1;
    265275
     
    269279void EEP_fclose(FILEEEP *fp)
    270280{
    271         f_close(&fp->fp);
     281        close(fp->fd);
    272282}
    273283
    274284int EEP_fseek(FILEEEP *fp, int pos, int mode)
    275285{
    276         FRESULT ret;
     286        int ret;
    277287
    278288        switch (mode){
    279289        case EEP_SEEKTOP:
    280                 ret = f_lseek(&fp->fp, pos);
     290                ret = lseek(fp->fd, pos, SEEK_SET);
    281291                break;
    282292        case EEP_SEEKCUR:
    283                 ret = f_lseek(&fp->fp, fp->fp.fptr + pos);
     293                ret = lseek(fp->fd, pos, SEEK_CUR);
    284294                break;
    285295        case EEP_SEEKEND:
    286                 ret = f_lseek(&fp->fp, fp->fp.fsize - pos);
     296                ret = lseek(fp->fd, pos, SEEK_END);
    287297                break;
    288298        default:
    289                 ret = FR_INVALID_PARAMETER;
    290                 break;
    291         }
    292 
    293         if (ret != FR_OK)
    294                 f_lseek(&fp->fp, 0);
    295 
    296         return fp->fp.fptr;
     299                return -1;
     300                break;
     301        }
     302
     303        if (ret < 0)
     304                return false;
     305
     306        return true;
    297307}
    298308
    299309int EEP_ffilesize(const char *str)
    300310{
    301         FRESULT ret;
    302         FILINFO fno;
    303 
    304         ret = f_stat(str, &fno);
    305         if (ret != FR_OK)
     311        int ret;
     312        struct stat fno;
     313
     314        ret = stat(str, &fno);
     315        if (ret < 0)
    306316                return 0;
    307317
    308         return fno.fsize;
     318        return fno.st_size;
    309319}
    310320
    311321bool EEP_fEof(FILEEEP *fp)
    312322{
    313         return f_tell(&fp->fp) == f_size(&fp->fp);
     323        int ret, pos;
     324        struct stat fno;
     325
     326        ret = fstat(fp->fd, &fno);
     327        if (ret < 0)
     328                return false;
     329
     330        pos = lseek(fp->fd, 0, SEEK_CUR);
     331
     332        return pos == fno.st_size;
    314333}
    315334
    316335bool EEP_fexist(const char *path)
    317336{
    318         FRESULT ret;
    319         FILINFO fno;
    320 
    321         ret = f_stat(path, &fno);
    322 
    323         return (ret == FR_OK);
     337        int ret;
     338        struct stat fno;
     339
     340        ret = stat(path, &fno);
     341
     342        return (ret == 0);
    324343}
    325344
    326345bool EEP_fcopy(const char *src, const char *dst)
    327346{
    328         FIL fsrc, fdst;
    329         BYTE buffer[512];
    330         FRESULT res;
    331         UINT br, bw;
    332 
    333         res = f_open(&fsrc, src, FA_OPEN_EXISTING | FA_READ);
    334         if (res != FR_OK)
    335                 return false;
    336 
    337         res = f_open(&fdst, dst, FA_CREATE_ALWAYS | FA_WRITE);
    338         if (res != FR_OK){
    339                 f_close(&fsrc);
     347        int fsrc, fdst;
     348        unsigned char buffer[512];
     349        int res = true;
     350        int br, bw;
     351
     352        fsrc = open(src, O_RDONLY);
     353        if (fsrc < 0)
     354                return false;
     355
     356        fdst = open(dst, O_RDWR | O_CREAT);
     357        if (fdst < 0){
     358                close(fsrc);
    340359                return false;
    341360        }
    342361
    343362        for (;;) {
    344                 res = f_read(&fsrc, buffer, sizeof(buffer), &br);
    345                 if ((res != FR_OK) || br == 0)
     363                br = read(fsrc, buffer, sizeof(buffer));
     364                if (br < 0) {
     365                        res = false;
    346366                        break;
    347 
    348                 res = f_write(&fdst, buffer, br, &bw);
    349                 if ((res != FR_OK) || bw < br)
     367                }
     368                if (br == 0)
    350369                        break;
    351         }
    352 
    353         f_close(&fsrc);
    354         f_close(&fdst);
    355 
    356         return (res == FR_OK);
     370
     371                bw = write(fdst, buffer, br);
     372                if ((bw < 0) || bw < br) {
     373                        res = false;
     374                        break;
     375                }
     376        }
     377
     378        close(fsrc);
     379        close(fdst);
     380
     381        return res;
    357382}
    358383
    359384bool EEP_fdelete(const char *path)
    360385{
    361         FRESULT res;
    362 
    363         res = f_unlink(path);
    364 
    365         return (res == FR_OK);
    366 }
    367 
    368 bool SD_begin()
    369 {
    370         DSTATUS dst;
    371         FRESULT res;
    372         BYTE pdrv = 1, type;
    373 
    374         if (Sd.FatFs.fs_type != 0)
    375                 return true;
    376 
    377         if ((dst = disk_initialize(pdrv)) != RES_OK) {
    378                 return false;
    379         }
    380 
    381         if ((dst = disk_ioctl(pdrv, MMC_GET_TYPE, &type)) != RES_OK) {
    382                 Sd.dst = dst;
    383                 Sd.type = 0;
    384         }
    385         else {
    386                 Sd.dst = RES_OK;
    387                 Sd.type = type;
    388         }
    389 
    390         if ((res = f_mount(&Sd.FatFs, "1:", 1)) != FR_OK) {
    391                 return false;
    392         }
    393 
     386        int res;
     387
     388        res = unlink(path);
     389
     390        return (res == 0);
     391}
     392
     393bool SD_open(File *file, const char *path, int mode)
     394{
     395        int ret;
     396
     397        if (!SD_begin())
     398                return false;
     399
     400        ret = open(path, mode);
     401        if (ret < 0) {
     402                file->fd = 0;
     403                return false;
     404        }
     405
     406        file->fd = ret;
    394407        return true;
    395408}
    396409
    397 bool SD_open(File *file, const char *path, int mode)
    398 {
    399         FRESULT ret;
     410bool SD_rmdir(const char *path)
     411{
     412        int ret;
    400413
    401414        if (!SD_begin())
    402415                return false;
    403416
    404         ret = f_open(&file->fp, path, mode);
    405 
    406         return ret == FR_OK;
    407 }
    408 
    409 bool SD_rmdir(const char *path)
    410 {
    411         FRESULT ret;
     417        ret = rmdir(path);
     418
     419        return ret == 0;
     420}
     421
     422bool SD_rename(const char *path1, const char *path2)
     423{
     424        int ret;
    412425
    413426        if (!SD_begin())
    414427                return false;
    415428
    416         ret = f_unlink(path);
    417 
    418         return ret == FR_OK;
    419 }
    420 
    421 bool SD_rename(const char *path1, const char *path2)
    422 {
    423         FRESULT ret;
     429        ret = rename(path1, path2);
     430       
     431        return ret == 0;
     432}
     433
     434bool SD_remove(const char *path)
     435{
     436        int ret;
    424437
    425438        if (!SD_begin())
    426439                return false;
    427440
    428         ret = f_rename(path1, path2);
    429        
    430         return ret == FR_OK;
    431 }
    432 
    433 bool SD_remove(const char *path)
    434 {
    435         FRESULT ret;
     441        ret = unlink(path);
     442
     443        return ret == 0;
     444}
     445
     446bool SD_exists(const char *path)
     447{
     448        int ret;
     449        struct stat fno;
    436450
    437451        if (!SD_begin())
    438452                return false;
    439453
    440         ret = f_unlink(path);
    441 
    442         return ret == FR_OK;
    443 }
    444 
    445 bool SD_exists(const char *path)
    446 {
    447         FRESULT ret;
    448         FILINFO fno;
     454        ret = stat(path, &fno);
     455
     456        return ret == 0;
     457}
     458
     459bool SD_mkdir(const char *path)
     460{
     461        int ret;
    449462
    450463        if (!SD_begin())
    451464                return false;
    452465
    453         ret = f_stat(path, &fno);
    454 
    455         return ret == FR_OK;
    456 }
    457 
    458 bool SD_mkdir(const char *path)
    459 {
    460         FRESULT ret;
    461 
    462         if (!SD_begin())
    463                 return false;
    464 
    465         ret = f_mkdir(path);
    466 
    467         return ret == FR_OK;
    468 }
    469 
    470 int file_write(File *fd, unsigned char *str, int len)
    471 {
    472         FRESULT ret;
    473         UINT bw;
    474 
    475         ret = f_write(&fd->fp, str, len, &bw);
    476         if (ret != FR_OK)
    477                 return -1;
    478 
    479         return bw;
    480 }
    481 
    482 void file_close(File *fd)
    483 {
    484         f_close(&fd->fp);
    485 }
    486 
    487 int file_size(File *fd)
    488 {
    489         return f_size(&fd->fp);
    490 }
    491 
    492 int file_position(File *fd)
    493 {
    494         return fd->fp.fptr;
    495 }
    496 
    497 void file_flush(File *fd)
    498 {
    499         f_flush(&fd->fp);
    500 }
    501 
    502 bool file_seek(File *fd, int pos)
    503 {
    504         FRESULT ret;
    505 
    506         ret = f_lseek(&fd->fp, pos);
    507 
    508         return ret != FR_OK;
    509 }
    510 
    511 int file_read(File *fd)
    512 {
    513         FRESULT ret;
     466        ret = mkdir(path, 0777);
     467
     468        return ret == 0;
     469}
     470
     471int file_write(File *fp, unsigned char *str, int len)
     472{
     473        int ret;
     474
     475        ret = write(fp->fd, str, len);
     476        if (ret < 0)
     477                return -1;
     478
     479        return ret;
     480}
     481
     482void file_close(File *fp)
     483{
     484        close(fp->fd);
     485}
     486
     487int file_size(File *fp)
     488{
     489        int ret;
     490        struct stat fno;
     491
     492        ret = fstat(fp->fd, &fno);
     493        if (ret < 0)
     494                return -1;
     495
     496        return fno.st_size;
     497}
     498
     499int file_position(File *fp)
     500{
     501        int ret;
     502        struct stat fno;
     503
     504        ret = lseek(fp->fd, 0, SEEK_CUR);
     505        if (ret < 0)
     506                return -1;
     507
     508        return ret;
     509}
     510
     511void file_flush(File *fp)
     512{
     513        fsync(fp->fd);
     514}
     515
     516bool file_seek(File *fp, int pos)
     517{
     518        int ret;
     519
     520        ret = lseek(fp->fd, pos, SEEK_SET);
     521
     522        return ret == 0;
     523}
     524
     525int file_read(File *fp)
     526{
     527        int ret;
    514528        char c;
    515         UINT br;
    516 
    517         ret = f_read(&fd->fp, &c, 1, &br);
    518         if (ret != FR_OK)
     529
     530        ret = read(fp->fd, &c, 1);
     531        if (ret < 0)
    519532                return -1;
    520533
     
    524537void system_reboot(int mode)
    525538{
    526 
     539        exit(mode);
    527540}
    528541
     
    531544        return 0;
    532545}
    533 
    534 DWORD get_fattime(void)
    535 {
    536         time_t temp;
    537         struct tm _tm;
    538 
    539         time(&temp);
    540         gmtime_r(&temp, &_tm);
    541 
    542         return    ((DWORD)(_tm.tm_year - 1980) << 25)
    543                 | ((DWORD)_tm.tm_mon << 21)
    544                 | ((DWORD)_tm.tm_mday << 16)
    545                 | ((DWORD)_tm.tm_hour << 11)
    546                 | ((DWORD)_tm.tm_min << 5)
    547                 | ((DWORD)_tm.tm_sec >> 1);
    548 }
    549 
    550 void mrb_gr_peach_gem_init(mrb_state *mrb)
    551 {
    552 }
    553 
    554 void mrb_gr_peach_gem_final(mrb_state *mrb)
    555 {
    556 
    557 }
  • EcnlProtoTool/trunk/prototool/src/arduino.h

    r278 r279  
    3030#include <us_ticker_api.h>
    3131#include <wait_api.h>
    32 #include "diskio.h"
    33 #include "ff.h"
    3432
    3533#ifndef bool
     
    116114
    117115typedef struct File {
    118         FIL fp;
     116        int  fd;
    119117} File;
    120118
     
    130128int file_read(File *fd);
    131129
    132 typedef struct SD {
    133         DSTATUS dst;
    134         BYTE type;
    135         FATFS FatFs;
    136 } SD;
    137 
    138 extern SD Sd;
    139 
    140130bool SD_begin();
    141131bool SD_open(File *file, const char *path, int mode);
     
    147137
    148138typedef struct FILEEEP {
    149         FIL fp;
     139        int fd;
    150140} FILEEEP;
    151141
  • EcnlProtoTool/trunk/prototool/src/main.c

    r270 r279  
    11/*
    22 *  TOPPERS ECHONET Lite Communication Middleware
    3  * 
    4  *  Copyright (C) 2014-2017 Cores Co., Ltd. Japan
    5  * 
     3 *
     4 *  Copyright (C) 2014-2016 Cores Co., Ltd. Japan
     5 *
    66 *  上記著作権è€
    77は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
     
    4040 *      å
    4141è²¬ã™ã‚‹ã“と.
    42  * 
     42 *
    4343 *  本ソフトウェアは,無保証で提供されているものである.上記著作権è€
    4444お
     
    4747 *  アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
    4848 *  の責任を負わない.
    49  * 
     49 *
    5050 *  @(#) $Id$
    5151 */
    5252
    53 /*
    54  *  サンプルプログラム(1)の本体
    55  */
    56 
    57 #include <kernel.h>
    58 #include <t_syslog.h>
    59 #include <t_stdlib.h>
    60 #include <sil.h>
    61 #include <stdlib.h>
     53 /*
     54  *  サンプルプログラム(1)の本体
     55  */
     56
     57#include <sys/types.h>
     58#include <sys/socket.h>
     59#include <sys/un.h>
     60#include <netinet/in.h>
     61#include <netinet/tcp.h>
     62#include <arpa/inet.h>
     63#include <fcntl.h>
     64#include <netdb.h>
     65#include <unistd.h>
     66#include <setjmp.h>
     67#include <signal.h>
     68#include <time.h>
     69#include <sys/time.h>
     70#include <sys/unistd.h>
     71#include <errno.h>
     72
    6273#include <string.h>
    63 #include "syssvc/serial.h"
    64 #include "syssvc/syslog.h"
    65 #include "kernel_cfg.h"
    66 #include "main.h"
    67 #include "rza1.h"
    6874#include <mruby.h>
    6975#include <mruby/compile.h>
     
    7480#include <mruby/dump.h>
    7581#include <mruby/string.h>
    76 #include <tinet_config.h>
    77 #include <netinet/in.h>
    78 #include <netinet/in_itron.h>
    79 #include <tinet_nic_defs.h>
    80 #include <tinet_cfg.h>
    81 #include <netinet/in_var.h>
    82 #include <net/ethernet.h>
    83 #include <net/if6_var.h>
    84 #include <net/net.h>
    85 #include <net/if_var.h>
    86 #include <netinet/udp_var.h>
    87 #include <ethernet_api.h>
    88 #include "../webserver/httpd.h"
    89 #include "../webserver/httpd-fs.h"
    90 #include "../webserver/http-strings.h"
    91 #include "../webserver/base64.h"
    92 #include "../webserver/sha1.h"
    93 #include "mruby_arduino.h"
    94 #include "gpio_api.h"
    95 #include "arduino.h"
    96 #include "ff.h"
    97 
    98 #ifndef _MSC_VER
    99 void strcpy_s(char *dst, int size, const char *src);
    100 void strcat_s(char *dst, int size, const char *src);
    101 #endif
    102 
    103 uint8_t mac_addr[6] = {0x00, 0x30, 0x13, 0x06, 0x62, 0xC0};
    104 
    105 SYSTIM main_time;
    106 struct httpd_state *uploding;
    107 
    108 static void netif_link_callback(T_IFNET *ether);
    109 static void execute_ruby();
    110 
    111 /*  TCP 送受信ウィンドバッファ  */
    112 uint8_t tcp_swbuf1[TCP_SWBUF_SIZE];
    113 uint8_t tcp_rwbuf1[TCP_RWBUF_SIZE];
    114 uint8_t tcp_swbuf2[TCP_SWBUF_SIZE];
    115 uint8_t tcp_rwbuf2[TCP_RWBUF_SIZE];
    116 
    117 #define ISO_nl      0x0a
    118 #define ISO_space   0x20
    119 #define ISO_bang    0x21
    120 #define ISO_percent 0x25
    121 #define ISO_period  0x2e
    122 #define ISO_slash   0x2f
    123 #define ISO_colon   0x3a
    124 
    125 ID ecn_api_mailboxid;
    126 ID WEBSOCKET_MBXID;
    127 
    128 struct httpd_state httpd_state[2] = {
    129         {MAIN1_TASK, TCP_CEPID1},
    130         {MAIN2_TASK, TCP_CEPID2},
    131 };
    132 
    133 /*
    134 *  ネットワーク層の選択
    135 */
    136 
    137 #ifdef SUPPORT_INET6
    138 
    139 #define TCP_ACP_CEP(c,r,d,t)    tcp6_acp_cep(c,r,d,t)
    140 #define IP2STR(s,a)             ipv62str(s,a)
    141 
    142 #else   /* of #ifdef SUPPORT_INET6 */
    143 
    144 #ifdef SUPPORT_INET4
    145 
    146 #define TCP_ACP_CEP(c,r,d,t)    tcp_acp_cep(c,r,d,t)
    147 #define IP2STR(s,a)             ip2str(s,a)
    148 
    149 #endif  /* of #ifdef SUPPORT_INET4 */
    150 
    151 #endif  /* of #ifdef SUPPORT_INET6 */
    152 
    153 struct httpd_state *get_httpd(ID cepid)
    154 {
    155         for (int i = 0; i < 2; i++) {
    156                 if (httpd_state[i].cepid != cepid)
    157                         continue;
    158 
    159                 return &httpd_state[i];
    160         }
    161         return NULL;
    162 }
    163 
    164 struct websocket *websocket_getws(ID wbsid)
    165 {
    166         for (int i = 0; i < 2; i++) {
    167                 if (httpd_state[i].websocket.wbsid != wbsid)
    168                         continue;
    169 
    170                 return &httpd_state[i].websocket;
    171         }
    172         return NULL;
    173 }
    174 
    175 void send_file(struct httpd_state *s)
    176 {
    177         char *buf;
    178         int len, slen;
    179 
    180         while (s->file.len > 0) {
    181                 slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    182                 if (slen < 0) {
    183                         syslog(LOG_ERROR, "send_file#tcp_get_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    184                         s->state = STATE_CLOSING;
    185                         break;
    186                 }
    187                 if (slen == 0)
    188                         return;
    189 
    190                 len = s->file.len;
    191                 if (len > slen)
    192                         len = slen;
    193 
    194                 len = httpd_fs_read(&s->file, buf, len);
    195                 if (len <= 0) {
    196                         syslog(LOG_ERROR, "send_file#httpd_fs_read(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    197                         break;
    198                 }
    199 
    200                 s->file.len -= len;
    201                 s->file.pos += len;
    202 
    203                 if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
    204                         syslog(LOG_ERROR, "send_file#tcp_snd_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    205                         s->state = STATE_CLOSING;
    206                         break;
    207                 }
    208         }
    209 
    210         syslog(LOG_NOTICE, "close:     %s.%d %s", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    211         httpd_fs_close(&s->file);
    212         s->file.len = 0;
    213         s->file.pos = 0;
    214 
    215         s->out.state = OUT_STATE_SEND_END;
    216 }
    217 
    218 void send_data(struct httpd_state *s)
    219 {
    220         char *buf;
    221         int len, slen;
    222 
    223         while (s->response_len > 0) {
    224                 slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    225                 if (slen < 0) {
    226                         syslog(LOG_ERROR, "send_data#tcp_get_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    227                         s->state = STATE_CLOSING;
    228                         break;
    229                 }
    230                 if (slen == 0)
    231                         return;
    232 
    233                 len = s->response_len;
    234                 if (len > slen)
    235                         len = slen;
    236 
    237                 memcpy(buf, &s->response_body[s->response_pos], len);
    238 
    239                 s->response_len -= len;
    240                 s->response_pos += len;
    241 
    242                 if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
    243                         syslog(LOG_ERROR, "send_data#tcp_snd_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    244                         s->state = STATE_CLOSING;
    245                         break;
    246                 }
    247         }
    248 
    249         s->response_body = NULL;
    250         s->response_len = 0;
    251         s->response_pos = 0;
    252 
    253         s->out.state = OUT_STATE_SEND_END;
    254 }
    255 
    256 void send_headers(struct httpd_state *s, const char *statushdr)
    257 {
    258         int len;
    259         char *ptr;
    260 
    261         len = strlen(statushdr);
    262         tcp_snd_dat(s->cepid, (void *)statushdr, len, TMO_FEVR);
    263 
    264         if ((s->filename[0] == '0') && (s->file.len > 0)) {
    265                 len = sizeof(http_content_encoding_gzip) - 1;
    266                 tcp_snd_dat(s->cepid, (void *)http_content_encoding_gzip, len, TMO_FEVR);
    267         }
    268 
    269         if (s->file.redirect) {
    270                 len = sizeof(http_location) - 1;
    271                 tcp_snd_dat(s->cepid, (void *)http_location, len, TMO_FEVR);
    272                 if (s->filename[0] == '1') {
    273                         len = 2;
    274                         tcp_snd_dat(s->cepid, "/~", len, TMO_FEVR);
    275                 }
    276                 len = strlen(s->filename);
    277                 tcp_snd_dat(s->cepid, s->filename, len, TMO_FEVR);
    278                 if (s->query != NULL) {
    279                         tcp_snd_dat(s->cepid, "?", 1, TMO_FEVR);
    280                         len = strlen(s->query);
    281                         tcp_snd_dat(s->cepid, s->query, len, TMO_FEVR);
    282                 }
    283                 len = 2;
    284                 tcp_snd_dat(s->cepid, "\r", len, TMO_FEVR);
    285         }
    286 
    287         ptr = strrchr(s->filename, ISO_period);
    288         if (ptr == NULL) {
    289                 len = sizeof(http_content_type_binary) - 1;
    290                 tcp_snd_dat(s->cepid, (void *)http_content_type_binary, len, TMO_FEVR);
    291         }
    292         else if (strncmp(http_html, ptr, sizeof(http_html) - 1) == 0 ||
    293                 strncmp(http_htm, ptr, sizeof(http_htm) - 1) == 0) {
    294                 len = sizeof(http_content_type_html) - 1;
    295                 tcp_snd_dat(s->cepid, (void *)http_content_type_html, len, TMO_FEVR);
    296         }
    297         else if (strncmp(http_css, ptr, sizeof(http_css) - 1) == 0) {
    298                 len = sizeof(http_content_type_css) - 1;
    299                 tcp_snd_dat(s->cepid, (void *)http_content_type_css, len, TMO_FEVR);
    300         }
    301         else if (strncmp(http_js, ptr, sizeof(http_js) - 1) == 0) {
    302                 len = sizeof(http_content_type_js) - 1;
    303                 tcp_snd_dat(s->cepid, (void *)http_content_type_js, len, TMO_FEVR);
    304         }
    305         else if (strncmp(http_json, ptr, sizeof(http_json) - 1) == 0) {
    306                 len = sizeof(http_content_type_json) - 1;
    307                 tcp_snd_dat(s->cepid, (void *)http_content_type_json, len, TMO_FEVR);
    308         }
    309         else if (strncmp(http_png, ptr, sizeof(http_png) - 1) == 0) {
    310                 len = sizeof(http_content_type_png) - 1;
    311                 tcp_snd_dat(s->cepid, (void *)http_content_type_png, len, TMO_FEVR);
    312         }
    313         else if (strncmp(http_gif, ptr, sizeof(http_gif) - 1) == 0) {
    314                 len = sizeof(http_content_type_gif) - 1;
    315                 tcp_snd_dat(s->cepid, (void *)http_content_type_gif, len, TMO_FEVR);
    316         }
    317         else if (strncmp(http_jpg, ptr, sizeof(http_jpg) - 1) == 0) {
    318                 len = sizeof(http_content_type_jpg) - 1;
    319                 tcp_snd_dat(s->cepid, (void *)http_content_type_jpg, len, TMO_FEVR);
    320         }
    321         else if (strncmp(http_svg, ptr, sizeof(http_svg) - 1) == 0) {
    322                 len = sizeof(http_content_type_svg) - 1;
    323                 tcp_snd_dat(s->cepid, (void *)http_content_type_svg, len, TMO_FEVR);
    324         }
    325         else if (strncmp(http_text, ptr, sizeof(http_text) - 1) == 0) {
    326                 len = sizeof(http_content_type_text) - 1;
    327                 tcp_snd_dat(s->cepid, (void *)http_content_type_text, len, TMO_FEVR);
    328         }
    329         else if (strncmp(http_eot, ptr, sizeof(http_eot) - 1) == 0) {
    330                 len = sizeof(http_content_type_eot) - 1;
    331                 tcp_snd_dat(s->cepid, (void *)http_content_type_eot, len, TMO_FEVR);
    332         }
    333         else if (strncmp(http_ttf, ptr, sizeof(http_ttf) - 1) == 0) {
    334                 len = sizeof(http_content_type_ttf) - 1;
    335                 tcp_snd_dat(s->cepid, (void *)http_content_type_ttf, len, TMO_FEVR);
    336         }
    337         else if (strncmp(http_woff, ptr, sizeof(http_woff) - 1) == 0) {
    338                 len = sizeof(http_content_type_woff) - 1;
    339                 tcp_snd_dat(s->cepid, (void *)http_content_type_woff, len, TMO_FEVR);
    340         }
    341         else if (strncmp(http_woff2, ptr, sizeof(http_woff2) - 1) == 0) {
    342                 len = sizeof(http_content_type_woff2) - 1;
    343                 tcp_snd_dat(s->cepid, (void *)http_content_type_woff2, len, TMO_FEVR);
    344         }
    345         else if (strncmp(http_ico, ptr, sizeof(http_ico) - 1) == 0) {
    346                 len = sizeof(http_content_type_ico) - 1;
    347                 tcp_snd_dat(s->cepid, (void *)http_content_type_ico, len, TMO_FEVR);
    348         }
    349         else {
    350                 len = sizeof(http_content_type_plain) - 1;
    351                 tcp_snd_dat(s->cepid, (void *)http_content_type_plain, len, TMO_FEVR);
    352         }
    353 
    354         if (s->file.len > 0) {
    355                 len = sizeof(http_content_length) - 1;
    356                 tcp_snd_dat(s->cepid, (void *)http_content_length, len, TMO_FEVR);
    357                 sprintf(s->temp, "%d\r\n", s->file.len);
    358                 tcp_snd_dat(s->cepid, (void *)s->temp, strlen(s->temp), TMO_FEVR);
    359         }
    360 
    361         if (s->message.should_keep_alive && s->reset == 0) {
    362                 len = sizeof(http_connection_keep_alive) - 1;
    363                 tcp_snd_dat(s->cepid, (void *)http_connection_keep_alive, len, TMO_FEVR);
    364         }
    365         else {
    366                 len = sizeof(http_connection_close) - 1;
    367                 tcp_snd_dat(s->cepid, (void *)http_connection_close, len, TMO_FEVR);
    368         }
    369 
    370         tcp_snd_dat(s->cepid, (void *)http_crnl, 2, TMO_FEVR);
    371 
    372         if (s->filename != NULL) {
    373                 s->out.state = OUT_STATE_SEND_FILE;
    374         }
    375         else {
    376                 s->out.state = OUT_STATE_SEND_DATA;
    377         }
    378 }
    379 
    380 void handle_output(struct httpd_state *s)
    381 {
    382         s->out.wait = false;
    383 
    384         switch (s->out.state) {
    385         case OUT_STATE_WAIT_REQUEST:
    386                 s->out.wait = true;
    387                 break;
    388         case OUT_STATE_OPEN_GET_FILE:
    389                 syslog(LOG_NOTICE, "open:      %s.%d %s", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    390                 if (!httpd_fs_open(s->filename, sizeof(s->message.request_url), &s->file)) {
    391                         s->filename = NULL;
    392                         s->response_body = http_content_404;
    393                         s->response_pos = 0;
    394                         s->response_len = sizeof(http_content_403) - 1;
    395                         s->out.statushdr = http_header_404;
    396                 }
    397                 else {
    398                         s->out.statushdr = s->file.redirect ? http_header_301 : http_header_200;
    399                 }
    400                 s->out.state = OUT_STATE_SEND_HEADER;
    401                 break;
    402         case OUT_STATE_WAIT_POST_BODY:
    403                 s->out.wait = true;
    404                 break;
    405         case OUT_STATE_BODY_RECEIVED:
    406                 s->out.statushdr = http_header_200;
    407                 s->out.state = OUT_STATE_SEND_HEADER;
    408                 break;
    409         case OUT_STATE_SEND_HEADER:
    410                 send_headers(s, s->out.statushdr);
    411                 break;
    412         case OUT_STATE_SEND_FILE:
    413                 send_file(s);
    414                 break;
    415         case OUT_STATE_SEND_DATA:
    416                 send_data(s);
    417                 break;
    418         case OUT_STATE_SEND_END:
    419                 s->out.wait = true;
    420                 if (s->message.should_keep_alive && s->reset == 0) {
    421                         s->out.state = OUT_STATE_WAIT_REQUEST;
    422                 }
    423                 else {
    424                         s->state = STATE_CLOSING;
    425                 }
    426                 break;
    427         }
    428 }
    429 
    430 void send_ws_headers(struct httpd_state *s, const char *statushdr)
    431 {
    432         int len;
    433 
    434         len = strlen(statushdr);
    435         tcp_snd_dat(s->cepid, (void *)statushdr, len, TMO_FEVR);
    436 
    437         len = sizeof(http_upgrade) - 1;
    438         tcp_snd_dat(s->cepid, (void *)http_upgrade, len, TMO_FEVR);
    439         len = strlen(s->message.upgrade);
    440         tcp_snd_dat(s->cepid, s->message.upgrade, len, TMO_FEVR);
    441         len = sizeof(http_crnl) - 1;
    442         tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
    443 
    444         len = sizeof(http_connection) - 1;
    445         tcp_snd_dat(s->cepid, (void *)http_connection, len, TMO_FEVR);
    446         len = strlen(s->message.connection);
    447         tcp_snd_dat(s->cepid, s->message.connection, len, TMO_FEVR);
    448         len = sizeof(http_crnl) - 1;
    449         tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
    450 
    451         len = sizeof(http_sec_websocket_accept) - 1;
    452         tcp_snd_dat(s->cepid, (void *)http_sec_websocket_accept, len, TMO_FEVR);
    453         len = strlen(s->message.response_key);
    454         tcp_snd_dat(s->cepid, s->message.response_key, len, TMO_FEVR);
    455         len = sizeof(http_crnl) - 1;
    456         tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
    457 
    458         len = sizeof(http_sec_websocket_protocol) - 1;
    459         tcp_snd_dat(s->cepid, (void *)http_sec_websocket_protocol, len, TMO_FEVR);
    460         len = strlen(s->message.sec_websocket_protocol);
    461         tcp_snd_dat(s->cepid, s->message.sec_websocket_protocol, len, TMO_FEVR);
    462         len = sizeof(http_crnl) - 1;
    463         tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
    464 
    465         len = sizeof(http_crnl) - 1;
    466         tcp_snd_dat(s->cepid, (void *)http_crnl, len, TMO_FEVR);
    467 }
    468 
    469 void send_ws_data(struct httpd_state *s)
    470 {
    471         char *buf;
    472         int slen;
    473 
    474         slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    475         if (slen < 0) {
    476                 syslog(LOG_ERROR, "send_ws_data#tcp_get_buf(%s.%d) => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    477                 return;
    478         }
    479 
    480         websocket_output(&s->websocket, buf, slen);
    481 }
    482 
    483 void handle_ws_output(struct httpd_state *s)
    484 {
    485         char shaHash[20];
    486         SHA_CTX sha1;
    487         int len;
    488 
    489         strlncat(s->message.response_key, sizeof(s->message.response_key),
    490                 s->message.sec_websocket_key, sizeof(s->message.sec_websocket_key));
    491         len = strlncat(s->message.response_key, sizeof(s->message.response_key),
    492                 http_websocket_guid, sizeof(http_websocket_guid));
    493         memset(shaHash, 0, sizeof(shaHash));
    494         SHA1_Init(&sha1);
    495         SHA1_Update(&sha1, (sha1_byte *)s->message.response_key, len);
    496         SHA1_Final((sha1_byte *)shaHash, &sha1);
    497         base64_encode((unsigned char *)s->message.response_key,
    498                 sizeof(s->message.response_key), (unsigned char *)shaHash, sizeof(shaHash));
    499 
    500         send_ws_headers(s, http_header_101);
    501 
    502         s->message.response_key[0] = '\0';
    503 
    504         do {
    505                 while (!websocket_newdata(&s->websocket))
    506                         slp_tsk();
    507 
    508                 send_ws_data(s);
    509         } while ((s->state == STATE_CONNECTED) && (!s->close_req));
    510         s->state = STATE_DISCONNECTED;
    511         websocket_destroy(&s->websocket);
    512         s->close_req = 0;
    513 
    514         s->state = STATE_CLOSING;
    515 }
    516 
    517 void handle_input(struct httpd_state *s)
    518 {
    519         size_t done;
    520         int len;
    521 
    522         s->in.wait = false;
    523 
    524         switch (s->in.state) {
    525         case IN_STATE_START:
    526                 http_parser_init(&s->parser, HTTP_REQUEST);
    527                 s->in.state = IN_STATE_REQUEST;
    528                 break;
    529         case IN_STATE_REQUEST:
    530         case IN_STATE_RESPONSE:
    531         case IN_STATE_UPLOAD:
    532                 if ((len = tcp_rcv_buf(s->cepid, (void **)&s->in.data, TMO_POL)) <= 0) {
    533                         if ((len == E_TMOUT) || (len == 0)) {
    534                                 // 3秒はå¾
    535 ã¤
    536                                 if (main_time - s->in.timer < 30000000) {
    537                                         s->in.wait = true;
    538                                         break;
    539                                 }
    540                         }
    541                         syslog(LOG_ERROR, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    542                         uploding = NULL;
    543                         s->state = STATE_CLOSING;
    544                         return;
    545                 }
    546                 done = http_parser_execute(&s->parser, &websvr_settings, s->in.data, len);
    547                 tcp_rel_buf(s->cepid, done);
    548                 if (s->parser.http_errno != HPE_OK) {
    549                         syslog(LOG_ERROR, "http_parser error %s.%d => %d", s->addr, ((T_IPV4EP *)s->dst)->portno, s->parser.http_errno);
    550                         uploding = NULL;
    551                         s->state = STATE_CLOSING;
    552                         return;
    553                 }
    554 
    555                 s->parse_pos = done;
    556                 s->parse_len = len - done;
    557                 break;
    558         case IN_STATE_UPLOAD_WAIT:
    559                 if (uploding != NULL) {
    560                         s->in.wait = true;
    561                 }
    562                 else {
    563                         uploding = s;
    564                         s->in.state = IN_STATE_UPLOAD;
    565                 }
    566                 break;
    567         case IN_STATE_WEBSOCKET:
    568                 if (s->parse_len <= 0) {
    569                         if ((len = tcp_rcv_buf(s->cepid, (void **)&s->in.data, TMO_POL)) <= 0) {
    570                                 if ((len == E_TMOUT) || (len == 0)) {
    571                                         s->in.wait = true;
    572                                         break;
    573                                 }
    574                                 syslog(LOG_ERROR, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    575                                 s->state = STATE_CLOSING;
    576                                 break;
    577                         }
    578 
    579                         s->parse_pos = 0;
    580                         s->parse_len = len;
    581                 }
    582                 else
    583                         len = s->parse_len;
    584                 done = websocket_input(&s->websocket, (void *)s->in.data, s->parse_len);
    585                 tcp_rel_buf(s->cepid, done);
    586                 if ((done != 0) || (s->websocket.rstate.opecode == connection_close)) {
    587                         s->close_req = 1;
    588                         s->state = STATE_CLOSING;
    589                         break;
    590                 }
    591                 s->parse_pos = done;
    592                 s->parse_len -= done;
    593                 break;
    594         case IN_STATE_END:
    595                 s->in.wait = true;
    596                 break;
    597         default:
    598                 s->state = STATE_CLOSING;
    599                 break;
    600         }
    601 }
    602 
    603 /*
    604  *  ノンブロッキングコールのコールバック関数
    605  */
    606 ER
    607 callback_nblk_tcp(ID cepid, FN fncd, void *p_parblk)
    608 {
    609         struct httpd_state *s = get_httpd(cepid);
    610 
    611         if (s == NULL)
    612                 syslog(LOG_NOTICE, "callback_nblk_tcp(%d, %d)", fncd, cepid);
    613         else
    614                 syslog(LOG_NOTICE, "callback_nblk_tcp(%d, %s.%d)", fncd, s->addr, ((T_IPV4EP *)s->dst)->portno);
    615 
    616         return E_PAR;
    617 }
    618 
    619 /*
    620  * メインタスク
    621  */
    622 void main_task(intptr_t exinf)
    623 {
    624         ER ret, ret2;
    625         struct httpd_state *s = &httpd_state[exinf];
    626 
    627         if (exinf == 0) {
    628                 gpio_t led_blue, led_green, led_red, sw;
    629                 gpio_init_out(&led_blue, LED_BLUE);
    630                 gpio_init_out(&led_green, LED_GREEN);
    631                 gpio_init_out(&led_red, LED_RED);
    632                 gpio_init_in(&sw, USER_BUTTON0);
    633 
    634                 bool_t exec = gpio_read(&sw) == 1;
    635 
    636                 gpio_write(&led_blue, 1);
    637                 gpio_write(&led_green, exec ? 1 : 0);
    638                 gpio_write(&led_red, 0);
    639 
    640                 /* 初期化 */
    641                 if (mruby_arduino_init() == 0) {
    642                         gpio_write(&led_blue, 0);
    643                 }
    644                 else {
    645                         gpio_write(&led_blue, 0);
    646                         gpio_write(&led_red, 1);
    647                 }
    648 
    649                 gpio_write(&led_green, 0);
    650 
    651                 /* TINETが起動するまでå¾
    652 ã¤ */
    653                 ether_set_link_callback(netif_link_callback);
    654 
    655                 act_tsk(MAIN2_TASK);
    656 
    657                 if (exec) {
    658                         strcpy_s(RubyFilename, sizeof(RubyFilename), "1:/upload/main.mrb");
    659 
    660                         execute_ruby();
    661                 }
    662         }
    663 
    664         for (;;) {
    665                 ret2 = get_tim(&main_time);
    666                 if (ret2 != E_OK) {
    667                         syslog(LOG_ERROR, "get_tim");
    668                         return;
    669                 }
    670 
    671                 switch (s->state) {
    672                 case STATE_DISCONNECTED:
    673                         memset(&s->dst, 0, sizeof(s->dst));
    674                         if ((ret = TCP_ACP_CEP(s->cepid, TCP_REPID, (T_IPV4EP *)s->dst, TMO_FEVR)) != E_OK) {
    675                                 syslog(LOG_ERROR, "tcp_acp_cep(%d) => %d", s->cepid, ret);
    676                                 tslp_tsk(100000);       // TODO
    677                                 s->state = STATE_CLOSING;
    678                                 break;
    679                         }
    680                         IP2STR(s->addr, &((T_IPV4EP *)s->dst)->ipaddr);
    681                         syslog(LOG_NOTICE, "connected: %s.%d", s->addr, ((T_IPV4EP *)s->dst)->portno);
    682                         memset(&s->in, 0, sizeof(s->in));
    683                         memset(&s->out, 0, sizeof(s->out));
    684                         s->in.timer = main_time;
    685                         s->state = STATE_CONNECTED;
    686                         break;
    687                 case STATE_CONNECTED:
    688                         handle_input(s);
    689                         handle_output(s);
    690                         break;
    691                 case STATE_WEBSOCKET:
    692                         handle_input(s);
    693                         handle_ws_output(s);
    694                         break;
    695                 case STATE_CLOSING:
    696                         syslog(LOG_NOTICE, "close:     %s.%d", s->addr, ((T_IPV4EP *)s->dst)->portno);
    697                         tcp_sht_cep(s->cepid);
    698                         tcp_cls_cep(s->cepid, TMO_FEVR);
    699 
    700                         if (s->reset) {
    701                                 s->reset = 0;
    702                                 s->state = STATE_RESET;
    703                         }
    704                         else{
    705                                 s->state = STATE_DISCONNECTED;
    706                         }
    707                         break;
    708                 case STATE_RESET:
    709                         execute_ruby();
    710                         s->state = STATE_DISCONNECTED;
    711                         break;
    712                 }
    713 
    714                 if (s->in.wait && s->out.wait) {
    715                         tslp_tsk(100000);
    716                 }
    717         }
    718 }
    719 
    720 struct udp_msg {
    721         T_IPV4EP dst;
     82#include <usrcmd.h>
     83
     84#define ETHER_MAX_LEN 1518
     85#define IF_FLAG_UP              1
     86#define IF_FLAG_LINK_UP 2
     87#define MAKE_IPV4_ADDR(a,b,c,d)         htonl(((uint32_t)(a)<<24)|((uint32_t)(b)<<16)|((uint32_t)(c)<<8)|(d))
     88
     89struct udp_msg
     90{
     91        struct sockaddr_in dst;
    72292        int len;
    72393        uint8_t buffer[ETHER_MAX_LEN];
    72494};
    72595
    726 SYSTIM mruby_time;
     96typedef struct
     97{
     98        cmd_table_t *table;
     99        cmd_table_t *count;
     100} cmd_table_info_t;
     101
     102extern int mrbc_main(int argc, char **argv);
     103extern int mrdb_main(int argc, char **argv);
     104extern int mruby_main(int argc, char **argv);
     105extern int mirb_main(int argc, char **argv);
     106extern int tcc_main(int argc, char **argv);
     107extern int vi_main(int argc, char **argv);
     108extern int onitest_main(int argc, char **argv);
     109extern int tcp_echo_main(int argc, char **argv);
     110extern int echo_client_main(int argc, char **argv);
     111extern int mrdb_break(void);
     112
     113static const cmd_table_t cmdlist[] = {
     114        {"mrbc", "mruby compiler executable", mrbc_main},
     115        {"mrdb","mruby debugger command", mrdb_main},
     116        {"mruby","mruby command", mruby_main},
     117        {"mirb", "Embeddable Interactive Ruby Shell", mirb_main},
     118        {"tcc", "Tiny C compiler", tcc_main},
     119        {"vi", "Text editor", vi_main},
     120        {"cd", "change directory", usrcmd_cd },
     121        {"ls", "list files", usrcmd_ls },
     122        {"cp", "copy file", usrcmd_cp },
     123        {"rm", "remove file", usrcmd_rm },
     124        {"mv", "move file", usrcmd_mv },
     125        {"mkdir", "Make directory", usrcmd_mkdir},
     126        {"hexdump", "Hex dump", usrcmd_hexdump},
     127        {"onitest", "Onigumo Test", onitest_main},
     128        {"tcp_echo", "TCP echo server/client", tcp_echo_main},
     129        {"help", "This is a description text string for help command.", usrcmd_help},
     130        {"info", "This is a description text string for info command.", usrcmd_info},
     131        {"exit", "Exit Natural Tyny Shell", usrcmd_exit},
     132};
     133cmd_table_info_t cmd_table_info = { &cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
     134
     135int echonet = 1;
     136struct timeval main_time;
    727137struct RClass *_module_target_board;
    728 
    729 extern const uint8_t main_rb_code[];
    730 uint8_t RubyCode[16 * 1024];
    731 
    732 /* MACアドレスの設定時に呼ばれる */
    733 void mbed_mac_address(char *mac)
    734 {
    735         memcpy(mac, mac_addr, 6);
    736 }
    737 
    738 /*
    739  *  mruby実行タスク
    740  */
    741 void mruby_task(intptr_t exinf)
    742 {
    743         ER ret;
     138int sock;
     139
     140int main(int argc, char **argv)
     141{
     142        if (argc == 0) {
     143                return 0;
     144        }
     145        const cmd_table_t *p = cmd_table_info.table;
     146        for (int i = 0; i < cmd_table_info.count; i++) {
     147                if (strcmp((const char *)argv[0], p->cmd) == 0) {
     148                        return p->func(argc, argv);
     149                }
     150                p++;
     151        }
     152        printf("Unknown command found.\n");
     153        return 0;
     154}
     155
     156int run_mruby_code(int argc, char **argv, const uint8_t *code, const char *cmdline)
     157{
     158        mrb_state *mrb;
    744159        struct RProc* n;
    745160        struct mrb_irep *irep;
    746         mrb_state *mrb;
    747 
    748         syslog(LOG_NOTICE, "mruby_task");
     161        mrb_value ARGV;
     162        mrbc_context *c;
     163        mrb_value v;
     164        mrb_sym zero_sym;
     165
     166        echonet = 0;
    749167
    750168        /* mrubyの初期化 */
    751169        mrb = mrb_open();
    752170        if (mrb == NULL)
    753                 abort();
    754 
    755         ret = get_tim(&mruby_time);
    756         if (ret != E_OK) {
    757                 syslog(LOG_ERROR, "get_tim => %d", ret);
    758                 return;
    759         }
    760 
    761         irep = mrb_read_irep(mrb, RubyCode);
     171                return -1;
     172
     173        int ai = mrb_gc_arena_save(mrb);
     174        ARGV = mrb_ary_new_capa(mrb, argc);
     175        for (int i = 0; i < argc; i++) {
     176                mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i]));
     177        }
     178        mrb_define_global_const(mrb, "ARGV", ARGV);
     179
     180        c = mrbc_context_new(mrb);
     181        c->dump_result = TRUE;
     182
     183        /* Set $0 */
     184        zero_sym = mrb_intern_lit(mrb, "$0");
     185        mrbc_filename(mrb, c, cmdline);
     186        mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
     187
     188        irep = mrb_read_irep(mrb, code);
    762189        n = mrb_proc_new(mrb, irep);
    763190        mrb_run(mrb, n, mrb_nil_value());
    764191
     192        mrb_gc_arena_restore(mrb, ai);
     193        mrbc_context_free(mrb, c);
     194        if (mrb->exc) {
     195                if (!mrb_undef_p(v)) {
     196                        mrb_print_error(mrb);
     197                }
     198                n = -1;
     199        }
     200
    765201        mrb_close(mrb);
    766 }
    767 
    768 static void execute_ruby()
    769 {
    770         FIL fd;
    771         FRESULT res;
    772         ER ret;
    773 
    774         syslog(LOG_NOTICE, "execute_ruby");
    775 
    776         ret = ter_tsk(MRUBY_TASK);
    777         if ((ret != E_OK) && (ret != E_OBJ)) {
    778                 syslog(LOG_ERROR, "ter_tsk => %d", ret);
    779         }
    780 
    781         tslp_tsk(100000);
    782 
    783         reset_heap();
    784 
    785         strcpy_s(ExeFilename, sizeof(ExeFilename), RubyFilename);
    786         RubyFilename[0] = '\0';
    787         ExeFilename[sizeof(ExeFilename) - 1] = '\0';
    788 
    789         if (ExeFilename[0] == '\0') {
     202        return 0;
     203}
     204
     205int tcp_echo_main(int argc, char **argv)
     206{
     207        extern const uint8_t echo_server_code[];
     208        extern const uint8_t echo_client_code[];
     209
     210        if (argc >= 2) {
     211                if (strcmp(argv[1], "-s") == 0) {
     212                        return run_mruby_code(argc - 2, &argv[2], echo_server_code, "echo_server");
     213                }
     214                else if (strcmp(argv[1], "-c") == 0) {
     215                        return run_mruby_code(argc - 2, &argv[2], echo_client_code, "echo_client");
     216                }
     217        }
     218
     219        printf("tcp_echo -s port\n");
     220        printf("tcp_echo -c ipaddr port\n");
     221
     222        return 0;
     223}
     224
     225int usrcmd_help(int argc, char **argv)
     226{
     227        const cmd_table_t *p = cmd_table_info.table;
     228        for (int i = 0; i < cmd_table_info.count; i++) {
     229                printf(p->cmd);
     230                printf("\t:");
     231                printf(p->desc);
     232                printf("\n");
     233                p++;
     234        }
     235        return 0;
     236}
     237
     238void sigusr1_handler(int sig)
     239{
     240        printf("signal called\n");
     241}
     242
     243void mrb_target_board_init()
     244{
     245        int ret;
     246        struct sockaddr_in ep;
     247        struct ip_mreq mreq;
     248
     249        signal(SIGUSR1, sigusr1_handler);
     250
     251        sock = socket(AF_INET, SOCK_DGRAM, 0);
     252
     253        memset(&ep, 0, sizeof(ep));
     254        ep.sin_family = AF_INET;
     255        ep.sin_port = htons(3610);
     256        ep.sin_addr.s_addr = INADDR_ANY;
     257        //ep.sin_addr.s_addr = MAKE_IPV4_ADDR(192,168,137,200);
     258
     259        ret = bind(sock, &ep, sizeof(ep));
     260        if (ret != 0) {
     261                printf("bind %d", ret);
    790262                return;
    791263        }
    792264
    793         syslog(LOG_NOTICE, "%s", ExeFilename);
    794 
    795         wai_sem(SEM_FILESYSTEM);
    796 
    797         if ((res = f_open(&fd, ExeFilename, FA_OPEN_EXISTING | FA_READ)) == FR_OK) {
    798                 if (fd.fsize < sizeof(RubyCode)) {
    799                         int len = fd.fsize;
    800                         for (int pos = 0; pos < fd.fsize; pos += len) {
    801                                 if (len > 1024)
    802                                         len = 1024;
    803 
    804                                 UINT rlen;
    805                                 if ((res = f_read(&fd, &RubyCode[pos], len, &rlen)) != FR_OK)
    806                                         break;
    807 
    808                                 // 他に使う人のために解放
    809                                 sig_sem(SEM_FILESYSTEM);
    810                                 wai_sem(SEM_FILESYSTEM);
    811                         }
    812                 }
    813                 f_close(&fd);
    814         }
    815 
    816         sig_sem(SEM_FILESYSTEM);
    817 
    818         if (res == FR_OK) {
    819                 ret = act_tsk(MRUBY_TASK);
    820                 if (ret != E_OK) {
    821                         syslog(LOG_ERROR, "act_tsk => %d", ret);
    822                 }
    823         }
    824 }
    825 
    826 ER callback_nblk_udp(ID cepid, FN fncd, void *p_parblk)
    827 {
    828         static struct udp_msg msg_inst[2];
    829         static int msg_no = 0;
    830         struct udp_msg *msg = &msg_inst[msg_no];
    831         ER      error = E_OK;
    832 
    833         switch (fncd) {
    834         case TFN_UDP_CRE_CEP:
    835         case TFN_UDP_RCV_DAT:
    836                 /* ECN_CAP_PUT("[UDP ECHO SRV] callback_nblk_udp() recv: %u", *(int *)p_parblk); */
    837                 memset(msg, 0, sizeof(struct udp_msg));
    838                 if ((msg->len = udp_rcv_dat(cepid, &msg->dst, msg->buffer, sizeof(msg->buffer), 0)) < 0) {
    839                         syslog(LOG_WARNING, "[UDP ECHO SRV] recv, error: %s", itron_strerror(msg->len));
    840                         return msg->len;
    841                 }
    842                 msg_no = (msg_no + 1) % 2;
    843                 return snd_dtq(MRUBY_DATAQUEUE, (intptr_t)msg);
    844 
    845         case TFN_UDP_SND_DAT:
    846                 break;
    847         default:
    848                 syslog(LOG_WARNING, "[UDP ECHO SRV] fncd:0x%04X(%s)", -fncd,
    849                         (fncd == TFN_UDP_CRE_CEP ? "TFN_UDP_CRE_CEP" :
    850                         (fncd == TFN_UDP_RCV_DAT ? "TFN_UDP_RCV_DAT" :
    851                                 (fncd == TFN_UDP_SND_DAT ? "TFN_UDP_SND_DAT" : "undef"))));
    852 
    853                 error = E_PAR;
    854                 break;
    855         }
    856         return error;
    857 }
    858 
    859 static void netif_link_callback(T_IFNET *ether)
    860 {
    861         static struct udp_msg msg_inst[2];
    862         static int msg_no = 0;
    863         struct udp_msg *msg = &msg_inst[msg_no];
    864         T_RTSK rtsk;
    865         ER ret;
    866 
    867         ret = ref_tsk(MRUBY_TASK, &rtsk);
    868         if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
     265        mreq.imr_interface.s_addr = INADDR_ANY;
     266        mreq.imr_multiaddr.s_addr = MAKE_IPV4_ADDR(224, 0, 23, 0);
     267
     268        ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
     269        if (ret != 0) {
     270                printf("setsockopt %d", ret);
    869271                return;
    870 
    871         memset(msg, 0, sizeof(struct udp_msg));
    872 
    873         msg->len = 1;
    874         msg->buffer[0] = ether->flags;
    875 
    876         msg_no = (msg_no + 1) % 2;
    877         snd_dtq(MRUBY_DATAQUEUE, (intptr_t)msg);
     272        }
     273
     274        ret = gettimeofday(&main_time, NULL);
     275        if (ret != 0) {
     276                printf("gettimeofday");
     277                return;
     278        }
     279}
     280
     281void mrb_target_board_final()
     282{
     283        close(sock);
     284}
     285
     286void mrb_target_board_break()
     287{
     288        raise(SIGUSR1);
    878289}
    879290
     
    883294static mrb_value mrb_target_board_wait_msg(mrb_state *mrb, mrb_value self)
    884295{
    885         TMO timer;
    886         SYSTIM now;
    887         ER ret, ret2;
    888         struct udp_msg *msg;
     296        int tmr;
     297        struct timeval timer, *ptimer;
     298        struct timeval now, elps;
     299        int ret, ret2;
     300        struct udp_msg _msg;
     301        struct udp_msg *msg = &_msg;
    889302        mrb_value arv[3];
    890         static int first = 1;
    891 
    892         mrb_get_args(mrb, "i", &timer);
    893         if (timer != TMO_FEVR)
    894                 timer *= 1000;
    895 
    896         if (first) {
    897                 first = 0;
    898                 syslog(LOG_NOTICE, "wait_msg");
    899         }
     303        fd_set readfds, writefds, errorfds;
     304
     305        mrb_get_args(mrb, "i", &tmr);
     306
     307        if (tmr < 0)
     308                ptimer = NULL;
     309        else {
     310                timer.tv_sec = tmr / 1000;
     311                timer.tv_usec = (tmr % 1000) * 1000;
     312                ptimer = &timer;
     313        }
     314
     315        FD_ZERO(&readfds);
     316        FD_ZERO(&writefds);
     317        FD_ZERO(&errorfds);
     318        FD_SET(sock, &readfds);
     319        FD_SET(sock, &writefds);
     320        FD_SET(sock, &errorfds);
    900321
    901322        /* メッセージå¾
    902323ち */
    903         ret = trcv_dtq(MRUBY_DATAQUEUE, (intptr_t *)&msg, timer);
    904         if ((ret != E_OK) && (ret != E_TMOUT)) {
    905                 syslog(LOG_ERROR, "trcv_dtq => %d", ret);
     324        memset(msg, 0, sizeof(*msg));
     325        ret = select(sock + 1, &readfds, &writefds, &errorfds, ptimer);
     326        if (ret < 0)
     327                mrb_raise(mrb, E_RUNTIME_ERROR, "socket select error");
     328        if (FD_ISSET(sock, &readfds)) {
     329                int fromlen = sizeof(msg->dst);
     330                msg->len = recvfrom(sock, msg->buffer, sizeof(msg->buffer), 0, &msg->dst, &fromlen);
     331                if (msg->len < 0) {
     332                        printf("recvfrom %d", msg->len);
     333                        return mrb_nil_value();
     334                }
     335        }
     336
     337        ret2 = gettimeofday(&now, NULL);
     338        if (ret2 != 0) {
     339                printf("gettimeofday");
    906340                return mrb_nil_value();
    907341        }
    908342
    909         ret2 = get_tim(&now);
    910         if (ret2 != E_OK) {
    911                 syslog(LOG_ERROR, "get_tim => %d", ret);
    912                 return mrb_nil_value();
    913         }
    914 
    915         arv[0] = mrb_fixnum_value((now - mruby_time) / 1000);
    916         mruby_time = now;
     343        timersub(&now, &main_time, &elps);
     344        arv[0] = mrb_fixnum_value(elps.tv_sec);
     345        main_time = now;
    917346
    918347        /* タイムアウトの場合 */
    919         if (ret == E_TMOUT) {
     348        if (ret == 0) {
    920349                return mrb_ary_new_from_values(mrb, 1, arv);
    921350        }
     
    923352        /* å†
    924353部イベントの場合 */
    925         if (msg->dst.ipaddr == 0) {
     354        if (msg->dst.sin_addr.s_addr == 0) {
    926355                /* Ethernet Link up */
    927356                if (msg->buffer[0] & IF_FLAG_LINK_UP) {
     
    931360                else if (msg->buffer[0] & IF_FLAG_UP) {
    932361                        arv[1] = mrb_fixnum_value(2);
    933                 }
    934                 else {
    935                         arv[1] = mrb_fixnum_value(0);
    936362                }
    937363
     
    967393        mrb_value rep;
    968394        mrb_value rdat;
    969         T_IPV4EP *ep;
    970         ER_UINT ret;
     395        struct sockaddr_in *ep;
     396        int ret;
    971397
    972398        mrb_get_args(mrb, "SS", &rep, &rdat);
    973399
    974         if (RSTRING_LEN(rep) != sizeof(T_IPV4EP)) {
     400        if (RSTRING_LEN(rep) != sizeof(struct sockaddr_in)) {
    975401                mrb_raise(mrb, E_RUNTIME_ERROR, "snd_msg");
    976402                return mrb_nil_value();
    977403        }
    978404
    979         ep = (T_IPV4EP *)RSTRING_PTR(rep);
    980 
    981         ret = udp_snd_dat(MRUBY_ECNL_UDP_CEPID, ep, RSTRING_PTR(rdat), RSTRING_LEN(rdat), TMO_FEVR);
     405        ep = (struct sockaddr_in *)RSTRING_PTR(rep);
     406
     407        ret = sendto(sock, RSTRING_PTR(rdat), RSTRING_LEN(rdat), 0, (struct sockaddr *)ep, sizeof(*ep));
    982408        if (ret < 0) {
    983                 mrb_raise(mrb, E_RUNTIME_ERROR, "snd_msg");
     409                mrb_raise(mrb, E_RUNTIME_ERROR, "sendto");
    984410                return mrb_nil_value();
    985411        }
     
    994420{
    995421        mrb_value rep;
    996         T_IPV4EP *ep;
     422        struct sockaddr_in *ep;
    997423
    998424        mrb_get_args(mrb, "S", &rep);
    999425
    1000         if (RSTRING_LEN(rep) < sizeof(T_IPV4EP)) {
     426        if (RSTRING_LEN(rep) < sizeof(struct sockaddr_in)) {
    1001427                mrb_raise(mrb, E_RUNTIME_ERROR, "is_local_addr");
    1002428                return mrb_nil_value();
    1003429        }
    1004430
    1005         ep = (T_IPV4EP *)RSTRING_PTR(rep);
    1006 
    1007         return (ep->ipaddr == MAKE_IPV4_ADDR(127, 0, 0, 1)) ? mrb_true_value() : mrb_false_value();
     431        ep = (struct sockaddr_in *)RSTRING_PTR(rep);
     432
     433        return (ep->sin_addr.s_addr == MAKE_IPV4_ADDR(127, 0, 0, 1)) ? mrb_true_value() : mrb_false_value();
    1008434}
    1009435
     
    1014440{
    1015441        mrb_value rep;
    1016         T_IPV4EP *ep;
     442        struct sockaddr_in *ep;
    1017443
    1018444        mrb_get_args(mrb, "S", &rep);
    1019445
    1020         if (RSTRING_LEN(rep) < sizeof(T_IPV4EP)) {
     446        if (RSTRING_LEN(rep) < sizeof(struct sockaddr_in)) {
    1021447                mrb_raise(mrb, E_RUNTIME_ERROR, "is_multicast_addr");
    1022448                return mrb_nil_value();
    1023449        }
    1024450
    1025         ep = (T_IPV4EP *)RSTRING_PTR(rep);
    1026 
    1027         return (ep->ipaddr == MAKE_IPV4_ADDR(224, 0, 23, 0)) ? mrb_true_value() : mrb_false_value();
     451        ep = (struct sockaddr_in *)RSTRING_PTR(rep);
     452
     453        return (ep->sin_addr.s_addr == MAKE_IPV4_ADDR(224, 0, 23, 0)) ? mrb_true_value() : mrb_false_value();
    1028454}
    1029455
     
    1034460{
    1035461        mrb_value rep1, rep2;
    1036         T_IPV4EP *ep1, *ep2;
     462        struct sockaddr_in *ep1, *ep2;
    1037463
    1038464        mrb_get_args(mrb, "SS", &rep1, &rep2);
    1039465
    1040         if ((RSTRING_LEN(rep1) != sizeof(T_IPV4EP)) || (RSTRING_LEN(rep2) != sizeof(T_IPV4EP))) {
     466        if ((RSTRING_LEN(rep1) != sizeof(struct sockaddr_in)) || (RSTRING_LEN(rep2) != sizeof(struct sockaddr_in))) {
    1041467                mrb_raise(mrb, E_RUNTIME_ERROR, "equals_addr");
    1042468                return mrb_nil_value();
    1043469        }
    1044470
    1045         ep1 = (T_IPV4EP *)RSTRING_PTR(rep1);
    1046         ep2 = (T_IPV4EP *)RSTRING_PTR(rep2);
    1047 
    1048         return (ep1->ipaddr == ep2->ipaddr) ? mrb_true_value() : mrb_false_value();
     471        ep1 = (struct sockaddr_in *)RSTRING_PTR(rep1);
     472        ep2 = (struct sockaddr_in *)RSTRING_PTR(rep2);
     473
     474        return (ep1->sin_addr.s_addr == ep2->sin_addr.s_addr) ? mrb_true_value() : mrb_false_value();
    1049475}
    1050476
     
    1054480static mrb_value mrb_target_board_get_local_addr(mrb_state *mrb, mrb_value self)
    1055481{
    1056         T_IPV4EP ep;
     482        struct sockaddr_in ep;
    1057483        mrb_value rep;
    1058484
    1059         ep.ipaddr = MAKE_IPV4_ADDR(127, 0, 0, 1);
    1060         ep.portno = 3610;
     485        memset(&ep, 0, sizeof(ep));
     486        ep.sin_family = AF_INET;
     487        ep.sin_addr.s_addr = MAKE_IPV4_ADDR(127, 0, 0, 1);
     488        ep.sin_port = htons(3610);
    1061489
    1062490        rep = mrb_str_new(mrb, (char *)&ep, sizeof(ep));
     
    1070498static mrb_value mrb_target_board_get_multicast_addr(mrb_state *mrb, mrb_value self)
    1071499{
    1072         T_IPV4EP ep;
     500        struct sockaddr_in ep;
    1073501        mrb_value rep;
    1074502
    1075         ep.ipaddr = MAKE_IPV4_ADDR(224, 0, 23, 0);
    1076         ep.portno = 3610;
     503        memset(&ep, 0, sizeof(ep));
     504        ep.sin_family = AF_INET;
     505        ep.sin_addr.s_addr = MAKE_IPV4_ADDR(224, 0, 23, 0);
     506        ep.sin_port = htons(3610);
    1077507
    1078508        rep = mrb_str_new(mrb, (char *)&ep, sizeof(ep));
     
    1083513void mrb_mruby_others_gem_init(mrb_state* mrb)
    1084514{
     515        if (!echonet)
     516                return;
     517
    1085518        _module_target_board = mrb_define_module(mrb, "TargetBoard");
    1086 
    1087         // mbed Pin Names
    1088         mrb_define_const(mrb, _module_target_board, "LED1", mrb_fixnum_value(LED1));
    1089         mrb_define_const(mrb, _module_target_board, "LED2", mrb_fixnum_value(LED2));
    1090         mrb_define_const(mrb, _module_target_board, "LED3", mrb_fixnum_value(LED3));
    1091         mrb_define_const(mrb, _module_target_board, "LED4", mrb_fixnum_value(LED4));
    1092 
    1093         mrb_define_const(mrb, _module_target_board, "LED_RED", mrb_fixnum_value(LED_RED));
    1094         mrb_define_const(mrb, _module_target_board, "LED_GREEN", mrb_fixnum_value(LED_GREEN));
    1095         mrb_define_const(mrb, _module_target_board, "LED_BLUE", mrb_fixnum_value(LED_BLUE));
    1096         mrb_define_const(mrb, _module_target_board, "LED_USER", mrb_fixnum_value(LED_USER));
    1097 
    1098         mrb_define_const(mrb, _module_target_board, "USBTX", mrb_fixnum_value(USBTX));
    1099         mrb_define_const(mrb, _module_target_board, "USBRX", mrb_fixnum_value(USBRX));
    1100 
    1101         // Arduiono Pin Names
    1102         mrb_define_const(mrb, _module_target_board, "D0", mrb_fixnum_value(D0));
    1103         mrb_define_const(mrb, _module_target_board, "D1", mrb_fixnum_value(D1));
    1104         mrb_define_const(mrb, _module_target_board, "D2", mrb_fixnum_value(D2));
    1105         mrb_define_const(mrb, _module_target_board, "D3", mrb_fixnum_value(D3));
    1106         mrb_define_const(mrb, _module_target_board, "D4", mrb_fixnum_value(D4));
    1107         mrb_define_const(mrb, _module_target_board, "D5", mrb_fixnum_value(D5));
    1108         mrb_define_const(mrb, _module_target_board, "D6", mrb_fixnum_value(D6));
    1109         mrb_define_const(mrb, _module_target_board, "D7", mrb_fixnum_value(D7));
    1110         mrb_define_const(mrb, _module_target_board, "D8", mrb_fixnum_value(D8));
    1111         mrb_define_const(mrb, _module_target_board, "D9", mrb_fixnum_value(D9));
    1112         mrb_define_const(mrb, _module_target_board, "D10", mrb_fixnum_value(D10));
    1113         mrb_define_const(mrb, _module_target_board, "D11", mrb_fixnum_value(D11));
    1114         mrb_define_const(mrb, _module_target_board, "D12", mrb_fixnum_value(D12));
    1115         mrb_define_const(mrb, _module_target_board, "D13", mrb_fixnum_value(D13));
    1116         mrb_define_const(mrb, _module_target_board, "D14", mrb_fixnum_value(D14));
    1117         mrb_define_const(mrb, _module_target_board, "D15", mrb_fixnum_value(D15));
    1118 
    1119         mrb_define_const(mrb, _module_target_board, "A0", mrb_fixnum_value(A0));
    1120         mrb_define_const(mrb, _module_target_board, "A1", mrb_fixnum_value(A1));
    1121         mrb_define_const(mrb, _module_target_board, "A2", mrb_fixnum_value(A2));
    1122         mrb_define_const(mrb, _module_target_board, "A3", mrb_fixnum_value(A3));
    1123         mrb_define_const(mrb, _module_target_board, "A4", mrb_fixnum_value(A4));
    1124         mrb_define_const(mrb, _module_target_board, "A5", mrb_fixnum_value(A5));
    1125 
    1126         mrb_define_const(mrb, _module_target_board, "I2C_SCL", mrb_fixnum_value(I2C_SCL));
    1127         mrb_define_const(mrb, _module_target_board, "I2C_SDA", mrb_fixnum_value(I2C_SDA));
    1128 
    1129         mrb_define_const(mrb, _module_target_board, "USER_BUTTON0", mrb_fixnum_value(USER_BUTTON0));
    1130 
    1131         // Not connected
    1132         mrb_define_const(mrb, _module_target_board, "NC", mrb_fixnum_value(NC));
    1133519
    1134520        mrb_define_class_method(mrb, _module_target_board, "wait_msg", mrb_target_board_wait_msg, MRB_ARGS_REQ(1));
     
    1140526        mrb_define_class_method(mrb, _module_target_board, "get_local_addr", mrb_target_board_get_local_addr, MRB_ARGS_NONE());
    1141527        mrb_define_class_method(mrb, _module_target_board, "get_multicast_addr", mrb_target_board_get_multicast_addr, MRB_ARGS_NONE());
     528
     529        mrb_target_board_init();
    1142530}
    1143531
    1144532void mrb_mruby_others_gem_final(mrb_state* mrb)
    1145533{
    1146 }
     534        if (!echonet)
     535                return;
     536
     537        mrb_target_board_final();
     538}
     539
     540// Provide implementation of _sbrk (low-level dynamic memory allocation
     541// routine) for GCC_ARM which compares new heap pointer with MSP instead of
     542// SP.  This make it compatible with RTX RTOS thread stacks.
     543
     544// Linker defined symbol used by _sbrk to indicate where heap should start.
     545int _end;
     546uint32_t  _stack;
     547
     548// Turn off the errno macro and use actual global variable instead.
     549#undef errno
     550int errno;
     551
     552static unsigned char* heap = (unsigned char*)&_end;
     553
     554// Dynamic memory allocation related syscall.
     555caddr_t _sbrk(int incr) {
     556        unsigned char*        prev_heap = heap;
     557        unsigned char*        new_heap = heap + incr;
     558
     559        if (new_heap >= (unsigned char*)&_stack) {     /* _stack is end of heap section */
     560                errno = ENOMEM;
     561                return (caddr_t)-1;
     562        }
     563
     564        heap = new_heap;
     565        return (caddr_t) prev_heap;
     566}
     567
     568char *optarg;
     569int _data, _mdata, _edata;
     570int _bss, _ebss;
     571
     572int _PowerON_Reset(int argc, char **argv)
     573{
     574        memcpy(&_data, &_mdata, (size_t)&_edata - (size_t)&_data);
     575        memset(&_bss, 0, (size_t)&_ebss - (size_t)&_bss);
     576
     577        optarg = *argv;
     578        return main(argc, argv);
     579}
     580
     581#define FVECT_SECT          __attribute__ ((section (".fvectors")))
     582const void *HardwareVectors[] FVECT_SECT = {
     583        _PowerON_Reset,
     584        mrdb_break,
     585};
     586
     587char stack_space[0x100000] __attribute__ ((section (".stack")));
Note: See TracChangeset for help on using the changeset viewer.