]> sigrok.org Git - libsigrok.git/blob - hardware/common/scpi_serial.c
remove unused static functions
[libsigrok.git] / hardware / common / scpi_serial.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5  * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23
24 #include <glib.h>
25 #include <string.h>
26
27 #define LOG_PREFIX "scpi_serial"
28
29 #define SCPI_READ_RETRIES 100
30 #define SCPI_READ_RETRY_TIMEOUT 10000
31
32 struct scpi_serial {
33         struct sr_serial_dev_inst *serial;
34         char last_character;
35 };
36
37 static int scpi_serial_dev_inst_new(void *priv, const char *resource,
38                 char **params, const char *serialcomm)
39 {
40         struct scpi_serial *sscpi = priv;
41
42         (void)params;
43
44         if (!(sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm)))
45                 return SR_ERR;
46
47         return SR_OK;
48 }
49
50 static int scpi_serial_open(void *priv)
51 {
52         struct scpi_serial *sscpi = priv;
53         struct sr_serial_dev_inst *serial = sscpi->serial;
54
55         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
56                 return SR_ERR;
57
58         if (serial_flush(serial) != SR_OK)
59                 return SR_ERR;
60
61         return SR_OK;
62 }
63
64 static int scpi_serial_source_add(void *priv, int events, int timeout,
65                         sr_receive_data_callback_t cb, void *cb_data)
66 {
67         struct scpi_serial *sscpi = priv;
68         struct sr_serial_dev_inst *serial = sscpi->serial;
69
70         return serial_source_add(serial, events, timeout, cb, cb_data);
71 }
72
73 static int scpi_serial_source_remove(void *priv)
74 {
75         struct scpi_serial *sscpi = priv;
76         struct sr_serial_dev_inst *serial = sscpi->serial;
77
78         return serial_source_remove(serial);
79 }
80
81 static int scpi_serial_send(void *priv, const char *command)
82 {
83         int len, result, written;
84         gchar *terminated_command;
85         struct scpi_serial *sscpi = priv;
86         struct sr_serial_dev_inst *serial = sscpi->serial;
87
88         terminated_command = g_strconcat(command, "\n", NULL);
89         len = strlen(terminated_command);
90         written = 0;
91         while (written < len) {
92                 result = serial_write(serial, terminated_command + written, len - written);
93                 if (result < 0) {
94                         sr_err("Error while sending SCPI command: '%s'.", command);
95                         g_free(terminated_command);
96                         return SR_ERR;
97                 }
98                 written += result;
99         }
100
101         g_free(terminated_command);
102
103         sr_spew("Successfully sent SCPI command: '%s'.", command);
104
105         return SR_OK;
106 }
107
108 static int scpi_serial_read_begin(void *priv)
109 {
110         struct scpi_serial *sscpi = priv;
111
112         sscpi->last_character = '\0';
113
114         return SR_OK;
115 }
116
117 static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
118 {
119         struct scpi_serial *sscpi = priv;
120         int ret;
121
122         ret = serial_read(sscpi->serial, buf, maxlen);
123
124         if (ret < 0)
125                 return ret;
126
127         if (ret > 0) {
128                 sscpi->last_character = buf[ret - 1];
129                 if (sscpi->last_character == '\n')
130                         ret--;
131         }
132
133         return ret;
134 }
135
136 static int scpi_serial_read_complete(void *priv)
137 {
138         struct scpi_serial *sscpi = priv;
139
140         return (sscpi->last_character == '\n');
141 }
142
143 static int scpi_serial_close(void *priv)
144 {
145         struct scpi_serial *sscpi = priv;
146
147         return serial_close(sscpi->serial);
148 }
149
150 static void scpi_serial_free(void *priv)
151 {
152         struct scpi_serial *sscpi = priv;
153
154         sr_serial_dev_inst_free(sscpi->serial);
155 }
156
157 SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
158         .name          = "serial",
159         .prefix        = "",
160         .priv_size     = sizeof(struct scpi_serial),
161         .dev_inst_new  = scpi_serial_dev_inst_new,
162         .open          = scpi_serial_open,
163         .source_add    = scpi_serial_source_add,
164         .source_remove = scpi_serial_source_remove,
165         .send          = scpi_serial_send,
166         .read_begin    = scpi_serial_read_begin,
167         .read_data     = scpi_serial_read_data,
168         .read_complete = scpi_serial_read_complete,
169         .close         = scpi_serial_close,
170         .free          = scpi_serial_free,
171 };