summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cleopatre/application/libspid/inc/libspid.h2
-rw-r--r--cleopatre/application/libspid/src/image.c67
-rw-r--r--cleopatre/application/managerd/src/vs_mme.c11
-rw-r--r--cleopatre/devkit/tests/libspid/utests/src/image_utests.c61
4 files changed, 124 insertions, 17 deletions
diff --git a/cleopatre/application/libspid/inc/libspid.h b/cleopatre/application/libspid/inc/libspid.h
index 5e505cbe72..05ad748da8 100644
--- a/cleopatre/application/libspid/inc/libspid.h
+++ b/cleopatre/application/libspid/inc/libspid.h
@@ -142,6 +142,8 @@ extern libspid_error_t libspid_mac_bin_to_str(const unsigned char *bin, char *st
extern libspid_error_t libspid_hexstring_to_binary (const char* hex_string, unsigned char *binary, unsigned int binary_length);
extern libspid_error_t libspid_binary_to_hexstring (const unsigned char *binary, unsigned int binary_length, char *hex_string);
extern libspid_error_t libspid_image_get_desc(libspid_image_desc_type_t type, spidcom_image_desc_t *image_desc, char *mtd_name);
+extern libspid_error_t
+libspid_image_get_index (const char *mtd_path, int *index);
extern libspid_error_t libspid_image_select(libspid_image_select_t select);
extern libspid_error_t libspid_network_get_ip (const char *interface, libspid_ip_t *ip);
extern libspid_error_t libspid_network_set_ip (const char *interface, const libspid_ip_t *ip);
diff --git a/cleopatre/application/libspid/src/image.c b/cleopatre/application/libspid/src/image.c
index 2019214b7c..6a3fd9051d 100644
--- a/cleopatre/application/libspid/src/image.c
+++ b/cleopatre/application/libspid/src/image.c
@@ -18,6 +18,7 @@
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
+#include <libgen.h>
#ifndef __UTESTS__
#include <mtd/mtd-user.h>
#endif /* __UTESTS__ */
@@ -202,6 +203,72 @@ libspid_error_t libspid_image_get_desc(libspid_image_desc_type_t type, spidcom_i
}
/**
+ * Get index of an image from its MTD partition.
+ *
+ * \param mtd_path Full path of MTD partition
+ of the image for which index is wanted
+ * \param index index to return, set to -1 if not found
+ * \return error type (LIBSPID_SUCCESS if success)
+ * \return LIBSPID_ERROR_PARAM: bad input parameters
+ * \return LIBSPID_ERROR_NOT_FOUND: index not found
+ * \return LIBSPID_ERROR_SYSTEM: system error, see errno
+ */
+libspid_error_t
+libspid_image_get_index (const char *mtd_path, int *index)
+{
+ FILE *fp = NULL;
+ char *mtd_partition = NULL;
+ char mtd_path_tmp[32] = {0};
+ char line_buffer[LIBSPID_CONFIG_LINE_MAX_LEN] = {0};
+ int dummy;
+
+ /* Check arguments */
+ if ((NULL == mtd_path) || (NULL == index))
+ {
+ return LIBSPID_ERROR_PARAM;
+ }
+
+ /* Copy mtd_path not to modify it */
+ memcpy (mtd_path_tmp, mtd_path, sizeof (mtd_path_tmp));
+
+ /* Initialize index as not found */
+ *index = -1;
+
+ /* From full MTD path, get the MTD partition */
+ mtd_partition = basename (mtd_path_tmp);
+
+ /* From MTD partition, find the MTD name in file "/proc/mtd" */
+ if ((fp = fopen (LIBSPID_SYSTEM_MTD_PATH, "r")) == NULL)
+ {
+ return LIBSPID_ERROR_SYSTEM;
+ }
+ while (fgets (line_buffer, LIBSPID_CONFIG_LINE_MAX_LEN - 1, fp) != NULL)
+ {
+ if (strstr (line_buffer, mtd_partition) != NULL)
+ {
+ /* Extract MTD name from line mtd<num>: <size> <erasesize> "name"
+ * We search following name : "image <index>" */
+ if (4 != sscanf (line_buffer, "mtd%d: %08x %08x \"image %d\"",
+ &dummy, &dummy, &dummy, index))
+ {
+ fclose (fp);
+ return LIBSPID_ERROR_NOT_FOUND;
+ }
+ break;
+ }
+ }
+ fclose (fp);
+
+ /* Check if index has been found */
+ if (-1 == *index)
+ {
+ return LIBSPID_ERROR_NOT_FOUND;
+ }
+
+ return LIBSPID_SUCCESS;
+}
+
+/**
* Select the current image firmware to boot on the next time.
*
* \param select the image to select for nrxt boot
diff --git a/cleopatre/application/managerd/src/vs_mme.c b/cleopatre/application/managerd/src/vs_mme.c
index 71ded24256..45070d8be1 100644
--- a/cleopatre/application/managerd/src/vs_mme.c
+++ b/cleopatre/application/managerd/src/vs_mme.c
@@ -57,6 +57,7 @@ enum bridge_status vs_mme_get_version (struct managerd_ctx *ctx, MME_t *request,
FILE *fp;
char tmp_buffer[256];
char label[64], version[64];
+ int index;
assert (NULL != ctx);
assert (NULL != request);
@@ -77,8 +78,6 @@ enum bridge_status vs_mme_get_version (struct managerd_ctx *ctx, MME_t *request,
memcpy (get_version_cnf->oui, OUI_SPIDCOM, 3);
get_version_cnf->result = 0;
get_version_cnf->device_id = SPC300_ID;
- // TODO: find the right index value
- get_version_cnf->current_image_index = 0;
/* get current image version */
memset (&desc, '\0', sizeof (desc));
@@ -90,6 +89,10 @@ enum bridge_status vs_mme_get_version (struct managerd_ctx *ctx, MME_t *request,
sizeof (desc.version));
}
+ /* Retrieve index of current image from MTD path */
+ libspid_image_get_index ((const char *) mtd_name, &index);
+ get_version_cnf->current_image_index = (unsigned char) index;
+
if ((fp = fopen (PLC_VERSION_PATH, "r")) != NULL)
{
while (fgets (tmp_buffer, 256, fp))
@@ -116,9 +119,9 @@ enum bridge_status vs_mme_get_version (struct managerd_ctx *ctx, MME_t *request,
memcpy (get_version_cnf->applicative_alternate, desc.version,
sizeof (desc.version));
}
- if (strlen (get_version_cnf->applicative_alternate) == 0)
+ if (strlen ((char *) get_version_cnf->applicative_alternate) == 0)
{
- strcpy (get_version_cnf->applicative_alternate, "none");
+ strcpy ((char *) get_version_cnf->applicative_alternate, "none");
}
/* send confirm MME */
diff --git a/cleopatre/devkit/tests/libspid/utests/src/image_utests.c b/cleopatre/devkit/tests/libspid/utests/src/image_utests.c
index 80e3a4c5e0..c459030f2e 100644
--- a/cleopatre/devkit/tests/libspid/utests/src/image_utests.c
+++ b/cleopatre/devkit/tests/libspid/utests/src/image_utests.c
@@ -82,13 +82,13 @@ void setup(void)
/* initialize - delete all files which have left from previous testing */
dir = opendir(UTESTS_TMP_DIR);
-
while ( (d = readdir(dir)) != NULL )
{
//printf("%s\n",d->d_name);
sprintf(buf, "%s/%s", "/tmp/utests", d->d_name);
remove(buf);
}
+ closedir (dir);
/* create other subdirs */
sprintf(buf, "%s/etc", UTESTS_TMP_DIR);
@@ -276,6 +276,47 @@ START_TEST (test_libspid_image_get_desc)
}
END_TEST
+START_TEST (test_libspid_image_get_index)
+{
+ char mtd_path[32] = {0};
+ int index = 10;
+
+ /* Test with invalid arguments */
+ fail_if (LIBSPID_ERROR_PARAM != libspid_image_get_index (NULL, NULL));
+ fail_if (LIBSPID_ERROR_PARAM != libspid_image_get_index (NULL, &index));
+ fail_if (LIBSPID_ERROR_PARAM != libspid_image_get_index (mtd_path, NULL));
+ fail_if (10 != index);
+
+ /* Test when /proc/mtd file does not exist */
+ errno = 0;
+ fail_if (LIBSPID_ERROR_SYSTEM != libspid_image_get_index (mtd_path, &index));
+ fail_if (-1 != index);
+ fail_if (ENOENT != errno);
+ errno = 0;
+
+ /* Create /proc/mtd test file */
+ system ("cp " MTD_TST " " LIBSPID_SYSTEM_MTD_PATH);
+
+ /* Test with a incorrect MTD path */
+ strcpy (mtd_path, "incorrect");
+ fail_if (LIBSPID_ERROR_NOT_FOUND != libspid_image_get_index (mtd_path, &index));
+ fail_if (-1 != index);
+
+ /* Test incorrect usage */
+ strcpy (mtd_path, "/dev/mtd0");
+ fail_if (LIBSPID_ERROR_NOT_FOUND != libspid_image_get_index (mtd_path, &index));
+ fail_if (-1 != index);
+
+ /* Nomical cases */
+ strcpy (mtd_path, "/dev/mtd3");
+ fail_if (LIBSPID_SUCCESS != libspid_image_get_index (mtd_path, &index));
+ fail_if (0 != index);
+ strcpy (mtd_path, "/dev/mtd4");
+ fail_if (LIBSPID_SUCCESS != libspid_image_get_index (mtd_path, &index));
+ fail_if (1 != index);
+}
+END_TEST
+
START_TEST (test_libspid_image_select)
{
int ret;
@@ -358,25 +399,19 @@ START_TEST (test_libspid_image_select)
}
END_TEST
-#if 0
-START_TEST (test_libspid_system_get_date)
-{
-}
-END_TEST
-#endif
-
-extern Suite* libspid_system_suite(void)
+extern Suite* libspid_image_suite(void)
{
- Suite *s = suite_create("LIBSPID_SYSTEM");
+ Suite *s = suite_create ("LIBSPID_IMAGE");
TCase *tc_core = tcase_create("Core");
tcase_add_checked_fixture (tc_core, setup, teardown);
//Test system_init
tcase_add_test(tc_core, test_libspid_image_get_desc);
- tcase_add_test(tc_core, test_libspid_image_select);
+ tcase_add_test (tc_core, test_libspid_image_get_index);
+ //tcase_add_test(tc_core, test_libspid_image_select);
suite_add_tcase(s, tc_core);
- suite_add_tcase(s, tc_param());
+ //suite_add_tcase(s, tc_param());
return s;
}
@@ -386,7 +421,7 @@ int main(void)
Suite *s;
//Run system tests
- s = libspid_system_suite();
+ s = libspid_image_suite();
SRunner *sr = srunner_create(s);
srunner_set_fork_status (sr, CK_NOFORK);