Changeset 116


Ignore:
Timestamp:
06/28/2007 11:21:55 AM (5 years ago)
Author:
ayb
Message:
 
Location:
nano-RK/projects/network_stack
Files:
6 edited

Legend:

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

    r113 r116  
    732732} 
    733733/*************************************************************************************************/ 
    734 ReceiveBufferUDP* remove_rx_pq(int8_t port) 
     734ReceiveBufferUDP* remove_rx_pq(ReceiveBufferManager *rbm) 
    735735{ 
    736736        // The remove is always done from the head of the queue since its a priority queue       
     
    739739        //enter_cr(bm_sem, "remove_rx_pq()"); 
    740740                         
    741         if(rx_buf_mgr[port].head_pq == NULL)    // no more queued segments 
     741        if(rbm -> head_pq == NULL)      // no more queued segments 
    742742        { 
    743743                //leave_cr(bm_sem, "remove_rx_pq()"); 
     
    745745        } 
    746746                 
    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; 
     747        if(rbm -> head_pq == rbm -> tail_pq)    // only one buffer in queue  
     748                rbm -> tail_pq = NULL; 
     749                 
     750        ptr = rbm -> head_pq; 
     751         
     752        rbm -> head_pq = rbm -> head_pq -> next; 
    753753                 
    754754        //leave_cr(bm_sem, "remove_rx_pq()");    
     
    756756} 
    757757/*************************************************************************************************/ 
    758  
    759 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t port, int8_t status) 
    760 { 
    761         //enter_cr(bm_sem, "insert_rx_fq()");    
    762                  
     758void insert_rx_fq(ReceiveBufferUDP *buf, int8_t index, int8_t status) 
     759{ 
    763760        // 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; 
     761        if(rx_buf_mgr[index].head_fq == NULL)   // the free queue is empty  
     762        { 
     763                rx_buf_mgr[index].head_fq = rx_buf_mgr[index].tail_fq = buf; 
    767764                buf -> next = NULL; 
    768765        }        
    769766        else 
    770767        { 
    771                 rx_buf_mgr[port].tail_fq -> next = buf; 
     768                rx_buf_mgr[index].tail_fq -> next = buf; 
    772769                buf -> next = NULL; 
    773                 rx_buf_mgr[port].tail_fq = buf; 
     770                rx_buf_mgr[index].tail_fq = buf; 
    774771        } 
    775772 
    776773        if(status == EMPTY)     // this function was called from bind() / setReceiveQueueSize() 
    777774        { 
    778                 rx_buf_mgr[port].countFree++; 
     775                rx_buf_mgr[index].countFree++; 
    779776                buf -> status = EMPTY; 
    780777        } 
    781778         
    782         //leave_cr(bm_sem, "insert_rx_fq()");    
    783779        return; 
    784780} 
    785781 
    786782/***************************************************************************************************/ 
    787 ReceiveBufferUDP* remove_rx_fq(uint8_t port, int8_t status) 
     783ReceiveBufferUDP* remove_rx_fq(ReceiveBufferManager *rbm, int8_t status) 
    788784{ 
    789785        // This function removes the next receive buffer from the free queue having a given status       
     
    791787        ReceiveBufferUDP *prev = NULL; 
    792788         
    793         if(rx_buf_mgr[port].head_fq == NULL)    // no buffers remaining in free queue    
     789        if(rbm -> head_fq == NULL)      // no buffers remaining in free queue    
    794790                return NULL; 
    795791                 
    796         ptr = rx_buf_mgr[port].head_fq; 
     792        ptr = rbm -> head_fq; 
    797793        while(ptr != NULL) 
    798794        { 
     
    812808        if(prev == NULL)        // the EMPTY / FULL buffer is at the head of the queue 
    813809        { 
    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; 
     810                if(rbm -> head_fq == rbm -> tail_fq)    // only one buffer in free queue 
     811                        rbm -> tail_fq = NULL; 
     812                         
     813                rbm -> head_fq = rbm -> head_fq -> next; 
    818814                if(status == EMPTY) 
    819                         rx_buf_mgr[port].countFree--; 
     815                        (rbm -> countFree)--; 
    820816                return ptr; 
    821817        } 
     
    824820        prev -> next = ptr -> next; 
    825821        if(status == EMPTY) 
    826                 rx_buf_mgr[port].countFree--; 
     822                (rbm -> countFree)--; 
    827823        return ptr; 
    828824} 
  • nano-RK/projects/network_stack/BufferManager.h

    r113 r116  
    3636 
    3737// priority level of task ....SHIFT this to Anthony's code  
    38 #define MAX_TASK_PRIORITY 10   
     38#define MAX_TASK_PRIORITY 127   
    3939  
    4040// priority levels of messages  
     
    5959typedef struct 
    6060{ 
    61         int8_t pid;                                                             // pid of task associated with the port  
     61        int8_t pid;                                                             // pid of the process holding this receive buffer manager        
     62        Port *port;                                                             // pointer to the port information   
    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  
     69         
    7070}ReceiveBufferManager;                                  // one for every port in the system  
    7171 
     
    9090}TransmitBufferManager; 
    9191 
    92 typedef struct  
    93 { 
    94         nrk_sig_t send_done_signal; 
    95 }SendDoneSignal; 
    96  
    9792/***************************** FUNCTION PROTOTYPES ************************************/ 
    9893 
  • nano-RK/projects/network_stack/NWErrorCodes.h

    r103 r116  
    1919#define NO_TX_BUFFERS_AVAILABLE 12  
    2020#define SOCKET_TIMEOUT 13  
     21#define NO_PORT_ELEMENT_AVAILABLE 14  
     22#define NO_RBM_ELEMENT_AVAILABLE 15  
    2123 
    2224#endif  
  • nano-RK/projects/network_stack/TransportLayerUDP.c

    r113 r116  
    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 number indicating which port numbers are free or taken 
    15                                                                                         // a value of 1 at bit position 'x' indicates that port 'x' has been taken 
    16                                                                                         // a value of 0 at bit position 'x' indicates that port 'x' is available 
    17                                                                                          
     11Socket sock[MAX_PORTS];                         // array to store available socket descriptors 0 - (MAX_PORTS - 1) 
     12 
     13Port ports[MAX_PORTS];                          // array to store information about each port 
     14 
    1815int8_t tlayer_init_done;                        // flag to indicate whether initialization has been done correctly  
    1916 
     
    3128extern TransmitBuffer tx_buf[]; 
    3229extern TransmitBufferManager tx_buf_mgr; 
    33 extern SendDoneSignal sd_sig[]; 
    3430extern nrk_sem_t *bm_sem; 
    3531 
     
    4541extern void pack_TL_UDP_header(uint8_t*, Transport_Segment_UDP*); 
    4642 
    47 // From Debug.c  
    48 extern void go_into_panic(int8_t *); 
    49  
    5043/************************** Function definitions ***************************************************/ 
    5144void initialise_transport_layer_udp() 
    5245{ 
    5346        int8_t i;       // loop index  
    54         // make all the socket descriptors unassigned  
     47         
     48        // initialise the socket descriptors and port elements  
    5549        for(i = 0; i < MAX_PORTS; i++) 
    5650        { 
    57                 sock[i].pid = INVALID_PID; 
    58                 sock[i].port = INVALID_PORT; 
    59                 sock[i].timeout.secs = 0;               // indicates no timeout  
    60                 sock[i].timeout.nano_secs = 0;  
    61         } 
    62         // make all the ports available  
    63         ports = 0; 
    64          
    65         tl_sem = nrk_sem_create(1,MAX_TASK_PRIORITY);   // create the mutex   
     51                sock[i].port = NULL;                                    // indicates the absence of socket-port mapping 
     52                sock[i].rbm = NULL;                                     // indicates the absence of socket-port mapping  
     53                sock[i].pid = INVALID_PID;                      // indicates an unassigned socket descriptor             
     54                sock[i].timeout.secs = 0;                       // indicates no timeout  
     55                sock[i].timeout.nano_secs = 0;  // indicates no timeout 
     56                 
     57                ports[i].pno = INVALID_PORT;            // indicates an unassigned port number  
     58        } 
     59         
     60        // create the mutex of priority ceiling equal to MAX_TASK_PRIORITY       
     61        tl_sem = nrk_sem_create(1,MAX_TASK_PRIORITY);    
    6662        if(tl_sem == NULL) 
    6763        { 
     
    7167                        nrk_kprintf(PSTR("initialise_transport_layer_udp(): Error creating the semaphore\r\n")); 
    7268        }        
     69         
     70        // check to see if the user has given a valid number for MAX_PORTS 
     71        if(MAX_PORTS > 127)     // a node cannot hold more than 127 ports  
     72        { 
     73                nrk_int_disable(); 
     74                nrk_led_set(RED_LED); 
     75                while(1) 
     76                        nrk_kprintf(PSTR("initialise_transport_layer_udp(): Error in value of MAX_PORTS\r\n"))l 
     77        } 
     78         
    7379        // set the init_done flag  
    7480        tlayer_init_done = 1; 
     
    7783/**************************************************************************************************/ 
    7884int8_t get_next_available_socket() 
    79 // private function. This returns the index of the next available socket descriptor or NRK_ERROR if 
    80 // none are available  
    8185{ 
    8286        int8_t i;               // loop index 
     
    8892                        return i; 
    8993        } 
     94         
    9095        _nrk_errno_set(NO_SOCKET_DESC_AVAILABLE);        
    91          
    9296        return NRK_ERROR; 
    9397} 
     
    118122                                } 
    119123                                break; 
    120                  
     124                                 
     125                case SOCK_IPC: 
     126                                result = get_next_available_socket(); 
     127                                if(result != NRK_ERROR) 
     128                                { 
     129                                        sock[result].pid = nrk_get_pid(); 
     130                                        sock[result].type = SOCK_IPC; 
     131                                } 
     132                                break; 
     133         
    121134                default:  
    122135                                _nrk_errno_set(UNSUPPORTED_SOCK_TYPE); 
     
    124137        } 
    125138                 
    126         leave_cr(tl_sem, "create_socket()"); 
     139        leave_cr(tl_sem, 3); 
    127140        return result;  
    128141} 
    129142 
    130143/*****************************************************************************************/ 
    131 int8_t get_next_available_port() 
    132 // This private function gets the next available ephemeral port number if present or NRK_ERROR 
    133 // if all are taken  
     144uint8_t get_next_available_port() 
     145{ 
     146        uint8_t i;      // loop index for the ephemeral ports  
     147        int8_t j;       // loop index 
     148         
     149        for(i = EPHEMERAL_PORT_NUM_START; i <= MAX_PORT_NUM; i++) 
     150        { 
     151                // check to see if port 'i' is available  
     152                for(j = 0; j < MAX_PORTS; j++) 
     153                        if(ports[j].pno == i)   // port 'i' is taken  
     154                                break; 
     155                                 
     156                if(j == MAX_PORTS)                      // port 'i' is free 
     157                        return i;                                       // return this ephemeral port number  
     158        } 
     159         
     160        // if the code reaches this point, it means that all the ephemeral port numbers are taken                         
     161        _nrk_errno_set(NO_PORTS_AVAILABLE); 
     162        return INVALID_PORT;                                                      
     163} 
     164/******************************************************************************************/ 
     165int8_t check_port_available(uint8_t pt) 
    134166{ 
    135167        int8_t i;       // loop index  
    136         if(DEBUG_TL == 2) 
    137                 printf("Inside get_next_available_ports(). Value of ports = %d\r\n", ports);  
    138          
    139         //for(i = EPHEMERAL_PORT_NUM_START; i <= MAX_PORTS; i++)        // check only for ephemeral port numbers 
    140         for(i = 3; i <= MAX_PORTS; i++)  
    141         { 
    142                 if(DEBUG_TL == 2) 
    143                         printf("Inside get_next_available_ports(). i = %d\r\n", i); 
    144                 if( ((ports >> i) & ((uint32_t)1)) == 0 )       // bit 'i' is 0  
     168         
     169        for(i = 0; i < MAX_PORTS; i++) 
     170                if(ports[i].pno == pt)          // port 'pt' has been taken 
     171                { 
     172                        _nrk_errno_set(PORT_UNAVAILABLE);                
     173                        return NRK_ERROR; 
     174                } 
     175         
     176        // if the code reaches here, it means that port 'pt' is available  
     177        return NRK_OK; 
     178} 
     179 
     180/********************************************************************************************/ 
     181int8_t assign_port(Port *p, uint8_t pt) 
     182{ 
     183        int8_t ret1, ret2;      // to hold the return values of various functions 
     184                 
     185        // fill up the members of the port element  
     186        p -> pno = pt; 
     187        p -> send_done_signal = nrk_signal_create(); 
     188        p -> data_arrived_signal = nrk_signal_create(); 
     189         
     190        if(p -> send_done_signal == NRK_ERROR)  
     191        { 
     192                if(DEBUG_TL == 1) 
     193                        nrk_kprintf(PSTR("assign_port(): Error creating send_done signal\r\n")); 
     194                         
     195                _nrk_errno_set(NO_SIGNALS_AVAILABLE); 
     196                return NRK_ERROR; 
     197        } 
     198         
     199        if(p -> data_arrived_signal == NRK_ERROR) 
     200        { 
     201                if(DEBUG_TL == 1) 
     202                        nrk_kprintf(PSTR("assign_port(): Error creating data_arrived signal\r\n", pt); 
     203                         
     204                _nrk_errno_set(NO_SIGNALS_AVAILABLE); 
     205                return NRK_ERROR; 
     206        } 
     207         
     208        ret1 = nrk_signal_register(p -> send_done_signal); 
     209        ret2 = nrk_signal_register(p -> data_arrived_signal); 
     210        if(ret1 == NRK_ERROR) 
     211        { 
     212                nrk_int_disable(); 
     213                nrk_led_set(RED_LED); 
     214                while(1) 
     215                        nrk_kprintf(PSTR("assign_port(): Error registering send_done signal\r\n")); 
     216        } 
     217        if(ret2 == NRK_ERROR) 
     218        { 
     219                nrk_int_disable(); 
     220                nrk_led_set(RED_LED); 
     221                while(1) 
     222                        nrk_kprintf(PSTR("assign_port(): Error registering data_arrived signal\r\n", pt)); 
     223        } 
     224         
     225        return NRK_OK;   
     226} 
     227/******************************************************************************************/ 
     228void release_port(Port *p) 
     229{ 
     230        int8_t ret1, ret2;      // to hold the return type of function calls     
     231         
     232        // invalidate the members of the given port element      
     233        p -> port = INVALID_PORT; 
     234        ret1 = nrk_signal_delete(p -> send_done_signal); 
     235        ret2 = nrk_signal_delete(p -> data_arrived_signal);      
     236         
     237        if(ret1 == NRK_ERROR ) 
     238        { 
     239                nrk_int_disable(); 
     240                nrk_led_set(RED_LED); 
     241                while(1) 
     242                        nrk_kprintf(PSTR("release_port(): Error deleting send_done signal\r\n")); 
     243        } 
     244 
     245        if(ret2 == NRK_ERROR) 
     246        { 
     247                nrk_int_disable(); 
     248                nrk_led_set(RED_LED); 
     249                while(1) 
     250                        nrk_kprintf(PSTR("release_port(): Error deleting send_done_signal\r\n")); 
     251        } 
     252        return;  
     253} 
     254/*****************************************************************************************/ 
     255int8_t get_index_unassigned_port_element() 
     256{ 
     257        int8_t i;       // loop index 
     258         
     259        for(i = 0; i < MAX_PORTS; i++) 
     260                if(ports[i].pno == INVALID_PORT) 
    145261                        return i; 
    146         } 
    147         _nrk_errno_set(NO_PORTS_AVAILABLE);      
    148         return NRK_ERROR;                                                         
    149 } 
    150 /******************************************************************************************/ 
    151 int8_t check_port_available(int8_t pt) 
    152 // This private function checks to see if port 'pt' is available and returns NRK_OK or NRK_ERROR  
    153 // accordingly  
    154 { 
    155         if( ((ports >> pt) & ((uint32_t)1)) == 0 )                      // bit 'pt' is 0  
    156                 return NRK_OK; 
    157          
    158         _nrk_errno_set(PORT_UNAVAILABLE);        
     262                         
     263        _nrk_errno_set(NO_PORT_ELEMENT_AVAILABLE); 
    159264        return NRK_ERROR; 
    160265} 
    161 /*****************************************************************************************/ 
    162 void assign_port(int8_t pt) 
    163 // This private function records the fact that port 'pt' has been assigned  
    164 { 
    165         ports |= (uint32_t)1 << pt;                     // set bit 'pt' in 'ports' 
    166         return; 
    167 } 
    168 /******************************************************************************************/ 
    169 void release_port(int8_t pt) 
    170 // This private function records the fact that port 'pt' is now released  
    171 { 
    172         ports &= ~( (uint32_t)1 << pt );                // clear bit 'pt' in 'ports' 
    173         return;  
    174 } 
    175  
    176 /*****************************************************************************************/ 
     266/*******************************************************************************************/ 
     267int8_t get_index_unassigned_rbm_element() 
     268{ 
     269        int8_t i; 
     270         
     271        for(i = 0; i < MAX_PORTS; i++) 
     272                if(rx_buf_mgr[i].port == NULL)   
     273                        return i; 
     274                         
     275        _nrk_errno_set(NO_RBM_ELEMENT_AVAILABLE); 
     276        return NRK_ERROR; 
     277} 
     278/*********************************************************************************************/ 
    177279int8_t bind(int8_t sock_num, int8_t port) 
    178280{ 
    179         int8_t index;                                                   // stores the index of next available receive buffer                      
     281        int8_t buf_index;                                               // stores the index of next available receive buffer 
     282        int8_t port_index;                                      // stores the index of the next available port element 
     283        int8_t rbm_index;                                               // stores the index of the next available rbm element  
    180284        int8_t i;                                                               // loop index  
    181285        int8_t size;                                                    // size of receive queue allocated for this port  
    182          
    183         enter_cr(bm_sem, 8);     
     286        Port *p;                                                                        // pointer to port element  
     287         
     288        enter_cr(bm_sem, 8);                                    // capture the necessary semaphores  
    184289        enter_cr(tl_sem, 8); 
    185290                         
    186         if(tlayer_init_done != 1) 
     291        // ERROR CHECKING        
     292        if(tlayer_init_done != 1)                       // transport layer not initialised  
    187293        { 
    188294                nrk_int_disable(); 
     
    192298        } 
    193299                                 
    194         // ERROR CHECKING  
    195300        if(sock_num < 0 || sock_num >= MAX_PORTS)       // bad input  
    196301        { 
    197302                _nrk_errno_set(INVALID_ARGUMENT); 
    198303                 
    199                 leave_cr(tl_sem, "bind()");      
    200                 leave_cr(bm_sem, "bind()");      
     304                leave_cr(tl_sem, 8);     
     305                leave_cr(bm_sem, 8);     
    201306                return NRK_ERROR; 
    202307        } 
     
    206311                _nrk_errno_set(INVALID_ARGUMENT); 
    207312                 
    208                 leave_cr(tl_sem, "bind()");      
    209                 leave_cr(bm_sem, "bind()"); 
     313                leave_cr(tl_sem, 8);     
     314                leave_cr(bm_sem, 8); 
    210315                return NRK_ERROR; 
    211316        } 
     
    215320                _nrk_errno_set(INVALID_SOCKET); 
    216321                 
    217                 leave_cr(tl_sem, "bind()");      
    218                 leave_cr(bm_sem, "bind()"); 
    219                 return NRK_ERROR; 
    220         } 
    221          
    222         if(rx_buf_mgr[port].pid == nrk_get_pid())       // bind() / set_rx_queue_size() was called earlier 
     322                leave_cr(tl_sem, 8);     
     323                leave_cr(bm_sem, 8); 
     324                return NRK_ERROR; 
     325        } 
     326         
     327        if(sock[sock_num].port != NULL) // bind() / set_rx_queue_size() was called earlier 
    223328        { 
    224329                _nrk_errno_set(INVALID_CALL); 
    225330                 
    226                 leave_cr(tl_sem, "bind()");      
    227                 leave_cr(bm_sem, "bind()"); 
    228                 return NRK_ERROR; 
    229         } 
    230          
    231         if(check_port_available(port) == NRK_ERROR)             // check to see if port is already taken  
    232         { 
    233                 leave_cr(tl_sem, "bind()");      
    234                 leave_cr(bm_sem, "bind()");      
     331                leave_cr(tl_sem, 8);     
     332                leave_cr(bm_sem, 8); 
     333                return NRK_ERROR; 
     334        } 
     335         
     336        if(check_port_available((uint8_t)port) == NRK_ERROR)            // check to see if port is already taken  
     337        { 
     338                leave_cr(tl_sem, 8);     
     339                leave_cr(bm_sem, 8);     
    235340                return NRK_ERROR;                                                                               // error no is already set = PORT_UNAVAILABLE 
    236341        }  
     
    240345                _nrk_errno_set(NO_RX_BUFFERS_AVAILABLE); 
    241346                 
    242                 leave_cr(tl_sem, "bind()");      
    243                 leave_cr(bm_sem, "bind()"); 
    244                 return NRK_ERROR; 
    245         } 
    246                                  
     347                leave_cr(tl_sem, 8);     
     348                leave_cr(bm_sem, 8); 
     349                return NRK_ERROR; 
     350        } 
     351         
     352        // error checking complete. Begin processing      
    247353        size = DEFAULT_RX_QUEUE_SIZE; 
    248354        // at this point, we know that 
     
    251357        // 3. bind() / setReceiveQueueSize() was not called earlier 
    252358         
    253         // update the necessary state variables 
    254         sock[sock_num].port = port;                                     // assign the socket a port number  
    255         assign_port(port);                                                              // set bit 'port' in 'ports' equal to 1 
    256          
    257         rx_buf_mgr[port].pid = nrk_get_pid();           // fill up the members of the ReceiveBufferManager 
    258         if( nrk_signal_register(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 
     359        // get the indices of the port element and the rbm element  
     360        port_index = get_index_unassigned_port_element(); 
     361        rbm_index = get_index_unassigned_rbm_element(); 
     362        if(port_index == NRK_ERROR || buf_index == NRK_ERROR)   // this should not happen 
    259363        { 
    260364                nrk_int_disable(); 
    261                 nrk_led_set(RED_LED); 
    262                 while(1) 
    263                         nrk_kprintf(PSTR("bind(): Error while registering for seg_arrived_signal\r\n")); 
    264         } 
    265                  
     365                nrk_set_led(RED_LED); 
     366                while(1) 
     367                        nrk_kprintf(PSTR("bind(): Bug discovered in implementation of port/rbm element array\r\n")); 
     368        }  
     369 
     370        // fill up the members of the port element       
     371        assign_port( &ports[port_index], (uint8_t)port);         
     372         
     373        // fill up the members of the socket             
     374        sock[sock_num].port = &ports[port_index];        
     375        sock[sock_num].rbm = &rx_buf_mgr[rbm_index] 
     376         
     377        // fill up the members of the receive buffer manager      
     378        rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 
     379        rx_buf_mgr[rbm_index].port = &ports[port_index];         
     380         
    266381        // allocate 'size' buffers  
    267382        for(i = 1; i <= size; i++) 
    268383        { 
    269                 index = get_index_unallocated_rx_buf(); // look for the unallocated receive buffer  
    270                 if(index == NRK_ERROR)  //       this should not happen  
     384                buf_index = get_index_unallocated_rx_buf();     // look for the unallocated receive buffer  
     385                if(buf_index == NRK_ERROR)      //       this should not happen  
    271386                { 
    272387                        nrk_int_disable(); 
     
    276391                } 
    277392                 
    278                 insert_rx_fq(&rx_buf_udp[index], port, EMPTY);  // insert the buffer in the free queue of the port 
    279                 rx_buf_mgr[port].countTotal++;                                          // increment the countTotal for this port  
     393                // insert the buffer in the free queue of the port 
     394                insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); 
     395                rx_buf_mgr[rbm_index].countTotal++;                     // increment the countTotal for this port  
    280396                num_bufs_free--;                 
    281397        } 
    282398         
    283         leave_cr(tl_sem, "bind()");      
    284         leave_cr(bm_sem, "bind()"); 
     399        leave_cr(tl_sem, 8);     
     400        leave_cr(bm_sem, 8); 
    285401        return NRK_OK;  
    286402} 
     
    288404int8_t get_rx_queue_size(int8_t sock_num) 
    289405{ 
    290         int8_t port;            // to store the port number associated with this socket number  
    291         int8_t count; 
    292          
    293         enter_cr(bm_sem, 9);     
     406        int8_t count;           // to store the number of rx buffers associated with the port number  
     407         
     408        enter_cr(bm_sem, 9);    // capture the necessary semaphores  
    294409        enter_cr(tl_sem, 9); 
    295410         
     
    313428        } 
    314429         
    315         if(sock[sock_num].port == INVALID_PORT) // no socket operations were performed yet 
     430        if(sock[sock_num].port == NULL) // no socket operations were performed yet 
    316431        { 
    317432                leave_cr(tl_sem, 9);     
    318433                leave_cr(bm_sem, 9);     
    319                 return 0; 
    320         } 
    321                  
    322         // at this point we know that some socket operation has been performed on the socket prior to this call 
    323         port = sock[sock_num].port; 
    324         count = rx_buf_mgr[port].countTotal; 
     434                return 0;                                                       // hence return 0  
     435        } 
     436         
     437        if(sock[sock_num].rbm == NULL)  // this should not happen 
     438        { 
     439                nrk_int_disable(); 
     440                nrk_led_set(RED_LED); 
     441                while(1) 
     442                        nrk_kprintf(PSTR("get_rx_queue_size(): Bug discovered in implementation of sock array\r\n")); 
     443        }                
     444        // at this point we know that some socket operation has been performed on the socket 
     445        // prior to this call 
     446        count = sock[sock_num].rbm -> countTotal; 
    325447         
    326448        leave_cr(tl_sem, 9);     
     
    332454int8_t set_rx_queue_size(int8_t sock_num, int8_t size) 
    333455{ 
    334         int8_t port;            // to store the port number associated with given socket number 
     456        uint8_t port;           // to store the port number associated with given socket number 
    335457        int8_t i;                       // loop index 
    336458        int8_t flag;            // to mark whether bind() was called prior to this call or not  
    337         int8_t index;           // stores the index of next unallocated receive buffer   
    338                  
    339         enter_cr(bm_sem, 10); 
     459        int8_t buf_index;       // stores the index of next unallocated receive buffer 
     460        int8_t rbm_index; // stores the index of an unassigned rbm element 
     461        int8_t port_index;// stores the index of an unassigned port element   
     462                 
     463        enter_cr(bm_sem, 10);   // capture the necessary semaphores  
    340464        enter_cr(tl_sem, 10); 
    341465                 
     
    368492        } 
    369493         
    370         if(sock[sock_num].port == INVALID_PORT) // there is no port associated with this socket yet 
     494        if(sock[sock_num].port == NULL)                         // there is no port associated with this socket yet 
    371495        { 
    372496                flag = 1;                                                                               // bind() was not called earlier  
    373497                port = get_next_available_port();               // search for a ephemeral port number  
    374                 if(port == NRK_ERROR)                                           // no port available  
     498                if(port == INVALID_PORT)                                        // no port available  
    375499                { 
    376500                        leave_cr(tl_sem, 10); 
     
    387511           } 
    388512                // port available and default num of buffers available,  
    389                 // assign the new port number to the socket desc                 
    390                 sock[sock_num].port = port; 
    391                 assign_port(port); 
    392                 if( nrk_signal_register(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 
     513                // get the indices of the rbm and port element  
     514                port_index = get_index_unassigned_port_element(); 
     515                rbm_index = get_index_unassigned_rbm_element(); 
     516                 
     517                if(port_index == NRK_ERROR || rbm_index == NRK_ERROR)   // this should not happen  
    393518                { 
    394519                        nrk_int_disable(); 
    395520                        nrk_led_set(RED_LED); 
    396521                        while(1) 
    397                                 nrk_kprintf(PSTR("set_rx_queue_size(): Error registering for seg_arrived_signal\r\n")); 
    398                 }  
    399         } 
     522                                nrk_kprintf(PSTR("set_rx_queue_size(): Bug discovered in implementation of port/rbm array")); 
     523                }        
     524                 
     525                // fill up the contents of the port  
     526                assign_port(&ports[port_index], port); 
     527                                 
     528                // fill up the members of the socket descriptor 
     529                sock[sock_num].port = &ports[port_index]; 
     530                sock[sock_num].rbm = &rx_buf_mgr[rbm_index]; 
     531                                 
     532                // fill up the members of the rbm element 
     533                rx_buf_mgr[rbm_element].pid = nrk_get_pid(); 
     534                rx_buf_mgr[rbm_element].port = &ports[port_index]; 
     535                 
     536        } // end if(bind was not called earlier) 
    400537        else // bind was called earlier. DEFAULT_RX_QUEUE_SIZE buffers already allocated 
    401538        { 
    402                 // FIX ME  
    403539                if(size == DEFAULT_RX_QUEUE_SIZE)       // nothing to do, buffers already allocated 
    404540                { 
     
    407543                        return size; 
    408544                } 
    409         } 
    410          
    411         // check to see if there are 'size' buffers available in the system      
     545                else                                                                                    // size > DEFAULT_RX_QUEUE_SIZE  
     546                        size -= DEFAULT_RX_QUEUE_SIZE; 
     547        } 
     548         
     549        // at this point, the socket has been given a valid port number and a receive buffer manager 
     550        // with at least one receive buffer available for its use 
     551                 
     552        // check to see if there are actually 'size' buffers available in the system     
    412553        if(get_num_bufs_free() < size) 
    413554                size = get_num_bufs_free(); 
    414555                 
    415         // at this point, the socket is associated with a valid port number 
    416         // and at least one receive buffer is free 
    417         // fill in the values of the corresponding ReceiveBufferManager 
    418         rx_buf_mgr[port].pid = nrk_get_pid(); 
    419          
    420556        // allocate 'size' buffers  
    421557        for(i = 1; i <= size; i++) 
    422558        { 
    423                 index = get_index_unallocated_rx_buf(); // look for the next unallocated receive buffer  
    424                 if(index == NRK_ERROR)  //       this should not happen  
     559                buf_index = get_index_unallocated_rx_buf();     // look for the next unallocated receive buffer  
     560                if(buf_index == NRK_ERROR)      //       this should not happen  
    425561                { 
    426562                        nrk_int_disable(); 
     
    429565                                nrk_kprintf(PSTR("set_rx_queue_size(): Bug found in implementation of num_bufs_free\r\n")); 
    430566                }                
    431                 insert_rx_fq(&rx_buf_udp[index], port, EMPTY);  // insert the buffer in the free queue of the port 
    432                 rx_buf_mgr[port].countTotal++;  
     567                // insert the buffer in the free queue of the port               
     568                insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); 
     569                rx_buf_mgr[rbm_index].countTotal++;  
    433570                num_bufs_free--;                 
    434571        } 
     
    441578int8_t release_buffer(int8_t sock_num, uint8_t *ptr) 
    442579{ 
    443         int8_t port;                                            // to store the port number associated with the socket 
     580        int8_t rbm_index;                                       // index of the rbm element associated with this socket  
    444581        ReceiveBufferUDP *buf;                  // pointer to traverse the buffer list of the free queue  
    445582         
     
    464601                return NRK_ERROR; 
    465602        } 
    466         if(sock[sock_num].port == INVALID_PORT) // if no socket operation was performed earlier  
     603        if(sock[sock_num].port == NULL) // if no socket operation was performed earlier  
    467604        { 
    468605                _nrk_errno_set(INVALID_SOCKET); 
     
    473610        } 
    474611                 
    475         port = sock[sock_num].port;                               // retrieve the port number from the socket descriptor  
     612        if(sock[sock_num].rbm == NULL)  // this should not happen. Sanity check  
     613        { 
     614                nrk_int_disable(); 
     615                nrk_led_set(RED_LED); 
     616                while(1) 
     617                        nrk_kprintf(PSTR("release_buffer(): Bug discovered in implementation of sock element\r\n")); 
     618        }        
    476619         
    477620        // check to see if the pointer passed is valid 
    478         buf = rx_buf_mgr[port].head_fq; 
     621        buf = sock[sock_num].rbm -> head_fq; 
    479622        while(buf != NULL) 
    480623        { 
     
    493636                 
    494637        buf -> status = EMPTY;          // mark the buffer as available now 
    495         rx_buf_mgr[port].countFree++;  
     638        (sock[sock_num].rbm -> countFree)++;  
    496639         
    497640        leave_cr(tl_sem, 11);    
     
    502645int8_t close_socket(int8_t sock_num) 
    503646{ 
    504         int8_t port;                            // to store the port number assoicated with the given socket number 
    505647        ReceiveBufferUDP *ptr;  // temp variable  
    506648                 
     
    527669                 
    528670        // Free up resources associated with this socket descriptor  
    529         port = sock[sock_num].port; 
    530         if(port != INVALID_PORT)        // some operation was performed earlier with this socket  
    531         { 
    532                 rx_buf_mgr[port].pid = INVALID_PID; 
    533                 if( nrk_signal_delete(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 
     671        if(sock[sock_num].port != NULL) // some operation was performed earlier with this socket  
     672        { 
     673                // remove all the FULL buffers from the port queue                       
     674                while( (ptr = remove_rx_pq(sock[sock_num].rbm)) != NULL )                
     675                        ptr -> status = UNALLOCATED;             
     676                         
     677                // remove all the EMPTY buffers from the free queue              
     678                while( (ptr = remove_rx_fq(sock[sock_num].rbm, EMPTY)) != NULL )  
     679                        ptr -> status = UNALLOCATED; 
     680                         
     681                // remove all the FULL buffers from the free queue               
     682                while( (ptr = remove_rx_fq(sock[sock_num].rbm, FULL)) != NULL ) 
     683                        ptr -> status = UNALLOCATED; 
     684 
     685                // free up the rbm element               
     686                sock[sock_num].rbm -> pid = INVALID_PID; 
     687                sock[sock_num[.rbm -> port = NULL;               
     688                                 
     689                // release the port              
     690                release_port(sock[sock_num].port); 
     691        }   
     692         
     693        sock[sock_num].port = NULL; 
     694        sock[sock_num].pid = INVALID_PID; 
     695        sock[sock_num].rbm = NULL; 
     696         
     697        leave_cr(tl_sem, 12); 
     698        leave_cr(bm_sem, 12);    
     699        return NRK_OK; 
     700} 
     701/************************************************************************************************/ 
     702int8_t is_port_associated(int16_t port) 
     703{ 
     704        // ERROR checking 
     705        if(port <= 0 || port > MAX_PORT_NUM)    // bad input 
     706        { 
     707                _nrk_errno_set(INVALID_ARGUMENT); 
     708                return NRK_ERROR; 
     709        }        
     710         
     711        enter_cr(tl_sem, 13); 
     712        if(check_port_available((uint8_t)port) == NRK_OK) 
     713        { 
     714                leave_cr(tl_sem, 13); 
     715                return FALSE; 
     716        } 
     717        leave_cr(tl_sem, 13); 
     718        return TRUE; 
     719} 
     720/************************************************************************************************/  
     721int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int8_t dest_port, int8_t prio) 
     722{ 
     723        int8_t result;                  // to hold the return type of various functions  
     724         
     725        if(DEBUG_TL == 2) 
     726                nrk_kprintf(PSTR("Entered send operation of TL\r\n")); 
     727                         
     728        enter_cr(bm_sem, 14); 
     729        enter_cr(tl_sem, 14); 
     730         
     731        // ERROR checking 
     732        if(sock_num < 0 || sock_num >= MAX_PORTS)                                                               // bad input  
     733        { 
     734                _nrk_errno_set(INVALID_ARGUMENT); 
     735                 
     736                leave_cr(tl_sem, 14); 
     737                leave_cr(bm_sem, 14);            
     738                return NRK_ERROR; 
     739        } 
     740                 
     741        if(ptr == NULL || len <= 0 || len > MAX_APP_PAYLOAD)                            // bad input  
     742        { 
     743                _nrk_errno_set(INVALID_ARGUMENT); 
     744                 
     745                leave_cr(tl_sem, 14); 
     746                leave_cr(bm_sem, 14); 
     747                return NRK_ERROR; 
     748        } 
     749                 
     750        if(dest_addr < 0 || dest_port <= 0 || dest_port > MAX_PORTS)    // bad input  
     751        { 
     752                _nrk_errno_set(INVALID_ARGUMENT); 
     753                 
     754                leave_cr(tl_sem, 14); 
     755                leave_cr(bm_sem, 14); 
     756                return NRK_ERROR; 
     757        } 
     758         
     759        if(prio <= 0 || prio > MAX_PRIORITY)                                                                    // bad input  
     760        { 
     761                _nrk_errno_set(INVALID_ARGUMENT); 
     762                 
     763                leave_cr(tl_sem, 14); 
     764                leave_cr(bm_sem, 14); 
     765                return NRK_ERROR; 
     766        } 
     767                 
     768        if(sock[sock_num].pid != nrk_get_pid())                                                         // wrong socket number  
     769        { 
     770                _nrk_errno_set(INVALID_SOCKET); 
     771                 
     772                leave_cr(tl_sem, 14); 
     773                leave_cr(bm_sem, 14); 
     774                return NRK_ERROR; 
     775        } 
     776                 
     777        /* Error checking is complete. Begin processing */ 
     778        if(sock[sock_num].port == NULL) // this means that bind() / set_rx_queue_size() was  
     779                                                                                                // not called earlier 
     780        { 
     781                int8_t buf_index, rbm_index, port_index; 
     782                uint8_t port; 
     783                 
     784                // retrieve the corresponding indices 
     785                rbm_index = get_index_unassigned_rbm_element(); 
     786                port_index = get_index_unassigned_port_element(); 
     787                if(rbm_index == NRK_ERROR || port_index == NRK_ERROR)   // this should not happen 
    534788                { 
    535789                        nrk_int_disable(); 
    536790                        nrk_led_set(RED_LED); 
    537791                        while(1) 
    538                                 nrk_kprintf(PSTR("close_socket(): Error in deleting seg_arrived_signal\r\n")); 
     792                                nrk_kprintf(PSTR("send(): Bug detected in implementation of port and rbm array\r\n")); 
    539793                } 
    540  
    541                 // remove all the FULL buffers from the port queue                       
    542                 while( (ptr = remove_rx_pq(port)) != NULL )              
    543                         ptr -> status = UNALLOCATED;             
    544                          
    545                 // remove all the EMPTY buffers from the free queue              
    546                 while( (ptr = remove_rx_fq(port, EMPTY)) != NULL )  
    547                         ptr -> status = UNALLOCATED; 
    548                          
    549                 // remove all the FULL buffers from the free queue               
    550                 while( (ptr = remove_rx_fq(port,FULL)) != NULL ) 
    551                         ptr -> status = UNALLOCATED; 
    552                  
    553                 release_port(port); 
    554         }   
    555          
    556         sock[sock_num].port = INVALID_PORT; 
    557         sock[sock_num].pid = INVALID_PID; 
    558          
    559         leave_cr(tl_sem, 12); 
    560         leave_cr(bm_sem, 12);    
    561         return NRK_OK; 
    562 } 
    563 /************************************************************************************************/ 
    564 int8_t is_port_associated(int8_t port) 
    565 { 
    566         enter_cr(tl_sem, 13); 
    567                  
    568         if(check_port_available(port) == NRK_OK) 
    569         { 
    570                 leave_cr(tl_sem, 13); 
    571                 return FALSE; 
    572         } 
    573         leave_cr(tl_sem, 13); 
    574         return TRUE; 
    575 } 
    576 /************************************************************************************************/  
    577 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int16_t dest_addr, int8_t dest_port, int8_t prio) 
    578 { 
    579         int8_t port;                    // to hold the port number corresponding to the socket number passed 
    580         int8_t result;                  // to hold the return type of various functions  
    581          
    582         if(DEBUG_TL == 2) 
    583                 nrk_kprintf(PSTR("Entered send operation of TL\r\n")); 
    584                          
    585         enter_cr(bm_sem, 14); 
    586         enter_cr(tl_sem, 14); 
    587          
    588         if(DEBUG_TL == 2) 
    589                 nrk_kprintf(PSTR("Inside CR of send operation of TL\r\n")); 
    590          
    591         // ERROR checking 
    592         if(sock_num < 0 || sock_num >= MAX_PORTS)                                                               // bad input  
    593         { 
    594                 _nrk_errno_set(INVALID_ARGUMENT); 
    595                  
    596                 leave_cr(tl_sem, 14); 
    597                 leave_cr(bm_sem, 14);            
    598                 return NRK_ERROR; 
    599         } 
    600                  
    601         if(ptr == NULL || len <= 0 || len > MAX_APP_PAYLOAD)                            // bad input  
    602         { 
    603                 _nrk_errno_set(INVALID_ARGUMENT); 
    604                  
    605                 leave_cr(tl_sem, 14); 
    606                 leave_cr(bm_sem, 14); 
    607                 return NRK_ERROR; 
    608         } 
    609                  
    610         if(dest_addr < 0 || dest_port <= 0 || dest_port > MAX_PORTS)    // bad input  
    611         { 
    612                 _nrk_errno_set(INVALID_ARGUMENT); 
    613                  
    614                 leave_cr(tl_sem, 14); 
    615                 leave_cr(bm_sem, 14); 
    616                 return NRK_ERROR; 
    617         } 
    618          
    619         if(prio <= 0 || prio > MAX_PRIORITY)                                                                    // bad input  
    620         { 
    621                 _nrk_errno_set(INVALID_ARGUMENT); 
    622                  
    623                 leave_cr(tl_sem, 14); 
    624                 leave_cr(bm_sem, 14); 
    625                 return NRK_ERROR; 
    626         } 
    627                  
    628         if(sock[sock_num].pid != nrk_get_pid())                                                         // wrong socket number  
    629         { 
    630                 _nrk_errno_set(INVALID_SOCKET); 
    631                  
    632                 leave_cr(tl_sem, 14); 
    633                 leave_cr(bm_sem, 14); 
    634                 return NRK_ERROR; 
    635         } 
    636                  
    637         /* Error checking is complete. Begin processing */ 
    638         if(sock[sock_num].port == INVALID_PORT) // this means that bind() / set_rx_queue_size() was  
    639                                                                                                                         // not called earlier 
    640         { 
    641                 // FIX ME IMPORTANT.................     
    642                 leave_cr(tl_sem, 14); 
    643                 leave_cr(bm_sem, 14);    
    644                 result = set_rx_queue_size(sock_num, DEFAULT_RX_QUEUE_SIZE); 
    645                 enter_cr(bm_sem, 14); 
    646                 enter_cr(tl_sem, 14); 
    647                 if(result == NRK_ERROR)         // error number will be set already  
    648                 { 
    649                         leave_cr(tl_sem, 14); 
    650                         leave_cr(bm_sem, 14);            
     794                buf_index = get_index_unallocated_rx_buf(); 
     795                if(buf_index == NRK_ERROR) 
     796                        return NRK_ERROR;                       // error no is already set to NO_RX_BUFFERS_AVAILABLE  
     797                         
     798                port = get_next_available_port(); 
     799                if(port == INVALID_PORT)        // error no is already set to NO_PORTS_AVAILABLE         
    651800                        return NRK_ERROR; 
    652                 } 
    653         } 
    654         port = sock[sock_num].port;     // retrieve the port number 
    655         // register for the 'send done' signal  
    656         if( nrk_signal_register(sd_sig[port].send_done_signal) == NRK_ERROR ) 
    657         { 
    658                 nrk_int_disable(); 
    659                 nrk_led_set(RED_LED); 
    660                 while(1) 
    661                         nrk_kprintf(PSTR("send(): Error while registering for send_done_signal\r\n")); 
    662         }        
    663                  
     801                 
     802                // fill up the members of the port 
     803                assign_port(&ports[port_index], port); 
     804                                 
     805                // fill up the members of the socket  
     806                sock[sock_num].port = &ports[port_index]; 
     807                sock[sock_num].rbm = &rx_buf_mgr[rbm_index]; 
     808                 
     809                // fill up the members of the receive buffer manager 
     810                rx_buf_mgr[rbm_index].port = &ports[port_index]; 
     811                rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 
     812                insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); 
     813                rx_buf_mgr[rbm_index].countTotal++;  
     814                num_bufs_free--;         
     815        } 
     816         
    664817        // prepare a UDP segment  
    665         udp_seg.srcPort = port; 
     818        udp_seg.srcPort = sock[sock_num].port -> pno; 
    666819        udp_seg.destPort = dest_port; 
    667820        udp_seg.length = len; 
     
    680833        // insert the packet into the active transmit queue of the system 
    681834         
    682         if(DEBUG_TL == 2) 
    683                 nrk_kprintf(PSTR("Inside send(). Before inserting packet in transmit queue\r\n")); 
    684                  
    685         result = insert_tx_aq(&pkt); 
    686          
    687         if(DEBUG_TL == 2) 
    688                 nrk_kprintf(PSTR("Inside send(). After inserting packet in transmit queue\r\n")); 
    689                  
     835        result = insert_tx_aq(&pkt); 
     836         
    690837        if(result == NRK_ERROR) 
    691838                _nrk_errno_set(NO_TX_BUFFERS_AVAILABLE); 
     
    736883} 
    737884/*************************************************************************************************/ 
    738 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr, int8_t *srcPort, int8_t *rssi) 
    739 { 
    740         int8_t port;                                    // to hold the port number associated with the passed socket number      
    741         nrk_sig_mask_t my_sigs;         // to hold the return value of network layer signals  
    742         ReceiveBufferUDP *buf;          // temporary variable  
    743         Transport_Segment_UDP *seg;// pointer to received segment from the network layer  
     885uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi) 
     886{ 
     887        nrk_sig_mask_t my_sigs;                 // to hold the return value of network layer signals  
     888        ReceiveBufferUDP *buf;                  // temporary variable  
     889        Transport_Segment_UDP *seg;     // pointer to received segment from the network layer  
    744890         
    745891        enter_cr(bm_sem, 16); 
     
    756902        } 
    757903 
    758         if( sock[sock_num].pid != nrk_get_pid() || sock[sock_num].port == INVALID_PORT ) 
     904        if( sock[sock_num].pid != nrk_get_pid() || sock[sock_num].port == NULL ) 
    759905        { 
    760906                _nrk_errno_set(INVALID_SOCKET); // socket description not complete 
     
    766912         
    767913        // error checking complete. Begin processing     
    768         port = sock[sock_num].port; 
    769914        if(sock[sock_num].timeout.secs == 0 && sock[sock_num].timeout.nano_secs == 0) 
    770915        { 
     
    774919                 
    775920                // check the receive buffer manager for this port to see if there are any queued segments 
    776                 if(rx_buf_mgr[port].countFree == rx_buf_mgr[port].countTotal)   // no queued segments  
     921                if(sock[sock_num].rbm -> countFree == sock[sock_num].rbm -> countTotal) // no queued segments  
    777922                { 
    778923                        if(DEBUG_TL == 2) 
    779                                 printf("receive(): No segments in receive queue of port = %d\n", port); 
     924                                printf("receive(): No segments in receive queue of port = %d\n", sock[sock_num].port -> pno); 
     925                         
    780926                        while(1) 
    781927                        { 
    782928                                leave_cr(tl_sem, 16); 
    783929                                leave_cr(bm_sem, 16); 
    784                                 my_sigs = nrk_event_wait(SIG(rx_buf_mgr[port].seg_arrived_signal));     // block 
     930                                my_sigs = nrk_event_wait(SIG(sock[sock_num].port -> data_arrived_signal));      // block 
    785931                                enter_cr(bm_sem, 16); 
    786932                                enter_cr(tl_sem, 16);   
    787933                 
    788934                                // check what signal we got  
    789                         if(my_sigs == 0)                        
     935                        if(my_sigs == 0)        // this should not happen                        
    790936                        { 
    791937                                nrk_int_disable(); 
     
    794940                                                nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait (without timeout)\r\n")); 
    795941                                }        
    796                                 if( my_sigs & SIG(rx_buf_mgr[port].seg_arrived_signal) )        // got a segment  
     942                                if( my_sigs & SIG(sock[sock_num].port -> data_arrived_signal) ) // got a segment  
    797943                                { 
    798944                                        if(DEBUG_TL == 2) 
    799                                         nrk_kprintf(PSTR("receive(): Received the seg arrived signal\r\n")); 
     945                                        nrk_kprintf(PSTR("receive(): Received the data_arrived signal\r\n")); 
    800946                                break; 
    801947                        } 
     
    813959        { 
    814960                if(DEBUG_TL == 2) 
    815                         nrk_kprintf(PSTR("receive(): Inside the section that relates to 'with timeout' receive\r\n")); 
     961                        nrk_kprintf(PSTR("receive(): Inside the section that relates to 'with timeout'\r\n")); 
    816962                         
    817963                if( nrk_signal_register(nrk_wakeup_signal) == NRK_ERROR) 
     
    820966                        nrk_led_set(RED_LED); 
    821967                        while(1) 
    822                                 nrk_kprintf(PSTR("receive(): Error in registering for nrk_wakeup_signal (with timeout)\r\n")); 
     968                                nrk_kprintf(PSTR("receive(): Error in registering for nrk_wakeup_signal\r\n")); 
    823969                } 
    824970                if( nrk_set_next_wakeup(sock[sock_num].timeout) == NRK_ERROR) 
     
    827973                        nrk_led_set(RED_LED); 
    828974                        while(1) 
    829                                 nrk_kprintf(PSTR("receive(): Error returned by nrk_set_next_wakeup (with timeout)\r\n")); 
     975                                nrk_kprintf(PSTR("receive(): Error returned by nrk_set_next_wakeup()\r\n")); 
    830976                }                
    831977                // check the receive manager for this port to see if there are any queued segments 
    832                 if(rx_buf_mgr[port].countFree == rx_buf_mgr[port].countTotal)   // no queued segments  
     978                if(sock[sock_num].rbm -> countFree == sock[sock_num].rbm -> countTotal) // no queued segments  
    833979                { 
    834980                        while(1) 
     
    836982                                leave_cr(tl_sem, 16); 
    837983                                leave_cr(bm_sem, 16); 
    838                                 my_sigs = nrk_event_wait(SIG(rx_buf_mgr[port].seg_arrived_signal) | SIG(nrk_wakeup_signal)); 
     984                                my_sigs = nrk_event_wait(SIG(sock[sock_num].rbm -> data_arrived_signal) | SIG(nrk_wakeup_signal)); 
    839985                                enter_cr(bm_sem, 16); 
    840986                                enter_cr(tl_sem, 16);                                     
     
    848994                                                nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait() (with timeout)\r\n")); 
    849995                                }        
    850                         if( my_sigs & SIG(rx_buf_mgr[port].seg_arrived_signal) )        // got a segment  
     996                        if( my_sigs & SIG(rx_buf_mgr[port].data_arrived_signal) )       // got a segment  
    851997                        { 
    852998                                sock[sock_num].timeout.secs = 0;                // invalidate the timeout  
    853999                                sock[sock_num].timeout.nano_secs = 0; 
    8541000                                if(DEBUG_TL == 2) 
    855                                         nrk_kprintf(PSTR("Received the seg arrived signal\r\n")); 
    856                                 // FIX me change the wakeup period back to what it was 
    857                                 break; 
     1001                                        nrk_kprintf(PSTR("Received the data_arrived signal\r\n")); 
     1002                                break;  
    8581003                        } 
    8591004         
     
    8621007                                sock[sock_num].timeout.secs = 0;                        // invalidate the timeout value of the socket  
    8631008                                sock[sock_num].timeout.nano_secs = 0; 
    864                                 // FIX me change the wakeup period back to what it was  
    8651009                                _nrk_errno_set(SOCKET_TIMEOUT); 
    8661010                                 
     
    10801224} 
    10811225/**********************************************************************************************/ 
     1226void insert_pe_aq(Port *p) 
     1227{ 
     1228        // the new element always goes at the tail of the queue  
     1229        if(port_mgr.head_aq == NULL)    // the active queue is empty 
     1230        { 
     1231                port_mgr.head_aq = port_mgr.tail_aq = p; 
     1232        } 
     1233        else 
     1234        { 
     1235                port_mgr.tail_aq -> next = p; 
     1236                port_mgr.tail_aq = p; 
     1237        } 
     1238         
     1239        p -> next = NULL; 
     1240        port_mgr.count_aq++; 
     1241         
     1242        return; 
     1243} 
     1244/************************************************************************************************/ 
     1245int8_t remove_pe_aq(Port *pt) 
     1246{ 
     1247        Port *ptr = port_mgr.head_aq; 
     1248        Port *prev = NULL; 
     1249         
     1250        if(ptr == NULL) // the active queue is empty  
     1251                return NRK_OK; 
     1252                 
     1253        if(port_mgr.head_aq == port_mgr.tail_aq)        // only one element in the active queue  
     1254        { 
     1255                if(ptr == pt)   // found the port element 
     1256                { 
     1257                        port_mgr.head_aq = port_mgr.tail_aq = NULL; 
     1258                        port_mgr.count_aq--; 
     1259                        return NRK_OK; 
     1260                } 
     1261                else    // port element is not present in the active queue  
     1262                { 
     1263                        return NRK_ERROR; 
     1264                } 
     1265        } 
     1266         
     1267        // if the code reaches here it means there are at least two elements in the active queue  
     1268         
     1269        while(ptr != NULL) 
     1270        { 
     1271                if(ptr == pt)   // found the port element 
     1272                        break; 
     1273                 
     1274                prev  = ptr; 
     1275                ptr = ptr -> next; 
     1276        } 
     1277         
     1278        if(ptr == NULL) // pt was not found in the active queue  
     1279                return NRK_ERROR; 
     1280         
     1281        if(prev == NULL)        // pt is at the head of the active queue 
     1282        { 
     1283                port_mgr.head_aq = port_mgr_head_aq -> next; 
     1284                port_mgr.count_aq--; 
     1285                return NRK_OK; 
     1286        } 
     1287        else if(ptr == port_mgr.tail_aq)        // pt is found at the tail of the active queue 
     1288                  { 
     1289                                prev -> next = NULL; 
     1290                                port_mgr.tail_aq = prev; 
     1291                                port_mgr.count_aq--; 
     1292                                return NRK_OK; 
     1293                  } 
     1294                  else                                                          // pt is found somewhere in the middle of the queue  
     1295                  { 
     1296                                prev -> next = ptr -> next; 
     1297                                port_mgr.count_aq--; 
     1298                                return NRK_OK; 
     1299                  } 
     1300                   
     1301        return NRK_OK; 
     1302} 
     1303/***************************************************************************************************/ 
     1304void insert_pe_fq(Port *p) 
     1305{ 
     1306        // the new element always goes at the tail of the queue  
     1307        if(port_mgr.head_fq == NULL)    // the free queue is empty 
     1308        { 
     1309                port_mgr.head_fq = port_mgr.tail_fq = p; 
     1310        } 
     1311        else 
     1312        { 
     1313                port_mgr.tail_fq -> next = p; 
     1314                port_mgr.tail_fq = p; 
     1315        } 
     1316         
     1317        p -> next = NULL; 
     1318        port_mgr.count_fq++; 
     1319         
     1320        return; 
     1321}        
     1322/***************************************************************************************************/ 
     1323Port* remove_pe_fq() 
     1324{ 
     1325        Port *ptr;      // pointer to the head of the free queue  
     1326                 
     1327        // the element at the head of the free queue is always removed 
     1328        if(port_mgr.head_fq == NULL)    // the free queue is empty 
     1329                return NULL; 
     1330                 
     1331        if(port_mgr.head_fq == port_mgr.tail_fq)        // only one element in the free queue 
     1332                port_mgr.tail_fq = NULL; 
     1333                 
     1334        ptr = port_mgr.head_aq; 
     1335         
     1336        port_mgr.head_aq = port_mgr.head_aq -> next; 
     1337        port_mgr.count_fq--; 
     1338         
     1339        return ptr; 
     1340} 
     1341/*************************************************************************************************/ 
     1342                 
     1343         
     1344                         
     1345         
     1346                         
     1347         
     1348         
     1349                 
     1350 
  • nano-RK/projects/network_stack/TransportLayerUDP.h

    r113 r116  
    1919#define SOCK_RAW 3                                      // if the application wants to bypass the transport and network layers  
    2020 
    21 #define EPHEMERAL_PORT_NUM_START 11             // 11 is the smallest port number available to user tasks 
     21#define EPHEMERAL_PORT_NUM_START 11             // smallest port number available to user tasks 
     22#define MAX_PORT_NUM 255                                        // largest port number available to user tasks  
    2223 
    2324// limits on various object types  
     
    2930 
    3031/********************************* DATA STRUCTURES ****************************************/ 
    31  
    3232typedef struct 
    3333{ 
    34         int8_t srcPort;                                         // source port of the application 
    35         int8_t destPort;                                                // destination port of the application 
     34        uint8_t srcPort;                                                // source port of the application 
     35        uint8_t destPort;                                               // destination port of the application 
    3636        int8_t length;                                                  // actual length of the data payload 
    3737        uint8_t data[MAX_APP_PAYLOAD];  // application layer data  
     
    4141typedef struct 
    4242{ 
    43         int8_t port;                                                    // port number associated with this socket descriptor  
    44         int8_t pid;                                                             // pid of the process holding this socket descriptor 
     43        uint8_t pno;                                                    // the actual port number  
     44        nrk_sig_t data_arrived_signal;  // 'data arrived' signal for this port  
     45        nrk_sig_t send_done_signal;             // 'send done' signal for this port  
     46         
     47}Port;  
     48 
     49typedef struct 
     50{ 
     51        Port *port;                                                             // port information associated with this socket 
     52        ReceiveBufferManager *rbm;                      // receive buffer manager for this socket  
     53        int8_t pid;                                                             // id of the process holding this socket   
    4554        int8_t type;                                                    // type of socket 
    46         nrk_time_t timeout;                                     // to store a timeout value for the socket  
     55        nrk_time_t timeout;                                     // to store a timeout value for the socket 
    4756}Socket; 
    4857 
     
    6473 
    6574        PARAMS:         None 
    66         RETURNS:                index of the next available socket descriptor if available, NRK_ERROR otherwise 
     75        RETURNS:                index of the next available socket descriptor if available (0 - (MAX_PORTS - 1)), 
     76                                        NRK_ERROR otherwise 
    6777        ERROR NOS:      NO_SOCKET_DESC_AVAILABLE when no descriptors are available  
    6878        COMMENTS:       private function  
     
    7181int8_t create_socket(int8_t type); 
    7282/* 
    73 This function creates a socket of a given type and returns it's descriptor 
    74  
    75         PARAMS:         type: The type of the socket. Currently only SOCK_DGRAM is supported 
     83This function creates a socket of a given type and returns it's descriptor number 
     84 
     85        PARAMS:         type: The type of the socket. Currently only SOCK_DGRAM and SOCK_IPC are supported 
    7686        RETURNS:                socket descriptor or NRK_ERROR otherwise 
    7787        ERROR NOS:      UNSUPPORTED_SOCK_TYPE if an invalid socket type is passed 
     
    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 (EPHEMERAL_PORT_NUM_START - MAX_PORT_NUM) 
     98                                        INVALID_PORT otherwise 
    8899        ERROR NOS:      NO_PORTS_AVAILABLE if no ephemeral port numbers are available 
    89100        COMMENTS:       private function  
    90101*/ 
    91102 
    92 int8_t check_port_available(int8_t pt); 
     103int8_t check_port_available(uint8_t pt); 
    93104/*  
    94105This function checks whether port 'pt' is available 
     
    100111*/  
    101112 
    102 void assign_port(int8_t pt); 
    103 /* 
    104 This functions records the fact that port 'pt' is allocated 
    105  
    106         PARAMS:         pt: port number to be allocated 
     113int8_t assign_port(Port *p, uint8_t pt); 
     114/* 
     115This functions records the fact that port 'pt' is allocated. It also creates and registers 
     116the two signals associated with the port 
     117 
     118        PARAMS:         p: pointer to the empty element within the 'Port' array 
     119                                        pt: port number to be allocated 
     120        RETURNS:                NRK_OK if the assignment is successful 
     121                                        NRK_ERROR otherwise 
     122         
     123        ERROR NOS:      NO_SIGNALS_AVAILABLE if one of the two signals could not be created 
     124        COMMENTS:       private function 
     125         
     126*/ 
     127 
     128void release_port(Port *p); 
     129/* 
     130This functions records the fact that a given port number is now available 
     131 
     132        PARAMS:         p: Pointer to the port information 
    107133        RETURNS:                None 
    108134        COMMENTS:       private function 
    109          
    110 */ 
    111  
    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 
    117         RETURNS:                None 
    118         COMMENTS:       None 
     135*/ 
     136 
     137int8_t get_index_unassigned_port_element() 
     138/* 
     139This function returns the index of an unassigned port element if available 
     140         
     141        PARAMS:         None 
     142        RETURNS:                the index or NRK_ERROR if none is available 
     143         
     144        ERROR NOS:      NO_PORT_ELEMENT_AVAILABLE 
     145        Comments:       private function 
     146*/ 
     147 
     148int8_t get_index_unassigned_rbm_element() 
     149/* 
     150This function returns the index of an unassigned rbm element if available  
     151 
     152        PARAMS:         None 
     153        RETURNS:                the index or NRK_ERROR if none is available 
     154         
     155        ERROR NOS:      NO_RBM_ELEMENT_AVAILABLE 
     156        Comments:       private function 
    119157*/ 
    120158 
     
    138176*/ 
    139177 
    140 int8_t get_rx_queue_size(int8_t sock_num); 
     178int8_t get_rx_queue_size(int16_t sock_num); 
    141179/* 
    142180This function returns the size of the rx queue associated with a given socket descriptor 
     
    211249*/ 
    212250 
    213 int8_t is_port_associated(int8_t port); 
     251int8_t is_port_associated(int16_t port); 
    214252/* 
    215253This function checks whether a given port number has been assigned to a socket or not  
    216254                 
    217                 PARAMS: port: The port number 
    218                 RETURNS: TRUE if a mapping exists, FALSE otherwise 
     255                PARAMS:         port: The port number 
     256                RETURNS:        TRUE if a mapping exists, FALSE if a mapping does not exist 
     257                                                NRK_ERROR if some error is encountered 
     258                                         
     259                ERROR NOS:      INVALID_ARGUMENT when the passed parameter is faulty 
    219260                COMMENTS: User API  
    220261*/ 
    221262 
    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); 
     263int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int8_t dest_port, int8_t prio); 
    223264/* 
    224265This function can be used by the application task to send data to other nodes on the network 
     
    266307*/  
    267308 
    268 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr, int8_t *srcPort, int8_t *rssi); 
     309uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi); 
    269310/* 
    270311This function allows an application to receive a message along with network control information 
  • nano-RK/projects/network_stack/makefile

    r110 r116  
    11# Platform name  cc2420DK, firefly, micaZ 
    2 PLATFORM = firefly2_2 
     2PLATFORM = firefly2 
    33 
    44# Target file name (without extension). 
Note: See TracChangeset for help on using the changeset viewer.