GDB in a programming in C - gdb

I have a problem with a program in C. I need the absolute memory address of the local variables to the function main. I am debugging with the gdb.
How to know if there is an overflow in the variables.
Thank you
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
typedef int16_t int16;
#include "mulaw.h"
void decodeFile(FILE * fIn, FILE * fOut, uint32_t samples) {
uint32_t i;
uint8_t * inSamples = malloc(samples * sizeof(uint8_t));
int16_t * outSamples = malloc(samples * sizeof(int16_t));
fread(inSamples, sizeof(uint8_t), samples, fIn);
for (i = 0; i < samples; i++) {
outSamples[i] = 4 * muLaw[inSamples[i]];
}
fwrite(outSamples, sizeof(int16_t), samples, fOut);
free(inSamples);
free(outSamples);
}
#define MAX_FILE_SIZE 256
int main(int argc, char **argv)
{
char inputFile[MAX_FILE_SIZE];
char outputFile[MAX_FILE_SIZE];
FILE * fIn = NULL, * fOut = NULL;
struct header_t {
char ChunkID[4];
int32_t ChunkSize;
char Format[4];
} header;
char SubchunkID[4];
uint32_t SubchunkSize;
struct subheader_t {
int16_t AudioFormat;
int16_t NumChannels;
int32_t SampleRate;
int32_t ByteRate;
int16_t BlockAlign;
int16_t BitsPerSample;
int16_t ExtraParamSize;
int16_t Padding;
} subheader;
/* Usage */
if (argc != 3) {
puts("Usage is: mulaw INFILE OUTFILE\n");
exit(EXIT_FAILURE);
}
/* Careful here!!!!! */
strncpy(inputFile, argv[1], MAX_FILE_SIZE);
strncpy(outputFile, argv[2], MAX_FILE_SIZE);
/* Open input file */
fIn = fopen (inputFile, "rb");
/* Read main header */
fread(&header, sizeof(struct header_t), 1, fIn);
if (memcmp(header.ChunkID, "RIFF", 4) != 0
|| memcmp(header.Format, "WAVE", 4) != 0) {
fprintf(stderr, "Unknown input format\n");
exit(EXIT_FAILURE);
}
/* Read sub header */
while (fread(SubchunkID, sizeof(SubchunkID), 1, fIn)) {
fread(&SubchunkSize, sizeof(SubchunkSize), 1, fIn);
printf("Reading chunk of type %c%c%c%c (%d bytes)\n",
isprint(SubchunkID[0]) ? SubchunkID[0] : '?',
isprint(SubchunkID[1]) ? SubchunkID[1] : '?',
isprint(SubchunkID[2]) ? SubchunkID[2] : '?',
isprint(SubchunkID[3]) ? SubchunkID[3] : '?',
(int) SubchunkSize);
if (memcmp(SubchunkID, "fmt ", 4) == 0) {
/* read a fmt_ header */
fread(&subheader, SubchunkSize, 1, fIn);
/* we are going to adjust this header now to change the audio format */
if (subheader.AudioFormat != 7) {
fprintf(stderr, "Only mu-law audio input is supported\n");
exit(EXIT_FAILURE);
}
/* adjust audio format and bit depth */
subheader.AudioFormat = 1;
subheader.BitsPerSample = 16;
/* fix derivative fields */
subheader.ByteRate = subheader.SampleRate * subheader.NumChannels * subheader.BitsPerSample / 8;
subheader.BlockAlign = subheader.NumChannels * subheader.BitsPerSample / 8;
/* we don't write ExtraParamSize, because for AudioFormat == 1 it is not needed */
SubchunkSize -= 2;
/* Open file and write the header for our updated fmt_ chunk */
fOut = fopen (outputFile, "wb");
fwrite(&header, sizeof(struct header_t), 1, fOut); /* Main header */
fwrite(SubchunkID, sizeof(SubchunkID), 1, fOut); /* Subheader */
fwrite(&SubchunkSize, sizeof(SubchunkSize), 1, fOut);
fwrite(&subheader, SubchunkSize, 1, fOut);
} else if (memcmp(SubchunkID, "data", 4) == 0) {
/* here is our mu-law data */
/* write the header for our new chunk (it is twice as large) */
int32_t tSubchunkSize = SubchunkSize * 2;
fwrite(SubchunkID, sizeof(SubchunkID), 1, fOut);
fwrite(&tSubchunkSize, sizeof(SubchunkSize), 1, fOut);
/* process the data */
(fIn, fOut, SubchunkSize);
} else {
/* unknown chunk, skipping */
fseek(fIn, SubchunkSize, SEEK_CUR);
}
}
/* Cleanup and exit */
fclose(fIn);
fclose(fOut);
exit(EXIT_SUCCESS);
}
}

Just run gdb yourexe
Then in gdb:
break main
run
p &inputFile
That will give you the address of inputFile: ex $2 = (char (*)[256]) 0x62fd60
To get the size of a variable you can use p sizeof(variable) or p sizeof(type_of_the_variable>)
That said, not sure if it will help you to track down your problem.

Related

Debug Error - abort() has been called (gcz2tga)

