First round of coding style updates for GSM 07.10 implementation

This commit is contained in:
Marcel Holtmann 2009-09-06 00:08:53 +02:00
parent ec69d1abd0
commit f49e1f9f4f
2 changed files with 497 additions and 472 deletions

View File

@ -30,6 +30,24 @@
#include "gsm0710_p.h"
/* Frame types and subtypes */
#define GSM0710_OPEN_CHANNEL 0x3F
#define GSM0710_CLOSE_CHANNEL 0x53
#define GSM0710_DATA 0xEF
#define GSM0710_DATA_ALT 0x03
#define GSM0710_STATUS_SET 0xE3
#define GSM0710_STATUS_ACK 0xE1
#define GSM0710_TERMINATE_BYTE1 0xC3
#define GSM0710_TERMINATE_BYTE2 0x01
/* Status flags */
#define GSM0710_FC 0x02
#define GSM0710_DTR 0x04
#define GSM0710_DSR 0x04
#define GSM0710_RTS 0x08
#define GSM0710_CTS 0x08
#define GSM0710_DCD 0x80
/* Initialize a GSM 07.10 context, in preparation for startup */
void gsm0710_initialize(struct gsm0710_context *ctx)
{
@ -132,8 +150,8 @@ static const unsigned char crc_table[256] = {
static unsigned char gsm0710_compute_crc(const unsigned char *data, int len)
{
int sum = 0xFF;
while ( len > 0 ) {
sum = crc_table[ ( sum ^ *data++ ) & 0xFF ];
while (len > 0) {
sum = crc_table[(sum ^ *data++) & 0xFF];
--len;
}
return (~sum & 0xFF);
@ -153,7 +171,7 @@ static void gsm0710_write_frame(struct gsm0710_context *ctx, int channel,
frame[1] = ((channel << 2) | 0x03);
frame[2] = type;
crc = gsm0710_compute_crc(frame + 1, 2);
if ( type == 0x7E || type == 0x7D ) {
if (type == 0x7E || type == 0x7D) {
/* Need to quote the type field now that crc has been computed */
frame[2] = 0x7D;
frame[3] = (type ^ 0x20);
@ -161,17 +179,17 @@ static void gsm0710_write_frame(struct gsm0710_context *ctx, int channel,
} else {
size = 3;
}
while ( len > 0 ) {
while (len > 0) {
temp = *data++ & 0xFF;
--len;
if ( temp != 0x7E && temp != 0x7D ) {
if (temp != 0x7E && temp != 0x7D) {
frame[size++] = temp;
} else {
frame[size++] = 0x7D;
frame[size++] = (temp ^ 0x20);
}
}
if ( crc != 0x7E && crc != 0x7D ) {
if (crc != 0x7E && crc != 0x7D) {
frame[size++] = crc;
} else {
frame[size++] = 0x7D;
@ -219,19 +237,33 @@ int gsm0710_startup(struct gsm0710_context *ctx, int send_cmux)
if (send_cmux) {
int speed;
switch (ctx->port_speed) {
case 9600: speed = 1; break;
case 19200: speed = 2; break;
case 38400: speed = 3; break;
case 57600: speed = 4; break;
case 115200: speed = 5; break;
case 230400: speed = 6; break;
default: speed = 5; break;
case 9600:
speed = 1;
break;
case 19200:
speed = 2;
break;
case 38400:
speed = 3;
break;
case 57600:
speed = 4;
break;
case 115200:
speed = 5;
break;
case 230400:
speed = 6;
break;
default:
speed = 5;
break;
}
sprintf(command, "AT+CMUX=%d,0,%d,%d",
ctx->mode, speed, ctx->frame_size);
if (!ctx->at_command || !ctx->at_command(ctx, command)) {
gsm0710_debug
(ctx, "could not initialize multiplexing with AT+CMUX");
gsm0710_debug(ctx,
"could not initialize multiplexing with AT+CMUX");
return 0;
}
}
@ -244,9 +276,11 @@ int gsm0710_startup(struct gsm0710_context *ctx, int send_cmux)
layers to abort pending AT commands */
for (channel = 1; channel <= GSM0710_MAX_CHANNELS; ++channel) {
if (is_channel_used(ctx, channel)) {
gsm0710_write_frame(ctx, channel, GSM0710_OPEN_CHANNEL, 0, 0);
gsm0710_write_frame(ctx, channel,
GSM0710_OPEN_CHANNEL, 0, 0);
if (ctx->deliver_data)
ctx->deliver_data(ctx, channel, "\r\nERROR\r\n", 9);
ctx->deliver_data(ctx, channel,
"\r\nERROR\r\n", 9);
}
}
return 1;
@ -255,13 +289,14 @@ int gsm0710_startup(struct gsm0710_context *ctx, int send_cmux)
/* Shut down the GSM 07.10 session, closing all channels */
void gsm0710_shutdown(struct gsm0710_context *ctx)
{
static const unsigned char terminate[2] =
{GSM0710_TERMINATE_BYTE1, GSM0710_TERMINATE_BYTE2};
static const unsigned char terminate[2] = { GSM0710_TERMINATE_BYTE1,
GSM0710_TERMINATE_BYTE2 };
if (!ctx->server) {
int channel;
for (channel = 1; channel <= GSM0710_MAX_CHANNELS; ++channel) {
if(is_channel_used(ctx, channel)) {
gsm0710_write_frame(ctx, channel, GSM0710_CLOSE_CHANNEL, 0, 0);
if (is_channel_used(ctx, channel)) {
gsm0710_write_frame(ctx, channel,
GSM0710_CLOSE_CHANNEL, 0, 0);
}
}
gsm0710_write_frame(ctx, 0, GSM0710_DATA, terminate, 2);
@ -303,8 +338,8 @@ int gsm0710_is_channel_open(struct gsm0710_context *ctx, int channel)
}
/* Process an incoming GSM 07.10 packet */
static int gsm0710_packet( struct gsm0710_context *ctx, int channel, int type,
const unsigned char *data, int len )
static int gsm0710_packet(struct gsm0710_context *ctx, int channel, int type,
const unsigned char *data, int len)
{
if (ctx->packet_filter &&
ctx->packet_filter(ctx, channel, type, data, len)) {
@ -321,7 +356,8 @@ static int gsm0710_packet( struct gsm0710_context *ctx, int channel, int type,
} else if (channel == 0) {
/* An embedded command or response on channel 0 */
if (len >= 2 && data[0] == GSM0710_STATUS_SET) {
return gsm0710_packet(ctx, channel, GSM0710_STATUS_ACK,
return gsm0710_packet(ctx, channel,
GSM0710_STATUS_ACK,
data + 2, len - 2);
} else if (len >= 2 && data[0] == 0xC3 && ctx->server) {
/* Incoming terminate request on server side */
@ -331,7 +367,8 @@ static int gsm0710_packet( struct gsm0710_context *ctx, int channel, int type,
ctx->close_channel(ctx, channel);
}
}
memset(ctx->used_channels, 0, sizeof(ctx->used_channels));
memset(ctx->used_channels, 0,
sizeof(ctx->used_channels));
if (ctx->terminate)
ctx->terminate(ctx);
return 0;
@ -340,7 +377,8 @@ static int gsm0710_packet( struct gsm0710_context *ctx, int channel, int type,
unsigned char *resp = alloca(len);
memcpy(resp, data, len);
resp[0] = 0x41; /* Clear the C/R bit in the response */
gsm0710_write_frame(ctx, 0, GSM0710_DATA, resp, len);
gsm0710_write_frame(ctx, 0, GSM0710_DATA,
resp, len);
}
}
@ -355,13 +393,15 @@ static int gsm0710_packet( struct gsm0710_context *ctx, int channel, int type,
if (channel >= 1 && channel <= GSM0710_MAX_CHANNELS &&
is_channel_used(ctx, channel)) {
if (ctx->deliver_status)
ctx->deliver_status(ctx, channel, data[1] & 0xFF);
ctx->deliver_status(ctx, channel,
data[1] & 0xFF);
}
}
/* Send the response to the status change request to ACK it */
gsm0710_debug(ctx, "received status line signal, sending response");
if ( len > 31 )
gsm0710_debug(ctx,
"received status line signal, sending response");
if (len > 31)
len = 31;
resp[0] = GSM0710_STATUS_ACK;
resp[1] = ((len << 1) | 0x01);
@ -405,7 +445,7 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
return;
len = ctx->read(ctx, ctx->buffer + ctx->buffer_used,
sizeof(ctx->buffer) - ctx->buffer_used);
if ( len <= 0 )
if (len <= 0)
return;
/* Update the buffer size */
@ -436,7 +476,7 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
/* The low bit of the second byte should be 1,
which indicates a short channel number */
if ((ctx->buffer[posn + 1] & 0x01 ) == 0) {
if ((ctx->buffer[posn + 1] & 0x01) == 0) {
++posn;
continue;
}
@ -453,14 +493,14 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
len |= ((int)(ctx->buffer[posn + 4])) << 7;
header_size = 4;
}
if ((posn + header_size + 2 + len) > ctx->buffer_used )
if ((posn + header_size + 2 + len) > ctx->buffer_used)
break;
/* Verify the packet header checksum */
if (((gsm0710_compute_crc(ctx->buffer + posn + 1, header_size) ^
ctx->buffer[posn + len + header_size + 1]) & 0xFF)
!= 0) {
gsm0710_debug(ctx, "*** GSM 07.10 checksum check failed ***");
if (((gsm0710_compute_crc(ctx->buffer + posn + 1,
header_size) ^ ctx->buffer[posn + len + header_size + 1]) & 0xFF) != 0) {
gsm0710_debug(ctx,
"*** GSM 07.10 checksum check failed ***");
posn += len + header_size + 2;
continue;
}
@ -494,7 +534,7 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
}
if (len >= ctx->buffer_used) {
/* There are insufficient bytes for a packet at present */
if ( posn == 0 && len >= (int)sizeof( ctx->buffer ) ) {
if (posn == 0 && len >= (int)sizeof(ctx->buffer)) {
/* The buffer is full and we were unable to find a
legitimate packet. Discard the buffer and restart */
posn = len;
@ -508,7 +548,7 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
while (posn < len) {
if (ctx->buffer[posn] == 0x7D) {
++posn;
if ( posn >= len)
if (posn >= len)
break;
ctx->buffer[posn2++] = (ctx->buffer[posn++] ^ 0x20);
} else {
@ -519,13 +559,15 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
/* Validate the checksum on the packet header */
if (posn2 >= 3) {
if (((gsm0710_compute_crc(ctx->buffer, 2) ^
ctx->buffer[posn2 - 1]) & 0xFF) != 0 ) {
gsm0710_debug(ctx, "*** GSM 07.10 advanced checksum "
ctx->buffer[posn2 - 1]) & 0xFF) != 0) {
gsm0710_debug(ctx,
"*** GSM 07.10 advanced checksum "
"check failed ***");
continue;
}
} else {
gsm0710_debug(ctx, "*** GSM 07.10 advanced packet "
gsm0710_debug(ctx,
"*** GSM 07.10 advanced packet "
"is too small ***");
continue;
}
@ -544,8 +586,9 @@ void gsm0710_ready_read(struct gsm0710_context *ctx)
++posn;
}
}
if ( posn < ctx->buffer_used ) {
memmove( ctx->buffer, ctx->buffer + posn, ctx->buffer_used - posn );
if (posn < ctx->buffer_used) {
memmove(ctx->buffer, ctx->buffer + posn,
ctx->buffer_used - posn);
ctx->buffer_used -= posn;
} else {
ctx->buffer_used = 0;
@ -563,7 +606,7 @@ void gsm0710_write_data(struct gsm0710_context *ctx, int channel,
if (temp > ctx->frame_size)
temp = ctx->frame_size;
gsm0710_write_frame(ctx, channel, GSM0710_DATA, data, temp);
data = ((const unsigned char *)data) + temp;
data = ((const unsigned char *) data) + temp;
len -= temp;
}
}

View File

@ -34,24 +34,6 @@ extern "C" {
#define GSM0710_MODE_BASIC 0
#define GSM0710_MODE_ADVANCED 1
/* Frame types and subtypes */
#define GSM0710_OPEN_CHANNEL 0x3F
#define GSM0710_CLOSE_CHANNEL 0x53
#define GSM0710_DATA 0xEF
#define GSM0710_DATA_ALT 0x03
#define GSM0710_STATUS_SET 0xE3
#define GSM0710_STATUS_ACK 0xE1
#define GSM0710_TERMINATE_BYTE1 0xC3
#define GSM0710_TERMINATE_BYTE2 0x01
/* Status flags */
#define GSM0710_FC 0x02
#define GSM0710_DTR 0x04
#define GSM0710_DSR 0x04
#define GSM0710_RTS 0x08
#define GSM0710_CTS 0x08
#define GSM0710_DCD 0x80
struct gsm0710_context
{
/* GSM 07.10 implementation details */