Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out common functionality and implement device specific module #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/rockhopper/charger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
#
# LICENSE@@@

nyx_create_module(ChargerMain emulator/chargerlib.c)
if(${WEBOS_TARGET_MACHINE_IMPL} STREQUAL emulator)
nyx_create_module(ChargerMain chargerlib.c emulator/charger.c)
elseif(${WEBOS_TARGET_MACHINE_IMPL} STREQUAL device)
nyx_create_module(ChargerMain chargerlib.c device/charger.c)
endif()

Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@
#include <nyx/nyx_module.h>
#include <nyx/module/nyx_utils.h>

static nyx_device_t *nyxDev=NULL;
static void *charger_status_callback_context = NULL, *state_change_callback_context = NULL;
static nyx_device_callback_function_t charger_status_callback, state_change_callback;
static nyx_charger_event_t current_event = NYX_NO_NEW_EVENT;
#include "chargerlib.h"

nyx_charger_status_t gChargerStatus =
{
.charger_max_current = 0,
.connected = 0,
.powered = 0,
.dock_serial_number = {0},
};
nyx_device_t *nyxDev=NULL;
void *charger_status_callback_context = NULL;
void *state_change_callback_context = NULL;
nyx_device_callback_function_t charger_status_callback;
nyx_device_callback_function_t state_change_callback;

NYX_DECLARE_MODULE(NYX_DEVICE_CHARGER, "Charger");

Expand Down Expand Up @@ -77,10 +72,9 @@ nyx_error_t nyx_module_open (nyx_instance_t i, nyx_device_t** d)
nyx_module_register_method(i, (nyx_device_t*)nyxDev, NYX_CHARGER_QUERY_CHARGER_EVENT_MODULE_METHOD,
"charger_query_charger_event");


*d = (nyx_device_t*)nyxDev;

return NYX_ERROR_NONE;
return _charger_init();
}

nyx_error_t nyx_module_close (nyx_device_t* d)
Expand All @@ -98,12 +92,9 @@ nyx_error_t charger_query_charger_status(nyx_device_handle_t handle, nyx_charger
return NYX_ERROR_INVALID_VALUE;
}

memcpy(status,&gChargerStatus,sizeof(nyx_charger_status_t));
return NYX_ERROR_NONE;

return _charger_read_status(status);
}


nyx_error_t charger_register_charger_status_callback (nyx_device_handle_t handle, nyx_device_callback_function_t callback_func, void *context)
{
if (handle != nyxDev) {
Expand All @@ -129,8 +120,7 @@ nyx_error_t charger_enable_charging(nyx_device_handle_t handle, nyx_charger_stat
return NYX_ERROR_INVALID_VALUE;
}

memcpy(status,&gChargerStatus,sizeof(nyx_charger_status_t));
return NYX_ERROR_NONE;
return _charger_enable_charging(status);
}

nyx_error_t charger_disable_charging(nyx_device_handle_t handle, nyx_charger_status_t *status)
Expand All @@ -143,8 +133,7 @@ nyx_error_t charger_disable_charging(nyx_device_handle_t handle, nyx_charger_sta
return NYX_ERROR_INVALID_VALUE;
}

memcpy(status,&gChargerStatus,sizeof(nyx_charger_status_t));
return NYX_ERROR_NONE;
return _charger_disable_charging(status);
}

nyx_error_t charger_register_state_change_callback(nyx_device_handle_t handle, nyx_device_callback_function_t callback_func, void *context)
Expand All @@ -168,10 +157,5 @@ nyx_error_t charger_query_charger_event(nyx_device_handle_t handle, nyx_charger_
return NYX_ERROR_INVALID_HANDLE;
}

*event = current_event;

current_event = NYX_NO_NEW_EVENT;

return NYX_ERROR_NONE;
return _charger_query_charger_event(event);
}

28 changes: 28 additions & 0 deletions src/rockhopper/charger/chargerlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @@@LICENSE
*
* Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* LICENSE@@@ */

#ifndef CHARGER_H_
#define CHARGER_H_

nyx_error_t _charger_init(void);
nyx_error_t _charger_read_status(nyx_charger_status_t *status);
nyx_error_t _charger_enable_charging(nyx_charger_status_t *status);
nyx_error_t _charger_disable_charging(nyx_charger_status_t *status);
nyx_error_t _charger_query_charger_event(nyx_charger_event_t *event);

#endif
142 changes: 142 additions & 0 deletions src/rockhopper/charger/device/charger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* @@@LICENSE
*
* Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* LICENSE@@@ */

/**
* @file charger.c
*/

#include <glib.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>

