Update Iot.js
[platform/upstream/iotjs.git] / tools / src / module / iotjs_module_gpio.h
1 /* Copyright 2015-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_GPIO_H
18 #define IOTJS_MODULE_GPIO_H
19
20
21 #include "iotjs_def.h"
22 #include "iotjs_objectwrap.h"
23 #include "iotjs_reqwrap.h"
24
25
26 typedef enum {
27   kGpioDirectionIn = 0,
28   kGpioDirectionOut,
29 } GpioDirection;
30
31
32 typedef enum {
33   kGpioModeNone = 0,
34   kGpioModePullup,
35   kGpioModePulldown,
36   kGpioModeFloat,
37   kGpioModePushpull,
38   kGpioModeOpendrain,
39 } GpioMode;
40
41
42 typedef enum {
43   kGpioOpOpen,
44   kGpioOpWrite,
45   kGpioOpRead,
46   kGpioOpClose,
47 } GpioOp;
48
49
50 typedef struct {
51   bool value;
52   ssize_t result;
53   GpioOp op;
54 } iotjs_gpio_reqdata_t;
55
56
57 // This Gpio class provides interfaces for GPIO operation.
58 typedef struct {
59   iotjs_jobjectwrap_t jobjectwrap;
60   uint32_t pin;
61   GpioDirection direction;
62   GpioMode mode;
63 } IOTJS_VALIDATED_STRUCT(iotjs_gpio_t);
64
65
66 typedef struct {
67   iotjs_reqwrap_t reqwrap;
68   uv_work_t req;
69   iotjs_gpio_reqdata_t req_data;
70   iotjs_gpio_t* gpio_instance;
71 } IOTJS_VALIDATED_STRUCT(iotjs_gpio_reqwrap_t);
72
73
74 #define THIS iotjs_gpio_reqwrap_t* gpio_reqwrap
75
76 iotjs_gpio_reqwrap_t* iotjs_gpio_reqwrap_from_request(uv_work_t* req);
77 iotjs_gpio_reqdata_t* iotjs_gpio_reqwrap_data(THIS);
78
79 iotjs_gpio_t* iotjs_gpio_instance_from_reqwrap(THIS);
80
81 #undef THIS
82
83
84 #define GPIO_WORKER_INIT()                                                    \
85   iotjs_gpio_reqwrap_t* req_wrap = iotjs_gpio_reqwrap_from_request(work_req); \
86   iotjs_gpio_reqdata_t* req_data = iotjs_gpio_reqwrap_data(req_wrap);         \
87   iotjs_gpio_t* gpio = iotjs_gpio_instance_from_reqwrap(req_wrap);            \
88   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_gpio_t, gpio);
89
90
91 void iotjs_gpio_open_worker(uv_work_t* work_req);
92 void iotjs_gpio_write_worker(uv_work_t* work_req);
93 void iotjs_gpio_read_worker(uv_work_t* work_req);
94 void iotjs_gpio_close_worker(uv_work_t* work_req);
95 bool iotjs_gpio_write(int32_t pin, bool value);
96 int iotjs_gpio_read(int32_t pin);
97 bool iotjs_gpio_close(int32_t pin);
98
99 #endif /* IOTJS_MODULE_GPIO_H */