001package com.pi4j.io.serial;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SerialPort.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032import com.pi4j.io.gpio.exception.UnsupportedBoardType;
033import com.pi4j.system.SystemInfo;
034
035import java.io.File;
036import java.io.IOException;
037
038public class SerialPort {
039
040    /**
041     * Get the default serial port for the detected platform/model/board revision.
042     *
043     * @return com port device path
044     * @throws IOException
045     * @throws InterruptedException
046     */
047    public static String getDefaultPort() throws IOException, InterruptedException, UnsupportedBoardType {
048        return getDefaultPort(SystemInfo.getBoardType());
049    }
050
051    /**
052     * Get the default serial port for the specified platform/model/board revision.
053     *
054     * @param board hardware board type
055     * @return com port device path
056     */
057    public static String getDefaultPort(SystemInfo.BoardType board) throws UnsupportedBoardType {
058        switch (board){
059            // ------------------------
060            // ALL RASPBERRY PI MODELS
061            // (except Model 3B)
062            // ------------------------
063            case RaspberryPi_A:
064            case RaspberryPi_B_Rev1:
065            case RaspberryPi_B_Rev2:
066            case RaspberryPi_A_Plus:
067            case RaspberryPi_B_Plus:
068            case RaspberryPi_ComputeModule:
069            case RaspberryPi_2B:
070            case RaspberryPi_Zero:
071            case RaspberryPi_ComputeModule3:
072            case RaspberryPi_ZeroW:
073            case RaspberryPi_Alpha:
074            case RaspberryPi_Unknown: {
075                return RaspberryPiSerial.DEFAULT_COM_PORT;
076            }
077
078            // ---------------------------
079            // RASPBERRY PI MODEL 3B, 3B+
080            // ---------------------------
081            case RaspberryPi_3B:
082            case RaspberryPi_3B_Plus: {
083                // if the /dev/ttyS0 port exists, then use it as the default serial port
084                File s0ComPort = new File(RaspberryPiSerial.S0_COM_PORT);
085                if((s0ComPort.exists())){
086                    return RaspberryPiSerial.S0_COM_PORT;
087                }
088                return RaspberryPiSerial.DEFAULT_COM_PORT;
089            }
090
091            // ------------------------
092            // BANANAPI and BANANAPRO
093            // ------------------------
094            case BananaPi: {
095                return BananaPiSerial.DEFAULT_COM_PORT;
096            }
097            case BananaPro: {
098                return BananaProSerial.DEFAULT_COM_PORT;
099            }
100
101            // ------------------------
102            // BPI
103            // ------------------------
104            // TODO : Implement serial for BPI boards
105                        case Bpi_M1:
106                                break;
107                        case Bpi_M1P:
108                                break;
109                        case Bpi_M2:
110                                break;
111                        case Bpi_M2M:
112                                break;
113                        case Bpi_M2P:
114                                break;
115                        case Bpi_M2P_H2_Plus:
116                                break;
117                        case Bpi_M2P_H5:
118                                break;
119                        case Bpi_M2U:
120                                break;
121                        case Bpi_M2U_V40:
122                                break;
123                        case Bpi_M3:
124                                break;
125                        case Bpi_M64:
126                                break;
127                        case Bpi_R1:
128                                break;
129
130            // ------------------------
131            // NANOPI
132            // ------------------------
133            // TODO : Implement serial for NanoPi boards
134                        case NanoPi_A64:
135                                break;
136                        case NanoPi_K2:
137                                break;
138                        case NanoPi_M1:
139                                break;
140                        case NanoPi_M1_Plus:
141                                break;
142                        case NanoPi_M3:
143                                break;
144                        case NanoPi_NEO:
145                                break;
146                        case NanoPi_NEO2:
147                                break;
148                        case NanoPi_NEO2_Plus:
149                                break;
150                        case NanoPi_NEO_Air:
151                                break;
152                        case NanoPi_S2:
153                                break;
154
155            // ------------------------
156            // ODROID
157            // ------------------------
158            // TODO : Implement serial for Odroid boards
159
160                        case Odroid:
161                                break;
162
163            // ------------------------
164            // ORANGEPI
165            // ------------------------
166            // TODO : Implement serial for OrangePi boards
167                        case OrangePi:
168                                break;
169
170            // ------------------------
171            // UNKNOWN
172            // ------------------------
173                        case UNKNOWN:
174                                break;
175                        default:
176                                break;
177        }
178
179        // unknown board type, return null
180        throw new UnsupportedBoardType();
181        }
182}
183