001package com.pi4j.io.i2c;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  I2CFactory.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 java.io.IOException;
033import java.nio.file.Files;
034import java.nio.file.Path;
035import java.nio.file.Paths;
036import java.util.HashSet;
037import java.util.Set;
038import java.util.concurrent.TimeUnit;
039
040import com.pi4j.io.i2c.impl.I2CProviderImpl;
041
042/**
043 * I2C factory - it returns instances of {@link I2CBus} interface.
044 *
045 * @author Robert Savage (<a href="http://www.savagehomeautomation.com">http://www .savagehomeautomation.com</a>)
046 */
047public class I2CFactory {
048
049    public static final long DEFAULT_LOCKAQUIRE_TIMEOUT = 1000;
050
051    public static final TimeUnit DEFAULT_LOCKAQUIRE_TIMEOUT_UNITS = TimeUnit.MILLISECONDS;
052
053    public static class UnsupportedBusNumberException extends Exception {
054        private static final long serialVersionUID = 1L;
055
056        public UnsupportedBusNumberException() {
057            super();
058        }
059    }
060
061    volatile static I2CFactoryProvider provider = new I2CProviderImpl();
062
063    // private constructor
064    private I2CFactory() {
065        // forbid object construction
066    }
067
068    /**
069     * Create new I2CBus instance.
070     * <p>
071     * The timeout for locking the bus for exclusive communication is set to DEFAULT_LOCKAQUIRE_TIMEOUT.
072     *
073     * @param busNumber The bus number
074     * @return Return a new I2CBus instance
075     * @throws UnsupportedBusNumberException If the given bus-number is not supported by the underlying system
076     * @throws IOException If communication to i2c-bus fails
077     * @see I2CFactory#DEFAULT_LOCKAQUIRE_TIMEOUT
078     * @see I2CFactory#DEFAULT_LOCKAQUIRE_TIMEOUT_UNITS
079     */
080    public static I2CBus getInstance(int busNumber) throws UnsupportedBusNumberException, IOException {
081        return provider.getBus(busNumber, DEFAULT_LOCKAQUIRE_TIMEOUT, DEFAULT_LOCKAQUIRE_TIMEOUT_UNITS);
082    }
083
084    /**
085     * Create new I2CBus instance.
086     *
087     * @param busNumber The bus number
088     * @param lockAquireTimeout The timeout for locking the bus for exclusive communication
089     * @param lockAquireTimeoutUnit The units of lockAquireTimeout
090     * @return Return a new I2CBus instance
091     * @throws UnsupportedBusNumberException If the given bus-number is not supported by the underlying system
092     * @throws IOException If communication to i2c-bus fails
093     */
094    public static I2CBus getInstance(int busNumber, long lockAquireTimeout, TimeUnit lockAquireTimeoutUnit) throws UnsupportedBusNumberException, IOException {
095        return provider.getBus(busNumber, lockAquireTimeout, lockAquireTimeoutUnit);
096    }
097
098    /**
099     * allow changing the provider for the factory
100     *
101     * @param factoryProvider
102     */
103    public static void setFactory(I2CFactoryProvider factoryProvider) {
104        provider = factoryProvider;
105    }
106
107    /**
108     * Fetch all available I2C bus numbers from sysfs.
109     * Returns null, if nothing was found.
110     *
111     * @return Return found I2C bus numbers or null
112     * @throws IOException If fetching from sysfs interface fails
113     */
114    public static int[] getBusIds() throws IOException {
115        Set<Integer> set = null;
116        for (Path device: Files.newDirectoryStream(Paths.get("/sys/bus/i2c/devices"), "*")) {
117            String[] tokens = device.toString().split("-");
118            if (tokens.length == 2) {
119                if (set == null) {
120                    set = new HashSet<Integer>();
121                }
122                set.add(Integer.valueOf(tokens[1]));
123            }
124        }
125
126        int[] result = null;
127        if (set != null) {
128            int counter = 0;
129            result = new int[set.size()];
130            for (Integer value : set) {
131                result[counter] = value.intValue();
132                counter = counter + 1;
133            }
134        }
135        return result;
136    }
137}