////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2004 Neuroshare Project
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// A copy of the GNU Lesser General Public License can be obtained by writing to:
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA 02111-1307
// USA
//
// Contact information:
// Kirk Korver
// Cyberkinetics Inc.
// 391 G Chipeta Way
// Salt Lake City, UT 84108
// USA
// kkorver@cyberkineticsinc.com
//
//
// Website:
// www.neuroshare.org
//
// All other copyrights on this material are replaced by this license agreeement.
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// $Workfile: ns.c $
//
// Specification : Glue to a neuroshare library
//
// Authors : Gopal Santhanam
//
// $Date: 4/26/04 2:45p $
//
// $History: ns.c $
//
// ***************** Version 6 *****************
// User: Kkorver Date: 4/26/04 Time: 2:45p
// Updated in $/Neuroshare/Matlab wrapper
// Remove testing of "campatible" libraries
//
// ***************** Version 5 *****************
// User: Kkorver Date: 4/26/04 Time: 2:19p
// Updated in $/Neuroshare/Matlab wrapper
// Added ability to specify library both with and without extension
//
// ***************** Version 4 *****************
// User: Kkorver Date: 4/21/04 Time: 3:12p
// Updated in $/Neuroshare/Matlab wrapper
// Library no-longer automatically adds the 'DLL' or 'SO' extension
//
// ***************** Version 3 *****************
// User: Kkorver Date: 2/03/04 Time: 4:44p
// Updated in $/Neuroshare/nsNEVLibrary/ns
// Updated glue to use current version of API 1.3
//
// ***************** Version 2 *****************
// User: Kkorver Date: 2/03/04 Time: 1:08p
// Updated in $/Neuroshare/ns_NEV/ns
// Converted types from uint32 to UINT32
//
// ***************** Version 1 *****************
// User: Kkorver Date: 1/14/04 Time: 10:33a
// Created in $/Neuroshare/nsNEVLibrary/ns
// Glue for any neuroshare library
//
// $NoKeywords: $
//
//////////////////////////////////////////////////////////////////////////////
/*=========================================================================
| Project : Neuroshare API Glue
| File : ns.c
| Version : v1.01
|--------------------------------------------------------------------------
| Portions of this file are copyright (c) 2002-2003 Total Phase, Inc.
| All rights reserved.
| www.totalphase.com
|
| Redistribution and use in source and binary forms, with or without
| modification, are permitted provided that the following conditions
| are met:
|
| - Redistributions of source code must retain the above copyright
| notice, this list of conditions and the following disclaimer.
|
| - Redistributions in binary form must reproduce the above copyright
| notice, this list of conditions and the following disclaimer in the
| documentation and/or other materials provided with the distribution.
|
| - Neither the name of Total Phase, Inc. nor the names of its
| contributors may be used to endorse or promote products derived from
| this software without specific prior written permission.
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
| FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
| COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
| INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
| BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
| ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
| POSSIBILITY OF SUCH DAMAGE.
|--------------------------------------------------------------------------
|
| API definitions for the Neuroshare file format standard.
|
| Use the following files to access Neuroshare files:
| ns.h -- API header file
| ns.c -- API interface module
|
| A Neuroshare library is required in conjuction with this module.
| For example:
| ns_NEV.so -- Linux shared object
| or
| ns_NEV.dll -- Windows dynamic link library
|
========================================================================*/
/*=========================================================================
| INCLUDES
========================================================================*/
/* This #include can be customized to conform to the user's build paths. */
#include "ns.h"
/*=========================================================================
| VERSION CHECK
========================================================================*/
#define NS_CFILE_VERSION 0x0103 /* v1.03 */
#define NS_REQ_LIB_VERSION 0x0103 /* v1.03 */
/*
* Make sure that the ns.h was included and that
* the version numbers match.
*/
#ifndef NS_HEADER_VERSION
# error Unable to include ns.h file. Please check include path.
#elif NS_HEADER_VERSION != NS_CFILE_VERSION
# error Version mismatch between ns.c and ns.h files.
#endif
/*=========================================================================
| CONSTANTS
========================================================================*/
#define MAX_SO_PATH 256
/*=========================================================================
| LINUX SUPPORT
========================================================================*/
#ifdef linux
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dlfcn.h>
#include <fcntl.h>
#define DLL_HANDLE void *
#define SO_EXT ".so"
#define stricmp strcasecmp
/*
* These functions allow the Linux behavior to emulate
* the Windows behavior as specified below in the Windows
* support section.
*
* First, search for the shared object in the application
* binary path, then in the current working directory.
*
* Searching the application binary path requires /proc
* filesystem support, which is standard in 2.4.x kernels.
*
* If the /proc filesystem is not present, the shared object
* will not be loaded from the execution path unless that path
* is either the current working directory or explicitly
* specified in LD_LIBRARY_PATH.
*/
static int _checkPath (const char *path, char *so_name) {
char *filename = (char *)malloc(strlen(path) +1 + strlen(so_name) +1);
int fd;
/* Check if the file is readable */
sprintf(filename, "%s/%s", path, so_name);
fd = open(filename, O_RDONLY);
if (fd >= 0) {
strncpy(so_name, filename, MAX_SO_PATH);
close(fd);
}
/* Clean up and exit */
free(filename);
return (fd >= 0);
}
static void _setSearchPath (char *so_name) {
char path[MAX_SO_PATH+1];
int count;
char *p;
/* Make sure that SO_NAME is not an absolute path. */
if (so_name[0] == '/') return;
/* Check the execution directory name. */
count = readlink("/proc/self/exe", path, MAX