Moved lwip_prvmib.c and private_mib.h from unix/proj/minimal to apps/snmp_private_mib and extended it for write support and usage with memory buffers instead of files to be used under non-posix OSes as an example

This commit is contained in:
goldsimon 2010-02-02 19:39:25 +00:00
parent b411c22658
commit 880ed2c01e
4 changed files with 147 additions and 36 deletions

View File

@ -42,17 +42,41 @@
* Author: Christiaan Simons <christiaan.simons@axon.tv> * Author: Christiaan Simons <christiaan.simons@axon.tv>
*/ */
#include "private_mib.h"
#if LWIP_SNMP
/** Directory where the sensor files are */
#define SENSORS_DIR "w:\\sensors"
/** Set to 1 to read sensor values from files (in directory defined by SENSORS_DIR) */
#define SENSORS_USE_FILES 0
/** Set to 1 to search sensor files at startup (in directory defined by SENSORS_DIR) */
#define SENSORS_SEARCH_FILES 0
#if SENSORS_SEARCH_FILES
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#endif /* SENSORS_SEARCH_FILES */
#include <string.h>
#include <stdio.h>
#include "private_mib.h"
#include "lwip/snmp_msg.h" #include "lwip/snmp_msg.h"
#include "lwip/snmp_asn1.h" #include "lwip/snmp_asn1.h"
#if !SENSORS_USE_FILES || !SENSORS_SEARCH_FILES
/** When not using & searching files, defines the number of sensors */
#define SENSOR_COUNT 2
#endif /* !SENSORS_USE_FILES || !SENSORS_SEARCH_FILES */
#if !SENSORS_USE_FILES
/** When not using files, contains the values of the sensors */
s32_t sensor_values[SENSOR_COUNT];
#endif /* !SENSORS_USE_FILES */
/* /*
This example presents a table for a few (at most 10) sensors. This example presents a table for a few (at most 10) sensors.
Sensor detection takes place at initialization (once only). Sensor detection takes place at initialization (once only).
@ -71,9 +95,10 @@
text files in the "sensors" directory. text files in the "sensors" directory.
*/ */
#define SENSOR_MAX 10 #define SENSOR_MAX 10
#define SENSOR_NAME_LEN 20 #define SENSOR_NAME_LEN 20
#define SENSORS_DIR "sensors"
struct sensor_inf struct sensor_inf
{ {
char sensor_files[SENSOR_MAX][SENSOR_NAME_LEN + 1]; char sensor_files[SENSOR_MAX][SENSOR_NAME_LEN + 1];
@ -118,7 +143,7 @@ static void sensorentry_set_value_pc(u8_t rid, struct obj_def *od);
/* sensorentry .1.3.6.1.4.1.26381.1.1.1 (.level0.level1) /* sensorentry .1.3.6.1.4.1.26381.1.1.1 (.level0.level1)
where level 0 is the object identifier (temperature) and level 1 the index */ where level 0 is the object identifier (temperature) and level 1 the index */
struct mib_external_node sensorentry = { static struct mib_external_node sensorentry = {
&noleafs_get_object_def, &noleafs_get_object_def,
&noleafs_get_value, &noleafs_get_value,
&noleafs_set_test, &noleafs_set_test,
@ -150,11 +175,11 @@ struct mib_external_node sensorentry = {
}; };
/* sensortable .1.3.6.1.4.1.26381.1.1 */ /* sensortable .1.3.6.1.4.1.26381.1.1 */
const s32_t sensortable_ids[1] = { 1 }; static const s32_t sensortable_ids[1] = { 1 };
struct mib_node* const sensortable_nodes[1] = { static struct mib_node* const sensortable_nodes[1] = {
(struct mib_node* const)&sensorentry (struct mib_node* const)&sensorentry
}; };
const struct mib_array_node sensortable = { static const struct mib_array_node sensortable = {
&noleafs_get_object_def, &noleafs_get_object_def,
&noleafs_get_value, &noleafs_get_value,
&noleafs_set_test, &noleafs_set_test,
@ -166,11 +191,11 @@ const struct mib_array_node sensortable = {
}; };
/* example .1.3.6.1.4.1.26381.1 */ /* example .1.3.6.1.4.1.26381.1 */
const s32_t example_ids[1] = { 1 }; static const s32_t example_ids[1] = { 1 };
struct mib_node* const example_nodes[1] = { static struct mib_node* const example_nodes[1] = {
(struct mib_node* const)&sensortable (struct mib_node* const)&sensortable
}; };
const struct mib_array_node example = { static const struct mib_array_node example = {
&noleafs_get_object_def, &noleafs_get_object_def,
&noleafs_get_value, &noleafs_get_value,
&noleafs_set_test, &noleafs_set_test,
@ -182,9 +207,9 @@ const struct mib_array_node example = {
}; };
/* lwip .1.3.6.1.4.1.26381 */ /* lwip .1.3.6.1.4.1.26381 */
const s32_t lwip_ids[1] = { 1 }; static const s32_t lwip_ids[1] = { 1 };
struct mib_node* const lwip_nodes[1] = { (struct mib_node* const)&example }; static struct mib_node* const lwip_nodes[1] = { (struct mib_node* const)&example };
const struct mib_array_node lwip = { static const struct mib_array_node lwip = {
&noleafs_get_object_def, &noleafs_get_object_def,
&noleafs_get_value, &noleafs_get_value,
&noleafs_set_test, &noleafs_set_test,
@ -196,9 +221,9 @@ const struct mib_array_node lwip = {
}; };
/* enterprises .1.3.6.1.4.1 */ /* enterprises .1.3.6.1.4.1 */
const s32_t enterprises_ids[1] = { 26381 }; static const s32_t enterprises_ids[1] = { 26381 };
struct mib_node* const enterprises_nodes[1] = { (struct mib_node* const)&lwip }; static struct mib_node* const enterprises_nodes[1] = { (struct mib_node* const)&lwip };
const struct mib_array_node enterprises = { static const struct mib_array_node enterprises = {
&noleafs_get_object_def, &noleafs_get_object_def,
&noleafs_get_value, &noleafs_get_value,
&noleafs_set_test, &noleafs_set_test,
@ -210,8 +235,8 @@ const struct mib_array_node enterprises = {
}; };
/* private .1.3.6.1.4 */ /* private .1.3.6.1.4 */
const s32_t private_ids[1] = { 1 }; static const s32_t private_ids[1] = { 1 };
struct mib_node* const private_nodes[1] = { (struct mib_node* const)&enterprises }; static struct mib_node* const private_nodes[1] = { (struct mib_node* const)&enterprises };
const struct mib_array_node mib_private = { const struct mib_array_node mib_private = {
&noleafs_get_object_def, &noleafs_get_object_def,
&noleafs_get_value, &noleafs_get_value,
@ -223,6 +248,7 @@ const struct mib_array_node mib_private = {
private_nodes private_nodes
}; };
/** /**
* Initialises this private MIB before use. * Initialises this private MIB before use.
* @see main.c * @see main.c
@ -230,14 +256,20 @@ const struct mib_array_node mib_private = {
void void
lwip_privmib_init(void) lwip_privmib_init(void)
{ {
#if SENSORS_USE_FILES && SENSORS_SEARCH_FILES
char *buf, *ebuf, *cp; char *buf, *ebuf, *cp;
size_t bufsize; size_t bufsize;
int nbytes; int nbytes;
struct stat sb; struct stat sb;
struct dirent *dp; struct dirent *dp;
int fd; int fd;
#else /* SENSORS_USE_FILES && SENSORS_SEARCH_FILES */
int i;
#endif /* SENSORS_USE_FILES && SENSORS_SEARCH_FILES */
printf("SNMP private MIB start, detecting sensors.\n"); printf("SNMP private MIB start, detecting sensors.\n");
#if SENSORS_USE_FILES && SENSORS_SEARCH_FILES
/* look for sensors in sensors directory */ /* look for sensors in sensors directory */
fd = open(SENSORS_DIR, O_RDONLY); fd = open(SENSORS_DIR, O_RDONLY);
if (fd > -1) if (fd > -1)
@ -284,6 +316,23 @@ lwip_privmib_init(void)
} }
close(fd); close(fd);
} }
#else /* SENSORS_USE_FILES && SENSORS_SEARCH_FILES */
for (i = 0; i < SENSOR_COUNT; i++) {
struct mib_list_node *dummy;
s32_t index = i;
char name[256];
sprintf(name, "%d.txt", i);
snmp_mib_node_insert(&sensor_addr_inf.sensor_list_rn, index, &dummy);
strncpy(&sensor_addr_inf.sensor_files[index][0], name, SENSOR_NAME_LEN);
printf("%s\n", sensor_addr_inf.sensor_files[index]);
#if !SENSORS_USE_FILES
/* initialize sensor value to != zero */
sensor_values[i] = 11 * (i+1);
#endif /* !SENSORS_USE_FILES */
}
#endif /* SENSORS_USE_FILE && SENSORS_SEARCH_FILES */
if (sensor_addr_inf.sensor_list_rn.count != 0) if (sensor_addr_inf.sensor_list_rn.count != 0)
{ {
/* enable sensor table, 2 tree_levels under this node /* enable sensor table, 2 tree_levels under this node
@ -376,7 +425,10 @@ sensorentry_get_subid(void* addr_inf, u8_t level, u16_t idx, s32_t *sub_id)
static void static void
sensorentry_get_object_def_q(void* addr_inf, u8_t rid, u8_t ident_len, s32_t *ident) sensorentry_get_object_def_q(void* addr_inf, u8_t rid, u8_t ident_len, s32_t *ident)
{ {
u16_t sensor_register, sensor_address; s32_t sensor_register, sensor_address;
LWIP_UNUSED_ARG(addr_inf);
LWIP_UNUSED_ARG(rid);
ident_len += 1; ident_len += 1;
ident -= 1; ident -= 1;
@ -384,8 +436,8 @@ sensorentry_get_object_def_q(void* addr_inf, u8_t rid, u8_t ident_len, s32_t *id
/* send request */ /* send request */
sensor_register = ident[0]; sensor_register = ident[0];
sensor_address = ident[1]; sensor_address = ident[1];
LWIP_DEBUGF(SNMP_MIB_DEBUG,("sensor_request reg=%"U16_F" addr=%"U16_F"\n", LWIP_DEBUGF(SNMP_MIB_DEBUG,("sensor_request reg=%"S32_F" addr=%"S32_F"\n",
(u16_t)sensor_register, (u16_t)sensor_address)); sensor_register, sensor_address));
/* fake async quesion/answer */ /* fake async quesion/answer */
snmp_msg_event(rid); snmp_msg_event(rid);
} }
@ -393,6 +445,8 @@ sensorentry_get_object_def_q(void* addr_inf, u8_t rid, u8_t ident_len, s32_t *id
static void static void
sensorentry_get_object_def_a(u8_t rid, u8_t ident_len, s32_t *ident, struct obj_def *od) sensorentry_get_object_def_a(u8_t rid, u8_t ident_len, s32_t *ident, struct obj_def *od)
{ {
LWIP_UNUSED_ARG(rid);
/* return to object name, adding index depth (1) */ /* return to object name, adding index depth (1) */
ident_len += 1; ident_len += 1;
ident -= 1; ident -= 1;
@ -402,7 +456,7 @@ sensorentry_get_object_def_a(u8_t rid, u8_t ident_len, s32_t *ident, struct obj_
od->id_inst_ptr = ident; od->id_inst_ptr = ident;
od->instance = MIB_OBJECT_TAB; od->instance = MIB_OBJECT_TAB;
od->access = MIB_OBJECT_READ_ONLY; od->access = MIB_OBJECT_READ_WRITE;
od->asn_type = (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG); od->asn_type = (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG);
od->v_len = sizeof(s32_t); od->v_len = sizeof(s32_t);
} }
@ -416,12 +470,17 @@ sensorentry_get_object_def_a(u8_t rid, u8_t ident_len, s32_t *ident, struct obj_
static void static void
sensorentry_get_object_def_pc(u8_t rid, u8_t ident_len, s32_t *ident) sensorentry_get_object_def_pc(u8_t rid, u8_t ident_len, s32_t *ident)
{ {
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(ident_len);
LWIP_UNUSED_ARG(ident);
/* nop */ /* nop */
} }
static void static void
sensorentry_get_value_q(u8_t rid, struct obj_def *od) sensorentry_get_value_q(u8_t rid, struct obj_def *od)
{ {
LWIP_UNUSED_ARG(od);
/* fake async quesion/answer */ /* fake async quesion/answer */
snmp_msg_event(rid); snmp_msg_event(rid);
} }
@ -429,12 +488,18 @@ sensorentry_get_value_q(u8_t rid, struct obj_def *od)
static void static void
sensorentry_get_value_a(u8_t rid, struct obj_def *od, u16_t len, void *value) sensorentry_get_value_a(u8_t rid, struct obj_def *od, u16_t len, void *value)
{ {
u8_t i; s32_t i;
s32_t *temperature = value; s32_t *temperature = value;
#if SENSORS_USE_FILES
FILE* sensf; FILE* sensf;
char senspath[sizeof(SENSORS_DIR)+1+SENSOR_NAME_LEN+1] = SENSORS_DIR"/"; char senspath[sizeof(SENSORS_DIR)+1+SENSOR_NAME_LEN+1] = SENSORS_DIR"/";
#endif /* SENSORS_USE_FILES */
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(len);
i = od->id_inst_ptr[1]; i = od->id_inst_ptr[1];
#if SENSORS_USE_FILES
strncpy(&senspath[sizeof(SENSORS_DIR)], strncpy(&senspath[sizeof(SENSORS_DIR)],
sensor_addr_inf.sensor_files[i], sensor_addr_inf.sensor_files[i],
SENSOR_NAME_LEN); SENSOR_NAME_LEN);
@ -444,17 +509,25 @@ sensorentry_get_value_a(u8_t rid, struct obj_def *od, u16_t len, void *value)
fscanf(sensf,"%"S32_F,temperature); fscanf(sensf,"%"S32_F,temperature);
fclose(sensf); fclose(sensf);
} }
#else /* SENSORS_USE_FILES */
if (i <= SENSOR_COUNT) {
*temperature = sensor_values[i];
}
#endif /* SENSORS_USE_FILES */
} }
static void static void
sensorentry_get_value_pc(u8_t rid, struct obj_def *od) sensorentry_get_value_pc(u8_t rid, struct obj_def *od)
{ {
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(od);
/* nop */ /* nop */
} }
static void static void
sensorentry_set_test_q(u8_t rid, struct obj_def *od) sensorentry_set_test_q(u8_t rid, struct obj_def *od)
{ {
LWIP_UNUSED_ARG(od);
/* fake async quesion/answer */ /* fake async quesion/answer */
snmp_msg_event(rid); snmp_msg_event(rid);
} }
@ -462,19 +535,29 @@ sensorentry_set_test_q(u8_t rid, struct obj_def *od)
static u8_t static u8_t
sensorentry_set_test_a(u8_t rid, struct obj_def *od, u16_t len, void *value) sensorentry_set_test_a(u8_t rid, struct obj_def *od, u16_t len, void *value)
{ {
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(od);
LWIP_UNUSED_ARG(len);
LWIP_UNUSED_ARG(value);
/* sensors are read-only */ /* sensors are read-only */
return 0; return 1; // 0 -> read only, != 0 -> read/write
} }
static void static void
sensorentry_set_test_pc(u8_t rid, struct obj_def *od) sensorentry_set_test_pc(u8_t rid, struct obj_def *od)
{ {
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(od);
/* nop */ /* nop */
} }
static void static void
sensorentry_set_value_q(u8_t rid, struct obj_def *od, u16_t len, void *value) sensorentry_set_value_q(u8_t rid, struct obj_def *od, u16_t len, void *value)
{ {
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(od);
LWIP_UNUSED_ARG(len);
LWIP_UNUSED_ARG(value);
/* fake async quesion/answer */ /* fake async quesion/answer */
snmp_msg_event(rid); snmp_msg_event(rid);
} }
@ -482,10 +565,40 @@ sensorentry_set_value_q(u8_t rid, struct obj_def *od, u16_t len, void *value)
static void static void
sensorentry_set_value_a(u8_t rid, struct obj_def *od, u16_t len, void *value) sensorentry_set_value_a(u8_t rid, struct obj_def *od, u16_t len, void *value)
{ {
s32_t i;
s32_t *temperature = value;
#if SENSORS_USE_FILES
FILE* sensf;
char senspath[sizeof(SENSORS_DIR)+1+SENSOR_NAME_LEN+1] = SENSORS_DIR"/";
#endif /* SENSORS_USE_FILES */
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(len);
i = od->id_inst_ptr[1];
#if SENSORS_USE_FILES
strncpy(&senspath[sizeof(SENSORS_DIR)],
sensor_addr_inf.sensor_files[i],
SENSOR_NAME_LEN);
sensf = fopen(senspath, "w");
if (sensf != NULL)
{
fprintf(sensf, "%"S32_F, temperature);
fclose(sensf);
}
#else /* SENSORS_USE_FILES */
if (i <= SENSOR_COUNT) {
sensor_values[i] = *temperature;
}
#endif /* SENSORS_USE_FILES */
} }
static void static void
sensorentry_set_value_pc(u8_t rid, struct obj_def *od) sensorentry_set_value_pc(u8_t rid, struct obj_def *od)
{ {
LWIP_UNUSED_ARG(rid);
LWIP_UNUSED_ARG(od);
/* nop */ /* nop */
} }
#endif /* LWIP_SNMP */

View File

@ -21,6 +21,8 @@ struct private_msg
void lwip_privmib_init(void); void lwip_privmib_init(void);
#define SNMP_PRIVATE_MIB_INIT() lwip_privmib_init()
#endif #endif
#endif #endif

View File

@ -63,7 +63,8 @@ CORE4FILES=$(LWIPDIR)/core/ipv4/icmp.c $(LWIPDIR)/core/ipv4/ip.c \
# SNMPFILES: Extra SNMPv1 agent # SNMPFILES: Extra SNMPv1 agent
SNMPFILES=$(LWIPDIR)/core/snmp/asn1_dec.c $(LWIPDIR)/core/snmp/asn1_enc.c \ SNMPFILES=$(LWIPDIR)/core/snmp/asn1_dec.c $(LWIPDIR)/core/snmp/asn1_enc.c \
$(LWIPDIR)/core/snmp/mib2.c $(LWIPDIR)/core/snmp/mib_structs.c \ $(LWIPDIR)/core/snmp/mib2.c $(LWIPDIR)/core/snmp/mib_structs.c \
$(LWIPDIR)/core/snmp/msg_in.c $(LWIPDIR)/core/snmp/msg_out.c lwip_prvmib.c $(LWIPDIR)/core/snmp/msg_in.c $(LWIPDIR)/core/snmp/msg_out.c \
../../../../apps/snmp_private_mib/lwip_prvmib.c
# NETIFFILES: Files implementing various generic network interface functions.' # NETIFFILES: Files implementing various generic network interface functions.'
NETIFFILES=$(LWIPDIR)/netif/etharp.c mintapif.c NETIFFILES=$(LWIPDIR)/netif/etharp.c mintapif.c

View File

@ -1,11 +1,10 @@
<?xml version="1.0" encoding="Windows-1252"?> <?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject <VisualStudioProject
ProjectType="Visual C++" ProjectType="Visual C++"
Version="9,00" Version="8,00"
Name="lwIP_test" Name="lwIP_test"
ProjectGUID="{8CC0CE51-32CF-4585-BFAF-A9343BC5A96D}" ProjectGUID="{8CC0CE51-32CF-4585-BFAF-A9343BC5A96D}"
RootNamespace="lwIP_test" RootNamespace="lwIP_test"
TargetFrameworkVersion="131072"
> >
<Platforms> <Platforms>
<Platform <Platform
@ -79,8 +78,6 @@
AdditionalLibraryDirectories="$(PCAP_DIR)\Lib" AdditionalLibraryDirectories="$(PCAP_DIR)\Lib"
ProgramDatabaseFile=".\Release/test.pdb" ProgramDatabaseFile=".\Release/test.pdb"
SubSystem="1" SubSystem="1"
RandomizedBaseAddress="1"
DataExecutionPrevention="0"
TargetMachine="1" TargetMachine="1"
/> />
<Tool <Tool
@ -168,8 +165,6 @@
AdditionalLibraryDirectories="$(PCAP_DIR)\Lib" AdditionalLibraryDirectories="$(PCAP_DIR)\Lib"
GenerateDebugInformation="true" GenerateDebugInformation="true"
SubSystem="1" SubSystem="1"
RandomizedBaseAddress="1"
DataExecutionPrevention="0"
TargetMachine="1" TargetMachine="1"
/> />
<Tool <Tool
@ -371,11 +366,11 @@
Name="private_mib" Name="private_mib"
> >
<File <File
RelativePath="..\lwip_prvmib.c" RelativePath="..\..\..\apps\snmp_private_mib\lwip_prvmib.c"
> >
</File> </File>
<File <File
RelativePath="..\private_mib.h" RelativePath="..\..\..\apps\snmp_private_mib\private_mib.h"
> >
</File> </File>
</Filter> </Filter>