From ec32c5e6364eca27e82052e9ac2b2c09889ebaff Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 21 May 2013 12:47:07 -0600 Subject: [PATCH] Add runtime configuration for UDP discovery utility. From Max Holtzberg --- ChangeLog.txt | 3 +++ examples/discover/discover_main.c | 2 +- include/netutils/discover.h | 14 ++++++++++++- netutils/discover/discover.c | 35 +++++++++++++++++++------------ 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index a7f0fad8b..e6ceed475 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -555,3 +555,6 @@ Ken Pettit (2013-5-11). * apps/examples/helloxx: C++ name mangling was occurring when this example is built as an NSH built-in application. (2013-5-16). + * apps/netutils/discover: Added a runtime configuration for the + UDP discover utility. From Max Holtzberg (2013-5-21). + diff --git a/examples/discover/discover_main.c b/examples/discover/discover_main.c index 259a93d98..9c8f5f512 100644 --- a/examples/discover/discover_main.c +++ b/examples/discover/discover_main.c @@ -177,7 +177,7 @@ int discover_main(int argc, char *argv[]) #endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */ #endif /* CONFIG_NSH_BUILTIN_APPS */ - if (discover_start() < 0) + if (discover_start(NULL) < 0) { ndbg("Could not start discover daemon.\n"); return ERROR; diff --git a/include/netutils/discover.h b/include/netutils/discover.h index c2226918d..df792b8ca 100644 --- a/include/netutils/discover.h +++ b/include/netutils/discover.h @@ -36,16 +36,28 @@ #ifndef __APPS_INCLUDE_NETUTILS_DISCOVER_H #define __APPS_INCLUDE_NETUTILS_DISCOVER_H +#include + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ +struct discover_info_s +{ + uint8_t devclass; /* Device class, 0xff for all devices */ + const char *description; +}; + /**************************************************************************** * Name: discover_start * * Description: * Start the discover daemon. * + * Input Paramters: + * + * info Discover information, if NULL mconf defaults will be used. + * * Return: * The process ID (pid) of the new discover daemon is returned on * success; A negated errno is returned if the daemon was not successfully @@ -53,6 +65,6 @@ * ****************************************************************************/ -int discover_start(void); +int discover_start(struct discover_info_s *info); #endif /* __APPS_INCLUDE_NETUTILS_DISCOVER_H */ diff --git a/netutils/discover/discover.c b/netutils/discover/discover.c index 73b783209..dcd008a48 100644 --- a/netutils/discover/discover.c +++ b/netutils/discover/discover.c @@ -94,7 +94,7 @@ * Byte Description * 0 Protocol indentifier (0x99) * 1 Request command 0x01 - * 2 Destination device class (For querying subsets of available devices) + * 2 Destination device class (For querying subsets of available devices) * 0xff for all devices * 3 Checksum (Byte 0 - Byte 1 - Byte n) & 0xff */ @@ -103,7 +103,7 @@ * Byte Description * 0 Protocol indentifier (0x99) * 1 Reponse command (0x02) - * 2-33 Device description string with 0 bytes filled + * 2-33 Device description string with 0 bytes filled * 34 Checksum (Byte 0 - Byte 1 - Byte n) & 0xff */ @@ -123,6 +123,7 @@ typedef uint8_t response_t[DISCOVER_RESPONSE_SIZE]; struct discover_state_s { + struct discover_info_s info; in_addr_t serverip; request_t request; response_t response; @@ -132,7 +133,10 @@ struct discover_state_s * Public Data ****************************************************************************/ -struct discover_state_s g_state; +struct discover_state_s g_state = +{ + {CONFIG_DISCOVER_DEVICE_CLASS, CONFIG_DISCOVER_DESCR} +}; /**************************************************************************** * Private Function Prototypes @@ -158,7 +162,7 @@ static inline void discover_initresponse() g_state.response[0] = DISCOVER_PROTO_ID; g_state.response[1] = DISCOVER_RESPONSE; - strncpy((char*) &g_state.response[2], CONFIG_DISCOVER_DESCR, + strncpy((char*) &g_state.response[2], g_state.info.description, DISCOVER_RESPONSE_SIZE-3); for (i = 0; i < DISCOVER_RESPONSE_SIZE-1; i++) @@ -176,9 +180,9 @@ static int discover_daemon(int argc, char *argv[]) int sockfd = -1; int nbytes; int addrlen = sizeof(struct sockaddr_in); - struct sockaddr_in srcaddr; + struct sockaddr_in srcaddr; - memset(&g_state, 0, sizeof(struct discover_state_s)); + /* memset(&g_state, 0, sizeof(struct discover_state_s)); */ discover_initresponse(); nvdbg("Started\n"); @@ -198,7 +202,7 @@ static int discover_daemon(int argc, char *argv[]) } /* Read the next packet */ - + nbytes = recvfrom(sockfd, &g_state.request, sizeof(g_state.request), 0, (struct sockaddr*) &srcaddr, (socklen_t *) &addrlen); @@ -214,14 +218,14 @@ static int discover_daemon(int argc, char *argv[]) } continue; } - + if (discover_parse(g_state.request) != OK) { continue; } - + ndbg("Received discover from %08lx'\n", srcaddr.sin_addr.s_addr); - + discover_respond(&srcaddr.sin_addr.s_addr); } @@ -244,8 +248,8 @@ static inline int discover_parse(request_t packet) ndbg("Wrong command: %d\n", packet[1]); return ERROR; } - - if (packet[2] == 0xff || packet[2] == CONFIG_DISCOVER_DEVICE_CLASS) + + if (packet[2] == 0xff || packet[2] == g_state.info.devclass) { for (i = 0; i < DISCOVER_REQUEST_SIZE-1; i++) chk -= packet[i]; @@ -437,10 +441,15 @@ static inline int discover_openresponder(void) * ****************************************************************************/ -int discover_start() +int discover_start(struct discover_info_s *info) { pid_t pid; + if (info) + { + g_state.info = *info; + } + /* Then start the new daemon */ pid = TASK_CREATE("Discover daemon", CONFIG_DISCOVER_PRIORITY,