Skip to content

Commit

Permalink
revamped printk system
Browse files Browse the repository at this point in the history
Changed ioctl to allow for variable amount of debug printing at
cc2520.ko runtime. This way applications can completely turn off debug
output, or enable full debug output without having to recompile the
module.

Defaults to INFO level printing, as before.
  • Loading branch information
bradjc committed Apr 15, 2013
1 parent ea79252 commit a71694a
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 87 deletions.
11 changes: 6 additions & 5 deletions csma.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "csma.h"
#include "cc2520.h"
#include "radio.h"
#include "debug.h"

struct cc2520_interface *csma_top;
struct cc2520_interface *csma_bottom;
Expand Down Expand Up @@ -128,20 +129,20 @@ static enum hrtimer_restart cc2520_csma_timer_cb(struct hrtimer *timer)
// interrupt context, there's a few places
// where we spin lock and assume we can be
// preempted. If we're running in atomic mode
// that promise is broken. We use a work queue.
// that promise is broken. We use a work queue.

// The workqueue adds about 30uS of latency.
// The workqueue adds about 30uS of latency.
INIT_WORK(&work, cc2520_csma_wq);
queue_work(wq, &work);
return HRTIMER_NORESTART;
return HRTIMER_NORESTART;
}
else {
spin_lock_irqsave(&state_sl, flags);
if (csma_state == CC2520_CSMA_TX) {
csma_state = CC2520_CSMA_CONG;
spin_unlock_irqrestore(&state_sl, flags);

new_backoff =
new_backoff =
cc2520_csma_get_backoff(backoff_min, backoff_max_cong);

INFO((KERN_INFO "[cc2520] - channel still busy, waiting %d uS\n", new_backoff));
Expand Down Expand Up @@ -228,4 +229,4 @@ void cc2520_csma_set_init_backoff(int backoff)
void cc2520_csma_set_cong_backoff(int backoff)
{
backoff_max_cong = backoff;
}
}
23 changes: 23 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef DEBUG_H
#define DEBUG_H

// Define different levels of debug printing

// print nothing
#define DEBUG_PRINT_OFF 0
// print only when something goes wrong
#define DEBUG_PRINT_ERR 1
// print occasional messages about interesting things
#define DEBUG_PRINT_INFO 2
// print a good amount of debuging output
#define DEBUG_PRINT_DBG 3

// Defines the level of debug output
extern uint8_t debug_print;

// Define the printk macros.
#define ERR(x) do {if (debug_print >= DEBUG_PRINT_ERR) { printk x; }} while (0)
#define INFO(x) do {if (debug_print >= DEBUG_PRINT_INFO) { printk x; }} while (0)
#define DBG(x) do {if (debug_print >= DEBUG_PRINT_DBG) { printk x; }} while (0)

#endif
50 changes: 25 additions & 25 deletions interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "sack.h"
#include "csma.h"
#include "lpl.h"
#include "debug.h"

struct cc2520_interface *interface_bottom;

Expand Down Expand Up @@ -45,8 +46,6 @@ static struct semaphore rx_done_sem;
// Results, stored by the callbacks
static int tx_result;

static bool print_messages = false;

DECLARE_WAIT_QUEUE_HEAD(cc2520_interface_read_queue);

static void cc2520_interface_tx_done(u8 status);
Expand Down Expand Up @@ -135,7 +134,7 @@ static ssize_t interface_write(
}
tx_pkt_len = pkt_len;

if (print_messages) {
if (debug_print >= DEBUG_PRINT_DBG) {
interface_print_to_log(tx_buf_c, pkt_len, true);
}

Expand Down Expand Up @@ -163,7 +162,7 @@ static ssize_t interface_read(struct file *filp, char __user *buf, size_t count,
if (copy_to_user(buf, rx_buf_c, rx_pkt_len))
return -EFAULT;

if (print_messages) {
if (debug_print >= DEBUG_PRINT_DBG) {
interface_print_to_log(rx_buf_c, rx_pkt_len, false);
}

Expand All @@ -176,15 +175,15 @@ static long interface_ioctl(struct file *file,
{
switch (ioctl_num) {
case CC2520_IO_RADIO_INIT:
printk(KERN_INFO "[cc2520] - radio starting\n");
INFO((KERN_INFO "[cc2520] - radio starting\n"));
cc2520_radio_start();
break;
case CC2520_IO_RADIO_ON:
printk(KERN_INFO "[cc2520] - radio turning on\n");
INFO((KERN_INFO "[cc2520] - radio turning on\n"));
cc2520_radio_on();
break;
case CC2520_IO_RADIO_OFF:
printk(KERN_INFO "[cc2520] - radio turning off\n");
INFO((KERN_INFO "[cc2520] - radio turning off\n"));
cc2520_radio_off();
break;
case CC2520_IO_RADIO_SET_CHANNEL:
Expand Down Expand Up @@ -236,10 +235,11 @@ static void interface_ioctl_set_print(struct cc2520_set_print_messages_data *dat
return;
}

INFO((KERN_INFO "[cc2520] - setting debug message print: %d", ldata.enabled));
INFO((KERN_INFO "[cc2520] - setting debug message print: %i", ldata.debug_level));

print_messages = ldata.enabled;
debug_print = ldata.debug_level;
}

static void interface_ioctl_set_channel(struct cc2520_set_channel_data *data)
{
int result;
Expand All @@ -248,11 +248,11 @@ static void interface_ioctl_set_channel(struct cc2520_set_channel_data *data)
result = copy_from_user(&ldata, data, sizeof(struct cc2520_set_channel_data));

if (result) {
printk(KERN_ALERT "[cc2520] - an error occurred setting the channel\n");
ERR((KERN_ALERT "[cc2520] - an error occurred setting the channel\n"));
return;
}

printk(KERN_INFO "[cc2520] - Setting channel to %d\n", ldata.channel);
INFO((KERN_INFO "[cc2520] - Setting channel to %d\n", ldata.channel));
cc2520_radio_set_channel(ldata.channel);
}

Expand All @@ -263,12 +263,12 @@ static void interface_ioctl_set_address(struct cc2520_set_address_data *data)
result = copy_from_user(&ldata, data, sizeof(struct cc2520_set_address_data));

if (result) {
printk(KERN_ALERT "[cc2520] - an error occurred setting the address\n");
ERR((KERN_ALERT "[cc2520] - an error occurred setting the address\n"));
return;
}

printk(KERN_INFO "[cc2520] - setting addr: %d ext_addr: %lld pan_id: %d\n",
ldata.short_addr, ldata.extended_addr, ldata.pan_id);
INFO((KERN_INFO "[cc2520] - setting addr: %d ext_addr: %lld pan_id: %d\n",
ldata.short_addr, ldata.extended_addr, ldata.pan_id));
cc2520_radio_set_address(ldata.short_addr, ldata.extended_addr, ldata.pan_id);
}

Expand All @@ -279,11 +279,11 @@ static void interface_ioctl_set_txpower(struct cc2520_set_txpower_data *data)
result = copy_from_user(&ldata, data, sizeof(struct cc2520_set_txpower_data));

if (result) {
printk(KERN_ALERT "[cc2520] - an error occurred setting the txpower\n");
ERR((KERN_ALERT "[cc2520] - an error occurred setting the txpower\n"));
return;
}

printk(KERN_INFO "[cc2520] - setting txpower: %d\n", ldata.txpower);
INFO((KERN_INFO "[cc2520] - setting txpower: %d\n", ldata.txpower));
cc2520_radio_set_txpower(ldata.txpower);
}

Expand Down Expand Up @@ -371,7 +371,7 @@ int cc2520_interface_init()
// Allocate a major number for this device
result = alloc_chrdev_region(&char_d_mm, 0, 1, cc2520_name);
if (result < 0) {
printk(KERN_INFO "[cc2520] - Could not allocate a major number\n");
ERR((KERN_INFO "[cc2520] - Could not allocate a major number\n"));
goto error;
}
major = MAJOR(char_d_mm);
Expand All @@ -381,21 +381,21 @@ int cc2520_interface_init()
char_d_cdev.owner = THIS_MODULE;
result = cdev_add(&char_d_cdev, char_d_mm, 1);
if (result < 0) {
printk(KERN_INFO "[cc2520] - Unable to register char dev\n");
ERR((KERN_INFO "[cc2520] - Unable to register char dev\n"));
goto error;
}
printk(KERN_INFO "[cc2520] - Char interface registered on %d\n", major);
INFO((KERN_INFO "[cc2520] - Char interface registered on %d\n", major));

cl = class_create(THIS_MODULE, "cc2520");
if (cl == NULL) {
printk(KERN_INFO "[cc2520] - Could not create device class\n");
ERR((KERN_INFO "[cc2520] - Could not create device class\n"));
goto error;
}

// Create the device in /dev/radio
de = device_create(cl, NULL, char_d_mm, NULL, "radio");
if (de == NULL) {
printk(KERN_INFO "[cc2520] - Could not create device\n");
ERR((KERN_INFO "[cc2520] - Could not create device\n"));
goto error;
}

Expand All @@ -422,12 +422,12 @@ void cc2520_interface_free()

result = down_interruptible(&tx_sem);
if (result) {
printk("[cc2520] - critical error occurred on free.");
ERR(("[cc2520] - critical error occurred on free."));
}

result = down_interruptible(&rx_sem);
if (result) {
printk("[cc2520] - critical error occurred on free.");
ERR(("[cc2520] - critical error occurred on free."));
}

cdev_del(&char_d_cdev);
Expand All @@ -436,7 +436,7 @@ void cc2520_interface_free()
class_destroy(cl);


printk(KERN_INFO "[cc2520] - Removed character device\n");
INFO((KERN_INFO "[cc2520] - Removed character device\n"));

if (rx_buf_c) {
kfree(rx_buf_c);
Expand All @@ -447,4 +447,4 @@ void cc2520_interface_free()
kfree(tx_buf_c);
tx_buf_c = 0;
}
}
}
4 changes: 2 additions & 2 deletions interface.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef INTERFACE_H
#define iNTERFACE_H
#define INTERFACE_H

// Interface
int cc2520_interface_init(void);
void cc2520_interface_free(void);

extern struct cc2520_interface *interface_bottom;

#endif
#endif
4 changes: 2 additions & 2 deletions ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct cc2520_set_csma_data {
};

struct cc2520_set_print_messages_data {
bool enabled;
u8 debug_level;
};

// Possible TX Powers:
Expand All @@ -66,4 +66,4 @@ struct cc2520_set_txpower_data {
#define CC2520_IO_RADIO_SET_ACK _IOW(BASE, 6, struct cc2520_set_ack_data)
#define CC2520_IO_RADIO_SET_LPL _IOW(BASE, 7, struct cc2520_set_lpl_data)
#define CC2520_IO_RADIO_SET_CSMA _IOW(BASE, 8, struct cc2520_set_csma_data)
#define CC2520_IO_RADIO_SET_PRINT _IOW(BASE, 9, struct cc2520_set_print_messages_data)
#define CC2520_IO_RADIO_SET_PRINT _IOW(BASE, 9, struct cc2520_set_print_messages_data)
7 changes: 4 additions & 3 deletions lpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "lpl.h"
#include "packet.h"
#include "cc2520.h"
#include "debug.h"

struct cc2520_interface *lpl_top;
struct cc2520_interface *lpl_bottom;
Expand Down Expand Up @@ -125,7 +126,7 @@ static void cc2520_lpl_tx_done(u8 status)
}
else {
spin_unlock_irqrestore(&state_sl, flags);
//printk(KERN_INFO "[cc2520] - lpl retransmit.\n");
DBG((KERN_INFO "[cc2520] - lpl retransmit.\n"));
lpl_bottom->tx(cur_tx_buf, cur_tx_len);
}
}
Expand All @@ -138,7 +139,7 @@ static void cc2520_lpl_tx_done(u8 status)
else {
spin_unlock_irqrestore(&state_sl, flags);
lpl_bottom->tx(cur_tx_buf, cur_tx_len);
}
}
}
}
else {
Expand Down Expand Up @@ -190,4 +191,4 @@ void cc2520_lpl_set_listen_length(int length)
void cc2520_lpl_set_wakeup_interval(int interval)
{
lpl_interval = interval;
}
}
20 changes: 13 additions & 7 deletions module.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <linux/module.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
Expand All @@ -17,9 +17,13 @@
#include "sack.h"
#include "csma.h"
#include "unique.h"
#include "debug.h"

