objc

examples/Network/ntp/main.m
#ifndef WIFI_SSID
#error "WIFI_SSID not defined"
#endif
#ifndef WIFI_PASSWORD
#error "WIFI_PASSWORD not defined"
#endif
@interface MyAppDelegate : NXObject <ApplicationDelegate, WirelessDelegate,
NXTimer *_timer;
}
@end
@implementation MyAppDelegate
- (void)dealloc {
[_timer release];
_timer = nil;
// Call superclass dealloc
[super dealloc];
}
- (void)applicationDidFinishLaunching:(id)application {
// Initialize the wireless manager
if (wifi == nil) {
NXLog(@"WiFi not supported on this board");
[application terminateWithExitStatus:-1];
return;
}
// Set the delegate for wireless events
[wifi setDelegate:self];
// Initialise the network time manager
[[NXNetworkTime sharedInstance] setDelegate:self];
// Determine network to connect to
sys_assert(net);
// Connect to the network
if ([wifi connect:net withPassword:@WIFI_PASSWORD] == NO) {
NXLog(@"Failed to initiate connection to network: %@", net);
}
// Set up a timer which periodically reports the time
_timer = [[NXTimer timerWithInterval:5 * Second repeats:YES] retain];
[_timer setDelegate:self];
}
- (void)applicationReceivedSignal:(NXApplicationSignal)signal {
// Handle the received signal
NXLog(@"Application received signal: %d", (int)signal);
// Terminate the application
}
- (void)connectDidStart:(NXWirelessNetwork *)network {
NXLog(@"Connection started to network: %@", network);
}
- (void)connectionFailed:(NXWirelessNetwork *)network
withError:(NXWirelessError)error {
NXLog(@"Connection failed to network: %@, error: %d", network, (int)error);
switch (error) {
NXLog(@" >>>> Bad authentication");
break;
NXLog(@" >>>> Network not found");
break;
NXLog(@" >>>> General error");
break;
}
}
- (void)connected:(NXWirelessNetwork *)network {
NXLog(@"Connected to network: %@", network);
}
- (void)disconnected:(NXWirelessNetwork *)network {
NXLog(@"Disconnected from network: %@", network);
}
- (BOOL)networkTimeShouldUpdate:(NXDate *)time {
// Handle the network time update
NXLog(@"Network time updated: %@", time);
return YES;
}
- (void)timerFired:(NXTimer *)timer {
NXDate *now = [NXDate date];
NXLog(@"Current time: %@", now);
}
@end
int main(int argc, char *argv[]) {
// Initialize the NXApplication framework
return NXApplicationMain(argc, argv, [MyAppDelegate class],
}