/***************************************************************************** ** Function name: U16 tcpSend (void) ** ** Descriptions: Envia dados TCP *****************************************************************************/ U16 tcpSend (char *p, U8 sz,U8 soc,U8 send) { U8 *sendbuf,ip[4]; switch (tcp_get_state (soc)) { case TCP_STATE_FREE: case TCP_STATE_CLOSED: if(send == CMD_SEND_TO_SERVER) { inet_aton((U8*)cfg.tcp.ip_serv_rem,ip); tcp_connect(soc,ip,atoi(cfg.tcp.port_serv_rem),atoi(cfg.tcp.port_serv_loc)); } break; case TCP_STATE_CONNECT: /* We are connected, send command to remote peer. */ if(tcp_check_send (soc)) { sendbuf = tcp_get_buf (sz); strncpy((char *)sendbuf,(const char *)p,sz); if(tcp_send (soc, sendbuf, sz)) return __TRUE; /*Packet Sent*/ else return __FALSE; /*Send Error*/ } break; } return __TRUE; }
int system_send_buf_to_host( char* buf, int buf_size ) { char* sendbuf; int stat = 0; if( g_send_package_socket != 0) { stat = tcp_get_state( g_send_package_socket ); switch ( stat ) { case TCP_STATE_FREE: case TCP_STATE_CLOSED: tcp_connect ( g_send_package_socket, g_send_package_socket_ip, g_send_package_socket_port, 0); #if 1 return -2; #else // wait connect // maybe need while( 1 ){ stat = tcp_get_state( g_send_package_socket ); timer_poll (); main_TcpNet (); if( stat == TCP_STATE_CONNECT ) break; } #endif break; } if( stat == TCP_STATE_CONNECT ) { while(tcp_check_send ( g_send_package_socket) != __TRUE) { timer_poll (); main_TcpNet (); } if (tcp_check_send ( g_send_package_socket) == __TRUE) { sendbuf = tcp_get_buf( buf_size ); memcpy( sendbuf, buf, buf_size ); tcp_send ( g_send_package_socket, sendbuf, buf_size); #if 1 return 1; #else while( 1 ){ stat = tcp_get_state( g_send_package_socket ); timer_poll (); main_TcpNet (); if( stat == TCP_STATE_CONNECT ) break; } #endif } } else{ return -3; } } else return -4; return -5; }
/** 文字列出力 * @param cnsl コンソール * @param sndbf 出力文字 * @param len 出力文字数 * @return 実際の出力文字数 ****************************************************************************** */ int TELNET_Write( CONSOLE *cnsl, const UB *sndbf, int len ) { int sblen; UB *sbuf; ER errcd = E_TMOUT; TELNET_PARA *para; int sndlen; int lenBk = len; para = (TELNET_PARA*)cnsl->Parameter; if( !para->OpenFg ) goto EXIT; while( len > 0 ) { // 送信バッファ取得 for( ;; ) { errcd = (int)tcp_get_buf( para->CepID, (VP *)&sbuf, TMO_FEVR ); /* 送信用バッファ獲得 */ if( errcd < 0 ) { //DBG_PRINT( DBG_DISPLAY_ERROR, ( "tcp_get_buf() Error: %s" LINE_END, itron_strerror( errcd ) )); goto EXIT; } sblen = errcd; if( sblen > 0 ) break; // 獲得 } // 送信 sndlen = ( len < sblen )? len: sblen; memcpy( sbuf, sndbf, sndlen ); errcd = tcp_snd_buf( para->CepID, sndlen ); /* バッファ内データの送信 */ if( errcd != E_OK ) { //DBG_PRINT( DBG_DISPLAY_ERROR, ( "tcp_snd_buf() Error: %s" LINE_END, itron_strerror( errcd ) )); goto EXIT; } sndbf += sndlen; len -= sndlen; } errcd = lenBk - len; EXIT:; return errcd; }
void put_char (ID cepid, char ch) { if (connected) { if (ch == '\n' && (net_ioctl & IOCTL_CRLF) != 0) put_char(cepid, '\r'); syscall(wai_sem(SEM_NET_SEND)); if (snd_off >= snd_len) { if (tcp_snd_buf(cepid, snd_off) != E_OK) { syscall(sig_sem(SEM_NET_SEND)); return; } snd_off = 0; if ((snd_len = tcp_get_buf(cepid, (VP*)&snd_buff, TMO_FEVR)) <= 0) { syscall(sig_sem(SEM_NET_SEND)); return; } } snd_buff[snd_off ++] = ch; syscall(sig_sem(SEM_NET_SEND)); } }
static ER tcp_echo_srv (ID cepid, ID repid) { ER_UINT rblen, sblen; SYSTIM now; ER error; uint32_t total; uint16_t rlen, slen, soff, scount, rcount; char *rbuf, *sbuf, head, tail; if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_FEVR)) != E_OK) { syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(error)); return error; } #ifdef USE_TCP_EXTENTIONS if ((error = free_tcp_rep(repid, true)) != E_OK) syslog(LOG_NOTICE, "[TES:%02d DEL] REP delete error: %s", cepid, itron_strerror(error)); #endif /* of #ifdef USE_TCP_EXTENTIONS */ scount = rcount = total = 0; syscall(get_tim(&now)); syslog(LOG_NOTICE, "[TES:%02d ACP] connected: %6lu, from: %s.%u", cepid, now / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno); while (true) { if ((rblen = tcp_rcv_buf(cepid, (void**)&rbuf, RCV_TMO)) <= 0) { if (rblen != E_OK) syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", cepid, itron_strerror(rblen)); break; } head = *rbuf; tail = *(rbuf + rblen - 1); rcount ++; //syslog(LOG_NOTICE, "[TES:%02d RCV] len: %4d", cepid, (uint16_t)rblen); #ifdef SHOW_RCV_RANGE syslog(LOG_NOTICE, "[TES:%02d RCV] rcount: %4d, len: %4d, data %02x -> %02x", cepid, rcount, (uint16_t)rblen, head, tail); #endif /* of #ifdef SHOW_RCV_RANGE */ rlen = (uint16_t)rblen; total += rblen; soff = 0; while (rlen > 0) { if ((sblen = tcp_get_buf(cepid, (void**)&sbuf, SND_TMO)) < 0) { syslog(LOG_NOTICE, "[TES:%02d GET] error: %s", cepid, itron_strerror(sblen)); goto err_fin; } //syslog(LOG_NOTICE, "[TES:%02d SND] len: %4d", cepid, (uint16_t)sblen); scount ++; slen = rlen < (uint16_t)sblen ? rlen : (uint16_t)sblen; memcpy(sbuf, rbuf + soff, slen); if ((error = tcp_snd_buf(cepid, slen)) != E_OK) { syslog(LOG_NOTICE, "[TES:%02d SND] error: %s", cepid, itron_strerror(error)); goto err_fin; } #ifdef SHOW_RCV_RANGE syslog(LOG_NOTICE, "[TES:%02d SND] scount: %4d, len: %4d", cepid, scount, slen); #endif /* of #ifdef SHOW_RCV_RANGE */ rlen -= slen; soff += slen; } if ((error = tcp_rel_buf(cepid, rblen)) < 0) { syslog(LOG_NOTICE, "[TES:%02d REL] error: %s", cepid, itron_strerror(error)); break; } } err_fin: #ifdef USE_TCP_SHT_CEP if ((error = tcp_sht_cep(cepid)) != E_OK) syslog(LOG_NOTICE, "[TES:%02d SHT] error: %s", cepid, itron_strerror(error)); #endif /* of #ifdef USE_TCP_SHT_CEP */ if ((error = tcp_cls_cep(cepid, CLS_TMO)) != E_OK) syslog(LOG_NOTICE, "[TES:%02d CLS] error: %s", cepid, itron_strerror(error)); syscall(get_tim(&now)); syslog(LOG_NOTICE, "[TES:%02d FIN] finished: %6lu, snd: %4u, rcv: %4u, len: %lu", cepid, now / SYSTIM_HZ, scount, rcount, total); return error; }
static ER tcp_echo_srv (ID cepid, ID repid) { ER error; SYSTIM now; uint32_t total; uint16_t rblen, sblen, rlen, slen, soff, scount, rcount; char *rbuf, *sbuf, head, tail; if ((error = TCP_ACP_CEP(cepid, repid, &dst, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(error)); return error; } /* 相手から接続されるまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); if (nblk_error == E_OK) { syscall(get_tim(&now)); syslog(LOG_NOTICE, "[TES:%02d ACP] connected: %6lu, from: %s.%u", cepid, now / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno); } else { syslog(LOG_NOTICE, "[TES:%02d ACP] error: %s", cepid, itron_strerror(nblk_error)); return nblk_error; } #ifdef USE_TCP_EXTENTIONS if ((error = free_tcp_rep(repid, true)) != E_OK) syslog(LOG_NOTICE, "[TES:%02d DEL] REP delete error: %s", cepid, itron_strerror(error)); #endif /* of #ifdef USE_TCP_EXTENTIONS */ rlen = scount = rcount = total = 0; while (true) { if ((error = tcp_rcv_buf(cepid, (void**)&rbuf, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", cepid, itron_strerror(error)); break; } /* 受信するまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); if (nblk_rlen < 0) { /* エラー */ syslog(LOG_NOTICE, "[TES:%02d RCV] error: %s", cepid, itron_strerror(nblk_rlen)); break; } else if (nblk_rlen == 0) /* 受信終了 */ break; rblen = nblk_rlen; /* バッファの残りにより、受信長を調整する。*/ if (rblen > BUF_SIZE - rlen) rblen = BUF_SIZE - rlen; total += rblen; rlen = rblen; head = *rbuf; tail = *(rbuf + rblen - 1); rcount ++; #ifdef SHOW_RCV_RANGE syslog(LOG_NOTICE, "[TES:%02d RCV] " "rcount: %4d, len: %4d, data %02x -> %02x", cepid, rcount, rblen, head, tail); #endif /* of #ifdef SHOW_RCV_RANGE */ memcpy(buffer, rbuf, rblen); if ((error = tcp_rel_buf(cepid, rlen)) < 0) { syslog(LOG_NOTICE, "[TES:%02d REL] error: %s", cepid, itron_strerror(error)); break; } soff = 0; while (rlen > 0) { if ((error = tcp_get_buf(cepid, (void**)&sbuf, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TES:%02d GET] error: %s", cepid, itron_strerror(error)); goto err_fin; } /* 送信バッファの獲得が完了するまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); if (nblk_slen < 0) { syslog(LOG_NOTICE, "[TES:%02d GET] error: %s", cepid, itron_strerror(nblk_slen)); goto err_fin; } sblen = nblk_slen; scount ++; slen = sblen < rlen ? sblen : rlen; memcpy(sbuf, buffer + soff, slen); if ((error = tcp_snd_buf(cepid, slen)) != E_OK) { syslog(LOG_NOTICE, "[TES:%02d SND] error: %s", cepid, itron_strerror(error)); goto err_fin; } #ifdef SHOW_RCV_RANGE syslog(LOG_NOTICE, "[TES:%02d SND] scount: %4d, len: %4d", cepid, scount, slen); #endif /* of #ifdef SHOW_RCV_RANGE */ rlen -= slen; soff += slen; } } err_fin: #ifdef USE_TCP_SHT_CEP if ((error = tcp_sht_cep(cepid)) != E_OK) syslog(LOG_NOTICE, "[TES:%02d SHT] error: %s", cepid, itron_strerror(error)); #endif /* of #ifdef USE_TCP_SHT_CEP */ if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK) syslog(LOG_NOTICE, "[TES:%02d CLS] error: %s", cepid, itron_strerror(error)); /* 開放が完了するまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); syscall(get_tim(&now)); syslog(LOG_NOTICE, "[TES:%02d FIN] finished: %6lu, snd: %4u, rcv: %4u, len: %lu", cepid, now / SYSTIM_HZ, scount, rcount, total); return error == E_WBLK ? E_OK : error; }
void tcp_telnet_srv_task(VP_INT exinf) { UW total; UH rblen, sblen, rlen, slen, soff, count; UB *rbuf, *sbuf, head, tail; ID tskid; ER error = E_OK; T_IF_SOFTC *ic; get_tid(&tskid); syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK):%d,%d] (copy save API) started.", tskid, (INT)exinf); while (TRUE) { if ((error = tcp_acp_cep((INT)exinf, TCP_TELNET_SRV_REPID, &dst, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) ACP] error: %s", itron_strerror(error)); continue; } /* 相手から接続されるまで待つ。*/ syscall(wai_sem(SEM_TCP_TELNET_SRV_NBLK_READY)); if (nblk_error == E_OK) syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) ACP] connected from %s:%d", ip2str(NULL, &dst.ipaddr), dst.portno); else { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) ACP] error: %s", itron_strerror(nblk_error)); continue; } connect = TRUE; total = rlen = count = 0; while (TRUE) { if ((error = tcp_rcv_buf((INT)exinf, (VP *)&rbuf, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) RCV] error: %s", itron_strerror(error)); break; } /* 受信するまで待つ。*/ syscall(wai_sem(SEM_TCP_TELNET_SRV_NRCV_READY)); if (nblk_rlen < 0) { /* エラー */ syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) RCV] error: %s", itron_strerror(nblk_rlen)); break; } else if (nblk_rlen == 0) /* 受信終了 */ break; rblen = (UH)nblk_rlen; /* バッファの残りにより、受信長を調整する。*/ if (rblen > BUF_SIZE - rlen) rblen = BUF_SIZE - rlen; total += rblen; rlen = rblen; head = *rbuf; tail = *(rbuf + rblen - 1); count ++; /*syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) RCV] " "count: %4d, len: %4d, data %02x -> %02x", ++ count, rblen, head, tail);*/ memcpy(buffer, rbuf, rblen); if ((error = tcp_rel_buf((INT)exinf, rlen)) < 0) { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) REL] error: %s", itron_strerror(error)); break; } soff = 0; while (rlen > 0) { if ((error = tcp_get_buf((INT)exinf, (VP *)&sbuf, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) GET] error: %s", itron_strerror(error)); goto err_fin; } /* 送信バッファの獲得が完了するまで待つ。*/ syscall(wai_sem(SEM_TCP_TELNET_SRV_NSND_READY)); if (nblk_slen < 0) { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) GET] error: %s", itron_strerror(nblk_slen)); goto err_fin; } else /*syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) GET] len: %d", nblk_slen)*/; sblen = (UH)nblk_slen; slen = sblen < rlen ? sblen : rlen; #if 0 /* ROI DEBUG */ memcpy(sbuf, buffer + soff, slen); #else /* ROI DEBUG */ slen = telnet_copy(sbuf, buffer + soff, slen, &rblen); #endif /* ROI DEBUG */ if ((error = tcp_snd_buf((INT)exinf, slen)) != E_OK) { syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) SND] error: %s", itron_strerror(error)); goto err_fin; } /*syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) SND] len: %d", slen);*/ #if 0 /* ROI DEBUG */ rlen -= slen; #else /* ROI DEBUG */ rlen -= rblen; #endif /* ROI DEBUG */ soff += slen; } } err_fin: if ((error = tcp_sht_cep((INT)exinf)) != E_OK) syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) SHT] error: %s", itron_strerror(error)); if ((error = tcp_cls_cep((INT)exinf, TMO_NBLK)) != E_WBLK) syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) CLS] error: %s", itron_strerror(error)); connect = FALSE; /* 開放が完了するまで待つ。*/ syscall(wai_sem(SEM_TCP_TELNET_SRV_NBLK_READY)); syslog(LOG_NOTICE, "[TCP TELNET SRV (NBLK) FIN] finished, total count: %d, len: %d", count, total); } }
U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par) { /* This function is called by the TCP module on TCP event */ /* Check the 'Net_Config.h' for possible events. */ par = par; if (soc != socket_tcp) { return (0); } switch (evt) { case TCP_EVT_DATA: /* TCP data frame has arrived, data is located at *par1, */ /* data length is par2. Allocate buffer to send reply. */ //procrec(ptr); plazma_modbus_tcp[1]=ptr[0]; plazma_modbus_tcp[2]=ptr[1]; plazma_modbus_tcp[3]=ptr[2]; plazma_modbus_tcp[4]=ptr[3]; plazma_modbus_tcp[5]=ptr[4]; plazma_modbus_tcp[6]=ptr[5]; plazma_modbus_tcp[7]=ptr[6]; plazma_modbus_tcp[8]=ptr[7]; plazma_modbus_tcp[9]=ptr[8]; plazma_modbus_tcp[10]=ptr[9]; plazma_modbus_tcp[11]=ptr[10]; //plazma_modbus_tcp[4]=ptr[3]; //plazma_modbus_tcp[5]=par; //plazma_modbus_tcp[6]=ptr[5]; modbus_tcp_func=ptr[7]; modbus_tcp_unit=ptr[6]; modbus_tcp_rx_arg0=(((unsigned short)ptr[8])*((unsigned short)256))+((unsigned short)ptr[9]); modbus_tcp_rx_arg1=(((unsigned short)ptr[10])*((unsigned short)256))+((unsigned short)ptr[11]); if(modbus_tcp_unit==MODBUS_ADRESS) { char modbus_tcp_tx_buff[200]; if(modbus_tcp_func==3) //чтение произвольного кол-ва регистров хранения { U8 *sendbuf; modbus_hold_registers_transmit(MODBUS_ADRESS,modbus_tcp_func,modbus_tcp_rx_arg0,modbus_tcp_rx_arg1,MODBUS_TCP_PROT); sendbuf = tcp_get_buf((modbus_tcp_rx_arg1*2)+9); sendbuf[0]=ptr[0]; sendbuf[1]=ptr[1]; sendbuf[2]=ptr[2]; sendbuf[3]=ptr[3]; sendbuf[4]=((modbus_tcp_rx_arg1*2)+3)/256; sendbuf[5]=((modbus_tcp_rx_arg1*2)+3)%256;; sendbuf[6]=modbus_tcp_unit; sendbuf[7]=modbus_tcp_func; sendbuf[8]=(U8)(modbus_tcp_rx_arg1*2); mem_copy((char*)&sendbuf[9],modbus_tcp_out_ptr,(modbus_tcp_rx_arg1*2)); //sendbuf[9]=3; //sendbuf[10]=4; tcp_send (socket_tcp, sendbuf, ((modbus_tcp_rx_arg1*2)+9)); // //modbus_tcp_tx_buff[4]=0; //modbus_tcp_tx_buff[5]=5; //modbus_tcp_tx_buff[6]=1;//modbus_tcp_unit; //modbus_tcp_tx_buff[7]=3;//modbus_tcp_func; //modbus_tcp_tx_buff[8]=2; //mem_copy((char*)&modbus_tcp_tx_buff[9],modbus_tcp_out_ptr,2); //modbus_tcp_tx_buff[9]=2; //modbus_tcp_tx_buff[10]=3; //tcp_send (socket_tcp, modbus_tcp_tx_buff, 11); } if(modbus_tcp_func==4) //чтение произвольного кол-ва регистров входов { U8 *sendbuf; modbus_input_registers_transmit(MODBUS_ADRESS,modbus_tcp_func,modbus_tcp_rx_arg0,modbus_tcp_rx_arg1,MODBUS_TCP_PROT); sendbuf = tcp_get_buf((modbus_tcp_rx_arg1*2)+9); sendbuf[0]=ptr[0]; sendbuf[1]=ptr[1]; sendbuf[2]=ptr[2]; sendbuf[3]=ptr[3]; sendbuf[4]=((modbus_tcp_rx_arg1*2)+3)/256; sendbuf[5]=((modbus_tcp_rx_arg1*2)+3)%256;; sendbuf[6]=modbus_tcp_unit; sendbuf[7]=modbus_tcp_func; sendbuf[8]=(U8)(modbus_tcp_rx_arg1*2); mem_copy((char*)&sendbuf[9],modbus_tcp_out_ptr,(modbus_tcp_rx_arg1*2)); //sendbuf[9]=3; //sendbuf[10]=4; tcp_send (socket_tcp, sendbuf, ((modbus_tcp_rx_arg1*2)+9)); // //modbus_tcp_tx_buff[4]=0; //modbus_tcp_tx_buff[5]=5; //modbus_tcp_tx_buff[6]=1;//modbus_tcp_unit; //modbus_tcp_tx_buff[7]=3;//modbus_tcp_func; //modbus_tcp_tx_buff[8]=2; //mem_copy((char*)&modbus_tcp_tx_buff[9],modbus_tcp_out_ptr,2); //modbus_tcp_tx_buff[9]=2; //modbus_tcp_tx_buff[10]=3; //tcp_send (socket_tcp, modbus_tcp_tx_buff, 11); } else if(modbus_tcp_func==6) //запись регистров хранения { U8 *sendbuf; if(modbus_tcp_rx_arg0==11) //Установка времени { LPC_RTC->YEAR=(uint16_t)modbus_tcp_rx_arg1; } if(modbus_tcp_rx_arg0==12) //Установка времени { LPC_RTC->MONTH=(uint16_t)modbus_tcp_rx_arg1; } if(modbus_tcp_rx_arg0==13) //Установка времени { LPC_RTC->DOM=(uint16_t)modbus_tcp_rx_arg1; } if(modbus_tcp_rx_arg0==14) //Установка времени { plazma_modbus_tcp[0]++; LPC_RTC->HOUR=(uint16_t)modbus_tcp_rx_arg1; } if(modbus_tcp_rx_arg0==15) //Установка времени { LPC_RTC->MIN=(uint16_t)modbus_tcp_rx_arg1; } if(modbus_tcp_rx_arg0==16) //Установка времени { LPC_RTC->SEC=(uint16_t)modbus_tcp_rx_arg1; } if(modbus_tcp_rx_arg0==20) //ток стабилизации для режима стабилизации тока { if((modbus_tcp_rx_arg1>0)&&(modbus_tcp_rx_arg1<=18)) lc640_write_int(EE_NUMIST,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==21) //ток стабилизации для режима стабилизации тока { if((modbus_tcp_rx_arg1==0)||(modbus_tcp_rx_arg1==1)) lc640_write_int(EE_PAR,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==22) //ток стабилизации для режима стабилизации тока { if((modbus_tcp_rx_arg1==0)||(modbus_tcp_rx_arg1==1)) lc640_write_int(EE_ZV_ON,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==23) //ток стабилизации для режима стабилизации тока { if((modbus_tcp_rx_arg1==0)||(modbus_tcp_rx_arg1==1)) lc640_write_int(EE_TERMOKOMP,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==24) //ток стабилизации для режима стабилизации тока { if((modbus_tcp_rx_arg1>=0)||(modbus_tcp_rx_arg1<=20)) lc640_write_int(EE_UBM_AV,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==30) //напряжение стабилизации для режима стабилизации напряжения { if(modbus_tcp_rx_arg1<0)TBAT=0; else if((modbus_tcp_rx_arg1>0)&&(modbus_tcp_rx_arg1<=5))modbus_tcp_rx_arg1=0; else if(modbus_tcp_rx_arg1>=60)TBAT=60; else TBAT=modbus_tcp_rx_arg1; lc640_write_int(EE_TBAT,TBAT); } if(modbus_tcp_rx_arg0==31) // { lc640_write_int(EE_UMAX,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==32) // { lc640_write_int(EE_DU,UB20-modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==33) // { lc640_write_int(EE_UB0,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==34) // { lc640_write_int(EE_UB20,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==35) // { lc640_write_int(EE_USIGN,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==36) // { lc640_write_int(EE_UMN,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==37) // { lc640_write_int(EE_U0B,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==38) // { lc640_write_int(EE_IKB,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==39) // { lc640_write_int(EE_IZMAX,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==40) // { lc640_write_int(EE_IMAX,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==41) // { lc640_write_int(EE_IMIN,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==42) // { lc640_write_int(EE_UVZ,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==43) // { lc640_write_int(EE_TZAS,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==44) // { lc640_write_int(EE_TMAX,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==45) // { lc640_write_int(EE_TSIGN,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==46) // { lc640_write_int(EE_TBATMAX,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==47) // { lc640_write_int(EE_TBATSIGN,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==48) // { lc640_write_int(EE_SPEED_CHRG_CURR,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==49) // { lc640_write_int(EE_SPEED_CHRG_VOLT,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==50) // { lc640_write_int(EE_SPEED_CHRG_TIME,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==51) // { lc640_write_int(EE_U_OUT_KONTR_MAX,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==52) // { lc640_write_int(EE_U_OUT_KONTR_MIN,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==53) // { lc640_write_int(EE_U_OUT_KONTR_DELAY,modbus_tcp_rx_arg1); } if(modbus_tcp_rx_arg0==19) //вкл/выкл источника напр. { /* if(modbus_tcp_rx_arg1==1) { if(work_stat!=wsPS) { work_stat=wsPS; time_proc=0; time_proc_remain=T_PROC_PS; restart_on_PS(); lc640_write_int(EE_MAIN_MENU_MODE,mmmIN); } } if(modbus_tcp_rx_arg1==0) { if(work_stat==wsPS) { work_stat=wsOFF; restart_off(); } } */ } if(modbus_tcp_rx_arg0==20) //вкл/выкл источника тока { /* if(modbus_tcp_rx_arg1==1) { if(work_stat!=wsGS) { work_stat=wsGS; time_proc=0; time_proc_remain=T_PROC_GS; lc640_write_int(EE_MAIN_MENU_MODE,mmmIT); } } if(modbus_tcp_rx_arg1==0) { if(work_stat==wsGS) { work_stat=wsOFF; restart_off(); } }*/ } modbus_hold_registers_transmit(MODBUS_ADRESS,modbus_tcp_func,modbus_tcp_rx_arg0,1,MODBUS_TCP_PROT); sendbuf = tcp_get_buf(11); sendbuf[0]=ptr[0]; sendbuf[1]=ptr[1]; sendbuf[2]=ptr[2]; sendbuf[3]=ptr[3]; sendbuf[4]=0; sendbuf[5]=6; sendbuf[6]=modbus_tcp_unit; sendbuf[7]=modbus_tcp_func; sendbuf[8]=modbus_tcp_rx_arg0/256; sendbuf[9]=modbus_tcp_rx_arg0%256; mem_copy((char*)&sendbuf[10],modbus_tcp_out_ptr,2); //sendbuf[9]=3; //sendbuf[10]=4; tcp_send (socket_tcp, sendbuf, 12); // //modbus_tcp_tx_buff[4]=0; //modbus_tcp_tx_buff[5]=5; //modbus_tcp_tx_buff[6]=1;//modbus_tcp_unit; //modbus_tcp_tx_buff[7]=3;//modbus_tcp_func; //modbus_tcp_tx_buff[8]=2; //mem_copy((char*)&modbus_tcp_tx_buff[9],modbus_tcp_out_ptr,2); //modbus_tcp_tx_buff[9]=2; //modbus_tcp_tx_buff[10]=3; //tcp_send (socket_tcp, modbus_tcp_tx_buff, 11); } } break; case TCP_EVT_CONREQ: /* Remote peer requested connect, accept it */ return (1); case TCP_EVT_CONNECT: /* The TCP socket is connected */ return (1); } return (0); }
void tcp_echo_srv_task(intptr_t exinf) { ID tskid; ER_UINT rblen, sblen; ER error = E_OK; uint32_t total; uint16_t rlen, slen, soff, count; char *rbuf, *sbuf, head, tail; get_tid(&tskid); syslog(LOG_NOTICE, "[TCP ECHO SRV:%d,%d] (copy save API) started.", tskid, (int_t)exinf); while (true) { if (tcp_acp_cep((int_t)exinf, TCP_ECHO_SRV_REPID, &dst, TMO_FEVR) != E_OK) { syslog(LOG_NOTICE, "[TCP ECHO SRV ACP] error: %s", itron_strerror(error)); continue; } total = count = 0; syslog(LOG_NOTICE, "[TCP ECHO SRV ACP] connected from %s:%d", ip2str(NULL, &dst.ipaddr), dst.portno); while (true) { if ((rblen = tcp_rcv_buf((int_t)exinf, (void **)&rbuf, TMO_FEVR)) <= 0) { if (rblen != E_OK) syslog(LOG_NOTICE, "[TCP ECHO SRV RCV] error: %s", itron_strerror(rblen)); break; } rlen = (uint16_t)rblen; total += (uint32_t)rblen; head = *rbuf; tail = *(rbuf + rlen - 1); count ++; /*syslog(LOG_NOTICE, "[TCP ECHO SRV RCV] count: %4d, len: %4d, data %02x -> %02x", ++ count, rlen, head, tail);*/ soff = 0; while (rlen > 0) { if ((sblen = tcp_get_buf((int_t)exinf, (void **)&sbuf, TMO_FEVR)) < 0) { syslog(LOG_NOTICE, "[TCP ECHO SRV GET] error: %s", itron_strerror(sblen)); goto err_fin; } /*syslog(LOG_NOTICE, "[TCP ECHO SRV GET] len: %d", sblen);*/ slen = rlen < (uint16_t)sblen ? rlen : (uint16_t)sblen; memcpy(sbuf, rbuf + soff, slen); if ((error = tcp_snd_buf((int_t)exinf, slen)) != E_OK) { syslog(LOG_NOTICE, "[TCP ECHO SRV SND] error: %s", itron_strerror(error)); goto err_fin; } /*syslog(LOG_NOTICE, "[TCP ECHO SRV SND] len: %d", slen);*/ rlen -= slen; soff += slen; } if ((error = tcp_rel_buf((int_t)exinf, rblen)) < 0) { syslog(LOG_NOTICE, "[TCP ECHO SRV REL] error: %s", itron_strerror(error)); break; } } err_fin: if ((error = tcp_sht_cep((int_t)exinf)) != E_OK) syslog(LOG_NOTICE, "[TCP ECHO SRV SHT] error: %s", itron_strerror(error)); if ((error = tcp_cls_cep((int_t)exinf, TMO_FEVR)) != E_OK) syslog(LOG_NOTICE, "[TCP ECHO SRV CLS] error: %s", itron_strerror(error)); syslog(LOG_NOTICE, "[TCP ECHO SRV FIN] finished, total cnt: %d, len: %d", count, total); } }
void tcp_echo_srv_task(intptr_t exinf) { ID tskid; ER error = E_OK; uint32_t total; uint16_t rblen, sblen, rlen, slen, soff, count; char *rbuf, *sbuf, head, tail; get_tid(&tskid); syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK):%d,%d] (copy save API) started.", tskid, (int_t)exinf); while (true) { if ((error = tcp_acp_cep((int_t)exinf, TCP_ECHO_SRV_REPID, &dst, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) ACP] error: %s", itron_strerror(error)); continue; } /* 相手から接続されるまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); if (nblk_error == E_OK) syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) ACP] connected from %s:%d", ip2str(NULL, &dst.ipaddr), dst.portno); else { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) ACP] error: %s", itron_strerror(nblk_error)); continue; } total = rlen = count = 0; while (true) { if ((error = tcp_rcv_buf((int_t)exinf, (void **)&rbuf, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) RCV] error: %s", itron_strerror(error)); break; } /* 受信するまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); if (nblk_rlen < 0) { /* エラー */ syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) RCV] error: %s", itron_strerror(nblk_rlen)); break; } else if (nblk_rlen == 0) /* 受信終了 */ break; rblen = (uint16_t)nblk_rlen; /* バッファの残りにより、受信長を調整する。*/ if (rblen > BUF_SIZE - rlen) rblen = BUF_SIZE - rlen; total += rblen; rlen = rblen; head = *rbuf; tail = *(rbuf + rblen - 1); count ++; /*syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) RCV] " "count: %4d, len: %4d, data %02x -> %02x", ++ count, rblen, head, tail);*/ memcpy(buffer, rbuf, rblen); if ((error = tcp_rel_buf((int_t)exinf, rlen)) < 0) { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) REL] error: %s", itron_strerror(error)); break; } soff = 0; while (rlen > 0) { if ((error = tcp_get_buf((int_t)exinf, (void **)&sbuf, TMO_NBLK)) != E_WBLK) { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) GET] error: %s", itron_strerror(error)); goto err_fin; } /* 送信バッファの獲得が完了するまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); if (nblk_slen < 0) { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) GET] error: %s", itron_strerror(nblk_slen)); goto err_fin; } else /*syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) GET] len: %d", nblk_slen)*/; sblen = (uint16_t)nblk_slen; slen = sblen < rlen ? sblen : rlen; memcpy(sbuf, buffer + soff, slen); if ((error = tcp_snd_buf((int_t)exinf, slen)) != E_OK) { syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) SND] error: %s", itron_strerror(error)); goto err_fin; } /*syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) SND] len: %d", slen);*/ rlen -= slen; soff += slen; } } err_fin: if ((error = tcp_sht_cep((int_t)exinf)) != E_OK) syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) SHT] error: %s", itron_strerror(error)); if ((error = tcp_cls_cep((int_t)exinf, TMO_NBLK)) != E_WBLK) syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) CLS] error: %s", itron_strerror(error)); /* 開放が完了するまで待つ。*/ syscall(wai_sem(SEM_TCP_ECHO_SRV_NBLK_READY)); syslog(LOG_NOTICE, "[TCP ECHO SRV (NBLK) FIN] finished, total count: %d, len: %d", count, total); } }