2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <gio/gunixfdlist.h>
21 #include "peripheral_io.h"
22 #include "peripheral_gdbus.h"
23 #include "peripheral_common.h"
24 #include "peripheral_internal.h"
25 #include "peripheral_io_gdbus.h"
27 #define SPI_FD_INDEX 0
29 static PeripheralIoGdbusSpi *spi_proxy = NULL;
31 void spi_proxy_init(void)
35 if (spi_proxy != NULL) {
36 g_object_ref(spi_proxy);
40 spi_proxy = peripheral_io_gdbus_spi_proxy_new_for_bus_sync(
42 G_DBUS_PROXY_FLAGS_NONE,
43 PERIPHERAL_GDBUS_NAME,
44 PERIPHERAL_GDBUS_SPI_PATH,
49 void spi_proxy_deinit()
52 g_object_unref(spi_proxy);
53 if (!G_IS_OBJECT(spi_proxy))
58 int peripheral_gdbus_spi_open(peripheral_spi_h spi, int bus, int cs)
61 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
62 GUnixFDList *fd_list = NULL;
64 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
66 if (peripheral_io_gdbus_spi_call_open_sync(
76 _E("%s", error->message);
78 return PERIPHERAL_ERROR_UNKNOWN;
81 spi->fd = g_unix_fd_list_get(fd_list, SPI_FD_INDEX, &error);
83 _E("Failed to get fd for spi : %s", error->message);
85 ret = PERIPHERAL_ERROR_UNKNOWN;
88 g_object_unref(fd_list);
93 int peripheral_gdbus_spi_close(peripheral_spi_h spi)
96 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
98 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
100 if (peripheral_io_gdbus_spi_call_close_sync(
106 _E("%s", error->message);
108 return PERIPHERAL_ERROR_UNKNOWN;
114 int peripheral_gdbus_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)
116 GError *error = NULL;
117 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
119 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
121 if (peripheral_io_gdbus_spi_call_set_mode_sync(
128 _E("%s", error->message);
130 return PERIPHERAL_ERROR_UNKNOWN;
136 int peripheral_gdbus_spi_set_bit_order(peripheral_spi_h spi, bool lsb)
138 GError *error = NULL;
139 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
141 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
143 if (peripheral_io_gdbus_spi_call_set_bit_order_sync(
150 _E("%s", error->message);
152 return PERIPHERAL_ERROR_UNKNOWN;
158 int peripheral_gdbus_spi_set_bits_per_word(peripheral_spi_h spi, unsigned char bits)
160 GError *error = NULL;
161 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
163 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
165 if (peripheral_io_gdbus_spi_call_set_bits_per_word_sync(
172 _E("%s", error->message);
174 return PERIPHERAL_ERROR_UNKNOWN;
180 int peripheral_gdbus_spi_set_frequency(peripheral_spi_h spi, unsigned int freq_hz)
182 GError *error = NULL;
183 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
185 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
187 if (peripheral_io_gdbus_spi_call_set_frequency_sync(
194 _E("%s", error->message);
196 return PERIPHERAL_ERROR_UNKNOWN;
202 int peripheral_gdbus_spi_read(peripheral_spi_h spi, unsigned char *data, int length)
204 GError *error = NULL;
205 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
206 GVariant *data_array;
211 if (spi_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
213 if (peripheral_io_gdbus_spi_call_read_sync(
221 _E("%s", error->message);
223 return PERIPHERAL_ERROR_UNKNOWN;
226 g_variant_get(data_array, "a(y)", &iter);
227 while (g_variant_iter_loop(iter, "(y)", &str)) {
229 if (i++ == length) break;
231 g_variant_iter_free(iter);
232 g_variant_unref(data_array);
237 int peripheral_gdbus_spi_write(peripheral_spi_h spi, unsigned char *data, int length)
239 GError *error = NULL;
240 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
241 GVariantBuilder *builder;
242 GVariant *data_array;
245 if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
247 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
249 for (i = 0; i < length; i++)
250 g_variant_builder_add(builder, "(y)", data[i]);
251 g_variant_builder_add(builder, "(y)", 0x00);
253 data_array = g_variant_new("a(y)", builder);
254 g_variant_builder_unref(builder);
256 if (peripheral_io_gdbus_spi_call_write_sync(
264 _E("%s", error->message);
266 return PERIPHERAL_ERROR_UNKNOWN;
272 int peripheral_gdbus_spi_transfer(peripheral_spi_h spi, unsigned char *tx_data, unsigned char *rx_data, int length)
274 GError *error = NULL;
275 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
276 GVariantBuilder *builder;
277 GVariant *rx_data_array;
278 GVariant *tx_data_array;
283 if (spi_proxy == NULL || !rx_data || !tx_data) return PERIPHERAL_ERROR_UNKNOWN;
285 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
287 for (i = 0; i < length; i++)
288 g_variant_builder_add(builder, "(y)", tx_data[i]);
289 g_variant_builder_add(builder, "(y)", 0x00);
291 tx_data_array = g_variant_new("a(y)", builder);
292 g_variant_builder_unref(builder);
294 if (peripheral_io_gdbus_spi_call_transfer_sync(
303 _E("%s", error->message);
305 return PERIPHERAL_ERROR_UNKNOWN;
309 g_variant_get(rx_data_array, "a(y)", &iter);
310 while (g_variant_iter_loop(iter, "(y)", &str)) {
312 if (i++ == length) break;
314 g_variant_iter_free(iter);
315 g_variant_unref(rx_data_array);