#define DRIVER_AUTHOR "Andrew Robinson <[email protected]>"
#define DRIVER_DESC "A driver for the CC2520 radio. Be afraid."
#define DRIVER_AUTHOR "Andrew Robinson <[email protected]>"
#define DRIVER_DESC "A driver for the CC2520 radio. Be afraid."
#define DRIVER_VERSION "0.5"

uint8_t debug_print;

struct cc2520_state state;
const char cc2520_name[] = "cc2520";
Expand Down Expand Up @@ -48,11 +52,13 @@ int init_module()
{
int err = 0;

debug_print = DEBUG_PRINT_INFO;

setup_bindings();

memset(&state, 0, sizeof(struct cc2520_state));
INFO((KERN_INFO "loading CC2520 Kernel Module v0.01...\n"));

INFO((KERN_INFO "loading CC2520 Kernel Module v%s...\n", DRIVER_VERSION));

err = cc2520_plat_gpio_init();
if (err) {
Expand All @@ -69,7 +75,7 @@ int init_module()
err = cc2520_interface_init();
if (err) {
ERR((KERN_ALERT "[cc2520] - char driver error. aborting.\n"));
goto error5;
goto error5;
}

err = cc2520_radio_init();
Expand Down Expand Up @@ -135,4 +141,4 @@ void cleanup_module()

MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_DESCRIPTION(DRIVER_DESC);
7 changes: 4 additions & 3 deletions packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <linux/kernel.h>
#include "packet.h"
#include "cc2520.h"
#include "debug.h"

bool cc2520_packet_requires_ack_reply(u8 *buf)
{
Expand Down Expand Up @@ -89,8 +90,8 @@ u64 cc2520_packet_get_src(u8 *buf)
src_addr_offset += 10;
}
// WARNING: this is a weird edge case from the
// 802.15.4 spec.
// NOTE: Assuming we're on LE arch.
// 802.15.4 spec.
// NOTE: Assuming we're on LE arch.
else if (pan_compression) {
src_addr_offset += 2;
}
Expand All @@ -108,7 +109,7 @@ u64 cc2520_packet_get_src(u8 *buf)
memcpy(&ret, buf + src_addr_offset, 8);
}

INFO((KERN_INFO "[cc2520] - src_mode: %d dest_mode: %d pan_comp: %d src_addr_offset: %d\n",
DBG((KERN_INFO "[cc2520] - src_mode: %d dest_mode: %d pan_comp: %d src_addr_offset: %d\n",
src_addr_mode, dest_addr_mode, pan_compression, src_addr_offset));

return ret;
Expand Down
Loading

0 comments on commit a71694a

Please sign in to comment.