diff options
author | Frederic PAUT <frederic.paut@linux.intel.com> | 2012-02-08 16:23:18 +0100 |
---|---|---|
committer | Frederic PAUT <frederic.paut@linux.intel.com> | 2012-02-08 16:23:18 +0100 |
commit | 2ecb9890576c6dca460ca09e14ebaeae213bbe4f (patch) | |
tree | d0f00f43d29c2e41fd5f4c06f22249e03f1c58c6 /ncl | |
download | neardal-2ecb9890576c6dca460ca09e14ebaeae213bbe4f.tar.gz neardal-2ecb9890576c6dca460ca09e14ebaeae213bbe4f.tar.bz2 neardal-2ecb9890576c6dca460ca09e14ebaeae213bbe4f.zip |
Initial revision
Diffstat (limited to 'ncl')
-rw-r--r-- | ncl/Makefile.am | 7 | ||||
-rw-r--r-- | ncl/ncl.c | 433 | ||||
-rw-r--r-- | ncl/ncl.h | 57 | ||||
-rw-r--r-- | ncl/ncl_cmd.c | 1111 | ||||
-rw-r--r-- | ncl/ncl_cmd.h | 76 |
5 files changed, 1684 insertions, 0 deletions
diff --git a/ncl/Makefile.am b/ncl/Makefile.am new file mode 100644 index 0000000..b75a2c9 --- /dev/null +++ b/ncl/Makefile.am @@ -0,0 +1,7 @@ +noinst_PROGRAMS=ncl + +ncl_SOURCES = ncl.c \ + ncl_cmd.c + +ncl_CFLAGS = $(GLIB_CFLAGS) -I../lib +ncl_LDADD = $(GLIB_LIBS) ../lib/libneardal.la diff --git a/ncl/ncl.c b/ncl/ncl.c new file mode 100644 index 0000000..dec7697 --- /dev/null +++ b/ncl/ncl.c @@ -0,0 +1,433 @@ +/* + * NEARDAL Tester command line interpreter + * + * Copyright 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <glib.h> +#include <glib-object.h> +#include <signal.h> + +#include <neardal.h> +#include "neardal_traces_prv.h" + +#include "ncl.h" +#include "ncl_cmd.h" +#include <glib-2.0/glib/gstrfuncs.h> + +#define PROMPT_PREFIX "NCL> " +#define NB_MAX_PARAMETERS 20 /* Max number of parameters in a + command */ + +NCLContext gNclCtx; + +char *ncl_error_get_text(NCLError ec) +{ + switch (ec) { + case NCLERR_NOERROR: + return "Success"; + + case NCLERR_GLOBAL_ERROR: + return "General error"; + + case NCLERR_PARSING_PARAMETERS: + return "Invalid parameter"; + + case NCLERR_MEM_ALLOC: + return "Memory allocation error"; + + case NCLERR_INIT: + return "Error while initializing command line interpretor"; + + case NCLERR_LIB_ERROR: + return "Error from Linked Library"; + + default: + return "UNKNOWN ERROR !!!"; + } +} + +static NCLError ncl_prv_split_cmdLine(gchar *cmdLine, int *iArgc, + char *iArgv[]) +{ + NCLError err = NCLERR_NOERROR; + gssize splitPos; + char **argv = NULL; + int *argc; + char *bufPos; + char *prevBufPos; + bool split; + + /* Test input parameters */ + if (!cmdLine || !iArgc || !iArgv) + return NCLERR_PARSING_PARAMETERS; + argv = iArgv; + argc = iArgc; + + /* Splitting parameters list like argc/argv style */ + splitPos = 0; + split = true; + *argc = 0; + (((char **)(argv))[(*argc)++]) = cmdLine; + prevBufPos = cmdLine; + bufPos = prevBufPos; + if (bufPos == NULL) + return NCLERR_PARSING_PARAMETERS; + + while ((*argc) < NB_MAX_PARAMETERS && *bufPos != '\0') { + while(*bufPos != ' ' && *bufPos != '"' && *bufPos != '\0') { + bufPos++; + } + if (*bufPos == '"') { + if (split) + split = false; + else + split = true; + bufPos++; + } + if (bufPos && split == true) { + splitPos += ((gssize)((long)bufPos - (long)prevBufPos)); + cmdLine[splitPos] = '\0'; + bufPos++; + splitPos++; + if (((gssize)((long)bufPos - (long)prevBufPos)) > 0) + (((char **)(argv))[(*argc)++]) = bufPos; + prevBufPos = bufPos; + } else + bufPos++; + } + + return err; +} + +static ncl_cmd_func ncl_prv_find_func(char *cmd) +{ + int index; + NCLCmdInterpretor *it; /* commands interpretor array */ + int nbClCmd; + + it = ncl_cmd_get_list(&nbClCmd); + if (it == NULL) + return NULL; + + for (index = 0; index < nbClCmd; index++) + if (!strncmp(it[index].cmdName, cmd, strlen(cmd))) + return it[index].func; + + return NULL; +} + +NCLError ncl_exec(char *cmdName) +{ + NCLError ret = NCLERR_NOERROR; + GError *gerror = NULL; + ncl_cmd_func funcList = NULL; + char *cmd = NULL; + char *argv[NB_MAX_PARAMETERS]; + int argc; + + /* Duplicate Command line before split */ + cmd = g_strdup(cmdName); + if (cmd == NULL) + return NCLERR_MEM_ALLOC; + + /* Invoking 'list' command to display interpretor commands list */ + memset(argv, 0, sizeof(argv)); + ret = ncl_prv_split_cmdLine(cmd, &argc, argv); + if (ret != NCLERR_NOERROR) { + NCL_CMD_PRINTERR("Error while parsing '%s'\n", cmdName); + goto error; + } + + funcList = ncl_prv_find_func(argv[0]); + if (funcList != NULL) { + ret = (*funcList)(argc, argv); + if (ret < NCLERR_NOERROR) + NCL_CMD_PRINTERR( + "Error command '%s' return err %d (%s)\n", + argv[0], ret, ncl_error_get_text(ret)); + } else { + NCL_CMD_PRINTERR("Unknow NCL function '%s', trying shell...\n", + cmdName); + g_spawn_command_line_async(cmdName, &gerror); + if (gerror != NULL) { + NCL_CMD_PRINTERR("Shell return error %d:%s\n", + gerror->code, gerror->message); + g_error_free(gerror); + } + } + g_free(cmd); + + return ret; + +error: + if (cmd != NULL) + g_free(cmd); + return ret; +} + + +static gboolean ncl_prv_kbinput_cb(GIOChannel *source, GIOCondition condition, + gpointer data) +{ + NCLContext *nclCtx = (NCLContext *) data; + GError *error = NULL; + + switch (condition) { + case G_IO_IN: { + gsize terminator_pos; + GIOStatus status; + NCLCmdContext *nclCmdCtx; + + nclCmdCtx = ncl_cmd_get_ctx(); + if (!nclCmdCtx) + return FALSE; + + status = g_io_channel_read_line_string(source, + nclCmdCtx->clBuf, + &terminator_pos, + &error); + (void) status; /* Remove warning */ + if (nclCmdCtx->clBuf->str) { + nclCmdCtx->clBuf->str[terminator_pos] = '\0'; + + if (nclCmdCtx->clBuf->str[0] != '\0') { + nclCtx->errOnExit = NCLERR_PARSING_PARAMETERS; + nclCtx->errOnExit = ncl_exec( + nclCmdCtx->clBuf->str); + } + g_string_erase(nclCmdCtx->clBuf, 0, -1); + } else + NCL_CMD_PRINTERR("buf is NULL!!!\n"); + } + break; + case G_IO_PRI: + case G_IO_ERR: + case G_IO_HUP: + case G_IO_NVAL: + default: + NCL_CMD_PRINTERR("unhandled condition (%d)\n", condition); + break; + } + ncl_prompt(); + + return TRUE; +} + +void ncl_prompt(void) +{ + ncl_cmd_print(stdout, PROMPT_PREFIX); +} + +NCLContext *ncl_get_ctx(void) +{ + return &gNclCtx; +} + + +void ncl_finalize(void) +{ + NCL_CMD_PRINTIN(); + + /* Freeing command line interpretor context */ + ncl_cmd_finalize(); + + if (gNclCtx.channel) { + g_io_channel_unref(gNclCtx.channel); + g_io_channel_shutdown(gNclCtx.channel, TRUE, NULL); + g_source_remove(gNclCtx.tag); + } + if (gNclCtx.main_loop) + g_main_loop_unref(gNclCtx.main_loop); +} + +/* +static void signal_handler(int signum) +{ + NCL_CMD_PRINTERR("Receive signal %d\n", signum); +} +*/ +static NCLError ncl_prv_init(char *execCmdLineStr) +{ +/* struct sigaction sa; + int err = NCLERR_NOERROR; */ + + /* Initialize Test App context... */ + memset(&gNclCtx, 0, sizeof(gNclCtx)); + +/* + sa.sa_handler = signal_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + if (sigaction(SIGINT, &sa, NULL) == -1) + { + NCL_CMD_PRINTERR("Unable to handle system signals\n"); + return NCLERR_INIT; + } +*/ + + gNclCtx.main_loop = g_main_loop_new(NULL, FALSE); + + /* Initialize command line interpretor context */ + return ncl_cmd_init(execCmdLineStr); +} + +static void ncl_prv_parse_script_file(char *scriptFileStr) +{ + FILE *scriptFile; + char *cmdLineStr = NULL; + size_t cmdLineSize; + ssize_t nbRead; + + /* Opening file */ + scriptFile = fopen(scriptFileStr, "r"); + if (scriptFile == NULL) { + gNclCtx.errOnExit = NCLERR_GLOBAL_ERROR; + return; + } + + do { + /* Reading command line script file */ + nbRead = getline(&cmdLineStr, &cmdLineSize, scriptFile); + if (nbRead > 0 && strlen(cmdLineStr) > 1) { + cmdLineSize = strlen(cmdLineStr); + if (cmdLineStr[0] != '#') { + if (cmdLineStr[cmdLineSize - 1] == '\n') + cmdLineStr[cmdLineSize - 1] = '\0'; + + /* Executing command line */ + ncl_cmd_print(stdout, + "$$$$$$$$$$$$$$$$$$$$$$$$$'\n"); + ncl_cmd_print(stdout, + "Executing '%s'\n", cmdLineStr); + ncl_cmd_print(stdout, + "$$$$$$$$$$$$$$$$$$$$$$$$$'\n"); + gNclCtx.errOnExit = ncl_exec(cmdLineStr); + + /* Main loop */ + do { + g_main_context_iteration(NULL, false); + } while (g_main_context_pending(NULL)); + } + } + /* Freeing command line */ + if (cmdLineStr != NULL) { + free(cmdLineStr); + cmdLineStr = NULL; + } + } while (nbRead > 0 && gNclCtx.errOnExit == NCLERR_NOERROR); + g_free(scriptFile); +} + +int main(int argc, char *argv[]) +{ + NCLError err; + int fd; + GOptionContext *context; + GError *error = NULL; + static char *execCmdLineStr; + static char *scriptFileStr; +static GOptionEntry options[] = { + { "exec" , 'e', 0, G_OPTION_ARG_STRING, &execCmdLineStr, + "Execute specific command line function", "Command Line" }, + { "script", 's', 0, G_OPTION_ARG_STRING , &scriptFileStr + , "Script file to execute", "filename" }, + { NULL, 0, 0, 0, NULL, NULL, NULL} /* End of List */ + }; + + execCmdLineStr = NULL; + scriptFileStr = NULL; + NCL_CMD_PRINTIN(); + + context = g_option_context_new(""); + g_option_context_add_main_entries(context, options, ""); + if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) { + if (error != NULL) { + NCL_CMD_PRINTERR("%s\n", error->message); + g_error_free(error); + } else + NCL_CMD_PRINTERR("An unknown error occurred\n"); + return NCLERR_INIT; + } + g_option_context_free(context); + + err = ncl_prv_init(execCmdLineStr); + if (err != NCLERR_NOERROR) { + ncl_finalize(); + return NCLERR_INIT; + } + + /* Do we have a command line in parameters list ? */ + if (scriptFileStr == NULL) { + /* No, test application executed without a command line in + parameter. Do we have a command in parameters list ? */ + if (execCmdLineStr == NULL) { + /* No, test application executed without a command + line in parameter */ + + /* Wrap stdin (keyboard) on callback */ + fd = fileno(stdin); + gNclCtx.channel = g_io_channel_unix_new(fd); + g_io_channel_set_encoding(gNclCtx.channel, NULL, NULL); + g_io_channel_set_buffered(gNclCtx.channel, TRUE); + gNclCtx.tag = g_io_add_watch(gNclCtx.channel, G_IO_IN, + (GIOFunc) ncl_prv_kbinput_cb, + &gNclCtx); + + /* Invoking 'help' command to display commands line + * list */ + ncl_exec(LISTCMD_NAME); + ncl_prompt(); + + /* Launch main-loop */ + g_main_loop_run(gNclCtx.main_loop); + } else { + int eventsCount = 0; + + /* Yes, test application executed with a command line + * in parameter */ + NCL_CMD_PRINTF("Executing command ('%s')...\n", + execCmdLineStr); + gNclCtx.errOnExit = ncl_exec(execCmdLineStr); + + NCL_CMD_PRINT( + "Command executed('%s'), processing events...", + execCmdLineStr); + do { + NCL_CMD_PRINT("*"); + eventsCount++; + } while (g_main_context_iteration(NULL, false) == true); + NCL_CMD_PRINT("\n"); + NCL_CMD_PRINTF("All events have been processed (%d).\n", + eventsCount); + g_free(execCmdLineStr); + } + } else + ncl_prv_parse_script_file(scriptFileStr); + + err = gNclCtx.errOnExit; + + ncl_finalize(); + + if (err != NCLERR_NOERROR) + NCL_CMD_PRINTERR("Exit with error %d\n", err); + + return err; +} diff --git a/ncl/ncl.h b/ncl/ncl.h new file mode 100644 index 0000000..f1bf162 --- /dev/null +++ b/ncl/ncl.h @@ -0,0 +1,57 @@ +/* + * NEARDAL Tester command line interpreter + * + * Copyright 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + + +#ifndef __NCL_H__ +#define __NCL_H__ + +#include <glib.h> + +/* Buffer size for the command line interpretor */ +#define CL_BUF_SIZE 1024 + +/* Test Application Errors list (Only negative values are errors) */ +typedef enum { + NCLERR_NOERROR_HELP_DISP = 1, + NCLERR_NOERROR = 0, + NCLERR_GLOBAL_ERROR = -1, + NCLERR_PARSING_PARAMETERS = -2, + NCLERR_INIT = -3, + NCLERR_MEM_ALLOC = -4, + NCLERR_LIB_ERROR = -5 +} NCLError; + +/* Test Application Context */ +typedef struct { + GMainLoop *main_loop; + GIOChannel *channel; /* for stdin descriptor */ + guint tag; /* the ID of the source */ + NCLError errOnExit; /* Error returned on exit */ +} NCLContext; + +NCLContext *ncl_get_ctx(void); + +/* Name of the command interpretor to display commands list */ +#define LISTCMD_NAME "help" + +/* Display prompt */ +void ncl_prompt(void); + +#endif /* __NCL_H__ */ diff --git a/ncl/ncl_cmd.c b/ncl/ncl_cmd.c new file mode 100644 index 0000000..524088e --- /dev/null +++ b/ncl/ncl_cmd.c @@ -0,0 +1,1111 @@ +/* + * NEARDAL Tester command line interpreter + * + * Copyright 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> +#include <glib.h> +#include <glib-object.h> + +#include "neardal.h" +#include "ncl.h" +#include "ncl_cmd.h" +#include <lib/neardal.h> +#include <glib-2.0/glib/goption.h> + + +static NCLCmdContext sNclCmdCtx; +/* Some default values... + * Max number of parameters provided in a command */ +#define NB_MAX_PARAMETERS 20 + +typedef struct { + uint32_t idValue; + gchar *capStr; + gchar *helpStr; +} list_inf; + +#define DEFSTR(x) (x, #x) + + +/* Local Utilities functions */ +/****************************************************************************** + * Tool function : help command to dump parameters command + *****************************************************************************/ +static void ncl_cmd_prv_dumpOptions(GOptionEntry *options) +{ + GOptionEntry *optP = options; + char long_nameTmp[20]; + + while (optP->description != NULL) { + snprintf(long_nameTmp, sizeof(long_nameTmp), "--%s", + optP->long_name); + + ncl_cmd_print(stdout, "-%c,\t%s=%s\t%20s\n", optP->short_name, + long_nameTmp, optP->arg_description, + optP->description); + optP++; + } +} + + +/****************************************************************************** + * Tool function : parse parameters command (like g_option_context_parse but + * implicit exit() on '--help' disabled + *****************************************************************************/ +static NCLError ncl_cmd_prv_parseOptions(int *argc, char **argv[], + GOptionEntry *options) +{ + GOptionContext *context; + GError *error = NULL; + NCLError err = NCLERR_NOERROR; + char **argvP; + int argn; + int helpRequested = 0; + + context = g_option_context_new(NULL); + g_option_context_add_main_entries(context, options, NULL); + /* disable '--help' parameter else function 'exit()' is implicitly + * called... */ + g_option_context_set_help_enabled(context, FALSE); + + /* Searching 'help' in arguments */ + argvP = *argv; + argn = *argc; + while (argn && !helpRequested) { + helpRequested = (strstr(*argvP, "help") != NULL); + helpRequested |= (strstr(*argvP, "-h") != NULL); + helpRequested |= (strstr(*argvP, "/?") != NULL); + argn--; + argvP++; + } + + /* if help requested, generate a 'help' display */ + if (helpRequested) { + ncl_cmd_prv_dumpOptions(options); + err = NCLERR_NOERROR_HELP_DISP; + } else { + if (!g_option_context_parse(context, argc, argv, &error)) { + if (error != NULL) { + NCL_CMD_PRINTERR("%s\n", error->message); + g_error_free(error); + } else + NCL_CMD_PRINTERR("Unknown error occurred\n"); + err = NCLERR_GLOBAL_ERROR; + } + } + g_option_context_free(context); + return err; +} + + + +/* BEGIN : Interpretor commands array */ +static NCLCmdInterpretor itFunc[]; + +/****************************************************************************** + * Display Interpretor command list + *****************************************************************************/ +NCLError ncl_cmd_list(int argc, char *argv[]) +{ + int index; + char funcName[50]; + int nbCmd = ncl_cmd_get_nbCmd(); + + (void) argc; /* remove warning */ + (void) argv; /* remove warning */ + + ncl_cmd_print(stdout, "\nCommand line list\n"); + for (index = 0; index < nbCmd; index++) { + snprintf(funcName, sizeof(funcName), "%40s", + itFunc[index].cmdName); + ncl_cmd_print(stdout, "%s :\t%s\n", funcName, + itFunc[index].helpStr); + } + + return 0; +} + + +/****************************************************************************** + * Dump properties of an adapter + *****************************************************************************/ +static void ncl_cmd_prv_dump_adapter(neardal_adapter adapter) +{ + char **protocols; + char **targets; + + NCL_CMD_PRINT("Adapter\n"); + NCL_CMD_PRINT(".. Name:\t\t'%s'\n", adapter.name); + + NCL_CMD_PRINT(".. Polling:\t\t'%s'\n", + adapter.polling ? "TRUE" : "FALSE"); + NCL_CMD_PRINT(".. Powered:\t\t'%s'\n", + adapter.powered ? "TRUE" : "FALSE"); + + targets = adapter.targets; + NCL_CMD_PRINT(".. Number of targets:\t%d\n", adapter.nbTargets); + NCL_CMD_PRINT(".. Targets[]:\t\t"); + if (adapter.nbTargets > 0) { + while ((*targets) != NULL) { + NCL_CMD_PRINT("'%s', ", *targets); + targets++; + } + neardal_free_array(&adapter.targets); + } else + NCL_CMD_PRINT("No targets!"); + NCL_CMD_PRINT("\n"); + + protocols = adapter.protocols; + NCL_CMD_PRINT(".. Number of protocols:\t%d\n", adapter.nbProtocols); + NCL_CMD_PRINT(".. Protocols[]:\t\t"); + if (adapter.nbProtocols > 0) { + while ((*protocols) != NULL) { + NCL_CMD_PRINT("'%s', ", *protocols); + protocols++; + } + neardal_free_array(&adapter.protocols); + } else + NCL_CMD_PRINT("No protocols!"); + NCL_CMD_PRINT("\n"); +} + +/****************************************************************************** + * Dump properties of a target + *****************************************************************************/ +static void ncl_cmd_prv_dump_target(neardal_target target) +{ + char **records; + char **tagTypes; + + NCL_CMD_PRINT("Target:\n"); + NCL_CMD_PRINT(".. Name:\t\t'%s'\n", target.name); + + NCL_CMD_PRINT(".. Type:\t\t'%s'\n", target.type); + + NCL_CMD_PRINT(".. Number of 'Tag Type':%d\n", target.nbTagTypes); + tagTypes = target.tagType; + if (target.nbTagTypes > 0) { + NCL_CMD_PRINT(".. Tags type[]:\t\t"); + while ((*tagTypes) != NULL) { + NCL_CMD_PRINT("'%s', ", *tagTypes); + tagTypes++; + } + NCL_CMD_PRINT("\n"); + neardal_free_array(&target.tagType); + } + + records = target.records; + NCL_CMD_PRINT(".. Number of records:\t%d\n", target.nbRecords); + NCL_CMD_PRINT(".. Records[]:\t\t"); + if (records != NULL) { + while ((*records) != NULL) { + NCL_CMD_PRINT("'%s', ", *records); + records++; + } + neardal_free_array(&target.records); + } else + NCL_CMD_PRINT("No records!"); + + NCL_CMD_PRINT("\n"); + NCL_CMD_PRINT(".. ReadOnly:\t\t%s\n" , + target.readOnly ? "TRUE" : "FALSE"); +} + +/****************************************************************************** + * Dump properties of a record + *****************************************************************************/ +static void ncl_cmd_prv_dump_record(neardal_record record) +{ + NCL_CMD_PRINT("Record\n"); + NCL_CMD_PRINT(".. Name:\t\t%s\n" , record.name); + NCL_CMD_PRINT(".. Encoding:\t\t%s\n" , record.encoding); + NCL_CMD_PRINT(".. HandOver:\t\t%s\n" , + record.handOver ? "TRUE" : "FALSE"); + NCL_CMD_PRINT(".. Language:\t\t%s\n" , record.language); + NCL_CMD_PRINT(".. SmartPoster:\t\t%s\n" , + record.smartPoster ? "TRUE" : "FALSE"); + NCL_CMD_PRINT(".. Action:\t\t%s\n" , record.action); + + NCL_CMD_PRINT(".. Type:\t\t%s\n" , record.type); + NCL_CMD_PRINT(".. Representation:\t%s\n", record.representation); + NCL_CMD_PRINT(".. URI:\t\t\t%s\n" , record.uri); + NCL_CMD_PRINT(".. MIME:\t\t%s\n" , record.mime); +} + +/****************************************************************************** + * neardal_construct : BEGIN + * Instanciate NFC object, create Neard Dbus connection, register Neard's event + *****************************************************************************/ +static void ncl_cmd_cb_adapter_added(const char *adpName, void *user_data) +{ + neardal_t neardalObj = user_data; + errorCode_t ec; + neardal_adapter adapter; + + NCL_CMD_PRINTF("NFC Adapter added '%s'\n", adpName); + ec = neardal_get_adapter_properties(neardalObj, adpName, &adapter); + if (ec == NEARDAL_SUCCESS) + ncl_cmd_prv_dump_adapter(adapter); + else + NCL_CMD_PRINTF( + "Unable to read adapter properties. (error:%d='%s'). exit...\n", + ec, neardal_error_get_text(ec)); + + return; +} + +static void ncl_cmd_cb_adapter_removed(const char *adpName, void * user_data) +{ + neardal_t neardalObj = user_data; + + (void) neardalObj; + + NCL_CMD_PRINTF("NFC Adapter removed '%s'\n", adpName); +} + +static void ncl_cmd_cb_adapter_prop_changed(char *adpName, char *propName, + void *value, void *user_data) +{ + neardal_t neardalObj = user_data; + int polling; + + (void) neardalObj; + if (!strcmp(propName, "Polling")) { + polling = (int)value; + NCL_CMD_PRINTF("Polling=%d\n", polling); + } else + NCL_CMD_PRINTF("Adapter '%s' -> Property=%s=0x%X\n", adpName, + propName, value); + + NCL_CMD_PRINT("\n"); +} + +static void ncl_cmd_cb_target_found(const char *tgtName, void *user_data) +{ + neardal_t neardalObj = user_data; + neardal_target target; + errorCode_t ec; + + NCL_CMD_PRINTF("NFC Target found (%s)\n", tgtName); + + ec = neardal_get_target_properties(neardalObj, tgtName, &target); + if (ec == NEARDAL_SUCCESS) + ncl_cmd_prv_dump_target(target); + else + NCL_CMD_PRINTF( + "Unable to read target properties. (error:%d='%s'). exit...\n", + ec, neardal_error_get_text(ec)); + return; +} + +static void ncl_cmd_cb_target_lost(const char *tgtName, void *user_data) +{ + neardal_t neardalObj = user_data; + + NCL_CMD_PRINTF("NFC Target lost (%s)\n", tgtName); + (void) neardalObj; + +} + +static void ncl_cmd_cb_record_found(const char *rcdName, void *user_data) +{ + neardal_t neardalObj = user_data; + errorCode_t ec; + neardal_record record; + + NCL_CMD_PRINTF("Target Record found (%s)\n", rcdName); + ec = neardal_get_record_properties(neardalObj, rcdName, &record); + if (ec == NEARDAL_SUCCESS) { + ncl_cmd_prv_dump_record(record); +/* NCL_CMD_PRINTF("(Re)Start Poll\n"); + sleep(1); + neardal_start_poll(neardalObj, (char *) rcdName, NULL); */ + } else + NCL_CMD_PRINTF("Read record error. (error:%d='%s').\n", ec, + neardal_error_get_text(ec)); + + return; +} + +static NCLError ncl_cmd_neardal_construct(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + + (void) argc; /* remove warning */ + (void) argv; /* remove warning */ + + /* Construct NEARDAL object */ + sNclCmdCtx.neardalObj = neardal_construct(&ec); + if (sNclCmdCtx.neardalObj != NULL) + NCL_CMD_PRINTF("NFC object constructed"); + else + NCL_CMD_PRINTF("NFC object not constructed"); + if (ec != NEARDAL_SUCCESS) + NCL_CMD_PRINT(" with error:%d='%s'.\n", ec, + neardal_error_get_text(ec)); + NCL_CMD_PRINT("\n"); + + if (sNclCmdCtx.neardalObj == NULL) + goto exit; + + nclErr = NCLERR_NOERROR; + neardal_set_cb_adapter_added(sNclCmdCtx.neardalObj, + ncl_cmd_cb_adapter_added, + sNclCmdCtx.neardalObj); + neardal_set_cb_adapter_removed(sNclCmdCtx.neardalObj, + ncl_cmd_cb_adapter_removed, + sNclCmdCtx.neardalObj); + neardal_set_cb_adapter_property_changed(sNclCmdCtx.neardalObj, + ncl_cmd_cb_adapter_prop_changed, + sNclCmdCtx.neardalObj); + NCL_CMD_PRINTF("NFC adapter callback registered\n"); + neardal_set_cb_target_found(sNclCmdCtx.neardalObj, + ncl_cmd_cb_target_found, + sNclCmdCtx.neardalObj); + neardal_set_cb_target_lost(sNclCmdCtx.neardalObj, + ncl_cmd_cb_target_lost, + sNclCmdCtx.neardalObj); + NCL_CMD_PRINTF("NFC target registered\n"); + neardal_set_cb_record_found(sNclCmdCtx.neardalObj, + ncl_cmd_cb_record_found, + sNclCmdCtx.neardalObj); + NCL_CMD_PRINTF("NFC record callback registered\n"); + +exit: + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + if (ec == NEARDAL_SUCCESS) + nclErr = NCLERR_NOERROR ; + else + nclErr = NCLERR_LIB_ERROR; + + return nclErr; +} +/****************************************************************************** + * neardal_construct : END + *****************************************************************************/ + +/****************************************************************************** + * neardal_get_adapters : BEGIN + * Get adapters List + *****************************************************************************/ +static NCLError ncl_cmd_neardal_get_adapters(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + char **adpArray = NULL; + int adpLen; + int adpOff; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + ec = neardal_get_adapters(sNclCmdCtx.neardalObj, &adpArray, + &adpLen); + if (ec == NEARDAL_SUCCESS) { + adpOff = 0; + /* For each adapter */ + while (adpArray[adpOff] != NULL) + NCL_CMD_PRINT(".. Adapter '%s'\n", + adpArray[adpOff++]); + + neardal_free_array(&adpArray); + } else + NCL_CMD_PRINTF("No adapter\n"); + + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + if (ec == NEARDAL_SUCCESS) + nclErr = NCLERR_NOERROR ; + else + nclErr = NCLERR_LIB_ERROR; + + return nclErr; +} +/****************************************************************************** + * neardal_get_adapters : END + *****************************************************************************/ + +/****************************************************************************** + * ncl_cmd_neardal_get_adapter_properties : BEGIN + * Read adapter properties + *****************************************************************************/ +static NCLError ncl_cmd_neardal_get_adapter_properties(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + char *adapterName = NULL; + neardal_adapter adapter; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + adapterName = argv[1]; + ec = neardal_get_adapter_properties(sNclCmdCtx.neardalObj, + adapterName, &adapter); + + if (ec == NEARDAL_SUCCESS) + ncl_cmd_prv_dump_adapter(adapter); + else { + NCL_CMD_PRINTF("Read adapter properties error:%d='%s'.\n", ec, + neardal_error_get_text(ec)); + return NCLERR_LIB_ERROR; + } + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + return NCLERR_NOERROR; +} +/****************************************************************************** + * ncl_cmd_neardal_get_adapter_properties : END + *****************************************************************************/ + +/****************************************************************************** + * neardal_get_targets : BEGIN + * Get targets List + *****************************************************************************/ +static NCLError ncl_cmd_neardal_get_targets(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + char **tgtArray = NULL; + int tgtLen; + int tgtOff; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + ec = neardal_get_targets(sNclCmdCtx.neardalObj, argv[1], + &tgtArray, &tgtLen); + if (ec == NEARDAL_SUCCESS) { + tgtOff = 0; + /* For each target */ + while (tgtArray[tgtOff] != NULL) + NCL_CMD_PRINT(".. Target '%s'\n", + tgtArray[tgtOff++]); + + neardal_free_array(&tgtArray); + } else + NCL_CMD_PRINTF("No target\n"); + + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + if (ec == NEARDAL_SUCCESS) + nclErr = NCLERR_NOERROR ; + else + nclErr = NCLERR_LIB_ERROR; + + return nclErr; +} +/****************************************************************************** + * neardal_get_targets : END + *****************************************************************************/ + +/****************************************************************************** + * ncl_cmd_neardal_get_target_properties : BEGIN + * Read target properties + *****************************************************************************/ +static NCLError ncl_cmd_neardal_get_target_properties(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + char *targetName = NULL; + neardal_target target; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + + targetName = argv[1]; + ec = neardal_get_target_properties(sNclCmdCtx.neardalObj, targetName, + &target); + + if (ec == NEARDAL_SUCCESS) + ncl_cmd_prv_dump_target(target); + else { + NCL_CMD_PRINTF("Read target properties error:%d='%s'.\n", ec, + neardal_error_get_text(ec)); + return NCLERR_LIB_ERROR; + } + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + return NCLERR_NOERROR; +} +/****************************************************************************** + * ncl_cmd_neardal_get_target_properties : END + *****************************************************************************/ + +/****************************************************************************** + * neardal_get_records : BEGIN + * Get records List + *****************************************************************************/ +static NCLError ncl_cmd_neardal_get_records(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + char **rcdArray = NULL; + int rcdLen; + int rcdOff; + + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + ec = neardal_get_records(sNclCmdCtx.neardalObj, argv[1], + &rcdArray, &rcdLen); + if (ec == NEARDAL_SUCCESS) { + rcdOff = 0; + /* For each record */ + while (rcdArray[rcdOff] != NULL) + NCL_CMD_PRINT(".. Record '%s'\n", + rcdArray[rcdOff++]); + neardal_free_array(&rcdArray); + } else + NCL_CMD_PRINTF("No record\n"); + + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + if (ec == NEARDAL_SUCCESS) + nclErr = NCLERR_NOERROR ; + else + nclErr = NCLERR_LIB_ERROR; + + return nclErr; +} +/****************************************************************************** + * neardal_get_records : END + *****************************************************************************/ + +/****************************************************************************** + * ncl_cmd_neardal_get_record_properties : BEGIN + * Read a specific target + *****************************************************************************/ +static NCLError ncl_cmd_neardal_get_record_properties(int argc, char *argv[]) +{ + errorCode_t ec; + NCLError nclErr; + char *recordName = NULL; + neardal_record record; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + recordName = argv[1]; + ec = neardal_get_record_properties(sNclCmdCtx.neardalObj, recordName, + &record); + if (ec == NEARDAL_SUCCESS) + ncl_cmd_prv_dump_record(record); + else { + NCL_CMD_PRINTF("Read record error:%d='%s'.\n", ec, + neardal_error_get_text(ec)); + return NCLERR_LIB_ERROR; + } + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + return NCLERR_NOERROR; +} +/****************************************************************************** + * ncl_cmd_neardal_get_record_properties : END + *****************************************************************************/ + +/****************************************************************************** + * ncl_cmd_neardal_publish : BEGIN + * write NDEF record to tag + *****************************************************************************/ +static NCLError ncl_cmd_neardal_publish(int argc, char *argv[]) +{ + errorCode_t ec = NEARDAL_SUCCESS; + NCLError nclErr; + static neardal_record rcd; + static int smartPoster; + +static GOptionEntry options[] = { + { "act", 'c', 0, G_OPTION_ARG_STRING, &rcd.action + , "Action", "save"}, + + { "adp", 'a', 0, G_OPTION_ARG_STRING, &rcd.name + , "Adapter name", "/org/neard/nfc0"}, + + { "encoding", 'e', 0, G_OPTION_ARG_STRING, &rcd.encoding + , "Encoding", "UTF-8" }, + + { "lang", 'l', 0, G_OPTION_ARG_STRING , &rcd.language + , "Language", "en" }, + + { "mime", 'm', 0, G_OPTION_ARG_STRING , &rcd.mime + , "Mime-type", "audio/mp3"}, + + { "rep" , 'r', 0, G_OPTION_ARG_STRING , &rcd.representation + , "Representation", "sample text" }, + + { "smt" , 's', 0, G_OPTION_ARG_INT , &smartPoster + , "SmartPoster", "0 or <>0" }, + + { "type", 't', 0, G_OPTION_ARG_STRING, &rcd.type + , "Record type (Text, URI,...", "Text" }, + + { "uri", 'u', 0, G_OPTION_ARG_STRING, &rcd.uri + , "URI", "http://www.intel.com" }, + + { NULL, 0, 0, 0, NULL, NULL, NULL} /* End of List */ + }; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Parse options */ + memset(&rcd, 0, sizeof(neardal_record)); + nclErr = ncl_cmd_prv_parseOptions(&argc, &argv, options); + if (nclErr != NCLERR_NOERROR) + goto exit; + rcd.smartPoster = (smartPoster != 0) ? true : false; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + goto exit; + + ec = neardal_publish(sNclCmdCtx.neardalObj, &rcd); + +exit: + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + if (rcd.action != NULL) + g_free((gchar *) rcd.action); + if (rcd.name != NULL) + g_free((gchar *) rcd.name); + if (rcd.encoding != NULL) + g_free((gchar *) rcd.encoding); + if (rcd.language != NULL) + g_free((gchar *) rcd.language); + if (rcd.mime != NULL) + g_free((gchar *) rcd.mime); + if (rcd.representation != NULL) + g_free((gchar *) rcd.representation); + if (rcd.type != NULL) + g_free((gchar *) rcd.type); + if (rcd.uri != NULL) + g_free((gchar *) rcd.uri); + + return nclErr; +} +/****************************************************************************** + * ncl_cmd_neardal_publish : END + *****************************************************************************/ + + +/****************************************************************************** + * ncl_cmd_neardal_start_poll : BEGIN + * Request Neard to start polling + *****************************************************************************/ +static NCLError ncl_cmd_neardal_start_poll(int argc, char *argv[]) +{ + errorCode_t ec = NEARDAL_SUCCESS; + char *adpName = NULL; + NCLError nclErr; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + /* Start polling if adapter present */ + adpName = argv[1]; + neardal_start_poll(sNclCmdCtx.neardalObj, adpName, &ec); + if (ec != NEARDAL_SUCCESS) { + NCL_CMD_PRINTF("NFC polling activation error:%d='%s'\n", + ec, neardal_error_get_text(ec)); + if (ec == NEARDAL_ERROR_POLLING_ALREADY_ACTIVE) + ec = NEARDAL_SUCCESS; + } + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + return NCLERR_NOERROR; +} +/****************************************************************************** + * ncl_cmd_neardal_start_poll : END + *****************************************************************************/ + +/****************************************************************************** + * ncl_cmd_neardal_stop_poll : BEGIN + * Request Neard to stop polling + *****************************************************************************/ +static NCLError ncl_cmd_neardal_stop_poll(int argc, char *argv[]) +{ + errorCode_t ec = NEARDAL_SUCCESS; + NCLError nclErr = NCLERR_NOERROR; + char *adpName = NULL; + + if (argc <= 1) + return NCLERR_PARSING_PARAMETERS; + + /* Check if NEARDAL object exist */ + if (sNclCmdCtx.neardalObj == NULL) { + NCL_CMD_PRINTERR("Construct NEARDAL object...\n"); + nclErr = ncl_cmd_neardal_construct(argc, argv); + } + if ((nclErr != NCLERR_NOERROR) && (sNclCmdCtx.neardalObj == NULL)) + return nclErr; + + adpName = argv[1]; + neardal_stop_poll(sNclCmdCtx.neardalObj, adpName, &ec); + if (ec != NEARDAL_SUCCESS) { + NCL_CMD_PRINTF("Stop NFC polling error:%d='%s'.\n", ec, + neardal_error_get_text(ec)); + return NCLERR_LIB_ERROR; + } + NCL_CMD_PRINT("\nExit with error code %d:%s\n", ec, + neardal_error_get_text(ec)); + + return NCLERR_NOERROR; +} +/****************************************************************************** + * ncl_cmd_neardal_stop_poll : END + *****************************************************************************/ + +/****************************************************************************** + * test parameter type (sample code) : BEGIN + *****************************************************************************/ +static gboolean test_cb(const gchar *opt, const gchar *arg, gpointer data, + GError **err) +{ + gboolean success = TRUE; + + NCL_CMD_PRINT("Callback invoked from g_option_context_parse()\n"); + NCL_CMD_PRINT("opt = '%s'\n", opt); + NCL_CMD_PRINT("arg = '%s'\n", arg); + NCL_CMD_PRINT("data = 0x%08X\n", data); + NCL_CMD_PRINT("err = 0x%08X\n", err); + if (*err) { + NCL_CMD_PRINT("*err->domain = %d\n", (*err)->domain); + NCL_CMD_PRINT("*err->code = %d\n", (*err)->code); + NCL_CMD_PRINT("*err->message = '%s'\n", (*err)->message); + } + + /* If no parameter, emulate an error */ + if (arg[0] == 0) { + if (err) { + NCL_CMD_PRINTERR( + "missing argument with parameter '%s'\n", opt); + NCL_CMD_PRINTERR("Emulating parsing error\n"); + g_set_error(err, G_OPTION_ERROR, + G_OPTION_ERROR_UNKNOWN_OPTION, + "Missing option (False Error, emulation)"); + + success = FALSE; + } + } + + return success; +} + +/* Parameters Command line test */ +static NCLError ncl_cmd_test_parameters(int argc, char *argv[]) +{ + static int intTmp; + static char *stringTmp; + static double doubleTmp; + static long long int64Tmp; + NCLError err = NCLERR_NOERROR; +static GOptionEntry options[] = { + { "int" , 'i', 0, G_OPTION_ARG_INT , &intTmp + , "Integer parameter", "9999" }, + + { "string", 's', 0, G_OPTION_ARG_STRING , &stringTmp + , "String parameter", "STRING" }, + + { "double", 'd', 0, G_OPTION_ARG_DOUBLE , &doubleTmp + , "Double parameter", "9.99" }, + + { "int64" , 'l', 0, G_OPTION_ARG_INT64 , &int64Tmp + , "Integer64 parameter", "9999" }, + { "cb" , 'c', 0, G_OPTION_ARG_CALLBACK , test_cb + , "Callback test", "9999" }, + { NULL, 0, 0, 0, NULL, NULL, NULL} /* End of List */ + }; + + /* Parse input parameters... */ + intTmp = 0; + stringTmp = NULL; + doubleTmp = 0; + int64Tmp = 0ll; + err = ncl_cmd_prv_parseOptions(&argc, &argv, options); + if (err != NCLERR_NOERROR) + return err; + + /* Check 'int' parameter type... */ + NCL_CMD_PRINT("Integer parameter = %d\n", intTmp); + + /* Check 'string' parameter type... */ + if (stringTmp) { + NCL_CMD_PRINT("String parameter = %s\n", stringTmp); + g_free(stringTmp); stringTmp = NULL; + } + + /* Check 'double' parameter type... */ + NCL_CMD_PRINT("Double parameter = %f\n", doubleTmp); + + /* Check 'int64' parameter type... */ + NCL_CMD_PRINT("Int64 parameter = %lld\n", int64Tmp); + + NCL_CMD_PRINT("The following type have not been tested : "); + NCL_CMD_PRINT("G_OPTION_ARG_FILENAME, G_OPTION_ARG_STRING_ARRAY"); + NCL_CMD_PRINT("G_OPTION_ARG_FILENAME_ARRAY.\n"); + NCL_CMD_PRINT("enjoy...\n"); + + return err; +} +/****************************************************************************** + * test parameter type : END + *****************************************************************************/ + + +/****************************************************************************** + * + *****************************************************************************/ +/* Exiting from command line interpretor */ +static NCLError ncl_cmd_quit(int argc, char *argv[]) +{ + NCLError err = NCLERR_NOERROR; + NCLContext *nclCtxP = NULL; + + (void) argc; /* remove warning */ + (void) argv; /* remove warning */ + nclCtxP = ncl_get_ctx(); + + /* Release NEARDAL object */ + neardal_destroy(sNclCmdCtx.neardalObj); + sNclCmdCtx.neardalObj = NULL; + + /* Quit Main Loop */ + if (nclCtxP) + g_main_loop_quit(nclCtxP->main_loop); + else + err = NCLERR_GLOBAL_ERROR; + + return err; +} +/* END : Interpretor commands */ + + +/****************************************************************************** + * + *****************************************************************************/ +/* Array of command line functions interpretor (alphabetical order) */ +static NCLCmdInterpretor itFunc[] = { + { "get_adapters", + ncl_cmd_neardal_get_adapters, + "Get adapters list"}, + + { "get_adapter_properties", + ncl_cmd_neardal_get_adapter_properties, + "Get adapter properties (1st parameter is adapter name)"}, + + { "get_records", + ncl_cmd_neardal_get_records, + "Get records list (1st parameter is target name)"}, + + { "get_record_properties", + ncl_cmd_neardal_get_record_properties, + "Read a specific record. (1st parameter is record name)"}, + + { "get_targets", + ncl_cmd_neardal_get_targets, + "Get targets list (1st parameter is adapter name)"}, + + { "get_target_properties", + ncl_cmd_neardal_get_target_properties, + "Get target properties (1st parameter is target name)"}, + + { LISTCMD_NAME, + ncl_cmd_list, + "List all available commands. 'cmd' --help -h /? for a specific help" }, + + { "publish", + ncl_cmd_neardal_publish, + "Creates a NDEF record from parametersto be written to an NFC tag"}, + + { "quit", + ncl_cmd_quit, + "Exit from command line interpretor" }, + + { "start_poll", + ncl_cmd_neardal_start_poll, + "Request Neard to start polling (1st parameter is adapter name)"}, + + { "stop_poll", + ncl_cmd_neardal_stop_poll, + "Request Neard to stop polling (1st parameter is adapter name)"}, + + { "test_parameters", + ncl_cmd_test_parameters, + "Simple test to parse input parameters"} +}; +#define NB_CL_FUNC (sizeof(itFunc) / sizeof(NCLCmdInterpretor)) + + +/****************************************************************************** + * + *****************************************************************************/ +void ncl_cmd_print(FILE *stream, char *format, ...) +{ + gchar *bufTrace; + va_list ap; + + va_start(ap, format); + + bufTrace = g_strdup_vprintf(format, ap); + if (bufTrace != NULL) { + fprintf(stream, "%s", bufTrace); + fflush(stream); + } + va_end(ap); + g_free(bufTrace); +} + + +/****************************************************************************** + * + *****************************************************************************/ +NCLCmdInterpretor *ncl_cmd_get_list(int *nbCmd) +{ + if (nbCmd != NULL) + *nbCmd = NB_CL_FUNC; + return itFunc; +} + + +/****************************************************************************** + * + *****************************************************************************/ +int ncl_cmd_get_nbCmd(void) +{ + return NB_CL_FUNC; +} + + +/****************************************************************************** + * + *****************************************************************************/ +NCLCmdContext *ncl_cmd_get_ctx(void) +{ + return &sNclCmdCtx; +} + +/****************************************************************************** + * + *****************************************************************************/ +NCLError ncl_cmd_init(char *execCmdLineStr) +{ + memset(&sNclCmdCtx, 0, sizeof(sNclCmdCtx)); + + if (!execCmdLineStr) + sNclCmdCtx.clBuf = g_string_sized_new(CL_BUF_SIZE); + else + sNclCmdCtx.clBuf = g_string_new(execCmdLineStr); + + if (sNclCmdCtx.clBuf == NULL) { + NCL_CMD_PRINTERR("Unable to allocate %d bytes\n", CL_BUF_SIZE); + return NCLERR_INIT; + } + + return NCLERR_NOERROR; +} + + +/****************************************************************************** + * + *****************************************************************************/ +void ncl_cmd_finalize(void) +{ + + if (sNclCmdCtx.clBuf != NULL) + g_string_free(sNclCmdCtx.clBuf, TRUE); + + /* Release NFC object */ + if (sNclCmdCtx.neardalObj != NULL) + neardal_destroy(sNclCmdCtx.neardalObj); + +} diff --git a/ncl/ncl_cmd.h b/ncl/ncl_cmd.h new file mode 100644 index 0000000..4d6e6c9 --- /dev/null +++ b/ncl/ncl_cmd.h @@ -0,0 +1,76 @@ +/* + * NEARDAL Tester command line interpreter + * + * Copyright 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef __NCL_CMD_H__ +#define __NCL_CMD_H__ + +#include <stdarg.h> +#include <stdio.h> +#include <stdbool.h> + +#include "neardal.h" +#include "ncl.h" + +/* Command Line Interpretor context... */ +typedef struct { + /* NEARDAL Object */ + neardal_t neardalObj; + + /* command line interpretor context */ + GString *clBuf; /* Command line buffer */ + +} NCLCmdContext; + +/* Array prototype of command line functions interpretor */ +typedef NCLError(*ncl_cmd_func)(int argc, char *argv[]); +typedef struct { + char *cmdName; /* Command name */ + ncl_cmd_func func; /* Address of processing function */ + char *helpStr; /* Minimal help */ +} NCLCmdInterpretor; + +/* Initialize/Destroy command line interpretor context */ +NCLError ncl_cmd_init(char *execCmdLineStr); +void ncl_cmd_finalize(void); + +/* Return command line functions */ +NCLCmdInterpretor *ncl_cmd_get_list(int *nbCmd); +int ncl_cmd_get_nbCmd(void); +NCLCmdContext *ncl_cmd_get_ctx(void); + +/* Print out used by command line functions (and prompt) */ +void ncl_cmd_print(FILE *fprintout, char *format, ...); + +#define NCL_CMD_PRINT(format, ...) \ + ncl_cmd_print(stdout, format, ## __VA_ARGS__) + +#define NCL_CMD_PRINTF(format, ...) \ + ncl_cmd_print(stdout, "%s(): " format, __func__, \ + ## __VA_ARGS__) + +#define NCL_CMD_PRINTIN() \ + ncl_cmd_print(stdout, "%s(): Processing...\n", \ + __func__) + +#define NCL_CMD_PRINTERR(format, ...) \ + ncl_cmd_print(stderr, "ERR in %s(): " format, \ + __func__, ## __VA_ARGS__) + +#endif /* __NCL_CMD_H__ */ |