blob: 35c4d42a0aa86abe5c53926d6ce351a95475e28e [file] [log] [blame]
Mikhail Naganovc276c592016-08-31 18:08:10 -07001/*
Paul McLean8a3e33b2017-03-14 15:20:51 -07002 * Copyright (C) 2018 The Android Open Source Project
Mikhail Naganovc276c592016-08-31 18:08:10 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "NativeMIDI"
18
19#include <poll.h>
20#include <unistd.h>
21
22#include <binder/Binder.h>
Paul McLean8a3e33b2017-03-14 15:20:51 -070023#include <android_util_Binder.h>
Mikhail Naganovc276c592016-08-31 18:08:10 -070024#include <utils/Log.h>
25
Paul McLean8a3e33b2017-03-14 15:20:51 -070026#include <core_jni_helpers.h>
27
Mikhail Naganovc276c592016-08-31 18:08:10 -070028#include "android/media/midi/BpMidiDeviceServer.h"
Marco Nelissen92672802019-10-22 13:44:43 -070029#include "MidiDeviceInfo.h"
Mikhail Naganovc276c592016-08-31 18:08:10 -070030
Paul McLeanf79b8d12019-02-06 13:27:43 -070031#include "include/amidi/AMidi.h"
32#include "amidi_internal.h"
Mikhail Naganovc276c592016-08-31 18:08:10 -070033
Paul McLean8a3e33b2017-03-14 15:20:51 -070034using namespace android::media::midi;
35
Mikhail Naganovc276c592016-08-31 18:08:10 -070036using android::IBinder;
37using android::BBinder;
38using android::OK;
39using android::sp;
40using android::status_t;
41using android::base::unique_fd;
42using android::binder::Status;
Paul McLean71f672b2017-03-05 08:12:18 -070043
44struct AMIDI_Port {
Paul McLean8a3e33b2017-03-14 15:20:51 -070045 std::atomic_int state; // One of the port status constants below.
46 const AMidiDevice *device; // Points to the AMidiDevice associated with the port.
47 sp<IBinder> binderToken;// The Binder token associated with the port.
48 unique_fd ufd; // The unique file descriptor associated with the port.
Paul McLean71f672b2017-03-05 08:12:18 -070049};
Mikhail Naganovc276c592016-08-31 18:08:10 -070050
Paul McLean8a3e33b2017-03-14 15:20:51 -070051/*
52 * Port Status Constants
53 */
Paul McLean71f672b2017-03-05 08:12:18 -070054enum {
55 MIDI_PORT_STATE_CLOSED = 0,
56 MIDI_PORT_STATE_OPEN_IDLE,
57 MIDI_PORT_STATE_OPEN_ACTIVE
58};
59
Paul McLean8a3e33b2017-03-14 15:20:51 -070060/*
61 * Port Type Constants
62 */
Paul McLean71f672b2017-03-05 08:12:18 -070063enum {
64 PORTTYPE_OUTPUT = 0,
65 PORTTYPE_INPUT = 1
66};
67
Mikhail Naganovc276c592016-08-31 18:08:10 -070068/* TRANSFER PACKET FORMAT (as defined in MidiPortImpl.java)
69 *
70 * Transfer packet format is as follows (see MidiOutputPort.mThread.run() to see decomposition):
71 * |oc|md|md| ......... |md|ts|ts|ts|ts|ts|ts|ts|ts|
72 * ^ +--------------------+-----------------------+
73 * | ^ ^
74 * | | |
75 * | | + timestamp (8 bytes)
76 * | |
77 * | + MIDI data bytes (numBytes bytes)
78 * |
79 * + OpCode (AMIDI_OPCODE_DATA)
80 *
81 * NOTE: The socket pair is configured to use SOCK_SEQPACKET mode.
82 * SOCK_SEQPACKET, for a sequenced-packet socket that is connection-oriented, preserves message
83 * boundaries, and delivers messages in the order that they were sent.
84 * So 'read()' always returns a whole message.
85 */
Paul McLean8a3e33b2017-03-14 15:20:51 -070086#define AMIDI_PACKET_SIZE 1024
87#define AMIDI_PACKET_OVERHEAD 9
88#define AMIDI_BUFFER_SIZE (AMIDI_PACKET_SIZE - AMIDI_PACKET_OVERHEAD)
89
Paul McLean84b491302018-05-09 10:03:57 -060090// JNI IDs (see android_media_midi.cpp)
91namespace android { namespace midi {
Paul McLean8a3e33b2017-03-14 15:20:51 -070092// MidiDevice Fields
Paul McLean84b491302018-05-09 10:03:57 -060093extern jfieldID gFidMidiNativeHandle; // MidiDevice.mNativeHandle
94extern jfieldID gFidMidiDeviceServerBinder; // MidiDevice.mDeviceServerBinder
95extern jfieldID gFidMidiDeviceInfo; // MidiDevice.mDeviceInfo
Paul McLean8a3e33b2017-03-14 15:20:51 -070096
97// MidiDeviceInfo Fields
Paul McLean84b491302018-05-09 10:03:57 -060098extern jfieldID mFidMidiDeviceId; // MidiDeviceInfo.mId
99}}
100using namespace android::midi;
Paul McLean8a3e33b2017-03-14 15:20:51 -0700101
102static std::mutex openMutex; // Ensure that the device can be connected just once to 1 thread
103
Paul McLean8a3e33b2017-03-14 15:20:51 -0700104//// Handy debugging function.
105//static void AMIDI_logBuffer(const uint8_t *data, size_t numBytes) {
106// for (size_t index = 0; index < numBytes; index++) {
107// ALOGI(" data @%zu [0x%X]", index, data[index]);
108// }
109//}
Mikhail Naganovc276c592016-08-31 18:08:10 -0700110
Paul McLean71f672b2017-03-05 08:12:18 -0700111/*
112 * Device Functions
113 */
Paul McLean8a3e33b2017-03-14 15:20:51 -0700114/**
115 * Retrieves information for the native MIDI device.
116 *
117 * device The Native API token for the device. This value is obtained from the
118 * AMidiDevice_fromJava().
119 * outDeviceInfoPtr Receives the associated device info.
120 *
121 * Returns AMEDIA_OK or a negative error code.
122 * - AMEDIA_ERROR_INVALID_PARAMETER
123 * AMEDIA_ERROR_UNKNOWN
124 */
Elliott Hughes4797e712019-07-22 16:39:52 -0700125static media_status_t AMIDI_getDeviceInfo(const AMidiDevice *device,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700126 AMidiDeviceInfo *outDeviceInfoPtr) {
127 if (device == nullptr) {
128 return AMEDIA_ERROR_INVALID_PARAMETER;
129 }
130
Mikhail Naganovc276c592016-08-31 18:08:10 -0700131 MidiDeviceInfo deviceInfo;
Paul McLean71f672b2017-03-05 08:12:18 -0700132 Status txResult = device->server->getDeviceInfo(&deviceInfo);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700133 if (!txResult.isOk()) {
134 ALOGE("AMIDI_getDeviceInfo transaction error: %d", txResult.transactionError());
Paul McLean8a3e33b2017-03-14 15:20:51 -0700135 return AMEDIA_ERROR_UNKNOWN;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700136 }
137
Paul McLean8a3e33b2017-03-14 15:20:51 -0700138 outDeviceInfoPtr->type = deviceInfo.getType();
139 outDeviceInfoPtr->inputPortCount = deviceInfo.getInputPortNames().size();
140 outDeviceInfoPtr->outputPortCount = deviceInfo.getOutputPortNames().size();
Paul McLean71f672b2017-03-05 08:12:18 -0700141
Paul McLean8a3e33b2017-03-14 15:20:51 -0700142 return AMEDIA_OK;
143}
144
Paul McLean5090a6d2019-08-05 12:30:56 -0600145media_status_t AMIDI_API AMidiDevice_fromJava(JNIEnv *env, jobject j_midiDeviceObj,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700146 AMidiDevice** devicePtrPtr)
147{
Paul McLean8a3e33b2017-03-14 15:20:51 -0700148 if (j_midiDeviceObj == nullptr) {
149 ALOGE("AMidiDevice_fromJava() invalid MidiDevice object.");
150 return AMEDIA_ERROR_INVALID_OBJECT;
151 }
152
153 {
154 std::lock_guard<std::mutex> guard(openMutex);
155
Paul McLean84b491302018-05-09 10:03:57 -0600156 long handle = env->GetLongField(j_midiDeviceObj, gFidMidiNativeHandle);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700157 if (handle != 0) {
158 // Already opened by someone.
159 return AMEDIA_ERROR_INVALID_OBJECT;
160 }
161
Paul McLean84b491302018-05-09 10:03:57 -0600162 jobject serverBinderObj = env->GetObjectField(j_midiDeviceObj, gFidMidiDeviceServerBinder);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700163 sp<IBinder> serverBinder = android::ibinderForJavaObject(env, serverBinderObj);
164 if (serverBinder.get() == nullptr) {
165 ALOGE("AMidiDevice_fromJava couldn't connect to native MIDI server.");
166 return AMEDIA_ERROR_UNKNOWN;
167 }
168
169 // don't check allocation failures, just abort..
170 AMidiDevice* devicePtr = new AMidiDevice;
171 devicePtr->server = new BpMidiDeviceServer(serverBinder);
Paul McLean84b491302018-05-09 10:03:57 -0600172 jobject midiDeviceInfoObj = env->GetObjectField(j_midiDeviceObj, gFidMidiDeviceInfo);
173 devicePtr->deviceId = env->GetIntField(midiDeviceInfoObj, mFidMidiDeviceId);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700174
175 // Synchronize with the associated Java MidiDevice.
Paul McLean84b491302018-05-09 10:03:57 -0600176 env->SetLongField(j_midiDeviceObj, gFidMidiNativeHandle, (long)devicePtr);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700177 env->GetJavaVM(&devicePtr->javaVM);
178 devicePtr->midiDeviceObj = env->NewGlobalRef(j_midiDeviceObj);
179
180 if (AMIDI_getDeviceInfo(devicePtr, &devicePtr->deviceInfo) != AMEDIA_OK) {
181 // This is weird, but maybe not fatal?
182 ALOGE("AMidiDevice_fromJava couldn't retrieve attributes of native device.");
183 }
184
185 *devicePtrPtr = devicePtr;
186 }
187
188 return AMEDIA_OK;
189}
190
Paul McLean5090a6d2019-08-05 12:30:56 -0600191media_status_t AMIDI_API AMidiDevice_release(const AMidiDevice *device)
Paul McLean8a3e33b2017-03-14 15:20:51 -0700192{
193 if (device == nullptr || device->midiDeviceObj == nullptr) {
194 return AMEDIA_ERROR_INVALID_PARAMETER;
195 }
196
197 JNIEnv* env;
198 jint err = device->javaVM->GetEnv((void**)&env, JNI_VERSION_1_6);
199 LOG_ALWAYS_FATAL_IF(err != JNI_OK, "AMidiDevice_release Error accessing JNIEnv err:%d", err);
200
201 // Synchronize with the associated Java MidiDevice.
Paul McLean8a3e33b2017-03-14 15:20:51 -0700202 {
203 std::lock_guard<std::mutex> guard(openMutex);
Paul McLean84b491302018-05-09 10:03:57 -0600204 long handle = env->GetLongField(device->midiDeviceObj, gFidMidiNativeHandle);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700205 if (handle == 0) {
206 // Not opened as native.
207 ALOGE("AMidiDevice_release() device not opened in native client.");
208 return AMEDIA_ERROR_INVALID_OBJECT;
209 }
210
Paul McLean84b491302018-05-09 10:03:57 -0600211 env->SetLongField(device->midiDeviceObj, gFidMidiNativeHandle, 0L);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700212 }
213 env->DeleteGlobalRef(device->midiDeviceObj);
214
215 delete device;
216
217 return AMEDIA_OK;
218}
219
Paul McLean5090a6d2019-08-05 12:30:56 -0600220int32_t AMIDI_API AMidiDevice_getType(const AMidiDevice *device) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700221 if (device == nullptr) {
222 return AMEDIA_ERROR_INVALID_PARAMETER;
223 }
224 return device->deviceInfo.type;
225}
226
Paul McLean5090a6d2019-08-05 12:30:56 -0600227ssize_t AMIDI_API AMidiDevice_getNumInputPorts(const AMidiDevice *device) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700228 if (device == nullptr) {
229 return AMEDIA_ERROR_INVALID_PARAMETER;
230 }
231 return device->deviceInfo.inputPortCount;
232}
233
Paul McLean5090a6d2019-08-05 12:30:56 -0600234ssize_t AMIDI_API AMidiDevice_getNumOutputPorts(const AMidiDevice *device) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700235 if (device == nullptr) {
236 return AMEDIA_ERROR_INVALID_PARAMETER;
237 }
238 return device->deviceInfo.outputPortCount;
Paul McLean71f672b2017-03-05 08:12:18 -0700239}
240
241/*
242 * Port Helpers
243 */
Paul McLean8a3e33b2017-03-14 15:20:51 -0700244static media_status_t AMIDI_openPort(const AMidiDevice *device, int32_t portNumber, int type,
Paul McLean71f672b2017-03-05 08:12:18 -0700245 AMIDI_Port **portPtr) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700246 if (device == nullptr) {
247 return AMEDIA_ERROR_INVALID_PARAMETER;
248 }
249
Paul McLean71f672b2017-03-05 08:12:18 -0700250 sp<BBinder> portToken(new BBinder());
251 unique_fd ufd;
252 Status txResult = type == PORTTYPE_OUTPUT
253 ? device->server->openOutputPort(portToken, portNumber, &ufd)
254 : device->server->openInputPort(portToken, portNumber, &ufd);
255 if (!txResult.isOk()) {
256 ALOGE("AMIDI_openPort transaction error: %d", txResult.transactionError());
Paul McLean8a3e33b2017-03-14 15:20:51 -0700257 return AMEDIA_ERROR_UNKNOWN;
Paul McLean71f672b2017-03-05 08:12:18 -0700258 }
259
Paul McLean8a3e33b2017-03-14 15:20:51 -0700260 AMIDI_Port *port = new AMIDI_Port;
Paul McLean71f672b2017-03-05 08:12:18 -0700261 port->state = MIDI_PORT_STATE_OPEN_IDLE;
262 port->device = device;
263 port->binderToken = portToken;
264 port->ufd = std::move(ufd);
265
266 *portPtr = port;
267
Paul McLean8a3e33b2017-03-14 15:20:51 -0700268 return AMEDIA_OK;
Paul McLean71f672b2017-03-05 08:12:18 -0700269}
270
Paul McLean8a3e33b2017-03-14 15:20:51 -0700271static void AMIDI_closePort(AMIDI_Port *port) {
272 if (port == nullptr) {
273 return;
274 }
275
Paul McLean71f672b2017-03-05 08:12:18 -0700276 int portState = MIDI_PORT_STATE_OPEN_IDLE;
277 while (!port->state.compare_exchange_weak(portState, MIDI_PORT_STATE_CLOSED)) {
278 if (portState == MIDI_PORT_STATE_CLOSED) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700279 return; // Already closed
Paul McLean71f672b2017-03-05 08:12:18 -0700280 }
281 }
282
283 Status txResult = port->device->server->closePort(port->binderToken);
284 if (!txResult.isOk()) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700285 ALOGE("Transaction error closing MIDI port:%d", txResult.transactionError());
Paul McLean71f672b2017-03-05 08:12:18 -0700286 }
287
288 delete port;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700289}
290
291/*
292 * Output (receiving) API
293 */
Paul McLean5090a6d2019-08-05 12:30:56 -0600294media_status_t AMIDI_API AMidiOutputPort_open(const AMidiDevice *device, int32_t portNumber,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700295 AMidiOutputPort **outOutputPortPtr) {
296 return AMIDI_openPort(device, portNumber, PORTTYPE_OUTPUT, (AMIDI_Port**)outOutputPortPtr);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700297}
298
Paul McLean8a3e33b2017-03-14 15:20:51 -0700299/*
300 * A little RAII (https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization)
301 * class to ensure that the port state is correct irrespective of errors.
302 */
303class MidiReceiver {
304public:
305 MidiReceiver(AMIDI_Port *port) : mPort(port) {}
306
307 ~MidiReceiver() {
308 // flag the port state to idle
309 mPort->state.store(MIDI_PORT_STATE_OPEN_IDLE);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700310 }
311
Paul McLean8a3e33b2017-03-14 15:20:51 -0700312 ssize_t receive(int32_t *opcodePtr, uint8_t *buffer, size_t maxBytes,
313 size_t *numBytesReceivedPtr, int64_t *timestampPtr) {
314 int portState = MIDI_PORT_STATE_OPEN_IDLE;
315 // check to see if the port is idle, then set to active
316 if (!mPort->state.compare_exchange_strong(portState, MIDI_PORT_STATE_OPEN_ACTIVE)) {
317 // The port not idle or has been closed.
318 return AMEDIA_ERROR_UNKNOWN;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700319 }
320
Paul McLean8a3e33b2017-03-14 15:20:51 -0700321 struct pollfd checkFds[1] = { { mPort->ufd, POLLIN, 0 } };
322 if (poll(checkFds, 1, 0) < 1) {
323 // Nothing there
324 return 0;
325 }
326
Mikhail Naganovc276c592016-08-31 18:08:10 -0700327 uint8_t readBuffer[AMIDI_PACKET_SIZE];
Paul McLean8a3e33b2017-03-14 15:20:51 -0700328 ssize_t readCount = read(mPort->ufd, readBuffer, sizeof(readBuffer));
329 if (readCount == EINTR || readCount < 1) {
330 return AMEDIA_ERROR_UNKNOWN;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700331 }
332
Paul McLean8a3e33b2017-03-14 15:20:51 -0700333 // see Packet Format definition at the top of this file.
334 size_t numMessageBytes = 0;
335 *opcodePtr = readBuffer[0];
336 if (*opcodePtr == AMIDI_OPCODE_DATA && readCount >= AMIDI_PACKET_OVERHEAD) {
337 numMessageBytes = readCount - AMIDI_PACKET_OVERHEAD;
338 numMessageBytes = std::min(maxBytes, numMessageBytes);
339 memcpy(buffer, readBuffer + 1, numMessageBytes);
340 if (timestampPtr != nullptr) {
Mikhail Naganov25f51c62018-11-07 09:26:30 -0800341 memcpy(timestampPtr, readBuffer + readCount - sizeof(uint64_t),
342 sizeof(*timestampPtr));
Mikhail Naganovc276c592016-08-31 18:08:10 -0700343 }
Mikhail Naganovc276c592016-08-31 18:08:10 -0700344 }
Paul McLean8a3e33b2017-03-14 15:20:51 -0700345 *numBytesReceivedPtr = numMessageBytes;
346 return 1;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700347 }
348
Paul McLean8a3e33b2017-03-14 15:20:51 -0700349private:
350 AMIDI_Port *mPort;
351};
Paul McLean71f672b2017-03-05 08:12:18 -0700352
Paul McLean5090a6d2019-08-05 12:30:56 -0600353ssize_t AMIDI_API AMidiOutputPort_receive(const AMidiOutputPort *outputPort, int32_t *opcodePtr,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700354 uint8_t *buffer, size_t maxBytes, size_t* numBytesReceivedPtr, int64_t *timestampPtr) {
355
356 if (outputPort == nullptr || buffer == nullptr) {
357 return -EINVAL;
358 }
359
360 return MidiReceiver((AMIDI_Port*)outputPort).receive(opcodePtr, buffer, maxBytes,
361 numBytesReceivedPtr, timestampPtr);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700362}
363
Paul McLean5090a6d2019-08-05 12:30:56 -0600364void AMIDI_API AMidiOutputPort_close(const AMidiOutputPort *outputPort) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700365 AMIDI_closePort((AMIDI_Port*)outputPort);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700366}
367
368/*
369 * Input (sending) API
370 */
Paul McLean5090a6d2019-08-05 12:30:56 -0600371media_status_t AMIDI_API AMidiInputPort_open(const AMidiDevice *device, int32_t portNumber,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700372 AMidiInputPort **outInputPortPtr) {
373 return AMIDI_openPort(device, portNumber, PORTTYPE_INPUT, (AMIDI_Port**)outInputPortPtr);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700374}
375
Paul McLean5090a6d2019-08-05 12:30:56 -0600376void AMIDI_API AMidiInputPort_close(const AMidiInputPort *inputPort) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700377 AMIDI_closePort((AMIDI_Port*)inputPort);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700378}
379
Paul McLean71f672b2017-03-05 08:12:18 -0700380static ssize_t AMIDI_makeSendBuffer(
Paul McLean8a3e33b2017-03-14 15:20:51 -0700381 uint8_t *buffer, const uint8_t *data, size_t numBytes, uint64_t timestamp) {
382 // Error checking will happen in the caller since this isn't an API function.
Mikhail Naganovc276c592016-08-31 18:08:10 -0700383 buffer[0] = AMIDI_OPCODE_DATA;
384 memcpy(buffer + 1, data, numBytes);
385 memcpy(buffer + 1 + numBytes, &timestamp, sizeof(timestamp));
386 return numBytes + AMIDI_PACKET_OVERHEAD;
387}
388
Paul McLean5090a6d2019-08-05 12:30:56 -0600389ssize_t AMIDI_API AMidiInputPort_send(const AMidiInputPort *inputPort, const uint8_t *buffer,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700390 size_t numBytes) {
391 return AMidiInputPort_sendWithTimestamp(inputPort, buffer, numBytes, 0);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700392}
393
Paul McLean5090a6d2019-08-05 12:30:56 -0600394ssize_t AMIDI_API AMidiInputPort_sendWithTimestamp(const AMidiInputPort *inputPort,
Paul McLean8a3e33b2017-03-14 15:20:51 -0700395 const uint8_t *data, size_t numBytes, int64_t timestamp) {
396 if (inputPort == nullptr || data == nullptr) {
397 return AMEDIA_ERROR_INVALID_PARAMETER;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700398 }
399
400 // AMIDI_logBuffer(data, numBytes);
401
Paul McLean8a3e33b2017-03-14 15:20:51 -0700402 uint8_t writeBuffer[AMIDI_BUFFER_SIZE + AMIDI_PACKET_OVERHEAD];
403 size_t numSent = 0;
404 while (numSent < numBytes) {
405 size_t blockSize = AMIDI_BUFFER_SIZE;
406 blockSize = std::min(blockSize, numBytes - numSent);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700407
Paul McLean8a3e33b2017-03-14 15:20:51 -0700408 ssize_t numTransferBytes =
409 AMIDI_makeSendBuffer(writeBuffer, data + numSent, blockSize, timestamp);
410 ssize_t numWritten = write(((AMIDI_Port*)inputPort)->ufd, writeBuffer, numTransferBytes);
411 if (numWritten < 0) {
412 break; // error so bail out.
413 }
414 if (numWritten < numTransferBytes) {
415 ALOGE("AMidiInputPort_sendWithTimestamp Couldn't write MIDI data buffer."
416 " requested:%zu, written%zu",numTransferBytes, numWritten);
417 break; // bail
418 }
419
420 numSent += numWritten - AMIDI_PACKET_OVERHEAD;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700421 }
422
Paul McLean8a3e33b2017-03-14 15:20:51 -0700423 return numSent;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700424}
425
Paul McLean5090a6d2019-08-05 12:30:56 -0600426media_status_t AMIDI_API AMidiInputPort_sendFlush(const AMidiInputPort *inputPort) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700427 if (inputPort == nullptr) {
428 return AMEDIA_ERROR_INVALID_PARAMETER;
429 }
430
Mikhail Naganovc276c592016-08-31 18:08:10 -0700431 uint8_t opCode = AMIDI_OPCODE_FLUSH;
432 ssize_t numTransferBytes = 1;
Paul McLean71f672b2017-03-05 08:12:18 -0700433 ssize_t numWritten = write(((AMIDI_Port*)inputPort)->ufd, &opCode, numTransferBytes);
Mikhail Naganovc276c592016-08-31 18:08:10 -0700434
435 if (numWritten < numTransferBytes) {
Paul McLean8a3e33b2017-03-14 15:20:51 -0700436 ALOGE("AMidiInputPort_flush Couldn't write MIDI flush. requested:%zd, written:%zd",
Mikhail Naganovc276c592016-08-31 18:08:10 -0700437 numTransferBytes, numWritten);
Paul McLean8a3e33b2017-03-14 15:20:51 -0700438 return AMEDIA_ERROR_UNSUPPORTED;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700439 }
440
Paul McLean8a3e33b2017-03-14 15:20:51 -0700441 return AMEDIA_OK;
Mikhail Naganovc276c592016-08-31 18:08:10 -0700442}
443