Changeset 119
- Timestamp:
- 06/28/2007 07:11:40 PM (5 years ago)
- Location:
- nano-RK/projects/network_stack
- Files:
-
- 7 edited
-
App.c (modified) (3 diffs)
-
BufferManager.c (modified) (22 diffs)
-
BufferManager.h (modified) (7 diffs)
-
NWErrorCodes.h (modified) (1 diff)
-
NetworkLayer.c (modified) (9 diffs)
-
TransportLayerUDP.c (modified) (55 diffs)
-
TransportLayerUDP.h (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
nano-RK/projects/network_stack/App.c
r118 r119 45 45 uint8_t *rx_buf; 46 46 int8_t len; 47 int16_t srcAddr = 10;48 int8_t srcPort;47 uint16_t srcAddr = 10; 48 uint8_t srcPort; 49 49 int8_t rssi; 50 50 int8_t i; … … 118 118 119 119 if(NODE_ADDR == 1) 120 { 120 121 if( send(sock, tx_buf, strlen(tx_buf), 2, SERVER2_PORT, NORMAL_PRIORITY) == NRK_ERROR ) 121 122 { … … 123 124 flag = 1; 124 125 } 126 } 125 127 else 126 128 if( send(sock, tx_buf, strlen(tx_buf), 1, SERVER1_PORT, NORMAL_PRIORITY) == NRK_ERROR ) -
nano-RK/projects/network_stack/BufferManager.c
r118 r119 14 14 #include "Debug.h" 15 15 16 /***************************** External variables/ functions ************************/ 17 // From TransportLayerUDP.c 18 extern Port ports[]; 19 16 20 /**************************** Global buffers required by all tasks ******************/ 17 21 … … 21 25 TransmitBuffer tx_buf[MAX_TX_QUEUE_SIZE]; // transmit queue for the entire system 22 26 23 ReceiveBufferManager rx_buf_mgr[MAX_PORTS + 1];// create one receive manager per port27 ReceiveBufferManager rx_buf_mgr[MAX_PORTS]; // create one receive manager per port 24 28 // port 0 is reserved 25 29 TransmitBufferManager tx_buf_mgr; // one transmit manager for the whole system … … 28 32 // excess policy settings 29 33 30 SendDoneSignal sd_sig[MAX_PORTS + 1]; // create one send_done signal per port31 32 34 nrk_sem_t *bm_sem; // semaphore to access the above variables 33 35 … … 488 490 { 489 491 rx_buf_mgr[i].pid = INVALID_PID; // pid of task associated with the port 492 rx_buf_mgr[i].pindex = -1; // indicates absence of rbm/port mapping 490 493 rx_buf_mgr[i].head_fq = NULL; 491 494 rx_buf_mgr[i].tail_fq = NULL; … … 494 497 rx_buf_mgr[i].countTotal = 0; 495 498 rx_buf_mgr[i].countFree = 0; 496 rx_buf_mgr[i].seg_arrived_signal = nrk_signal_create();497 sd_sig[i].send_done_signal = nrk_signal_create();498 499 } 499 500 … … 536 537 return TRUE; 537 538 } 538 return FALSE; 539 return FALSE; 539 540 } 540 541 /*******************************************************************************************/ … … 591 592 } 592 593 /**************************************************************************************/ 594 int8_t port_to_rbm_index(uint8_t port) 595 { 596 int8_t i; // loop index 597 598 for(i = 0; i < MAX_PORTS; i++) 599 if(rx_buf_mgr[i].pindex != -1) // valid rbm element 600 if(ports[rx_buf_mgr[i].pindex].pno == port) // found the matching rbm element 601 return i; 602 603 // if the code reaches here, it means no match was found 604 return NRK_ERROR; 605 } 606 /**************************************************************************************/ 607 int8_t port_to_port_index(uint8_t port) 608 { 609 int8_t i; // loop index 610 611 for(i = 0; i < MAX_PORTS; i++) 612 if(ports[i].pno == port) // match found 613 return i; 614 615 // if the code reaches here, it means no match was found 616 return NRK_ERROR; 617 } 618 /**************************************************************************************/ 593 619 void insert_rx_pq(Transport_Segment_UDP *seg, int8_t prio, uint16_t addr, int8_t rssi) 594 620 { 595 int8_t port; // destination port of the segment 596 597 //enter_cr(bm_sem, "insert_rx_pq()"); 598 599 port = seg -> destPort; 600 ReceiveBufferUDP *buf = remove_rx_fq(port, EMPTY); // remove an EMPTY buffer from the free queue 621 int8_t rbm_index; // index of the corresponding rbm element 622 623 rbm_index = port_to_rbm_index(seg -> destPort); 624 ReceiveBufferUDP *buf = remove_rx_fq(rbm_index, EMPTY); // remove an EMPTY buffer from the free queue 601 625 602 626 // pointers for traversing the port queue 603 ReceiveBufferUDP *ptr = rx_buf_mgr[ port].head_pq;627 ReceiveBufferUDP *ptr = rx_buf_mgr[rbm_index].head_pq; 604 628 ReceiveBufferUDP *prev = NULL; 605 629 … … 625 649 626 650 if(prev == NULL) // the newly received segment is to be inserted at the head of the port queue 627 rx_buf_mgr[ port].head_pq = buf;651 rx_buf_mgr[rbm_index].head_pq = buf; 628 652 else 629 653 prev -> next = buf; … … 641 665 642 666 // check for 2 643 if(rx_buf_mgr[ port].head_pq == NULL)644 { 645 if(rx_buf_mgr[ port].countFree != 0) // debugging purposes667 if(rx_buf_mgr[rbm_index].head_pq == NULL) 668 { 669 if(rx_buf_mgr[rbm_index].countFree != 0) // debugging purposes 646 670 { 647 671 nrk_int_disable(); … … 658 682 // in the port queue has to be replaced 659 683 // check if the last segment in the port queue has a lower priority 660 if( (rx_buf_mgr[ port].tail_pq) -> prio < prio ) // this last segment should be replaced684 if( (rx_buf_mgr[rbm_index].tail_pq) -> prio < prio ) // this last segment should be replaced 661 685 { 662 686 // at this point we know that ptr cannot be NULL 663 687 // find the last but one segment in the port queue 664 688 665 ReceiveBufferUDP *qtr = rx_buf_mgr[ port].head_pq;689 ReceiveBufferUDP *qtr = rx_buf_mgr[rbm_index].head_pq; 666 690 // corner case, see if there is only one buffer in the port queue 667 if(rx_buf_mgr[ port].head_pq == rx_buf_mgr[port].tail_pq)691 if(rx_buf_mgr[rbm_index].head_pq == rx_buf_mgr[rbm_index].tail_pq) 668 692 { 669 693 // replace the members of this last buffer with those of the new one … … 682 706 683 707 // replace the members of the last buffer with those of the new one 684 (rx_buf_mgr[ port].tail_pq) -> seg = *seg;685 (rx_buf_mgr[ port].tail_pq) -> prio = prio;686 (rx_buf_mgr[ port].tail_pq) -> srcAddr = addr;687 (rx_buf_mgr[ port].tail_pq) -> rssi = rssi;708 (rx_buf_mgr[rbm_index].tail_pq) -> seg = *seg; 709 (rx_buf_mgr[rbm_index].tail_pq) -> prio = prio; 710 (rx_buf_mgr[rbm_index].tail_pq) -> srcAddr = addr; 711 (rx_buf_mgr[rbm_index].tail_pq) -> rssi = rssi; 688 712 689 713 // adjust the pointers so that this new segment occupies its correct position in the port queue 690 (rx_buf_mgr[ port].tail_pq) -> next = ptr;714 (rx_buf_mgr[rbm_index].tail_pq) -> next = ptr; 691 715 if(prev == NULL) 692 rx_buf_mgr[ port].head_pq = rx_buf_mgr[port].tail_pq;716 rx_buf_mgr[rbm_index].head_pq = rx_buf_mgr[rbm_index].tail_pq; 693 717 else 694 prev -> next = rx_buf_mgr[ port].tail_pq;718 prev -> next = rx_buf_mgr[rbm_index].tail_pq; 695 719 696 720 qtr -> next = NULL; 697 rx_buf_mgr[ port].tail_pq = qtr; // change the tail of the port queue721 rx_buf_mgr[rbm_index].tail_pq = qtr; // change the tail of the port queue 698 722 699 723 //leave_cr(bm_sem, "insert_rx_pq()"); … … 701 725 } 702 726 } // end if 703 else if( (rx_buf_mgr[ port].tail_pq) -> prio == prio )727 else if( (rx_buf_mgr[rbm_index].tail_pq) -> prio == prio ) 704 728 { 705 729 // check the setting of the excess policy for this priority level … … 707 731 { 708 732 // replace the members of the last buffer with those of the new one 709 (rx_buf_mgr[ port].tail_pq) -> seg = *seg;710 (rx_buf_mgr[ port].tail_pq) -> prio = prio;711 (rx_buf_mgr[ port].tail_pq) -> srcAddr = addr;712 (rx_buf_mgr[ port].tail_pq) -> rssi = rssi;733 (rx_buf_mgr[rbm_index].tail_pq) -> seg = *seg; 734 (rx_buf_mgr[rbm_index].tail_pq) -> prio = prio; 735 (rx_buf_mgr[rbm_index].tail_pq) -> srcAddr = addr; 736 (rx_buf_mgr[rbm_index].tail_pq) -> rssi = rssi; 713 737 714 738 //leave_cr(bm_sem, "insert_rx_pq()"); … … 732 756 } 733 757 /*************************************************************************************************/ 734 ReceiveBufferUDP* remove_rx_pq(int8_t port)758 ReceiveBufferUDP* remove_rx_pq(int8_t rbm_index) 735 759 { 736 760 // The remove is always done from the head of the queue since its a priority queue … … 739 763 //enter_cr(bm_sem, "remove_rx_pq()"); 740 764 741 if(rx_buf_mgr[ port].head_pq == NULL) // no more queued segments765 if(rx_buf_mgr[rbm_index].head_pq == NULL) // no more queued segments 742 766 { 743 767 //leave_cr(bm_sem, "remove_rx_pq()"); … … 745 769 } 746 770 747 if(rx_buf_mgr[ port].head_pq == rx_buf_mgr[port].tail_pq) // only one buffer in queue748 rx_buf_mgr[ port].tail_pq = NULL;749 750 ptr = rx_buf_mgr[ port].head_pq;751 752 rx_buf_mgr[ port].head_pq = rx_buf_mgr[port].head_pq -> next;771 if(rx_buf_mgr[rbm_index].head_pq == rx_buf_mgr[rbm_index].tail_pq) // only one buffer in queue 772 rx_buf_mgr[rbm_index].tail_pq = NULL; 773 774 ptr = rx_buf_mgr[rbm_index].head_pq; 775 776 rx_buf_mgr[rbm_index].head_pq = rx_buf_mgr[rbm_index].head_pq -> next; 753 777 754 778 //leave_cr(bm_sem, "remove_rx_pq()"); … … 757 781 /*************************************************************************************************/ 758 782 759 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t port, int8_t status) 760 { 761 //enter_cr(bm_sem, "insert_rx_fq()"); 762 783 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t rbm_index, int8_t status) 784 { 763 785 // 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;786 if(rx_buf_mgr[rbm_index].head_fq == NULL) // the free queue is empty 787 { 788 rx_buf_mgr[rbm_index].head_fq = rx_buf_mgr[rbm_index].tail_fq = buf; 767 789 buf -> next = NULL; 768 790 } 769 791 else 770 792 { 771 rx_buf_mgr[ port].tail_fq -> next = buf;793 rx_buf_mgr[rbm_index].tail_fq -> next = buf; 772 794 buf -> next = NULL; 773 rx_buf_mgr[ port].tail_fq = buf;795 rx_buf_mgr[rbm_index].tail_fq = buf; 774 796 } 775 797 776 798 if(status == EMPTY) // this function was called from bind() / setReceiveQueueSize() 777 799 { 778 rx_buf_mgr[ port].countFree++;800 rx_buf_mgr[rbm_index].countFree++; 779 801 buf -> status = EMPTY; 780 802 } 781 803 782 //leave_cr(bm_sem, "insert_rx_fq()");783 804 return; 784 805 } 785 806 786 807 /***************************************************************************************************/ 787 ReceiveBufferUDP* remove_rx_fq( uint8_t port, int8_t status)808 ReceiveBufferUDP* remove_rx_fq(int8_t rbm_index, int8_t status) 788 809 { 789 810 // This function removes the next receive buffer from the free queue having a given status … … 791 812 ReceiveBufferUDP *prev = NULL; 792 813 793 if(rx_buf_mgr[ port].head_fq == NULL) // no buffers remaining in free queue814 if(rx_buf_mgr[rbm_index].head_fq == NULL) // no buffers remaining in free queue 794 815 return NULL; 795 816 796 ptr = rx_buf_mgr[ port].head_fq;817 ptr = rx_buf_mgr[rbm_index].head_fq; 797 818 while(ptr != NULL) 798 819 { … … 812 833 if(prev == NULL) // the EMPTY / FULL buffer is at the head of the queue 813 834 { 814 if(rx_buf_mgr[ port].head_fq == rx_buf_mgr[port].tail_fq) // only one buffer in free queue815 rx_buf_mgr[ port].tail_fq = NULL;816 817 rx_buf_mgr[ port].head_fq = rx_buf_mgr[port].head_fq -> next;835 if(rx_buf_mgr[rbm_index].head_fq == rx_buf_mgr[rbm_index].tail_fq) // only one buffer in free queue 836 rx_buf_mgr[rbm_index].tail_fq = NULL; 837 838 rx_buf_mgr[rbm_index].head_fq = rx_buf_mgr[rbm_index].head_fq -> next; 818 839 if(status == EMPTY) 819 rx_buf_mgr[ port].countFree--;840 rx_buf_mgr[rbm_index].countFree--; 820 841 return ptr; 821 842 } … … 824 845 prev -> next = ptr -> next; 825 846 if(status == EMPTY) 826 rx_buf_mgr[ port].countFree--;847 rx_buf_mgr[rbm_index].countFree--; 827 848 return ptr; 828 849 } … … 940 961 /**************************************************************************************************/ 941 962 942 int8_t get_in_process_buf_count(int8_t port)963 int8_t get_in_process_buf_count(int8_t rbm_index) 943 964 { 944 965 int8_t count = 0; … … 946 967 //enter_cr(bm_sem, "get_in_process_buf_count()"); 947 968 948 ReceiveBufferUDP *ptr = rx_buf_mgr[ port].head_fq;969 ReceiveBufferUDP *ptr = rx_buf_mgr[rbm_index].head_fq; 949 970 950 971 while(ptr != NULL) -
nano-RK/projects/network_stack/BufferManager.h
r118 r119 59 59 typedef struct 60 60 { 61 int8_t pid; // pid of task associated with the port 61 int8_t pid; // pid of task associated with the port 62 int8_t pindex; // index of associated port element 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 inserted69 // into the port queue70 69 }ReceiveBufferManager; // one for every port in the system 71 70 … … 154 153 */ 155 154 155 int8_t port_to_rbm_index(uint8_t port); 156 /* 157 This function finds the index of the rbm element corresponding to a given port number 158 159 PARAMS: port: The port number 160 RETURNS: the index of the rbm element if present or NRK_ERROR if no match is found 161 Comments: private function 162 */ 163 156 164 void insert_rx_pq(Transport_Segment_UDP *seg, int8_t prio, uint16_t addr, int8_t rssi); 157 165 /* … … 168 176 */ 169 177 170 ReceiveBufferUDP* remove_rx_pq(int8_t port);178 ReceiveBufferUDP* remove_rx_pq(int8_t rbm_index); 171 179 /* 172 180 This function removes the receive buffer at the head of a given port queue 173 181 174 PARAMS: port: The port182 PARAMS: rbm_index: the index of the relevant receive buffer manager 175 183 RETURNS: pointer to the receive buffer or NULL if the port queue is empty 176 184 COMMENTS: private function 177 185 */ 178 186 179 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t port, int8_t status);187 void insert_rx_fq(ReceiveBufferUDP *buf, int8_t rbm_index, int8_t status); 180 188 /* 181 189 This function inserts a given receive buffer into the free queue of a given port … … 183 191 184 192 PARAMS: buf: pointer to the receive buffer 185 port: the port number associated with the free queue193 rbm_index: the index of the relevant receive buffer manager 186 194 status: EMPTY or FULL 187 195 … … 190 198 */ 191 199 192 ReceiveBufferUDP* remove_rx_fq( uint8_t port, int8_t status);200 ReceiveBufferUDP* remove_rx_fq(int8_t rbm_index, int8_t status); 193 201 /* 194 202 This function removes a receive buffer from the free queue with the given status 195 203 196 PARAMS: port: the port number associated with the free queue204 PARAMS: rbm_index: the index of the relevant receive buffer manager 197 205 status: EMPTY or FULL 198 206 … … 242 250 */ 243 251 244 int8_t get_in_process_buf_count(int8_t port);252 int8_t get_in_process_buf_count(int8_t rbm_index); 245 253 /* 246 254 This function returns the number of FULL buffers in the free queue associated with a given port 247 255 248 PARAMS: port: The port number256 PARAMS: rbm_index: the index of the relevant receive buffer manager 249 257 RETURNS: number of FULL buffers in the free queue 250 258 COMMENTS: private function -
nano-RK/projects/network_stack/NWErrorCodes.h
r118 r119 19 19 #define NO_TX_BUFFERS_AVAILABLE 12 20 20 #define SOCKET_TIMEOUT 13 21 #define UNMAPPED_SOCKET 14 22 #define NO_PORT_ELEMENT_AVAILABLE 15 23 #define NO_RBM_ELEMENT_AVAILABLE 16 21 24 22 25 #endif -
nano-RK/projects/network_stack/NetworkLayer.c
r118 r119 23 23 // From TransportLayerUDP.c 24 24 extern nrk_sem_t *tl_sem; 25 26 extern int8_t is_port_associated(int8_t); 25 extern Port ports[]; 26 27 extern int8_t is_port_associated(int16_t port); 27 28 28 29 //From BufferManager.c 29 30 extern nrk_sem_t *bm_sem; 30 extern SendDoneSignal sd_sig[];31 31 extern ReceiveBufferManager rx_buf_mgr[]; 32 32 33 extern void insert_rx_queue(Transport_Segment_UDP *, uint8_t, uint8_t, int8_t);34 33 extern void insert_rx_pq(Transport_Segment_UDP*, int8_t, uint16_t, int8_t); 35 34 extern TransmitBuffer* remove_tx_aq(); 36 35 extern void insert_tx_fq(TransmitBuffer*); 37 extern void enter_cr(nrk_sem_t *, int8_t *); 38 extern void leave_cr(nrk_sem_t *, int8_t *); 36 extern void enter_cr(nrk_sem_t *, int8_t); 37 extern void leave_cr(nrk_sem_t *, int8_t); 38 extern int8_t port_to_port_index(uint8_t); 39 39 40 40 … … 44 44 extern void pack_NodeToGatewaySerial_Packet_header(uint8_t *, NodeToGatewaySerial_Packet *); 45 45 extern void pack_TL_UDP_header(uint8_t *, Transport_Segment_UDP*); 46 extern void pack_NW_Packet_header(uint8_t *,NW_Packet*); 46 47 47 48 extern void unpack_Msg_NgbList(Msg_NgbList*, uint8_t *); … … 267 268 // check to see if the destination port in the header is associated with any socket 268 269 enter_cr(bm_sem, 28); 270 enter_cr(tl_sem, 28); 271 269 272 if(is_port_associated(udp_seg.destPort) == TRUE) // yes there is 270 273 { 274 int8_t port_index; 275 271 276 insert_rx_pq(&udp_seg, pkt -> prio, pkt -> src, rssi); 272 nrk_event_signal(rx_buf_mgr[udp_seg.destPort].seg_arrived_signal); // signal 'seg arrived' 277 278 port_index = port_to_port_index(udp_seg.destPort); 279 if(port_index == NRK_ERROR) //sanity check for debugging 280 { 281 nrk_int_disable(); 282 nrk_led_set(RED_LED); 283 while(1) 284 nrk_kprintf(PSTR("process_app_pkt(): Bug detected in implementation of port/rbm element array\r\n")); 285 } 286 nrk_event_signal(ports[port_index].data_arrived_signal); // signal 'data arrived' 273 287 } 274 288 275 289 else // if there is no socket associated with this port, simply drop the packet 276 290 ; 277 291 leave_cr(tl_sem, 28); 278 292 leave_cr(bm_sem, 28); 279 293 } … … 381 395 pkt_tx.prio = NORMAL_PRIORITY; 382 396 383 pack_Msg_ NgbList(pkt_tx.data, m);397 pack_Msg_Hello(pkt_tx.data, m); 384 398 return; 385 399 } … … 514 528 void nl_tx_task() 515 529 { 516 TransmitBuffer *ptr; // pointer to the buffer to be transmitted 517 Transport_Segment_UDP *seg; // temporary variable to retrieve the source port of the message 518 nrk_sig_t tx_done_signal; // to hold the tx_done signal from the link layer 519 int8_t ret; // to hold the return value of various functions 530 TransmitBuffer *ptr = NULL; // pointer to the buffer to be transmitted 531 Transport_Segment_UDP *seg = NULL; // temporary variable to retrieve the source port of the message 532 nrk_sig_t tx_done_signal; // to hold the tx_done signal from the link layer 533 int8_t ret; // to hold the return value of various functions 534 int8_t port_index; // to store the index of the corresponding port element 520 535 521 536 //static Msg_NgbList mnlist; … … 548 563 } 549 564 550 if( pkt_type( (ptr -> pkt).type) == APPLICATION )565 if( pkt_type(&(ptr -> pkt)) == APPLICATION ) 551 566 { 552 567 // remove the encapsulated TL segment from the packet … … 555 570 556 571 // pack the network packet header into the transmit buffer 557 pack_NW_Packet_header(tx_buf, ptr -> pkt);572 pack_NW_Packet_header(tx_buf, &(ptr -> pkt)); 558 573 // append the application payload into the transmit buffer 559 574 memcpy(tx_buf + SIZE_NW_PACKET_HEADER, (ptr -> pkt).data, (ptr -> pkt).length); … … 570 585 571 586 ret = nrk_event_wait (SIG(tx_done_signal)); 572 if( ret & SIG(tx_done_signal) == 0 )587 if((ret & SIG(tx_done_signal)) == 0 ) 573 588 { 574 589 nrk_int_disable(); … … 579 594 // signal 'send done' signal 580 595 enter_cr(bm_sem, 34); 581 nrk_event_signal(sd_sig[seg -> srcPort].send_done_signal); 596 enter_cr(tl_sem, 34); 597 port_index = port_to_port_index(seg -> srcPort); 598 nrk_event_signal(ports[port_index].send_done_signal); 599 leave_cr(tl_sem, 34); 582 600 leave_cr(bm_sem, 34); 583 601 } -
nano-RK/projects/network_stack/TransportLayerUDP.c
r118 r119 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 bit vector representing the available ports 11 Socket sock[MAX_PORTS]; // array to store available socket descriptors 0 - (MAX_PORTS - 1) 12 13 Port ports[MAX_PORTS]; // array to store available port numbers (1 - MAX_PORT_NUM) 15 14 16 15 int8_t tlayer_init_done; // flag to indicate whether initialization has been done correctly … … 33 32 34 33 extern void enter_cr(nrk_sem_t *, int8_t); 35 extern void leave_cr(nrk_sem_t *, int8_t *);34 extern void leave_cr(nrk_sem_t *, int8_t); 36 35 extern int8_t get_num_bufs_free(); 37 36 extern void insert_rx_fq(ReceiveBufferUDP*, int8_t, int8_t); … … 50 49 { 51 50 int8_t i; // loop index 52 // make all the socket descriptors unassigned 51 52 // make the socket descriptors and ports unassigned 53 53 for(i = 0; i < MAX_PORTS; i++) 54 54 { 55 sock[i].pid = INVALID_PID; 56 sock[i].port = INVALID_PORT; 57 sock[i].timeout.secs = 0; // indicates no timeout 58 sock[i].timeout.nano_secs = 0; 59 } 60 // make all the ports available 61 ports = 0; 62 63 tl_sem = nrk_sem_create(1,MAX_TASK_PRIORITY); // create the mutex 55 sock[i].pindex = -1; // indicates the absence of socket-port mapping 56 sock[i].rbmindex = -1; // indicates the absence of socket-port mapping 57 sock[i].pid = INVALID_PID; // indicates an unassigned socket descriptor 58 sock[i].timeout.secs = 0; // indicates no timeout 59 sock[i].timeout.nano_secs = 0; // indicates no timeout 60 61 ports[i].pno = INVALID_PORT; // indicates an unassigned port number 62 } 63 64 // create the mutex with priority ceiling equal to MAX_TASK_PRIORITY 65 tl_sem = nrk_sem_create(1,MAX_TASK_PRIORITY); 64 66 if(tl_sem == NULL) 65 67 { … … 75 77 /**************************************************************************************************/ 76 78 int8_t get_next_available_socket() 77 // private function. This returns the index of the next available socket descriptor or NRK_ERROR if78 // none are available79 79 { 80 80 int8_t i; // loop index … … 122 122 } 123 123 124 leave_cr(tl_sem, "create_socket()");124 leave_cr(tl_sem, 3); 125 125 return result; 126 126 } 127 127 128 128 /*****************************************************************************************/ 129 int8_t get_next_available_port() 130 // This private function gets the next available ephemeral port number if present or NRK_ERROR 131 // if all are taken 132 { 133 int8_t i; // loop index 134 if(DEBUG_TL == 2) 135 printf("Inside get_next_available_ports(). Value of ports = %d\r\n", ports); 136 137 //for(i = EPHEMERAL_PORT_NUM_START; i <= MAX_PORTS; i++) // check only for ephemeral port numbers 138 for(i = 3; i <= MAX_PORTS; i++) 139 { 140 if(DEBUG_TL == 2) 141 printf("Inside get_next_available_ports(). i = %d\r\n", i); 142 if( ((ports >> i) & ((uint32_t)1)) == 0 ) // bit 'i' is 0 143 return i; 144 } 129 uint8_t get_next_available_port() 130 { 131 uint16_t i; // loop index for ephemeral port numbers 132 int8_t j; // loop index 133 134 for(i = EPHEMERAL_PORT_NUM_START; i <= MAX_PORT_NUM; i++) 135 { 136 // check to see if port 'i' is available 137 for(j = 0; j < MAX_PORTS; j++) 138 if(ports[j].pno == i) // port 'i' has been taken 139 break; 140 141 if(j == MAX_PORTS) // port 'i' is free 142 return i; // return this ephemeral port number back 143 } 144 145 // if the code reaches there, it means that all ephemeral port numbers have been taken. 146 // This also means that the 'ports' array is full 147 145 148 _nrk_errno_set(NO_PORTS_AVAILABLE); 146 return NRK_ERROR;149 return INVALID_PORT; 147 150 } 148 151 /******************************************************************************************/ 149 int8_t check_port_available(int8_t pt) 150 // This private function checks to see if port 'pt' is available and returns NRK_OK or NRK_ERROR 151 // accordingly 152 { 153 if( ((ports >> pt) & ((uint32_t)1)) == 0 ) // bit 'pt' is 0 152 int8_t check_port_available(uint8_t pt) 153 { 154 int8_t i; // loop index 155 156 for(i = 0; i < MAX_PORTS; i++) 157 if(ports[i].pno == pt) // port 'pt' has been taken 158 break; 159 160 if(i == MAX_PORTS) // port 'pt' is available 154 161 return NRK_OK; 155 162 156 163 _nrk_errno_set(PORT_UNAVAILABLE); 157 164 return NRK_ERROR; 158 165 } 159 166 /*****************************************************************************************/ 160 void assign_port(int8_t pt) 161 // This private function records the fact that port 'pt' has been assigned 162 { 163 ports |= (uint32_t)1 << pt; // set bit 'pt' in 'ports' 167 void assign_port(int8_t pindex, uint8_t pt) 168 { 169 int8_t ret1, ret2; 170 171 // fill up the members of the port element 172 ports[pindex].pno = pt; 173 ports[pindex].send_done_signal = nrk_signal_create(); 174 ports[pindex].data_arrived_signal = nrk_signal_create(); 175 176 if( ports[pindex].send_done_signal == NRK_ERROR ) 177 { 178 nrk_int_disable(); 179 nrk_led_set(RED_LED); 180 while(1) 181 nrk_kprintf(PSTR("assign_port(): Error creating the send_done signal\r\n")); 182 } 183 184 if( ports[pindex].data_arrived_signal == NRK_ERROR ) 185 { 186 nrk_int_disable(); 187 nrk_led_set(RED_LED); 188 while(1) 189 nrk_kprintf(PSTR("assign_port(): Error creating the data_arrived signal\r\n")); 190 } 191 192 ret1 = nrk_signal_register(ports[pindex].send_done_signal); 193 ret2 = nrk_signal_register(ports[pindex].data_arrived_signal); 194 195 if(ret1 == NRK_ERROR) 196 { 197 nrk_int_disable(); 198 nrk_led_set(RED_LED); 199 while(1) 200 nrk_kprintf(PSTR("assign_port(): Error registering the send_done signal\r\n")); 201 } 202 203 if(ret2 == NRK_ERROR) 204 { 205 nrk_int_disable(); 206 nrk_led_set(RED_LED); 207 while(1) 208 nrk_kprintf(PSTR("assign_port(): Error registering the data_arrived signal\r\n")); 209 } 210 164 211 return; 165 212 } 166 213 /******************************************************************************************/ 167 void release_port(int8_t pt) 168 // This private function records the fact that port 'pt' is now released 169 { 170 ports &= ~( (uint32_t)1 << pt ); // clear bit 'pt' in 'ports' 214 void release_port(int8_t pindex) 215 { 216 int8_t ret1, ret2; 217 218 // invalidate members of the port element 219 ports[pindex].pno = INVALID_PID; 220 ret1 = nrk_signal_delete(ports[pindex].send_done_signal); 221 ret2 = nrk_signal_delete(ports[pindex].data_arrived_signal); 222 223 if(ret1 == NRK_ERROR) 224 { 225 nrk_int_disable(); 226 nrk_led_set(RED_LED); 227 while(1) 228 nrk_kprintf(PSTR("assign_port(): Error creating the send_done signal\r\n")); 229 } 230 231 if(ret2 == NRK_ERROR) 232 { 233 nrk_int_disable(); 234 nrk_led_set(RED_LED); 235 while(1) 236 nrk_kprintf(PSTR("assign_port(): Error creating the data_arrived signal\r\n")); 237 } 238 171 239 return; 172 240 } 173 174 241 /*****************************************************************************************/ 175 int8_t bind(int8_t sock_num, int8_t port) 176 { 177 int8_t index; // stores the index of next available receive buffer 242 int8_t get_index_unassigned_port_element() 243 { 244 int8_t i; 245 246 for(i = 0; i < MAX_PORTS; i++) 247 if(ports[i].pno == INVALID_PORT) 248 return i; 249 250 _nrk_errno_set(NO_PORT_ELEMENT_AVAILABLE); 251 return NRK_ERROR; 252 } 253 /*******************************************************************************************/ 254 int8_t get_index_unassigned_rbm_element() 255 { 256 int8_t i; 257 258 for(i = 0; i < MAX_PORTS; i++) 259 if(rx_buf_mgr[i].pindex == -1) 260 return i; 261 262 _nrk_errno_set(NO_RBM_ELEMENT_AVAILABLE); 263 return NRK_ERROR; 264 } 265 /*****************************************************************************************/ 266 uint8_t get_port_num(int8_t sock_num) 267 { 268 // ERROR checking 269 if(sock_num < 0 || sock_num > MAX_PORTS) 270 { 271 _nrk_errno_set(INVALID_ARGUMENT); 272 return INVALID_PORT; 273 } 274 275 if(sock[sock_num].pid != nrk_get_pid()) 276 { 277 _nrk_errno_set(INVALID_SOCKET); 278 return INVALID_PORT; 279 } 280 281 if(sock[sock_num].pindex == -1) 282 { 283 _nrk_errno_set(UNMAPPED_SOCKET); 284 return INVALID_PORT; 285 } 286 287 return ports[sock[sock_num].pindex].pno; 288 } 289 290 /*****************************************************************************************/ 291 int8_t bind(int8_t sock_num, int16_t port) 292 { 293 int8_t buf_index; // stores the index of next available receive buffer 294 int8_t port_index; // stores the index of the next unassigned port element 295 int8_t rbm_index; // stores the index of the next unassigned rbm element 178 296 int8_t i; // loop index 179 297 int8_t size; // size of receive queue allocated for this port … … 195 313 _nrk_errno_set(INVALID_ARGUMENT); 196 314 197 leave_cr(tl_sem, "bind()");198 leave_cr(bm_sem, "bind()");199 return NRK_ERROR; 200 } 201 202 if(port <= 0 || port > MAX_PORT S) // bad input203 { 204 _nrk_errno_set(INVALID_ARGUMENT); 205 206 leave_cr(tl_sem, "bind()");207 leave_cr(bm_sem, "bind()");315 leave_cr(tl_sem, 8); 316 leave_cr(bm_sem, 8); 317 return NRK_ERROR; 318 } 319 320 if(port <= 0 || port > MAX_PORT_NUM) // bad input 321 { 322 _nrk_errno_set(INVALID_ARGUMENT); 323 324 leave_cr(tl_sem, 8); 325 leave_cr(bm_sem, 8); 208 326 return NRK_ERROR; 209 327 } … … 213 331 _nrk_errno_set(INVALID_SOCKET); 214 332 215 leave_cr(tl_sem, "bind()");216 leave_cr(bm_sem, "bind()");217 return NRK_ERROR; 218 } 219 220 if( rx_buf_mgr[port].pid == nrk_get_pid())// bind() / set_rx_queue_size() was called earlier333 leave_cr(tl_sem, 8); 334 leave_cr(bm_sem, 8); 335 return NRK_ERROR; 336 } 337 338 if(sock[sock_num].rbmindex != -1) // bind() / set_rx_queue_size() was called earlier 221 339 { 222 340 _nrk_errno_set(INVALID_CALL); 223 341 224 leave_cr(tl_sem, "bind()");225 leave_cr(bm_sem, "bind()");226 return NRK_ERROR; 227 } 228 229 if(check_port_available( port) == NRK_ERROR)// check to see if port is already taken230 { 231 leave_cr(tl_sem, "bind()");232 leave_cr(bm_sem, "bind()");342 leave_cr(tl_sem, 8); 343 leave_cr(bm_sem, 8); 344 return NRK_ERROR; 345 } 346 347 if(check_port_available((uint8_t)port) == FALSE) // check to see if port is already taken 348 { 349 leave_cr(tl_sem, 8); 350 leave_cr(bm_sem, 8); 233 351 return NRK_ERROR; // error no is already set = PORT_UNAVAILABLE 234 352 } … … 238 356 _nrk_errno_set(NO_RX_BUFFERS_AVAILABLE); 239 357 240 leave_cr(tl_sem, "bind()"); 241 leave_cr(bm_sem, "bind()"); 242 return NRK_ERROR; 243 } 244 358 leave_cr(tl_sem, 8); 359 leave_cr(bm_sem, 8); 360 return NRK_ERROR; 361 } 362 363 if(sock[sock_num].pindex != -1) // sanity check for debugging 364 { 365 nrk_int_disable(); 366 nrk_led_set(RED_LED); 367 while(1) 368 nrk_kprintf(PSTR("bind(): Bug detected in implementation of port / rbm element array\r\n")); 369 } 370 371 // error checking complete. Begin processing 245 372 size = DEFAULT_RX_QUEUE_SIZE; 246 373 // at this point, we know that … … 249 376 // 3. bind() / setReceiveQueueSize() was not called earlier 250 377 251 // update the necessary state variables 252 sock[sock_num].port = port; // assign the socket a port number 253 assign_port(port); // set bit 'port' in 'ports' equal to 1 254 255 rx_buf_mgr[port].pid = nrk_get_pid(); // fill up the members of the ReceiveBufferManager 256 if( nrk_signal_register(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 257 { 258 nrk_int_disable(); 259 nrk_led_set(RED_LED); 260 while(1) 261 nrk_kprintf(PSTR("bind(): Error while registering for seg_arrived_signal\r\n")); 262 } 378 // retrieve the indices of the elements 379 port_index = get_index_unassigned_port_element(); 380 rbm_index = get_index_unassigned_rbm_element(); 381 382 if(port_index == NRK_ERROR || rbm_index == NRK_ERROR) // sanity check for debugging 383 { 384 nrk_int_disable(); 385 nrk_led_set(RED_LED); 386 while(1) 387 nrk_kprintf(PSTR("bind(): Bug detected in implementation of port / rbm element array\r\n")); 388 } 389 390 // fill up the members of the port element 391 assign_port(port_index, (uint8_t)port); 392 393 // fill up the members of the socket descriptor 394 sock[sock_num].pindex = port_index; 395 sock[sock_num].rbmindex = rbm_index; 396 397 // fill up the members of the rbm element 398 rx_buf_mgr[rbm_index].pindex = port_index; 399 rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 263 400 264 401 // allocate 'size' buffers 265 402 for(i = 1; i <= size; i++) 266 403 { 267 index = get_index_unallocated_rx_buf(); // look for the unallocated receive buffer268 if( index == NRK_ERROR) // this should not happen404 buf_index = get_index_unallocated_rx_buf(); // look for the unallocated receive buffer 405 if(buf_index == NRK_ERROR) // this should not happen 269 406 { 270 407 nrk_int_disable(); … … 274 411 } 275 412 276 insert_rx_fq(&rx_buf_udp[ index], port, EMPTY); // insert the buffer in the free queue of the port277 rx_buf_mgr[ port].countTotal++; // increment the countTotal for this port413 insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); // insert the buffer in the free queue of the port 414 rx_buf_mgr[rbm_index].countTotal++; // increment the countTotal for this port 278 415 num_bufs_free--; 279 416 } 280 417 281 leave_cr(tl_sem, "bind()");282 leave_cr(bm_sem, "bind()");418 leave_cr(tl_sem, 8); 419 leave_cr(bm_sem, 8); 283 420 return NRK_OK; 284 421 } … … 286 423 int8_t get_rx_queue_size(int8_t sock_num) 287 424 { 288 int8_t port; // to store the port number associated with this socket number 289 int8_t count; 425 int8_t count; // to store the count of total rx buffers set aside for the given socket 290 426 291 427 enter_cr(bm_sem, 9); … … 311 447 } 312 448 313 if(sock[sock_num].p ort == INVALID_PORT) // no socket operations were performed yet449 if(sock[sock_num].pindex == -1) // no socket operations were performed yet 314 450 { 315 451 leave_cr(tl_sem, 9); 316 452 leave_cr(bm_sem, 9); 317 return 0; 318 } 453 return 0; // hence return 0 454 } 455 456 if(sock[sock_num].rbmindex == -1) // sanity check for debugging 457 { 458 nrk_int_disable(); 459 nrk_led_set(RED_LED); 460 while(1) 461 nrk_kprintf(PSTR("get_rx_queue_size(): Bug detected in implementation of port/rbm element array\r\n")); 462 } 319 463 320 464 // at this point we know that some socket operation has been performed on the socket prior to this call 321 port = sock[sock_num].port; 322 count = rx_buf_mgr[port].countTotal; 465 count = rx_buf_mgr[sock[sock_num].rbmindex].countTotal; 323 466 324 467 leave_cr(tl_sem, 9); … … 330 473 int8_t set_rx_queue_size(int8_t sock_num, int8_t size) 331 474 { 332 int8_t port; // to store the port number associated with given socket number475 uint8_t port; // to store the port number associated with given socket number 333 476 int8_t i; // loop index 334 477 int8_t flag; // to mark whether bind() was called prior to this call or not 335 int8_t index; // stores the index of next unallocated receive buffer 478 479 int8_t buf_index; // stores the index of next unallocated receive buffer 480 int8_t port_index; // stores the index of the next unassigned port element 481 int8_t rbm_index; // stores the index of the next unassigned rbm element 336 482 337 483 enter_cr(bm_sem, 10); … … 366 512 } 367 513 368 if(sock[sock_num].p ort == INVALID_PORT)// there is no port associated with this socket yet514 if(sock[sock_num].pindex == -1) // there is no port associated with this socket yet 369 515 { 370 516 flag = 1; // bind() was not called earlier 371 517 port = get_next_available_port(); // search for a ephemeral port number 372 if(port == NRK_ERROR)// no port available518 if(port == INVALID_PORT) // no port available 373 519 { 374 520 leave_cr(tl_sem, 10); … … 385 531 } 386 532 // port available and default num of buffers available, 387 // assign the new port number to the socket desc 388 sock[sock_num].port = port; 389 assign_port(port); 390 if( nrk_signal_register(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 391 { 533 // retrieve the indices of the elements 534 port_index = get_index_unassigned_port_element(); 535 rbm_index = get_index_unassigned_rbm_element(); 536 537 if(port_index == NRK_ERROR || rbm_index == NRK_ERROR) // sanity check for debugging 538 { 392 539 nrk_int_disable(); 393 540 nrk_led_set(RED_LED); 394 541 while(1) 395 nrk_kprintf(PSTR("set_rx_queue_size(): Error registering for seg_arrived_signal\r\n")); 396 } 397 } 542 nrk_kprintf(PSTR("set_rx_queue_size(): Bug detected in implementation of port / rbm element array\r\n")); 543 } 544 545 // fill up the members of the port element 546 assign_port(port_index, port); 547 548 // fill up the members of the socket descriptor 549 sock[sock_num].pindex = port_index; 550 sock[sock_num].rbmindex = rbm_index; 551 552 // fill up the members of the rbm element 553 rx_buf_mgr[rbm_index].pindex = port_index; 554 rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 555 556 } // end if(bind was called earlier) 398 557 else // bind was called earlier. DEFAULT_RX_QUEUE_SIZE buffers already allocated 399 558 { 400 // FIX ME401 559 if(size == DEFAULT_RX_QUEUE_SIZE) // nothing to do, buffers already allocated 402 560 { … … 405 563 return size; 406 564 } 565 else // size > DEFAULT_RX_QUEUE_SIZE 566 size -= DEFAULT_RX_QUEUE_SIZE; 407 567 } 408 568 … … 413 573 // at this point, the socket is associated with a valid port number 414 574 // and at least one receive buffer is free 415 // fill in the values of the corresponding ReceiveBufferManager416 rx_buf_mgr[port].pid = nrk_get_pid();417 418 575 // allocate 'size' buffers 419 576 for(i = 1; i <= size; i++) 420 577 { 421 index = get_index_unallocated_rx_buf(); // look for the next unallocated receive buffer422 if( index == NRK_ERROR) // this should not happen578 buf_index = get_index_unallocated_rx_buf(); // look for the next unallocated receive buffer 579 if(buf_index == NRK_ERROR) // this should not happen 423 580 { 424 581 nrk_int_disable(); … … 427 584 nrk_kprintf(PSTR("set_rx_queue_size(): Bug found in implementation of num_bufs_free\r\n")); 428 585 } 429 insert_rx_fq(&rx_buf_udp[ index], port, EMPTY); // insert the buffer in the free queue of the port430 rx_buf_mgr[ port].countTotal++;586 insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); // insert the buffer in the free queue of the port 587 rx_buf_mgr[rbm_index].countTotal++; 431 588 num_bufs_free--; 432 589 } … … 434 591 leave_cr(tl_sem, 10); 435 592 leave_cr(bm_sem, 10); 593 594 if(flag == 0) // bind was called earlier 595 return size + DEFAULT_RX_QUEUE_SIZE; 596 436 597 return size; 437 598 } … … 439 600 int8_t release_buffer(int8_t sock_num, uint8_t *ptr) 440 601 { 441 int8_t port; // to store the port number associated with the socket442 602 ReceiveBufferUDP *buf; // pointer to traverse the buffer list of the free queue 443 603 … … 462 622 return NRK_ERROR; 463 623 } 464 if(sock[sock_num].p ort == INVALID_PORT) // if no socket operation was performed earlier624 if(sock[sock_num].pindex == -1) // if no socket operation was performed earlier 465 625 { 466 626 _nrk_errno_set(INVALID_SOCKET); … … 470 630 return NRK_ERROR; 471 631 } 472 473 port = sock[sock_num].port; // retrieve the port number from the socket descriptor 632 633 if(sock[sock_num].rbmindex == -1) // sanity check for debugging 634 { 635 nrk_int_disable(); 636 nrk_led_set(RED_LED); 637 while(1) 638 nrk_kprintf(PSTR("release_buffer(): Bug discovered in implementation of port / rbm element array\r\n")); 639 } 474 640 475 641 // check to see if the pointer passed is valid 476 buf = rx_buf_mgr[ port].head_fq;642 buf = rx_buf_mgr[sock[sock_num].rbmindex].head_fq; 477 643 while(buf != NULL) 478 644 { … … 491 657 492 658 buf -> status = EMPTY; // mark the buffer as available now 493 rx_buf_mgr[ port].countFree++;659 rx_buf_mgr[sock[sock_num].rbmindex].countFree++; 494 660 495 661 leave_cr(tl_sem, 11); … … 500 666 int8_t close_socket(int8_t sock_num) 501 667 { 502 int8_t port; // to store the port number assoicated with the given socket number503 668 ReceiveBufferUDP *ptr; // temp variable 504 669 … … 525 690 526 691 // Free up resources associated with this socket descriptor 527 port = sock[sock_num].port; 528 if(port != INVALID_PORT) // some operation was performed earlier with this socket 529 { 530 rx_buf_mgr[port].pid = INVALID_PID; 531 if( nrk_signal_delete(rx_buf_mgr[port].seg_arrived_signal) == NRK_ERROR ) 692 if(sock[sock_num].pindex != -1) // some operation was performed earlier with this socket 693 { 694 if(sock[sock_num].rbmindex == -1) // sanity check 532 695 { 533 696 nrk_int_disable(); 534 697 nrk_led_set(RED_LED); 535 698 while(1) 536 nrk_kprintf(PSTR("close_socket(): Error in deleting seg_arrived_signal\r\n")); 537 } 538 699 nrk_kprintf(PSTR("close_socket(): Bug discovered in implementation of port /rbm element array\r\n")); 700 } 539 701 // remove all the FULL buffers from the port queue 540 while( (ptr = remove_rx_pq( port)) != NULL )702 while( (ptr = remove_rx_pq(sock[sock_num].rbmindex)) != NULL ) 541 703 ptr -> status = UNALLOCATED; 542 704 543 705 // remove all the EMPTY buffers from the free queue 544 while( (ptr = remove_rx_fq( port, EMPTY)) != NULL )706 while( (ptr = remove_rx_fq(sock[sock_num].rbmindex, EMPTY)) != NULL ) 545 707 ptr -> status = UNALLOCATED; 546 708 547 709 // remove all the FULL buffers from the free queue 548 while( (ptr = remove_rx_fq( port,FULL)) != NULL )710 while( (ptr = remove_rx_fq(sock[sock_num].rbmindex,FULL)) != NULL ) 549 711 ptr -> status = UNALLOCATED; 550 551 release_port(port); 712 713 rx_buf_mgr[sock[sock_num].rbmindex].pid = INVALID_PID; 714 rx_buf_mgr[sock[sock_num].rbmindex].pindex = -1; 715 rx_buf_mgr[sock[sock_num].rbmindex].countTotal = 0; 716 rx_buf_mgr[sock[sock_num].rbmindex].countFree = 0; 717 718 release_port(sock[sock_num].pindex); 552 719 } 553 720 554 sock[sock_num].port = INVALID_PORT; 721 sock[sock_num].pindex = -1; 722 sock[sock_num].rbmindex = -1; 555 723 sock[sock_num].pid = INVALID_PID; 556 724 … … 560 728 } 561 729 /************************************************************************************************/ 562 int8_t is_port_associated(int8_t port) 563 { 730 int8_t is_port_associated(int16_t port) 731 { 732 // ERROR checking 733 if(port <= 0 || port > MAX_PORT_NUM) 734 { 735 _nrk_errno_set(INVALID_ARGUMENT); 736 return NRK_ERROR; 737 } 738 564 739 enter_cr(tl_sem, 13); 565 740 566 if(check_port_available( port) == NRK_OK)741 if(check_port_available((uint8_t)port) == NRK_OK) 567 742 { 568 743 leave_cr(tl_sem, 13); … … 573 748 } 574 749 /************************************************************************************************/ 575 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int16_t dest_addr, int8_t dest_port, int8_t prio) 576 { 577 int8_t port; // to hold the port number corresponding to the socket number passed 750 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int16_t dest_port, int8_t prio) 751 { 578 752 int8_t result; // to hold the return type of various functions 579 753 580 if(DEBUG_TL == 2)581 nrk_kprintf(PSTR("Entered send operation of TL\r\n"));582 583 754 enter_cr(bm_sem, 14); 584 755 enter_cr(tl_sem, 14); 585 756 586 if(DEBUG_TL == 2)587 nrk_kprintf(PSTR("Inside CR of send operation of TL\r\n"));588 589 757 // ERROR checking 590 758 if(sock_num < 0 || sock_num >= MAX_PORTS) // bad input … … 606 774 } 607 775 608 if(dest_addr < 0 || dest_port <= 0 || dest_port > MAX_PORT S) // bad input776 if(dest_addr < 0 || dest_port <= 0 || dest_port > MAX_PORT_NUM) // bad input 609 777 { 610 778 _nrk_errno_set(INVALID_ARGUMENT); … … 634 802 635 803 /* Error checking is complete. Begin processing */ 636 if(sock[sock_num].port == INVALID_PORT) // this means that bind() / set_rx_queue_size() was 637 // not called earlier 638 { 639 // FIX ME IMPORTANT................. 640 leave_cr(tl_sem, 14); 641 leave_cr(bm_sem, 14); 642 result = set_rx_queue_size(sock_num, DEFAULT_RX_QUEUE_SIZE); 643 enter_cr(bm_sem, 14); 644 enter_cr(tl_sem, 14); 645 if(result == NRK_ERROR) // error number will be set already 804 if(sock[sock_num].pindex == -1) // this means that bind() / set_rx_queue_size() was 805 // not called earlier 806 { 807 int8_t buf_index, port_index, rbm_index; // indices for the various arrays 808 uint8_t port; 809 810 if(get_num_bufs_free() < DEFAULT_RX_QUEUE_SIZE) // no rx buffers left for this socket 646 811 { 647 812 leave_cr(tl_sem, 14); 648 leave_cr(bm_sem, 14); 813 leave_cr(bm_sem, 14); 814 _nrk_errno_set(NO_RX_BUFFERS_AVAILABLE); 649 815 return NRK_ERROR; 650 816 } 651 } 652 port = sock[sock_num].port; // retrieve the port number 653 // register for the 'send done' signal 654 if( nrk_signal_register(sd_sig[port].send_done_signal) == NRK_ERROR ) 655 { 656 nrk_int_disable(); 657 nrk_led_set(RED_LED); 658 while(1) 659 nrk_kprintf(PSTR("send(): Error while registering for send_done_signal\r\n")); 660 } 661 817 // retrieve the indices of the elements 818 port_index = get_index_unassigned_port_element(); 819 rbm_index = get_index_unassigned_rbm_element(); 820 buf_index = get_index_unallocated_rx_buf(); 821 822 if(port_index == NRK_ERROR || rbm_index == NRK_ERROR) // sanity check for debugging 823 { 824 nrk_int_disable(); 825 nrk_led_set(RED_LED); 826 while(1) 827 nrk_kprintf(PSTR("send(): Bug detected in implementation of port / rbm element array\r\n")); 828 } 829 830 if(buf_index == NRK_ERROR) // sanity check for debugging 831 { 832 nrk_int_disable(); 833 nrk_led_set(RED_LED); 834 while(1) 835 nrk_kprintf(PSTR("send(): Bug detected in implementation of num_bufs_free\r\n")); 836 } 837 // retrieve the next ephemeral port number 838 port = get_next_available_port(); 839 840 // fill up the members of the port element 841 assign_port(port_index, port); 842 843 // fill up the members of the socket descriptor 844 sock[sock_num].pindex = port_index; 845 sock[sock_num].rbmindex = rbm_index; 846 847 // fill up the members of the rbm element 848 rx_buf_mgr[rbm_index].pindex = port_index; 849 rx_buf_mgr[rbm_index].pid = nrk_get_pid(); 850 851 // insert a rx buffer in the free queue 852 insert_rx_fq(&rx_buf_udp[buf_index], rbm_index, EMPTY); 853 rx_buf_mgr[rbm_index].countTotal++; // increment the countTotal for this port 854 num_bufs_free--; 855 856 } //end if(bind was called earlier) 857 662 858 // prepare a UDP segment 663 udp_seg.srcPort = port ;664 udp_seg.destPort = dest_port;859 udp_seg.srcPort = ports[sock[sock_num].pindex].pno; 860 udp_seg.destPort = (uint8_t)dest_port; 665 861 udp_seg.length = len; 666 862 memcpy(udp_seg.data, ptr, len); // application payload already packed … … 668 864 // prepare a network packet 669 865 pkt.src = NODE_ADDR; 670 pkt.dest = dest_addr;866 pkt.dest = (uint16_t)dest_addr; 671 867 pkt.ttl = MAX_NETWORK_DIAMETER; 672 868 pkt.type = UDP; … … 679 875 680 876 if(DEBUG_TL == 2) 681 nrk_kprintf(PSTR(" Insidesend(). Before inserting packet in transmit queue\r\n"));877 nrk_kprintf(PSTR("send(). Before inserting packet in transmit queue\r\n")); 682 878 683 879 result = insert_tx_aq(&pkt); 684 880 685 881 if(DEBUG_TL == 2) 686 nrk_kprintf(PSTR(" Insidesend(). After inserting packet in transmit queue\r\n"));882 nrk_kprintf(PSTR("send(). After inserting packet in transmit queue\r\n")); 687 883 688 884 if(result == NRK_ERROR) … … 734 930 } 735 931 /*************************************************************************************************/ 736 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr, int8_t *srcPort, int8_t *rssi) 737 { 738 int8_t port; // to hold the port number associated with the passed socket number 932 uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi) 933 { 739 934 nrk_sig_mask_t my_sigs; // to hold the return value of network layer signals 740 935 ReceiveBufferUDP *buf; // temporary variable 741 936 Transport_Segment_UDP *seg;// pointer to received segment from the network layer 937 int8_t rbm_index; // to store the index of the corresponding receive buffer manager 938 int8_t port_index; // to store the index of the port element 742 939 743 940 enter_cr(bm_sem, 16); … … 754 951 } 755 952 756 if( sock[sock_num].pid != nrk_get_pid() || sock[sock_num].p ort == INVALID_PORT)953 if( sock[sock_num].pid != nrk_get_pid() || sock[sock_num].pindex == -1 ) 757 954 { 758 955 _nrk_errno_set(INVALID_SOCKET); // socket description not complete … … 763 960 } 764 961 765 // error checking complete. Begin processing 766 port = sock[sock_num].port; 962 if(sock[sock_num].rbmindex == -1) // sanity check for debugging 963 { 964 nrk_int_disable(); 965 nrk_led_set(RED_LED); 966 while(1) 967 nrk_kprintf(PSTR("receive(): Bug detected in implementation of port/rbm element array\r\n")); 968 } 969 970 // error checking complete. Begin processing 971 rbm_index = sock[sock_num].rbmindex; 972 port_index = sock[sock_num].pindex; 767 973 if(sock[sock_num].timeout.secs == 0 && sock[sock_num].timeout.nano_secs == 0) 768 974 { … … 772 978 773 979 // check the receive buffer manager for this port to see if there are any queued segments 774 if(rx_buf_mgr[ port].countFree == rx_buf_mgr[port].countTotal) // no queued segments980 if(rx_buf_mgr[rbm_index].countFree == rx_buf_mgr[rbm_index].countTotal) // no queued segments 775 981 { 776 982 if(DEBUG_TL == 2) 777 printf("receive(): No segments in receive queue of port = %d\n", port );983 printf("receive(): No segments in receive queue of port = %d\n", ports[rx_buf_mgr[rbm_index].pindex].pno); 778 984 while(1) 779 985 { 780 986 leave_cr(tl_sem, 16); 781 987 leave_cr(bm_sem, 16); 782 my_sigs = nrk_event_wait(SIG( rx_buf_mgr[port].seg_arrived_signal)); // block988 my_sigs = nrk_event_wait(SIG(ports[port_index].data_arrived_signal)); // block 783 989 enter_cr(bm_sem, 16); 784 990 enter_cr(tl_sem, 16); … … 792 998 nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait (without timeout)\r\n")); 793 999 } 794 if( my_sigs & SIG( rx_buf_mgr[port].seg_arrived_signal) ) // got a segment1000 if( my_sigs & SIG(ports[port_index].data_arrived_signal) ) // got a segment 795 1001 { 796 1002 if(DEBUG_TL == 2) 797 nrk_kprintf(PSTR("receive(): Received the segarrived signal\r\n"));1003 nrk_kprintf(PSTR("receive(): Received the data arrived signal\r\n")); 798 1004 break; 799 1005 } … … 818 1024 nrk_led_set(RED_LED); 819 1025 while(1) 820 nrk_kprintf(PSTR("receive(): Error in registering for nrk_wakeup_signal (with timeout)\r\n"));1026 nrk_kprintf(PSTR("receive(): Error in registering for nrk_wakeup_signal\r\n")); 821 1027 } 822 1028 if( nrk_set_next_wakeup(sock[sock_num].timeout) == NRK_ERROR) … … 825 1031 nrk_led_set(RED_LED); 826 1032 while(1) 827 nrk_kprintf(PSTR("receive(): Error returned by nrk_set_next_wakeup (with timeout)\r\n"));1033 nrk_kprintf(PSTR("receive(): Error returned by nrk_set_next_wakeup\r\n")); 828 1034 } 829 1035 // check the receive manager for this port to see if there are any queued segments 830 if(rx_buf_mgr[ port].countFree == rx_buf_mgr[port].countTotal) // no queued segments1036 if(rx_buf_mgr[rbm_index].countFree == rx_buf_mgr[rbm_index].countTotal) // no queued segments 831 1037 { 832 1038 while(1) … … 834 1040 leave_cr(tl_sem, 16); 835 1041 leave_cr(bm_sem, 16); 836 my_sigs = nrk_event_wait(SIG( rx_buf_mgr[port].seg_arrived_signal) | SIG(nrk_wakeup_signal));1042 my_sigs = nrk_event_wait(SIG(ports[port_index].data_arrived_signal) | SIG(nrk_wakeup_signal)); 837 1043 enter_cr(bm_sem, 16); 838 1044 enter_cr(tl_sem, 16); … … 846 1052 nrk_kprintf(PSTR("receive(): Error calling nrk_event_wait() (with timeout)\r\n")); 847 1053 } 848 if( my_sigs & SIG( rx_buf_mgr[port].seg_arrived_signal) ) // got a segment1054 if( my_sigs & SIG(ports[port_index].data_arrived_signal) ) // got a segment 849 1055 { 850 1056 sock[sock_num].timeout.secs = 0; // invalidate the timeout 851 1057 sock[sock_num].timeout.nano_secs = 0; 852 1058 if(DEBUG_TL == 2) 853 nrk_kprintf(PSTR("Received the seg arrived signal\r\n")); 854 // FIX me change the wakeup period back to what it was 1059 nrk_kprintf(PSTR("Received the data arrived signal\r\n")); 855 1060 break; 856 1061 } … … 860 1065 sock[sock_num].timeout.secs = 0; // invalidate the timeout value of the socket 861 1066 sock[sock_num].timeout.nano_secs = 0; 862 // FIX me change the wakeup period back to what it was1067 863 1068 _nrk_errno_set(SOCKET_TIMEOUT); 864 1069 … … 883 1088 // In either case, retrieve the segment and return back to user task 884 1089 885 buf = remove_rx_pq( port); // remove the buffer from the port queue1090 buf = remove_rx_pq(rbm_index); // remove the buffer from the port queue 886 1091 if(buf == NULL) // this should not happen 887 1092 { … … 889 1094 nrk_led_set(RED_LED); 890 1095 while(1) 891 nrk_kprintf(PSTR("receive(): Bug found in implementation of seg_arrived_signal / rx buffer mgmt\r\n"));892 } 893 insert_rx_fq(buf, port, FULL); // insert the buffer into the free queue with status = FULL1096 nrk_kprintf(PSTR("receive(): Bug found in implementation of data_arrived_signal / rx buffer mgmt\r\n")); 1097 } 1098 insert_rx_fq(buf, rbm_index, FULL); // insert the buffer into the free queue with status = FULL 894 1099 seg = &(buf -> seg); 895 1100 … … 911 1116 int8_t check_receive_queue(int8_t sock_num) 912 1117 { 913 int8_t port;1118 int8_t rbm_index; // to store the index of rbm element associated with the socket 914 1119 int8_t count1, count2, count3; 915 1120 … … 917 1122 enter_cr(tl_sem, 17); 918 1123 919 920 1124 // ERROR checking 921 1125 if(sock_num < 0 || sock_num >= MAX_PORTS) // bad input … … 928 1132 } 929 1133 930 if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].p ort == INVALID_PORT) ) // wrong socket number1134 if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].pindex == -1) ) // wrong socket number 931 1135 { 932 1136 _nrk_errno_set(INVALID_SOCKET); … … 937 1141 } 938 1142 939 port = sock[sock_num].port;940 count1 = rx_buf_mgr[ port].countTotal;941 count2 = rx_buf_mgr[ port].countFree;942 count3 = get_in_process_buf_count( port);1143 rbm_index = sock[sock_num].rbmindex; 1144 count1 = rx_buf_mgr[rbm_index].countTotal; 1145 count2 = rx_buf_mgr[rbm_index].countFree; 1146 count3 = get_in_process_buf_count(rbm_index); 943 1147 944 1148 leave_cr(tl_sem, 17); … … 949 1153 int8_t wait_until_send_done(int8_t sock_num) 950 1154 { 951 int8_t port ; // to hold the port number associated with the given socket number1155 int8_t port_index; // to store the index of the port element associated with the socket 952 1156 nrk_sig_mask_t my_sigs; // temp variable 953 1157 … … 965 1169 } 966 1170 967 if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].p ort == INVALID_PORT) ) // bad socket1171 if( (sock[sock_num].pid != nrk_get_pid()) || (sock[sock_num].pindex == -1) ) // bad socket 968 1172 { 969 1173 _nrk_errno_set(INVALID_SOCKET); … … 974 1178 } 975 1179 976 port = sock[sock_num].port; 1180 // error checking complete. Begin processing 1181 1182 port_index = sock[sock_num].pindex; 977 1183 978 1184 if(sock[sock_num].timeout.secs == 0 && sock[sock_num].timeout.nano_secs == 0) … … 983 1189 leave_cr(tl_sem, 18); 984 1190 leave_cr(bm_sem, 18); 985 my_sigs = nrk_event_wait( SIG( sd_sig[port].send_done_signal) );1191 my_sigs = nrk_event_wait( SIG(ports[port_index].send_done_signal) ); 986 1192 enter_cr(bm_sem, 18); 987 1193 enter_cr(tl_sem, 18); … … 995 1201 } 996 1202 997 if( my_sigs & SIG( sd_sig[port].send_done_signal) )1203 if( my_sigs & SIG(ports[port_index].send_done_signal) ) // data sent 998 1204 { 999 1205 leave_cr(tl_sem, 18); … … 1031 1237 leave_cr(tl_sem, 18); 1032 1238 leave_cr(bm_sem, 18); 1033 my_sigs = nrk_event_wait( SIG( sd_sig[port].send_done_signal) | SIG(nrk_wakeup_signal) );1239 my_sigs = nrk_event_wait( SIG(ports[port_index].send_done_signal) | SIG(nrk_wakeup_signal) ); 1034 1240 enter_cr(bm_sem, 18); 1035 1241 enter_cr(tl_sem, 18); … … 1040 1246 nrk_led_set(RED_LED); 1041 1247 while(1) 1042 nrk_kprintf(PSTR("wait_until_send_done(): Error returned by nrk_event_wait() (with timeout)\r\n"));1248 nrk_kprintf(PSTR("wait_until_send_done(): Error returned by nrk_event_wait()\r\n")); 1043 1249 } 1044 1250 1045 if( my_sigs & SIG( sd_sig[port].send_done_signal) )1251 if( my_sigs & SIG(ports[port_index].send_done_signal) ) 1046 1252 { 1047 1253 sock[sock_num].timeout.secs = 0; // disable the timeout … … 1055 1261 if( my_sigs & SIG(nrk_wakeup_signal) ) 1056 1262 { 1057 sock[sock_num].timeout.secs = 0; 1263 sock[sock_num].timeout.secs = 0; // disable the timeout 1058 1264 sock[sock_num].timeout.nano_secs = 0; 1059 1265 _nrk_errno_set(SOCKET_TIMEOUT); -
nano-RK/projects/network_stack/TransportLayerUDP.h
r118 r119 20 20 21 21 #define EPHEMERAL_PORT_NUM_START 11 // 11 is the smallest port number available to user tasks 22 #define MAX_PORT_NUM 255 22 23 23 24 // limits on various object types … … 32 33 typedef struct 33 34 { 34 int8_t srcPort; // source port of the application35 int8_t destPort; // destination port of the application35 uint8_t srcPort; // source port of the application 36 uint8_t destPort; // destination port of the application 36 37 int8_t length; // actual length of the data payload 37 38 uint8_t data[MAX_APP_PAYLOAD]; // application layer data … … 39 40 }Transport_Segment_UDP; 40 41 42 typedef struct 43 { 44 uint8_t pno; // the actual port number 45 nrk_sig_t data_arrived_signal; // indicates that data has arrived for this port in the receive queue 46 nrk_sig_t send_done_signal; // indicates that the highest priority message from this port 47 // has been sent over the radio 48 }Port; 49 41 50 typedef struct 42 51 { 43 int8_t port; // port number associated with this socket descriptor 52 int8_t pindex; // index of the port element for this socket 53 int8_t rbmindex; // index of the rbm element for this socket 44 54 int8_t pid; // pid of the process holding this socket descriptor 45 55 int8_t type; // type of socket … … 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_ERRORotherwise97 RETURNS: port number if available, INVALID_PORT otherwise 88 98 ERROR NOS: NO_PORTS_AVAILABLE if no ephemeral port numbers are available 89 99 COMMENTS: private function 90 100 */ 91 101 92 int8_t check_port_available( int8_t pt);102 int8_t check_port_available(uint8_t pt); 93 103 /* 94 104 This function checks whether port 'pt' is available … … 100 110 */ 101 111 102 void assign_port(int8_t p t);112 void assign_port(int8_t pindex, uint8_t pt); 103 113 /* 104 114 This functions records the fact that port 'pt' is allocated 105 115 106 PARAMS: pt: port number to be allocated 116 PARAMS: pindex: index of the relevant port element 117 pt: port number to be allocated 107 118 RETURNS: None 108 119 COMMENTS: private function … … 110 121 */ 111 122 112 void release_port(int8_t p ort);113 /* 114 This functions records the fact that 'port'is now available115 116 PARAMS: p ort: port number to be released123 void release_port(int8_t pindex); 124 /* 125 This functions records the fact that a given port is now available 126 127 PARAMS: pindex: port element holding information about the port 117 128 RETURNS: None 118 COMMENTS: None 119 */ 120 121 int8_t bind(int8_t sock_num, int8_t port); 129 COMMENTS: private function 130 */ 131 132 int8_t get_unassigned_port_element(); 133 /* 134 This function returns the index an unassigned port element or NRK_ERROR if none are available 135 136 PARAMS: None 137 RETURNS: index of an unassigned port element or NRK_ERROR if none are available 138 139 ERROR NOS: NO_PORT_ELEMENT_AVAILABLE 140 Comments: private function 141 */ 142 143 int8_t get_unassigned_rbm_element(); 144 /* 145 This function returns the index an unassigned rbm element or NRK_ERROR if none are available 146 147 PARAMS: None 148 RETURNS: index of an unassigned rbm element or NRK_ERROR if none are available 149 150 ERROR NOS: NO_RBM_ELEMENT_AVAILABLE 151 Comments: private function 152 */ 153 154 uint8_t get_port_num(int8_t sock_num); 155 /* 156 This function can be used by an application to find the port number mapped to a given socket descriptor 157 158 PARAMS: sock_num: The socket descriptor 159 RETURNS: the corresponding port number or INVALID_PORT if none exists / error 160 161 ERROR NOS: INVALID_ARGUMENT if the passed parameter is faulty 162 INVALID_SOCKET if a wrong socket number is passed 163 UNMAPPED_SOCKET if no socket/port mapping exists 164 165 Comments: User API 166 A return value of INVALID_PORT could mean either an error has occurred or there 167 does not exist any socket/port mapping. Check the error number to find out. 168 */ 169 170 int8_t bind(int8_t sock_num, int16_t port); 122 171 /* 123 172 This function binds a given socket descriptor to a given port number … … 177 226 2. a valid port 178 227 3. a receive queue of size at least 1 228 179 229 */ 180 230 … … 193 243 194 244 COMMENTS: User API 195 This function should be called when the application has fin sihed processing245 This function should be called when the application has finished processing 196 246 a received message or has copied the message to a application-local storage 197 247 area … … 211 261 */ 212 262 213 int8_t is_port_associated(int 8_t port);263 int8_t is_port_associated(int16_t port); 214 264 /* 215 265 This function checks whether a given port number has been assigned to a socket or not 216 266 217 PARAMS: port: The port number 218 RETURNS: TRUE if a mapping exists, FALSE otherwise 219 COMMENTS: User API 220 */ 221 222 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int16_t dest_addr, int8_t dest_port, int8_t prio); 267 PARAMS: port: The port number 268 RETURNS: TRUE if a mapping exists, FALSE if a mapping does not exist, NRK_ERROR on error 269 270 ERROR NOS: INVALID_ARGUMENT if the passed paramter is faulty 271 COMMENTS: User API 272 */ 273 274 int8_t send(int8_t sock_num, uint8_t *ptr, int8_t len, int32_t dest_addr, int16_t dest_port, int8_t prio); 223 275 /* 224 276 This function can be used by the application task to send data to other nodes on the network … … 266 318 */ 267 319 268 uint8_t* receive(int8_t sock_num, int8_t *len, int8_t *srcAddr,int8_t *srcPort, int8_t *rssi);320 uint8_t* receive(int8_t sock_num, int8_t *len, uint16_t *srcAddr, uint8_t *srcPort, int8_t *rssi); 269 321 /* 270 322 This function allows an application to receive a message along with network control information … … 326 378 327 379 ERROR NOS: INVALID_ARGUMENT if the passed parameter is faulty 328 INVALID_SOCKET if an incomplete socket number is passed380 INVALID_SOCKET if an incomplete socket descriptor is passed 329 381 SOCKET_TIMEOUT if a timeout value is specified prior to this call and 330 382 the timeout value expires 331 383 Comments: User API 332 384 If you want to specify a maximum wait period, use set_timeout() prior to every such 333 wait_until_send_done() call. 385 wait_until_send_done() call. The return value of NRK_ERROR could mean either that an 386 error occured during processing or that the timeout expired. Check the error number 387 to find out 334 388 */ 335 389
Note: See TracChangeset
for help on using the changeset viewer.
