2 * This file is part of libsamsung-ipc.
4 * Copyright (C) 2013-2014 Paul Kocialkowski <contact@paulk.fr>
6 * libsamsung-ipc is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * libsamsung-ipc is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <asm/types.h>
31 #include <linux/netlink.h>
35 void *file_data_read(const char *path, size_t size, size_t chunk_size,
45 if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size)
48 fd = open(path, O_RDONLY);
52 seek = lseek(fd, (off_t) offset, SEEK_SET);
53 if (seek < (off_t) offset)
56 data = calloc(1, size);
58 p = (unsigned char *) data;
61 while (count < size) {
62 rc = read(fd, p, size - count > chunk_size ? chunk_size : size - count);
85 int file_data_write(const char *path, const void *data, size_t size,
86 size_t chunk_size, unsigned int offset)
94 if (path == NULL || data == NULL || size == 0 || chunk_size == 0 || chunk_size > size)
97 fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
101 seek = lseek(fd, (off_t) offset, SEEK_SET);
102 if (seek < (off_t) offset)
105 p = (unsigned char *) data;
108 while (count < size) {
109 rc = write(fd, p, size - count > chunk_size ? chunk_size : size - count);
130 int network_iface_up(const char *iface, int domain, int type)
139 memset(&ifr, 0, sizeof(ifr));
140 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
142 fd = socket(domain, type, 0);
146 rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
150 ifr.ifr_flags |= IFF_UP;
152 rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
169 int network_iface_down(const char *iface, int domain, int type)
178 memset(&ifr, 0, sizeof(ifr));
179 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
181 fd = socket(domain, type, 0);
185 rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
189 ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP));
191 rc = ioctl(fd, SIOCSIFFLAGS, &ifr);
208 int sysfs_value_read(const char *path)
218 fd = open(path, O_RDONLY);
222 rc = read(fd, &buffer, sizeof(buffer));
226 value = atoi(buffer);
239 int sysfs_value_write(const char *path, int value)
248 fd = open(path, O_WRONLY);
252 snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);
254 rc = write(fd, buffer, strlen(buffer));
255 if (rc < (int) strlen(buffer))
271 char *sysfs_string_read(const char *path, size_t length)
277 if (path == NULL || length == 0)
280 fd = open(path, O_RDONLY);
284 string = (char *) calloc(1, length);
286 rc = read(fd, string, length);
293 if (string != NULL) {
305 int sysfs_string_write(const char *path, const char *buffer, size_t length)
310 if (path == NULL || buffer == NULL || length == 0)
313 fd = open(path, O_WRONLY);
317 rc = write(fd, buffer, length);
318 if (rc < (int) length)
334 size_t data2string_length(const void *data, size_t size)
338 if (data == NULL || size == 0)
341 length = size * 2 + 1;
346 char *data2string(const void *data, size_t size)
353 if (data == NULL || size == 0)
356 length = data2string_length(data, size);
360 string = (char *) calloc(1, length);
364 for (i = 0; i < size; i++) {
365 sprintf(p, "%02x", *((unsigned char *) data + i));
366 p += 2 * sizeof(char);
372 size_t string2data_size(const char *string)
380 length = strlen(string);
387 size = (length - (length % 2)) / 2 + 1;
392 void *string2data(const char *string)
406 length = strlen(string);
415 size = string2data_size(string);
419 data = calloc(1, size);
421 p = (unsigned char *) data;
423 for (i = 0; i < length; i++) {
424 rc = sscanf(&string[i], "%01x", &b);
428 if ((shift % 2) == 0)
429 *p |= ((b & 0x0f) << 4);
439 // vim:ts=4:sw=4:expandtab