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.
20 #include "peripheral_io.h"
21 #include "peripheral_gdbus.h"
22 #include "peripheral_common.h"
23 #include "peripheral_internal.h"
24 #include "peripheral_io_gdbus.h"
26 static PeripheralIoGdbusI2c *i2c_proxy = NULL;
28 void i2c_proxy_init(void)
32 if (i2c_proxy != NULL) {
33 g_object_ref(i2c_proxy);
37 i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync(
39 G_DBUS_PROXY_FLAGS_NONE,
40 PERIPHERAL_GDBUS_NAME,
41 PERIPHERAL_GDBUS_I2C_PATH,
46 void i2c_proxy_deinit()
49 g_object_unref(i2c_proxy);
50 if (!G_IS_OBJECT(i2c_proxy))
55 int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address)
58 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
60 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
62 if (peripheral_io_gdbus_i2c_call_open_sync(
70 _E("Error in %s() : %s", __func__, error->message);
72 return PERIPHERAL_ERROR_UNKNOWN;
78 int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c)
81 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
83 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
85 if (peripheral_io_gdbus_i2c_call_close_sync(
91 _E("Error in %s() : %s", __func__, error->message);
93 return PERIPHERAL_ERROR_UNKNOWN;
99 int peripheral_gdbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length)
101 GError *error = NULL;
102 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
103 GVariant *data_array;
108 if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
110 if (peripheral_io_gdbus_i2c_call_read_sync(
118 _E("Error in %s() : %s", __func__, error->message);
120 return PERIPHERAL_ERROR_UNKNOWN;
123 g_variant_get(data_array, "a(y)", &iter);
124 while (g_variant_iter_loop(iter, "(y)", &str)) {
126 if (i++ == length) break;
128 g_variant_iter_free(iter);
129 g_variant_unref(data_array);
134 int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length)
136 GError *error = NULL;
137 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
138 GVariantBuilder *builder;
142 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
144 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
146 for (i = 0; i < length; i++)
147 g_variant_builder_add(builder, "(y)", data[i]);
148 g_variant_builder_add(builder, "(y)", 0x00);
150 g_data = g_variant_new("a(y)", builder);
151 g_variant_builder_unref(builder);
153 if (peripheral_io_gdbus_i2c_call_write_sync(
161 _E("Error in %s() : %s", __func__, error->message);
163 return PERIPHERAL_ERROR_UNKNOWN;
169 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)
171 GError *error = NULL;
172 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
174 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
176 if (peripheral_io_gdbus_i2c_call_smbus_ioctl_sync(
187 _E("Error in %s() : %s", __func__, error->message);
189 return PERIPHERAL_ERROR_UNKNOWN;