<commit_before>/*
 * Copyright (c) 2007, Swedish Institute of Computer Science
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file is part of the Contiki operating system.
 *
 * @(#)$Id: radio-uip-uaodv.c,v 1.4 2007/07/17 23:02:21 fros4943 Exp $
 */

#include "radio-uip-uaodv.h"
#include "net/hc.h"
#include "net/uip.h"
#include "net/uaodv.h"
#include "net/uaodv-rt.h"
#include "net/uaodv-def.h"
#include "lib/crc16.h"
#include "list.h"
#include <string.h>
#include <stdio.h>

/*
{ // DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT
  uip_ipaddr_t *DEST = &packet->finaldest;
  printf("%d.%d.%d.%d: XXXXX radio_uip_uaodv.c bad dest due to no ack %d.%d.%d.%d\n", 
    uip_ipaddr1(&uip_hostaddr), uip_ipaddr2(&uip_hostaddr), uip_ipaddr3(&uip_hostaddr), uip_ipaddr4(&uip_hostaddr), 
    uip_ipaddr1(DEST), uip_ipaddr2(DEST), uip_ipaddr3(DEST), uip_ipaddr4(DEST));
  fflush(stdout);
} // DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT
*/


/* Packet buffer size and retransmission settings */
#define MAX_BUFFERED_PACKETS 10
#define MAX_RETRANSMISSIONS_RREP 16
#define MAX_RETRANSMISSIONS_UNICAST 16

/* Forward packet (header) */
#define FWD_ID "fWd:"
#define FWD_ID_LENGTH 4
#define FWD_NEXT_IP FWD_ID_LENGTH
#define FWD_PACKET_LENGTH (FWD_NEXT_IP + 4)

/* Acknowledgement packet */
#define ACK_ID "aCk"
#define ACK_ID_LENGTH 3
#define ACK_CRC ACK_ID_LENGTH
#define ACK_PACKET_LENGTH (ACK_ID_LENGTH + 2)

enum {
  EVENT_SEND_ACK
};

struct buf_packet {
  struct buf_packet *next;
  u8_t data[UIP_BUFSIZE];
  int len;
  u8_t resends;
  u8_t acked;
  u8_t want_ack;
  u16_t crc;
  uip_ipaddr_t finaldest;
  struct etimer etimer;
};

LIST(buf_packet_list);
MEMB(buf_packet_mem, struct buf_packet, MAX_BUFFERED_PACKETS);

PROCESS(radio_uip_process, "radio uIP uAODV process");

static const struct radio_driver *radio;

/*---------------------------------------------------------------------------*/
int radio_uip_buffer_outgoing_packet(u8_t *buf, int len, uip_ipaddr_t *dest, int max_sends);
int radio_uip_is_ack(u8_t *buf, int len);
int radio_uip_uaodv_add_header(u8_t *buf, int len, uip_ipaddr_t *addr);
int radio_uip_uaodv_remove_header(u8_t *buf, int len);
void radio_uip_uaodv_change_header(u8_t *buf, int len, uip_ipaddr_t *addr);
int radio_uip_uaodv_header_exists(u8_t *buf, int len);
int radio_uip_uaodv_is_broadcast(uip_ipaddr_t *addr);
int radio_uip_uaodv_fwd_is_broadcast(u8_t *buf, int len);
int radio_uip_uaodv_fwd_is_me(u8_t *buf, int len);
int radio_uip_uaodv_dest_is_me(u8_t *buf, int len);
int radio_uip_uaodv_dest_port(u8_t *buf, int len);
/*---------------------------------------------------------------------------*/

