summaryrefslogtreecommitdiff
path: root/application/spidlib
diff options
context:
space:
mode:
authorJérome Lefranc2008-11-13 19:49:47 +0100
committerJérome Lefranc2008-11-13 19:49:47 +0100
commit8831b20c96a089d5ebf31c25d1e9371094f7cbe5 (patch)
tree43b285eea9fb1f5458e75af40c2d65d7e13e3823 /application/spidlib
parent40a34f256ef3f7cad9573c8b3707de710a6b9e9e (diff)
[spidlib] add nvram and image_desc functions
[spidlib] add 'check' unit testing frame
Diffstat (limited to 'application/spidlib')
-rw-r--r--application/spidlib/Makefile15
-rw-r--r--application/spidlib/Makefile_test2
-rw-r--r--application/spidlib/check/Makefile25
-rw-r--r--application/spidlib/check/check.c29
-rw-r--r--application/spidlib/check/check_ethernet.c58
-rw-r--r--application/spidlib/check/check_system.c50
-rw-r--r--application/spidlib/check/nvram.binbin0 -> 452 bytes
-rw-r--r--application/spidlib/inc/system.h93
-rw-r--r--application/spidlib/src/system/image_desc.c110
-rw-r--r--application/spidlib/src/system/nvram.c51
10 files changed, 356 insertions, 77 deletions
diff --git a/application/spidlib/Makefile b/application/spidlib/Makefile
index 1d796598dc..59bc6cc0b0 100644
--- a/application/spidlib/Makefile
+++ b/application/spidlib/Makefile
@@ -4,6 +4,8 @@ INCDIR=inc
OBJDIR=obj
SPIDLIB=spidlib.a
+INCLUDES=spidlib.h ethernet.h network.h plc.h services.h system.h
+
OBJS = 8021x_supplicant_state.o 8021x_supplicant_identity.o 8021x_supplicant_password.o \
8021x_auth_server_addr.o 8021x_auth_server_port.o 8021x_auth_server_secret.o 8021x_auth_mode.o 8021x_auth_state.o \
afe_version.o bands.o bridge.o board_version.o boot_version.o bootlog_messages.o bssid.o\
@@ -11,22 +13,24 @@ OBJS = 8021x_supplicant_state.o 8021x_supplicant_identity.o 8021x_supplicant_p
ip_netmask.o ip_mode.o mac.o mac_limit.o mac_list.o meminfo.o \
mode.o nb_hosts.o processes.o route_default.o snmp_state.o software_version.o serial_state.o \
serial_number.o speed.o static_spy.o system_version.o telnet_state.o tei.o uptime.o \
- vlan.o white_list.o reboot.o ip_gateway.o \
+ vlan.o white_list.o reboot.o ip_gateway.o nvram.o image_desc.o \
snmp_analog_alarm.o snmp_discrete_alarm.o \
admin_logical_id.o manufactory_info.o model_number.o vendor_info.o output_level.o
VPATH=$(SRCDIR) $(INCDIR)
-INCLUDE=-Iinc -I$(TOPDIR)/include/plc-drv
+INCLUDE=-Iinc -I$(TOPDIR)/include/plc-drv -I$(TOPDIR)/include
+
+export OBJS
CFLAGS=-march=armv5 -msoft-float -Wall #-DSPIDLIB_STUB
CC=arm-uclinux-gcc
AR=arm-uclinux-ar
-.PHONY: all clean distclean
+.PHONY: all clean distclean check
all: $(SPIDLIB)
-$(SPIDLIB): $(OBJS)
+$(SPIDLIB): $(OBJS) $(INCLUDES)
$(AR) -rco $@ $(OBJDIR)/*.o
%.o: %.c $(OBJDIR)
@@ -35,6 +39,9 @@ $(SPIDLIB): $(OBJS)
$(OBJDIR):
mkdir -p $@
+check:
+ make -C check all
+
distclean: clean
rm -f $(SRCDIR)/*~ $(INCDIR)/*~ *~ *.gdb
diff --git a/application/spidlib/Makefile_test b/application/spidlib/Makefile_test
index 99de7d8264..da71f9becc 100644
--- a/application/spidlib/Makefile_test
+++ b/application/spidlib/Makefile_test
@@ -18,7 +18,7 @@ OBJS = 8021x_supplicant_state.o 8021x_supplicant_identity.o 8021x_supplicant_p
VPATH = $(SRCDIR) $(INCDIR)
-INCLUDE = -I./inc -I$(TOPDIR)/include/plc-drv
+INCLUDE = -I./inc -I$(TOPDIR)/include/plc-drv -I$(TOPDIR)/include
CFLAGS = -s -Os -Wall -DTEST
CC = gcc
diff --git a/application/spidlib/check/Makefile b/application/spidlib/check/Makefile
new file mode 100644
index 0000000000..871a3a5ccd
--- /dev/null
+++ b/application/spidlib/check/Makefile
@@ -0,0 +1,25 @@
+PROGS = check
+
+FILE = ethernet system #network plc services system varfunc
+CHECK_OBJS = $(FILE:%=check_%.o)
+VPATH = ../src:../src/system:../src/plc:../src/ethernet:../src/network:../src/services:../src/varfunc
+
+CFLAGS = -I../inc -I../../../include -I../../../include/plc-drv -I../../../linux-2.6.10/include \
+ -O2 -DCHECK
+LDFLAGS = -lcheck
+CC = gcc
+LD = gcc
+
+.PHONY: all clean
+
+all: $(PROGS)
+
+$(PROGS): $(OBJS) $(CHECK_OBJS) check.o
+ $(LD) -o $@ $^ $(LDFLAGS)
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+clean:
+ rm -f *.o *~ $(PROGS)
+
diff --git a/application/spidlib/check/check.c b/application/spidlib/check/check.c
new file mode 100644
index 0000000000..bde8a7ff2d
--- /dev/null
+++ b/application/spidlib/check/check.c
@@ -0,0 +1,29 @@
+#include <stdlib.h>
+#include <check.h>
+
+extern Suite *ethernet_suite(void);
+extern Suite *network_suite(void);
+extern Suite *plc_suite(void);
+extern Suite *services_suite(void);
+extern Suite *system_suite(void);
+extern Suite *varfunc_suite(void);
+
+int main(void)
+{
+ int nf;
+
+ Suite *s = suite_create("spidlib");
+
+ SRunner *sr = srunner_create(s);
+ srunner_add_suite(sr, ethernet_suite());
+ /*srunner_add_suite(sr, network_suite());
+ srunner_add_suite(sr, plc_suite());
+ srunner_add_suite(sr, services_suite());*/
+ srunner_add_suite(sr, system_suite());
+ /*srunner_add_suite(sr, varfunc_suite());*/
+ srunner_run_all(sr, CK_NORMAL);
+ nf = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
diff --git a/application/spidlib/check/check_ethernet.c b/application/spidlib/check/check_ethernet.c
new file mode 100644
index 0000000000..6bb5916cc1
--- /dev/null
+++ b/application/spidlib/check/check_ethernet.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <check.h>
+
+START_TEST (test_get_ethernet_duplex)
+{
+ char buffer[32];
+ fail_unless(((spidlib_get_ethernet_duplex(NULL, 32) < 0)
+ && (errno == EINVAL)),
+ "buffer = NULL");
+
+ fail_unless(((spidlib_get_ethernet_duplex(buffer, -1) < 0)
+ && (errno == EINVAL)),
+ "buffer_len < 0");
+
+ fail_unless(((spidlib_get_ethernet_duplex(buffer, 1) < 0)
+ && (errno == ENOSPC)),
+ "buffer_len = 1");
+
+ freopen("test_duplex1.txt", "r", stdin);
+ fail_unless(((spidlib_get_ethernet_duplex(buffer, 32) >= 0)
+ && !strcmp(buffer, "half")),
+ "spidlib_get_ethernet_duplex");
+}
+END_TEST
+
+START_TEST (test_get_ethernet_speed)
+{
+
+}
+END_TEST
+
+TCase *
+ethernet_duplex_tcase (void)
+{
+ TCase *tc = tcase_create ("ethernet_duplex");
+ tcase_add_test (tc, test_get_ethernet_duplex);
+ return tc;
+}
+
+TCase *
+ethernet_speed_tcase (void)
+{
+ TCase *tc = tcase_create ("ethernet_speed");
+ tcase_add_test (tc, test_get_ethernet_speed);
+ return tc;
+}
+
+Suite *
+ethernet_suite (void)
+{
+ Suite *s = suite_create ("ethernet");
+ suite_add_tcase (s, ethernet_duplex_tcase ());
+ suite_add_tcase (s, ethernet_speed_tcase ());
+ return s;
+}
+
diff --git a/application/spidlib/check/check_system.c b/application/spidlib/check/check_system.c
new file mode 100644
index 0000000000..3d9bb3f421
--- /dev/null
+++ b/application/spidlib/check/check_system.c
@@ -0,0 +1,50 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <check.h>
+#include "nvram.h"
+
+START_TEST (test_get_nvram)
+{
+ spidcom_nvram_t nvram;
+ fail_unless(((spidlib_get_nvram(NULL) < 0)
+ && (errno == EINVAL)),
+ "nvram = NULL");
+
+ fail_unless(((spidlib_get_nvram(&nvram) >= 0)
+ && !strcmp(nvram.serialNumber, "999999")),
+ "serialNumber = '%s'", nvram.serialNumber);
+}
+END_TEST
+
+START_TEST (test_get_image_desc)
+{
+
+}
+END_TEST
+
+TCase *
+system_nvram_tcase (void)
+{
+ TCase *tc = tcase_create ("system_nvram");
+ tcase_add_test (tc, test_get_nvram);
+ return tc;
+}
+
+TCase *
+system_image_desc_tcase (void)
+{
+ TCase *tc = tcase_create ("system_image_desc");
+ tcase_add_test (tc, test_get_image_desc);
+ return tc;
+}
+
+Suite *
+system_suite (void)
+{
+ Suite *s = suite_create ("system");
+ suite_add_tcase (s, system_nvram_tcase ());
+ suite_add_tcase (s, system_image_desc_tcase ());
+ return s;
+}
+
diff --git a/application/spidlib/check/nvram.bin b/application/spidlib/check/nvram.bin
new file mode 100644
index 0000000000..76b1dc76f6
--- /dev/null
+++ b/application/spidlib/check/nvram.bin
Binary files differ
diff --git a/application/spidlib/inc/system.h b/application/spidlib/inc/system.h
index 07271b4736..03a0f6bcdc 100644
--- a/application/spidlib/inc/system.h
+++ b/application/spidlib/inc/system.h
@@ -13,103 +13,52 @@
#ifndef _SYSTEM_H_
#define _SYSTEM_H_
+#include "nvram.h"
+#include "image_desc.h"
+
#define SPIDLIB_ADMIN_LOGICAL_ID_STRING_MAX_LENGTH 40
#define SPIDLIB_ADMIN_LOGICAL_ID_NAME "ADMIN_LOGICAL_ID"
-/*---------------------- Function declarations ------------------------------*/
+#define UPTIME_FILE "/proc/uptime"
+#define BOOTLOG_MESSAGES_FILE "/var/log/messages"
+#define CPU_INFO_FILE "/proc/cpuinfo"
+#define MAX_HOSTNAME_LENGTH 128
+#define INTERRUPTS_FILE "/proc/interrupts"
+#define MEMINFO_FILE "/proc/meminfo"
+#define PROCESSES_COMMAND "ps"
+#define SYSTEM_VERSION_FILE "/proc/version"
+
+typedef enum {
+ SPIDLIB_IMAGE_DESC_TYPE_CURRENT,
+ SPIDLIB_IMAGE_DESC_TYPE_ALTERNATE,
+ SPIDLIB_IMAGE_DESC_TYPE_0,
+ SPIDLIB_IMAGE_DESC_TYPE_1,
+ SPIDLIB_IMAGE_DESC_TYPE_MAX
+} spidlib_image_desc_type_t;
extern int spidlib_get_admin_logical_id(char *admin_logical_id);
extern int spidlib_check_admin_logical_id(const char *admin_logical_id);
extern int spidlib_set_admin_logical_id(const char *admin_logical_id);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_afe_version(char *text, int buffer_len);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_board_version(char *text, int buffer_len);
-
-#define BOOTLOG_MESSAGES_FILE "/var/log/messages"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_bootlog_messages(char *text, int buffer_len);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_boot_version(char *text, int buffer_len);
-
-
-#define CPU_INFO_FILE "/proc/cpuinfo"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_cpuinfo(char *text, int buffer_len);
-
-
-#define MAX_HOSTNAME_LENGTH 128
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_hostname(char *text, int buffer_len);
extern int spidlib_check_hostname(char *text);
extern int spidlib_set_hostname(char *text);
-
-#define INTERRUPTS_FILE "/proc/interrupts"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_interrupts(char *text, int buffer_len);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_manufactory_info(char *manufactory_info);
-
-#define MEMINFO_FILE "/proc/meminfo"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_meminfo(char *text, int buffer_len);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_model_number(char *model_number);
-
-#define PROCESSES_COMMAND "ps"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_processes(char *text, int buffer_len);
-
-#define _REBOOT_H_
-
-/*---------------------- Function declarations ------------------------------*/
-
extern void spidlib_reboot(void);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_serial_number(char *text, int buffer_len);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_software_version(char *text, int buffer_len);
-
-#define SYSTEM_VERSION_FILE "/proc/version"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_system_version(char *text, int buffer_len);
-
-#define UPTIME_FILE "/proc/uptime"
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_uptime(char *text, int buffer_len);
-
-/*---------------------- Function declarations ------------------------------*/
-
extern int spidlib_get_vendor_info(char *vendor_info);
+extern int spidlib_get_nvram(spidcom_nvram_t *nvram);
+extern int spidlib_get_image_desc(spidlib_image_desc_type_t type, spidcom_image_desc_t *image_desc, char *mtd_name);
#endif /* _SYSTEM_H_ */
diff --git a/application/spidlib/src/system/image_desc.c b/application/spidlib/src/system/image_desc.c
new file mode 100644
index 0000000000..e95d5907ef
--- /dev/null
+++ b/application/spidlib/src/system/image_desc.c
@@ -0,0 +1,110 @@
+/*-----------------------------------------------------------------------------
+ File: nvram.c
+ Description: Function to get NVRAM content.
+ Project: SPiDLIB
+ Target: SPiDCOM modem w/ SPC200
+ Version: 0.2
+ Revision list: -
+ Company: SPiDCOM
+ Author: Jérôme Lefranc
+ Date: November 2008.
+-----------------------------------------------------------------------------*/
+
+/*---------------------- Include files --------------------------------------*/
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include "spidlib.h"
+
+/*-----------------------------------------------------------------------------
+ Function: spidlib_get_image_desc
+ Note: read the NVRAM data and the spidcom_nvram_t structure with it
+-----------------------------------------------------------------------------*/
+int
+spidlib_get_image_desc(spidlib_image_desc_type_t type, spidcom_image_desc_t *image_desc, char *mtd_name)
+{
+ int fd0, fd1;
+ spidcom_image_desc_t image_desc_temp[2];
+ static char *mtd_name_temp[2] = { SPIDCOM_IMG_DESC_MTD_NAME_0, SPIDCOM_IMG_DESC_MTD_NAME_1 };
+ int found_index = -1;
+
+ if((image_desc == NULL) || (mtd_name == NULL))
+ {
+ errno = EINVAL;
+ return -errno;
+ }
+
+ if((type < SPIDLIB_IMAGE_DESC_TYPE_CURRENT) || (type >= SPIDLIB_IMAGE_DESC_TYPE_MAX)) {
+ errno = EINVAL;
+ return -errno;
+ }
+
+ /* open the MTD devices for partition #0 and #1 */
+ if((fd0 = open(mtd_name_temp[0], O_RDONLY)) < 0)
+ return -errno;
+ if((fd1 = open(mtd_name_temp[1], O_RDONLY)) < 0)
+ {
+ close(fd0);
+ return -errno;
+ }
+
+ /* get image descriptor infos from both MTD */
+ if((read(fd0, &image_desc_temp[0], sizeof(spidcom_image_desc_t)) < sizeof(spidcom_image_desc_t))
+ || (read(fd1, &image_desc_temp[1], sizeof(spidcom_image_desc_t)) < sizeof(spidcom_image_desc_t)))
+ {
+ close(fd0);
+ close(fd1);
+ return -errno;
+ }
+ close(fd0);
+ close(fd1);
+
+ /* give asked type */
+ switch(type)
+ {
+ case SPIDLIB_IMAGE_DESC_TYPE_0 :
+ if(SPIDCOM_IMG_DESC_IS_VALID(image_desc_temp))
+ found_index = 0;
+ break;
+
+ case SPIDLIB_IMAGE_DESC_TYPE_1:
+ if(SPIDCOM_IMG_DESC_IS_VALID((image_desc_temp + 1)))
+ found_index = 1;
+ break;
+
+ case SPIDLIB_IMAGE_DESC_TYPE_CURRENT:
+ case SPIDLIB_IMAGE_DESC_TYPE_ALTERNATE:
+ /* check image validity */
+ if(!SPIDCOM_IMG_DESC_IS_VALID(image_desc_temp))
+ found_index = 1;
+ else if(!SPIDCOM_IMG_DESC_IS_VALID((image_desc_temp + 1)))
+ found_index = 0;
+ else
+ {
+ /* both are valid, find the highest index */
+ if(image_desc[0].index > image_desc[1].index)
+ found_index = 0;
+ else
+ found_index = 1;
+ }
+ /* give alternate index if asked */
+ if(type == SPIDLIB_IMAGE_DESC_TYPE_ALTERNATE)
+ found_index = (found_index + 1) % 2;
+ break;
+
+ default:
+ /* hum : not possible */
+ break;
+ }
+ if(index < 0)
+ {
+ errno = ENOENT;
+ return -errno;
+ }
+ memcpy(image_desc, &image_desc_temp[found_index], sizeof(spidcom_image_desc_t));
+ strcpy(mtd_name, mtd_name_temp[found_index]);
+ return 0;
+}
diff --git a/application/spidlib/src/system/nvram.c b/application/spidlib/src/system/nvram.c
new file mode 100644
index 0000000000..e2c8062a14
--- /dev/null
+++ b/application/spidlib/src/system/nvram.c
@@ -0,0 +1,51 @@
+/*-----------------------------------------------------------------------------
+ File: nvram.c
+ Description: Function to get NVRAM content.
+ Project: SPiDLIB
+ Target: SPiDCOM modem w/ SPC200
+ Version: 0.2
+ Revision list: -
+ Company: SPiDCOM
+ Author: Jérôme Lefranc
+ Date: November 2008.
+-----------------------------------------------------------------------------*/
+
+/*---------------------- Include files --------------------------------------*/
+
+#include <stdio.h>
+#include "spidlib.h"
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#ifdef CHECK
+#undef NVRAM_MTD_NAME
+#define NVRAM_MTD_NAME "./nvram.bin"
+#endif
+
+/*-----------------------------------------------------------------------------
+ Function: spidlib_get_nvram
+ Note: read the NVRAM data and the spidcom_nvram_t structure with it
+-----------------------------------------------------------------------------*/
+int
+spidlib_get_nvram(spidcom_nvram_t *nvram)
+{
+ int fd;
+
+ if(nvram == NULL) {
+ errno = EINVAL;
+ return -errno;
+ }
+
+ if((fd = open(NVRAM_MTD_NAME, O_RDONLY)) < 0)
+ return -errno;
+
+ if(read(fd, nvram, sizeof(spidcom_nvram_t)) < sizeof(spidcom_nvram_t))
+ {
+ close(fd);
+ return -errno;
+ }
+
+ close(fd);
+ return 0;
+}