<commit_before>/*
 * Copyright (c) 2010, 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: csma.c,v 1.6 2010/02/18 21:48:39 adamdunkels Exp $
 */

/**
 * \file
 *         A MAC 
 * \author
 *         <NAME> <<EMAIL>>
 */

#define CSMA_CONF_REXMIT 1

#include "net/mac/csma.h"
#include "net/rime/packetbuf.h"
#include "net/rime/queuebuf.h"
#include "net/rime/ctimer.h"

#include "lib/random.h"

#include "net/netstack.h"

#include "lib/list.h"
#include "lib/memb.h"

#include <string.h>

#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else /* DEBUG */
#define PRINTF(...)
#endif /* DEBUG */

struct queued_packet {
  struct queued_packet *next;
  struct queuebuf *buf;
  struct ctimer retransmit_timer;
  mac_callback_t sent;
  void *cptr;
  uint8_t transmissions;
};

#define MAX_RETRANSMITS 2

#define MAX_QUEUED_PACKETS 8
MEMB(packet_memb, struct queued_packet, MAX_QUEUED_PACKETS);


/*---------------------------------------------------------------------------*/
#if CSMA_CONF_REXMIT
static void
free_packet(struct queued_packet *q)
{
  queuebuf_free(q->buf);
  memb_free(&packet_memb, q);
}

static void retransmit_packet(void *ptr);

static void
packet_sent(void *ptr, int status, int num_transmissions)
{
  struct queued_packet *q = ptr;
  clock_time_t time = 0;
  mac_callback_t sent;
  void *cptr;
  int num_tx;
  
  sent = q->sent;
  cptr = q->cptr;
  num_tx = q->transmissions;
  
  if(status != MAC_TX_OK) {
    switch(status) {
    case MAC_TX_COLLISION:
      PRINTF("csma: rexmit collision %d\n", q->transmissions);
      break; 
    case MAC_TX_NOACK:
      PRINTF("csma: rexmit noack %d\n", q->transmissions);
      break;
   default:
     PRINTF("csma: rexmit err %d, %d\n", ret, q->transmissions);
    }
  
    time = NETSTACK_RDC.channel_check_interval();
    if(time == 0) {
      time = CLOCK_SECOND;
    }
    time = time + (random_rand() % (q->transmissions * 3 * time));
    
    if(q->transmissions - 1 < MAX_RETRANSMITS) {
      ctimer_set(&q->retransmit_timer, time,
                 retransmit_packet, q);
    } else {
      PRINTF("csma: drop after %d\n", q->transmissions);
      free_packet(q);
      mac_call_sent_callback(sent, ptr, status, num_tx);
    }
  } else {
    PRINTF("csma: rexmit ok %d\n", q->transmissions);
    free_packet(q);
    mac_call_sent_callback(sent, ptr, status, num_tx);
  }
}

