From 057180987cacfe47edbe21bf2f8c0573901ded2f Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 30 Mar 2011 20:08:34 +0200 Subject: host/mex, digital/avr/modules/host: add message type reservation, refs #157 --- digital/avr/modules/host/mex.h | 10 +++++++++ digital/avr/modules/host/mex.host.c | 37 ++++++++++++++++++++++++++++++++ digital/avr/modules/host/test/test_mex.c | 31 +++++++++++++++----------- 3 files changed, 66 insertions(+), 12 deletions(-) (limited to 'digital/avr') diff --git a/digital/avr/modules/host/mex.h b/digital/avr/modules/host/mex.h index feb91188..82e65103 100644 --- a/digital/avr/modules/host/mex.h +++ b/digital/avr/modules/host/mex.h @@ -35,6 +35,7 @@ enum mex_mtype_t MEX_MTYPE_DATE = 1, MEX_MTYPE_REQ = 2, MEX_MTYPE_RSP = 3, + MEX_MTYPE_RES = 4, }; /** Message structure opaque definition. */ @@ -121,4 +122,13 @@ mex_node_response (mex_msg_t *msg); void mex_node_register (u8 mtype, mex_handler_t *handler, void *user); +/** Request a message type reservation. */ +u8 +mex_node_reserve (const char *mtype_str); + +/** Request a message type reservation, using formated string. */ +u8 +mex_node_reservef (const char *mtype_fmt, ...) + __attribute__ ((format (printf, 1, 2))); + #endif /* mex_h */ diff --git a/digital/avr/modules/host/mex.host.c b/digital/avr/modules/host/mex.host.c index d82a4947..8921cadd 100644 --- a/digital/avr/modules/host/mex.host.c +++ b/digital/avr/modules/host/mex.host.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -501,6 +502,42 @@ mex_node_register (u8 mtype, mex_handler_t *handler, void *user) mex_node_global.handlers_user[mtype] = user; } +/** Request a message type reservation. */ +u8 +mex_node_reserve (const char *mtype_str) +{ + /* Send request. */ + mex_msg_t *m = mex_msg_new (MEX_MTYPE_RES); + mex_msg_push_buffer (m, mtype_str, strlen (mtype_str)); + mex_node_send (m); + /* Wait for response. */ + mex_msg_t *rsp; + rsp = mex_node_recv (); + while (rsp->mtype != MEX_MTYPE_RES) + { + mex_node_dispatch (rsp); + mex_msg_delete (rsp); + rsp = mex_node_recv (); + } + /* Return allocated message type. */ + u8 mtype; + mex_msg_pop (rsp, "B", &mtype); + return mtype; +} + +/** Request a message type reservation, using formated string. */ +u8 +mex_node_reservef (const char *mtype_fmt, ...) +{ + va_list ap; + char mtype_str[MEX_MSG_NEW_PAYLOAD_SIZE + 1]; + va_start (ap, mtype_fmt); + int r = vsnprintf (mtype_str, sizeof (mtype_str), mtype_fmt, ap); + assert (r < (int) sizeof (mtype_str)); + va_end (ap); + return mex_node_reserve (mtype_str); +} + /** Receive one message. */ static mex_msg_t * mex_node_recv (void) diff --git a/digital/avr/modules/host/test/test_mex.c b/digital/avr/modules/host/test/test_mex.c index 1e5d303e..e79e6f68 100644 --- a/digital/avr/modules/host/test/test_mex.c +++ b/digital/avr/modules/host/test/test_mex.c @@ -28,8 +28,10 @@ #include +u8 mtype_coucou1, mtype_coucou2, mtype_oucouc; + void -a82 (void *user, mex_msg_t *msg) +handle_oucouc (void *user, mex_msg_t *msg) { printf ("oucouc\n"); u8 nb; @@ -41,14 +43,14 @@ a82 (void *user, mex_msg_t *msg) } void -a801 (void *user, mex_msg_t *msg) +handle_coucou (void *user, mex_msg_t *msg) { printf ("coucou\n"); u8 b; u16 h; u32 l; mex_msg_pop (msg, "BHL", &b, &h, &l); - if (mex_msg_mtype (msg) == 0x80) + if (mex_msg_mtype (msg) == mtype_coucou1) assert (b == 1 && h == 2 && l == 3); else assert (b == 4 && h == 5 && l == 6); @@ -66,22 +68,27 @@ main (int argc, char **argv) fprintf (stderr, "%s 1|2\n", argv[0]); return 1; } + mex_node_connect (); + mtype_coucou1 = mex_node_reservef ("%s%d", "coucou", 1); + mtype_coucou2 = mex_node_reservef ("%s%d", "coucou", 2); + mtype_oucouc = mex_node_reserve ("oucouc"); if (argv[1][0] == '1') { - mex_node_register (0x82, a82, NULL); - mex_node_connect (); + mex_node_register (mtype_oucouc, handle_oucouc, NULL); i = host_fetch_integer ("reseted"); if (i == -1) { - mex_msg_t *m = mex_msg_new (0x80); + mex_node_wait_date (1); + mex_msg_t *m = mex_msg_new (mtype_coucou1); mex_msg_push (m, "BHL", 1, 2, 3); mex_node_send (m); host_register_integer ("reseted", 1); + mex_node_wait_date (2); host_reset (); } else { - mex_msg_t *m = mex_msg_new (0x81); + mex_msg_t *m = mex_msg_new (mtype_coucou2); mex_msg_push (m, "BHL", 4, 5, 6); mex_node_send (m); mex_node_wait (); @@ -89,13 +96,13 @@ main (int argc, char **argv) } else { - mex_node_register (0x80, a801, NULL); - mex_node_register (0x81, a801, NULL); - mex_node_connect (); - mex_msg_t *m = mex_msg_new (0x82); + mex_node_register (mtype_coucou1, handle_coucou, NULL); + mex_node_register (mtype_coucou2, handle_coucou, NULL); + mex_node_wait_date (1); + mex_msg_t *m = mex_msg_new (mtype_oucouc); mex_msg_push (m, "B", 42); mex_msg_t *r = mex_node_request (m); - assert (mex_msg_mtype (r) == 0x82); + assert (mex_msg_mtype (r) == mtype_oucouc); u8 rb; mex_msg_pop (r, "B", &rb); assert (rb == 43); -- cgit v1.2.3