src/monitor/monitor.c
src/monitor/monitor-thread.c
src/monitor/monitor-command.c
- src/monitor/request-server.c
src/monitor/request-handler.c
)
int monitor_command_init(struct monitor_command **cmd);
void monitor_command_exit(struct monitor_command *cmd);
+int request_server_init(void);
+void request_server_exit(void);
+
#endif
+++ /dev/null
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
- *
- * 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.
- */
-#ifndef __REQUEST_HANDLER_H__
-#define __REQUEST_HANDLER_H__
-
-#include <glib.h>
-#include <string.h>
-
-#include <util/resource.h>
-
-struct request_client {
- int socket_fd;
- int nr_resources;
- struct thread *worker;
- GHashTable *resource_table;
-};
-
-int create_request_client(int socket_fd);
-
-#endif /* __REQUEST_HANDLER_H__ */
#define _GNU_SOURCE
#include <errno.h>
#include <util/log.h>
-#include <util/request.h>
+#include <monitor/request.h>
#include <util/resource.h>
#include <stdio.h>
{
int ret;
+ ret = request_server_init();
+ if (ret < 0) {
+ _E("failed to initialize request server\n");
+ return ret;
+ }
+
ret = monitor_thread_init(&g_monitor);
if (ret < 0) {
_E("failed to initialize monitor thread\n");
+ request_server_exit();
return ret;
}
static void monitor_exit(void *data)
{
monitor_thread_exit(&g_monitor);
+ request_server_exit();
unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, monitor_setup, NULL);
}
*/
/**
- * @file request-handler-thread.c
+ * @file request-handler.c
* @brief TBD
* @ingroup TBD
*/
#include <util/log.h>
#include <util/resource.h>
#include <util/thread.h>
-#include <util/device-notifier.h>
-#include <util/devices.h>
-#include <util/request.h>
-#include <util/request-handler.h>
+#include <monitor/request.h>
+#include <monitor/monitor.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <assert.h>
+#define PENDING_MAX 3
+#define REQUEST_SERVER_PORT 10001
+
+struct request_client {
+ int socket_fd;
+ int nr_resources;
+ struct thread *worker;
+ GHashTable *resource_table;
+};
+
+static bool g_request_server_run;
+static struct thread *g_server_thread;
+
static void update_resource(gpointer key, gpointer value, gpointer user_data)
{
struct resource *res = value;
return THREAD_RETURN_DONE;
}
-int create_request_client(int socket_fd)
+static int create_request_client(int socket_fd)
{
struct request_client *client;
return 0;
}
+static int request_server_func(void *ctx, void **result)
+{
+ struct sockaddr_in address;
+ struct timeval wait;
+ int opt = true;
+ int server_socket;
+ int addrlen;
+ int ret;
+ fd_set fds;
+
+ if (!g_request_server_run)
+ return THREAD_RETURN_DONE;
+
+ init_resource_id();
+
+ /* 0. initialize server socket */
+ server_socket = socket(AF_INET, SOCK_STREAM, 0);
+ if (server_socket < 0) {
+ _E("Failed to initialize socket");
+ goto error_out;
+ }
+
+ if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
+ (char *)&opt, sizeof(opt)) < 0) {
+ _E("Failed to setsockopt");
+ goto error_out_close;
+ }
+
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = INADDR_ANY;
+ address.sin_port = htons(REQUEST_SERVER_PORT);
+
+ if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
+ _E("Failed to bind");
+ goto error_out_close;
+ }
+
+ if (listen(server_socket, PENDING_MAX) < 0) {
+ _E("Failed to begin listenning");
+ goto error_out_close;
+ }
+
+ addrlen = sizeof(address);
+
+ while (g_request_server_run) {
+ FD_ZERO(&fds);
+ FD_SET(server_socket, &fds);
+
+ wait.tv_sec = 1;
+ wait.tv_usec = 0;
+
+ ret = select(FD_SETSIZE, &fds, NULL, NULL, &wait);
+ if (ret < 0) {
+ _E("failed to select");
+ goto error_out_close;
+ }
+
+ if (FD_ISSET(server_socket, &fds)) {
+ int new_socket = accept(server_socket, (struct sockaddr *)&address,
+ (socklen_t *)&addrlen);
+
+ if (new_socket < 0) {
+ _E("Failed to accept");
+ goto error_out_close;
+ }
+
+ create_request_client(new_socket);
+ }
+ }
+
+ close(server_socket);
+
+ return THREAD_RETURN_DONE;
+
+error_out_close:
+ close(server_socket);
+error_out:
+
+ return THREAD_RETURN_ERROR;
+}
+
+int request_server_init(void)
+{
+ g_request_server_run = true;
+ if (create_daemon_thread(&g_server_thread, request_server_func, NULL))
+ _E("Failed to create request_server_thread ");
+
+ return 0;
+}
+
+void request_server_exit(void)
+{
+ g_request_server_run = false;
+ destroy_thread(g_server_thread);
+}
+++ /dev/null
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
- *
- * 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.
- */
-
-/**
- * @file request-server.c
- * @brief TBD
- * @ingroup TBD
- */
-
-#include <glib.h>
-
-#include <util/common.h>
-#include <util/log.h>
-#include <util/resource.h>
-#include <util/thread.h>
-#include <util/device-notifier.h>
-#include <util/devices.h>
-#include <util/request.h>
-#include <util/request-handler.h>
-
-#include <string.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h>
-#include <assert.h>
-
-#define PENDING_MAX 3
-#define REQUEST_SERVER_PORT 10001
-
-static bool g_request_server_run;
-
-static int request_server_func(void *ctx, void **result)
-{
- struct sockaddr_in address;
- struct timeval wait;
- int opt = true;
- int server_socket;
- int addrlen;
- int ret;
- fd_set fds;
-
- if (!g_request_server_run)
- return THREAD_RETURN_DONE;
-
- init_resource_id();
-
- /* 0. initialize server socket */
- server_socket = socket(AF_INET, SOCK_STREAM, 0);
- if (server_socket < 0) {
- _E("Failed to initialize socket");
- goto error_out;
- }
-
- if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
- (char *)&opt, sizeof(opt)) < 0) {
- _E("Failed to setsockopt");
- goto error_out_close;
- }
-
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(REQUEST_SERVER_PORT);
-
- if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
- _E("Failed to bind");
- goto error_out_close;
- }
-
- if (listen(server_socket, PENDING_MAX) < 0) {
- _E("Failed to begin listenning");
- goto error_out_close;
- }
-
- addrlen = sizeof(address);
-
- while (g_request_server_run) {
- FD_ZERO(&fds);
- FD_SET(server_socket, &fds);
-
- wait.tv_sec = 1;
- wait.tv_usec = 0;
-
- ret = select(FD_SETSIZE, &fds, NULL, NULL, &wait);
- if (ret < 0) {
- _E("failed to select");
- goto error_out_close;
- }
-
- if (FD_ISSET(server_socket, &fds)) {
- int new_socket = accept(server_socket, (struct sockaddr *)&address,
- (socklen_t *)&addrlen);
-
- if (new_socket < 0) {
- _E("Failed to accept");
- goto error_out_close;
- }
-
- create_request_client(new_socket);
- }
- }
-
- close(server_socket);
-
- return THREAD_RETURN_DONE;
-
-error_out_close:
- close(server_socket);
-error_out:
-
- return THREAD_RETURN_ERROR;
-}
-
-static struct thread *server_thread;
-
-static int request_server_init_done(void *data, void *user_data)
-{
- g_request_server_run = true;
- if (create_daemon_thread(&server_thread, request_server_func, NULL))
- _E("Failed to create request_server_thread ");
-
- return 0;
-}
-
-static void request_server_init(void *data)
-{
- /* nothing to do */
-}
-
-static void request_server_exit(void *data)
-{
- unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, request_server_init_done, NULL);
- g_request_server_run = false;
- destroy_thread(server_thread);
-}
-
-static int request_server_probe(void *data)
-{
- int ret = 0;
-
- ret = register_notifier(DEVICE_NOTIFIER_INIT_DONE,
- request_server_init_done, NULL);
- if (ret < 0) {
- _E("Failed to register a callback function");
- return ret;
- }
-
- return 0;
-}
-
-static const struct device_ops request_server_device_ops = {
- .name = "request-server",
- .probe = request_server_probe,
- .init = request_server_init,
- .exit = request_server_exit,
-};
-
-DEVICE_OPS_REGISTER(&request_server_device_ops);