aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--samsung-ipc/utils.c630
1 files changed, 322 insertions, 308 deletions
diff --git a/samsung-ipc/utils.c b/samsung-ipc/utils.c
index 1bba874..da35e59 100644
--- a/samsung-ipc/utils.c
+++ b/samsung-ipc/utils.c
@@ -37,446 +37,460 @@
void *file_data_read(struct ipc_client *client, const char *path, size_t size,
size_t chunk_size, unsigned int offset)
{
- void *data = NULL;
- int fd = -1;
- size_t count;
- off_t seek;
- unsigned char *p;
- int rc;
-
- if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size) {
- if (path == NULL) {
- ipc_client_log(client, "%s: Failed: path is NULL", __FUNCTION__);
- }
- if (size == 0) {
- ipc_client_log(client, "%s: Failed: size is 0", __FUNCTION__);
- }
- if (chunk_size == 0) {
- ipc_client_log(client, "%s: Failed: chunk_size is 0", __FUNCTION__);
- }
- if (chunk_size > size) {
- ipc_client_log(client, "%s: Failed: chunk_size > size ", __FUNCTION__);
- }
-
- return NULL;
- }
-
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- ipc_client_log(client, "%s: Error: fd: %d ", __FUNCTION__, fd);
- goto error;
- }
-
- seek = lseek(fd, (off_t) offset, SEEK_SET);
- if (seek < (off_t) offset) {
- ipc_client_log(client, "%s: Error: seek < (off_t) offset", __FUNCTION__);
- goto error;
- }
-
- data = calloc(1, size);
-
- p = (unsigned char *) data;
-
- count = 0;
- while (count < size) {
- rc = read(fd, p, size - count > chunk_size ? chunk_size : size - count);
- if (rc <= 0) {
- ipc_client_log(client, "%s: Error: rc < 0", __FUNCTION__);
+ void *data = NULL;
+ int fd = -1;
+ size_t count;
+ off_t seek;
+ unsigned char *p;
+ int rc;
+
+ if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size) {
+ if (path == NULL) {
+ ipc_client_log(client, "%s: Failed: path is NULL",
+ __func__);
+ }
+ if (size == 0) {
+ ipc_client_log(client, "%s: Failed: size is 0",
+ __func__);
+ }
+ if (chunk_size == 0) {
+ ipc_client_log(client, "%s: Failed: chunk_size is 0",
+ __func__);
+ }
+ if (chunk_size > size) {
+ ipc_client_log(client, "%s: Failed: chunk_size > size ",
+ __func__);
+ }
+
+ return NULL;
+ }
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ ipc_client_log(client, "%s: Error: fd: %d ", __func__, fd);
+ goto error;
+ }
+
+ seek = lseek(fd, (off_t) offset, SEEK_SET);
+ if (seek < (off_t) offset) {
+ ipc_client_log(client, "%s: Error: seek < (off_t) offset",
+ __func__);
goto error;
}
- p += rc;
- count += rc;
- }
+ data = calloc(1, size);
+
+ p = (unsigned char *) data;
- goto complete;
+ count = 0;
+ while (count < size) {
+ rc = read(fd, p,
+ size - count > chunk_size ?
+ chunk_size : size - count);
+ if (rc <= 0) {
+ ipc_client_log(client, "%s: Error: rc < 0", __func__);
+ goto error;
+ }
+
+ p += rc;
+ count += rc;
+ }
+
+ goto complete;
error:
- if (data != NULL) {
- free(data);
- data = NULL;
- }
+ if (data != NULL) {
+ free(data);
+ data = NULL;
+ }
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return data;
+ return data;
}
int file_data_write(struct ipc_client *client, const char *path,
- const void *data, size_t size, size_t chunk_size,
- unsigned int offset)
+ const void *data, size_t size, size_t chunk_size,
+ unsigned int 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) {
- if (path == NULL) {
- ipc_client_log(client, "%s failed: path is NULL", __FUNCTION__);
- }
- if (size == 0) {
- ipc_client_log(client, "%s failed: size is 0", __FUNCTION__);
- }
- if (chunk_size == 0) {
- ipc_client_log(client, "%s failed: chunk_size is 0", __FUNCTION__);
- }
- if (chunk_size > size) {
- ipc_client_log(client, "%s failed: chunk_size > size",
- __FUNCTION__);
- }
- return -1;
- }
-
- fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (fd < 0) {
- ipc_client_log(client, "%s: open failed with error %d", __FUNCTION__, fd);
- goto error;
- }
-
- seek = lseek(fd, (off_t) offset, SEEK_SET);
- if (seek < (off_t) offset) {
- ipc_client_log(client, "%s failed: seek < (off_t) offset", __FUNCTION__);
- 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) {
- ipc_client_log(client, "%s: write failed with error %d", __FUNCTION__, rc);
- goto error;
- }
-
- p += rc;
- count += rc;
- }
-
- rc = 0;
- goto complete;
+ 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) {
+ if (path == NULL) {
+ ipc_client_log(client, "%s failed: path is NULL",
+ __func__);
+ }
+ if (size == 0) {
+ ipc_client_log(client, "%s failed: size is 0",
+ __func__);
+ }
+ if (chunk_size == 0) {
+ ipc_client_log(client, "%s failed: chunk_size is 0",
+ __func__);
+ }
+ if (chunk_size > size) {
+ ipc_client_log(client, "%s failed: chunk_size > size",
+ __func__);
+ }
+ return -1;
+ }
+
+ fd = open(path, O_WRONLY | O_CREAT, 0644);
+ if (fd < 0) {
+ ipc_client_log(client, "%s: open failed with error %d",
+ __func__, fd);
+ goto error;
+ }
+
+ seek = lseek(fd, (off_t) offset, SEEK_SET);
+ if (seek < (off_t) offset) {
+ ipc_client_log(client, "%s failed: seek < (off_t) offset",
+ __func__);
+ 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) {
+ ipc_client_log(client, "%s: write failed with error %d",
+ __func__, rc);
+ goto error;
+ }
+
+ p += rc;
+ count += rc;
+ }
+
+ rc = 0;
+ goto complete;
error:
- rc = -1;
+ rc = -1;
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return rc;
+ return rc;
}
int network_iface_up(const char *iface, int domain, int type)
{
- struct ifreq ifr;
- int fd = -1;
- int rc;
+ struct ifreq ifr;
+ int fd = -1;
+ int rc;
- if (iface == NULL)
- return -1;
+ if (iface == NULL)
+ return -1;
- memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, iface, IFNAMSIZ);
- fd = socket(domain, type, 0);
- if (fd < 0)
- goto error;
+ fd = socket(domain, type, 0);
+ if (fd < 0)
+ goto error;
- rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
- if (rc < 0)
- goto error;
+ rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
- ifr.ifr_flags |= IFF_UP;
+ ifr.ifr_flags |= IFF_UP;
- rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
- if (rc < 0)
- goto error;
+ rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
- rc = 0;
- goto complete;
+ rc = 0;
+ goto complete;
error:
- rc = -1;
+ rc = -1;
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return rc;
+ return rc;
}
int network_iface_down(const char *iface, int domain, int type)
{
- struct ifreq ifr;
- int fd = -1;
- int rc;
+ struct ifreq ifr;
+ int fd = -1;
+ int rc;
- if (iface == NULL)
- return -1;
+ if (iface == NULL)
+ return -1;
- memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, iface, IFNAMSIZ);
- fd = socket(domain, type, 0);
- if (fd < 0)
- goto error;
+ fd = socket(domain, type, 0);
+ if (fd < 0)
+ goto error;
- rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
- if (rc < 0)
- goto error;
+ rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
- ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP));
+ ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP));
- rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
- if (rc < 0)
- goto error;
+ rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
+ if (rc < 0)
+ goto error;
- rc = 0;
- goto complete;
+ rc = 0;
+ goto complete;
error:
- rc = -1;
+ rc = -1;
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return rc;
+ return rc;
}
int sysfs_value_read(const char *path)
{
- char buffer[100];
- int value;
- int fd = -1;
- int rc;
+ char buffer[100];
+ int value;
+ int fd = -1;
+ int rc;
- if (path == NULL)
- return -1;
+ if (path == NULL)
+ return -1;
- fd = open(path, O_RDONLY);
- if (fd < 0)
- goto error;
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
- rc = read(fd, &buffer, sizeof(buffer));
- if (rc <= 0)
- goto error;
+ rc = read(fd, &buffer, sizeof(buffer));
+ if (rc <= 0)
+ goto error;
- value = atoi(buffer);
- goto complete;
+ value = atoi(buffer);
+ goto complete;
error:
- value = -1;
+ value = -1;
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return value;
+ return value;
}
int sysfs_value_write(const char *path, int value)
{
- char buffer[100];
- int fd = -1;
- int rc;
+ char buffer[100];
+ int fd = -1;
+ int rc;
- if (path == NULL)
- return -1;
+ if (path == NULL)
+ return -1;
- fd = open(path, O_WRONLY);
- if (fd < 0)
- goto error;
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
- snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);
+ snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);
- rc = write(fd, buffer, strlen(buffer));
- if (rc < (int) strlen(buffer))
- goto error;
+ rc = write(fd, buffer, strlen(buffer));
+ if (rc < (int) strlen(buffer))
+ goto error;
- rc = 0;
- goto complete;
+ rc = 0;
+ goto complete;
error:
- rc = -1;
+ rc = -1;
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return rc;
+ return rc;
}
char *sysfs_string_read(const char *path, size_t length)
{
- char *string = NULL;
- int fd = -1;
- int rc;
+ char *string = NULL;
+ int fd = -1;
+ int rc;
- if (path == NULL || length == 0)
- return NULL;
+ if (path == NULL || length == 0)
+ return NULL;
- fd = open(path, O_RDONLY);
- if (fd < 0)
- goto error;
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
- string = (char *) calloc(1, length);
+ string = (char *) calloc(1, length);
- rc = read(fd, string, length);
- if (rc <= 0)
- goto error;
+ rc = read(fd, string, length);
+ if (rc <= 0)
+ goto error;
- goto complete;
+ goto complete;
error:
- if (string != NULL) {
- free(string);
- string = NULL;
- }
+ if (string != NULL) {
+ free(string);
+ string = NULL;
+ }
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return string;
+ return string;
}
int sysfs_string_write(const char *path, const char *buffer, size_t length)
{
- int fd = -1;
- int rc;
+ int fd = -1;
+ int rc;
- if (path == NULL || buffer == NULL || length == 0)
- return -1;
+ if (path == NULL || buffer == NULL || length == 0)
+ return -1;
- fd = open(path, O_WRONLY);
- if (fd < 0)
- goto error;
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
- rc = write(fd, buffer, length);
- if (rc < (int) length)
- goto error;
+ rc = write(fd, buffer, length);
+ if (rc < (int) length)
+ goto error;
- rc = 0;
- goto complete;
+ rc = 0;
+ goto complete;
error:
- rc = -1;
+ rc = -1;
complete:
- if (fd >= 0)
- close(fd);
+ if (fd >= 0)
+ close(fd);
- return rc;
+ return rc;
}
size_t data2string_length(const void *data, size_t size)
{
- size_t length;
+ size_t length;
- if (data == NULL || size == 0)
- return 0;
+ if (data == NULL || size == 0)
+ return 0;
- length = size * 2 + 1;
+ length = size * 2 + 1;
- return length;
+ return length;
}
char *data2string(const void *data, size_t size)
{
- char *string;
- size_t length;
- char *p;
- size_t i;
+ char *string;
+ size_t length;
+ char *p;
+ size_t i;
- if (data == NULL || size == 0)
- return NULL;
+ if (data == NULL || size == 0)
+ return NULL;
- length = data2string_length(data, size);
- if (length == 0)
- return NULL;
+ length = data2string_length(data, size);
+ if (length == 0)
+ return NULL;
- string = (char *) calloc(1, length);
+ string = (char *) calloc(1, length);
- p = string;
+ p = string;
- for (i = 0; i < size; i++) {
- sprintf(p, "%02x", *((unsigned char *) data + i));
- p += 2 * sizeof(char);
- }
+ for (i = 0; i < size; i++) {
+ sprintf(p, "%02x", *((unsigned char *) data + i));
+ p += 2 * sizeof(char);
+ }
- return string;
+ return string;
}
size_t string2data_size(const char *string)
{
- size_t length;
- size_t size;
+ size_t length;
+ size_t size;
- if (string == NULL)
- return 0;
+ if (string == NULL)
+ return 0;
- length = strlen(string);
- if (length == 0)
- return 0;
+ length = strlen(string);
+ if (length == 0)
+ return 0;
- if (length % 2 == 0)
- size = length / 2;
- else
- size = (length - (length % 2)) / 2 + 1;
+ if (length % 2 == 0)
+ size = length / 2;
+ else
+ size = (length - (length % 2)) / 2 + 1;
- return size;
+ return size;
}
void *string2data(const char *string)
{
- 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)
- shift = 0;
- else
- shift = 1;
-
- size = string2data_size(string);
- if (size == 0)
- return NULL;
-
- 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++;
- }
+ 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)
+ shift = 0;
+ else
+ shift = 1;
+
+ size = string2data_size(string);
+ if (size == 0)
+ return NULL;
+
+ 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++;
+ }
- return data;
+ return data;
}
-
-// vim:ts=4:sw=4:expandtab