/* Main process - handles (re)transmissions and acks */
PROCESS_THREAD(radio_uip_process, ev, data)
{
  struct buf_packet *packet;

  PROCESS_BEGIN();

  while(1) {
    PROCESS_YIELD();

    if(ev == EVENT_SEND_ACK) {

      /* Prepare and send ack for given 16-bit CRC */
      char ackPacket[ACK_PACKET_LENGTH];
      memcpy(ackPacket, ACK_ID, ACK_ID_LENGTH);
      memcpy(&ackPacket[ACK_CRC], &data, 2);
      radio->send(ackPacket, ACK_PACKET_LENGTH);

    } else if(ev == PROCESS_EVENT_TIMER) {

      /* Locate which packet acknowledgement timed out */
      for(packet = list_head(buf_packet_list);
          packet != NULL;
          packet = packet->next) {
        if (etimer_expired(&packet->etimer)) {

          if (packet->acked) {
            /* Already acked packet, remove silently */
            list_remove(buf_packet_list, packet);
            memb_free(&buf_packet_mem, packet);

          } else if (packet->resends > 0) {
            /* Resend packet */
            packet->resends--;
            etimer_set(&packet->etimer, CLOCK_SECOND * 1);
            radio->send(packet->data, packet->len);

          } else {
            /* Packet was resent maximum number of times */

            /* If an ack was expected, flag destination to bad */
            if (packet->want_ack && !uip_ipaddr_cmp(&packet->finaldest, &uip_broadcast_addr)) {
              uaodv_bad_dest(&packet->finaldest);
            }

            list_remove(buf_packet_list, packet);
            memb_free(&buf_packet_mem, packet);
          }            
        }
      }
    }
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
static void<commit_msg>random ack time<commit_after>/*
 * Copyright (c) 2007, Swedish Institute of Computer Science
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file is part of the Contiki operating system.
 *
 * @(#)$Id: radio-uip-uaodv.c,v 1.5 2007/07/26 13:22:01 fros4943 Exp $
 */

#include "radio-uip-uaodv.h"
#include "net/hc.h"
#include "net/uip.h"
#include "net/uaodv.h"
#include "net/uaodv-rt.h"
#include "net/uaodv-def.h"
#include "lib/crc16.h"
#include "list.h"
#include <string.h>
#include <stdio.h>

/*
{ // DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT
  uip_ipaddr_t *DEST = &packet->finaldest;
  printf("%d.%d.%d.%d: XXXXX radio_uip_uaodv.c bad dest due to no ack %d.%d.%d.%d\n", 
    uip_ipaddr1(&uip_hostaddr), uip_ipaddr2(&uip_hostaddr), uip_ipaddr3(&uip_hostaddr), uip_ipaddr4(&uip_hostaddr), 
    uip_ipaddr1(DEST), uip_ipaddr2(DEST), uip_ipaddr3(DEST), uip_ipaddr4(DEST));
  fflush(stdout);
} // DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT - DEBUG OUTPUT
*/


/* Packet buffer size and retransmission settings */
#define MAX_BUFFERED_PACKETS 10
#define MAX_RETRANSMISSIONS_RREP 16
#define MAX_RETRANSMISSIONS_UNICAST 16

/* Forward packet (header) */
#define FWD_ID "fWd:"
#define FWD_ID_LENGTH 4
#define FWD_NEXT_IP FWD_ID_LENGTH
#define FWD_PACKET_LENGTH (FWD_NEXT_IP + 4)

/* Acknowledgement packet */
#define ACK_ID "aCk"
#define ACK_ID_LENGTH 3
#define ACK_CRC ACK_ID_LENGTH
#define ACK_PACKET_LENGTH (ACK_ID_LENGTH + 2)
#define ACK_TIMEOUT (CLOCK_SECOND / 50) * (random_rand() % 100)

enum {
  EVENT_SEND_ACK
};

struct buf_packet {
  struct buf_packet *next;
  u8_t data[UIP_BUFSIZE];
  int len;
  u8_t resends;
  u8_t acked;
  u8_t want_ack;
  u16_t crc;
  uip_ipaddr_t finaldest;
  struct etimer etimer;
};

LIST(buf_packet_list);
MEMB(buf_packet_mem, struct buf_packet, MAX_BUFFERED_PACKETS);

PROCESS(radio_uip_process, "radio uIP uAODV process");

static const struct radio_driver *radio;

/*---------------------------------------------------------------------------*/
int radio_uip_buffer_outgoing_packet(u8_t *buf, int len, uip_ipaddr_t *dest, int max_sends);
int radio_uip_is_ack(u8_t *buf, int len);
int radio_uip_uaodv_add_header(u8_t *buf, int len, uip_ipaddr_t *addr);
int radio_uip_uaodv_remove_header(u8_t *buf, int len);
void radio_uip_uaodv_change_header(u8_t *buf, int len, uip_ipaddr_t *addr);
int radio_uip_uaodv_header_exists(u8_t *buf, int len);
int radio_uip_uaodv_is_broadcast(uip_ipaddr_t *addr);
int radio_uip_uaodv_fwd_is_broadcast(u8_t *buf, int len);
int radio_uip_uaodv_fwd_is_me(u8_t *buf, int len);
int radio_uip_uaodv_dest_is_me(u8_t *buf, int len);
int radio_uip_uaodv_dest_port(u8_t *buf, int len);
/*---------------------------------------------------------------------------*/

/* Main process - handles (re)transmissions and acks */
PROCESS_THREAD(radio_uip_process, ev, data)
{
  struct buf_packet *packet;

  PROCESS_BEGIN();

  while(1) {
    PROCESS_YIELD();

    if(ev == EVENT_SEND_ACK) {

      /* Prepare and send ack for given 16-bit CRC */
      char ackPacket[ACK_PACKET_LENGTH];
      memcpy(ackPacket, ACK_ID, ACK_ID_LENGTH);
      memcpy(&ackPacket[ACK_CRC], &data, 2);
      radio->send(ackPacket, ACK_PACKET_LENGTH);

    } else if(ev == PROCESS_EVENT_TIMER) {

      /* Locate which packet acknowledgement timed out */
      for(packet = list_head(buf_packet_list);
          packet != NULL;
          packet = packet->next) {
        if (etimer_expired(&packet->etimer)) {

          if (packet->acked) {
            /* Already acked packet, remove silently */
            list_remove(buf_packet_list, packet);
            memb_free(&buf_packet_mem, packet);

          } else if (packet->resends > 0) {
            /* Resend packet */
            packet->resends--;
            etimer_set(&packet->etimer, ACK_TIMEOUT);

            radio->send(packet->data, packet->len);

          } else {
            /* Packet was resent maximum number of times */

            /* If an ack was expected, flag destination to bad */
            if (packet->want_ack && !uip_ipaddr_cmp(&packet->finaldest, &uip_broadcast_addr)) {
              uaodv_bad_dest(&packet->finaldest);
            }

            list_remove(buf_packet_list, packet);
            memb_free(&buf_packet_mem, packet);
          }            
        }
      }
    }
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
static void
{'fraction_non_alphanumeric': 0.1333952075599055, 'fraction_numerical': 0.011981100236247048, 'mean_word_length': 3.115625, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7572043', 'n_tokens_mistral': 4111, 'n_tokens_neox': 3862, 'n_words': 1120}