Skip to content

Commit 2674f59

Browse files
committed
Start of GPIO implementation based on GPIO API
* applied comments from #129 * setPin() API ready, others are blank * x86 native dummy code with setPin() for testingy, others are blank IoT.js-DCO-1.0-Signed-off-by: SaeHie Park [email protected]
1 parent b527602 commit 2674f59

File tree

9 files changed

+535
-197
lines changed

9 files changed

+535
-197
lines changed

src/device/arm-linux/giopctl-arm-linux.cpp

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,50 @@
2020
namespace iotjs {
2121

2222

23-
class GpioControlInst : public GpioControl {
23+
class GpioControlImpl : public GpioControl {
2424
public:
25-
explicit GpioControlInst(JObject& jgpioctl);
25+
explicit GpioControlImpl(JObject& jgpioctl);
26+
2627
virtual int Initialize(void);
2728
virtual void Release(void);
28-
virtual int PinMode(uint32_t portpin);
29-
virtual int WritePin(uint32_t portpin, uint8_t data);
30-
virtual int ReadPin(uint32_t portpin, uint8_t* pdata);
29+
virtual int SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb);
30+
virtual int SetPin(int32_t pin, int32_t dir, int32_t mode);
3131
};
3232

3333

3434
//-----------------------------------------------------------------------------
3535

3636
GpioControl* GpioControl::Create(JObject& jgpioctl)
3737
{
38-
return new GpioControlInst(jgpioctl);
38+
return new GpioControlImpl(jgpioctl);
3939
}
4040

4141

42-
GpioControlInst::GpioControlInst(JObject& jgpioctl)
42+
GpioControlImpl::GpioControlImpl(JObject& jgpioctl)
4343
: GpioControl(jgpioctl) {
4444
}
4545

4646

47-
int GpioControlInst::Initialize(void) {
47+
int GpioControlImpl::Initialize(void) {
4848
if (_fd > 0 )
49-
return IOTJS_GPIO_INUSE;
49+
return GPIO_ERR_INITALIZE;
5050

5151
_fd = 1;
5252
return _fd;
5353
}
5454

5555

56-
void GpioControlInst::Release(void) {
56+
void GpioControlImpl::Release(void) {
5757
_fd = 0;
5858
}
5959

6060

61-
int GpioControlInst::PinMode(uint32_t portpin) {
62-
if (_fd <= 0)
63-
return IOTJS_GPIO_NOTINITED;
64-
return 0;
65-
}
66-
67-
68-
int GpioControlInst::WritePin(uint32_t portpin, uint8_t data) {
69-
if (_fd <= 0)
70-
return IOTJS_GPIO_NOTINITED;
61+
int GpioControlImpl::SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb) {
7162
return 0;
7263
}
7364

7465

75-
int GpioControlInst::ReadPin(uint32_t portpin, uint8_t* pdata) {
76-
if (_fd <= 0)
77-
return IOTJS_GPIO_NOTINITED;
66+
int GpioControlImpl::SetPin(int32_t pin, int32_t dir, int32_t mode) {
7867
return 0;
7968
}
8069

src/device/arm-nuttx/gpioctl_arm_nuttx.cpp

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,81 +28,56 @@
2828
namespace iotjs {
2929

3030

31-
class GpioControlInst : public GpioControl {
31+
class GpioControlImpl : public GpioControl {
3232
public:
33-
explicit GpioControlInst(JObject& jgpioctl);
33+
explicit GpioControlImpl(JObject& jgpioctl);
34+
3435
virtual int Initialize(void);
3536
virtual void Release(void);
36-
virtual int PinMode(uint32_t portpin);
37-
virtual int WritePin(uint32_t portpin, uint8_t data);
38-
virtual int ReadPin(uint32_t portpin, uint8_t* pdata);
37+
virtual int SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb);
38+
virtual int SetPin(int32_t pin, int32_t dir, int32_t mode);
3939
};
4040

4141
//-----------------------------------------------------------------------------
4242

4343
GpioControl* GpioControl::Create(JObject& jgpioctl)
4444
{
45-
return new GpioControlInst(jgpioctl);
45+
return new GpioControlImpl(jgpioctl);
4646
}
4747

4848

49-
GpioControlInst::GpioControlInst(JObject& jgpioctl)
49+
GpioControlImpl::GpioControlImpl(JObject& jgpioctl)
5050
: GpioControl(jgpioctl) {
5151
}
5252

5353

54-
int GpioControlInst::Initialize(void) {
54+
int GpioControlImpl::Initialize(void) {
5555
if (_fd > 0 )
56-
return IOTJS_GPIO_INUSE;
56+
return GPIO_ERR_INITALIZE;
5757

5858
const char* devfilepath = "/dev/gpio";
5959
_fd = open(devfilepath, O_RDWR);
6060
DDDLOG("gpio> %s : fd(%d)", devfilepath, _fd);
6161
return _fd;
6262
}
6363

64-
void GpioControlInst::Release(void) {
64+
65+
void GpioControlImpl::Release(void) {
6566
if (_fd > 0) {
6667
close(_fd);
6768
}
6869
_fd = 0;
6970
}
7071

7172

72-
int GpioControlInst::PinMode(uint32_t portpin) {
73-
if (_fd > 0) {
74-
struct gpioioctl_config_s cdata;
75-
cdata.port = portpin;
76-
return ioctl(_fd, GPIOIOCTL_CONFIG, (long unsigned int)&cdata);
77-
}
78-
return IOTJS_GPIO_NOTINITED;
79-
}
80-
8173

82-
int GpioControlInst::WritePin(uint32_t portpin, uint8_t data) {
83-
if (_fd > 0) {
84-
struct gpioioctl_write_s wdata;
85-
wdata.port = portpin;
86-
wdata.data = data;
87-
return ioctl(_fd, GPIOIOCTL_WRITE, (long unsigned int)&wdata);
88-
}
89-
return IOTJS_GPIO_NOTINITED;
74+
int GpioControlImpl::SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb) {
75+
return 0;
9076
}
9177

9278

93-
int GpioControlInst::ReadPin(uint32_t portpin, uint8_t* pdata) {
94-
if (_fd > 0) {
95-
struct gpioioctl_write_s wdata;
96-
int ret;
97-
wdata.port = portpin;
98-
wdata.port = *pdata = 0;
99-
ret = ioctl(_fd, GPIOIOCTL_READ, (long unsigned int)&wdata);
100-
if (ret >= 0) {
101-
*pdata = wdata.data;
102-
}
103-
return ret;
104-
}
105-
return IOTJS_GPIO_NOTINITED;
79+
int GpioControlImpl::SetPin(int32_t pin, int32_t dir, int32_t mode) {
80+
return 0;
10681
}
10782

10883

src/device/x86-linux/gpioctl_x86_linux.cpp

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,133 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if defined(__LINUX__)
17-
1816
#include "iotjs_def.h"
1917
#include "iotjs_objectwrap.h"
2018
#include "iotjs_module_gpioctl.h"
2119

20+
//
21+
// this is a dummy emulator code for x86.
22+
// please add new 'device' file for your purpose.
23+
// use timer with 1msec delay to implement async callback.
24+
// real gpio implementation would be better to use threading
25+
//
2226

2327
namespace iotjs {
2428

25-
/*
26-
* Use default dummy for linux-x86
27-
*/
29+
30+
// type for delayed callback
31+
typedef enum {
32+
DELAYEDCALL_SETPIN = 1,
33+
} DelayedCallType;
34+
35+
36+
// base class for delayed callback
37+
struct DelayedCallBase {
38+
DelayedCallType type;
39+
uv_timer_t timer;
40+
};
41+
42+
43+
// setspin delayed callback class
44+
struct DelayedCallSetpin : public DelayedCallBase {
45+
GpioCbDataSetpin* data;
46+
GpioSetpinCb aftersetpin;
47+
int result;
48+
};
49+
50+
51+
static void timerHandleTimeout(uv_timer_t* handle) {
52+
uv_timer_stop(handle);
53+
54+
DelayedCallBase* base;
55+
base = reinterpret_cast<DelayedCallBase*>(handle->data);
56+
switch (base->type) {
57+
case DELAYEDCALL_SETPIN : {
58+
DelayedCallSetpin* dcsp =
59+
reinterpret_cast<DelayedCallSetpin*>(base);
60+
GpioCbDataSetpin* setpin =
61+
reinterpret_cast<GpioCbDataSetpin*>(dcsp->data);
62+
63+
DDDLOG("x86 linux gpio dummy setpin w/cb pin(%d), dir(%d), mode(%d)",
64+
setpin->pin, setpin->dir, setpin->mode);
65+
66+
dcsp->result = (setpin->pin >= 0) ? 0 : GPIO_ERR_INVALIDPARAM;
67+
dcsp->aftersetpin(dcsp->data, dcsp->result);
68+
delete dcsp;
69+
break;
70+
}
71+
default:
72+
IOTJS_ASSERT(false);
73+
break;
74+
}
75+
}
76+
77+
78+
//-----------------------------------------------------------------------------
79+
80+
class GpioControlImpl : public GpioControl {
81+
public:
82+
explicit GpioControlImpl(JObject& jgpioctl);
83+
84+
virtual int Initialize(void);
85+
virtual void Release(void);
86+
virtual int SetPin(GpioCbDataSetpin* setpin, GpioSetpinCb cb);
87+
virtual int SetPin(int32_t pin, int32_t dir, int32_t mode);
88+
};
89+
2890

2991
GpioControl* GpioControl::Create(JObject& jgpioctl)
3092
{
31-
return new GpioControl(jgpioctl);
93+
return new GpioControlImpl(jgpioctl);
3294
}
3395

3496

35-
} // namespace iotjs
97+
GpioControlImpl::GpioControlImpl(JObject& jgpioctl)
98+
: GpioControl(jgpioctl) {
99+
}
100+
101+
102+
int GpioControlImpl::Initialize(void) {
103+
if (_fd > 0 )
104+
return GPIO_ERR_INITALIZE;
105+
106+
DDDLOG("x86 linux gpio dummy initalize");
107+
_fd = 1;
108+
return _fd;
109+
}
110+
111+
112+
void GpioControlImpl::Release(void) {
113+
DDDLOG("x86 linux gpio dummy release");
114+
_fd = 0;
115+
}
116+
117+
118+
// callback is provided for SetPin
119+
int GpioControlImpl::SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb) {
120+
Environment* env = Environment::GetEnv();
121+
DelayedCallSetpin* delay = new DelayedCallSetpin;
122+
delay->type = DELAYEDCALL_SETPIN;
123+
delay->data = setpin_data;
124+
delay->aftersetpin = cb;
125+
delay->result = 0;
36126

37-
#endif // __NUTTX__
127+
uv_timer_init(env->loop(), &delay->timer);
128+
delay->timer.data = delay;
129+
uv_timer_start(&delay->timer, timerHandleTimeout, 1, 0);
130+
131+
return 0;
132+
}
133+
134+
135+
// callback is omiited for SetPin
136+
int GpioControlImpl::SetPin(int32_t pin, int32_t dir, int32_t mode) {
137+
DDDLOG("x86 linux gpio dummy setpin pin(%d), dir(%d), mode(%d)",
138+
pin, dir, mode);
139+
140+
// return result of SetPin
141+
return (pin >= 0) ? 0 : GPIO_ERR_INVALIDPARAM;
142+
}
143+
144+
145+
} // namespace iotjs

src/iotjs_gpiocbwrap.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright 2015 Samsung Electronics Co., Ltd.
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+
#include "iotjs_gpiocbwrap.h"
17+
18+
19+
namespace iotjs {
20+
21+
22+
GpioCbWrap::GpioCbWrap(JObject& jcallback, GpioCbData* cbdata)
23+
: __cbdata(cbdata)
24+
, _jcallback(NULL) {
25+
if (!jcallback.IsNull()) {
26+
_jcallback = new JObject(jcallback);
27+
}
28+
}
29+
30+
31+
GpioCbWrap::~GpioCbWrap() {
32+
if (_jcallback != NULL) {
33+
delete _jcallback;
34+
}
35+
}
36+
37+
38+
JObject& GpioCbWrap::jcallback() {
39+
IOTJS_ASSERT(_jcallback != NULL);
40+
return *_jcallback;
41+
}
42+
43+
44+
GpioCbData* GpioCbWrap::cbdata() {
45+
return __cbdata;
46+
}
47+
48+
49+
void GpioCbWrap::Dispatched() {
50+
cbdata()->data = this;
51+
}
52+
53+
54+
} // namespace iotjs

0 commit comments

Comments
 (0)