Complete C++/C newbie here. I recompiled gcz2tga (I did not make this code, just found it on the webs) into a debug .exe file using Visual Studio 2019. Everything works well until it gets to "split_images" and then the program spits out this error:
Debug Error!
Program: C:\Users\Harrison\source\repos\gcz2tga\Debug\gcz2tga.exe
abort() has been called
(Press Retry to debug the application)
When I hit Retry, the program closes. The code is set up like this:
/* gcz2tga.c: Slice up a directory full of GCZ (texture) files into TGA files.
*
* Credit goes to afwefwe for reverse-engineering the texture format
* and LZSS compression */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* Assuming x86, usual endian crap is not accounted for */
typedef unsigned char u8_t;
typedef unsigned short u16_t;
typedef unsigned int u32_t;
struct image
{
unsigned int width, height;
u8_t *planes;
};
struct clip
{
short x, y, w, h;
};
static u16_t swab16(u16_t in)
{
/* GC headers are big-endian */
return ((in & 0xFF) << 8) | (in >> 8);
}
static void unpack_gc(struct image *out, u8_t *in, size_t out_sz)
{
unsigned int npixels;
unsigned int i;
unsigned int j;
u16_t *pixels;
u16_t *pheight;
u16_t *pwidth;
u16_t *pmagic;
u16_t pixel;
pmagic = (u16_t *) in;
pheight = (u16_t *) (in + 14);
pwidth = (u16_t *) (in + 12);
if (*pmagic != 0x4347) {
fprintf(stderr, "(FAIL: Invalid header)\n");
exit(EXIT_FAILURE);
}
/* Set up output image struct */
in += 24;
out->width = swab16(*pwidth);
out->height = swab16(*pheight);
out->planes = malloc(out->width * out->height * 4);
/* Clamp W/H (don't know why this is necessary but it is) */
out->width = out->width > 1024 ? 1024 : out->width;
out->height = out->height > 1024 ? 1024 : out->height;
fprintf(stderr, "(%dx%d)", out->width, out->height);
/* Unpack pixels */
pixels = (u16_t *) in;
npixels = out->width * out->height;
if (out_sz > npixels * 4) {
/* Deep image (i.e. 32-bit) */
memcpy(out->planes, pixels, npixels * 4);
} else {
/* Shallow image (i.e. 16-bit) */
for (i = 0, j = 0 ; i < npixels ; i++) {
pixel = pixels[i];
out->planes[j++] = ((pixel ) & 0x1F) << 3; /* B */
out->planes[j++] = ((pixel >> 5) & 0x1F) << 3; /* G */
out->planes[j++] = ((pixel >> 10) ) << 3; /* R */
out->planes[j++] = pixel & 0x8000 ? 0xFF : 0x00; /* A */
}
}
}
static u8_t *expand_lzss(u8_t *lzss, size_t *pout_sz)
{
static u8_t ring[0x1000];
unsigned int ring_pos = 0x0FEE;
unsigned int chunk_offset;
unsigned int chunk_length;
u32_t control_word = 1;
size_t length;
u8_t cmd1;
u8_t cmd2;
u8_t *out;
u8_t *pos;
u8_t *in;
/* Header = 32 bit unpacked file length */
length = *((u32_t *) lzss);
*pout_sz = length;
if (length > 8000000) {
fprintf(stderr, "(FAIL: Unreasonably large expanded size %d)\n",
length);
exit(EXIT_FAILURE);
}
out = malloc(length * 2); /* Seems to overrun */
pos = out;
in = lzss + 4;
while (length > 0) {
if (control_word == 1) {
/* Read a control byte */
control_word = 0x100 | *in++;
}
/* Decode a byte according to the current control byte bit */
if (control_word & 1) {
/* Straight copy */
*pos++ = *in;
ring[ring_pos] = *in++;
ring_pos = (ring_pos + 1) % 0x1000;
length--;
} else {
/* Reference to data in ring buffer */
cmd1 = *in++;
cmd2 = *in++;
chunk_length = (cmd2 & 0x0F) + 3;
chunk_offset = ((cmd2 & 0xF0) << 4) | cmd1;
for ( ; chunk_length > 0 ; chunk_length--) {
/* Copy historical data to output AND current ring pos */
*pos++ = ring[chunk_offset];
ring[ring_pos] = ring[chunk_offset];
/* Update counters */
chunk_offset = (chunk_offset + 1) % 0x1000;
ring_pos = (ring_pos + 1) % 0x1000;
length--;
}
}
/* Get next control bit */
control_word >>= 1;
}
return out;
}
static void readfile(const char *filename, u8_t **data, long *nbytes)
{
FILE *f;
f = fopen(filename, "rb");
if (f == NULL) abort();
fseek(f, 0, SEEK_END);
*nbytes = ftell(f);
fseek(f, 0, SEEK_SET);
*data = malloc(*nbytes);
fread(*data, *nbytes, 1, f);
fclose(f);
}
void put8(FILE *f, unsigned char val)
{
fwrite(&val, 1, 1, f);
}
void put16(FILE *f, unsigned short val)
{
fwrite(&val, 2, 1, f);
}
void split_images(const char *in_dir, const char *out_dir,
struct image *images, int nimages)
{
struct clip *clips;
char filename[512];
long nbytes;
u8_t *data;
char *name;
FILE *f;
int i;
int j;
int k;
/* Read file and get TOC */
sprintf(filename, "%s/system.idx", in_dir);
readfile(filename, &data, &nbytes);
clips = (struct clip *) (data + 0x01BC);
name = (char *) (data + 8 + *((long *) data));
/* Guess how many clips there are with a heuristic */
for (i = 0 ; clips[i].w != 0 && clips[i].h != 0 ; i++) {
sprintf(filename, "%s/%s.tga", out_dir, name);
name += strlen(name) + 3;
f = fopen(filename, "wb");
if (f == NULL) abort();
/* Locate the correct source image */
j = 0;
while (clips[i].y > images[j].height) {
clips[i].y -= images[j].height;
j++;
}
/* Write header */
put8(f, 0); put8(f, 0); put8(f, 2);
put16(f, 0); put16(f, 0); put8(f, 0);
put16(f, 0); put16(f, 0); put16(f, clips[i].w); put16(f, clips[i].h);
put8(f, 32); put8(f, 32);
/* Write scanlines */
for (k = 0 ; k < clips[i].h ; k++) {
if (clips[i].y == images[j].height) {
clips[i].y = 0;
j++;
}
fwrite(images[j].planes + ((images[j].width * clips[i].y) +
clips[i].x) * 4, clips[i].w, 4, f);
clips[i].y++;
}
/* Close output file */
fclose(f);
}
/* Cleanup */
free(data);
}
int main(int argc, char **argv)
{
char *in_dir;
char *out_dir;
struct image images[32];
char filename[256];
unsigned int i;
long filesize;
u8_t *lzss, *gc;
size_t out_sz;
FILE *f;
/* Usage */
if (argc != 3) {
fprintf(stderr, "Usage: %s [indir] [outdir]\n", argv[0]);
return EXIT_FAILURE;
}
/* Setup */
memset(images, 0, sizeof(images));
in_dir = argv[1];
out_dir = argv[2];
for (i = 0 ; i < 32 ; i++) {
/* Open 0.gcz, 1.gcz etc ... */
sprintf(filename, "%s/%d.gcz", in_dir, i);
f = fopen(filename, "rb");
if (f == NULL) break;
/* Read entire file */
fseek(f, 0, SEEK_END);
filesize = ftell(f);
fseek(f, 0, SEEK_SET);
fprintf(stderr, "%s: fread", filename);
lzss = malloc(filesize);
fread(lzss, filesize, 1, f);
fclose(f);
/* Decompress */
fprintf(stderr, "(OK) expand_lzss");
gc = expand_lzss(lzss, &out_sz);
free(lzss);
/* Unpack GC to 32-bit RGBA */
fprintf(stderr, "(OK) unpack_gc");
unpack_gc(&images[i], gc, out_sz);
free(gc);
fprintf(stderr, "(OK)\n");
}
/* Sanity check */
if (i == 0) {
fprintf(stderr, "No GCZ files found\n");
exit(EXIT_FAILURE);
}
/* Emit pile of TGAs */
fprintf(stderr, "split_images");
split_images(in_dir, out_dir, images, i);
fprintf(stderr, "(OK)\n\n");
return 0;
}
What is wrong with the code that could be causing this? The code is unaltered save for #define _CRT_SECURE_NO_WARNINGS being added to the code before the #include headers and having the program compiled as C.

