gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
+ pb_pwm_data_h pwm_handle;
- st_pwm.device = device;
- st_pwm.channel = channel;
-
- ret = peripheral_bus_pwm_open(&st_pwm);
- peripheral_io_gdbus_pwm_complete_open(pwm, invocation, ret);
+ ret = peripheral_bus_pwm_open(device, channel, &pwm_handle, user_data);
+ peripheral_io_gdbus_pwm_complete_open(pwm, invocation, GPOINTER_TO_UINT(pwm_handle), ret);
return true;
}
gboolean handle_pwm_close(
PeripheralIoGdbusPwm *pwm,
GDBusMethodInvocation *invocation,
- gint device,
- gint channel,
+ gint handle,
gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
+ pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
- st_pwm.device = device;
- st_pwm.channel = channel;
- ret = peripheral_bus_pwm_close(&st_pwm);
+ ret = peripheral_bus_pwm_close(pwm_handle, user_data);
peripheral_io_gdbus_pwm_complete_close(pwm, invocation, ret);
return true;
gboolean handle_pwm_set_duty_cycle(
PeripheralIoGdbusPwm *pwm,
GDBusMethodInvocation *invocation,
- gint device,
- gint channel,
+ gint handle,
gint duty_cycle,
gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
-
- st_pwm.device = device;
- st_pwm.channel = channel;
+ pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
- ret = peripheral_bus_pwm_set_duty_cycle(&st_pwm, duty_cycle);
+ ret = peripheral_bus_pwm_set_duty_cycle(pwm_handle, duty_cycle);
peripheral_io_gdbus_pwm_complete_set_duty_cycle(pwm, invocation, ret);
return true;
gboolean handle_pwm_set_period(
PeripheralIoGdbusPwm *pwm,
GDBusMethodInvocation *invocation,
- gint device,
- gint channel,
+ gint handle,
gint period,
gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
-
- st_pwm.device = device;
- st_pwm.channel = channel;
+ pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
- ret = peripheral_bus_pwm_set_period(&st_pwm, period);
+ ret = peripheral_bus_pwm_set_period(pwm_handle, period);
peripheral_io_gdbus_pwm_complete_set_period(pwm, invocation, ret);
return true;
gboolean handle_pwm_set_enable(
PeripheralIoGdbusPwm *pwm,
GDBusMethodInvocation *invocation,
- gint device,
- gint channel,
+ gint handle,
gint enable,
gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
-
- st_pwm.device = device;
- st_pwm.channel = channel;
+ pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
- ret = peripheral_bus_pwm_set_enable(&st_pwm, enable);
+ ret = peripheral_bus_pwm_set_enable(pwm_handle, enable);
peripheral_io_gdbus_pwm_complete_set_enable(pwm, invocation, ret);
return true;
gboolean handle_pwm_get_duty_cycle(
PeripheralIoGdbusPwm *pwm,
GDBusMethodInvocation *invocation,
- gint device,
- gint channel,
+ gint handle,
gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
-
- st_pwm.device = device;
- st_pwm.channel = channel;
+ pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
+ int duty_cycle;
- ret = peripheral_bus_pwm_get_duty_cycle(&st_pwm, &st_pwm.duty_cycle);
- peripheral_io_gdbus_pwm_complete_get_duty_cycle(pwm, invocation, st_pwm.duty_cycle, ret);
+ ret = peripheral_bus_pwm_get_duty_cycle(pwm_handle, &duty_cycle);
+ peripheral_io_gdbus_pwm_complete_get_duty_cycle(pwm, invocation, duty_cycle, ret);
return true;
}
gboolean handle_pwm_get_period(
PeripheralIoGdbusPwm *pwm,
GDBusMethodInvocation *invocation,
- gint device,
- gint channel,
+ gint handle,
gpointer user_data)
{
peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
- struct _peripheral_pwm_s st_pwm;
-
- st_pwm.device = device;
- st_pwm.channel = channel;
+ pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
+ int period;
- ret = peripheral_bus_pwm_get_period(&st_pwm, &st_pwm.period);
- peripheral_io_gdbus_pwm_complete_get_period(pwm, invocation, st_pwm.period, ret);
+ ret = peripheral_bus_pwm_get_period(pwm_handle, &period);
+ peripheral_io_gdbus_pwm_complete_get_period(pwm, invocation, period, ret);
return true;
}
g_signal_connect(pb_data->pwm_skeleton,
"handle-open",
G_CALLBACK(handle_pwm_open),
- NULL);
+ pb_data);
g_signal_connect(pb_data->pwm_skeleton,
"handle-close",
G_CALLBACK(handle_pwm_close),
- NULL);
+ pb_data);
g_signal_connect(pb_data->pwm_skeleton,
"handle-set-duty-cycle",
G_CALLBACK(handle_pwm_set_duty_cycle),
- NULL);
+ pb_data);
g_signal_connect(pb_data->pwm_skeleton,
"handle-set-period",
G_CALLBACK(handle_pwm_set_period),
- NULL);
+ pb_data);
g_signal_connect(pb_data->pwm_skeleton,
"handle-set-enable",
G_CALLBACK(handle_pwm_set_enable),
- NULL);
+ pb_data);
g_signal_connect(pb_data->pwm_skeleton,
- "handle-get-duty",
+ "handle-get-duty-cycle",
G_CALLBACK(handle_pwm_get_duty_cycle),
- NULL);
+ pb_data);
g_signal_connect(pb_data->pwm_skeleton,
"handle-get-period",
G_CALLBACK(handle_pwm_get_period),
- NULL);
+ pb_data);
manager = g_dbus_object_manager_server_new(PERIPHERAL_GDBUS_PWM_PATH);
/* devices */
GList *gpio_list;
GList *i2c_list;
+ GList *pwm_list;
GList *uart_list;
/* gdbus variable */
guint reg_id;
} peripheral_bus_i2c_data_s;
typedef struct {
+ int device;
+ int channel;
+ /* client info */
+ pb_client_info_s client_info;
+} peripheral_bus_pwm_data_s;
+
+typedef struct {
int port;
int fd;
uint8_t *buffer;
typedef peripheral_bus_gpio_data_s *pb_gpio_data_h;
typedef peripheral_bus_i2c_data_s *pb_i2c_data_h;
+typedef peripheral_bus_pwm_data_s *pb_pwm_data_h;
typedef peripheral_bus_uart_data_s *pb_uart_data_h;
void peripheral_bus_emit_gpio_changed(PeripheralIoGdbusGpio *gpio,
#include <peripheral_io.h>
#include "pwm.h"
+#include "peripheral_io_gdbus.h"
+#include "peripheral_bus.h"
#include "peripheral_common.h"
-int peripheral_bus_pwm_open(peripheral_pwm_context_h pwm)
+static pb_pwm_data_h peripheral_bus_pwm_data_get(int device, int channel, GList **list)
{
- return pwm_open(pwm->device, pwm->channel);
+ GList *pwm_list = *list;
+ GList *link;
+ pb_pwm_data_h pwm_data;
+
+ link = pwm_list;
+ while (link) {
+ pwm_data = (pb_pwm_data_h)link->data;
+ if (pwm_data->device == device && pwm_data->channel == channel)
+ return pwm_data;
+ link = g_list_next(link);
+ }
+
+ return NULL;
+}
+
+static pb_pwm_data_h peripheral_bus_pwm_data_new(GList **list)
+{
+ GList *pwm_list = *list;
+ pb_pwm_data_h pwm_data;
+
+ pwm_data = (pb_pwm_data_h)calloc(1, sizeof(peripheral_bus_pwm_data_s));
+ if (pwm_data == NULL) {
+ _E("failed to allocate peripheral_bus_pwm_data_s");
+ return NULL;
+ }
+
+ *list = g_list_append(pwm_list, pwm_data);
+
+ return pwm_data;
+}
+
+static int peripheral_bus_pwm_data_free(pb_pwm_data_h pwm_handle, GList **list)
+{
+ GList *pwm_list = *list;
+ GList *link;
+ pb_pwm_data_h pwm_data;
+
+ link = pwm_list;
+ while (link) {
+ pwm_data = (pb_pwm_data_h)link->data;
+
+ if (pwm_data == pwm_handle) {
+ *list = g_list_remove_link(pwm_list, link);
+ free(pwm_data);
+ g_list_free(link);
+ return 0;
+ }
+ link = g_list_next(link);
+ }
+
+ return -1;
}
-int peripheral_bus_pwm_close(peripheral_pwm_context_h pwm)
+int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointer user_data)
{
- return pwm_close(pwm->device, pwm->channel);
+ peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
+ pb_pwm_data_h pwm_handle;
+ int ret;
+
+ if (peripheral_bus_pwm_data_get(device, channel, &pb_data->pwm_list)) {
+ _E("Resource is in use, device : %d, channel : %d", device, channel);
+ return PERIPHERAL_ERROR_RESOURCE_BUSY;
+ }
+
+ if ((ret = pwm_open(device, channel)) < 0)
+ goto open_err;
+
+ pwm_handle = peripheral_bus_pwm_data_new(&pb_data->pwm_list);
+ if (!pwm_handle) {
+ _E("peripheral_bus_pwm_data_new error");
+ ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+ goto err;
+ }
+
+ pwm_handle->device = device;
+ pwm_handle->channel = channel;
+ *pwm = pwm_handle;
+
+ return PERIPHERAL_ERROR_NONE;
+
+err:
+ pwm_close(device, channel);
+
+open_err:
+ return ret;
+}
+
+int peripheral_bus_pwm_close(pb_pwm_data_h pwm, gpointer user_data)
+{
+ peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
+ int ret;
+
+ if ((ret = pwm_close(pwm->device, pwm->channel)) < 0) {
+ _E("gpio_close error (%d)", ret);
+ return ret;
+ }
+
+ peripheral_bus_pwm_data_free(pwm, &pb_data->pwm_list);
+
+ return PERIPHERAL_ERROR_NONE;
}
-int peripheral_bus_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle)
+int peripheral_bus_pwm_set_duty_cycle(pb_pwm_data_h pwm, int duty_cycle)
{
return pwm_set_duty_cycle(pwm->device, pwm->channel, duty_cycle);
}
-int peripheral_bus_pwm_set_period(peripheral_pwm_context_h pwm, int period)
+int peripheral_bus_pwm_set_period(pb_pwm_data_h pwm, int period)
{
return pwm_set_period(pwm->device, pwm->channel, period);
}
-int peripheral_bus_pwm_set_enable(peripheral_pwm_context_h pwm, int enable)
+int peripheral_bus_pwm_set_enable(pb_pwm_data_h pwm, int enable)
{
return pwm_set_enable(pwm->device, pwm->channel, enable);
}
-int peripheral_bus_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle)
+int peripheral_bus_pwm_get_duty_cycle(pb_pwm_data_h pwm, int *duty_cycle)
{
return pwm_get_duty_cycle(pwm->device, pwm->channel, duty_cycle);
}
-int peripheral_bus_pwm_get_period(peripheral_pwm_context_h pwm, int *period)
+int peripheral_bus_pwm_get_period(pb_pwm_data_h pwm, int *period)
{
return pwm_get_period(pwm->device, pwm->channel, period);
}
#ifndef __PERIPHERAL_BUS_PWM_H__
#define __PERIPHERAL_BUS_PWM_H__
-int peripheral_bus_pwm_open(peripheral_pwm_context_h pwm);
-int peripheral_bus_pwm_close(peripheral_pwm_context_h pwm);
-int peripheral_bus_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle);
-int peripheral_bus_pwm_set_period(peripheral_pwm_context_h pwm, int period);
-int peripheral_bus_pwm_set_enable(peripheral_pwm_context_h pwm, int enable);
-int peripheral_bus_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle);
-int peripheral_bus_pwm_get_period(peripheral_pwm_context_h pwm, int*period);
+int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointer user_data);
+int peripheral_bus_pwm_close(pb_pwm_data_h pwm, gpointer user_data);
+int peripheral_bus_pwm_set_duty_cycle(pb_pwm_data_h pwm, int duty_cycle);
+int peripheral_bus_pwm_set_period(pb_pwm_data_h pwm, int period);
+int peripheral_bus_pwm_set_enable(pb_pwm_data_h pwm, int enable);
+int peripheral_bus_pwm_get_duty_cycle(pb_pwm_data_h pwm, int *duty_cycle);
+int peripheral_bus_pwm_get_period(pb_pwm_data_h pwm, int *period);
#endif /* __PERIPHERAL_BUS_PWM_H__ */
<method name="Open">
<arg type="i" name="device" direction="in"/>
<arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="out"/>
<arg type="i" name="result" direction="out"/>
</method>
<method name="Close">
- <arg type="i" name="device" direction="in"/>
- <arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="in"/>
<arg type="i" name="result" direction="out"/>
</method>
<method name="GetDutyCycle">
- <arg type="i" name="device" direction="in"/>
- <arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="in"/>
<arg type="i" name="duty_cycle" direction="out"/>
<arg type="i" name="result" direction="out"/>
</method>
<method name="SetDutyCycle">
- <arg type="i" name="device" direction="in"/>
- <arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="in"/>
<arg type="i" name="duty_cycle" direction="in"/>
<arg type="i" name="result" direction="out"/>
</method>
<method name="GetPeriod">
- <arg type="i" name="device" direction="in"/>
- <arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="in"/>
<arg type="i" name="period" direction="out"/>
<arg type="i" name="result" direction="out"/>
</method>
<method name="SetPeriod">
- <arg type="i" name="device" direction="in"/>
- <arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="in"/>
<arg type="i" name="period" direction="in"/>
<arg type="i" name="result" direction="out"/>
</method>
<method name="SetEnable">
- <arg type="i" name="device" direction="in"/>
- <arg type="i" name="channel" direction="in"/>
+ <arg type="u" name="handle" direction="in"/>
<arg type="i" name="enable" direction="in"/>
<arg type="i" name="result" direction="out"/>
</method>