summaryrefslogtreecommitdiff
path: root/src/util_mfld_blackbay.c
blob: b05273f008cf90254cf255dffb8f6d8eeaa89433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
*
*  tel-plugin-mfld-blackbay
*
* Copyright (C) 2013  Intel Corporation. All rights reserved.
*
* 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.
*
*/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>

#include <glib.h>

#include <log.h>
#include <util_mfld_blackbay.h>

#define MAX_READ_ATTEMPTS	4
#define MAX_WRITE_ATTEMPTS	10

/* Read data from a GIOChannel */
size_t channel_read(GIOChannel *channel, void* buf, size_t nbytes)
{
	GIOStatus status;
	size_t rbytes;
	size_t toread = nbytes;
	size_t bytes_read = 0;
	unsigned int read_count = 0;

	do {
		rbytes = 0;

		status = g_io_channel_read_chars(channel, buf, toread, &rbytes, NULL);

		read_count++;

		err("g_io_channel_read_chars.. status[%d] rbytes =%d read_count = %d", status, rbytes, read_count);

		bytes_read += rbytes;
		toread -= rbytes;

		if (rbytes > 0)
			buf+=rbytes;

		if (toread == 0)
			break;

	} while (status == G_IO_STATUS_NORMAL && rbytes > 0 && read_count < MAX_READ_ATTEMPTS);

	return bytes_read;
}

static void __selectsleep(int sec,int msec)
{
	struct timeval tv;

	tv.tv_sec=sec;
	tv.tv_usec=msec;

	select(0,NULL,NULL,NULL,&tv);

	return;
}

/* Write data to a GIOChannel */
size_t channel_write(GIOChannel *channel, void* buf, size_t nbytes)
{
	GIOStatus status;
	size_t tbytes = 0;
	size_t bytes_written = 0;
	unsigned int retry = 0;

	do {
		status = g_io_channel_write_chars(channel, (const gchar*)buf, nbytes - bytes_written, &tbytes, NULL);

		if (status == G_IO_STATUS_AGAIN) {
			err("write failed. Retry.. status[%d] Nb attempt[%d]", status, retry);

			__selectsleep(0,50);

			if (retry == MAX_WRITE_ATTEMPTS)
				return 0;

			retry = retry + 1;

			continue;
		}

		if (status != G_IO_STATUS_NORMAL) {
			if (bytes_written != nbytes)
				err("write failed. status[%d] with errno[%d]", status, errno);

			return bytes_written;
		}

		bytes_written += tbytes;
		buf += tbytes;

	} while (bytes_written < nbytes);

	return bytes_written;
}

void util_hex_dump(char *pad, int size, const void *data)
{
	char buf[255] = {0, };
	char hex[4] = {0, };
	int i;
	unsigned char *p;

	if (size <= 0) {
		msg("%sno data", pad);
		return;
	}

	p = (unsigned char *) data;

	snprintf(buf, 255, "%s%04X: ", pad, 0);
	for (i = 0; i < size; i++) {
		snprintf(hex, 4, "%02X ", p[i]);
		strcat(buf, hex);

		if ((i + 1) % 8 == 0) {
			if ((i + 1) % 16 == 0) {
				msg("%s", buf);
				memset(buf, 0, 255);
				snprintf(buf, 255, "%s%04X: ", pad, i + 1);
			} else {
				strcat(buf, "  ");
			}
		}
	}

	msg("%s", buf);
}