xref: /btstack/platform/wiced/btstack_stdin_wiced.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1b03afde1SMatthias Ringwald /*
2b03afde1SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3b03afde1SMatthias Ringwald  *
4b03afde1SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5b03afde1SMatthias Ringwald  * modification, are permitted provided that the following conditions
6b03afde1SMatthias Ringwald  * are met:
7b03afde1SMatthias Ringwald  *
8b03afde1SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9b03afde1SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10b03afde1SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11b03afde1SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12b03afde1SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13b03afde1SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14b03afde1SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15b03afde1SMatthias Ringwald  *    from this software without specific prior written permission.
16b03afde1SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17b03afde1SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18b03afde1SMatthias Ringwald  *    monetary gain.
19b03afde1SMatthias Ringwald  *
20b03afde1SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21b03afde1SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b03afde1SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25b03afde1SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26b03afde1SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27b03afde1SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28b03afde1SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29b03afde1SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30b03afde1SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b03afde1SMatthias Ringwald  * SUCH DAMAGE.
32b03afde1SMatthias Ringwald  *
33b03afde1SMatthias Ringwald  * Please inquire about commercial licensing options at
34b03afde1SMatthias Ringwald  * [email protected]
35b03afde1SMatthias Ringwald  *
36b03afde1SMatthias Ringwald  */
37b03afde1SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_stdin_wiced.c"
39b03afde1SMatthias Ringwald 
40b03afde1SMatthias Ringwald #include <stdio.h>
41b03afde1SMatthias Ringwald 
42b03afde1SMatthias Ringwald #include "btstack_run_loop_wiced.h"
43b03afde1SMatthias Ringwald #include "btstack_defines.h"
44b03afde1SMatthias Ringwald #include <stdlib.h>
45b03afde1SMatthias Ringwald 
46b03afde1SMatthias Ringwald #include "btstack_stdin.h"
47b03afde1SMatthias Ringwald #include "wiced.h"
48b03afde1SMatthias Ringwald 
49b03afde1SMatthias Ringwald static int activated = 0;
50b03afde1SMatthias Ringwald static wiced_thread_t stdin_reader_thread;
51b03afde1SMatthias Ringwald static void (*stdin_handler)(char c);
52b03afde1SMatthias Ringwald 
stdin_reader_notify(void * p)53b03afde1SMatthias Ringwald static wiced_result_t stdin_reader_notify(void * p){
54b03afde1SMatthias Ringwald     char c = (char)(uintptr_t) p;
55b03afde1SMatthias Ringwald     (*stdin_handler)(c);
56b03afde1SMatthias Ringwald     return WICED_SUCCESS;
57b03afde1SMatthias Ringwald }
58b03afde1SMatthias Ringwald 
59739fcdb2SMatthias Ringwald static btstack_context_callback_registration_t callback_registration;
stdin_reader_thread_process(wiced_thread_arg_t p)60b03afde1SMatthias Ringwald static void stdin_reader_thread_process(wiced_thread_arg_t p){
61b03afde1SMatthias Ringwald     UNUSED(p);
62739fcdb2SMatthias Ringwald     callback_registration.callback = &callback_registration;
63ff3cc4a5SMatthias Ringwald     while (true){
64b03afde1SMatthias Ringwald         uint8_t c = getchar();
65739fcdb2SMatthias Ringwald         callback_registration.context =  (void *)(uintptr_t) c;
66739fcdb2SMatthias Ringwald         btstack_run_loop_execute_code_on_main_thread(&callback_registration);
67b03afde1SMatthias Ringwald     }
68b03afde1SMatthias Ringwald }
69b03afde1SMatthias Ringwald 
btstack_stdin_setup(void (* handler)(char c))70b03afde1SMatthias Ringwald void btstack_stdin_setup(void (*handler)(char c)){
71b03afde1SMatthias Ringwald     if (activated) return;
72b03afde1SMatthias Ringwald     activated = 1;
73b03afde1SMatthias Ringwald     stdin_handler = handler;
74b03afde1SMatthias Ringwald 
75b03afde1SMatthias Ringwald     /* Remove buffering from all std streams */
76b03afde1SMatthias Ringwald     setvbuf( stdin,  NULL, _IONBF, 0 );
77b03afde1SMatthias Ringwald     setvbuf( stdout, NULL, _IONBF, 0 );
78b03afde1SMatthias Ringwald     setvbuf( stderr, NULL, _IONBF, 0 );
79b03afde1SMatthias Ringwald 
80b03afde1SMatthias Ringwald     wiced_rtos_create_thread(&stdin_reader_thread, WICED_APPLICATION_PRIORITY, "STDIN", &stdin_reader_thread_process, 512, NULL);
81b03afde1SMatthias Ringwald }
82b03afde1SMatthias Ringwald 
btstack_stdin_reset(void)83b03afde1SMatthias Ringwald void btstack_stdin_reset(void){
84b03afde1SMatthias Ringwald     if (!activated) return;
85b03afde1SMatthias Ringwald     // TODO: implement
86b03afde1SMatthias Ringwald }
87