SerialPort.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2009-2011 Cedric Priscal
  3. *
  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. #include <termios.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <string.h>
  22. #include <jni.h>
  23. #include "SerialPort.h"
  24. #include "android/log.h"
  25. static const char *TAG="serial_port";
  26. #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
  27. #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
  28. #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
  29. static speed_t getBaudrate(jint baudrate)
  30. {
  31. switch(baudrate) {
  32. case 0: return B0;
  33. case 50: return B50;
  34. case 75: return B75;
  35. case 110: return B110;
  36. case 134: return B134;
  37. case 150: return B150;
  38. case 200: return B200;
  39. case 300: return B300;
  40. case 600: return B600;
  41. case 1200: return B1200;
  42. case 1800: return B1800;
  43. case 2400: return B2400;
  44. case 4800: return B4800;
  45. case 9600: return B9600;
  46. case 19200: return B19200;
  47. case 38400: return B38400;
  48. case 57600: return B57600;
  49. case 115200: return B115200;
  50. case 230400: return B230400;
  51. case 460800: return B460800;
  52. case 500000: return B500000;
  53. case 576000: return B576000;
  54. case 921600: return B921600;
  55. case 1000000: return B1000000;
  56. case 1152000: return B1152000;
  57. case 1500000: return B1500000;
  58. case 2000000: return B2000000;
  59. case 2500000: return B2500000;
  60. case 3000000: return B3000000;
  61. case 3500000: return B3500000;
  62. case 4000000: return B4000000;
  63. default: return -1;
  64. }
  65. }
  66. /*
  67. * Class: android_serialport_SerialPort
  68. * Method: open
  69. * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
  70. */
  71. JNIEXPORT jobject JNICALL Java_com_android_demo_activity_SerialPort_open
  72. (JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
  73. {
  74. int fd;
  75. speed_t speed;
  76. jobject mFileDescriptor;
  77. /* Check arguments */
  78. {
  79. speed = getBaudrate(baudrate);
  80. if (speed == -1) {
  81. /* TODO: throw an exception */
  82. LOGE("Invalid baudrate");
  83. return NULL;
  84. }
  85. }
  86. /* Opening device */
  87. {
  88. jboolean iscopy;
  89. const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
  90. LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
  91. fd = open(path_utf, O_RDWR | flags);
  92. LOGD("open() fd = %d", fd);
  93. (*env)->ReleaseStringUTFChars(env, path, path_utf);
  94. if (fd == -1)
  95. {
  96. /* Throw an exception */
  97. LOGE("Cannot open port");
  98. /* TODO: throw an exception */
  99. return NULL;
  100. }
  101. }
  102. /* Configure device */
  103. {
  104. struct termios cfg;
  105. LOGD("Configuring serial port");
  106. if (tcgetattr(fd, &cfg))
  107. {
  108. LOGE("tcgetattr() failed");
  109. close(fd);
  110. /* TODO: throw an exception */
  111. return NULL;
  112. }
  113. cfmakeraw(&cfg);
  114. cfsetispeed(&cfg, speed);
  115. cfsetospeed(&cfg, speed);
  116. if (tcsetattr(fd, TCSANOW, &cfg))
  117. {
  118. LOGE("tcsetattr() failed");
  119. close(fd);
  120. /* TODO: throw an exception */
  121. return NULL;
  122. }
  123. }
  124. /* Create a corresponding file descriptor */
  125. {
  126. jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor");
  127. jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()V");
  128. jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I");
  129. mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor);
  130. (*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd);
  131. }
  132. return mFileDescriptor;
  133. }
  134. /*
  135. * Class: cedric_serial_SerialPort
  136. * Method: close
  137. * Signature: ()V
  138. */
  139. JNIEXPORT void JNICALL Java_com_android_demo_activity_SerialPort_close
  140. (JNIEnv *env, jobject thiz)
  141. {
  142. jclass SerialPortClass = (*env)->GetObjectClass(env, thiz);
  143. jclass FileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor");
  144. jfieldID mFdID = (*env)->GetFieldID(env, SerialPortClass, "mFd", "Ljava/io/FileDescriptor;");
  145. jfieldID descriptorID = (*env)->GetFieldID(env, FileDescriptorClass, "descriptor", "I");
  146. jobject mFd = (*env)->GetObjectField(env, thiz, mFdID);
  147. jint descriptor = (*env)->GetIntField(env, mFd, descriptorID);
  148. LOGD("close(fd = %d)", descriptor);
  149. close(descriptor);
  150. }