./helper/loader.c \
./helper/got_patching.c \
./helper/get_got.c \
+ ./helper/file_buffer.c \
./helper/screenshot_iface.c
PROBE_SRCS = \
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <malloc.h>
+#include "file_buffer.h"
+#include "daprobe.h"
+
+
+static size_t fsize(int fd)
+{
+ struct stat st;
+ if (fstat(fd, &st) != 0)
+ return 0;
+ return st.st_size;
+}
+
+static int do_read_file(int fd, void *data, size_t len)
+{
+ while (len) {
+ ssize_t n = read(fd, data, len);
+ if (n == -1) {
+ if (errno == EINTR)
+ continue;
+
+ return -errno;
+ } else if (n == 0) {
+ return -EPERM;
+ }
+
+ len -= n;
+ data += n;
+ }
+
+ return 0;
+}
+
+struct file_buffer *file_buffer_create(const char *path)
+{
+ int fd, ret;
+ struct file_buffer *fb;
+ const size_t MAX_FILE_SIZE = 32 * 1024 * 1024;
+
+ fb = malloc(sizeof(*fb));
+ if (!fb) {
+ PRINTERR("Out of memory");
+ return NULL;
+ }
+
+ fd = open(path, O_RDONLY);
+ if (fd == -1) {
+ PRINTERR("Cannot open '%s', errno=%d", path, fd);
+ goto free_fb;
+ }
+
+ fb->size = fsize(fd);
+ if (!fb->size) {
+ PRINTERR("Cannot calculate file size, path='%s'", path);
+ goto close_fd;
+ }
+
+ if (fb->size > MAX_FILE_SIZE) {
+ PRINTERR("File size is very long, size=%zu", fb->size);
+ goto close_fd;
+ }
+
+ fb->data = malloc(fb->size);
+ if (!fb->data) {
+ PRINTERR("Out of memory");
+ goto close_fd;
+ }
+
+ ret = do_read_file(fd, fb->data, fb->size);
+ if (ret) {
+ PRINTERR("Cannot read file, ret=%d", ret);
+ goto free_data;
+ }
+
+ close(fd);
+ return fb;
+
+free_data:
+ free(fb->data);
+close_fd:
+ close(fd);
+free_fb:
+ free(fb);
+ return NULL;
+}
+
+void file_buffer_destroy(struct file_buffer *fb)
+{
+ free(fb->data);
+ free(fb);
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#ifndef FILE_BUFFER_H
+#define FILE_BUFFER_H
+
+
+#include <unistd.h>
+
+
+struct file_buffer {
+ size_t size;
+ void *data;
+};
+
+
+struct file_buffer *file_buffer_create(const char *path);
+void file_buffer_destroy(struct file_buffer *fb);
+
+
+#endif // FILE_BUFFER_H
#include "dahelper.h"
#include "binproto.h"
+#include "file_buffer.h"
#define MAX_HEIGHT 720
#define CAPTURE_TIMEOUT 2.0
}
-struct file_data {
- size_t len;
- void *data;
-};
-
-static size_t fsize(int fd)
-{
- struct stat st;
- if (fstat(fd, &st) != 0)
- return 0;
- return st.st_size;
-}
-
-static int do_read_file(int fd, void *data, size_t len)
-{
- while (len) {
- ssize_t n = read(fd, data, len);
- if (n == -1) {
- if (errno == EINTR)
- continue;
-
- return -errno;
- } else if (n == 0) {
- return -EPERM;
- }
-
- len -= n;
- data += n;
- }
-
- return 0;
-}
-
-static struct file_data *file_data_create(const char *path)
-{
- int fd, ret;
- struct file_data *fdata;
- const size_t MAX_FILE_SIZE = 32 * 1024 * 1024;
-
- fdata = malloc(sizeof(*fdata));
- if (!fdata) {
- PRINTERR("Out of memory");
- return NULL;
- }
-
- fd = open(path, O_RDONLY);
- if (fd == -1) {
- PRINTERR("Cannot open '%s', errno=%d", path, fd);
- goto free_fdata;
- }
-
- fdata->len = fsize(fd);
- if (!fdata->len) {
- PRINTERR("Cannot calculate file size, path='%s'", path);
- goto close_fd;
- }
-
- if (fdata->len > MAX_FILE_SIZE) {
- PRINTERR("file size is very long, len=%zu", fdata->len);
- goto close_fd;
- }
-
- fdata->data = malloc(fdata->len);
- if (!fdata->data) {
- PRINTERR("Out of memory");
- goto close_fd;
- }
-
- ret = do_read_file(fd, fdata->data, fdata->len);
- if (ret) {
- PRINTERR("Cannot read file, ret=%d", ret);
- goto free_data;
- }
-
- close(fd);
- return fdata;
-
-free_data:
- free(fdata->data);
-close_fd:
- close(fd);
-free_fdata:
- free(fdata);
- return NULL;
-}
-
-static void file_data_free(struct file_data *data)
-{
- free(data->data);
- free(data);
-}
-
-
static size_t screenshot_pack_info(char *buf)
{
/* pack probe */
static int screenshot_send_to_socket(const char *path)
{
- struct file_data *fdata;
+ struct file_buffer *fb;
char sh_info[256];
void *buf, *p;
size_t sh_info_len, buf_size;
- fdata = file_data_create(path);
- if (!fdata)
+ fb = file_buffer_create(path);
+ if (!fb)
return -1;
/* pack common colums */
* | 4 | ... | 4 | ... |
*
*/
- buf_size = 4 + sh_info_len + 4 + fdata->len;
+ buf_size = 4 + sh_info_len + 4 + fb->size;
buf = malloc(buf_size);
if (!buf) {
PRINTERR("Out of memory");
- file_data_free(fdata);
+ file_buffer_destroy(fb);
return -1;
}
p = pack_int32(p, current_angle_get());
/* pack img.png */
- p = pack_bin(p, fdata->data, fdata->len);
+ p = pack_bin(p, fb->data, fb->size);
/* send screenshot to manager */
msg_send(APP_MSG_IMAGE, buf, buf_size);
free(buf);
- file_data_free(fdata);
+ file_buffer_destroy(fb);
return 0;
}