Upload packaging folder
[platform/upstream/iotjs.git] / tools / src / module / iotjs_module_i2c.h
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 #ifndef IOTJS_MODULE_I2C_H
18 #define IOTJS_MODULE_I2C_H
19
20 #include "iotjs_def.h"
21 #include "iotjs_objectwrap.h"
22 #include "iotjs_reqwrap.h"
23
24 #if defined(__NUTTX__)
25 #include <nuttx/i2c/i2c_master.h>
26 #endif
27
28 typedef enum {
29   kI2cOpSetAddress,
30   kI2cOpOpen,
31   kI2cOpClose,
32   kI2cOpWrite,
33   kI2cOpWriteByte,
34   kI2cOpWriteBlock,
35   kI2cOpRead,
36   kI2cOpReadByte,
37   kI2cOpReadBlock,
38 } I2cOp;
39
40 typedef enum {
41   kI2cErrOk = 0,
42   kI2cErrOpen = -1,
43   kI2cErrRead = -2,
44   kI2cErrReadBlock = -3,
45   kI2cErrWrite = -4,
46 } I2cError;
47
48
49 typedef struct {
50 #if defined(__linux__) || defined(__APPLE__)
51   iotjs_string_t device;
52 #elif defined(__NUTTX__)
53   uint32_t device;
54 #endif
55   char* buf_data;
56   uint8_t buf_len;
57   uint8_t byte;
58   uint8_t cmd;
59   int32_t delay;
60
61   I2cOp op;
62   I2cError error;
63 } iotjs_i2c_reqdata_t;
64
65
66 // This I2c class provides interfaces for I2C operation.
67 typedef struct {
68 #if defined(__NUTTX__)
69   struct i2c_master_s* i2c_master;
70   struct i2c_config_s config;
71 #endif
72   iotjs_jobjectwrap_t jobjectwrap;
73 } IOTJS_VALIDATED_STRUCT(iotjs_i2c_t);
74
75
76 typedef struct {
77   iotjs_reqwrap_t reqwrap;
78   uv_work_t req;
79   iotjs_i2c_reqdata_t req_data;
80   iotjs_i2c_t* i2c_data;
81 } IOTJS_VALIDATED_STRUCT(iotjs_i2c_reqwrap_t);
82
83
84 iotjs_i2c_t* iotjs_i2c_create(const iotjs_jval_t* ji2c);
85 iotjs_i2c_t* iotjs_i2c_instance_from_jval(const iotjs_jval_t* ji2c);
86
87 #define THIS iotjs_i2c_reqwrap_t* i2c_reqwrap
88 iotjs_i2c_reqwrap_t* iotjs_i2c_reqwrap_create(const iotjs_jval_t* jcallback,
89                                               const iotjs_jval_t* ji2c,
90                                               I2cOp op);
91 void iotjs_i2c_reqwrap_dispatched(THIS);
92 uv_work_t* iotjs_i2c_reqwrap_req(THIS);
93 const iotjs_jval_t* iotjs_i2c_reqwrap_jcallback(THIS);
94 iotjs_i2c_reqwrap_t* iotjs_i2c_reqwrap_from_request(uv_work_t* req);
95 iotjs_i2c_reqdata_t* iotjs_i2c_reqwrap_data(THIS);
96 iotjs_i2c_t* iotjs_i2c_instance_from_reqwrap(THIS);
97 #undef THIS
98
99
100 void I2cSetAddress(iotjs_i2c_t* i2c, uint8_t address);
101 void OpenWorker(uv_work_t* work_req);
102 void I2cClose(iotjs_i2c_t* i2c);
103 void WriteWorker(uv_work_t* work_req);
104 void WriteByteWorker(uv_work_t* work_req);
105 void WriteBlockWorker(uv_work_t* work_req);
106 void ReadWorker(uv_work_t* work_req);
107 void ReadByteWorker(uv_work_t* work_req);
108 void ReadBlockWorker(uv_work_t* work_req);
109
110
111 #endif /* IOTJS_MODULE_I2C_H */