2015-06-28 09:30:25 +00:00
|
|
|
/*
|
|
|
|
***************************************************************************
|
|
|
|
*
|
|
|
|
* Author: Teunis van Beelen
|
|
|
|
*
|
2017-01-02 09:59:35 +00:00
|
|
|
* Copyright (C) 2015, 2016, 2017 Teunis van Beelen
|
2015-06-28 09:30:25 +00:00
|
|
|
*
|
|
|
|
* Email: teuniz@gmail.com
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
|
|
|
|
2015-09-01 17:08:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2016-09-18 08:00:30 +00:00
|
|
|
#include <netinet/tcp.h>
|
2017-09-09 16:40:22 +00:00
|
|
|
#include <netdb.h>
|
2015-09-01 17:08:30 +00:00
|
|
|
|
|
|
|
#include "tmc_dev.h"
|
|
|
|
#include "utils.h"
|
2015-06-28 09:30:25 +00:00
|
|
|
#include "tmc_lan.h"
|
|
|
|
|
|
|
|
|
2015-09-01 17:08:30 +00:00
|
|
|
|
2015-06-28 09:30:25 +00:00
|
|
|
#define TMC_LAN_TIMEOUT 5000
|
|
|
|
|
|
|
|
#define MAX_CMD_LEN (255)
|
|
|
|
#define MAX_RESP_LEN (1024 * 1024 * 2)
|
|
|
|
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
int sockfd;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
struct sockaddr_in inet_address;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
struct timeval timeout;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
fd_set tcp_fds; /* filedescriptor pool */
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int tmclan_send(const char *str)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2015-08-31 18:30:05 +00:00
|
|
|
int len;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
fd_set temp_tcp_fds = tcp_fds; /* because select overwrites the arguments */
|
|
|
|
struct timeval temp_timeout = timeout;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
len = strlen(str);
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
if(select(sockfd + 1, 0, &temp_tcp_fds, 0, &temp_timeout) != -1)
|
2015-08-29 15:13:34 +00:00
|
|
|
{
|
2015-08-31 18:30:05 +00:00
|
|
|
if(FD_ISSET(sockfd, &temp_tcp_fds)) /* check if our file descriptor is set */
|
|
|
|
{
|
|
|
|
return send(sockfd, str, len, MSG_NOSIGNAL);
|
|
|
|
}
|
2015-08-29 15:13:34 +00:00
|
|
|
}
|
2015-08-31 18:30:05 +00:00
|
|
|
|
|
|
|
return -1;
|
2015-08-29 15:13:34 +00:00
|
|
|
}
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
static int tmclan_recv(char *buf, int sz)
|
|
|
|
{
|
2015-08-31 18:30:05 +00:00
|
|
|
fd_set temp_tcp_fds = tcp_fds; /* because select overwrites the arguments */
|
|
|
|
struct timeval temp_timeout = timeout;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
if(select(sockfd + 1, &temp_tcp_fds, 0, 0, &temp_timeout) != -1)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
if(FD_ISSET(sockfd, &temp_tcp_fds)) /* check if our file descriptor is set */
|
|
|
|
{
|
|
|
|
return recv(sockfd, buf, sz, MSG_NOSIGNAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
|
2017-09-09 16:40:22 +00:00
|
|
|
struct tmcdev * tmclan_open(const char *host_or_ip)
|
2015-08-29 15:13:34 +00:00
|
|
|
{
|
2017-09-09 16:40:22 +00:00
|
|
|
char ip_address[256]={""};
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
struct tmcdev *tmc_device;
|
|
|
|
|
2017-09-09 16:40:22 +00:00
|
|
|
struct addrinfo *addr_result, *res;
|
|
|
|
|
|
|
|
struct sockaddr_in *ipv4_addr;
|
|
|
|
|
|
|
|
if((host_or_ip[0] >= 'a') && (host_or_ip[0] <= 'z'))
|
|
|
|
{
|
|
|
|
printf("Resolving hostname: %s...\n", host_or_ip);
|
|
|
|
if(getaddrinfo(host_or_ip, NULL, NULL, &addr_result))
|
|
|
|
{
|
|
|
|
printf("Error: getaddrinfo() file: %s line: %i", __FILE__, __LINE__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(res=addr_result; res!=NULL; res=res->ai_next)
|
|
|
|
{
|
|
|
|
if(res->ai_family == AF_INET)
|
|
|
|
{
|
|
|
|
if(res->ai_socktype == SOCK_STREAM)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res == NULL)
|
|
|
|
{
|
|
|
|
printf("Error: getaddrinfo() file: %s line: %i", __FILE__, __LINE__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv4_addr = (struct sockaddr_in *)res->ai_addr;
|
|
|
|
inet_ntop(AF_INET, &(ipv4_addr->sin_addr), ip_address, INET_ADDRSTRLEN);
|
|
|
|
freeaddrinfo(addr_result);
|
|
|
|
printf("Resolved hostname %s into address: %s\n", host_or_ip, ip_address);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(ip_address, host_or_ip);
|
|
|
|
}
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
sockfd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if(sockfd == -1)
|
|
|
|
{
|
2015-06-28 09:30:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:00:30 +00:00
|
|
|
int tcp_nodelay = 1; /* turn NODELAY on */
|
|
|
|
|
|
|
|
if(setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&tcp_nodelay, sizeof tcp_nodelay) == -1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&tcp_fds); /* clear file descriptor pool */
|
|
|
|
FD_SET(sockfd, &tcp_fds); /* add our filedescriptor to pool */
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = TMC_LAN_TIMEOUT;
|
|
|
|
timeout.tv_usec *= 1000;
|
|
|
|
|
|
|
|
memset(&inet_address, 0, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
inet_address.sin_family = AF_INET;
|
|
|
|
if(inet_aton(ip_address, &inet_address.sin_addr) == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
inet_address.sin_port = htons(5555);
|
|
|
|
|
|
|
|
if(connect(sockfd, (struct sockaddr *) &inet_address, sizeof(struct sockaddr)) < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
tmc_device = (struct tmcdev *)calloc(1, sizeof(struct tmcdev));
|
|
|
|
if(tmc_device == NULL)
|
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
close(sockfd);
|
|
|
|
sockfd = -1;
|
2015-06-28 09:30:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmc_device->hdrbuf = (char *)calloc(1, MAX_RESP_LEN + 1024);
|
|
|
|
if(tmc_device->hdrbuf == NULL)
|
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
close(sockfd);
|
|
|
|
sockfd = -1;
|
2016-10-16 07:38:30 +00:00
|
|
|
free(tmc_device);
|
|
|
|
tmc_device = NULL;
|
2015-06-28 09:30:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmc_device->buf = tmc_device->hdrbuf;
|
|
|
|
|
|
|
|
return tmc_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tmclan_close(struct tmcdev *tmc_device)
|
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
if(sockfd != -1)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
close(sockfd);
|
|
|
|
sockfd = -1;
|
2015-06-28 09:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(tmc_device != NULL)
|
|
|
|
{
|
|
|
|
free(tmc_device->hdrbuf);
|
|
|
|
|
|
|
|
free(tmc_device);
|
|
|
|
|
|
|
|
tmc_device = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tmclan_write(struct tmcdev *tmc_device __attribute__ ((unused)), const char *cmd)
|
|
|
|
{
|
2015-09-01 17:08:30 +00:00
|
|
|
int i, n, len, qry=0;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
char buf[MAX_CMD_LEN + 16],
|
|
|
|
str[256];
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
if(sockfd == -1)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
len = strlen(cmd);
|
|
|
|
|
|
|
|
if(len > MAX_CMD_LEN)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
printf("tmc_lan error: command too long\n");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
if(len < 2)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
printf("tmc_lan error: command too short\n");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
if(cmd[len - 1] == '?')
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
qry = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(buf, cmd, MAX_CMD_LEN);
|
|
|
|
|
|
|
|
buf[MAX_CMD_LEN] = 0;
|
|
|
|
|
|
|
|
strcat(buf, "\n");
|
|
|
|
|
2016-09-18 08:00:30 +00:00
|
|
|
if(!(!strncmp(buf, ":TRIG:STAT?", 11) || /* don't print these commands to the console */
|
|
|
|
!strncmp(buf, ":TRIG:SWE?", 10) || /* because they are used repeatedly */
|
2015-06-28 09:30:25 +00:00
|
|
|
!strncmp(buf, ":WAV:DATA?", 10) ||
|
|
|
|
!strncmp(buf, ":WAV:MODE NORM", 14) ||
|
|
|
|
!strncmp(buf, ":WAV:FORM BYTE", 14) ||
|
|
|
|
!strncmp(buf, ":WAV:SOUR CHAN", 14) ||
|
|
|
|
!strncmp(buf, ":TRIG:SWE?", 10) ||
|
|
|
|
!strncmp(buf, ":ACQ:SRAT?", 10) ||
|
|
|
|
!strncmp(buf, ":ACQ:MDEP?", 10) ||
|
2017-05-14 16:16:49 +00:00
|
|
|
!strncmp(buf, ":MEAS:COUN:VAL?", 15) ||
|
|
|
|
!strncmp(buf, ":FUNC:WREC:OPER?", 16) ||
|
|
|
|
!strncmp(buf, ":FUNC:WREP:OPER?", 16) ||
|
|
|
|
!strncmp(buf, ":FUNC:WREP:FMAX?", 16) ||
|
|
|
|
!strncmp(buf, ":FUNC:WREC:FMAX?", 16) ||
|
|
|
|
!strncmp(buf, ":FUNC:WREP:FCUR?", 16)))
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
printf("tmc_lan write: %s", buf);
|
|
|
|
}
|
|
|
|
|
2016-10-11 16:42:59 +00:00
|
|
|
if(!strncmp(buf, "*RST", 4)) qry = 1;
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
n = tmclan_send(buf);
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
if(n != (len + 1))
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2016-10-11 16:50:01 +00:00
|
|
|
printf("tmclan error: device write error");
|
|
|
|
|
2015-06-28 09:30:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!qry)
|
|
|
|
{
|
2015-09-01 17:08:30 +00:00
|
|
|
for(i=0; i<20; i++)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2015-08-31 18:30:05 +00:00
|
|
|
usleep(25000);
|
2015-06-28 09:30:25 +00:00
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
if(tmclan_send("*OPC?\n") != 6)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2016-10-11 16:50:01 +00:00
|
|
|
printf("tmclan error: device write error");
|
2015-08-31 18:30:05 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = tmclan_recv(str, 128);
|
|
|
|
|
|
|
|
if(n < 0)
|
|
|
|
{
|
2016-10-11 16:50:01 +00:00
|
|
|
printf("tmclan error: device read error");
|
2015-08-31 18:30:05 +00:00
|
|
|
|
2015-06-28 09:30:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
if(n == 2)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
if(str[0] == '1')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
return len;
|
2015-06-28 09:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TMC Blockheader ::= #NXXXXXX: is used to describe
|
|
|
|
* the length of the data stream, wherein, # is the start denoter of
|
|
|
|
* the data stream; N is less than or equal to 9; the N figures
|
|
|
|
* followed N represents the length of the data stream in bytes.
|
|
|
|
* For example, #9001152054. Wherein, N is 9 and 001152054
|
|
|
|
* represents that the data stream contains 1152054 bytes
|
|
|
|
* effective data.
|
|
|
|
*/
|
|
|
|
int tmclan_read(struct tmcdev *tmc_device)
|
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
int n, size, size2, len;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
char blockhdr[32];
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
if(sockfd == -1)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmc_device->hdrbuf[0] = 0;
|
|
|
|
|
|
|
|
tmc_device->sz = 0;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
n = tmclan_recv(tmc_device->hdrbuf + size, MAX_RESP_LEN - size);
|
|
|
|
|
|
|
|
if(n < 1)
|
2015-06-28 09:30:25 +00:00
|
|
|
{
|
2015-08-29 15:13:34 +00:00
|
|
|
return -2;
|
2015-06-28 09:30:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
size += n;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
if(tmc_device->hdrbuf[size - 1] == '\n')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((size < 2) || (size > MAX_RESP_LEN))
|
|
|
|
{
|
|
|
|
tmc_device->hdrbuf[0] = 0;
|
|
|
|
|
|
|
|
tmc_device->buf[0] = 0;
|
|
|
|
|
2015-08-29 15:13:34 +00:00
|
|
|
return -3;
|
2015-06-28 09:30:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:30:05 +00:00
|
|
|
tmc_device->hdrbuf[size] = 0;
|
2015-06-28 09:30:25 +00:00
|
|
|
|
|
|
|
if(tmc_device->hdrbuf[0] != '#')
|
|
|
|
{
|
|
|
|
if(tmc_device->hdrbuf[size - 1] == '\n')
|
|
|
|
{
|
|
|
|
tmc_device->hdrbuf[--size] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmc_device->buf = tmc_device->hdrbuf;
|
|
|
|
|
|
|
|
tmc_device->sz = size;
|
|
|
|
|
|
|
|
return tmc_device->sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(blockhdr, tmc_device->hdrbuf, 16);
|
|
|
|
|
|
|
|
len = blockhdr[1] - '0';
|
|
|
|
|
|
|
|
if((len < 1) || (len > 9))
|
|
|
|
{
|
|
|
|
blockhdr[31] = 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockhdr[len + 2] = 0;
|
|
|
|
|
|
|
|
size2 = atoi(blockhdr + 2);
|
|
|
|
|
|
|
|
size--; // remove the last character
|
|
|
|
|
|
|
|
if(size < size2)
|
|
|
|
{
|
|
|
|
blockhdr[31] = 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmc_device->buf = tmc_device->hdrbuf + len + 2;
|
|
|
|
|
|
|
|
tmc_device->sz = size2;
|
|
|
|
|
|
|
|
return tmc_device->sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|