Add runtime configuration for UDP discovery utility. From Max Holtzberg

This commit is contained in:
Gregory Nutt 2013-05-21 12:47:07 -06:00
parent dba4210e37
commit ec32c5e636
4 changed files with 39 additions and 15 deletions

View File

@ -555,3 +555,6 @@
Ken Pettit (2013-5-11). Ken Pettit (2013-5-11).
* apps/examples/helloxx: C++ name mangling was occurring when this * apps/examples/helloxx: C++ name mangling was occurring when this
example is built as an NSH built-in application. (2013-5-16). 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).

View File

@ -177,7 +177,7 @@ int discover_main(int argc, char *argv[])
#endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */ #endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */
#endif /* CONFIG_NSH_BUILTIN_APPS */ #endif /* CONFIG_NSH_BUILTIN_APPS */
if (discover_start() < 0) if (discover_start(NULL) < 0)
{ {
ndbg("Could not start discover daemon.\n"); ndbg("Could not start discover daemon.\n");
return ERROR; return ERROR;

View File

@ -36,16 +36,28 @@
#ifndef __APPS_INCLUDE_NETUTILS_DISCOVER_H #ifndef __APPS_INCLUDE_NETUTILS_DISCOVER_H
#define __APPS_INCLUDE_NETUTILS_DISCOVER_H #define __APPS_INCLUDE_NETUTILS_DISCOVER_H
#include <stdint.h>
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
struct discover_info_s
{
uint8_t devclass; /* Device class, 0xff for all devices */
const char *description;
};
/**************************************************************************** /****************************************************************************
* Name: discover_start * Name: discover_start
* *
* Description: * Description:
* Start the discover daemon. * Start the discover daemon.
* *
* Input Paramters:
*
* info Discover information, if NULL mconf defaults will be used.
*
* Return: * Return:
* The process ID (pid) of the new discover daemon is returned on * The process ID (pid) of the new discover daemon is returned on
* success; A negated errno is returned if the daemon was not successfully * 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 */ #endif /* __APPS_INCLUDE_NETUTILS_DISCOVER_H */

View File

@ -94,7 +94,7 @@
* Byte Description * Byte Description
* 0 Protocol indentifier (0x99) * 0 Protocol indentifier (0x99)
* 1 Request command 0x01 * 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 * 0xff for all devices
* 3 Checksum (Byte 0 - Byte 1 - Byte n) & 0xff * 3 Checksum (Byte 0 - Byte 1 - Byte n) & 0xff
*/ */
@ -103,7 +103,7 @@
* Byte Description * Byte Description
* 0 Protocol indentifier (0x99) * 0 Protocol indentifier (0x99)
* 1 Reponse command (0x02) * 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 * 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_state_s
{ {
struct discover_info_s info;
in_addr_t serverip; in_addr_t serverip;
request_t request; request_t request;
response_t response; response_t response;
@ -132,7 +133,10 @@ struct discover_state_s
* Public Data * Public Data
****************************************************************************/ ****************************************************************************/
struct discover_state_s g_state; struct discover_state_s g_state =
{
{CONFIG_DISCOVER_DEVICE_CLASS, CONFIG_DISCOVER_DESCR}
};
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
@ -158,7 +162,7 @@ static inline void discover_initresponse()
g_state.response[0] = DISCOVER_PROTO_ID; g_state.response[0] = DISCOVER_PROTO_ID;
g_state.response[1] = DISCOVER_RESPONSE; 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); DISCOVER_RESPONSE_SIZE-3);
for (i = 0; i < DISCOVER_RESPONSE_SIZE-1; i++) 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 sockfd = -1;
int nbytes; int nbytes;
int addrlen = sizeof(struct sockaddr_in); 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(); discover_initresponse();
nvdbg("Started\n"); nvdbg("Started\n");
@ -198,7 +202,7 @@ static int discover_daemon(int argc, char *argv[])
} }
/* Read the next packet */ /* Read the next packet */
nbytes = recvfrom(sockfd, &g_state.request, sizeof(g_state.request), 0, nbytes = recvfrom(sockfd, &g_state.request, sizeof(g_state.request), 0,
(struct sockaddr*) &srcaddr, (struct sockaddr*) &srcaddr,
(socklen_t *) &addrlen); (socklen_t *) &addrlen);
@ -214,14 +218,14 @@ static int discover_daemon(int argc, char *argv[])
} }
continue; continue;
} }
if (discover_parse(g_state.request) != OK) if (discover_parse(g_state.request) != OK)
{ {
continue; continue;
} }
ndbg("Received discover from %08lx'\n", srcaddr.sin_addr.s_addr); ndbg("Received discover from %08lx'\n", srcaddr.sin_addr.s_addr);
discover_respond(&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]); ndbg("Wrong command: %d\n", packet[1]);
return ERROR; 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++) for (i = 0; i < DISCOVER_REQUEST_SIZE-1; i++)
chk -= packet[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; pid_t pid;
if (info)
{
g_state.info = *info;
}
/* Then start the new daemon */ /* Then start the new daemon */
pid = TASK_CREATE("Discover daemon", CONFIG_DISCOVER_PRIORITY, pid = TASK_CREATE("Discover daemon", CONFIG_DISCOVER_PRIORITY,