One step is triggered the transport handle_input function: this one fills the
rx_msg buffer. It also used to read from that rx_msg buffer right again and
trigger the state machine. This patch decouples the second part and is needed
for further splitting into substates.
---
lib/obex.c | 2 +-
lib/obex_client.c | 13 +++++++--
lib/obex_client.h | 2 +-
lib/obex_main.c | 73 ++++++++++++++++++++++++++++++++-----------------
lib/obex_main.h | 2 +
lib/obex_server.c | 18 +++++++++---
lib/obex_server.h | 2 +-
lib/obex_transport.c | 2 +-
8 files changed, 78 insertions(+), 36 deletions(-)
diff --git a/lib/obex.c b/lib/obex.c
index bf2b71b..e56ef74 100644
--- a/lib/obex.c
+++ b/lib/obex.c
@@ -535,7 +535,7 @@ int CALLAPI OBEX_Request(obex_t *self, obex_object_t
*object)
self->mode = MODE_CLI;
self->state = STATE_SEND;
- return obex_client(self, NULL, 0);
+ return obex_client_send(self, NULL, 0);
}
/**
diff --git a/lib/obex_client.c b/lib/obex_client.c
index 0199fbf..e17528a 100644
--- a/lib/obex_client.c
+++ b/lib/obex_client.c
@@ -238,22 +238,29 @@ int obex_client_send(obex_t *self, buf_t *msg, int rsp)
* Handle client operations
*
*/
-int obex_client(obex_t *self, buf_t *msg, int final)
+int obex_client(obex_t *self)
{
+ int ret = -1;
+ buf_t *msg = obex_data_receive(self);
int rsp = msg_get_rsp(msg);
DEBUG(4, "\n");
switch (self->state) {
case STATE_SEND:
- return obex_client_send(self, msg, rsp);
+ ret = obex_client_send(self, msg, rsp);
+ break;
case STATE_REC:
- return obex_client_recv(self, msg, rsp);
+ ret = obex_client_recv(self, msg, rsp);
+ break;
default:
DEBUG(0, "Unknown state\n");
obex_deliver_event(self, OBEX_EV_PARSEERR, rsp, 0, TRUE);
return -1;
}
+
+ obex_data_receive_finished(self);
+ return ret;
}
diff --git a/lib/obex_client.h b/lib/obex_client.h
index bed3593..d1ae8fd 100644
--- a/lib/obex_client.h
+++ b/lib/obex_client.h
@@ -25,7 +25,7 @@
struct obex;
struct databuffer;
-int obex_client(struct obex *self, struct databuffer *msg, int final);
+int obex_client(struct obex *self);
int obex_client_send(obex_t *self, buf_t *msg, int rsp);
#endif
diff --git a/lib/obex_main.c b/lib/obex_main.c
index 884c9e3..5337208 100644
--- a/lib/obex_main.c
+++ b/lib/obex_main.c
@@ -257,6 +257,20 @@ int obex_data_request(obex_t *self, buf_t *msg)
return status;
}
+static int obex_mode(obex_t *self)
+{
+ switch (self->mode) {
+ case MODE_SRV:
+ return obex_server(self);
+
+ case MODE_CLI:
+ return obex_client(self);
+
+ default:
+ return -1;
+ }
+}
+
/*
* Function obex_work (self, timeout)
*
@@ -265,6 +279,8 @@ int obex_data_request(obex_t *self, buf_t *msg)
*/
int obex_work(obex_t *self, int timeout)
{
+ int ret;
+
/* Waiting for an incoming packet will not work for single response mode
* as the client is not supposed to send any when we (as server) are
* sending the response.
@@ -273,8 +289,6 @@ int obex_work(obex_t *self, int timeout)
self->object->rsp_mode != OBEX_RSP_MODE_NORMAL &&
self->state == STATE_SEND &&
!(self->srm_flags & OBEX_SRM_FLAG_WAIT_LOCAL)) {
- int ret;
-
/* Still, we need to do a zero-wait check for an ABORT
* and for connection errors. */
ret = obex_transport_handle_input(self, 0);
@@ -298,7 +312,10 @@ int obex_work(obex_t *self, int timeout)
return sizeof(obex_common_hdr_t);
}
- return obex_transport_handle_input(self, timeout);
+ ret = obex_transport_handle_input(self, timeout);
+ if (ret == 1)
+ ret = obex_mode(self);
+ return ret;
}
/*
@@ -314,15 +331,14 @@ int obex_get_buffer_status(buf_t *msg) {
/*
* Function obex_data_indication (self)
*
- * Read/Feed some input from device and find out which packet it is
+ * Read some input from device and find out which packet it is
*
*/
int obex_data_indication(obex_t *self)
{
obex_common_hdr_t *hdr;
buf_t *msg;
- uint8_t opcode;
- int final, ret, actual = 0;
+ int actual;
unsigned int size;
DEBUG(4, "\n");
@@ -342,7 +358,7 @@ int obex_data_indication(obex_t *self)
* partial buffer (custom transport) */
if (actual < 0) {
obex_deliver_event(self, OBEX_EV_LINKERR, 0, 0, TRUE);
- return actual;
+ return -1;
}
}
@@ -366,14 +382,14 @@ int obex_data_indication(obex_t *self)
if (actual < 0) {
obex_deliver_event(self, OBEX_EV_LINKERR,
0, 0, TRUE);
- return actual;
+ return -1;
}
}
} else {
/* Wait until we have at least 3 bytes data */
DEBUG(3, "Need at least 3 bytes got only %lu!\n",
(unsigned long) msg->data_size);
- return actual;
+ return 0;
}
/* New data has been inserted at the end of message */
@@ -392,16 +408,20 @@ int obex_data_indication(obex_t *self)
size, (unsigned long)msg->data_size);
/* I'll be back! */
- return msg->data_size;
+ return 0;
}
DUMPBUFFER(2, "Rx", msg);
- actual = msg->data_size;
- opcode = hdr->opcode & ~OBEX_FINAL;
- final = hdr->opcode & OBEX_FINAL; /* Extract final bit */
+ return 1;
+}
+
+buf_t* obex_data_receive(obex_t *self)
+{
+ buf_t *msg = self->rx_msg;
+ obex_common_hdr_t *hdr = (obex_common_hdr_t *)msg->data;
+ uint8_t opcode = hdr->opcode & ~OBEX_FINAL;
- /* Dispatch to the mode we are in */
if (self->mode == MODE_SRV) {
/* Single response mode makes it possible for the client to send
* the next request (e.g. PUT) while still receiving the last
@@ -411,23 +431,26 @@ int obex_data_indication(obex_t *self)
self->object->rsp_mode != OBEX_RSP_MODE_NORMAL
&&
self->state == STATE_SEND &&
!(opcode == OBEX_CMD_ABORT || opcode ==
self->object->cmd))
- return 0;
+ return NULL;
+ }
- self->srm_flags &= ~OBEX_SRM_FLAG_WAIT_LOCAL;
- ret = obex_server(self, msg, final);
- } else
- ret = obex_client(self, msg, final);
+ if (!obex_get_buffer_status(msg))
+ return NULL;
+
+ self->srm_flags &= ~OBEX_SRM_FLAG_WAIT_LOCAL;
+ return msg;
+}
+
+void obex_data_receive_finished(obex_t *self)
+{
+ buf_t *msg = self->rx_msg;
+ obex_common_hdr_t *hdr = (obex_common_hdr_t *)msg->data;
+ unsigned int size = ntohs(hdr->len);
DEBUG(4, "Pulling %u bytes\n", size);
buf_remove_begin(msg, size);
if (msg->data_size == 0)
buf_reuse(msg);
-
- /* Check parse errors */
- if (ret < 0)
- actual = ret;
-
- return actual;
}
/*
diff --git a/lib/obex_main.h b/lib/obex_main.h
index 53d2768..8a3e975 100644
--- a/lib/obex_main.h
+++ b/lib/obex_main.h
@@ -69,6 +69,8 @@ void obex_deliver_event(struct obex *self, int event, int
cmd, int rsp, int del)
int obex_work(struct obex *self, int timeout);
int obex_get_buffer_status(buf_t *msg);
int obex_data_indication(struct obex *self);
+buf_t* obex_data_receive(obex_t *self);
+void obex_data_receive_finished(obex_t *self);
void obex_response_request(struct obex *self, uint8_t opcode);
void obex_data_request_prepare(struct obex *self, struct databuffer *msg,
diff --git a/lib/obex_server.c b/lib/obex_server.c
index 52f490e..1902e4f 100644
--- a/lib/obex_server.c
+++ b/lib/obex_server.c
@@ -324,22 +324,29 @@ static int obex_server_idle(obex_t *self, buf_t *msg, int
final,
* Handle server-operations
*
*/
-int obex_server(obex_t *self, buf_t *msg, int final)
+int obex_server(obex_t *self)
{
+ int ret = -1;
+ buf_t *msg = obex_data_receive(self);
+ obex_common_hdr_t *hdr = (obex_common_hdr_t *)msg->data;
int cmd = msg_get_cmd(msg);
uint16_t len = msg_get_len(msg);
+ int final = hdr->opcode & OBEX_FINAL; /* Extract final bit */
DEBUG(4, "\n");
switch (self->state) {
case STATE_IDLE:
- return obex_server_idle(self, msg, final, cmd, len);
+ ret = obex_server_idle(self, msg, final, cmd, len);
+ break;
case STATE_REC:
- return obex_server_recv(self, msg, final, cmd, len);
+ ret = obex_server_recv(self, msg, final, cmd, len);
+ break;
case STATE_SEND:
- return obex_server_send(self, msg, cmd, len);
+ ret = obex_server_send(self, msg, cmd, len);
+ break;
default:
DEBUG(0, "Unknown state\n");
@@ -347,4 +354,7 @@ int obex_server(obex_t *self, buf_t *msg, int final)
obex_deliver_event(self, OBEX_EV_PARSEERR, cmd, 0, TRUE);
return -1;
}
+
+ obex_data_receive_finished(self);
+ return ret;
}
diff --git a/lib/obex_server.h b/lib/obex_server.h
index 3906e6f..2d4bc18 100644
--- a/lib/obex_server.h
+++ b/lib/obex_server.h
@@ -25,7 +25,7 @@
struct obex;
struct databuffer;
-int obex_server(struct obex *self, struct databuffer *msg, int final);
+int obex_server(struct obex *self);
int obex_server_send(obex_t *self, buf_t *msg, int cmd, uint16_t len);
#endif
diff --git a/lib/obex_transport.c b/lib/obex_transport.c
index aa113ed..6207279 100644
--- a/lib/obex_transport.c
+++ b/lib/obex_transport.c
@@ -251,7 +251,7 @@ int obex_transport_handle_input(obex_t *self, int timeout)
self->trans.timeout = timeout;
if (obex_get_buffer_status(self->rx_msg)) {
DEBUG(4, "full message already in buffer\n");
- return obex_data_indication(self);
+ return 1;
}
if (self->trans.ops.handle_input)
--
1.7.5.4
------------------------------------------------------------------------------
Got Input? Slashdot Needs You.
Take our quick survey online. Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
Openobex-users mailing list
[email protected]
http://lists.sourceforge.net/lists/listinfo/openobex-users