static void
retransmit_packet(void *ptr)
{
  struct queued_packet *q = ptr;

  queuebuf_to_packetbuf(q->buf);
  q->transmissions++;
  NETSTACK_RDC.send(packet_sent, q);
}
/*---------------------------------------------------------------------------*/
static void
sent_packet_1(void *ptr, int status, int num_transmissions)
{
  struct queued_packet *q = ptr;
  clock_time_t time;
  rimeaddr_t receiver;
  
  rimeaddr_copy(&receiver, packetbuf_addr(PACKETBUF_ADDR_RECEIVER));

  if(rimeaddr_cmp(&receiver, &rimeaddr_null)) {
    PRINTF("broadcast/");
  }
  if(status != MAC_TX_OK) {
    switch(status) {
    case MAC_TX_COLLISION:
      PRINTF("csma: collision\n");
      break; 
    case MAC_TX_NOACK:
      PRINTF("csma: noack\n");
      break;
   default:
      PRINTF("csma: err %d\n", ret);
    }
  } else {
    PRINTF("csma: ok\n");
  }
  
  /* Check if we saw a collission, and if we have a queuebuf with the
     packet available. Only retransmit unicast packets. Retransmit
     only once, for now. */
  if((status == MAC_TX_COLLISION || status == MAC_TX_NOACK) &&
     !rimeaddr_cmp(&receiver, &rimeaddr_null)) {
    /* If the packet couldn't be sent because of a collision or the
         lack of an ACK, we let the other transmissions get through
         before we try again. */
    time = NETSTACK_RDC.channel_check_interval();
    if(time == 0) {
      time = CLOCK_SECOND;
    }
    time = time + (random_rand() % (3 * time));

    ctimer_set(&q->retransmit_timer, time,
	       retransmit_packet, q);
  } else {
    mac_callback_t sent;
    void *cptr;
    int num_tx;
    
    sent = q->sent;
    cptr = q->cptr;
    num_tx = q->transmissions;
    free_packet(q);
    mac_call_sent_callback(sent, ptr, status, num_tx);
  }
}
/*---------------------------------------------------------------------------*/
static void
send_packet(mac_callback_t sent, void *ptr)
{
  struct queued_packet *q;

  /* Remember packet for later. */
  q = memb_alloc(&packet_memb);
  if(q != NULL) {<commit_msg>Bugfix: the wrong callback pointer was passed to higher layers.<commit_after>/*
 * Copyright (c) 2010, 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: csma.c,v 1.7 2010/02/23 18:49:45 adamdunkels Exp $
 */

/**
 * \file
 *         A MAC 
 * \author
 *         <NAME> <<EMAIL>>
 */

#define CSMA_CONF_REXMIT 1

#include "net/mac/csma.h"
#include "net/rime/packetbuf.h"
#include "net/rime/queuebuf.h"
#include "net/rime/ctimer.h"

#include "lib/random.h"

#include "net/netstack.h"

#include "lib/list.h"
#include "lib/memb.h"

#include <string.h>

#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else /* DEBUG */
#define PRINTF(...)
#endif /* DEBUG */

struct queued_packet {
  struct queued_packet *next;
  struct queuebuf *buf;
  struct ctimer retransmit_timer;
  mac_callback_t sent;
  void *cptr;
  uint8_t transmissions;
};

#define MAX_RETRANSMITS 4

#define MAX_QUEUED_PACKETS 8
MEMB(packet_memb, struct queued_packet, MAX_QUEUED_PACKETS);


/*---------------------------------------------------------------------------*/
#if CSMA_CONF_REXMIT
static void
free_packet(struct queued_packet *q)
{
  queuebuf_free(q->buf);
  memb_free(&packet_memb, q);
}

static void retransmit_packet(void *ptr);

static void
packet_sent(void *ptr, int status, int num_transmissions)
{
  struct queued_packet *q = ptr;
  clock_time_t time = 0;
  mac_callback_t sent;
  void *cptr;
  int num_tx;
  
  sent = q->sent;
  cptr = q->cptr;
  num_tx = q->transmissions;
  
  if(status != MAC_TX_OK) {
    switch(status) {
    case MAC_TX_COLLISION:
      PRINTF("csma: rexmit collision %d\n", q->transmissions);
      break; 
    case MAC_TX_NOACK:
      PRINTF("csma: rexmit noack %d\n", q->transmissions);
      break;
   default:
     PRINTF("csma: rexmit err %d, %d\n", status, q->transmissions);
    }
  
    time = NETSTACK_RDC.channel_check_interval();
    if(time == 0) {
      time = CLOCK_SECOND;
    }
    time = time + (random_rand() % (q->transmissions * 3 * time));
    
    if(q->transmissions - 1 < MAX_RETRANSMITS) {
      ctimer_set(&q->retransmit_timer, time,
                 retransmit_packet, q);
    } else {
      PRINTF("csma: drop after %d\n", q->transmissions);
      free_packet(q);
      mac_call_sent_callback(sent, cptr, status, num_tx);
    }
  } else {
    PRINTF("csma: rexmit ok %d\n", q->transmissions);
    free_packet(q);
    mac_call_sent_callback(sent, cptr, status, num_tx);
  }
}

static void
retransmit_packet(void *ptr)
{
  struct queued_packet *q = ptr;

  queuebuf_to_packetbuf(q->buf);
  q->transmissions++;
  NETSTACK_RDC.send(packet_sent, q);
}
/*---------------------------------------------------------------------------*/
static void
sent_packet_1(void *ptr, int status, int num_transmissions)
{
  struct queued_packet *q = ptr;
  clock_time_t time;
  rimeaddr_t receiver;
  
  rimeaddr_copy(&receiver, packetbuf_addr(PACKETBUF_ADDR_RECEIVER));

  if(rimeaddr_cmp(&receiver, &rimeaddr_null)) {
    PRINTF("broadcast/");
  }
  if(status != MAC_TX_OK) {
    switch(status) {
    case MAC_TX_COLLISION:
      PRINTF("csma: collision\n");
      break; 
    case MAC_TX_NOACK:
      PRINTF("csma: noack\n");
      break;
   default:
      PRINTF("csma: err %d\n", status);
    }
  } else {
    PRINTF("csma: ok\n");
  }
  
  /* Check if we saw a collission, and if we have a queuebuf with the
     packet available. Only retransmit unicast packets. Retransmit
     only once, for now. */
  if((status == MAC_TX_COLLISION || status == MAC_TX_NOACK) &&
     !rimeaddr_cmp(&receiver, &rimeaddr_null)) {
    /* If the packet couldn't be sent because of a collision or the
         lack of an ACK, we let the other transmissions get through
         before we try again. */
    time = NETSTACK_RDC.channel_check_interval();
    if(time == 0) {
      time = CLOCK_SECOND;
    }
    time = time + (random_rand() % (3 * time));

    ctimer_set(&q->retransmit_timer, time,
	       retransmit_packet, q);
  } else {
    mac_callback_t sent;
    void *cptr;
    int num_tx;
    
    sent = q->sent;
    cptr = q->cptr;
    num_tx = q->transmissions;
    free_packet(q);
    mac_call_sent_callback(sent, cptr, status, num_tx);
  }
}
/*---------------------------------------------------------------------------*/
static void
send_packet(mac_callback_t sent, void *ptr)
{
  struct queued_packet *q;

  /* Remember packet for later. */
  q = memb_alloc(&packet_memb);
  if(q != NULL) {
{'fraction_non_alphanumeric': 0.14294073392961631, 'fraction_numerical': 0.005851375073142188, 'mean_word_length': 2.9110820529584833, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 49, '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': '2927805', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3868, 'n_words': 1173}