aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/ipc_util.c
diff options
context:
space:
mode:
authorPaulK <contact@paulk.fr>2011-11-07 21:54:30 +0100
committerPaulK <contact@paulk.fr>2011-11-07 21:54:30 +0100
commita1a0fe643b21a0f1265216d7a8550aae4f63b3d9 (patch)
treec371489371bfafcaf65c31ed88482a339d3e8492 /samsung-ipc/ipc_util.c
parent53d8243b28c848aee3124889a84065dee58392be (diff)
downloadexternal_libsamsung-ipc-a1a0fe643b21a0f1265216d7a8550aae4f63b3d9.zip
external_libsamsung-ipc-a1a0fe643b21a0f1265216d7a8550aae4f63b3d9.tar.gz
external_libsamsung-ipc-a1a0fe643b21a0f1265216d7a8550aae4f63b3d9.tar.bz2
Switched utility functions to ipc_utils (for client-side logging)
Diffstat (limited to 'samsung-ipc/ipc_util.c')
-rw-r--r--samsung-ipc/ipc_util.c157
1 files changed, 155 insertions, 2 deletions
diff --git a/samsung-ipc/ipc_util.c b/samsung-ipc/ipc_util.c
index 8fdd074..486f3b2 100644
--- a/samsung-ipc/ipc_util.c
+++ b/samsung-ipc/ipc_util.c
@@ -18,8 +18,19 @@
*
*/
-#include <radio.h>
+#include <stdio.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 <asm/types.h>
+#include <mtd/mtd-abi.h>
+
+#include <radio.h>
+#include "ipc_private.h"
#define IPC_STR(f) case f: return strdup(#f);
@@ -167,5 +178,147 @@ const char *ipc_command_type_to_str(int command) {
}
}
-// vim:ts=4:sw=4:expandtab
+void ipc_hex_dump(struct ipc_client *client, void *data, int size)
+{
+ /* dumps size bytes of *data to stdout. Looks like:
+ * [0000] 75 6E 6B 6E 6F 77 6E 20
+ * 30 FF 00 00 00 00 39 00 unknown 0.....9.
+ * (in a single line of course)
+ */
+
+ unsigned char *p = data;
+ unsigned char c;
+ int n;
+ char bytestr[4] = {0};
+ char addrstr[10] = {0};
+ char hexstr[ 16*3 + 5] = {0};
+ char charstr[16*1 + 5] = {0};
+ for(n=1;n<=size;n++) {
+ if (n%16 == 1) {
+ /* store address for this line */
+ snprintf(addrstr, sizeof(addrstr), "%.4x",
+ ((unsigned int)p-(unsigned int)data) );
+ }
+
+ c = *p;
+ if (isalnum(c) == 0) {
+ c = '.';
+ }
+
+ /* store hex str (for left side) */
+ snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
+ strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
+
+ /* store char str (for right side) */
+ snprintf(bytestr, sizeof(bytestr), "%c", c);
+ strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
+
+ if(n%16 == 0) {
+ /* line completed */
+ ipc_client_log(client, "[%4.4s] %-50.50s %s", addrstr, hexstr, charstr);
+ hexstr[0] = 0;
+ charstr[0] = 0;
+ } else if(n%8 == 0) {
+ /* half line: add whitespaces */
+ strncat(hexstr, " ", sizeof(hexstr)-strlen(hexstr)-1);
+ strncat(charstr, " ", sizeof(charstr)-strlen(charstr)-1);
+ }
+ p++; /* next byte */
+ }
+
+ if (strlen(hexstr) > 0) {
+ /* print rest of buffer if not empty */
+ ipc_client_log(client, "[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
+ }
+}
+
+void *ipc_mtd_read(struct ipc_client *client, char *mtd_name, int size, int block_size)
+{
+ void *mtd_p=NULL;
+ uint8_t *data_p=NULL;
+
+ loff_t offs;
+ int fd;
+ int i;
+
+ if(mtd_name == NULL || size <= 0 || block_size <= 0)
+ goto error;
+
+ ipc_client_log(client, "mtd_read: reading 0x%x bytes from %s with 0x%x bytes block size\n", size, mtd_name, block_size);
+
+ fd=open(mtd_name, O_RDONLY);
+ if(fd < 0)
+ goto error;
+
+ mtd_p=malloc(size);
+ if(mtd_p == NULL)
+ goto error;
+
+ memset(mtd_p, 0, size);
+
+ data_p=(uint8_t *) mtd_p;
+
+ for(i=0 ; i < size / block_size ; i++)
+ {
+ offs = i * block_size;
+ if(ioctl(fd, MEMGETBADBLOCK, &offs) == 1)
+ {
+ ipc_client_log(client, "mtd_read: warning: bad block at offset %lld\n", (long long int) offs);
+ data_p+=block_size;
+ continue;
+ }
+ read(fd, data_p, block_size);
+ data_p+=block_size;
+ }
+
+ close(fd);
+
+ return mtd_p;
+
+error:
+ ipc_client_log(client, "%s: something went wrong\n", __func__);
+ return NULL;
+}
+
+void *ipc_file_read(struct ipc_client *client, char *file_name, int size, int block_size)
+{
+ void *file_p=NULL;
+ uint8_t *data_p=NULL;
+
+ int fd;
+ int i;
+
+ if(file_name == NULL || size <= 0 || block_size <= 0)
+ goto error;
+
+ ipc_client_log(client, "file_read: reading 0x%x bytes from %s with 0x%x bytes block size\n", size, file_name, block_size);
+
+ fd=open(file_name, O_RDONLY);
+ if(fd < 0)
+ goto error;
+
+ file_p=malloc(size);
+ if(file_p == NULL)
+ goto error;
+
+ memset(file_p, 0, size);
+
+ data_p=(uint8_t *) file_p;
+
+ for(i=0 ; i < size / block_size ; i++)
+ {
+ read(fd, data_p, block_size);
+ data_p+=block_size;
+ }
+
+ close(fd);
+
+ return file_p;
+
+error:
+ ipc_client_log(client, "%s: something went wrong\n", __func__);
+ return NULL;
+}
+
+// vim:ts=4:sw=4:expandtab