Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b73db547 authored by Rashika Kheria's avatar Rashika Kheria Committed by Greg Kroah-Hartman
Browse files

Staging: dgrp: Refactor the function dgrp_receive() in drrp_net_ops.c



The function dgrp_receive() in dgrp_net_ops.c is too long and can be
refactored. It uses various switch statements and goto labels. I have
removed a label called data and tried to extract a new function out of
it called as handle_data_in_packet().

This helps to make the code more modularize and simple to read and
understand.

Signed-off-by: default avatarRashika Kheria <rashika.kheria@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent c79bfed6
Loading
Loading
Loading
Loading
+175 −155
Original line number Diff line number Diff line
@@ -2232,114 +2232,20 @@ static ssize_t dgrp_net_read(struct file *file, char __user *buf, size_t count,
	return rtn;
}

/**
 * dgrp_receive() -- decode data packets received from the remote PortServer.
 * @nd: pointer to a node structure
/*
 * Common Packet Handling code
 */
static void dgrp_receive(struct nd_struct *nd)

static void handle_data_in_packet(struct nd_struct *nd, struct ch_struct *ch,
				  long dlen, long plen, int n1, u8 *dbuf)
{
	struct ch_struct *ch;
	u8 *buf;
	u8 *b;
	u8 *dbuf;
	char *error;
	long port;
	long dlen;
	long plen;
	long remain;
	long n;
	long mlast;
	long elast;
	long mstat;
	long estat;

	char ID[3];

	nd->nd_tx_time = jiffies;

	ID_TO_CHAR(nd->nd_ID, ID);
	long remain;
	u8 *buf;
	u8 *b;

	b = buf = nd->nd_iobuf;
	remain = nd->nd_remain;

	/*
	 *  Loop to process Realport protocol packets.
	 */

	while (remain > 0) {
		int n0 = b[0] >> 4;
		int n1 = b[0] & 0x0f;

		if (n0 <= 12) {
			port = (nd->nd_rx_module << 4) + n1;

			if (port >= nd->nd_chan_count) {
				error = "Improper Port Number";
				goto prot_error;
			}

			ch = nd->nd_chan + port;
		} else {
			port = -1;
			ch = NULL;
		}

		/*
		 *  Process by major packet type.
		 */

		switch (n0) {

		/*
		 *  Process 1-byte header data packet.
		 */

		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
			dlen = n0 + 1;
			plen = dlen + 1;

			dbuf = b + 1;
			goto data;

		/*
		 *  Process 2-byte header data packet.
		 */

		case 8:
			if (remain < 3)
				goto done;

			dlen = b[1];
			plen = dlen + 2;

			dbuf = b + 2;
			goto data;

		/*
		 *  Process 3-byte header data packet.
		 */

		case 9:
			if (remain < 4)
				goto done;

			dlen = get_unaligned_be16(b + 1);
			plen = dlen + 3;

			dbuf = b + 3;

		/*
		 *  Common packet handling code.
		 */

data:
	nd->nd_tx_work = 1;

	/*
@@ -2349,7 +2255,9 @@ static void dgrp_receive(struct nd_struct *nd)

	if (ch->ch_state < CS_READY) {
		error = "Data received before RWIN established";
				goto prot_error;
		nd->nd_remain = 0;
		nd->nd_state = NS_SEND_ERROR;
		nd->nd_error = error;
	}

	/*
@@ -2361,7 +2269,9 @@ static void dgrp_receive(struct nd_struct *nd)

	if (dlen > n) {
		error = "Receive data overrun";
				goto prot_error;
		nd->nd_remain = 0;
		nd->nd_state = NS_SEND_ERROR;
		nd->nd_error = error;
	}

	/*
@@ -2468,7 +2378,6 @@ static void dgrp_receive(struct nd_struct *nd)

		if ((ch->ch_flag & CH_INPUT) != 0) {
			ch->ch_flag &= ~CH_INPUT;

			wake_up_interruptible(&ch->ch_flag_wait);
		}
	}
@@ -2486,8 +2395,119 @@ static void dgrp_receive(struct nd_struct *nd)
		put_unaligned_be16(dlen, b + 1);

		remain = 3;
				goto done;
		if (remain > 0 && b != buf)
			memcpy(buf, b, remain);

		nd->nd_remain = remain;
		return;
	}
}

/**
 * dgrp_receive() -- decode data packets received from the remote PortServer.
 * @nd: pointer to a node structure
 */
static void dgrp_receive(struct nd_struct *nd)
{
	struct ch_struct *ch;
	u8 *buf;
	u8 *b;
	u8 *dbuf;
	char *error;
	long port;
	long dlen;
	long plen;
	long remain;
	long n;
	long mlast;
	long elast;
	long mstat;
	long estat;

	char ID[3];

	nd->nd_tx_time = jiffies;

	ID_TO_CHAR(nd->nd_ID, ID);

	b = buf = nd->nd_iobuf;
	remain = nd->nd_remain;

	/*
	 *  Loop to process Realport protocol packets.
	 */

	while (remain > 0) {
		int n0 = b[0] >> 4;
		int n1 = b[0] & 0x0f;

		if (n0 <= 12) {
			port = (nd->nd_rx_module << 4) + n1;

			if (port >= nd->nd_chan_count) {
				error = "Improper Port Number";
				goto prot_error;
			}

			ch = nd->nd_chan + port;
		} else {
			port = -1;
			ch = NULL;
		}

		/*
		 *  Process by major packet type.
		 */

		switch (n0) {

		/*
		 *  Process 1-byte header data packet.
		 */

		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
			dlen = n0 + 1;
			plen = dlen + 1;

			dbuf = b + 1;
			handle_data_in_packet(nd, ch, dlen, plen, n1, dbuf);
			break;

		/*
		 *  Process 2-byte header data packet.
		 */

		case 8:
			if (remain < 3)
				goto done;

			dlen = b[1];
			plen = dlen + 2;

			dbuf = b + 2;
			handle_data_in_packet(nd, ch, dlen, plen, n1, dbuf);
			break;

		/*
		 *  Process 3-byte header data packet.
		 */

		case 9:
			if (remain < 4)
				goto done;

			dlen = get_unaligned_be16(b + 1);
			plen = dlen + 3;

			dbuf = b + 3;

			break;

		/*