| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 const serial = chrome.serial; | 5 const serial = chrome.serial; |
| 6 | 6 |
| 7 // TODO(miket): opening Bluetooth ports on OSX is unreliable. Investigate. | 7 // TODO(miket): opening Bluetooth ports on OSX is unreliable. Investigate. |
| 8 function shouldSkipPort(portName) { | 8 function shouldSkipPort(portName) { |
| 9 return portName.match(/[Bb]luetooth/); | 9 return portName.match(/[Bb]luetooth/); |
| 10 } | 10 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 var bytesToReceive = bufferLength; | 32 var bytesToReceive = bufferLength; |
| 33 | 33 |
| 34 var operation = 0; | 34 var operation = 0; |
| 35 var doNextOperation = function() { | 35 var doNextOperation = function() { |
| 36 switch (operation++) { | 36 switch (operation++) { |
| 37 case 0: | 37 case 0: |
| 38 serial.getDevices(onGetDevices); | 38 serial.getDevices(onGetDevices); |
| 39 break; | 39 break; |
| 40 case 1: | 40 case 1: |
| 41 var bitrate = 57600; | 41 var bitrate = 57600; |
| 42 console.log('Opening serial device ' + serialPort + ' at ' + | 42 console.log('Connecting to serial device ' + serialPort + ' at ' + |
| 43 bitrate + ' bps.'); | 43 bitrate + ' bps.'); |
| 44 serial.open(serialPort, {bitrate: bitrate}, onOpen); | 44 serial.connect(serialPort, {bitrate: bitrate}, onConnect); |
| 45 break; | 45 break; |
| 46 case 2: | 46 case 2: |
| 47 serial.setControlSignals(connectionId, {dtr: true}, onSetControlSignals); | 47 serial.setControlSignals(connectionId, {dtr: true}, onSetControlSignals); |
| 48 break; | 48 break; |
| 49 case 3: | 49 case 3: |
| 50 serial.getControlSignals(connectionId,onGetControlSignals); | 50 serial.getControlSignals(connectionId,onGetControlSignals); |
| 51 break; | 51 break; |
| 52 case 4: | 52 case 4: |
| 53 serial.onReceive.addListener(onReceive); | 53 serial.onReceive.addListener(onReceive); |
| 54 serial.onReceiveError.addListener(onReceiveError); | 54 serial.onReceiveError.addListener(onReceiveError); |
| 55 serial.send(connectionId, sendBuffer, onSend); | 55 serial.send(connectionId, sendBuffer, onSend); |
| 56 break; | 56 break; |
| 57 case 50: // GOTO 4 EVER | 57 case 50: // GOTO 4 EVER |
| 58 serial.close(connectionId, onClose); | 58 serial.disconnect(connectionId, onDisconnect); |
| 59 break; | 59 break; |
| 60 default: | 60 default: |
| 61 // Beware! If you forget to assign a case for your next test, the whole | 61 // Beware! If you forget to assign a case for your next test, the whole |
| 62 // test suite will appear to succeed! | 62 // test suite will appear to succeed! |
| 63 chrome.test.succeed(); | 63 chrome.test.succeed(); |
| 64 break; | 64 break; |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 | 67 |
| 68 var skipToTearDown = function() { | 68 var skipToTearDown = function() { |
| 69 operation = 50; | 69 operation = 50; |
| 70 doNextOperation(); | 70 doNextOperation(); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 var repeatOperation = function() { | 73 var repeatOperation = function() { |
| 74 operation--; | 74 operation--; |
| 75 doNextOperation(); | 75 doNextOperation(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 var onClose = function(result) { | 78 var onDisconnect = function(result) { |
| 79 chrome.test.assertTrue(result); | 79 chrome.test.assertTrue(result); |
| 80 doNextOperation(); | 80 doNextOperation(); |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 var onReceive = function(receiveInfo) { | 83 var onReceive = function(receiveInfo) { |
| 84 var data = new Uint8Array(receiveInfo.data); | 84 var data = new Uint8Array(receiveInfo.data); |
| 85 bytesToReceive -= data.length; | 85 bytesToReceive -= data.length; |
| 86 var receiveBufferIndex = bufferLength - data.length; | 86 var receiveBufferIndex = bufferLength - data.length; |
| 87 for (var i = 0; i < data.length; i++) | 87 for (var i = 0; i < data.length; i++) |
| 88 receiveBufferUint8View[i + receiveBufferIndex] = data[i]; | 88 receiveBufferUint8View[i + receiveBufferIndex] = data[i]; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 chrome.test.assertTrue(typeof options.dtr != 'undefined', "No DTR set"); | 110 chrome.test.assertTrue(typeof options.dtr != 'undefined', "No DTR set"); |
| 111 chrome.test.assertTrue(typeof options.ri != 'undefined', "No RI set"); | 111 chrome.test.assertTrue(typeof options.ri != 'undefined', "No RI set"); |
| 112 doNextOperation(); | 112 doNextOperation(); |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 var onSetControlSignals = function(result) { | 115 var onSetControlSignals = function(result) { |
| 116 chrome.test.assertTrue(result); | 116 chrome.test.assertTrue(result); |
| 117 doNextOperation(); | 117 doNextOperation(); |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 var onOpen = function(connectionInfo) { | 120 var onConnect = function(connectionInfo) { |
| 121 chrome.test.assertTrue(!!connectionInfo, |
| 122 'Failed to connect to serial port.'); |
| 121 connectionId = connectionInfo.connectionId; | 123 connectionId = connectionInfo.connectionId; |
| 122 chrome.test.assertTrue(connectionId > 0, 'Failed to open serial port.'); | |
| 123 doNextOperation(); | 124 doNextOperation(); |
| 124 }; | 125 }; |
| 125 | 126 |
| 126 var onGetDevices = function(devices) { | 127 var onGetDevices = function(devices) { |
| 127 if (devices.length > 0) { | 128 if (devices.length > 0) { |
| 128 var portNumber = 0; | 129 var portNumber = 0; |
| 129 while (portNumber < devices.length) { | 130 while (portNumber < devices.length) { |
| 130 if (shouldSkipPort(devices[portNumber].path)) { | 131 if (shouldSkipPort(devices[portNumber].path)) { |
| 131 portNumber++; | 132 portNumber++; |
| 132 continue; | 133 continue; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 144 // No serial device found. This is still considered a success because we | 145 // No serial device found. This is still considered a success because we |
| 145 // can't rely on specific hardware being present on the machine. | 146 // can't rely on specific hardware being present on the machine. |
| 146 chrome.test.succeed(); | 147 chrome.test.succeed(); |
| 147 } | 148 } |
| 148 }; | 149 }; |
| 149 | 150 |
| 150 doNextOperation(); | 151 doNextOperation(); |
| 151 }; | 152 }; |
| 152 | 153 |
| 153 chrome.test.runTests([testSerial]); | 154 chrome.test.runTests([testSerial]); |
| OLD | NEW |