|
| 1 | +/************************************************************************ |
| 2 | + * NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes” |
| 3 | + * |
| 4 | + * Copyright (c) 2020 United States Government as represented by the |
| 5 | + * Administrator of the National Aeronautics and Space Administration. |
| 6 | + * All Rights Reserved. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | + * not use this file except in compliance with the License. You may obtain |
| 10 | + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + ************************************************************************/ |
| 18 | + |
| 19 | +/* |
| 20 | + * File: bsp_console.c |
| 21 | + * |
| 22 | + * Purpose: |
| 23 | + * OSAL BSP debug console abstraction |
| 24 | + */ |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <string.h> |
| 29 | +#include <unistd.h> |
| 30 | +#include <sys/types.h> |
| 31 | +#include <sys/wait.h> |
| 32 | + |
| 33 | +#include "generic_qnx_bsp_internal.h" |
| 34 | +#include "bsp-impl.h" |
| 35 | + |
| 36 | +/*---------------------------------------------------------------- |
| 37 | + OS_BSP_ExecTput() |
| 38 | +
|
| 39 | + Helper function: Use the system "tput" utility to set the given |
| 40 | + console capability. |
| 41 | +
|
| 42 | + This uses a fork/exec to invoke the external command which outputs |
| 43 | + the control sequence directly to the controlling terminal. |
| 44 | +
|
| 45 | + It is assumed that this will only be used during debug/testing. |
| 46 | + Otherwise it would be preferable to cache the control strings to |
| 47 | + avoid repetitive fork/exec operations. |
| 48 | + ------------------------------------------------------------------*/ |
| 49 | +static void OS_BSP_ExecTput(const char *cap, const char *param) |
| 50 | +{ |
| 51 | + pid_t cpid; |
| 52 | + int status; |
| 53 | + |
| 54 | + cpid = fork(); |
| 55 | + if (cpid < 0) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + if (cpid == 0) |
| 60 | + { |
| 61 | + execlp("tput", "tput", cap, param, NULL); |
| 62 | + exit(EXIT_FAILURE); |
| 63 | + } |
| 64 | + waitpid(cpid, &status, 0); |
| 65 | +} |
| 66 | + |
| 67 | +/**************************************************************************************** |
| 68 | + BSP CONSOLE IMPLEMENTATION FUNCTIONS |
| 69 | + ****************************************************************************************/ |
| 70 | + |
| 71 | +/*---------------------------------------------------------------- |
| 72 | + OS_BSP_ConsoleOutput_Impl |
| 73 | + See full description in header |
| 74 | + ------------------------------------------------------------------*/ |
| 75 | +void OS_BSP_ConsoleOutput_Impl(const char *Str, size_t DataLen) |
| 76 | +{ |
| 77 | + ssize_t WriteLen; |
| 78 | + |
| 79 | + while (DataLen > 0) |
| 80 | + { |
| 81 | + /* writes the raw data directly to STDOUT_FILENO (unbuffered) */ |
| 82 | + WriteLen = write(STDOUT_FILENO, Str, DataLen); |
| 83 | + if (WriteLen <= 0) |
| 84 | + { |
| 85 | + /* no recourse if this fails, just stop. */ |
| 86 | + break; |
| 87 | + } |
| 88 | + Str += WriteLen; |
| 89 | + DataLen -= WriteLen; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/*---------------------------------------------------------------- |
| 94 | + OS_BSP_ConsoleSetMode_Impl() definition |
| 95 | + See full description in header |
| 96 | + ------------------------------------------------------------------*/ |
| 97 | +void OS_BSP_ConsoleSetMode_Impl(uint32 ModeBits) |
| 98 | +{ |
| 99 | + char param[32]; |
| 100 | + |
| 101 | + if (OS_BSP_GenericQnxGlobal.EnableTermControl) |
| 102 | + { |
| 103 | + if (ModeBits == OS_BSP_CONSOLEMODE_NORMAL) |
| 104 | + { |
| 105 | + OS_BSP_ExecTput("sgr0", NULL); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + if ((ModeBits & OS_BSP_CONSOLEMODE_HIGHLIGHT) == 0) |
| 110 | + { |
| 111 | + /* no highlight (standout) text */ |
| 112 | + OS_BSP_ExecTput("rmso", NULL); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + /* set highlight (standout) text */ |
| 117 | + OS_BSP_ExecTput("smso", NULL); |
| 118 | + } |
| 119 | + |
| 120 | + snprintf(param, sizeof(param), "%d", OS_BSP_CONSOLEMODE_TO_ANSICOLOR(ModeBits)); |
| 121 | + OS_BSP_ExecTput("setaf", param); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments