?? main.c
字號:
///#include <ipOS.h>///#include <ipHAL.h>///#include <ipStack.h>///#include <ipEthernet.h>///#include "hust_syn.h"#include <ipOS.h>#include <ipHAL.h>#include <ipStack.h>#include <ipEthernet.h>#include "hust_rtp.h"#include "hust_rtplibcommon.h"#include "hust_rtcp.h"#include "hust_event.h"#include "hust_hash.h"#include "hust_linux.h"/* * IP2022 configuration block */CONFIG_BLOCK ( FUSE0(FUSE0_XTAL | FUSE0_PIN_DIV1 | FUSE0_POUT_DIV2 | FUSE0_WUDP_128us | FUSE0_WUDX_1ms), FUSE1(0), OSC1_FREQ, "UBICOM", "UDP", CONFIG_VER(0, 0, 0, 0), CONFIG_DATE(0, 0, 0), CONFIG_DATE(0, 0, 0) );#define RUNTIME_DEBUG 0#define QUEUEMAXLEN 0x4d1c#define EXTMEM_BEGIN 0x800000#define EXTMEM_END 0x81fffe //128KB extmemu32_t extmem_addr=0;u32_t extmem_test_addr=0;/* * Define the filename to be used for assertions. */THIS_FILE("main");u8_t mac_addr[6] = {0x01, 0x00, 0x5E, 0x03, 0x05, 0x01};struct udp_socket *us,*us2;struct netbuf *nb1;struct netbuf *nb2;struct netbuf *nb;struct ip_datalink_instance *eii;addr_t temp,datalen;addr_t sizeflag;struct session *psn;struct stream *prtp_stm=0;u8_t looptime=0;bool firstrtp=1;volatile netpage_t npst __attribute__ ((section(".gpr.npst"))); //bit 0: need to set VALID pin to invalid status //bit 2:np1 act, bit 3:np2 act //bit 1: data ready, need to set VALID pin to high//volatile u8_t np_len __attribute__ ((section(".gpr.np_len"))); volatile netpage_t np1 __attribute__ ((section(".gpr.np1")));volatile netpage_t np2 __attribute__ ((section(".gpr.np2")));volatile netpage_t np __attribute__ ((section(".gpr.np")));volatile u8_t np_off __attribute__ ((section(".gpr.np_off")));///int nb1_start;///int nb1_end;///int nb2_start;///int nb2_end;u8_t data_write[RTP_DATASIZE]={0};///u16_t data_test[100]={0};bool heap_empty=0;bool error_flag=0;u32_t former;u8_t errcnt=0;///////////////////igmpu32_t src_addr;u8_t spdata[8];int spflag;int groupflag;void write_fifo();void udp_rtp_recv(struct udp_socket *udps, struct ip_datalink_instance *link, u32_t src_addr, u16_t src_port, u32_t dest_addr, u32_t spec_dest_addr, u16_t dest_port, u8_t ttl, u8_t tos, u16_t id, struct netbuf *udpnb) __attribute__ ((section (".pram.udp_rtp_recv")));void udp_rtp_recv(struct udp_socket *udps, struct ip_datalink_instance *link, u32_t src_addr, u16_t src_port, u32_t dest_addr, u32_t spec_dest_addr, u16_t dest_port, u8_t ttl, u8_t tos, u16_t id, struct netbuf *udpnb){// struct rtpln *pln;// struct rtp *prtp;/// struct stream *pstm;// bool head; // int rv;// u8_t gettemp1,gettemp2; // pln = (struct rtpln *) heap_alloc(sizeof(struct rtpln));// if (pln == NULL) {/// debug_abort();/// return ;// }// memset(pln,0,sizeof(struct rtpln)); // gettemp1=netbuf_fwd_read_u8(udpnb);// gettemp2=netbuf_fwd_read_u8(udpnb);/* prtp = &(pln->rln_rtp); prtp->rtp_ver=gettemp1>>6; prtp->rtp_pad=(gettemp1>>5)&0x1; prtp->rtp_ext=(gettemp1>>4)&0x1; prtp->rtp_cc=gettemp1&0xf; prtp->rtp_mark=(gettemp2>>7)&0x1; prtp->rtp_payload=gettemp2&0x7f; prtp->rtp_seq=netbuf_fwd_read_u16(udpnb); prtp->rtp_time=netbuf_fwd_read_u32(udpnb); prtp->rtp_ssrc=netbuf_fwd_read_u32(udpnb); pln->rln_len=netbuf_get_end(udpnb)-netbuf_get_pos(udpnb);*//// if((extmem_addr+pln->rln_len)>=EXTMEM_END)// extmem_addr=EXTMEM_BEGIN; // prtp->rtp_data_addr=extmem_addr;// extmem_test_addr=extmem_addr; netbuf_fwd_read_mem(udpnb, data_write, RTP_DATASIZE);// extmem_write(extmem_addr , data_write, pln->rln_len);// extmem_addr+=pln->rln_len; /* if(firstrtp) { former= prtp->rtp_seq; firstrtp= 0; } else { if(prtp->rtp_seq!= (former+1) ) { errcnt++; debug_set_lights(errcnt); } if(errcnt>10) { error_flag=1; /// debug_abort(); } former= prtp->rtp_seq; }*/ /* if (pln->rln_len < RTP_HEADERMINLEN ||prtp->rtp_ver != RTP_CURRVERS ||prtp->rtp_cc > MAXCC ||prtp->rtp_payload != MPEG2TYPE) {/// extmem_addr=prtp->rtp_data_addr;//////接收的包不能存下來,清除寫入外部存儲器的數據 heap_free(pln); return ; }*/ //////測試寫入的內容 /* for(i=0 ; i<100; i++) { data_test[i]=extmem_read_u16(extmem_test_addr); extmem_test_addr+=2; }*/ // if (psn->sn_rtcpto == RTP_INADDRUNINITIALIZED) {// psn->sn_rtcpto = src_addr;// } /// pstm = rtpgetstream(psn, prtp->rtp_ssrc); // if (pstm == NULL) {/// if ((pstm = rtpnewstream(psn, prtp->rtp_ssrc)) == NULL) {/*產生新的stream*//// extmem_addr=prtp->rtp_data_addr; // heap_free(pln);// return ;// }// }// else {// if (RTP_INACTIVE(pstm->stm_inactive)) {/*收到BYE包或超時*/// rtpreleasestream(psn, pstm);// extmem_addr=prtp->rtp_data_addr;// heap_free(pln);// return ;// }// } /// if (pstm->stm_ip == RTP_INADDRUNINITIALIZED)/// pstm->stm_ip = src_addr;/*記錄流的參與者的ip地址*/ /* * Update stream's statistics and check if stream is still * on probation. *//* if (rtpupdate(psn, pstm, prtp) == ERROR) { rtpreleasestream(psn, pstm); extmem_addr=prtp->rtp_data_addr; heap_free(pln); return ; }*//// pln->rln_seq=prtp->rtp_seq;// rv = _rtpqinsert(&prtp_stm->stm_queue, pln, &head);/* if (rv == OK) { if(prtp_stm->stm_queue.rq_len > QUEUEMAXLEN) rtpwritefifo(prtp_stm);/////寫fifo并清空流中的隊列 return ; } extmem_addr=prtp->rtp_data_addr;*/ write_fifo(); /// heap_free(pln); return ;}void rtppacket_extract(void *app, void *param) __attribute__ ((section (".pram.rtppacket_extract")));void rtppacket_extract(void *app, void *param){ if(prtp_stm->stm_queue.rq_len > QUEUEMAXLEN) rtpwritefifo(prtp_stm);/////寫fifo并清空流中的隊列 }void udp_rtcp_recv(struct udp_socket *udps, struct ip_datalink_instance *link, u32_t src_addr, u16_t src_port, u32_t dest_addr, u32_t spec_dest_addr, u16_t dest_port, u8_t ttl, u8_t tos, u16_t id, struct netbuf *udpnb) __attribute__ ((section (".pram.udp_rtcp_recv")));void udp_rtcp_recv(struct udp_socket *udps, struct ip_datalink_instance *link, u32_t src_addr, u16_t src_port, u32_t dest_addr, u32_t spec_dest_addr, u16_t dest_port, u8_t ttl, u8_t tos, u16_t id, struct netbuf *udpnb){ struct rtcp *prtcp; u8_t first=1; addr_t len; u8_t gettemp; ///char rtcpbuf[RTCP_MAXPACKETSZ]; len=netbuf_get_remaining(udpnb); netbuf_set_pos_to_start(udpnb); if (len < 0) return ERROR; /* * Extract source address as RTCP destination for unicast session * if not already learned. */ if (psn->sn_rtcpto == INADDR_ANY) psn->sn_rtcpto = src_addr; /* * Iterate over the compound packet. stop if we discover packet * is truncated due to the use of fixed-sized buffers. */ while(len > RTCP_HEADERSZ) { prtcp = (struct rtcp *) heap_alloc(sizeof(struct rtcp));/* psdchunk=(struct sdchunk*)heap_alloc(sizeof(struct sdchunk)); psditem=(struct sditem*)heap_alloc(sizeof(struct sditem)); prtcp->rtcp_data=psdchunk; psdchunk->sdc_data=psditem; */ memset(prtcp,0,sizeof(struct rtcp)); gettemp=netbuf_fwd_read_u8(udpnb); prtcp->rtcp_ver=gettemp>>6; prtcp->rtcp_pad=(gettemp>>5)&0x1; prtcp->rtcp_count=gettemp&0x1f; prtcp->rtcp_type=netbuf_fwd_read_u8(udpnb); if(first) { first=0; if((prtcp->rtcp_type!=RTCP_SR)&&(prtcp->rtcp_type!=RTCP_RR)) break; } prtcp->rtcp_length=netbuf_fwd_read_u16(udpnb); if (len < RTCP_LENGTH_BYTE(prtcp)) break; /* datalen=RTCP_LENGTH_BYTE(prtcp)-RTCP_HEADERSZ; prtcp->rtcp_data=heap_alloc(datalen); psditem->sdi_data=heap_alloc(psditem->sdi_len); memset(prtcp->rtcp_data,0,datalen); netbuf_fwd_read_mem(udpnb,prtcp->rtcp_data,datalen); */ rtcpprocess(psn, prtcp,udpnb); len -= RTCP_LENGTH_BYTE(prtcp); heap_free(prtcp); }/// heap_add((addr_t)(&_bss_end), (addr_t)(RAMEND - (DEFAULT_STACK_SIZE - 1)) - (addr_t)(&_bss_end)); return OK;}void udp_recv_icmp(struct udp_socket *udps, struct ip_datalink_instance *link, u8_t icmp_type, u8_t icmp_code, u32_t src_addr, u16_t src_port, u32_t dest_addr, u32_t spec_dest_addr, u16_t dest_port, u8_t ttl, u8_t tos, u16_t id, struct netbuf *nbicmp){ udp_send_netbuf(us, NULL, 0xc0a8013a, 1000, 0,100, UDP_TTL_DEFAULT , UDP_TOS_DEFAULT , UDP_ID_DEFAULT , UDP_DF_DEFAULT , nb); }/* * uart_recv_intr() * This function is called each time a byte is received by the UART. */void uart_recv_intr(void *protocol_instance){ u8_t data; struct uart_instance *uarti = (struct uart_instance*)protocol_instance; /* * Read the received byte from the UART. */ data = uarti->recv(uarti); spdata[spflag]=data; spflag++; /* * Transmit the middle byte back again. */ if(spflag==8) { if((spdata[0]==spdata[4])&&(spdata[1]==spdata[5])&&(spdata[2]==spdata[6])&&(spdata[3]==spdata[7])) { if(spdata[3]!=groupflag) { uarti->send(uarti, spdata[3]); if(groupflag!=0) { igmp_send_netbuf_test(NULL, groupflag, 0x0, IGMP_MEMBERSHIP_LEAVE, src_addr, IGMP_TTL_DEFAULT, IGMP_TOS_DEFAULT, IGMP_ID_DEFAULT, IGMP_DF_DEFAULT); } groupflag=(int)spdata[3]; set_current_gnum(groupflag); igmp_send_netbuf_test(NULL, groupflag, 0x0, IGMP_MEMBERSHIP_REPORT, src_addr, IGMP_TTL_DEFAULT, IGMP_TOS_DEFAULT, IGMP_ID_DEFAULT, IGMP_DF_DEFAULT); //Default value of the unsolicited report interval is 10(s) //1(s) set_current_state(1,1000,0); } } spflag=0; }}/* * init() */void init(void){ debug_init(); heap_add((addr_t)(&_bss_end), (addr_t)(RAMEND - (DEFAULT_STACK_SIZE - 1)) - (addr_t)(&_bss_end)); netpage_init();/* nb1=netbuf_alloc_with_fwd_space(1500); nb2=netbuf_alloc_with_fwd_space(1500); nb1_start=netbuf_get_start(nb1); //nb1_end=nb1_start; nb1_end=netbuf_get_end(nb1); nb2_start=netbuf_get_start(nb2); //nb2_end=nb2_start; nb2_end=netbuf_get_end(nb2); np1=nb1->pages[0]; np2=nb2->pages[0]; np=np1; nb=nb1; //np1=netpage_alloc(); //np2=netpage_alloc(); //np=np1; //npst=0x04;//np1 active np_off=0; //np_len=0;*/ timer_init(); struct ethdev_instance *edi = ip2k_eth_instance_alloc(mac_addr); struct eth_mux_instance *ethi = eth_mux_instance_alloc((struct ethdev_instance *)edi); eii=eth_ip_arp_instance_alloc(ethi, ETH_LOCAL_IP_ADDRESS, ETH_LOCAL_IP_SUBNET); us = udp_socket_alloc(NULL); us2= udp_socket_alloc(NULL); /// psn=rtpopen(ETH_LOCAL_IP_ADDRESS,5000,500,3); prtp_stm=rtpnewstream();/// psn=rtpopen(0xef010101,5000,500,3);///////////////////////////////////////////////////igmp set_igmp_timer(0); //initialize timerflag to be 0 set_current_state(0, 0, 0); //initialize state to be NONMEMBER, default mode is using fixed timer ticks. set_current_gnum(0); //initialize channel number to be 0 spflag=0; groupflag=0; src_addr=0xc0a801ca; struct uart_instance *uarti = echo_uart_vp_instance_alloc(); uarti->listen(uarti, uarti, NULL, uart_recv_intr, NULL); //udp_listen(us, eii, 0, 1000, udp_recv, udp_recv_icmp); }/* * start() * The system has been initialised and is ready to start. */void start(void){ /* * Instead of calling the attach method here, we call the callback to * kick off the timer. */ udp_listen(us, eii, 0, 9000, udp_rtp_recv, udp_recv_icmp); udp_listen(us2, eii, 0, 5001, udp_rtcp_recv, udp_recv_icmp);/// event_register(system_event_poll, (event_callback_t)rtppacket_extract, NULL); /// extmem_init();/// extmem_enable();/// extmem_addr=EXTMEM_BEGIN;/// temp=heap_get_free(); //udp_listen(us, eii, 0, 0, udp_recv, NULL);/* char *tempchar1,*tempchar2,*tempchar3; temp=heap_get_free(); tempchar1=(char*)heap_alloc(10); memset(tempchar1,1,10); temp=heap_get_free(); tempchar2=(char*)heap_alloc(20); memset(tempchar2,2,20); temp=heap_get_free(); tempchar3=(char*)heap_alloc(30); memset(tempchar3,3,30); temp=heap_get_free(); heap_free(tempchar1); temp=heap_get_free(); heap_free(tempchar2); temp=heap_get_free(); heap_free(tempchar3); temp=heap_get_free();*/ }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -