Changeset 119


Ignore:
Timestamp:
06/28/2007 07:11:40 PM (5 years ago)
Author:
ayb
Message:

changed implementation of ports

Location:
nano-RK/projects/network_stack
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • nano-RK/projects/network_stack/App.c

    r118 r119  
    4545  uint8_t *rx_buf; 
    4646  int8_t len; 
    47   int16_t srcAddr = 10; 
    48   int8_t srcPort; 
     47  uint16_t srcAddr = 10; 
     48  uint8_t srcPort; 
    4949  int8_t rssi; 
    5050  int8_t i; 
     
    118118                         
    119119                        if(NODE_ADDR == 1) 
     120                        { 
    120121                                if( send(sock, tx_buf, strlen(tx_buf), 2,  SERVER2_PORT, NORMAL_PRIORITY) == NRK_ERROR )         
    121122                                { 
     
    123124                                        flag = 1; 
    124125                                } 
     126                        } 
    125127                        else 
    126128                                if( send(sock, tx_buf, strlen(tx_buf), 1,  SERVER1_PORT, NORMAL_PRIORITY) == NRK_ERROR ) 
  • nano-RK/projects/network_stack/BufferManager.c

    r118 r119  
    1414#include "Debug.h" 
    1515 
     16/***************************** External variables/ functions ************************/ 
     17// From TransportLayerUDP.c 
     18extern Port ports[]; 
     19 
    1620/**************************** Global buffers required by all tasks ******************/ 
    1721 
     
    2125TransmitBuffer tx_buf[MAX_TX_QUEUE_SIZE];                       // transmit queue for the entire system  
    2226 
    23 ReceiveBufferManager rx_buf_mgr[MAX_PORTS + 1]; // create one receive manager per port 
     27ReceiveBufferManager rx_buf_mgr[MAX_PORTS];             // create one receive manager per port 
    2428                                                                                                                                // port 0 is reserved  
    2529TransmitBufferManager tx_buf_mgr;                                       // one transmit manager for the whole system  
     
    2832                                                                                                                                // excess policy settings 
    2933                                                                                                                                 
    30 SendDoneSignal sd_sig[MAX_PORTS + 1];                           // create one send_done signal per port  
    31  
    3234nrk_sem_t *bm_sem;                                                                              // semaphore to access the above variables  
    3335 
     
    488490        { 
    489491                rx_buf_mgr[i].pid = INVALID_PID;                        // pid of task associated with the port  
     492                rx_buf_mgr[i].pindex = -1;                                      // indicates absence of rbm/port mapping  
    490493                rx_buf_mgr[i].head_fq = NULL; 
    491494                rx_buf_mgr[i].tail_fq = NULL; 
     
    494497                rx_buf_mgr[i].countTotal = 0; 
    495498                rx_buf_mgr[i].countFree = 0; 
    496            rx_buf_mgr[i].seg_arrived_signal = nrk_signal_create(); 
    497            sd_sig[i].send_done_signal = nrk_signal_create(); 
    498499        } 
    499500         
     
    536537                        return TRUE; 
    537538        } 
    538         return FALSE; 
     539        return FALSE;  
    539540} 
    540541/*******************************************************************************************/ 
     
    591592} 
    592593/**************************************************************************************/ 
     594int8_t port_to_rbm_index(uint8_t port) 
     595{ 
     596        int8_t i;       // loop index 
     597         
     598        for(i = 0; i < MAX_PORTS; i++) 
     599                if(rx_buf_mgr[i].pindex != -1)  // valid rbm element  
     600                        if(ports[rx_buf_mgr[i].pindex].pno == port)     // found the matching rbm element  
     601                                return i; 
     602         
     603        // if the code reaches here, it means no match was found 
     604        return NRK_ERROR; 
     605} 
     606/**************************************************************************************/ 
     607int8_t port_to_port_index(uint8_t port) 
     608{ 
     609        int8_t i;       // loop index 
     610         
     611        for(i = 0; i < MAX_PORTS; i++) 
     612                if(ports[i].pno == port)        // match found 
     613                        return i; 
     614         
     615        // if the code reaches here, it means no match was found 
     616        return NRK_ERROR; 
     617} 
     618/**************************************************************************************/ 
    593619void insert_rx_pq(Transport_Segment_UDP *seg, int8_t prio, uint16_t addr, int8_t rssi) 
    594620{ 
    595         int8_t port;                                                    // destination port of the segment 
    596          
    597         //enter_cr(bm_sem, "insert_rx_pq()"); 
    598           
    599         port = seg -> destPort;  
    600         ReceiveBufferUDP *buf = remove_rx_fq(port, EMPTY);              // remove an EMPTY buffer from the free queue 
     621        int8_t rbm_index;                                               // index of the corresponding rbm element  
     622         
     623        rbm_index = port_to_rbm_index(seg -> destPort);  
     624        ReceiveBufferUDP *buf = remove_rx_fq(rbm_index, EMPTY);         // remove an EMPTY buffer from the free queue 
    601625         
    602626        // pointers for traversing the port queue  
    603         ReceiveBufferUDP *ptr = rx_buf_mgr[port].head_pq;                                 
     627        ReceiveBufferUDP *ptr = rx_buf_mgr[rbm_index].head_pq;                            
    604628        ReceiveBufferUDP *prev = NULL;  
    605629         
     
    625649                 
    626650                if(prev == NULL)        // the newly received segment is to be inserted at the head of the port queue  
    627                         rx_buf_mgr[port].head_pq = buf; 
     651                        rx_buf_mgr[rbm_index].head_pq = buf; 
    628652                else 
    629653                        prev -> next = buf; 
     
    641665         
    642666        // check for 2  
    643         if(rx_buf_mgr[port].head_pq == NULL) 
    644         { 
    645                 if(rx_buf_mgr[port].countFree != 0)     // debugging purposes  
     667        if(rx_buf_mgr[rbm_index].head_pq == NULL) 
     668        { 
     669                if(rx_buf_mgr[rbm_index].countFree != 0)        // debugging purposes  
    646670                { 
    647671                        nrk_int_disable(); 
     
    658682        // in the port queue has to be replaced    
    659683        // check if the last segment in the port queue has a lower priority 
    660         if( (rx_buf_mgr[port].tail_pq) -> prio < prio ) // this last segment should be replaced 
     684        if( (rx_buf_mgr[rbm_index].tail_pq) -> prio < prio ) // this last segment should be replaced 
    661685        { 
    662686                // at this point we know that ptr cannot be NULL                 
    663687                // find the last but one segment in the port queue  
    664688                 
    665                 ReceiveBufferUDP *qtr = rx_buf_mgr[port].head_pq; 
     689                ReceiveBufferUDP *qtr = rx_buf_mgr[rbm_index].head_pq; 
    666690                // corner case, see if there is only one buffer in the port queue  
    667                 if(rx_buf_mgr[port].head_pq == rx_buf_mgr[port].tail_pq) 
     691                if(rx_buf_mgr[rbm_index].head_pq == rx_buf_mgr[rbm_index].tail_pq) 
    668692                { 
    669693                        // replace the members of this last buffer with those of the new one  
     
    682706                                 
    683707                        // replace the members of the last buffer with those of the new one 
    684                         (rx_buf_mgr[port].tail_pq) -> seg = *seg; 
    685                         (rx_buf_mgr[port].tail_pq) -> prio = prio; 
    686                         (rx_buf_mgr[port].tail_pq) -> srcAddr = addr; 
    687                         (rx_buf_mgr[port].tail_pq) -> rssi = rssi; 
     708                        (rx_buf_mgr[rbm_index].tail_pq) -> seg = *seg; 
     709                        (rx_buf_mgr[rbm_index].tail_pq) -> prio = prio; 
     710                        (rx_buf_mgr[rbm_index].tail_pq) -> srcAddr = addr; 
     711                        (rx_buf_mgr[rbm_index].tail_pq) -> rssi = rssi; 
    688712                                 
    689713                        // adjust the pointers so that this new segment occupies its correct position in the port queue                  
    690                         (rx_buf_mgr[port].tail_pq) -> next = ptr; 
     714                        (rx_buf_mgr[rbm_index].tail_pq) -> next = ptr; 
    691715                        if(prev == NULL) 
    692                                 rx_buf_mgr[port].head_pq = rx_buf_mgr[port].tail_pq; 
     716                                rx_buf_mgr[rbm_index].head_pq = rx_buf_mgr[rbm_index].tail_pq; 
    693717                        else 
    694                                 prev -> next = rx_buf_mgr[port].tail_pq; 
     718                                prev -> next = rx_buf_mgr[rbm_index].tail_pq; 
    695719                                         
    696720                        qtr -> next = NULL;                              
    697                         rx_buf_mgr[port].tail_pq = qtr; // change the tail of the port queue 
     721                        rx_buf_mgr[rbm_index].tail_pq = qtr;    // change the tail of the port queue 
    698722                         
    699723                        //leave_cr(bm_sem, "insert_rx_pq()"); 
     
    701725                } 
    702726        } // end if  
    703         else if( (rx_buf_mgr[port].tail_pq) -> prio == prio )    
     727        else if( (rx_buf_mgr[rbm_index].tail_pq) -> prio == prio )       
    704728                  { 
    705729                                // check the setting of the excess policy for this priority level  
     
    707731                                { 
    708732                                        // replace the members of the last buffer with those of the new one  
    709                                         (rx_buf_mgr[port].tail_pq) -> seg = *seg; 
    710                                         (rx_buf_mgr[port].tail_pq) -> prio = prio; 
    711                                         (rx_buf_mgr[port].tail_pq) -> srcAddr = addr; 
    712                                         (rx_buf_mgr[port].tail_pq) -> rssi = rssi; 
     733                                        (rx_buf_mgr[rbm_index].tail_pq) -> seg = *seg; 
     734                                        (rx_buf_mgr[rbm_index].tail_pq) -> prio = prio; 
     735                                        (rx_buf_mgr[rbm_index].tail_pq) -> srcAddr = addr; 
     736                                        (rx_buf_mgr[rbm_index].tail_pq) -> rssi = rssi; 
    713737                                         
    714738                                        //leave_cr(bm_sem, "insert_rx_pq()"); 
     
    732756} 
    733757/*************************************************************************************************/ 
    734 ReceiveBufferUDP* remove_rx_pq(int8_t port) 
     758ReceiveBufferUDP* remove_rx_pq(int8_t rbm_index) 
    735759{ 
    736760        // The remove is always done from the head of the queue since its a priority queue       
     
    739763        //enter_cr(bm_sem, "remove_rx_pq()"); 
    740764                         
    741         if(rx_buf_mgr[port].head_pq == NULL)    // no more queued segments 
     765        if(rx_buf_mgr[rbm_index].head_pq == NULL)       // no more queued segments 
    742766        { 
    743767                //leave_cr(bm_sem, "remove_rx_pq()"); 
     
    745769        } 
    746770                 
    747         if(rx_buf_mgr[port].head_pq == rx_buf_mgr[port].tail_pq)        // only one buffer in queue  
    748                 rx_buf_mgr[port].tail_pq = NULL; 
    749                  
    750         ptr = rx_buf_mgr[port].head_pq; 
    751          
    752         rx_buf_mgr[port].head_pq = rx_buf_mgr[port].head_pq -> next; 
     771        if(rx_buf_mgr[rbm_index].head_pq == rx_buf_mgr[rbm_index].tail_pq)      // only one buffer in queue  
     772                rx_buf_mgr[rbm_index].tail_pq = NULL; 
     773                 
     774        ptr = rx_buf_mgr[rbm_index].head_pq; 
     775         
     776        rx_buf_mgr[rbm_index].head_pq = rx_buf_mgr[rbm_index].head_pq -> next; 
    753777                 
    754778        //leave_cr(bm_sem, "remove_rx_pq()");    
     
    757781/*************************************************************************************************/ 
    758782 
    759 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t port, int8_t status) 
    760 { 
    761         //enter_cr(bm_sem, "insert_rx_fq()");    
    762                  
     783void insert_rx_fq(ReceiveBufferUDP *buf, int8_t rbm_index, int8_t status) 
     784{ 
    763785        // the insert is always done at the tail of the queue  
    764         if(rx_buf_mgr[port].head_fq == NULL)    // the free queue is empty  
    765         { 
    766                 rx_buf_mgr[port].head_fq = rx_buf_mgr[port].tail_fq = buf; 
     786        if(rx_buf_mgr[rbm_index].head_fq == NULL)       // the free queue is empty  
     787        { 
     788                rx_buf_mgr[rbm_index].head_fq = rx_buf_mgr[rbm_index].tail_fq = buf; 
    767789                buf -> next = NULL; 
    768790        }        
    769791        else 
    770792        { 
    771                 rx_buf_mgr[port].tail_fq -> next = buf; 
     793                rx_buf_mgr[rbm_index].tail_fq -> next = buf; 
    772794                buf -> next = NULL; 
    773                 rx_buf_mgr[port].tail_fq = buf; 
     795                rx_buf_mgr[rbm_index].tail_fq = buf; 
    774796        } 
    775797 
    776798        if(status == EMPTY)     // this function was called from bind() / setReceiveQueueSize() 
    777799        { 
    778                 rx_buf_mgr[port].countFree++; 
     800                rx_buf_mgr[rbm_index].countFree++; 
    779801                buf -> status = EMPTY; 
    780802        } 
    781803         
    782         //leave_cr(bm_sem, "insert_rx_fq()");    
    783804        return; 
    784805} 
    785806 
    786807/***************************************************************************************************/ 
    787 ReceiveBufferUDP* remove_rx_fq(uint8_t port, int8_t status) 
     808ReceiveBufferUDP* remove_rx_fq(int8_t rbm_index, int8_t status) 
    788809{ 
    789810        // This function removes the next receive buffer from the free queue having a given status       
     
    791812        ReceiveBufferUDP *prev = NULL; 
    792813         
    793         if(rx_buf_mgr[port].head_fq == NULL)    // no buffers remaining in free queue    
     814        if(rx_buf_mgr[rbm_index].head_fq == NULL)       // no buffers remaining in free queue    
    794815                return NULL; 
    795816                 
    796         ptr = rx_buf_mgr[port].head_fq; 
     817        ptr = rx_buf_mgr[rbm_index].head_fq; 
    797818        while(ptr != NULL) 
    798819        { 
     
    812833        if(prev == NULL)        // the EMPTY / FULL buffer is at the head of the queue 
    813834        { 
    814                 if(rx_buf_mgr[port].head_fq == rx_buf_mgr[port].tail_fq)        // only one buffer in free queue 
    815                         rx_buf_mgr[port].tail_fq = NULL; 
    816                          
    817                 rx_buf_mgr[port].head_fq = rx_buf_mgr[port].head_fq -> next; 
     835                if(rx_buf_mgr[rbm_index].head_fq == rx_buf_mgr[rbm_index].tail_fq)      // only one buffer in free queue 
     836                        rx_buf_mgr[rbm_index].tail_fq = NULL; 
     837                         
     838                rx_buf_mgr[rbm_index].head_fq = rx_buf_mgr[rbm_index].head_fq -> next; 
    818839                if(status == EMPTY) 
    819                         rx_buf_mgr[port].countFree--; 
     840                        rx_buf_mgr[rbm_index].countFree--; 
    820841                return ptr; 
    821842        } 
     
    824845        prev -> next = ptr -> next; 
    825846        if(status == EMPTY) 
    826                 rx_buf_mgr[port].countFree--; 
     847                rx_buf_mgr[rbm_index].countFree--; 
    827848        return ptr; 
    828849} 
     
    940961/**************************************************************************************************/ 
    941962 
    942 int8_t get_in_process_buf_count(int8_t port) 
     963int8_t get_in_process_buf_count(int8_t rbm_index) 
    943964{ 
    944965        int8_t count = 0; 
     
    946967        //enter_cr(bm_sem, "get_in_process_buf_count()"); 
    947968         
    948         ReceiveBufferUDP *ptr = rx_buf_mgr[port].head_fq; 
     969        ReceiveBufferUDP *ptr = rx_buf_mgr[rbm_index].head_fq; 
    949970         
    950971        while(ptr != NULL) 
  • nano-RK/projects/network_stack/BufferManager.h

    r118 r119  
    5959typedef struct 
    6060{ 
    61         int8_t pid;                                                             // pid of task associated with the port  
     61        int8_t pid;                                                             // pid of task associated with the port 
     62        int8_t pindex;                                                  // index of associated port element  
    6263        ReceiveBufferUDP *head_fq;                      // pointer to head of free queue (EMPTY or FULL buffers) 
    6364        ReceiveBufferUDP *tail_fq;                      // pointer to tail of free queue (EMPTY or FULL buffers)  
     
    6667        int8_t countTotal;                                      // total number of buffers reserved for this port  
    6768        int8_t countFree;                                               // number of buffers EMPTY at any time  
    68         nrk_sig_t seg_arrived_signal;   // is sent by network layer when a segment is inserted 
    69                                                                                                 // into the port queue  
    7069}ReceiveBufferManager;                                  // one for every port in the system  
    7170 
     
    154153*/ 
    155154 
     155int8_t port_to_rbm_index(uint8_t port); 
     156/* 
     157This function finds the index of the rbm element corresponding to a given port number 
     158 
     159        PARAMS:         port: The port number 
     160        RETURNS:                the index of the rbm element if present or NRK_ERROR if no match is found  
     161        Comments:       private function 
     162*/ 
     163 
    156164void insert_rx_pq(Transport_Segment_UDP *seg, int8_t prio, uint16_t addr, int8_t rssi); 
    157165/* 
     
    168176*/ 
    169177 
    170 ReceiveBufferUDP* remove_rx_pq(int8_t port); 
     178ReceiveBufferUDP* remove_rx_pq(int8_t rbm_index); 
    171179/* 
    172180This function removes the receive buffer at the head of a given port queue 
    173181 
    174         PARAMS:         port: The port 
     182        PARAMS:         rbm_index: the index of the relevant receive buffer manager 
    175183        RETURNS:                pointer to the receive buffer or NULL if the port queue is empty  
    176184        COMMENTS:       private function 
    177185*/  
    178186 
    179 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t port, int8_t status); 
     187void insert_rx_fq(ReceiveBufferUDP *buf, int8_t rbm_index, int8_t status); 
    180188/*  
    181189This function inserts a given receive buffer into the free queue of a given port 
     
    183191 
    184192        PARAMS:         buf:    pointer to the receive buffer 
    185                                         port: the port number associated with the free queue 
     193                                        rbm_index: the index of the relevant receive buffer manager 
    186194                                        status: EMPTY or FULL 
    187195                                         
     
    190198*/  
    191199 
    192 ReceiveBufferUDP* remove_rx_fq(uint8_t port, int8_t status); 
     200ReceiveBufferUDP* remove_rx_fq(int8_t rbm_index, int8_t status); 
    193201/* 
    194202This function removes a receive buffer from the free queue with the given status 
    195203 
    196         PARAMS:         port: the port number associated with the free queue 
     204        PARAMS:         rbm_index: the index of the relevant receive buffer manager 
    197205                                        status: EMPTY or FULL 
    198206                                         
     
    242250*/  
    243251 
    244 int8_t get_in_process_buf_count(int8_t port); 
     252int8_t get_in_process_buf_count(int8_t rbm_index); 
    245253/* 
    246254This function returns the number of FULL buffers in the free queue associated with a given port 
    247255 
    248         PARAMS:         port: The port number 
     256        PARAMS:         rbm_index: the index of the relevant receive buffer manager 
    249257        RETURNS:                number of FULL buffers in the free queue  
    250258        COMMENTS:       private function 
  • nano-RK/projects/network_stack/NWErrorCodes.h

    r118 r119  
    1919#define NO_TX_BUFFERS_AVAILABLE 12  
    2020#define SOCKET_TIMEOUT 13  
     21#define UNMAPPED_SOCKET 14 
     22#define NO_PORT_ELEMENT_AVAILABLE 15 
     23#define NO_RBM_ELEMENT_AVAILABLE 16  
    2124 
    2225#endif  
  • nano-RK/projects/network_stack/NetworkLayer.c

    r118 r119  
    2323// From TransportLayerUDP.c  
    2424extern nrk_sem_t *tl_sem; 
    25  
    26 extern int8_t is_port_associated(int8_t); 
     25extern Port ports[]; 
     26 
     27extern int8_t is_port_associated(int16_t port); 
    2728 
    2829//From BufferManager.c  
    2930extern nrk_sem_t *bm_sem; 
    30 extern SendDoneSignal sd_sig[]; 
    3131extern ReceiveBufferManager rx_buf_mgr[]; 
    3232 
    33 extern void insert_rx_queue(Transport_Segment_UDP *, uint8_t, uint8_t, int8_t); 
    3433extern void insert_rx_pq(Transport_Segment_UDP*, int8_t, uint16_t, int8_t); 
    3534extern TransmitBuffer* remove_tx_aq(); 
    3635extern void insert_tx_fq(TransmitBuffer*); 
    37 extern void enter_cr(nrk_sem_t *, int8_t *); 
    38 extern void leave_cr(nrk_sem_t *, int8_t *); 
     36extern void enter_cr(nrk_sem_t *, int8_t); 
     37extern void leave_cr(nrk_sem_t *, int8_t); 
     38extern int8_t port_to_port_index(uint8_t); 
    3939 
    4040 
     
    4444extern void pack_NodeToGatewaySerial_Packet_header(uint8_t *, NodeToGatewaySerial_Packet *); 
    4545extern void pack_TL_UDP_header(uint8_t *, Transport_Segment_UDP*); 
     46extern void pack_NW_Packet_header(uint8_t *,NW_Packet*);  
    4647 
    4748extern void unpack_Msg_NgbList(Msg_NgbList*, uint8_t *); 
     
    267268                // check to see if the destination port in the header is associated with any socket  
    268269                enter_cr(bm_sem, 28); 
     270                enter_cr(tl_sem, 28); 
     271                 
    269272                if(is_port_associated(udp_seg.destPort) == TRUE)        // yes there is  
    270273                { 
     274                        int8_t port_index; 
     275                         
    271276                        insert_rx_pq(&udp_seg, pkt -> prio, pkt -> src, rssi); 
    272                         nrk_event_signal(rx_buf_mgr[udp_seg.destPort].seg_arrived_signal);      // signal 'seg arrived'  
     277                         
     278                        port_index = port_to_port_index(udp_seg.destPort); 
     279                        if(port_index == NRK_ERROR)     //sanity check for debugging 
     280                        { 
     281                                nrk_int_disable(); 
     282                                nrk_led_set(RED_LED); 
     283                                while(1) 
     284                                        nrk_kprintf(PSTR("process_app_pkt(): Bug detected in implementation of port/rbm element array\r\n")); 
     285                        }                        
     286                        nrk_event_signal(ports[port_index].data_arrived_signal);        // signal 'data arrived'  
    273287                } 
    274288         
    275289                else // if there is no socket associated with this port, simply drop the packet 
    276290                        ; 
    277                          
     291                leave_cr(tl_sem, 28);    
    278292                leave_cr(bm_sem, 28); 
    279293        } 
     
    381395        pkt_tx.prio = NORMAL_PRIORITY; 
    382396         
    383         pack_Msg_NgbList(pkt_tx.data, m); 
     397        pack_Msg_Hello(pkt_tx.data, m); 
    384398        return; 
    385399} 
     
    514528void nl_tx_task() 
    515529{ 
    516         TransmitBuffer *ptr;                            // pointer to the buffer to be transmitted  
    517         Transport_Segment_UDP *seg;     // temporary variable to retrieve the source port of the message  
    518         nrk_sig_t tx_done_signal;               // to hold the tx_done signal from the link layer  
    519         int8_t ret;                                                     // to hold the return value of various functions  
     530        TransmitBuffer *ptr = NULL;                             // pointer to the buffer to be transmitted  
     531        Transport_Segment_UDP *seg = NULL;         // temporary variable to retrieve the source port of the message  
     532        nrk_sig_t tx_done_signal;                                       // to hold the tx_done signal from the link layer  
     533        int8_t ret;                                                                             // to hold the return value of various functions  
     534        int8_t port_index;                                                      // to store the index of the corresponding port element  
    520535         
    521536        //static Msg_NgbList mnlist; 
     
    548563                }  
    549564                         
    550                 if( pkt_type((ptr -> pkt).type) == APPLICATION ) 
     565                if( pkt_type(&(ptr -> pkt)) == APPLICATION ) 
    551566                {                
    552567                        // remove the encapsulated TL segment from the packet  
     
    555570                         
    556571                // pack the network packet header into the transmit buffer  
    557                 pack_NW_Packet_header(tx_buf, ptr -> pkt); 
     572                pack_NW_Packet_header(tx_buf, &(ptr -> pkt)); 
    558573                // append the application payload into the transmit buffer 
    559574                memcpy(tx_buf + SIZE_NW_PACKET_HEADER, (ptr -> pkt).data, (ptr -> pkt).length); 
     
    570585                 
    571586                ret = nrk_event_wait (SIG(tx_done_signal)); 
    572            if(ret & SIG(tx_done_signal) == 0 )  
     587           if((ret & SIG(tx_done_signal)) == 0 )  
    573588                { 
    574589                        nrk_int_disable(); 
     
    579594                // signal 'send done' signal  
    580595                enter_cr(bm_sem, 34); 
    581                 nrk_event_signal(sd_sig[seg -> srcPort].send_done_signal); 
     596                enter_cr(tl_sem, 34); 
     597                port_index = port_to_port_index(seg -> srcPort); 
     598                nrk_event_signal(ports[port_index].send_done_signal); 
     599                leave_cr(tl_sem, 34);            
    582600                leave_cr(bm_sem, 34); 
    583601   } 
  • nano-RK/projects/network_stack/TransportLayerUDP.c

    r118 r119  
    99/****************** Global data structures required for port and socket management ***************/ 
    1010 
    11 Socket sock[MAX_PORTS];                         // array to store available socket descriptors 0 - (MAX_PORTS - 1)  
    12                                                                                         // a pid value of -1 (INVALID_PID) indicates that it is unassigned  
    13  
    14 uint32_t ports;                                         // a 32 bit bit vector representing the available ports                                                                                  
     11Socket sock[MAX_PORTS];                         // array to store available socket descriptors 0 - (MAX_PORTS - 1) 
     12 
     13Port ports[MAX_PORTS];                          // array to store available port numbers (1 - MAX_PORT_NUM) 
    1514 
    1615int8_t tlayer_init_done;                        // flag to indicate whether initialization has been done correctly  
     
    3332 
    3433extern void enter_cr(nrk_sem_t *, int8_t); 
    35 extern void leave_cr(nrk_sem_t *, int8_t *); 
     34extern void leave_cr(nrk_sem_t *, int8_t); 
    3635extern int8_t get_num_bufs_free(); 
    3736extern void insert_rx_fq(ReceiveBufferUDP*, int8_t, int8_t); 
     
    5049{ 
    5150        int8_t i;       // loop index  
    52         // make all the socket descriptors unassigned  
     51         
     52        // make the socket descriptors and ports unassigned  
    5353        for(i = 0; i < MAX_PORTS; i++) 
    5454        { 
    55                 sock[i].pid = INVALID_PID; 
    56                 sock[i].port = INVALID_PORT; 
    57                 sock[i].timeout.secs = 0;               // indicates no timeout  
    58                 sock[i].timeout.nano_secs = 0;  
    59         } 
    60         // make all the ports available  
    61         ports = 0; 
    62          
    63         tl_sem = nrk_sem_create(1,MAX_TASK_PRIORITY);   // create the mutex   
     55                sock[i].pindex = -1;                                    // indicates the absence of socket-port mapping  
     56                sock[i].rbmindex = -1;                          // indicates the absence of socket-port mapping  
     57                sock[i].pid = INVALID_PID;                      // indicates an unassigned socket descriptor  
     58                sock[i].timeout.secs = 0;                       // indicates no timeout  
     59                sock[i].timeout.nano_secs = 0;  // indicates no timeout  
     60                 
     61                ports[i].pno = INVALID_PORT;            // indicates an unassigned port number           
     62        } 
     63         
     64        // create the mutex with priority ceiling equal to MAX_TASK_PRIORITY     
     65        tl_sem = nrk_sem_create(1,MAX_TASK_PRIORITY);    
    6466        if(tl_sem == NULL) 
    6567        { 
     
    7577/**************************************************************************************************/ 
    7678int8_t get_next_available_socket() 
    77 // private function. This returns the index of the next available socket descriptor or NRK_ERROR if 
    78 // none are available  
    7979{ 
    8080        int8_t i;               // loop index 
     
    122122        } 
    123123                 
    124         leave_cr(tl_sem, "create_socket()"); 
     124        leave_cr(tl_sem, 3); 
    125125        return result;  
    126126} 
    127127 
    128128/*****************************************************************************************/ 
    129 int8_t get_next_available_port() 
    130 // This private function gets the next available ephemeral port number if present or NRK_ERROR 
    131 // if all are taken  
    132 { 
    133         int8_t i;       // loop index  
    134         if(DEBUG_TL == 2) 
    135                 printf("Inside get_next_available_ports(). Value of ports = %d\r\n", ports);  
    136          
    137         //for(i = EPHEMERAL_PORT_NUM_START; i <= MAX_PORTS; i++)        // check only for ephemeral port numbers 
    138         for(i = 3; i <= MAX_PORTS; i++)  
    139         { 
    140                 if(DEBUG_TL == 2) 
    141                         printf("Inside get_next_available_ports(). i = %d\r\n", i); 
    142                 if( ((ports >> i) & ((uint32_t)1)) == 0 )       // bit 'i' is 0  
    143                         return i; 
    144         } 
     129uint8_t get_next_available_port() 
     130{ 
     131        uint16_t i;     // loop index for ephemeral port numbers 
     132        int8_t j;       // loop index 
     133         
     134        for(i = EPHEMERAL_PORT_NUM_START; i <= MAX_PORT_NUM; i++) 
     135        { 
     136                // check to see if port 'i' is available 
     137                for(j = 0; j < MAX_PORTS; j++) 
     138                        if(ports[j].pno == i)   // port 'i' has been taken 
     139                                break; 
     140                                 
     141                if(j == MAX_PORTS)                      // port 'i' is free 
     142                        return i;                                       // return this ephemeral port number back 
     143        }  
     144         
     145        // if the code reaches there, it means that all ephemeral port numbers have been taken. 
     146        // This also means that the 'ports' array is full  
     147          
    145148        _nrk_errno_set(NO_PORTS_AVAILABLE);      
    146         return NRK_ERROR;                                                         
     149        return INVALID_PORT;                                                      
    147150} 
    148151/******************************************************************************************/ 
    149 int8_t check_port_available(int8_t pt) 
    150 // This private function checks to see if port 'pt' is available and returns NRK_OK or NRK_ERROR  
    151 // accordingly  
    152 { 
    153         if( ((ports >> pt) & ((uint32_t)1)) == 0 )                      // bit 'pt' is 0  
     152int8_t check_port_available(uint8_t pt) 
     153{ 
     154        int8_t i;       // loop index 
     155         
     156        for(i = 0; i < MAX_PORTS; i++) 
     157                if(ports[i].pno == pt)  // port 'pt' has been taken 
     158                        break; 
     159                         
     160        if(i == MAX_PORTS)                      // port 'pt' is available  
    154161                return NRK_OK; 
    155          
     162                 
    156163        _nrk_errno_set(PORT_UNAVAILABLE);        
    157164        return NRK_ERROR; 
    158165} 
    159166/*****************************************************************************************/ 
    160 void assign_port(int8_t pt) 
    161 // This private function records the fact that port 'pt' has been assigned  
    162 { 
    163         ports |= (uint32_t)1 << pt;                     // set bit 'pt' in 'ports' 
     167void assign_port(int8_t pindex, uint8_t pt) 
     168{ 
     169        int8_t ret1, ret2;       
     170         
     171        // fill up the members of the port element 
     172        ports[pindex].pno = pt; 
     173        ports[pindex].send_done_signal = nrk_signal_create(); 
     174        ports[pindex].data_arrived_signal = nrk_signal_create(); 
     175         
     176        if( ports[pindex].send_done_signal == NRK_ERROR ) 
     177        { 
     178                nrk_int_disable(); 
     179                nrk_led_set(RED_LED); 
     180                while(1) 
     181                        nrk_kprintf(PSTR("assign_port(): Error creating the send_done signal\r\n")); 
     182        } 
     183         
     184        if( ports[pindex].data_arrived_signal == NRK_ERROR ) 
     185        { 
     186                nrk_int_disable(); 
     187                nrk_led_set(RED_LED); 
     188                while(1) 
     189                        nrk_kprintf(PSTR("assign_port(): Error creating the data_arrived signal\r\n")); 
     190        } 
     191         
     192        ret1 = nrk_signal_register(ports[pindex].send_done_signal); 
     193        ret2 = nrk_signal_register(ports[pindex].data_arrived_signal); 
     194         
     195        if(ret1 == NRK_ERROR) 
     196        { 
     197                nrk_int_disable(); 
     198                nrk_led_set(RED_LED); 
     199                while(1) 
     200                        nrk_kprintf(PSTR("assign_port(): Error registering the send_done signal\r\n")); 
     201        } 
     202         
     203        if(ret2 == NRK_ERROR) 
     204        { 
     205                nrk_int_disable(); 
     206                nrk_led_set(RED_LED); 
     207                while(1) 
     208                        nrk_kprintf(PSTR("assign_port(): Error registering the data_arrived signal\r\n")); 
     209        } 
     210                 
    164211        return; 
    165212} 
    166213/******************************************************************************************/ 
    167 void release_port(int8_t pt) 
    168 // This private function records the fact that port 'pt' is now released  
    169 { 
    170         ports &= ~( (uint32_t)1 << pt );                // clear bit 'pt' in 'ports' 
     214void release_port(int8_t pindex) 
     215{ 
     216        int8_t ret1, ret2; 
     217 
     218        // invalidate members of the port element  
     219        ports[pindex].pno = INVALID_PID; 
     220        ret1 = nrk_signal_delete(ports[pindex].send_done_signal); 
     221        ret2 = nrk_signal_delete(ports[pindex].data_arrived_signal); 
     222         
     223        if(ret1 == NRK_ERROR) 
     224        { 
     225                nrk_int_disable(); 
     226                nrk_led_set(RED_LED); 
     227                while(1) 
     228                        nrk_kprintf(PSTR("assign_port(): Error creating the send_done signal\r\n")); 
     229        } 
     230 
     231        if(ret2 == NRK_ERROR) 
     232        { 
     233                nrk_int_disable(); 
     234                nrk_led_set(RED_LED); 
     235                while(1) 
     236                        nrk_kprintf(PSTR("assign_port(): Error creating the data_arrived signal\r\n")); 
     237        }        
     238         
    171239        return;  
    172240} 
    173  
    174241/*****************************************************************************************/ 
    175 int8_t bind(int8_t sock_num, int8_t port) 
    176 { 
    177         int8_t index;                                                   // stores the index of next available receive buffer                      
     242int8_t get_index_unassigned_port_element() 
     243{ 
     244        int8_t i; 
     245         
     246        for(i = 0; i < MAX_PORTS; i++) 
     247                if(ports[i].pno == INVALID_PORT) 
     248                        return i; 
     249                         
     250        _nrk_errno_set(NO_PORT_ELEMENT_AVAILABLE); 
     251        return NRK_ERROR; 
     252} 
     253/*******************************************************************************************/ 
     254int8_t get_index_unassigned_rbm_element() 
     255{ 
     256        int8_t i; 
     257         
     258        for(i = 0; i < MAX_PORTS; i++) 
     259                if(rx_buf_mgr[i].pindex == -1) 
     260                        return i; 
     261                         
     262        _nrk_errno_set(NO_RBM_ELEMENT_AVAILABLE); 
     263        return NRK_ERROR; 
     264} 
     265/*****************************************************************************************/ 
     266uint8_t get_port_num(int8_t sock_num) 
     267{ 
     268        // ERROR checking 
     269        if(sock_num < 0 || sock_num > MAX_PORTS) 
     270        { 
     271                _nrk_errno_set(INVALID_ARGUMENT); 
     272                return INVALID_PORT; 
     273        } 
     274         
     275        if(sock[sock_num].pid != nrk_get_pid()) 
     276        { 
     277                _nrk_errno_set(INVALID_SOCKET); 
     278                return INVALID_PORT; 
     279        } 
     280         
     281        if(sock[sock_num].pindex == -1) 
     282        { 
     283                _nrk_errno_set(UNMAPPED_SOCKET); 
     284                return INVALID_PORT; 
     285        } 
     286         
     287        return ports[sock[sock_num].pindex].pno; 
     288} 
     289 
     290/*****************************************************************************************/ 
     291int8_t bind(int8_t sock_num, int16_t port) 
     292{ 
     293        int8_t buf_index;                                               // stores the index of next available receive buffer 
     294        int8_t port_index;                                      // stores the index of the next unassigned port element 
     295        int8_t rbm_index;                                               // stores the index of the next unassigned rbm element                    
    178296        int8_t i;                                                               // loop index  
    179297        int8_t size;                                                    // size of receive queue allocated for this port  
     
    195313                _nrk_errno_set(INVALID_ARGUMENT); 
    196314                 
    197                 leave_cr(tl_sem, "bind()");      
    198                 leave_cr(bm_sem, "bind()");      
    199                 return NRK_ERROR; 
    200         } 
    201                  
    202         if(port <= 0 || port > MAX_PORTS)                       // bad input  
    203         { 
    204                 _nrk_errno_set(INVALID_ARGUMENT); 
    205                  
    206                 leave_cr(tl_sem, "bind()");      
    207                 leave_cr(bm_sem, "bind()"); 
     315                leave_cr(tl_sem, 8);     
     316                leave_cr(bm_sem, 8);     
     317                return NRK_ERROR; 
     318        } 
     319                 
     320        if(port <= 0 || port > MAX_PORT_NUM)                    // bad input  
     321        { 
     322                _nrk_errno_set(INVALID_ARGUMENT); 
     323                 
     324                leave_cr(tl_sem, 8);     
     325                leave_cr(bm_sem, 8); 
    208326                return NRK_ERROR; 
    209327        } 
     
    213331                _nrk_errno_set(INVALID_SOCKET); 
    214332                 
    215                 leave_cr(tl_sem, "bind()");      
    216                 leave_cr(bm_sem, "bind()"); 
    217                 return NRK_ERROR; 
    218         } 
    219          
    220         if(rx_buf_mgr[port].pid == nrk_get_pid())       // bind() / set_rx_queue_size() was called earlier 
     333                leave_cr(tl_sem, 8);     
     334                leave_cr(bm_sem, 8); 
     335                return NRK_ERROR; 
     336        } 
     337         
     338        if(sock[sock_num].rbmindex != -1)                       // bind() / set_rx_queue_size() was called earlier 
    221339        { 
    222340                _nrk_errno_set(INVALID_CALL); 
    223341                 
    224                 leave_cr(tl_sem, "bind()");      
    225                 leave_cr(bm_sem, "bind()"); 
    226                 return NRK_ERROR; 
    227         } 
    228          
    229         if(check_port_available(port) == NRK_ERROR)             // check to see if port is already taken  
    230         { 
    231                 leave_cr(tl_sem, "bind()");      
    232                 leave_cr(bm_sem, "bind()");      
     342                leave_cr(tl_sem, 8);     
     343                leave_cr(bm_sem, 8); 
     344                return NRK_ERROR; 
     345        } 
     346         
     347        if(check_port_available((uint8_t)port) == FALSE)        // check to see if port is already taken  
     348        { 
     349                leave_cr(tl_sem, 8);     
     350                leave_cr(bm_sem, 8);     
    233351                return NRK_ERROR;                                                                               // error no is already set = PORT_UNAVAILABLE 
    234352        }  
     
    238356                _nrk_errno_set(NO_RX_BUFFERS_AVAILABLE); 
    239357                 
    240                 leave_cr(tl_sem, "bind()");      
    241                 leave_cr(bm_sem, "bind()"); 
    242                 return NRK_ERROR; 
    243         } 
    244                                  
     358                leave_cr(tl_sem, 8);     
     359                leave_cr(bm_sem, 8); 
     360                return NRK_ERROR; 
     361        } 
     362         
     363        if(sock[sock_num].pindex != -1)                                         // sanity check for debugging 
     364        { 
     365                nrk_int_disable(); 
     366                nrk_led_set(RED_LED); 
     367                while(1) 
     368                        nrk_kprintf(PSTR("bind(): Bug detected in implementation of port / rbm element array\r\n")); 
     369        } 
     370         
     371        // error checking complete. Begin processing                             
    245372        size = DEFAULT_RX_QUEUE_SIZE; 
    246373        // at this point, we know that 
     
    249376        // 3. bind() / setReceiveQueueSize() was not called earlier 
    250377         
    251         // update the necessary state variables 
    252         sock[sock_num].port = port;                                     // assign the socket a port number  
    253         assign_port(port);                                                              // set bit 'port' in 'ports' equal to 1 
    254          
    255         rx_buf_mgr[port].pid = nrk_get_pid();           // fill up the members of the ReceiveBufferManager 
    256         if( nrk_signal_register(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 
    257         { 
    258                 nrk_int_disable(); 
    259                 nrk_led_set(RED_LED); 
    260                 while(1) 
    261                         nrk_kprintf(PSTR("bind(): Error while registering for seg_arrived_signal\r\n")); 
    262         } 
     378        // retrieve the indices of the elements 
     379        port_index = get_index_unassigned_port_element(); 
     380        rbm_index = get_index_unassigned_rbm_element(); 
     381         
     382        if(port_index == NRK_ERROR || rbm_index == NRK_ERROR)   // sanity check for debugging  
     383        {        
     384                nrk_int_disable(); 
     385                nrk_led_set(RED_LED); 
     386                while(1) 
     387                        nrk_kprintf(PSTR("bind(): Bug detected in implementation of port / rbm element array\r\n")); 
     388        } 
     389         
     390        // fill up the members of the port element 
     391        assign_port(port_index, (uint8_t)port); 
     392         
     393        // fill up the members of the socket descriptor 
     394        sock[sock_num].pindex = port_index; 
     395        sock[sock_num].rbmindex = rbm_index; 
     396         
     397        // fill up the members of the rbm element 
     398        rx_buf_mgr[rbm_index].pindex = port_index; 
     399        rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 
    263400                 
    264401        // allocate 'size' buffers  
    265402        for(i = 1; i <= size; i++) 
    266403        { 
    267                 index = get_index_unallocated_rx_buf(); // look for the unallocated receive buffer  
    268                 if(index == NRK_ERROR)  //       this should not happen  
     404                buf_index = get_index_unallocated_rx_buf();     // look for the unallocated receive buffer  
     405                if(buf_index == NRK_ERROR)      //       this should not happen  
    269406                { 
    270407                        nrk_int_disable(); 
     
    274411                } 
    275412                 
    276                 insert_rx_fq(&rx_buf_udp[index], port, EMPTY);  // insert the buffer in the free queue of the port 
    277                 rx_buf_mgr[port].countTotal++;                                          // increment the countTotal for this port  
     413                insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); // insert the buffer in the free queue of the port 
     414                rx_buf_mgr[rbm_index].countTotal++;                                             // increment the countTotal for this port  
    278415                num_bufs_free--;                 
    279416        } 
    280417         
    281         leave_cr(tl_sem, "bind()");      
    282         leave_cr(bm_sem, "bind()"); 
     418        leave_cr(tl_sem, 8);     
     419        leave_cr(bm_sem, 8); 
    283420        return NRK_OK;  
    284421} 
     
    286423int8_t get_rx_queue_size(int8_t sock_num) 
    287424{ 
    288         int8_t port;            // to store the port number associated with this socket number  
    289         int8_t count; 
     425        int8_t count;   // to store the count of total rx buffers set aside for the given socket  
    290426         
    291427        enter_cr(bm_sem, 9);     
     
    311447        } 
    312448         
    313         if(sock[sock_num].port == INVALID_PORT) // no socket operations were performed yet 
     449        if(sock[sock_num].pindex == -1) // no socket operations were performed yet 
    314450        { 
    315451                leave_cr(tl_sem, 9);     
    316452                leave_cr(bm_sem, 9);     
    317                 return 0; 
    318         } 
     453                return 0;                                                       // hence return 0  
     454        } 
     455         
     456        if(sock[sock_num].rbmindex == -1) // sanity check for debugging 
     457        { 
     458                nrk_int_disable(); 
     459                nrk_led_set(RED_LED); 
     460                while(1) 
     461                        nrk_kprintf(PSTR("get_rx_queue_size(): Bug detected in implementation of port/rbm element array\r\n")); 
     462        }        
    319463                 
    320464        // at this point we know that some socket operation has been performed on the socket prior to this call 
    321         port = sock[sock_num].port; 
    322         count = rx_buf_mgr[port].countTotal; 
     465        count = rx_buf_mgr[sock[sock_num].rbmindex].countTotal;  
    323466         
    324467        leave_cr(tl_sem, 9);     
     
    330473int8_t set_rx_queue_size(int8_t sock_num, int8_t size) 
    331474{ 
    332         int8_t port;            // to store the port number associated with given socket number 
     475        uint8_t port;           // to store the port number associated with given socket number 
    333476        int8_t i;                       // loop index 
    334477        int8_t flag;            // to mark whether bind() was called prior to this call or not  
    335         int8_t index;           // stores the index of next unallocated receive buffer   
     478         
     479        int8_t buf_index;        // stores the index of next unallocated receive buffer 
     480        int8_t port_index; // stores the index of the next unassigned port element 
     481        int8_t rbm_index;        // stores the index of the next unassigned rbm element   
    336482                 
    337483        enter_cr(bm_sem, 10); 
     
    366512        } 
    367513         
    368         if(sock[sock_num].port == INVALID_PORT) // there is no port associated with this socket yet 
     514        if(sock[sock_num].pindex == -1)                         // there is no port associated with this socket yet 
    369515        { 
    370516                flag = 1;                                                                               // bind() was not called earlier  
    371517                port = get_next_available_port();               // search for a ephemeral port number  
    372                 if(port == NRK_ERROR)                                           // no port available  
     518                if(port == INVALID_PORT)                                        // no port available  
    373519                { 
    374520                        leave_cr(tl_sem, 10); 
     
    385531           } 
    386532                // port available and default num of buffers available,  
    387                 // assign the new port number to the socket desc                 
    388                 sock[sock_num].port = port; 
    389                 assign_port(port); 
    390                 if( nrk_signal_register(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 
    391                 { 
     533                // retrieve the indices of the elements 
     534                port_index = get_index_unassigned_port_element(); 
     535                rbm_index = get_index_unassigned_rbm_element(); 
     536                 
     537                if(port_index == NRK_ERROR || rbm_index == NRK_ERROR)   // sanity check for debugging  
     538                {        
    392539                        nrk_int_disable(); 
    393540                        nrk_led_set(RED_LED); 
    394541                        while(1) 
    395                                 nrk_kprintf(PSTR("set_rx_queue_size(): Error registering for seg_arrived_signal\r\n")); 
    396                 }  
    397         } 
     542                                nrk_kprintf(PSTR("set_rx_queue_size(): Bug detected in implementation of port / rbm element array\r\n")); 
     543                } 
     544                 
     545                // fill up the members of the port element 
     546                assign_port(port_index, port); 
     547                 
     548                // fill up the members of the socket descriptor 
     549                sock[sock_num].pindex = port_index; 
     550                sock[sock_num].rbmindex = rbm_index; 
     551                 
     552                // fill up the members of the rbm element 
     553                rx_buf_mgr[rbm_index].pindex = port_index; 
     554                rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 
     555         
     556        } // end if(bind was called earlier) 
    398557        else // bind was called earlier. DEFAULT_RX_QUEUE_SIZE buffers already allocated 
    399558        { 
    400                 // FIX ME  
    401559                if(size == DEFAULT_RX_QUEUE_SIZE)       // nothing to do, buffers already allocated 
    402560                { 
     
    405563                        return size; 
    406564                } 
     565                else // size > DEFAULT_RX_QUEUE_SIZE 
     566                        size -= DEFAULT_RX_QUEUE_SIZE; 
    407567        } 
    408568         
     
    413573        // at this point, the socket is associated with a valid port number 
    414574        // and at least one receive buffer is free 
    415         // fill in the values of the corresponding ReceiveBufferManager 
    416         rx_buf_mgr[port].pid = nrk_get_pid(); 
    417          
    418575        // allocate 'size' buffers  
    419576        for(i = 1; i <= size; i++) 
    420577        { 
    421                 index = get_index_unallocated_rx_buf(); // look for the next unallocated receive buffer  
    422                 if(index == NRK_ERROR)  //       this should not happen  
     578                buf_index = get_index_unallocated_rx_buf();     // look for the next unallocated receive buffer  
     579                if(buf_index == NRK_ERROR)      //       this should not happen  
    423580                { 
    424581                        nrk_int_disable(); 
     
    427584                                nrk_kprintf(PSTR("set_rx_queue_size(): Bug found in implementation of num_bufs_free\r\n")); 
    428585                }                
    429                 insert_rx_fq(&rx_buf_udp[index], port, EMPTY);  // insert the buffer in the free queue of the port 
    430                 rx_buf_mgr[port].countTotal++;  
     586                insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); // insert the buffer in the free queue of the port 
     587                rx_buf_mgr[rbm_index].countTotal++;  
    431588                num_bufs_free--;                 
    432589        } 
     
    434591        leave_cr(tl_sem, 10); 
    435592        leave_cr(bm_sem, 10); 
     593         
     594        if(flag == 0)   // bind was called earlier 
     595                return size + DEFAULT_RX_QUEUE_SIZE;     
     596         
    436597        return size; 
    437598} 
     
    439600int8_t release_buffer(int8_t sock_num, uint8_t *ptr) 
    440601{ 
    441         int8_t port;                                            // to store the port number associated with the socket 
    442602        ReceiveBufferUDP *buf;                  // pointer to traverse the buffer list of the free queue  
    443603         
     
    462622                return NRK_ERROR; 
    463623        } 
    464         if(sock[sock_num].port == INVALID_PORT) // if no socket operation was performed earlier  
     624        if(sock[sock_num].pindex == -1) // if no socket operation was performed earlier  
    465625        { 
    466626                _nrk_errno_set(INVALID_SOCKET); 
     
    470630                return NRK_ERROR; 
    471631        } 
    472                  
    473         port = sock[sock_num].port;                               // retrieve the port number from the socket descriptor  
     632         
     633        if(sock[sock_num].rbmindex == -1)       // sanity check for debugging  
     634        { 
     635                nrk_int_disable(); 
     636                nrk_led_set(RED_LED); 
     637                while(1) 
     638                        nrk_kprintf(PSTR("release_buffer(): Bug discovered in implementation of port / rbm element array\r\n")); 
     639        } 
    474640         
    475641        // check to see if the pointer passed is valid 
    476         buf = rx_buf_mgr[port].head_fq; 
     642        buf = rx_buf_mgr[sock[sock_num].rbmindex].head_fq; 
    477643        while(buf != NULL) 
    478644        { 
     
    491657                 
    492658        buf -> status = EMPTY;          // mark the buffer as available now 
    493         rx_buf_mgr[port].countFree++;  
     659        rx_buf_mgr[sock[sock_num].rbmindex].countFree++;  
    494660         
    495661        leave_cr(tl_sem, 11);    
     
    500666int8_t close_socket(int8_t sock_num) 
    501667{ 
    502         int8_t port;                            // to store the port number assoicated with the given socket number 
    503668        ReceiveBufferUDP *ptr;  // temp variable  
    504669                 
     
    525690                 
    526691        // Free up resources associated with this socket descriptor  
    527         port = sock[sock_num].port; 
    528         if(port != INVALID_PORT)        // some operation was performed earlier with this socket  
    529         { 
    530                 rx_buf_mgr[port].pid = INVALID_PID; 
    531                 if( nrk_signal_delete(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 
     692        if(sock[sock_num].pindex != -1) // some operation was performed earlier with this socket  
     693        { 
     694                if(sock[sock_num].rbmindex == -1)       // sanity check 
    532695                { 
    533696                        nrk_int_disable(); 
    534697                        nrk_led_set(RED_LED); 
    535698                        while(1) 
    536                                 nrk_kprintf(PSTR("close_socket(): Error in deleting seg_arrived_signal\r\n")); 
    537                 } 
    538  
     699                                nrk_kprintf(PSTR("close_socket(): Bug discovered in implementation of port /rbm element array\r\n")); 
     700                }                
    539701                // remove all the FULL buffers from the port queue                       
    540                 while( (ptr = remove_rx_pq(port)) != NULL )              
     702                while( (ptr = remove_rx_pq(sock[sock_num].rbmindex)) != NULL )           
    541703                        ptr -> status = UNALLOCATED;             
    542704                         
    543705                // remove all the EMPTY buffers from the free queue              
    544                 while( (ptr = remove_rx_fq(port, EMPTY)) != NULL )  
     706                while( (ptr = remove_rx_fq(sock[sock_num].rbmindex, EMPTY)) != NULL )  
    545707                        ptr -> status = UNALLOCATED; 
    546708                         
    547709                // remove all the FULL buffers from the free queue               
    548                 while( (ptr = remove_rx_fq(port,FULL)) != NULL ) 
     710                while( (ptr = remove_rx_fq(sock[sock_num].rbmindex,FULL)) != NULL ) 
    549711                        ptr -> status = UNALLOCATED; 
    550                  
    551                 release_port(port); 
     712                         
     713                rx_buf_mgr[sock[sock_num].rbmindex].pid = INVALID_PID; 
     714                rx_buf_mgr[sock[sock_num].rbmindex].pindex = -1; 
     715                rx_buf_mgr[sock[sock_num].rbmindex].countTotal = 0; 
     716                rx_buf_mgr[sock[sock_num].rbmindex].countFree = 0; 
     717                 
     718                release_port(sock[sock_num].pindex); 
    552719        }   
    553720         
    554         sock[sock_num].port = INVALID_PORT; 
     721        sock[sock_num].pindex = -1; 
     722        sock[sock_num].rbmindex = -1; 
    555723        sock[sock_num].pid = INVALID_PID; 
    556724         
     
    560728} 
    561729/************************************************************************************************/ 
    562 int8_t is_port_associated(int8_t port) 
    563 { 
     730int8_t is_port_associated(int16_t port) 
     731{ 
     732        // ERROR checking 
     733        if(port <= 0 || port > MAX_PORT_NUM) 
     734        { 
     735                _nrk_errno_set(INVALID_ARGUMENT); 
     736                return NRK_ERROR; 
     737        } 
     738                 
    564739        enter_cr(tl_sem, 13); 
    565740                 
    566         if(check_port_available(port) == NRK_OK) 
     741        if(check_port_available((uint8_t)port) == NRK_OK) 
    567742        { 
    568743                leave_cr(tl_sem, 13); 
     
    573748} 
    574749/************************************************************************************************/  
    575 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int16_t dest_addr, int8_t dest_port, int8_t prio) 
    576 { 
    577         int8_t port;                    // to hold the port number corresponding to the socket number passed 
     750int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int16_t dest_port, int8_t prio) 
     751{ 
    578752        int8_t result;                  // to hold the return type of various functions  
    579753         
    580         if(DEBUG_TL == 2) 
    581                 nrk_kprintf(PSTR("Entered send operation of TL\r\n")); 
    582                          
    583754        enter_cr(bm_sem, 14); 
    584755        enter_cr(tl_sem, 14); 
    585756         
    586         if(DEBUG_TL == 2) 
    587                 nrk_kprintf(PSTR("Inside CR of send operation of TL\r\n")); 
    588          
    589757        // ERROR checking 
    590758        if(sock_num < 0 || sock_num >= MAX_PORTS)                                                               // bad input  
     
    606774        } 
    607775                 
    608         if(dest_addr < 0 || dest_port <= 0 || dest_port > MAX_PORTS)    // bad input  
     776        if(dest_addr < 0 || dest_port <= 0 || dest_port > MAX_PORT_NUM) // bad input  
    609777        { 
    610778                _nrk_errno_set(INVALID_ARGUMENT); 
     
    634802                 
    635803        /* Error checking is complete. Begin processing */ 
    636         if(sock[sock_num].port == INVALID_PORT) // this means that bind() / set_rx_queue_size() was  
    637                                                                                                                         // not called earlier 
    638         { 
    639                 // FIX ME IMPORTANT.................     
    640                 leave_cr(tl_sem, 14); 
    641                 leave_cr(bm_sem, 14);    
    642                 result = set_rx_queue_size(sock_num, DEFAULT_RX_QUEUE_SIZE); 
    643                 enter_cr(bm_sem, 14); 
    644                 enter_cr(tl_sem, 14); 
    645                 if(result == NRK_ERROR)         // error number will be set already  
     804        if(sock[sock_num].pindex == -1) // this means that bind() / set_rx_queue_size() was  
     805                                                                                                // not called earlier 
     806        { 
     807                int8_t buf_index, port_index, rbm_index; // indices for the various arrays 
     808                uint8_t port;  
     809                 
     810                if(get_num_bufs_free() < DEFAULT_RX_QUEUE_SIZE) // no rx buffers left for this socket  
    646811                { 
    647812                        leave_cr(tl_sem, 14); 
    648                         leave_cr(bm_sem, 14);            
     813                        leave_cr(bm_sem, 14); 
     814                        _nrk_errno_set(NO_RX_BUFFERS_AVAILABLE); 
    649815                        return NRK_ERROR; 
    650816                } 
    651         } 
    652         port = sock[sock_num].port;     // retrieve the port number 
    653         // register for the 'send done' signal  
    654         if( nrk_signal_register(sd_sig[port].send_done_signal) == NRK_ERROR ) 
    655         { 
    656                 nrk_int_disable(); 
    657                 nrk_led_set(RED_LED); 
    658                 while(1) 
    659                         nrk_kprintf(PSTR("send(): Error while registering for send_done_signal\r\n")); 
    660         }        
    661                  
     817                // retrieve the indices of the elements 
     818                port_index = get_index_unassigned_port_element(); 
     819                rbm_index = get_index_unassigned_rbm_element(); 
     820                buf_index = get_index_unallocated_rx_buf(); 
     821                 
     822                if(port_index == NRK_ERROR || rbm_index == NRK_ERROR)   // sanity check for debugging  
     823                {        
     824                        nrk_int_disable(); 
     825                        nrk_led_set(RED_LED); 
     826                        while(1) 
     827                                nrk_kprintf(PSTR("send(): Bug detected in implementation of port / rbm element array\r\n")); 
     828                } 
     829                 
     830                if(buf_index == NRK_ERROR)      // sanity check for debugging 
     831                { 
     832                        nrk_int_disable(); 
     833                        nrk_led_set(RED_LED); 
     834                        while(1) 
     835                                nrk_kprintf(PSTR("send(): Bug detected in implementation of num_bufs_free\r\n")); 
     836                }                        
     837                // retrieve the next ephemeral port number 
     838                port = get_next_available_port(); 
     839                                 
     840                // fill up the members of the port element 
     841                assign_port(port_index, port); 
     842                 
     843                // fill up the members of the socket descriptor 
     844                sock[sock_num].pindex = port_index; 
     845                sock[sock_num].rbmindex = rbm_index; 
     846                 
     847                // fill up the members of the rbm element 
     848                rx_buf_mgr[rbm_index].pindex = port_index; 
     849                rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 
     850                 
     851                // insert a rx buffer in the free queue  
     852                insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); 
     853                rx_buf_mgr[rbm_index].countTotal++;     // increment the countTotal for this port  
     854                num_bufs_free--; 
     855         
     856        } //end if(bind was called earlier) 
     857         
    662858        // prepare a UDP segment  
    663         udp_seg.srcPort = port; 
    664         udp_seg.destPort = dest_port; 
     859        udp_seg.srcPort = ports[sock[sock_num].pindex].pno; 
     860        udp_seg.destPort = (uint8_t)dest_port; 
    665861        udp_seg.length = len; 
    666862        memcpy(udp_seg.data, ptr, len); // application payload already packed  
     
    668864        // prepare a network packet      
    669865        pkt.src = NODE_ADDR; 
    670         pkt.dest = dest_addr; 
     866        pkt.dest = (uint16_t)dest_addr; 
    671867        pkt.ttl = MAX_NETWORK_DIAMETER; 
    672868        pkt.type = UDP; 
     
    679875         
    680876        if(DEBUG_TL == 2) 
    681                 nrk_kprintf(PSTR("Inside send(). Before inserting packet in transmit queue\r\n")); 
     877                nrk_kprintf(PSTR("send(). Before inserting packet in transmit queue\r\n")); 
    682878                 
    683879        result = insert_tx_aq(&pkt); 
    684880         
    685881        if(DEBUG_TL == 2) 
    686                 nrk_kprintf(PSTR("Inside send(). After inserting packet in transmit queue\r\n")); 
     882                nrk_kprintf(PSTR("send(). After inserting packet in transmit queue\r\n")); 
    687883                 
    688884        if(result == NRK_ERROR) 
     
    734930} 
    735931/*************************************************************************************************/ 
    736 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr, int8_t *srcPort, int8_t *rssi) 
    737 { 
    738         int8_t port;                                    // to hold the port number associated with the passed socket number      
     932uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi) 
     933{ 
    739934        nrk_sig_mask_t my_sigs;         // to hold the return value of network layer signals  
    740935        ReceiveBufferUDP *buf;          // temporary variable  
    741936        Transport_Segment_UDP *seg;// pointer to received segment from the network layer  
     937        int8_t rbm_index;                               // to store the index of the corresponding receive buffer manager 
     938        int8_t port_index;                      // to store the index of the port element   
    742939         
    743940        enter_cr(bm_sem, 16); 
     
    754951        } 
    755952 
    756         if( sock[sock_num].pid != nrk_get_pid() || sock[sock_num].port == INVALID_PORT ) 
     953        if( sock[sock_num].pid != nrk_get_pid() || sock[sock_num].pindex == -1 ) 
    757954        { 
    758955                _nrk_errno_set(INVALID_SOCKET); // socket description not complete 
     
    763960        } 
    764961         
    765         // error checking complete. Begin processing     
    766         port = sock[sock_num].port; 
     962        if(sock[sock_num].rbmindex == -1)       // sanity check for debugging 
     963        { 
     964                nrk_int_disable(); 
     965                nrk_led_set(RED_LED); 
     966                while(1) 
     967                        nrk_kprintf(PSTR("receive(): Bug detected in implementation of port/rbm element array\r\n")); 
     968        } 
     969         
     970        // error checking complete. Begin processing  
     971        rbm_index = sock[sock_num].rbmindex;     
     972        port_index = sock[sock_num].pindex; 
    767973        if(sock[sock_num].timeout.secs == 0 && sock[sock_num].timeout.nano_secs == 0) 
    768974        { 
     
    772978                 
    773979                // check the receive buffer manager for this port to see if there are any queued segments 
    774                 if(rx_buf_mgr[port].countFree == rx_buf_mgr[port].countTotal)   // no queued segments  
     980                if(rx_buf_mgr[rbm_index].countFree == rx_buf_mgr[rbm_index].countTotal) // no queued segments  
    775981                { 
    776982                        if(DEBUG_TL == 2) 
    777                                 printf("receive(): No segments in receive queue of port = %d\n", port); 
     983                                printf("receive(): No segments in receive queue of port = %d\n", ports[rx_buf_mgr[rbm_index].pindex].pno); 
    778984                        while(1) 
    779985                        { 
    780986                                leave_cr(tl_sem, 16); 
    781987                                leave_cr(bm_sem, 16); 
    782                                 my_sigs = nrk_event_wait(SIG(rx_buf_mgr[port].seg_arrived_signal));     // block 
     988                                my_sigs = nrk_event_wait(SIG(ports[port_index].data_arrived_signal));   // block 
    783989                                enter_cr(bm_sem, 16); 
    784990                                enter_cr(tl_sem, 16);   
     
    792998                                                nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait (without timeout)\r\n")); 
    793999                                }        
    794                                 if( my_sigs & SIG(rx_buf_mgr[port].seg_arrived_signal) )        // got a segment  
     1000                                if( my_sigs & SIG(ports[port_index].data_arrived_signal) )      // got a segment  
    7951001                                { 
    7961002                                        if(DEBUG_TL == 2) 
    797                                         nrk_kprintf(PSTR("receive(): Received the seg arrived signal\r\n")); 
     1003                                        nrk_kprintf(PSTR("receive(): Received the data arrived signal\r\n")); 
    7981004                                break; 
    7991005                        } 
     
    8181024                        nrk_led_set(RED_LED); 
    8191025                        while(1) 
    820                                 nrk_kprintf(PSTR("receive(): Error in registering for nrk_wakeup_signal (with timeout)\r\n")); 
     1026                                nrk_kprintf(PSTR("receive(): Error in registering for nrk_wakeup_signal\r\n")); 
    8211027                } 
    8221028                if( nrk_set_next_wakeup(sock[sock_num].timeout) == NRK_ERROR) 
     
    8251031                        nrk_led_set(RED_LED); 
    8261032                        while(1) 
    827                                 nrk_kprintf(PSTR("receive(): Error returned by nrk_set_next_wakeup (with timeout)\r\n")); 
     1033                                nrk_kprintf(PSTR("receive(): Error returned by nrk_set_next_wakeup\r\n")); 
    8281034                }                
    8291035                // check the receive manager for this port to see if there are any queued segments 
    830                 if(rx_buf_mgr[port].countFree == rx_buf_mgr[port].countTotal)   // no queued segments  
     1036                if(rx_buf_mgr[rbm_index].countFree == rx_buf_mgr[rbm_index].countTotal) // no queued segments  
    8311037                { 
    8321038                        while(1) 
     
    8341040                                leave_cr(tl_sem, 16); 
    8351041                                leave_cr(bm_sem, 16); 
    836                                 my_sigs = nrk_event_wait(SIG(rx_buf_mgr[port].seg_arrived_signal) | SIG(nrk_wakeup_signal)); 
     1042                                my_sigs = nrk_event_wait(SIG(ports[port_index].data_arrived_signal) | SIG(nrk_wakeup_signal)); 
    8371043                                enter_cr(bm_sem, 16); 
    8381044                                enter_cr(tl_sem, 16);                                     
     
    8461052                                                nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait() (with timeout)\r\n")); 
    8471053                                }        
    848                         if( my_sigs & SIG(rx_buf_mgr[port].seg_arrived_signal) )        // got a segment  
     1054                        if( my_sigs & SIG(ports[port_index].data_arrived_signal) )      // got a segment  
    8491055                        { 
    8501056                                sock[sock_num].timeout.secs = 0;                // invalidate the timeout  
    8511057                                sock[sock_num].timeout.nano_secs = 0; 
    8521058                                if(DEBUG_TL == 2) 
    853                                         nrk_kprintf(PSTR("Received the seg arrived signal\r\n")); 
    854                                 // FIX me change the wakeup period back to what it was 
     1059                                        nrk_kprintf(PSTR("Received the data arrived signal\r\n")); 
    8551060                                break; 
    8561061                        } 
     
    8601065                                sock[sock_num].timeout.secs = 0;                        // invalidate the timeout value of the socket  
    8611066                                sock[sock_num].timeout.nano_secs = 0; 
    862                                 // FIX me change the wakeup period back to what it was  
     1067                                 
    8631068                                _nrk_errno_set(SOCKET_TIMEOUT); 
    8641069                                 
     
    8831088   // In either case, retrieve the segment and return back to user task  
    8841089    
    885    buf = remove_rx_pq(port);    // remove the buffer from the port queue  
     1090   buf = remove_rx_pq(rbm_index);       // remove the buffer from the port queue  
    8861091   if(buf == NULL)      // this should not happen  
    8871092   { 
     
    8891094                nrk_led_set(RED_LED); 
    8901095                while(1) 
    891                         nrk_kprintf(PSTR("receive(): Bug found in implementation of seg_arrived_signal / rx buffer mgmt\r\n")); 
    892         } 
    893    insert_rx_fq(buf, port, FULL);       // insert the buffer into the free queue with status = FULL  
     1096                        nrk_kprintf(PSTR("receive(): Bug found in implementation of data_arrived_signal / rx buffer mgmt\r\n")); 
     1097        } 
     1098   insert_rx_fq(buf, rbm_index, FULL);  // insert the buffer into the free queue with status = FULL  
    8941099   seg = &(buf -> seg); 
    8951100    
     
    9111116int8_t check_receive_queue(int8_t sock_num) 
    9121117{ 
    913         int8_t port; 
     1118        int8_t rbm_index;                       // to store the index of rbm element associated with the socket  
    9141119        int8_t count1, count2, count3; 
    9151120         
     
    9171122        enter_cr(tl_sem, 17); 
    9181123         
    919          
    9201124        // ERROR checking 
    9211125        if(sock_num < 0 || sock_num >= MAX_PORTS)               // bad input  
     
    9281132        } 
    9291133 
    930         if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].port == INVALID_PORT) ) // wrong socket number  
     1134        if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].pindex == -1) ) // wrong socket number  
    9311135        { 
    9321136                _nrk_errno_set(INVALID_SOCKET); 
     
    9371141        } 
    9381142         
    939         port = sock[sock_num].port; 
    940         count1 = rx_buf_mgr[port].countTotal; 
    941         count2 = rx_buf_mgr[port].countFree; 
    942         count3 = get_in_process_buf_count(port); 
     1143        rbm_index = sock[sock_num].rbmindex; 
     1144        count1 = rx_buf_mgr[rbm_index].countTotal; 
     1145        count2 = rx_buf_mgr[rbm_index].countFree; 
     1146        count3 = get_in_process_buf_count(rbm_index); 
    9431147         
    9441148        leave_cr(tl_sem, 17); 
     
    9491153int8_t wait_until_send_done(int8_t sock_num) 
    9501154{ 
    951         int8_t port;                            // to hold the port number associated with the given socket number  
     1155        int8_t port_index;              // to store the index of the port element associated with the socket     
    9521156        nrk_sig_mask_t my_sigs; // temp variable  
    9531157         
     
    9651169        } 
    9661170         
    967         if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].port == INVALID_PORT) ) // bad socket  
     1171        if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].pindex == -1) ) // bad socket  
    9681172        { 
    9691173                _nrk_errno_set(INVALID_SOCKET); 
     
    9741178        } 
    9751179         
    976         port = sock[sock_num].port; 
     1180        // error checking complete. Begin processing  
     1181         
     1182        port_index = sock[sock_num].pindex; 
    9771183         
    9781184        if(sock[sock_num].timeout.secs == 0 && sock[sock_num].timeout.nano_secs == 0) 
     
    9831189                        leave_cr(tl_sem, 18); 
    9841190                        leave_cr(bm_sem, 18);                    
    985                         my_sigs = nrk_event_wait( SIG(sd_sig[port].send_done_signal) ); 
     1191                        my_sigs = nrk_event_wait( SIG(ports[port_index].send_done_signal) ); 
    9861192                        enter_cr(bm_sem, 18); 
    9871193                        enter_cr(tl_sem, 18); 
     
    9951201                        } 
    9961202                         
    997                         if( my_sigs & SIG(sd_sig[port].send_done_signal) ) 
     1203                        if( my_sigs & SIG(ports[port_index].send_done_signal) ) // data sent  
    9981204                        { 
    9991205                                leave_cr(tl_sem, 18); 
     
    10311237                        leave_cr(tl_sem, 18); 
    10321238                        leave_cr(bm_sem, 18);                    
    1033                         my_sigs = nrk_event_wait( SIG(sd_sig[port].send_done_signal) | SIG(nrk_wakeup_signal) ); 
     1239                        my_sigs = nrk_event_wait( SIG(ports[port_index].send_done_signal) | SIG(nrk_wakeup_signal) ); 
    10341240                        enter_cr(bm_sem, 18); 
    10351241                        enter_cr(tl_sem, 18); 
     
    10401246                                nrk_led_set(RED_LED); 
    10411247                                while(1) 
    1042                                         nrk_kprintf(PSTR("wait_until_send_done(): Error returned by nrk_event_wait() (with timeout)\r\n")); 
     1248                                        nrk_kprintf(PSTR("wait_until_send_done(): Error returned by nrk_event_wait()\r\n")); 
    10431249                        } 
    10441250                                 
    1045                         if( my_sigs & SIG(sd_sig[port].send_done_signal) ) 
     1251                        if( my_sigs & SIG(ports[port_index].send_done_signal) ) 
    10461252                        { 
    10471253                                sock[sock_num].timeout.secs = 0;                        // disable the timeout  
     
    10551261                        if( my_sigs & SIG(nrk_wakeup_signal) ) 
    10561262                        { 
    1057                                 sock[sock_num].timeout.secs = 0; 
     1263                                sock[sock_num].timeout.secs = 0;                        // disable the timeout  
    10581264                                sock[sock_num].timeout.nano_secs = 0;  
    10591265                                _nrk_errno_set(SOCKET_TIMEOUT); 
  • nano-RK/projects/network_stack/TransportLayerUDP.h

    r118 r119  
    2020 
    2121#define EPHEMERAL_PORT_NUM_START 11             // 11 is the smallest port number available to user tasks 
     22#define MAX_PORT_NUM 255 
    2223 
    2324// limits on various object types  
     
    3233typedef struct 
    3334{ 
    34         int8_t srcPort;                                         // source port of the application 
    35         int8_t destPort;                                                // destination port of the application 
     35        uint8_t srcPort;                                                // source port of the application 
     36        uint8_t destPort;                                               // destination port of the application 
    3637        int8_t length;                                                  // actual length of the data payload 
    3738        uint8_t data[MAX_APP_PAYLOAD];  // application layer data  
     
    3940}Transport_Segment_UDP; 
    4041 
     42typedef struct  
     43{ 
     44        uint8_t pno;                                                    // the actual port number  
     45        nrk_sig_t data_arrived_signal;  // indicates that data has arrived for this port in the receive queue  
     46        nrk_sig_t send_done_signal;             // indicates that the highest priority message from this port  
     47                                                                                                // has been sent over the radio  
     48}Port; 
     49 
    4150typedef struct 
    4251{ 
    43         int8_t port;                                                    // port number associated with this socket descriptor  
     52        int8_t pindex;                                                  // index of the port element for this socket 
     53        int8_t rbmindex;                                                // index of the rbm element for this socket       
    4454        int8_t pid;                                                             // pid of the process holding this socket descriptor 
    4555        int8_t type;                                                    // type of socket 
     
    8090*/ 
    8191 
    82 int8_t get_next_available_port(); 
     92uint8_t get_next_available_port(); 
    8393/* 
    8494This function returns the next available port number if present 
    8595 
    8696        PARAMS:         None 
    87         RETURNS:                port number if available, NRK_ERROR otherwise 
     97        RETURNS:                port number if available, INVALID_PORT otherwise 
    8898        ERROR NOS:      NO_PORTS_AVAILABLE if no ephemeral port numbers are available 
    8999        COMMENTS:       private function  
    90100*/ 
    91101 
    92 int8_t check_port_available(int8_t pt); 
     102int8_t check_port_available(uint8_t pt); 
    93103/*  
    94104This function checks whether port 'pt' is available 
     
    100110*/  
    101111 
    102 void assign_port(int8_t pt); 
     112void assign_port(int8_t pindex, uint8_t pt); 
    103113/* 
    104114This functions records the fact that port 'pt' is allocated 
    105115 
    106         PARAMS:         pt: port number to be allocated 
     116        PARAMS:         pindex: index of the relevant port element 
     117                                        pt: port number to be allocated 
    107118        RETURNS:                None 
    108119        COMMENTS:       private function 
     
    110121*/ 
    111122 
    112 void release_port(int8_t port); 
    113 /* 
    114 This functions records the fact that 'port' is now available 
    115  
    116         PARAMS:         port: port number to be released 
     123void release_port(int8_t pindex); 
     124/* 
     125This functions records the fact that a given port is now available 
     126 
     127        PARAMS:         pindex: port element holding information about the port 
    117128        RETURNS:                None 
    118         COMMENTS:       None 
    119 */ 
    120  
    121 int8_t bind(int8_t sock_num, int8_t port); 
     129        COMMENTS:       private function 
     130*/ 
     131 
     132int8_t get_unassigned_port_element(); 
     133/* 
     134This function returns the index an unassigned port element or NRK_ERROR if none are available 
     135 
     136        PARAMS:         None 
     137        RETURNS:                index of an unassigned port element or NRK_ERROR if none are available 
     138         
     139        ERROR NOS:      NO_PORT_ELEMENT_AVAILABLE 
     140        Comments:       private function 
     141*/ 
     142 
     143int8_t get_unassigned_rbm_element(); 
     144/* 
     145This function returns the index an unassigned rbm element or NRK_ERROR if none are available 
     146 
     147        PARAMS:         None 
     148        RETURNS:                index of an unassigned rbm element or NRK_ERROR if none are available 
     149         
     150        ERROR NOS:      NO_RBM_ELEMENT_AVAILABLE 
     151        Comments:       private function 
     152*/ 
     153 
     154uint8_t get_port_num(int8_t sock_num); 
     155/* 
     156This function can be used by an application to find the port number mapped to a given socket descriptor 
     157 
     158        PARAMS:         sock_num: The socket descriptor 
     159        RETURNS:                the corresponding port number or INVALID_PORT if none exists / error 
     160         
     161        ERROR NOS:      INVALID_ARGUMENT if the passed parameter is faulty 
     162                                        INVALID_SOCKET if a wrong socket number is passed 
     163                                        UNMAPPED_SOCKET if no socket/port mapping exists 
     164                                         
     165        Comments:       User API 
     166                                        A return value of INVALID_PORT could mean either an error has occurred or there  
     167                                        does not exist any socket/port mapping. Check the error number to find out. 
     168*/  
     169 
     170int8_t bind(int8_t sock_num, int16_t port); 
    122171/* 
    123172This function binds a given socket descriptor to a given port number 
     
    177226                                        2. a valid port 
    178227                                        3. a receive queue of size at least 1 
     228                                         
    179229*/  
    180230 
     
    193243                                         
    194244        COMMENTS:       User API 
    195                                         This function should be called when the application has finsihed processing 
     245                                        This function should be called when the application has finished processing 
    196246                                        a received message or has copied the message to a application-local storage  
    197247                                        area 
     
    211261*/ 
    212262 
    213 int8_t is_port_associated(int8_t port); 
     263int8_t is_port_associated(int16_t port); 
    214264/* 
    215265This function checks whether a given port number has been assigned to a socket or not  
    216266                 
    217                 PARAMS: port: The port number 
    218                 RETURNS: TRUE if a mapping exists, FALSE otherwise 
    219                 COMMENTS: User API  
    220 */ 
    221  
    222 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int16_t dest_addr, int8_t dest_port, int8_t prio); 
     267                PARAMS:         port: The port number 
     268                RETURNS:        TRUE if a mapping exists, FALSE if a mapping does not exist, NRK_ERROR on error 
     269                 
     270                ERROR NOS:      INVALID_ARGUMENT if the passed paramter is faulty                
     271                COMMENTS:       User API  
     272*/ 
     273 
     274int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int16_t dest_port, int8_t prio); 
    223275/* 
    224276This function can be used by the application task to send data to other nodes on the network 
     
    266318*/  
    267319 
    268 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr, int8_t *srcPort, int8_t *rssi); 
     320uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi); 
    269321/* 
    270322This function allows an application to receive a message along with network control information 
     
    326378         
    327379        ERROR NOS:      INVALID_ARGUMENT if the passed parameter is faulty 
    328                                         INVALID_SOCKET if an incomplete socket number is passed 
     380                                        INVALID_SOCKET if an incomplete socket descriptor is passed 
    329381                                        SOCKET_TIMEOUT if a timeout value is specified prior to this call and  
    330382                                        the timeout value expires 
    331383        Comments:       User API 
    332384                                        If you want to specify a maximum wait period, use set_timeout() prior to every such 
    333                                         wait_until_send_done() call.   
     385                                        wait_until_send_done() call. The return value of NRK_ERROR could mean either that an  
     386                                        error occured during processing or that the timeout expired. Check the error number 
     387                                        to find out 
    334388*/  
    335389 
Note: See TracChangeset for help on using the changeset viewer.