#include <nyx/nyx_module.h>
#include <nyx/module/nyx_utils.h>

#include <glib.h>
#include <libudev.h>

GIOChannel *channel;

struct udev *udev;
struct udev_monitor *mon;

extern nyx_device_t *nyxDev;
extern void *charger_status_callback_context;
extern nyx_device_callback_function_t charger_status_callback;

#define CHARGER_USB_SYSFS_PATH "/sys/class/power_supply/usb/"
#define CHARGER_AC_SYSFS_PATH "/sys/class/power_supply/ac/"

nyx_charger_status_t gChargerStatus =
{
.charger_max_current = 0,
.connected = 0,
.powered = 0,
.dock_serial_number = {0},
};

static nyx_charger_event_t current_event = NYX_NO_NEW_EVENT;

gboolean _handle_power_supply_event(GIOChannel *channel, GIOCondition condition, gpointer data)
{
struct udev_device *dev;

if ((condition & G_IO_IN) == G_IO_IN) {
dev = udev_monitor_receive_device(mon);
if (dev) {
/* something related to power supply has changed; notify connected clients so
* they can query the new status */
charger_status_callback(nyxDev, NYX_CALLBACK_STATUS_DONE, charger_status_callback_context);
}
}

return TRUE;
}

nyx_error_t _charger_init(void)
{
int fd;

udev = udev_new();
if (!udev) {
nyx_error("Could not initialize udev component; battery status updates will not be available");
return;
}

mon = udev_monitor_new_from_netlink(udev, "kernel");
udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);

channel = g_io_channel_unix_new(fd);
g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_NVAL, _handle_power_supply_event, NULL);

return NYX_ERROR_NONE;
}

nyx_error_t _charger_read_status(nyx_charger_status_t *status)
{
int32_t online;

/* before we start to update the charger status we reset it completely */
memset(&gChargerStatus, 0, sizeof(nyx_charger_status_t));

if (nyx_utils_read_value(CHARGER_USB_SYSFS_PATH "online")) {
gChargerStatus.connected |= NYX_CHARGER_PC_CONNECTED;
gChargerStatus.powered |= NYX_CHARGER_USB_POWERED;
gChargerStatus.is_charging = 1;
}

if (nyx_utils_read_value(CHARGER_AC_SYSFS_PATH "online")) {
gChargerStatus.connected |= NYX_CHARGER_WALL_CONNECTED;
gChargerStatus.powered |= NYX_CHARGER_DIRECT_POWERED;
gChargerStatus.is_charging = 1;
}

memcpy(status, &gChargerStatus, sizeof(nyx_charger_status_t));

return NYX_ERROR_NONE;
}

nyx_error_t _charger_enable_charging(nyx_charger_status_t *status)
{
memcpy(status, &gChargerStatus, sizeof(nyx_charger_status_t));

return NYX_ERROR_NONE;
}

nyx_error_t _charger_disable_charging(nyx_charger_status_t *status)
{
memcpy(status, &gChargerStatus, sizeof(nyx_charger_status_t));

return NYX_ERROR_NONE;
}

nyx_error_t _charger_query_charger_event(nyx_charger_event_t *event)
{
*event = current_event;

current_event = NYX_NO_NEW_EVENT;

return NYX_ERROR_NONE;
}
74 changes: 74 additions & 0 deletions src/rockhopper/charger/emulator/charger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* @@@LICENSE
*
* Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* LICENSE@@@ */

/**
* @file charger.c
*/

#include <glib.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>

#include <nyx/nyx_module.h>

nyx_charger_status_t gChargerStatus =
{
.charger_max_current = 0,
.connected = 0,
.powered = 0,
.dock_serial_number = {0},
};

static nyx_charger_event_t current_event = NYX_NO_NEW_EVENT;

nyx_error_t _charger_init(void)
{
return NYX_ERROR_NONE;
}

nyx_error_t _charger_read_status(nyx_charger_status_t *status)
{
memcpy(status, &gChargerStatus, sizeof(nyx_charger_status_t));
return NYX_ERROR_NONE;
}

nyx_error_t _charger_enable_charging(nyx_charger_status_t *status)
{
memcpy(status, &gChargerStatus, sizeof(nyx_charger_status_t));
return NYX_ERROR_NONE;
}

nyx_error_t _charger_disable_charging(nyx_charger_status_t *status)
{
memcpy(status, &gChargerStatus, sizeof(nyx_charger_status_t));
return NYX_ERROR_NONE;
}

nyx_error_t _charger_query_charger_event(nyx_charger_event_t *event)
{
*event = current_event;

current_event = NYX_NO_NEW_EVENT;

return NYX_ERROR_NONE;
}