865cf92792ea9b788b62cb4386d789ebb8fadbf9
[platform/core/api/peripheral-io.git] / src / peripheral_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 #include <unistd.h>
20 #include <assert.h>
21
22 #include "peripheral_io.h"
23 #include "peripheral_dbus.h"
24 #include "peripheral_common.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define I2C_NAME        "i2c"
31 int I2C_Addr = 0;
32
33 peripheral_i2c_context_h peripheral_i2c_init(int bus)
34 {
35         peripheral_i2c_context_h dev;
36         int ret = PERIPHERAL_ERROR_NONE;
37
38         assert(bus >= 0);
39
40         /* Initialize peripheral_i2c_context_h */
41         dev = (peripheral_i2c_context_h)malloc(sizeof(struct _peripheral_i2c_s));
42
43         if (dev == NULL) {
44                 _E("Failed to allocate peripheral_i2c_context_h");
45                 return NULL;
46         }
47
48         if (!get_dbus_connection())
49                 set_dbus_connection();
50
51         ret = peripheral_dbus_i2c(dev, I2C_NAME, "INIT", bus, 0, I2C_Addr);
52
53         if (ret != PERIPHERAL_ERROR_NONE) {
54                 free(dev);
55                 _E("[PERIPHERAL] I2C init error\n");
56                 dev = NULL;
57         }
58
59         return dev;
60 }
61
62 int peripheral_i2c_stop(peripheral_i2c_context_h dev)
63 {
64         int ret = PERIPHERAL_ERROR_NONE;
65         /* Free peripheral_i2c_context_h */
66
67         if (dev != NULL) {
68                 ret = peripheral_dbus_i2c(dev, I2C_NAME, "STOP", 0, 0, I2C_Addr);
69
70                 free(dev);
71                 dev = NULL;
72         }
73
74         return ret;
75 }
76
77 int peripheral_i2c_set_frequency(peripheral_i2c_context_h dev, peripheral_i2c_mode_e mode)
78 {
79         /* Set the clocking for the selected frequency */
80         return peripheral_dbus_i2c(dev, I2C_NAME, "SET_FREQ", mode, 0, I2C_Addr);
81 }
82
83 int peripheral_i2c_set_address(peripheral_i2c_context_h dev, int address)
84 {
85         /* Set the i2c slave address */
86
87         //I2C_Addr = address;
88         return peripheral_dbus_i2c(dev, I2C_NAME, "SET_ADDR", address, 0, I2C_Addr);
89 }
90
91 int peripheral_i2c_read(peripheral_i2c_context_h dev, uint8_t *data, int length)
92 {
93         /* Read i2c data */
94         return peripheral_dbus_i2c(dev, I2C_NAME, "READ", length, data, I2C_Addr);
95 }
96
97 int peripheral_i2c_write(peripheral_i2c_context_h dev, uint8_t *data, int length)
98 {
99         /* Write i2c data */
100         return peripheral_dbus_i2c(dev, I2C_NAME, "WRITE", length, data, I2C_Addr);
101 }
102
103 #ifdef __cplusplus
104 }
105 #endif