aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kocialkowski <contact@paulk.fr>2014-02-13 20:02:00 +0100
committerPaul Kocialkowski <contact@paulk.fr>2014-02-13 20:02:00 +0100
commit39ac0d29e1062a28528e94756e16a2e856eb3b49 (patch)
tree79ce02b331338359776e73efccd028448dda0ebf
parent60e94298d83d1180a49e668ed85bb2bfa0e38095 (diff)
downloadexternal_libsamsung-ipc-39ac0d29e1062a28528e94756e16a2e856eb3b49.zip
external_libsamsung-ipc-39ac0d29e1062a28528e94756e16a2e856eb3b49.tar.gz
external_libsamsung-ipc-39ac0d29e1062a28528e94756e16a2e856eb3b49.tar.bz2
utils: Files rename, new functions, proper types and few other cleanups
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rw-r--r--Android.mk2
-rw-r--r--samsung-ipc/Makefile.am4
-rw-r--r--samsung-ipc/devices/aries/aries_ipc.c4
-rw-r--r--samsung-ipc/devices/crespo/crespo_ipc.c4
-rw-r--r--samsung-ipc/devices/maguro/maguro_ipc.c4
-rw-r--r--samsung-ipc/devices/xmm6160/xmm6160.c4
-rw-r--r--samsung-ipc/devices/xmm6260/xmm6260_hsic.c4
-rw-r--r--samsung-ipc/devices/xmm6260/xmm6260_mipi.c4
-rw-r--r--samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c2
-rw-r--r--samsung-ipc/rfs.c14
-rw-r--r--samsung-ipc/utils.c (renamed from samsung-ipc/util.c)179
-rw-r--r--samsung-ipc/utils.h (renamed from samsung-ipc/util.h)22
12 files changed, 189 insertions, 58 deletions
diff --git a/Android.mk b/Android.mk
index 4373b6d..8b6f445 100644
--- a/Android.mk
+++ b/Android.mk
@@ -63,7 +63,7 @@ LOCAL_SRC_FILES := \
samsung-ipc/devices/piranha/piranha_ipc.c \
samsung-ipc/devices/i9300/i9300_ipc.c \
samsung-ipc/devices/n7100/n7100_ipc.c \
- samsung-ipc/util.c \
+ samsung-ipc/utils.c \
samsung-ipc/call.c \
samsung-ipc/sms.c \
samsung-ipc/sec.c \
diff --git a/samsung-ipc/Makefile.am b/samsung-ipc/Makefile.am
index 3a93959..4c0ba02 100644
--- a/samsung-ipc/Makefile.am
+++ b/samsung-ipc/Makefile.am
@@ -52,8 +52,8 @@ libsamsung_ipc_la_SOURCES = \
devices/i9300/i9300_ipc.h \
devices/n7100/n7100_ipc.c \
devices/n7100/n7100_ipc.h \
- util.c \
- util.h \
+ utils.c \
+ utils.h \
call.c \
sms.c \
sec.c \
diff --git a/samsung-ipc/devices/aries/aries_ipc.c b/samsung-ipc/devices/aries/aries_ipc.c
index 40e03d3..aede718 100644
--- a/samsung-ipc/devices/aries/aries_ipc.c
+++ b/samsung-ipc/devices/aries/aries_ipc.c
@@ -32,7 +32,7 @@
#include <samsung-ipc.h>
#include <ipc.h>
-#include <util.h>
+#include <utils.h>
#include "onedram.h"
#include "phonet.h"
@@ -64,7 +64,7 @@ int aries_ipc_bootstrap(struct ipc_client *client)
ipc_client_log(client, "Starting aries modem bootstrap");
- modem_image_data = file_data_read(ARIES_MODEM_IMAGE_DEVICE, ARIES_MODEM_IMAGE_SIZE, 0x1000);
+ modem_image_data = file_data_read(ARIES_MODEM_IMAGE_DEVICE, ARIES_MODEM_IMAGE_SIZE, 0x1000, 0);
if (modem_image_data == NULL) {
ipc_client_log(client, "Reading modem image data failed");
goto error;
diff --git a/samsung-ipc/devices/crespo/crespo_ipc.c b/samsung-ipc/devices/crespo/crespo_ipc.c
index b29c4fc..94dc695 100644
--- a/samsung-ipc/devices/crespo/crespo_ipc.c
+++ b/samsung-ipc/devices/crespo/crespo_ipc.c
@@ -29,7 +29,7 @@
#include <samsung-ipc.h>
#include <ipc.h>
-#include <util.h>
+#include <utils.h>
#include "crespo_modem_ctl.h"
@@ -51,7 +51,7 @@ int crespo_ipc_bootstrap(struct ipc_client *client)
ipc_client_log(client, "Starting crespo modem bootstrap");
- modem_image_data = file_data_read(CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000);
+ modem_image_data = file_data_read(CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000, 0);
if (modem_image_data == NULL) {
ipc_client_log(client, "Reading modem image data failed");
goto error;
diff --git a/samsung-ipc/devices/maguro/maguro_ipc.c b/samsung-ipc/devices/maguro/maguro_ipc.c
index 15c72ac..632cd3b 100644
--- a/samsung-ipc/devices/maguro/maguro_ipc.c
+++ b/samsung-ipc/devices/maguro/maguro_ipc.c
@@ -26,7 +26,7 @@
#include <samsung-ipc.h>
#include <ipc.h>
-#include <util.h>
+#include <utils.h>
#include "xmm6260.h"
#include "xmm6260_mipi.h"
@@ -142,7 +142,7 @@ int maguro_ipc_bootstrap(struct ipc_client *client)
}
ipc_client_log(client, "Sent XMM6260 MIPI nv_data");
- mps_data = file_data_read(MAGURO_MPS_DATA_DEVICE, MAGURO_MPS_DATA_SIZE, MAGURO_MPS_DATA_SIZE);
+ mps_data = file_data_read(MAGURO_MPS_DATA_DEVICE, MAGURO_MPS_DATA_SIZE, MAGURO_MPS_DATA_SIZE, 0);
if (mps_data == NULL) {
ipc_client_log(client, "Reading MPS data failed");
goto error;
diff --git a/samsung-ipc/devices/xmm6160/xmm6160.c b/samsung-ipc/devices/xmm6160/xmm6160.c
index 2af6837..f0aa05b 100644
--- a/samsung-ipc/devices/xmm6160/xmm6160.c
+++ b/samsung-ipc/devices/xmm6160/xmm6160.c
@@ -25,7 +25,7 @@
#include <sys/select.h>
#include <samsung-ipc.h>
-#include <util.h>
+#include <utils.h>
#include "xmm6160.h"
@@ -252,7 +252,7 @@ int xmm6160_nv_data_send(struct ipc_client *client, int device_fd,
}
ipc_client_log(client, "Checked nv_data md5");
- nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
if (nv_data == NULL) {
ipc_client_log(client, "Reading nv_data failed");
goto error;
diff --git a/samsung-ipc/devices/xmm6260/xmm6260_hsic.c b/samsung-ipc/devices/xmm6260/xmm6260_hsic.c
index ed3b453..d53ff11 100644
--- a/samsung-ipc/devices/xmm6260/xmm6260_hsic.c
+++ b/samsung-ipc/devices/xmm6260/xmm6260_hsic.c
@@ -28,7 +28,7 @@
#include <sys/select.h>
#include <samsung-ipc.h>
-#include <util.h>
+#include <utils.h>
#include "xmm6260.h"
#include "xmm6260_hsic.h"
@@ -561,7 +561,7 @@ int xmm6260_hsic_nv_data_send(struct ipc_client *client, int device_fd)
}
ipc_client_log(client, "Checked nv_data md5");
- nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
if (nv_data == NULL) {
ipc_client_log(client, "Reading nv_data failed");
goto error;
diff --git a/samsung-ipc/devices/xmm6260/xmm6260_mipi.c b/samsung-ipc/devices/xmm6260/xmm6260_mipi.c
index 4f11361..06a2b90 100644
--- a/samsung-ipc/devices/xmm6260/xmm6260_mipi.c
+++ b/samsung-ipc/devices/xmm6260/xmm6260_mipi.c
@@ -28,7 +28,7 @@
#include <sys/select.h>
#include <samsung-ipc.h>
-#include <util.h>
+#include <utils.h>
#include "xmm6260.h"
#include "xmm6260_mipi.h"
@@ -611,7 +611,7 @@ int xmm6260_mipi_nv_data_send(struct ipc_client *client, int device_fd)
}
ipc_client_log(client, "Checked nv_data md5");
- nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
if (nv_data == NULL) {
ipc_client_log(client, "Reading nv_data failed");
goto error;
diff --git a/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c b/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c
index d3ddfae..e7b89b3 100644
--- a/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c
+++ b/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c
@@ -28,7 +28,7 @@
#include <samsung-ipc.h>
#include <ipc.h>
-#include <util.h>
+#include <utils.h>
#include "modem.h"
#include "modem_prj.h"
diff --git a/samsung-ipc/rfs.c b/samsung-ipc/rfs.c
index a386775..5b56e6c 100644
--- a/samsung-ipc/rfs.c
+++ b/samsung-ipc/rfs.c
@@ -31,7 +31,7 @@
#include <samsung-ipc.h>
#include "ipc.h"
-#include "util.h"
+#include "utils.h"
#define MD5_STRING_SIZE MD5_DIGEST_LENGTH * 2 + 1
@@ -79,7 +79,7 @@ void nv_data_md5_generate(struct ipc_client *client)
ipc_client_log(client, "nv_data_md5_generate: generating MD5 hash");
nv_data_p = file_data_read(ipc_client_nv_data_path(client),
- ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
nv_data_md5_compute(nv_data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash);
free(nv_data_p);
@@ -165,7 +165,7 @@ void nv_data_backup_create(struct ipc_client *client)
/* Read the content of the backup file. */
nv_data_p = file_data_read(ipc_client_nv_data_path(client),
- ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
/* Compute the backup file MD5 hash. */
nv_data_md5_compute(nv_data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash);
@@ -262,7 +262,7 @@ nv_data_backup_create_write:
/* Read the newly-written .nv_data.bak. */
nv_data_bak_p = file_data_read(ipc_client_nv_data_backup_path(client),
- ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
/* Compute the MD5 hash for nv_data.bin. */
nv_data_md5_compute(nv_data_bak_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash);
@@ -362,7 +362,7 @@ void nv_data_backup_restore(struct ipc_client *client)
/* Read the content of the backup file. */
nv_data_bak_p = file_data_read(ipc_client_nv_data_backup_path(client),
- ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
/* Compute the backup file MD5 hash. */
nv_data_md5_compute(nv_data_bak_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash);
@@ -454,7 +454,7 @@ nv_data_backup_restore_write:
/* Read the newly-written nv_data.bin. */
nv_data_p = file_data_read(ipc_client_nv_data_path(client),
- ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
/* Compute the MD5 hash for nv_data.bin. */
nv_data_md5_compute(nv_data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash);
@@ -564,7 +564,7 @@ int nv_data_md5_check(struct ipc_client *client)
memset(nv_data_md5_hash_string, 0, MD5_STRING_SIZE);
nv_data_p = file_data_read(ipc_client_nv_data_path(client),
- ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client));
+ ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0);
data_p = nv_data_p;
nv_data_md5_compute(data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash);
diff --git a/samsung-ipc/util.c b/samsung-ipc/utils.c
index 3dca3b4..364cbbd 100644
--- a/samsung-ipc/util.c
+++ b/samsung-ipc/utils.c
@@ -1,7 +1,7 @@
/*
* This file is part of libsamsung-ipc.
*
- * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ * Copyright (C) 2013-2014 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
@@ -31,33 +31,37 @@
#include <linux/netlink.h>
#include <net/if.h>
-#include "util.h"
+#include "utils.h"
-void *file_data_read(char *path, int size, int chunk)
+void *file_data_read(const char *path, size_t size, size_t chunk_size,
+ size_t offset)
{
void *data = NULL;
int fd = -1;
-
+ size_t count;
+ off_t seek;
unsigned char *p;
- int count;
int rc;
- if (path == NULL || size <= 0 || chunk <= 0)
+ if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size)
return NULL;
fd = open(path, O_RDONLY);
if (fd < 0)
goto error;
- data = malloc(size);
- memset(data, 0, size);
+ seek = lseek(fd, (off_t) offset, SEEK_SET);
+ if (seek < (off_t) offset)
+ goto error;
+
+ data = calloc(1, size);
p = (unsigned char *) data;
count = 0;
while (count < size) {
- rc = read(fd, p, size - count > chunk ? chunk : size - count);
- if (rc < 0)
+ rc = read(fd, p, size - count > chunk_size ? chunk_size : size - count);
+ if (rc <= 0)
goto error;
p += rc;
@@ -67,9 +71,10 @@ void *file_data_read(char *path, int size, int chunk)
goto complete;
error:
- if (data != NULL)
+ if (data != NULL) {
free(data);
- data = NULL;
+ data = NULL;
+ }
complete:
if (fd >= 0)
@@ -78,7 +83,52 @@ complete:
return data;
}
-int network_iface_up(char *iface, int domain, int type)
+int file_data_write(const char *path, const void *data, size_t size,
+ size_t chunk_size, size_t offset)
+{
+ int fd = -1;
+ size_t count;
+ off_t seek;
+ unsigned char *p;
+ int rc;
+
+ if (path == NULL || data == NULL || size == 0 || chunk_size == 0 || chunk_size > size)
+ return -1;
+
+ fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if (fd < 0)
+ goto error;
+
+ seek = lseek(fd, (off_t) offset, SEEK_SET);
+ if (seek < (off_t) offset)
+ goto error;
+
+ p = (unsigned char *) data;
+
+ count = 0;
+ while (count < size) {
+ rc = write(fd, p, size - count > chunk_size ? chunk_size : size - count);
+ if (rc <= 0)
+ goto error;
+
+ p += rc;
+ count += rc;
+ }
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int network_iface_up(const char *iface, int domain, int type)
{
struct ifreq ifr;
int fd = -1;
@@ -117,7 +167,7 @@ complete:
return rc;
}
-int network_iface_down(char *iface, int domain, int type)
+int network_iface_down(const char *iface, int domain, int type)
{
struct ifreq ifr;
int fd = -1;
@@ -156,7 +206,7 @@ complete:
return rc;
}
-int sysfs_value_read(char *path)
+int sysfs_value_read(const char *path)
{
char buffer[100];
int value;
@@ -187,7 +237,7 @@ complete:
return value;
}
-int sysfs_value_write(char *path, int value)
+int sysfs_value_write(const char *path, int value)
{
char buffer[100];
int fd = -1;
@@ -219,41 +269,46 @@ complete:
return rc;
}
-int sysfs_string_read(char *path, char *buffer, int length)
+char *sysfs_string_read(const char *path, size_t length)
{
+ char *string = NULL;
int fd = -1;
int rc;
- if (path == NULL || buffer == NULL || length <= 0)
- return -1;
+ if (path == NULL || length == 0)
+ return NULL;
fd = open(path, O_RDONLY);
if (fd < 0)
goto error;
- rc = read(fd, buffer, length);
+ string = (char *) calloc(1, length);
+
+ rc = read(fd, string, length);
if (rc <= 0)
goto error;
- rc = 0;
goto complete;
error:
- rc = -1;
+ if (string != NULL) {
+ free(string);
+ string = NULL;
+ }
complete:
if (fd >= 0)
close(fd);
- return rc;
+ return string;
}
-int sysfs_string_write(char *path, char *buffer, int length)
+int sysfs_string_write(const char *path, const char *buffer, size_t length)
{
int fd = -1;
int rc;
- if (path == NULL || buffer == NULL || length <= 0)
+ if (path == NULL || buffer == NULL || length == 0)
return -1;
fd = open(path, O_WRONLY);
@@ -261,7 +316,7 @@ int sysfs_string_write(char *path, char *buffer, int length)
goto error;
rc = write(fd, buffer, length);
- if (rc <= 0)
+ if (rc < (int) length)
goto error;
rc = 0;
@@ -277,4 +332,76 @@ complete:
return rc;
}
+char *data2string(const void *data, size_t size)
+{
+ char *string;
+ size_t length;
+ char *p;
+ size_t i;
+
+ if (data == NULL || size == 0)
+ return NULL;
+
+ length = size * 2 + 1;
+ string = (char *) calloc(1, length);
+
+ p = string;
+
+ for (i = 0; i < size; i++) {
+ sprintf(p, "%02x", *((unsigned char *) data + i));
+ p += 2 * sizeof(char);
+ }
+
+ return string;
+}
+
+void *string2data(const char *string, size_t *size_p)
+{
+ void *data;
+ size_t size;
+ size_t length;
+ int shift;
+ unsigned char *p;
+ unsigned int b;
+ size_t i;
+ int rc;
+
+ if (string == NULL)
+ return NULL;
+
+ length = strlen(string);
+ if (length == 0)
+ return NULL;
+
+ if (length % 2 == 0) {
+ size = length / 2;
+ shift = 0;
+ } else {
+ size = (length - (length % 2)) / 2 + 1;
+ shift = 1;
+ }
+
+ data = calloc(1, size);
+
+ p = (unsigned char *) data;
+
+ for (i = 0; i < length; i++) {
+ rc = sscanf(&string[i], "%01x", &b);
+ if (rc < 1)
+ b = 0;
+
+ if ((shift % 2) == 0)
+ *p |= ((b & 0x0f) << 4);
+ else
+ *p++ |= b & 0x0f;
+
+ shift++;
+ }
+
+ if (size_p != NULL)
+ *size_p = size;
+
+ return data;
+}
+
// vim:ts=4:sw=4:expandtab
diff --git a/samsung-ipc/util.h b/samsung-ipc/utils.h
index c2a36a1..4e52ca6 100644
--- a/samsung-ipc/util.h
+++ b/samsung-ipc/utils.h
@@ -1,8 +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>
+ * Copyright (C) 2013-2014 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
@@ -22,13 +21,18 @@
#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);
+void *file_data_read(const char *path, size_t size, size_t chunk_size,
+ size_t offset);
+int file_data_write(const char *path, const void *data, size_t size,
+ size_t chunk_size, size_t offset);
+int network_iface_up(const char *iface, int domain, int type);
+int network_iface_down(const char *iface, int domain, int type);
+int sysfs_value_read(const char *path);
+int sysfs_value_write(const char *path, int value);
+char *sysfs_string_read(const char *path, size_t length);
+int sysfs_string_write(const char *path, const char *buffer, size_t length);
+char *data2string(const void *data, size_t size);
+void *string2data(const char *string, size_t *size_p);
#endif