/* user main function, called by AppFramework after system init done && wifi * station on in user_main thread. */ OSStatus user_main( app_context_t * const app_context ) { OSStatus err = kNoErr; int time_sencond = 50*1000; /* 60s */ require(app_context, exit); net_init(app_context); /* Create a new thread */ err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "RGB_LED", LED_handleThread, 1024, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the RGB LED thread ." ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "MP3_PLAY", MP3_handleThread, 1024, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the MP3 PLAY thread" ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "BAT_DETECT", BAT_handleThread, 500, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the BAT DETECT thread ." ); KEY_Init(KEY_irq_handler); //按键初始化 mico_rtos_init_semaphore(&cupTimeObj.playMp3_sem, 1); //信号量初始化 mico_rtos_init_semaphore(&cupTimeObj.playLed_sem, 1); mico_rtos_init_semaphore(&cupTimeObj.stopLed_sem, 1); err = mico_init_timer(&cupTimeObj.cup_timer, time_sencond, cup_timer_timeout_handler, (void *)&cupTimeObj); cupTimeObj.drinkTime = 1; cupTimeObj.playMode = PLAY_MP3_LED; if (KEY_getValue() == KEY_DOWN) { TIMER_start(); //启动定时喝水 } while(1) { // printf("this is main thread.\r\n"); //net_test(app_context); mico_thread_sleep(10); } exit: if(kNoErr != err) { printf("ERROR: user_main thread exit with err=%d", err); } mico_rtos_delete_thread(NULL); return kNoErr; }
int application_start( void ) { OSStatus err = kNoErr; mico_thread_t t_handler = NULL; /* Create a new thread */ err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "Thread 1", thread_1, 500, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the thread 1." ); while(1){ /* Create a new thread, and this thread will delete its self and clean its resource */ err = mico_rtos_create_thread( &t_handler, MICO_APPLICATION_PRIORITY, "Thread 2", thread_2, 500, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the thread 2." ); mico_rtos_thread_join( &t_handler ); } exit: if( err != kNoErr ) os_thread_log( "Thread exit with err: %d", err ); mico_rtos_delete_thread(NULL); return err; }
OSStatus airkiss_discovery_start( char *appid, char *deviceid ) { OSStatus err = kNoErr; _appid = appid; _deviceid = deviceid; err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "ak discovery", ak_discovery_thread, 0x800, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the airkiss discovery thread." ); _started = true; exit: return err; }
OSStatus mico_system_monitor_daemen_start( void ) { OSStatus err = kNoErr; /*Start system monotor thread*/ err = MICOStartSystemMonitor( ); require_noerr_string( err, exit, "ERROR: Unable to start the system monitor." ); /* Register first monitor */ err = mico_system_monitor_register(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000/2, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); exit: return err; }
int application_start( void ) { OSStatus err = kNoErr; /*Register user function for MiCO nitification: WiFi status changed */ err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler, NULL ); require_noerr( err, exit ); /* Start MiCO system functions according to mico_config.h */ err = mico_system_init( mico_system_context_init( 0 ) ); require_noerr( err, exit ); /* Start TCP server listener thread*/ err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "TCP_server", tcp_server_thread, 0x800, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the tcp server thread." ); exit: mico_rtos_delete_thread( NULL ); return err; }
int application_start( void ) { OSStatus err = kNoErr; /*Register user function for MiCO nitification: WiFi status changed */ err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler, NULL ); require_noerr( err, exit ); /* Start MiCO system functions according to mico_config.h */ err = mico_system_init( mico_system_context_init( 0 ) ); require_noerr( err, exit ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp_unicast", udp_unicast_thread, 0x800, NULL ); require_noerr_string( err, exit, "ERROR: Unable to start the UDP thread." ); exit: if( err != kNoErr ) udp_unicast_log("Thread exit with err: %d", err); mico_rtos_delete_thread( NULL ); return err; }
int main( int argc, char* argv[] ) { DNSStatus err; // Set up DNS Services and install a Console Control Handler to handle things like control-c signals. err = DNSServicesInitialize( kDNSFlagAdvertise, 0 ); require_noerr_string( err, exit, "could not initialize DNSServiceTest" ); #if( __MACH__ ) signal( SIGINT, SigIntHandler ); #endif #if( defined( WINVER ) ) SetConsoleCtrlHandler( ConsoleControlHandler, TRUE ); #endif ProcessArgs( argc, argv ); exit: DNSServicesFinalize(); return( err ); }
int HKSecureSocketSend( int sockfd, void *buf, size_t len, security_session_t *session) { OSStatus err = kNoErr; uint8_t* encryptedData = NULL; ssize_t sentLength = 0; uint64_t encryptedDataLen; encryptedData = malloc(len + crypto_aead_chacha20poly1305_ABYTES + 4); require_action(encryptedData, exit, err = kNoMemoryErr); *(int *)encryptedData = len; err = crypto_aead_chacha20poly1305_encrypt(encryptedData+4, &encryptedDataLen, (uint8_t *)buf, len, (const uint8_t *)encryptedData, 4, NULL, (uint8_t *)(&session->outputSeqNo), (const unsigned char *)session->OutputKey); session->outputSeqNo++; require_noerr_string(err, exit, "crypto_aead_chacha20poly1305_encrypt failed"); require_string(encryptedDataLen - crypto_aead_chacha20poly1305_ABYTES == len, exit, "encryptedDataLen is not properly set"); err = SocketSend( sockfd, encryptedData, encryptedDataLen + 4 ); require_noerr( err, exit ); exit: if(encryptedData) free(encryptedData); return sentLength; }
static int ProcessArgs( int argc, char* argv[] ) { DNSStatus err; int i; const char * name; const char * type; const char * domain; uint16_t port; const char * text; size_t textSize; DNSBrowserRef browser; DNSResolverFlags resolverFlags; DNSDomainRegistrationType domainType; const char * label; const char * host; const char * ip; unsigned int b[ 4 ]; DNSNetworkAddress addr; dns_service_discovery_ref emulatedRef; // Parse the command line arguments (ignore first argument since it's just the program name). require_action_string( argc >= 2, exit, err = kDNSBadParamErr, "no arguments specified" ); for( i = 1; i < argc; ++i ) { if( strcmp( argv[ i ], "-bbd" ) == 0 ) { // 'b'rowse for 'b'rowsing 'd'omains fprintf( stdout, "browsing for browsing domains\n" ); err = DNSBrowserCreate( 0, BrowserCallBack, NULL, &browser ); require_noerr_string( err, exit, "create browser failed" ); err = DNSBrowserStartDomainSearch( browser, 0 ); require_noerr_string( err, exit, "start domain search failed" ); } else if( strcmp( argv[ i ], "-brd" ) == 0 ) { // 'b'rowse for 'r'egistration 'd'omains fprintf( stdout, "browsing for registration domains\n" ); err = DNSBrowserCreate( 0, BrowserCallBack, NULL, &browser ); require_noerr_string( err, exit, "create browser failed" ); err = DNSBrowserStartDomainSearch( browser, kDNSBrowserFlagRegistrationDomainsOnly ); require_noerr_string( err, exit, "start domain search failed" ); } else if( strcmp( argv[ i ], "-bs" ) == 0 ) { // 'b'rowse for 's'ervices <type> <domain> require_action_string( argc > ( i + 2 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; type = argv[ i++ ]; domain = argv[ i ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "browsing for \"%s.%s\"\n", type, domain ); err = DNSBrowserCreate( 0, BrowserCallBack, NULL, &browser ); require_noerr_string( err, exit, "create browser failed" ); err = DNSBrowserStartServiceSearch( browser, kDNSBrowserFlagAutoResolve, type, domain ); require_noerr_string( err, exit, "start service search failed" ); } else if( strcmp( argv[ i ], "-lsi" ) == 0 ) { // 'l'ookup 's'ervice 'i'nstance <name> <type> <domain> require_action_string( argc > ( i + 3 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; name = argv[ i++ ]; type = argv[ i++ ]; domain = argv[ i ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "resolving \"%s.%s.%s\"\n", name, type, domain ); resolverFlags = kDNSResolverFlagOnlyIfUnique | kDNSResolverFlagAutoReleaseByName; err = DNSResolverCreate( resolverFlags, name, type, domain, ResolverCallBack, 0, NULL, NULL ); require_noerr_string( err, exit, "create resolver failed" ); } else if( ( strcmp( argv[ i ], "-rdb" ) == 0 ) || ( strcmp( argv[ i ], "-rdbd" ) == 0 ) ) { // 'r'egister 'd'omain for 'b'rowsing ['d'efault] <domain> require_action_string( argc > ( i + 1 ), exit, err = kDNSBadParamErr, "missing arguments" ); if( strcmp( argv[ i ], "-rdb" ) == 0 ) { domainType = kDNSDomainRegistrationTypeBrowse; label = ""; } else { domainType = kDNSDomainRegistrationTypeBrowseDefault; label = "default "; } ++i; domain = argv[ i ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "registering \"%s\" as %sbrowse domain\n", domain, label ); err = DNSDomainRegistrationCreate( 0, domain, domainType, NULL ); require_noerr_string( err, exit, "create domain registration failed" ); } else if( ( strcmp( argv[ i ], "-rdr" ) == 0 ) || ( strcmp( argv[ i ], "-rdrd" ) == 0 ) ) { // 'r'egister 'd'omain for 'r'egistration ['d'efault] <domain> require_action_string( argc > ( i + 1 ), exit, err = kDNSBadParamErr, "missing arguments" ); if( strcmp( argv[ i ], "-rdr" ) == 0 ) { domainType = kDNSDomainRegistrationTypeRegistration; label = ""; } else { domainType = kDNSDomainRegistrationTypeRegistrationDefault; label = "default "; } ++i; domain = argv[ i ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "registering \"%s\" as %sregistration domain\n", domain, label ); err = DNSDomainRegistrationCreate( 0, domain, domainType, NULL ); require_noerr_string( err, exit, "create domain registration failed" ); } else if( strcmp( argv[ i ], "-rs" ) == 0 ) { // 'r'egister 's'ervice <name> <type> <domain> <port> <txt> require_action_string( argc > ( i + 5 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; name = argv[ i++ ]; type = argv[ i++ ]; domain = argv[ i++ ]; port = (uint16_t) atoi( argv[ i++ ] ); text = argv[ i ]; textSize = strlen( text ); if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "registering service \"%s.%s.%s\" port %d text \"%s\"\n", name, type, domain, port, text ); err = DNSRegistrationCreate( 0, name, type, domain, (DNSPort) port, text, (DNSCount) textSize, NULL, NULL, RegistrationCallBack, NULL, NULL ); require_noerr_string( err, exit, "create registration failed" ); } else if( strcmp( argv[ i ], "-rps" ) == 0 ) { DNSHostRegistrationFlags hostFlags; // 'r'egister 'p'roxy 's'ervice <host> <ip> <name> <type> <domain> <port> <txt> require_action_string( argc > ( i + 7 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; host = argv[ i++ ]; ip = argv[ i++ ]; name = argv[ i++ ]; type = argv[ i++ ]; domain = argv[ i++ ]; port = (uint16_t) atoi( argv[ i++ ] ); text = argv[ i ]; textSize = strlen( text ); if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } sscanf( ip, "%u.%u.%u.%u", &b[ 0 ], &b[ 1 ], &b[ 2 ], &b[ 3 ] ); addr.addressType = kDNSNetworkAddressTypeIPv4; addr.u.ipv4.addr.v8[ 0 ] = (DNSUInt8) b[ 0 ]; addr.u.ipv4.addr.v8[ 1 ] = (DNSUInt8) b[ 1 ]; addr.u.ipv4.addr.v8[ 2 ] = (DNSUInt8) b[ 2 ]; addr.u.ipv4.addr.v8[ 3 ] = (DNSUInt8) b[ 3 ]; fprintf( stdout, "registering proxy service \"%s.%s.%s\" port %d text \"%s\"\n", name, type, domain, port, text ); hostFlags = kDNSHostRegistrationFlagOnlyIfNotFound | kDNSHostRegistrationFlagAutoRenameOnConflict; err = DNSHostRegistrationCreate( hostFlags, host, domain, &addr, NULL, HostRegistrationCallBack, NULL, NULL ); require_noerr_string( err, exit, "create host registration failed" ); err = DNSRegistrationCreate( 0, name, type, domain, (DNSPort) port, text, (DNSCount) textSize, host, NULL, RegistrationCallBack, NULL, NULL ); require_noerr_string( err, exit, "create registration failed" ); } else if( strcmp( argv[ i ], "-rnss" ) == 0 ) { // 'r'egister 'n'o 's'uch 's'ervice <name> <type> <domain> require_action_string( argc > ( i + 3 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; name = argv[ i++ ]; type = argv[ i++ ]; domain = argv[ i++ ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "registering no-such-service \"%s.%s.%s\"\n", name, type, domain ); err = DNSNoSuchServiceRegistrationCreate( 0, name, type, domain, NULL, RegistrationCallBack, NULL, NULL ); require_noerr_string( err, exit, "create no-such-service registration failed" ); } else if( strcmp( argv[ i ], "-ebs" ) == 0 ) { // 'e'mulated 'b'rowse for 's'ervices <type> <domain> require_action_string( argc > ( i + 2 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; type = argv[ i++ ]; domain = argv[ i ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "emulated browsing for \"%s.%s\"\n", type, domain ); emulatedRef = DNSServiceBrowserCreate( type, domain, EmulatedBrowserCallBack, NULL ); require_action_string( emulatedRef, exit, err = kDNSUnknownErr, "create emulated browser failed" ); } else if( strcmp( argv[ i ], "-ebd" ) == 0 ) { int registrationOnly; // 'e'mulated 'b'rowse for 'd'omains <registration/browse> require_action_string( argc > ( i + 1 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; type = argv[ i++ ]; if( strcmp( type, "registration" ) == 0 ) { registrationOnly = 1; } else if( strcmp( type, "browse" ) == 0 ) { registrationOnly = 0; } else { require_action_string( 0, exit, err = kDNSBadParamErr, "invalid browse type" ); } fprintf( stdout, "emulated browsing for %s domains\n", type ); emulatedRef = DNSServiceDomainEnumerationCreate( registrationOnly, EmulatedDomainEnumerationCallBack, NULL ); require_action_string( emulatedRef, exit, err = kDNSUnknownErr, "create emulated domain browser failed" ); } else if( strcmp( argv[ i ], "-elsi" ) == 0 ) { // 'e'mulated 'l'ookup 's'ervice 'i'nstance <name> <type> <domain> require_action_string( argc > ( i + 3 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; name = argv[ i++ ]; type = argv[ i++ ]; domain = argv[ i ]; if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "emulated resolving \"%s.%s.%s\"\n", name, type, domain ); emulatedRef = DNSServiceResolverResolve( name, type, domain, EmulatedResolverCallBack, NULL ); require_action_string( emulatedRef, exit, err = kDNSUnknownErr, "create emulated resolver failed" ); } else if( strcmp( argv[ i ], "-ers" ) == 0 ) { // 'e'mulated 'r'egister 's'ervice <name> <type> <domain> <port> <txt> require_action_string( argc > ( i + 5 ), exit, err = kDNSBadParamErr, "missing arguments" ); ++i; name = argv[ i++ ]; type = argv[ i++ ]; domain = argv[ i++ ]; port = (uint16_t) atoi( argv[ i++ ] ); text = argv[ i ]; textSize = strlen( text ); if( ( domain[ 0 ] == '\0' ) || ( ( domain[ 0 ] == '.' ) && ( domain[ 1 ] == '\0' ) ) ) { domain = "local."; } fprintf( stdout, "registering service \"%s.%s.%s\" port %d text \"%s\"\n", name, type, domain, port, text ); emulatedRef = DNSServiceRegistrationCreate( name, type, domain, htons( port ), text, EmulatedRegistrationCallBack, NULL ); require_action_string( emulatedRef, exit, err = kDNSUnknownErr, "create emulated registration failed" ); } else if( ( argv[ i ][ 0 ] == '-' ) && isdigit( argv[ i ][ 1 ] ) ) { // Preset ProcessPreset( atoi( &argv[ i ][ 1 ] ) ); err = 0; goto exit; } else if( strcmp( argv[ i ], "-q" ) == 0 ) { // Quiet (no text records) gPrintTXTRecords = 0; } else if( ( strcmp( argv[ i ], "-help" ) == 0 ) || ( strcmp( argv[ i ], "-h" ) == 0 ) ) { // Help Usage(); err = 0; goto exit; } else { // Unknown parameter. require_action_string( 0, exit, err = kDNSBadParamErr, "unknown parameter" ); goto exit; } } // Run until control-C'd. #if( __MACH__ ) CFRunLoopRun(); #endif #if( defined( WINVER ) ) while( !gQuit ) { Sleep( 200 ); } #endif err = kDNSNoErr; exit: if( err ) { Usage(); } return( err ); }
//------------------------------------- API ------------------------------------ OSStatus micokit_STmems_init(void) { OSStatus err = kUnknownErr; #if defined(MICOKIT_STMEMS_KEY1)||defined(MICOKIT_STMEMS_KEY2) button_init_t init; #endif //init RGB LED(P9813) rgb_led_init(); rgb_led_close(); // off dc_motor_init(); dc_motor_set(0); // off // init OLED OLED_Init(); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_1, (uint8_t*)MODEL); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, (uint8_t*)"MiCO "); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, (uint8_t*)" Starting... "); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_4, (uint8_t*)" "); #ifdef MICOKIT_STMEMS_KEY1 init.gpio = MICOKIT_STMEMS_KEY1; init.pressed_func = micokit_STmems_key1_clicked_callback; init.long_pressed_func = NULL; init.long_pressed_timeout = 5000; button_init( IOBUTTON_USER_1, init); #endif #ifdef MICOKIT_STMEMS_KEY2 init.gpio = MICOKIT_STMEMS_KEY2; init.pressed_func = micokit_STmems_key2_clicked_callback; init.long_pressed_func = NULL; init.long_pressed_timeout = 5000; button_init( IOBUTTON_USER_2, init); #endif /*init HTS221 */ err = hts221_sensor_init(); require_noerr_string( err, exit, "ERROR: Unable to Init HTS221" ); /*init UVIS25 */ err = uvis25_sensor_init(); require_noerr_string( err, exit, "ERROR: Unable to Init UVIS25" ); /*init LSM9DS1_ACC_GYR */ err = lsm9ds1_acc_gyr_sensor_init(); require_noerr_string( err, exit, "ERROR: Unable to Init LSM9DS1_ACC_GYR" ); err = lsm9ds1_mag_sensor_init(); require_noerr_string( err, exit, "ERROR: Unable to Init LSM9DS1_MAG" ); /*init LPS25HB */ err = lps25hb_sensor_init(); require_noerr_string( err, exit, "ERROR: Unable to Init LPS25HB" ); light_sensor_init(); exit: return err; }