2015-12-12 16:43:32 +01:00
|
|
|
/****************************************************************************
|
2021-05-08 20:33:52 +02:00
|
|
|
* apps/examples/module/module_main.c
|
2015-12-12 16:43:32 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-12-12 16:43:32 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-12 16:43:32 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-12-12 16:43:32 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2017-01-30 16:04:01 +01:00
|
|
|
#include <sys/boardctl.h>
|
2015-12-12 16:43:32 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-03-26 08:04:50 +01:00
|
|
|
#include <string.h>
|
2015-12-12 16:43:32 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/module.h>
|
2019-12-10 15:39:22 +01:00
|
|
|
#include <nuttx/symtab.h>
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2018-08-05 22:10:44 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
|
2017-01-30 16:04:01 +01:00
|
|
|
# include <nuttx/drivers/ramdisk.h>
|
|
|
|
#endif
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Check configuration. This is not all of the configuration settings that
|
|
|
|
* are required -- only the more obvious.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_MODULE
|
|
|
|
# error "You must select CONFIG_MODULE in your configuration file"
|
|
|
|
#endif
|
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_BUILTINFS)
|
2018-08-06 01:03:08 +02:00
|
|
|
# if !defined(CONFIG_FS_ROMFS) && !defined(CONFIG_FS_CROMFS)
|
2018-08-05 22:10:44 +02:00
|
|
|
# error "You must select CONFIG_FS_ROMFS or CONFIG_FS_CROMFS in your configuration file"
|
2017-01-30 16:04:01 +01:00
|
|
|
# endif
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2017-01-30 16:04:01 +01:00
|
|
|
# ifdef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
# error "You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file"
|
|
|
|
# endif
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2018-08-06 01:03:08 +02:00
|
|
|
# if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
|
|
|
|
/* Describe the ROMFS file system */
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2018-08-05 22:10:44 +02:00
|
|
|
# define SECTORSIZE 64
|
|
|
|
# define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
|
|
|
|
# define MOUNTPT "/mnt/romfs"
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2018-08-05 22:10:44 +02:00
|
|
|
# ifndef CONFIG_EXAMPLES_MODULE_DEVMINOR
|
|
|
|
# define CONFIG_EXAMPLES_MODULE_DEVMINOR 0
|
|
|
|
# endif
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2018-08-05 22:10:44 +02:00
|
|
|
# ifndef CONFIG_EXAMPLES_MODULE_DEVPATH
|
|
|
|
# define CONFIG_EXAMPLES_MODULE_DEVPATH "/dev/ram0"
|
|
|
|
# endif
|
2018-08-06 01:03:08 +02:00
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
# elif defined(CONFIG_EXAMPLES_MODULE_CROMFS)
|
2018-08-05 22:10:44 +02:00
|
|
|
/* Describe the CROMFS file system */
|
|
|
|
|
|
|
|
# define MOUNTPT "/mnt/cromfs"
|
2017-01-30 16:04:01 +01:00
|
|
|
# endif
|
2018-08-05 22:10:44 +02:00
|
|
|
|
|
|
|
# define BINDIR MOUNTPT
|
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
#elif defined(CONFIG_EXAMPLES_MODULE_FSMOUNT)
|
2018-08-06 01:03:08 +02:00
|
|
|
/* Describe how to auto-mount the external file system */
|
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
# define MOUNTPT "/mnt/" CONFIG_EXAMPLES_MODULE_FSTYPE
|
|
|
|
# define BINDIR MOUNTPT
|
|
|
|
|
2017-01-30 16:04:01 +01:00
|
|
|
#else
|
2018-08-06 01:03:08 +02:00
|
|
|
/* Describe how to use the pre-mounted external file system */
|
|
|
|
|
2018-08-05 22:10:44 +02:00
|
|
|
# define BINDIR CONFIG_EXAMPLES_MODULE_BINDIR
|
2018-08-05 23:35:38 +02:00
|
|
|
|
2017-01-30 16:04:01 +01:00
|
|
|
#endif /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-15 05:03:12 +02:00
|
|
|
static const char g_write_string[] = "Hi there installed driver\n";
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Symbols from Auto-Generated Code
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-14 05:32:35 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS) || defined(CONFIG_EXAMPLES_MODULE_CROMFS)
|
|
|
|
extern const unsigned char romfs_img[];
|
|
|
|
extern const unsigned int romfs_img_len;
|
|
|
|
#endif
|
|
|
|
|
2018-08-07 17:15:01 +02:00
|
|
|
#ifdef CONFIG_BUILD_FLAT
|
2018-07-12 17:53:38 +02:00
|
|
|
extern const struct symtab_s g_mod_exports[];
|
|
|
|
extern const int g_mod_nexports;
|
2018-08-07 17:15:01 +02:00
|
|
|
#endif
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: module_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2018-08-07 17:15:01 +02:00
|
|
|
#ifdef CONFIG_BUILD_FLAT
|
2015-12-13 15:14:44 +01:00
|
|
|
struct boardioc_symtab_s symdesc;
|
2018-08-07 17:15:01 +02:00
|
|
|
#endif
|
2021-05-13 11:24:47 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_BUILTINFS) && \
|
|
|
|
defined(CONFIG_EXAMPLES_MODULE_ROMFS)
|
|
|
|
struct boardioc_romdisk_s desc;
|
|
|
|
#endif
|
2018-08-07 18:51:04 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_FSMOUNT) && \
|
2018-08-07 17:15:01 +02:00
|
|
|
defined(CONFIG_EXAMPLES_MODULE_FSREMOVEABLE)
|
2018-08-05 23:35:38 +02:00
|
|
|
struct stat buf;
|
|
|
|
#endif
|
2017-01-22 22:10:12 +01:00
|
|
|
FAR void *handle;
|
2015-12-12 16:43:32 +01:00
|
|
|
char buffer[128];
|
|
|
|
ssize_t nbytes;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
2018-08-07 17:15:01 +02:00
|
|
|
#ifdef CONFIG_BUILD_FLAT
|
2015-12-13 15:14:44 +01:00
|
|
|
/* Set the OS symbol table indirectly through the boardctl() */
|
|
|
|
|
2018-07-12 17:53:38 +02:00
|
|
|
symdesc.symtab = (FAR struct symtab_s *)g_mod_exports;
|
|
|
|
symdesc.nsymbols = g_mod_nexports;
|
2015-12-13 15:14:44 +01:00
|
|
|
ret = boardctl(BOARDIOC_OS_SYMTAB, (uintptr_t)&symdesc);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-05-08 20:33:52 +02:00
|
|
|
fprintf(stderr, "ERROR: boardctl(BOARDIOC_OS_SYMTAB) failed: %s\n",
|
|
|
|
strerror(errno));
|
2015-12-13 15:14:44 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-08-07 17:15:01 +02:00
|
|
|
#endif
|
2015-12-13 15:14:44 +01:00
|
|
|
|
2017-01-30 16:04:01 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_MODULE_BUILTINFS
|
2018-08-05 23:35:38 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
|
2015-12-12 16:43:32 +01:00
|
|
|
/* Create a ROM disk for the ROMFS filesystem */
|
|
|
|
|
2015-12-14 20:21:39 +01:00
|
|
|
printf("main: Registering romdisk at /dev/ram%d\n",
|
|
|
|
CONFIG_EXAMPLES_MODULE_DEVMINOR);
|
|
|
|
|
2021-05-08 20:33:52 +02:00
|
|
|
desc.minor = CONFIG_EXAMPLES_MODULE_DEVMINOR; /* Minor device number of the ROM disk. */
|
|
|
|
desc.nsectors = NSECTORS(romfs_img_len); /* The number of sectors in the ROM disk */
|
|
|
|
desc.sectsize = SECTORSIZE; /* The size of one sector in bytes */
|
|
|
|
desc.image = (FAR uint8_t *)romfs_img; /* File system image */
|
|
|
|
|
|
|
|
ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
|
2015-12-12 16:43:32 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-05-08 20:33:52 +02:00
|
|
|
fprintf(stderr, "ERROR: romdisk_register failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
2015-12-12 16:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mount the file system */
|
|
|
|
|
2015-12-14 20:21:39 +01:00
|
|
|
printf("main: Mounting ROMFS filesystem at target=%s with source=%s\n",
|
2018-08-05 22:10:44 +02:00
|
|
|
MOUNTPT, CONFIG_EXAMPLES_MODULE_DEVPATH);
|
2015-12-12 16:43:32 +01:00
|
|
|
|
2018-08-05 22:10:44 +02:00
|
|
|
ret = mount(CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT, "romfs",
|
|
|
|
MS_RDONLY, NULL);
|
2015-12-12 16:43:32 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: mount(%s,%s,romfs) failed: %s\n",
|
2020-03-26 08:04:50 +01:00
|
|
|
CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT, strerror(errno));
|
2015-12-12 16:43:32 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-08-05 22:10:44 +02:00
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
#elif defined(CONFIG_EXAMPLES_MODULE_CROMFS)
|
2018-08-05 22:10:44 +02:00
|
|
|
/* Mount the CROMFS file system */
|
|
|
|
|
|
|
|
printf("Mounting CROMFS filesystem at target=%s\n", MOUNTPT);
|
|
|
|
|
|
|
|
ret = mount(NULL, MOUNTPT, "cromfs", MS_RDONLY, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errmsg("ERROR: mount(%s, cromfs) failed: %d\n", MOUNTPT, errno);
|
|
|
|
}
|
|
|
|
|
2018-08-05 23:35:38 +02:00
|
|
|
#endif /* CONFIG_EXAMPLES_MODULE_ROMFS */
|
2020-01-31 16:18:05 +01:00
|
|
|
#else /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
|
2018-08-05 23:35:38 +02:00
|
|
|
/* An external file system is being used */
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_FSMOUNT)
|
|
|
|
#if defined(CONFIG_EXAMPLES_MODULE_FSREMOVEABLE)
|
|
|
|
/* The file system is removable, wait until the block driver is available */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = stat(CONFIG_EXAMPLES_MODULE_DEVPATH, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
if (errcode == ENOENT)
|
|
|
|
{
|
|
|
|
printf("%s does not exist. Waiting...\n",
|
|
|
|
CONFIG_EXAMPLES_MODULE_DEVPATH);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("ERROR: stat(%s) failed: %d Aborting...\n",
|
|
|
|
CONFIG_EXAMPLES_MODULE_DEVPATH, errcode);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!S_ISBLK(buf.st_mode))
|
|
|
|
{
|
|
|
|
printf("ERROR: stat(%s) exists but is not a block driver: %04x\n",
|
|
|
|
CONFIG_EXAMPLES_MODULE_DEVPATH, buf.st_mode);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret < 0);
|
2020-01-31 16:18:05 +01:00
|
|
|
#endif /* CONFIG_EXAMPLES_MODULE_FSREMOVEABLE */
|
2018-08-05 23:35:38 +02:00
|
|
|
|
|
|
|
/* Mount the external file system */
|
|
|
|
|
2018-08-07 18:51:04 +02:00
|
|
|
printf("Mounting %s filesystem at target=%s\n",
|
|
|
|
CONFIG_EXAMPLES_MODULE_FSTYPE, MOUNTPT);
|
2018-08-05 23:35:38 +02:00
|
|
|
|
|
|
|
ret = mount(CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT,
|
|
|
|
CONFIG_EXAMPLES_MODULE_FSTYPE, MS_RDONLY, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-03-26 08:15:05 +01:00
|
|
|
printf("ERROR: mount(%s, %s, %s) failed: %d\n",
|
2018-08-05 23:35:38 +02:00
|
|
|
CONFIG_EXAMPLES_MODULE_DEVPATH, CONFIG_EXAMPLES_MODULE_FSTYPE,
|
|
|
|
MOUNTPT, errno);
|
|
|
|
}
|
|
|
|
|
2018-08-06 01:03:08 +02:00
|
|
|
#endif /* CONFIG_EXAMPLES_MODULE_FSMOUNT */
|
2017-01-30 16:04:01 +01:00
|
|
|
#endif /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
/* Install the character driver */
|
|
|
|
|
2017-01-30 16:04:01 +01:00
|
|
|
handle = insmod(BINDIR "/chardev", "chardev");
|
2017-01-22 22:10:12 +01:00
|
|
|
if (handle == NULL)
|
2015-12-12 16:43:32 +01:00
|
|
|
{
|
2017-01-22 22:10:12 +01:00
|
|
|
int errcode = errno;
|
2018-08-06 01:03:08 +02:00
|
|
|
fprintf(stderr, "ERROR: insmod(%s/chardev, chardev) failed: %d\n",
|
|
|
|
BINDIR, errcode);
|
2015-12-12 16:43:32 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the installed character driver */
|
|
|
|
|
|
|
|
fd = open("/dev/chardev", O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Failed to open /dev/chardev: %d\n", errcode);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read from the character driver */
|
|
|
|
|
|
|
|
nbytes = read(fd, buffer, 128);
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Read from /dev/chardev failed: %d\n", errcode);
|
|
|
|
close(fd);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:54:36 +01:00
|
|
|
printf("main: Read %d bytes\n", (int)nbytes);
|
2015-12-14 20:21:39 +01:00
|
|
|
lib_dumpbuffer("main: Bytes read", (FAR const uint8_t *)buffer, nbytes);
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
/* Write to the character driver */
|
|
|
|
|
|
|
|
nbytes = write(fd, g_write_string, strlen(g_write_string));
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Write to /dev/chardev failed: %d\n", errcode);
|
|
|
|
close(fd);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:54:36 +01:00
|
|
|
printf("main: Wrote %d bytes\n", (int)nbytes);
|
2020-03-26 08:15:05 +01:00
|
|
|
lib_dumpbuffer("main: Bytes written", (FAR const uint8_t *)g_write_string,
|
|
|
|
nbytes);
|
2015-12-12 16:43:32 +01:00
|
|
|
|
|
|
|
close(fd);
|
2017-01-22 22:10:12 +01:00
|
|
|
ret = rmmod(handle);
|
2015-12-12 18:40:33 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: rmmod failed: %d\n", ret);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-12-12 16:43:32 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|