42 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 60 PortPurpose_Destination
69 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 76 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 86 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 93 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 100 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 108 #define _________________________________________FUNCTIONS_SECTION_________________________________________________________________________ 134 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
136 oC_IntMan_EnterCriticalSection();
141 ReservedPorts = oC_List_New(&Allocator, AllocationFlags_Default);
142 PortReleased = oC_Event_New( oC_Event_State_Inactive, &Allocator, AllocationFlags_Default );
143 ModuleBusy = oC_Mutex_New( oC_Mutex_Type_Normal , &Allocator, AllocationFlags_Default);
146 ErrorCondition( ReservedPorts !=
NULL , oC_ErrorCode_AllocationError )
147 && ErrorCondition( ModuleBusy !=
NULL , oC_ErrorCode_AllocationError )
148 && ErrorCondition( PortReleased !=
NULL , oC_ErrorCode_AllocationError )
152 errorCode = oC_ErrorCode_None;
156 bool listDeleted = oC_List_Delete( ReservedPorts , AllocationFlags_Default);
157 bool mutexDeleted = oC_Mutex_Delete(&ModuleBusy , AllocationFlags_Default);
158 bool eventDeleted = oC_Event_Delete(&PortReleased , AllocationFlags_Default);
160 oC_SaveIfFalse(
"UDP:TurnOn - Cannot delete list " , listDeleted , oC_ErrorCode_ReleaseError );
161 oC_SaveIfFalse(
"UDP:TurnOn - Cannot delete mutex " , mutexDeleted , oC_ErrorCode_ReleaseError );
162 oC_SaveIfFalse(
"UDP:TurnOn - Cannot delete event " , eventDeleted , oC_ErrorCode_ReleaseError );
165 oC_IntMan_ExitCriticalSection();
190 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
192 oC_IntMan_EnterCriticalSection();
199 bool allPacketsReleased =
true;
201 bool listDeleted = oC_List_Delete ( ReservedPorts , AllocationFlags_Default );
202 bool eventDeleted = oC_Event_Delete( &PortReleased , AllocationFlags_Default );
203 bool mutexDeleted = oC_Mutex_Delete( &ModuleBusy , AllocationFlags_Default );
206 ErrorCondition( listDeleted , oC_ErrorCode_ReleaseError )
207 && ErrorCondition( allPacketsReleased , oC_ErrorCode_ReleaseError )
208 && ErrorCondition( eventDeleted , oC_ErrorCode_ReleaseError )
209 && ErrorCondition( mutexDeleted , oC_ErrorCode_ReleaseError )
213 errorCode = oC_ErrorCode_None;
216 oC_IntMan_ExitCriticalSection();
237 bool reserved =
false;
241 && oC_SaveIfFalse(
"UDP::IsPortReserved - ", Port !=
oC_Udp_Port_Empty , oC_ErrorCode_PortNotCorrect )
242 && oC_SaveIfFalse(
"UDP::IsPortReserved - ", Process ==
NULL || oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect )
283 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
287 && ErrorCondition( isram(Port) , oC_ErrorCode_OutputAddressNotInRAM )
288 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
289 && ErrorCondition( *Port == 0 ||
IsSpecialPort(*Port) ==
false || iscurroot() , oC_ErrorCode_PermissionDenied )
292 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
293 bool freePortFound =
false;
295 errorCode = oC_ErrorCode_PortNotAvailable;
298 freePortFound ==
false 299 && errorCode == oC_ErrorCode_PortNotAvailable
300 && ErrorCondition( oC_KTime_GetTimestamp() <= endTimestamp, oC_ErrorCode_PortNotAvailable )
303 Timeout = endTimestamp - oC_KTime_GetTimestamp();
305 if( ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleBusy ) )
316 freePortFound = reservation ==
NULL;
322 freePortFound = reservation ==
NULL;
325 if(ErrorCondition( freePortFound , oC_ErrorCode_PortNotAvailable ))
327 if(ErrorCondition( port !=
oC_Udp_Port_Empty , oC_ErrorCode_InternalDataAreDamaged ))
329 reservation = kmalloc(
sizeof(
ReservationData_t), &Allocator, AllocationFlags_Default );
331 if(ErrorCondition( reservation !=
NULL , oC_ErrorCode_AllocationError ))
333 reservation->Busy = oC_Mutex_New( oC_Mutex_Type_Normal , &Allocator, AllocationFlags_Default );
335 if( ErrorCondition( reservation->Busy !=
NULL , oC_ErrorCode_AllocationError ) )
337 reservation->Port = port;
338 reservation->Process = getcurprocess();
340 bool addedToList = oC_List_PushBack(ReservedPorts,reservation,&Allocator);
342 if( ErrorCondition( addedToList, oC_ErrorCode_CannotAddObjectToList ) )
344 errorCode = oC_ErrorCode_None;
348 oC_SaveIfFalse(
"UDP:Reserve - cannot release reservation memory ", kfree(reservation, AllocationFlags_Default), oC_ErrorCode_ReleaseError);
353 oC_SaveIfFalse(
"UDP:Reserve - cannot release reservation memory ", kfree(reservation, AllocationFlags_Default), oC_ErrorCode_ReleaseError);
361 oC_Event_ClearStateBits( PortReleased, oC_Event_StateMask_Full );
363 Timeout = endTimestamp - oC_KTime_GetTimestamp();
375 oC_Mutex_Give( ModuleBusy );
414 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
415 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
420 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
421 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleBusy )
427 ErrorCondition( reservation !=
NULL , oC_ErrorCode_PortNotReserved )
428 && ErrorCondition( reservation->Process == getcurprocess() , oC_ErrorCode_PortReservedByDifferentProcess )
429 && ErrorCondition( oC_Mutex_Take(reservation->Busy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_PortBusy )
432 bool mutexDeleted = oC_Mutex_Delete(&reservation->Busy, AllocationFlags_Default);
433 bool deletedFromList = oC_List_RemoveAll(ReservedPorts,reservation);
434 bool deleted = kfree(reservation,AllocationFlags_Default);
437 ErrorCondition( mutexDeleted , oC_ErrorCode_CannotDeleteObject )
438 && ErrorCondition( deletedFromList , oC_ErrorCode_CannotRemoveObjectFromList )
439 && ErrorCondition( deleted , oC_ErrorCode_ReleaseError )
442 oC_Event_SetState(PortReleased,Port);
443 errorCode = oC_ErrorCode_None;
447 oC_Mutex_Give(ModuleBusy);
483 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
484 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
488 && ErrorCondition( getcuruser() == getrootuser() , oC_ErrorCode_PossibleOnlyForRoot )
489 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
490 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleBusy )
495 errorCode = oC_ErrorCode_None;
499 && ErrorCondition( oC_KTime_GetTimestamp() <= endTimestamp , oC_ErrorCode_Timeout )
502 reservation->Process = getcurprocess();
504 Timeout = endTimestamp - oC_KTime_GetTimestamp();
511 oC_Mutex_Give(ModuleBusy);
553 oC_SaveIfFalse(
"Udp::PacketNew: " ,
oC_Module_IsTurnedOn(oC_Module_Udp) , oC_ErrorCode_ModuleNotStartedYet )
556 && oC_SaveIfFalse(
"Udp::PacketNew: Size is not correct - " , Size > 0 , oC_ErrorCode_SizeNotCorrect )
558 && oC_SaveIfFalse(
"Udp::PacketNew: Data address incorrect - " , Data ==
NULL || isaddresscorrect(Data) , oC_ErrorCode_WrongAddress )
559 && oC_SaveIfFalse(
"Udp::PacketNew: Allocator incorrect - " , isaddresscorrect(PacketAllocator) , oC_ErrorCode_WrongAddress )
564 if(oC_SaveIfFalse(
"Udp::PacketNew: Cannot allocate memory for a packet - ", packet !=
NULL , oC_ErrorCode_AllocationError))
572 void * dataDestination = datagram;
576 memcpy(dataDestination,Data,Size);
591 bool success =
false;
593 if(isram(Packet) && isram(*Packet))
595 success = kfree(*Packet,AllocationFlags_Default);
611 if(isaddresscorrect(Packet))
631 bool success =
false;
639 memcpy(datagram->
Data,Data,Size);
654 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
657 ErrorCondition( isaddresscorrect(Packet) , oC_ErrorCode_WrongAddress )
658 && ErrorCondition( isram(outData) , oC_ErrorCode_OutputAddressNotInRAM )
664 if( ErrorCondition( isram(datagram) , oC_ErrorCode_PacketNotCorrect) )
666 memcpy( outData, datagram->
Data, Size );
667 errorCode = oC_ErrorCode_None;
684 if(isaddresscorrect(datagram))
686 data = datagram->
Data;
734 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
739 oC_Timestamp_t timestamp = oC_KTime_GetTimestamp();
740 oC_Timestamp_t endTimestamp = timestamp + Timeout;
745 && ErrorCondition( LocalPort > 0 , oC_ErrorCode_PortNotCorrect )
746 && ErrorCondition( isram(Packet) , oC_ErrorCode_OutputAddressNotInRAM )
747 && ErrorCondition( isaddresscorrect(Destination) , oC_ErrorCode_WrongAddress )
749 && ErrorCondition( Destination->
Port > 0 , oC_ErrorCode_PortNotCorrect )
750 && ErrorCondition( ((
oC_Net_PacketType_t)addressType) == packetType , oC_ErrorCode_TypeNotCorrect )
751 && ErrorCondition( packetSize > 0 , oC_ErrorCode_SizeNotCorrect )
752 && ErrorCondition( Timeout > 0 , oC_ErrorCode_TimeNotCorrect )
753 && ErrorCondition( reservation !=
NULL , oC_ErrorCode_PortNotReserved )
754 && ErrorCondition( reservation->Process == getcurprocess() , oC_ErrorCode_PortReservedByDifferentProcess )
756 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleBusy )
763 oC_Mutex_Give(ModuleBusy);
766 ErrorCondition(
oC_Netif_IsCorrect(Netif) , oC_ErrorCode_CannotFindNetifForTheAddress )
767 && ErrorCondition( oC_Mutex_Take(reservation->Busy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_PortBusy )
768 && ErrorCondition( isram(datagram) , oC_ErrorCode_PacketNotCorrect )
792 oC_SaveError(
"UDP::Send - Function is not implemented for IPv6!", oC_ErrorCode_NotImplemented);
801 errorCode =
oC_Netif_SendPacket( Netif, &Packet->Packet, oC_KTime_CalculateTimeout(endTimestamp) );
854 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
857 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
862 && ErrorCondition( LocalPort > 0 , oC_ErrorCode_PortNotCorrect )
863 && ErrorCondition( Timeout > 0 , oC_ErrorCode_TimeNotCorrect )
864 && ErrorCondition( isram(Packet) , oC_ErrorCode_OutputAddressNotInRAM )
865 && ErrorCondition( reservation !=
NULL , oC_ErrorCode_PortNotReserved )
866 && ErrorCondition( reservation->Process == getcurprocess() , oC_ErrorCode_PortReservedByDifferentProcess )
867 && ErrorCondition( packetSize > 0 , oC_ErrorCode_SizeNotCorrect )
868 && ErrorCondition( oC_Mutex_Take(reservation->Busy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_PortBusy )
869 && ErrorCondition( oC_Mutex_Take(ModuleBusy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
883 .DestinationPort = LocalPort ,
884 .ExpectedNetif = Netif ,
887 oC_Mutex_Give(ModuleBusy);
893 if(ErrorCondition( isram(datagram) , oC_ErrorCode_PacketNotCorrect ))
896 errorCode = oC_ErrorCode_None;
928 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
932 && ErrorCondition( isram(Packet) , oC_ErrorCode_AddressNotInRam )
933 && ErrorCondition( outChecksumCorrect ==
NULL || isram(outChecksumCorrect) , oC_ErrorCode_OutputAddressNotInRAM )
934 && ErrorCondition( outExpectedChecksum ==
NULL || isram(outExpectedChecksum) , oC_ErrorCode_OutputAddressNotInRAM )
939 if( ErrorCondition(isram(datagram), oC_ErrorCode_PacketNotCorrect) )
948 if(outChecksumCorrect !=
NULL)
953 if(outExpectedChecksum)
955 *outExpectedChecksum = checksum;
958 errorCode = oC_ErrorCode_None;
966 #undef _________________________________________FUNCTIONS_SECTION_________________________________________________________________________ 973 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 982 bool packetForUs =
false;
986 if(header !=
NULL && Parameter !=
NULL)
992 packetForUs = filterData->ExpectedNetif ==
NULL || filterData->ExpectedNetif == Netif;
1008 foreach(ReservedPorts,reservation)
1012 || (reservation->Process == Process && Process !=
NULL )
1015 foundReservation = reservation;
1019 return foundReservation;
1029 uint32_t checksum = 0;
1034 uint16_t * srcAddr = (uint16_t*)&Packet->IPv4.Header.
SourceIp;
1035 uint16_t * dstAddr = (uint16_t*)&Packet->IPv4.Header.
DestinationIp;
1037 checksum += srcAddr[1];
1038 checksum += srcAddr[0];
1040 checksum += dstAddr[1];
1041 checksum += dstAddr[0];
1053 while(checksum >> 16)
1055 checksum = (checksum & 0xFFFF) + (checksum >> 16);
1061 return (uint16_t)checksum;
1082 return reservationData !=
NULL;
1111 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ oC_Net_Ipv4_t SourceIp
Source IP address.
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
uint32_t Length
Length of the packet in bytes.
oC_ErrorCode_t oC_Udp_ReleaseAllPortsReservedBy(oC_Process_t Process, oC_Time_t Timeout)
releases all ports reserved by the process
struct PACKED oC_Udp_Header_t
stores UDP header
uint32_t QoS
Quality of Service or Type of Service - describes the priority of the packet
static oC_Net_Packet_t * oC_Net_Packet_New(Allocator_t Allocator, oC_Net_PacketType_t Type, void *Data, uint16_t Size)
allocates memory for a packet
bool oC_Udp_IsPortReserved(oC_Udp_Port_t Port, oC_Process_t Process)
checks if the given UDP port is reserved
oC_Udp_Port_t
stores UDP port number
static uint16_t oC_Net_ConvertUint16FromNetworkEndianess(uint16_t v)
converts uint16_t from network byte order
oC_Net_Ipv4PacketHeader_t Header
Header of the IPv4 packet.
identifier for allocations
uint32_t DF
Don't Fragment - packet can be fragmented or not
bool oC_MemMan_FreeAllMemoryOfAllocator(Allocator_t Allocator)
release all memory of allocator
The file with interface for process manager.
oC_ErrorCode_t oC_NetifMan_ReceivePacket(const oC_Net_Address_t *Address, oC_Net_Packet_t *outPacket, oC_Time_t Timeout, oC_Netif_t *outNetif, oC_NetifMan_PacketFilterFunction_t FilterFunction, const void *Parameter)
receives packet from the network
number of special ports, that are reserved for special service
Stores interface of netif manager module.
oC_Net_AddressType_t
stores type of the network address
oC_Net_PacketType_t
stores type of the packet
bool oC_Netif_IsCorrect(oC_Netif_t Netif)
checks if the Netif object is correct
static oC_Net_PacketType_t oC_Net_Packet_GetType(const oC_Net_Packet_t *Packet)
returns type of the packet
static void ConvertHeaderToNetworkEndianess(oC_Udp_Header_t *Header)
converts UDP header to network endianess
static uint16_t oC_Net_GetMaximumPacketDataSize(oC_Net_PacketType_t Type)
returns maximum size of the packet data
static uint16_t oC_Net_ConvertUint16ToNetworkEndianess(uint16_t v)
converts uint16_t to network byte order
static void ConvertHeaderFromNetworkEndianess(oC_Udp_Header_t *Header)
converts UDP header from network endianess
bool oC_Udp_Packet_Delete(oC_Udp_Packet_t **Packet)
release memory allocated for a UDP packet
oC_ErrorCode_t oC_Udp_ReservePort(oC_Udp_Port_t *Port, oC_Time_t Timeout)
reserves a UDP port
The file with interface for the module library.
oC_ErrorCode_t oC_Udp_TurnOn(void)
turns on UDP module
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
The file with interface for event module.
oC_ErrorCode_t oC_Udp_CalculateChecksum(oC_Udp_Packet_t *Packet, bool *outChecksumCorrect, uint16_t *outExpectedChecksum)
Calculates checksum for UDP packet.
oC_ErrorCode_t oC_Udp_ReleasePort(oC_Udp_Port_t Port, oC_Time_t Timeout)
releases a port
oC_Net_Ipv4_t oC_Netif_GetIpv4Address(oC_Netif_t Netif)
returns address of the network interface
uint32_t FragmentOffset
Field to identify position of fragment within original packet.
oC_ErrorCode_t oC_Udp_TurnOff(void)
turns off UDP module
oC_Net_Ipv4_t IPv4
Address in IPv4 version.
uint32_t ID
ID value for reconstruction the packet from segments.
The file with list library.
The file with interface for interrupt manager.
uint16_t oC_Udp_Packet_GetDataSize(const oC_Udp_Packet_t *Packet)
returns size of the data inside UDP packet
uint32_t IHL
4 bits, that contain Internet Header Length, which is the length of the header in multiples of 4 (num...
uint16_t oC_Udp_Packet_GetMaximumDataSize(oC_Net_PacketType_t Type)
returns maximum size of the data for UDP packet
Special value for marks port as not filled.
oC_Net_Ipv6_t IPv6
Address in IPv6 version.
static uint16_t CalculateChecksum(oC_Udp_Packet_t *Packet)
calculates checksum for a packet
static bool oC_Net_IsAddressCorrect(const oC_Net_Address_t *Address)
returns true if the given address is correct
The file with interface for mutex managing.
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
oC_ErrorCode_t oC_Udp_Packet_ReadData(const oC_Udp_Packet_t *Packet, void *outData, uint16_t Size)
reads data of the UDP packet
uint32_t TTL
Time to live - number of hops the packet is allowed to pass before it dies.
uint8_t Data[IPv4_MAXIMUM_DATA_LENGTH]
Data of the packet.
oC_Udp_Packet_t * oC_Udp_Packet_New(Allocator_t PacketAllocator, oC_Net_PacketType_t Type, const void *Data, uint16_t Size)
allocates memory for a UDP packet
oC_Net_Ipv6_t Source
Source IP address.
oC_Net_Ipv4Packet_t IPv4
Packet in format IPv4.
uint16_t SourcePort
This field identifies the sender's port when meaningful and should be assumed to be the port to reply...
uint32_t Checksum
Header checksum - number used for errors detection.
oC_Netif_t oC_NetifMan_GetNetifForAddress(const oC_Net_Address_t *Destination)
returns network interface according to destination address
The file with memory manager interface.
static void * oC_Net_Packet_GetDataReference(oC_Net_Packet_t *Packet)
returns reference to the payload in the packet
uint16_t DestinationPort
This field identifies the receiver's port and is required.
uint32_t Protocol
Contains selected protocol (TCP/UDP/ICMP,etc).
uint32_t MF
More Fragments - whether more fragments of a packet follow
bool oC_Udp_Packet_SetData(oC_Udp_Packet_t *Packet, const void *Data, uint16_t Size)
sets data inside UDP packet
static uint16_t oC_Net_CalculateChecksum(const void *Buffer, uint16_t Size, uint16_t InitialValue, bool ConvertEndianess, bool PrepareChecksumToSend)
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
static bool IsSpecialPort(oC_Udp_Port_t Port)
returns true if port is special port
static const oC_Allocator_t Allocator
oC_ErrorCode_t oC_Udp_Send(oC_Netif_t Netif, oC_Udp_Port_t LocalPort, const oC_Net_Address_t *Destination, oC_Udp_Packet_t *Packet, oC_Time_t Timeout)
sends UDP packet
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
oC_ErrorCode_t oC_Netif_SendPacket(oC_Netif_t Netif, oC_Net_Packet_t *Packet, oC_Time_t Timeout)
sends packet by using the network interface
oC_Net_Ipv4_t DestinationIp
Destination IP address.
oC_ErrorCode_t oC_Udp_Receive(oC_Netif_t Netif, oC_Udp_Port_t LocalPort, oC_Udp_Packet_t *Packet, oC_Time_t Timeout)
receives UDP packet
static bool PacketFilterFunction(oC_Net_Packet_t *Packet, const void *Parameter, oC_Netif_t Netif)
function used in NetifMan for filtering packets
void * oC_Udp_Packet_GetDataReference(oC_Udp_Packet_t *Packet)
returns reference to the data inside UDP packet
bool oC_Netif_ReadIpv6Address(oC_Netif_t Netif, oC_Net_Ipv6_t *outAddress)
reads IPv6 address of the network interface
static ReservationData_t * FindReservation(oC_Udp_Port_t Port, oC_Process_t Process)
finds reservation of port or process
oC_Net_AddressType_t Type
Type of the address stored inside.
oC_Net_Ipv6Packet_t IPv6
Packet in format IPv6.
static bool IsPortReserved(oC_Udp_Port_t Port)
checks if port is reserved
oC_Net_Port_t Port
Port of the address.
oC_Net_Ipv6_t Destination
Destination IP address.
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off