25#include "IEEE802_11.h"
26#include "IEEE802_11_Phy.h"
27#include "IEEE1609_Interface.h"
29static PIEEE1609_BUFFER get_ieee1609_buffer(NETSIM_ID nDeviceId,
30 NETSIM_ID nInterfaceId,
31 IEEE1609_CHANNEL_TYPE type)
33 PIEEE1609_MAC_VAR mac = GET_IEEE1609_MAC_VAR(nDeviceId, nInterfaceId);
36 case IEEE1609_ChannelType_CCH:
37 return &(mac->buffer[0]);
39 case IEEE1609_ChannelType_SCH:
40 return &(mac->buffer[1]);
42 case IEEE1609_ChannelType_GUARD:
46 fnNetSimError(
"Unknown channel type %d\n", type);
52PIEEE802_11_MAC_VAR IEEE802_11_MAC(NETSIM_ID ndeviceId, NETSIM_ID nInterfaceId)
56 fnNetSimError(
"Device id is passed 0 in function %s", __FUNCTION__);
59 if (nInterfaceId == 0)
61 fnNetSimError(
"Interface id is passed 0 in function %s", __FUNCTION__);
64 switch (DEVICE_MACLAYER(ndeviceId, nInterfaceId)->nMacProtocolId)
66 case MAC_PROTOCOL_IEEE1609:
67 return ((PIEEE1609_MAC_VAR)DEVICE_MACVAR(ndeviceId, nInterfaceId))->secondary_protocol_var;
69 case MAC_PROTOCOL_IEEE802_11:
70 return ((PIEEE802_11_MAC_VAR)DEVICE_MACVAR(ndeviceId, nInterfaceId));
73 fnNetSimError(
"Unknown mac protocol in %s\n", __FUNCTION__);
79static IEEE1609_CHANNEL_TYPE get_curr_channel_type(NETSIM_ID nDeviceId,NETSIM_ID nInterfaceId)
81 return GET_IEEE1609_MAC_VAR(nDeviceId, nInterfaceId)->CHANNEL_INFO.channel_type;
84static IEEE1609_CHANNEL_TYPE get_channel_type_of_packet(NetSim_PACKET* packet)
86 switch (packet->nPacketType)
89 return IEEE1609_ChannelType_CCH;
91 return IEEE1609_ChannelType_SCH;
95PIEEE802_11_PHY_VAR IEEE802_11_PHY(NETSIM_ID ndeviceId, NETSIM_ID nInterfaceId)
99 fnNetSimError(
"Device id is passed 0 in function %s", __FUNCTION__);
102 if (nInterfaceId == 0)
104 fnNetSimError(
"Interface id is passed 0 in function %s", __FUNCTION__);
107 switch (DEVICE_MACLAYER(ndeviceId, nInterfaceId)->nMacProtocolId)
109 case MAC_PROTOCOL_IEEE1609:
110 return ((PIEEE1609_PHY_VAR)DEVICE_PHYVAR(ndeviceId, nInterfaceId))->secondary_protocol_var;;
112 case MAC_PROTOCOL_IEEE802_11:
113 return ((PIEEE802_11_PHY_VAR)DEVICE_PHYVAR(ndeviceId, nInterfaceId));
116 fnNetSimError(
"Unknown phy protocol in %s\n", __FUNCTION__);
122void SET_IEEE802_11_MAC(NETSIM_ID ndeviceId, NETSIM_ID nInterfaceId,PIEEE802_11_MAC_VAR mac)
124 switch (DEVICE_MACLAYER(ndeviceId, nInterfaceId)->nMacProtocolId)
126 case MAC_PROTOCOL_IEEE1609:
127 ((PIEEE1609_MAC_VAR)DEVICE_MACVAR(ndeviceId, nInterfaceId))->secondary_protocol_var = mac;
129 case MAC_PROTOCOL_IEEE802_11:
130 DEVICE_MACVAR(ndeviceId, nInterfaceId) = mac;
133 fnNetSimError(
"Unknown mac protocol in %s\n", __FUNCTION__);
138void SET_IEEE802_11_PHY(NETSIM_ID ndeviceId, NETSIM_ID nInterfaceId,PIEEE802_11_PHY_VAR phy)
140 switch (DEVICE_MACLAYER(ndeviceId, nInterfaceId)->nMacProtocolId)
142 case MAC_PROTOCOL_IEEE1609:
143 ((PIEEE1609_PHY_VAR)DEVICE_PHYVAR(ndeviceId, nInterfaceId))->secondary_protocol_var = phy;
145 case MAC_PROTOCOL_IEEE802_11:
146 DEVICE_PHYVAR(ndeviceId, nInterfaceId) = phy;
149 fnNetSimError(
"Unknown mac protocol in %s\n", __FUNCTION__);
154bool isIEEE802_11_Configure(NETSIM_ID ndeviceId, NETSIM_ID nInterfaceId)
156 if (!DEVICE_MACLAYER(ndeviceId, nInterfaceId))
159 switch (DEVICE_MACLAYER(ndeviceId, nInterfaceId)->nMacProtocolId)
161 case MAC_PROTOCOL_IEEE1609:
162 return ((PIEEE1609_MAC_VAR)DEVICE_MACVAR(ndeviceId, nInterfaceId))->secondary_protocol == MAC_PROTOCOL_IEEE802_11;
164 case MAC_PROTOCOL_IEEE802_11:
173static NETSIM_ID add_to_IEEE1609_queue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NetSim_PACKET* packet)
175 IEEE1609_CHANNEL_TYPE type = get_channel_type_of_packet(packet);
176 PIEEE1609_BUFFER buf = get_ieee1609_buffer(nDeviceId,nInterfaceId,type);
179 buf->tail->pstruNextPacket = packet;
190NETSIM_ID add_to_queue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NetSim_PACKET* packet)
192 switch (DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId)
194 case MAC_PROTOCOL_IEEE1609:
195 return add_to_IEEE1609_queue(nDeviceId, nInterfaceId, packet);
197 case MAC_PROTOCOL_IEEE802_11:
198 return fn_NetSim_IEEE802_11e_AddtoQueue(nDeviceId, nInterfaceId, packet);
201 fnNetSimError(
"Unknown Mac protocol %d in %s.",
202 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId, __FUNCTION__);
208static bool isPacketinIEEE1609Queue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, IEEE1609_CHANNEL_TYPE type)
210 PIEEE1609_BUFFER buf = get_ieee1609_buffer(nDeviceId, nInterfaceId, type);
212 if (!buf)
return false;
213 return buf->head ? true :
false;
216bool isPacketInQueue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
218 switch (DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId)
220 case MAC_PROTOCOL_IEEE1609:
221 return isPacketinIEEE1609Queue(nDeviceId, nInterfaceId, get_curr_channel_type(nDeviceId, nInterfaceId));
223 case MAC_PROTOCOL_IEEE802_11:
224 return fn_NetSim_IEEE802_11e_IsPacketInQueue(nDeviceId, nInterfaceId);
227 fnNetSimError(
"Unknown Mac protocol %d in %s.",
228 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId, __FUNCTION__);
234static NetSim_PACKET* get_from_IEEE1609_queue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId,
237 IEEE1609_CHANNEL_TYPE type)
239 NetSim_PACKET* p = NULL, *t = NULL;
241 PIEEE1609_BUFFER buf = get_ieee1609_buffer(nDeviceId, nInterfaceId, type);
249 while (buf->head && c<nPacketRequire)
253 t->pstruNextPacket = buf->head;
254 t = t->pstruNextPacket;
261 buf->head = buf->head->pstruNextPacket;
262 t->pstruNextPacket = NULL;
271NetSim_PACKET* get_from_queue(NETSIM_ID nDeviceId,
272 NETSIM_ID nInterfaceId,
276 switch (DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId)
278 case MAC_PROTOCOL_IEEE1609:
281 return get_from_IEEE1609_queue(nDeviceId,
285 get_curr_channel_type(nDeviceId,nInterfaceId));
288 case MAC_PROTOCOL_IEEE802_11:
289 return fn_NetSim_IEEE802_11e_GetPacketFromQueue(nDeviceId, nInterfaceId,
294 fnNetSimError(
"Unknown Mac protocol %d in %s.",
295 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId, __FUNCTION__);
301static void readd_to_IEEE1609_queue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NetSim_PACKET* packet)
303 IEEE1609_CHANNEL_TYPE type = get_channel_type_of_packet(packet);
304 PIEEE1609_BUFFER buf = get_ieee1609_buffer(nDeviceId, nInterfaceId, type);
307 packet->pstruNextPacket = buf->head;
317void readd_to_queue(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NetSim_PACKET* packet)
319 switch (DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId)
321 case MAC_PROTOCOL_IEEE1609:
322 readd_to_IEEE1609_queue(nDeviceId, nInterfaceId, packet);
324 case MAC_PROTOCOL_IEEE802_11:
325 fnNetSimError(
"Mustnot call this function %s for protocol %d.", __FUNCTION__,
326 MAC_PROTOCOL_IEEE802_11);
330 fnNetSimError(
"Unknown Mac protocol %d in %s.",
331 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId, __FUNCTION__);
336static bool is_time_in_same_channel(
double time, NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
338 PIEEE1609_MAC_VAR mac = GET_IEEE1609_MAC_VAR(nDeviceId, nInterfaceId);
339 if (mac->CHANNEL_INFO.dEndTime >= time)
345static void revert_packet(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
347 PIEEE802_11_MAC_VAR mac = IEEE802_11_MAC(nDeviceId, nInterfaceId);
348 if (!mac->currentProcessingPacket)
351 if (isIEEE802_11_CtrlPacket(mac->currentProcessingPacket))
353 readd_to_IEEE1609_queue(nDeviceId, nInterfaceId,
355 mac->waitingforCTS = NULL;
356 fn_NetSim_Packet_FreePacket(mac->currentProcessingPacket);
357 mac->currentProcessingPacket = NULL;
361 readd_to_IEEE1609_queue(nDeviceId, nInterfaceId,
362 mac->currentProcessingPacket);
363 mac->currentProcessingPacket = NULL;
367static void make_idle(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
369 PIEEE802_11_MAC_VAR mac = IEEE802_11_MAC(nDeviceId, nInterfaceId);
370 PIEEE802_11_PHY_VAR phy = IEEE802_11_PHY(nDeviceId, nInterfaceId);
372 IEEE802_11_Change_Mac_State(mac, IEEE802_11_MACSTATE_MAC_IDLE);
373 phy->radio.radioState = RX_ON_IDLE;
374 phy->radio.eventId = pstruEventDetails->nEventId;
377bool validate_processing_time(
double time, NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
379 switch (DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId)
381 case MAC_PROTOCOL_IEEE1609:
383 if (is_time_in_same_channel(time, nDeviceId, nInterfaceId))
385 revert_packet(nDeviceId,nInterfaceId);
386 make_idle(nDeviceId, nInterfaceId);
390 case MAC_PROTOCOL_IEEE802_11:
394 fnNetSimError(
"Unknown MAC protocol %d in %s\n",
395 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->nMacProtocolId,