e0a39ac45c3fb768b719014e8a9b87026c1d88d6
[platform/core/api/peripheral-io.git] / src / peripheral_gdbus_i2c.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
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"
25
26 static PeripheralIoGdbusI2c *i2c_proxy = NULL;
27
28 void i2c_proxy_init(void)
29 {
30         GError *error = NULL;
31
32         if (i2c_proxy != NULL) {
33                 g_object_ref(i2c_proxy);
34                 return;
35         }
36
37         i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync(
38                 G_BUS_TYPE_SYSTEM,
39                 G_DBUS_PROXY_FLAGS_NONE,
40                 PERIPHERAL_GDBUS_NAME,
41                 PERIPHERAL_GDBUS_I2C_PATH,
42                 NULL,
43                 &error);
44 }
45
46 void i2c_proxy_deinit()
47 {
48         if (i2c_proxy) {
49                 g_object_unref(i2c_proxy);
50                 if (!G_IS_OBJECT(i2c_proxy))
51                         i2c_proxy = NULL;
52         }
53 }
54
55 int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address)
56 {
57         GError *error = NULL;
58         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
59
60         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
61
62         if (peripheral_io_gdbus_i2c_call_open_sync(
63                         i2c_proxy,
64                         bus,
65                         address,
66                         &i2c->handle,
67                         &ret,
68                         NULL,
69                         &error) == FALSE) {
70                 _E("Error in %s() : %s", __func__, error->message);
71                 g_error_free(error);
72                 return PERIPHERAL_ERROR_UNKNOWN;
73         }
74
75         return ret;
76 }
77
78 int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c)
79 {
80         GError *error = NULL;
81         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
82
83         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
84
85         if (peripheral_io_gdbus_i2c_call_close_sync(
86                         i2c_proxy,
87                         i2c->handle,
88                         &ret,
89                         NULL,
90                         &error) == FALSE) {
91                 _E("Error in %s() : %s", __func__, error->message);
92                 g_error_free(error);
93                 return PERIPHERAL_ERROR_UNKNOWN;
94         }
95
96         return ret;
97 }
98
99 int peripheral_gdbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length)
100 {
101         GError *error = NULL;
102         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
103         GVariant *data_array;
104         GVariantIter *iter;
105         guint8 str;
106         int i = 0;
107
108         if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
109
110         if (peripheral_io_gdbus_i2c_call_read_sync(
111                         i2c_proxy,
112                         i2c->handle,
113                         length,
114                         &data_array,
115                         &ret,
116                         NULL,
117                         &error) == FALSE) {
118                 _E("Error in %s() : %s", __func__, error->message);
119                 g_error_free(error);
120                 return PERIPHERAL_ERROR_UNKNOWN;
121         }
122
123         g_variant_get(data_array, "a(y)", &iter);
124         while (g_variant_iter_loop(iter, "(y)", &str)) {
125                 data[i] = str;
126                 if (i++ == length) break;
127         }
128         g_variant_iter_free(iter);
129         g_variant_unref(data_array);
130
131         return ret;
132 }
133
134 int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length)
135 {
136         GError *error = NULL;
137         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
138         GVariantBuilder *builder;
139         GVariant *g_data;
140         int i = 0;
141
142         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
143
144         builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
145
146         for (i = 0; i < length; i++)
147                 g_variant_builder_add(builder, "(y)", data[i]);
148         g_variant_builder_add(builder, "(y)", 0x00);
149
150         g_data = g_variant_new("a(y)", builder);
151         g_variant_builder_unref(builder);
152
153         if (peripheral_io_gdbus_i2c_call_write_sync(
154                         i2c_proxy,
155                         i2c->handle,
156                         length,
157                         g_data,
158                         &ret,
159                         NULL,
160                         &error) == FALSE) {
161                 _E("Error in %s() : %s", __func__, error->message);
162                 g_error_free(error);
163                 return PERIPHERAL_ERROR_UNKNOWN;
164         }
165
166         return ret;
167 }
168
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)
170 {
171         GError *error = NULL;
172         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
173
174         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
175
176         if (peripheral_io_gdbus_i2c_call_smbus_ioctl_sync(
177                         i2c_proxy,
178                         i2c->handle,
179                         read_write,
180                         command,
181                         size,
182                         data_in,
183                         data_out,
184                         &ret,
185                         NULL,
186                         &error) == FALSE) {
187                 _E("Error in %s() : %s", __func__, error->message);
188                 g_error_free(error);
189                 return PERIPHERAL_ERROR_UNKNOWN;
190         }
191
192         return ret;
193 }