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
|
/***************************************************************************
lib/ibFindLstn.c
-------------------
copyright : (C) 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 <unistd.h>
#include <stdlib.h>
/* Strictly speaking, addressing the controller as talker is
* not part of the FindListener 488.2 protocol. However, doing
* so helps with boards that cannot reliably read the state of
* the NDAC line due to their transceivers. Such boards can
* only read the state of NDAC (and NRFD and SRQ) when the
* transceiver has the line configured as an input. Addressing
* the board as talker insures NDAC (and NRFD) will be inputs
* while the FindListener protocol is running.
*/
int address_board_as_talker(ibConf_t *conf)
{
uint8_t cmd[2];
int j;
unsigned int board_pad;
int board_sad;
ibBoard_t *board;
board = interfaceBoard(conf);
j = 0;
/* controller's talk address */
if (query_pad(board, &board_pad) < 0)
return 0;
cmd[j++] = MTA(board_pad);
if (query_sad(board, &board_sad) < 0)
return 0;
if (board_sad >= 0)
cmd[j++] = MSA(board_sad);
return my_ibcmd(conf, conf->settings.usec_timeout, cmd, j);
}
int unlisten_untalk(ibConf_t *conf)
{
uint8_t cmd[2];
int j;
j = 0;
cmd[j++] = UNL;
cmd[j++] = UNT;
return my_ibcmd(conf, conf->settings.usec_timeout, cmd, j);
}
int listenerFound(ibConf_t *conf, const Addr4882_t addressList[])
{
uint8_t *cmd;
int i, j;
short line_status;
int retval;
if (addressList == NULL)
return 0;
if (addressListIsValid(addressList) == 0)
return -1;
cmd = malloc(16 + 2 * numAddresses(addressList));
if (cmd == NULL) {
setIberr(EDVR);
setIbcnt(ENOMEM);
return -1;
}
j = 0;
cmd[j++] = UNL;
for (i = 0; i < numAddresses(addressList); i++) {
int pad, sad;
pad = extractPAD(addressList[i]);
sad = extractSAD(addressList[i]);
cmd[j++] = MLA(pad);
if (sad >= 0)
cmd[j++] = MSA(sad);
}
retval = my_ibcmd(conf, conf->settings.usec_timeout, cmd, j);
free(cmd);
cmd = NULL;
if (retval < 0)
return retval;
retval = internal_ibgts(conf, 0);
if (retval < 0)
return -1;
usleep(1500);
retval = internal_iblines(conf, &line_status);
if (retval < 0)
return retval;
if (((line_status & ValidNRFD) && (line_status & BusNRFD)) ||
((line_status & ValidNDAC) && (line_status & BusNDAC)))
return 1;
return 0;
}
int secondaryListenerFound(ibConf_t *conf, unsigned int pad)
{
Addr4882_t testAddress[32];
int j;
for (j = 0; j <= gpib_addr_max; j++)
testAddress[j] = packAddress(pad, j);
testAddress[j] = NOADDR;
return listenerFound(conf, testAddress);
}
void FindLstn(int boardID, const Addr4882_t padList[],
Addr4882_t resultList[], int maxNumResults)
{
int i;
ibConf_t *conf;
int retval;
int resultIndex;
short line_status;
conf = enter_library(boardID);
if (conf == NULL) {
exit_library(boardID, 1);
return;
}
if (conf->is_interface == 0) {
setIberr(EDVR);
exit_library(boardID, 1);
return;
}
retval = internal_iblines(conf, &line_status);
if (retval < 0) {
exit_library(boardID, 1);
return;
}
if ((line_status & ValidNDAC) == 0) {
setIberr(ECAP);
exit_library(boardID, 1);
return;
}
retval = address_board_as_talker(conf);
if (retval < 0) {
exit_library(boardID, 1);
return;
}
resultIndex = 0;
for (i = 0; i < numAddresses(padList); i++) {
Addr4882_t pad;
Addr4882_t testAddress[2];
pad = GetPAD(padList[i]);
testAddress[0] = pad;
testAddress[1] = NOADDR;
retval = listenerFound(conf, testAddress);
if (retval < 0) {
// XXX status/error settings
exit_library(boardID, 1);
return;
}
if (retval > 0) {
if (resultIndex >= maxNumResults) {
setIberr(ETAB);
exit_library(boardID, 1);
return;
}
resultList[resultIndex++] = testAddress[0];
setIbcnt(resultIndex);
} else {
retval = secondaryListenerFound(conf, pad);
if (retval < 0) {
exit_library(boardID, 1);
return;
}
if (retval > 0) {
int j;
for (j = 0; j <= gpib_addr_max; j++) {
testAddress[0] = packAddress(pad, j);
testAddress[1] = NOADDR;
retval = listenerFound(conf, testAddress);
if (retval < 0) {
exit_library(boardID, 1);
return;
}
if (retval > 1) {
if (resultIndex >= maxNumResults) {
setIberr(ETAB);
exit_library(boardID, 1);
return;
}
resultList[resultIndex++] = testAddress[0];
setIbcnt(resultIndex);
}
}
}
}
}
retval = unlisten_untalk(conf);
if (retval < 0) {
exit_library(boardID, 1);
return;
}
exit_library(boardID, 0);
} /* FindLstn */
int ibln(int ud, int pad, int sad, short *found_listener)
{
ibConf_t *conf;
Addr4882_t addressList[2];
int retval;
conf = enter_library(ud);
if (conf == NULL)
return exit_library(ud, 1);
retval = address_board_as_talker(conf);
if (retval < 0) {
return exit_library(ud, 1);
}
switch(sad) {
case ALL_SAD:
retval = secondaryListenerFound(conf, pad);
break;
case NO_SAD:
default:
addressList[0] = MakeAddr(pad, sad);
addressList[1] = NOADDR;
retval = listenerFound(conf, addressList);
break;
}
if (retval < 0)
return exit_library(ud, 1);
*found_listener = retval;
retval = unlisten_untalk(conf);
if (retval < 0)
return exit_library(ud, 1);
return exit_library(ud, 0);
}
|