aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kocialkowski <contact@paulk.fr>2013-06-28 12:27:23 +0200
committerPaul Kocialkowski <contact@paulk.fr>2013-06-28 12:27:23 +0200
commit1edc56a5fcb62dfea78fc27e29d7d87eda8c5662 (patch)
treeba3d1db7ab5a34eaffcd1885494c586138a17342
parenta81f656e6f6109a1fb838d08235ad46d5d0a87de (diff)
downloadhardware_replicant_libsamsung-ipc-1edc56a5fcb62dfea78fc27e29d7d87eda8c5662.tar.gz
hardware_replicant_libsamsung-ipc-1edc56a5fcb62dfea78fc27e29d7d87eda8c5662.tar.bz2
hardware_replicant_libsamsung-ipc-1edc56a5fcb62dfea78fc27e29d7d87eda8c5662.zip
New utilities (not exposed by the API) for various I/O and ipc fill functions
Change-Id: I0bd1d7781971f217903f307b5ac418180afc0d50 Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rw-r--r--Android.mk1
-rw-r--r--include/samsung-ipc.h17
-rw-r--r--samsung-ipc/Makefile.am2
-rw-r--r--samsung-ipc/ipc_util.c33
-rw-r--r--samsung-ipc/util.c282
-rw-r--r--samsung-ipc/util.h35
6 files changed, 359 insertions, 11 deletions
diff --git a/Android.mk b/Android.mk
index 07ebe8d..e1f635e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -60,6 +60,7 @@ samsung-ipc_files := \
samsung-ipc/net.c \
samsung-ipc/sec.c \
samsung-ipc/sms.c \
+ samsung-ipc/util.c \
samsung-ipc/device/crespo/crespo_ipc.c \
samsung-ipc/device/aries/aries_ipc.c \
samsung-ipc/device/xmm6260/xmm6260_loader.c \
diff --git a/include/samsung-ipc.h b/include/samsung-ipc.h
index 02d2e07..b68f6fc 100644
--- a/include/samsung-ipc.h
+++ b/include/samsung-ipc.h
@@ -37,6 +37,7 @@
struct ipc_client;
struct ipc_handlers;
+struct ipc_header;
struct ipc_message_info {
unsigned char mseq;
@@ -106,20 +107,16 @@ char *ipc_client_gprs_get_iface(struct ipc_client *client, int cid);
int ipc_client_gprs_get_capabilities(struct ipc_client *client,
struct ipc_client_gprs_capabilities *capabilities);
-/* Utility functions */
-void ipc_client_log_recv(struct ipc_client *client,
- struct ipc_message_info *response, const char *prefix);
-void ipc_client_log_send(struct ipc_client *client,
- struct ipc_message_info *request, const char *prefix);
const char *ipc_response_type_to_str(int type);
const char *ipc_request_type_to_str(int type);
const char *ipc_command_to_str(int command);
-
void ipc_client_hex_dump(struct ipc_client *client, void *data, int size);
-void *ipc_client_mtd_read(struct ipc_client *client, char *mtd_name, int size,
- int block_size);
-void *ipc_client_file_read(struct ipc_client *client, char *file_name, int size,
- int block_size);
+void ipc_client_log_recv(struct ipc_client *client,
+ struct ipc_message_info *response, const char *prefix);
+void ipc_client_log_send(struct ipc_client *client,
+ struct ipc_message_info *request, const char *prefix);
+void ipc_header_fill(struct ipc_header *header, struct ipc_message_info *message);
+void ipc_message_info_fill(struct ipc_header *header, struct ipc_message_info *message);
/*
* Samsung-IPC protocol
diff --git a/samsung-ipc/Makefile.am b/samsung-ipc/Makefile.am
index d303310..25d730f 100644
--- a/samsung-ipc/Makefile.am
+++ b/samsung-ipc/Makefile.am
@@ -27,8 +27,10 @@ libsamsung_ipc_la_SOURCES = \
gprs.c \
call.c \
net.c \
+ util.c \
ipc.h \
ipc_devices.h \
+ util.h \
device/crespo/crespo_ipc.c \
device/aries/aries_ipc.c \
device/aries/sipc4.h \
diff --git a/samsung-ipc/ipc_util.c b/samsung-ipc/ipc_util.c
index 2eb54ef..6ec54f2 100644
--- a/samsung-ipc/ipc_util.c
+++ b/samsung-ipc/ipc_util.c
@@ -1,6 +1,7 @@
/*
* This file is part of libsamsung-ipc.
*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
* Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
*
* libsamsung-ipc is free software: you can redistribute it and/or modify
@@ -19,6 +20,7 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
@@ -30,7 +32,6 @@
#include <mtd/mtd-abi.h>
#include <samsung-ipc.h>
-
#include "ipc.h"
/* Log utils */
@@ -442,6 +443,36 @@ void ipc_client_log_send(struct ipc_client *client,
}
}
+void ipc_header_fill(struct ipc_header *header, struct ipc_message_info *message)
+{
+ if (header == NULL || message == NULL)
+ return;
+
+ memset(header, 0, sizeof(struct ipc_header));
+ header->mseq = message->mseq;
+ header->aseq = message->aseq;
+ header->group = message->group;
+ header->index = message->index;
+ header->type = message->type;
+ header->length = message->length + sizeof(struct ipc_header);
+}
+
+void ipc_message_info_fill(struct ipc_header *header, struct ipc_message_info *message)
+{
+ if (header == NULL || message == NULL)
+ return;
+
+ memset(message, 0, sizeof(struct ipc_message_info));
+ message->mseq = header->mseq;
+ message->aseq = header->aseq;
+ message->group = header->group;
+ message->index = header->index;
+ message->type = header->type;
+ message->cmd = IPC_COMMAND(message);
+ message->length = 0;
+ message->data = NULL;
+}
+
void *ipc_client_mtd_read(struct ipc_client *client, char *mtd_name, int size,
int block_size)
{
diff --git a/samsung-ipc/util.c b/samsung-ipc/util.c
new file mode 100644
index 0000000..85fffcb
--- /dev/null
+++ b/samsung-ipc/util.c
@@ -0,0 +1,282 @@
+/*
+ * This file is part of libsamsung-ipc.
+ *
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * libsamsung-ipc is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libsamsung-ipc is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <asm/types.h>
+#include <mtd/mtd-abi.h>
+#include <linux/netlink.h>
+#include <net/if.h>
+
+#include "util.h"
+
+void *file_data_read(char *path, int size, int block_size)
+{
+ void *data = NULL;
+ int fd = -1;
+
+ unsigned char *p;
+ int count;
+ int rc;
+
+ if (path == NULL || size <= 0 || block_size <= 0)
+ return NULL;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ data = malloc(size);
+ memset(data, 0, size);
+
+ p = (unsigned char *) data;
+
+ count = 0;
+ while (count < size) {
+ rc = read(fd, p, size - count > block_size ? block_size : size - count);
+ if (rc < 0)
+ goto error;
+
+ p += rc;
+ count += rc;
+ }
+
+ goto complete;
+
+error:
+ if (data != NULL)
+ free(data);
+ data = NULL;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return data;
+}
+
+int network_iface_up(char *iface, int domain, int type)
+{
+ struct ifreq ifr;
+ int fd = -1;
+ int rc;
+
+ if (iface == NULL)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+
+ fd = socket(domain, type, 0);
+ if (fd < 0)
+ goto error;
+
+ rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
+
+ ifr.ifr_flags |= IFF_UP;
+
+ rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int network_iface_down(char *iface, int domain, int type)
+{
+ struct ifreq ifr;
+ int fd = -1;
+ int rc;
+
+ if (iface == NULL)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+
+ fd = socket(domain, type, 0);
+ if (fd < 0)
+ goto error;
+
+ rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
+
+ ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP));
+
+ rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_value_read(char *path)
+{
+ char buffer[100];
+ int value;
+ int fd = -1;
+ int rc;
+
+ if (path == NULL)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = read(fd, &buffer, sizeof(buffer));
+ if (rc <= 0)
+ goto error;
+
+ value = atoi(buffer);
+ goto complete;
+
+error:
+ value = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return value;
+}
+
+int sysfs_value_write(char *path, int value)
+{
+ char buffer[100];
+ int fd = -1;
+ int rc;
+
+ if (path == NULL)
+ return -1;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
+
+ snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);
+
+ rc = write(fd, buffer, strlen(buffer));
+ if (rc < (int) strlen(buffer))
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_string_read(char *path, char *buffer, int length)
+{
+ int fd = -1;
+ int rc;
+
+ if (path == NULL || buffer == NULL || length <= 0)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = read(fd, buffer, length);
+ if (rc <= 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_string_write(char *path, char *buffer, int length)
+{
+ int fd = -1;
+ int rc;
+
+ if (path == NULL || buffer == NULL || length <= 0)
+ return -1;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = write(fd, buffer, length);
+ if (rc <= 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+// vim:ts=4:sw=4:expandtab
diff --git a/samsung-ipc/util.h b/samsung-ipc/util.h
new file mode 100644
index 0000000..c2a36a1
--- /dev/null
+++ b/samsung-ipc/util.h
@@ -0,0 +1,35 @@
+/*
+ * This file is part of libsamsung-ipc.
+ *
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
+ *
+ * libsamsung-ipc is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libsamsung-ipc is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef __UTIL_H__
+#define __UTIL_H__
+
+void *file_data_read(char *file_name, int size, int block_size);
+int network_iface_up(char *iface, int domain, int type);
+int network_iface_down(char *iface, int domain, int type);
+int sysfs_value_read(char *path);
+int sysfs_value_write(char *path, int value);
+int sysfs_string_read(char *path, char *buffer, int length);
+int sysfs_string_write(char *path, char *buffer, int length);
+
+#endif
+
+// vim:ts=4:sw=4:expandtab