Changeset 116
- Timestamp:
- 06/28/2007 11:21:55 AM (5 years ago)
- Location:
- nano-RK/projects/network_stack
- Files:
-
- 6 edited
-
BufferManager.c (modified) (7 diffs)
-
BufferManager.h (modified) (4 diffs)
-
NWErrorCodes.h (modified) (1 diff)
-
TransportLayerUDP.c (modified) (40 diffs)
-
TransportLayerUDP.h (modified) (10 diffs)
-
makefile (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
nano-RK/projects/network_stack/BufferManager.c
r113 r116 732 732 } 733 733 /*************************************************************************************************/ 734 ReceiveBufferUDP* remove_rx_pq( int8_t port)734 ReceiveBufferUDP* remove_rx_pq(ReceiveBufferManager *rbm) 735 735 { 736 736 // The remove is always done from the head of the queue since its a priority queue … … 739 739 //enter_cr(bm_sem, "remove_rx_pq()"); 740 740 741 if(r x_buf_mgr[port].head_pq == NULL) // no more queued segments741 if(rbm -> head_pq == NULL) // no more queued segments 742 742 { 743 743 //leave_cr(bm_sem, "remove_rx_pq()"); … … 745 745 } 746 746 747 if(r x_buf_mgr[port].head_pq == rx_buf_mgr[port].tail_pq) // only one buffer in queue748 r x_buf_mgr[port].tail_pq = NULL;749 750 ptr = r x_buf_mgr[port].head_pq;751 752 r x_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; 753 753 754 754 //leave_cr(bm_sem, "remove_rx_pq()"); … … 756 756 } 757 757 /*************************************************************************************************/ 758 759 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t port, int8_t status) 760 { 761 //enter_cr(bm_sem, "insert_rx_fq()"); 762 758 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t index, int8_t status) 759 { 763 760 // the insert is always done at the tail of the queue 764 if(rx_buf_mgr[ port].head_fq == NULL) // the free queue is empty765 { 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; 767 764 buf -> next = NULL; 768 765 } 769 766 else 770 767 { 771 rx_buf_mgr[ port].tail_fq -> next = buf;768 rx_buf_mgr[index].tail_fq -> next = buf; 772 769 buf -> next = NULL; 773 rx_buf_mgr[ port].tail_fq = buf;770 rx_buf_mgr[index].tail_fq = buf; 774 771 } 775 772 776 773 if(status == EMPTY) // this function was called from bind() / setReceiveQueueSize() 777 774 { 778 rx_buf_mgr[ port].countFree++;775 rx_buf_mgr[index].countFree++; 779 776 buf -> status = EMPTY; 780 777 } 781 778 782 //leave_cr(bm_sem, "insert_rx_fq()");783 779 return; 784 780 } 785 781 786 782 /***************************************************************************************************/ 787 ReceiveBufferUDP* remove_rx_fq( uint8_t port, int8_t status)783 ReceiveBufferUDP* remove_rx_fq(ReceiveBufferManager *rbm, int8_t status) 788 784 { 789 785 // This function removes the next receive buffer from the free queue having a given status … … 791 787 ReceiveBufferUDP *prev = NULL; 792 788 793 if(r x_buf_mgr[port].head_fq == NULL) // no buffers remaining in free queue789 if(rbm -> head_fq == NULL) // no buffers remaining in free queue 794 790 return NULL; 795 791 796 ptr = r x_buf_mgr[port].head_fq;792 ptr = rbm -> head_fq; 797 793 while(ptr != NULL) 798 794 { … … 812 808 if(prev == NULL) // the EMPTY / FULL buffer is at the head of the queue 813 809 { 814 if(r x_buf_mgr[port].head_fq == rx_buf_mgr[port].tail_fq) // only one buffer in free queue815 r x_buf_mgr[port].tail_fq = NULL;816 817 r x_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; 818 814 if(status == EMPTY) 819 rx_buf_mgr[port].countFree--;815 (rbm -> countFree)--; 820 816 return ptr; 821 817 } … … 824 820 prev -> next = ptr -> next; 825 821 if(status == EMPTY) 826 rx_buf_mgr[port].countFree--;822 (rbm -> countFree)--; 827 823 return ptr; 828 824 } -
nano-RK/projects/network_stack/BufferManager.h
r113 r116 36 36 37 37 // priority level of task ....SHIFT this to Anthony's code 38 #define MAX_TASK_PRIORITY 1 038 #define MAX_TASK_PRIORITY 127 39 39 40 40 // priority levels of messages … … 59 59 typedef struct 60 60 { 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 62 63 ReceiveBufferUDP *head_fq; // pointer to head of free queue (EMPTY or FULL buffers) 63 64 ReceiveBufferUDP *tail_fq; // pointer to tail of free queue (EMPTY or FULL buffers) … … 66 67 int8_t countTotal; // total number of buffers reserved for this port 67 68 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 70 70 }ReceiveBufferManager; // one for every port in the system 71 71 … … 90 90 }TransmitBufferManager; 91 91 92 typedef struct93 {94 nrk_sig_t send_done_signal;95 }SendDoneSignal;96 97 92 /***************************** FUNCTION PROTOTYPES ************************************/ 98 93 -
nano-RK/projects/network_stack/NWErrorCodes.h
r103 r116 19 19 #define NO_TX_BUFFERS_AVAILABLE 12 20 20 #define SOCKET_TIMEOUT 13 21 #define NO_PORT_ELEMENT_AVAILABLE 14 22 #define NO_RBM_ELEMENT_AVAILABLE 15 21 23 22 24 #endif -
nano-RK/projects/network_stack/TransportLayerUDP.c
r113 r116 9 9 /****************** Global data structures required for port and socket management ***************/ 10 10 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 11 Socket sock[MAX_PORTS]; // array to store available socket descriptors 0 - (MAX_PORTS - 1) 12 13 Port ports[MAX_PORTS]; // array to store information about each port 14 18 15 int8_t tlayer_init_done; // flag to indicate whether initialization has been done correctly 19 16 … … 31 28 extern TransmitBuffer tx_buf[]; 32 29 extern TransmitBufferManager tx_buf_mgr; 33 extern SendDoneSignal sd_sig[];34 30 extern nrk_sem_t *bm_sem; 35 31 … … 45 41 extern void pack_TL_UDP_header(uint8_t*, Transport_Segment_UDP*); 46 42 47 // From Debug.c48 extern void go_into_panic(int8_t *);49 50 43 /************************** Function definitions ***************************************************/ 51 44 void initialise_transport_layer_udp() 52 45 { 53 46 int8_t i; // loop index 54 // make all the socket descriptors unassigned 47 48 // initialise the socket descriptors and port elements 55 49 for(i = 0; i < MAX_PORTS; i++) 56 50 { 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); 66 62 if(tl_sem == NULL) 67 63 { … … 71 67 nrk_kprintf(PSTR("initialise_transport_layer_udp(): Error creating the semaphore\r\n")); 72 68 } 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 73 79 // set the init_done flag 74 80 tlayer_init_done = 1; … … 77 83 /**************************************************************************************************/ 78 84 int8_t get_next_available_socket() 79 // private function. This returns the index of the next available socket descriptor or NRK_ERROR if80 // none are available81 85 { 82 86 int8_t i; // loop index … … 88 92 return i; 89 93 } 94 90 95 _nrk_errno_set(NO_SOCKET_DESC_AVAILABLE); 91 92 96 return NRK_ERROR; 93 97 } … … 118 122 } 119 123 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 121 134 default: 122 135 _nrk_errno_set(UNSUPPORTED_SOCK_TYPE); … … 124 137 } 125 138 126 leave_cr(tl_sem, "create_socket()");139 leave_cr(tl_sem, 3); 127 140 return result; 128 141 } 129 142 130 143 /*****************************************************************************************/ 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 144 uint8_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 /******************************************************************************************/ 165 int8_t check_port_available(uint8_t pt) 134 166 { 135 167 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 /********************************************************************************************/ 181 int8_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 /******************************************************************************************/ 228 void 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 /*****************************************************************************************/ 255 int8_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) 145 261 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); 159 264 return NRK_ERROR; 160 265 } 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 /*******************************************************************************************/ 267 int8_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 /*********************************************************************************************/ 177 279 int8_t bind(int8_t sock_num, int8_t port) 178 280 { 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 180 284 int8_t i; // loop index 181 285 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 184 289 enter_cr(tl_sem, 8); 185 290 186 if(tlayer_init_done != 1) 291 // ERROR CHECKING 292 if(tlayer_init_done != 1) // transport layer not initialised 187 293 { 188 294 nrk_int_disable(); … … 192 298 } 193 299 194 // ERROR CHECKING195 300 if(sock_num < 0 || sock_num >= MAX_PORTS) // bad input 196 301 { 197 302 _nrk_errno_set(INVALID_ARGUMENT); 198 303 199 leave_cr(tl_sem, "bind()");200 leave_cr(bm_sem, "bind()");304 leave_cr(tl_sem, 8); 305 leave_cr(bm_sem, 8); 201 306 return NRK_ERROR; 202 307 } … … 206 311 _nrk_errno_set(INVALID_ARGUMENT); 207 312 208 leave_cr(tl_sem, "bind()");209 leave_cr(bm_sem, "bind()");313 leave_cr(tl_sem, 8); 314 leave_cr(bm_sem, 8); 210 315 return NRK_ERROR; 211 316 } … … 215 320 _nrk_errno_set(INVALID_SOCKET); 216 321 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 earlier322 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 223 328 { 224 329 _nrk_errno_set(INVALID_CALL); 225 330 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 taken232 { 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); 235 340 return NRK_ERROR; // error no is already set = PORT_UNAVAILABLE 236 341 } … … 240 345 _nrk_errno_set(NO_RX_BUFFERS_AVAILABLE); 241 346 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 247 353 size = DEFAULT_RX_QUEUE_SIZE; 248 354 // at this point, we know that … … 251 357 // 3. bind() / setReceiveQueueSize() was not called earlier 252 358 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 259 363 { 260 364 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 266 381 // allocate 'size' buffers 267 382 for(i = 1; i <= size; i++) 268 383 { 269 index = get_index_unallocated_rx_buf(); // look for the unallocated receive buffer270 if( index == NRK_ERROR) // this should not happen384 buf_index = get_index_unallocated_rx_buf(); // look for the unallocated receive buffer 385 if(buf_index == NRK_ERROR) // this should not happen 271 386 { 272 387 nrk_int_disable(); … … 276 391 } 277 392 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 280 396 num_bufs_free--; 281 397 } 282 398 283 leave_cr(tl_sem, "bind()");284 leave_cr(bm_sem, "bind()");399 leave_cr(tl_sem, 8); 400 leave_cr(bm_sem, 8); 285 401 return NRK_OK; 286 402 } … … 288 404 int8_t get_rx_queue_size(int8_t sock_num) 289 405 { 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 294 409 enter_cr(tl_sem, 9); 295 410 … … 313 428 } 314 429 315 if(sock[sock_num].port == INVALID_PORT) // no socket operations were performed yet430 if(sock[sock_num].port == NULL) // no socket operations were performed yet 316 431 { 317 432 leave_cr(tl_sem, 9); 318 433 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; 325 447 326 448 leave_cr(tl_sem, 9); … … 332 454 int8_t set_rx_queue_size(int8_t sock_num, int8_t size) 333 455 { 334 int8_t port; // to store the port number associated with given socket number456 uint8_t port; // to store the port number associated with given socket number 335 457 int8_t i; // loop index 336 458 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 340 464 enter_cr(tl_sem, 10); 341 465 … … 368 492 } 369 493 370 if(sock[sock_num].port == INVALID_PORT)// there is no port associated with this socket yet494 if(sock[sock_num].port == NULL) // there is no port associated with this socket yet 371 495 { 372 496 flag = 1; // bind() was not called earlier 373 497 port = get_next_available_port(); // search for a ephemeral port number 374 if(port == NRK_ERROR)// no port available498 if(port == INVALID_PORT) // no port available 375 499 { 376 500 leave_cr(tl_sem, 10); … … 387 511 } 388 512 // 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 393 518 { 394 519 nrk_int_disable(); 395 520 nrk_led_set(RED_LED); 396 521 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) 400 537 else // bind was called earlier. DEFAULT_RX_QUEUE_SIZE buffers already allocated 401 538 { 402 // FIX ME403 539 if(size == DEFAULT_RX_QUEUE_SIZE) // nothing to do, buffers already allocated 404 540 { … … 407 543 return size; 408 544 } 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 412 553 if(get_num_bufs_free() < size) 413 554 size = get_num_bufs_free(); 414 555 415 // at this point, the socket is associated with a valid port number416 // and at least one receive buffer is free417 // fill in the values of the corresponding ReceiveBufferManager418 rx_buf_mgr[port].pid = nrk_get_pid();419 420 556 // allocate 'size' buffers 421 557 for(i = 1; i <= size; i++) 422 558 { 423 index = get_index_unallocated_rx_buf(); // look for the next unallocated receive buffer424 if( index == NRK_ERROR) // this should not happen559 buf_index = get_index_unallocated_rx_buf(); // look for the next unallocated receive buffer 560 if(buf_index == NRK_ERROR) // this should not happen 425 561 { 426 562 nrk_int_disable(); … … 429 565 nrk_kprintf(PSTR("set_rx_queue_size(): Bug found in implementation of num_bufs_free\r\n")); 430 566 } 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++; 433 570 num_bufs_free--; 434 571 } … … 441 578 int8_t release_buffer(int8_t sock_num, uint8_t *ptr) 442 579 { 443 int8_t port; // to store the port number associated with the socket580 int8_t rbm_index; // index of the rbm element associated with this socket 444 581 ReceiveBufferUDP *buf; // pointer to traverse the buffer list of the free queue 445 582 … … 464 601 return NRK_ERROR; 465 602 } 466 if(sock[sock_num].port == INVALID_PORT) // if no socket operation was performed earlier603 if(sock[sock_num].port == NULL) // if no socket operation was performed earlier 467 604 { 468 605 _nrk_errno_set(INVALID_SOCKET); … … 473 610 } 474 611 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 } 476 619 477 620 // 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; 479 622 while(buf != NULL) 480 623 { … … 493 636 494 637 buf -> status = EMPTY; // mark the buffer as available now 495 rx_buf_mgr[port].countFree++;638 (sock[sock_num].rbm -> countFree)++; 496 639 497 640 leave_cr(tl_sem, 11); … … 502 645 int8_t close_socket(int8_t sock_num) 503 646 { 504 int8_t port; // to store the port number assoicated with the given socket number505 647 ReceiveBufferUDP *ptr; // temp variable 506 648 … … 527 669 528 670 // 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 /************************************************************************************************/ 702 int8_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 /************************************************************************************************/ 721 int8_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 534 788 { 535 789 nrk_int_disable(); 536 790 nrk_led_set(RED_LED); 537 791 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")); 539 793 } 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 651 800 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 664 817 // prepare a UDP segment 665 udp_seg.srcPort = port;818 udp_seg.srcPort = sock[sock_num].port -> pno; 666 819 udp_seg.destPort = dest_port; 667 820 udp_seg.length = len; … … 680 833 // insert the packet into the active transmit queue of the system 681 834 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 690 837 if(result == NRK_ERROR) 691 838 _nrk_errno_set(NO_TX_BUFFERS_AVAILABLE); … … 736 883 } 737 884 /*************************************************************************************************/ 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 885 uint8_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 744 890 745 891 enter_cr(bm_sem, 16); … … 756 902 } 757 903 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 ) 759 905 { 760 906 _nrk_errno_set(INVALID_SOCKET); // socket description not complete … … 766 912 767 913 // error checking complete. Begin processing 768 port = sock[sock_num].port;769 914 if(sock[sock_num].timeout.secs == 0 && sock[sock_num].timeout.nano_secs == 0) 770 915 { … … 774 919 775 920 // 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 segments921 if(sock[sock_num].rbm -> countFree == sock[sock_num].rbm -> countTotal) // no queued segments 777 922 { 778 923 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 780 926 while(1) 781 927 { 782 928 leave_cr(tl_sem, 16); 783 929 leave_cr(bm_sem, 16); 784 my_sigs = nrk_event_wait(SIG( rx_buf_mgr[port].seg_arrived_signal)); // block930 my_sigs = nrk_event_wait(SIG(sock[sock_num].port -> data_arrived_signal)); // block 785 931 enter_cr(bm_sem, 16); 786 932 enter_cr(tl_sem, 16); 787 933 788 934 // check what signal we got 789 if(my_sigs == 0) 935 if(my_sigs == 0) // this should not happen 790 936 { 791 937 nrk_int_disable(); … … 794 940 nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait (without timeout)\r\n")); 795 941 } 796 if( my_sigs & SIG( rx_buf_mgr[port].seg_arrived_signal) ) // got a segment942 if( my_sigs & SIG(sock[sock_num].port -> data_arrived_signal) ) // got a segment 797 943 { 798 944 if(DEBUG_TL == 2) 799 nrk_kprintf(PSTR("receive(): Received the segarrived signal\r\n"));945 nrk_kprintf(PSTR("receive(): Received the data_arrived signal\r\n")); 800 946 break; 801 947 } … … 813 959 { 814 960 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")); 816 962 817 963 if( nrk_signal_register(nrk_wakeup_signal) == NRK_ERROR) … … 820 966 nrk_led_set(RED_LED); 821 967 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")); 823 969 } 824 970 if( nrk_set_next_wakeup(sock[sock_num].timeout) == NRK_ERROR) … … 827 973 nrk_led_set(RED_LED); 828 974 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")); 830 976 } 831 977 // 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 segments978 if(sock[sock_num].rbm -> countFree == sock[sock_num].rbm -> countTotal) // no queued segments 833 979 { 834 980 while(1) … … 836 982 leave_cr(tl_sem, 16); 837 983 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)); 839 985 enter_cr(bm_sem, 16); 840 986 enter_cr(tl_sem, 16); … … 848 994 nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait() (with timeout)\r\n")); 849 995 } 850 if( my_sigs & SIG(rx_buf_mgr[port]. seg_arrived_signal) ) // got a segment996 if( my_sigs & SIG(rx_buf_mgr[port].data_arrived_signal) ) // got a segment 851 997 { 852 998 sock[sock_num].timeout.secs = 0; // invalidate the timeout 853 999 sock[sock_num].timeout.nano_secs = 0; 854 1000 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; 858 1003 } 859 1004 … … 862 1007 sock[sock_num].timeout.secs = 0; // invalidate the timeout value of the socket 863 1008 sock[sock_num].timeout.nano_secs = 0; 864 // FIX me change the wakeup period back to what it was865 1009 _nrk_errno_set(SOCKET_TIMEOUT); 866 1010 … … 1080 1224 } 1081 1225 /**********************************************************************************************/ 1226 void 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 /************************************************************************************************/ 1245 int8_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 /***************************************************************************************************/ 1304 void 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 /***************************************************************************************************/ 1323 Port* 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 19 19 #define SOCK_RAW 3 // if the application wants to bypass the transport and network layers 20 20 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 22 23 23 24 // limits on various object types … … 29 30 30 31 /********************************* DATA STRUCTURES ****************************************/ 31 32 32 typedef struct 33 33 { 34 int8_t srcPort; // source port of the application35 int8_t destPort; // destination port of the application34 uint8_t srcPort; // source port of the application 35 uint8_t destPort; // destination port of the application 36 36 int8_t length; // actual length of the data payload 37 37 uint8_t data[MAX_APP_PAYLOAD]; // application layer data … … 41 41 typedef struct 42 42 { 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 49 typedef 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 45 54 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 47 56 }Socket; 48 57 … … 64 73 65 74 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 67 77 ERROR NOS: NO_SOCKET_DESC_AVAILABLE when no descriptors are available 68 78 COMMENTS: private function … … 71 81 int8_t create_socket(int8_t type); 72 82 /* 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 issupported83 This 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 76 86 RETURNS: socket descriptor or NRK_ERROR otherwise 77 87 ERROR NOS: UNSUPPORTED_SOCK_TYPE if an invalid socket type is passed … … 80 90 */ 81 91 82 int8_t get_next_available_port();92 uint8_t get_next_available_port(); 83 93 /* 84 94 This function returns the next available port number if present 85 95 86 96 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 88 99 ERROR NOS: NO_PORTS_AVAILABLE if no ephemeral port numbers are available 89 100 COMMENTS: private function 90 101 */ 91 102 92 int8_t check_port_available( int8_t pt);103 int8_t check_port_available(uint8_t pt); 93 104 /* 94 105 This function checks whether port 'pt' is available … … 100 111 */ 101 112 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 113 int8_t assign_port(Port *p, uint8_t pt); 114 /* 115 This functions records the fact that port 'pt' is allocated. It also creates and registers 116 the 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 128 void release_port(Port *p); 129 /* 130 This functions records the fact that a given port number is now available 131 132 PARAMS: p: Pointer to the port information 107 133 RETURNS: None 108 134 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 137 int8_t get_index_unassigned_port_element() 138 /* 139 This 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 148 int8_t get_index_unassigned_rbm_element() 149 /* 150 This 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 119 157 */ 120 158 … … 138 176 */ 139 177 140 int8_t get_rx_queue_size(int 8_t sock_num);178 int8_t get_rx_queue_size(int16_t sock_num); 141 179 /* 142 180 This function returns the size of the rx queue associated with a given socket descriptor … … 211 249 */ 212 250 213 int8_t is_port_associated(int 8_t port);251 int8_t is_port_associated(int16_t port); 214 252 /* 215 253 This function checks whether a given port number has been assigned to a socket or not 216 254 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 219 260 COMMENTS: User API 220 261 */ 221 262 222 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int 16_t dest_addr, int8_t dest_port, int8_t prio);263 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int8_t dest_port, int8_t prio); 223 264 /* 224 265 This function can be used by the application task to send data to other nodes on the network … … 266 307 */ 267 308 268 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr,int8_t *srcPort, int8_t *rssi);309 uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi); 269 310 /* 270 311 This function allows an application to receive a message along with network control information -
nano-RK/projects/network_stack/makefile
r110 r116 1 1 # Platform name cc2420DK, firefly, micaZ 2 PLATFORM = firefly2 _22 PLATFORM = firefly2 3 3 4 4 # Target file name (without extension).
Note: See TracChangeset
for help on using the changeset viewer.
