1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/***************************************************************************
lib/ibCmd.c
-------------------
copyright : (C) 2001,2002,2003 by Frank Mori Hess
email : fmhess@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "ib_internal.h"
#include <assert.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <stdlib.h>
int ibcmd(int ud, const void *cmd_buffer, long cnt)
{
ibConf_t *conf;
ssize_t count;
conf = enter_library(ud);
if (conf == NULL)
return exit_library(ud, 1);
// check that ud is an interface board
if (conf->is_interface == 0) {
setIberr(EARG);
return exit_library(ud, 1);
}
count = my_ibcmd(conf, conf->settings.usec_timeout, cmd_buffer, cnt);
if (count < 0)
return exit_library(ud, 1);
if (count != cnt)
return exit_library(ud, 1);
return exit_library(ud, 0);
}
int ibcmda(int ud, const void *cmd_buffer, long cnt)
{
ibConf_t *conf;
ibBoard_t *board;
int retval;
conf = general_enter_library(ud, 1, 0);
if (conf == NULL)
return general_exit_library(ud, 1, 0, 0, 0, 0, 1);
// check that ud is an interface board
if (conf->is_interface == 0) {
setIberr(EARG);
return general_exit_library(ud, 1, 0, 0, 0, 0, 1);
}
board = interfaceBoard(conf);
retval = is_cic(board);
if (retval <= 0) {
if (retval == 0)
setIberr(ECIC);
return general_exit_library(ud, 1, 0, 0, 0, 0, 1);
}
retval = gpib_aio_launch(ud, conf, GPIB_AIO_COMMAND,
(void*)cmd_buffer, cnt);
if (retval < 0)
return general_exit_library(ud, 1, 0, 0, 0, 0, 1);
return general_exit_library(ud, 0, 0, 0, 0, 0, 1);
}
ssize_t my_ibcmd(ibConf_t *conf, unsigned int usec_timeout, const uint8_t *buffer, size_t count)
{
struct gpib_read_write_ioctl cmd;
int retval;
ibBoard_t *board;
board = interfaceBoard(conf);
retval = is_cic(board);
if (retval <= 0) {
if (retval == 0)
setIberr(ECIC);
return -1;
}
assert(sizeof(buffer) <= sizeof(cmd.buffer_ptr));
cmd.buffer_ptr = (uintptr_t)buffer;
cmd.requested_transfer_count = count;
cmd.completed_transfer_count = 0;
cmd.handle = conf->handle;
/* set the suppress setting CMPL flag if an asyc read or write is in progress */
cmd.end = (conf->async.in_progress &&
(conf->async.aio_type == GPIB_AIO_READ || conf->async.aio_type == GPIB_AIO_WRITE));
set_timeout(board, usec_timeout);
retval = ioctl(board->fileno, IBCMD, &cmd);
if (retval < 0) {
switch(errno) {
case ETIMEDOUT:
conf->timed_out = 1;
setIberr(EBUS);
break;
case ENOTCONN:
setIberr(ENOL);
break;
case EINTR:
setIberr(EABO);
break;
default:
setIberr(EDVR);
setIbcnt(errno);
break;
}
return -1;
}
return cmd.completed_transfer_count;
}
unsigned int create_send_setup(const ibBoard_t *board,
const Addr4882_t addressList[], uint8_t *cmdString)
{
unsigned int i, j;
unsigned int board_pad;
int board_sad;
if (addressList == NULL) {
fprintf(stderr, "libgpib: bug! addressList NULL in create_send_setup()\n");
return 0;
}
if (addressListIsValid(addressList) == 0) {
fprintf(stderr, "libgpib: bug! bad address list\n");
return 0;
}
i = 0;
/* controller's talk address */
if (query_pad(board, &board_pad) < 0)
return 0;
cmdString[i++] = MTA(board_pad);
if (query_sad(board, &board_sad) < 0)
return 0;
if (board_sad >= 0)
cmdString[i++] = MSA(board_sad);
cmdString[ i++ ] = UNL;
for (j = 0; j < numAddresses(addressList); j++) {
unsigned int pad;
int sad;
pad = extractPAD(addressList[ j ]);
sad = extractSAD(addressList[ j ]);
cmdString[ i++ ] = MLA(pad);
if (sad >= 0)
cmdString[ i++ ] = MSA(sad);
}
return i;
}
unsigned int send_setup_string(const ibConf_t *conf,
uint8_t *cmdString)
{
ibBoard_t *board;
Addr4882_t addressList[ 2 ];
board = interfaceBoard(conf);
addressList[ 0 ] = packAddress(conf->settings.pad, conf->settings.sad);
addressList[ 1 ] = NOADDR;
return create_send_setup(board, addressList, cmdString);
}
int send_setup(ibConf_t *conf, unsigned int usec_timeout)
{
uint8_t cmdString[8];
int retval;
retval = send_setup_string(conf, cmdString);
if (my_ibcmd(conf, usec_timeout, cmdString, retval) < 0)
return -1;
return 0;
}
int InternalSendSetup(ibConf_t *conf, const Addr4882_t addressList[])
{
int i;
ibBoard_t *board;
uint8_t *cmd;
int count;
int retval;
if (addressListIsValid(addressList) == 0 ||
numAddresses(addressList) == 0) {
setIberr(EARG);
return -1;
}
if (conf->is_interface == 0) {
setIberr(EDVR);
return -1;
}
board = interfaceBoard(conf);
retval = is_cic(board);
if (retval <= 0) {
if (retval == 0)
setIberr(ECIC);
return -1;
}
cmd = malloc(16 + 2 * numAddresses(addressList));
if (cmd == NULL) {
setIberr(EDVR);
setIbcnt(ENOMEM);
return -1;
}
i = create_send_setup(board, addressList, cmd);
//XXX detect no listeners (EBUS) error
count = my_ibcmd(conf, conf->settings.usec_timeout, cmd, i);
free(cmd);
cmd = NULL;
if (count != i)
return -1;
return 0;
}
void SendSetup(int boardID, const Addr4882_t addressList[])
{
int retval;
ibConf_t *conf;
conf = enter_library(boardID);
if (conf == NULL) {
exit_library(boardID, 1);
return;
}
retval = InternalSendSetup(conf, addressList);
if (retval < 0) {
exit_library(boardID, 1);
return;
}
exit_library(boardID, 0);
}
void SendCmds(int boardID, const void *buffer, long count)
{
ibcmd(boardID, buffer, count);
}
|