Adafruit SHT31-D and Raspberry Pi2 -- Unable to read data from sensor

hopefully one of you out there can help me!
I am trying to use the Adafruit SHT31-D (an i2c device) board with my Pi2. I am going off of this datasheet to guide my coding efforts. I am using Wiring Pi (wiringpi.com) to facilitate things.
I am able to successfully open a connection to the device, and sending commands seems to work fine, but I am unable to read data back! Here is the little mini library I have put together. I am hoping that one of you might have some experience with this sort of thing and be able to help me see where I've gone wrong.
To rule out any possible issues with the sensor hardware, I have tested it with my Arduino UNO and it works without issues.
Here is my C++ code:
SHT3x.h
#pragma once
/* Sensor Commands */
#define DEFAULT_SHT_ADDR 0x44
#define MEAS_HREP_STRETCH 0x2C06
#define MEAS_MREP_STRETCH 0x2C0D
#define MEAS_LREP_STRETCH 0x2C10
#define MEAS_HREP 0x2400
#define MEAS_MREP 0x240B
#define MEAS_LREP 0x2416
#include <cstdint>
class SHT3x {
public:
SHT3x(const uint8_t& i2cAddr);
float readHumidity(const uint16_t& command) const;
float readTempC(const uint16_t& command) const;
float readTempF(const uint16_t& command) const;
private:
int8_t _fd;
uint8_t _header;
uint32_t getMeasurement(const uint16_t& command) const;
void sendCommand(const uint16_t& command) const;
uint32_t receiveData(void) const;
};
SHT3x.cpp
#include <stdexcept>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include "SHT3x.h"
SHT3x::SHT3x(const uint8_t& i2cAddr) {
_fd = wiringPiI2CSetup(i2cAddr);
_header = i2cAddr << 1;
if (_fd < 0) {
throw std::runtime_error("Unable to connect");
}
}
float SHT3x::readHumidity(const uint16_t& command) const {
uint32_t raw_data = getMeasurement(command);
if (!raw_data) {
throw std::runtime_error("Bad Reading.");
}
uint16_t raw_humidity = raw_data & 0xFFFF;
float humidity = 100.0 * ((float) raw_humidity / (float) 0xFFFF);
return humidity;
}
float SHT3x::readTempC(const uint16_t& command) const {
uint32_t raw_data = getMeasurement(command);
if (!raw_data) {
throw std::runtime_error("Bad Reading.");
}
uint16_t raw_temp = raw_data >> 16;
float tempC = -45.0 + (175.0 * ((float) raw_temp / (float) 0xFFFF));
return tempC;
}
float SHT3x::readTempF(const uint16_t& command) const {
uint32_t raw_data = getMeasurement(command);
if (!raw_data) {
throw std::runtime_error("Bad Reading.");
}
uint16_t raw_temp = raw_data >> 16;
float tempF = -49.0 + (315.0 * ((float) raw_temp / (float) 0xFFFF));
return tempF;
}
uint32_t SHT3x::getMeasurement(const uint16_t& command) const {
try {
sendCommand(command);
} catch (std::runtime_error& e) {
throw;
}
return receiveData();
}
void SHT3x::sendCommand(const uint16_t& command) const {
// break command into bytes
uint8_t MSB = command >> 8;
uint8_t LSB = command & 0xFF;
// send header
int8_t ack = wiringPiI2CWrite(_fd, _header);
// send command
ack &= wiringPiI2CWrite(_fd, MSB);
ack &= wiringPiI2CWrite(_fd, LSB);
// handle errors
if (ack) {
throw std::runtime_error("Sending command failed.");
}
}
uint32_t SHT3x::receiveData(void) const {
uint32_t data;
// send header
uint8_t read_header = _header | 0x01;
int8_t ack = wiringPiI2CWrite(_fd, read_header);
// handle errors
if (ack) throw std::runtime_error("Unable to read data.");
// read data
data = wiringPiI2CRead(_fd);
for (size_t i = 0; i < 4; i++) {
printf("Data: %d\n", data);
data <<= 8;
if (i != 1) {
data |= wiringPiI2CRead(_fd);
} else {
wiringPiI2CRead(_fd); // skip checksum
}
}
wiringPiI2CRead(_fd); // second checksum
return data;
}
The SHT31 uses 16bit read and write, rather than using 2 8bit writes you might be better off using wiringpi's 16bit write. wiringPiI2CWriteReg16(). Same thing applies to the read.
Below is a very early copy of what I've done to read the sht31-d on a PI. It has no dependencies except i2c-dev. Heater enable/disable is not working, but softreset, clearstatus, getserial & get temp/humid are all fine.
/*
* Referances
* https://www.kernel.org/doc/Documentation/i2c/dev-interface
* https://github.com/adafruit/Adafruit_SHT31
* https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity_and_Temperature_Sensors/Sensirion_Humidity_and_Temperature_Sensors_SHT3x_Datasheet_digital.pdf
*
* This depends on i2c dev lib
* sudo apt-get install libi2c-dev
*
* Below is also a good one to have, but be careful i2cdump from the below cause the sht31 interface to become unstable for me
* and requires a hard-reset to recover correctly.
* sudo apt-get install i2c-tools
*
* on PI make sure below 2 commands are in /boot/config.txt
* dtparam=i2c_arm=on
* dtparam=i2c1_baudrate=10000
* I know we are slowing down the baurate from optimal, but it seems to be the most stable setting in my testing.
* add another 0 to the above baudrate for max setting, ie dtparam=i2c1_baudrate=100000
*/
#include <linux/i2c-dev.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <elf.h>
#include <unistd.h>
#define SHT31_INTERFACE_ADDR 1
#define SHT31_DEFAULT_ADDR 0x44
#define SHT31_READ_SERIALNO 0x3780
#define SHT31_MEAS_HIGHREP_STRETCH 0x2C06 // Doesn't work on PI
#define SHT31_MEAS_MEDREP_STRETCH 0x2C0D // Seems to work on PI but shouldn't
#define SHT31_MEAS_LOWREP_STRETCH 0x2C10 // Seems to work on PI but shouldn't
#define SHT31_MEAS_HIGHREP 0x2400 // Doesn't work on PI
#define SHT31_MEAS_MEDREP 0x240B
#define SHT31_MEAS_LOWREP 0x2416
#define SHT31_READSTATUS 0xF32D
#define SHT31_CLEARSTATUS 0x3041
#define SHT31_SOFTRESET 0x30A2
#define SHT31_HEATER_ENABLE 0x306D
#define SHT31_HEATER_DISABLE 0x3066
#define CHECK_BIT(var,pos) (((var)>>(pos)) & 1)
/*
* delay:
* Wait for some number of milliseconds
*********************************************************************************
*/
void delay (unsigned int howLong)
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
nanosleep (&sleeper, &dummy) ;
}
/*
*
* CRC-8 formula from page 14 of SHT spec pdf
*
* Test data 0xBE, 0xEF should yield 0x92
*
* Initialization data 0xFF
* Polynomial 0x31 (x8 + x5 +x4 +1)
* Final XOR 0x00
*/
uint8_t crc8(const uint8_t *data, int len)
{
const uint8_t POLYNOMIAL = 0x31;
uint8_t crc = 0xFF;
int j;
int i;
for (j = len; j; --j ) {
crc ^= *data++;
for ( i = 8; i; --i ) {
crc = ( crc & 0x80 )
? (crc << 1) ^ POLYNOMIAL
: (crc << 1);
}
}
return crc;
}
/*
*
* buffer should return with data read, size defined by readsize
*********************************************************************************
*/
int writeandread(int fd, uint16_t sndword, uint8_t *buffer, int readsize)
{
int rtn;
uint8_t snd[3];
// Split the 16bit word into two 8 bits that are flipped.
snd[0]=(sndword >> 8) & 0xff;
snd[1]=sndword & 0xff;
rtn = write(fd, snd, 2);
if ( rtn != 2 ) {
return 1;
}
if (readsize > 0) {
delay(10);
rtn = read(fd, buffer, readsize);
if ( rtn < readsize) {
return 2;
}
}
return 0;
}
void printserialnum(int file)
{
uint8_t buf[10];
int rtn;
rtn = writeandread(file, SHT31_READ_SERIALNO, buf, 6);
if (rtn != 0)
printf("ERROR:- Get serial i2c %s failed\n",(rtn==1?"write":"read"));
else {
if (buf[2] != crc8(buf, 2) || buf[5] != crc8(buf+3, 2))
printf("WARNING:- Get serial CRC check failed, don't trust result\n");
uint32_t serialNo = ((uint32_t)buf[0] << 24)
| ((uint32_t)buf[1] << 16)
| ((uint32_t)buf[3] << 8)
| (uint32_t)buf[4];
printf("Serial# = %d\n",serialNo);
}
}
void printtempandhumidity(int file)
{
uint8_t buf[10];
int rtn;
rtn = writeandread(file, SHT31_MEAS_MEDREP_STRETCH, buf, 6);
if (rtn != 0)
printf("ERROR:- Get temp/humidity i2c %s failed\n",(rtn==1?"write":"read"));
else {
if ( buf[2] != crc8(buf, 2) || buf[5] != crc8(buf+3, 2))
printf("WARNING:- Get temp/humidity CRC check failed, don't trust results\n");
uint16_t ST, SRH;
ST = buf[0];
ST <<= 8;
ST |= buf[1];
SRH = buf[3];
SRH <<= 8;
SRH |= buf[4];
double stemp = ST;
stemp *= 175;
stemp /= 0xffff;
stemp = -45 + stemp;
double stempf = ST;
stempf *= 315;
stempf /= 0xffff;
stempf = -49 + stempf;
printf("Temperature %.2fc - %.2ff\n",stemp,stempf);
double shum = SRH;
shum *= 100;
shum /= 0xFFFF;
printf("Humidity %.2f%%\n",shum);
}
}
void printBitStatus(uint16_t stat)
{
printf("Status\n");
printf(" Checksum status %d\n", CHECK_BIT(stat,0));
printf(" Last command status %d\n", CHECK_BIT(stat,1));
printf(" Reset detected status %d\n", CHECK_BIT(stat,4));
printf(" 'T' tracking alert %d\n", CHECK_BIT(stat,10));
printf(" 'RH' tracking alert %d\n", CHECK_BIT(stat,11));
printf(" Heater status %d\n", CHECK_BIT(stat,13));
printf(" Alert pending status %d\n", CHECK_BIT(stat,15));
}
void printstatus(int file)
{
uint8_t buf[10];
int rtn;
rtn = writeandread(file, SHT31_READSTATUS, buf, 3);
if (rtn != 0)
printf("ERROR:- readstatus %s failed\n",(rtn==1?"write":"read"));
else {
if ( buf[2] != crc8(buf, 2))
printf("WARNING:- Get status CRC check failed, don't trust results\n");
uint16_t stat = buf[0];
stat <<= 8;
stat |= buf[1];
printBitStatus(stat);
}
}
void clearstatus(int file)
{
if( writeandread(file, SHT31_CLEARSTATUS, NULL, 0) != 0)
printf("ERROR:- sht31 clear status failed\n");
else
printf("Clearing status - ok\n");
}
void softreset(int file)
{
if( writeandread(file, SHT31_SOFTRESET, NULL, 0) != 0)
printf("ERROR:- sht31 soft reset failed\n");
else
printf("Soft reset - ok\n");
}
void enableheater(int file)
{
if( writeandread(file, SHT31_HEATER_ENABLE, NULL, 0) != 0)
printf("ERROR:- sht31 heater enable failed\n");
else
printf("Enabiling heater - ok\n");
}
void disableheater(int file)
{
if( writeandread(file, SHT31_HEATER_DISABLE, NULL, 0) != 0)
printf("ERROR:- sht31 heater enable failed\n");
else
printf("Disableing heater - ok\n");
}
int main()
{
int file;
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", SHT31_INTERFACE_ADDR);
file = open(filename, O_RDWR);
if (file < 0) {
printf("ERROR:- Can't open %s\n",filename);
exit(1);
}
if (ioctl(file, I2C_SLAVE, SHT31_DEFAULT_ADDR) < 0) {
printf("ERROR:- Connecting to sht31 I2C address 0x%02hhx\n", SHT31_DEFAULT_ADDR);
exit(1);
}
softreset(file);
printtempandhumidity(file);
printstatus(file);
close(file);
return 0;
}

