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 I2C_FD_INDEX 0
29 static PeripheralIoGdbusI2c *i2c_proxy = NULL;
31 void i2c_proxy_init(void)
35 if (i2c_proxy != NULL) {
36 g_object_ref(i2c_proxy);
40 i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync(
42 G_DBUS_PROXY_FLAGS_NONE,
43 PERIPHERAL_GDBUS_NAME,
44 PERIPHERAL_GDBUS_I2C_PATH,
49 void i2c_proxy_deinit()
52 g_object_unref(i2c_proxy);
53 if (!G_IS_OBJECT(i2c_proxy))
58 int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address)
61 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
62 GUnixFDList *fd_list = NULL;
64 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
66 if (peripheral_io_gdbus_i2c_call_open_sync(
76 _E("Error in %s() : %s", __func__, error->message);
78 return PERIPHERAL_ERROR_UNKNOWN;
81 i2c->fd = g_unix_fd_list_get(fd_list, I2C_FD_INDEX, &error);
83 _E("Failed to get fd for i2c : %s", error->message);
85 ret = PERIPHERAL_ERROR_UNKNOWN;
88 g_object_unref(fd_list);
93 int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c)
96 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
98 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
100 if (peripheral_io_gdbus_i2c_call_close_sync(
106 _E("Error in %s() : %s", __func__, error->message);
108 return PERIPHERAL_ERROR_UNKNOWN;
114 int peripheral_gdbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length)
116 GError *error = NULL;
117 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
118 GVariant *data_array;
123 if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
125 if (peripheral_io_gdbus_i2c_call_read_sync(
133 _E("Error in %s() : %s", __func__, error->message);
135 return PERIPHERAL_ERROR_UNKNOWN;
138 g_variant_get(data_array, "a(y)", &iter);
139 while (g_variant_iter_loop(iter, "(y)", &str)) {
141 if (i++ == length) break;
143 g_variant_iter_free(iter);
144 g_variant_unref(data_array);
149 int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length)
151 GError *error = NULL;
152 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
153 GVariantBuilder *builder;
157 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
159 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
161 for (i = 0; i < length; i++)
162 g_variant_builder_add(builder, "(y)", data[i]);
163 g_variant_builder_add(builder, "(y)", 0x00);
165 g_data = g_variant_new("a(y)", builder);
166 g_variant_builder_unref(builder);
168 if (peripheral_io_gdbus_i2c_call_write_sync(
176 _E("Error in %s() : %s", __func__, error->message);
178 return PERIPHERAL_ERROR_UNKNOWN;
184 int peripheral_gdbus_i2c_smbus_ioctl(peripheral_i2c_h i2c, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out)
186 GError *error = NULL;
187 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
189 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
191 if (peripheral_io_gdbus_i2c_call_smbus_ioctl_sync(
202 _E("Error in %s() : %s", __func__, error->message);
204 return PERIPHERAL_ERROR_UNKNOWN;