[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
next step to multiple phones support
From: |
Ladislav Michl |
Subject: |
next step to multiple phones support |
Date: |
Fri, 13 Dec 2002 14:53:03 +0100 |
User-agent: |
Mutt/1.2.5i |
hi,
following patch moves link related static variables into state
(completely done for atbus only). i'll do it also for other links
if you like this way. please comment/improve it.
ladis
PS gn_statemachine is randomly passed as first or last parameter. this
makes some troubles to me, because i can't cache too many things in memory
and therefore i have to look at headers very often ;). i'd like to see
gn_statemachine as last (first) parameter everywhere. objections?
PS2 nice to see cvs compile again :)
Index: common/gsm-statemachine.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/gsm-statemachine.c,v
retrieving revision 1.36
diff -u -r1.36 gsm-statemachine.c
--- common/gsm-statemachine.c 12 Dec 2002 15:07:19 -0000 1.36
+++ common/gsm-statemachine.c 13 Dec 2002 13:08:27 -0000
@@ -55,7 +55,7 @@
state->current_state = GN_SM_MessageSent;
/* FIXME - clear KeepAlive timer */
- return state->link.send_message(messagesize, messagetype,
message);
+ return state->link.send_message(messagesize, messagetype,
message, state);
}
else return GN_ERR_NOTREADY;
}
@@ -77,7 +77,7 @@
loop_timeout.tv_sec = 0;
loop_timeout.tv_usec = 100000;
- state->link.loop(&loop_timeout);
+ state->link.loop(&loop_timeout, state);
}
/* FIXME - add calling a KeepAlive function here */
Index: common/links/atbus.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/atbus.c,v
retrieving revision 1.22
diff -u -r1.22 atbus.c
--- common/links/atbus.c 13 Dec 2002 08:38:45 -0000 1.22
+++ common/links/atbus.c 13 Dec 2002 13:08:27 -0000
@@ -44,29 +44,13 @@
#include "gnokii-internal.h"
-static gn_error atbus_loop(struct timeval *timeout);
-static bool atbus_serial_open(int mode, char *device);
-static void atbus_rx_statemachine(unsigned char rx_char);
-
-/* FIXME - pass device_* the link stuff?? */
-/* FIXME - win32 stuff! */
-
static void at_printf(char *prefix, char *buf, int len);
-/* FIXME - when sending an AT command while another one is still in */
-/* progress, the old command is aborted and the new ignored. */
-/* the result is _one_ error message from the phone. */
-
-/* Some globals */
-static struct gn_statemachine *statemachine;
-
-/* The buffer for phone responses not only holds the data from
-the phone but also a byte which holds the compiled status of the
-response. it is placed at [0]. */
-static char reply_buf[1024];
-static int reply_buf_pos = 1;
-static int binlength = 1;
-
+/*
+ * FIXME - when sending an AT command while another one is still in progress,
+ * the old command is aborted and the new ignored. the result is _one_ error
+ * message from the phone.
+ */
static int xwrite(unsigned char *d, int len)
{
@@ -89,56 +73,6 @@
return 0;
}
-static gn_error at_send_message(u16 message_length, u8 message_type, unsigned
char *msg)
-{
- usleep(10000);
- return xwrite(msg, message_length) ? GN_ERR_UNKNOWN : GN_ERR_NONE;
-}
-
-/*
- * rx state machine for receive handling. called once for each character
- * received from the phone.
- */
-static void atbus_rx_statemachine(unsigned char rx_char)
-{
- reply_buf[reply_buf_pos++] = rx_char;
- reply_buf[reply_buf_pos] = '\0';
-
- if (reply_buf_pos < binlength)
- return;
-
- reply_buf[0] = GN_AT_NONE;
- /* first check if <cr><lf> is found at end of reply_buf.
- * attention: the needed length is greater 2 because we
- * don't need to enter if no result/error will be found. */
- if ((reply_buf_pos > 4) && (!strncmp(reply_buf+reply_buf_pos-2, "\r\n",
2))) {
- /* no lenght check needed */
- if (!strncmp(reply_buf+reply_buf_pos-4, "OK\r\n", 4))
- reply_buf[0] = GN_AT_OK;
- else if ((reply_buf_pos > 7) &&
(!strncmp(reply_buf+reply_buf_pos-7, "ERROR\r\n", 7)))
- reply_buf[0] = GN_AT_ERROR;
- }
- /* check if SMS prompt is found */
- if ((reply_buf_pos > 4) && (!strncmp(reply_buf+reply_buf_pos-4, "\r\n>
", 4)))
- reply_buf[0] = GN_AT_PROMPT;
- if (reply_buf[0] != GN_AT_NONE) {
- at_printf("read : ", reply_buf + 1, reply_buf_pos - 1);
- sm_incoming_function(statemachine, statemachine->last_msg_type,
reply_buf, reply_buf_pos - 1);
- reply_buf_pos = 1;
- binlength = 1;
- return;
- }
-#if 0
- /* needed for binary date etc */
- TODO: correct reading of variable length integers
- if (reply_buf_pos == 12) {
- if (!strncmp(reply_buf + 3, "ABC", 3) {
- binlength = atoi(reply_buf + 8);
- }
- }
-#endif
-}
-
static bool atbus_serial_open(int mode, char *device)
{
int result = device_open(device, false, false, mode, GN_CT_Serial);
@@ -175,7 +109,59 @@
return true;
}
-static gn_error atbus_loop(struct timeval *timeout)
+static gn_error at_send_message(u16 message_length, u8 message_type, unsigned
char *msg, struct gn_statemachine *sm)
+{
+ usleep(10000);
+ return xwrite(msg, message_length) ? GN_ERR_UNKNOWN : GN_ERR_NONE;
+}
+
+/*
+ * rx state machine for receive handling. called once for each character
+ * received from the phone.
+ */
+static void atbus_rx_statemachine(unsigned char rx_char, struct
gn_statemachine *sm)
+{
+ atbus_instance *bi = AT_BUSINST(sm);
+
+ bi->rbuf[bi->rbuf_pos++] = rx_char;
+ bi->rbuf[bi->rbuf_pos] = '\0';
+
+ if (bi->rbuf_pos < bi->binlen)
+ return;
+
+ bi->rbuf[0] = GN_AT_NONE;
+ /* first check if <cr><lf> is found at end of reply_buf.
+ * attention: the needed length is greater 2 because we
+ * don't need to enter if no result/error will be found. */
+ if (bi->rbuf_pos > 4 && !strncmp(bi->rbuf + bi->rbuf_pos - 2, "\r\n",
2)) {
+ /* no lenght check needed */
+ if (!strncmp(bi->rbuf + bi->rbuf_pos - 4, "OK\r\n", 4))
+ bi->rbuf[0] = GN_AT_OK;
+ else if (bi->rbuf_pos > 7 && !strncmp(bi->rbuf + bi->rbuf_pos -
7, "ERROR\r\n", 7))
+ bi->rbuf[0] = GN_AT_ERROR;
+ }
+ /* check if SMS prompt is found */
+ if (bi->rbuf_pos > 4 && !strncmp(bi->rbuf + bi->rbuf_pos - 4, "\r\n> ",
4))
+ bi->rbuf[0] = GN_AT_PROMPT;
+ if (bi->rbuf[0] != GN_AT_NONE) {
+ at_printf("read : ", bi->rbuf + 1, bi->rbuf_pos - 1);
+ sm_incoming_function(sm, sm->last_msg_type, bi->rbuf,
bi->rbuf_pos - 1);
+ bi->rbuf_pos = 1;
+ bi->binlen = 1;
+ return;
+ }
+#if 0
+ /* needed for binary date etc */
+ TODO: correct reading of variable length integers
+ if (reply_buf_pos == 12) {
+ if (!strncmp(reply_buf + 3, "ABC", 3) {
+ binlength = atoi(reply_buf + 8);
+ }
+ }
+#endif
+}
+
+static gn_error atbus_loop(struct timeval *timeout, struct gn_statemachine *sm)
{
unsigned char buffer[255];
int count, res;
@@ -184,7 +170,7 @@
if (res > 0) {
res = device_read(buffer, 255);
for (count = 0; count < res; count++)
- atbus_rx_statemachine(buffer[count]);
+ atbus_rx_statemachine(buffer[count], sm);
} else
return GN_ERR_TIMEOUT;
/* This traps errors from device_read */
@@ -200,23 +186,37 @@
/* bug reports. this is pretty silly for /dev/ttyS?. */
gn_error atbus_initialise(struct gn_statemachine *state, int mode)
{
- /* 'Copy in' the global structures */
- statemachine = state;
+ int error = GN_ERR_NONE;
+ atbus_instance *businst;
+
+ if (!(businst = malloc(sizeof(atbus_instance))))
+ return GN_ERR_FAILED;
/* Fill in the link functions */
state->link.loop = &atbus_loop;
state->link.send_message = &at_send_message;
+ businst->rbuf_pos = 1;
+ businst->binlen = 1;
+ AT_BUSINST(state) = businst;
if ((state->link.connection_type == GN_CT_Serial) ||
(state->link.connection_type == GN_CT_Irda)) {
- if (!atbus_serial_open(mode, state->link.port_device))
- return GN_ERR_FAILED;
+ if (!atbus_serial_open(mode, state->link.port_device)) {
+ error = GN_ERR_FAILED;
+ goto out;
+ }
} else {
- dprintf("Device not supported by ATBUS\n");
- return GN_ERR_FAILED;
+ dprintf("Device not supported by AT bus\n");
+ error = GN_ERR_FAILED;
+ goto out;
+ }
+out:
+ if (error) {
+ dprintf("AT bus initialization failed (%d)\n", error);
+ free(AT_BUSINST(state));
+ AT_BUSINST(state) = NULL;
}
-
- return GN_ERR_NONE;
+ return error;
}
static void at_printf(char *prefix, char *buf, int len)
Index: common/links/cbus.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/cbus.c,v
retrieving revision 1.20
diff -u -r1.20 cbus.c
--- common/links/cbus.c 9 Dec 2002 15:27:20 -0000 1.20
+++ common/links/cbus.c 13 Dec 2002 13:08:28 -0000
@@ -460,7 +460,7 @@
return send_packet_ack("\x3e\x68", 2, 0x3f, false);
}
-static gn_error at_message_send(u16 message_length, u8 message_type, unsigned
char *buffer)
+static gn_error at_send_message(u16 message_length, u8 message_type, unsigned
char *buffer, struct gn_statemachine *state)
{
gn_error res;
@@ -493,7 +493,7 @@
* This is the main loop function which must be called regularly
* timeout can be used to make it 'busy' or not
*/
-static gn_error cbus_loop(struct timeval *timeout)
+static gn_error cbus_loop(struct timeval *timeout, struct gn_statemachine
*state)
{
/* SM_IncomingFunction(statemachine, statemachine->LastMsgType,
clink.AT_message, clink.AT_message_len);*/
return GN_ERR_NONE;
@@ -508,7 +508,7 @@
/* Fill in the link functions */
state->link.loop = &cbus_loop;
- state->link.send_message = &at_message_send;
+ state->link.send_message = &at_send_message;
if (state->link.connection_type == GN_CT_Serial) {
if (!cbus_open_serial(state->link.port_device))
Index: common/links/fbus-3110.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/fbus-3110.c,v
retrieving revision 1.13
diff -u -r1.13 fbus-3110.c
--- common/links/fbus-3110.c 10 Dec 2002 11:20:32 -0000 1.13
+++ common/links/fbus-3110.c 13 Dec 2002 13:08:28 -0000
@@ -49,7 +49,7 @@
static bool fb3110_serial_open(void);
static void fb3110_rx_state_machine(unsigned char rx_byte);
static gn_error fb3110_tx_frame_send(u8 message_length, u8 message_type, u8
sequence_byte, u8 *buffer);
-static gn_error fb3110_message_send(u16 messagesize, u8 messagetype, unsigned
char *message);
+static gn_error fb3110_message_send(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state);
static void fb3110_tx_ack_send(u8 *message, int length);
static void fb3110_sequence_number_update(void);
@@ -173,7 +173,7 @@
* This is the main loop function which must be called regularly
* timeout can be used to make it 'busy' or not
*/
-static gn_error fb3110_loop(struct timeval *timeout)
+static gn_error fb3110_loop(struct timeval *timeout, struct gn_statemachine
*state)
{
unsigned char buffer[255];
int count, res;
@@ -249,7 +249,7 @@
/*
* Main function to send an fbus message
*/
-static gn_error fb3110_message_send(u16 messagesize, u8 messagetype, unsigned
char *message)
+static gn_error fb3110_message_send(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state)
{
u8 seqnum;
Index: common/links/fbus-phonet.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/fbus-phonet.c,v
retrieving revision 1.19
diff -u -r1.19 fbus-phonet.c
--- common/links/fbus-phonet.c 10 Dec 2002 11:05:56 -0000 1.19
+++ common/links/fbus-phonet.c 13 Dec 2002 13:08:28 -0000
@@ -52,7 +52,7 @@
#include "gnokii-internal.h"
static void phonet_rx_statemachine(unsigned char rx_byte);
-static gn_error phonet_send_message(u16 messagesize, u8 messagetype, unsigned
char *message);
+static gn_error phonet_send_message(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state);
/* FIXME - pass device_* the link stuff?? */
/* FIXME - win32 stuff! */
@@ -172,7 +172,7 @@
/* This is the main loop function which must be called regularly */
/* timeout can be used to make it 'busy' or not */
-static gn_error phonet_loop(struct timeval *timeout)
+static gn_error phonet_loop(struct timeval *timeout, struct gn_statemachine
*state)
{
gn_error error = GN_ERR_INTERNALERROR;
unsigned char buffer[255];
@@ -197,7 +197,8 @@
/* Main function to send an fbus message */
-static gn_error phonet_send_message(u16 messagesize, u8 messagetype, unsigned
char *message) {
+static gn_error phonet_send_message(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state)
+{
u8 out_buffer[PHONET_TRANSMIT_MAX_LENGTH + 5];
int current = 0;
Index: common/links/fbus.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/fbus.c,v
retrieving revision 1.33
diff -u -r1.33 fbus.c
--- common/links/fbus.c 10 Dec 2002 12:51:33 -0000 1.33
+++ common/links/fbus.c 13 Dec 2002 13:08:29 -0000
@@ -52,7 +52,7 @@
#include "gnokii-internal.h"
static void fbus_rx_statemachine(unsigned char rx_byte);
-static gn_error fbus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message);
+static gn_error fbus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state);
static int fbus_tx_send_ack(u8 message_type, u8 message_seq);
/* FIXME - pass device_* the link stuff?? */
@@ -72,7 +72,7 @@
/*--------------------------------------------*/
-static bool fbus_serial_open(bool dlr3)
+static bool fbus_serial_open(bool dlr3, struct gn_statemachine *state)
{
if (dlr3) dlr3 = 1;
/* Open device. */
@@ -88,7 +88,7 @@
return true;
}
-static bool at2fbus_serial_open()
+static bool at2fbus_serial_open(struct gn_statemachine *state)
{
unsigned char init_char = 0x55;
unsigned char end_init_char = 0xc1;
@@ -127,7 +127,7 @@
return true;
}
-static bool fbus_ir_open(void)
+static bool fbus_ir_open(struct gn_statemachine *state)
{
struct timeval timeout;
unsigned char init_char = 0x55;
@@ -156,7 +156,7 @@
device_changespeed(115200);
- fbus_send_message(7, 0x02, connect_seq);
+ fbus_send_message(7, 0x02, connect_seq, state);
/* Wait for 1 sec. */
timeout.tv_sec = 1;
@@ -378,7 +378,7 @@
/* This is the main loop function which must be called regularly */
/* timeout can be used to make it 'busy' or not */
-static gn_error fbus_loop(struct timeval *timeout)
+static gn_error fbus_loop(struct timeval *timeout, struct gn_statemachine
*state)
{
unsigned char buffer[255];
int count, res;
@@ -469,7 +469,7 @@
/* Main function to send an fbus message */
/* Splits up the message into frames if necessary */
-static gn_error fbus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message)
+static gn_error fbus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state)
{
u8 seqnum, frame_buffer[FBUS_CONTENT_MAX_LENGTH + 2];
u8 nom, lml; /* number of messages, last message len */
@@ -559,17 +559,17 @@
#ifndef WIN32
case GN_CT_Tekram:
#endif
- if (!fbus_ir_open())
+ if (!fbus_ir_open(state))
return GN_ERR_FAILED;
break;
case GN_CT_Serial:
switch (try) {
case 0:
case 1:
- err = fbus_serial_open(1 - try);
+ err = fbus_serial_open(1 - try, state);
break;
case 2:
- err = at2fbus_serial_open();
+ err = at2fbus_serial_open(state);
break;
default:
return GN_ERR_FAILED;
@@ -577,16 +577,16 @@
if (!err) return GN_ERR_FAILED;
break;
case GN_CT_DAU9P:
- if (!fbus_serial_open(0))
+ if (!fbus_serial_open(0, state))
return GN_ERR_FAILED;
break;
case GN_CT_DLR3P:
switch (try) {
case 0:
- err = at2fbus_serial_open();
+ err = at2fbus_serial_open(state);
break;
case 1:
- err = fbus_serial_open(1);
+ err = fbus_serial_open(1, state);
break;
default:
return GN_ERR_FAILED;
Index: common/links/m2bus.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/m2bus.c,v
retrieving revision 1.6
diff -u -r1.6 m2bus.c
--- common/links/m2bus.c 10 Dec 2002 11:05:56 -0000 1.6
+++ common/links/m2bus.c 13 Dec 2002 13:08:29 -0000
@@ -53,7 +53,7 @@
#include "gnokii-internal.h"
static void m2bus_rx_statemachine(unsigned char rx_byte);
-static gn_error m2bus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message);
+static gn_error m2bus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state);
static int m2bus_tx_send_ack(u8 message_seq);
/* FIXME - pass device_* the link stuff?? */
@@ -261,7 +261,7 @@
/* This is the main loop function which must be called regularly */
/* timeout can be used to make it 'busy' or not */
-static gn_error m2bus_loop(struct timeval *timeout)
+static gn_error m2bus_loop(struct timeval *timeout, struct gn_statemachine
*state)
{
unsigned char buffer[255];
int count, res;
@@ -306,7 +306,7 @@
(0x1f) and other values according the value specified when called.
Calculates checksum and then sends the lot down the pipe... */
-static gn_error m2bus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message)
+static gn_error m2bus_send_message(u16 messagesize, u8 messagetype, unsigned
char *message, struct gn_statemachine *state)
{
u8 *out_buffer;
int count, i = 0;
Index: common/links/utils.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/links/utils.c,v
retrieving revision 1.11
diff -u -r1.11 utils.c
--- common/links/utils.c 9 Dec 2002 15:27:20 -0000 1.11
+++ common/links/utils.c 13 Dec 2002 13:08:29 -0000
@@ -42,6 +42,8 @@
gn_error link_terminate(struct gn_statemachine *state)
{
/* device_close(&(state->Device)); */
+ if (state->link.link_instance)
+ free(state->link.link_instance);
device_close();
return GN_ERR_NONE; /* FIXME */
}
Index: include/gsm-data.h
===================================================================
RCS file: /cvsroot/gnokii/gnokii/include/gsm-data.h,v
retrieving revision 1.48
diff -u -r1.48 gsm-data.h
--- include/gsm-data.h 9 Dec 2002 00:24:16 -0000 1.48
+++ include/gsm-data.h 13 Dec 2002 13:08:29 -0000
@@ -91,22 +91,28 @@
gn_phone_model *phone;
} gn_data;
-/* A structure to hold information about the particular link */
-/* The link comes 'under' the phone */
+/*
+ * A structure to hold information about the particular link
+ * The link comes 'under' the phone
+ */
typedef struct {
- char port_device[GN_DEVICE_NAME_MAX_LENGTH]; /* The port device */
- int init_length; /* Number of chars sent
to sync the serial port */
- unsigned int sms_timeout; /* SMS timeout: how many
seconds should we wait
- for the SMSC response.
Defaults to 10 seconds */
- gn_connection_type connection_type; /* Connection type,
serial, ir etc */
-
- /* A regularly called loop function */
- /* timeout can be used to make the function block or not */
- gn_error (*loop)(struct timeval *timeout);
-
- /* A pointer to the function used to send out a message */
- /* This is used by the phone specific code to send a message over the
link */
- gn_error (*send_message)(u16 messagesize, u8 messagetype, unsigned char
*message);
+ /* The port device */
+ char port_device[GN_DEVICE_NAME_MAX_LENGTH];
+ /* Number of chars sent to sync the serial port */
+ int init_length;
+ /* SMS timeout: how many seconds should we wait for the SMSC response.
+ * Defaults to 10 seconds */
+ unsigned int sms_timeout;
+ /* Connection type, serial, ir etc */
+ gn_connection_type connection_type;
+ /* A regularly called loop function. Timeout can be used to make the
+ * function block or not */
+ gn_error (*loop)(struct timeval *timeout, struct gn_statemachine
*state);
+ /* A pointer to the function used to send out a message. This is used
+ * by the phone specific code to send a message over the link */
+ gn_error (*send_message)(u16 messagesize, u8 messagetype, unsigned char
*message,
+ struct gn_statemachine *state);
+ void *link_instance;
} gn_link;
typedef enum {
Index: include/links/atbus.h
===================================================================
RCS file: /cvsroot/gnokii/gnokii/include/links/atbus.h,v
retrieving revision 1.10
diff -u -r1.10 atbus.h
--- include/links/atbus.h 9 Dec 2002 15:27:19 -0000 1.10
+++ include/links/atbus.h 13 Dec 2002 13:08:29 -0000
@@ -33,8 +33,10 @@
gn_error atbus_initialise(struct gn_statemachine *state, int mode);
-/* Define some result/error codes internal to the AT command functions.
- Also define a code for an unterminated message. */
+/*
+ * Define some result/error codes internal to the AT command functions.
+ * Also define a code for an unterminated message.
+ */
typedef enum {
GN_AT_NONE, /* NO or unknown result code */
GN_AT_PROMPT, /* SMS command waiting for input */
@@ -43,5 +45,16 @@
GN_AT_CMS, /* SMS Command failed */
GN_AT_CME, /* Extended error code found */
} at_result;
+
+typedef struct {
+ /* The buffer for phone responses not only holds the data from the
+ * phone but also a byte which holds the compiled status of the
+ * response. it is placed at [0]. */
+ char rbuf[1024];
+ int rbuf_pos;
+ int binlen;
+} atbus_instance;
+
+#define AT_BUSINST(s) ((atbus_instance *)((s)->link.link_instance))
#endif /* #ifndef _gnokii_atbus_h */
- next step to multiple phones support,
Ladislav Michl <=