AF_LINK not defined in bits/socket.h and sys/socket.h

I am writing a code to read the MAC address from the interface for listing the interface and it MAC address as I am not able to find the AF_LINK definition in either of the socket.h files.
As per internet resources i should see below:
#define AF_LINK 18 /* Link layer interface */
But my bits/socket.h contains:
#define PF_ASH 18 /* Ash. */
.
.
#define AF_ASH PF_ASH
Should I be using PF_ASH in place of AF_LINK?
Here is the part of my code that uses AF_LINK:
if ((family == AF_LINK) && (ifa->ifa_name[0] == 'e')) {
//This is for extracting interface number from interface name//
char newi[3];
int i, j;
for (i=0, j=0; i < strlen(ifa->ifa_name); i++) {
if (ifa->ifa_name[i] >= '0' && ifa->ifa_name[i] <= '9') {
newi[j++] = ifa->ifa_name[i];
}
}
newi[j] = '\0';
if_num = atoi(newi);
printf("Interface %d : %d\n", k++, if_num);
}
Full code:
/*
* ethernetsocket.c
*
* Created on: Feb 25, 2015
* Author: tsp3859
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <bits/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <ifaddrs.h>
#define MY_DEST_MAC0 0xff
#define MY_DEST_MAC1 0xff
#define MY_DEST_MAC2 0xff
#define MY_DEST_MAC3 0xff
#define MY_DEST_MAC4 0xff
#define MY_DEST_MAC5 0xff
// Source Ethernet interface
#define DEFAULT_InterFace "eth0"
#define DEFAULT_PayLoad "1.1.10"
// Allocating size to different containers
#define MAX_FRAME_SIZE 1024
#define MAX_PAYLD_SIZE 1000
#define HEADER_SIZE 14
int payLoad_Size = -1;
int frame_Size = -1;
int main(int argc, char *argv[]) {
int sockfd;
struct ifreq if_idx; // destination ethernet (optional)
struct ifreq if_mac; // destination mac address
int tx_len = 0; // header counter
char ifName[IFNAMSIZ]; // interface name
uint8_t header[HEADER_SIZE]; // ethernet header
char dummy_Payload[MAX_PAYLD_SIZE];
int if_num; // interface number, used for genarating VID
/*
* Run as one of the following command
* 1. ./a.out
* 2 ./a.out eth3
* 3. ./a.out eth4 PayLoad
*/
// Get Ethernet interface name from command line (optional)
if (argc > 1) {
if (argc == 2) {
strcpy(ifName, argv[1]);
strcpy(dummy_Payload, DEFAULT_PayLoad);
}
if (argc == 3) {
strcpy(ifName, argv[1]);
if (strlen(argv[2]) > 1000) {
memcpy(dummy_Payload, argv[2], MAX_PAYLD_SIZE);
} else
strcpy(dummy_Payload, argv[2]);
}
} else {
// Default case: All fields are optional
if (argc < 2) {
strcpy(ifName, DEFAULT_InterFace);
strcpy(dummy_Payload, DEFAULT_PayLoad);
}
}
//Getting interface number
struct ifaddrs *ifaddr, *ifa;
int family, s, n;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
int k = 1; // Interface SNo.
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if ((family == AF_INET || family == AF_INET6) && (ifa->ifa_name[0] == 'e')) {
char newi[3];
int i, j;
for (i=0, j=0; i < strlen(ifa->ifa_name); i++) {
if (ifa->ifa_name[i] >= '0' && ifa->ifa_name[i] <= '9') {
newi[j++] = ifa->ifa_name[i];
}
}
newi[j] = '\0';
if_num = atoi(newi);
printf("Interface %d : %d\n", k++, if_num);
}
}
// Setting frame size
payLoad_Size = strlen(dummy_Payload);
// Setting payload, contains VID
char payLoad[payLoad_Size];
//memcpy(payLoad,dummy_Payload,payLoad_Size);
int len=0;
payLoad[len++]=1;
payLoad[len++]=1;
payLoad[len]=10;
frame_Size = HEADER_SIZE + strlen(payLoad);
//printf("Payload size is %d\n ", payLoad_Size);
printf("Frame size is %d\n ", frame_Size);
printf("Payload size is %d\n\n ", strlen(payLoad));
payLoad_Size=strlen(payLoad);
// creating frame
uint8_t frame[frame_Size];
struct ether_header *eh = (struct ether_header *) header;
struct sockaddr_ll socket_address;
// Open RAW socket to send on
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("Socket Error");
}
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, ifName, IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
perror("SIOCGIFINDEX - Misprint Compatibility");
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, ifName, IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0)
perror(
"SIOCGIFHWADDR - Either interface is not correct or disconnected");
// Initializing the Ethernet Header
memset(header, 0, HEADER_SIZE);
// Print-test initial header
printf("Zeros: %02x:%02x:%02x:%02x:%02x:%02x %02x:%02x:%02x:%02x:%02x:%02x %02x:%02x\n",
header[0], header[1], header[2], header[3], header[4], header[5],
header[6], header[7], header[8], header[9], header[10], header[11],
header[12], header[13]);
/*
* Ethernet Header - 14 bytes
*
* 6 bytes - Source MAC Address
* 6 bytes - Destination MAC Address
* 2 bytes - EtherType
*
*/
eh->ether_shost[0] = ((uint8_t *) &if_mac.ifr_hwaddr.sa_data)[0];
eh->ether_shost[1] = ((uint8_t *) &if_mac.ifr_hwaddr.sa_data)[1];
eh->ether_shost[2] = ((uint8_t *) &if_mac.ifr_hwaddr.sa_data)[2];
eh->ether_shost[3] = ((uint8_t *) &if_mac.ifr_hwaddr.sa_data)[3];
eh->ether_shost[4] = ((uint8_t *) &if_mac.ifr_hwaddr.sa_data)[4];
eh->ether_shost[5] = ((uint8_t *) &if_mac.ifr_hwaddr.sa_data)[5];
eh->ether_dhost[0] = MY_DEST_MAC0;
eh->ether_dhost[1] = MY_DEST_MAC1;
eh->ether_dhost[2] = MY_DEST_MAC2;
eh->ether_dhost[3] = MY_DEST_MAC3;
eh->ether_dhost[4] = MY_DEST_MAC4;
eh->ether_dhost[5] = MY_DEST_MAC5;
eh->ether_type = htons(0x8010);
tx_len += sizeof(struct ether_header);
// Copying header to frame
memcpy(frame, header, 14);
// Copying payLoad to frame
//printf("Payload: %d\n", payLoad[1]);
memcpy(frame + 14, payLoad, strlen(payLoad));
// Printing initial frame
printf(" Frame: %02x:%02x:%02x:%02x:%02x:%02x %02x:%02x:%02x:%02x:%02x:%02x %02x:%02x\n",
frame[0], frame[1], frame[2], frame[3], frame[4], frame[5],
frame[6], frame[7], frame[8], frame[9], frame[10], frame[11],
frame[12], frame[13]);
// Printing payLoad
printf("Payload: %d.%d.%d\n", frame[14],frame[15],frame[16]);
// Index of the network device
socket_address.sll_ifindex = if_idx.ifr_ifindex;
// Address length - 6 bytes
socket_address.sll_halen = ETH_ALEN;
// Destination MAC Address
socket_address.sll_addr[0] = MY_DEST_MAC0;
socket_address.sll_addr[1] = MY_DEST_MAC1;
socket_address.sll_addr[2] = MY_DEST_MAC2;
socket_address.sll_addr[3] = MY_DEST_MAC3;
socket_address.sll_addr[4] = MY_DEST_MAC4;
socket_address.sll_addr[5] = MY_DEST_MAC5;
// Send packet
if (sendto(sockfd, frame, tx_len + strlen(payLoad), 0,
(struct sockaddr*) &socket_address, sizeof(struct sockaddr_ll)) < 0)
printf("Send failed\n");
freeifaddrs(ifaddr);
exit(EXIT_SUCCESS);
return 0;
}

PCM audio playback using alsa in RHEL6

I am trying to play a wave file in RHEL6 using alsa library calls in my C Code in Qt. I am reading the wave file ("t15.wav") in a buffer(wave_buffer). The wave header has been stripped off since the alsa library requires raw PCM samples to be played. Further I have set up the PCM hardware & Software params using 'snd_pcm_hw_params(PCM, params)' & 'snd_pcm_sw_params_current(PCM, swparams)' and many other calls. I am writing the PCM samples on the PCM handle using 'snd_pcm_writei' command. For this purpose i am reading a chunk(32 or 1024 or 2048 or 4096 or 8192 bytes) of data from the wave_buffer and sending it for playing using the snd_pcm_writei command. If I choose a small chunk the audio quality falters but playback is uninterrupted. If I use a bigger chunk(greater than 4096 i.e. 8192) I get perfect audio quality but it is interrupted( When next chunk of data is required for playing ). My constraint is that I can have access to data in chunks only and not as a file or entire buffer. Can anybody help me in removing the interruptions in playing the wave data so that I can get uninterrupted audio playback. Following is my code :
The two variables buffer_time & period_time return the period size which is the size of chunk.
If buffer_time = 5000 & period_time=1000 the period_size returned by alsa library is 32 bytes //audio quality falters but no interruptions
If buffer_time = 500000 & period_time=100000 the period_size returned by alsa library is 8192 bytes //good audio quality but interrupted
Tuning these parameters seems useless as I have wasted a lot of time doing this. Please help me get through this problem-----
Stucture of Wave File :
Sample Rate : 44100
Bits per Sample : 16
Channels : 2
mainwindow.h----
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <alsa/asoundlib.h>
#define BLOCKSIZE 44100 * 2 * 2 // Sample Rate * Channels * Byte per Sample(Bits per sample / 8)
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
int init_alsa();
int play_snd();
~MainWindow();
snd_pcm_t *PCM;
snd_pcm_sframes_t delayp;
snd_pcm_sframes_t availp;
snd_pcm_sw_params_t *swparams;
snd_pcm_hw_params_t *params;
static snd_pcm_sframes_t period_size;
static snd_pcm_sframes_t buffer_size;
unsigned char wave_buffer[900000];
unsigned char play_buffer[BLOCKSIZE];
int filesize;
FILE *fp;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp---
#include "mainwindow.h"
#include "ui_mainwindow.h"
snd_pcm_sframes_t MainWindow::period_size;
snd_pcm_sframes_t MainWindow::buffer_size;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
if((fp = fopen("t15.wav","rb"))==NULL)
printf("Error Opening File");
fseek(fp,0L,SEEK_END);
filesize = ftell(fp)-44;
fseek(fp,0L,SEEK_SET);
fseek(fp,44,SEEK_SET);
fread(wave_buffer,filesize,1,fp);
fclose(fp);
delayp = 0;
init_alsa();
play_snd();
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::init_alsa()
{
unsigned int rate = 44100;
int err,dir;
unsigned int rrate = 44100;
snd_pcm_uframes_t size;
static unsigned int buffer_time = 500000;
static unsigned int period_time = 100000;
static int period_event = 0;
if ((err=snd_pcm_open(&PCM,"plughw:0,0",SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
fprintf(stderr, "Can't use sound: %s\n", snd_strerror(err));
return err;
}
snd_pcm_hw_params_alloca(&params);
snd_pcm_sw_params_alloca(&swparams);
//snd_pcm_nonblock(PCM,0);
/* choose all parameters */
err = snd_pcm_hw_params_any(PCM, params);
if (err < 0) {
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
return err;
}
/* set hardware resampling */
err = snd_pcm_hw_params_set_rate_resample(PCM, params, 1);
if (err < 0) {
printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
return err;
}
/* set the interleaved read/write format */
err = snd_pcm_hw_params_set_access(PCM, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
printf("Access type not available for playback: %s\n", snd_strerror(err));
return err;
}
/* set the sample format */
err = snd_pcm_hw_params_set_format(PCM, params, SND_PCM_FORMAT_S16_LE);
if (err < 0) {
printf("Sample format not available for playback: %s\n", snd_strerror(err));
return err;
}
/* set the count of channels */
err = snd_pcm_hw_params_set_channels(PCM, params, 2);
if (err < 0) {
printf("Channels count (%i) not available for playbacks: %s\n", 2, snd_strerror(err));
return err;
}
/* set the stream rate */
rrate = rate;
err = snd_pcm_hw_params_set_rate_near(PCM, params, &rrate, 0);
if (err < 0) {
printf("Rate %iHz not available for playback: %s\n", 44100, snd_strerror(err));
return err;
}
if (rrate != 44100) {
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rrate, err);
return -EINVAL;
}
/* set the buffer time */
err = snd_pcm_hw_params_set_buffer_time_near(PCM, params, &buffer_time, &dir);
if (err < 0) {
printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_get_buffer_size(params, &size);
if (err < 0) {
printf("Unable to get buffer size for playback: %s\n", snd_strerror(err));
return err;
}
buffer_size = size;
/* set the period time */
err = snd_pcm_hw_params_set_period_time_near(PCM, params, &period_time, &dir);
if (err < 0) {
printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_get_period_size(params, &size, &dir);
if (err < 0) {
printf("Unable to get period size for playback: %s\n", snd_strerror(err));
return err;
}
period_size = size;
/* write the parameters to device */
err = snd_pcm_hw_params(PCM, params);
if (err < 0) {
printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
return err;
}
printf("Size = %ld",period_size);
snd_pcm_sw_params_current(PCM, swparams); /* get the current swparams */
/* start the transfer when the buffer is almost full: */
/* (buffer_size / avail_min) * avail_min */
snd_pcm_sw_params_set_start_threshold(PCM, swparams, (buffer_size / period_size) * period_size);
/* allow the transfer when at least period_size samples can be processed */
/* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
snd_pcm_sw_params_set_avail_min(PCM, swparams, period_event ? buffer_size : period_size);
snd_pcm_sw_params(PCM, swparams);/* write the parameters to the playback device */
return 1;
}
int MainWindow::play_snd()
{
int curr_pos = 0;
int buff_size = 0;
long val = 0;
while(curr_pos < filesize)
{
if(filesize-curr_pos >= period_size)
{
memcpy(play_buffer,wave_buffer+curr_pos,period_size);
buff_size = period_size;
curr_pos += buff_size;
}
else
{
memcpy(play_buffer,wave_buffer+curr_pos,filesize-curr_pos);
buff_size = filesize - curr_pos;
curr_pos += buff_size;
}
int i=1;
unsigned char *ptr = play_buffer;
while(buff_size > 0)
{
val = snd_pcm_writei(PCM,&play_buffer,buff_size);
if (val == -EAGAIN)
continue;
ptr += val * 2;
buff_size -= val;
}
}
return 0;
}
I have a similar C Code of alsa library which generates sine wave samples at runtime and plays them using same snd_pcm_writei command and it plays perfectly without any interruptions....This is the alsa library code---
/*
* This small demo sends a simple sinusoidal wave to your speakers.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include "alsa/asoundlib.h"
#include <sys/time.h>
#include <math.h>
static char *device = "plughw:0,0"; /* playback device */
static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; /* sample format */
static unsigned int rate = 44100; /* stream rate */
static unsigned int channels = 2; /* count of channels */
static unsigned int buffer_time = 5000; /* ring buffer length in us */
static unsigned int period_time = 1000; /* period time in us */
static double freq = 440; /* sinusoidal wave frequency in Hz */
static int resample = 1; /* enable alsa-lib resampling */
static int period_event = 0; /* produce poll event after each period */
static snd_pcm_sframes_t buffer_size;
static snd_pcm_sframes_t period_size;
static snd_output_t *output = NULL;
snd_pcm_sframes_t delayp;
snd_pcm_sframes_t availp;
static void generate_sine(const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
int count, double *_phase)
{
static double max_phase = 2. * M_PI;
double phase = *_phase;
double step = max_phase*freq/(double)rate;
unsigned char *samples[channels];
int steps[channels];
unsigned int chn;
int format_bits = snd_pcm_format_width(format);
unsigned int maxval = (1 << (format_bits - 1)) - 1;
int bps = format_bits / 8; /* bytes per sample */
int phys_bps = snd_pcm_format_physical_width(format) / 8;
int big_endian = snd_pcm_format_big_endian(format) == 1;
int to_unsigned = snd_pcm_format_unsigned(format) == 1;
int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
format == SND_PCM_FORMAT_FLOAT_BE);
/* verify and prepare the contents of areas */
for (chn = 0; chn < channels; chn++) {
samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
steps[chn] = areas[chn].step / 8;
samples[chn] += offset * steps[chn];
}
/* fill the channel areas */
while (count-- > 0) {
union {
float f;
int i;
} fval;
int res, i;
if (is_float)
{
fval.f = sin(phase) * maxval;
res = fval.i;
}
else
res = sin(phase) * maxval;
if (to_unsigned)
res ^= 1U << (format_bits - 1);
for (chn = 0; chn < channels; chn++) {
/* Generate data in native endian format */
if (big_endian) {
for (i = 0; i < bps; i++)
*(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
} else {
for (i = 0; i < bps; i++)
*(samples[chn] + i) = (res >> i * 8) & 0xff;
}
samples[chn] += steps[chn];
}
phase += step;
if (phase >= max_phase)
phase -= max_phase;
}
*_phase = phase;
}
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, snd_pcm_access_t access)
{
unsigned int rrate;
snd_pcm_uframes_t size;
int dir;
snd_pcm_hw_params_any(handle, params); /* choose all parameters */
snd_pcm_hw_params_set_rate_resample(handle, params, resample);/* set hardware resampling */
snd_pcm_hw_params_set_access(handle, params, access); /* set the interleaved read/write format */
snd_pcm_hw_params_set_format(handle, params, format); /* set the sample format */
snd_pcm_hw_params_set_channels(handle, params, channels); /* set the count of channels */
rrate = rate; /* set the stream rate */
snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);/* set the buffer time */
snd_pcm_hw_params_get_buffer_size(params, &size);
buffer_size = size;
snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);/* set the period time */
snd_pcm_hw_params_get_period_size(params, &size, &dir);
period_size = size;
snd_pcm_hw_params(handle, params); /* write the parameters to device */
return 0;
}
static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
{
snd_pcm_sw_params_current(handle, swparams); /* get the current swparams */
/* start the transfer when the buffer is almost full: */
/* (buffer_size / avail_min) * avail_min */
snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size);
/* allow the transfer when at least period_size samples can be processed */
/* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size);
snd_pcm_sw_params(handle, swparams);/* write the parameters to the playback device */
return 0;
}
/*
* Transfer method - write only
*/
static int write_loop(snd_pcm_t *handle, signed short *samples, snd_pcm_channel_area_t *areas)
{
double phase = 0;
signed short *ptr;
int err, cptr;
int i=0;
printf("Period Size = %ld",period_size);
while (1) {
fflush(stdout);
generate_sine(areas, 0, period_size, &phase);
ptr = samples;
cptr = period_size;
i=1;
while (cptr > 0) {
err = snd_pcm_writei(handle, ptr, cptr);
snd_pcm_avail_delay(handle,&availp,&delayp);
printf("available frames =%ld delay = %ld i = %d\n",availp,delayp,i);
if (err == -EAGAIN)
continue;
ptr += err * channels;
cptr -= err;
i++;
}
}
}
/*
* Transfer method - asynchronous notification
*/
int main()
{
snd_pcm_t *handle;
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
signed short *samples;
unsigned int chn;
snd_pcm_channel_area_t *areas;
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
snd_output_stdio_attach(&output, stdout, 0);
printf("Playback device is %s\n", device);
printf("Stream parameters are %iHz, %s, %i channels\n", rate, snd_pcm_format_name(format), channels);
printf("Sine wave rate is %.4fHz\n", freq);
snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0);
set_hwparams(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
set_swparams(handle, swparams);
samples = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
for (chn = 0; chn < channels; chn++) {
areas[chn].addr = samples;
areas[chn].first = chn * snd_pcm_format_physical_width(format);
areas[chn].step = channels * snd_pcm_format_physical_width(format);
}
write_loop(handle, samples, areas);
free(areas);
free(samples);
snd_pcm_close(handle);
return 0;
}
I solved the problem by altering the length argument of snd_pcm_writei...perviously i was giving it equal to the data contained in play_buffer...now i changed it to "buff_size/4" and the audio is playing perfectly without breaks. Actually it is the size after which the system should start buffering for new pcm samples as per my understanding. Previously it was buffering after playing the entire length buff_size and that resulted in breaks in audio output...

