I have a jailbroken Iphone 4 running iOS 5.0.1 and I'm trying to send serial data through the Dock Connector to my Arduino. I have successful sent data to the Arduino using Minicom but I can't seem to get it working in an app. This is my code and the error is at the bottom.
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#import "ViewController.h"
static struct termios gOriginalTTYAttrs;
static int OpenSerialPort(void);
#implementation ViewController
#synthesize dataLabel, startData;
#synthesize timer;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(IBAction)startSerial:(id)sender{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(targetMethod:) userInfo:nil repeats:YES];
}
static int OpenSerialPort()
{
int fileDescriptor = -1;
int handshake;
struct termios options;
// Open the serial port read/write, with no controlling terminal, and don't wait for a connection.
// The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking.
// See open(2) ("man 2 open") for details.
fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NDELAY);
NSLog(#"%d",fileDescriptor);
if (fileDescriptor == -1)
{
printf("Error opening serial port %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
// unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
// processes.
// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.
if (ioctl(fileDescriptor, TIOCEXCL) == -1)
{
printf("Error setting TIOCEXCL on %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
// See fcntl(2) ("man 2 fcntl") for details.
if (fcntl(fileDescriptor, F_SETFL, 0) == -1)
{
printf("Error clearing O_NONBLOCK %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Get the current options and save them so we can restore the default settings later.
if (tcgetattr(fileDescriptor, &gOriginalTTYAttrs) == -1)
{
printf("Error getting tty attributes %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// The serial port attributes such as timeouts and baud rate are set by modifying the termios
// structure and then calling tcsetattr() to cause the changes to take effect. Note that the
// changes will not become effective without the tcsetattr() call.
// See tcsetattr(4) ("man 4 tcsetattr") for details.
options = gOriginalTTYAttrs;
// Print the current input and output baud rates.
// See tcsetattr(4) ("man 4 tcsetattr") for details.
printf("Current input baud rate is %d\n", (int) cfgetispeed(&options));
printf("Current output baud rate is %d\n", (int) cfgetospeed(&options));
// Set raw input (non-canonical) mode, with reads blocking until either a single character
// has been received or a one second timeout expires.
// See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.
cfmakeraw(&options);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 10;
// The baud rate, word length, and handshake options can be set as follows:
cfsetspeed(&options, B19200); // Set 19200 baud
options.c_cflag |= (CS8); // RTS flow control of input
printf("Input baud rate changed to %d\n", (int) cfgetispeed(&options));
printf("Output baud rate changed to %d\n", (int) cfgetospeed(&options));
// Cause the new options to take effect immediately.
if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1)
{
printf("Error setting tty attributes %s - %s(%d).\n",
"/dev/tty.iap", strerror(errno), errno);
goto error;
}
// Success
return fileDescriptor;
// Failure "/dev/tty.iap"
error:
if (fileDescriptor != -1)
{
close(fileDescriptor);
}
return -1;
}
-(void) targetMethod: (NSTimer *) theTimer {
//dataLabel.text = #"timer running";
int fd;
char somechar[8];
fd=OpenSerialPort(); // Open tty.iap with no hardware control, 8 bit, BLOCKING and at 19200 baud
NSLog(#"%d",fd);
dataLabel.text = [NSString stringWithFormat:#"%d",fd];
if(fd>-1)
{
// dataLabel.text = #"got port";
write(fd,"*",1); // Write handshaking message over serial
///////////////////////////////////////////////////////////////////////////////////////////////////
// After this, our device or our PC program should be strobing serial ground to gain access to the Iphone Serial Line
//////////////////////////////////////////////////////////////////////////////////////////////////
read(fd,&somechar[0],1); // Read 1 byte over serial. This will block (wait) untill the byte has been received
if(somechar[0]=='*') // Check if this byte is a "handshaking" message
{
dataLabel.text = #"handshake accepted";
printf("Serial connection established!\n"); // If it is, we have established a connection to the device and can freely read/write over serial!
while(true) // Do this forever or untill someone presses CTRL+C
{
dataLabel.text = #"in the loop";
read(fd,&somechar[0],1); // Read a character over serial!
dataLabel.text = [NSString stringWithFormat:#"%#",somechar[0]]; // Write the character to the Terminal!!
}
}
}
}
#end
But the error i'm getting is:
2012-03-01 19:58:03.507 Iphone-Serial[2470:707] -1
Error opening serial port /dev/tty.iap - Resource busy(16).
Should I restart my phone? or somehow close the serial port?
Thanks,
Andrew
Your app must have root permission to access tty.iap. Simply ssh to your device(as root) and chmod 777 YourApp.app may fix the problem. Just start with simpler code, get response and extend more and more.
Related
I'm very new to iOS coding and I've been practicing with little app examples found online.
I'm reading examples in the "iOS Sensor Apps with Arduino" book and I've been following instructions for the serial communication example with the Adruino UNO board using the Redpark serial cable for the iPhone/iPad
I have the Redpark serial cable and their demo app, and a registered working developer profile. I've been able to communicate over serial with the demo app on the iPhone and the serial port on my computer.
In working with the example from the book and I've copied in this line to the iOS code:
bytesRead = [rscMgr read:rxBuffer length:numBytes];
However, when compiling this with the rest of the code it throws an error described as
Use of undeclared identifier "if"
I've looked everywhere for the word "if" and can't find it. I've also looked at the sample code from Redpark and the is similar if not identical, except for the buffer name.
Please let me know if I've provided enough detail and I'm forward to any advice this community may have, thanks again for offering your time.
Bellow is more of the code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
rscMgr = [[RscMgr alloc] init];
[rscMgr setDelegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sendString:(id)sender {
[self.textEntry resignFirstResponder];
//Add code here
}
#pragma mark - RscMgrDelegate methods
- (void)cableConnected:(NSString *)protocol {
[rscMgr setBaud:9600];
[rscMgr open];
}
- (void)cableDisconnected {
}
- (void)portStatusChanged {
- (void) readBytesAvailable:(UInt32)numBytes {
int bytesRead;
// Read the data out
**bytesRead = [rscMgr read:rxBuffer length:numBytes];**
NSLog( #"Read %d bytes from serial cable.", bytesRead);
for(int i = 0;i < numBytes;++i) {
lf.serialView.text =
[NSString stringWithFormat:#"%#%c",
self.textView.text,
((char *)rxBuffer)[i]];
}
}
- (BOOL) rscMessageReceived:(UInt8 *)msg TotalLength:(int)len {
return FALSE;
}
- (void) didReceivePortConfig {
}
#end
I'm currently trying to create an iOS audio project and I need to use the CARingBuffer class available in the Extras/CoreAudio/PublicUtility folder of XCode.
The problem is when I include the CARingBuffer.h in the header of my viewController and I declare a CARingBuffer object, I receive 4 compile errors.
To reproduce my problem it's pretty simple. Just create a new view based application and try to #include "CARingBuffer.h" in the header of your viewController.
Here's the content of my testViewController.h :
#import <UIKit/UIKit.h>
#include "CARingBuffer.h"
#interface testViewController : UIViewController {
}
#end
Here's the content of my testViewController.m :
#import "testViewController.h"
#implementation testViewController
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Bellow the 4 compile errors located (strangely) in the CARingBuffer according to XCode 4 :
1) Initializer element is not a constant on line :
const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;
2) Expected ';' after top level declarator, Expected '='... or 'atribute' before 'CARingBuffer' :
class CARingBuffer {
3) Initializer element is not a constant on line :
const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;
4) Expected ';' after top level declarator, Expected '='... or 'atribute' before 'CARingBuffer' :
class CARingBuffer {
Thanks in advance for your help.
Also take a look at this alternative
You need to rename the class that you are including the ring buffer in to be a .mm file.
This tells the compiler to use objective c++ .
You need change your testViewController.m to testViewController.mm because CARingBuffer is c++ class.
About how to use it, here is an extend of CARingBuffer : CARingBufferEx
//header file
#include "CARingBuffer.h"
class CARingBufferEx : public CARingBuffer {
public:
CARingBufferEx();
~CARingBufferEx();
CARingBufferError Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);
CARingBufferError Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);
private:
SInt64 firstInputSampleTime;
SInt64 firstOutputSampleTime;
SInt64 offset;
};
//Class
#include "CARingBufferEx.h"
#include "stdio.h"
CARingBufferEx::CARingBufferEx():firstInputSampleTime(-1), firstOutputSampleTime(-1), offset(0) {
}
CARingBufferEx::~CARingBufferEx() {
}
CARingBufferError CARingBufferEx::Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber) {
if (firstInputSampleTime < 0) {
firstInputSampleTime = frameNumber;
if (firstOutputSampleTime > 0 && offset == 0) {
offset = firstInputSampleTime - firstOutputSampleTime;
}
}
return CARingBuffer::Store(abl, nFrames, frameNumber);
}
CARingBufferError CARingBufferEx::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber) {
if (firstOutputSampleTime < 0) {
firstOutputSampleTime = frameNumber;
if (firstInputSampleTime > 0 && offset == 0) {
offset = firstInputSampleTime - firstOutputSampleTime;
}
}
return CARingBuffer::Fetch(abl, nFrames, frameNumber + offset);
}
Usage:
CARingBufferEx* _musicMixerRingBuffer;
_musicMixerRingBuffer = new CARingBufferEx();
_musicMixerRingBuffer->Allocate(2, sizeof(AudioUnitSampleType), 1024 * 50);
//1024 is length for one package. and 50 means this buffer contains 50 packages at most.
//store
//ioData is AudioBufferList ,inTimeStamp is AudioTimeStamp
musicMixerRingBuffer->Store(ioData, inNumberFrames, inTimeStamp->mSampleTime);
//Fetch
musicMixerRingBuffer->Fetch(ioData, inNumberFrames, inTimeStamp->mSampleTime);
(This is a work in progress. I wonder if someone could to improve it)
in Objective C, it's easy to resolve a hostname with NSHost.
[[NSHost hostWithName:#"www.google.com"] address]
Sadly iOS (iPhone) contains only a private version of NSHost.
I found many ways of doing this with other Objects or methods, but all of them got only IPv4 addresses in the results. So here is for the moment the only efficient method I have found.
I first tried to use the asynchronous CFHostStartInfoResolution as did bdunagan, but failed to adapt it to IPv6.
Some of you will appreciate to get a method working, so here is one, but if you know a way which would be Asynchronous I would appreciate to learn about it... cause for the moment I use a Popup to alert about the next freeze that could occur with slow cellular connection
/**
Give the IPs corresponding to a Hostname
Sometime only 1 IPv4 is shown even if there's more.
Sometime only 1 IPv6 is shown even if there's more.
Certainly due to iOS Memory optimisation when locally cached
#author Christian Gonzalvez, http://wiki.gonzofamily.com
#param hostName A hostname
#return an Array of NSString of all the corresponding IP addresses. The first
is the Canonical name, the following are IPs (all NSString)
*/
+ (NSArray *)addressesForHostname:(NSString *)hostname
{
const char* hostnameC = [hostname UTF8String];
struct addrinfo hints, *res;
struct sockaddr_in *s4;
struct sockaddr_in6 *s6;
int retval;
char buf[64];
NSMutableArray *result; //the array which will be return
NSMutableArray *result4; //the array of IPv4, to order them at the end
NSString *previousIP = nil;
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = PF_UNSPEC;//AF_INET6;
hints.ai_flags = AI_CANONNAME;
//AI_ADDRCONFIG, AI_ALL, AI_CANONNAME, AI_NUMERICHOST
//AI_NUMERICSERV, AI_PASSIVE, OR AI_V4MAPPED
retval = getaddrinfo(hostnameC, NULL, &hints, &res);
if (retval == 0)
{
if (res->ai_canonname)
{
result = [NSMutableArray arrayWithObject:[NSString stringWithUTF8String:res->ai_canonname]];
}
else
{
//it means the DNS didn't know this host
return nil;
}
result4= [NSMutableArray array];
while (res) {
switch (res->ai_family){
case AF_INET6:
s6 = (struct sockaddr_in6 *)res->ai_addr;
if(inet_ntop(res->ai_family, (void *)&(s6->sin6_addr), buf, sizeof(buf))
== NULL)
{
NSLog(#"inet_ntop failed for v6!\n");
}
else
{
//surprisingly every address is in double, let's add this test
if (![previousIP isEqualToString:[NSString stringWithUTF8String:buf]]) {
[result addObject:[NSString stringWithUTF8String:buf]];
}
}
break;
case AF_INET:
s4 = (struct sockaddr_in *)res->ai_addr;
if(inet_ntop(res->ai_family, (void *)&(s4->sin_addr), buf, sizeof(buf))
== NULL)
{
NSLog(#"inet_ntop failed for v4!\n");
}
else
{
//surprisingly every address is in double, let's add this test
if (![previousIP isEqualToString:[NSString stringWithUTF8String:buf]]) {
[result4 addObject:[NSString stringWithUTF8String:buf]];
}
}
break;
default:
NSLog(#"Neither IPv4 nor IPv6!");
}
//surprisingly every address is in double, let's add this test
previousIP = [NSString stringWithUTF8String:buf];
res = res->ai_next;
}
}else{
NSLog(#"no IP found");
return nil;
}
return [result arrayByAddingObjectsFromArray:result4];
}
NB: I noticed that most of the time only 1 IPv6 is returned, I suspect it's due to iOS Memory optimisation when locally cached. if you run this method again and again, sometime you have 3 IPv6, but then you have only 1 IPv4.
If you want a method to run on a background thread, the simplest way is to use performSelectorInBackground:withObject:; this is an instance method of NSObject, so any object can use it without any extra work (including, interestingly enough, class objects, which is good in this case because this is a class method):
[[self class] performSelectorInBackground:#selector(addressesForHostName:)
withObject:theHostName];
Inside the method, you will need to set up an autorelease pool for the thread. You will also need some kind of callback method set up to get the return value back to your main thread. Make sure that you don't try to do any GUI activity on the background thread. It's only safe to do that on the main thread.
+ (NSArray *)addressesForHostname:(NSString *)hostname
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Do your stuff...
// Wait until done to allow memory to be managed properly
// If we did not do this, the array might be deallocated
// before the main thread had a chance to retain it
[self performSelectorOnMainThread:#selector(addressesCallback:)
withObject:[result arrayByAddingObjectsFromArray:result4]
waitUntilDone:YES];
// Inside a class method, self refers to the class object.
[pool drain];
}
If you were not on the main thread to begin with, or if you needed more control, you could also look into NSOperation, which is more powerful and therefore requires more work. It's still easier than explicit thread management, though!
Hope that solves your problem. It sounded like you have this method doing what you need, you just need it to not block the main thread.
thanks to Josh I could do it, but here is what I had to do :
Instead of calling directly
self.ipAddressesString = [CJGIpAddress addressesForHostname:#"www.google.com"];
I call
[self resolveNow:#"www.google.com"];
And create 3 new methods:
- (void)resolveNow:(NSString *)hostname
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self performSelectorInBackground:#selector(hostname2ipAddresses:)
withObject:hostname];
}
- (void)hostname2ipAddresses:(NSString *)hostname
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//Here is my previous lonely line !! safely started in an other thread
self.ipAddressesString = [CJGIpAddress addressesForHostname:hostname];
[self performSelectorOnMainThread:#selector(resolutionDidFinish)
withObject:nil
waitUntilDone:YES];
[pool drain];
}
- (void)resolutionDidFinish
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
//My STUFF with self.ipAddressesString (now filled)
}
Edit:
In practice I use all of that in a Model, so I had a crash when I close the View before the end of the resolution
So in the view I added in dealloc what is necessary to avoid a crash
- (void)dealloc
{
self.model.delegate = nil;
[super dealloc];
}
Then - in the model - I test delegate before doing anything with it.
I have a problem where I am not getting a data callback from a CF socket. My application allows the user to set up up to 4 channels of data transfer (I'll call them "trials", since I don't know if channel means something specific.) So far, I am only testing in the simulator, and am only working with UDP. For each trial, I initialize a new instance of my socket driver class (called SimSocket for the simulator case, initialized as publisher or subscriber for send or receive respectively), send/receive a relatively small amount of data, then close the socket and release SimSocket. Then I do another trial, etc, etc.
Here is how it fails: I set up trial #0 as a receiver, then set up trial #1 to send data to 127.0.0.1. I start the receiver, then the sender, and data transfer works fine. I can repeat this over and over, no problems. Then, without changing any IP or port addresses or packet sizes or packet rates, I run just trial 1, so the data goes out to the ether or somewhere. I don't see any issues in my console output when this happens. Next I start the receiver again, trial 0, then run the sender, trial 1. But now I never see any data callbacks, and trial 0 never gets any data to process. My question is, why does trial 0 fail to receive data after that sequence?
Some supporting info.... I run Wireshark on the Mac looking at the loopback interface. Even on the run where trial 0 fails, I still see the UDP datagrams in Wireshark that I would expect to see. For the entire sequence described above, I see in my console output that sockets are always being opened and closed properly (except the last trial 0 that fails - it never gets to the point of closing of course). All receive code is executing on the main thread. When I send a set of data, I do that on a separate thread, but that is the same when things work and when they don't. I can get this failure when sending 10 50-byte 'packets' of raw data at 2 Kbps.
Below are pertinent parts of my SimSocket implementation file. I appreciate any help you can provide. Thanks.
#import "SimSocket.h"
void simReceivedDataCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
// NOTE: I don't know if I'm accessing the address argument properly here, or if it contains useful information!
NSLog(#"SimSocket: simReceivedDataCallback function entry. Socket=%d, address=%s, port=%d", CFSocketGetNative(s), inet_ntoa(((struct sockaddr_in *)address)->sin_addr), htons(((struct sockaddr_in *)address)->sin_port));
// send data to SimSocket delegate here
}
#implementation SimSocket
#synthesize readyToUse, delegate, errors;
- (id)initWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 {
if (self = [super init]) {
hostStr = host1;
port = port1;
isPublish = YES;
connectionType = connectionType1;
readyToUse = NO;
int sockType = (connectionType == SimSocketConnectionTcp) ? SOCK_STREAM : SOCK_DGRAM;
sockfd = socket(AF_INET, sockType, 0);
if (sockfd == -1) {
NSLog(#"SimSocket:initWithType: Error with socket() - %s", strerror(errno));
return self;
}
memset((void *)&address_in, 0, sizeof(address_in));
address_in.sin_family = AF_INET;
address_in.sin_port = htons(port);
address_in.sin_addr.s_addr = inet_addr([hostStr UTF8String]);
addressData = [NSData dataWithBytes:&address_in length:sizeof(address_in)];
readyToUse = YES;
return self;
}
else {
return nil;
}
}
- (id)initPublishWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 {
if ([self initWithType:connectionType1 host:host1 port:port1]) {
readyToUse = NO;
NSLog(#"SimSocket:initPublishWithType: Successfully created BSD socket - %d", sockfd);
cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketConnectCallBack, simCallback, NULL);
if (cfsocket == NULL) {
NSLog(#"SimSocket:initPublishWithType: Error with CFSocketCreateWithNative()");
return self;
}
readyToUse = YES;
}
return self;
}
- (id)initSubscribeWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 delegate:(id <SimSocketEvents>)delegate1 {
NSLog(#"SimSocket:initSubscribeWithType: starting initialization with delegate %#", delegate1);
if ([self initWithType:connectionType1 host:host1 port:port1]) {
readyToUse = NO;
NSLog(#"SimSocket:initSubscribeWithType: Successfully created BSD socket - %d", sockfd);
delegate = delegate1;
address_in.sin_addr.s_addr = htonl(INADDR_ANY);
addressData = [NSData dataWithBytes:&address_in length:sizeof(address_in)];
int yes = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
NSLog(#"SimSocket:initSubscribeWithType: Error with setsockopt() with SO_REUSEADDR - %s", strerror(errno));
}
if (bind(sockfd, (struct sockaddr*) &address_in, sizeof(address_in)) == -1) {
NSLog(#"SimSocket:initSubscribeWithType: Error with bind() - %s", strerror(errno));
errors = [NSString stringWithFormat:#"Bind Failed - %s. Try using a Different Port number.", strerror(errno)];
close (sockfd);
return self;
}
CFSocketContext context = { 0, (void *)self, NULL, NULL, NULL };
// TCP
if (connectionType == SimSocketConnectionTcp) {
if (listen(sockfd, 10) == -1) {
NSLog(#"SimSocket:initSubscribeWithType: Error with listen() - %s", strerror(errno));
return self;
}
cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketAcceptCallBack, simAcceptCallback, &context);
}
// UDP
else if (connectionType == SimSocketConnectionUdp) {
cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketDataCallBack, simReceivedDataCallback, &context);
}
if (cfsocket == NULL) {
NSLog(#"SimSocket:initSubscribeWithType: Error with CFSocketCreateWithNative()");
return self;
}
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cfsocket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
NSLog(#"SimSocket:initSubscribeWithType: Current run loop = %x. Main thread run loop = %x.", CFRunLoopGetCurrent(), CFRunLoopGetMain());
readyToUse = YES;
NSLog(#"SimSocket:initSubscribeWithType: Completed socket initialization successfully.");
}
return self;
}
- (void)dealloc {
if (cfsocket) CFRelease(cfsocket);
if (sockfd != -1) close(sockfd);
[errors release];
[super dealloc];
}
-(void)shutdown
{
NSLog(#"SimSocket: shutdown: Am shutting down the socket. sockfd = %d", sockfd);
if (cfsocket) CFRelease(cfsocket);
if (sockfd != -1) close(sockfd);
}
- (void)sendBytes:(const void *)bytes length:(int)length {
if (!readyToUse) {
NSLog(#"SimSocket:sendBytes: socket not ready to use for sending");
return;
}
int bytesSent;
if (connectionType == SimSocketConnectionTcp) {
bytesSent = send(sockfd, bytes, length, 0);
}
else {
bytesSent = sendto(sockfd, bytes, length, 0, (struct sockaddr *)&address_in, sizeof(address_in));
}
if (bytesSent < 0) {
NSLog(#"SimSocket:sendBytes: Oops, error- %s. No bytes sent.", strerror(errno));
}
else if (bytesSent < length) {
NSLog(#"SimSocket:sendBytes: Oops, error- %s. Only %i sent out of %i", strerror(errno), bytesSent, length);
}
else {
NSLog(#"SimSocket:sendBytes: Successfully sent the packet (%d bytes) to %s", length, inet_ntoa(address_in.sin_addr));
}
NSLog(#"SimSocket:sendBytes: Current run loop = %x. Main thread run loop = %x.", CFRunLoopGetCurrent(), CFRunLoopGetMain());
}
#end
Folks, it looks like I found the problem. When shutting things down, I was only releasing the CFSocket. Following the lead of the UDPEcho sample project, I replaced this with 3 steps: a CFSocketInvalidate call, a CFRelease call, then set the CFSocket reference to NULL. It seems to be working fine now.
The third-party library generates sequential buffers of 16-bit signed stereo samples of any desired size. I can't figure out which framework/functions to use to play from these buffers. I've been working off the example in this answer using AudioQueue but it's obviously incomplete in ways I can't resolve (local variables are used as if they are object members, undeclared variables, etc).
The code in Apple's SpeakHere example project only shows how to use AudioQueue to play audio from a file. Can someone point me in the right direction?
Figured it out. Here's a simple, complete and working white noise generator using AudioQueue created from the Window-based Application project template.
StaticAppDelegate.h:
// StaticAppDelegate.h
#import <UIKit/UIKit.h>
// STATIC ADDITIONS
// Add > Existing Frameworks... > AudioToolbox.framework to your Target
#import <AudioToolbox/AudioToolbox.h>
// END STATIC ADDITIONS
#interface StaticAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
// STATIC ADDITIONS
- (void)startStatic;
- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
buffer:(AudioQueueBufferRef)inBuffer;
// END STATIC ADDITIONS
#end
StaticAppDelegate.m:
// StaticAppDelegate.m
#import "StaticAppDelegate.h"
// STATIC ADDITIONS
#define kNumberBuffers 4
#define kBufferSize 2048
// AudioQueue callback
void AQOutputCallback(
void *inData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer
)
{
StaticAppDelegate *staticApp = (StaticAppDelegate *)inData;
[staticApp handleBufferCompleteForQueue:inAQ buffer:inBuffer];
}
// END STATIC ADDITIONS
#implementation StaticAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
// STATIC ADDITIONS
[self startStatic];
// END STATIC ADDITIONS
}
- (void)dealloc {
[window release];
[super dealloc];
}
// STATIC ADDITIONS
- (void)startStatic
{
srandom(time(NULL));
// initiate audio session
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 category = kAudioSessionCategory_MediaPlayback; // plays through sleep lock and silent switch
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
AudioSessionSetActive(true);
// following is modified from http://stackoverflow.com/questions/1710133/playing-generated-audio-on-an-iphone
// setup queue
AudioQueueRef audioQueue;
AudioQueueBufferRef buffers[kNumberBuffers];
AudioStreamBasicDescription format;
memset(&format, 0, sizeof(format));
format.mSampleRate = 44100;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
format.mChannelsPerFrame = 1;
format.mBitsPerChannel = 16;
format.mBytesPerFrame = (format.mBitsPerChannel / 8) * format.mChannelsPerFrame;
format.mFramesPerPacket = 1;
format.mBytesPerPacket = format.mBytesPerFrame * format.mFramesPerPacket;
AudioQueueNewOutput(&format,
AQOutputCallback,
self,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes,
0,
&audioQueue);
// allocate and fill the buffers
for (int i = 0; i < kNumberBuffers; ++i)
{
AudioQueueAllocateBuffer(audioQueue, kBufferSize, &buffers[i]);
AQOutputCallback(self, audioQueue, buffers[i]);
}
AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, 1.0);
AudioQueueStart(audioQueue, NULL);
}
- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
buffer:(AudioQueueBufferRef)inBuffer
{
inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
int *buffer = (int *)inBuffer->mAudioData;
for (int i = 0; i < inBuffer->mAudioDataByteSize/sizeof(int); ++i)
{
buffer[i] = (int)rand(); // refill the buffer
}
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}
// END STATIC ADDITIONS
#end
Once I figured out how to actually refill the buffer (inBuffer->mAudioData with some type casting) everything else fell into place.