|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Copyright 2019 Arduino LLC (http://www.arduino.cc/) |
| 5 | + * |
| 6 | + * Arduino is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + * |
| 20 | + * As a special exception, you may use this file as part of a free software |
| 21 | + * library without restriction. Specifically, if other files instantiate |
| 22 | + * templates or use macros or inline functions from this file, or you compile |
| 23 | + * this file and link it with other files to produce an executable, this |
| 24 | + * file does not by itself cause the resulting executable to be covered by |
| 25 | + * the GNU General Public License. This exception does not however |
| 26 | + * invalidate any other reasons why the executable file might be covered by |
| 27 | + * the GNU General Public License. |
| 28 | + */ |
| 29 | + |
| 30 | +package cc.arduino.cli; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Iterator; |
| 35 | + |
| 36 | +import cc.arduino.cli.commands.ArduinoCoreGrpc; |
| 37 | +import cc.arduino.cli.commands.ArduinoCoreGrpc.ArduinoCoreBlockingStub; |
| 38 | +import cc.arduino.cli.commands.Commands.Configuration; |
| 39 | +import cc.arduino.cli.commands.Commands.InitReq; |
| 40 | +import cc.arduino.cli.commands.Commands.InitResp; |
| 41 | +import cc.arduino.cli.commands.Common.Instance; |
| 42 | +import io.grpc.ManagedChannel; |
| 43 | +import io.grpc.ManagedChannelBuilder; |
| 44 | +import processing.app.BaseNoGui; |
| 45 | +import processing.app.debug.MessageSiphon; |
| 46 | +import processing.app.helpers.ProcessUtils; |
| 47 | + |
| 48 | +public class ArduinoCore { |
| 49 | + |
| 50 | + private Process cliProcess; |
| 51 | + private ArduinoCoreBlockingStub blocking; |
| 52 | + // private ArduinoCoreStub async; |
| 53 | + |
| 54 | + public ArduinoCore() throws IOException { |
| 55 | + String cliPath = BaseNoGui.getContentFile("arduino-cli").getAbsolutePath(); |
| 56 | + cliProcess = ProcessUtils.exec(new String[] { cliPath, "daemon" }); |
| 57 | + new MessageSiphon(cliProcess.getInputStream(), (msg) -> { |
| 58 | + System.out.println("CLI> " + msg); |
| 59 | + }); |
| 60 | + new MessageSiphon(cliProcess.getErrorStream(), (msg) -> { |
| 61 | + System.err.println("CLI> " + msg); |
| 62 | + }); |
| 63 | + |
| 64 | + // TODO: Do a better job managing the arduino-cli process |
| 65 | + try { |
| 66 | + Thread.sleep(1000); |
| 67 | + } catch (InterruptedException e) { |
| 68 | + } |
| 69 | + if (!cliProcess.isAlive()) { |
| 70 | + int res; |
| 71 | + try { |
| 72 | + res = cliProcess.waitFor(); |
| 73 | + throw new IOException( |
| 74 | + "Arduino server terminated with return code " + res); |
| 75 | + } catch (InterruptedException e) { |
| 76 | + } |
| 77 | + throw new IOException("Arduino server terminated"); |
| 78 | + } |
| 79 | + |
| 80 | + ManagedChannel channel = ManagedChannelBuilder // |
| 81 | + .forAddress("127.0.0.1", 50051) // |
| 82 | + .usePlaintext() // |
| 83 | + .build(); |
| 84 | + |
| 85 | + blocking = ArduinoCoreGrpc.newBlockingStub(channel); |
| 86 | + // async = ArduinoCoreGrpc.newStub(channel); |
| 87 | + } |
| 88 | + |
| 89 | + public ArduinoCoreInstance init(File dataDir, File sketchbookDir) { |
| 90 | + InitReq req = InitReq.newBuilder() |
| 91 | + .setConfiguration(Configuration.newBuilder() // |
| 92 | + .setDataDir(dataDir.getAbsolutePath()) // |
| 93 | + .setSketchbookDir(sketchbookDir.getAbsolutePath())) |
| 94 | + .build(); |
| 95 | + Iterator<InitResp> resp = blocking.init(req); |
| 96 | + Instance instance = null; |
| 97 | + while (resp.hasNext()) { |
| 98 | + InitResp r = resp.next(); |
| 99 | + if (r.hasTaskProgress()) { |
| 100 | + System.out.println(r.getTaskProgress()); |
| 101 | + } |
| 102 | + if (r.hasDownloadProgress()) { |
| 103 | + System.out.println(r.getDownloadProgress()); |
| 104 | + } |
| 105 | + if (r.getInstance() != null) { |
| 106 | + if (!r.getLibrariesIndexError().isEmpty()) { |
| 107 | + System.err.println(r.getLibrariesIndexError()); |
| 108 | + } |
| 109 | + r.getPlatformsIndexErrorsList().forEach(System.err::println); |
| 110 | + instance = r.getInstance(); |
| 111 | + } |
| 112 | + } |
| 113 | + return new ArduinoCoreInstance(instance, blocking); |
| 114 | + } |
| 115 | + |
| 116 | + public static void main(String[] args) { |
| 117 | + try { |
| 118 | + ArduinoCore core = new ArduinoCore(); |
| 119 | + ArduinoCoreInstance instance = core.init(new File("/mnt/ramdisk"), |
| 120 | + new File("/mnt/ramdisk/sketchbook")); |
| 121 | + instance.boardDetails("arduino:samd:mkr1000"); |
| 122 | + instance.compile("arduino:samd:mkr1000", |
| 123 | + "/home/megabug/Arduino/alloc_check"); |
| 124 | + } catch (Throwable e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + System.exit(0); |
| 128 | + } |
| 129 | +} |
0 commit comments