?? mac-gprs.cc
字號:
// base_stn() doesnt work here. since it hasnt yet got its // ARP reply and doesnt know the MAC address of its base station!!! this->hdr_src((char*)dh1, this->addr()); this->hdr_dst((char*)dh1, dst); ch1->uid() = 0; ch1->direction() = hdr_cmn::DOWN; ch1->ptype() = PT_MAC; //ch1->size() = slot_packet_len_; ch1->size() = 20; //arbitly set at 20. ch1->iface() = -2; ch1->error() = 0; dh1->dh_fc.fc_type = MAC_Type_Control; dh1->dh_fc.fc_subtype = MAC_Subtype_res_request; ch1->chan() = 0; // send on upslot 0, freq 0 ie random access chan pktTx[0] = p1; //block the IFQ from sending down further pkts till a res_reply is //recvd q_->block(); if (verbose_==1) printf("<%d> %f res_request created. dst= %d. IFQ blocked.\n", index_, NOW, /*node_->base_stn() */ dst); rx_from_ll (pktTx[0]);}//create a resource reply message and schedule it to be sent in//downlsot 0void MacGprs::send_res_reply(int dst, int freq, int slot){ //dst= the node that sent the res_request... //or the node to which a res_reply has to be sent. Packet *p1 = Packet::alloc(); struct hdr_cmn* ch1= HDR_CMN(p1); struct hdr_mac_gprs* dh1 = HDR_MAC_GPRS(p1); this->hdr_src((char*)dh1, this->addr()); this->hdr_dst((char*)dh1, dst); ch1->uid() = 0; ch1->ptype() = PT_MAC; //ch1->size() = slot_packet_len_; ch1->size() = 20; //arbitly set at 20. ch1->iface() = -2; ch1->error() = 0; ch1->direction() = hdr_cmn::DOWN; if (verbose_==1) printf("<%d> %f res_reply created. freq=%d, slot=%d booked for dst=%d\n", index_, NOW, freq, slot,dst); dh1->dh_fc.fc_type = MAC_Type_Control; dh1->dh_fc.fc_subtype = MAC_Subtype_res_reply; dh1->dh_freq = freq; dh1->dh_slot = slot; ch1->chan() = 0; // send on upslot 0, freq 0 ie random access chan if (txQ[0][0]==0){ txQ[0][0] = p1; rx_from_ll (txQ[0][0]); } else { // if I already have an as-yet-untransmitted res_reply and // another comes along - store the new arrival and tx it on // the next TDMA frame. temp_reply=p1; rx_from_ll(temp_reply); }// { // printf("<%d> %f i have another res_reply to send, cant send yours.\n", index_, NOW); // vlr_.down_table[freq][slot]==-10; // vlr_.up_table[freq][slot]==-10; // printf("<%d> %f vlr_entry %d, %d cleared\n",index_, NOW, freq, slot);// } }// Prepare pkt to be sent onto PHY. ie add headers. Is actually// rx from IFQ! void MacGprs::rx_from_ll(Packet* p) { u_int32_t dst, src, size; struct hdr_cmn* ch = HDR_CMN(p); struct hdr_mac_gprs* dh = HDR_MAC_GPRS(p); if (verbose_==1) printf("<%d> %f rxing_from_ll, size=%d\n", index_, NOW,ch->size()); // Update the MAC header, same as 802.11 dh->dh_fc.fc_protocol_version = MAC_ProtocolVersion; if (dh->dh_fc.fc_type != MAC_Type_Control){ ch->size() += ETHER_HDR_LEN; dh->dh_fc.fc_type = MAC_Type_Data; dh->dh_fc.fc_subtype = MAC_Subtype_data; } dh->dh_fc.fc_to_ds = 0; dh->dh_fc.fc_from_ds = 0; dh->dh_fc.fc_more_frag = 0; dh->dh_fc.fc_retry = 0; dh->dh_fc.fc_pwr_mgt = 0; dh->dh_fc.fc_more_data = 0; dh->dh_fc.fc_wep = 0; dh->dh_fc.fc_order = 0; if ( dh->dh_wait != 1) dh->dh_wait = 0; if((u_int32_t)ETHER_ADDR(dh->dh_da) != MAC_BROADCAST) dh->dh_duration = DATA_DURATION; else dh->dh_duration = 0;}// Actually TX the packet. void MacGprs::tx_onto_PHY(Packet *p) { double stime; double temp; struct hdr_cmn* ch = HDR_CMN(p); struct hdr_mac_gprs* dh = HDR_MAC_GPRS(p); //check if any other MS is txing too... if this is the Random Access Channel if ( ch->chan()==0 && up_slot_ ==0 && index_!=0) { //have to amke sure it's not a BS - coz BS may tx //on chan0 in downslot3 switch (chan0_0) { case IDLE: chan0_0 = BUSY; if (verbose_==1) printf("<%d> %f chan0_0 made BUSY \n", index_, NOW); break; case BUSY: coll_count = 2; chan0_0 = COLL; if (verbose_==1) printf("<%d> %f chan0_0=%0x coll_count=%d \n", index_, NOW,chan0_0, coll_count); break; case COLL: coll_count ++; if (verbose_==1) printf("<%d> %f chan0_0 => COLL coll_count=%d \n", index_, NOW, coll_count); ch->error() = 1; //mark the pkt as erroneous break; } } if (dh->dh_fc.fc_subtype == MAC_Subtype_res_request) { //sent only by ms in Upslot 0. MS waits for 7 TS temp= (double)((SLOTS_PER_FRAME-1)*slot_time_); mhwait_.start(temp); if (verbose_==1) printf("<%d> %f wait timer started \n", index_, NOW); } stime = TX_Time(p); if (stime > slot_time_) stime= slot_time_ ;// just to make sure. assert(stime >= 0); if (verbose_==1) printf("<%d> %f txing_onto_PHY, sendtime=%f, size=%d\n", index_, NOW, stime, ch->size()); mhTxPkt_.start(p->copy(), stime); //mhTxPkt_.start(p->copy(), slot_time_); downtarget_->recv(p, this); }// Turn on / off the radiovoid MacGprs::radioSwitch(int i) { radio_active_ = i; EnergyModel *em = netif_->node()->energy_model(); if (i == ON) { if (em && em->sleep()){ em->set_node_sleep(0); } if (verbose_==1) printf("<%d> %f radio switched ON\n", index_, NOW); return; } if (i == OFF) { if (em && !em->sleep()) { em->set_node_sleep(1); }; if (verbose_==1) printf("<%d> %f radio switched OFF\n", index_, NOW); return; }} //============================================================// Timers' handlers //============================================================/* Slot Timers:*/void MacGprs::downslotHandler(Event *e) { int i, len; struct hdr_mac_gprs *dh; MobileNode * node_ = (MobileNode*)(netif_->node()); if (( first_round_ ==1) & (down_slot_==0)) { vlr_.hier_addr_[index_] = Address::instance().get_nodeaddr(node_->address()); // set info in data_at_bs - since we cdnlt do it in the // constructor as there wasnt yet a pointer to netif_........ if (verbose_==1) printf(" hier_Add[%d]=%d ; grps=%d \n", index_, vlr_.hier_addr_[index_], gprs_); } // Restart timer for next slot. mhDownSlot_.start((Packet *)e, slot_time_); if (index_==0 && verbose_==1) printf("<%d> %f down slot %d starts now. \n", index_, NOW, down_slot_); //start Upslottimer after 3 downslots to ensure skew if (( first_round_ ==1) & (down_slot_==3)) { first_round_ = 0; mhUpSlot_.start((Packet *) (& intr1_), 0); } if (node_->base_stn() == node_->address()) {//node is a BS for (i=0; i<max_num_freq_ ; i ++) { if (down_slot_==0 && txQ[i][down_slot_]==0) { if (temp_reply != 0) { radioSwitch(ON); tx_onto_PHY (temp_reply); temp_reply=0; } } // check if any chan has to tx in this slot. if (txQ[i][down_slot_] !=NULL) { dh = HDR_MAC_GPRS(txQ[i][down_slot_]); if (dh->dh_wait !=1) { radioSwitch(ON); tx_onto_PHY ( txQ[i][down_slot_] ); txQ[i][down_slot_] = 0; } } } } if (node_->base_stn() != node_->address()) {//node is an MS /* I do nothing here! all taken care of in ms_recv() */ } down_slot_ ++; if (down_slot_ == SLOTS_PER_FRAME ) { down_slot_ = 0; //ie wrap around. } return; } void MacGprs::upslotHandler(Event *e) { int i,len; MobileNode * node_ = (MobileNode*)(netif_->node()); struct hdr_cmn* ch ; if (index_==0 && verbose_==1 ) printf("<%d> %f Up slot %d starts now. \n", index_, NOW, up_slot_); // Restart timer for next slot. mhUpSlot_.start((Packet *)e, slot_time_); if (node_->base_stn() != node_->address()) {//node is an MS if (pktTx[up_slot_] != NULL) { //check if no more pkts to tx, start slot release timer if (q_ !=NULL && gprs_==1){ len= q_->length(); //printf("<%d> %f MAC que=%0x length=%d \n", index_, NOW, q_, len); if (up_slot_ != 0) {// tx_chan[0]=0 always.for random access/signals if ( len==0 && (mhRel_.busy() ==0) && (tx_chan[up_slot_] !=-10) ) { // slot_release_time_ =(2*(SLOTS_PER_FRAME-1) - up_slot_ +1)* slot_time_; slot_release_time_ = ((4*SLOTS_PER_FRAME) ) *slot_time_; mhRel_.start(slot_release_time_); if (verbose_==1) printf("<%d> %f release timer started \n",index_, NOW); } } } else { //printf("<%d> %f MAC que NULL q=%0x \n",index_, NOW, q_ ); } //start transmission radioSwitch(ON); ch = HDR_CMN(pktTx[up_slot_]); if (verbose_==1) printf("<%d> %f in upslothandler. ptype=%s size=%d, slot=%d \n", index_, NOW, packet_info.name(ch->ptype()), ch->size(),up_slot_ ); tx_onto_PHY ( pktTx[up_slot_] ); pktTx[up_slot_] =0; } } if (node_->base_stn() == node_->address()) {//node is itself a BS /* Do nothign here. taken care of in bs_recv() */ } up_slot_ ++; if (up_slot_ == SLOTS_PER_FRAME ) { up_slot_ = 0; //ie wrap around. } return;} void MacGprs::recvHandler(Event *e) { u_int32_t dst, src; int size; Packet * p= (Packet * )e; struct hdr_cmn *ch = HDR_CMN(p); struct hdr_mac_gprs *dh = HDR_MAC_GPRS(p); /* Check for collision. */ if (ch->error() == EF_COLLISION) { ch->error() = 1; } dst = ETHER_ADDR(dh->dh_da); src = ETHER_ADDR(dh->dh_sa); size = ch->size(); if (verbose_==1) printf("<%d> %f pkt [from %d to %d], size = %d uid=%d recv compleeted.\n", index_, NOW, src, dst, size, ch->uid()); //richa radioSwitch(OFF); if ((dst != MAC_BROADCAST) && (dst != (u_int32_t)index_)) { // some error happened somewhere and the pkt landed up with me // somehow... so am dropping. drop(p); return; } fwd_DATA_to_LL(p); return;} // Called after transmission a packet ie when txTimer expires.// Frees the pkt. Turns off the radio.void MacGprs::sendHandler(Event *e) { if (verbose_==1) printf("<%d> %f pkt uid = %d send complete.\n", index_, NOW, HDR_CMN((Packet*)e)->uid()); Packet::free((Packet *)e); radioSwitch(OFF); /* unlock IFQ if not waiting for res_reply. */ if (mhwait_.busy() ==0 ) { if(callback_) { Handler *h = callback_; callback_ = 0; h->handle((Event*) 0); } } return;}// Called when the release timer expiresvoid MacGprs::releaseHandler(){ if (verbose_==1) printf("<%d> %f in release handler. \n" ,index_, NOW ); send_let_go() ; return;} // Called when the back off timer expiresvoid MacGprs::backoffHandler(){ //resend resource request if (verbose_==1) printf("<%d> %f Backoff timer expired \n" , index_, NOW); send_res_request();}// Called when the wait timer expiresvoid MacGprs::waitHandler(){ int temp; double backoff_time; temp = Random::integer(30); backoff_time = ( double)( temp * slot_time_) ; if (verbose_==1) printf("<%d> %f res reply not recvd in 7 TS. will backoff for %fs (%d slots)\n", index_, NOW, backoff_time, temp); // arbitly limited the backoff timer to 30 slots.- // integer(K) gives a uniformly distributed integer btwn 0 and k-1 // maybe shud backoff for some num of frames instead. mhBackoff_.start(backoff_time); //start back off timer} //create a resource release messagevoid MacGprs::send_let_go() { // sent by an MS to its BS int dst, j; Packet *p1 = Packet::alloc(); struct hdr_cmn* ch1= HDR_CMN(p1); struct hdr_mac_gprs* dh1 = HDR_MAC_GPRS(p1); MobileNode * node_ = (MobileNode*)(netif_->node()); //dst=node_->base_stn(); dst=0; this->hdr_src((char*)dh1, this->addr()); this->hdr_dst((char*)dh1, dst); ch1->uid() = 0; ch1->direction() = hdr_cmn::DOWN; ch1->ptype() = PT_MAC; //ch1->size() = slot_packet_len_; ch1->size() = 20; //arbitly set at 20. ch1->iface() = -2; ch1->error() = 0; if (verbose_==1) printf("<%d> %f in send_let_go dst= %d\n", index_, NOW, /*node_->base_stn() */ dst); dh1->dh_fc.fc_type = MAC_Type_Control; dh1->dh_fc.fc_subtype = MAC_Subtype_tx_end; // find upslot/freq to tx on for ( j=1; j< SLOTS_PER_FRAME ; j++) { if ( tx_chan[j] != -10 ) { break; } } ch1->chan() = tx_chan[j]; // sent on freq j, upslot 0. tx_chan[j]= -10; //release the channels... rx_chan[j]= -10; pktTx[j] = p1; return;}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -