3 * tel-plugin-mfld-blackbay
\r
5 * Copyright (C) 2013 Intel Corporation. All rights reserved.
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
25 #include <sys/time.h>
\r
26 #include <sys/mman.h>
\r
33 #include <util_mfld_blackbay.h>
\r
35 #define MAX_READ_ATTEMPTS 4
\r
36 #define MAX_WRITE_ATTEMPTS 10
\r
38 /* Read data from a GIOChannel */
\r
39 size_t channel_read(GIOChannel *channel, void* buf, size_t nbytes)
\r
43 size_t toread = nbytes;
\r
44 size_t bytes_read = 0;
\r
45 unsigned int read_count = 0;
\r
50 status = g_io_channel_read_chars(channel, buf, toread, &rbytes, NULL);
\r
54 dbg("g_io_channel_read_chars.. status[%d] rbytes =%d read_count = %d", status, rbytes, read_count);
\r
56 bytes_read += rbytes;
\r
65 } while (status == G_IO_STATUS_NORMAL && rbytes > 0 && read_count < MAX_READ_ATTEMPTS);
\r
70 static void __selectsleep(int sec,int msec)
\r
77 select(0,NULL,NULL,NULL,&tv);
\r
82 /* Write data to a GIOChannel */
\r
83 size_t channel_write(GIOChannel *channel, void* buf, size_t nbytes)
\r
87 size_t bytes_written = 0;
\r
88 unsigned int retry = 0;
\r
91 status = g_io_channel_write_chars(channel, (const gchar*)buf, nbytes - bytes_written, &tbytes, NULL);
\r
93 if (status == G_IO_STATUS_AGAIN) {
\r
94 err("write failed. Retry.. status[%d] Nb attempt[%d]", status, retry);
\r
96 __selectsleep(0,50);
\r
98 if (retry == MAX_WRITE_ATTEMPTS)
\r
106 if (status != G_IO_STATUS_NORMAL) {
\r
107 if (bytes_written != nbytes)
\r
108 err("write failed. status[%d] with errno[%d]", status, errno);
\r
110 return bytes_written;
\r
113 bytes_written += tbytes;
\r
116 } while (bytes_written < nbytes);
\r
118 return bytes_written;
\r
121 void util_hex_dump(char *pad, int size, const void *data)
\r
123 char buf[255] = {0, };
\r
124 char hex[4] = {0, };
\r
129 dbg("<%s> no data", pad);
\r
133 p = (unsigned char *) data;
\r
135 snprintf(buf, 255, "%s%04X: ", pad, 0);
\r
136 for (i = 0; i < size; i++) {
\r
137 snprintf(hex, 4, "%02X ", p[i]);
\r
140 if ((i + 1) % 8 == 0) {
\r
141 if ((i + 1) % 16 == 0) {
\r
143 memset(buf, 0, 255);
\r
144 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
\r