print the inode bitmap as an unsigned char

how can I print the bits of inode bitmap which is fetched as an unsigned char? this is my code which is used to fetch bitmap.what i want to do is to print the bitmap in binary format.
#include <cstdlib>
#include <ext2fs/ext2fs.h>
#include <linux/fs.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <fcntl.h>
using namespace std;
int main() {
int fd, block_size;
char boot[1024];
struct ext2_super_block super_block;
fd = open("/dev/sda6", O_RDONLY);
printf("Error: %s\n", strerror(errno));
/* Reads the boot section and the superblock */
read(fd, boot, 1024);
read(fd, &super_block, sizeof (struct ext2_super_block));
if (super_block.s_magic != EXT2_SUPER_MAGIC) {
fprintf(stderr, "Not an Ext2 filesystem!\n");
getchar();
exit(1);
}
/* Prints the Magic Number */
// printf("%x\n", super_block.s_magic);
printf("%x\n", super_block.s_log_block_size);
block_size = 4096 << super_block.s_log_block_size;
/* calculate number of block groups on the disk */
unsigned int group_count =
1 + (super_block.s_blocks_count - 1) / super_block.s_blocks_per_group;
/* calculate size of the group descriptor list in bytes */
unsigned int descr_list_size =
group_count * sizeof (struct ext2_group_desc);
struct ext2_group_desc group_descr;
/* position head above the group descriptor block */
lseek(fd, 1024 + block_size, SEEK_SET);
read(fd, &group_descr, sizeof (group_descr));
/* location of the super-block in the first group */
#define BASE_OFFSET 1024
#define BLOCK_OFFSET(block) (BASE_OFFSET + (block-1)*block_size)
struct ext2_super_block super;
struct ext2_group_desc group;
unsigned char *bitmap, bits[8];
/* the super block */
/* the group descritopr */
/* ... [read superblock and group descriptor] ... */
bitmap = (unsigned char*) malloc(block_size);
/* allocate memory for the bitmap */
lseek(fd, BLOCK_OFFSET(group.bg_block_bitmap), SEEK_SET);
read(fd, bitmap, block_size);
/* read bitmap from disk */
unsigned char* bla = (unsigned char*) bitmap;
printf("bitmap: ");
for (int i = 0; i < block_size; i++) {
printf("%x", &bitmap[i]);
}
free(bitmap);
printf("%x\n", block_size);
close(fd);
return 0;
}
i'm a little doubtfull about this code.how can I be sure about the result.I'm running it on ubuntu 12.4 ex4 file system.
EDIT : I've made a raw disk image and tried to read it but get the error that it's not a EXtended2 file system?
this is the address which i used :
fd = open("/dev/zero", O_RDONLY);